From 7cb55a9485b9e0c9a352dc15ee0cb3e77fca7c77 Mon Sep 17 00:00:00 2001 From: Vova_rs Date: Sat, 30 May 2026 13:45:13 +0300 Subject: [PATCH 1/2] actual world merge --- src/world/intention.rs | 12 +++---- src/world/mod.rs | 80 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 74 insertions(+), 18 deletions(-) diff --git a/src/world/intention.rs b/src/world/intention.rs index 52b4663..c5daf42 100644 --- a/src/world/intention.rs +++ b/src/world/intention.rs @@ -2,22 +2,20 @@ // use super::{MagicContainer, MCError}; // use super::TorusGrid; - -#[derive(Clone)] +#[derive(PartialEq, Clone)] pub enum Intention { MoveTo, StakedOut, } - -#[derive(Clone)] +#[derive(PartialEq, Clone)] pub enum LocalItentoin { Rotate(i8), - SwapCell, + SwapCell((i32, i32)), } -#[derive(Clone)] -pub struct LinkIntention{ +#[derive(PartialEq, Clone)] +pub struct LinkIntention { pub it: T, pub lin: usize, } diff --git a/src/world/mod.rs b/src/world/mod.rs index 7bc1887..0399f5a 100644 --- a/src/world/mod.rs +++ b/src/world/mod.rs @@ -14,7 +14,7 @@ pub use m_container::*; mod intention; pub use intention::*; -pub struct World{ +pub struct World { grid: TorusGrid, agents: MagicContainer, a_grid: TorusGrid>, @@ -26,11 +26,11 @@ impl World { World { grid: TorusGrid::new(false, s), agents: MagicContainer::new(), - a_grid: TorusGrid::new(None, s), + a_grid: TorusGrid::new(None, s), size: s, } } - pub fn get_size(&self) -> (usize, usize){ + pub fn get_size(&self) -> (usize, usize) { self.size.clone() } pub fn add_agent(&mut self, ag: Agent) -> Result<(), &str> { @@ -43,27 +43,85 @@ impl World { *p = Some(id); Ok(()) } - pub fn get_grid(&self) -> &TorusGrid{ + pub fn get_grid(&self) -> &TorusGrid { &self.grid } - pub fn get_mut_grid(&mut self) -> &mut TorusGrid{ + pub fn get_mut_grid(&mut self) -> &mut TorusGrid { &mut self.grid } - pub fn get_a_grid(&self) -> &TorusGrid>{ + pub fn get_a_grid(&self) -> &TorusGrid> { &self.a_grid } - pub fn update(&mut self){ - let mut plane_of_intentions: TorusGrid; 4]>> = TorusGrid::new(SmallVec::new(), self.size.clone()); + pub fn update(&mut self) { + let mut plane_of_intentions: TorusGrid; 4]>> = + TorusGrid::new(SmallVec::new(), self.size.clone()); let mut local_it: Vec> = Vec::new(); for i in 0..self.agents.len() { if let Some(ag) = self.agents.get(i) { let cell = self.grid.get_wrap(ag.get_pos()); - - + local_it.push(LinkIntention { + it: LocalItentoin::Rotate((-1 * *cell as i8) + (1 * !*cell as i8)), + lin: i, + }); + local_it.push(LinkIntention { + it: LocalItentoin::SwapCell(ag.get_pos()), + lin: i, + }); + } + } + for i in 0..local_it.len() { + match local_it[i].it { + LocalItentoin::Rotate(r) => { + if let Some(ag) = self.agents.get_mut(local_it[i].lin) { + ag.rotate(r); + } + } + LocalItentoin::SwapCell(cell) => { + let g = self.get_mut_grid(); + if let Some(s) = g.get_mut(cell) { + *s = !*s; + } + } + } + } + for i in 0..self.agents.len() { + if let Some(ag) = self.agents.get(i) { + let pos = ag.get_pos(); + let mut p = plane_of_intentions.get_mut_wrap(pos); + p.push(LinkIntention { + it: Intention::StakedOut, + lin: i, + }); + let mv = ag.wolk_a(); + p = plane_of_intentions.get_mut_wrap(mv); + p.push(LinkIntention { + it: Intention::MoveTo, + lin: i, + }); + } + } + for i in 0..self.size.0 { + for j in 0..self.size.1 { + let cell = plane_of_intentions.get_mut_wrap((i as i32, j as i32)); + if cell.len() == 1 { + if cell[0].it == Intention::MoveTo { + let ag = self.agents.get_mut(cell[0].lin).unwrap(); + ag.move_to(ag.wolk_a()); + } + } + } + } + } + pub fn render_grid_updaete(&mut self) { + self.a_grid = TorusGrid::new(None, self.size); + for i in 0..self.agents.len() { + if let Some(ag) = self.agents.get(i) { + let pos = ag.get_pos(); + let cell = self.a_grid.get_mut_wrap(pos); + *cell = Some(i); } } } } - From 6708bf5be66910315c0081ca08fe1d64ef320e97 Mon Sep 17 00:00:00 2001 From: Vova_rs Date: Sat, 30 May 2026 14:14:22 +0300 Subject: [PATCH 2/2] debug fn --- src/world/.mod.rs.kate-swp | Bin 324 -> 0 bytes src/world/agent.rs | 3 +++ src/world/mod.rs | 20 +++++++++++++++++++- 3 files changed, 22 insertions(+), 1 deletion(-) delete mode 100644 src/world/.mod.rs.kate-swp diff --git a/src/world/.mod.rs.kate-swp b/src/world/.mod.rs.kate-swp deleted file mode 100644 index 241a0e52d426a4f942dde428206a5a064ce0830e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 324 zcmZQzU=Z?7EJ;-eE>A2_aLdd|RWQ;sU|?VnsXo|ItzGS*U)>?dK|#S4MTi3=1j0d3bsP)~3_Pf6ctG+n3{%GgvJq9C089;xhN%@`U|^7e qsI`Z (i32, i32) { self.pos.clone() } + pub fn get_a(&self) -> i8 { + self.ang.clone() + } } diff --git a/src/world/mod.rs b/src/world/mod.rs index 0399f5a..6eda647 100644 --- a/src/world/mod.rs +++ b/src/world/mod.rs @@ -1,7 +1,7 @@ use smallvec::{SmallVec, smallvec}; mod grid; -use std::sync::{Arc, Mutex}; +use std::{fmt::format, sync::{Arc, Mutex}}; pub use grid::TorusGrid; @@ -124,4 +124,22 @@ impl World { } } } + fn debug_a(&self, n: usize) -> Option { + if let Some(a) = self.agents.get(n) { + return Some(format!("pos: {:?}, ang: {:?}, id: {n}", a.get_pos(), a.get_a())); + } + + None + } + pub fn debug(&self) -> String { + let mut a: String = String::new(); + for i in 0..self.agents.len() { + if let Some(t) = self.debug_a(i) { + a.push_str(&t); + } else { + a.push_str("None\r\n"); + } + } + a + } }