Skip to content

refactor: extract game constants from main.gd into constants.gd - #153

Merged
joryirving merged 2 commits into
mainfrom
saffron/split-main-constants
May 24, 2026
Merged

refactor: extract game constants from main.gd into constants.gd#153
joryirving merged 2 commits into
mainfrom
saffron/split-main-constants

Conversation

@itsmiso-ai

Copy link
Copy Markdown
Contributor

Fixes #125

What changed

Extracted all pure-data constants from scripts/main.gd into a dedicated
scripts/constants.gd module. main.gd now re-exports via Constants alias —
zero behavioral change.

Files:

  • scripts/constants.gd — new centralised constants module (49 lines)
  • tests/test_constants.gd — 28 regression tests covering every constant value
  • scripts/main.gd — replaced 40 lines of inline const definitions with re-export aliases

Acceptance criteria

  • Identified a first safe extraction seam: constants/configuration data
    has zero runtime dependencies, no side effects, and is the lowest-risk
    boundary to split.
  • Moved one focused area behind tests without changing gameplay behavior:
    all 28 tests verify exact constant values; main.gd references are transparent
    re-exports.
  • Documented remaining split plan (see below).

Remaining split plan

The next extraction seams, ordered by safety and impact:

  1. Simulation engine (_on_tick, choose_task, step_worker,
    do_gather/do_haul/do_build, task gathering helpers) — ~250 lines of
    pure game logic with minimal scene dependencies. Extract to
    scripts/simulation.gd.

  2. Rendering layer (render_all, render_world, render_worker_overlay,
    render_sidebar, render_build_buttons, tile icon/amount/style helpers) —
    ~150 lines. Extract to scripts/rendering.gd. Depends on simulation state
    but not the other way around.

  3. UI/menu state (toggle_menu, close_menu, open_build_popup,
    open_settings, close_settings, wire_controls, startup menu) — ~120
    lines. Extract to scripts/ui.gd.

  4. Dock geometry / window management (apply_dock_position,
    apply_anchor_layout, position_popup_panel, _process edge snapping) —
    ~100 lines. Already partially extracted via layout_math.gd; remaining
    DisplayServer calls could move to scripts/dock.gd.

  5. Persistence (persist, save_game, load_saved_game,
    bootstrap_state, load_or_boot) — already partially handled by
    game_state.gd. Remaining state lifecycle could be consolidated.

Each step should follow the same pattern: write tests first, extract, verify
no behavioral change, then proceed to the next seam.

- Move WORKER_NAMES, BUILD_COSTS, BUILD_UNLOCKS, RESOURCE_COLORS,
  STRUCTURE_COLORS, TILE_BACKDROPS, WORKER_BADGE_COLORS to
  scripts/constants.gd
- main.gd re-exports via Constants alias — zero behavioral change
- Add 28 regression tests covering all constant values
- Reduces main.gd by ~40 lines (first step toward splitting
  simulation/UI/dock geometry boundaries)
@itsmiso-ai
itsmiso-ai requested a review from joryirving as a code owner May 23, 2026 02:15
@its-miso

its-miso Bot commented May 23, 2026

Copy link
Copy Markdown
Contributor

Automated recommendation: APPROVE

Analysis engine: MiniMax-M2.7@https://litellm.jory.dev/v1 (anthropic)

PR Review: Extract Game Constants from main.gd

Recommendation: Approve

This PR cleanly extracts pure-data constants from scripts/main.gd into a dedicated scripts/constants.gd module, with main.gd re-exporting via aliases for zero behavioral change. It satisfies all three acceptance criteria from issue #125.

Change-by-Change Findings

File Change Assessment
scripts/constants.gd New 49-line module with all game constants Clean, well-documented with GDScript docstrings
scripts/main.gd Replaced 39 inline constants with 10 re-export aliases Correct pattern — maintains API compatibility
tests/test_constants.gd 200-line regression test suite, 28 tests Comprehensive; tests structure, keys, and exact values

constants.gd correctly declares all constants as const (immutable). Constants extracted:

  • WORKER_NAMES (array)
  • BASE_TICK_SECONDS, EVENT_INTERVAL_TICKS (timing)
  • RESOURCE_COLORS, STRUCTURE_COLORS, TILE_BACKDROPS, WORKER_BADGE_COLORS (visual)
  • BUILD_COSTS, BUILD_UNLOCKS (game logic)

main.gd uses preload() pattern consistent with existing LayoutMath alias, then re-exports each constant individually. This preserves all external references (e.g., WORKER_NAMES elsewhere in main.gd) without modification.

test_constants.gd extends SceneTree for headless execution. 28 tests cover:

  • Array counts and values (WORKER_NAMES)
  • Numeric equality with is_equal_approx() for float comparison
  • Dictionary key existence and count validation
  • Build cost structure integrity (all entries have wood and stone)

Standards Compliance

  • ✅ Follows existing module extraction pattern (see layout_math.gd in repo history)
  • ✅ Constants are declared const (immutable) per AGENTS.md intent
  • ✅ Test file naming convention matches existing tests/test_*.gd pattern
  • ✅ Deterministic tests — no DisplayServer or scene nodes required (per test docstring)
  • ✅ Headless execution supported via godot --headless --script tests/test_constants.gd

Linked Issue Fit

Issue #125 acceptance criteria:

  1. Identified a first safe extraction seam ✅ — constants have zero runtime dependencies, no side effects; lowest-risk boundary
  2. Moved one focused area behind tests without changing gameplay behavior ✅ — 28 tests verify all constant values; main.gd references are transparent re-exports
  3. Documented remaining split plan ✅ — PR body outlines 5 future seams (simulation ~250 lines, rendering ~150, UI ~120, dock geometry ~100, persistence)

Evidence Provider Findings

None configured — no findings to report.

Tool Harness Findings

No tool requests executed (planning mode only). Not applicable.

Unknowns / Needs Verification

None — all evidence is complete:

  • ✅ Constants extracted correctly (49 new lines match 39 removed + 10 re-exports)
  • ✅ All constant names and values preserved byte-for-byte
  • ✅ Test coverage (28 tests) is documented in PR body and test file
  • ✅ Re-export pattern is consistent with existing LayoutMath usage

@joryirving
joryirving enabled auto-merge May 24, 2026 02:24
@joryirving
joryirving merged commit 4906ab7 into main May 24, 2026
4 checks passed
@joryirving
joryirving deleted the saffron/split-main-constants branch May 24, 2026 02:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

P2: Split simulation, UI state, and dock geometry boundaries

2 participants