@@ -77,7 +77,7 @@ impl Map {
7777 }
7878 pub fn tick ( & mut self ) {
7979 //log("in map.tick");
80- log ( format ! ( "current tick is {}" , self . current_tick) . as_str ( ) ) ;
80+ // log(format!("current tick is {}", self.current_tick).as_str());
8181 let mut new_grazers = Vec :: new ( ) ;
8282 // let mut new_predators = Vec::new();
8383 let mut new_plants = Vec :: new ( ) ;
@@ -151,10 +151,10 @@ impl Map {
151151 150.0 ,
152152 ) ,
153153 self . get_rocks_within_vicinity ( pred. mover . entity . x , pred. mover . entity . y , 150.0 ) ,
154- self . get_visible_grazers_within_vicinity (
154+ & mut self . get_visible_grazers_within_vicinity (
155155 pred. mover . entity . x , pred. mover . entity . y , 150.0 ,
156156 ) ,
157- self . get_grazers_within_vicinity ( pred. mover . entity . x , pred. mover . entity . y , 25.0 ) ,
157+ & mut self . get_grazers_within_vicinity ( pred. mover . entity . x , pred. mover . entity . y , 25.0 ) ,
158158 self . predator_max_offspring ,
159159 self . predator_offspring_energy ,
160160 self . get_predator_gestation ( ) ,
@@ -908,7 +908,7 @@ impl Mover {
908908 result_x = char. entity . x - target. x ;
909909 result_y = char. entity . y - target. y ;
910910
911- log ( "in flee" ) ;
911+ // log("in flee");
912912 ( result_x, result_y) =
913913 Mover :: avoid ( result_x, result_y, char, rocks, delta_time, width, height) ;
914914
@@ -960,12 +960,12 @@ impl Mover {
960960 goal_velocity_x = goal_velocity_x * goalSpeed; // -14
961961 goal_velocity_y = goal_velocity_y * goalSpeed; // -14
962962
963- log ( format ! ( "velocity ({},{})" , goal_velocity_x, goal_velocity_y) . as_str ( ) ) ;
963+ // log(format!("velocity ({},{})", goal_velocity_x, goal_velocity_y).as_str());
964964
965965 result_x = goal_velocity_x;
966966 result_y = goal_velocity_y;
967967
968- log ( "in arrive" ) ;
968+ // log("in arrive");
969969 ( result_x, result_y) =
970970 Mover :: avoid ( result_x, result_y, char, rocks, delta_time, width, height) ;
971971
@@ -1004,7 +1004,7 @@ impl Mover {
10041004 result_orien = num * max_rotation;
10051005
10061006 //log(format!("WANDER result x {}, result y {}, delta time{}", result_x, result_y, delta_time).as_str());
1007- log ( "in wander" ) ;
1007+ // log("in wander");
10081008 ( result_x, result_y) =
10091009 Mover :: avoid ( result_x, result_y, char, rocks, delta_time, width, height) ;
10101010
@@ -1026,9 +1026,9 @@ impl Mover {
10261026 let mut min_dist = 150 as f32 ;
10271027 let mut closest_rock = & Rock :: default ( ) ;
10281028 let mut radius = 0.0 ;
1029- log ( "in avoid" ) ;
1029+ // log("in avoid");
10301030 if !rocks. is_empty ( ) {
1031- log ( "in rocks is not empty in avoid" ) ;
1031+ // log("in rocks is not empty in avoid");
10321032 for rock in rocks. iter ( ) {
10331033 let distance = ( ( rock. entity . x - char. entity . x ) . powi ( 2 )
10341034 + ( rock. entity . y - char. entity . y ) . powi ( 2 ) )
@@ -1264,10 +1264,7 @@ impl Grazer {
12641264 //to integrate sight just change the function called when tick is called in map.
12651265 let mut new_graz = Vec :: new ( ) ;
12661266 let mut ded_plants = Vec :: new ( ) ;
1267- log ( format ! ( "energy {}" , self . mover. energy) . as_str ( ) ) ;
1268- if self . mover . energy > 1000 {
1269- log ( format ! ( "energy {}" , self . mover. energy) . as_str ( ) ) ;
1270- }
1267+
12711268
12721269 if self . mover . energy > 25 && self . ticks_at_speed < maintain_speed {
12731270 self . mover . max_speed = max_speed;
@@ -1286,7 +1283,7 @@ impl Grazer {
12861283
12871284 //first check for predators to run from
12881285 if !predators. is_empty ( ) {
1289- log ( "pred not emp" ) ;
1286+ // log("pred not emp");
12901287 self . ticks_in_loc = 0 ;
12911288 //seek rock away from closest pred
12921289 //set movers target
@@ -1337,20 +1334,20 @@ impl Grazer {
13371334 self . ticks_at_speed += 1 ;
13381335 }
13391336 } else if self . mover . energy >= energy_reproduce {
1340- log ( "reproduce" ) ;
1337+ // log("reproduce");
13411338
13421339 new_graz. push ( self . reproduce ( ) ) ;
13431340 }
13441341 // here means no predators
13451342 // check if at food for plant in 5 du
13461343 // been at plant
13471344 else if !at_plants. is_empty ( ) && self . ticks_in_loc != 0 {
1348- log ( "at plant" ) ;
1345+ // log("at plant");
13491346
13501347 //now check if tick at loc is at max
13511348 self . mover . state = 0 ;
1352- log ( format ! ( "tick in loc {}" , self . ticks_in_loc) . as_str ( ) ) ;
1353- log ( format ! ( "energy {}" , self . mover. energy) . as_str ( ) ) ;
1349+ // log(format!("tick in loc {}", self.ticks_in_loc).as_str());
1350+ // log(format!("energy {}", self.mover.energy).as_str());
13541351 if self . ticks_in_loc == 600 {
13551352 for plant in at_plants. iter ( ) {
13561353 ded_plants. push ( plant. clone ( ) ) ;
@@ -1369,7 +1366,7 @@ impl Grazer {
13691366 }
13701367 // first tick at plant
13711368 else if !at_plants. is_empty ( ) && self . ticks_in_loc == 0 {
1372- log ( "first at plant" ) ;
1369+ // log("first at plant");
13731370
13741371 // just arrived at plant
13751372 self . ticks_in_loc = 0 ;
@@ -1379,7 +1376,7 @@ impl Grazer {
13791376 max_speed, energy_out, self . mover . entity , rocks, width, height,
13801377 ) ;
13811378 } else if at_plants. is_empty ( ) && !plants. is_empty ( ) {
1382- log ( "seek plant" ) ;
1379+ // log("seek plant");
13831380
13841381 //find closest plant and set arrive target
13851382 self . ticks_in_loc = 0 ;
@@ -1401,7 +1398,7 @@ impl Grazer {
14011398 } else {
14021399 self . ticks_in_loc = 0 ;
14031400 //start wandering
1404- log ( "wander" ) ;
1401+ // log("wander");
14051402 self . mover . state = 2 ;
14061403 self . mover . tick (
14071404 max_speed, energy_out, self . mover . entity , rocks, width, height,
@@ -1645,8 +1642,8 @@ impl Predator {
16451642 mates : Vec < Predator > ,
16461643 preds : Vec < Predator > ,
16471644 rocks : Vec < Rock > ,
1648- grazers : Vec < Grazer > , //sight\
1649- graz : Vec < Grazer > , //smell
1645+ grazers : & mut Vec < Grazer > , //sight\
1646+ graz : & mut Vec < Grazer > , //smell
16501647 max_offspring : u32 ,
16511648 offspring_energy : u32 ,
16521649 gestation : u64 ,
@@ -1666,7 +1663,15 @@ impl Predator {
16661663 predators. retain ( |obj| self . get_entity ( ) . get_id ( ) != obj. get_entity ( ) . get_id ( ) ) ;
16671664 predators. retain ( |pred| !self . family . contains ( & pred. get_entity ( ) . get_id ( ) ) ) ;
16681665 predators. retain ( |pred| !pred. family . contains ( & self . get_entity ( ) . get_id ( ) ) ) ;
1669- grazers. append ( graz) ; //Note should remove duplicates
1666+
1667+ grazers. retain ( |obj| {
1668+ !graz
1669+ . iter ( )
1670+ . any ( |r| r. get_entity ( ) . id == obj. get_entity ( ) . id ) //
1671+ } ) ;
1672+ grazers. append ( graz) ; //add smelled grazers to list of seeable grazers
1673+
1674+
16701675 //(|pred| pred.get_entity().get_id() != self.get_entity().get_id());
16711676 // if energy and not pregnant
16721677 // has a mate
@@ -1723,7 +1728,7 @@ impl Predator {
17231728 }
17241729 }
17251730
1726- log ( format ! ( "max speed is {}" , self . mover. max_speed) . as_str ( ) ) ;
1731+ // log(format!("max speed is {}", self.mover.max_speed).as_str());
17271732 if self . is_pregnant {
17281733 if self . get_ticks_til_birth ( ) < cur_tick {
17291734 if let Some ( partner) = partner {
@@ -1738,9 +1743,9 @@ impl Predator {
17381743 }
17391744 }
17401745 }
1741- if self . willing_to_mate ( energy_to_reproduce) && !self . is_pregnant {
1746+ if self . willing_to_mate ( energy_to_reproduce) && !self . is_pregnant && !predators . is_empty ( ) {
17421747 // if vaible candidate is found
1743- log ( "trying to find mate" ) ;
1748+ // log("trying to find mate");
17441749 let pred = mates
17451750 . iter ( )
17461751 . filter ( |p| p. willing_to_mate ( energy_to_reproduce) )
@@ -2215,7 +2220,6 @@ impl Predator {
22152220 //log("wander");
22162221 //not mating and no possible prey around
22172222 self . mover . state = 2 ;
2218-
22192223 self . mover
22202224 . tick ( max_speed, energy, self . mover . entity , rocks, width, height) ;
22212225 }
0 commit comments