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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
|
use crate::{BehaviorOreState, LoadedMissionState, MissionControlDeviceTableState};
use fparkan_assets::{decode_behavior_work_rates, research_project_rows};
use fparkan_vfs::Vfs;
use fparkan_world::{identity_metadata, ObjectHandle, OwnerId, World};
use std::collections::BTreeMap;
pub(super) const LAB_CLASS: u32 = 0x8000_0400;
/// Executed laboratory orders and technology state changes.
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct LaboratoryReport {
/// Accepted/rejected orders by clan and laboratory.
pub orders: Vec<(usize, u32, u64, u64)>,
/// Completed technologies by clan, laboratory and TRF row.
pub completed: Vec<(usize, u32, usize, u64)>,
/// Newly available technologies by clan and TRF row.
pub available: Vec<(usize, usize, u64)>,
/// Orders stopped by unavailable runtime state, by clan and laboratory.
pub stopped: Vec<(usize, u32, u64)>,
/// Due task updates; the idle owner phase is still approximate.
pub clock_ticks: Vec<(usize, u32, u64)>,
}
#[derive(Default)]
pub(super) struct Laboratories {
tasks: Vec<Order>,
rates: Option<[f32; 2]>,
progress: BTreeMap<(usize, usize), f32>,
orders: BTreeMap<(usize, u32), (u64, u64)>,
completed: BTreeMap<(usize, u32, usize), u64>,
available: BTreeMap<(usize, usize), u64>,
stopped: BTreeMap<(usize, u32), u64>,
clock_ticks: BTreeMap<(usize, u32), u64>,
}
struct Order {
clan: usize,
lab: ObjectHandle,
slot: usize,
rows: Vec<usize>,
current: Option<(usize, Work)>,
duration: f32,
last_ms: u32,
clock: crate::behavior_clock::TaskClock,
}
#[derive(Default)]
struct Work {
ore_cost: f32,
power_cost: f32,
duration: f32,
ore: f32,
power: f32,
time: f32,
}
impl Work {
fn fraction(&self) -> f32 {
let ratio = |value, target| if target > 0.0 { value / target } else { 1.0 };
ratio(self.ore, self.ore_cost)
.min(ratio(self.power, self.power_cost))
.min(ratio(self.time, self.duration))
}
fn advance(
&mut self,
seconds: f32,
rate: f32,
uses: [f32; 2],
buffer: &mut BehaviorOreState,
progress: f32,
) -> f32 {
let before = self.fraction();
self.ore += (rate * uses[1] * seconds).min(f32::from_bits(buffer.current_ore_bits));
let remaining = self.ore_cost - self.ore;
buffer.current_ore_bits = 0;
buffer.maximum_ore_bits = (if remaining > 5.0 {
remaining * 0.5
} else {
remaining
})
.max(0.0)
.to_bits();
self.power += rate * uses[0] * seconds;
self.time += seconds;
(progress + (self.fraction() - before)).min(1.0)
}
}
impl Laboratories {
pub(super) fn active(&self, lab: ObjectHandle) -> bool {
self.tasks.iter().any(|order| order.lab == lab)
}
pub(super) fn rejected(&mut self, clan: usize, lab: ObjectHandle, reason: &str) {
let rates = self.orders.entry((clan, lab.slot)).or_default();
rates.1 += 1;
if rates.1 == 1 {
eprintln!(
"script_lab_research_rejected clan={clan} lab={} reason={reason}",
lab.slot
);
}
}
pub(super) fn report(&self) -> LaboratoryReport {
LaboratoryReport {
orders: self
.orders
.iter()
.map(|(&(clan, lab), &(accepted, rejected))| (clan, lab, accepted, rejected))
.collect(),
completed: self
.completed
.iter()
.map(|(&(clan, lab, row), &count)| (clan, lab, row, count))
.collect(),
available: self
.available
.iter()
.map(|(&(clan, row), &count)| (clan, row, count))
.collect(),
stopped: self
.stopped
.iter()
.map(|(&(clan, lab), &count)| (clan, lab, count))
.collect(),
clock_ticks: self
.clock_ticks
.iter()
.map(|(&(clan, lab), &count)| (clan, lab, count))
.collect(),
}
}
}
pub(super) fn order(
state: &mut Laboratories,
catalogs: &mut BTreeMap<usize, crate::clan_research::ClanResearch>,
drafts: &[crate::MissionObjectDraft],
free: &crate::research::FreeResearch,
world: &World,
vfs: Option<&dyn Vfs>,
clan: usize,
lab: ObjectHandle,
now_ms: u32,
) -> Result<(), String> {
let owner = OwnerId(u16::try_from(clan).map_err(|_| "invalid research clan")?);
let identity = identity_metadata(world, lab).map_err(|error| error.to_string())?;
if identity.owner_id != Some(owner) {
return Err("laboratory belongs to another clan".into());
}
let slot = identity
.original_id
.ok_or("laboratory has no resource slot")?
.0 as usize;
let draft = drafts.get(slot).ok_or("laboratory draft is unavailable")?;
if draft.unit_dat_header_flags != Some(LAB_CLASS) {
return Err("research target is not a laboratory".into());
}
let vfs = vfs.ok_or("research VFS is unavailable")?;
let catalog = catalogs
.get_mut(&clan)
.ok_or("research catalog is unavailable")?;
catalog.refresh(vfs)?;
if state.active(lab) || free.active(lab) {
return Err("laboratory is busy".into());
}
let selected = catalog
.candidates
.entries
.get(catalog.candidates.selected_index as usize)
.ok_or("research project is unavailable")?;
if selected.status != 0 {
return Err("research project is already complete".into());
}
let bytes = vfs
.read(&selected.path)
.map_err(|error| error.to_string())?;
let unit = fparkan_prototype::decode_unit_dat(&bytes).map_err(|error| error.to_string())?;
let rows = research_project_rows(&unit, &catalog.table).map_err(|error| error.to_string())?;
if rows.is_empty() {
return Err("research project has no technologies".into());
}
let duration = f32::from_bits(
crate::research::property(draft, b"FreeResearchTime").unwrap_or(2.0_f32.to_bits()),
);
if !duration.is_finite() || duration < 0.0 {
return Err("invalid laboratory research time".into());
}
if state.rates.is_none() {
state.rates = Some(load_rates(vfs)?);
}
state.orders.entry((clan, lab.slot)).or_default().0 += 1;
state.tasks.push(Order {
clan,
lab,
slot,
rows,
current: None,
duration,
last_ms: now_ms,
clock: Default::default(),
});
eprintln!(
"sim_coverage script_lab_research_approx=1 clan={clan} lab={} project={}",
lab.slot,
selected.path.as_str()
);
Ok(())
}
pub(super) fn tick(mission: &mut LoadedMissionState, world: &World, now_ms: u32, rng: &mut u32) {
let mut state = std::mem::take(&mut mission.laboratories);
let mut index = 0;
while index < state.tasks.len() {
let task = &mut state.tasks[index];
if !task.clock.due(now_ms, rng) {
index += 1;
continue;
}
let count = state
.clock_ticks
.entry((task.clan, task.lab.slot))
.or_default();
*count += 1;
if *count == 1 {
eprintln!("sim_coverage script_lab_clock_approx=1 clan={} lab={} (active task timer; idle owner phases remain open)", task.clan, task.lab.slot);
}
let result = advance(&mut state, index, mission, world, now_ms);
if matches!(result, Ok(true)) {
index += 1;
continue;
}
let task = state.tasks.remove(index);
if let Err(reason) = result {
*state.stopped.entry((task.clan, task.lab.slot)).or_default() += 1;
eprintln!(
"script_lab_research_stopped clan={} lab={} reason={reason}",
task.clan, task.lab.slot
);
}
if let Some(ore) = mission
.behavior_ore_states
.iter_mut()
.find(|ore| ore.handle == task.lab)
{
ore.current_ore_bits = 0;
ore.maximum_ore_bits = 0;
}
if let Some(table) = mission
.control_device_tables
.iter_mut()
.find(|table| table.handle == task.lab)
{
if let Err(reason) = power(table, 0.0) {
eprintln!(
"script_lab_power_reset_failed lab={} reason={reason}",
task.lab.slot
);
}
}
}
mission.laboratories = state;
}
fn advance(
state: &mut Laboratories,
index: usize,
mission: &mut LoadedMissionState,
world: &World,
now_ms: u32,
) -> Result<bool, String> {
let order = &mut state.tasks[index];
let owner = OwnerId(order.clan as u16);
if !identity_metadata(world, order.lab).is_ok_and(|id| id.owner_id == Some(owner)) {
return Ok(false);
}
let seconds = now_ms.wrapping_sub(order.last_ms) as f32 * 0.001;
order.last_ms = now_ms;
let catalog = mission
.clan_ai_events
.research_catalogs
.get_mut(&order.clan)
.ok_or("research catalog disappeared")?;
let ore = mission
.behavior_ore_states
.iter_mut()
.find(|ore| ore.handle == order.lab)
.ok_or("laboratory ore state is unavailable")?;
let table = mission
.control_device_tables
.iter_mut()
.find(|table| table.handle == order.lab)
.ok_or("laboratory controls are unavailable")?;
let uses = state.rates.ok_or("laboratory rates are unavailable")?;
if order.current.is_none() {
let Some(row) = order.rows.iter().copied().find(|&row| {
catalog
.table
.flag_byte(row)
.is_some_and(|flag| flag & 3 == 1)
}) else {
return Ok(false);
};
let free = mission.free_research.take_free(order.slot);
let data = catalog
.table
.row_words(row)
.ok_or("research row is unavailable")?;
let ore_cost = if free { 0.0 } else { f32::from_bits(data[1]) };
let power_cost = if free { 0.0 } else { f32::from_bits(data[0]) };
if !ore_cost.is_finite() || !power_cost.is_finite() {
return Err("research costs are non-finite".into());
}
order.current = Some((
row,
Work {
ore_cost,
power_cost,
duration: order.duration,
..Default::default()
},
));
ore.current_ore_bits = 0;
ore.maximum_ore_bits = ore_cost.to_bits();
power(table, uses[0])?;
eprintln!("script_lab_technology_started clan={} lab={} row={row} ore={ore_cost} power={power_cost} free={free} queue_time_ms={now_ms}", order.clan, order.lab.slot);
}
let (row, work) = order.current.as_mut().unwrap();
if catalog
.table
.flag_byte(*row)
.is_some_and(|flag| flag & 2 != 0)
{
order.current = None;
return Ok(true);
}
let rate = crate::query_behavior_profile_ore_rate(table, 1.0_f32.to_bits())
.map_err(|error| format!("{error:?}"))?;
let progress = state.progress.entry((order.clan, *row)).or_default();
*progress = work.advance(seconds, rate, uses, ore, *progress);
if *progress + 0.001 >= 1.0 {
*progress = 1.0;
let changes = catalog
.table
.complete_research(*row)
.map_err(|error| error.to_string())?;
for row in changes.available {
*state.available.entry((order.clan, row)).or_default() += 1;
eprintln!(
"script_lab_technology_available clan={} row={row} queue_time_ms={now_ms}",
order.clan
);
}
for row in changes.learned {
mission.free_research.learned.insert((owner, row));
*state
.completed
.entry((order.clan, order.lab.slot, row))
.or_default() += 1;
eprintln!(
"script_lab_technology_researched clan={} lab={} row={row} queue_time_ms={now_ms}",
order.clan, order.lab.slot
);
}
order.current = None;
}
Ok(true)
}
fn power(table: &mut MissionControlDeviceTableState, demand: f32) -> Result<(), String> {
let crate::MissionControlDeviceTableStatus::Available(devices) = &table.status else {
return Err("laboratory control table is unavailable".into());
};
for index in 0..devices.len() {
crate::set_control_device_table_selector(table, index, 0x500, demand)
.map_err(|error| format!("{error:?}"))?;
}
Ok(())
}
fn load_rates(vfs: &dyn Vfs) -> Result<[f32; 2], String> {
let path = crate::normalize_engine_path("research profile", "behpsp.res")
.map_err(|error| error.to_string())?;
let data = vfs.read(&path).map_err(|error| error.to_string())?;
let archive = fparkan_assets::decode_nres_payload(data).map_err(|error| error.to_string())?;
let entry = archive
.find("prof_institute.var")
.ok_or("laboratory profile is unavailable")?;
let rates =
decode_behavior_work_rates(archive.payload(entry).map_err(|error| error.to_string())?)
.map_err(|error| error.to_string())?;
let values = [rates.power_bits, rates.ore_bits].map(f32::from_bits);
if values.iter().any(|rate| !rate.is_finite() || *rate < 0.0) {
return Err("invalid laboratory work rates".into());
}
Ok(values)
}
#[cfg(test)]
mod tests {
use super::*;
fn buffer(current: f32) -> BehaviorOreState {
BehaviorOreState {
original_id: fparkan_world::OriginalObjectId(0),
handle: ObjectHandle {
slot: 0,
generation: 0,
},
class_flags: LAB_CLASS,
current_ore_bits: current.to_bits(),
maximum_ore_bits: 0,
}
}
#[test]
fn native_paid_tick_uses_the_slowest_completion_fraction_and_requests_remaining_ore() {
let mut work = Work {
ore_cost: 10.0,
power_cost: 30.0,
duration: 10.0,
..Default::default()
};
let mut ore = buffer(4.0);
assert_eq!(work.advance(1.0, 2.0, [3.0, 1.0], &mut ore, 0.0), 0.1);
assert_eq!([work.ore, work.power, work.time], [2.0, 6.0, 1.0]);
assert_eq!(ore.current_ore_bits, 0);
assert_eq!(f32::from_bits(ore.maximum_ore_bits), 4.0);
}
#[test]
fn native_free_work_finishes_by_time_without_resource_supply() {
let mut work = Work {
duration: 5.0,
..Default::default()
};
let mut ore = buffer(0.0);
let progress = work.advance(1.0, 0.0, [3.0, 1.0], &mut ore, 0.0);
assert_eq!(progress, 0.2);
assert_eq!(work.advance(4.0, 0.0, [3.0, 1.0], &mut ore, progress), 1.0);
}
#[test]
#[ignore = "requires licensed corpus"]
fn licensed_paid_research_waits_for_control_power_and_resumes_after_supply() {
use crate::{
create, frame, load_gameplay_mission, EngineServices, MissionMode, MissionRequest,
MissionStartupInput,
};
use fparkan_vfs::DirectoryVfs;
use std::sync::Arc;
let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").unwrap();
let mut engine = create(EngineServices::new(Arc::new(DirectoryVfs::new(root)))).unwrap();
load_gameplay_mission(
&mut engine,
MissionRequest {
key: "MISSIONS/Single.01/data.tma".into(),
mode: MissionMode::SingleMission,
},
MissionStartupInput {
queue_time_ms: 0,
research_creation_gate: false,
},
None,
)
.unwrap();
let loaded = engine.loaded.as_mut().unwrap();
let lab = loaded
.control_device_tables
.iter()
.find(|table| {
table.original_id.is_some_and(|id| {
loaded.object_drafts[id.0 as usize].unit_dat_header_flags == Some(LAB_CLASS)
}) && identity_metadata(&engine.world, table.handle)
.unwrap()
.owner_id
== Some(OwnerId(1))
})
.unwrap()
.handle;
let slot = identity_metadata(&engine.world, lab)
.unwrap()
.original_id
.unwrap()
.0 as usize;
// Debug outage: consume free jobs, empty the local reserve and stop external generation.
while loaded.free_research.take_free(slot) {}
let supply = loaded
.behavior_profiles
.iter()
.map(|profile| profile.transfer_power_out_bits)
.collect::<Vec<_>>();
for profile in &mut loaded.behavior_profiles {
profile.transfer_power_out_bits = 0;
}
crate::set_control_device_table_multiplier(
loaded
.control_device_tables
.iter_mut()
.find(|table| table.handle == lab)
.unwrap(),
0.0,
)
.unwrap();
for now in (0..=1_000).step_by(50) {
frame(&mut engine, now).unwrap();
}
let loaded = engine.loaded.as_ref().unwrap();
assert!(
loaded
.control_resources
.ticks()
.any(|(unit, count)| unit == lab.slot && count > 0),
"laboratory Control clock must run"
);
assert!(!loaded
.control_resources
.stops()
.any(|(unit, _, _)| unit == lab.slot));
let task = loaded
.laboratories
.tasks
.iter()
.find(|task| task.lab == lab)
.unwrap();
let (row, work) = task.current.as_ref().expect("active paid research");
assert!(work.power_cost > 0.0);
let (row, before, elapsed) = (*row, work.power, work.time);
for now in (1_050..=2_000).step_by(50) {
frame(&mut engine, now).unwrap();
}
let loaded = engine.loaded.as_ref().unwrap();
let task = loaded
.laboratories
.tasks
.iter()
.find(|task| task.lab == lab)
.unwrap();
let (current, work) = task.current.as_ref().unwrap();
assert_eq!(*current, row);
assert_eq!(
work.power, before,
"no power work while the reserve and supply are empty"
);
assert!(work.time > elapsed);
let loaded = engine.loaded.as_mut().unwrap();
for (profile, bits) in loaded.behavior_profiles.iter_mut().zip(supply) {
profile.transfer_power_out_bits = bits;
}
for now in (2_050..=10_000).step_by(50) {
frame(&mut engine, now).unwrap();
let state = &engine.loaded.as_ref().unwrap().laboratories;
if state.completed.contains_key(&(1, lab.slot, row))
|| state
.tasks
.iter()
.find(|task| task.lab == lab)
.and_then(|task| task.current.as_ref())
.is_some_and(|(current, work)| *current == row && work.power > before)
{
return;
}
}
panic!("research power work must resume after restoring the original generation");
}
#[test]
#[ignore = "requires licensed corpus"]
fn licensed_stock_ai_laboratories_continue_research_and_keep_the_request_pending() {
use crate::{
create, frame, load_gameplay_mission, EngineServices, MissionMode, MissionRequest,
MissionStartupInput,
};
use fparkan_vfs::DirectoryVfs;
use std::sync::Arc;
let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").unwrap();
let vfs = Arc::new(DirectoryVfs::new(root));
assert_eq!(load_rates(vfs.as_ref()).unwrap(), [3.0, 1.0]);
let mut engine = create(EngineServices::new(vfs)).unwrap();
load_gameplay_mission(
&mut engine,
MissionRequest {
key: "MISSIONS/Single.02/data.tma".into(),
mode: MissionMode::SingleMission,
},
MissionStartupInput {
queue_time_ms: 0,
research_creation_gate: false,
},
None,
)
.unwrap();
for now in (0..=900_000).step_by(100) {
frame(&mut engine, now).unwrap();
let loaded = engine.loaded.as_ref().unwrap();
for task in &loaded.laboratories.tasks {
assert!(
loaded
.clan_ai_events
.pending_problems()
.any(|problem| problem == (task.clan, 10)),
"research request was removed while laboratory {} was busy at {now}",
task.lab.slot
);
}
}
let loaded = engine.loaded.as_ref().unwrap();
assert!(loaded.clan_ai_events.stops().next().is_none());
assert!(
!loaded.laboratories.completed.is_empty(),
"no completed laboratory technology"
);
assert!(!loaded.free_research.learned.is_empty());
assert!(loaded.laboratories.stopped.is_empty());
for clan in [1, 2] {
assert!(
loaded
.laboratories
.completed
.keys()
.filter(|&&(owner, _, _)| owner == clan)
.count()
>= 2,
"clan {clan} did not continue research after its first project"
);
}
assert!(
!loaded.laboratories.available.is_empty(),
"later projects did not unlock technologies"
);
for &(clan, _, row, _) in &loaded.laboratories.report().completed {
assert!(loaded
.free_research
.learned
.contains(&(OwnerId(clan as u16), row)));
assert_eq!(
loaded.clan_ai_events.research_catalogs[&clan]
.table
.flag_byte(row)
.unwrap()
& 2,
2
);
}
assert!(loaded
.clan_ai_events
.research_status_stubs()
.all(|(_, handler, _)| handler != 41));
println!(
"stock research completions={:?}",
loaded.laboratories.report().completed
);
}
}
|