Files
Viano-Core/tests/clientless/test_adventure_area_segment.py
T

69 lines
3.1 KiB
Python

"""Run the production segment probe with terrain and player doubles."""
from pathlib import Path
import subprocess
import tempfile
root = Path(__file__).resolve().parents[2]
source = (root / 'src/server/game/World/AdventureAreaMgr.cpp').read_text()
start = source.index('bool AdventureAreaMgr::AllowsSegment(')
end = source.index('bool AdventureAreaMgr::AllowsEdge(', start)
code = r'''
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <unordered_map>
#include <vector>
using uint32=unsigned;
struct Position {float x=0,y=0,z=0;
float GetPositionX()const{return x;}float GetPositionY()const{return y;}float GetPositionZ()const{return z;}
float GetExactDist(Position const* p)const{return std::hypot(x-p->x,y-p->y,z-p->z);}
void Relocate(float a,float b,float c){x=a;y=b;z=c;}
};
struct Phase{std::map<unsigned,int> swaps;auto const& GetVisibleMapIds()const{return swaps;}};
struct Player{bool bot=true,denied=false;Phase phase;auto const& GetPhaseShift()const{return phase;}};
class AdventureAreaMgr {
public:
struct SegmentProbe {unsigned NextSample=0;std::vector<uint32> Areas;};
std::unordered_map<std::string,SegmentProbe> _segments;
unsigned probes=0;bool closed=false;
bool Applies(Player const* p){return p&&p->bot;}
bool EnsureLoaded(){return true;}
bool AllowsArea(Player const* p,unsigned area){return area!=2 || (!closed&&!p->denied);}
unsigned AreaAt(Player const*,unsigned,Position const& p){++probes;return p.x>=40&&p.x<=80?2:1;}
bool AllowsSegment(Player const*,unsigned,Position const&,Position const&);
};
'''+source[start:end]+r'''
int main(){
AdventureAreaMgr mgr;Player p;Position a,b;b.x=120;
assert(mgr.AllowsSegment(&p,0,a,b));unsigned first=mgr.probes;
assert(first==4);
assert(mgr.AllowsSegment(&p,0,a,b)&&mgr.probes==first);
// Same static geography, different faction/level permission.
Player forbidden;forbidden.denied=true;
assert(!mgr.AllowsSegment(&forbidden,0,a,b)&&mgr.probes==first);
// Reload changes permissions without invalidating the geometry cache.
mgr.closed=true;assert(!mgr.AllowsSegment(&p,0,a,b)&&mgr.probes==first);
mgr.closed=false;assert(mgr.AllowsSegment(&p,0,a,b));
// A denied first probe stops early; another bot can finish the same probe.
mgr._segments.clear();mgr.probes=0;
assert(!mgr.AllowsSegment(&forbidden,0,a,b)&&mgr.probes==2);
assert(mgr.AllowsSegment(&p,0,a,b)&&mgr.probes==4);
// A new terrain swap and a different map cannot reuse the old probe.
p.phase.swaps.emplace(123,0);assert(mgr.AllowsSegment(&p,0,a,b)&&mgr.probes==8);
assert(mgr.AllowsSegment(&p,1,a,b)&&mgr.probes==12);
b.x=300000;assert(!mgr.AllowsSegment(&p,0,a,b));
p.bot=false;assert(mgr.AllowsSegment(&p,0,a,b));
std::cout<<"Segment geography cache, faction isolation, reload permissions, lazy completion and terrain swaps passed.\n";
}
'''
with tempfile.TemporaryDirectory(prefix='vcore-area-segment-') as folder:
cpp = Path(folder) / 'test.cpp'
binary = Path(folder) / 'test'
cpp.write_text(code)
subprocess.run(['c++', '-std=c++17', str(cpp), '-o', str(binary)], check=True)
subprocess.run([str(binary)], check=True)