aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/clan_combat_groups.rs
diff options
context:
space:
mode:
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(())
}
}