diff --git a/src/population.rs b/src/population.rs index eb315cf..1426eae 100644 --- a/src/population.rs +++ b/src/population.rs @@ -85,20 +85,27 @@ impl Population { } } - fn push_if_inside(&self, point_list: &mut Vec, point: Point) { + fn is_inside_and_infected(&self, point: Point) -> bool { 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 } } pub fn propagate(&mut self) -> [i32; 4] { - let mut people_to_check: Vec = + let mut people_to_check: Vec = Vec::with_capacity((self.width * self.height) as usize); - let mut people_to_infect: Vec = + let mut people_to_infect: Vec = Vec::with_capacity((self.width * self.height) as usize); - let mut people_to_cure: Vec = + let mut people_to_cure: Vec = Vec::with_capacity((self.width * self.height) as usize); - let mut people_to_kill: Vec = + let mut people_to_kill: Vec = Vec::with_capacity((self.width * self.height) as usize); let mut stats: [i32; 4] = [0, 0, 0, 0]; // stats[0] Normal stats[1] Infected stats[2] Immune stats[3] Dead @@ -133,83 +140,78 @@ impl Population { if roll(self.plague.death_rate) { //cheks if the man dies people_to_kill.push(Point { x: pos.x, y: pos.y }); - } else { - let mut possible_infections: Vec = Vec::with_capacity(8); - // Vec::new(); - //possible_infections.push(Point{x:pos.x,y:pos.y}); - - self.push_if_inside( - &mut possible_infections, - Point { - x: pos.x - 1, - y: pos.y - 1, - }, - ); //Top Left - self.push_if_inside( - &mut possible_infections, - Point { - x: pos.x, - y: pos.y - 1, - }, - ); //Top - self.push_if_inside( - &mut possible_infections, - Point { - x: pos.x + 1, - y: pos.y - 1, - }, - ); //Top Right - self.push_if_inside( - &mut possible_infections, - Point { - x: pos.x - 1, - y: pos.y, - }, - ); //Left - self.push_if_inside( - &mut possible_infections, - Point { - x: pos.x + 1, - y: pos.y, - }, - ); //Right - self.push_if_inside( - &mut possible_infections, - Point { - x: pos.x - 1, - y: pos.y + 1, - }, - ); //Bottom Left - self.push_if_inside( - &mut possible_infections, - Point { - x: pos.x, - y: pos.y + 1, - }, - ); //Bottom - self.push_if_inside( - &mut possible_infections, - Point { - x: pos.x + 1, - y: pos.y + 1, - }, - ); //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 pos in self.humans.iter() { + if pos.present_state == State::Normal { + let infected: bool = self.is_inside_and_infected( + Point { + x: pos.x - 1, + y: pos.y - 1, + }, + ) || //Top Left + self.is_inside_and_infected( + Point { + x: pos.x, + y: pos.y - 1, + }, + ) || //Top + self.is_inside_and_infected( + Point { + x: pos.x + 1, + y: pos.y - 1, + }, + ) || //Top Right + self.is_inside_and_infected( + Point { + x: pos.x - 1, + y: pos.y, + }, + ) || //Left + self.is_inside_and_infected( + Point { + x: pos.x + 1, + y: pos.y, + }, + ) || //Right + self.is_inside_and_infected( + Point { + x: pos.x - 1, + y: pos.y + 1, + }, + ) || //Bottom Left + self.is_inside_and_infected( + Point { + x: pos.x, + y: pos.y + 1, + }, + ) || //Bottom + self.is_inside_and_infected( + Point { + x: pos.x + 1, + y: pos.y + 1, + }, + ); + if infected { + people_to_infect.push(Point { x: pos.x, y: 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() { // println!("To infect: {:?}", infected_position);