Added thread creation limit (wip)
This commit is contained in:
parent
47519bea20
commit
c10941b102
@ -112,12 +112,14 @@ pub(crate) struct Creature {
|
|||||||
|
|
||||||
impl Creature {
|
impl Creature {
|
||||||
pub(crate) fn move_to(&mut self, new_position: (u16, u16), moved_in_direction: (DirectionX, DirectionY), colision: bool) {
|
pub(crate) fn move_to(&mut self, new_position: (u16, u16), moved_in_direction: (DirectionX, DirectionY), colision: bool) {
|
||||||
|
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;
|
||||||
|
return;
|
||||||
|
}
|
||||||
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.0 <= 0 || new_position.1 > TERM_SIZE.1 || new_position.1 <= 0 || colision {
|
|
||||||
self.killed = true;
|
|
||||||
}
|
|
||||||
let part_to_append = if self.species == Species::Wesp {
|
let part_to_append = if self.species == Species::Wesp {
|
||||||
Element::WespBody
|
Element::WespBody
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -11,6 +11,7 @@ use std::thread::sleep;
|
|||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
|
||||||
mod part_handler;
|
mod part_handler;
|
||||||
mod components;
|
mod components;
|
||||||
mod pathfinder;
|
mod pathfinder;
|
||||||
|
|||||||
@ -72,8 +72,6 @@ pub fn handle_killed(creatures: &mut Vec<Creature>, cleared_coords: &mut Vec<(u1
|
|||||||
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() > 20 {
|
if creature.species == Species::DetachedSnake && creature.parts.len() > 20 {
|
||||||
cleared_coords.push(creature.parts[1].position); //0 Is head, 1 is oldes part
|
cleared_coords.push(creature.parts[1].position); //0 Is head, 1 is oldes part
|
||||||
creature.parts.remove(1);
|
creature.parts.remove(1);
|
||||||
@ -84,7 +82,6 @@ pub fn handle_killed(creatures: &mut Vec<Creature>, cleared_coords: &mut Vec<(u1
|
|||||||
creature.parts.remove(1);
|
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 {
|
||||||
|
|||||||
@ -4,6 +4,9 @@ use crate::CanvasParts;
|
|||||||
use std::thread;
|
use std::thread;
|
||||||
use std::sync::mpsc;
|
use std::sync::mpsc;
|
||||||
|
|
||||||
|
const MAX_THREADS: u32 = 24;
|
||||||
|
|
||||||
|
|
||||||
fn snake_path_match(elem:Element) -> i64 {
|
fn snake_path_match(elem:Element) -> i64 {
|
||||||
match elem {
|
match elem {
|
||||||
Element::Wall | Element::Spawn => -100,
|
Element::Wall | Element::Spawn => -100,
|
||||||
@ -23,7 +26,7 @@ fn wesp_path_match(elem:Element) -> i64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn recursive_colision_check(path_to_match: fn(Element) -> i64, parts_in_sight: &Vec<Part>, fov: isize, position: &(u16, u16),
|
fn recursive_colision_check(path_to_match: fn(Element) -> i64, parts_in_sight: &Vec<Part>, fov: isize, position: &(u16, u16),
|
||||||
direction: &(DirectionX, DirectionY), iterations_left: u8) -> (i32, i32, (DirectionX, DirectionY), i64){
|
direction: &(DirectionX, DirectionY), iterations_left: u8, iterations_passed: u32) -> (i32, i32, (DirectionX, DirectionY), 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, (DirectionX, DirectionY), i64)> = Vec::new();
|
let mut dyn_pos_res: Vec<(i32, i32, (DirectionX, DirectionY), i64)> = Vec::new();
|
||||||
|
|
||||||
@ -51,10 +54,14 @@ fn recursive_colision_check(path_to_match: fn(Element) -> i64, parts_in_sight: &
|
|||||||
if iterations_left > 0 {
|
if iterations_left > 0 {
|
||||||
let sender = sender.clone();
|
let sender = sender.clone();
|
||||||
let parts_in_sight = parts_in_sight.clone();
|
let parts_in_sight = parts_in_sight.clone();
|
||||||
threads.push(thread::spawn(move || {
|
if ((dyn_pos_res.len() as i32).pow(iterations_passed) as u32) < MAX_THREADS {
|
||||||
let t_val = recursive_colision_check(path_to_match, &parts_in_sight, fov, &(x as u16, y as u16), &v_direction, iterations_left - 1).3;
|
threads.push(thread::spawn(move || {
|
||||||
sender.send((t_val, index)).unwrap();
|
let t_val = recursive_colision_check(path_to_match, &parts_in_sight, fov, &(x as u16, y as u16), &v_direction, iterations_left - 1, iterations_passed +1).3;
|
||||||
}));
|
sender.send((t_val, index)).unwrap();
|
||||||
|
}));
|
||||||
|
} else {
|
||||||
|
dyn_pos_res[index].3 += recursive_colision_check(path_to_match, &parts_in_sight, fov, &(x as u16, y as u16), &v_direction, iterations_left - 1, iterations_passed +1).3;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,7 +115,7 @@ pub fn head_handle(canvas: &mut CanvasParts) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let res = recursive_colision_check(matcher, &parts_in_sight, fov,
|
let res = recursive_colision_check(matcher, &parts_in_sight, fov,
|
||||||
&head.position, &creature.curent_direction , sight_radius as u8);
|
&head.position, &creature.curent_direction , sight_radius as u8, 0);
|
||||||
|
|
||||||
// Check colisions of new position
|
// Check colisions of new position
|
||||||
let mut colision = false;
|
let mut colision = false;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user