// HQ-owned ground rendezvous using delivered positions and existing finite cargo. class ERR_InfantryResupplyRules { static bool Fresh(ERR_HQWatchEntry entry, float now) { return entry && !entry.Closed && entry.Count > 0 && entry.Fit > 0 && ERR_ForceRules.Finite(now) && entry.ObservedAt >= 0 && now >= entry.ObservedAt && now - entry.ObservedAt <= 120000 && ERR_ForceRules.FiniteVector(entry.Position); } static bool Need(ERR_HQWatchEntry entry, float now) { return Fresh(entry, now) && entry.AmmoState && entry.AmmoState.Valid(entry.Count) && entry.AmmoState.Complete && entry.AmmoState.Members == entry.Count && entry.AmmoState.SmallArmsMuzzles > 0 && entry.AmmoState.SmallArmsRounds + entry.AmmoState.SmallArmsChambers <= entry.Count * 30; } static bool Pair(ERR_HQWatchEntry recipient, ERR_HQWatchEntry donor, float now) { return Need(recipient, now) && Fresh(donor, now) && recipient.SourceId != donor.SourceId && vector.DistanceXZ(recipient.Position, donor.Position) <= 200; } } class ERR_InfantryResupply { protected ERR_HQ m_HQ; protected ERR_GameMode m_Mode; protected ERR_ConvoyTask m_Convoy; protected ERR_ConvoyVehicle m_Truck; protected ERR_HQUnit m_Recipient; protected ERR_HQUnit m_Donor; protected ref array m_Stock = {}; protected ref array m_Spent = {}; protected ref array m_Members = {}; protected ref array m_Served = {}; protected ref map m_Consumed = new map(); protected ref map m_NextAttempt = new map(); protected ref ERR_HQRoute m_Order; protected ref ERR_InfantryAmmoTransfer m_Move; protected IEntity m_Receiving; protected AIWaypoint m_MoveWaypoint; protected AIWaypoint m_HoldWaypoint; protected vector m_Rendezvous; protected int m_State; // 0 stock startup, 1 idle, 2 truck downlink, 3 stopped status, 4 infantry downlink, 5 transfer protected int m_Trip; protected int m_Token; protected int m_ReportId; protected int m_Delivered; protected int m_TripDelivered; protected float m_Deadline; protected float m_HeldAt; protected float m_NextTick; protected bool m_Stopped; protected string m_Reason; void ERR_InfantryResupply(ERR_HQ hq, ERR_GameMode mode) { m_HQ = hq; m_Mode = mode; } int State() { return m_State; } int Delivered() { return m_Delivered; } int StockCount() { return m_Stock.Count(); } protected void Audit(string reason) { m_Reason = reason; m_HQ.Journal().Write("INFANTRY SUPPLY", string.Format("trip=%1 state=%2 moved=%3 spent=%4/%5; %6", m_Trip, m_State, m_Delivered, m_Spent.Count(), m_Stock.Count(), reason), m_ReportId); } protected bool Current() { return !m_Stopped && Replication.IsServer() && m_Mode && m_Mode == ERR_GameMode.Get() && m_Mode.Active(); } protected bool Contains(IEntity owner, IEntity item) { if (!owner || !item) return false; InventoryStorageManagerComponent inventory = InventoryStorageManagerComponent.Cast(owner.FindComponent(InventoryStorageManagerComponent)); if (!inventory) return false; array items = {}; ERR_Inventory.Items(inventory, items); return items.Count() <= 128 && items.Contains(item); } protected bool TruckValid() { if (!m_Convoy || m_Convoy.Stopped || !m_Convoy.Ready || !m_Convoy.Receipt || !m_Convoy.Receipt.Consumed || m_HQ.ConvoyTask() != m_Convoy || m_Convoy.Vehicles.Count() != 6 || m_Convoy.Vehicles[1] != m_Truck) return false; if (!m_Truck || m_Truck.Index != 1 || !m_Truck.Registered || m_Truck.Released || m_Truck.Arrived || !m_Truck.CrewValid(m_Mode) || ERR_ConvoyRules.Captured(m_Truck.Entity, m_Truck.Group)) return false; ERR_ForceRecord record = m_Mode.Forces().FindGroup(m_Truck.Group); return record && record.Physical && !record.Eliminated && record.Kind == ERR_ForceKind.CONVOY && m_Convoy.Receipt.Records.Count() == 6 && record.Id == m_Convoy.Receipt.Records[1]; } protected bool Quiet(ERR_HQUnit unit) { return unit && unit.Group && unit.Utility && !unit.Group.IsSlave() && unit.Group.GetPlayerCount(true) == 0 && unit.Group.GetFaction() == m_Mode.EnemyFaction() && (!unit.Utility.m_Perception || unit.Utility.m_Perception.m_aTargets.IsEmpty()); } protected bool Roster() { if (!m_Recipient || !Quiet(m_Recipient) || !m_Recipient.StatusSampler.OwnsRoster(m_Recipient.Group)) return false; int strength = m_Recipient.StatusSampler.InitialStrength(); if (strength < 1 || strength > 32) return false; array agents = {}; m_Recipient.Group.GetAgents(agents); if (agents.IsEmpty() || agents.Count() > 32) return false; foreach (AIAgent agent : agents) if (!agent || !m_Members.Contains(agent.GetControlledEntity())) return false; bool living; foreach (IEntity actor : m_Members) { if (!actor || !ERR_Actors.Alive(actor)) continue; // Returning to the group cannot undo a sampler's permanent release of an // original slot after transfer or player control during this trip. bool attributed = false; for (int slot = 0; slot < strength; slot++) { if (m_Recipient.StatusSampler.InitialMember(slot) != actor) continue; attributed = m_Recipient.StatusSampler.CanAttribute(slot, actor); break; } if (!attributed) return false; if (ERR_Actors.Player(actor) || ERR_Actors.Group(actor) != m_Recipient.Group || SCR_FactionManager.SGetFaction(actor) != m_Mode.EnemyFaction() || CompartmentAccessComponent.GetVehicleIn(actor)) return false; if (ERR_Actors.CanFight(actor)) living = true; } return living; } protected void Stock(float now) { m_Convoy = m_HQ.ConvoyTask(); if (!m_Convoy || !m_Convoy.Ready || m_Convoy.Vehicles.Count() != 6) return; m_Truck = m_Convoy.Vehicles[1]; if (!TruckValid()) { Stop(); return; } foreach (ERR_ConvoyCargoCallback operation : m_Convoy.Cargo) if (!operation.Finished) { if (now >= m_Convoy.Deadline) Stop(); return; } InventoryStorageManagerComponent inventory = InventoryStorageManagerComponent.Cast(m_Truck.Entity.FindComponent(InventoryStorageManagerComponent)); if (!inventory) { Stop(); return; } array items = {}; ERR_Inventory.Items(inventory, items); if (items.Count() > 128) { Stop(); return; } foreach (IEntity item : items) { if (!item || !item.GetPrefabData() || item.GetPrefabData().GetPrefabName() != ERR_ConvoyResources.Magazine()) continue; if (m_Stock.Count() >= 48) { Stop(); return; } m_Stock.Insert(item); } m_State = 1; Audit("truck magazine identities captured once after startup; absent/looted stock is never replaced"); } bool TryPlan(ERR_HQUnit recipient, ERR_HQUnit donor, ERR_HQWatchEntry demand, ERR_HQWatchEntry donorStatus, float now) { if (!Current() || m_State != 1 || m_Move || m_Trip >= 128 || !TruckValid()) return false; if (m_Truck.Combat.Held || m_Truck.SupplyToken > 0 || !recipient || !donor) return false; if (donor.Group != m_Truck.Group || recipient.HomeGuard || !Quiet(recipient) || !Quiet(donor)) return false; if (donor.Downlink || donor.PendingOrder || donor.PendingConvoyOrder) return false; if (!ERR_InfantryResupplyRules.Pair(demand, donorStatus, now)) return false; if (demand.SourceId != recipient.ForceSourceId || donorStatus.SourceId != donor.ForceSourceId) return false; if (demand.MessageId <= m_Consumed.Get(demand.SourceId) || now < m_NextAttempt.Get(demand.SourceId)) return false; bool available; foreach (IEntity magazine : m_Stock) if (magazine && !m_Spent.Contains(magazine) && Contains(m_Truck.Entity, magazine)) { available = true; break; } if (!available) return false; m_Members.Clear(); for (int i = 0; i < recipient.StatusSampler.InitialStrength(); i++) { IEntity actor = recipient.StatusSampler.InitialMember(i); if (actor && recipient.StatusSampler.CanAttribute(i, actor)) m_Members.Insert(actor); } m_Recipient = recipient; m_Donor = donor; if (!Roster()) { m_Recipient = null; m_Donor = null; return false; } // Disjoint bounded token range from the existing mortar courier's trip IDs. int token = 100000 + m_Trip + 1; if (!m_HQ.ReserveLogistics(recipient.Group, token)) { m_Recipient = null; m_Donor = null; return false; } m_Trip++; m_Token = token; m_Truck.SupplyToken = token; m_ReportId = demand.MessageId; m_TripDelivered = 0; m_Served.Clear(); m_Consumed.Set(demand.SourceId, demand.MessageId); m_NextAttempt.Set(demand.SourceId, now + 180000); m_Deadline = Math.Min(now + 120000, demand.ObservedAt + 180000); m_State = 2; m_Order = m_HQ.Downlink(donor.Group, "INFANTRY SUPPLY HOLD", "stop existing ammo truck and report position", m_ReportId); if (!m_Order) Finish("truck radio downlink unavailable"); else Audit("existing patrol reserved; truck hold awaits radio delivery"); return true; } protected bool Waypoints() { m_MoveWaypoint = AIWaypoint.Cast(ERR_Actors.Spawn(ERR_Resources.MoveWaypoint(), ERR_Actors.Ground(m_Rendezvous))); m_HoldWaypoint = AIWaypoint.Cast(ERR_Actors.Spawn(ERR_Resources.DefendWaypoint(), ERR_Actors.Ground(m_Rendezvous))); if (!m_MoveWaypoint || !m_HoldWaypoint) return false; // The reported rendezvous is 3 m from the donor report. A 2 m move // radius allowed arrival 5 m away, beyond the transfer's physical reach. m_MoveWaypoint.SetCompletionRadius(0.5); m_HoldWaypoint.SetCompletionRadius(0.5); m_Recipient.Group.AddWaypointAt(m_HoldWaypoint, 0); m_Recipient.Group.AddWaypointAt(m_MoveWaypoint, 0); m_Recipient.Group.ActivateAI(); return true; } protected void ClearWaypoints() { if (m_MoveWaypoint) { if (m_Recipient && m_Recipient.Group) m_Recipient.Group.RemoveWaypoint(m_MoveWaypoint); ERR_Actors.DeleteSpawned(m_MoveWaypoint); } if (m_HoldWaypoint) { if (m_Recipient && m_Recipient.Group) m_Recipient.Group.RemoveWaypoint(m_HoldWaypoint); ERR_Actors.DeleteSpawned(m_HoldWaypoint); } m_MoveWaypoint = null; m_HoldWaypoint = null; } protected void Finish(string reason) { Audit(reason); if (m_Move) m_Move.Stop(); ClearWaypoints(); if (m_Recipient) m_HQ.ReleaseLogistics(m_Recipient.Group, m_Token); if (m_Truck && m_Truck.SupplyToken == m_Token) { m_Truck.SupplyToken = 0; m_Truck.SupplyHeld = false; } m_Recipient = null; m_Donor = null; m_Order = null; m_Receiving = null; m_State = 1; } protected void Transfer(float now) { if (m_Move) { int result = m_Move.Tick(now); if (result == 0) return; if (result < 0) { Finish("native transfer rejected, expired or lost authority; no item retry"); return; } m_Move = null; m_Delivered++; m_TripDelivered++; m_Served.Insert(m_Receiving); m_Receiving = null; Audit("exact finite magazine confirmed in original soldier inventory"); if (m_TripDelivered >= 6) { Finish("six-magazine trip allowance reached"); return; } } foreach (IEntity actor : m_Members) { if (!ERR_Actors.CanFight(actor) || m_Served.Contains(actor) || vector.DistanceXZ(actor.GetOrigin(), m_Truck.Entity.GetOrigin()) > 4) continue; ERR_HQAmmoSnapshot local = new ERR_HQAmmoSnapshot(); local.Complete = true; local.LauncherComplete = true; ERR_HQAmmoSampler.Inspect(actor, m_Recipient.Group, local); if (!local.Complete || local.SmallArmsMuzzles <= 0 || local.SmallArmsRounds + local.SmallArmsChambers > 30) continue; foreach (IEntity magazine : m_Stock) { if (!magazine || m_Spent.Contains(magazine) || !Contains(m_Truck.Entity, magazine) || !ERR_InfantryAmmoTransfer.Compatible(actor, magazine)) continue; m_Spent.Insert(magazine); m_Receiving = actor; m_Move = new ERR_InfantryAmmoTransfer(m_Mode, m_Truck.Entity, m_Truck.Group, actor, m_Recipient.Group, magazine); m_Move.Tick(now); if (m_Move.Submitted()) return; // No native operation exists yet: approach geometry, stopped state or // capacity may improve while the same bounded rendezvous remains valid. // Keep every submitted identity spent, including a synchronous rejection. m_Spent.RemoveItem(magazine); m_Move = null; m_Receiving = null; break; // At most one unsubmitted preflight per member on this slow tick. } } } void Tick(float now) { if (!Current() || !ERR_ForceRules.Finite(now) || now < m_NextTick) return; m_NextTick = now + 1000; if (m_State == 0) { Stock(now); return; } if (m_State == 1) { if (m_Move && !m_Move.Unresolved()) m_Move = null; return; } if (now >= m_Deadline || !TruckValid() || !Quiet(m_Donor) || !Roster() || m_Truck.Combat.Held || m_HQ.LogisticsToken(m_Recipient.Group) != m_Token) { Finish("rendezvous expired, contact appeared or original ownership changed"); return; } if (m_Truck.SupplyToken != m_Token) { Finish("truck hold authority lost"); return; } if (m_State == 2) { int delivered = m_Order.Advance(now, m_HQ.Journal()); if (delivered == 0) return; if (delivered < 0 || m_Truck.SupplyToken != m_Token) { Finish("truck hold downlink interrupted or reservation lost"); return; } m_Truck.SupplyHeld = true; m_Truck.ClearOrder(); m_HeldAt = -1; m_Order = null; m_State = 3; Audit("truck received stop; waiting for delivered stopped-position report"); return; } if (m_State == 3) { // The delivered hold asks the original crew to report only after physically stopping. if (m_HeldAt < 0) { Physics physics = m_Truck.Entity.GetPhysics(); if (!physics) return; float speed = physics.GetVelocity().Length(); float angular = physics.GetAngularVelocity().Length(); if (!(speed >= 0 && speed <= 0.25 && angular >= 0 && angular <= 0.1)) return; m_HeldAt = now; m_Donor.NextStatus = now; return; } ERR_HQWatchEntry stopped = m_HQ.Watch().Find(m_Donor.ForceSourceId); if (!ERR_InfantryResupplyRules.Fresh(stopped, now) || stopped.ObservedAt < m_HeldAt) return; m_Rendezvous = stopped.Position + "3 0 0"; m_Order = m_HQ.Downlink(m_Recipient.Group, "INFANTRY SUPPLY RENDEZVOUS", string.Format("walk to reported stopped ammo truck at %1", m_Rendezvous), m_ReportId); if (!m_Order) { Finish("infantry radio downlink unavailable"); return; } m_State = 4; return; } if (m_State == 4) { int delivered = m_Order.Advance(now, m_HQ.Journal()); if (delivered == 0) return; if (delivered < 0 || !Waypoints()) { Finish("infantry order interrupted or native waypoint creation failed"); return; } m_Order = null; m_State = 5; Audit("infantry received reported rendezvous; native movement and physical transfer required"); } if (m_State == 5) Transfer(now); } string Status() { return string.Format("Infantry supply: state=%1 trips=%2 moved=%3 stock=%4 spent=%5; %6", m_State, m_Trip, m_Delivered, m_Stock.Count(), m_Spent.Count(), m_Reason); } void Stop() { if (m_Stopped) return; Finish("service stopped; original forces and all remaining cargo retained"); m_Stopped = true; } }