aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/clan_combat_groups.rs
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-11-25 12:14:50 +0300
committerValentin Popov <valentin@popov.link>2026-11-25 12:14:50 +0300
commit3278b382a1580e45ba915b64ad283168ab9ea7e1 (patch)
treedc51110409e0e9ff1931792fecd3081ad23626c7 /crates/fparkan-runtime/src/clan_combat_groups.rs
parent6cce33ff5b29616fb3d5caf9325729068d0631eb (diff)
downloadfparkan-3278b382a1580e45ba915b64ad283168ab9ea7e1.tar.xz
fparkan-3278b382a1580e45ba915b64ad283168ab9ea7e1.zip
feat(ai): recruit free groups before assigning their target
Handler(25) TAKE_ALL_FREE takes the free roster in order and returns ERROR for an empty group. Target selection now belongs to Handler(28), so the script can form a group first and then issue its attack. Attack orders remain associated with their group; cleanup preserves a subsequent order. Coverage counts recruitments by clan and mode, including empty results. Original GOG ai.dll RVA 0xab85 confirmed membership across classes, busy and reserved exclusions, assignment updates, order and the empty result. Validated with Rust order-lifecycle checks, a licensed test that waits on the stock empty clan before a deliberate debug roster transfer, workspace fmt/check/test, and 900-second simulations of all 29 missions. Mission 12 ran from stock start in the window with clean Vulkan validation. The debug transfer verifies the attack mechanism, not a mission playthrough. Occupancy and routing remain approximate. Group orders for other classes fail explicitly. Script stops remain in missions 15 (71/72), 19 (Problems0 orders) and 29 (building counters/construction and Handler 40).
Diffstat (limited to 'crates/fparkan-runtime/src/clan_combat_groups.rs')
-rw-r--r--crates/fparkan-runtime/src/clan_combat_groups.rs64
1 files changed, 52 insertions, 12 deletions
diff --git a/crates/fparkan-runtime/src/clan_combat_groups.rs b/crates/fparkan-runtime/src/clan_combat_groups.rs
index 4a758388..b1476df0 100644
--- a/crates/fparkan-runtime/src/clan_combat_groups.rs
+++ b/crates/fparkan-runtime/src/clan_combat_groups.rs
@@ -1,6 +1,6 @@
use crate::clan_combat_strength::unit_strength;
use crate::unit_patrol::UnitPatrol;
-use crate::{MissionControlDeviceTableState, MissionDocument};
+use crate::{MissionControlDeviceTableState, MissionDocument, MissionObjectDraft};
use fparkan_script::{Handler15Invocation, Handler15TargetPayload};
use fparkan_world::{identity_metadata, transform_state, ObjectHandle, OwnerId, World};
@@ -11,6 +11,7 @@ pub(super) struct CombatGroup {
#[derive(Clone, Copy, Debug, PartialEq)]
pub(super) enum GroupTarget {
+ Unassigned,
Object(ObjectHandle, Option<OwnerId>),
Point([u32; 2]),
}
@@ -37,6 +38,7 @@ impl GroupTarget {
fn position(self, world: &World) -> Option<[f32; 2]> {
match self {
+ Self::Unassigned => None,
Self::Object(handle, _) => {
let [x, y, _] = transform_state(world, handle)
.ok()?
@@ -50,6 +52,7 @@ impl GroupTarget {
pub(super) fn valid(self, world: &World) -> bool {
match self {
+ Self::Unassigned => false,
Self::Object(handle, owner) => {
identity_metadata(world, handle).is_ok_and(|identity| identity.owner_id == owner)
}
@@ -59,6 +62,7 @@ impl GroupTarget {
pub(super) fn has_order(self, patrol: &UnitPatrol, unit: ObjectHandle) -> bool {
match self {
+ Self::Unassigned => false,
Self::Object(target, _) => patrol.has_object_order(unit, target),
Self::Point(point) => patrol.has_point_order(unit, point.map(|value| value as f32)),
}
@@ -66,12 +70,21 @@ impl GroupTarget {
pub(super) fn clear_order(self, patrol: &mut UnitPatrol, unit: ObjectHandle) {
match self {
+ Self::Unassigned => {}
Self::Object(target, _) => patrol.clear_object_order(unit, target),
Self::Point(point) => patrol.clear_point_order(unit, point.map(|value| value as f32)),
}
}
}
+pub(super) fn recruit_all(units: impl Iterator<Item = ObjectHandle>) -> Option<CombatGroup> {
+ let members: Vec<_> = units.collect();
+ (!members.is_empty()).then_some(CombatGroup {
+ members,
+ target: GroupTarget::Unassigned,
+ })
+}
+
pub(super) fn recruit(
world: &World,
units: impl Iterator<Item = ObjectHandle>,
@@ -96,7 +109,10 @@ pub(super) fn recruit(
})
.collect();
let (members, strength) = take_by_hits(&mut candidates, requested);
- let group = (strength > 0.0).then(|| CombatGroup { members, target });
+ let group = (strength > 0.0).then(|| CombatGroup {
+ members,
+ target: GroupTarget::Unassigned,
+ });
(group, strength)
}
@@ -119,26 +135,48 @@ fn take_by_hits(
impl CombatGroup {
pub(super) fn dispatch(
- &self,
+ &mut self,
patrol: &mut UnitPatrol,
world: &World,
mission: &MissionDocument,
+ drafts: &[MissionObjectDraft],
order: Handler15Invocation,
- ) -> Option<()> {
+ ) -> Result<(), String> {
let (0, kind @ (3 | 4), 3) = (order.word_0, order.word_1, order.word_2) else {
- return None;
+ return Err(
+ "Handler(28) requires a replacing patrol or attack order on group 0".into(),
+ );
};
- let target = GroupTarget::resolve(world, mission, order.target)?;
- if target != self.target {
- return None;
+ let target = GroupTarget::resolve(world, mission, order.target)
+ .ok_or("Handler(28) target is unavailable")?;
+ for &unit in &self.members {
+ let class = identity_metadata(world, unit)
+ .ok()
+ .and_then(|identity| identity.original_id)
+ .and_then(|id| drafts.get(id.0 as usize))
+ .and_then(|draft| draft.unit_dat_header_flags);
+ if class != Some(crate::boarding::WARBOT_CLASS) {
+ return Err(format!(
+ "Handler(28) unsupported group member class {class:?} unit={}",
+ unit.slot
+ ));
+ }
}
- let point = target.position(world)?;
+ let point = target
+ .position(world)
+ .ok_or("Handler(28) target has no position")?;
let attack = match (kind, target) {
(3, GroupTarget::Object(handle, _)) => Some((
handle,
- identity_metadata(world, handle).ok()?.original_id?.0 as usize,
+ identity_metadata(world, handle)
+ .ok()
+ .and_then(|identity| identity.original_id)
+ .ok_or("Handler(28) target has no runtime ID")?
+ .0 as usize,
)),
- (3, GroupTarget::Point(_)) => return None,
+ (3, GroupTarget::Point(_)) => {
+ return Err("Handler(28) attack by place is unsupported".into())
+ }
_ => None,
};
for &unit in &self.members {
@@ -150,6 +188,7 @@ impl CombatGroup {
radius => radius as f32,
};
match target {
+ GroupTarget::Unassigned => unreachable!("resolved order has a target"),
GroupTarget::Object(handle, _) => {
patrol.order_around_object(world, unit, handle, point, radius);
}
@@ -159,7 +198,8 @@ impl CombatGroup {
}
}
}
- Some(())
+ self.target = target;
+ Ok(())
}
}