From 25af75b381fa022dd7b89c3c31fcef53c085904f Mon Sep 17 00:00:00 2001 From: Vova_rs Date: Fri, 15 May 2026 17:33:20 +0300 Subject: [PATCH 1/7] refactor add_agent --- src/world/mod.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/world/mod.rs b/src/world/mod.rs index e15a709..3493b74 100644 --- a/src/world/mod.rs +++ b/src/world/mod.rs @@ -34,8 +34,9 @@ impl World { 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 rec_ag = Arc::new(Mutex::new(ag)); + self.agents.push(Arc::clone(&rec_ag)); + *p = Some(Arc::clone(&rec_ag)); Ok(()) } pub fn get_grid(&self) -> &TorusGrid{ From 580894ed0a6fafe5d0b6c8b2364c81fc9b2eeaf3 Mon Sep 17 00:00:00 2001 From: Vova_rs Date: Fri, 22 May 2026 12:37:41 +0300 Subject: [PATCH 2/7] m_container: is done --- src/main.rs | 36 ++++++++++ src/world/{grid/mod.rs => grid.rs} | 0 src/world/m_container.rs | 103 +++++++++++++++++++++++++++++ src/world/mod.rs | 20 +++--- 4 files changed, 149 insertions(+), 10 deletions(-) rename src/world/{grid/mod.rs => grid.rs} (100%) create mode 100644 src/world/m_container.rs diff --git a/src/main.rs b/src/main.rs index 96e1d75..7aebfbc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,43 @@ mod world; +use world::*; fn main() { println!("Hello, dev!"); + + let mut ma: MagicContainer = MagicContainer::new(); + + ma.add(2); + ma.add(3); + ma.add(4); + ma.add(5); + ma.add(6); + + ma.debug_log(); + + if let Err(a) = ma.rm(1) { + println!("ERROR: {:?}", a); + return; + } + + ma.debug_log(); + + if let Err(a) = ma.rm(0) { + println!("ERROR: {:?}", a); + return; + } + + ma.debug_log(); + + + ma.add(8); + + ma.debug_log(); + + ma.add(9); + ma.add(10); + ma.add(11); + + ma.debug_log(); } 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/m_container.rs b/src/world/m_container.rs new file mode 100644 index 0000000..10a40fe --- /dev/null +++ b/src/world/m_container.rs @@ -0,0 +1,103 @@ +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 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]) + } +} + +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!("-------------------"); + } +} diff --git a/src/world/mod.rs b/src/world/mod.rs index 3493b74..9f25f74 100644 --- a/src/world/mod.rs +++ b/src/world/mod.rs @@ -6,10 +6,13 @@ pub use grid::TorusGrid; mod agent; pub use agent::Agent; +mod m_container; +pub use m_container::*; + pub struct World{ grid: TorusGrid, - agents: Vec>>, - a_grid: TorusGrid>>>, + agents: MagicContainer, + a_grid: TorusGrid>, size: (usize, usize), } @@ -17,7 +20,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,15 +31,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 занято"); } - let rec_ag = Arc::new(Mutex::new(ag)); - self.agents.push(Arc::clone(&rec_ag)); - *p = Some(Arc::clone(&rec_ag)); + let id = self.agents.add_and_id(ag); + *p = Some(id); Ok(()) } pub fn get_grid(&self) -> &TorusGrid{ @@ -45,8 +44,9 @@ 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 } From c6b1b6857230bc4a29c4bf8ad827447b8261ab43 Mon Sep 17 00:00:00 2001 From: Vova_rs Date: Tue, 26 May 2026 12:28:00 +0300 Subject: [PATCH 3/7] intention is dote --- Cargo.toml | 3 +++ src/world/.mod.rs.kate-swp | Bin 0 -> 324 bytes src/world/agent.rs | 24 +++++++----------------- src/world/intention.rs | 23 +++++++++++++++++++++++ src/world/m_container.rs | 23 ++++++++++++++++++++++- src/world/mod.rs | 19 ++++++++++++++++++- 6 files changed, 73 insertions(+), 19 deletions(-) create mode 100644 src/world/.mod.rs.kate-swp create mode 100644 src/world/intention.rs diff --git a/Cargo.toml b/Cargo.toml index 621c4e1..e72f758 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,6 @@ version = "0.1.0" edition = "2024" [dependencies] +smallvec = "1.15" + + diff --git a/src/world/.mod.rs.kate-swp b/src/world/.mod.rs.kate-swp new file mode 100644 index 0000000000000000000000000000000000000000..241a0e52d426a4f942dde428206a5a064ce0830e GIT binary patch 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 { - 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/intention.rs b/src/world/intention.rs new file mode 100644 index 0000000..52b4663 --- /dev/null +++ b/src/world/intention.rs @@ -0,0 +1,23 @@ +// use super::Agent; +// use super::{MagicContainer, MCError}; +// use super::TorusGrid; + + +#[derive(Clone)] +pub enum Intention { + MoveTo, + StakedOut, +} + + +#[derive(Clone)] +pub enum LocalItentoin { + Rotate(i8), + SwapCell, +} + +#[derive(Clone)] +pub struct LinkIntention{ + pub it: T, + pub lin: usize, +} diff --git a/src/world/m_container.rs b/src/world/m_container.rs index 10a40fe..d7ea0ac 100644 --- a/src/world/m_container.rs +++ b/src/world/m_container.rs @@ -26,6 +26,9 @@ impl MagicContainer{ } 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() { @@ -80,10 +83,19 @@ impl MagicContainer { 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) { + pub fn _debug(&self) { println!("data_index: {:?}", self.data_index); println!("id: {:?}", self.id); println!("data: {:?}", self.data); @@ -101,3 +113,12 @@ impl MagicContainer{ 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 9f25f74..7bc1887 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}; @@ -9,6 +11,9 @@ pub use agent::Agent; mod m_container; pub use m_container::*; +mod intention; +pub use intention::*; + pub struct World{ grid: TorusGrid, agents: MagicContainer, @@ -47,6 +52,18 @@ impl World { pub fn get_a_grid(&self) -> &TorusGrid>{ &self.a_grid } - // pub fn + 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()); + + + } + } + } } From d4cfb25c8de91ebd21d78bca74011187142683be Mon Sep 17 00:00:00 2001 From: lemon-cat Date: Tue, 26 May 2026 13:01:56 +0300 Subject: [PATCH 4/7] add local itention for agent --- src/world/intention.rs | 6 ++---- src/world/mod.rs | 28 +++++++++++++++++----------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/world/intention.rs b/src/world/intention.rs index 52b4663..b48f4b9 100644 --- a/src/world/intention.rs +++ b/src/world/intention.rs @@ -2,22 +2,20 @@ // use super::{MagicContainer, MCError}; // use super::TorusGrid; - #[derive(Clone)] pub enum Intention { MoveTo, StakedOut, } - #[derive(Clone)] pub enum LocalItentoin { Rotate(i8), - SwapCell, + SwapCell((i32, i32)), } #[derive(Clone)] -pub struct LinkIntention{ +pub struct LinkIntention { pub it: T, pub lin: usize, } diff --git a/src/world/mod.rs b/src/world/mod.rs index 7bc1887..f2f1d64 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,33 @@ 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, + }); } } } } - From 93b0c65fedd4e48e1122e00000e619ee3214e73a Mon Sep 17 00:00:00 2001 From: lemon-cat Date: Tue, 26 May 2026 13:37:05 +0300 Subject: [PATCH 5/7] relase local inteintoon --- src/world/mod.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/world/mod.rs b/src/world/mod.rs index f2f1d64..e9eae17 100644 --- a/src/world/mod.rs +++ b/src/world/mod.rs @@ -71,5 +71,20 @@ impl World { }); } } + 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; + } + } + } + } } } From 60bd764bcf394f9fd05232227bf19b7e0a602bb1 Mon Sep 17 00:00:00 2001 From: lemon-cat Date: Wed, 27 May 2026 11:22:25 +0300 Subject: [PATCH 6/7] add vec gloval_it and push in this --- src/world/intention.rs | 4 ++-- src/world/mod.rs | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/world/intention.rs b/src/world/intention.rs index b48f4b9..dfd8156 100644 --- a/src/world/intention.rs +++ b/src/world/intention.rs @@ -4,8 +4,8 @@ #[derive(Clone)] pub enum Intention { - MoveTo, - StakedOut, + MoveTo((i32, i32)), + StakedOut((i32, i32)), } #[derive(Clone)] diff --git a/src/world/mod.rs b/src/world/mod.rs index e9eae17..941a7a8 100644 --- a/src/world/mod.rs +++ b/src/world/mod.rs @@ -86,5 +86,18 @@ impl World { } } } + let mut global_it: Vec> = Vec::new(); + for i in 0..self.agents.len() { + if let Some(ag) = self.agents.get(i) { + global_it.push(LinkIntention { + it: Intention::MoveTo(ag.wolk_a()), + lin: i, + }); + global_it.push(LinkIntention { + it: Intention::StakedOut(ag.get_pos()), + lin: i, + }); + } + } } } From a02b40c0436a9478e575815e7704fd6e5aa3b8ff Mon Sep 17 00:00:00 2001 From: lemon-cat Date: Wed, 27 May 2026 12:48:31 +0300 Subject: [PATCH 7/7] relese global intention --- src/world/intention.rs | 10 +++++----- src/world/mod.rs | 24 +++++++++++++++++++----- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/world/intention.rs b/src/world/intention.rs index dfd8156..c5daf42 100644 --- a/src/world/intention.rs +++ b/src/world/intention.rs @@ -2,19 +2,19 @@ // use super::{MagicContainer, MCError}; // use super::TorusGrid; -#[derive(Clone)] +#[derive(PartialEq, Clone)] pub enum Intention { - MoveTo((i32, i32)), - StakedOut((i32, i32)), + MoveTo, + StakedOut, } -#[derive(Clone)] +#[derive(PartialEq, Clone)] pub enum LocalItentoin { Rotate(i8), SwapCell((i32, i32)), } -#[derive(Clone)] +#[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 941a7a8..2d29a36 100644 --- a/src/world/mod.rs +++ b/src/world/mod.rs @@ -86,18 +86,32 @@ impl World { } } } - let mut global_it: Vec> = Vec::new(); for i in 0..self.agents.len() { if let Some(ag) = self.agents.get(i) { - global_it.push(LinkIntention { - it: Intention::MoveTo(ag.wolk_a()), + let pos = ag.get_pos(); + let mut p = plane_of_intentions.get_mut_wrap(pos); + p.push(LinkIntention { + it: Intention::StakedOut, lin: i, }); - global_it.push(LinkIntention { - it: Intention::StakedOut(ag.get_pos()), + 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()); + } + } + } + } } }