Files
EscapistsReintroduced/tests/test_placement_diagnostics.py
T
2026-10-24 03:03:10 +02:00

68 lines
5.6 KiB
Python

"""Execute production placement rejection classification and bounded summaries."""
from policy_harness import ROOT, body, run
p=ROOT/'addons/core/Scripts/Game/ERR/00_Core/ERR_GeneratedSites.c'
methods='\n'.join(body(p,sig) for sig in ('protected void RecordRejection(', 'protected string RejectionSummary(', 'protected void ReportSearch(', 'protected void ReportProgress(', 'protected void ClearDiagnostics()'))
methods=methods.replace('protected ', '').replace('ERR_SiteDefinition site','Site* site').replace('site.','site->').replace('System.GetTickCount()', 'Clock::now').replace('int count;', 'int count=0;').replace('count.ToString()', 'std::to_string(count)').replace('foreach (string reason, int count : m_Rejections)', 'for (auto [reason,count] : m_Rejections)')
run(r'''
#include <cassert>
#include <string>
#include <map>
#include <vector>
struct string:std::string{using std::string::string;using std::string::operator=;string(std::string s):std::string(s){}bool IsEmpty(){return empty();}};
template<class K,class V>struct Map:std::map<K,V>{void Find(K k,V&out){auto i=this->find(k);if(i!=this->end())out=i->second;}void Set(K k,V v){(*this)[k]=v;}void Clear(){this->clear();}};
namespace Clock{int now=0;}
struct Site{string Id="radio-1",Label="Radio station";};
struct Report{string stage,id,label,summary;int attempts,elapsed;};std::vector<Report> reports;
void PrintFormat(string,string stage,string id,string label,int attempts,int elapsed,string summary){reports.push_back({stage,id,label,summary,attempts,elapsed});}
struct Diagnostics{Map<string,int>m_Rejections;int m_NextDiagnostic=0,m_Began=100,m_Attempts=123;
'''+methods+r'''
};
int main(){
Diagnostics d;Site site;assert(d.RejectionSummary()=="none recorded");
for(int i=0;i<10;i++){d.RecordRejection("water");}d.RecordRejection("uneven ground");d.RecordRejection("");
assert(d.m_Rejections.size()==3&&d.m_Rejections["water"]==10);
assert(d.RejectionSummary().find("water=10")!=string::npos&&d.RejectionSummary().find("uneven ground=1")!=string::npos&&d.RejectionSummary().find("unspecified terrain clearance=1")!=string::npos);
Clock::now=30099;d.ReportProgress(&site);assert(reports.empty());Clock::now=30100;d.ReportProgress(&site);assert(reports.size()==1);
assert(reports.back().stage=="still searching"&&reports.back().id==site.Id&&reports.back().label==site.Label&&reports.back().attempts==123&&reports.back().elapsed==30000);
for(int i=0;i<100;i++){d.ReportProgress(&site);}assert(reports.size()==1);
Clock::now=60099;d.ReportProgress(&site);assert(reports.size()==1);Clock::now=60100;d.ReportProgress(&site);assert(reports.size()==2);
d.ReportSearch(&site,"exhausted");assert(reports.size()==3&&reports.back().stage=="exhausted"&&reports.back().summary.find("water=10")!=string::npos);
d.ClearDiagnostics();assert(d.m_Rejections.empty()&&d.m_NextDiagnostic==0);
d.m_Began=Clock::now;d.ReportProgress(&site);assert(reports.size()==3);d.RecordRejection("obstructed footprint");assert(d.RejectionSummary()=="obstructed footprint=1");
}
''')
# Execute the actual water, elevation, flatness and physical-trace short circuits.
clear=body(p,'protected bool Reject(')+'\n'+body(p,'bool Clear(')
clear=clear.replace('protected ', '').replace('out float lowest','float& lowest').replace('float.MAX','FLT_MAX').replace('Math.','Math::').replace('TraceBox trace','TraceBox* trace').replace('trace.','trace->').replace('TraceFlags.','TraceFlags::').replace('null','nullptr').replace('ChimeraWorldUtils.','ChimeraWorldUtils::')
for a,b in [('"-1.42 0 -1.42"','Vector(-1.42,0,-1.42)'),('"1.42 0.1 1.42"','Vector(1.42,.1,1.42)')]:clear=clear.replace(a,b)
run(r'''
#include <cassert>
#include <cfloat>
#include <string>
#include <algorithm>
using string=std::string;
struct vector{float p[3];float operator[](int i){return p[i];}};vector Vector(float x,float y,float z){return {{x,y,z}};}vector operator+(vector a,vector b){return Vector(a[0]+b[0],a[1]+b[1],a[2]+b[2]);}
namespace Math{float Min(float a,float b){return std::min(a,b);}float Max(float a,float b){return std::max(a,b);}}
namespace TraceFlags{int ENTS=1,WORLD=2;}struct TraceBox{vector Start,End,Mins,Maxs;int Flags;};
struct World{bool water=false,blocked=false,slope=false;float height=42;int traces=0;float GetSurfaceY(float x,float){return height+(slope?x:0);}float TraceMove(TraceBox*,void*){traces++;return blocked?0:1;}};
struct Game{World world;World&GetWorld(){return world;}}game;Game&GetGame(){return game;}
namespace ChimeraWorldUtils{bool TryGetWaterSurfaceSimple(World&w,vector){return w.water;}}
struct Terrain{string m_Rejection;vector Rotate(vector p,float){return p;}
'''+clear+r'''
};
int main(){Terrain t;float floor;auto check=[&](){return t.Clear(Vector(0,0,0),0,10,.6,floor);};
game.world.water=true;assert(!check()&&t.m_Rejection=="water"&&game.world.traces==0);
game.world.water=false;game.world.height=.5;assert(!check()&&t.m_Rejection=="ground below minimum elevation");
game.world.height=42;game.world.slope=true;assert(!check()&&t.m_Rejection=="uneven ground"&&game.world.traces==0);
game.world.slope=false;game.world.blocked=true;assert(!check()&&t.m_Rejection=="obstructed footprint"&&game.world.traces==1);
game.world.blocked=false;assert(check()&&t.m_Rejection.empty()&&floor==42);
}
''')
# Both paths publish counts before resetting, and expose them in startup errors.
for filename in ('ERR_GeneratedSites.c','ERR_TerrainSites.c'):
source=(p.parent/filename).read_text()
assert 'ReportProgress(site);' in source
assert 'ReportSearch(site, "placed"); ClearDiagnostics();' in source
assert 'Checks: ' in source and 'RejectionSummary();' in source
print('PASS: actual terrain refusal reasons, counted summaries, site/attempt/time context, 30-second rate limit and per-site reset')