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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
use crate::{unit_patrol::UnitPatrol, BehaviorOreState};
use fparkan_world::{
identity_metadata, registration_sequence, transform_state, ObjectHandle, OwnerId, World,
};
// Behavior.dll config constructor RVA 0x16250 and build task RVA 0x28ff0.
const BUILDING_COST_THRESHOLD: f32 = 100.0;
const LOADING_RATE: f32 = 100.0;
pub(super) struct BuilderSupply {
pub(super) source: ObjectHandle,
last_ms: Option<u32>,
}
impl BuilderSupply {
pub(super) fn plan(
world: &World,
ores: &[BehaviorOreState],
builder: ObjectHandle,
owner: OwnerId,
class: u32,
stock: f32,
) -> Result<Option<Self>, String> {
if class == crate::mining::MINE_CLASS || stock >= BUILDING_COST_THRESHOLD {
return Ok(None);
}
let source = source(world, ores, builder, owner).ok_or("builder has no ore source")?;
Ok(Some(Self {
source,
last_ms: None,
}))
}
pub(super) fn tick(
&mut self,
world: &World,
ores: &mut [BehaviorOreState],
patrol: &mut Option<UnitPatrol>,
builder: ObjectHandle,
owner: OwnerId,
cost: f32,
now_ms: u32,
) -> Result<bool, String> {
let elapsed = self.last_ms.map_or(0, |last| now_ms.wrapping_sub(last));
self.last_ms = Some(now_ms);
if !owned(world, self.source, owner) {
self.source =
source(world, ores, builder, owner).ok_or("builder lost its ore source")?;
}
let target = ores
.iter()
.position(|ore| ore.handle == self.source)
.ok_or("builder ore source is unavailable")?;
let cargo = ores
.iter()
.position(|ore| ore.handle == builder)
.ok_or("builder ore state is unavailable")?;
let [x, y, _] = transform_state(world, builder)
.map_err(|err| err.to_string())?
.position
.map(f32::from_bits);
let [a, b, _] = transform_state(world, self.source)
.map_err(|err| err.to_string())?
.position
.map(f32::from_bits);
let distance = (x - a).hypot(y - b);
let moving = patrol
.as_ref()
.is_some_and(|patrol| patrol.is_moving_to_destination(builder));
// shortcut: use an exterior approach until native dock and port movement are wired.
if distance > 48.0 {
if !moving {
let offset = 40.0 / distance;
patrol.get_or_insert_with(UnitPatrol::scripted).move_to(
world,
builder,
[a + (x - a) * offset, b + (y - b) * offset],
false,
);
eprintln!(
"script_builder_supply_route clan={} builder={} source={} queue_time_ms={now_ms}",
owner.0, builder.slot, self.source.slot
);
}
return Ok(false);
}
if let Some(patrol) = patrol {
patrol.clear_move(builder);
}
let stock = f32::from_bits(ores[cargo].current_ore_bits);
let available = f32::from_bits(ores[target].current_ore_bits);
if !stock.is_finite() || !available.is_finite() {
return Err("builder supply contains non-finite ore".into());
}
let amount = batch(stock, available, cost, elapsed);
ores[target].current_ore_bits = (available - amount).to_bits();
let collected = stock + amount;
ores[cargo].current_ore_bits = collected.to_bits();
Ok(collected >= cost)
}
}
fn owned(world: &World, handle: ObjectHandle, owner: OwnerId) -> bool {
registration_sequence(world, handle)
.ok()
.flatten()
.is_some()
&& identity_metadata(world, handle).is_ok_and(|id| id.owner_id == Some(owner))
}
fn source(
world: &World,
ores: &[BehaviorOreState],
builder: ObjectHandle,
owner: OwnerId,
) -> Option<ObjectHandle> {
let from = transform_state(world, builder)
.ok()?
.position
.map(f32::from_bits);
ores.iter()
.filter(|ore| {
matches!(ore.class_flags, crate::mining::MINE_CLASS | 0x8000_0008)
&& eligible_source(f32::from_bits(ore.current_ore_bits))
&& owned(world, ore.handle, owner)
})
.filter_map(|ore| {
let to = transform_state(world, ore.handle)
.ok()?
.position
.map(f32::from_bits);
let distance = (from[0] - to[0])
.hypot(from[1] - to[1])
.hypot(from[2] - to[2]);
distance.is_finite().then_some((ore.handle, distance))
})
.min_by(|a, b| a.1.total_cmp(&b.1))
.map(|(handle, _)| handle)
}
fn eligible_source(stock: f32) -> bool {
stock.is_finite() && stock > BUILDING_COST_THRESHOLD * 1.5
}
fn batch(stock: f32, available: f32, cost: f32, elapsed_ms: u32) -> f32 {
let missing = (cost - stock).max(0.0);
let limit = elapsed_ms as f32 * 0.001 * LOADING_RATE;
limit.min(available).min(missing)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn native_start_uses_the_fixed_threshold_and_bypasses_supply_for_a_mine() {
let world = fparkan_world::new(fparkan_world::WorldConfig);
let builder = ObjectHandle {
slot: 0,
generation: 0,
};
for (class, stock) in [(0x8000_0010, 100.0), (crate::mining::MINE_CLASS, 0.0)] {
assert!(
BuilderSupply::plan(&world, &[], builder, OwnerId(1), class, stock)
.unwrap()
.is_none()
);
}
assert!(
matches!(BuilderSupply::plan(&world, &[], builder, OwnerId(1), 0x8000_0010, 99.0), Err(reason) if reason == "builder has no ore source")
);
}
#[test]
fn native_loading_stops_at_the_price_or_available_ore() {
for (stock, available, cost, elapsed, expected) in [
(0.0, 1000.0, 770.0, 1000, 100.0),
(700.0, 1000.0, 770.0, 1000, 70.0),
(0.0, 10.0, 770.0, 1000, 10.0),
(800.0, 1000.0, 770.0, 1000, 0.0),
(-670.0, 1000.0, 560.0, 1000, 100.0),
] {
assert_eq!(batch(stock, available, cost, elapsed), expected);
}
assert!(!eligible_source(150.0));
assert!(eligible_source(150.01));
}
}
|