#!/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)