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

94 lines
6.4 KiB
C++

#include "optional_test_codec.hpp"
#include "world_translation.hpp"
#include "gateway/warrior_stance_profile.hpp"
#include "gateway/turtle_entity_profile.hpp"
#ifdef _WIN32
#include <windows.h>
#include <cstring>
#else
#include <dlfcn.h>
#endif
#include <iostream>
using namespace gateway;
static void check(bool value){if(!value)throw Error("warrior source stance lifecycle assertion");}
template<class F>void rejects(F fn){try{fn();}catch(const Error&){return;}throw Error("expected stance rejection");}
static Bytes cast_request(uint32_t spell,Guid128 target={}){
Guid128 cast{};cast[0]=7;Bytes out=pack_guid(cast);le(out,0,1);
for(unsigned i=0;i<3;++i)le(out,0,4);
le(out,spell,4);le(out,0,4);le(out,0,4);le(out,target==Guid128{}?0:2,4);
append(out,pack_guid(target));append(out,pack_guid(Guid128{}));append(out,pack_guid(Guid128{}));
le(out,0,2);le(out,0,4);le(out,0,4);append(out,pack_guid(Guid128{}));
for(unsigned i=0;i<3;++i)le(out,0,4);
le(out,0,1);le(out,0,1);return out;
}
static NativeWorldTranslation world(){
NativeWorldTranslation w{};w.selected.guid=4528;w.selected.cls=1;w.realm=70;w.self_created=true;
w.location=WorldLocation{0,{1,2,3,0}};w.source_health=100;w.source_stand=0;w.source_form=17;
w.spells={2457,71,2458,100,355,1680,1715};return w;
}
static void form(NativeWorldTranslation&w,uint8_t value){
LegacyObjectUpdate delta;delta.guid=w.selected.guid;delta.kind=0;
delta.fields[turtle_entity_fields::UNIT_FIELD_BYTES_1]=uint32_t(value)<<16;w.observe_death_updates({delta});
}
static void queue(NativeWorldTranslation&w,const Bytes&body,uint32_t stance=71){
const auto packet=w.gameplay(0x3e0176,body);auto held=w.prepare_stance_cast(0x3e0176,body,packet);check(bool(held));
check(!w.stance_cast&&!w.pending_cast); // capacity-only planning has no state mutation
const auto again=w.prepare_stance_cast(0x3e0176,body,packet);check(again&&again->stance==held->stance);
const auto request=w.stance_switch_packet(*held);Bytes expected;le(expected,stance,4);le(expected,0,2);
check(request.opcode==0x12e&&request.body==expected);w.stance_switch_sent(std::move(*held));
check(w.stance_cast&&w.pending_cast&&!w.stance_cast_request());
}
int main(){try{
check(warrior_stance::Rules.size()==78);
const std::set<uint32_t>stances{2457,71,2458};
for(const auto&rule:warrior_stance::Rules){
auto learned=stances;learned.insert(rule.spell);
for(uint8_t f:{uint8_t(0),uint8_t(17),uint8_t(18),uint8_t(19)}){
const auto choice=warrior_stance::choose(rule.spell,1,f,learned);
const bool allowed=f&&(rule.mask&(1u<<(f-1)));
check(bool(choice)==!allowed);
if(choice)check(rule.mask&(1u<<(choice->form-1)));
}
}
check(!warrior_stance::choose(100,2,18,{100,2457}));
check(!warrior_stance::choose(100,1,std::nullopt,{100,2457}));
check(!warrior_stance::choose(100,1,7,{100,2457}));
check(!warrior_stance::choose(100,1,18,{100}));
check(!warrior_stance::choose(100,1,18,{2457}));
check(warrior_stance::choose(1715,1,18,{1715,2458})->spell==2458);
check(warrior_stance::choose(1715,1,18,{1715,2458,2457})->spell==2457);
const auto body=cast_request(355);auto w=world();queue(w,body);
rejects([&]{w.prepare_stance_cast(0x3e0176,body,w.gameplay(0x3e0176,body));});
{LegacyObjectUpdate other;other.guid=99999;other.kind=0;other.fields[turtle_entity_fields::UNIT_FIELD_BYTES_1]=18u<<16;w.observe_death_updates({other});check(!w.stance_cast_request());}
form(w,19);check(!w.stance_cast_request());form(w,18);
const auto ready=w.stance_cast_request();check(ready&&ready->body==w.gameplay(0x3e0176,body).body);
check(w.stance_cast_request()->body==ready->body);w.stance_ability_sent();check(!w.stance_cast&&w.pending_cast->spell.spell==355);
check(!w.prepare_stance_cast(0x3e0176,body,*ready)); // correct form never switches again
for(unsigned mode=0;mode<8;++mode){auto q=world();queue(q,body);form(q,18);
if(mode==0)q.location->map=1;if(mode==1)q.selected.guid=99999;if(mode==2)q.self_created=false;
if(mode==3)q.source_health=0;if(mode==4)q.logout_complete=true;
if(mode==5)q.stance_cast->deadline=std::chrono::steady_clock::now()-std::chrono::seconds(1);
if(mode==6)q.pending_teleport=NearTeleport{};if(mode==7)q.spells.erase(71);
check(!q.stance_cast_request()&&!q.stance_cast&&!q.pending_cast);
}
{auto q=world();auto target=forever_player_guid(4514,70);auto&actor=q.entities.actors[4514];actor.modern=target;actor.sent=true;actor.generation=1;
queue(q,cast_request(355,target));form(q,18);actor.generation=2;check(!q.stance_cast_request()&&!q.stance_cast);}
{auto q=world();queue(q,body);auto original=forever_spell_request(body);Bytes cancel=pack_guid(original.cast);le(cancel,355,4);
check(q.cancel_stance_cast(0x3e0179,cancel)&&!q.pending_cast&&!q.stance_cast);}
{auto q=world();queue(q,body);LegacyObjectUpdate create;create.kind=2;create.type=4;create.guid=q.selected.guid;q.observe_death_updates({create});check(q.source_form==0&&!q.stance_cast&&!q.pending_cast);}
{auto q=world();q.source_stand=1;q.source_stand_pending=true;auto p=q.gameplay(0x3e0176,body);check(q.defer_standing_cast(0x3e0176,body,p));q.stand_state_received(0);
auto standing=q.standing_cast_request();check(bool(standing));auto held=q.prepare_stance_cast(0x3e0176,q.standing_cast->original,*standing);check(bool(held));q.stance_switch_sent(std::move(*held));check(!q.standing_cast&&q.stance_cast);}
if (test_fixture::optional_codec_missing(GW_ENTITY_CODEC)) return 77;
#ifdef _WIN32
auto library=LoadLibraryA(GW_ENTITY_CODEC);check(library!=nullptr);
const auto proc=GetProcAddress(library,"gw_codec_serialize");decltype(&gw_codec_serialize) codec=nullptr;
static_assert(sizeof(codec)==sizeof(proc));std::memcpy(&codec,&proc,sizeof(codec));check(codec!=nullptr);
#else
void* library=dlopen(GW_ENTITY_CODEC,RTLD_NOW|RTLD_LOCAL);check(library!=nullptr);
auto codec=reinterpret_cast<decltype(&gw_codec_serialize)>(dlsym(library,"gw_codec_serialize"));check(codec!=nullptr);
#endif
{auto q=world();queue(q,body);Bytes failure;le(failure,71,4);le(failure,2,1);le(failure,35,1);q.translate({0x130,failure},1,0,codec);check(!q.stance_cast&&!q.pending_cast);}
{auto q=world();queue(q,body);Bytes interrupted;le(interrupted,q.selected.guid,8);le(interrupted,71,4);q.translate({0x2a6,interrupted},1,0,codec);form(q,18);check(!q.stance_cast_request()&&q.stance_cast->failed);}
std::cout<<"78-ability native stance policy, authoritative form, failures, stand ordering and stale-context guards PASS\n";return 0;
}catch(const std::exception&e){std::cerr<<e.what()<<'\n';return 1;}}