From faeb22084ea59194036785d84b68396981dfcbae Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Wed, 2 Dec 2026 17:09:19 +0400 Subject: feat(ui): inspect the current factory project --- crates/fparkan-assets/src/strategic_factory.rs | 223 +++++++++++++++------ crates/fparkan-runtime/src/production.rs | 2 + crates/fparkan-runtime/src/production/batch.rs | 2 +- crates/fparkan-runtime/src/production/strategic.rs | 60 +++++- 4 files changed, 213 insertions(+), 74 deletions(-) (limited to 'crates') diff --git a/crates/fparkan-assets/src/strategic_factory.rs b/crates/fparkan-assets/src/strategic_factory.rs index aaa590c6..1cf6d8a4 100644 --- a/crates/fparkan-assets/src/strategic_factory.rs +++ b/crates/fparkan-assets/src/strategic_factory.rs @@ -20,11 +20,18 @@ pub const VISIBLE_PLANTS: usize = ((EXTENT[1] - ROW_TOP) / ROW_PITCH) as usize; const BUILD: [u32; 4] = [263, 150, 313, 169]; const BATCH: [u32; 4] = [211, 150, 261, 169]; const CONSTRUCTOR: [u32; 4] = [5, 150, 55, 169]; +const ACTIVE: [u32; 4] = [135, 125, 165, 142]; +const FREE_BUILD: [u32; 4] = [228, 127, 258, 142]; +const MINDS: [u32; 4] = [274, 125, 307, 144]; +pub const TOOLTIP_IDS: [u32; 10] = [ + 0x611, 0xbfe, 0x185e, 0x185f, 0x5e7, 0xbfc, 0xbfd, 0xbfb, 0x1868, 0x1869, +]; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum FactoryAction { Close, Project(usize), + ActiveProject, Build, Batch, Cancel, @@ -59,46 +66,80 @@ fn project_rect(index: usize) -> [u32; 4] { [left, 125, left + 19, 142] } -pub fn hit_test( - [x, y]: [f32; 2], - plants: usize, - projects: usize, - orders: FactoryOrders, - designer_available: bool, -) -> Option { - let [x, y] = [x.round_ties_even() - ORIGIN[0] as f32, y.round_ties_even()]; - let hit = |[left, top, right, bottom]: [u32; 4]| { - (left as f32..right as f32).contains(&x) && (top as f32..bottom as f32).contains(&y) - }; - if hit([283, 0, 318, 19]) { - return Some(FactoryAction::Close); - } - if hit(BUILD) { - return match orders { - FactoryOrders::Ready => Some(FactoryAction::Build), - FactoryOrders::Single => Some(FactoryAction::Cancel), - _ => None, - }; - } - if hit(BATCH) { - return match orders { - FactoryOrders::Ready => Some(FactoryAction::Batch), - FactoryOrders::Batch => Some(FactoryAction::Cancel), - _ => None, - }; - } - if designer_available && hit(CONSTRUCTOR) { - return Some(FactoryAction::Designer); +#[derive(Clone, Copy)] +pub struct FactoryControls { + pub plants: usize, + pub projects: usize, + pub orders: FactoryOrders, + pub designer_available: bool, + pub active_available: bool, + pub free_build: bool, +} + +impl FactoryControls { + pub fn hit_test(self, at: [f32; 2]) -> Option { + let hit = |rect| contains(at, rect); + if hit([283, 0, 318, 19]) { + return Some(FactoryAction::Close); + } + if hit(BUILD) { + return match self.orders { + FactoryOrders::Ready => Some(FactoryAction::Build), + FactoryOrders::Single => Some(FactoryAction::Cancel), + _ => None, + }; + } + if hit(BATCH) { + return match self.orders { + FactoryOrders::Ready => Some(FactoryAction::Batch), + FactoryOrders::Batch => Some(FactoryAction::Cancel), + _ => None, + }; + } + if self.designer_available && hit(CONSTRUCTOR) { + return Some(FactoryAction::Designer); + } + if self.active_available && hit(ACTIVE) { + return Some(FactoryAction::ActiveProject); + } + if let Some(index) = (0..self.projects).find(|&index| hit(project_rect(index))) { + return Some(FactoryAction::Project(index)); + } + (0..self.plants.min(VISIBLE_PLANTS)) + .find(|&index| { + let top = ROW_TOP + index as u32 * ROW_PITCH; + hit([0, top, 318, top + ROW_HEIGHT]) + }) + .map(FactoryAction::Plant) } - if let Some(index) = (0..projects).find(|&index| hit(project_rect(index))) { - return Some(FactoryAction::Project(index)); + + pub fn tooltip(self, at: [f32; 2]) -> Option { + let hit = |rect| contains(at, rect); + let busy = matches!(self.orders, FactoryOrders::Single | FactoryOrders::Batch); + if hit(BUILD) { + Some(if busy { 0xbfe } else { 0x611 }) + } else if hit(BATCH) { + Some(if busy { 0x185f } else { 0x185e }) + } else if hit(CONSTRUCTOR) { + Some(0x5e7) + } else if self.active_available && hit(ACTIVE) { + Some(0xbfd) + } else if (0..self.projects).any(|index| hit(project_rect(index))) { + Some(0xbfc) + } else if hit(FREE_BUILD) { + Some(if self.free_build { 0x1868 } else { 0x1869 }) + } else if hit(MINDS) { + Some(0xbfb) + } else { + None + } } - (0..plants.min(VISIBLE_PLANTS)) - .find(|&index| { - let top = ROW_TOP + index as u32 * ROW_PITCH; - hit([0, top, 318, top + ROW_HEIGHT]) - }) - .map(FactoryAction::Plant) +} + +fn contains([x, y]: [f32; 2], [left, top, right, bottom]: [u32; 4]) -> bool { + let x = x.round_ties_even() - ORIGIN[0] as f32; + let y = y.round_ties_even(); + (left as f32..right as f32).contains(&x) && (top as f32..bottom as f32).contains(&y) } pub struct FactoryRow<'a> { @@ -110,12 +151,11 @@ pub struct FactoryPicture<'a> { pub rows: &'a [FactoryRow<'a>], pub project_name: Option<&'a str>, pub stats: &'a [Option>], - pub projects: usize, + pub controls: FactoryControls, pub selected_project: Option, + pub viewing_active: bool, pub progress: Option, pub free_minds: u32, - pub orders: FactoryOrders, - pub designer_available: bool, } pub struct FactoryImages { @@ -126,6 +166,8 @@ pub struct FactoryImages { ray: RgbaImage, icons: [RgbaImage; 7], batch_icons: [RgbaImage; 2], + active_project: RgbaImage, + free_bots_icons: [RgbaImage; 2], } impl FactoryImages { @@ -148,7 +190,7 @@ impl FactoryImages { "ccres_frame_edge_v", ], )?; - let [exit, project, project_frame, build, stop, constructor, free, batch, stop_batch] = + let [exit, project, project_frame, build, stop, constructor, free, batch, stop_batch, active_project, free_bots, no_free_bots] = load_ui_sprites( vfs, "ui/hq.cfg", @@ -162,6 +204,9 @@ impl FactoryImages { "brain_icon", "batch_build_icon", "batch_stop_build_icon", + "active_project", + "free_bots_icon", + "no_free_bots_icon", ], )?; let atlas = load_ui_page(vfs, "ui_menu")?; @@ -212,6 +257,8 @@ impl FactoryImages { free, ], batch_icons: [batch, stop_batch], + active_project, + free_bots_icons: [no_free_bots, free_bots], }) } @@ -229,7 +276,7 @@ impl FactoryImages { )?; warbot_card::paint_at(&self.font, &mut image, picture.stats, [10, 144, 147], 43)?; } - for index in 0..picture.projects { + for index in 0..picture.controls.projects { let rect = project_rect(index); stamp( &mut image, @@ -237,13 +284,28 @@ impl FactoryImages { [rect[0] - 2, rect[1] - 2, rect[2] + 2, rect[3] + 2], [255; 3], ); - let tint = if picture.selected_project == Some(index) { + let tint = if !picture.viewing_active && picture.selected_project == Some(index) { [255; 3] } else { [128, 255, 128] }; stamp(&mut image, project, rect, tint); } + if picture.controls.active_available { + stamp(&mut image, frame, [133, 123, 167, 144], [255; 3]); + let tint = if picture.viewing_active { + [255; 3] + } else { + [128, 255, 128] + }; + stamp(&mut image, &self.active_project, ACTIVE, tint); + } + stamp( + &mut image, + &self.free_bots_icons[usize::from(picture.controls.free_build)], + FREE_BUILD, + [255; 3], + ); stamp(&mut image, free, [274, 127, 289, 142], [128, 255, 128]); text( &self.font, @@ -263,18 +325,23 @@ impl FactoryImages { [255; 3], )?; } - let busy = matches!(picture.orders, FactoryOrders::Single | FactoryOrders::Batch); + let orders = picture.controls.orders; + let busy = matches!(orders, FactoryOrders::Single | FactoryOrders::Batch); for (rect, icon, enabled) in [ - (CONSTRUCTOR, constructor, picture.designer_available), + ( + CONSTRUCTOR, + constructor, + picture.controls.designer_available, + ), ( BATCH, &self.batch_icons[usize::from(busy)], - matches!(picture.orders, FactoryOrders::Ready | FactoryOrders::Batch), + matches!(orders, FactoryOrders::Ready | FactoryOrders::Batch), ), ( BUILD, if busy { stop } else { build }, - matches!(picture.orders, FactoryOrders::Ready | FactoryOrders::Single), + matches!(orders, FactoryOrders::Ready | FactoryOrders::Single), ), ] { let tint = if enabled { [255; 3] } else { [104; 3] }; @@ -330,41 +397,67 @@ mod tests { #[test] fn factory_actions_follow_project_and_row_extents() { use FactoryOrders::{Batch, Ready, Single, Unavailable}; - + let mut controls = FactoryControls { + plants: 1, + projects: 2, + orders: Ready, + designer_available: true, + active_available: false, + free_build: true, + }; assert_eq!( - hit_test([95.0, 132.0], 1, 2, Ready, true), + controls.hit_test([95.0, 132.0]), Some(FactoryAction::Project(1)) ); - assert_eq!(hit_test([95.0, 132.0], 1, 1, Ready, true), None); assert_eq!( - hit_test([340.0, 160.0], 1, 2, Ready, true), + controls.hit_test([340.0, 160.0]), Some(FactoryAction::Build) ); - assert_eq!(hit_test([340.0, 160.0], 1, 2, Unavailable, true), None); - assert_eq!(FactoryOrders::for_plant(false, true, false, 0), Single); + assert_eq!(controls.tooltip([340.0, 160.0]), Some(0x611)); assert_eq!( - hit_test([340.0, 160.0], 1, 0, Single, true), - Some(FactoryAction::Cancel) + controls.hit_test([289.0, 160.0]), + Some(FactoryAction::Batch) ); assert_eq!( - hit_test([200.0, 172.0], 1, 2, Ready, true), + controls.hit_test([200.0, 172.0]), Some(FactoryAction::Plant(0)) ); - assert_eq!(hit_test([80.0, 160.0], 1, 0, Unavailable, false), None); assert_eq!( - hit_test([80.0, 160.0], 1, 0, Unavailable, true), + controls.hit_test([80.0, 160.0]), Some(FactoryAction::Designer) ); - assert_eq!(FactoryOrders::for_plant(true, false, true, 0), Batch); - assert_eq!(hit_test([340.0, 160.0], 1, 2, Batch, true), None); - assert_eq!(hit_test([289.0, 160.0], 1, 2, Single, true), None); + assert_eq!(controls.hit_test([201.0, 132.0]), None); + assert_eq!(controls.tooltip([201.0, 132.0]), None); + assert_eq!(controls.tooltip([95.0, 132.0]), Some(0xbfc)); + assert_eq!(controls.tooltip([287.0, 132.0]), Some(0x1868)); + controls.orders = Single; + controls.active_available = true; + assert_eq!(controls.hit_test([289.0, 160.0]), None); assert_eq!( - hit_test([289.0, 160.0], 1, 2, Ready, true), - Some(FactoryAction::Batch) + controls.hit_test([340.0, 160.0]), + Some(FactoryAction::Cancel) ); assert_eq!( - hit_test([289.0, 160.0], 1, 2, Batch, true), + controls.hit_test([201.0, 132.0]), + Some(FactoryAction::ActiveProject) + ); + assert_eq!(controls.tooltip([201.0, 132.0]), Some(0xbfd)); + + controls.orders = Batch; + assert_eq!(controls.hit_test([340.0, 160.0]), None); + assert_eq!( + controls.hit_test([289.0, 160.0]), Some(FactoryAction::Cancel) ); + assert_eq!(controls.tooltip([289.0, 160.0]), Some(0x185f)); + + controls.orders = Unavailable; + controls.projects = 1; + controls.designer_available = false; + controls.free_build = false; + assert_eq!(controls.hit_test([95.0, 132.0]), None); + assert_eq!(controls.hit_test([340.0, 160.0]), None); + assert_eq!(controls.hit_test([80.0, 160.0]), None); + assert_eq!(controls.tooltip([287.0, 132.0]), Some(0x1869)); } } diff --git a/crates/fparkan-runtime/src/production.rs b/crates/fparkan-runtime/src/production.rs index 50acdd94..37246b8a 100644 --- a/crates/fparkan-runtime/src/production.rs +++ b/crates/fparkan-runtime/src/production.rs @@ -60,6 +60,7 @@ pub(super) struct Production { struct Order { slot: usize, + scheme: usize, plant: ObjectHandle, plant_index: usize, owner: OwnerId, @@ -745,6 +746,7 @@ fn order_owned_factory( }; let order = Order { slot, + scheme, plant, plant_index, owner, diff --git a/crates/fparkan-runtime/src/production/batch.rs b/crates/fparkan-runtime/src/production/batch.rs index c0fb2690..ed8dee9a 100644 --- a/crates/fparkan-runtime/src/production/batch.rs +++ b/crates/fparkan-runtime/src/production/batch.rs @@ -10,7 +10,7 @@ use fparkan_world::{identity_metadata, ObjectHandle, OwnerId}; pub(super) struct BatchOrder { pub(super) plant: ObjectHandle, pub(super) owner: OwnerId, - scheme: usize, + pub(super) scheme: usize, } /// Starts a repeating order at an idle factory and reserves one clan slot. diff --git a/crates/fparkan-runtime/src/production/strategic.rs b/crates/fparkan-runtime/src/production/strategic.rs index 5b4e08f9..83360548 100644 --- a/crates/fparkan-runtime/src/production/strategic.rs +++ b/crates/fparkan-runtime/src/production/strategic.rs @@ -82,6 +82,8 @@ pub struct FactoryPanelPlant { pub free_minds: u32, pub progress: Option, pub batch: bool, + pub active_project: Option, + pub free_build: bool, pub projects: Vec, } @@ -98,7 +100,7 @@ pub fn factory_panel_plants( }; let free_minds = production.free_minds(&engine.world, mission, owner); let mut plants: Vec<_> = owned_factories(&engine.world, mission, production, owner) - .filter_map(|(index, handle, _)| { + .filter_map(|(index, handle, free_build)| { if factory_depleted(mission, handle) { return None; } @@ -113,6 +115,14 @@ pub fn factory_panel_plants( .pending .iter() .find(|order| order.plant == handle && order.owner == owner); + let batch = production + .batches + .iter() + .find(|batch| batch.plant == handle && batch.owner == owner); + let active_project = order + .map(|order| order.scheme) + .or_else(|| batch.map(|batch| batch.scheme)) + .filter(|&scheme| production.player_project(scheme)); let progress = order.map(|order| { let time = ratio( now_ms.wrapping_sub(order.start_ms) as f32, @@ -135,10 +145,9 @@ pub fn factory_panel_plants( label_id: label, free_minds, progress, - batch: production - .batches - .iter() - .any(|batch| batch.plant == handle && batch.owner == owner), + batch: batch.is_some(), + active_project, + free_build, projects, }, )) @@ -162,7 +171,11 @@ pub struct FactoryPanelProject { pub stats: WarbotStats, } -pub fn factory_panel_project(engine: &Engine, scheme: usize) -> Option { +pub fn factory_panel_project( + engine: &Engine, + scheme: usize, + current_factory: Option, +) -> Option { let (mission, production) = loaded_production(engine).ok()?; let root = factory_project_root(engine, scheme)?; let draft = &mission.object_drafts[root]; @@ -180,7 +193,14 @@ pub fn factory_panel_project(engine: &Engine, scheme: usize) -> Option