146 lines
4.4 KiB
Rust
146 lines
4.4 KiB
Rust
use smallvec::{SmallVec, smallvec};
|
|
|
|
mod grid;
|
|
use std::{fmt::format, sync::{Arc, Mutex}};
|
|
|
|
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<bool>,
|
|
agents: MagicContainer<Agent>,
|
|
a_grid: TorusGrid<Option<usize>>,
|
|
size: (usize, usize),
|
|
}
|
|
|
|
impl World {
|
|
pub fn new(s: (usize, usize)) -> Self {
|
|
World {
|
|
grid: TorusGrid::new(false, s),
|
|
agents: MagicContainer::new(),
|
|
a_grid: TorusGrid::new(None, s),
|
|
size: s,
|
|
}
|
|
}
|
|
pub fn get_size(&self) -> (usize, usize) {
|
|
self.size.clone()
|
|
}
|
|
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.is_some() {
|
|
return Err("ERROR: pos занято");
|
|
}
|
|
let id = self.agents.add_and_id(ag);
|
|
*p = Some(id);
|
|
Ok(())
|
|
}
|
|
pub fn get_grid(&self) -> &TorusGrid<bool> {
|
|
&self.grid
|
|
}
|
|
pub fn get_mut_grid(&mut self) -> &mut TorusGrid<bool> {
|
|
&mut self.grid
|
|
}
|
|
pub fn get_a_grid(&self) -> &TorusGrid<Option<usize>> {
|
|
&self.a_grid
|
|
}
|
|
pub fn update(&mut self) {
|
|
let mut plane_of_intentions: TorusGrid<SmallVec<[LinkIntention<Intention>; 4]>> =
|
|
TorusGrid::new(SmallVec::new(), self.size.clone());
|
|
|
|
let mut local_it: Vec<LinkIntention<LocalItentoin>> = 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);
|
|
}
|
|
}
|
|
}
|
|
fn debug_a(&self, n: usize) -> Option<String> {
|
|
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
|
|
}
|
|
}
|