remove dead code

This commit is contained in:
2022-05-05 15:25:07 +02:00
parent 94466c7f1b
commit 66bded5763

View File

@@ -1,11 +1,5 @@
use crate::prelude::*;
#[derive(Debug)]
pub struct Point {
x: i32,
y: i32,
}
pub struct Population {
pub start_infected_ratio: i32,
pub start_immune_ratio: i32,
@@ -75,190 +69,6 @@ impl Population {
size: size,
}
}
// pub fn change_disease(&mut self, plague:Disease){
// self.plague = plague;
// }
fn is_inside(&self, pos: &Point) -> bool {
if pos.x >= 0 && pos.x < self.width && pos.y >= 0 && pos.y < self.height {
true
} else {
false
}
}
fn is_inside_and_infected(&self, point: Point) -> bool {
if self.is_inside(&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<Point> =
Vec::with_capacity(self.size);
let mut possible_infected: Vec<Point> =
Vec::with_capacity(self.size);
let mut people_to_infect: Vec<Point> =
Vec::with_capacity(self.size);
let mut people_to_cure: Vec<Point> =
Vec::with_capacity(self.size);
let mut people_to_kill: Vec<Point> =
Vec::with_capacity(self.size);
let mut stats: [i32; 4] = [0, 0, 0, 0];
// stats[0] Normal stats[1] Infected stats[2] Immune stats[3] Dead
for h in self.humans.iter() {
match h.present_state {
State::Normal => {
possible_infected.push(Point{ x: h.x, y: h.y});
stats[0] += 1;
}
State::Infected => {
people_to_check.push(Point { x: h.x, y: h.y });
stats[1] += 1;
}
State::Immune => {
stats[2] += 1;
}
State::Dead => {
stats[3] += 1;
}
}
}
// for pos in &people_to_check {
for pos in people_to_check.iter() {
//people_to_check.iter().map(|pos|{
//get all the other people next to me and check if i die cure or infect
//now we can start to check if people would be infected or not
//let idx = human_idx(pos.x as i32, pos.y as i32, self.width as i32);
if roll(self.plague.curing_rate) {
//checks if the man recovers
people_to_cure.push(Point { x: pos.x, y: pos.y });
} else {
if roll(self.plague.death_rate) {
//cheks if the man dies
people_to_kill.push(Point { x: pos.x, y: pos.y });
}
}
}
for pos in possible_infected.iter() {
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,
},
); //Bottom Right
if infected {
people_to_infect.push(Point { x: pos.x, y: pos.y });
}
}
for infected_position in people_to_infect.iter() {
// println!("To infect: {:?}", infected_position);
//people_to_infect.iter().map(|infected_position|{
let infected_index = human_idx(infected_position.x, infected_position.y, self.width);
// let _ = infected_position.x;
//DEBUG
//println!("x: {} y: {} index: {}",infected_position.x,infected_position.y,infected_index);
self.humans[infected_index].present_state = State::Infected;
//DEBUG
//println!("Infected someone");
}
for cured_position in people_to_cure.iter() {
//people_to_cure.iter().map(|cured_position|{
let cured_index = human_idx(cured_position.x, cured_position.y, self.width);
if self.humans[cured_index].present_state != State::Infected {
println!("not infected");
}
self.humans[cured_index].present_state = State::Immune;
//DEBUG
//println!("Cured someone");
}
for dead_position in people_to_kill.iter() {
//people_to_kill.iter().map(|dead_position|{
let dead_index = human_idx(dead_position.x, dead_position.y, self.width);
if self.humans[dead_index].present_state == State::Dead {
// println!("Already dead");
} else {
self.humans[dead_index].present_state = State::Dead;
}
//DEBUG
}
assert_eq!(
stats[0] + stats[1] + stats[2] + stats[3],
self.size as i32
);
stats
}
// pub fn display(&mut self){
// let sprite = "#";
// print!("\n");
// for x in 0..self.width{
// for y in 0..self.height{
// let index = human_idx(x as i32,y as i32,self.width as i32);
// match self.humans[index].present_state {
// State::Normal => print!("{}",style(sprite).green()),
// State::Dead => print!("{}",style(sprite).black()),
// State::Infected => print!("{}",style(sprite).red()),
// State::Immune => print!("{}",style(sprite).blue()),
// _ => print!("{}",style(sprite).white()),
// }
// }
// print!("\n");
// }
// }
pub fn propagate_new(&mut self) -> [i32; 4] {
let mut stats: [i32; 4] = [0, 0, 0, 0];