1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
//! Orders issued by the player from the strategic warbot panel.
use crate::{
strategic_roster::warbot_rows,
unit_patrol::{UnitOrder, UnitPatrol},
Engine, StartingCockpit,
};
use fparkan_terrain::NavigationQuery;
use fparkan_world::{identity_metadata, registration_sequence, ObjectHandle};
/// Where the warbot is to patrol.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum PatrolTarget {
/// Fixed world coordinates.
Point([f32; 2]),
/// Live object whose position determines the patrol center.
Object(ObjectHandle),
}
/// Tests a route waypoint using the selected warbot's chassis and the native areal flag.
pub fn accepts_waypoint(engine: &Engine, actor: ObjectHandle, [x, y]: [f32; 2]) -> bool {
if !x.is_finite() || !y.is_finite() {
return false;
}
let Some(mission) = &engine.loaded else {
return false;
};
let Some(original) = identity_metadata(&engine.world, actor)
.ok()
.and_then(|meta| meta.original_id)
else {
return false;
};
let terrain = &mission.terrain;
let Some(areal) = terrain.locate_areal([x, y, 0.0]).ok().flatten() else {
return false;
};
// iron3d+0x76bc0 allows chassis selector 1 in any resolved areal.
mission
.unit_chassis_flags
.get(&original.0)
.is_some_and(|flags| flags.chassis_type == Some(1))
|| terrain
.areal_logic_flag(areal)
.is_some_and(|flag| flag != 0)
}
/// Stops the selected warbot and clears its outstanding movement orders.
pub fn stand(engine: &mut Engine, cockpit: StartingCockpit, actor: ObjectHandle) -> bool {
issue(engine, cockpit, actor, UnitOrder::Stand)
}
/// Orders the selected warbot to search for hostile mobile units.
pub fn search_and_destroy(
engine: &mut Engine,
cockpit: StartingCockpit,
actor: ObjectHandle,
) -> bool {
issue(engine, cockpit, actor, UnitOrder::SearchAndDestroy)
}
/// Sends the selected warbot to a friendly facility to replenish its supplies.
pub fn recharge(engine: &mut Engine, cockpit: StartingCockpit, actor: ObjectHandle) -> bool {
issue(engine, cockpit, actor, UnitOrder::Recharge)
}
/// Replaces the selected warbot's order with the confirmed sequence of world points.
pub fn follow_route(
engine: &mut Engine,
cockpit: StartingCockpit,
actor: ObjectHandle,
points: &[[f32; 2]],
) -> bool {
if points.is_empty()
|| !points
.iter()
.all(|&point| accepts_waypoint(engine, actor, point))
{
return false;
}
issue(engine, cockpit, actor, UnitOrder::Route(points.to_vec()))
}
/// Whether an object may be used as the center of a patrol.
pub fn accepts_patrol_object(engine: &Engine, target: ObjectHandle) -> bool {
registration_sequence(&engine.world, target)
.ok()
.flatten()
.is_some()
&& engine.loaded.as_ref().is_some_and(|mission| {
!mission
.control_device_tables
.iter()
.any(|table| table.handle == target && table.node_provider_depletion_marked)
})
}
/// Replaces the selected warbot's order with a patrol around a point or an object.
pub fn patrol_at(
engine: &mut Engine,
cockpit: StartingCockpit,
actor: ObjectHandle,
target: PatrolTarget,
) -> bool {
let valid = match target {
PatrolTarget::Point(point) => accepts_waypoint(engine, actor, point),
PatrolTarget::Object(target) => accepts_patrol_object(engine, target),
};
valid && issue(engine, cockpit, actor, UnitOrder::StrategicPatrol(target))
}
fn issue(
engine: &mut Engine,
cockpit: StartingCockpit,
actor: ObjectHandle,
order: UnitOrder,
) -> bool {
if !warbot_rows(engine, cockpit)
.iter()
.any(|row| row.handle == actor && row.selectable)
{
return false;
}
let Some(mission) = engine.loaded.as_mut() else {
return false;
};
mission
.unit_patrol
.get_or_insert_with(UnitPatrol::scripted)
.command_order(&engine.world, actor, order);
true
}
|