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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
use super::{
mission_events::on_communicator, normalize_engine_path, ClanBody, Engine, EngineError,
LoadedMissionState, MissionObjectDraft, StartingCockpit,
};
use fparkan_assets::ResearchTrfDocument;
use fparkan_vfs::Vfs;
use fparkan_world::{
handle_by_original_id, identity_metadata, transform_state, ObjectHandle, OwnerId, World,
};
use std::collections::HashSet;
use std::sync::Arc;
const LAB_CLASS: u32 = 0x8000_0400;
pub(super) const RESEARCH_AVAILABLE: u8 = 1;
pub(super) const RESEARCHED: u8 = 2;
#[derive(Default)]
pub(super) struct FreeResearch {
remaining: Vec<(usize, u32)>,
pending: Option<Order>,
pub(super) learned: HashSet<(OwnerId, usize)>,
}
struct Order {
lab: ObjectHandle,
owner: OwnerId,
row: usize,
component: String,
start_ms: u32,
duration_ms: u32,
}
impl FreeResearch {
pub(super) fn active(&self, lab: ObjectHandle) -> bool {
self.pending.as_ref().is_some_and(|order| order.lab == lab)
}
pub(super) fn take_free(&mut self, slot: usize) -> bool {
let Some((_, count)) = self
.remaining
.iter_mut()
.find(|(index, count)| *index == slot && *count > 0)
else {
return false;
};
*count -= 1;
true
}
pub(super) fn new(drafts: &[MissionObjectDraft]) -> Self {
let remaining = drafts
.iter()
.enumerate()
.filter_map(|(index, draft)| {
if draft.unit_dat_header_flags != Some(LAB_CLASS) {
return None;
}
let count = property(draft, b"FreeTechnoNum")? as i32;
(count > 0).then_some((index, count as u32))
})
.collect();
Self {
remaining,
..Self::default()
}
}
pub(super) fn tick(&mut self, world: &World, now_ms: u32) -> Option<String> {
let order = self.pending.as_ref()?;
if !identity_metadata(world, order.lab)
.is_ok_and(|identity| identity.owner_id == Some(order.owner))
{
self.pending = None;
return None;
}
if now_ms.wrapping_sub(order.start_ms) < order.duration_ms {
return None;
}
let order = self.pending.take()?;
self.learned.insert((order.owner, order.row));
Some(order.component)
}
}
/// Starts the first available free technology at the player's laboratory.
///
/// # Errors
/// Returns a diagnostic if the lab, research slot, or clan table is unavailable.
pub fn order_lab_research(
engine: &mut Engine,
cockpit: StartingCockpit,
now_ms: u32,
) -> Result<(String, u32), EngineError> {
let fail = |message: &str| EngineError::Production(message.into());
let mission = engine
.loaded
.as_mut()
.ok_or_else(|| fail("no loaded mission"))?;
if mission.free_research.pending.is_some() {
return Err(fail("laboratory research already in progress"));
}
let owner = identity_metadata(&engine.world, cockpit.handle)?
.owner_id
.ok_or_else(|| fail("player clan is unavailable"))?;
let position = transform_state(&engine.world, cockpit.handle)?
.position
.map(f32::from_bits);
let (quota, lab, draft) = mission
.free_research
.remaining
.iter()
.enumerate()
.find_map(|(slot, &(index, count))| {
if count == 0 {
return None;
}
let draft = &mission.object_drafts[index];
let lab = handle_by_original_id(&engine.world, draft.original_id?)?;
(identity_metadata(&engine.world, lab).ok()?.owner_id == Some(owner)
&& on_communicator(
&mission.mission_assets,
index,
LAB_CLASS,
transform_state(&engine.world, lab).ok()?,
position,
))
.then_some((slot, lab, draft))
})
.ok_or_else(|| {
fail("stand on a friendly laboratory communicator with free research remaining")
})?;
let duration = f32::from_bits(property(draft, b"FreeResearchTime").unwrap_or_default());
if !duration.is_finite() || duration <= 0.0 || duration >= u32::MAX as f32 / 1000.0 {
return Err(fail("laboratory research time is unavailable"));
}
let owned;
let research = match mission
.research_trf_tables
.iter()
.find(|table| table.clan_index == usize::from(owner.0))
{
Some(table) => &table.document,
None => {
owned = load_clan_table(
engine
.services
.vfs
.as_ref()
.ok_or(EngineError::MissingVfs)?,
mission,
owner,
)?;
&owned
}
};
// shortcut: select the first eligible row until the laboratory exposes a technology list.
let row = (0..research.row_count())
.find(|&row| {
research.flag_byte(row).unwrap_or_default() & (RESEARCH_AVAILABLE | RESEARCHED)
== RESEARCH_AVAILABLE
&& !mission.free_research.learned.contains(&(owner, row))
})
.ok_or_else(|| fail("no available technology to research"))?;
let component =
String::from_utf8_lossy(research.row_name(row).map_err(research_error)?).into_owned();
let duration_ms = (duration * 1000.0).ceil() as u32;
mission.free_research.remaining[quota].1 -= 1;
mission.free_research.pending = Some(Order {
lab,
owner,
row,
component: component.clone(),
start_ms: now_ms,
duration_ms,
});
Ok((component, duration_ms))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
capture_neutral_building, create, frame, load_gameplay_mission, starting_cockpit,
EngineServices, MissionMode, MissionRequest, MissionStartupInput,
};
use fparkan_vfs::DirectoryVfs;
use fparkan_world::{set_transform, OriginalObjectId};
#[test]
#[ignore = "requires licensed corpus"]
fn licensed_teleport_lab_researches_the_available_turret() {
let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").unwrap();
let vfs: Arc<dyn Vfs> = Arc::new(DirectoryVfs::new(std::path::PathBuf::from(root)));
let mut engine = create(EngineServices::new(vfs.clone())).unwrap();
load_gameplay_mission(
&mut engine,
MissionRequest {
key: "MISSIONS/CAMPAIGN/CAMPAIGN.00/Mission.04/data.tma".into(),
mode: MissionMode::SingleMission,
},
MissionStartupInput {
queue_time_ms: 0,
research_creation_gate: false,
},
None,
)
.unwrap();
let hero = starting_cockpit(&engine).unwrap();
let mut pose = transform_state(&engine.world, hero.handle).unwrap();
pose.position = [950.7_f32, 869.4, 69.98].map(f32::to_bits);
set_transform(&mut engine.world, hero.handle, pose).unwrap();
assert!(order_lab_research(&mut engine, hero, 1_000).is_err());
assert_eq!(
capture_neutral_building(&mut engine, hero).unwrap(),
Some(OriginalObjectId(5))
);
let (component, duration) = order_lab_research(&mut engine, hero, 1_000).unwrap();
assert_eq!((component.as_str(), duration), ("e_tur_bt_01", 5_000));
assert!(order_lab_research(&mut engine, hero, 1_001).is_err());
assert!(frame(&mut engine, 5_999)
.unwrap()
.researched_component
.is_none());
assert_eq!(
frame(&mut engine, 6_000).unwrap().researched_component,
Some(component)
);
assert!(frame(&mut engine, 6_001)
.unwrap()
.researched_component
.is_none());
let mission = engine.loaded.as_ref().unwrap();
let table = load_clan_table(&vfs, mission, OwnerId(0)).unwrap();
let row = table.find_key(b"e_tur_bb_01").unwrap().unwrap();
assert!(mission.free_research.learned.contains(&(OwnerId(0), row)));
assert!(!mission.free_research.learned.contains(&(OwnerId(1), row)));
assert!(order_lab_research(&mut engine, hero, 6_001).is_err());
}
}
pub(super) fn property(draft: &MissionObjectDraft, name: &[u8]) -> Option<u32> {
draft
.properties
.iter()
.find(|property| property.name_raw == name)
.map(|property| property.raw_value[1])
}
pub(super) fn load_clan_table(
vfs: &Arc<dyn Vfs>,
mission: &LoadedMissionState,
owner: OwnerId,
) -> Result<ResearchTrfDocument, EngineError> {
load_technology_table(vfs.as_ref(), &mission.mission, owner)
}
pub(super) fn load_technology_table(
vfs: &dyn Vfs,
mission: &crate::MissionDocument,
owner: OwnerId,
) -> Result<ResearchTrfDocument, EngineError> {
let clan = mission
.clans
.get(usize::from(owner.0))
.ok_or_else(|| research_error("clan is unavailable"))?;
let raw = match &clan.body {
ClanBody::Standard {
second_resource, ..
}
| ClanBody::Spatial {
second_resource, ..
} => &second_resource.path.raw,
};
let raw = raw.split(|&byte| byte == 0).next().unwrap_or_default();
let path = std::str::from_utf8(raw).map_err(research_error)?;
if path.is_empty() {
return Err(research_error("clan has no technology table"));
}
let path = normalize_engine_path("clan technology table", path)?;
let bytes = vfs.read(&path).map_err(|source| EngineError::Vfs {
path: path.as_str().into(),
source,
})?;
ResearchTrfDocument::decode(bytes).map_err(research_error)
}
fn research_error(error: impl std::fmt::Display) -> EngineError {
EngineError::Production(error.to_string())
}
|