generated from GarandPLG/rust-flake-template
Refactor skirmish tasks and update dependencies
- Disable automatic board.advance_turn and replace it with explicit unit movement handling. - Add Tunnel structure creation during digging tasks. - Extend DiggingTask with pop_front and current_target helpers. - Rename task getters to get_ref_tasks and add mutable get_mut_tasks. - Update Unit trait and Units enum to expose these new methods. - Remove unused EMPTY_TASKS lazy static. - Bump flake.lock revisions, hashes, and timestamps for several dependencies.
This commit is contained in:
Generated
+12
-12
@@ -8,11 +8,11 @@
|
|||||||
"rust-analyzer-src": "rust-analyzer-src"
|
"rust-analyzer-src": "rust-analyzer-src"
|
||||||
},
|
},
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1775462255,
|
"lastModified": 1779612045,
|
||||||
"narHash": "sha256-YRzdvh6nvMebcgO2nDpr8dqVwKHpp1BBRUHeMxX9UAY=",
|
"narHash": "sha256-+7lfNVnmXJDkiRYHd5NoNwYoyUcc0LcXPaIJqjO7VWM=",
|
||||||
"owner": "nix-community",
|
"owner": "nix-community",
|
||||||
"repo": "fenix",
|
"repo": "fenix",
|
||||||
"rev": "f90343f1ed330243d4bbdbce51acbd93b776a797",
|
"rev": "d7be747f0a65af378de515fc3cee131bf99a008f",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
@@ -31,11 +31,11 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1769799857,
|
"lastModified": 1778151388,
|
||||||
"narHash": "sha256-88IFXZ7Sa1vxbz5pty0Io5qEaMQMMUPMonLa3Ls/ss4=",
|
"narHash": "sha256-lldMJPUeouEjO8/7aLuwhcsIw29vVihm2ZALzjiqfec=",
|
||||||
"owner": "nix-community",
|
"owner": "nix-community",
|
||||||
"repo": "naersk",
|
"repo": "naersk",
|
||||||
"rev": "9d4ed44d8b8cecdceb1d6fd76e74123d90ae6339",
|
"rev": "efdddff9ff4d8e7d0056d57ec67dac50f75ab8f6",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
@@ -46,11 +46,11 @@
|
|||||||
},
|
},
|
||||||
"nixpkgs": {
|
"nixpkgs": {
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1775036866,
|
"lastModified": 1779508470,
|
||||||
"narHash": "sha256-ZojAnPuCdy657PbTq5V0Y+AHKhZAIwSIT2cb8UgAz/U=",
|
"narHash": "sha256-Ap9KJX+5xHIn3bPIpfNgT6MEXdAECECwo4/rmlQD74M=",
|
||||||
"owner": "nixos",
|
"owner": "nixos",
|
||||||
"repo": "nixpkgs",
|
"repo": "nixpkgs",
|
||||||
"rev": "6201e203d09599479a3b3450ed24fa81537ebc4e",
|
"rev": "29916453413845e54a65b8a1cf996842300cd299",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
@@ -70,11 +70,11 @@
|
|||||||
"rust-analyzer-src": {
|
"rust-analyzer-src": {
|
||||||
"flake": false,
|
"flake": false,
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1775429583,
|
"lastModified": 1779569060,
|
||||||
"narHash": "sha256-bFC/p7Ywyd9QIr9DbU3Q75c7AcaCm9wVmEvcI3702cY=",
|
"narHash": "sha256-NSnk5D+3KEfRdbgPijs33N2RAKSG6A74SwfnynLcouo=",
|
||||||
"owner": "rust-lang",
|
"owner": "rust-lang",
|
||||||
"repo": "rust-analyzer",
|
"repo": "rust-analyzer",
|
||||||
"rev": "38fb8f92ac15853d7fa9fb47fc2d81fdd5cd6c7e",
|
"rev": "987ea33645ab1c709b1df6823038abcb2fe8973e",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ pub struct SkirmishState {
|
|||||||
|
|
||||||
impl SkirmishState {
|
impl SkirmishState {
|
||||||
pub fn tick_update(&mut self) {
|
pub fn tick_update(&mut self) {
|
||||||
self.board.advance_turn();
|
// self.board.advance_turn();
|
||||||
|
|
||||||
// if self.board.is_victory() {}
|
// if self.board.is_victory() {}
|
||||||
|
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ use crate::app::{
|
|||||||
FocusedCell, Offset,
|
FocusedCell, Offset,
|
||||||
skirmish_states::{
|
skirmish_states::{
|
||||||
CellSizes, MarkedCells, MoveFocusedCell, Players, ZoomLevel,
|
CellSizes, MarkedCells, MoveFocusedCell, Players, ZoomLevel,
|
||||||
structures::{BaseBuilding, Ore, Stone, Structures},
|
structures::{BaseBuilding, Ore, Stone, Structures, Tunnel},
|
||||||
tasks::{DiggingTask, Tasks},
|
tasks::{DiggingTask, Task, Tasks},
|
||||||
units::{MinerUnit, Unit, Units},
|
units::{MinerUnit, Unit, Units},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -219,7 +219,14 @@ impl BoardState {
|
|||||||
let (row, col) = self.marked_cells.selected_unit.get_coords();
|
let (row, col) = self.marked_cells.selected_unit.get_coords();
|
||||||
|
|
||||||
if self.marked_cells.marked_cells.len() > 1 && !del {
|
if self.marked_cells.marked_cells.len() > 1 && !del {
|
||||||
let path: VecDeque<(usize, usize)> = self.marked_cells.marked_cells.clone();
|
let path: VecDeque<(usize, usize)> = self
|
||||||
|
.marked_cells
|
||||||
|
.marked_cells
|
||||||
|
.iter()
|
||||||
|
.skip(1)
|
||||||
|
.cloned()
|
||||||
|
.collect();
|
||||||
|
|
||||||
let tick_increase: f32 = self.marked_cells.selected_unit.get_digging_power();
|
let tick_increase: f32 = self.marked_cells.selected_unit.get_digging_power();
|
||||||
|
|
||||||
let task: Tasks = Tasks::Digging(DiggingTask::new(path, tick_increase));
|
let task: Tasks = Tasks::Digging(DiggingTask::new(path, tick_increase));
|
||||||
@@ -318,21 +325,101 @@ impl BoardState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_active_tasks(&self) -> Vec<&Tasks> {
|
// fn get_active_tasks(&self) -> Vec<&Tasks> {
|
||||||
self.cells
|
// self.cells
|
||||||
.iter()
|
// .iter()
|
||||||
.flat_map(|row| row.iter())
|
// .flat_map(|row| row.iter())
|
||||||
.filter_map(|cell| {
|
// .filter_map(|cell| {
|
||||||
cell.get_ref_option_unit()
|
// cell.get_ref_option_unit()
|
||||||
.as_ref()
|
// .as_ref()
|
||||||
.and_then(|unit| unit.get_tasks().front())
|
// .and_then(|unit| unit.get_ref_tasks().front())
|
||||||
})
|
// })
|
||||||
.collect()
|
// .collect()
|
||||||
}
|
// // for row in self.marked_cells.marked_cells {
|
||||||
|
|
||||||
|
// // }
|
||||||
|
// }
|
||||||
|
|
||||||
pub fn advance_turn(&mut self) {
|
pub fn advance_turn(&mut self) {
|
||||||
for task in self.get_active_tasks() {
|
for row in 0..self.map_height {
|
||||||
// task.progress()
|
for col in 0..self.map_width {
|
||||||
|
/* ---------------------------------------------------------
|
||||||
|
* 1️⃣ Take the unit out of the current cell (if any). This
|
||||||
|
* gives us an *owned* `Units`, so we no longer hold a
|
||||||
|
* mutable reference into `self`.
|
||||||
|
* ------------------------------------------------------- */
|
||||||
|
let maybe_unit: Option<Units> =
|
||||||
|
self.get_mut_cell(row, col).get_mut_option_unit().take();
|
||||||
|
|
||||||
|
let Some(mut unit) = maybe_unit else {
|
||||||
|
continue; // no unit in this cell
|
||||||
|
};
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------
|
||||||
|
* 2️⃣ Look at the unit’s task queue. We need a mutable
|
||||||
|
* reference to the *front* task only while we are
|
||||||
|
* processing it. The mutable borrow is confined to a
|
||||||
|
* small inner block so that it ends before we move the
|
||||||
|
* unit itself.
|
||||||
|
* ------------------------------------------------------- */
|
||||||
|
if let Some(task) = unit.get_mut_tasks().front_mut() {
|
||||||
|
// `Tasks` currently only has the Digging variant.
|
||||||
|
// If new variants appear later, you can handle them here.
|
||||||
|
let maybe_target: Option<(usize, usize)> = {
|
||||||
|
// -------------------------------------------------
|
||||||
|
// Mutable borrow of the task lives only inside this
|
||||||
|
// block. When the block ends, `dig` is dropped.
|
||||||
|
// -------------------------------------------------
|
||||||
|
let dig: &mut DiggingTask = match task {
|
||||||
|
Tasks::Digging(d) => d,
|
||||||
|
};
|
||||||
|
|
||||||
|
dig.progress();
|
||||||
|
|
||||||
|
// Remember the coordinates of the next tunnel cell.
|
||||||
|
let target: Option<(usize, usize)> = dig.current_target();
|
||||||
|
|
||||||
|
// If we actually moved, the current step of the
|
||||||
|
// digging task is finished – pop it from the queue.
|
||||||
|
if target.is_some() {
|
||||||
|
dig.pop_front();
|
||||||
|
}
|
||||||
|
|
||||||
|
target
|
||||||
|
}; // ← `dig` (and the mutable borrow) ends here
|
||||||
|
|
||||||
|
if let Some((t_row, t_col)) = maybe_target {
|
||||||
|
/* -------------------------------------------------
|
||||||
|
* 3️⃣ We have a target – create the tunnel, move the
|
||||||
|
* unit, and clean up the source cell.
|
||||||
|
* ------------------------------------------------- */
|
||||||
|
|
||||||
|
// a) create the tunnel in the target cell
|
||||||
|
self.get_mut_cell(t_row, t_col)
|
||||||
|
.set_structure(Structures::Tunnel(Tunnel::new()));
|
||||||
|
|
||||||
|
// b) (optional) keep the old coordinates – they are
|
||||||
|
// no longer needed but kept for possible future use
|
||||||
|
let _old_coords: (usize, usize) = unit.get_coords();
|
||||||
|
|
||||||
|
// c) move the unit into the target cell
|
||||||
|
*self.get_mut_cell(t_row, t_col).get_mut_option_unit() = Some(unit);
|
||||||
|
|
||||||
|
// d) the source cell is already empty because of the
|
||||||
|
// earlier `take()`. Nothing else to do.
|
||||||
|
} else {
|
||||||
|
// No reachable target – put the unit back where it was.
|
||||||
|
self.get_mut_cell(row, col)
|
||||||
|
.get_mut_option_unit()
|
||||||
|
.replace(unit);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// The unit has no tasks – simply put it back.
|
||||||
|
self.get_mut_cell(row, col)
|
||||||
|
.get_mut_option_unit()
|
||||||
|
.replace(unit);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,15 @@ impl DiggingTask {
|
|||||||
tick_increase,
|
tick_increase,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn pop_front(&mut self) {
|
||||||
|
self.path.pop_front();
|
||||||
|
self.progress = 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn current_target(&self) -> Option<(usize, usize)> {
|
||||||
|
self.path.front().copied()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Task for DiggingTask {
|
impl Task for DiggingTask {
|
||||||
|
|||||||
@@ -79,10 +79,14 @@ impl Unit for MinerUnit {
|
|||||||
self.coords
|
self.coords
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_tasks(&self) -> &VecDeque<Tasks> {
|
fn get_ref_tasks(&self) -> &VecDeque<Tasks> {
|
||||||
&self.tasks
|
&self.tasks
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_mut_tasks(&mut self) -> &mut VecDeque<Tasks> {
|
||||||
|
&mut self.tasks
|
||||||
|
}
|
||||||
|
|
||||||
fn get_tasks_formatted(&self) -> Vec<Line<'_>> {
|
fn get_tasks_formatted(&self) -> Vec<Line<'_>> {
|
||||||
Vec::from(
|
Vec::from(
|
||||||
self.tasks
|
self.tasks
|
||||||
|
|||||||
@@ -26,7 +26,9 @@ pub trait Unit {
|
|||||||
|
|
||||||
fn get_coords(&self) -> (usize, usize);
|
fn get_coords(&self) -> (usize, usize);
|
||||||
|
|
||||||
fn get_tasks(&self) -> &VecDeque<Tasks>;
|
fn get_ref_tasks(&self) -> &VecDeque<Tasks>;
|
||||||
|
|
||||||
|
fn get_mut_tasks(&mut self) -> &mut VecDeque<Tasks>;
|
||||||
|
|
||||||
fn set_task(&mut self, task: Tasks);
|
fn set_task(&mut self, task: Tasks);
|
||||||
|
|
||||||
@@ -39,7 +41,7 @@ pub trait Unit {
|
|||||||
let hp_percent: u16 = hp * 100 / max_hp;
|
let hp_percent: u16 = hp * 100 / max_hp;
|
||||||
let can_dig: bool = self.get_can_dig();
|
let can_dig: bool = self.get_can_dig();
|
||||||
let digging_power: f32 = self.get_digging_power();
|
let digging_power: f32 = self.get_digging_power();
|
||||||
let have_any_tasks: bool = self.get_tasks().is_empty();
|
let have_any_tasks: bool = self.get_ref_tasks().is_empty();
|
||||||
let tasks: Vec<Line<'_>> = self.get_tasks_formatted();
|
let tasks: Vec<Line<'_>> = self.get_tasks_formatted();
|
||||||
|
|
||||||
let mut lines: Vec<Line<'_>> = Vec::from([
|
let mut lines: Vec<Line<'_>> = Vec::from([
|
||||||
|
|||||||
@@ -3,12 +3,9 @@ use crate::app::states::skirmish_states::{
|
|||||||
tasks::Tasks,
|
tasks::Tasks,
|
||||||
units::{MinerUnit, Unit},
|
units::{MinerUnit, Unit},
|
||||||
};
|
};
|
||||||
use once_cell::sync::Lazy;
|
|
||||||
use ratatui::text::Line;
|
use ratatui::text::Line;
|
||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
|
|
||||||
static EMPTY_TASKS: Lazy<VecDeque<Tasks>> = Lazy::new(|| VecDeque::new());
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub enum Units {
|
pub enum Units {
|
||||||
Miner(MinerUnit),
|
Miner(MinerUnit),
|
||||||
@@ -16,14 +13,14 @@ pub enum Units {
|
|||||||
|
|
||||||
impl Units {
|
impl Units {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn u(&self) -> &dyn Unit {
|
pub fn u(&self) -> &dyn Unit {
|
||||||
match self {
|
match self {
|
||||||
Self::Miner(m) => m,
|
Self::Miner(m) => m,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn u_mut(&mut self) -> &mut dyn Unit {
|
pub fn u_mut(&mut self) -> &mut dyn Unit {
|
||||||
match self {
|
match self {
|
||||||
Self::Miner(m) => m,
|
Self::Miner(m) => m,
|
||||||
}
|
}
|
||||||
@@ -63,8 +60,12 @@ impl Unit for Units {
|
|||||||
self.u().get_coords()
|
self.u().get_coords()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_tasks(&self) -> &VecDeque<Tasks> {
|
fn get_ref_tasks(&self) -> &VecDeque<Tasks> {
|
||||||
self.u().get_tasks()
|
self.u().get_ref_tasks()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_mut_tasks(&mut self) -> &mut VecDeque<Tasks> {
|
||||||
|
self.u_mut().get_mut_tasks()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_tasks_formatted(&self) -> Vec<Line<'_>> {
|
fn get_tasks_formatted(&self) -> Vec<Line<'_>> {
|
||||||
@@ -113,8 +114,16 @@ impl Unit for Option<Units> {
|
|||||||
self.as_ref().map_or((0, 0), |u| u.get_coords())
|
self.as_ref().map_or((0, 0), |u| u.get_coords())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_tasks(&self) -> &VecDeque<Tasks> {
|
fn get_ref_tasks(&self) -> &VecDeque<Tasks> {
|
||||||
self.as_ref().map_or(&EMPTY_TASKS, |u| u.get_tasks())
|
self.as_ref()
|
||||||
|
.expect("attempted to get referenced tasks from `None`")
|
||||||
|
.get_ref_tasks()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_mut_tasks(&mut self) -> &mut VecDeque<Tasks> {
|
||||||
|
self.as_mut()
|
||||||
|
.expect("attempted to get mutable tasks from `None`")
|
||||||
|
.get_mut_tasks()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_tasks_formatted(&self) -> Vec<Line<'_>> {
|
fn get_tasks_formatted(&self) -> Vec<Line<'_>> {
|
||||||
|
|||||||
Reference in New Issue
Block a user