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

417 lines
32 KiB
C++

#include "optional_test_codec.hpp"
#include "world_translation.hpp"
#include <dlfcn.h>
#include "aura_translation.hpp"
#include "gateway/aura_definition_profile.hpp"
#include "gateway/server_cast_identity.hpp"
#include "gateway/remote_speeds.hpp"
#include <fstream>
#include <iterator>
#include <string_view>
#include <zlib.h>
#include "gateway/pet_presentation.hpp"
#include <algorithm>
#include "gateway/turtle_entity_profile.hpp"
#include "serializer.h"
#include <bit>
#include <array>
#include <cstdlib>
#include <iostream>
#include <source_location>
using namespace gateway;
static void expect(bool x,std::source_location l=std::source_location::current()){if(!x)throw Error("companion fixture line "+std::to_string(l.line()));}
static Bytes packed(uint64_t id){Bytes b;le(b,id,8);return pack_guid(b);}
static LegacyPacket create(uint64_t guid,const std::map<uint16_t,uint32_t>& extra={},uint32_t entry=37057,uint32_t spell=37057,uint32_t display=6477){
std::map<uint16_t,uint32_t> f{{3,entry},{4,std::bit_cast<uint32_t>(1.3f)},{12,4509},{14,4509},{22,70},{28,70},{34,1},{35,35},{36,0x1020100},{46,134218496},{129,std::bit_cast<uint32_t>(1.5f)},{130,std::bit_cast<uint32_t>(1.5f)},{131,display},{132,display},{146,spell}};
for(auto [i,v]:extra)f[i]=v;
Bytes b;le(b,1,4);le(b,0,1);le(b,2,1);append(b,packed(guid));le(b,3,1);le(b,0x20,1);le(b,0,4);le(b,100,4);
for(float v:{1.f,2.f,3.f,0.f})le(b,std::bit_cast<uint32_t>(v),4);
le(b,0,4);
for(float v:{2.5f,7.f,4.5f,4.72f,2.5f,3.14f})le(b,std::bit_cast<uint32_t>(v),4);
auto n=f.rbegin()->first/32+1;le(b,n,1);std::vector<uint32_t> mask(n);
for(auto [i,v]:f){(void)v;mask[i/32]|=1u<<(i%32);}
for(auto v:mask)le(b,v,4);
for(auto [i,v]:f){(void)i;le(b,v,4);}return {0xa9,b};
}
static LegacyPacket values(uint64_t guid,const std::map<uint16_t,uint32_t>& f){
Bytes b;le(b,1,4);le(b,0,1);le(b,0,1);append(b,packed(guid));auto n=f.rbegin()->first/32+1;le(b,n,1);std::vector<uint32_t> mask(n);
for(auto [i,v]:f){(void)v;mask[i/32]|=1u<<(i%32);}for(auto v:mask)le(b,v,4);
for(auto [i,v]:f){(void)i;le(b,v,4);}return {0xa9,b};
}
static LegacyPacket definition(uint32_t entry=37057,uint32_t type=8,uint32_t display=6477,const char* name="Swiftstar",uint32_t family=0){
Bytes b;le(b,entry,4);cstring(b,name);
for(unsigned i=0;i<3;++i)cstring(b,"");
cstring(b,"");
for(auto v:{0u,type,family,0u,0u,0u,display})le(b,v,4);
le(b,0,1);le(b,0,1);return {0x61,b};
}
static void pet_movement_boundary(const Bytes& captured_speed={}){
const uint64_t source=(0xf140ULL<<48)|(78ULL<<24)|32;
const auto pet=forever_pet_guid(source,37057,70,0);
auto source_spline=[](uint64_t guid,uint8_t face=0,uint64_t target=0){
Bytes b=packed(guid);for(float v:{1.f,2.f,3.f})le(b,std::bit_cast<uint32_t>(v),4);
le(b,42,4);le(b,face,1);if(face==1)return LegacyPacket{0xdd,b};
if(face==3)le(b,target,8);else if(face==4)le(b,std::bit_cast<uint32_t>(-1.f),4);
le(b,0x100,4);le(b,500,4);le(b,1,4);for(float v:{5.f,6.f,3.f})le(b,std::bit_cast<uint32_t>(v),4);
return LegacyPacket{0xdd,b};
};
auto fail=[](auto action){bool held=false;try{action();}catch(const Error&){held=true;}expect(held);};
const auto move=source_spline(source);const auto parsed=turtle_monster_spline(move.body);
auto output=forever_monster_spline(parsed,pet);Reader id(output);expect(unpack_guid(id,16)==Bytes(pet.begin(),pet.end()));
auto stop=turtle_monster_spline(source_spline(source,1).body);expect(!forever_monster_spline(stop,pet).empty());
auto angle=turtle_monster_spline(source_spline(source,4).body);expect(!forever_monster_spline(angle,pet).empty());
auto facing=turtle_monster_spline(source_spline(source,3,4509).body);
expect(!forever_monster_spline(facing,pet,SplineFacingTarget{forever_player_guid(4509,70),.25f}).empty());
const uint64_t other=(0xf140ULL<<48)|(99ULL<<24)|33;
auto pet_facing=turtle_monster_spline(source_spline(source,3,other).body);
expect(!forever_monster_spline(pet_facing,pet,SplineFacingTarget{forever_pet_guid(other,37071,70,0),.25f}).empty());
fail([&]{forever_monster_spline(pet_facing,pet,SplineFacingTarget{forever_pet_guid(other,37071,71,0),.25f});});
Reader words(pet);words.le(8);const auto high=words.le(8);
auto native=[](uint64_t low,uint64_t hi){Bytes b;le(b,low,8);le(b,hi,8);Guid128 result{};std::copy(b.begin(),b.end(),result.begin());return result;};
std::vector<Guid128> invalid{native(33,high),native(32,high|1),native(32,high|(uint64_t(1)<<55)),native(32,(10ULL<<58)|(70ULL<<42)),native(32,(high&~(63ULL<<58))|(8ULL<<58)),native(32,(high&~(63ULL<<58))|(9ULL<<58))};
for(const auto& bad:invalid)fail([&]{forever_monster_spline(parsed,bad);});
fail([&]{turtle_monster_spline(source_spline(0xf14000004e000000ULL).body);});
fail([&]{turtle_monster_spline(source_spline(0xf11000004e000020ULL).body);});
for(const auto& route:remote_speeds::Routes){
Bytes b=packed(source);le(b,std::bit_cast<uint32_t>(8.f),4);
auto update=remote_speeds::turtle_packet(route.spline,b);expect(update.has_value());
auto packet=remote_speeds::forever_packet(*update,pet,70,0);expect(packet.opcode==route.native_spline);
Reader body(packet.body);expect(unpack_guid(body,16)==Bytes(pet.begin(),pet.end()) && body.f32()==8.f);body.end();
for(const auto& bad:invalid)fail([&]{remote_speeds::forever_packet(*update,bad,70,0);});
fail([&]{remote_speeds::forever_packet(*update,pet,71,0);});fail([&]{remote_speeds::forever_packet(*update,pet,70,1);});
}
const auto& run=remote_speeds::Routes[1];Bytes observer=packed(source);le(observer,0,4);le(observer,123,4);
for(float v:{1.f,2.f,3.f,0.f})le(observer,std::bit_cast<uint32_t>(v),4);
le(observer,0,4);le(observer,std::bit_cast<uint32_t>(8.f),4);
auto speed=remote_speeds::turtle_packet(run.observer,observer);expect(speed && speed->movement);
expect(remote_speeds::forever_packet(*speed,pet,70,0).opcode==run.native_observer);
// The actual normal route uses only its current, presented full actor binding.
Character self{};self.guid=4509;self.race=1;self.cls=1;self.name="Fixture";
NativeEntityTranslation entities;entities.self_presentation_ready=true;
NativeEntityTranslation::Codec codec=[](uint32_t mode,const Bytes&){expect(mode==GW_CODEC_COMPANIONS);Bytes b;le(b,0,2);le(b,1,4);return b;};
auto translate=[&](const LegacyPacket& p){return entities.translate(p,self,70,0x46010046,0,codec);};
expect(translate(move).empty());expect(translate(create(source)).empty());expect(translate(definition()).size()==2);
auto sent=translate(move);expect(sent.size()==1 && sent[0].opcode==0x600002 && sent[0].body==output);
Bytes records;le(records,move.body.size()+2,1);le(records,0xdd,2);append(records,move.body);
uLongf compressed_size=compressBound(records.size());Bytes compressed(compressed_size);
expect(compress2(compressed.data(),&compressed_size,records.data(),records.size(),Z_DEFAULT_COMPRESSION)==Z_OK);compressed.resize(compressed_size);
Bytes envelope;le(envelope,records.size(),4);append(envelope,compressed);
sent=translate({0x2fb,envelope});expect(sent.size()==1 && sent[0].opcode==0x600002 && sent[0].body==output);
Bytes raw_speed=packed(source);le(raw_speed,std::bit_cast<uint32_t>(8.f),4);
sent=translate({run.spline,raw_speed});expect(sent.size()==1 && sent[0].opcode==run.native_spline);
sent=translate({run.observer,observer});expect(sent.size()==1 && sent[0].opcode==run.native_observer);
auto nested=remote_speeds::forever_packet(*speed,pet,70,0);expect(sent[0].body==nested.body);
Bytes removed;le(removed,source,8);translate({0xaa,removed});expect(translate(move).empty() && translate({run.spline,raw_speed}).empty());
for(auto legacy:{uint64_t(4509),uint64_t(0xf130000001000001ULL)}){
auto bound=legacy==4509?forever_player_guid(legacy,70):forever_creature_guid(legacy,1,70,0);
expect(!forever_monster_spline(turtle_monster_spline(source_spline(legacy).body),bound).empty());
Bytes b=packed(legacy);le(b,std::bit_cast<uint32_t>(8.f),4);auto u=remote_speeds::turtle_packet(run.spline,b);expect(u && !remote_speeds::forever_packet(*u,bound,70,0).body.empty());
}
if(!captured_speed.empty()){
auto actual=remote_speeds::turtle_packet(run.spline,captured_speed);expect(actual && (actual->source_guid>>48)==0xf140);
auto current=forever_pet_guid(actual->source_guid,37057,70,0);auto packet=remote_speeds::forever_packet(*actual,current,70,0);
Reader body(packet.body);expect(unpack_guid(body,16)==Bytes(current.begin(),current.end()) && body.f32()==actual->speed);body.end();
}
std::cout<<"Bound Pet spline/stop/facing and six speed routes pass; canonical binding, realm/map and retired/unknown actor holds preserved.\n";
}
static Bytes file_bytes(const char* path){std::ifstream file(path,std::ios::binary);if(!file)throw Error("missing private companion fixture input");return Bytes(std::istreambuf_iterator<char>(file),{});}
static void pet_aura_boundary(const LegacyPacket& source_create,const LegacyPacket& source_template,
const Bytes& prior_codec_input={},const Bytes& prior_codec_body={},bool expect_old_reject=false){
const auto updates=turtle_object_updates(source_create.opcode,source_create.body);expect(updates.size()==1);
const auto source=updates.front().guid;expect((source>>48)==0xf140);
Character self{};self.guid=4509;self.race=1;self.cls=1;self.name="Fixture";
NativeEntityTranslation entities;entities.self_presentation_ready=true;
NativeAuraTranslation auras;auras.consume(updates);
expect(auras.units.at(source).initial && auras.dirty.contains(source));
for(auto id:auras.units.at(source).spells)expect(id==0);
unsigned calls=0;
NativeEntityTranslation::Codec codec=[&](uint32_t mode,const Bytes& input){
expect(mode==GW_CODEC_COMPANIONS);++calls;
if(!prior_codec_input.empty()){expect(input==prior_codec_input);return prior_codec_body;}
Bytes out;le(out,0,2);le(out,1,4);return out;
};
auto out=entities.translate(source_create,self,70,0x46010046,0,codec);
expect(out.empty() && calls==0 && !entities.actors.at(source).sent);
const std::map<uint64_t,Guid128> self_only{{4509,forever_player_guid(4509,70)}};
expect(auras.flush(self_only).empty() && auras.dirty.contains(source));
out=entities.translate(source_template,self,70,0x46010046,0,codec);
expect(out.size()==2 && out[0].opcode==0x4b0006 && out[1].opcode==0x5e0000 && calls==1);
const auto pet=entities.actors.at(source).modern;
expect(entities.actors.at(source).sent && (pet[15]>>2)==10);
auto known=self_only;known[source]=pet;
std::vector<LegacyPacket> empty;
try{empty=auras.flush(known);}catch(const Error& error){
if(!expect_old_reject || std::string_view(error.what())!="invalid aura unit/count")throw;
std::cout<<"Actual L preimage reproduces initial empty Pet aura rejection before World bundle return.\n";return;
}
expect(!expect_old_reject && empty.size()==1 && empty[0].opcode==0x690011);
Bytes expected{0x80,0};append(expected,pack_guid(pet));expect(empty[0].body==expected);
expect(!auras.units.at(source).initial && !auras.dirty.contains(source));
// A genuine nonempty Source slot uses the same Unit owner/slot contract.
expect(aura_definition_profile::mask(11392)==1);
LegacyObjectUpdate active;active.guid=source;active.kind=0;
active.fields={{turtle_entity_fields::UNIT_FIELD_AURA+7,11392},
{turtle_entity_fields::UNIT_FIELD_AURAFLAGS,4u<<28},
{turtle_entity_fields::UNIT_FIELD_AURALEVELS+1,60u<<24}};
auras.consume({active});auto nonempty=auras.flush(known);expect(nonempty.size()==1);
Reader reader(nonempty[0].body);expect(reader.le(1)==0 && reader.le(1)==0x40);
auto raw=unpack_guid(reader,16);expect(raw==Bytes(pet.begin(),pet.end()));
expect(reader.le(2)==7 && reader.le(1)==0x80 && reader.le(2)==0 && reader.le(4)==11392);
reader.le(4);expect(reader.le(4)==0 && reader.le(2)==2 && reader.le(4)==1 && reader.le(2)==60 && reader.le(1)==1);
active.fields={{turtle_entity_fields::UNIT_FIELD_AURA+7,0}};auras.consume({active});
auto clear=auras.flush(known);expect(clear.size()==1 && !auras.units.at(source).shown[7]);
for(auto unit:{forever_player_guid(4509,70),forever_creature_guid(0xf130000001000001ULL,1,70,0)})
expect(!forever_aura_update({unit,true,{}}).empty());
auto rejects=[](AuraSlotUpdate value){bool rejected=false;try{(void)forever_aura_update(value);}catch(const Error&){rejected=true;}expect(rejected);};
for(uint64_t type:{0u,3u,9u,11u,47u}){Guid128 bad{};bad[0]=1;auto high=type<<58;for(unsigned i=0;i<8;++i)bad[8+i]=uint8_t(high>>(8*i));rejects({bad,true,{}});}
rejects({{},true,{}});rejects({pet,true,{{48,std::nullopt}}});rejects({pet,true,{{0,std::nullopt},{0,std::nullopt}}});
AuraSlotUpdate too_many{pet,true,{}};too_many.slots.resize(49);rejects(too_many);
// A pet is a native Unit and may carry a validated source self-cast.
// Preserve its actual CastID; reject conflicting or foreign provenance.
AuraSlotData bad{11392,1,2,60,1};
const auto cast=forever_server_cast_guid(70,0,11392,0x8000000001ULL);
bad.server_self_cast=cast;
const auto with_cast=forever_aura_update({pet,false,{{7,bad}}});Reader cast_reader(with_cast);
cast_reader.le(2);expect(unpack_guid(cast_reader,16)==Bytes(pet.begin(),pet.end()));
expect(cast_reader.le(2)==7 && cast_reader.le(1)==0x80);
expect(unpack_guid(cast_reader,16)==Bytes(cast.begin(),cast.end()));
bad.server_self_cast=forever_server_cast_guid(71,0,11392,0x8000000001ULL);rejects({pet,false,{{7,bad}}});
bad.server_self_cast=forever_server_cast_guid(70,0,11393,0x8000000001ULL);rejects({pet,false,{{7,bad}}});
bad.server_self_cast=cast;bad.cast_unit=pet;rejects({pet,false,{{7,bad}}});
bad.cast_unit.reset();bad.server_self_cast.reset();bad.active_flags=0;rejects({pet,false,{{7,bad}}});
auras.forget(source);expect(!auras.units.contains(source) && !auras.dirty.contains(source));
std::cout<<"Pet entity/template creation -> empty initial aura -> nonempty/clear slot boundary passes; type/count and cast provenance restrictions retained.\n";
}
static void controlled_pet_book(decltype(&gw_codec_serialize) codec,uint8_t cls){
NativeWorldTranslation w{};w.selected.guid=4509;w.selected.cls=cls;w.selected.race=1;w.selected.name="Fixture";
w.realm=70;w.location=WorldLocation{};w.self_created=true;w.entities.self_presentation_ready=true;
const uint64_t pet=(0xf140ULL<<48)|(78ULL<<24)|32;
const uint32_t entry=cls==3?118u:416u,type=cls==3?1u:3u,family=cls==3?1u:23u;
const auto native=forever_pet_guid(pet,entry,w.realm,0);
auto dispatch=[&](LegacyPacket p){
const auto bundle=w.translate(p,0x46010046,0,codec);
Reader r(bundle);auto count=r.le(4);std::map<uint32_t,std::vector<Bytes>> result;
for(unsigned i=0;i<count;++i){const auto id=uint32_t(r.le(4));const auto size=r.le(4);result[id].push_back(r.take(size));}
r.end();return result;
};
auto raw=[](uint64_t id){Bytes b;le(b,id,8);return b;};
auto source_book=[&](uint64_t id,unsigned spells,unsigned cds){
Bytes b=raw(id);le(b,0,4);append(b,Bytes{1,2,0,0});
for(unsigned i=0;i<10;++i)le(b,0x81000000u+i,4);
le(b,spells,1);for(unsigned i=0;i<spells;++i)le(b,0xc100119fu+i,4);
le(b,cds,1);for(unsigned i=0;i<cds;++i){le(b,4500+i,2);le(b,0,2);le(b,5678+i,4);le(b,0,4);}
return LegacyPacket{0x179,b};
};
auto link=[&](uint64_t id,uint16_t field){return values(w.selected.guid,{{field,uint32_t(id)},{uint16_t(field+1),uint32_t(id>>32)}});};
// Hunter pet happiness (Source power 5: field 27, max 33) -> native slot 6, band-preserving 0..1000000.
std::map<uint16_t,uint32_t> pet_fields{{139,78},{140,1234}};
if(cls==3){pet_fields[27]=500000;pet_fields[33]=1050000;}
auto pet_create=create(pet,pet_fields,entry,cls==3?1515:688);
auto meta=definition(entry,type,6477,"Fixture",family);
auto mode=[&](uint64_t id){Bytes b=raw(id);append(b,Bytes{2,1,0,8});return LegacyPacket{0x17a,b};};
auto book=source_book(pet,2,1);
expect(!dispatch(book).contains(0x690014));expect(!dispatch(pet_create).contains(0x690014));
auto meta_out=dispatch(meta);expect(!meta_out.contains(0x690014));
auto carries=[](const std::map<uint32_t,std::vector<Bytes>>& out,int32_t v){
Bytes needle;le(needle,uint32_t(v),4);
for(const auto& [op,list]:out)for(const auto& b:list)if(std::search(b.begin(),b.end(),needle.begin(),needle.end())!=b.end())return true;
return false;};
if(cls==3){
expect(forever_pet_happiness(332999)<NativePetHappinessMax/3 && forever_pet_happiness(333000)==NativePetHappinessMax/3);
expect(forever_pet_happiness(666000)==2*NativePetHappinessMax/3 && forever_pet_happiness(1050000)==NativePetHappinessMax);
expect(forever_pet_happiness(500000)==500500);
expect(carries(meta_out,500500) && carries(meta_out,NativePetHappinessMax)); // Content stays Content
}
expect(w.entities.actors.at(pet).sent && w.entities.actors.at(pet).owned_stat_pet && !w.entities.actors.at(pet).companion);
expect(!dispatch(values(w.selected.guid,{{SourceSummonField,uint32_t(pet)}})).contains(0x690014));
auto presented=dispatch(values(w.selected.guid,{{uint16_t(SourceSummonField+1),uint32_t(pet>>32)}}));
expect(presented.contains(0x5e0000) && w.entities.self_controlled[1].presented==native);
auto bar=presented.at(0x690014).at(0);
Reader r(bar);expect(unpack_guid(r,16)==Bytes(native.begin(),native.end()) && r.le(2)==family);
expect(!r.le(2) && !r.le(4) && r.le(1)==2 && !r.le(1) && r.le(1)==1);
for(unsigned i=0;i<10;++i)expect(r.le(4)==0x81000000u+i);
for(unsigned i:{0u,1u,2u,3u})expect(r.le(4)==0x81000000u+i);
expect(r.le(4)==0x16000000);for(unsigned i:{7u,8u,9u})expect(r.le(4)==0x81000000u+i);
expect(r.le(4)==2 && r.le(4)==1 && !r.le(4) && r.le(4)==0xc100119f && r.le(4)==0xc10011a0);
expect(r.le(4)==4500 && r.le(4)==5678 && !r.le(4) && r.f32()==1.f && !r.le(2));r.end();
// Q57: DoLootRelease clears UNIT_FLAG_LOOTING, not SUMMON/CHARM.
// Exercise the actual world translator/codec with an acquired corpse opening.
// Both source packet orders must preserve a usable, already-published pet bar.
const auto before_loot=w;
unsigned loot_checks=0;
auto check_loot=[&](bool condition){++loot_checks;expect(condition);};
for(bool flags_first:{false,true}){
w=before_loot;
const uint64_t corpse=(0xf130ULL<<48)|(uint64_t(entry)<<24)|99;
dispatch(create(corpse,{},entry,0));
const auto corpse_native=w.entities.actors.at(corpse).modern;
auto request=pack_guid(corpse_native);
check_loot(w.gameplay(0x3e00cc,request).opcode==0x15d);
w.gameplay_sent(0x3e00cc,request);
check_loot(w.loot.opening && w.loot.opening->source==corpse);
auto response=raw(corpse);response.push_back(1);le(response,0,4);response.push_back(0);
auto opened=dispatch({0x160,response});
check_loot(opened.contains(0x4700bf) && w.loot.window && !w.loot.opening);
dispatch(values(w.selected.guid,{{46,0x400}})); // Source UNIT_FLAG_LOOTING
auto cleared_flags=[&]{
auto out=dispatch(values(w.selected.guid,{{46,0}}));
check_loot(!out.contains(0x690014));
};
if(flags_first)cleared_flags();
auto release=raw(corpse);release.push_back(1);
auto released=dispatch({0x161,release});
check_loot(released.contains(0x4700c5) && !w.loot.opening && !w.loot.window);
check_loot(!released.contains(0x690014));
if(!flags_first)cleared_flags();
check_loot(w.pet_bar && w.pet_bar->shown && w.pet_bar->state.unit==pet);
check_loot(w.entities.self_controlled[1].source_guid()==pet &&
w.entities.self_controlled[1].presented==native);
auto stop=w.gameplay(0x3f0022,pack_guid(native));
check_loot(stop.opcode==0x2ea && stop.body==raw(pet));
// A real zero control field is different from an omitted sparse field.
dispatch(link(0,SourceSummonField));
check_loot(!w.pet_bar && w.entities.self_controlled[1].presented==Guid128{});
bool rejected=false;try{w.gameplay(0x3f0022,pack_guid(native));}catch(const Error&){rejected=true;}
check_loot(rejected);
}
w=before_loot;
auto cleared_book=dispatch({0x179,raw(0)});
check_loot(cleared_book.contains(0x690014) && !w.pet_bar);
check_loot(w.entities.self_controlled[1].source_guid()==pet); // Snapshot clear is not SUMMON clear.
bool cleared_rejected=false;try{w.gameplay(0x3f0022,pack_guid(native));}catch(const Error&){cleared_rejected=true;}
check_loot(cleared_rejected);
w=before_loot;
std::cout<<"Q57 class "<<unsigned(cls)<<": "<<loot_checks<<" loot/control checks PASS\n";
expect(w.spells.empty());expect(!dispatch(mode(pet+1)).contains(0x47001f));
expect(dispatch(values(pet,{{139,78},{140,5678},{22,63}})).contains(0x5e0000));
expect(w.entities.actors.at(pet).pending_values.at(140)==5678 && w.pet_bar->shown);
if(cls==3){auto fed=dispatch(values(pet,{{27,700000}}));const auto happy=forever_pet_happiness(700000);
expect(happy>=2*NativePetHappinessMax/3 && carries(fed,happy));} // fed to Happy
auto mode_out=dispatch(mode(pet));auto expected=pack_guid(native);append(expected,Bytes{1,8,2});
expect(mode_out.at(0x47001f).at(0)==expected);
auto sound=[&](uint64_t id,uint32_t action){Bytes b=raw(id);le(b,action,4);return dispatch({0x324,b});};
for(unsigned action:{0u,1u}){auto snd=pack_guid(native);le(snd,action,4);expect(sound(pet,action).at(0x47014d)[0]==snd);}
expect(!sound(pet,2).contains(0x47014d) && !sound(pet+1,1).contains(0x47014d));
auto full=dispatch(source_book(pet,255,255));expect(full.at(0x690014).at(0).size()==bar.size()+253*4+254*18);
auto empty=dispatch({0x179,raw(0)});expect(empty.at(0x690014).front().size()==97 && !w.pet_bar);
expect(!dispatch(mode(pet)).contains(0x47001f));
expect(dispatch(book).contains(0x690014));
const auto generation=w.entities.actors.at(pet).generation;
dispatch({0xaa,raw(pet)});expect(!w.pet_bar && w.entities.self_controlled[1].presented==Guid128{});
dispatch(pet_create);expect(w.entities.actors.at(pet).generation!=generation);
expect(!dispatch(mode(pet)).contains(0x47001f));expect(!dispatch(link(pet,SourceSummonField)).contains(0x690014));
expect(dispatch(book).contains(0x690014));
const auto vanity=pet+1;dispatch(create(vanity));dispatch(definition());dispatch(link(vanity,SourceSummonField));
expect(w.entities.actors.at(vanity).companion && !dispatch(source_book(vanity,1,0)).contains(0x690014));
expect(!dispatch(mode(vanity)).contains(0x47001f));
const auto peer=pet+2;dispatch(create(peer,{{12,5555},{14,5555},{139,78}},entry,688));
dispatch(link(peer,SourceSummonField));
expect(!w.entities.actors.at(peer).sent && !dispatch(source_book(peer,1,0)).contains(0x690014));
const auto crossed=pet+3;dispatch(create(crossed,{{139,79}},entry,688));
expect(!w.entities.actors.at(crossed).owned_stat_pet && !dispatch(source_book(crossed,1,0)).contains(0x690014));
const uint64_t charm=(0xf130ULL<<48)|(uint64_t(entry)<<24)|34;
dispatch(create(charm,{},entry,0));dispatch(link(charm,SourceCharmField));
auto charm_book=dispatch(source_book(charm,1,0)).at(0x690014).at(0);Reader ch(charm_book);
const auto charm_id=forever_creature_guid(charm,entry,w.realm,0);
expect(unpack_guid(ch,16)==Bytes(charm_id.begin(),charm_id.end()) && ch.le(2)==0);
dispatch(link(0,SourceCharmField));expect(!w.pet_bar);
for(unsigned reason=1;reason<=11;++reason)expect(dispatch({0x173,Bytes{uint8_t(reason)}}).at(0x47015f)[0]==Bytes({uint8_t(reason),0,0,0}));
expect(!dispatch({0x173,{12}}).contains(0x47015f));
for(unsigned reason=1;reason<=4;++reason)expect(dispatch({0x2c6,Bytes{uint8_t(reason)}}).at(0x4701f4)[0]==Bytes({uint8_t(reason),0,0,0,0,0,0,0}));
expect(!dispatch({0x2c6,{5}}).contains(0x4701f4));
for(unsigned reason:{1u,8u,9u,10u}){
auto code=uint8_t(reason==10?14:reason);
expect(dispatch({0x273,{uint8_t(reason)}}).at(0x47002a)[0]==Bytes({code,0,0,0}));
}
expect(!dispatch({0x273,{6}}).contains(0x47002a));
expect(!sound(pet,0).contains(0x47014d));
dispatch(book);w.location->map=1;
expect(!dispatch(mode(pet)).contains(0x47001f) && !w.pet_bar);
for(auto size:{0u,7u,8u,43u,unsigned(book.body.size()-1),unsigned(book.body.size()+1)}){
auto invalid=book.body;invalid.resize(size);bool reject=false;try{(void)turtle_pet_spellbook(invalid);}catch(const Error&){reject=true;}expect(reject);
}
}
int main(int argc,char** argv){try{
// Hunter pet happiness bands: Source <333000 / <666000 / max 1050000 -> native thirds of 1000000.
expect(forever_pet_happiness(0)==0 && forever_pet_happiness(332999)<NativePetHappinessMax/3);
expect(forever_pet_happiness(333000)==NativePetHappinessMax/3 && forever_pet_happiness(665999)<2*NativePetHappinessMax/3);
expect(forever_pet_happiness(666000)==2*NativePetHappinessMax/3 && forever_pet_happiness(1050000)==NativePetHappinessMax);
expect(forever_pet_happiness(500000)==500500 && forever_pet_happiness(2000000)==NativePetHappinessMax);
if(argc==3 && std::string_view(argv[1])=="--controlled-pet"){
if (test_fixture::optional_codec_missing(argv[2])) return 77;
auto library = dlopen(argv[2], RTLD_NOW | RTLD_LOCAL);
if (!library) {
std::cerr << dlerror() << '\n';
return 1;
}
auto codec=reinterpret_cast<decltype(&gw_codec_serialize)>(dlsym(library,"gw_codec_serialize"));
expect(codec);controlled_pet_book(codec,3);controlled_pet_book(codec,9);dlclose(library);
std::cout<<"Controlled Hunter and Warlock pet actors, bar, mode, clear, helpers and vanity boundaries pass.\n";return 0;
}
if(argc>=2 && std::string_view(argv[1])=="--movement-boundary-only"){
if(argc>3)throw Error("private movement boundary accepts optional captured speed path");
pet_movement_boundary(argc==3?file_bytes(argv[2]):Bytes{});return 0;
}
if(argc>=2 && std::string_view(argv[1])=="--aura-boundary-only"){
if(argc!=6 && argc!=7)throw Error("private aura boundary requires create/reply/input/body paths");
pet_aura_boundary({0x1f6,file_bytes(argv[2])},{0x61,file_bytes(argv[3])},file_bytes(argv[4]),file_bytes(argv[5]),argc==7 && std::string_view(argv[6])=="--expect-old-reject");return 0;
}
pet_movement_boundary();
pet_aura_boundary(create((0xf140ULL<<48)|(78ULL<<24)|32),definition());
static_assert(GW_CODEC_COMPANIONS==56 && GW_CODEC_COMPANION_VALUES==57 && GW_CODEC_COMPANION_NAME==58);
for(auto row:companion_profile::Relations)expect(source_companion_summon(row.spell,row.entry));
expect(!source_companion_summon(12809,37057) && !source_companion_summon(37057,78));
Character self{};self.guid=4509;self.race=1;self.cls=1;self.name="Fixture";
constexpr uint64_t source=(0xf140ULL<<48)|(78ULL<<24)|32;
auto modern=forever_pet_guid(source,37057,70,0);Reader id(modern);expect(id.le(8)==32);auto high=id.le(8);expect(high>>58==10 && ((high>>6)&0x7fffff)==37057);
bool refused=false;try{forever_pet_guid(0xf130000000000001ULL,37057,70,0);}catch(const Error&){refused=true;}expect(refused);
std::vector<uint32_t> modes;std::vector<Bytes> inputs;
NativeEntityTranslation::Codec codec=[&](uint32_t mode,const Bytes& input){modes.push_back(mode);inputs.push_back(input);Bytes out;le(out,0,2);le(out,1,4);return out;};
NativeEntityTranslation state;state.self_presentation_ready=true;
auto run=[&](NativeEntityTranslation& s,const LegacyPacket& p){return s.translate(p,self,70,0x46010046,0,codec);};
expect(run(state,create(source)).empty());expect(state.actors.at(source).companion && !state.actors.at(source).sent && state.requests.size()==1 && state.classes.empty());
expect(state.requests.front().opcode==0x60);Reader query(state.requests.front().body);expect(query.le(4)==37057 && query.le(8)==0);query.end();
auto out=run(state,definition());expect(out.size()==2 && out[0].opcode==0x4b0006 && out[1].opcode==0x5e0000);
expect(state.actors.at(source).modern==modern && state.actors.at(source).sent && state.actors.at(source).eligible && state.classes.empty());
expect(modes==std::vector<uint32_t>{GW_CODEC_COMPANIONS});
Reader create_input(inputs.back());create_input.take(8+2+4+4+4+4+4);create_input.cstring(63);create_input.le(3);
expect(create_input.le(4)==1 && create_input.le(4)==37057 && create_input.le(1)==8 && create_input.le(4)==1 && create_input.le(8)==source);
auto bindings=companion_bindings(create_input,70,0);expect(bindings.size()==1 && bindings.at(source).owner==forever_player_guid(4509,70));
auto source_op=create_input.le(4);auto original=turtle_object_updates(source_op,create_input.take(create_input.left()));expect(original.size()==1 && source_passive_companion(original[0].fields));
auto name=state.companion_name_query(pack_guid(modern),70,0);expect(name.has_value());Reader name_input(*name);expect(name_input.take(16)==Bytes(modern.begin(),modern.end()) && name_input.le(4)==0 && name_input.cstring(255)=="Swiftstar");name_input.end();
expect(!state.companion_name_query(pack_guid(modern),70,1));
out=run(state,values(source,{{13,0},{22,35},{131,6477}}));expect(out.size()==1 && modes.back()==GW_CODEC_COMPANION_VALUES && state.actors.at(source).pending_values.at(12)==4509);
auto generation=state.actors.at(source).generation;
out=run(state,values(source,{{12,4508}}));expect(out.size()==1 && !state.actors.at(source).sent && !state.actors.at(source).eligible && !state.companion_name_query(pack_guid(modern),70,0));
Bytes removed;le(removed,source,8);run(state,{0xaa,removed});expect(state.actors.empty() && state.pet_source_value_fields==0 && state.retained_create_bytes==0);
out=run(state,create(source));expect(out.size()==2 && state.actors.at(source).sent && state.actors.at(source).generation>generation);
out=run(state,values(source,{{3,37058}}));expect(out.size()==1 && !state.actors.at(source).sent); // entry/owner transformations wait for a genuine fresh create.
for(auto fields:{std::map<uint16_t,uint32_t>{{146,0}},{{139,99}},{{140,123}},{{14,4508}},{{12,4508},{14,4508}}}){
NativeEntityTranslation held;held.self_presentation_ready=true;run(held,create(source,fields));run(held,definition());
expect(!held.actors.at(source).sent && held.actors.at(source).modern==Guid128{} && !held.companion_name_query(pack_guid(modern),70,0));
}
// The current clone's genuine Source creature-query replies identify
// these exact shop summons. Repair Bot and Kodo have CreatureType 0;
// Drezbit has type 7. Type 0 must pass the same owner/spell/template gates.
struct Shop {uint32_t entry,spell,display,type;const char* name;};
for(const auto& row:std::array<Shop,3>{{
{50041,46457,1097,0,"Field Repair Bot 75B"},
{51300,46483,7933,0,"Caravan Kodo"},
{51680,46472,7993,7,"Auctioneer Drezbit"}}}){
NativeEntityTranslation shop;shop.self_presentation_ready=true;
const auto shop_guid=(0xf140ULL<<48)|(uint64_t(row.entry)<<24)|32;
expect(source_companion_summon(row.spell,row.entry));
expect(run(shop,create(shop_guid,{},row.entry,row.spell,row.display)).empty());
expect(shop.actors.at(shop_guid).companion && !shop.actors.at(shop_guid).sent);
auto shown=run(shop,definition(row.entry,row.type,row.display,row.name));
expect(shown.size()==2 && shown[0].opcode==0x4b0006 && shown[1].opcode==0x5e0000);
expect(shop.templates.at(row.entry).type==row.type && shop.actors.at(shop_guid).sent &&
shop.actors.at(shop_guid).eligible && shop.actors.at(shop_guid).modern==forever_pet_guid(shop_guid,row.entry,70,0));
}
NativeEntityTranslation no_self;run(no_self,create(source));run(no_self,definition());expect(!no_self.actors.at(source).sent);
std::cout<<"Companion effect97 cohort, distinct Pet GUID, Source template/name authority, sparse ownership, lifecycle and controlled-Pet holds pass.\n";
return 0;
}catch(const std::exception& e){std::cerr<<e.what()<<'\n';return 1;}}