aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/fparkan-assets/src/strategic_factory.rs64
-rw-r--r--crates/fparkan-runtime/src/lib.rs4
-rw-r--r--crates/fparkan-runtime/src/production.rs26
-rw-r--r--crates/fparkan-runtime/src/production/strategic.rs46
4 files changed, 110 insertions, 30 deletions
diff --git a/crates/fparkan-assets/src/strategic_factory.rs b/crates/fparkan-assets/src/strategic_factory.rs
index 8120c8cd..9eac9721 100644
--- a/crates/fparkan-assets/src/strategic_factory.rs
+++ b/crates/fparkan-assets/src/strategic_factory.rs
@@ -18,6 +18,7 @@ const ROW_PITCH: u32 = 20;
const ROW_HEIGHT: u32 = 19;
pub const VISIBLE_PLANTS: usize = ((EXTENT[1] - ROW_TOP) / ROW_PITCH) as usize;
const BUILD: [u32; 4] = [263, 150, 313, 169];
+const CONSTRUCTOR: [u32; 4] = [5, 150, 55, 169];
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FactoryAction {
@@ -25,6 +26,7 @@ pub enum FactoryAction {
Project(usize),
Build,
Cancel,
+ Designer,
Plant(usize),
}
@@ -57,6 +59,7 @@ pub fn hit_test(
plants: usize,
projects: usize,
order_button: FactoryOrderButton,
+ designer_available: bool,
) -> Option<FactoryAction> {
let [x, y] = [x.round_ties_even() - ORIGIN[0] as f32, y.round_ties_even()];
let hit = |[left, top, right, bottom]: [u32; 4]| {
@@ -72,6 +75,9 @@ pub fn hit_test(
FactoryOrderButton::Hidden => None,
};
}
+ if designer_available && hit(CONSTRUCTOR) {
+ return Some(FactoryAction::Designer);
+ }
if let Some(index) = (0..projects).find(|&index| hit(project_rect(index))) {
return Some(FactoryAction::Project(index));
}
@@ -97,6 +103,7 @@ pub struct FactoryPicture<'a> {
pub progress: Option<u8>,
pub free_minds: u32,
pub order_button: FactoryOrderButton,
+ pub designer_available: bool,
}
pub struct FactoryImages {
@@ -105,7 +112,7 @@ pub struct FactoryImages {
row_cap: [RgbaImage; 2],
row_body: RgbaImage,
ray: RgbaImage,
- icons: [RgbaImage; 6],
+ icons: [RgbaImage; 7],
}
impl FactoryImages {
@@ -128,7 +135,7 @@ impl FactoryImages {
"ccres_frame_edge_v",
],
)?;
- let [exit, project, project_frame, build, stop, free] = load_ui_sprites(
+ let [exit, project, project_frame, build, stop, constructor, free] = load_ui_sprites(
vfs,
"ui/hq.cfg",
[
@@ -137,6 +144,7 @@ impl FactoryImages {
"short_button_frame_off",
"build_icon",
"stop_build_icon",
+ "constructor_icon",
"brain_icon",
],
)?;
@@ -178,13 +186,21 @@ impl FactoryImages {
row_cap: [row_off, row_on],
row_body: body,
ray,
- icons: [project, project_frame, plant, build, stop, free],
+ icons: [
+ project,
+ project_frame,
+ plant,
+ build,
+ stop,
+ constructor,
+ free,
+ ],
})
}
pub fn paint(&self, picture: FactoryPicture<'_>) -> Result<RgbaImage, String> {
let mut image = self.base.clone();
- let [project, frame, plant, build, stop, free] = &self.icons;
+ let [project, frame, plant, build, stop, constructor, free] = &self.icons;
if let Some(name) = picture.project_name {
let width = u32::from(self.font.line_metrics(name)?[0]).min(170);
text(
@@ -219,13 +235,13 @@ impl FactoryImages {
[289, 125, 306, 144],
[128, 255, 128],
)?;
- stamp(&mut image, &self.row_body, [5, 150, 263, 169], [255; 3]);
+ stamp(&mut image, &self.row_body, [55, 150, 263, 169], [255; 3]);
if let Some(progress) = picture.progress {
text(
&self.font,
&mut image,
&format!("{progress}%"),
- [5, 150, 263, 169],
+ [55, 150, 263, 169],
[255; 3],
)?;
}
@@ -234,14 +250,17 @@ impl FactoryImages {
FactoryOrderButton::Stop => Some(stop),
FactoryOrderButton::Hidden => None,
};
- if let Some(icon) = order_icon {
- stamp(&mut image, &self.row_body, BUILD, [255; 3]);
- stamp(
- &mut image,
- icon,
- [BUILD[0] + 10, BUILD[1] + 2, BUILD[0] + 40, BUILD[1] + 17],
- [255; 3],
- );
+ for (rect, icon) in [
+ (
+ CONSTRUCTOR,
+ picture.designer_available.then_some(constructor),
+ ),
+ (BUILD, order_icon),
+ ] {
+ let Some(icon) = icon else { continue };
+ let [x, y, _, _] = rect;
+ stamp(&mut image, &self.row_body, rect, [255; 3]);
+ stamp(&mut image, icon, [x + 10, y + 2, x + 40, y + 17], [255; 3]);
}
for (index, row) in picture.rows.iter().take(VISIBLE_PLANTS).enumerate() {
let y = ROW_TOP + index as u32 * ROW_PITCH;
@@ -293,23 +312,28 @@ mod tests {
use FactoryOrderButton::{Hidden, Start, Stop};
assert_eq!(
- hit_test([95.0, 132.0], 1, 2, Start),
+ hit_test([95.0, 132.0], 1, 2, Start, true),
Some(FactoryAction::Project(1))
);
- assert_eq!(hit_test([95.0, 132.0], 1, 1, Start), None);
+ assert_eq!(hit_test([95.0, 132.0], 1, 1, Start, true), None);
assert_eq!(
- hit_test([340.0, 160.0], 1, 2, Start),
+ hit_test([340.0, 160.0], 1, 2, Start, true),
Some(FactoryAction::Build)
);
- assert_eq!(hit_test([340.0, 160.0], 1, 2, Hidden), None);
+ assert_eq!(hit_test([340.0, 160.0], 1, 2, Hidden, true), None);
assert_eq!(FactoryOrderButton::for_plant(true, false, 0), Stop);
assert_eq!(
- hit_test([340.0, 160.0], 1, 0, Stop),
+ hit_test([340.0, 160.0], 1, 0, Stop, true),
Some(FactoryAction::Cancel)
);
assert_eq!(
- hit_test([200.0, 172.0], 1, 2, Start),
+ hit_test([200.0, 172.0], 1, 2, Start, true),
Some(FactoryAction::Plant(0))
);
+ assert_eq!(hit_test([80.0, 160.0], 1, 0, Hidden, false), None);
+ assert_eq!(
+ hit_test([80.0, 160.0], 1, 0, Hidden, true),
+ Some(FactoryAction::Designer)
+ );
}
}
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs
index f073672e..c72f5552 100644
--- a/crates/fparkan-runtime/src/lib.rs
+++ b/crates/fparkan-runtime/src/lib.rs
@@ -215,8 +215,8 @@ pub use production::{
cancel_clan_factory_order, clan_factory_schemes, factory_panel_plants, factory_panel_project,
factory_part_variants, factory_project_components, factory_project_root,
factory_project_schemes, factory_saved_schemes, factory_schemes, order_clan_factory_scheme,
- order_factory_design, order_factory_robot, order_factory_scheme, FactoryPanelPlant,
- FactoryPanelProject,
+ order_factory_design, order_factory_robot, order_factory_scheme,
+ remember_strategic_factory_project, FactoryPanelPlant, FactoryPanelProject,
};
pub use projectile::{
apply_projectile_contact, apply_projectile_hit, apply_projectile_impact,
diff --git a/crates/fparkan-runtime/src/production.rs b/crates/fparkan-runtime/src/production.rs
index c6bad31c..8c3b2c13 100644
--- a/crates/fparkan-runtime/src/production.rs
+++ b/crates/fparkan-runtime/src/production.rs
@@ -26,8 +26,8 @@ use std::sync::Arc;
mod strategic;
pub use strategic::{
- cancel_clan_factory_order, factory_panel_plants, factory_panel_project, FactoryPanelPlant,
- FactoryPanelProject,
+ cancel_clan_factory_order, factory_panel_plants, factory_panel_project,
+ remember_strategic_factory_project, FactoryPanelPlant, FactoryPanelProject,
};
// shortcut: GPU geometry is prepared for a bounded number of live builds per scheme.
@@ -47,6 +47,7 @@ pub(super) struct Production {
ai_schemes: Vec<crate::clan_factory_schemes::ScriptDesign>,
controls: std::collections::BTreeMap<usize, MissionControlDeviceTableState>,
plants: Vec<(usize, u32)>,
+ transferred_projects: std::collections::BTreeMap<(usize, u16), Vec<usize>>,
pending: VecDeque<Order>,
clock_ticks: std::collections::BTreeMap<(u16, u32), u64>,
buildings: BuildingFootprints,
@@ -185,6 +186,7 @@ impl Production {
ai_schemes,
controls: (reserve_start..).zip(controls).collect(),
plants: Vec::new(),
+ transferred_projects: Default::default(),
pending: VecDeque::new(),
clock_ticks: Default::default(),
buildings: BuildingFootprints::default(),
@@ -434,7 +436,8 @@ pub fn factory_schemes(
) -> Result<Vec<(usize, &str)>, EngineError> {
let (mission, production) = loaded_production(engine)?;
let (plant, _) = factory_at_communicator(&engine.world, mission, production, cockpit)?;
- Ok(recent_schemes_for_plant(mission, production, plant))
+ let owner = player_owner(&engine.world, cockpit)?;
+ Ok(recent_schemes_for_plant(mission, production, plant, owner))
}
/// Lists the live factories of the player's clan and matching configured schemes.
@@ -446,7 +449,7 @@ pub fn clan_factory_schemes(
let owner = player_owner(&engine.world, cockpit)?;
Ok(owned_factories(&engine.world, mission, production, owner)
.flat_map(|(index, factory, _)| {
- recent_schemes_for_plant(mission, production, index)
+ recent_schemes_for_plant(mission, production, index, owner)
.into_iter()
.map(move |(scheme, path)| (factory, scheme, path))
})
@@ -534,9 +537,20 @@ fn recent_schemes_for_plant<'a>(
mission: &'a LoadedMissionState,
production: &'a Production,
plant: usize,
+ owner: OwnerId,
) -> Vec<(usize, &'a str)> {
- let mut recent = schemes_for_plant(mission, production, plant)
- .filter(|&(scheme, _)| scheme < production.player_scheme_count)
+ let history = production.transferred_projects.get(&(plant, owner.0));
+ let mut recent = history
+ .into_iter()
+ .flatten()
+ .map(|&id| (id, production.schemes[id].as_str()))
+ .chain(
+ schemes_for_plant(mission, production, plant).filter(|&(scheme, _)| {
+ scheme < production.player_scheme_count
+ && history.is_none_or(|history| !history.contains(&scheme))
+ }),
+ )
+ .take(fparkan_assets::factory_projects::PROJECT_SLOTS)
.collect::<Vec<_>>();
if recent.is_empty() {
recent.extend(
diff --git a/crates/fparkan-runtime/src/production/strategic.rs b/crates/fparkan-runtime/src/production/strategic.rs
index c39566fe..cbbafee0 100644
--- a/crates/fparkan-runtime/src/production/strategic.rs
+++ b/crates/fparkan-runtime/src/production/strategic.rs
@@ -1,6 +1,6 @@
use super::{
clear_factory_demand, loaded_production, owned_factories, player_owner, production_error,
- recent_schemes_for_plant,
+ recent_schemes_for_plant, schemes_for_plant,
};
use crate::{
factory_project_root,
@@ -36,6 +36,34 @@ pub fn cancel_clan_factory_order(
Ok(())
}
+/// Places a compatible prepared project in the selected factory's recent list.
+pub fn remember_strategic_factory_project(
+ engine: &mut Engine,
+ cockpit: StartingCockpit,
+ plant: ObjectHandle,
+ scheme: usize,
+) -> Result<(), EngineError> {
+ let index = super::player_factory_index(engine, cockpit, Some(plant))?;
+ let owner = player_owner(&engine.world, cockpit)?;
+ let (mission, production) = loaded_production(engine)?;
+ if !production.player_project(scheme)
+ || !schemes_for_plant(mission, production, index).any(|(id, _)| id == scheme)
+ {
+ return Err(production_error(
+ "project is incompatible with the selected factory",
+ ));
+ }
+ let production = engine.production.as_mut().expect("loaded production");
+ let history = production
+ .transferred_projects
+ .entry((index, owner.0))
+ .or_default();
+ history.retain(|&id| id != scheme);
+ history.insert(0, scheme);
+ history.truncate(fparkan_assets::factory_projects::PROJECT_SLOTS);
+ Ok(())
+}
+
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FactoryPanelPlant {
pub handle: ObjectHandle,
@@ -97,7 +125,7 @@ pub fn factory_panel_plants(
});
(time.min(collected) * 100.0).round_ties_even() as u8
});
- let projects = recent_schemes_for_plant(mission, production, index)
+ let projects = recent_schemes_for_plant(mission, production, index, owner)
.into_iter()
.map(|(id, _)| id)
.collect();
@@ -210,5 +238,19 @@ mod tests {
.unwrap(),
duration
);
+ let other = plant.projects[1];
+ remember_strategic_factory_project(&mut engine, cockpit, plant.handle, other).unwrap();
+ let after_transfer = factory_panel_plants(&engine, cockpit, 100 + duration);
+ assert_eq!(after_transfer[0].projects, [other, scheme]);
+ assert_eq!(after_transfer[0].free_minds, 2);
+ assert!(engine
+ .production
+ .as_ref()
+ .unwrap()
+ .pending
+ .iter()
+ .all(|order| {
+ order.slot == engine.production.as_ref().unwrap().slot_starts[scheme]
+ }));
}
}