theding experiment + fixed recursive_colision_check not using SIGHT_RADIUS

This commit is contained in:
someone 2024-03-06 21:47:33 +01:00
parent 6e67163855
commit 3cc0e1bd10
2 changed files with 30 additions and 12 deletions

View File

@ -103,7 +103,7 @@ fn main() {
draw_canvas(&canvas); draw_canvas(&canvas);
state.iterations += 1; state.iterations += 1;
sleep(Duration::from_millis(16)); sleep(Duration::from_millis(32));
} }
} }

View File

@ -93,7 +93,8 @@ pub fn spawner_handle(canvas: &mut CanvasParts) {
fn recursive_colision_check(color: &Color, parts_in_sight: &Vec<Part>, position: &(u16, u16), 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){ 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();
// Create a vector of tuples with the possible positions and their values(liklihood of beeing chosen) // Create a vector of tuples with the possible positions and their values(liklihood of beeing chosen)
for direction_to_check in CHECK_DIRECTIONS[&direction].0.iter() { for direction_to_check in CHECK_DIRECTIONS[&direction].0.iter() {
@ -101,27 +102,43 @@ fn recursive_colision_check(color: &Color, parts_in_sight: &Vec<Part>, position:
dyn_pos_res.push((i32::from(position.0) + DIRECTIONS[&dir_x], i32::from(position.1) + DIRECTIONS[&dir_y], (dir_x, dir_y), 0)); dyn_pos_res.push((i32::from(position.0) + DIRECTIONS[&dir_x], i32::from(position.1) + DIRECTIONS[&dir_y], (dir_x, dir_y), 0));
} }
let (sender, receiver) = mpsc::channel();
let mut threads: Vec<thread::JoinHandle<()>> = Vec::new();
// Iterate over the vector and check if the position is in sight, what value the sight has // Iterate over the vector and check if the position is in sight, what value the sight has
for index in 0..dyn_pos_res.len() { for index in 0..dyn_pos_res.len() {
let (x, y, v_direction, val): &mut (i32, i32, (Direction, Direction), i64) = &mut dyn_pos_res[index]; let (x, y, v_direction, mut val): (i32, i32, (Direction, Direction), i64) = dyn_pos_res[index].clone();
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 = match part.element {
Element::Wall => -1000, Element::Wall | Element::Spawn => -100,
Element::BodyPartHori | Element::BodyPartVert | Element::BodyPartHori | Element::BodyPartVert |
Element::BodyPartLeftLean | Element::BodyPartRightLean | Element::BodyPartHead => *val -1000, Element::BodyPartLeftLean | Element::BodyPartRightLean | Element::BodyPartHead => val -100,
Element::Food => 100, Element::Food => 10,
_ => 0, _ => 0,
} }
} }
} }
dyn_pos_res[index].3 = val;
if iterations_left > 0 { if iterations_left > 0 {
*val += recursive_colision_check(color, &parts_in_sight, &(*x as u16, *y as u16), &v_direction, iterations_left - 1).3; let sender = sender.clone();
let color = color.clone();
let parts_in_sight = parts_in_sight.clone();
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;
sender.send((t_val, index)).unwrap();
}));
} }
//dyn_pos_res[index].3 = *val;
} }
for _ in 0..(threads.len()) {
let thread_res = receiver.recv().unwrap();
dyn_pos_res[thread_res.1].3 += thread_res.0;
}
dyn_pos_res.sort_by(|a, b| b.3.cmp(&a.3)); dyn_pos_res.sort_by(|a, b| b.3.cmp(&a.3));
dyn_pos_res[0] dyn_pos_res[0]
/*In this example, the sort_by method is used to sort the vector data. /*In this example, the sort_by method is used to sort the vector data.
@ -130,6 +147,7 @@ fn recursive_colision_check(color: &Color, parts_in_sight: &Vec<Part>, position:
} }
pub fn head_handle(canvas: &mut CanvasParts) { pub fn head_handle(canvas: &mut CanvasParts) {
//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();
@ -156,7 +174,7 @@ pub fn head_handle(canvas: &mut CanvasParts) {
let thread = thread::spawn(move || { let thread = thread::spawn(move || {
let result: (i32, i32, (Direction, Direction), i64) = recursive_colision_check(&head.color, &parts_in_sight_thread, let result: (i32, i32, (Direction, Direction), i64) = recursive_colision_check(&head.color, &parts_in_sight_thread,
&head.position, &dir_thread , 3); &head.position, &dir_thread , SIGHT_RADIUS as u8);
sender.send(result).unwrap(); sender.send(result).unwrap();
}); });