aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-11-25 14:41:21 +0300
committerValentin Popov <valentin@popov.link>2026-11-25 14:41:21 +0300
commit7a7001c96afdbe0e5d574337bec476f230692477 (patch)
tree74d887cfb724d06aee67c61dfa5421b9e64c1408 /crates/fparkan-runtime
parent27d6ea19b0206b71ef6c309142e0875275882bf0 (diff)
downloadfparkan-7a7001c96afdbe0e5d574337bec476f230692477.tar.xz
fparkan-7a7001c96afdbe0e5d574337bec476f230692477.zip
feat(ai): construct clan factories, storage and laboratories
Allow ORDER_ROBOT_BUILD to use the first BuildDat scheme for factories, storage and laboratories, with the existing technology and onboard-ore gates. Register completed factories for production and reset their free-build quota when a prepared slot is reused. Exclude reserved models from initial BAS cuts using the mission object count, preserving placed neutral buildings. Validation: fmt, workspace check and tests; ignored licensed stock-start test builds a factory in mission 29 and queues a transport there. Original Behavior.dll price calls confirm 670/770/560 ore for storage/factory/laboratory. Stock simulation also builds a laboratory; a validation-enabled window reaches the first factory. The new buildings were outside the player's window view; visual construction parity is not established. G7 still needs mine query 40, research transition 8, builder resupply and live BAS updates.
Diffstat (limited to 'crates/fparkan-runtime')
-rw-r--r--crates/fparkan-runtime/src/clan_ai_events.rs11
-rw-r--r--crates/fparkan-runtime/src/construction.rs103
-rw-r--r--crates/fparkan-runtime/src/production.rs40
3 files changed, 128 insertions, 26 deletions
diff --git a/crates/fparkan-runtime/src/clan_ai_events.rs b/crates/fparkan-runtime/src/clan_ai_events.rs
index 67919fd3..8ac5b238 100644
--- a/crates/fparkan-runtime/src/clan_ai_events.rs
+++ b/crates/fparkan-runtime/src/clan_ai_events.rs
@@ -88,7 +88,12 @@ pub(super) fn tick(engine: &mut crate::Engine, now_ms: u32) -> Vec<MissionNotice
let Some(loaded) = &mut engine.loaded else {
return notices;
};
- crate::construction::tick(loaded, &mut engine.world, now_ms);
+ crate::construction::tick(
+ loaded,
+ &mut engine.world,
+ engine.production.as_mut(),
+ now_ms,
+ );
loaded.clan_ai_events.groups.retain(|&(clan, _), group| {
if !group.target.valid(&engine.world) {
if let Some(patrol) = &mut loaded.unit_patrol {
@@ -694,9 +699,9 @@ impl LoadedMissionState {
(order.word_1, order.word_2, order.target)
{
if !problem.as_ref().is_some_and(|p| p.key.word_0 == BUILDING_NEEDED)
- || !matches!(order.word_3, 0x8010_0000 | 0x8020_0000)
+ || !crate::construction::supports_class(order.word_3)
{
- return Err("ORDER_ROBOT_BUILD requires a supported defensive tower scheme".into());
+ return Err("ORDER_ROBOT_BUILD requires a supported building scheme".into());
}
let owner = OwnerId(clan as u16);
let builder = self.mission_events.logic_objects(&self.mission, world, order.word_0)
diff --git a/crates/fparkan-runtime/src/construction.rs b/crates/fparkan-runtime/src/construction.rs
index f85d53d1..3f347d9f 100644
--- a/crates/fparkan-runtime/src/construction.rs
+++ b/crates/fparkan-runtime/src/construction.rs
@@ -24,15 +24,22 @@ pub(super) fn reserve_schemes(
})
})
});
- // shortcut: prepare the defensive tower schemes exercised by the site query.
+ // shortcut: prepare a bounded number of buildings until runtime GPU instancing exists.
categories
.iter()
- .filter(move |category| enabled && matches!(category.mask, 0x8010_0000 | 0x8020_0000))
+ .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,
@@ -141,7 +148,7 @@ pub(super) fn order(
.ok_or("unit has no builder ore state")?;
let slot = construction
.available_slot(world, drafts, class)
- .ok_or("no free prepared tower slot")?;
+ .ok_or("no free prepared building slot")?;
let table = crate::research::load_technology_table(
vfs.ok_or("no clan technology provider")?,
mission,
@@ -170,7 +177,12 @@ pub(super) fn order(
Ok(())
}
-pub(super) fn tick(mission: &mut LoadedMissionState, world: &mut World, now_ms: u32) {
+pub(super) fn tick(
+ mission: &mut LoadedMissionState,
+ world: &mut World,
+ mut production: Option<&mut crate::production::Production>,
+ now_ms: u32,
+) {
let mut index = 0;
while let Some(task) = mission.construction.tasks.get_mut(index) {
let owned =
@@ -210,7 +222,7 @@ pub(super) fn tick(mission: &mut LoadedMissionState, world: &mut World, now_ms:
patrol.clear_move(task.builder);
}
if finish {
- if let Err(error) = complete(mission, world, &task) {
+ if let Err(error) = complete(mission, world, &task, production.as_deref_mut()) {
eprintln!(
"script_building_construction_failed clan={} builder={} reason={error}",
task.owner.0, task.builder.slot
@@ -224,6 +236,7 @@ fn complete(
mission: &mut LoadedMissionState,
world: &mut World,
task: &Task,
+ production: Option<&mut crate::production::Production>,
) -> Result<(), crate::EngineError> {
let index = crate::behavior_ore_index_for_handle(&mission.behavior_ore_states, task.builder)
.map_err(crate::EngineError::Production)?;
@@ -289,6 +302,9 @@ fn complete(
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
@@ -319,6 +335,79 @@ mod tests {
#[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) {
+ 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()
+ }) {
+ 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_transport_schemes::TRANSPORT_CLASS,
+ crate::clan_transport_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_spawns_when_available() {
let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").unwrap();
let vfs = Arc::new(DirectoryVfs::new(root));
@@ -415,9 +504,9 @@ mod tests {
},
)
.unwrap();
- tick(mission, &mut engine.world, 8_000);
+ tick(mission, &mut engine.world, None, 8_000);
assert!(handle_by_original_id(&engine.world, id).is_none());
- tick(mission, &mut engine.world, 9_000);
+ 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,
diff --git a/crates/fparkan-runtime/src/production.rs b/crates/fparkan-runtime/src/production.rs
index c144eb43..2375cf9d 100644
--- a/crates/fparkan-runtime/src/production.rs
+++ b/crates/fparkan-runtime/src/production.rs
@@ -170,29 +170,37 @@ impl Production {
"build scheme must be a mobile Unit DAT".into(),
));
}
- let plants = drafts[..reserve_start]
- .iter()
- .enumerate()
- .filter(|(_, draft)| draft.unit_dat_header_flags == Some(PLANT_CLASS))
- .map(|(index, draft)| {
- let count = draft
- .properties
- .iter()
- .find(|property| property.name_raw == b"FreeBotNum")
- .map_or(1, |property| property.raw_value[1] as i32);
- (index, count.max(0) as u32)
- })
- .collect();
- Ok(Self {
+ let mut production = Self {
reserve_start,
schemes,
player_scheme_count,
transport_schemes,
controls,
- plants,
+ plants: Vec::new(),
pending: VecDeque::new(),
rng: 1,
- })
+ };
+ production.register_factories(0, &drafts[..reserve_start]);
+ Ok(production)
+ }
+
+ pub(super) fn register_factories(&mut self, first_slot: usize, drafts: &[MissionObjectDraft]) {
+ self.plants
+ .retain(|&(index, _)| !(first_slot..first_slot + drafts.len()).contains(&index));
+ self.plants.extend(
+ drafts
+ .iter()
+ .enumerate()
+ .filter(|(_, draft)| draft.unit_dat_header_flags == Some(PLANT_CLASS))
+ .map(|(index, draft)| {
+ let count = draft
+ .properties
+ .iter()
+ .find(|property| property.name_raw == b"FreeBotNum")
+ .map_or(1, |property| property.raw_value[1] as i32);
+ (first_slot + index, count.max(0) as u32)
+ }),
+ );
}
pub(super) fn tick(