refactor: split magic_numbers into per-module files

Extracted all doc-commented magic numbers into module-local magic_numbers.rs
files, co-located with the module that owns them. No logic changes.

- entities/shared_systems/magic_numbers: pathfinding + dig constants
- world/generation/terrain/magic_numbers: terrain + cave constants
- world/generation/forestry/magic_numbers: tree constants
- world/tiles/magic_numbers: A*, benchmark, memory constants
- entities/item/magic_numbers: item sprite z-offset
- Removed src/magic_numbers.rs (central file deleted)

Constants imported as `crate::{module}::magic_numbers::*` from each
consumer. src/constants.rs retains only structural constants (PIXEL_RATIO,
TILE_SIZE, SEED, pathfinding tier thresholds).
This commit is contained in:
2026-03-21 17:31:39 +00:00
parent 776d3caefc
commit 7fe33d4b2d
19 changed files with 159 additions and 45 deletions
+6 -3
View File
@@ -17,6 +17,7 @@ use bevy::prelude::*;
use crate::constants::ITILE_SIZE;
use crate::world::chunks::{CHUNK_SIZE, Z_ABOVE, Z_BELOW};
use crate::world::tiles::magic_numbers::ASTAR_DEFAULT_WEIGHT;
/// Number of z-levels in a chunk (Z_BELOW + Z_ABOVE + 1 for inclusive range).
/// Terrain generation uses -Z_BELOW..=Z_ABOVE (inclusive at both ends).
@@ -216,10 +217,10 @@ impl ChunkData {
#[inline]
pub fn get_astar_weight(&self, local_x: i32, local_y: i32, z: i32) -> u8 {
if local_x < 0 || local_x >= CHUNK_SIZE || local_y < 0 || local_y >= CHUNK_SIZE {
return 100;
return ASTAR_DEFAULT_WEIGHT;
}
if z < -(Z_BELOW as i32) || z > (Z_ABOVE as i32) {
return 100;
return ASTAR_DEFAULT_WEIGHT;
}
let idx = Self::pos_to_index(local_x, local_y, z);
self.astar_weights[idx]
@@ -273,7 +274,9 @@ impl ChunkData {
self.stand_in_fixture.fill(0);
self.stand_on_fixture.fill(0);
self.tile_ids.fill(0);
self.astar_weights.fill(100);
// MAGIC ASTAR_DEFAULT_WEIGHT matches normal grass/traversable tile weight — cleared
// chunks present as passable rather than impassable walls to pathfinding.
self.astar_weights.fill(ASTAR_DEFAULT_WEIGHT);
}
}
+8
View File
@@ -0,0 +1,8 @@
// Tiles
/// Default A* weight returned for out-of-bounds or unloaded positions.
/// Matches normal traversable tile weight so pathfinding degrades gracefully.
pub const ASTAR_DEFAULT_WEIGHT: u8 = 100;
/// Rolling history length for z-change frame timing in benchmark reporting.
pub const Z_CHANGE_HISTORY_LEN: usize = 100;
/// Bytes per megabyte, for memory reporting.
pub const BYTES_PER_MB: f32 = 1_048_576.0;
+1
View File
@@ -1,5 +1,6 @@
pub mod benchmark;
pub mod chunk_data;
pub mod magic_numbers;
pub mod rendering;
pub mod tile_changed;
pub mod tilemap;
+2 -1
View File
@@ -35,6 +35,7 @@ use super::chunk_data::ChunkData;
use crate::constants::ITILE_SIZE;
use crate::entities::item::drop_table::DropTable;
use crate::world::chunks::{world_to_chunk, CHUNK_SIZE, Z_ABOVE, Z_BELOW};
use crate::world::tiles::magic_numbers::ASTAR_DEFAULT_WEIGHT;
/// Packed floor tile data for efficient storage. ~35 bytes vs 76 bytes tuple.
#[derive(Clone, Debug)]
@@ -296,7 +297,7 @@ impl TileMap {
self.floor_tiles
.get(&world_pos)
.map(|t| t.astar_weight)
.unwrap_or(100)
.unwrap_or(ASTAR_DEFAULT_WEIGHT)
}
/// Remove a fixture tile, clearing both the HashMap entry and ChunkData bitsets.
+4 -2
View File
@@ -9,6 +9,7 @@ use crate::constants::{ITILE_SIZE, TILE_PIXELS, TILE_SIZE};
use crate::game::ZIndex;
use crate::world::chunks::{CHUNK_SIZE, CHUNK_SIZE_TILE, Z_BELOW, Z_TOTAL};
use crate::world::textures::TilemapTileset;
use crate::world::tiles::magic_numbers::{BYTES_PER_MB, Z_CHANGE_HISTORY_LEN};
use crate::world::tiles::{FloorTileData, TileMap};
pub const LAYER_FLOOR: u8 = 0;
@@ -139,6 +140,7 @@ fn tile_for_depth(real_index: u16, z_diff: i32) -> TileData {
}
fn is_tile_visible_at_z(tile_data: &FloorTileData, z_index: usize) -> bool {
// MAGIC u8::MAX + 1; z_index stored as u8 in layer key so this is out-of-range
if z_index >= 256 {
return false;
}
@@ -386,7 +388,7 @@ pub fn populate_tilemap_chunk_data(
let elapsed = now.elapsed().as_secs_f64() * 1000.0;
bench.last_z_change_populate_ms = elapsed;
bench.z_change_history_ms.push(elapsed);
if bench.z_change_history_ms.len() > 100 {
if bench.z_change_history_ms.len() > Z_CHANGE_HISTORY_LEN {
bench.z_change_history_ms.remove(0);
}
}
@@ -431,7 +433,7 @@ pub fn populate_tilemap_chunk_data(
bench.dirty_keys_last = processed;
bench.dirty_keys_total += processed;
bench.tile_data_mb = (registry.entities.len() as f64 * (CHUNK_SIZE * CHUNK_SIZE) as f64 * 4.0)
/ (1024.0 * 1024.0);
/ BYTES_PER_MB as f64;
}
pub fn on_camera_z_changed(