From c10941b102eb0e6adbe7c6d6cfec0d6eaff6f919 Mon Sep 17 00:00:00 2001 From: someone Date: Sat, 9 Mar 2024 20:16:51 +0100 Subject: [PATCH] Added thread creation limit (wip) --- src/components.rs | 8 +++++--- src/main.rs | 1 + src/part_handler.rs | 3 --- src/pathfinder.rs | 19 +++++++++++++------ 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/components.rs b/src/components.rs index 627673d..ccde0ed 100644 --- a/src/components.rs +++ b/src/components.rs @@ -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 { diff --git a/src/main.rs b/src/main.rs index 46edd8d..138803e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,7 @@ use std::thread::sleep; use std::time::Instant; use std::collections::HashMap; + mod part_handler; mod components; mod pathfinder; diff --git a/src/part_handler.rs b/src/part_handler.rs index 7e7ece7..213ed15 100644 --- a/src/part_handler.rs +++ b/src/part_handler.rs @@ -72,8 +72,6 @@ pub fn handle_killed(creatures: &mut Vec, cleared_coords: &mut Vec<(u1 let mut to_remove: Vec = 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, cleared_coords: &mut Vec<(u1 creature.parts.remove(1); } - if creature.killed { for _ in 0..=3 { if creature.parts.len() == 0 { diff --git a/src/pathfinder.rs b/src/pathfinder.rs index b72fc76..c373692 100644 --- a/src/pathfinder.rs +++ b/src/pathfinder.rs @@ -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, 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;