check surroundings instead

This commit is contained in:
2022-05-03 23:01:48 +02:00
parent 5affe83fdc
commit 89020eeb21
+46 -44
View File
@@ -85,9 +85,16 @@ impl Population {
} }
} }
fn push_if_inside(&self, point_list: &mut Vec<Point>, point: Point) { fn is_inside_and_infected(&self, point: Point) -> bool {
if self.is_inside(&point) { if self.is_inside(&point) {
point_list.push(point); let idx = human_idx(point.x, point.y, self.width);
if self.humans[idx].present_state == State::Infected {
roll(self.plague.infection_rate)
} else {
false
}
} else {
false
} }
} }
@@ -133,83 +140,78 @@ impl Population {
if roll(self.plague.death_rate) { if roll(self.plague.death_rate) {
//cheks if the man dies //cheks if the man dies
people_to_kill.push(Point { x: pos.x, y: pos.y }); people_to_kill.push(Point { x: pos.x, y: pos.y });
} else { }
let mut possible_infections: Vec<Point> = Vec::with_capacity(8); }
// Vec::new(); }
//possible_infections.push(Point{x:pos.x,y:pos.y}); for pos in self.humans.iter() {
if pos.present_state == State::Normal {
self.push_if_inside( let infected: bool = self.is_inside_and_infected(
&mut possible_infections,
Point { Point {
x: pos.x - 1, x: pos.x - 1,
y: pos.y - 1, y: pos.y - 1,
}, },
); //Top Left ) || //Top Left
self.push_if_inside( self.is_inside_and_infected(
&mut possible_infections,
Point { Point {
x: pos.x, x: pos.x,
y: pos.y - 1, y: pos.y - 1,
}, },
); //Top ) || //Top
self.push_if_inside( self.is_inside_and_infected(
&mut possible_infections,
Point { Point {
x: pos.x + 1, x: pos.x + 1,
y: pos.y - 1, y: pos.y - 1,
}, },
); //Top Right ) || //Top Right
self.push_if_inside( self.is_inside_and_infected(
&mut possible_infections,
Point { Point {
x: pos.x - 1, x: pos.x - 1,
y: pos.y, y: pos.y,
}, },
); //Left ) || //Left
self.push_if_inside( self.is_inside_and_infected(
&mut possible_infections,
Point { Point {
x: pos.x + 1, x: pos.x + 1,
y: pos.y, y: pos.y,
}, },
); //Right ) || //Right
self.push_if_inside( self.is_inside_and_infected(
&mut possible_infections,
Point { Point {
x: pos.x - 1, x: pos.x - 1,
y: pos.y + 1, y: pos.y + 1,
}, },
); //Bottom Left ) || //Bottom Left
self.push_if_inside( self.is_inside_and_infected(
&mut possible_infections,
Point { Point {
x: pos.x, x: pos.x,
y: pos.y + 1, y: pos.y + 1,
}, },
); //Bottom ) || //Bottom
self.push_if_inside( self.is_inside_and_infected(
&mut possible_infections,
Point { Point {
x: pos.x + 1, x: pos.x + 1,
y: pos.y + 1, y: pos.y + 1,
}, },
); //Bottom Right );
for poss_infected_pos in possible_infections.iter() { if infected {
//possible_infections.iter().map(|poss_infected_pos|{ people_to_infect.push(Point { x: pos.x, y: pos.y });
let inf_idx =
human_idx(poss_infected_pos.x, poss_infected_pos.y, self.width);
if self.humans[inf_idx].present_state == State::Normal {
if roll(self.plague.infection_rate) {
people_to_infect.push(Point {
x: poss_infected_pos.x,
y: poss_infected_pos.y,
});
}
}
}
} }
} }
} }
// ); //Bottom Right
// for poss_infected_pos in possible_infections.iter() {
// //possible_infections.iter().map(|poss_infected_pos|{
// let inf_idx =
// human_idx(poss_infected_pos.x, poss_infected_pos.y, self.width);
// if self.humans[inf_idx].present_state == State::Normal {
// if roll(self.plague.infection_rate) {
// people_to_infect.push(Point {
// x: poss_infected_pos.x,
// y: poss_infected_pos.y,
// });
// }
// }
// }
for infected_position in people_to_infect.iter() { for infected_position in people_to_infect.iter() {
// println!("To infect: {:?}", infected_position); // println!("To infect: {:?}", infected_position);