89 lines
7.2 KiB
Python
89 lines
7.2 KiB
Python
"""Production opening geometry and first-contact HQ order selection."""
|
|
import re
|
|
from policy_harness import ROOT, body, run
|
|
rules_path=ROOT/'addons/core/Scripts/Game/ERR/10_AI/ERR_HunterOpeningPlan.c'
|
|
rules='\n'.join(body(rules_path,x) for x in ('static bool Spread(', 'static bool FreshOrder(', 'static bool Select('))
|
|
rules=re.sub(r'\bvector (?=\w)', 'Vec ', rules).replace('vector.', 'Geometry::').replace('array<vector> candidates','Array<Vec> candidates').replace('array<int> selected','Array<int>& selected')
|
|
p=ROOT/'addons/core/Scripts/Game/ERR/10_AI/ERR_HunterService.c'
|
|
queue=body(p,'protected void QueueDestination(').replace('protected ','').replace('foreach (','for (')
|
|
queue=queue.replace('array<ref ERR_HQReport> reports = {};','Array<ERR_HQReport*> reports;')
|
|
for typ in ('ERR_HunterVehicle','ERR_HQ','ERR_HQReport','ERR_HQBelief','ERR_SiteRuntime'):
|
|
queue=re.sub(r'\b'+typ+r' (?=\w)', typ+'* ',queue)
|
|
for name in ('unit','hq','report','belief','best','m_Mode','site'):
|
|
queue=queue.replace(name+'.',name+'->')
|
|
queue=queue.replace('best->Confidence.ToString()', 'std::to_string(best->Confidence)')
|
|
queue=queue.replace('Memory().Get','Memory()->Get').replace('Sites().','Sites()->').replace('Definition.','Definition->')
|
|
queue=queue.replace('Waypoint.IsWithinCompletionRadius','Waypoint->IsWithinCompletionRadius').replace('Entity.GetOrigin','Entity->GetOrigin').replace('Group.GetFaction','Group->GetFaction')
|
|
queue=queue.replace('ERR_HQBelief* best;', 'ERR_HQBelief* best=nullptr;').replace('bool freshContact;', 'bool freshContact=false;').replace('bool found;', 'bool found=false;')
|
|
queue=queue.replace('vector destination, angles;', 'Vec destination, angles;')
|
|
for cls in ('ERR_HunterOpeningRules','ERR_SupplyConvoySystem','ERR_ObjectiveDefence','Math','vector'):
|
|
queue=queue.replace(cls+'.', ('Geometry' if cls=='vector' else cls)+'::')
|
|
queue=queue.replace('null','nullptr').replace('nullptrptr','nullptr')
|
|
run(r'''
|
|
#include <cassert>
|
|
#include <cmath>
|
|
#include <string>
|
|
#include <vector>
|
|
using string=std::string;
|
|
struct Vec{float x=0,y=0,z=0;float& operator[](int i){return i==0?x:i==1?y:z;}Vec operator-(Vec b){return {x-b.x,y-b.y,z-b.z};}string ToString(){return "position";}};
|
|
namespace Geometry{float DistanceXZ(Vec a,Vec b){return std::hypot(a.x-b.x,a.z-b.z);}float Dot(Vec a,Vec b){return a.x*b.x+a.y*b.y+a.z*b.z;}}
|
|
template<class T>struct Array:std::vector<T>{using std::vector<T>::vector;int Count(){return this->size();}void Insert(T v){this->push_back(v);}void Clear(){this->clear();}};
|
|
struct ERR_HunterOpeningRules{
|
|
'''+rules+r'''
|
|
};
|
|
struct Group{int GetFaction(){return 1;}};
|
|
struct Entity{Vec GetOrigin(){return {};}};
|
|
struct Waypoint{bool complete=false;bool IsWithinCompletionRadius(Group*){return complete;}};
|
|
struct Route{};
|
|
struct ERR_HunterVehicle{float NextOrder=99999,LastContact=-1,NextContactOrder=0;Route* Downlink=nullptr;int SourceMessage=0;Vec Destination,ContactPosition,DeliveredContactPosition;bool DeliveredContact=false;string Source="hunter";struct Group* Group=nullptr;struct Entity* Entity=nullptr;struct Waypoint* Waypoint=nullptr;};
|
|
struct ERR_HQReport{int Target=1;};
|
|
struct ERR_HQBelief{bool actionable=true;float ObservedAt=10,Confidence=1;int MessageId=7;Vec PredictedPosition={1000,0,2000};bool Actionable(){return actionable;}};
|
|
struct Memory{ERR_HQBelief belief;ERR_HQBelief* Get(int){return &belief;}};
|
|
struct ERR_HQ{bool contact=false,connected=true;int requests=0;struct Memory memory;Route route;void SupportContacts(Array<ERR_HQReport*>& out,float){static ERR_HQReport report;if(contact)out.Insert(&report);}struct Memory* Memory(){return &memory;}Route* Downlink(Group*,string,string,int){requests++;return connected?&route:nullptr;}};
|
|
struct Definition{int Kind=1;Vec Position;};struct ERR_SiteRuntime{struct Definition* Definition=nullptr;struct Group* Group=nullptr;};
|
|
struct Sites{int Count(){return 0;}ERR_SiteRuntime* At(int){return nullptr;}};
|
|
struct Mode{struct Sites sites;struct Sites* Sites(){return &sites;}int EnemyFaction(){return 1;}};
|
|
namespace Math{int RandomInt(int,int){return 0;}}
|
|
namespace ERR_ObjectiveDefence{bool Objective(int){return true;}}
|
|
namespace ERR_SupplyConvoySystem{bool available=true;int calls=0;bool ApproachDirected(Vec p,Vec& dst,Vec& angles,float radius=500){calls++;dst=p;return available;}}
|
|
struct Service{Mode mode;Mode* m_Mode=&mode;
|
|
'''+queue+r'''
|
|
};
|
|
Vec polar(float degrees,float radius=1000){return {std::sin(degrees*3.14159265f/180)*radius,0,std::cos(degrees*3.14159265f/180)*radius};}
|
|
int main(){
|
|
Array<int> chosen;Vec center;
|
|
assert(ERR_HunterOpeningRules::Select(center,{polar(0),polar(120),polar(240)},chosen)&&chosen.Count()==3);
|
|
assert(!ERR_HunterOpeningRules::Select(center,{polar(0),polar(5),polar(10)},chosen)&&chosen.empty());
|
|
assert(!ERR_HunterOpeningRules::Spread(center,polar(359),polar(1)));
|
|
assert(!ERR_HunterOpeningRules::Spread(center,polar(0,599),polar(180)));
|
|
Array<Vec> tooMany(65,polar(0));assert(!ERR_HunterOpeningRules::Select(center,tooMany,chosen));
|
|
assert(!ERR_HunterOpeningRules::FreshOrder(9,10,100,0,false));
|
|
assert(!ERR_HunterOpeningRules::FreshOrder(10,10,100,0,false));
|
|
assert(!ERR_HunterOpeningRules::FreshOrder(11,10,100,101,false));
|
|
assert(!ERR_HunterOpeningRules::FreshOrder(11,10,100,0,true));
|
|
Service service;ERR_HQ hq;ERR_HunterVehicle unit;Route patrol;unit.Downlink=&patrol;
|
|
service.QueueDestination(&unit,&hq,100);assert(unit.Downlink==&patrol&&hq.requests==0); // no magical position
|
|
hq.contact=true;service.QueueDestination(&unit,&hq,100);
|
|
assert(unit.Downlink==&hq.route&&hq.requests==1&&unit.SourceMessage==7&&unit.LastContact==10);
|
|
assert(unit.Destination.x==1000&&unit.Destination.z==2000); // delivered belief only, before patrol cooldown
|
|
hq.memory.belief.ObservedAt=11;service.QueueDestination(&unit,&hq,6000);
|
|
assert(hq.requests==1); // in-flight interception isn't starved by newer observations
|
|
unit.Downlink=nullptr;service.QueueDestination(&unit,&hq,6000);assert(hq.requests==2&&unit.LastContact==11);
|
|
unit.Downlink=nullptr;service.QueueDestination(&unit,&hq,7000);assert(hq.requests==2); // no repeated order for same report
|
|
ERR_HunterVehicle disconnected;hq.connected=false;service.QueueDestination(&disconnected,&hq,10000);
|
|
assert(!disconnected.Downlink&&disconnected.LastContact==-1);
|
|
// Repeated/new reports in an already delivered area must not churn routing.
|
|
Waypoint route;unit.Waypoint=&route;unit.DeliveredContact=true;unit.DeliveredContactPosition={1000,0,2000};
|
|
unit.Downlink=nullptr;unit.NextOrder=0;unit.NextContactOrder=0;
|
|
int kept=hq.requests;int projections=ERR_SupplyConvoySystem::calls;
|
|
hq.memory.belief.ObservedAt=12;service.QueueDestination(&unit,&hq,12000);
|
|
assert(hq.requests==kept&&ERR_SupplyConvoySystem::calls==projections&&unit.LastContact==12);
|
|
hq.memory.belief.PredictedPosition.x=1100;hq.memory.belief.ObservedAt=13;
|
|
service.QueueDestination(&unit,&hq,18000);assert(hq.requests==kept+1); // Material displacement redirects.
|
|
hq.memory.belief.ObservedAt=11;
|
|
int requests=hq.requests;service.QueueDestination(&disconnected,&hq,11000);assert(hq.requests==requests);
|
|
hq.connected=true;service.QueueDestination(&disconnected,&hq,15000);assert(disconnected.Downlink&&disconnected.LastContact==11);
|
|
}
|
|
''')
|
|
print('PASS: real opening road geometry enforces wide azimuths; fresh delivered contact preempts patrol cooldown, respects radio failures and does not starve in-flight interception')
|