Files
WoW-Hybrid-gateway/tests/native_pet_command_test.cpp

147 lines
13 KiB
C++

// Synthetic: native pet commands (TC PetPackets Read) -> Tortoise PetHandler requests, only for the
// shown Source pet bar unit, within Source command/reaction/spell sets.
#include "world_translation.hpp"
#include <cstring>
#include <iostream>
using namespace gateway;
static void expect(bool ok,const char* what){if(!ok)throw Error(what);}
template<class F>static void rejects(F f,const char* what){bool r=false;try{f();}catch(const Error&){r=true;}expect(r,what);}
static Guid128 g(uint64_t lo,uint64_t hi){Guid128 x{};Bytes b;le(b,lo,8);le(b,hi,8);std::copy(b.begin(),b.end(),x.begin());return x;}
static Bytes cast(uint32_t spell,uint32_t flags,const Guid128& target){ // native SpellCastRequest (as native_gameobject_cast_test)
Bytes b;Guid128 id{};id[0]=1;append(b,pack_guid(id));le(b,0,1);for(int i=0;i<3;++i)le(b,0,4);
le(b,spell,4);le(b,0,4);le(b,0,4);le(b,flags,4);append(b,pack_guid(target));
Guid128 zero{};append(b,pack_guid(zero));append(b,pack_guid(zero));le(b,0,2);
le(b,0,4);le(b,0,4);append(b,pack_guid(zero));for(int i=0;i<3;++i)le(b,0,4);
le(b,0,1);le(b,0,1);return b;
}
static uint32_t f(float v){uint32_t u;std::memcpy(&u,&v,4);return u;}
int main(){try{
NativeWorldTranslation w{};w.realm=70;w.selected.guid=4509;w.selected.race=3;w.selected.cls=3;w.selected.name="Hunter";
w.self_created=true;w.location=WorldLocation{0,{0,0,0,0}};w.virtual_realm_address=0x10046;
const uint64_t pet=0xF140000005000202ull,mob=0xF130000000000303ull;const auto pet_m=g(0x202,0x2000000000000002ull),mob_m=g(0x303,0x2000000000000003ull);
NativeEntityTranslation::Actor a;a.kind=3;a.modern=pet_m;a.sent=true;a.generation=1;w.entities.actors[pet]=a;a.modern=mob_m;w.entities.actors[mob]=a;
PetSpellbook book{};book.unit=pet;book.buttons={0x07000002,0x07000001,0x07000000,0xc1000011,0x81000022,0,0,0x06000002,0x06000001,0x06000000};
book.actions={0xc1000011,0x81000022,0x01000033};NativeWorldTranslation::PetBar bar{book,70,0};bar.shown=true;bar.generation=1;w.pet_bar=bar;
w.entities.actors.at(pet).eligible=true;{auto& c=w.entities.self_controlled[1];c.words={uint32_t(pet),uint32_t(pet>>32)};c.known=3;c.presented=pet_m;}
auto pg=[&](Guid128 x){return pack_guid(x);};
auto action=[&](uint32_t act,const Bytes& target){Bytes b=pg(pet_m);le(b,act,4);append(b,target);for(int i=0;i<3;++i)le(b,f(0),4);return b;};
// Attack the mob: 0x175 {pet, data, target}.
{auto r=w.gameplay(0x3f0021,action(0x07000002,pg(mob_m)));Reader q(r.body);expect(r.opcode==0x175 && q.le(8)==pet && q.le(4)==0x07000002 && q.le(8)==mob,"attack");q.end();}
{auto r=w.gameplay(0x3f0021,action(0x07000001,pg(Guid128{})));Reader q(r.body);q.le(8);q.le(4);expect(q.le(8)==0,"follow without target");}
{auto r=w.gameplay(0x3f0021,action(0xc1000011,pg(mob_m)));expect(r.opcode==0x175,"known pet spell");}
rejects([&]{w.gameplay(0x3f0021,action(0x07000004,pg(Guid128{})));},"MOVE_TO has no Source form");
rejects([&]{w.gameplay(0x3f0021,action(0x06000003,pg(Guid128{})));},"ASSIST has no Source form");
rejects([&]{w.gameplay(0x3f0021,action(0xc1000099,pg(mob_m)));},"unknown pet spell");
{Bytes b=pg(mob_m);le(b,0x07000002,4);append(b,pg(Guid128{}));for(int i=0;i<3;++i)le(b,0,4);rejects([&]{w.gameplay(0x3f0021,b);},"not the pet bar unit");}
// Set action: cells 0..9 only.
{Bytes b=pg(pet_m);le(b,3,4);le(b,0x81000022,4);auto r=w.gameplay(0x3f0020,b);Reader q(r.body);expect(r.opcode==0x174 && q.le(8)==pet && q.le(4)==3 && q.le(4)==0x81000022,"set action");q.end();}
{Bytes b=pg(pet_m);le(b,12,4);le(b,0x07000001,4);rejects([&]{w.gameplay(0x3f0020,b);},"supplemental cell");}
{Bytes b=pg(pet_m);le(b,4,4);le(b,0,4);auto r=w.gameplay(0x3f0020,b);Reader q(r.body);q.le(8);expect(q.le(4)==4 && q.le(4)==0,"clear a cell");}
{Bytes b=pg(pet_m);le(b,4,4);le(b,0x81000000,4);expect(w.gameplay(0x3f0020,b).opcode==0x174,"ACT_DISABLED|0 clear");}
{Bytes b=pg(pet_m);le(b,4,4);le(b,0x01000000,4);expect(w.gameplay(0x3f0020,b).opcode==0x174,"ACT_PASSIVE|0 clear");}
{Bytes b=pg(pet_m);le(b,5,4);le(b,0x07000001,4);rejects([&]{w.gameplay(0x3f0020,b);},"command singleton move held");}
{Bytes b=pg(pet_m);le(b,0,4);le(b,0xc1000011,4);rejects([&]{w.gameplay(0x3f0020,b);},"spell onto command cell held");}
{Bytes b=pg(pet_m);le(b,8,4);le(b,0,4);rejects([&]{w.gameplay(0x3f0020,b);},"clearing a reaction cell held");}
{Bytes b=pg(pet_m);le(b,5,4);le(b,0x06000002,4);rejects([&]{w.gameplay(0x3f0020,b);},"reaction singleton move held");}
rejects([&]{w.gameplay(0x3f0021,action(0x00000011,pg(mob_m)));},"type 0 not dispatched by Source");
{auto h=w;h.selected.cls=9;rejects([&]{h.gameplay(0x3f0023,pg(pet_m));},"warlock summoned pet cannot be abandoned");}
// Stop attack, abandon, cancel aura, autocast, info.
{auto r=w.gameplay(0x3f0022,pg(pet_m));expect(r.opcode==0x2ea && r.body.size()==8,"stop attack");}
{auto r=w.gameplay(0x3f0023,pg(pet_m));Reader q(r.body);expect(r.opcode==0x176 && q.le(8)==pet,"abandon");}
{Bytes b=pg(pet_m);le(b,12345,4);auto r=w.gameplay(0x3f0025,b);Reader q(r.body);expect(r.opcode==0x26b && q.le(8)==pet && q.le(4)==12345,"cancel aura");}
{Bytes b=pg(pet_m);le(b,0x11,4);b.push_back(0x80);auto r=w.gameplay(0x3f0026,b);Reader q(r.body);q.le(8);expect(r.opcode==0x2f3 && q.le(4)==0x11 && q.le(1)==1,"autocast on");}
{Bytes b=pg(pet_m);le(b,0x11,4);b.push_back(0);auto r=w.gameplay(0x3f0026,b);Reader q(r.body);q.le(8);q.le(4);expect(q.le(1)==0,"autocast off");}
{Bytes b=pg(pet_m);le(b,0x99,4);b.push_back(0x80);rejects([&]{w.gameplay(0x3f0026,b);},"autocast unknown");}
expect(w.gameplay(0x3f0027,{}).opcode==0x279,"pet info");
// Pet cast at the mob: 0x1F0 {pet, spell, targets UNIT}.
{Bytes b=pg(pet_m);append(b,cast(0x22,2,mob_m));auto r=w.gameplay(0x3e0175,b);Reader q(r.body);
expect(r.opcode==0x1f0 && q.le(8)==pet && q.le(4)==0x22 && q.le(2)==2,"pet cast");auto t=unpack_guid(q,8);Reader tg(t);expect(tg.le(8)==mob,"cast target");q.end();}
{Bytes b=pg(pet_m);append(b,cast(0x77,2,mob_m));rejects([&]{w.gameplay(0x3e0175,b);},"pet cast unknown spell");}
// Rename: PetNumber must be the Source pet number (5); declined names have no Source form.
{Bytes b=pg(pet_m);le(b,5,4);b.push_back(4);b.push_back(0);for(char c:std::string("Fang"))b.push_back(uint8_t(c));
auto r=w.gameplay(0x4400b0,b);Reader q(r.body);expect(r.opcode==0x177 && q.le(8)==pet && q.cstring(64)=="Fang","rename");q.end();
// 0x4400B2 is SET_SAVED_INSTANCE_EXTEND (WPP 69876); Tortoise has no lock extension, so it is never a rename.
{bool kept=false;try{kept=w.gameplay(0x4400b2,b).opcode!=0x177;}catch(const Error&){kept=true;}expect(kept,"0x4400b2 is not pet rename");}
// Source rejects with an empty PET_NAME_INVALID: native result echoes the sent pet/number/name.
NativeWorldTranslation x=w; // translate also sweeps the pet bar; keep the fixture for the later checks
x.gameplay_sent(0x4400b0,b);auto bundle=x.translate({0x178,{}},0x10046,0,nullptr);
Reader z(bundle);auto n=z.le(4);bool seen=false;
for(uint64_t i=0;i<n;++i){auto op=z.le(4);auto sz=z.le(4);auto body=z.take(sz);if(op!=0x470170)continue;seen=true;
Bytes e;le(e,1,4);append(e,pack_guid(Bytes(16,0)));le(e,0,4);e.push_back(4);e.push_back(0);for(char c:std::string("Fang"))e.push_back(uint8_t(c));
expect(Bytes(body.begin(),body.end())==e,"pet name invalid body");}
expect(seen && x.pet_rename_sent.empty(),"pet name invalid once");
// Two outstanding attempts: the first failure cannot be attributed to a name (a silent success may precede it).
x.gameplay_sent(0x4400b0,b);x.gameplay_sent(0x4400b0,b);auto two=x.translate({0x178,{}},0x10046,0,nullptr);
{Reader y(two);auto m=y.le(4);for(uint64_t i=0;i<m;++i){auto op=y.le(4);auto sz=y.le(4);auto body=y.take(sz);
if(op==0x470170){Reader v(body);v.le(4);v.take(2);v.le(4);expect(v.le(1)==0,"ambiguous: no name");}}}
x.pet_rename_sent.clear();
// A late failure for an earlier pet is not given the current pet's name.
{NativeWorldTranslation o=w;o.gameplay_sent(0x4400b0,b);o.pet_rename_sent.front().pet^=1; // attempt for another pet
auto late=o.translate({0x178,{}},0x10046,0,nullptr);Reader y(late);auto m=y.le(4);bool got=false;
for(uint64_t i=0;i<m;++i){auto op=y.le(4);auto sz=y.le(4);auto body=y.take(sz);if(op==0x470170){got=true;Reader v(body);v.le(4);v.take(2);v.le(4);expect(v.le(1)==0,"old pet: no name");}}
expect(got && o.pet_rename_sent.empty(),"old pet failure consumed");}
// Overflow: nine attempts; while the dropped one is outstanding, failures are generic (no uncertain name).
{NativeWorldTranslation o=w;for(int i=0;i<9;++i)o.gameplay_sent(0x4400b0,b);
expect(o.pet_rename_dropped==1 && o.pet_rename_sent.size()==8,"overflow counted");
auto first=o.translate({0x178,{}},0x10046,0,nullptr);Reader y(first);auto m=y.le(4);bool got=false;
for(uint64_t i=0;i<m;++i){auto op=y.le(4);auto sz=y.le(4);auto body=y.take(sz);if(op==0x470170){got=true;Reader v(body);v.le(4);v.take(2);v.le(4);expect(v.le(1)==0,"dropped answered first: generic");}}
expect(got && o.pet_rename_dropped==0 && o.pet_rename_sent.size()==8,"one failure consumed the dropped attempt");}
auto again=x.translate({0x178,{}},0x10046,0,nullptr);expect(again.empty() || Reader(again).le(4)==0,"unsolicited ignored");}
{Bytes b=pg(pet_m);le(b,6,4);b.push_back(4);b.push_back(0);for(char c:std::string("Fang"))b.push_back(uint8_t(c));rejects([&]{w.gameplay(0x4400b0,b);},"wrong number");}
{Bytes b=pg(pet_m);le(b,5,4);b.push_back(4);b.push_back(0x80);for(char c:std::string("Fang"))b.push_back(uint8_t(c));rejects([&]{w.gameplay(0x4400b0,b);},"declined");}
// Packed list words: 0x01000033 is spell 0x33 (passive); a truncated 16-bit view must not match.
{Bytes b=pg(pet_m);le(b,0x33,4);b.push_back(0);expect(w.gameplay(0x3f0026,b).opcode==0x2f3,"24-bit spell id");}
// Control lost (summon field cleared) or a stale generation: commands reject even with the cached bar.
{auto h=w;h.entities.self_controlled[1].known=0;rejects([&]{h.gameplay(0x3f0022,pg(pet_m));},"control lost");}
{auto h=w;h.entities.actors.at(pet).generation=2;rejects([&]{h.gameplay(0x3f0022,pg(pet_m));},"stale generation");}
{auto h=w;h.pending_teleport=NearTeleport{};rejects([&]{h.gameplay(0x3f0022,pg(pet_m));},"near teleport");}
// Q57 diagnostics: exact fixed tags, preserving first-predicate precedence.
unsigned reason_checks=0;
auto reason=[&](NativeWorldTranslation h,const Guid128& id,const char* prefix,const char* tag){
const std::string expected=std::string(prefix)+" [reason="+tag+"]";
std::string actual;auto body=pg(id);Reader reader(body);
try{h.current_pet_command_unit(reader);}catch(const Error& e){actual=e.what();}
if(actual!=expected)throw Error("pet diagnostic expected "+expected+" got "+actual);++reason_checks;
};
const char* bar_prefix="pet command outside the current Source pet bar";
const char* control_prefix="pet command unit is no longer under current Source control";
for(unsigned i=0;i<6;++i){auto h=w;
// Also fail later predicates so this tests order, not only isolated conditions.
if(i<=0)h.self_created=false;
if(i<=1)h.pending_teleport=NearTeleport{};
if(i<=2)h.far_transfer=NativeWorldTranslation::FarTransfer{};
if(i<=3)h.pet_bar.reset();
else{h.pet_bar->shown=i>4;h.pet_bar->state.unit=mob;}
constexpr const char* tags[]={"self-not-created","pending-teleport","far-transfer","bar-missing","bar-unshown","bar-unit-mismatch"};
reason(h,pet_m,bar_prefix,tags[i]);
}
// bindings() rejects absent/unsent ordinary actors before the control guard.
// Use its existing self binding solely to reach those defensive predicates;
// these deliberately inconsistent states must reject, never emit a command.
for(unsigned i=0;i<7;++i){auto h=w;const auto id=forever_player_guid(h.selected.guid,h.realm);
Guid128 self_id{};std::copy(id.begin(),id.end(),self_id.begin());
h.pet_bar->state.unit=h.selected.guid;
auto actor=h.entities.actors.at(pet);actor.modern=self_id;
h.entities.actors[h.selected.guid]=actor;
auto& control=h.entities.self_controlled[0];control.known=3;
control.words={uint32_t(h.selected.guid),uint32_t(h.selected.guid>>32)};control.presented=self_id;
if(i==0)h.entities.actors.erase(h.selected.guid);
else{
auto& a=h.entities.actors.at(h.selected.guid);
a.sent=i>1;a.eligible=i>2;if(i<=3)++a.generation;
}
if(i<=4)control.known=0;
if(i<=5)control.words={};
control.presented=Guid128{};
constexpr const char* tags[]={"actor-missing","actor-unsent","actor-ineligible","actor-generation-mismatch","control-unknown","control-unit-mismatch","control-presentation-mismatch"};
reason(h,self_id,control_prefix,tags[i]);
}
{auto r=w.gameplay(0x3f0022,pg(pet_m));Bytes expected;le(expected,pet,8);
expect(r.opcode==0x2ea && r.body==expected,"diagnostics preserve valid command");++reason_checks;}
std::cout<<"Q57 fixed pet guard reasons "<<reason_checks<<" PASS\n";
// No shown pet bar: everything rejects.
w.pet_bar->shown=false;rejects([&]{w.gameplay(0x3f0022,pg(pet_m));},"unshown bar");
w.pet_bar.reset();rejects([&]{w.gameplay(0x3f0021,action(0x07000001,pg(Guid128{})));},"no pet");
std::cout<<"PASS native pet commands\n";return 0;
}catch(const std::exception& e){std::cerr<<e.what()<<'\n';return 1;}}