Add new spawn points and update visuals, added detached snakes and wesps
This commit is contained in:
parent
c38bb5d992
commit
eed340b6bb
@ -6,7 +6,7 @@ use crossterm::style::Color;
|
|||||||
use crate::TERM_SIZE;
|
use crate::TERM_SIZE;
|
||||||
use crate::part_handler;
|
use crate::part_handler;
|
||||||
|
|
||||||
#[derive(PartialEq, Clone)]
|
#[derive(PartialEq, Clone, Hash, Eq, Copy)]
|
||||||
pub enum Species {
|
pub enum Species {
|
||||||
NormalSnake,
|
NormalSnake,
|
||||||
DetachedSnake,
|
DetachedSnake,
|
||||||
@ -59,7 +59,7 @@ impl Creature {
|
|||||||
let old_position = self.parts[0].position; // 0 Allways the head
|
let old_position = self.parts[0].position; // 0 Allways the head
|
||||||
self.parts[0].position = new_position;
|
self.parts[0].position = new_position;
|
||||||
self.curent_direction = moved_in_direction;
|
self.curent_direction = moved_in_direction;
|
||||||
if new_position.0 > TERM_SIZE.0 || new_position.1 > TERM_SIZE.1 || colision {
|
if new_position.0 > TERM_SIZE.0 || new_position.0 <= 0 || new_position.1 > TERM_SIZE.1 || new_position.1 <= 0 || colision {
|
||||||
self.killed = true;
|
self.killed = true;
|
||||||
}
|
}
|
||||||
self.parts.append(&mut vec![Part { element: part_handler::CHECK_DIRECTIONS[&self.curent_direction].1,
|
self.parts.append(&mut vec![Part { element: part_handler::CHECK_DIRECTIONS[&self.curent_direction].1,
|
||||||
|
|||||||
11
src/main.rs
11
src/main.rs
@ -14,14 +14,16 @@ use std::collections::HashMap;
|
|||||||
mod part_handler;
|
mod part_handler;
|
||||||
mod components;
|
mod components;
|
||||||
|
|
||||||
|
const SPAWNERS:u16 = 3;
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref TERM_SIZE: (u16, u16) = crossterm::terminal::size().unwrap_or_else(|_| panic!("Cannot get terminal size"));
|
static ref TERM_SIZE: (u16, u16) = crossterm::terminal::size().unwrap_or_else(|_| panic!("Cannot get terminal size"));
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static!{
|
lazy_static!{
|
||||||
static ref ELEMENT_VISUALS: HashMap<components::Element, char> = HashMap::from([
|
static ref ELEMENT_VISUALS: HashMap<components::Element, char> = HashMap::from([
|
||||||
(components::Element::Wall, 'H'),
|
(components::Element::Wall, '𐲕'),
|
||||||
(components::Element::Spawn, '@'),
|
(components::Element::Spawn, '⬟'),
|
||||||
(components::Element::BodyPartVert, '|'),
|
(components::Element::BodyPartVert, '|'),
|
||||||
(components::Element::BodyPartHori, '-'),
|
(components::Element::BodyPartHori, '-'),
|
||||||
(components::Element::BodyPartRightLean, '\\'),
|
(components::Element::BodyPartRightLean, '\\'),
|
||||||
@ -47,9 +49,10 @@ fn create_canvas() -> CanvasParts{
|
|||||||
canvas.add_element(components::Element::Wall, (0, y), Some(wall_color), None); // Use array indexing instead of tuple indexing
|
canvas.add_element(components::Element::Wall, (0, y), Some(wall_color), None); // Use array indexing instead of tuple indexing
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _ in 0..3{
|
let spawner_ranges = (TERM_SIZE.0-1) / SPAWNERS as u16;
|
||||||
|
for i in 0..SPAWNERS{
|
||||||
// Use array indexing instead of tuple indexing
|
// Use array indexing instead of tuple indexing
|
||||||
canvas.add_element(components::Element::Spawn, (rng.gen_range(1..TERM_SIZE.0-1), rng.gen_range(1..TERM_SIZE.1-1)), Some(Color::Rgb { r: 10, g: 255, b: 10 }), None);
|
canvas.add_element(components::Element::Spawn, (rng.gen_range((spawner_ranges*i)..spawner_ranges*(i+1)), rng.gen_range(1..TERM_SIZE.1-1)), Some(Color::Rgb { r: 10, g: 255, b: 10 }), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
canvas // Return the canvas vector
|
canvas // Return the canvas vector
|
||||||
|
|||||||
@ -2,7 +2,6 @@ use std::thread;
|
|||||||
use std::sync::mpsc;
|
use std::sync::mpsc;
|
||||||
use crossterm::style::Color;
|
use crossterm::style::Color;
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use rand::seq::index;
|
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use crate::HashMap;
|
use crate::HashMap;
|
||||||
|
|
||||||
@ -32,6 +31,13 @@ lazy_static!{
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lazy_static!{
|
||||||
|
static ref ITERATION_TIMES: HashMap<components::Species, u8> = HashMap::from([
|
||||||
|
(Species::DetachedSnake, 1),
|
||||||
|
(Species::NormalSnake, 1),
|
||||||
|
(Species::Wesp, 2),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
fn check_collision(canvas: &Vec<Part>, position: (u16, u16)) -> bool {
|
fn check_collision(canvas: &Vec<Part>, position: (u16, u16)) -> bool {
|
||||||
@ -75,16 +81,47 @@ pub fn spawner_handle(canvas: &mut CanvasParts) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if !check_collision(&canvas.environment, (pos.0, pos.1)) {
|
if !check_collision(&canvas.environment, (pos.0, pos.1)) {
|
||||||
canvas.add_creature((pos.0, pos.1), get_unused_color(&canvas.alive), pos.2, Species::NormalSnake, canvas.interactable[index].position);
|
let color = get_unused_color(&canvas.alive);
|
||||||
canvas.interactable[index].color = Color::Rgb { r: 10, g: 100, b: 10 };
|
match x % 10 {
|
||||||
|
0 | 1 => canvas.add_creature((pos.0, pos.1), color, pos.2, Species::DetachedSnake, canvas.interactable[index].position),
|
||||||
|
2 => canvas.add_creature((pos.0, pos.1), color, pos.2, Species::Wesp, canvas.interactable[index].position),
|
||||||
|
_ => canvas.add_creature((pos.0, pos.1), color, pos.2, Species::NormalSnake, canvas.interactable[index].position),
|
||||||
|
}
|
||||||
|
canvas.interactable[index].color = Color::Rgb { r: 10, g: 100, b: 10 };
|
||||||
|
canvas.alive.sort_by(|a, b| {
|
||||||
|
match (a.species, b.species) {
|
||||||
|
(_, Species::Wesp) => std::cmp::Ordering::Less,
|
||||||
|
(Species::Wesp, _) => std::cmp::Ordering::Greater,
|
||||||
|
_ => std::cmp::Ordering::Equal,
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn recursive_colision_check(color: &Color, parts_in_sight: &Vec<Part>, position: &(u16, u16),
|
|
||||||
direction: &(Direction, Direction), iterations_left: u8) -> (i32, i32, (Direction, Direction), i64){
|
fn snake_path_match(elem:Element) -> i64 {
|
||||||
|
match elem {
|
||||||
|
Element::Wall | Element::Spawn => -100,
|
||||||
|
Element::BodyPartHori | Element::BodyPartVert |
|
||||||
|
Element::BodyPartLeftLean | Element::BodyPartRightLean | Element::BodyPartHead => -100,
|
||||||
|
Element::Food => 10,
|
||||||
|
_ => 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn wesp_path_match(elem:Element) -> i64 {
|
||||||
|
match elem {
|
||||||
|
Element::Wall | Element::Spawn | Element::Food => -100,
|
||||||
|
Element::BodyPartHead => 30,
|
||||||
|
_ => 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn recursive_colision_check(path_to_match: fn(Element) -> i64, parts_in_sight: &Vec<Part>, position: &(u16, u16),
|
||||||
|
direction: &(Direction, Direction), iterations_left: u8, ) -> (i32, i32, (Direction, Direction), i64){
|
||||||
// Position to check x, Position to check y, Direction to walk, Vale of Direction
|
// Position to check x, Position to check y, Direction to walk, Vale of Direction
|
||||||
let mut dyn_pos_res: Vec<(i32, i32, (Direction, Direction), i64)> = Vec::new();
|
let mut dyn_pos_res: Vec<(i32, i32, (Direction, Direction), i64)> = Vec::new();
|
||||||
|
|
||||||
@ -103,23 +140,16 @@ fn recursive_colision_check(color: &Color, parts_in_sight: &Vec<Part>, position:
|
|||||||
for part in parts_in_sight {
|
for part in parts_in_sight {
|
||||||
if part.position == (x as u16, y as u16) {
|
if part.position == (x as u16, y as u16) {
|
||||||
//When element in sight, part is element in sight
|
//When element in sight, part is element in sight
|
||||||
val = match part.element {
|
val = path_to_match(part.element)
|
||||||
Element::Wall | Element::Spawn => -100,
|
|
||||||
Element::BodyPartHori | Element::BodyPartVert |
|
|
||||||
Element::BodyPartLeftLean | Element::BodyPartRightLean | Element::BodyPartHead => val -100,
|
|
||||||
Element::Food => 10,
|
|
||||||
_ => 0,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
dyn_pos_res[index].3 = val;
|
dyn_pos_res[index].3 = val;
|
||||||
|
|
||||||
if iterations_left > 0 {
|
if iterations_left > 0 {
|
||||||
let sender = sender.clone();
|
let sender = sender.clone();
|
||||||
let color = color.clone();
|
|
||||||
let parts_in_sight = parts_in_sight.clone();
|
let parts_in_sight = parts_in_sight.clone();
|
||||||
threads.push(thread::spawn(move || {
|
threads.push(thread::spawn(move || {
|
||||||
let t_val = recursive_colision_check(&color, &parts_in_sight, &(x as u16, y as u16), &v_direction, iterations_left - 1).3;
|
let t_val = recursive_colision_check(path_to_match, &parts_in_sight, &(x as u16, y as u16), &v_direction, iterations_left - 1).3;
|
||||||
sender.send((t_val, index)).unwrap();
|
sender.send((t_val, index)).unwrap();
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
@ -143,55 +173,75 @@ pub fn head_handle(canvas: &mut CanvasParts) {
|
|||||||
//Todo: Reomve or improve threading in this function!!
|
//Todo: Reomve or improve threading in this function!!
|
||||||
// Find all elements that head can see
|
// Find all elements that head can see
|
||||||
let (sender, receiver) = mpsc::channel();
|
let (sender, receiver) = mpsc::channel();
|
||||||
|
let mut wesp_kills: Vec<usize> = Vec::new();
|
||||||
|
|
||||||
let unified_canvas = canvas.clone();
|
let cloned_canvas = canvas.clone();
|
||||||
for creature in canvas.alive.iter_mut() {
|
for creature in canvas.alive.iter_mut() {
|
||||||
let head = creature.parts.iter().filter(|elem| elem.element == Element::BodyPartHead).next();
|
for _ in 0..ITERATION_TIMES[&creature.species] {
|
||||||
if head.is_none() {
|
let head = creature.parts.iter().filter(|elem| elem.element == Element::BodyPartHead).next();
|
||||||
continue;
|
if head.is_none() {
|
||||||
}
|
continue;
|
||||||
let head = head.unwrap().clone(); // Clone the head to avoid borrowing issues
|
}
|
||||||
|
let head = head.unwrap().clone(); // Clone the head to avoid borrowing issues
|
||||||
|
|
||||||
|
|
||||||
|
// Concartination of all parts in sight
|
||||||
|
let parts_in_sight: Vec<Part> = cloned_canvas.unify_elements().iter()
|
||||||
|
.filter(|elem|
|
||||||
|
(elem.position.0 as i64 - head.position.0 as i64).pow(2) +
|
||||||
|
(elem.position.1 as i64 - head.position.1 as i64).pow(2) <= SIGHT_RADIUS.pow(2) as i64)
|
||||||
|
.map(|elem| **elem)
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
let matcher: fn(Element) -> i64 = match creature.species {
|
||||||
|
Species::DetachedSnake | Species::NormalSnake => snake_path_match,
|
||||||
|
Species::Wesp => wesp_path_match,
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
// Concartination of all parts in sight
|
let parts_in_sight_thread = parts_in_sight.clone();
|
||||||
let parts_in_sight: Vec<Part> = unified_canvas.unify_elements().iter()
|
let dir_thread = creature.curent_direction.clone();
|
||||||
.filter(|elem|
|
let sender = sender.clone();
|
||||||
(elem.position.0 as i64 - head.position.0 as i64).pow(2) +
|
|
||||||
(elem.position.1 as i64 - head.position.1 as i64).pow(2) <= SIGHT_RADIUS.pow(2) as i64)
|
|
||||||
.map(|elem| **elem)
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
|
|
||||||
let parts_in_sight_thread = parts_in_sight.clone();
|
let thread = thread::spawn(move || {
|
||||||
let dir_thread = creature.curent_direction.clone();
|
let result: (i32, i32, (Direction, Direction), i64) = recursive_colision_check(matcher, &parts_in_sight_thread,
|
||||||
let sender = sender.clone();
|
&head.position, &dir_thread , SIGHT_RADIUS as u8);
|
||||||
|
sender.send(result).unwrap();
|
||||||
|
});
|
||||||
|
|
||||||
let thread = thread::spawn(move || {
|
let res: (i32, i32, (Direction, Direction), i64) = receiver.recv().unwrap();
|
||||||
let result: (i32, i32, (Direction, Direction), i64) = recursive_colision_check(&head.color, &parts_in_sight_thread,
|
thread.join().unwrap();
|
||||||
&head.position, &dir_thread , SIGHT_RADIUS as u8);
|
|
||||||
sender.send(result).unwrap();
|
|
||||||
});
|
|
||||||
|
|
||||||
let res: (i32, i32, (Direction, Direction), i64) = receiver.recv().unwrap();
|
// Check colisions of new position
|
||||||
thread.join().unwrap();
|
let mut colision = false;
|
||||||
// Check colisions of new position
|
if creature.species == Species::Wesp {
|
||||||
let colision = parts_in_sight.iter().filter(|elem| elem.position == (res.0 as u16, res.1 as u16))
|
for part in parts_in_sight.iter() {
|
||||||
.filter(|elem| elem.element != Element::Food).count() > 0;
|
if part.position == (res.0 as u16, res.1 as u16) && part.element == Element::BodyPartHead{
|
||||||
|
wesp_kills.push(cloned_canvas.alive.iter().position(|x| x.parts[0].position == (res.0 as u16, res.1 as u16)).unwrap());
|
||||||
// Move the head to the new position, spawn a new body part and update the direction
|
} else if part.position == (res.0 as u16, res.1 as u16) && part.element == Element::Food {
|
||||||
creature.move_to((res.0 as u16, res.1 as u16), res.2, colision);
|
colision = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
colision = parts_in_sight.iter().any(|elem| elem.position == (res.0 as u16, res.1 as u16) && elem.element != Element::Food);
|
||||||
|
}
|
||||||
|
// Move the head to the new position, spawn a new body part and update the direction
|
||||||
|
creature.move_to((res.0 as u16, res.1 as u16), res.2, colision);
|
||||||
|
|
||||||
// Clean up food
|
// Clean up food
|
||||||
let mut to_remove: Vec<usize> = Vec::new();
|
let mut to_remove: Vec<usize> = Vec::new();
|
||||||
// Iterate over a mutable reference to the canvas vector
|
// Iterate over a mutable reference to the canvas vector
|
||||||
for (index, element) in canvas.interactable.iter_mut().enumerate() {
|
for (index, element) in canvas.interactable.iter_mut().enumerate() {
|
||||||
if element.position == (res.0 as u16, res.1 as u16) && element.element == Element::Food {
|
if element.position == (res.0 as u16, res.1 as u16) && element.element == Element::Food {
|
||||||
to_remove.push(index);
|
to_remove.push(index);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove elements from the canvas vector based on the condition
|
||||||
|
to_remove.iter().for_each(|x| {canvas.interactable.remove(*x);});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove elements from the canvas vector based on the condition
|
|
||||||
to_remove.iter().for_each(|x| {canvas.interactable.remove(*x);});
|
|
||||||
}
|
}
|
||||||
|
wesp_kills.iter().for_each(|x| {canvas.alive[*x].killed = true;});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -199,9 +249,20 @@ pub fn handle_killed(creatures: &mut Vec<Creature>, cleared_coords: &mut Vec<(u1
|
|||||||
//let killed_creatures: Vec<usize> = creatures.iter().enumerate().filter(|(_, elem)| elem.killed).map(|(index, _)| index).collect::<Vec<_>>();
|
//let killed_creatures: Vec<usize> = creatures.iter().enumerate().filter(|(_, elem)| elem.killed).map(|(index, _)| index).collect::<Vec<_>>();
|
||||||
let mut to_remove: Vec<usize> = Vec::new();
|
let mut to_remove: Vec<usize> = Vec::new();
|
||||||
for (index, creature) in creatures.iter_mut().enumerate().rev() {
|
for (index, creature) in creatures.iter_mut().enumerate().rev() {
|
||||||
//if creature.species == Species::DetachedSnake && creature.parts.len() > 4{
|
|
||||||
// creatures[index].parts.remove(1);
|
|
||||||
//}
|
|
||||||
|
if creature.species == Species::DetachedSnake && creature.parts.len() > 20 {
|
||||||
|
cleared_coords.push(creature.parts[1].position); //0 Is head, 1 is oldes part
|
||||||
|
creature.parts.remove(1);
|
||||||
|
} else if creature.species == Species::Wesp && creature.parts.len() > 5 {
|
||||||
|
cleared_coords.push(creature.parts[1].position); //
|
||||||
|
cleared_coords.push(creature.parts[2].position);
|
||||||
|
creature.parts.remove(1); //0 Is head, 1 is oldes part, tow parts are removed
|
||||||
|
creature.parts.remove(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
if creature.killed {
|
if creature.killed {
|
||||||
for _ in 0..=3 {
|
for _ in 0..=3 {
|
||||||
if creature.parts.len() == 0 {
|
if creature.parts.len() == 0 {
|
||||||
@ -222,7 +283,7 @@ pub fn handle_killed(creatures: &mut Vec<Creature>, cleared_coords: &mut Vec<(u1
|
|||||||
pub fn spawn_food(canvas: &mut CanvasParts) {
|
pub fn spawn_food(canvas: &mut CanvasParts) {
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
|
|
||||||
if rng.gen_bool(0.35){
|
if rng.gen_bool(0.30){
|
||||||
let mut pos: (u16, u16) = (rng.gen_range(1..crate::TERM_SIZE.0 - 1), rng.gen_range(1..crate::TERM_SIZE.1 - 1));
|
let mut pos: (u16, u16) = (rng.gen_range(1..crate::TERM_SIZE.0 - 1), rng.gen_range(1..crate::TERM_SIZE.1 - 1));
|
||||||
while check_collision(&canvas.unify_elements().iter().map(|part| **part).collect(), pos) {
|
while check_collision(&canvas.unify_elements().iter().map(|part| **part).collect(), pos) {
|
||||||
pos = (rng.gen_range(1..crate::TERM_SIZE.0 - 1), rng.gen_range(1..crate::TERM_SIZE.1 - 1));
|
pos = (rng.gen_range(1..crate::TERM_SIZE.0 - 1), rng.gen_range(1..crate::TERM_SIZE.1 - 1));
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user