aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/construction.rs
blob: 6074af1fe2ac3fa1bfd5c4dfad65228165d0ea78 (plain) (blame)
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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
use crate::unit_spawn::{spawn_prepared_unit, SpawnedUnit};
use crate::{LoadedMissionState, MissionScriptBundle};
use fparkan_path::ResourceName;
use fparkan_script::ScriptDispatchSelector;
use fparkan_terrain::SurfaceQuery;
use fparkan_world::{
    handle_by_original_id, identity_metadata, transform_state, ObjectHandle, OwnerId, World,
};

pub(super) const BUILDER_CLASS: u32 = 0x0100_4000;
const SLOTS_PER_CLASS: usize = 3;
const WAIT_MS: u32 = 1_000;
const ARRIVAL_RADIUS: f32 = 1.0;

pub(super) fn reserve_schemes(
    categories: &[crate::BuildCategory],
    bundles: &[MissionScriptBundle],
) -> Vec<ResourceName> {
    let enabled = bundles.iter().any(|bundle| {
        bundle.package.events.iter().any(|event| {
            event.name_raw == b"PBM_BUILDING_NEEDED_Start\0"
                && event.instructions.iter().any(|instruction| {
                    instruction.dispatch_selector() == ScriptDispatchSelector::Handler(18)
                })
        })
    });
    // shortcut: prepare a bounded number of buildings until runtime GPU instancing exists.
    categories
        .iter()
        .filter(move |category| enabled && supports_class(category.mask))
        .filter_map(|category| category.unit_paths.first())
        .flat_map(|path| (0..SLOTS_PER_CLASS).map(move |_| crate::resource_name(path.as_bytes())))
        .collect()
}

pub(super) fn supports_class(class: u32) -> bool {
    matches!(
        class,
        0x8000_0008 | 0x8000_0010 | 0x8000_0400 | 0x8010_0000 | 0x8020_0000
    )
}

#[derive(Default)]
pub(super) struct Construction {
    first_slot: usize,
    controls: Vec<crate::MissionControlDeviceTableState>,
    tasks: Vec<Task>,
    pub(super) supply_orders: std::collections::BTreeMap<(u16, u32, u32), u64>,
}

struct Task {
    builder: ObjectHandle,
    owner: OwnerId,
    slot: usize,
    class: u32,
    position: [f32; 3],
    cost: f32,
    arrived_at: Option<u32>,
    supply: Option<crate::builder_supply::BuilderSupply>,
}

impl Construction {
    pub(super) fn new(
        first_slot: usize,
        controls: Vec<crate::MissionControlDeviceTableState>,
    ) -> Self {
        Self {
            first_slot,
            controls,
            tasks: Vec::new(),
            supply_orders: Default::default(),
        }
    }

    pub(super) fn active(&self, builder: ObjectHandle) -> bool {
        self.tasks.iter().any(|task| task.builder == builder)
    }

    pub(super) fn pending_count(&self, owner: OwnerId, class: u32) -> usize {
        self.tasks
            .iter()
            .filter(|task| task.owner == owner && task.class == class)
            .count()
    }

    fn available_slot(
        &self,
        world: &World,
        drafts: &[crate::MissionObjectDraft],
        class: u32,
    ) -> Option<usize> {
        (self.first_slot..self.first_slot + self.controls.len()).find(|&index| {
            let draft = &drafts[index];
            draft.unit_dat_header_flags == Some(class)
                && draft
                    .original_id
                    .is_some_and(|id| handle_by_original_id(world, id).is_none())
                && !self.tasks.iter().any(|task| task.slot == index)
        })
    }

