"""Actual finite mine transaction/callback and shared reservation; native boundaries doubled.""" from pathlib import Path import os,re from policy_harness import ROOT,body,run P=Path(os.environ.get('ERR_MINE_TRANSFER_SOURCE',ROOT/'addons/core/Scripts/Game/ERR/10_AI/ERR_InfantryMineTransfer.c')) s=P.read_text() for forbidden in ('GetItem(', 'TrySpawn', 'SetAmmoCount', 'ActivateTrigger(', 'DeleteEntity', 'SetOrigin'): assert forbidden not in s assert s.count('TryMoveItemToStorage(')==1 assert s.index('m_Reservation.Commit()')','Array').replace('Array attempted','Array* attempted') for field in ('m_Original','m_Spent'): s=s.replace('Array '+field,'Array* '+field).replace(field+'.',field+'->') s=s.replace('Array original, Array spent','Array* original, Array* spent') for ns in ('Replication','SCR_Global','ERR_GameMode','ERR_Actors','ERR_EnemyEquipmentRules','ERR_ConvoyRules','SCR_FactionManager','CompartmentAccessComponent','ERR_InfantryAmmoTransfer','ERR_InfantryAmmoAccess','ERR_ForceRules','ERR_EnemyLoadoutCatalog','ERR_InfantryAmmoTransferState','EStoragePurpose'): s=s.replace(ns+'.',ns+'::') for var in ('hq','m_Mine','exactMine','mode','donorVehicle','recipientActor','originalRoster','usage','item','actor','group','inventory','carried','trigger','m_Mode','m_Donor','m_DonorGroup','m_Recipient','m_RecipientGroup','m_Roster','m_DonorRecord','m_RecipientRecord','m_Source','m_Destination','m_Reservation','m_Callback','m_FromSlot','m_FromStorage','m_Storage','slot','storage','utility','physics','Transfer','entity'): s=re.sub(r'\b'+var+r'\.',var+'->',s) for call in ('Forces()','GetPilotCompartmentSlot()','GetPrefabData()','GetParentSlot()'): s=s.replace(call+'.',call+'->') s=s.replace('m_VehicleMgr.','m_VehicleMgr->') s=re.sub(r'\b(bool|int|IEntity) (\w+);',r'\1 \2{};',s) s=re.sub(r'^( \w+\*) (\w+);',r'\1 \2=nullptr;',s,flags=re.M) return s.replace('\n}\n','\n};\n') run(r''' #include #include #include #include #include #include using string=std::string; templatestruct Array:std::vector{using std::vector::vector;int Count(){return this->size();}void Insert(T v){this->push_back(v);}bool Contains(T v){return std::find(this->begin(),this->end(),v)!=this->end();}void RemoveItem(T v){auto i=std::find(this->begin(),this->end(),v);if(i!=this->end())this->erase(i);}}; struct Entity;struct Group;using IEntity=Entity*;using Vehicle=Entity*;using SCR_AIGroup=Group*; struct Vector{float n=0;float Length(){return n;}}; struct Physics{Vector speed,angular;Vector GetVelocity(){return speed;}Vector GetAngularVelocity(){return angular;}}; struct BaseInventoryStorageComponent{IEntity owner=nullptr;IEntity GetOwner(){return owner;}}; struct InventoryStorageSlot{IEntity attached=nullptr;BaseInventoryStorageComponent*storage=nullptr;IEntity GetAttachedEntity(){return attached;}BaseInventoryStorageComponent*GetStorage(){return storage;}}; struct InventoryItemComponent{InventoryStorageSlot*slot=nullptr;InventoryStorageSlot*GetParentSlot(){return slot;}}; struct SCR_PressureTriggerComponent{bool active=false,initial=false;bool IsActivated(){return active;}bool IsActivatedByDefault(){return initial;}}; struct Prefab{string name="PMN4";string GetPrefabName(){return name;}}; struct Seat{IEntity occupant=nullptr;IEntity GetOccupant(){return occupant;}}; struct SCR_AIVehicleUsageComponent{Seat*seat=nullptr;Seat*GetPilotCompartmentSlot(){return seat;}}; struct InventoryStorageManagerComponent; struct Entity{InventoryStorageManagerComponent*inventory=nullptr;InventoryItemComponent*item=nullptr;SCR_PressureTriggerComponent*trigger=nullptr;SCR_AIVehicleUsageComponent*usage=nullptr;Prefab*prefab=nullptr;Physics*physics=nullptr;IEntity vehicle=nullptr,parent=nullptr;SCR_AIGroup group=nullptr;int faction=1;bool alive=true,fit=true,player=false,collision=true,damage=true; Prefab*GetPrefabData(){return prefab;}Physics*GetPhysics(){return physics;}IEntity GetParent(){return parent;}}; struct VehicleManager{bool found=true;bool FindVehicle(IEntity){return found;}}; struct SCR_AIGroupUtilityComponent{bool military=true;VehicleManager*m_VehicleMgr=nullptr;bool IsMilitary(){return military;}}; struct Group{bool slave=false;int players=0,faction=1;SCR_AIGroupUtilityComponent*utility=nullptr;bool IsSlave(){return slave;}int GetPlayerCount(bool){return players;}int GetFaction(){return faction;}SCR_AIGroupUtilityComponent*GetGroupUtilityComponent(){return utility;}}; struct ERR_ForceRecord{SCR_AIGroup Group=nullptr;bool Physical=true,Eliminated=false;}; struct Ledger{SCR_AIGroup donor=nullptr,recipient=nullptr;ERR_ForceRecord*d=nullptr,*r=nullptr;ERR_ForceRecord*FindGroup(SCR_AIGroup g){return g==donor?d:g==recipient?r:nullptr;}}; struct ERR_HQ{bool designated=true;bool MineSupplyReady(SCR_AIGroup,IEntity,bool replacement=false){return !replacement||designated;}}; struct ERR_GameMode{ERR_HQ hq;bool command=true;ERR_HQ*Command(){return command?&hq:nullptr;}inline static ERR_GameMode*current=nullptr;bool active=true;Ledger*ledger=nullptr;static ERR_GameMode*Get(){return current;}bool Active(){return active;}Ledger*Forces(){return ledger;}int EnemyFaction(){return 1;}}; struct ERR_HQStatusSampler{SCR_AIGroup group=nullptr;IEntity actor=nullptr;bool valid=true;int strength=1;bool OwnsRoster(SCR_AIGroup g){return g==group;}int InitialStrength(){return strength;}IEntity InitialMember(int i){return i==0?actor:nullptr;}bool CanAttribute(int i,IEntity a,bool){return valid&&i==0&&a==actor;}}; struct Replication{inline static bool server=true;static bool IsServer(){return server;}}; struct SCR_Global{inline static bool edit=false;static bool IsEditMode(){return edit;}}; struct ERR_Actors{static bool Alive(IEntity e){return e&&e->damage&&e->alive;}static bool CanFight(IEntity e){return Alive(e)&&e->fit;}static bool Player(IEntity e){return e&&e->player;}static SCR_AIGroup Group(IEntity e){return e?e->group:nullptr;}}; struct ERR_EnemyEquipmentRules{static bool Eligible(ERR_GameMode*,SCR_AIGroup g,IEntity e){return g&&!g->slave&&!g->players&&g->faction==1&&ERR_Actors::CanFight(e)&&!e->player&&e->group==g;}}; struct SCR_FactionManager{static int SGetFaction(IEntity e){return e?e->faction:0;}}; struct CompartmentAccessComponent{static IEntity GetVehicleIn(IEntity e){return e?e->vehicle:nullptr;}}; struct ERR_ConvoyRules{inline static bool captured=false;static bool Captured(IEntity,SCR_AIGroup){return captured;}static bool PartOf(IEntity e,IEntity root){return e&&e==root;}}; struct ERR_InfantryAmmoAccess{inline static bool clear=true;static bool Clear(IEntity,IEntity){return clear;}}; struct ERR_ForceRules{static bool Finite(float n){return std::isfinite(n);}}; struct ERR_EnemyLoadoutCatalog{inline static string MINE="PMN4";}; struct EStoragePurpose{static const int PURPOSE_DEPOSIT=1;}; enum ERR_InfantryAmmoTransferState{FAILED=-1,PENDING,SUCCEEDED}; struct ScriptedInventoryOperationCallback{virtual ~ScriptedInventoryOperationCallback()=default;virtual void OnComplete(){}virtual void OnAllComplete(){}virtual void OnFailed(){}virtual void OnAllFailed(){}}; struct InventoryStorageManagerComponent{IEntity owner=nullptr;Arrayitems;Arraystorages;BaseInventoryStorageComponent*destination=nullptr;bool capacity=true,accept=true;int calls=0;ScriptedInventoryOperationCallback*last=nullptr;std::functioneffect; IEntity GetOwner(){return owner;}void GetItems(Array&a,int=0){a=items;}void GetStorages(Array&a,int){a=storages;}BaseInventoryStorageComponent*FindStorageForItem(IEntity,int){return destination;} bool CanMoveItemToStorage(IEntity,BaseInventoryStorageComponent*,int){return capacity;} bool TryMoveItemToStorage(IEntity,BaseInventoryStorageComponent*,int,ScriptedInventoryOperationCallback*cb){calls++;last=cb;if(effect)effect();return accept;}}; struct ERR_InfantryAmmoTransfer{ '''+cpp(stored)+r''' }; '''+cpp(reservation)+r''' struct ERR_InfantryMineTransfer; struct ERR_InfantryMineCallback : ScriptedInventoryOperationCallback{ERR_InfantryMineTransfer*Transfer=nullptr;void OnComplete();void OnAllComplete();void OnFailed();void OnAllFailed();}; '''+cpp(s[s.index('class ERR_InfantryMineTransfer\n'):])+r''' '''+''.join(cpp(body(P,'protected override void '+method+'(')).replace('void '+method+'(', 'void ERR_InfantryMineCallback::'+method+'(') for method in ('OnComplete','OnAllComplete','OnFailed','OnAllFailed'))+r''' struct Fixture{ERR_GameMode mode;Ledger ledger;Group donorGroup,recipientGroup;ERR_ForceRecord donorRecord,recipientRecord;Entity truck,driver,actor,incoming,original,extra;InventoryStorageManagerComponent source,destination;BaseInventoryStorageComponent sourceStorage,destStorage;InventoryStorageSlot from,to,ownSlot,extraSlot;InventoryItemComponent inItem,ownItem,extraItem;Prefab prefab;SCR_PressureTriggerComponent inTrigger,ownTrigger,extraTrigger;SCR_AIVehicleUsageComponent usage;Seat seat;Physics physics;VehicleManager vehicleMgr;SCR_AIGroupUtilityComponent utility;ERR_HQStatusSampler roster;Arraystock,spent;ERR_FiniteMagazineReservation reservation; Fixture():reservation(&stock,&spent,&incoming){Replication::server=true;SCR_Global::edit=false;ERR_ConvoyRules::captured=false;ERR_InfantryAmmoAccess::clear=true;ERR_GameMode::current=&mode;mode.ledger=&ledger; ledger.donor=&donorGroup;ledger.recipient=&recipientGroup;ledger.d=&donorRecord;ledger.r=&recipientRecord;donorRecord.Group=&donorGroup;recipientRecord.Group=&recipientGroup; truck.inventory=&source;actor.inventory=&destination;truck.physics=&physics;truck.usage=&usage;usage.seat=&seat;seat.occupant=&driver;driver.vehicle=&truck;driver.group=&donorGroup;actor.group=&recipientGroup;donorGroup.utility=&utility;utility.m_VehicleMgr=&vehicleMgr; source.owner=&truck;destination.owner=&actor;sourceStorage.owner=&truck;destStorage.owner=&actor;source.storages={&sourceStorage};destination.storages={&destStorage};destination.destination=&destStorage; from.storage=&sourceStorage;from.attached=&incoming;to.storage=&destStorage;ownSlot.storage=&destStorage;ownSlot.attached=&original;extraSlot.storage=&destStorage;extraSlot.attached=&extra; incoming.prefab=&prefab;incoming.trigger=&inTrigger;incoming.item=&inItem;inItem.slot=&from;original.prefab=&prefab;original.trigger=&ownTrigger;original.item=&ownItem;ownItem.slot=&ownSlot;extra.prefab=&prefab;extra.trigger=&extraTrigger;extra.item=&extraItem;extraItem.slot=&extraSlot; source.items={&incoming};destination.items={&original};roster.group=&recipientGroup;roster.actor=&actor;stock={&incoming};assert(reservation.Reserve());} ERR_InfantryMineTransfer Job(bool empty=false){return ERR_InfantryMineTransfer(&mode,&truck,&donorGroup,&actor,&recipientGroup,&incoming,&reservation,&roster,0,empty);} void Move(){source.items.clear();destination.items.Insert(&incoming);from.attached=nullptr;to.attached=&incoming;inItem.slot=&to;} }; int main(){ // Inline native completion cannot resurrect a cancelled transfer or bypass // original authority, in either zero replacement or one-mine top-up mode. for(int empty=0;empty<2;empty++)for(int loss=0;loss<5;loss++){ Fixture f;if(empty){f.destination.items.clear();}auto t=f.Job(empty); f.source.effect=[&]{if(loss==0)t.Stop();if(loss==1)f.mode.active=false;if(loss==2)f.roster.valid=false;if(loss==3)ERR_ConvoyRules::captured=true;if(loss==4){if(empty)f.mode.hq.designated=false;else f.destination.items.clear();}f.Move();f.source.last->OnComplete();}; assert(t.Tick(0)==FAILED&&t.Submitted()&&!t.Unresolved()&&f.source.calls==1); assert(t.Tick(1)==FAILED&&f.source.calls==1&&f.spent.Count()==1&&!f.reservation.ReleaseUnsubmitted());} // A synchronous success callback without moved custody remains quarantined // after cancellation; late proof settles it without resuming or refunding. {Fixture f;auto t=f.Job();f.source.effect=[&]{t.Stop();f.source.last->OnComplete();}; assert(t.Tick(0)==FAILED&&t.Unresolved()&&f.source.calls==1);f.Move(); assert(!t.Unresolved()&&t.Tick(1)==FAILED&&f.source.calls==1&&f.spent.Count()==1&&!f.reservation.ReleaseUnsubmitted());} // Authoritative designation plus an inspected empty inventory permits exactly one finite replacement. {Fixture f;f.destination.items.clear();auto t=f.Job(true);assert(t.Tick(0)==PENDING&&t.Submitted());f.Move();f.source.last->OnComplete();assert(t.Tick(1)==SUCCEEDED&&f.destination.items.Count()==1&&f.source.calls==1);} for(int k=0;k<7;k++){Fixture f;f.destination.items.clear(); if(k==0)f.mode.hq.designated=false;if(k==1)f.destination.items.Insert(nullptr);if(k==2)f.destination.items.Insert(&f.original);if(k==3)f.destination.owner=&f.extra;if(k==4)f.destination.items.resize(129,&f.original);if(k==5){f.original.damage=false;f.destination.items.Insert(&f.original);}if(k==6)f.mode.command=false; auto t=f.Job(true);assert(t.Tick(0)==FAILED&&!t.Submitted()&&f.source.calls==0);} // Empty/one mode never switches after dispatch; a foreign new mine or revoked entitlement aborts and retains uncertainty. for(int k=0;k<2;k++){Fixture f;f.destination.items.clear();auto t=f.Job(true);t.Tick(0);if(k==0)f.destination.items.Insert(&f.original);if(k==1){f.mode.hq.designated=false;}assert(t.Tick(1)==FAILED&&t.Unresolved()&&f.source.calls==1);f.Move();f.source.last->OnComplete();assert(!t.Unresolved()&&t.Tick(2)==FAILED);} {Fixture f;f.destination.items.clear();auto t=f.Job(true);t.Tick(0);t.Stop();f.Move();f.source.last->OnComplete();assert(!t.Unresolved()&&t.Tick(1)==FAILED&&f.source.calls==1&&!f.reservation.ReleaseUnsubmitted());} {Fixture f;auto t=f.Job();assert(t.Tick(0)==PENDING&&t.Submitted()&&t.Unresolved()&&f.source.calls==1);assert(f.reservation.CommittedFor(&f.incoming)&&!f.reservation.ReleaseUnsubmitted()); f.source.last->OnComplete();assert(t.Tick(1)==PENDING&&t.Unresolved());f.Move();assert(t.Tick(2)==SUCCEEDED&&!t.Unresolved());assert(f.destination.items.Count()==2);t.Tick(3);assert(f.source.calls==1);} // Partially constructed contexts never bind later or reach native mutation. for(int k=0;k<10;k++){Fixture f; if(k==4)f.mode.ledger=nullptr;if(k==5)f.ledger.d=nullptr;if(k==6)f.ledger.r=nullptr;if(k==7)f.seat.occupant=nullptr; ERR_InfantryMineTransfer t(k==0?nullptr:&f.mode,k==1?nullptr:&f.truck,&f.donorGroup,k==2?nullptr:&f.actor,&f.recipientGroup,k==8?nullptr:&f.incoming,&f.reservation,k==3?nullptr:&f.roster,k==9?1:0); assert(t.Tick(0)==FAILED&&!t.Submitted()&&f.source.calls==0);} // No entitlement for zero/two carriers, uncertain inventory, or replaced original mine. for(int k=0;k<8;k++){Fixture f;if(k==0)f.destination.items.clear();if(k==1)f.destination.items.Insert(&f.extra);if(k==2)f.ownTrigger.active=true;if(k==3)f.ownTrigger.initial=true;if(k==4)f.ownItem.slot=nullptr;if(k==5)f.ownSlot.attached=&f.extra;if(k==6)f.destination.items.Insert(nullptr);if(k==7)f.destination.items.resize(129,&f.original); auto t=f.Job();assert(t.Tick(0)==FAILED&&!t.Submitted()&&f.source.calls==0&&f.reservation.ReleaseUnsubmitted());} {Fixture f;auto t=f.Job();f.destination.items={&f.extra};assert(t.Tick(0)==FAILED&&f.source.calls==0);} {Fixture f;f.extra.damage=false;f.destination.items.Insert(&f.extra);assert(!ERR_InfantryMineTransfer::Carrier(&f.actor,&f.recipientGroup));auto t=f.Job();assert(t.Tick(0)==FAILED&&f.source.calls==0);} // Duplicates in native enumeration are one item, not a second entitlement. {Fixture f;f.destination.items.Insert(&f.original);assert(ERR_InfantryMineTransfer::Carrier(&f.actor,&f.recipientGroup)==&f.original);} // Atomic shared claim before native request, including synchronous callback and false return. {Fixture f;f.source.accept=false;f.source.effect=[&]{assert(f.reservation.CommittedFor(&f.incoming)&&!f.reservation.ReleaseUnsubmitted());f.Move();f.source.last->OnComplete();};auto t=f.Job();assert(t.Tick(0)==SUCCEEDED&&!t.Unresolved()&&f.source.calls==1);} {Fixture f;ERR_FiniteMagazineReservation rival(&f.stock,&f.spent,&f.incoming);assert(!rival.Reserve());auto t=f.Job();assert(t.Tick(0)==PENDING&&f.spent.Count()==1);} {Fixture f;f.reservation.ReleaseUnsubmitted();auto t=f.Job();assert(t.Tick(0)==FAILED&&!t.Submitted()&&f.source.calls==0);} {Fixture f;f.source.accept=false;auto t=f.Job();assert(t.Tick(0)==FAILED&&t.Submitted()&&t.Unresolved());f.source.last->OnFailed();assert(!t.Unresolved()&&!f.reservation.ReleaseUnsubmitted());t.Tick(1);assert(f.source.calls==1);} // Failure cannot release partial movement; late success is not permission to retry. {Fixture f;auto t=f.Job();t.Tick(0);f.inItem.slot=&f.to;f.source.last->OnFailed();assert(t.Tick(1)==FAILED&&t.Unresolved());f.source.last->OnComplete();assert(t.Unresolved()&&f.source.calls==1);} {Fixture f;auto t=f.Job();t.Tick(0);t.Stop();f.mode.active=false;f.Move();f.source.last->OnComplete();assert(!t.Unresolved()&&t.Tick(1)==FAILED&&f.source.calls==1);} {Fixture f;auto t=f.Job();t.Tick(0);assert(t.Tick(8000)==FAILED&&t.Unresolved());f.Move();f.source.last->OnComplete();assert(!t.Unresolved()&&t.Tick(8001)==FAILED&&f.source.calls==1);} {Fixture f;auto t=f.Job();t.Tick(10);assert(t.Tick(9)==FAILED&&t.Unresolved()&&f.source.calls==1);} // Only deletion, not damage/capture, settles an unknown one-item movement. {Fixture f;auto t=f.Job();t.Tick(0);t.m_Mine=nullptr;f.reservation.m_Item=nullptr;f.stock[0]=nullptr;f.spent[0]=nullptr;assert(t.Tick(1)==FAILED&&!t.Unresolved());f.source.last->OnComplete();assert(t.Tick(2)==FAILED&&!f.reservation.ReleaseUnsubmitted()&&f.spent.Count()==1);} for(int k=0;k<3;k++){Fixture f;auto t=f.Job();t.Tick(0);if(k==0)f.incoming.alive=false;if(k==1)f.actor.player=true;if(k==2){ERR_ConvoyRules::captured=true;}assert(t.Tick(1)==FAILED&&t.Unresolved()&&f.source.calls==1);} // Original actor/ledger/driver/managers/capacity/reach/stock all remain required. for(int k=0;k<25;k++){Fixture f;auto t=f.Job();ERR_ForceRecord replacement;InventoryStorageManagerComponent changed; switch(k){case 0:Replication::server=false;break;case 1:SCR_Global::edit=true;break;case 2:f.mode.active=false;break;case 3:ERR_GameMode::current=nullptr;break;case 4:f.roster.valid=false;break;case 5:f.roster.actor=&f.extra;break;case 6:f.roster.strength=2;break;case 7:f.actor.fit=false;break;case 8:f.actor.player=true;break;case 9:f.actor.vehicle=&f.truck;break;case 10:f.ledger.r=&replacement;break;case 11:f.donorRecord.Eliminated=true;break;case 12:f.vehicleMgr.found=false;break;case 13:f.seat.occupant=&f.extra;break;case 14:f.truck.inventory=&changed;break;case 15:f.actor.inventory=&changed;break;case 16:f.physics.speed.n=1;break;case 17:f.physics.angular.n=1;break;case 18:ERR_InfantryAmmoAccess::clear=false;break;case 19:f.source.capacity=false;break;case 20:f.destination.destination=nullptr;break;case 21:f.inTrigger.active=true;break;case 22:f.from.attached=&f.extra;break;case 23:f.source.items.clear();break;case 24:f.destination.items.Insert(&f.incoming);break;} assert(t.Tick(0)==FAILED&&!t.Submitted()&&f.source.calls==0);} // Ownership lost after submission remains quarantined until exact physical proof. {Fixture f;auto t=f.Job();t.Tick(0);f.roster.valid=false;assert(t.Tick(1)==FAILED&&t.Unresolved());f.Move();f.source.last->OnComplete();assert(!t.Unresolved()&&t.Tick(2)==FAILED);} } '''.replace(';if(', ';\n if(')) print('PASS: actual finite PMN4 designated-zero/one entitlement, pinned original ownership, shared commit, physical custody, callback quarantine/deletion and no retry')