games: walked distance survives a reload instead of resetting to zero
- Commit
2f06f82924a9619c123cc0fe8a7db1ffadfffbcf- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 11:58 UTC (5 days ago)
- Parents
15a67f8
player_save wrote StatsSave.walked on every save and the load path restored the neighbouring stats (placed, broken, night_seen) — but never read stats.walked back, and Player::new zeroes walk_dist. Every restart reset the lifetime walked stat (published to achievements/HUD each frame) to 0: saved 1234, reloaded 0. new_state now restores walk_dist from the save (corrupt negatives clamp to 0) and initialises prev_walk to the restored total, so the ambience step counter does not treat the whole backlog as one giant first step. Regression test saves with walk_dist 1234.7, asserts 1234 on disk, reloads and asserts both fields (failed pre-fix: 0 vs 1234). Chunk lifecycle, negative coordinates, raycast placement and inventory were audited and cleared. Verified: cargo test -p voxels_engine 257 tests; :games:voxels: testDevUnitTest 221 tests and lint BUILD SUCCESSFUL.
Diff
games/voxels/src/main/rust/src/engine.rs
diff --git a/games/voxels/src/main/rust/src/engine.rs b/games/voxels/src/main/rust/src/engine.rsindex 95a40b556b..f190671d07 100644--- a/games/voxels/src/main/rust/src/engine.rs+++ b/games/voxels/src/main/rust/src/engine.rs@@ -207,9 +207,13 @@ pub fn new_state(files_dir: String, seed: u32, render_distance: i32) -> EngineSt (0.0, 80.0, 0.0, 0.0, 0.0, inv) }; + // The lifetime walk total is saved with the other stats; give it back or every+ // restart zeroes it. A corrupt negative total reads as never having walked.+ let walked = player_save.as_ref().map(|ps| ps.stats.walked.max(0) as f32).unwrap_or(0.0); let mut player = Player::new(px, py, pz); player.yaw = yaw; player.pitch = pitch;+ player.walk_dist = walked; if had_save { player.max_health = progress.max_health.clamp(crate::player::MIN_MAX_HEALTH, crate::player::CAP_MAX_HEALTH); player.health = player.max_health;@@ -294,7 +298,9 @@ pub fn new_state(files_dir: String, seed: u32, render_distance: i32) -> EngineSt // Resume mid-downpour rather than fading in from a clear sky. rain: rain_target(progress.weather), ambience: crate::ambience::Ambience::default(),- prev_walk: 0.0,+ // Starts at the restored total: the ambience tick diffs against this, so+ // starting at zero would count the whole restored backlog as fresh walking.+ prev_walk: walked, fishing: crate::fishing::Fishing::default(), farm_cd: 0.0, crafted_recipes,
games/voxels/src/main/rust/src/engine_part6.rs
diff --git a/games/voxels/src/main/rust/src/engine_part6.rs b/games/voxels/src/main/rust/src/engine_part6.rsindex 82a4ffeeef..7a9df7bce0 100644--- a/games/voxels/src/main/rust/src/engine_part6.rs+++ b/games/voxels/src/main/rust/src/engine_part6.rs@@ -71,6 +71,24 @@ mod state_tests { assert_eq!(fresh.get_block_world(2, 100, 2), 9); }) } + // The walked distance is written to player.json on every save, so a reload must give it+ // back: resetting it would zero the lifetime stat (and the achievements that read it)+ // on every restart, no matter how far the player had walked before quitting.+ #[test]+ fn walked_distance_survives_a_reload() { big_stack(|| {+ let mut s = small_state("walked");+ s.player.walk_dist = 1234.7;+ let dir = s.save_dir.clone();+ save_world(&mut s);+ let saved = crate::world::save::load_player(&dir).expect("save wrote player.json");+ assert_eq!(saved.stats.walked, 1234, "the walk total must reach disk");+ let reloaded = new_state(dir, 1234, 2);+ assert_eq!(reloaded.player.walk_dist as i32, 1234, "the walk total was dropped on load");+ // The ambience tracker diffs against the previous frame's total, so it must start+ // from the restored value or the whole backlog counts as one giant first step.+ assert_eq!(reloaded.prev_walk, reloaded.player.walk_dist);+ }) }+ // The pause menu freezes a single-player world: nothing moves and the autosave clock stops, // while unpausing resumes exactly where it left off. #[test]