mirror of
https://github.com/a-b-street/abstreet.git
synced 2024-12-29 17:34:58 +03:00
fix div by 0 bug with percent_dist_crossed for empty paths
This commit is contained in:
parent
0ba3ab92ed
commit
7001139dc0
@ -132,6 +132,14 @@ impl Path {
|
||||
self.total_length
|
||||
}
|
||||
|
||||
pub fn percent_dist_crossed(&self) -> f64 {
|
||||
// Sometimes this happens
|
||||
if self.total_length == Distance::ZERO {
|
||||
return 1.0;
|
||||
}
|
||||
self.crossed_so_far / self.total_length
|
||||
}
|
||||
|
||||
pub fn is_last_step(&self) -> bool {
|
||||
self.steps.len() == 1
|
||||
}
|
||||
|
@ -146,7 +146,7 @@ impl Car {
|
||||
.blocked_since
|
||||
.map(|t| now - t)
|
||||
.unwrap_or(Duration::ZERO),
|
||||
percent_dist_crossed: path.crossed_so_far() / path.total_length(),
|
||||
percent_dist_crossed: path.percent_dist_crossed(),
|
||||
trip_time_so_far: now - self.started_at,
|
||||
}
|
||||
}
|
||||
|
@ -672,7 +672,7 @@ impl DrivingSimState {
|
||||
.blocked_since
|
||||
.map(|t| now - t)
|
||||
.unwrap_or(Duration::ZERO),
|
||||
percent_dist_crossed: path.crossed_so_far() / path.total_length(),
|
||||
percent_dist_crossed: path.percent_dist_crossed(),
|
||||
trip_time_so_far: now - car.started_at,
|
||||
});
|
||||
}
|
||||
|
@ -272,7 +272,7 @@ impl WalkingSimState {
|
||||
vehicle_type: None,
|
||||
pos: ped.get_draw_ped(now, map).pos,
|
||||
time_spent_blocked: ped.blocked_since.map(|t| now - t).unwrap_or(Duration::ZERO),
|
||||
percent_dist_crossed: ped.path.crossed_so_far() / ped.path.total_length(),
|
||||
percent_dist_crossed: ped.path.percent_dist_crossed(),
|
||||
trip_time_so_far: now - ped.started_at,
|
||||
});
|
||||
}
|
||||
@ -509,7 +509,7 @@ impl Pedestrian {
|
||||
.blocked_since
|
||||
.map(|t| now - t)
|
||||
.unwrap_or(Duration::ZERO),
|
||||
percent_dist_crossed: self.path.crossed_so_far() / self.path.total_length(),
|
||||
percent_dist_crossed: self.path.percent_dist_crossed(),
|
||||
trip_time_so_far: now - self.started_at,
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user