From b66c1d8e25b22df521f3a699ba50dea7370ec293 Mon Sep 17 00:00:00 2001 From: GarandPLG Date: Wed, 6 May 2026 20:01:49 +0200 Subject: [PATCH] Use VecDeque for tasks and marked cells Replace `Vec` with `VecDeque` in `BoardState` marked cells, `DiggingTask` path, and `MinerUnit` tasks to enable efficient push/pop operations. Remove the `Copy` trait from several structs and enums, adjust derives accordingly, and use `as_ref` in `Option` implementations to avoid consuming the option. Update cell rendering to clone widgets and adjust `CellWidget::get_option_unit` to return a cloned `Option`. Add necessary `VecDeque` imports. --- src/app/states/skirmish_states/board.rs | 11 ++++++----- .../states/skirmish_states/tasks/digging.rs | 12 +++++------- .../skirmish_states/tasks/tasks_enum.rs | 2 +- src/app/states/skirmish_states/units/miner.rs | 7 ++++--- .../skirmish_states/units/units_enum.rs | 19 ++++++++++--------- src/app/widgets/board.rs | 2 +- src/app/widgets/cell.rs | 4 ++-- 7 files changed, 29 insertions(+), 28 deletions(-) diff --git a/src/app/states/skirmish_states/board.rs b/src/app/states/skirmish_states/board.rs index dc128b5..ed63768 100644 --- a/src/app/states/skirmish_states/board.rs +++ b/src/app/states/skirmish_states/board.rs @@ -11,6 +11,7 @@ use crate::app::{ widgets::CellWidget, }; use ratatui::layout::Rect; +use std::collections::VecDeque; #[derive(Debug, Clone, PartialEq)] pub struct BoardState { @@ -29,7 +30,7 @@ pub struct BoardState { player_base_coords: (usize, usize), enemy_base_coords: (usize, usize), pub marking_cells: bool, - marked_cells: Vec<(usize, usize)>, + marked_cells: VecDeque<(usize, usize)>, } impl BoardState { @@ -105,7 +106,7 @@ impl BoardState { } let marking_cells: bool = false; - let marked_cells: Vec<(usize, usize)> = Vec::new(); + let marked_cells: VecDeque<(usize, usize)> = VecDeque::new(); Self { cells_area, @@ -159,7 +160,7 @@ impl BoardState { cell.set_marked(true); } - self.marked_cells.push((new_cell.0, new_cell.1)); + self.marked_cells.push_back((new_cell.0, new_cell.1)); } pub fn undo_marked_cell(&mut self) { @@ -178,7 +179,7 @@ impl BoardState { self.get_mut_cell(new.0, new.1).set_selected(true); - self.marked_cells.pop(); + self.marked_cells.pop_back(); self.focused_cell.set_focused_cell(new); } @@ -190,7 +191,7 @@ impl BoardState { let cell: &mut CellWidget = self.get_mut_cell(row, col); cell.set_marked(true); - self.marked_cells.push((row, col)); + self.marked_cells.push_back((row, col)); } pub fn clear_marked_cells(&mut self) { diff --git a/src/app/states/skirmish_states/tasks/digging.rs b/src/app/states/skirmish_states/tasks/digging.rs index fcb1c1f..8897990 100644 --- a/src/app/states/skirmish_states/tasks/digging.rs +++ b/src/app/states/skirmish_states/tasks/digging.rs @@ -1,14 +1,12 @@ -use crate::app::widgets::CellWidget; +use std::collections::VecDeque; -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Clone, PartialEq)] pub struct DiggingTask { - // path: Vec<(usize, usize)>, + path: VecDeque<(usize, usize)>, } impl DiggingTask { - pub fn new(path: Vec<(u16, u16)>) -> Self { - Self { - // path - } + pub fn new(path: VecDeque<(usize, usize)>) -> Self { + Self { path } } } diff --git a/src/app/states/skirmish_states/tasks/tasks_enum.rs b/src/app/states/skirmish_states/tasks/tasks_enum.rs index a2f2aee..cab8f67 100644 --- a/src/app/states/skirmish_states/tasks/tasks_enum.rs +++ b/src/app/states/skirmish_states/tasks/tasks_enum.rs @@ -1,6 +1,6 @@ use crate::app::states::skirmish_states::tasks::DiggingTask; -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Clone, PartialEq)] pub enum Tasks { Digging(DiggingTask), } diff --git a/src/app/states/skirmish_states/units/miner.rs b/src/app/states/skirmish_states/units/miner.rs index d592198..7302242 100644 --- a/src/app/states/skirmish_states/units/miner.rs +++ b/src/app/states/skirmish_states/units/miner.rs @@ -1,12 +1,13 @@ use crate::app::states::skirmish_states::{Players, tasks::Tasks, units::Unit}; +use std::collections::VecDeque; -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Clone, PartialEq)] pub struct MinerUnit { owner: Players, hp: u16, can_dig: bool, digging_power: f32, - // tasks: Vec, + pub tasks: VecDeque, } impl MinerUnit { @@ -16,7 +17,7 @@ impl MinerUnit { hp: 100, can_dig: true, digging_power: 0.55, - // tasks: Vec::new(), + tasks: VecDeque::new(), } } } diff --git a/src/app/states/skirmish_states/units/units_enum.rs b/src/app/states/skirmish_states/units/units_enum.rs index 2cf15cc..2276e97 100644 --- a/src/app/states/skirmish_states/units/units_enum.rs +++ b/src/app/states/skirmish_states/units/units_enum.rs @@ -3,12 +3,13 @@ use crate::app::states::skirmish_states::{ units::{MinerUnit, Unit}, }; -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Clone, PartialEq)] pub enum Units { Miner(MinerUnit), } impl Units { + #[inline] fn u(&self) -> &dyn Unit { match self { Self::Miner(m) => m, @@ -48,34 +49,34 @@ impl Unit for Units { impl Unit for Option { fn is_unit(&self) -> bool { - self.map_or(false, |u| u.is_unit()) + self.as_ref().map_or(false, |u| u.is_unit()) } fn get_tag(&self) -> char { - self.map_or(' ', |u| u.get_tag()) + self.as_ref().map_or(' ', |u| u.get_tag()) } fn get_name(&self) -> &'static str { - self.map_or("", |u| u.get_name()) + self.as_ref().map_or("", |u| u.get_name()) } fn get_owner(&self) -> Players { - self.map_or(Players::Unclaimed, |u| u.get_owner()) + self.as_ref().map_or(Players::Unclaimed, |u| u.get_owner()) } fn get_hp(&self) -> u16 { - self.map_or(0, |u| u.get_hp()) + self.as_ref().map_or(0, |u| u.get_hp()) } fn get_max_hp(&self) -> u16 { - self.map_or(0, |u| u.get_max_hp()) + self.as_ref().map_or(0, |u| u.get_max_hp()) } fn get_can_dig(&self) -> bool { - self.map_or(false, |u| u.get_can_dig()) + self.as_ref().map_or(false, |u| u.get_can_dig()) } fn get_digging_power(&self) -> f32 { - self.map_or(0.0, |u| u.get_digging_power()) + self.as_ref().map_or(0.0, |u| u.get_digging_power()) } } diff --git a/src/app/widgets/board.rs b/src/app/widgets/board.rs index 76ece52..d4bdb85 100644 --- a/src/app/widgets/board.rs +++ b/src/app/widgets/board.rs @@ -38,7 +38,7 @@ impl Widget for BoardWidget<'_> { .cells .get(row_idx + self.state.vertical_offset.get_value()) .and_then(|rows| rows.get(col_idx + self.state.horizontal_offset.get_value())) - .map(|cell| cell.render(*cell_area, buf)); + .map(|cell| cell.clone().render(*cell_area, buf)); } } } diff --git a/src/app/widgets/cell.rs b/src/app/widgets/cell.rs index 44acc36..bd2cb0d 100644 --- a/src/app/widgets/cell.rs +++ b/src/app/widgets/cell.rs @@ -11,7 +11,7 @@ use ratatui::{ widgets::{Block, BorderType, Borders, Paragraph, Widget}, }; -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Clone, PartialEq)] pub struct CellWidget { row: usize, col: usize, @@ -57,7 +57,7 @@ impl CellWidget { } pub fn get_option_unit(&self) -> Option { - self.unit + self.unit.clone() } pub fn set_structure(&mut self, structure: Structures) -> &mut Self {