Added thread creation limit (wip)

This commit is contained in:
someone 2024-03-09 20:16:51 +01:00
parent 47519bea20
commit c10941b102
4 changed files with 19 additions and 12 deletions

View File

@ -112,12 +112,14 @@ pub(crate) struct Creature {
impl Creature {
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
self.parts[0].position = new_position;
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 {
Element::WespBody
} else {

View File

@ -11,6 +11,7 @@ use std::thread::sleep;
use std::time::Instant;
use std::collections::HashMap;
mod part_handler;
mod components;
mod pathfinder;

View File

@ -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();
for (index, creature) in creatures.iter_mut().enumerate().rev() {
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);
@ -84,7 +82,6 @@ pub fn handle_killed(creatures: &mut Vec<Creature>, cleared_coords: &mut Vec<(u1
creature.parts.remove(1);
}
if creature.killed {
for _ in 0..=3 {
if creature.parts.len() == 0 {

View File

@ -4,6 +4,9 @@ use crate::CanvasParts;
use std::thread;
use std::sync::mpsc;
const MAX_THREADS: u32 = 24;
fn snake_path_match(elem:Element) -> i64 {
match elem {
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),
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
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 {
let sender = sender.clone();
let parts_in_sight = parts_in_sight.clone();
threads.push(thread::spawn(move || {
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;
sender.send((t_val, index)).unwrap();
}));
if ((dyn_pos_res.len() as i32).pow(iterations_passed) as u32) < MAX_THREADS {
threads.push(thread::spawn(move || {
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,
&head.position, &creature.curent_direction , sight_radius as u8);
&head.position, &creature.curent_direction , sight_radius as u8, 0);
// Check colisions of new position
let mut colision = false;