91 lines
7.4 KiB
Python
91 lines
7.4 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 actual terrain-height water queries, low dry land and physical checks.
|
|
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::').replace('ERR_ForceRules.','ERR_ForceRules::')
|
|
dry=body(p.parent/'ERR_Actors.c','static bool DryGround(').replace('ERR_ForceRules.','ERR_ForceRules::').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 <cmath>
|
|
#include <limits>
|
|
#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,puddle=false,blocked=false,slope=false;float height=42,waterLevel=43;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 ERR_ForceRules{bool Finite(float n){return std::isfinite(n);}}
|
|
namespace ChimeraWorldUtils{bool TryGetWaterSurfaceSimple(World&w,vector p){return (w.water||(w.puddle&&p[0]==2&&p[2]==2))&&p[1]<w.waterLevel;}}
|
|
namespace ERR_Actors{'''+dry+r'''}
|
|
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);
|
|
// Placeholder Y=0 must not make high dry land look submerged.
|
|
game.world.waterLevel=0;assert(check()&&t.m_Rejection.empty());
|
|
game.world.water=false;
|
|
for(float h:{.5f,0.f,-25.f}){game.world.height=h;assert(check()&&floor==h&&ERR_Actors::DryGround(Vector(0,999,0)));}
|
|
// A terrain-relative ocean/lake still rejects both high and low submerged ground.
|
|
for(float h:{42.f,-25.f,.5f}){game.world.height=h;game.world.waterLevel=h+.05f;game.world.water=true;assert(!check()&&t.m_Rejection=="water"&&!ERR_Actors::DryGround(Vector(0,999,0)));}
|
|
game.world.water=false;game.world.height=42;game.world.waterLevel=43;game.world.puddle=true;
|
|
assert(!check()&&t.m_Rejection=="water");game.world.puddle=false; // Water between the old nine sample points.
|
|
game.world.height=std::numeric_limits<float>::quiet_NaN();assert(!check()&&t.m_Rejection=="invalid terrain height"&&!ERR_Actors::DryGround(Vector(0,0,0)));
|
|
game.world.traces=0;
|
|
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')
|
|
|
|
# Every downstream startup gate uses actual dry terrain rather than map altitude.
|
|
for filename in ('ERR_SiteService.c','ERR_SpawnPlacement.c','ERR_AirfieldPlacement.c'):
|
|
source=(p.parent/filename).read_text()
|
|
assert 'ERR_Actors.DryGround(' in source
|
|
assert '[1] < 1' not in source and '[1] < 2' not in source
|
|
source=(p.parent.parent/'10_AI/ERR_MortarSystem.c').read_text()
|
|
assert 'ERR_Actors.DryGround(position)' in source and 'position[1] < 1' not in source
|
|
print('PASS: actual low/negative dry terrain, height-correct shallow/lake/ocean/interior water refusal and downstream startup dry-ground gates')
|