    fn start(
        &mut self,
        builder: ObjectHandle,
        owner: OwnerId,
        slot: usize,
        class: u32,
        position: [f32; 3],
        cost: f32,
        supply: Option<crate::builder_supply::BuilderSupply>,
    ) {
        if let Some(supply) = &supply {
            *self
                .supply_orders
                .entry((owner.0, builder.slot, supply.source.slot))
                .or_default() += 1;
            eprintln!(
                "sim_coverage script_builder_supply_approx=1 clan={} builder={} source={}",
                owner.0, builder.slot, supply.source.slot
            );
        }
        self.tasks.push(Task {
            builder,
            owner,
            slot,
            class,
            position,
            cost,
            arrived_at: None,
            supply,
        });
    }
}

pub(super) fn order(
    construction: &mut Construction,
    patrol: &mut Option<crate::unit_patrol::UnitPatrol>,
    world: &World,
    terrain: &fparkan_terrain::TerrainWorld,
    mission: &crate::MissionDocument,
    drafts: &[crate::MissionObjectDraft],
    mine_sites: &[crate::mining::MineSite],
    ores: &[crate::BehaviorOreState],
    research: &crate::research::FreeResearch,
    vfs: Option<&dyn fparkan_vfs::Vfs>,
    builder: Option<ObjectHandle>,
    owner: OwnerId,
    class: u32,
    xy: [u32; 2],
) -> Result<(), String> {
    let builder = builder.ok_or("builder is unavailable")?;
    if construction.active(builder) {
        return Err("builder is busy".into());
    }
    if patrol
        .as_ref()
        .is_some_and(|patrol| patrol.is_shut_down(builder))
    {
        return Err("builder is shut down".into());
    }
    let cargo = ores
        .iter()
        .find(|ore| ore.handle == builder && ore.class_flags == BUILDER_CLASS)
        .ok_or("unit has no builder ore state")?;
    let slot = if class == crate::mining::MINE_CLASS {
        mine_sites.iter().find_map(|site| {
            let point = [site.position[0], site.position[1]].map(|axis| axis.trunc() as i64 as u32);
            let id = site.control.as_ref()?.original_id?;
            (point == xy && site.live_mine(world).is_none()).then_some(id.0 as usize)
        })
    } else {
        construction.available_slot(world, drafts, class)
    }
    .filter(|&slot| !construction.tasks.iter().any(|task| task.slot == slot))
    .ok_or("no free prepared building slot")?;
    let table = crate::research::load_technology_table(
        vfs.ok_or("no clan technology provider")?,
        mission,
        owner,
    )
    .map_err(|err| err.to_string())?;
    let cost = crate::production::scheme_cost(&drafts[slot].unit_components, &table, |row| {
        research.learned.contains(&(owner, row))
    })
    .map_err(|err| err.to_string())?
    .ore;
    let stock = f32::from_bits(cargo.current_ore_bits);
    if !stock.is_finite() {
        return Err("builder has non-finite ore".into());
    }
    let supply =
        crate::builder_supply::BuilderSupply::plan(world, ores, builder, owner, class, stock)?;
    let from = transform_state(world, builder)
        .map_err(|err| err.to_string())?
        .position
        .map(f32::from_bits);
    let position = destination(terrain, from, xy).ok_or("no route to the building site")?;
    if supply.is_none() {
        patrol
            .get_or_insert_with(crate::unit_patrol::UnitPatrol::scripted)
            .move_to(world, builder, [position[0], position[1]], false);
    }
    construction.start(builder, owner, slot, class, position, cost, supply);
    Ok(())
}

