diff --git a/Cargo.toml b/Cargo.toml index 3b98a3a..b386c27 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,4 @@ edition = "2024" [dependencies] crossterm = "0.29" rand = "0.8" +smallvec = "1.15" diff --git a/src/console_render/mod.rs b/src/console_render/mod.rs index 28e7ee3..66979c7 100644 --- a/src/console_render/mod.rs +++ b/src/console_render/mod.rs @@ -81,7 +81,7 @@ impl Render { ResetColor ); } - pub fn frame(&mut self, grid: &TorusGrid, a_grid: &TorusGrid>>>) { + pub fn frame(&mut self, grid: &TorusGrid, a_grid: &TorusGrid>) { let mut out = self.out.lock(); _ = queue!(out, MoveTo(0, 0)); for i in 0..self.rez.0 { diff --git a/src/input/mod.rs b/src/input/mod.rs index a4dac8b..01c378a 100644 --- a/src/input/mod.rs +++ b/src/input/mod.rs @@ -1,5 +1,6 @@ use core::error; use crossterm::{ + cursor::Show, event::{self, Event, KeyCode}, terminal::{disable_raw_mode, enable_raw_mode}, }; @@ -48,6 +49,7 @@ pub enum KeyEvent { W, S, D, + P, } pub fn Key_pars() -> Result, std::io::Error> { if let Event::Key(key) = event::read()? { @@ -76,6 +78,9 @@ pub fn Key_pars() -> Result, std::io::Error> { KeyCode::Char('d') => { return Ok(Some(KeyEvent::D)); } + KeyCode::Char('p') => { + return Ok(Some(KeyEvent::P)); + } _ => {} } } diff --git a/src/main.rs b/src/main.rs index d353780..cdf283e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ use std::{clone, sync::atomic::AtomicI32}; use rand::Rng; mod world; -pub use world::*; +use world::*; mod input; use input::*; @@ -92,6 +92,10 @@ fn main() { KeyEvent::D => { R.offset_pos(PosCom::RH); } + KeyEvent::P => { + w.update(); + w.render_grid_updaete(); + } _ => {} } } diff --git a/src/world/.mod.rs.kate-swp b/src/world/.mod.rs.kate-swp new file mode 100644 index 0000000..241a0e5 Binary files /dev/null and b/src/world/.mod.rs.kate-swp differ diff --git a/src/world/agent.rs b/src/world/agent.rs index 0002e24..20579f4 100644 --- a/src/world/agent.rs +++ b/src/world/agent.rs @@ -1,5 +1,5 @@ - +const dir: [(i32, i32); 4] = [(1, 0), (0, 1), (-1, 0), (0, -1)]; #[derive(Clone)] pub struct Agent{ @@ -15,22 +15,12 @@ impl Agent { ang: a, } } - pub fn wolk_a(&mut self){ - match self.ang { - 0 => { - self.pos.0 += 1; - } - 1 => { - self.pos.1 += 1; - } - 2 => { - self.pos.0 -= 1; - } - 3 => { - self.pos.1 -= 1; - } - _ => {} - } + pub fn wolk_a(&self) -> (i32, i32){ + let a = dir[self.ang as usize].clone(); + (a.0 + self.pos.0, a.1 + self.pos.1) + } + pub fn move_to(&mut self, pos: (i32, i32)){ + self.pos = pos; } pub fn rotate(&mut self, a: i8){ self.ang = ((self.ang + a) % 4 + 4) % 4; diff --git a/src/world/grid/mod.rs b/src/world/grid.rs similarity index 100% rename from src/world/grid/mod.rs rename to src/world/grid.rs diff --git a/src/world/intention.rs b/src/world/intention.rs new file mode 100644 index 0000000..c5daf42 --- /dev/null +++ b/src/world/intention.rs @@ -0,0 +1,21 @@ +// use super::Agent; +// use super::{MagicContainer, MCError}; +// use super::TorusGrid; + +#[derive(PartialEq, Clone)] +pub enum Intention { + MoveTo, + StakedOut, +} + +#[derive(PartialEq, Clone)] +pub enum LocalItentoin { + Rotate(i8), + SwapCell((i32, i32)), +} + +#[derive(PartialEq, Clone)] +pub struct LinkIntention { + pub it: T, + pub lin: usize, +} diff --git a/src/world/m_container.rs b/src/world/m_container.rs new file mode 100644 index 0000000..d7ea0ac --- /dev/null +++ b/src/world/m_container.rs @@ -0,0 +1,124 @@ +use std::fmt::Debug; + + +#[derive(Debug)] +pub enum MCError { + BrocenLen, + EntityNotExisit, +} + + + +pub struct MagicContainer{ + data_index: Vec, + id: Vec, + data: Vec, +} + +impl MagicContainer{ + pub fn new() -> Self { + MagicContainer { + data_index: Vec::new(), + id: Vec::new(), + data: Vec::new(), + } + } +} + +impl MagicContainer { + pub fn len(&self) -> usize { + self.data_index.len() + } + pub fn add(&mut self, value: T){ + let l = self.data_index.len(); + if l == self.data.len() { + self.data_index.push(l); + self.id.push(l); + self.data.push(value); + } else if l > self.data.len() { + self.data.push(value); + } else { + panic!("data_index < data"); + } + } + pub fn add_and_id(&mut self, value: T) -> usize{ + let l = self.data_index.len(); + if l == self.data.len() { + self.data_index.push(l); + self.id.push(l); + self.data.push(value); + } else if l > self.data.len() { + self.data.push(value); + } else { + panic!("data_index < data"); + } + + self.id[self.data.len() -1] + } + pub fn rm(&mut self, entity_id: usize) -> Result<(), MCError>{ + let index = self.data_index[entity_id]; + let last = self.data.len() - 1; + + if self.data.len() > self.data_index.len() { + return Err(MCError::BrocenLen); + } + if index > last { + return Err(MCError::EntityNotExisit); + } + + self.id.swap(last, index); + self.data.swap(last, index); + self.data_index[self.id[index]] = index; + self.data_index[self.id[last]] = last; + self.data.pop(); + + Ok(()) + } + pub fn get(&self, entity_id: usize) -> Option<&T> { + let index = *self.data_index.get(entity_id)?; + + if index >= self.data.len() { + return None; + } + + Some(&self.data[index]) + } + pub fn get_mut(&mut self, entity_id: usize) -> Option<&mut T> { + let index = *self.data_index.get(entity_id)?; + + if index >= self.data.len() { + return None; + } + + Some(&mut self.data[index]) + } +} + +impl MagicContainer{ + pub fn _debug(&self) { + println!("data_index: {:?}", self.data_index); + println!("id: {:?}", self.id); + println!("data: {:?}", self.data); + } + pub fn debug_log(&self){ + println!("--- access test ---"); + + for id in 0..self.data_index.len() { + match self.get(id) { + Some(v) => println!("ID {} => {:?}", id, v), + None => println!("ID {} => ", id), + } + } + + println!("-------------------"); + } +} + +impl IntoIterator for MagicContainer { + type Item = T; + type IntoIter = std::vec::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.data.into_iter() + } +} diff --git a/src/world/mod.rs b/src/world/mod.rs index b3cd2b0..0399f5a 100644 --- a/src/world/mod.rs +++ b/src/world/mod.rs @@ -1,3 +1,5 @@ +use smallvec::{SmallVec, smallvec}; + mod grid; use std::sync::{Arc, Mutex}; @@ -6,10 +8,16 @@ pub use grid::TorusGrid; mod agent; pub use agent::Agent; +mod m_container; +pub use m_container::*; + +mod intention; +pub use intention::*; + pub struct World { grid: TorusGrid, - agents: Vec>>, - a_grid: TorusGrid>>>, + agents: MagicContainer, + a_grid: TorusGrid>, size: (usize, usize), } @@ -17,7 +25,7 @@ impl World { pub fn new(s: (usize, usize)) -> Self { World { grid: TorusGrid::new(false, s), - agents: Vec::new(), + agents: MagicContainer::new(), a_grid: TorusGrid::new(None, s), size: s, } @@ -28,14 +36,11 @@ impl World { pub fn add_agent(&mut self, ag: Agent) -> Result<(), &str> { let pos = ag.get_pos(); let p = self.a_grid.get_mut_wrap(pos); - // if *p != None { - // return Err("w"); - // } if p.is_some() { return Err("ERROR: pos занято"); } - self.agents.push(Arc::new(Mutex::new(ag))); - *p = Some(Arc::clone(&self.agents[self.agents.len() - 1])); + let id = self.agents.add_and_id(ag); + *p = Some(id); Ok(()) } pub fn get_grid(&self) -> &TorusGrid { @@ -44,7 +49,79 @@ impl World { 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()); + + 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); + } + } + } }