"""Execute actual Lua weighted dictionaries and companion selection.""" from pathlib import Path import subprocess,tempfile root=Path(__file__).resolve().parents[2] s=(root/'src/server/game/World/ClientlessLuaEngine.cpp').read_text() def fn(sig): a=s.index(sig);b=s.index('{',a);e=b+1;n=1 while n:n+=(s[e]=='{')-(s[e]=='}');e+=1 return s[a:e] code=r''' #include #include #include #include #include #include using uint32=unsigned; struct ClientlessWeightedMount{uint32 DisplayId,Weight;}; struct Profile{std::vectorCompanions;unsigned CompanionSpell=0;}profile; unsigned roll=1;unsigned urand(unsigned low,unsigned high){assert(roll>=low && roll<=high);return roll;} struct ClientlessLuaEngine{bool available=true;Profile const*GetAdventureProfile(unsigned)const{return available?&profile:nullptr;}unsigned SelectCompanionSpell(unsigned)const;}; '''+fn('std::vector ReadMountDictionary(')+fn('uint32 ClientlessLuaEngine::SelectCompanionSpell(')+r''' int main(){ lua_State*L=luaL_newstate();luaL_openlibs(L); assert(luaL_dostring(L,"return { companions = { [10673]=9,[10675]=1,[10676]=0,[10707]=-1,[123]=math.huge,[124]=0/0,['bad']=8,[125]=1e30 } }")==LUA_OK); profile.Companions=ReadMountDictionary(L,-1,"companions");assert(profile.Companions.size()==2); ClientlessLuaEngine engine;std::unordered_mapcounts; for(roll=1;roll<=10;++roll)++counts[engine.SelectCompanionSpell(50)]; assert(counts[10673]==9 && counts[10675]==1 && counts.size()==2); profile.Companions.clear();profile.CompanionSpell=10707;assert(engine.SelectCompanionSpell(50)==10707); engine.available=false;assert(!engine.SelectCompanionSpell(50));lua_close(L); } ''' with tempfile.TemporaryDirectory() as d: p=Path(d);(p/'test.cpp').write_text(code) flags=subprocess.check_output(['pkg-config','--cflags','--libs','lua5.4'],text=True).split() subprocess.run(['g++','-std=c++17',str(p/'test.cpp'),'-o',str(p/'test'),*flags],check=True) subprocess.run([str(p/'test')],check=True) print('Production profile selection: real Lua ID dictionaries, exact 9:1 companion weighting, zero/negative/infinite/NaN/oversized weights rejected and legacy companion fallback passed')