diff --git a/scripts/main.gd b/scripts/main.gd index 947c324..f497faa 100644 --- a/scripts/main.gd +++ b/scripts/main.gd @@ -349,7 +349,7 @@ func _on_startup_new_game() -> void: sync_dock_option(chosen_anchor) save_settings() # Backup current save before destructive reset (issue #178) - var _gs: GameState = GameState.new() + var _gs = GameState.new() var _bk: String = _gs.backup_save() if not _bk.is_empty(): push_event_safe("Backup saved before reset: %s" % _bk.get_file()) diff --git a/scripts/worker_cap_logic.gd b/scripts/worker_cap_logic.gd new file mode 100644 index 0000000..058622b --- /dev/null +++ b/scripts/worker_cap_logic.gd @@ -0,0 +1,27 @@ +## Worker cap calculation logic — extracted from main.gd for testability. +## This module has no dependencies on GameState or scene nodes, making it +## suitable for headless unit testing. + +const Constants := preload("res://scripts/constants.gd") + + +## Calculate the worker capacity based on builds and constants. +## - Base cap from Constants.BASE_WORKER_CAP +## - Hut bonus for each completed hut from Constants.WORKER_CAP_BONUSES +static func calculate_worker_cap(builds: Array) -> int: + var cap: int = Constants.BASE_WORKER_CAP + for build in builds: + if bool(build.get("complete", false)): + var kind: String = str(build.get("kind", "")) + cap += int(Constants.WORKER_CAP_BONUSES.get(kind, 0)) + return cap + + +## Check if the colony can recruit another worker. +## - If no workers exist yet, always allow recruitment. +## - Otherwise, compare current count against calculated cap. +static func can_recruit(builds: Array, workers: Array) -> bool: + if workers.size() == 0: + return true + var cap: int = calculate_worker_cap(builds) + return workers.size() < cap diff --git a/tests/test_recruit_worker.gd b/tests/test_recruit_worker.gd index 9bcdfd8..6a53424 100644 --- a/tests/test_recruit_worker.gd +++ b/tests/test_recruit_worker.gd @@ -80,7 +80,7 @@ func test_recruit_adds_worker_to_state(main: Control) -> void: {"name": "Jun", "task": {"kind": "", "data": {}}}, ]) _assert(main.can_recruit_worker(), "precondition: can recruit") - var initial_count := main.state.workers.size() + var initial_count: int = main.state.workers.size() main.recruit_worker() _assert_eq(main.state.workers.size(), initial_count + 1, "recruit: state workers count increases by 1")