pub(super) fn tick(
    mission: &mut LoadedMissionState,
    world: &mut World,
    mut production: Option<&mut crate::production::Production>,
    now_ms: u32,
) -> Vec<SpawnedUnit> {
    let mut constructed = Vec::new();
    let mut index = 0;
    while let Some(task) = mission.construction.tasks.get_mut(index) {
        let owned =
            identity_metadata(world, task.builder).is_ok_and(|id| id.owner_id == Some(task.owner));
        let running = !mission
            .unit_patrol
            .as_ref()
            .is_some_and(|patrol| patrol.is_shut_down(task.builder));
        let mut finish = false;
        if let Some(pose) = transform_state(world, task.builder)
            .ok()
            .filter(|_| owned && running)
        {
            if let Some(supply) = &mut task.supply {
                match supply.tick(
                    world,
                    &mut mission.behavior_ore_states,
                    &mut mission.unit_patrol,
                    task.builder,
                    task.owner,
                    task.cost,
                    now_ms,
                ) {
                    Ok(ready) => {
                        if ready {
                            task.supply = None;
                            mission
                                .unit_patrol
                                .get_or_insert_with(crate::unit_patrol::UnitPatrol::scripted)
                                .move_to(
                                    world,
                                    task.builder,
                                    [task.position[0], task.position[1]],
                                    false,
                                );
                            eprintln!(
                                "script_builder_supplied clan={} builder={} queue_time_ms={now_ms}",
                                task.owner.0, task.builder.slot
                            );
                        }
                        index += 1;
                        continue;
                    }
                    Err(reason) => eprintln!(
                        "script_builder_supply_failed clan={} builder={} reason={reason}",
                        task.owner.0, task.builder.slot
                    ),
                }
            } else {
                let [x, y, _] = pose.position.map(f32::from_bits);
                let [a, b, _] = task.position;
                if (x - a).hypot(y - b) > ARRIVAL_RADIUS {
                    task.arrived_at = None;
                    if mission
                        .unit_patrol
                        .as_ref()
                        .is_some_and(|patrol| patrol.is_moving_to_destination(task.builder))
                    {
                        index += 1;
                        continue;
                    }
                } else {
                    let arrived_at = *task.arrived_at.get_or_insert(now_ms);
                    if now_ms.wrapping_sub(arrived_at) < WAIT_MS {
                        index += 1;
                        continue;
                    }
                    finish = true;
                }
            }
        }
        let task = mission.construction.tasks.remove(index);
        if let Some(patrol) = &mut mission.unit_patrol {
            patrol.clear_move(task.builder);
        }
        if finish {
            match complete(mission, world, &task, production.as_deref_mut()) {
                Ok(building) => constructed.push(building),
                Err(error) => eprintln!(
                    "script_building_construction_failed clan={} builder={} reason={error}",
                    task.owner.0, task.builder.slot
                ),
            }
        }
    }
    constructed
}

fn complete(
    mission: &mut LoadedMissionState,
    world: &mut World,
    task: &Task,
    production: Option<&mut crate::production::Production>,
) -> Result<SpawnedUnit, crate::EngineError> {
    let index = crate::behavior_ore_index_for_handle(&mission.behavior_ore_states, task.builder)
        .map_err(crate::EngineError::Production)?;
    let stock = f32::from_bits(mission.behavior_ore_states[index].current_ore_bits);
    if !stock.is_finite() {
        return Err(crate::EngineError::Production(
            "builder has non-finite ore".into(),
        ));
    }
    let [x, y, _] = task.position;
    if !crate::construction_site::building_centers(world, &mission.object_drafts)
        .all(|other| crate::construction_site::separated([x, y], other))
    {
        return Err(crate::EngineError::Production(
            "building site is occupied".into(),
        ));
    }
    let draft = &mission.object_drafts[task.slot];
    let id = draft.original_id.expect("prepared building has an ID");
    let class = draft
        .unit_dat_header_flags
        .expect("prepared building has a class");
    let profile = mission
        .behavior_profiles
        .iter()
        .position(|profile| profile.original_id == id);
    let control = if class == crate::mining::MINE_CLASS {
        mission
            .mine_sites
            .iter()
            .filter_map(|site| site.control.as_ref())
            .find(|control| control.original_id == Some(id))
            .cloned()
            .ok_or_else(|| crate::EngineError::Production("mine control is unavailable".into()))?
    } else {
        mission.construction.controls[task.slot - mission.construction.first_slot].clone()
    };
    let handle = spawn_prepared_unit(
        world,
        mission,
        task.slot,
        task.owner.0,
        task.position,
        control,
    )?;
    mission.behavior_ore_states[index].current_ore_bits = (stock - task.cost).to_bits();
    mission
        .behavior_ore_states
        .retain(|ore| ore.original_id != id);
    mission
        .behavior_ore_states
        .push(crate::initialize_behavior_ore_state(id, handle, class, &[]));
    if let Some(profile) = profile {
        mission.behavior_profiles[profile].handle = handle;
    }
    if let Some(distributor) = mission.behavior_distributors.get_mut(task.owner.0 as usize) {
        distributor
            .registered_objects
            .retain(|entry| entry.original_id != id);
        crate::add_behavior_registered_object(
            distributor,
            crate::BehaviorRegisteredObject {
                original_id: id,
                handle,
                selector: u32::from(task.owner.0),
                class_flags: class,
                share: 0.0,
                native_tail: [0; 5],
            },
        );
    }
    mission
        .mission_events
        .issue_logic_id(&mission.mission, world, handle);
    if let Some(production) = production {
        production.register_factories(task.slot, &mission.object_drafts[task.slot..=task.slot]);
    }
    eprintln!(
        "script_building_constructed clan={} builder={} building={} class={class:#010x}",
        task.owner.0, task.builder.slot, handle.slot
    );
    Ok(SpawnedUnit {
        object_index: task.slot,
        handle,
        clan: task.owner.0,
    })
}

