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

419 lines
23 KiB
C++

#include "world_translation.hpp"
#include "gateway/trainer_offer_filter.hpp"
#include <algorithm>
#include <source_location>
using namespace gateway;
namespace {
constexpr uint32_t TrainerEntry=trainer_offer_filter::Drops.front().creature_entry;
constexpr uint64_t Trainer=(0xf130ULL<<48)|(uint64_t(TrainerEntry)<<24)|99;
constexpr uint32_t Address=0x46010046;
void check(bool okay,const std::source_location& at=std::source_location::current()){
if(!okay)throw Error("trainer success refresh at line "+std::to_string(at.line()));
}
Bytes list_packet(uint8_t state=0,uint32_t type=0,bool profession=false){
Bytes out;le(out,Trainer,8);le(out,type,4);le(out,2,4);
for(uint32_t service:{1229u,1423u}){
le(out,service,4);le(out,state,1);le(out,100,4);le(out,profession,4);le(out,profession,4);
le(out,6,1);for(unsigned i=0;i<5;++i)le(out,0,4);
}
cstring(out,"Training");return out;
}
Bytes success(uint64_t trainer=Trainer,uint32_t service=1423){
Bytes out;le(out,trainer,8);le(out,service,4);return out;
}
NativeWorldTranslation fixture(){
NativeWorldTranslation world{};world.selected.guid=4509;world.selected.race=1;world.selected.cls=1;
world.realm=70;world.self_created=true;world.spells={78};
NativeEntityTranslation::Actor actor;actor.entry=TrainerEntry;actor.kind=3;actor.sent=true;
actor.modern=forever_creature_guid(Trainer,TrainerEntry,70,0);world.entities.actors[Trainer]=actor;
world.translate({0x1b1,list_packet()},Address,0,nullptr);
check(world.trainer && world.trainer->spells.size()==1 && world.trainer->spells[0].spell==1423);
return world;
}
void held(NativeWorldTranslation& world,const Bytes& body){
const auto spells=world.spells;
auto out=world.translate({0x1b3,body},Address,0,nullptr);
check(out==Bytes(4) && world.requests.empty() && world.spells==spells);
}
constexpr uint64_t Mallory = (0xf130ULL << 48) | (1215ULL << 24) | 99;
constexpr uint32_t MalloryTrainer = 1001215;
struct Lesson {
uint32_t service, display, cost, rank;
bool primary = false;
uint8_t state = 0;
bool slot = true;
uint8_t level = 0;
};
constexpr Lesson Apprentice = {2275,2275,10,0,true};
constexpr Lesson MalloryLessons[] = {
{2341,2337,250,55}, {7846,7845,2700,140}, {7839,7837,900,130}, {3186,3177,1800,130},
{3185,3176,1350,125}, {3181,3173,1350,120}, {3458,3447,3600,110}, {7842,7841,900,100},
{3179,3171,450,90}, {7180,7179,405,90}, {7838,7836,225,80}, {11536,2334,225,50},
{2340,2332,135,40}, {2339,2331,100,25}, {3184,3170,50,15}, Apprentice,
};
Bytes lesson_list(std::span<const Lesson> lessons, uint64_t npc = Mallory, uint32_t skill = 171,
const std::string& greeting = "Alchemy") {
Bytes packet;
le(packet, npc, 8); le(packet, 2, 4); le(packet, lessons.size(), 4);
for (const auto& lesson : lessons) {
le(packet, lesson.service, 4); le(packet, lesson.state, 1);
le(packet, lesson.cost, 4); le(packet, lesson.primary && lesson.slot, 4); le(packet, lesson.primary, 4);
le(packet, lesson.primary ? 5 : lesson.level, 1); le(packet, lesson.primary ? 0 : skill, 4); le(packet, lesson.rank, 4);
for (unsigned i = 0; i != 3; ++i) le(packet, 0, 4);
}
cstring(packet, greeting); return packet;
}
using WireLesson = std::array<uint32_t, 4>; // Cost, level and required skill/rank.
std::map<uint32_t, WireLesson> published_lessons(const Bytes& frames, const Guid128& npc, uint32_t trainer_id,
uint32_t expected_step, std::span<const uint32_t> recipe_order = {},
uint8_t expected_type = 2) {
Reader response(frames);
const auto count = response.le(4);
check(count == 1 || count == 2);
if (count == 2) {
check(response.le(4) == 0x47031b);
check(response.take(response.le(4)) == forever_interaction_open(npc, 7));
}
check(response.le(4) == 0x47018a);
const auto payload = response.take(response.le(4)); response.end();
Reader rows(payload);
check(rows.take(pack_guid(npc).size()) == pack_guid(npc));
check(rows.le(1) == expected_type && rows.le(4) == trainer_id);
const auto services = rows.le(4);
check(services >= 1);
if (!recipe_order.empty()) check(services == recipe_order.size() + 1);
std::map<uint32_t, WireLesson> result;
for (uint32_t i = 0; i < services; ++i) {
const auto id = uint32_t(rows.le(4));
if (i == 0) check(id == expected_step);
else if (!recipe_order.empty()) check(id == recipe_order[i - 1]);
check(rows.le(4) == 0); // The pinned writer has no serialized Source availability byte.
WireLesson lesson{uint32_t(rows.le(4)), uint32_t(rows.le(1)), uint32_t(rows.le(4)), uint32_t(rows.le(4))};
check(result.emplace(id, lesson).second);
rows.take(12);
}
return result;
}
void learn(NativeWorldTranslation& world, uint32_t spell) {
Bytes body; le(body, spell, 4);
const auto out = world.translate({0x12b, body}, Address, 0, nullptr);
Reader update(out);
check(update.le(4) == 1 && update.le(4) == 0x690040);
check(update.take(update.le(4)) == forever_spell_learning(0x12b, body));
update.end(); check(world.spells.contains(spell));
}
void source_skill_snapshot(NativeWorldTranslation& world, uint16_t skill, uint16_t rank, uint16_t maximum) {
std::map<uint16_t,uint32_t> fields;
if (skill) {
fields[SourceSkillFirst] = skill;
fields[SourceSkillFirst + 1] = rank | (uint32_t(maximum) << 16);
}
world.entities.self_profession_skills.initialize(fields, SourceSkillFirst, world.selected.guid);
}
void tradeskill_offers() {
auto world = fixture();
source_skill_snapshot(world, 171, 25, 75);
const auto modern = forever_creature_guid(Mallory, 1215, 70, 0);
NativeEntityTranslation::Actor actor;
actor.entry = 1215; actor.kind = 3; actor.sent = true; actor.modern = modern;
world.entities.actors[Mallory] = actor;
auto select = [&](uint32_t native_spell, uint32_t id = MalloryTrainer) {
Bytes body = pack_guid(modern);
le(body, id, 4); le(body, native_spell, 4);
return world.gameplay(0x3f0046, body);
};
auto reject = [&](uint32_t spell, uint32_t id = MalloryTrainer) {
bool rejected = false;
try { select(spell, id); } catch (const Error&) { rejected = true; }
check(rejected);
};
const auto frames = world.translate({0x1b1, lesson_list(MalloryLessons)}, Address, 0, nullptr);
Reader response(frames);
check(response.le(4) == 2 && response.le(4) == 0x47031b);
check(response.take(response.le(4)) == forever_interaction_open(modern, 7));
check(response.le(4) == 0x47018a);
const auto payload = response.take(response.le(4)); response.end();
Reader rows(payload);
check(rows.take(pack_guid(modern).size()) == pack_guid(modern));
check(rows.le(1) == 2 && rows.le(4) == MalloryTrainer && rows.le(4) == std::size(MalloryLessons));
for (size_t i = 0; i < std::size(MalloryLessons); ++i) {
const auto& lesson = i == 0 ? Apprentice : MalloryLessons[i - 1];
check(rows.le(4) == lesson.display && rows.le(4) == 0 && rows.le(4) == lesson.cost);
check(rows.le(1) == (lesson.primary ? 5u : 0u));
check(rows.le(4) == (lesson.primary ? 0u : 171u) && rows.le(4) == lesson.rank);
for (unsigned i = 0; i != 3; ++i) check(rows.le(4) == 0);
}
check(rows.le(1) == 0 && rows.le(1) == (7u << 5));
check(rows.take(7) == Bytes({'A','l','c','h','e','m','y'})); rows.end();
check(world.trainer && world.trainer->spells.size() == 16);
check(!world.spells.contains(2259));
const auto recipe = select(2331);
check(recipe.opcode == 0x1b2 && recipe.body == success(Mallory, 2339));
reject(2259); reject(2339); reject(2331, 1);
auto duplicate_step_display = *world.trainer;
duplicate_step_display.spells.at(1).native_spell = 2275;
check(!supports_trainer(duplicate_step_display));
bool refused = false;
try { forever_trainer_list(duplicate_step_display, modern); } catch (const Error&) { refused = true; }
check(refused);
auto misplaced = *world.trainer;
std::swap(misplaced.spells[0], misplaced.spells[1]);
check(!supports_trainer(misplaced));
refused = false;
try { forever_trainer_list(misplaced, modern); } catch (const Error&) { refused = true; }
check(refused);
auto failure = success(Mallory, 2339); le(failure, 1, 4);
const auto failed = world.translate({0x1b4, failure}, Address, 0, nullptr);
Reader message(failed);
check(message.le(4) == 1 && message.le(4) == 0x47018b);
Bytes wanted = pack_guid(modern); le(wanted, 2331, 4); le(wanted, 1, 4);
check(message.take(message.le(4)) == wanted); message.end();
held(world, success(Mallory, 2331));
world.translate({0x1b3, success(Mallory, 2339)}, Address, 0, nullptr);
check(world.requests.size() == 1 && world.requests.front().opcode == 0x1b0);
world.requests.clear();
constexpr Lesson refreshed_lessons[] = {
Apprentice, {2339,2331,100,25,false,2}, {3184,3170,50,15,false,1}, {3458,3447,3600,110}
};
const auto refreshed = world.translate({0x1b1, lesson_list(refreshed_lessons)}, Address, 0, nullptr);
Reader refresh(refreshed); check(refresh.le(4) == 1 && refresh.le(4) == 0x47018a);
reject(2331); reject(3170); reject(2259);
check(select(3447).body == success(Mallory, 3458));
constexpr Lesson mixed[] = {
{11536,2334,225,50}, {2363,2334,225,50}, {987654,0,1,1}, {3184,3170,50,15}, Apprentice
};
world.translate({0x1b1, lesson_list(mixed)}, Address, 0, nullptr);
check(world.trainer && world.trainer->spells.size() == 2);
reject(2334); reject(987654); reject(3447);
check(select(3170).body == success(Mallory, 3184));
reject(2259);
source_skill_snapshot(world, 0, 0, 0);
auto newcomer = std::to_array(MalloryLessons);
for (auto& lesson : newcomer)
if (!lesson.primary) lesson.state = 1; // No Alchemy skill for any recipe prerequisite.
std::array<uint32_t, std::size(MalloryLessons) - 1> recipe_order{};
for (size_t i = 0; i < recipe_order.size(); ++i) recipe_order[i] = MalloryLessons[i].display;
auto interleaved = newcomer;
std::rotate(interleaved.begin() + 4, interleaved.end() - 1, interleaved.end());
const auto newcomer_frames = world.translate({0x1b1, lesson_list(interleaved)}, Address, 0, nullptr);
const auto newcomer_rows = published_lessons(newcomer_frames, modern, MalloryTrainer, 2275, recipe_order, 0);
check(world.trainer && world.trainer->type == 2); // Source still owns profession purchases.
const auto raw_menu = *world.trainer;
check(trainer_profession_skill(raw_menu) == 171);
check(native_trainer_type(raw_menu) == 0 && native_trainer_type(raw_menu, 164) == 0 &&
native_trainer_type(raw_menu, 171) == 2);
auto direct = forever_trainer_list(raw_menu, modern);
auto unrelated = forever_trainer_list(raw_menu, modern, 164);
auto trained_direct = forever_trainer_list(raw_menu, modern, 171);
check(direct == unrelated && direct.at(pack_guid(modern).size()) == 0);
direct.at(pack_guid(modern).size()) = 2;
check(trained_direct == direct); // Rank, price, order, trainer ID and greeting match byte for byte.
check(newcomer_rows.size() == 16 && newcomer_rows.contains(2275) && !newcomer_rows.contains(2259));
check(world.trainer->spells.front().state == 0 && world.trainer->spells.front().native_state() == 1);
for (const auto& lesson : newcomer)
if (!lesson.primary) reject(lesson.display);
// A confirmed stock buy carries the teaching service. Only Source can acknowledge or grant it.
const auto starter_buy = select(2275);
check(starter_buy.opcode == 0x1b2 && starter_buy.body == success(Mallory, 2275));
check(world.requests.empty() && !world.spells.contains(2259));
reject(2259);
learn(world, 2259);
Bytes query; le(query, Mallory, 8);
auto acknowledge = [&](uint32_t service) {
const auto spells = world.spells;
check(world.translate({0x1b3, success(Mallory, service)}, Address, 0, nullptr) == Bytes(4));
check(world.requests.size() == 1 && world.requests.front().opcode == 0x1b0 &&
world.requests.front().body == query && world.spells == spells);
world.requests.pop_front();
};
acknowledge(2275);
// At acquisition, all this trainer's recipes still require at least 15 Alchemy.
newcomer.back().state = 2;
const auto without_skill = published_lessons(world.translate({0x1b1, lesson_list(newcomer)}, Address, 0, nullptr),
modern, MalloryTrainer, 2275, recipe_order, 0);
check(without_skill == newcomer_rows); // The learned spell alone does not establish a player skill line.
source_skill_snapshot(world, 171, 1, 75);
const auto trained = published_lessons(world.translate({0x1b1, lesson_list(newcomer)}, Address, 0, nullptr),
modern, MalloryTrainer, 2275, recipe_order);
check(trained == newcomer_rows); // The reference writer reserves the same bytes for every Source state.
check(world.trainer->spells.front().state == 2 && world.trainer->spells.front().native_state() == 0);
reject(2275); reject(3170);
// Later Source admits the first recipe at skill 15. The buy identifies its genuine wrapper.
source_skill_snapshot(world, 171, 15, 75);
newcomer[14].state = 0;
world.translate({0x1b1, lesson_list(newcomer)}, Address, 0, nullptr);
const auto first_recipe = select(3170);
check(first_recipe.opcode == 0x1b2 && first_recipe.body == success(Mallory, 3184));
check(!world.spells.contains(3170));
learn(world, 3170);
acknowledge(3184);
newcomer[14].state = 2;
world.translate({0x1b1, lesson_list(newcomer)}, Address, 0, nullptr);
reject(3170); reject(3184); reject(2275);
source_skill_snapshot(world, 171, 75, 75);
auto capped = std::to_array(MalloryLessons);
for (auto& lesson : capped)
lesson.state = lesson.service == 2339 ? 0 : 2;
for (bool free_slot : {false, true}) {
capped.back().slot = free_slot;
const auto visible = published_lessons(world.translate({0x1b1, lesson_list(capped)}, Address, 0, nullptr),
modern, MalloryTrainer, 2275, recipe_order);
check(visible.size() == std::size(MalloryLessons));
for (const auto& lesson : capped) {
check(visible.contains(lesson.display));
if (lesson.state == 2) reject(lesson.display);
}
check(select(2331).body == success(Mallory, 2339));
}
for (const auto& restricted : {Lesson{2275,2275,10,0,true,1}, Lesson{2275,2275,10,0,true,0,false}}) {
const Lesson offers[] = {{2339,2331,100,25,false,1}, restricted};
const auto frames = world.translate({0x1b1, lesson_list(offers)}, Address, 0, nullptr);
const auto visible = published_lessons(frames, modern, MalloryTrainer, 2275);
check(visible.size() == 2);
check(world.trainer->spells.front().native_state() == (restricted.state == 1 ? 2 : 1));
reject(2275); reject(2331);
}
auto stale_after = [&](std::span<const Lesson> next) {
world.translate({0x1b1, lesson_list(capped)}, Address, 0, nullptr);
check(world.trainer && world.trainer->spells.size() == 16);
const auto output = world.translate({0x1b1, lesson_list(next)}, Address, 0, nullptr);
check(output == Bytes(4) && !world.trainer);
reject(2275); reject(2331);
held(world, success(Mallory, 2275));
held(world, success(Mallory, 2339));
check(world.translate({0x1b4, failure}, Address, 0, nullptr) == Bytes(4));
};
stale_after({});
stale_after(std::span(MalloryLessons).first(15)); // Recipes alone cannot supply a rank service.
for (const Lesson other : {Lesson{2155,0,10,0,true}, Lesson{987654,0,10,0,true}}) {
const Lesson unresolved[] = {other, {2339,2331,100,25}};
stale_after(unresolved);
const Lesson first[] = {other, Apprentice, {2339,2331,100,25}};
const Lesson last[] = {{2339,2331,100,25}, Apprentice, other};
for (auto offered : {std::span(first), std::span(last)}) {
const auto visible = published_lessons(world.translate({0x1b1, lesson_list(offered)}, Address, 0, nullptr),
modern, MalloryTrainer, 2275);
check(visible.size() == 2 && visible.contains(2275) && visible.contains(2331));
reject(other.service);
}
}
world.translate({0x1b1, lesson_list(MalloryLessons)}, Address, 0, nullptr);
constexpr Lesson duplicate_service[] = {Apprentice, {2339,2331,100,25}, Apprentice};
bool duplicate_refused = false;
try { world.translate({0x1b1, lesson_list(duplicate_service)}, Address, 0, nullptr); }
catch (const Error&) { duplicate_refused = true; }
check(duplicate_refused && !world.trainer);
reject(2275); reject(2331);
}
void blacksmith_rank_selection() {
constexpr uint32_t entry = 1383;
constexpr uint32_t trainer_id = 1001383;
constexpr uint64_t smith = (0xf130ULL << 48) | (uint64_t(entry) << 24) | 99;
const auto modern = forever_creature_guid(smith, entry, 70, 0);
auto world = fixture();
NativeEntityTranslation::Actor actor;
actor.entry = entry; actor.kind = 3; actor.sent = true; actor.modern = modern;
world.entities.actors[smith] = actor;
auto select = [&](uint32_t spell) {
Bytes body = pack_guid(modern); le(body, trainer_id, 4); le(body, spell, 4);
return world.gameplay(0x3f0046, body);
};
auto reject = [&](uint32_t spell) {
bool rejected = false;
try { select(spell); } catch (const Error&) { rejected = true; }
check(rejected);
};
source_skill_snapshot(world, 164, 50, 75);
const Lesson apprentice{2020,2020,9,0,true,2};
const Lesson journeyman{2021,2021,450,50,false,0,true,10};
const Lesson breastplate{2753,2675,900,145,false,1};
learn(world, 2018);
for (bool reversed : {false, true}) {
auto lessons = std::array{apprentice, journeyman, breastplate};
if (reversed) std::reverse(lessons.begin(), lessons.end());
const auto frames = world.translate({0x1b1, lesson_list(lessons, smith, 164, "Blacksmithing")},
Address, 0, nullptr);
const auto rows = published_lessons(frames, modern, trainer_id, 2021);
check(rows.size() == 2 && rows.at(2021) == WireLesson{450,10,164,50} && rows.contains(2675));
check(!rows.contains(2020) && !rows.contains(3100));
reject(2020); reject(2018); reject(3100); reject(2675);
const auto buy = select(2021);
check(buy.opcode == 0x1b2 && buy.body == success(smith, 2021));
}
// If a higher rank is still unavailable, the known apprentice remains the single step.
auto waiting = std::array{apprentice, journeyman, breastplate};
waiting[1].state = 1;
const auto rows = published_lessons(world.translate({0x1b1, lesson_list(waiting, smith, 164, "Blacksmithing")},
Address, 0, nullptr), modern, trainer_id, 2020);
check(rows.size() == 2 && rows.contains(2020) && !rows.contains(2021));
reject(2020); reject(2021); reject(2675);
learn(world, 3100);
source_skill_snapshot(world, 164, 150, 150);
for (bool reversed : {false, true}) {
auto lessons = std::array{apprentice, journeyman, breastplate};
lessons[1].state = 2; lessons[2].state = 0;
if (reversed) std::reverse(lessons.begin(), lessons.end());
const auto frames = world.translate({0x1b1, lesson_list(lessons, smith, 164, "Blacksmithing")},
Address, 0, nullptr);
const auto rows = published_lessons(frames, modern, trainer_id, 2021);
check(rows.size() == 2 && rows.at(2021) == WireLesson{450,10,164,50} &&
rows.at(2675) == WireLesson{900,0,164,145});
check(!rows.contains(2020));
reject(2020); reject(2021); reject(3100); reject(2753);
const auto buy = select(2675);
check(buy.opcode == 0x1b2 && buy.body == success(smith, 2753));
}
}
}
int main(){
auto world=fixture();const auto spells=world.spells;
Bytes class_selection = pack_guid(world.entities.actors.at(Trainer).modern);
le(class_selection, 1, 4); le(class_selection, 1423, 4);
const auto class_buy = world.gameplay(0x3f0046, class_selection);
check(class_buy.opcode == 0x1b2 && class_buy.body == success());
auto native=world.translate({0x1b3,success()},Address,0,nullptr);
Bytes query;le(query,Trainer,8);
check(native==Bytes(4) && world.requests.size()==1 && world.requests.front().opcode==0x1b0 && world.requests.front().body==query);
check(world.spells==spells && world.trainer->spells[0].state==0 && world.trainer->spells[0].cost==100);
world.translate({0x1b3,success()},Address,0,nullptr);
check(world.requests.size()==1); // No duplicate request while queued.
world.requests.pop_front(); // Existing transport drains the genuine request.
auto source=list_packet(2);auto refreshed=world.translate({0x1b1,source},Address,0,nullptr);
auto expected=turtle_trainer_list(source);expected.spells.erase(expected.spells.begin());
Reader response(refreshed);check(response.le(4)==1 && response.le(4)==0x47018a);
check(response.take(response.le(4))==forever_trainer_list(expected,world.entities.actors.at(Trainer).modern));response.end();
check(world.trainer->spells[0].state==2 && world.spells==spells); // State comes only from real list.
auto stale=fixture();held(stale,success(Trainer+1));held(stale,success(Trainer,123456));
held(stale,success(Trainer,1229)); // Existing catalogue filter removed this offer.
stale.trainer.reset();held(stale,success());
auto hidden=fixture();hidden.entities.actors.at(Trainer).sent=false;held(hidden,success());
auto gone=fixture();gone.entities.actors.erase(Trainer);held(gone,success());
auto pre=fixture();pre.self_created=false;held(pre,success());
auto logout=fixture();logout.logout_complete=true;held(logout,success());
auto profession=fixture();profession.translate({0x1b1,list_packet(0,2,true)},Address,0,nullptr);held(profession,success());
tradeskill_offers();
blacksmith_rank_selection();
auto full=fixture();for(unsigned i=0;i<4096;++i)full.requests.push_back({0x50,{}});
check(full.translate({0x1b3,success()},Address,0,nullptr)==Bytes(4) && full.requests.size()==4096);
auto malformed=fixture();
for(bool extra:{false,true}){
auto body=success();if(extra)body.push_back(0);else body.pop_back();
bool rejected=false;try{malformed.translate({0x1b3,body},Address,0,nullptr);}catch(const Error&){rejected=true;}
check(rejected && malformed.requests.empty() && malformed.spells==spells);
}
}