diff --git a/src/human.rs b/src/human.rs index d4edcbb..76b325c 100644 --- a/src/human.rs +++ b/src/human.rs @@ -7,7 +7,7 @@ pub enum State { Dead, Immune, } -#[derive(Clone)] +#[derive(Clone,Copy)] pub struct Human { pub present_state : State, pub x : i32, diff --git a/src/main.rs b/src/main.rs index 77a81b4..b6a54dc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,11 +20,24 @@ fn main() { let disease = Disease::new(20,10,5,String::from("Covid 44")); - let mut population = Population::new(20,10,5,30,30,disease); + let mut population = Population::new(20,10,5,300,300,disease); + //population.change_disease(disease); println!("Before Filling"); - population.display(); + //population.display(); population.generate(); println!("After Filling"); - population.display(); + //population.display(); + println!("After Propagation"); + let mut stats: [i32;4] = [0,0,0,0]; + let mut counter:u32 = 0; + loop{ + counter += 1; + stats=population.propagate(); + //population.display(); + println!("Infecteds: {} Immunes: {} Deads: {}",stats[1],stats[2],stats[3]); + if stats[1] == 0 {break;} + } + println!("Propagation finished in {} steps",counter); + //population.display(); } diff --git a/src/population.rs b/src/population.rs index 5d88b54..00ff42a 100644 --- a/src/population.rs +++ b/src/population.rs @@ -1,13 +1,18 @@ use crate::prelude::*; +pub struct Point{ + x:i32, + y:i32, +} + pub struct Population{ - pub start_infected_ratio:u32, - pub start_immune_ratio:u32, - pub start_dead_ratio:u32, + pub start_infected_ratio:i32, + pub start_immune_ratio:i32, + pub start_dead_ratio:i32, pub humans:Vec, - pub width:u32, - pub height:u32, - pub age:u32, + pub width:i32, + pub height:i32, + pub age:i32, pub plague:Disease, } pub fn human_idx(x: i32, y: i32,width:i32) -> usize { @@ -26,11 +31,11 @@ impl Population{ } } Self{ - start_infected_ratio:start_infected_ratio, - start_immune_ratio:start_immune_ratio, - start_dead_ratio:start_dead_ratio, - width:width, - height:height, + start_infected_ratio:start_infected_ratio as i32, + start_immune_ratio:start_immune_ratio as i32, + start_dead_ratio:start_dead_ratio as i32, + width:width as i32, + height:height as i32, plague:plague, age:0, humans:the_humans, @@ -65,6 +70,134 @@ impl Population{ } } } + pub fn propagate(&mut self)->[i32;4]{ + let mut people_to_check:Vec = Vec::new(); + let mut people_to_infect:Vec = Vec::new(); + let mut people_to_cure:Vec = Vec::new(); + let mut people_to_kill:Vec = Vec::new(); + let mut stats: [i32;4] = [0,0,0,0]; + // stats[0] Normal stats[1] Infected stats[2] Immune stats[3] Dead + + for x in 0..self.width{ + for y in 0..self.height{ + let idx = human_idx(x as i32, y as i32, self.width as i32); + match self.humans[idx].present_state{ + State::Normal => {stats[0] += 1;} + State::Infected => {people_to_check.push(Point{x:x,y:y});stats[1]+=1;} + State::Immune => {stats[2] += 1;} + State::Dead => {stats[3] += 1;} + } + } + } + for pos in &people_to_check { + //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 pos.x > 0 && pos.x < self.width as i32 -1 && pos.y > 0 && pos.y < self.height as i32 -1{ + if self.roll(self.plague.curing_rate){ + //checks if the man dies + people_to_cure.push(Point{x:pos.x,y:pos.y}); + }else{ + if self.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::new(); + //possible_infections.push(Point{x:pos.x,y:pos.y}); + + possible_infections.push(Point{x:pos.x -1,y:pos.y -1}); //Top Left + possible_infections.push(Point{x:pos.x,y:pos.y-1}); //Top + possible_infections.push(Point{x:pos.x +1,y:pos.y -1}); //Top Right + + possible_infections.push(Point{x:pos.x -1,y:pos.y}); //Left + possible_infections.push(Point{x:pos.x +1,y:pos.y}); //Right + + possible_infections.push(Point{x:pos.x -1,y:pos.y + 1}); //Bottom Left + possible_infections.push(Point{x:pos.x,y:pos.y + 1}); //Bottom + possible_infections.push(Point{x:pos.x + 1,y:pos.y + 1}); //Bottom Right + + for poss_infected_pos in &possible_infections{ + //possible_infections.iter().map(|poss_infected_pos|{ + let inf_idx = human_idx(poss_infected_pos.x as i32, poss_infected_pos.y as i32, self.width as i32); + if self.humans[inf_idx].present_state == State::Normal{ + if self.roll(self.plague.infection_rate){ + people_to_infect.push(Point{x:poss_infected_pos.x,y:poss_infected_pos.y}); + } + } + }; + } + } + }else{ + //TODO + //Check every special cases (corners sides etc..) + + //REMOVE WHEN IMPLEMENTED + //It is here to prevent an infected in the borders to keep the programm running as he thinks there are still people to simulate + people_to_kill.push(Point{x:pos.x,y:pos.y}); + + // @## + // ### + // ### + + // #@# + // ### + // ### + + // ##@ + // ### + // ### + + // ### + // @## + // ### + + // ### + // ##@ + // ### + + // ### + // ### + // @## + + // ### + // ### + // #@# + + // ### + // ### + // ##@ + } + for infected_position in &people_to_infect{ + //people_to_infect.iter().map(|infected_position|{ + let infected_index = human_idx(infected_position.x as i32, infected_position.y as i32, self.width as i32); + //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{ + //people_to_cure.iter().map(|cured_position|{ + let cured_index = human_idx(cured_position.x as i32, cured_position.y as i32, self.width as i32); + self.humans[cured_index].present_state = State::Immune; + //DEBUG + //println!("Cured someone"); + }; + for dead_position in &people_to_kill{ + //people_to_kill.iter().map(|dead_position|{ + let dead_index = human_idx(dead_position.x as i32, dead_position.y as i32, self.width as i32); + self.humans[dead_index].present_state = State::Dead; + //DEBUG + //println!("Killed someone"); + }; + }; + stats + } + pub fn roll(&self,probability:u32)->bool{ + let mut rng = rand::thread_rng(); + rng.gen_range(0..CORRECTED_PERCENTAGE) <= probability as i32 + } pub fn display(&mut self){ let sprite = "#"; print!("\n");