#!/usr/bin/env python3 """Compile the production query/protocol bodies against original group classes and deterministic fixtures.""" from pathlib import Path import re import subprocess import tempfile ROOT=Path(__file__).resolve().parents[2] SRC=ROOT/'src/server/game/src' header=(SRC/'item_manager.h').read_bytes().decode('latin1') groups=header[header.index('class CSpecialAttrGroup'):header.index('class ITEM;')] points=sorted(set(re.findall(r'\bPOINT_[A-Z_]+', (SRC/'port_features.h').read_text()))) code=r''' #include #include #include #include #include #include #include #include using namespace std; using DWORD=unsigned;using BYTE=unsigned char; #define itertype(v) decltype((v).begin()) static int rng=0;int number(int,int){++rng;return 1;} enum {ITEM_POLYMORPH=19,ITEM_GIFTBOX=23,ITEM_TREASURE_BOX=20,CHAR_TYPE_MONSTER=0,CHAR_TYPE_STONE=2,MOB_RANK_MAX_NUM=6,ITEM_NAME_MAX_LEN=24}; POINTS GROUPS #include "port_feature_types.h" #include "item_manager_private_types.h" std::vector g_vec_pkCommonDropItem[MOB_RANK_MAX_NUM]; struct TItemTable{unsigned dwVnum;unsigned bType=0;}; struct MobTable{unsigned bType=0,bRank=0,dwPolymorphItemVnum=0,dwDropItemVnum=0;unsigned char bLevel=0;}; struct CMob{MobTable m_table;}; class CMobManager { public:std::map mobs; static CMobManager& instance(){static CMobManager m;return m;} const CMob* Get(unsigned id){auto it=mobs.find(id);return it==mobs.end()?nullptr:it->second;} auto begin(){return mobs.begin();}auto end(){return mobs.end();} }; class ITEM_MANAGER { public: std::vector tables; std::map m_map_pkDropItemGroup; std::map m_map_pkMobItemGroup; std::map m_map_pkLevelItemGroup; std::map m_map_pkGloveItemGroup; std::map m_map_dwEtcItemDropProb; std::vector m_vec_pkCommonNewItemGroup; std::map special; static ITEM_MANAGER& instance(){static ITEM_MANAGER m;return m;} TItemTable* GetTable(unsigned id){for(auto& t:tables)if(t.dwVnum==id)return &t;return nullptr;} const std::vector& GetTable(){return tables;} const CSpecialItemGroup* GetSpecialItemGroup(unsigned id){auto it=special.find(id);return it==special.end()?nullptr:it->second;} std::vector PortWikiDrops(DWORD race); std::vector PortWikiChest(DWORD id); }; #include "port_wiki_queries.inc" static DWORD clockms=1000;unsigned get_dword_time(){return clockms;}long long get_global_time(){return 1700000000;} struct Character { unsigned m_portFeatureLastQuery=0;bool pc=true;std::vector messages;std::map points; bool IsPC(){return pc;}void* GetDesc(){return this;} int GetPoint(unsigned point){return points[point];} int GetHP(){return 123;}int GetMaxHP(){return 500;}int GetSP(){return 42;}int GetMaxSP(){return 100;}int GetLevel(){return 30;} void ChatPacket(int,const char* format,...) { char out[1024];va_list args;va_start(args,format);vsnprintf(out,sizeof(out),format,args);va_end(args); assert(strlen(out)<512);messages.emplace_back(out); } }; using LPCHARACTER=Character*; #define CHAT_TYPE_COMMAND 5 #define ACMD(name) void name(LPCHARACTER ch,const char* argument) #include static void PortCalendarQuery(LPCHARACTER,unsigned,unsigned){} static bool PortDungeonQuery(LPCHARACTER,unsigned){return true;} static unsigned travelBoss=0;static void PortTrackTravel(LPCHARACTER,unsigned,unsigned boss){travelBoss=boss;} static unsigned bossQueries=0;void PortBossQuery(LPCHARACTER,unsigned request){bossQueries=request;} static unsigned rankQuery=99;static void PortRankQuery(LPCHARACTER,unsigned,unsigned index){rankQuery=index;} #include "port_features.h" #include "port_dungeon_rules.h" static void query(Character& ch,const char* text){clockms+=300;do_port_features(&ch,text);} int main(){ assert(PortLair(100,3600,300,400).phase==1); // inclusive join boundary assert(PortLair(100,3600,300,401).phase==2); assert(PortLair(100,3600,300,3700).phase==2); // original strict-negative availability assert(PortLair(100,3600,300,3701).phase==0); assert(PortLair(0,3600,300,1700000000).phase==0); auto& items=ITEM_MANAGER::instance();auto& mobs=CMobManager::instance(); items.tables={{19,0},{30006,0},{30220,0},{50011,23},{50012,23},{50013,23},{70104,19}}; CMob mob;mob.m_table.dwDropItemVnum=30220;mobs.mobs[101]=&mob; CMob npc;npc.m_table.bType=1;mobs.mobs[9001]=&npc; g_vec_pkCommonDropItem[0].emplace_back(1,30,50,19); g_vec_pkCommonDropItem[0].emplace_back(1,30,50,70104); // Wrong polymorph mob must be excluded. CDropItemGroup drop(0,101,"fixture");drop.AddItem(30006,100,2);items.m_map_pkDropItemGroup[101]=&drop; CMobItemGroup kill(101,50,"fixture");kill.AddItem(19,1,20,0);kill.AddItem(30006,3,80,5);items.m_map_pkMobItemGroup[101]=&kill; CLevelItemGroup level(20);level.AddItem(30220,10,1);items.m_map_pkLevelItemGroup[101]=&level; CBuyerThiefGlovesItemGroup glove(0,101,"fixture");glove.AddItem(19,25,1);items.m_map_pkGloveItemGroup[101]=&glove; items.m_map_dwEtcItemDropProb[30220]=40; auto rows=items.PortWikiDrops(101);assert(rows.size()==7); assert(rows[0].rule==WIKI_COMMON && rows[0].minimum==1 && rows[0].maximum==30); assert(rows[1].item==30006 && rows[1].count==2); assert(rows[2].weight==20 && rows[3].weight==80 && rows[3].total==100 && rows[3].rare==5); assert(rows[4].rule==WIKI_LEVEL && rows[4].minimum==20); assert(rows[5].rule==WIKI_GLOVE && rows[6].rule==WIKI_ETC); assert(items.PortWikiDrops(9001).empty() && items.PortWikiDrops(99999).empty()); drop.AddItem(30220,25,1);assert(items.PortWikiDrops(101).size()==8); drop.AddItem(70104,1,1);assert(items.PortWikiDrops(101).size()==9); // Non-common groups retain configured polymorph candidates. // Sees active changes, not exported text. // common_drop_new: player range clipped to the mob level +-20; groups out of reach are left out mob.m_table.bLevel=10;CCommonDropNew low,high;low.minLevel=1;low.maxLevel=40;low.AddItem(30006,3,1200);high.minLevel=31;high.maxLevel=60;high.AddItem(19,1,1200); items.m_vec_pkCommonNewItemGroup={&low,&high};rows=items.PortWikiDrops(101);assert(rows.size()==10); assert(rows[1].item==30006 && rows[1].count==3 && rows[1].rule==WIKI_COMMON && rows[1].minimum==1 && rows[1].maximum==30 && rows[1].weight==1200); high.minLevel=30;assert(items.PortWikiDrops(101).size()==11 && items.PortWikiDrops(101)[2].minimum==30); assert(low.CanDropThisMap(41));low.m_vecMapIndex={1};assert(low.CanDropThisMap(1) && !low.CanDropThisMap(41)); items.m_vec_pkCommonNewItemGroup.clear();mob.m_table.bLevel=0; CSpecialItemGroup chest(50011);chest.AddItem(19,1,80,0);chest.AddItem(1,500,20,0);items.special[50011]=&chest; CSpecialItemGroup multi(50012,CSpecialItemGroup::PCT);multi.AddItem(19,2,20,0);multi.AddItem(3,101,150,0);items.special[50012]=&multi; CSpecialItemGroup attr(50013,CSpecialItemGroup::SPECIAL);attr.AddItem(19,1,100,0);items.special[50013]=&attr; auto rewards=items.PortWikiChest(50011);assert(rewards.size()==2 && rewards[0].weight==80 && rewards[1].weight==20 && rewards[1].item==1 && rewards[1].count==500); 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 99 travel 2493 0");assert(travelBoss==2493);query(ch,"1 1 cap");assert(ch.messages.back()=="MTF1 cap 1 1 1983 1700000000"); query(ch,"1 90 rank 0 0");assert(rankQuery==0); query(ch,"1 91 rank 10 0");assert(ch.messages.back()=="MTF1 error 91 invalid" && rankQuery==0); query(ch,"1 92 rank 6 1");assert(ch.messages.back()=="MTF1 error 92 invalid" && rankQuery==0); 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"); query(ch,"1 5 chest 50013 0");assert(ch.messages.back()=="MTF1 error 5 unsupported_chest"); query(ch,"1 6 sources 30006 0");assert(ch.messages.back()=="MTF1 end 6 2"); ch.messages.clear();ch.points[POINT_ATTBONUS_STONE]=10;query(ch,"1 7 stats"); bool stone=false,hp=false;for(auto& line:ch.messages){stone|=line.find(" stone 10")!=std::string::npos;hp|=line.find(" hp 123")!=std::string::npos;} assert(stone && hp && ch.messages.size()==sizeof(kPortStats)/sizeof(kPortStats[0])+2); do_port_features(&ch,"1 8 stats");assert(ch.messages.back()=="MTF1 error 8 rate"); query(ch,"1 11 bosses");assert(bossQueries==11);query(ch,"1 12 bosses 1 0");assert(bossQueries==11 && ch.messages.back()=="MTF1 error 12 unsupported"); for(auto bad:{"2 9 stats","1 0 cap","1 -1 cap","1 4294967296 cap","1 10 cap too many extra"}) { auto size=ch.messages.size();query(ch,bad);assert(ch.messages.size()==size); } assert(rng==0); // Neither previews nor reverse lookup advance gameplay RNG. puts("PASS: actual query/protocol bodies; active groups, conditions, weights, special rewards, stats, pagination, validation; RNG untouched"); } ''' # cstring is required by the stub logger, before its declaration. code=code.replace('#include ','#include \n#include ') code=code.replace('POINTS','enum {'+', '.join([points[0]+'=1']+points[1:])+'};').replace('GROUPS',groups) with tempfile.TemporaryDirectory(prefix='mt-feature-query-') as folder: path=Path(folder);(path/'test.cpp').write_text(code) subprocess.run(['c++','-std=c++17','-I'+str(SRC),str(path/'test.cpp'),'-o',str(path/'test')],check=True) subprocess.run([str(path/'test')],check=True)