diff --git a/docs/FEATURE-PICKUP.md b/docs/FEATURE-PICKUP.md new file mode 100644 index 00000000..33fb2858 --- /dev/null +++ b/docs/FEATURE-PICKUP.md @@ -0,0 +1,60 @@ +# PORT batch pickup, v1 + +AVA2-style batch pickup (reference: AVA2 `do_pickup_all`). One command picks up every ground item the character owns within the normal pickup distance, filtered by category. Source: `src/server/game/src/port_pickup.h`, command `mt_pickup` (cmd.cpp), throttle field `m_portPickupLastCommand` in char.h. It reuses the quiet-INFO hook (`m_portQuietInfo`) added for the switchbot. No packet header, no DB schema, no affect. AVA2's `HEADER_CG_ITEMS_PICKUP` packet and its premium automatic pickup (`AFFECT_AUTO_PICK_UP`, item 70002) are not ported. + +## Rules + +- Collection walks `ForEachAround` on the character's sectree. An item is taken only when it: + - is on the ground (has a sectree); + - passes `DistanceValid` (the original 300-unit pickup distance); + - passes `IsOwnership(ch)` (own or ownerless); + - matches the category mask. +- Party loot owned by someone else is excluded. `PickupItem`'s party branch would hand it to the requester, which is what AVA2 also avoids. +- Items are sorted nearest-first and capped at 48 per command. Each one is looked up again by VID and goes through the original `CHARACTER::PickupItem`. Distance, stacking, inventory space, `ItemLog GET` and quest triggers are therefore the original path's. +- Each successful pickup still sends its own INFO line. Of the failures (usually a full inventory), only the first INFO line is sent, once. +- Busy: dead, stunned, observer mode, warping, or no sectree. +- Throttle: one command per 500 ms per character. + +## Protocol (CHAT_TYPE_COMMAND, version 1) + +``` +/mt_pickup 1 MASK MASK = 1..255, at most 3 digits +MTPU1 done FOUND PICKED FAILED +MTPU1 error rate|invalid|busy +``` + +A request with a version other than `1`, a token longer than 10 characters or more than 3 tokens is ignored silently. + +| Bit | Category | Item type | +| --- | --- | --- | +| 1 | yang | `ITEM_ELK` | +| 2 | weapon | `ITEM_WEAPON` | +| 4 | armor / jewellery | `ITEM_ARMOR` | +| 8 | skill book | `ITEM_SKILLBOOK` | +| 16 | stone | `ITEM_METIN` | +| 32 | material | `ITEM_MATERIAL` | +| 64 | costume | `ITEM_COSTUME` | +| 128 | other | every other type | + +Capability: the MTF1 cap mask advertises bit 128 (127 -> 255). The client also falls back to single pickup when it gets no reply within 3 s. + +## Verification + +- `tests/features/test_pickup.py` compiles the production header against stubs of the PickupItem/ChatPacket contract. It covers: + - nearest-first order; + - foreign and far items left on the ground; + - per-category masks; + - the yang/INFO pass-through; + - a full inventory (`done 8 3 5`, one failure line); + - the 48 cap; + - rate, invalid, busy and silently ignored input. +- `tests/features/test_queries.py` was updated for cap mask 255. `test_switchbot.py` still passes. +- FreeBSD build of the whole game target on 203 in `/root/metin2-features-dev`, log `/root/metin2-features-dev/pickup-build.log`. The live `/usr/metin2` binary was not replaced and no live service was restarted. +- Live test on 2026-10-15 against the channel-5 test core (`/usr/metin2/server/channel5/first`, port 13040, isolated binary) with the macOS native client as [SA]Admin. The test created weapon 19, armor 11209, material 30003, stone 28030 and skill book 50300 with `/item`, then dropped each one and 1000 yang: + - mask 2 → `done 1 1 0` (weapon only); + - mask 48 → `done 2 2 0` (stone + material); + - mask 255 → `done 3 3 0` (armor, book, yang; the yang balance was restored); + - nothing left → `done 0 0 0`, and an immediate second command → `error rate`. + - `log.log` has one `GET` row per item, matching the `DROP` rows. The original path logs no GET for yang. + - The core's syserr shows no new errors. The LOCALE_ERROR there comes from the original yang-drop message. +- Not done yet: a live full-inventory run and Android device validation. diff --git a/src/server/game/src/char.h b/src/server/game/src/char.h index c5ccee11..41ae917c 100755 --- a/src/server/game/src/char.h +++ b/src/server/game/src/char.h @@ -521,6 +521,7 @@ class CHARACTER : public CEntity, public CFSM, public CHorseRider DWORD m_portSwitchbotLastCommand = 0; // PORT: switchbot command throttle. bool m_portQuietInfo = false; // PORT: switchbot rolls hold back per-roll INFO lines... std::string m_portLastInfo; // ...keeping the last one to report a rejected roll. + DWORD m_portPickupLastCommand = 0; // PORT: batch pickup throttle. LPCHARACTER FindCharacterInView(const char * name, bool bFindPCOnly); void UpdatePacket(); diff --git a/src/server/game/src/cmd.cpp b/src/server/game/src/cmd.cpp index cada55e2..19a33fbc 100755 --- a/src/server/game/src/cmd.cpp +++ b/src/server/game/src/cmd.cpp @@ -11,6 +11,7 @@ ACMD(do_server_bonuses); ACMD(do_port_biolog); ACMD(do_port_features); ACMD(do_port_switchbot); +ACMD(do_port_pickup); ACMD(do_user_horse_ride); ACMD(do_user_horse_back); ACMD(do_user_horse_feed); @@ -287,6 +288,7 @@ struct command_info cmd_info[] = { "autohunt_pull", do_autohunt_pull, 0, POS_STANDING, GM_PLAYER }, { "mt_features", do_port_features, 0, POS_DEAD, GM_PLAYER }, { "mt_switchbot", do_port_switchbot, 0, POS_DEAD, GM_PLAYER }, + { "mt_pickup", do_port_pickup, 0, POS_DEAD, GM_PLAYER }, { "bio", do_port_biolog, 0, POS_DEAD, GM_PLAYER }, { "biolog", do_port_biolog, 0, POS_DEAD, GM_PLAYER }, { "server_bonuses", do_server_bonuses, 0, POS_DEAD, GM_PLAYER }, diff --git a/src/server/game/src/cmd_general.cpp b/src/server/game/src/cmd_general.cpp index 21c5bf13..169068a1 100755 --- a/src/server/game/src/cmd_general.cpp +++ b/src/server/game/src/cmd_general.cpp @@ -44,6 +44,7 @@ #include "port_calendar_runtime.h" #include "port_features.h" #include "port_switchbot.h" +#include "port_pickup.h" extern int g_server_id; diff --git a/src/server/game/src/port_features.h b/src/server/game/src/port_features.h index c8f09964..0b3c914b 100644 --- a/src/server/game/src/port_features.h +++ b/src/server/game/src/port_features.h @@ -43,7 +43,7 @@ ACMD(do_port_features) { ch->ChatPacket(CHAT_TYPE_COMMAND,"MTF1 error %u rate",request);return; } ch->m_portFeatureLastQuery=get_dword_time(); - if(kind=="cap" && args.size()==3) {ch->ChatPacket(CHAT_TYPE_COMMAND,"MTF1 cap %u 1 127 %lld",request,(long long)get_global_time());return;} + if(kind=="cap" && args.size()==3) {ch->ChatPacket(CHAT_TYPE_COMMAND,"MTF1 cap %u 1 255 %lld",request,(long long)get_global_time());return;} if(kind=="dungeons" && args.size()==3) {PortDungeonQuery(ch,request);return;} if(kind=="stats" && args.size()==3) { ch->ChatPacket(CHAT_TYPE_COMMAND,"MTF1 begin %u stats 0 0 %u 0 %lld",request,unsigned(sizeof(kPortStats)/sizeof(kPortStats[0])),(long long)get_global_time()); diff --git a/src/server/game/src/port_pickup.h b/src/server/game/src/port_pickup.h new file mode 100644 index 00000000..6d125b23 --- /dev/null +++ b/src/server/game/src/port_pickup.h @@ -0,0 +1,77 @@ +#ifndef METIN2_PORT_PICKUP_H +#define METIN2_PORT_PICKUP_H +// PORT: AVA2-style batch pickup (do_pickup_all). One command picks up every ground item this character +// owns within the original pickup distance, filtered by category. Each item goes through the original +// CHARACTER::PickupItem, so distance, stacking, inventory space, ItemLog GET and quest triggers are unchanged. +#include +#include +#include +#include +#include + +static const unsigned kPortPickupMaxItems=48; +static const DWORD kPortPickupGapMs=500; +enum EPortPickupCategory { + PORT_PICKUP_YANG=1,PORT_PICKUP_WEAPON=2,PORT_PICKUP_ARMOR=4,PORT_PICKUP_BOOK=8, + PORT_PICKUP_STONE=16,PORT_PICKUP_MATERIAL=32,PORT_PICKUP_COSTUME=64,PORT_PICKUP_OTHER=128,PORT_PICKUP_ALL=255 +}; + +static unsigned PortPickupCategory(LPITEM item) { + switch(item->GetType()) { + case ITEM_ELK:return PORT_PICKUP_YANG; + case ITEM_WEAPON:return PORT_PICKUP_WEAPON; + case ITEM_ARMOR:return PORT_PICKUP_ARMOR; + case ITEM_SKILLBOOK:return PORT_PICKUP_BOOK; + case ITEM_METIN:return PORT_PICKUP_STONE; + case ITEM_MATERIAL:return PORT_PICKUP_MATERIAL; + case ITEM_COSTUME:return PORT_PICKUP_COSTUME; + default:return PORT_PICKUP_OTHER; + } +} + +// Own (or ownerless) items only, like AVA2: PickupItem's party branch would hand someone else's loot to us. +struct FPortPickupCollect { + LPCHARACTER ch;unsigned mask;std::vector > found; + FPortPickupCollect(LPCHARACTER c,unsigned m):ch(c),mask(m){} + void operator()(LPENTITY ent) { + if(!ent->IsType(ENTITY_ITEM))return; + LPITEM item=(LPITEM)ent; + if(!item->GetSectree() || !item->DistanceValid(ch) || !item->IsOwnership(ch) || !(PortPickupCategory(item)&mask))return; + found.push_back(std::make_pair(DISTANCE_APPROX(item->GetX()-ch->GetX(),item->GetY()-ch->GetY()),item->GetVID())); + } +}; + +ACMD(do_port_pickup) { + if(!ch || !ch->IsPC() || !ch->GetDesc())return; + std::istringstream input(argument);std::vector args;std::string token; + while(input>>token){if(token.size()>10 || args.size()>=3)return;args.push_back(token);} + if(args.empty() || args[0]!="1")return; + if(ch->m_portPickupLastCommand && get_dword_time()-ch->m_portPickupLastCommandChatPacket(CHAT_TYPE_COMMAND,"MTPU1 error rate");return; + } + ch->m_portPickupLastCommand=get_dword_time(); + unsigned mask=0; + if(args.size()!=2 || args[1].empty() || args[1].size()>3 || args[1].find_first_not_of("0123456789")!=std::string::npos || + (mask=unsigned(atoi(args[1].c_str())))<1 || mask>PORT_PICKUP_ALL) { + ch->ChatPacket(CHAT_TYPE_COMMAND,"MTPU1 error invalid");return; + } + if(ch->IsDead() || ch->IsStun() || ch->IsObserverMode() || ch->IsWarping() || !ch->GetSectree()) { + ch->ChatPacket(CHAT_TYPE_COMMAND,"MTPU1 error busy");return; + } + FPortPickupCollect collect(ch,mask); + ch->GetSectree()->ForEachAround(collect); + std::sort(collect.found.begin(),collect.found.end()); + if(collect.found.size()>kPortPickupMaxItems)collect.found.resize(kPortPickupMaxItems); + // Successful pickups keep their own INFO line; a failure (usually a full inventory) is reported once. + unsigned picked=0,failed=0;std::string failure; + for(const auto& entry:collect.found) { + ch->m_portQuietInfo=true;ch->m_portLastInfo.clear(); + const bool ok=ch->PickupItem(entry.second); + ch->m_portQuietInfo=false; + if(ok) {++picked;if(!ch->m_portLastInfo.empty())ch->ChatPacket(CHAT_TYPE_INFO,"%s",ch->m_portLastInfo.c_str());} + else {++failed;if(failure.empty())failure=ch->m_portLastInfo;} + } + if(!failure.empty())ch->ChatPacket(CHAT_TYPE_INFO,"%s",failure.c_str()); + ch->ChatPacket(CHAT_TYPE_COMMAND,"MTPU1 done %u %u %u",unsigned(collect.found.size()),picked,failed); +} +#endif diff --git a/tests/features/test_pickup.py b/tests/features/test_pickup.py new file mode 100644 index 00000000..6c7ebb91 --- /dev/null +++ b/tests/features/test_pickup.py @@ -0,0 +1,106 @@ +#!/usr/bin/env python3 +"""Compile the production batch-pickup header against stubs of the original PickupItem/ChatPacket contract.""" +from pathlib import Path +import subprocess +import tempfile +ROOT=Path(__file__).resolve().parents[2] +SRC=ROOT/'src/server/game/src' +assert '"mt_pickup", do_port_pickup' in (SRC/'cmd.cpp').read_bytes().decode('latin1') +assert '#include "port_pickup.h"' in (SRC/'cmd_general.cpp').read_bytes().decode('latin1') +code=r''' +#include +#include +#include +#include +#include +#include +using DWORD=unsigned;using BYTE=unsigned char; +enum {ITEM_NONE,ITEM_WEAPON,ITEM_ARMOR,ITEM_USE,ITEM_AUTOUSE,ITEM_MATERIAL,ITEM_ELK=9,ITEM_METIN=10,ITEM_SKILLBOOK=17,ITEM_QUEST=18,ITEM_COSTUME=28}; +enum {CHAT_TYPE_INFO=1,CHAT_TYPE_COMMAND=5,ENTITY_ITEM=2}; +#define DISTANCE_APPROX(dx,dy) (abs(dx)>abs(dy)?abs(dx)+abs(dy)/2:abs(dy)+abs(dx)/2) +static DWORD clockms=1000;DWORD get_dword_time(){return clockms;} +struct CHARACTER;struct SECTREE; +struct CEntity{int etype;virtual ~CEntity(){}bool IsType(int t){return etype==t;}}; +using LPENTITY=CEntity*; +struct CItem:CEntity{ + DWORD vid,ownerPid;BYTE type;int x,y;bool ground=true;std::string name; + CItem(DWORD v,BYTE t,int px,int py,DWORD owner,const char* n):vid(v),ownerPid(owner),type(t),x(px),y(py),name(n){etype=ENTITY_ITEM;} + SECTREE* GetSectree();BYTE GetType(){return type;}DWORD GetVID(){return vid;}int GetX(){return x;}int GetY(){return y;} + bool DistanceValid(CHARACTER* ch);bool IsOwnership(CHARACTER* ch); +}; +using LPITEM=CItem*; +struct CMob:CEntity{CMob(){etype=1;}}; +static std::vector world; +struct SECTREE{template void ForEachAround(F& f){std::vector snap=world;for(auto* e:snap)f(e);}}; +static SECTREE tree; +SECTREE* CItem::GetSectree(){return ground?&tree:nullptr;} +struct CHARACTER{ + DWORD m_portPickupLastCommand=0;bool m_portQuietInfo=false;std::string m_portLastInfo; + DWORD pid=7;int x=1000,y=1000,space=100;bool dead=false,stun=false;std::vector messages,info;std::vector order; + bool IsPC(){return true;}void* GetDesc(){return this;}DWORD GetPlayerID(){return pid;}int GetX(){return x;}int GetY(){return y;} + bool IsDead(){return dead;}bool IsStun()const{return stun;}bool IsObserverMode(){return false;}bool IsWarping()const{return false;} + SECTREE* GetSectree(){return &tree;} + void ChatPacket(BYTE type,const char* format,...){ + char out[513];va_list a;va_start(a,format);vsnprintf(out,sizeof(out),format,a);va_end(a); + if(m_portQuietInfo&&type==CHAT_TYPE_INFO){m_portLastInfo=out;return;} + (type==CHAT_TYPE_INFO?info:messages).emplace_back(out); + } + // Original contract: own items only here, INFO on success and on a full inventory. + bool PickupItem(DWORD v){ + LPITEM item=nullptr;for(auto* e:world)if(e->IsType(ENTITY_ITEM)&&((LPITEM)e)->vid==v)item=(LPITEM)e; + if(!item||!item->GetSectree()||!item->DistanceValid(this))return false; + if(item->type!=ITEM_ELK){if(!space){ChatPacket(CHAT_TYPE_INFO,"inventory full");return false;}--space;ChatPacket(CHAT_TYPE_INFO,"got %s",item->name.c_str());} + item->ground=false;order.push_back(v);return true; + } +}; +using LPCHARACTER=CHARACTER*; +bool CItem::DistanceValid(CHARACTER* ch){return DISTANCE_APPROX(x-ch->x,y-ch->y)<=300;} +bool CItem::IsOwnership(CHARACTER* ch){return !ownerPid||ownerPid==ch->pid;} +#define ACMD(name) void name(LPCHARACTER ch,const char* argument) +#include "port_pickup.h" +static CHARACTER ch; +static void cmd(const char* text){clockms+=600;ch.messages.clear();ch.info.clear();ch.order.clear();do_port_pickup(&ch,text);} +static const std::string& last(){return ch.messages.back();} +static void reset(){for(auto* e:world)if(e->IsType(ENTITY_ITEM))((LPITEM)e)->ground=true;} +int main(){ + CItem yang(1,ITEM_ELK,1010,1000,7,"yang"),sword(2,ITEM_WEAPON,1200,1000,7,"sword"),armor(3,ITEM_ARMOR,1100,1000,0,"armor"), + book(4,ITEM_SKILLBOOK,1000,1050,7,"book"),stone(5,ITEM_METIN,1000,1250,7,"stone"),ore(6,ITEM_MATERIAL,1000,1020,7,"ore"), + hat(7,ITEM_COSTUME,1030,1000,7,"hat"),potion(8,ITEM_USE,1000,1030,7,"potion"), + far(9,ITEM_USE,1400,1000,7,"far"),foreign(10,ITEM_USE,1000,1010,8,"foreign"); + CMob mob; + for(CEntity* e:std::vector{&yang,&sword,&armor,&book,&stone,&ore,&hat,&potion,&far,&mob,&foreign})world.push_back(e); + + // All categories: own and ownerless items within 300, nearest first; far and foreign items stay. + cmd("1 255");assert(last()=="MTPU1 done 8 8 0"); + assert((ch.order==std::vector{1,6,7,8,4,3,2,5})); + assert(ch.info.size()==7 && ch.info[0]=="got ore"); // yang gives no INFO; item lines pass through + assert(far.ground && foreign.ground && !ch.m_portQuietInfo); + + // Category filter. + reset();cmd("1 6");assert(last()=="MTPU1 done 2 2 0" && (ch.order==std::vector{3,2})); + reset();cmd("1 128");assert(last()=="MTPU1 done 1 1 0" && ch.order==std::vector{8}); + reset();cmd("1 1");assert(last()=="MTPU1 done 1 1 0" && ch.info.empty()); + + // Full inventory: picks what fits, the failure line is sent once. + reset();ch.space=2;cmd("1 255");assert(last()=="MTPU1 done 8 3 5"); + assert(ch.info.size()==3 && ch.info[2]=="inventory full");ch.space=100; + + // The rest is picked on the next press, then nothing is left around. + cmd("1 255");assert(last()=="MTPU1 done 5 5 0"); + cmd("1 255");assert(last()=="MTPU1 done 0 0 0" && ch.info.empty()); + + // Throttle, validation and busy states. + reset();do_port_pickup(&ch,"1 255");assert(last()=="MTPU1 error rate"); + for(auto bad:{"1","1 0","1 256","1 -1","1 x","1 0255","1 255 1"}){cmd(bad);assert(last()=="MTPU1 error invalid");} + ch.dead=true;cmd("1 255");assert(last()=="MTPU1 error busy");ch.dead=false; + ch.stun=true;cmd("1 255");assert(last()=="MTPU1 error busy");ch.stun=false; + cmd("2 255");assert(ch.messages.empty()); + cmd("1 99999999999");assert(ch.messages.empty()); // overlong token ignored + assert(sword.ground && yang.ground); // nothing picked by rejected commands + puts("PASS: radius, ownership, nearest-first order, categories, full inventory, throttle, validation, busy"); +} +''' +with tempfile.TemporaryDirectory(prefix='mt-pickup-') as folder: + path=Path(folder);(path/'test.cpp').write_text(code) + subprocess.run(['c++','-std=c++17','-Wall','-I'+str(SRC),str(path/'test.cpp'),'-o',str(path/'test')],check=True) + subprocess.run([str(path/'test')],check=True) diff --git a/tests/features/test_queries.py b/tests/features/test_queries.py index b7e9501d..b8ba038f 100644 --- a/tests/features/test_queries.py +++ b/tests/features/test_queries.py @@ -106,7 +106,7 @@ int main(){ assert(rewards[0].total==100 && rewards[0].rule==WIKI_SINGLE); rewards=items.PortWikiChest(50012);assert(rewards[1].rule==WIKI_INDEPENDENT && rewards[1].weight==150 && rewards[1].total==100); assert(items.PortWikiChest(50013).empty() && items.PortWikiChest(19).empty()); - Character ch;query(ch,"1 1 cap");assert(ch.messages.back()=="MTF1 cap 1 1 127 1700000000"); + Character ch;query(ch,"1 1 cap");assert(ch.messages.back()=="MTF1 cap 1 1 255 1700000000"); ch.messages.clear();query(ch,"1 2 mob 101 0");assert(ch.messages.size()==10 && ch.messages.back()=="MTF1 end 2 8"); query(ch,"1 3 mob 101 999");assert(ch.messages.back()=="MTF1 error 3 offset"); query(ch,"1 4 mob 9001 0");assert(ch.messages.back()=="MTF1 error 4 missing");