extends SceneTree # Headless gameplay test: run with # godot --headless -s res://tests/test_planet_conquest_gate.gd # Verifies: # 1. GameState starts every planet unconquered, and re-registering a # garrison on a conquered planet cannot un-conquer it # 2. a planet falls only when its LAST defender does, and the # planet_conquered signal fires exactly once, carrying what it paid # 3. over an unconquered planet, the extractor build option is disabled, # the info popup explains why, and confirming spends nothing and builds # nothing # 4. once conquered, the same build flow works and charges the cost var main_scene: Node var fleet: Node var game_state: Node var controller: Node var button: IconButton var popup: Control var target_chunk: Vector2i var stage: int = 0 var elapsed: float = 0.0 var conquered_signal_count: int = 0 # #212 needs the award to travel WITH the signal, so whatever announces # conquest reports a number that was actually paid. var conquered_signal_points: int = -1 func _initialize() -> void: var found: bool = false for x in range(-15, 15): for y in range(-15, 15): var chunk: Vector2i = Vector2i(x, y) # #75: this test is about the conquest gate, not resource type - # skip a research_lab specifically, since it yields nothing and # is correctly refused regardless of conquest (see # test_research_lab_extractor_gate.gd for that behaviour). if WorldGen.has_planet_at_chunk(chunk) \ and WorldGen.planet_data_at_chunk(chunk).definition_id != "research_lab": target_chunk = chunk found = true break if found: break if not found: return _fail("no planet chunk to test with") game_state = root.get_node("GameState") game_state.rocks = 200 game_state.buildings.clear() game_state.extractors.clear() game_state.planet_capture.clear() game_state.reset_progression() for node in GameState.SKILL_TREE: game_state.unlocked.append(node.id) var target_planet_id: String = "chunk:%d:%d" % [target_chunk.x, target_chunk.y] # Unit-level checks on GameState itself, before any scene involvement. if game_state.is_planet_conquered(target_planet_id): return _fail("a fresh planet reported conquered") game_state.register_planet_garrison(target_planet_id, 3) if game_state.garrison_remaining(target_planet_id) != 3: return _fail("a garrison of 3 registered as %d" \ % game_state.garrison_remaining(target_planet_id)) game_state.planet_conquered.connect(func(_id, points): conquered_signal_count += 1 conquered_signal_points = points) game_state.mark_garrison_enemy_destroyed(target_planet_id) game_state.mark_garrison_enemy_destroyed(target_planet_id) if game_state.is_planet_conquered(target_planet_id): return _fail("conquered with a defender still standing") game_state.mark_garrison_enemy_destroyed(target_planet_id) if not game_state.is_planet_conquered(target_planet_id): return _fail("the whole garrison is down but the planet is not conquered") # A stray kill reported after the planet has fallen must not fire again. game_state.mark_garrison_enemy_destroyed(target_planet_id) # Nor may re-landing hand the planet back: registration is refused # outright once it is taken. game_state.register_planet_garrison(target_planet_id, 3) if not game_state.is_planet_conquered(target_planet_id): return _fail("re-registering a garrison un-conquered a planet already taken") if conquered_signal_count != 1: return _fail("planet_conquered fired %d times, expected exactly 1" % conquered_signal_count) # This planet was registered with no definition and no override, so the # conquest paid nothing - and the signal has to say so rather than leave # the announcement to guess. if conquered_signal_points != 0: return _fail("an unrewarded conquest reported %d research points" \ % conquered_signal_points) # A rewarded one reports what it paid. var paid_planet_id: String = target_planet_id + ":paid" var points_before: int = game_state.research_points game_state.register_planet_garrison(paid_planet_id, 1, "", 4) conquered_signal_points = -1 game_state.mark_garrison_enemy_destroyed(paid_planet_id) if conquered_signal_points != 4: return _fail("a 4-point conquest reported %d" % conquered_signal_points) if game_state.research_points != points_before + 4: return _fail("the reported points were not the ones actually banked") print("conquest reports the research points it paid") # Reset conquest for the scene-level gate check below - this run tests # the unconquered path first, then conquers it. game_state.planet_capture.clear() main_scene = load("res://world/Main.tscn").instantiate() root.add_child(main_scene) fleet = main_scene.get_node("Fleet") controller = main_scene.get_node("BuildController") button = main_scene.get_node("UI/BuildMenu/ExtractorButton") popup = main_scene.get_node("UI/BuildMenu/InfoPopup") func _process(delta: float) -> bool: elapsed += delta match stage: 0: main_scene.get_node("AsteroidField").set_process(false) main_scene.get_node("EnemySpawner").set_process(false) var near: Vector2 = WorldGen.chunk_origin(target_chunk) + Vector2.ONE * WorldGen.CHUNK_SIZE * 0.5 for ship in fleet.ships: ship.position = near fleet.move_to(near) elapsed = 0.0 stage = 1 return false 1: var planet_field: Node = main_scene.get_node("PlanetField") if elapsed < planet_field.check_interval + 0.2: return false if not planet_field.populated_chunks.has(target_chunk): return _fail("planet never spawned") var planet: Node2D = planet_field.populated_chunks[target_chunk][0] for ship in fleet.ships: ship.position = planet.position fleet.move_to(planet.position) elapsed = 0.0 stage = 2 return false 2: if elapsed < 0.3: return false var planet_field: Node = main_scene.get_node("PlanetField") var planet: Node2D = planet_field.populated_chunks[target_chunk][0] controller._on_build_pressed() if button.enabled: return _fail("extractor option enabled over an unconquered planet") controller._show_building_info(GameState.BUILDING_EXTRACTOR) var explains_capture: bool = popup.visible \ and "captur" in popup.get_node("Panel/MessageLabel").text.to_lower() var offered: bool = popup.get_node("Panel/BuildButton").visible if not explains_capture or offered: return _fail("unconquered popup did not explain itself, or offered a build") var rocks_before: int = game_state.rocks controller._confirm_build() # must do nothing even if somehow pressed if game_state.rocks != rocks_before or not game_state.extractors.is_empty(): return _fail("build proceeded over an unconquered planet") popup.visible = false # Now conquer it and confirm the same flow succeeds. game_state.register_planet_garrison(planet.planet_id, 2) game_state.mark_garrison_enemy_destroyed(planet.planet_id) game_state.mark_garrison_enemy_destroyed(planet.planet_id) controller._on_build_pressed() if not button.enabled: return _fail("extractor option stayed disabled after conquest") controller._show_building_info(GameState.BUILDING_EXTRACTOR) if not popup.get_node("Panel/BuildButton").visible: return _fail("conquered planet did not offer a build") controller._confirm_build() var built: bool = game_state.has_extractor_at(planet.planet_id) var charged: bool = game_state.rocks == rocks_before - int(game_state.building_costs(GameState.BUILDING_EXTRACTOR)[GameState.RESOURCE_ORE]) if not built or not charged: return _fail("conquered planet did not build/charge correctly") print("TEST_PLANET_CONQUEST_GATE: PASS") quit(0) return true return false func _fail(message: String) -> bool: print("TEST_PLANET_CONQUEST_GATE: FAIL (%s)" % message) quit(1) return true