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

173 lines
13 KiB
C++

#include "entity_translation.hpp"
#include "world_translation.hpp"
#include "serializer.h"
#include <bit>
#include <cmath>
#include <iostream>
#include <zlib.h>
using namespace gateway;
namespace {
constexpr uint64_t self=42,target=(0xf130ULL<<48)|(123ULL<<24)|7;
constexpr uint16_t realm=70;
void check(bool yes,const char* message){if(!yes)throw Error(message);}
Bytes packed(uint64_t id){Bytes b;le(b,id,8);return pack_guid(b);}
void xyz(Bytes& b,std::array<float,3> p){for(float f:p)le(b,std::bit_cast<uint32_t>(f),4);}
LegacyPacket position(uint64_t guid,std::array<float,3> pos,uint32_t flags=0,bool create=false){
Bytes b;le(b,1,4);le(b,0,1);le(b,create?3:1,1);
if(create){append(b,packed(guid));le(b,3,1);}else le(b,guid,8);
le(b,0x20,1);le(b,flags,4);le(b,100,4);xyz(b,pos);le(b,0,4);le(b,0,4);
for(float speed:{2.5f,7.f,4.5f,4.7f,2.5f,3.14f})le(b,std::bit_cast<uint32_t>(speed),4);
if(create){std::map<uint16_t,uint32_t> fields{{3,123},{36,257},{131,19723}};std::vector<uint32_t> mask(5);
for(auto [field,value]:fields){(void)value;mask[field/32]|=1u<<(field%32);}le(b,mask.size(),1);
for(auto word:mask)le(b,word,4);for(auto [field,value]:fields){(void)field;le(b,value,4);}}
return {0xa9,std::move(b)};
}
LegacyPacket spline(uint64_t mover,uint64_t face_target=target,bool stop=false){
Bytes b=packed(mover);xyz(b,{0,0,0});le(b,17,4);le(b,stop?1:(face_target?3:0),1);
if(!stop){if(face_target)le(b,face_target,8);le(b,0x100,4);le(b,900,4);le(b,1,4);xyz(b,{9,9,0});}
return {0xdd,std::move(b)};
}
LegacyPacket regular(uint64_t guid,uint32_t flags,std::array<float,3> pos){
Bytes b=packed(guid);le(b,flags,4);le(b,200,4);xyz(b,pos);le(b,0,4);le(b,0,4);return {flags?0xb5u:0xb7u,std::move(b)};
}
LegacyPacket compressed(const LegacyPacket& p){
Bytes raw;le(raw,p.body.size()+2,1);le(raw,p.opcode,2);append(raw,p.body);Bytes out;le(out,raw.size(),4);
uLongf n=compressBound(raw.size());Bytes data(n);check(compress2(data.data(),&n,raw.data(),raw.size(),Z_BEST_SPEED)==Z_OK,"compression fixture");data.resize(n);append(out,data);return {0x2fb,std::move(out)};
}
NativeEntityTranslation state(){
NativeEntityTranslation s;s.self_presentation_ready=true;
CreatureTemplateReply info;info.entry=123;info.found=true;info.type=7;info.display=19723;info.names[0]="Fixture";s.templates[123]=info;return s;
}
NativeEntityTranslation::Codec codec=[](uint32_t mode,const Bytes&){
check(mode==GW_CODEC_NPCS_TYPES,"unexpected codec operation");Bytes b;le(b,0,2);le(b,1,4);return b;
};
Character identity(){Character c;c.guid=self;c.name="Fixture";c.race=1;c.cls=1;return c;}
std::vector<LegacyPacket> run(NativeEntityTranslation& s,const LegacyPacket& p,uint16_t r=realm,uint32_t map=0){return s.translate(p,identity(),r,1,map,codec);}
void bound_target(NativeEntityTranslation& s,std::array<float,3> p={10,10,0}){
auto out=run(s,position(target,p,0,true));check(!out.empty() && s.actors.at(target).sent,"genuine creature create binding");
check(s.source_positions.contains(target),"genuine create XYZ retained");
}
void target_wire(const LegacyPacket& p,const Guid128& mover,const Guid128& victim,float expected_angle){
check(p.opcode==0x600002,"wrong MonsterMove opcode");Reader r(p.body);
check(unpack_guid(r,16)==Bytes(mover.begin(),mover.end()),"mover identity");
check(r.le(4)==17 && r.le(4)==0x400000 && r.le(1)==2,"spline ID/ground flags/TARGET enum");
check(r.le(4)==0 && r.le(4)==900 && r.le(4)==0 && r.le(1)==0,"native spline time/default fields");
check(unpack_guid(r,16)==Bytes(16) && r.le(1)==255,"empty transport/default seat");
check(std::abs(r.f32()-expected_angle)<0.00001f,"target angle must use genuine XY");
check(unpack_guid(r,16)==Bytes(victim.begin(),victim.end()),"TARGET must write angle then bound GUID");
}
}
int main(){try{
auto s=state();bound_target(s);auto victim=s.actors.at(target).modern;
auto out=run(s,spline(self));check(out.size()==1,"owned ground TARGET spline missing");
target_wire(out[0],forever_player_guid(self,realm),victim,float(std::atan2(10.,10.)));
out=run(s,compressed(spline(self)));check(out.size()==1,"compressed self TARGET route");
target_wire(out[0],forever_player_guid(self,realm),victim,float(std::atan2(10.,10.)));
auto unknown=run(s,spline(self,target+1));check(unknown.empty(),"unbound target must hold");
run(s,regular(target,1,{20,5,0}));check(!s.source_positions.contains(target),"moving target stale XYZ not invalidated");
check(run(s,spline(self)).empty(),"moving target must not use guessed endpoint/angle");
run(s,regular(target,0,{20,5,0}));out=run(s,spline(self));check(out.size()==1,"genuine stop XYZ not refreshed");
target_wire(out[0],forever_player_guid(self,realm),victim,float(std::atan2(5.,20.)));
run(s,compressed(regular(target,1,{25,5,0})));check(!s.source_positions.contains(target),"compressed motion stale position");
run(s,position(target,{8,4,0}));check(s.source_positions.contains(target),"full movement-update XYZ not retained");
run(s,spline(target,0));check(!s.source_positions.contains(target),"NPC spline must invalidate former stationary XYZ");
run(s,spline(target,0,true));check(s.source_positions.contains(target),"actual spline stop origin must refresh XYZ");
check(run(s,spline(self)).empty(),"overlapping zero XY bearing must hold");
run(s,position(target,{8,4,0}));auto old=s.actors.at(target).generation;
bound_target(s,{6,-3,0});check(s.actors.at(target).generation!=old,"recreate generation not advanced");
out=run(s,spline(self));check(out.size()==1,"new generation genuine position");
target_wire(out[0],forever_player_guid(self,realm),s.actors.at(target).modern,float(2*std::acos(-1.)+std::atan2(-3.,6.)));
++s.actors.at(target).generation;check(run(s,spline(self)).empty(),"stale position generation accepted");
run(s,position(target,{10,0,0}));check(s.source_positions.contains(target),"position refresh");
s.invalidate_source_position(target);check(run(s,spline(self)).empty(),"owned motion invalidation helper");
run(s,position(target,{10,0,0}));Bytes gone;le(gone,target,8);run(s,{0xaa,gone});
check(!s.source_positions.contains(target) && run(s,spline(self)).empty(),"despawn kept stale target position");
auto foreign=state();bound_target(foreign);auto wrong=spline(self);run(foreign,wrong,realm+1);
check(foreign.source_positions.empty(),"realm change retained position cache");
auto unsupported=state();bound_target(unsupported);run(unsupported,{0x2fb,Bytes{1,0,0,0,0}});
check(unsupported.source_positions.empty(),"undecodable motion retained stale positions");
auto moving_create=state();bound_target(moving_create);run(moving_create,position(target,{5,4,0},1));
check(!moving_create.source_positions.contains(target),"full moving update stale XYZ");
// Source sessions do not echo owned regular movement. Reproduce the
// post-send hook using exact native intent + real source serialization.
NativeWorldTranslation w{};w.selected=identity();w.realm=realm;w.self_created=true;
w.location=WorldLocation{0,{0,0,0,0}};w.entities=state();bound_target(w.entities);
w.entities.self_position_generation=1;
auto owned=[&](uint32_t opcode,std::array<float,4> xyzo,uint32_t flags=0){
auto native=forever_ground_movement(GroundMovement{self,flags,250,xyzo},realm);
auto wire=w.player_movement(opcode,native,std::vector<Character>{w.selected});
w.movement_sent(opcode,native,wire);return std::pair{native,wire};
};
check(run(w.entities,spline(target,self)).empty(),"regression: NPC TARGET self must initially lack pose");
auto [moving_native,moving_wire]=owned(0x420000,{20,5,2,0},1);
check(w.entities.source_positions.at(self).owned_projection,"owned projection provenance missing");
check(w.entities.source_positions.at(self).xyz==std::array<float,3>{20,5,2},"moving current XYZ not refreshed");
auto chase=run(w.entities,spline(target,self));check(chase.size()==1,"NPC chase held after genuine owned movement");
target_wire(chase[0],w.entities.actors.at(target).modern,forever_player_guid(self,realm),float(std::atan2(5.,20.)));
owned(0x420002,{7,20,2,0});chase=run(w.entities,spline(target,self));
check(chase.size()==1,"NPC chase held after owned stop");
target_wire(chase[0],w.entities.actors.at(target).modern,forever_player_guid(self,realm),float(std::atan2(20.,7.)));
auto corrupt=moving_wire;corrupt.back().body[8]^=1;
w.mover_confirmed=false;
w.movement_sent(0x420000,moving_native,corrupt);
check(!w.mover_confirmed,"rejected serialized movement confirmed mover");
check(!w.entities.source_positions.contains(self),"arbitrary source XYZ accepted");
owned(0x420002,{7,20,2,0});w.movement_sent(0x420000,std::span(moving_native).first(3),moving_wire);
check(!w.entities.source_positions.contains(self),"malformed native intent retained position");
owned(0x420002,{7,20,2,0});w.movement_sent(0x420029,moving_native,moving_wire);
check(!w.entities.source_positions.contains(self),"unsupported ACK acquired owned position");
// Controller ACKs have a proved GUID+sequence prefix before MovementInfo.
w.pending_speeds[19]={0xe2,7.f};
auto speed=forever_ground_movement(GroundMovement{self,0,251,{11,12,3,0}},realm);
le(speed,19,4);le(speed,std::bit_cast<uint32_t>(7.f),4);
auto speed_wire=w.player_movement(0x420029,speed,std::vector<Character>{w.selected});
w.mover_confirmed=false;
speed_wire=w.player_movement(0x420029,speed,std::vector<Character>{w.selected});
auto bad_speed=speed_wire;bad_speed.back().body.back()^=1;
w.movement_sent(0x420029,speed,bad_speed);
check(w.pending_speeds.contains(19) && !w.mover_confirmed,"rejected speed ACK retired state");
w.movement_sent(0x420029,speed,speed_wire);
check(w.entities.source_positions.at(self).xyz==std::array<float,3>{11,12,3},"speed ACK MovementInfo offset wrong");
check(w.pending_speeds.empty(),"speed ACK state not completed");
w.pending_modes[20]=0xe8;
auto rooted=forever_ground_movement(GroundMovement{self,0x1000,252,{13,14,3,0}},realm);le(rooted,20,4);
auto root_wire=w.player_movement(0x42002c,rooted,std::vector<Character>{w.selected});
w.mover_confirmed=false;
root_wire=w.player_movement(0x42002c,rooted,std::vector<Character>{w.selected});
auto bad_root=root_wire;bad_root.back().body.back()^=1;
w.movement_sent(0x42002c,rooted,bad_root);
check(w.pending_modes.contains(20) && !w.mover_confirmed,"rejected mode ACK retired state");
w.movement_sent(0x42002c,rooted,root_wire);
check(w.entities.source_positions.at(self).xyz==std::array<float,3>{13,14,3},"mode ACK MovementInfo offset wrong");
check(w.pending_modes.empty(),"mode ACK state not completed");
// Native Status with real swim/fall tails, using the independent named
// packet order. These current XYZ reports remain useful while moving.
for(bool jump:{false,true}){
auto native=pack_guid(forever_player_guid(self,realm));le(native,jump?0x801:0x100001,8);le(native,253,4);
for(float f:{15.f,16.f,4.f,.5f,.25f,0.f})le(native,std::bit_cast<uint32_t>(f),4);
le(native,0,4);le(native,0,4);le(native,std::bit_cast<uint32_t>(1.f),4);le(native,jump?0x20:0,1);le(native,0,1);
if(jump){le(native,50,4);le(native,std::bit_cast<uint32_t>(4.f),4);le(native,0x80,1);
for(float f:{0.f,1.f,3.f})le(native,std::bit_cast<uint32_t>(f),4);}
auto wire=w.player_movement(jump?0x420006:0x420018,native,std::vector<Character>{w.selected});
w.movement_sent(jump?0x420006:0x420018,native,wire);
check(w.entities.source_positions.at(self).xyz==std::array<float,3>{15,16,4},"swim/fall current position lost");
}
owned(0x420002,{7,20,2,0});w.entities.position_map=1;
owned(0x420002,{7,20,2,0});check(!w.entities.source_positions.contains(self),"map mismatch acquired owned position");
w.entities.position_map=0;w.entities.self_position_generation=0;
owned(0x420002,{7,20,2,0});check(!w.entities.source_positions.contains(self),"uncreated self generation acquired owned position");
w.entities.self_position_generation=2;
GroundMovement foreignmove{self+1,0,1,{1,2,3,0}};
check(!w.entities.observe_owned_source_position(foreignmove,self,realm,0),"cross-actor position accepted");
auto invalid=GroundMovement{self,0,1,{INFINITY,2,3,0}};
check(!w.entities.observe_owned_source_position(invalid,self,realm,0),"nonfinite position accepted");
auto full=state();full.position_realm=realm;full.self_position_generation=1;
for(uint64_t i=1000;i<5097;++i)full.source_positions[i]={1,realm,0,{1,2,3}};
check(!full.observe_owned_source_position(GroundMovement{self,1,1,{1,2,3,0}},self,realm,0),"owned insert exceeded cache bound");
check(full.source_positions.size()==4097,"bounded cache changed on refusal");
w.pending_teleport=NearTeleport{};
owned(0x420002,{7,20,2,0});check(!w.entities.source_positions.contains(self),"pending teleport accepted owned position");
std::cout<<"Native self ground TARGET spline: create/update/compressed motion/generation/removal and wire-order contracts PASS\n";
return 0;
}catch(const std::exception& e){std::cerr<<"Native TARGET pipeline: "<<e.what()<<'\n';return 1;}}