Files
client/tests/inventory_tests.h

121 lines
9.2 KiB
C++

#pragma once
#include "../src/game/inventory_tools.h"
namespace inventory_test {
inline std::vector<world::item> stock;
inline world::item cursor;
inline int transfers=0,swaps=0;
inline bool defer_swap=false;inline int pending_source=-1,pending_destination=-1;
inline std::vector<std::string> transferred_groups;
struct type {unsigned char padding[0xd8]{};std::string name;};
inline type native_type;
inline uintptr_t counter=reinterpret_cast<uintptr_t>(&native_type);
inline unsigned char stack[48]{};
inline uintptr_t __fastcall item(uintptr_t,const std::string* group,int index) {
const auto value=*group=="cursor_items" ? cursor : index>=0 && index<int(stock.size()) ? stock[index] : world::item{};
native_type.name=value.name;*reinterpret_cast<uintptr_t*>(stack+8)=reinterpret_cast<uintptr_t>(&counter);
stack[0x22]=static_cast<unsigned char>(value.count);return reinterpret_cast<uintptr_t>(stack);
}
inline void __fastcall swap(uintptr_t,const std::string*,int index) {
// Native full-take refuses to replace a different held item.
assert(!cursor.count);++swaps;std::swap(cursor,stock.at(index));
}
inline void __fastcall place(uintptr_t,const std::string*,int index) {
assert(cursor.count);++swaps;
if(defer_swap && stock.at(index).count){pending_destination=index;return;}
std::swap(cursor,stock.at(index));
}
inline void refresh(inventory_tools::snapshot& state) {
state.panels[0].items=stock;state.held=cursor;state.cursor=cursor.count>0;
}
inline void __fastcall transfer(uintptr_t,int amount,const std::string* group,int index){assert(amount==stock.at(index).count && cursor.count==0);transferred_groups.push_back(*group);stock[index]={};++transfers;}
inline uint32_t __fastcall tick(uintptr_t){return 7;}
inline void __fastcall decline(uintptr_t,int,const std::string*,int){++transfers;}
}
inline void test_inventory_tools() {
using namespace inventory_tools;using namespace inventory_test;
auto old_item=get_item;auto old_swap=swap_cursor;auto old_place=place_all;auto old_auto=auto_place;
get_item=&item;swap_cursor=&swap;place_all=&place;auto_place=&transfer;
alignas(8) unsigned char controller[0x12d0]{};*reinterpret_cast<uintptr_t*>(controller+0x12c0)=1;
const auto owner=reinterpret_cast<uintptr_t>(controller);
stock={{"stone",64},{"apple",12},{"dirt",8},{"apple",3},{}};cursor={};
snapshot state;state.controller=owner;state.panels={{"container_items",stock},{"inventory_items",stock}};
engine worker;worker.start({owner,action::sort,"container_items"});
for(int i=0;i<40 && worker.active();++i){refresh(state);worker.step(state,1000+i*100,true);}
assert(!worker.active() && cursor.count==0 && stock[0].name=="apple" && stock[1].name=="apple" && stock[2].name=="dirt" && stock[3].name=="stone" && stock[4].name.empty());
assert(stock[0].count==12 && stock[1].count==3 && stock[3].count==64);
worker.start({owner,action::sort,"container_items"});state.cursor=true;worker.step(state,5000,true);assert(!worker.active());state.cursor=false;
worker.start({owner,action::sort,"container_items"});worker.step(state,5000,false);assert(!worker.active());
worker.start({owner+8,action::sort,"container_items"});worker.step(state,5000,true);assert(!worker.active());
transfers=0;worker.start({owner,action::steal,""});
for(int i=0;i<10 && worker.active();++i){state.panels[0].items=stock;worker.step(state,6000+i*100,true);}
assert(transfers==4 && !worker.active());
stock={{"stone",64}};state.panels[0].items=stock;auto_place=&decline;transfers=0;
worker.start({owner,action::steal,""});worker.step(state,8000,true);worker.step(state,8200,true);worker.step(state,8800,true);
assert(transfers==1 && !worker.active() && stock[0].count==64);
// Real UI sequencing: pickup, cursor swap, place-back, each acknowledged on later ticks.
stock={{"stone",64},{"apple",12}};cursor={};refresh(state);defer_swap=true;swaps=0;
worker.start({owner,action::sort,"container_items"});worker.step(state,10000,true);
refresh(state);worker.step(state,10100,true);assert(swaps==2 && cursor.name=="apple");
refresh(state);worker.step(state,10200,true);assert(swaps==2 && worker.active());
std::swap(cursor,stock[pending_destination]);refresh(state);worker.step(state,10300,true);
assert(cursor.count==0);refresh(state);worker.step(state,10400,true);worker.step(state,10500,true);
assert(!worker.active() && stock[0].name=="apple" && stock[1].name=="stone");
// Rejected swaps return the picked-up stack instead of abandoning it.
stock={{"stone",64},{"apple",12}};cursor={};refresh(state);worker.start({owner,action::sort,"container_items"});
worker.step(state,12000,true);refresh(state);worker.step(state,12100,true);
refresh(state);worker.step(state,13700,true);assert(cursor.count==0);refresh(state);worker.step(state,13800,true);
assert(!worker.active() && stock[0].name=="stone" && stock[1].name=="apple");defer_swap=false;
// The reported failure: sorting must move all 10 arrows, not a half-stack of 5.
stock={{"stone",64},{"apple",7},{"arrow",10}};cursor={};refresh(state);swaps=0;
worker.start({owner,action::sort,"container_items"});worker.step(state,14000,true);
assert(cursor.name=="apple" && cursor.count==7 && stock[1].count==0);
bool whole_arrows_seen=false;
for(int i=1;i<20 && worker.active();++i) {
refresh(state);worker.step(state,14000+i*100,true);
if(cursor.name=="arrow"){assert(cursor.count==10);whole_arrows_seen=true;}
}
assert(whole_arrows_seen);
assert(!worker.active() && !cursor.count && stock[0].name=="apple" && stock[0].count==7 && stock[1].name=="arrow" && stock[1].count==10 && stock[2].count==64);
// Dump routes inventory and optional hotbar stacks through quick move.
stock={{"stone",64}};state.panels={{"container_items",{}},{"inventory_items",stock},{"hotbar_items",stock}};
auto_place=&transfer;transferred_groups.clear();include_hotbar=true;
worker.start({owner,action::dump,""});worker.step(state,11000,true);
state.panels[1].items=stock;worker.step(state,11100,true);
stock={{"apple",3}};state.panels[2].items=stock;worker.step(state,11200,true);
state.panels[2].items=stock;worker.step(state,11300,true);
assert(!worker.active() && cursor.count==0 && transferred_groups==std::vector<std::string>({"inventory_items","hotbar_items"}));include_hotbar=false;
// Filtered transfers use exact native item IDs and retain unrelated stacks.
stock={{"arrow",10},{"golden_apple",4},{"arrow",5},{"apple",3}};cursor={};
state.panels={{"container_items",stock},{"inventory_items",stock}};state.cursor=false;
transfers=0;transferred_groups.clear();worker.start({owner,action::steal,"","arrow"});
for(int i=0;i<12 && worker.active();++i){state.panels[0].items=stock;worker.step(state,16000+i*100,true);}
assert(!worker.active() && transfers==2 && stock[0].count==0 && stock[2].count==0 && stock[1].count==4 && stock[3].count==3);
transfers=0;worker.start({owner,action::steal,"","diamond"});
for(int i=0;i<12 && worker.active();++i){state.panels[0].items=stock;worker.step(state,18000+i*100,true);}
assert(!worker.active() && transfers==0 && stock[1].count==4);
stock={{"apple",3},{"golden_apple",4}};state.panels[1].items=stock;transfers=0;
worker.start({owner,action::dump,"","apple"});
for(int i=0;i<8 && worker.active();++i){state.panels[1].items=stock;worker.step(state,20000+i*100,true);}
assert(transfers==1 && stock[0].count==0 && stock[1].count==4);
snapshot picker_fixture;picker_fixture.panels={{"container_items",{{"arrow",10},{"arrow",5},{"apple",3}}},
{"inventory_items",{{"stone",64}}},{"hotbar_items",{{"arrow",20},{"stone",6}}}};
auto menu=choices(picker_fixture,action::steal,false);
assert(menu.size()==2 && menu[0].name=="apple" && menu[1].name=="arrow" && menu[1].count==15);
menu=choices(picker_fixture,action::dump,false);assert(menu.size()==1 && menu[0].name=="stone" && menu[0].count==64);
menu=choices(picker_fixture,action::dump,true);assert(menu.size()==2 && menu[0].name=="arrow" && menu[1].count==70);
worker.start({owner,action::dump,"","apple"});worker.cancel();assert(worker.filter.empty());
assert(next_minimum({},0)==-1);
// Additional controllers must not cancel an operation owned by a fresh chest.
auto old_tick=original_tick;auto old_current=current;auto old_published=latest();const bool old_enabled=enabled;
original_tick=&inventory_test::tick;enabled=true;state.controller=owner;state.stamp=GetTickCount64();
{std::lock_guard lock(mutex);published=state;}
current.start({owner,action::sort,"container_items"});
assert(tick_detour(owner+1)==7 && current.active() && latest().controller==owner);
current.cancel(engine::stop_reason::requested);assert(current.last_stop==engine::stop_reason::requested);
current=old_current;original_tick=old_tick;enabled=old_enabled;
{std::lock_guard lock(mutex);published=old_published;}
get_item=old_item;swap_cursor=old_swap;place_all=old_place;auto_place=old_auto;
std::cout<<"PASS: inventory transfer/sort scheduling, separate same-name stacks, empty slots, cursor/focus/controller cancellation\n";
}