Modern Apps git

← Modern-Apps.git

kart: rank a wrong-way wrap behind the start, not 97% round

Commit
a8b635926f9933f00ab901429c7951f2b68f156c
Author
Vayun Mathur <[email protected]>
When
2026-12-02 01:25 UTC (4 days ago)
Parents
a53bca8
Symptom: driving the wrong way across the start/finish line put
the kart first in the standings at about 97% lap progress, ahead
of rivals legitimately halfway round. It also headed the winner
order and the leader progress the AI rubber-band reads.

Root cause: progress_key computed lap + track_s / TRACK_LENGTH
from the cyclic arc position. Physics correctly refuses the lap
for a wrong-way crossing (no halfway credit), but the wrapped
track_s near the lap end still read as near-completion.

Fix: derive the lap fraction with the halfway credit. Until it
is earned going forward, a position in the second half can only
have been reached backwards over the line, so it counts as that
far behind the start; standings show the same fraction clamped
to zero. Two existing fixtures that hand-placed a kart on the
halfway point now set passed_half, as physics does on crossing.

Verified: new race test failed before the fix (standings showed
0.974 in first place) and passes after; cargo test (130 passed),
clippy and fmt --check pass, and :games:kart:testDevUnitTest,
:games:kart:lint and :games:kart:lintVitalRelease all pass.

Diff

games/kart/src/main/rust/src/race.rs
diff --git a/games/kart/src/main/rust/src/race.rs b/games/kart/src/main/rust/src/race.rsindex a86b043d5d..fab19e52bd 100644--- a/games/kart/src/main/rust/src/race.rs+++ b/games/kart/src/main/rust/src/race.rs@@ -242,7 +242,7 @@ impl Race {             .iter()             .map(|s| {                 let lap = s.kart.lap.min(RACE_LAPS);-                let frac = (s.kart.track_s / track::TRACK_LENGTH).clamp(0.0, 1.0);+                let frac = lap_fraction(&s.kart).clamp(0.0, 1.0);                 (s.id.clone(), lap, frac, progress_key(&s.kart))             })             .collect();@@ -295,9 +295,27 @@ impl Race {     } } +/// Where in its current lap a kart sits, as a fraction (may be negative).+///+/// `track_s` is cyclic: a kart that crossed the start line the wrong way+/// wraps to the end of the lap with its lap count unchanged and the+/// halfway credit cleared. Reading that raw position as ~97% of a lap+/// promotes it ahead of karts legitimately halfway round. Until the+/// halfway point is passed going forward, a position in the second+/// half can only have been reached backwards over the line, so it+/// counts as that far *behind* the start instead.+fn lap_fraction(k: &KartBody) -> f32 {+    let frac = k.track_s / track::TRACK_LENGTH;+    if !k.passed_half && frac >= 0.5 {+        frac - 1.0+    } else {+        frac+    }+}+ /// Total progress in laps (fractional) for ordering. fn progress_key(k: &KartBody) -> f32 {-    k.lap as f32 + k.track_s / track::TRACK_LENGTH+    k.lap as f32 + lap_fraction(k) }  /// Grid lane stagger: alternate ±3 m so karts don't spawn stacked.@@ -358,6 +376,32 @@ mod tests {         assert_eq!(order[0].0, race.seats[0].id);     } +    #[test]+    fn standings_put_a_wrong_way_wrap_behind_the_start() {+        // A kart that crossed the line the wrong way wraps to the end of+        // the lap with no lap and no halfway credit: it is a few metres+        // *behind* the start, not 97% through its first lap, so a rival+        // legitimately halfway round must rank ahead of it.+        let mut race = Race::new(&["p1".to_string()]);+        assert!(race.seat_count() >= 2, "needs a rival to order");+        race.seats[0].kart.lap = 0;+        race.seats[0].kart.track_s = track::TRACK_LENGTH - 20.0;+        race.seats[0].kart.passed_half = false;+        race.seats[1].kart.lap = 0;+        race.seats[1].kart.track_s = track::TRACK_LENGTH * 0.5;+        race.seats[1].kart.passed_half = true;+        let order = race.standings();+        assert_eq!(+            order[0].0, race.seats[1].id,+            "wrong-way kart must rank behind the mid-lap rival: {order:?}"+        );+        assert!(+            order[1].2 < 0.05,+            "wrong-way kart should show ~no lap progress, got {}",+            order[1].2+        );+    }+     #[test]     fn winner_needs_finish_flag() {         let mut race = Race::new(&["p1".to_string()]);
games/kart/src/main/rust/src/race/race_tests.rs
diff --git a/games/kart/src/main/rust/src/race/race_tests.rs b/games/kart/src/main/rust/src/race/race_tests.rsindex c2098dba56..c46bdd911b 100644--- a/games/kart/src/main/rust/src/race/race_tests.rs+++ b/games/kart/src/main/rust/src/race/race_tests.rs@@ -18,6 +18,9 @@ fn grid_lane_alternates_and_progress_key_orders() {     let mut k = KartBody::spawn(0.0, 0.0);     k.lap = 2;     k.track_s = track::TRACK_LENGTH / 2.0;+    // A kart sitting on the halfway point arrived there going forward:+    // physics sets the credit as it crosses, so the fixture must too.+    k.passed_half = true;     assert!((progress_key(&k) - 2.5).abs() < 1e-5);     k.lap = 0;     k.track_s = 0.0;@@ -126,6 +129,7 @@ fn standings_rank_by_total_progress_beyond_race_laps() {     race.seats[0].kart.track_s = 10.0;     race.seats[1].kart.lap = RACE_LAPS;     race.seats[1].kart.track_s = track::TRACK_LENGTH * 0.5;+    race.seats[1].kart.passed_half = true;     let st = race.standings();     assert_eq!(st[0].0, "a", "total progress must rank first: {st:?}");     assert_eq!(st[1].0, "b");