fn destination(
    terrain: &fparkan_terrain::TerrainWorld,
    from: [f32; 3],
    xy: [u32; 2],
) -> Option<[f32; 3]> {
    let [x, y] = xy.map(|x| x as f32);
    let position = [x, y, terrain.height_at([x, y]).ok()??];
    crate::route_motion::route_points(terrain, from, position).ok()??;
    Some(position)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        create, load_gameplay_mission, EngineServices, MissionMode, MissionRequest,
        MissionStartupInput,
    };
    use fparkan_vfs::DirectoryVfs;
    use std::sync::Arc;

    #[test]
    #[ignore = "requires licensed corpus"]
    fn licensed_stock_builder_resupplies_and_finishes_another_building() {
        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.02/data.tma".into(),
                mode: MissionMode::SingleMission,
            },
            MissionStartupInput {
                queue_time_ms: 0,
                research_creation_gate: false,
            },
            None,
        )
        .unwrap();
        let mut supplied = None;
        for now in (0..=900_000).step_by(100) {
            crate::frame(&mut engine, now).unwrap();
            let loaded = engine.loaded.as_ref().unwrap();
            assert!(loaded.clan_ai_events.stops().next().is_none());
            if supplied.is_none() {
                if let Some(task) = loaded
                    .construction
                    .tasks
                    .iter()
                    .find(|task| task.supply.is_some())
                {
                    let source = task.supply.as_ref().unwrap().source;
                    assert_eq!(
                        identity_metadata(&engine.world, source).unwrap().owner_id,
                        Some(task.owner)
                    );
                    let ore = loaded
                        .behavior_ore_states
                        .iter()
                        .find(|ore| ore.handle == task.builder)
                        .unwrap();
                    assert!(f32::from_bits(ore.current_ore_bits) < 100.0);
                    supplied = Some((
                        loaded.object_drafts[task.slot].original_id.unwrap(),
                        task.builder,
                    ));
                }
            }
            if let Some((id, builder)) = supplied {
                if let Some(building) = handle_by_original_id(&engine.world, id) {
                    assert_eq!(
                        identity_metadata(&engine.world, building).unwrap().owner_id,
                        identity_metadata(&engine.world, builder).unwrap().owner_id
                    );
                    assert!(loaded
                        .construction
                        .supply_orders
                        .keys()
                        .any(|&(_, slot, _)| slot == builder.slot));
                    println!(
                        "stock builder {} resupplied and completed {} at {now} ms",
                        builder.slot, building.slot
                    );
                    return;
                }
            }
        }
        panic!("stock builders did not resupply and complete another building");
    }

    #[test]
    #[ignore = "requires licensed corpus"]
    fn licensed_stock_clan_builds_a_working_factory() {
        let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").unwrap();
        let vfs = Arc::new(DirectoryVfs::new(root));
        let mut engine = create(EngineServices::new(vfs.clone())).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();
        let loaded = engine.loaded.as_ref().unwrap();
        let start = loaded.construction.first_slot;
        let end = start + loaded.construction.controls.len();
        for owner in [OwnerId(1), OwnerId(2)] {
            let table =
                crate::research::load_technology_table(vfs.as_ref(), &loaded.mission, owner)
                    .unwrap();
            for slot in (start..end).step_by(SLOTS_PER_CLASS) {
                let draft = &loaded.object_drafts[slot];
                let cost =
                    crate::production::scheme_cost(&draft.unit_components, &table, |_| false);
                match draft.unit_dat_header_flags.unwrap() {
                    0x8000_0008 => assert_eq!(cost.unwrap().ore, 670.0),
                    0x8000_0010 => assert_eq!(cost.unwrap().ore, 770.0),
                    0x8000_0400 => assert_eq!(cost.unwrap().ore, 560.0),
                    _ => assert!(cost.is_err()),
                }
            }
        }
        for now in (0..=900_000).step_by(100) {
            let frame = crate::frame(&mut engine, now).unwrap();
            let loaded = engine.loaded.as_ref().unwrap();
            if let Some(handle) = loaded.object_drafts[start..end].iter().find_map(|draft| {
                (draft.unit_dat_header_flags == Some(0x8000_0010))
                    .then(|| handle_by_original_id(&engine.world, draft.original_id.unwrap()))
                    .flatten()
            }) {
                let building = frame
                    .constructed_buildings
                    .iter()
                    .find(|building| building.handle == handle)
                    .expect("first live factory must be published in the same frame");
                assert!(frame.snapshot.objects.contains(&handle));
                let pose = transform_state(&engine.world, handle).unwrap();
                let mut draft = loaded.object_drafts[building.object_index].clone();
                draft.position = pose.position.map(f32::from_bits);
                draft.orientation_raw = pose.orientation.map(f32::from_bits);
                draft.scale = pose.scale.map(f32::from_bits);
                let footprint = crate::mission_basement_footprint(
                    &loaded.mission_assets,
                    building.object_index,
                    &draft,
                )
                .unwrap()
                .expect("the constructed factory has BAS rings at its live pose");
                let mut footprints = crate::mission_basement_footprints(
                    &loaded.mission_assets,
                    &loaded.object_drafts[..loaded.summary.object_count],
                )
                .unwrap();
                footprints.push(footprint);
                let rings: Vec<_> = footprints
                    .iter()
                    .map(crate::MissionBasementFootprint::contours)
                    .collect();
                let foundation = fparkan_terrain::build_basement_foundations(
                    loaded.terrain.source_mesh().unwrap(),
                    &rings,
                )
                .expect("live factory footprint fits the stock terrain");
                let cut_terrain = loaded
                    .terrain
                    .clone()
                    .with_basement_foundation(foundation)
                    .unwrap();
                let [x, y, _] = draft.position;
                assert!(loaded.terrain.support_at([x, y]).unwrap().is_some());
                assert!(cut_terrain.support_at([x, y]).unwrap().is_none());
                assert_eq!(
                    identity_metadata(&engine.world, handle).unwrap().owner_id,
                    Some(OwnerId(1))
                );
                assert!(loaded
                    .control_device_tables
                    .iter()
                    .any(|table| table.handle == handle));
                assert!(loaded
                    .behavior_ore_states
                    .iter()
                    .any(|ore| ore.handle == handle && ore.class_flags == 0x8000_0010));
                let result = crate::production::order_script_factory(
                    &mut engine,
                    OwnerId(1),
                    handle,
                    crate::clan_factory_schemes::TRANSPORT_CLASS,
                    crate::clan_factory_schemes::SELECT_FASTEST,
                    now,
                );
                assert_eq!(result.unwrap(), 60_000);
                return;
            }
        }
        panic!("clans did not complete any building from the stock start");
    }

    #[test]
    #[ignore = "requires licensed corpus"]
    fn tower_order_rejects_unresearched_parts_and_preserves_native_ore_debt() {
        let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").unwrap();
        let vfs = Arc::new(DirectoryVfs::new(root));
        let mut engine = create(EngineServices::new(vfs.clone())).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 owner = OwnerId(1);
        let class = 0x8010_0000;
        let mission = engine.loaded.as_mut().unwrap();
        let builder_ore = mission
            .behavior_ore_states
            .iter()
            .position(|ore| {
                ore.class_flags == BUILDER_CLASS
                    && identity_metadata(&engine.world, ore.handle)
                        .is_ok_and(|id| id.owner_id == Some(owner))
            })
            .unwrap();
        let builder = mission.behavior_ore_states[builder_ore].handle;
        let site = crate::construction_site::SiteReservations::default()
            .find(
                &mission.terrain,
                &engine.world,
                &mission.object_drafts,
                &mission.mine_sites,
                mission.mission.clans[1].anchor.map(f32::round_ties_even),
                7_000,
            )
            .unwrap();
        let slot = mission
            .construction
            .available_slot(&engine.world, &mission.object_drafts, class)
            .unwrap();
        let id = mission.object_drafts[slot].original_id.unwrap();
        let order_for = |mission: &mut LoadedMissionState, world: &World| {
            order(
                &mut mission.construction,
                &mut mission.unit_patrol,
                world,
                &mission.terrain,
                &mission.mission,
                &mission.object_drafts,
                &mission.mine_sites,
                &mission.behavior_ore_states,
                &mission.free_research,
                Some(vfs.as_ref()),
                Some(builder),
                owner,
                class,
                site,
            )
        };
        assert!(order_for(mission, &engine.world)
            .unwrap_err()
            .contains("unavailable for construction"));
        let table =
            crate::research::load_technology_table(vfs.as_ref(), &mission.mission, owner).unwrap();
        // Debug setup isolates the native payment rule after the stock research gate.
        for component in &mission.object_drafts[slot].unit_components {
            if let Some(row) = table.find_key(&component.resource_raw).unwrap() {
                mission.free_research.learned.insert((owner, row));
            }
        }
        let stock_before = 500.0_f32;
        mission.behavior_ore_states[builder_ore].current_ore_bits = stock_before.to_bits();
        order_for(mission, &engine.world).unwrap();
        assert!(mission.construction.active(builder));
        assert_eq!(mission.construction.pending_count(owner, class), 1);
        assert_eq!(mission.construction.pending_count(OwnerId(0), class), 0);
        assert!(handle_by_original_id(&engine.world, id).is_none());
        let cost = mission.construction.tasks[0].cost;
        assert_eq!(cost, 1100.0);
        let pose = transform_state(&engine.world, builder).unwrap();
        fparkan_world::set_transform(
            &mut engine.world,
            builder,
            fparkan_world::TransformState {
                position: mission.construction.tasks[0].position.map(f32::to_bits),
                ..pose
            },
        )
        .unwrap();
        tick(mission, &mut engine.world, None, 8_000);
        assert!(handle_by_original_id(&engine.world, id).is_none());
        tick(mission, &mut engine.world, None, 9_000);
        let building = handle_by_original_id(&engine.world, id).unwrap();
        assert_eq!(
            identity_metadata(&engine.world, building).unwrap().owner_id,
            Some(owner)
        );
        assert_eq!(
            f32::from_bits(mission.behavior_ore_states[builder_ore].current_ore_bits),
            stock_before - cost
        );
        assert!(!mission.construction.active(builder));
        assert_eq!(mission.construction.pending_count(owner, class), 0);
    }
}