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

187 lines
9.5 KiB
C++

// TransportMgr computes the destination keyframe heading from the full taxi path. Exercise
// deck-local offsets and facing through TransferPending and NewWorld encoder inputs.
#include "world_translation.hpp"
#include "gateway/object_updates.hpp"
#include <bit>
#include <cmath>
#include <cstring>
#include <iostream>
using namespace gateway;
static void expect(bool value,const char* what){if(!value)throw Error(what);}
using Pose = std::array<float, 4>;
static Bytes new_world(uint32_t map, const Pose& position) {
Bytes packet;
le(packet, map, 4);
for (float value : position)
le(packet, std::bit_cast<uint32_t>(value), 4);
return packet;
}
static void check_position(Reader& reader, const Pose& expected, unsigned components) {
for (unsigned i = 0; i < components; ++i) {
const float tolerance = i == 3 ? 0.00001f : 0.001f;
expect(std::abs(reader.f32() - expected[i]) < tolerance, "incorrect world-space arrival pose");
}
}
static void check_transfer(NativeWorldTranslation world, uint32_t entry, uint32_t map, const Pose& source,
const Pose& expected, const NativeEntityTranslation::Codec& codec) {
const uint32_t old_map = 1 - map;
world.location->map = old_map;
world.entities.position_map = old_map;
Bytes pending;
le(pending, map, 4);
if (entry) {
le(pending, entry, 4);
le(pending, old_map, 4);
}
expect(world.receive_far_transfer(0x3f, pending, codec).empty(), "pending output");
expect(world.source_far_pending && (entry ? world.source_far_pending->ship && world.source_far_pending->ship->entry==entry : !world.source_far_pending->ship), "transport entry");
const auto out = world.receive_far_transfer(0x3e, new_world(map, source), codec);
if (entry == 999999) {
expect(out.empty() && !world.far_transfer, "unknown route was released");
return;
}
expect(out.size() == 2 && world.far_transfer && !world.source_far_pending, "transfer held");
Reader announce(out[0].body);
expect(announce.le(1) == 0 && announce.le(4) == map, "pending destination");
check_position(announce, expected, 3);
announce.end();
Reader destination(world.far_transfer->new_world);
expect(destination.le(1) == 2 && destination.le(4) == map, "new world destination");
check_position(destination, expected, 4);
expect(destination.le(4) == 1, "world counter");
destination.end();
}
// Keep the real packet dispatcher and source owner lifecycle, and inspect its native encoder inputs.
static int pass_codec(uint32_t, const uint8_t* input, uint32_t size, uint32_t, uint8_t* output,
uint32_t capacity, uint32_t* written) {
*written=size;
if(!output || capacity<size)return GW_CODEC_CAPACITY;
if(size)std::memcpy(output,input,size);
return GW_CODEC_OK;
}
static void check_passenger_teardown(NativeWorldTranslation world, uint32_t entry, uint32_t destination,
const Pose& arrival_pose) {
const uint32_t origin=1-destination;
world.location->map=origin;
world.entities.position_map=origin;
auto packet=[](NativeWorldTranslation& state,uint32_t opcode,const Bytes& body){
return state.translate({opcode,body},0x10046,1100,pass_codec);
};
Bytes pending;
le(pending,destination,4);le(pending,entry,4);le(pending,origin,4);
Bytes self,other;
le(self,world.selected.guid,8);le(other,866,8);
const auto target=new_world(destination,{0.f,0.f,0.f,0.f});
packet(world,0x3f,pending);
packet(world,0xaa,self);
packet(world,0xaa,other);
packet(world,0xa9,Bytes{0,0,0,0,0});
expect(!world.entities.self_presentation_ready && world.entities.self_position_generation==9,
"expected ordinary self teardown in the source dispatcher");
// All following controls begin with the same genuine ownership snapshot.
auto cancelled=world;
packet(cancelled,0x40,{});
packet(cancelled,0x3e,target);
expect(!cancelled.far_transfer && !cancelled.source_far_pending,"aborted passenger transfer");
auto replaced=world;
Bytes next=pending;
next[0]=uint8_t(origin);
packet(replaced,0x3f,next); // cannot certify a new route after self teardown
packet(replaced,0x3e,target);
expect(!replaced.far_transfer,"superseded passenger transfer");
auto stale=world;
++stale.entities.self_position_generation;
stale.entities.self_presentation_ready=true;
packet(stale,0x3e,target);
expect(!stale.far_transfer && !stale.source_far_pending,"new player generation reused ship ownership");
auto stale_duplicate=world;
++stale_duplicate.entities.self_position_generation;
stale_duplicate.entities.self_presentation_ready=true;
packet(stale_duplicate,0x3f,pending);
packet(stale_duplicate,0x3e,target);
expect(!stale_duplicate.far_transfer,"duplicate pending rebound to a fresh generation");
auto switched_character=world;
++switched_character.selected.guid;
packet(switched_character,0x3e,target);
expect(!switched_character.far_transfer,"different character reused pending ship");
auto switched_realm=world;
++switched_realm.realm;
packet(switched_realm,0x3e,target);
expect(!switched_realm.far_transfer,"different realm reused pending ship");
auto wrong_destination=world;
packet(wrong_destination,0x3e,new_world(2,{}));
expect(!wrong_destination.far_transfer,"different destination reused pending ship");
auto wrong_origin=world;
wrong_origin.location->map=2;
packet(wrong_origin,0x3e,target);
expect(!wrong_origin.far_transfer,"map change reused pending ship");
auto plain=world;
Bytes plain_pending;le(plain_pending,destination,4);
packet(plain,0x3f,plain_pending);
packet(plain,0x3e,target);
expect(!plain.far_transfer,"plain transfer inherited the ship snapshot");
packet(world,0x3f,pending); // idempotent while the source self is temporarily absent
packet(world,0x3e,target);
expect(world.far_transfer && world.far_transfer->owner_generation==9 && world.world_transfer_counter==1,
"ship teardown lost the pending owner");
Reader arrival(world.far_transfer->new_world);
expect(arrival.le(1)==2 && arrival.le(4)==destination,"ship destination map");
check_position(arrival,arrival_pose,4);
packet(world,0x3f,pending);
packet(world,0x3e,target);
expect(world.far_transfer && world.world_transfer_counter==1 && !world.source_far_pending,
"duplicate passenger handoff restarted transfer");
Bytes token;le(token,7,4);
world.suspend_transfer_response(token);
expect(world.far_transfer->phase==1,"suspend after teardown");
world.local_presentation(0x10046);
expect(world.far_transfer->phase==2,"NewWorld delivery after teardown");
auto ack=world.gameplay(0x440025,{});
expect(ack.opcode==0xdc && ack.body.empty(),"source WorldportAck");
world.gameplay_sent(0x440025,{});
expect(world.worldport_send_completed() && world.location->map==destination,"passenger destination commit");
expect(!world.worldport_send_completed(),"duplicate WorldportAck completion");
}
int main(){try{
NativeEntityTranslation::Codec codec=[](uint32_t,const Bytes& input){return input;};
NativeWorldTranslation world{};world.selected.guid=123;world.selected.race=2;world.selected.cls=1;world.realm=70;
world.self_created=true;world.location=WorldLocation{1,{1320.f,-5750.f,60.f,0.5f}};world.entities.self_presentation_ready=true;
world.entities.self_position_generation=9;world.entities.generation=9;world.entities.position_realm=70;world.entities.position_map=1;world.native_movement_counter=7;
check_passenger_teardown(world,190550,0,{89.40f,-5019.30f,-.80f,5.512455940f});
// Path 116 crosses to Theramore at node 16 and wraps to Menethil at node 1.
check_passenger_teardown(world,176231,1,{-4298.48f,-5627.37f,0.f,4.688140869f});
check_passenger_teardown(world,176231,0,{-3787.54f,178.12f,0.f,0.600936949f});
// Original path 121, nodes 20 and 1. The reverse traversal also crosses 2*pi.
check_transfer(world, 164871, 0, {1.5f,-2.f,.25f,.5f}, {3052.22136f,1768.67903f,125.28f,1.09270024f}, codec);
check_transfer(world, 164871, 1, {-4.f,3.f,.75f,2.5f}, {1338.36722f,-5682.58615f,71.83f,.78682919f}, codec);
// Path 116 wraps a negative facing. Standing at the origin of path 294 adds only the vessel pose.
check_transfer(world, 176231, 1, {2.f,-3.f,-.75f,-5.f}, {-4301.52761f,-5629.29667f,-.75f,5.97132618f}, codec);
check_transfer(world, 181646, 1, {0.f,0.f,0.f,0.f}, {6077.49f,746.96f,0.f,2.53794122f}, codec);
const Pose plain = {-8913.f,554.f,93.f,.75f};
check_transfer(world, 0, 0, plain, plain, codec);
check_transfer(world, 999999, 0, {}, {}, codec);
// Turtle and client time the route differently; the map switch must land on the same instant for both.
uint32_t c=0,period=0;
expect(turtle_mo_client_progress(164871,1,356175,49759,c,period) && c==49775 && period==356282,"dock arrival anchor");
expect(turtle_mo_client_progress(164871,0,356175,158292,c,period) && c>158329,"map-0 create starts the map-0 leg");
expect(turtle_mo_client_progress(164871,1,356175,158291,c,period) && c<158329,"map-1 create stays on the map-1 leg");
expect(turtle_mo_client_progress(164871,0,356175,158300,c,period) && c>=158579 && c<158700,"just after the switch: 250 ms into the map-0 leg");
expect(!turtle_mo_client_progress(164871,0,356000,1000,c,period),"unknown server period falls back");
std::cout<<"native ship passenger transfer PASS\n";return 0;
}catch(const std::exception& e){std::cerr<<e.what()<<'\n';return 1;}}