#!/usr/bin/env python3 """Compile the production batch-chest header against stubs of the original UseItem gift-box contract.""" from pathlib import Path import subprocess import tempfile ROOT=Path(__file__).resolve().parents[2] SRC=ROOT/'src/server/game/src' cmd=(SRC/'cmd.cpp').read_bytes().decode('latin1') assert 'ACMD(do_port_chest);' in cmd and '"mt_chest", do_port_chest, 0, POS_DEAD, GM_PLAYER' in cmd assert '#include "port_chest.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;using WORD=unsigned short;using INT=int; enum {ITEM_USE=3,ITEM_GIFTBOX=23,INVENTORY=1,DRAGON_SOUL_INVENTORY=3,INVENTORY_MAX_NUM=10,DRAGON_SOUL_INVENTORY_MAX_NUM=4}; enum {CHAT_TYPE_INFO=1,CHAT_TYPE_COMMAND=5}; static DWORD clockms=1000;DWORD get_dword_time(){return clockms;} struct TItemPos{BYTE window_type;WORD cell;TItemPos(BYTE w,WORD c):window_type(w),cell(c){}}; struct CItem{DWORD vnum;int count;BYTE type;bool exchanging=false;WORD cell=0; DWORD GetVnum(){return vnum;}int GetCount(){return count;}BYTE GetType(){return type;}bool IsExchanging(){return exchanging;}WORD GetCell(){return cell;}}; using LPITEM=CItem*; struct CSpecialItemGroup{ enum EGiveType{NONE,GOLD,EXP,MOB,SLOW,DRAIN_HP,POISON,MOB_GROUP}; std::vector vnums;bool IsEmpty()const{return vnums.empty();} bool Contains(DWORD v)const{for(DWORD x:vnums)if(x==v)return true;return false;} }; static std::map groups; struct ITEM_MANAGER{static ITEM_MANAGER& instance(){static ITEM_MANAGER m;return m;} const CSpecialItemGroup* GetSpecialItemGroup(DWORD v){auto it=groups.find(v);return it==groups.end()?nullptr:&it->second;}}; struct CHARACTER{ bool m_portQuietInfo=false;std::string m_portLastInfo; LPITEM bag[INVENTORY_MAX_NUM]={};LPITEM ds[DRAGON_SOUL_INVENTORY_MAX_NUM]={};std::vector owned; int gold=0;bool dead=false,shop=false,refuse=false;int uses=0;std::vector messages,info; bool IsPC(){return true;}void* GetDesc(){return this;}DWORD GetPlayerID(){return 7;} bool IsDead(){return dead;}bool IsStun()const{return false;}bool IsWarping()const{return false;}bool IsObserverMode(){return false;} bool CanHandleItem(){return true;}void* GetExchange(){return nullptr;}void* GetMyShop(){return shop?this:nullptr;} void* GetShopOwner(){return nullptr;}bool IsOpenSafebox(){return false;}bool IsCubeOpen(){return false;} INT GetGold()const{return gold;} LPITEM GetInventoryItem(WORD c)const{return c=size?0:-1;} int CountSpecifyItem(DWORD v)const{int n=0;for(auto* i:bag)if(i&&i->vnum==v)n+=i->count;return n;} LPITEM Put(DWORD v,int n,BYTE t=ITEM_USE,int at=-1){ for(int c=0;ccell=c;bag[c]=i;owned.push_back(i);return i;} return nullptr;} 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 gift-box contract: one box consumed, 100 gold + one 27001 (stacking) + a dragon soul for box 50100, // one INFO line per reward; the DS-quest style refusal returns false with an INFO line and consumes nothing. bool UseItem(TItemPos p){ ++uses;LPITEM box=GetItem(p);assert(box && p.window_type==INVENTORY && m_portQuietInfo); if(refuse){ChatPacket(CHAT_TYPE_INFO,"finish the dragon soul quest first");return false;} gold+=100;ChatPacket(CHAT_TYPE_INFO,"gold 100"); if(--box->count==0)bag[p.cell]=nullptr; bool stacked=false;for(auto* i:bag)if(i&&i->vnum==27001){++i->count;stacked=true;break;} if(!stacked)Put(27001,1); if(box->vnum==50100)for(auto*& d:ds)if(!d){d=new CItem{110000,1,ITEM_USE};owned.push_back(d);break;} ChatPacket(CHAT_TYPE_INFO,"got potion"); return true; } }; using LPCHARACTER=CHARACTER*; #define ACMD(name) void name(LPCHARACTER ch,const char* argument) #include "port_chest.h" static CHARACTER* ch; static void cmd(const char* text){clockms+=600;ch->messages.clear();ch->info.clear();ch->uses=0;do_port_chest(ch,text);} static const std::string& last(){return ch->messages.back();} static void fresh(){ch=new CHARACTER;} int main(){ groups[50100].vnums={27001,1,110000};groups[50200].vnums={27001,CSpecialItemGroup::MOB};groups[50300].vnums={}; // Non-stacking boxes spread over cells: chosen cell first, then the others; gains summed, INFO held back. fresh();ch->Put(50100,1,ITEM_GIFTBOX,0);ch->Put(50100,1,ITEM_GIFTBOX,4);ch->Put(50100,1,ITEM_GIFTBOX,5); cmd("1 open 4 3"); assert(ch->messages.size()==3 && ch->messages[0]=="MTCH1 got 27001 3" && ch->messages[1]=="MTCH1 got 110000 3"); assert(last()=="MTCH1 done 50100 3 3 done 300" && ch->info.empty() && !ch->m_portQuietInfo && ch->CountSpecifyItem(50100)==0); // Stacked box, fewer asked than held; then more asked than held -> empty. fresh();ch->Put(50100,5,ITEM_GIFTBOX);cmd("1 open 0 2");assert(last()=="MTCH1 done 50100 2 2 done 200" && ch->bag[0]->count==3); cmd("1 open 0 200");assert(last()=="MTCH1 done 50100 200 3 empty 300" && ch->uses==3); // Single boxes free their own cell for the reward, so free space never shrinks. fresh();for(int i=0;i<7;++i)ch->Put(50100,1,ITEM_GIFTBOX); cmd("1 open 0 7");assert(last()=="MTCH1 done 50100 7 7 done 700"); // A stack + 6 fillers leaves exactly 3 free cells: the first reward takes one, then it stops at 2 free. fresh();ch->Put(50100,7,ITEM_GIFTBOX);for(int i=0;i<6;++i)ch->Put(1,1); cmd("1 open 0 7");assert(last()=="MTCH1 done 50100 7 1 full 100" && ch->uses==1); cmd("1 open 0 1");assert(last()=="MTCH1 error full" && ch->uses==0); // Refusal by the original path: stops, forwards its INFO once. fresh();ch->Put(50100,3,ITEM_GIFTBOX);ch->refuse=true;cmd("1 open 0 3"); assert(last()=="MTCH1 done 50100 3 0 rejected 0" && ch->info.size()==1 && ch->info[0]=="finish the dragon soul quest first"); // Kinds: not a gift box, monster group box, no/empty group. fresh();ch->Put(27001,5);cmd("1 open 0 1");assert(last()=="MTCH1 error kind"); ch->Put(50200,2,ITEM_GIFTBOX);cmd("1 open 1 1");assert(last()=="MTCH1 error kind"); ch->Put(50300,2,ITEM_GIFTBOX);cmd("1 open 2 1");assert(last()=="MTCH1 error kind"); ch->Put(50400,2,ITEM_GIFTBOX);cmd("1 open 3 1");assert(last()=="MTCH1 error kind" && ch->uses==0); // Item, exchange, busy, validation, throttle. cmd("1 open 9 1");assert(last()=="MTCH1 error item"); LPITEM box=ch->Put(50100,2,ITEM_GIFTBOX);box->exchanging=true;cmd("1 open 4 1");assert(last()=="MTCH1 error item");box->exchanging=false; ch->shop=true;cmd("1 open 4 1");assert(last()=="MTCH1 error busy");ch->shop=false; for(auto bad:{"1 open 4 0","1 open 4 201","1 open 10 1","1 open 4","1 open x 1","1 open 4 1 1","1 close 4 1","1 open 4 999999"}){cmd(bad);assert(last()=="MTCH1 error invalid");} cmd("2 open 4 1");assert(ch->messages.empty()); cmd("1 open 4 1");assert(last()=="MTCH1 done 50100 1 1 done 100"); do_port_chest(ch,"1 open 4 1");assert(last()=="MTCH1 error rate" && ch->CountSpecifyItem(50100)==1); cmd("1 open 4 1");assert(last()=="MTCH1 done 50100 1 1 done 100"); puts("PASS: cross-cell order, stacks, gains summary, free-cell stop, refusal, kinds, item/busy/validation/throttle"); } ''' with tempfile.TemporaryDirectory(prefix='mt-chest-') 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)