Compare commits
19
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b565771db2 | ||
|
|
021d91d1ea | ||
|
|
6708bf5be6 | ||
|
|
7cb55a9485 | ||
|
|
9a2a3937e2 | ||
|
|
a02b40c043 | ||
|
|
60bd764bcf | ||
|
|
93b0c65fed | ||
|
|
d4cfb25c8d | ||
|
|
c6b1b68572 | ||
|
|
580894ed0a | ||
|
|
cac35a4047 | ||
|
|
25af75b381 | ||
|
|
0eab202b00 | ||
|
|
f9842f635b | ||
|
|
125fd73625 | ||
|
|
66c0bb839e | ||
|
|
63f2ba31d0 | ||
|
|
2359187628 |
No files matched your search
@@ -6,3 +6,4 @@ edition = "2024"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
crossterm = "0.29"
|
crossterm = "0.29"
|
||||||
rand = "0.8"
|
rand = "0.8"
|
||||||
|
smallvec = "1.15"
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
error,
|
error,
|
||||||
fmt::format,
|
fmt::format,
|
||||||
@@ -12,6 +14,7 @@ use crossterm::{
|
|||||||
terminal::{Clear, ClearType, enable_raw_mode},
|
terminal::{Clear, ClearType, enable_raw_mode},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use super::Agent;
|
||||||
use super::TorusGrid;
|
use super::TorusGrid;
|
||||||
|
|
||||||
pub enum PosCom {
|
pub enum PosCom {
|
||||||
@@ -78,17 +81,28 @@ impl Render {
|
|||||||
ResetColor
|
ResetColor
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
pub fn frame(&mut self, grid: &TorusGrid<i32>) {
|
pub fn frame(&mut self, grid: &TorusGrid<bool>, a_grid: &TorusGrid<Option<usize>>) {
|
||||||
let mut out = self.out.lock();
|
let mut out = self.out.lock();
|
||||||
_ = queue!(out, MoveTo(0, 0));
|
_ = queue!(out, Clear(ClearType::All), MoveTo(0, 0));
|
||||||
for i in 0..self.rez.0 {
|
for i in 0..self.rez.0 {
|
||||||
for j in 0..self.rez.1 {
|
for j in 0..self.rez.1 {
|
||||||
let a = grid.get_wrap((i as i32 + self.pos.0, j as i32 + self.pos.1));
|
let ca: Color;
|
||||||
let b = *a as u8;
|
let cg: Color;
|
||||||
|
let a = a_grid.get_wrap((i as i32 + self.pos.0, j as i32 + self.pos.1));
|
||||||
|
let g = grid.get_wrap((i as i32 + self.pos.0, j as i32 + self.pos.1));
|
||||||
|
let c = *g as u8 * 200;
|
||||||
|
cg = Color::Rgb { r: c, g: c, b: c };
|
||||||
|
if a.is_some() {
|
||||||
|
//c = Color::Red;
|
||||||
|
ca = Color::Red;
|
||||||
|
} else {
|
||||||
|
ca = cg;
|
||||||
|
}
|
||||||
_ = queue!(
|
_ = queue!(
|
||||||
out,
|
out,
|
||||||
SetBackgroundColor(Color::Rgb { r: b, g: b, b: b }),
|
SetBackgroundColor(cg),
|
||||||
Print(" "),
|
SetForegroundColor(ca),
|
||||||
|
Print("#"),
|
||||||
ResetColor
|
ResetColor
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
use core::error;
|
use core::error;
|
||||||
use crossterm::{
|
use crossterm::{
|
||||||
|
cursor::Show,
|
||||||
event::{self, Event, KeyCode},
|
event::{self, Event, KeyCode},
|
||||||
terminal::{disable_raw_mode, enable_raw_mode},
|
terminal::{disable_raw_mode, enable_raw_mode},
|
||||||
};
|
};
|
||||||
@@ -48,6 +49,7 @@ pub enum KeyEvent {
|
|||||||
W,
|
W,
|
||||||
S,
|
S,
|
||||||
D,
|
D,
|
||||||
|
P,
|
||||||
}
|
}
|
||||||
pub fn Key_pars() -> Result<Option<KeyEvent>, std::io::Error> {
|
pub fn Key_pars() -> Result<Option<KeyEvent>, std::io::Error> {
|
||||||
if let Event::Key(key) = event::read()? {
|
if let Event::Key(key) = event::read()? {
|
||||||
@@ -76,6 +78,9 @@ pub fn Key_pars() -> Result<Option<KeyEvent>, std::io::Error> {
|
|||||||
KeyCode::Char('d') => {
|
KeyCode::Char('d') => {
|
||||||
return Ok(Some(KeyEvent::D));
|
return Ok(Some(KeyEvent::D));
|
||||||
}
|
}
|
||||||
|
KeyCode::Char('p') => {
|
||||||
|
return Ok(Some(KeyEvent::P));
|
||||||
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+36
-17
@@ -1,6 +1,8 @@
|
|||||||
|
use std::{clone, sync::atomic::AtomicI32};
|
||||||
|
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
mod world;
|
mod world;
|
||||||
pub use world::*;
|
use world::*;
|
||||||
|
|
||||||
mod input;
|
mod input;
|
||||||
use input::*;
|
use input::*;
|
||||||
@@ -8,33 +10,46 @@ use input::*;
|
|||||||
mod console_render;
|
mod console_render;
|
||||||
use console_render::*;
|
use console_render::*;
|
||||||
|
|
||||||
enum Input_type {
|
enum InputType {
|
||||||
comand,
|
comand,
|
||||||
keybard,
|
keybard,
|
||||||
}
|
}
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello, world!");
|
let mut rnd = rand::thread_rng();
|
||||||
let mut rng = rand::thread_rng();
|
let mut it = InputType::comand;
|
||||||
let mut it = Input_type::comand;
|
|
||||||
let mut R = Render::new();
|
let mut R = Render::new();
|
||||||
R.clear();
|
R.clear();
|
||||||
R.set_rez(40, 80);
|
_ = R.set_rez(40, 80);
|
||||||
|
|
||||||
let g_rez: (usize, usize) = (100, 50);
|
let rez: (usize, usize) = (100, 100);
|
||||||
let mut grid: TorusGrid<i32> = TorusGrid::new(0, g_rez);
|
let mut w = world::World::new(rez);
|
||||||
|
|
||||||
for i in 0..g_rez.0 {
|
/*
|
||||||
for j in 0..g_rez.1 {
|
for i in 0..rez.0 {
|
||||||
if let Some(a) = grid.get_mut((i as i32, j as i32)) {
|
for j in 0..rez.1 {
|
||||||
*a = rng.gen_range(0..=255) as i32;
|
if let Some(a) = w.get_mut_grid().get_mut((i as i32, j as i32)) {
|
||||||
|
*a = rnd.gen_range(0..=1) == 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
for mut i in 0..20 {
|
||||||
|
let x = rnd.gen_range(0..=rez.0) as i32;
|
||||||
|
let y = rnd.gen_range(0..=rez.1) as i32;
|
||||||
|
let a = w.add_agent(world::Agent::new(x, y, 0));
|
||||||
|
if a.is_err() {
|
||||||
|
R.print_err(a.err().unwrap());
|
||||||
|
i -= 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
//R.clear();
|
//R.clear();
|
||||||
R.frame(&grid);
|
R.frame(w.get_grid(), w.get_a_grid());
|
||||||
|
R.print(&w.debug());
|
||||||
match it {
|
match it {
|
||||||
Input_type::comand => {
|
InputType::comand => {
|
||||||
R.print("Input your comand: ");
|
R.print("Input your comand: ");
|
||||||
let input = Comand_pars();
|
let input = Comand_pars();
|
||||||
if input.is_err() {
|
if input.is_err() {
|
||||||
@@ -47,13 +62,13 @@ fn main() {
|
|||||||
}
|
}
|
||||||
ComandEvent::Key => {
|
ComandEvent::Key => {
|
||||||
R.print("jamp to keybard\n");
|
R.print("jamp to keybard\n");
|
||||||
it = Input_type::keybard
|
it = InputType::keybard
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Input_type::keybard => {
|
InputType::keybard => {
|
||||||
let input = Key_pars();
|
let input = Key_pars();
|
||||||
if input.is_err() {
|
if input.is_err() {
|
||||||
R.print_err(&format!("ERROR: {}", input.err().unwrap()));
|
R.print_err(&format!("ERROR: {}", input.err().unwrap()));
|
||||||
@@ -66,7 +81,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
KeyEvent::Comand => {
|
KeyEvent::Comand => {
|
||||||
R.print("jamp to comand");
|
R.print("jamp to comand");
|
||||||
it = Input_type::comand
|
it = InputType::comand
|
||||||
}
|
}
|
||||||
KeyEvent::A => {
|
KeyEvent::A => {
|
||||||
R.offset_pos(PosCom::LF);
|
R.offset_pos(PosCom::LF);
|
||||||
@@ -80,6 +95,10 @@ fn main() {
|
|||||||
KeyEvent::D => {
|
KeyEvent::D => {
|
||||||
R.offset_pos(PosCom::RH);
|
R.offset_pos(PosCom::RH);
|
||||||
}
|
}
|
||||||
|
KeyEvent::P => {
|
||||||
|
w.update();
|
||||||
|
w.render_grid_updaete();
|
||||||
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
|
||||||
|
const dir: [(i32, i32); 4] = [(1, 0), (0, 1), (-1, 0), (0, -1)];
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Agent{
|
||||||
|
pos: (i32, i32),
|
||||||
|
ang: i8,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl Agent {
|
||||||
|
pub fn new(x: i32, y: i32, a: i8) -> Self{
|
||||||
|
Agent {
|
||||||
|
pos: (x, y),
|
||||||
|
ang: a,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
pub fn get_pos(&self) -> (i32, i32) {
|
||||||
|
self.pos.clone()
|
||||||
|
}
|
||||||
|
pub fn get_a(&self) -> i8 {
|
||||||
|
self.ang.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
fn wrap_pos(p: (i32, i32), s: (usize, usize)) -> (usize, usize) {
|
||||||
|
let x = ((p.0 % s.0 as i32 + s.0 as i32) % s.0 as i32) as usize;
|
||||||
|
let y = ((p.1 % s.1 as i32 + s.1 as i32) % s.1 as i32) as usize;
|
||||||
|
|
||||||
|
(x, y)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct TorusGrid<T: Clone>{
|
||||||
|
vec: Vec<T>,
|
||||||
|
size: (usize, usize),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Clone> TorusGrid<T> {
|
||||||
|
pub fn new(value: T, s: (usize, usize)) -> Self{
|
||||||
|
let v = vec![value; s.0 * s.1];
|
||||||
|
TorusGrid {
|
||||||
|
vec: v,
|
||||||
|
size: s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn index(&self, x: usize, y: usize) -> usize {
|
||||||
|
y * self.size.0 + x
|
||||||
|
}
|
||||||
|
pub fn if_in_grid(&self, s: (i32, i32)) -> bool{
|
||||||
|
if s.0 < 0 || s.0 > self.size.0 as i32 - 1{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if s.1 < 0 || s.1 > self.size.1 as i32 - 1{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
|
pub fn get(&self, s: (i32, i32)) -> Option<&T> {
|
||||||
|
if self.if_in_grid(s) {
|
||||||
|
return Some(&self.vec[self.index(s.0 as usize, s.1 as usize)]);
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
pub fn get_mut(&mut self, s: (i32, i32)) -> Option<&mut T> {
|
||||||
|
if self.if_in_grid(s) {
|
||||||
|
let idx = self.index(s.0 as usize, s.1 as usize);
|
||||||
|
return Some(&mut self.vec[idx]);
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
pub fn get_wrap(&self, p: (i32, i32)) -> &T {
|
||||||
|
let a = wrap_pos(p, self.size);
|
||||||
|
&self.vec[self.index(a.0, a.1)]
|
||||||
|
}
|
||||||
|
pub fn get_mut_wrap(&mut self, p: (i32, i32)) -> &mut T {
|
||||||
|
let a = wrap_pos(p, self.size);
|
||||||
|
let idx = self.index(a.0, a.1);
|
||||||
|
&mut self.vec[idx]
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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<T: Clone> {
|
||||||
|
pub it: T,
|
||||||
|
pub lin: usize,
|
||||||
|
}
|
||||||
@@ -0,0 +1,124 @@
|
|||||||
|
use std::fmt::Debug;
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum MCError {
|
||||||
|
BrocenLen,
|
||||||
|
EntityNotExisit,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
pub struct MagicContainer<T>{
|
||||||
|
data_index: Vec<usize>,
|
||||||
|
id: Vec<usize>,
|
||||||
|
data: Vec<T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Clone> MagicContainer<T>{
|
||||||
|
pub fn new() -> Self {
|
||||||
|
MagicContainer {
|
||||||
|
data_index: Vec::new(),
|
||||||
|
id: Vec::new(),
|
||||||
|
data: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> MagicContainer<T> {
|
||||||
|
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<T: Debug> MagicContainer<T>{
|
||||||
|
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 {} => <deleted>", id),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("-------------------");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> IntoIterator for MagicContainer<T> {
|
||||||
|
type Item = T;
|
||||||
|
type IntoIter = std::vec::IntoIter<T>;
|
||||||
|
|
||||||
|
fn into_iter(self) -> Self::IntoIter {
|
||||||
|
self.data.into_iter()
|
||||||
|
}
|
||||||
|
}
|
||||||
+135
-35
@@ -1,52 +1,152 @@
|
|||||||
fn wrap_pos(p: (i32, i32), s: (usize, usize)) -> (usize, usize) {
|
use smallvec::{SmallVec, smallvec};
|
||||||
let x = ((p.0 % s.0 as i32 + s.0 as i32) % s.0 as i32) as usize;
|
|
||||||
let y = ((p.1 % s.1 as i32 + s.1 as i32) % s.1 as i32) as usize;
|
|
||||||
|
|
||||||
(x, y)
|
mod grid;
|
||||||
}
|
use std::{
|
||||||
|
fmt::format,
|
||||||
|
sync::{Arc, Mutex},
|
||||||
|
};
|
||||||
|
|
||||||
pub struct TorusGrid<T: Clone> {
|
pub use grid::TorusGrid;
|
||||||
vec: Vec<T>,
|
|
||||||
|
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),
|
size: (usize, usize),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Clone> TorusGrid<T> {
|
impl World {
|
||||||
pub fn new(value: T, s: (usize, usize)) -> Self {
|
pub fn new(s: (usize, usize)) -> Self {
|
||||||
let v = vec![value; s.0 * s.1];
|
World {
|
||||||
TorusGrid { vec: v, size: s }
|
grid: TorusGrid::new(false, s),
|
||||||
|
agents: MagicContainer::new(),
|
||||||
|
a_grid: TorusGrid::new(None, s),
|
||||||
|
size: s,
|
||||||
}
|
}
|
||||||
fn index(&self, x: usize, y: usize) -> usize {
|
|
||||||
y * self.size.0 + x
|
|
||||||
}
|
}
|
||||||
pub fn if_in_grid(&self, s: (i32, i32)) -> bool {
|
pub fn get_size(&self) -> (usize, usize) {
|
||||||
if s.0 < 0 || s.0 > self.size.0 as i32 - 1 {
|
self.size.clone()
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
if s.1 < 0 || s.1 > self.size.1 as i32 - 1 {
|
pub fn add_agent(&mut self, ag: Agent) -> Result<(), &str> {
|
||||||
return false;
|
let pos = ag.get_pos();
|
||||||
|
let p = self.a_grid.get_mut_wrap(pos);
|
||||||
|
if p.is_some() {
|
||||||
|
return Err("ERROR: pos занято");
|
||||||
}
|
}
|
||||||
true
|
let id = self.agents.add_and_id(ag);
|
||||||
|
*p = Some(id);
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
pub fn get(&self, s: (i32, i32)) -> Option<&T> {
|
pub fn get_grid(&self) -> &TorusGrid<bool> {
|
||||||
if self.if_in_grid(s) {
|
&self.grid
|
||||||
return Some(&self.vec[self.index(s.0 as usize, s.1 as usize)]);
|
|
||||||
}
|
}
|
||||||
|
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
|
None
|
||||||
}
|
}
|
||||||
pub fn get_mut(&mut self, s: (i32, i32)) -> Option<&mut T> {
|
pub fn debug(&self) -> String {
|
||||||
if self.if_in_grid(s) {
|
let mut a: String = String::new();
|
||||||
let idx = self.index(s.0 as usize, s.1 as usize);
|
for i in 0..self.agents.len() {
|
||||||
return Some(&mut self.vec[idx]);
|
if let Some(t) = self.debug_a(i) {
|
||||||
|
a.push_str(&t);
|
||||||
|
} else {
|
||||||
|
a.push_str("None\r\n");
|
||||||
}
|
}
|
||||||
None
|
|
||||||
}
|
}
|
||||||
pub fn get_wrap(&self, p: (i32, i32)) -> &T {
|
a
|
||||||
let a = wrap_pos(p, self.size);
|
|
||||||
&self.vec[self.index(a.0, a.1)]
|
|
||||||
}
|
|
||||||
pub fn get_mut_wrap(&mut self, p: (i32, i32)) -> &mut T {
|
|
||||||
let a = wrap_pos(p, self.size);
|
|
||||||
let idx = self.index(a.0, a.1);
|
|
||||||
&mut self.vec[idx]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user