From be763908b267bd078a89269eccf218f47ef2af7c Mon Sep 17 00:00:00 2001 From: Jeroen Schweitzer Date: Tue, 17 Feb 2026 16:35:05 +0100 Subject: [PATCH] refactor(client): replace fog byte magic numbers with named constants Define VIS_HIDDEN/PERIPHERAL/FORWARD and EXP_UNEXPLORED/EXPLORED/ VISIBLE in FogState. Replaces 7 magic number usages in _resize() and update_from_state(). Provides stable assertion targets for fog tests. Implements #476. Co-Authored-By: Claude Opus 4.6 --- client/scripts/autoloads/fog_state.gd | 29 ++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/client/scripts/autoloads/fog_state.gd b/client/scripts/autoloads/fog_state.gd index d63acd96e..64388e241 100644 --- a/client/scripts/autoloads/fog_state.gd +++ b/client/scripts/autoloads/fog_state.gd @@ -4,6 +4,17 @@ extends Node ## Read by fog_shader.gd for shader uniforms. Not a renderer — pure data. ## Architecture: docs/architecture/fog-shader-spec.md | D-059 +# Fog texture byte values — visibility and exploration layers. +# Used by fog shader to distinguish visual treatment per tile. +# Test assertions reference these: assert_that(byte).is_equal(FogState.VIS_FORWARD) +const VIS_HIDDEN: int = 0 # Not in LOS — fully fogged +const VIS_PERIPHERAL: int = 180 # In LOS, peripheral sector — light fog dimming +const VIS_FORWARD: int = 255 # In LOS, forward sector — clear vision + +const EXP_UNEXPLORED: int = 0 # Never seen — total darkness +const EXP_EXPLORED: int = 128 # Previously seen, now out of LOS — deep fog +const EXP_VISIBLE: int = 255 # Currently in LOS — clear (written each frame) + var map_bounds: Rect2i = Rect2i(0, 0, 1, 1) var visibility_texture: ImageTexture var exploration_texture: ImageTexture @@ -31,13 +42,13 @@ func _resize(bounds: Rect2i) -> void: _vis_bytes = PackedByteArray() _vis_bytes.resize(sz) - _vis_bytes.fill(0) + _vis_bytes.fill(VIS_HIDDEN) _vis_image = Image.create_from_data(_width, _height, false, Image.FORMAT_R8, _vis_bytes) visibility_texture = ImageTexture.create_from_image(_vis_image) _exp_bytes = PackedByteArray() _exp_bytes.resize(sz) - _exp_bytes.fill(0) + _exp_bytes.fill(EXP_UNEXPLORED) _exp_image = Image.create_from_data(_width, _height, false, Image.FORMAT_R8, _exp_bytes) exploration_texture = ImageTexture.create_from_image(_exp_image) @@ -62,21 +73,21 @@ func update_from_state() -> void: var positions: Dictionary = GameState.visible_positions var sectors: Dictionary = GameState.visibility_sectors - # TODO(v0.2): gradual decay over game-time instead of immediate 255→128 + # TODO(v0.2): gradual decay over game-time instead of immediate EXP_VISIBLE→EXP_EXPLORED # 1. Clear visibility, then write current LOS - _vis_bytes.fill(0) + _vis_bytes.fill(VIS_HIDDEN) for pos in positions: var px: int = pos.x - ox var py: int = pos.y - oy if px < 0 or py < 0 or px >= _width or py >= _height: continue var sector: String = sectors.get(pos, "Forward") - _vis_bytes[py * _width + px] = 255 if sector == "Forward" else 180 + _vis_bytes[py * _width + px] = VIS_FORWARD if sector == "Forward" else VIS_PERIPHERAL _vis_image.set_data(_width, _height, false, Image.FORMAT_R8, _vis_bytes) visibility_texture.update(_vis_image) - # 2. Exploration: tiles leaving LOS decay to 128, visible tiles stay 255 + # 2. Exploration: tiles leaving LOS decay to EXP_EXPLORED, visible tiles stay EXP_VISIBLE # Only touch tiles that changed (O(visible) not O(map_size)) for pos in _prev_visible: if not positions.has(pos): @@ -84,14 +95,14 @@ func update_from_state() -> void: var py: int = pos.y - oy if px >= 0 and py >= 0 and px < _width and py < _height: var idx: int = py * _width + px - if _exp_bytes[idx] > 128: - _exp_bytes[idx] = 128 + if _exp_bytes[idx] > EXP_EXPLORED: + _exp_bytes[idx] = EXP_EXPLORED for pos in positions: var px: int = pos.x - ox var py: int = pos.y - oy if px >= 0 and py >= 0 and px < _width and py < _height: - _exp_bytes[py * _width + px] = 255 + _exp_bytes[py * _width + px] = EXP_VISIBLE _exp_image.set_data(_width, _height, false, Image.FORMAT_R8, _exp_bytes) exploration_texture.update(_exp_image)