spaceship/tests/test_resources_and_buildings.gd
Loïc Wisniewski a3c6316676 Rework how a game starts: dev mode, a bare opening, and fleet growth
Adds a Dev Mode entry to the main menu and takes the play-testing
conveniences out of a normal game, which turn out to be the same change
from two directions.

DEV MODE. Banks 2000 of every resource, grants 500 research points, and
plants one of each planet type in the chunks around the spawn. Built ON TOP
of reset_new_game() rather than beside it, so a dev game is a normal game
plus a stock rather than a second definition of "fresh game" to keep in
sync. Offered only when running from Godot - OS.has_feature("editor"), the
tag an export clears - as is the map editor. Hidden AND guarded: pressing
either anyway is refused, because visibility is presentation and the rule is
the rule.

The menu now lays its stack out in code. Hiding two of four entries with
scene-authored offsets would leave holes in a shipped build - the same
failure the HUD columns had - so it measures the entries that actually exist
and centres on them.

A NORMAL GAME NOW HANDS OVER NOTHING. No 500 banked ore (removed from both
reset_new_game and the autoload's own default, so the two cannot disagree),
and no gas giant guaranteed in the origin chunk. Both were play-testing
conveniences that had become part of the real world's shape: every player
got the same landmark in the same place. Play-testing wanted all four planet
types anyway, and a normal game wanted none of them - different needs, now
different mechanisms.

WorldGen cannot read GameState back (an autoload is not resolvable as a
global identifier that early, the hazard PlanetSetups.gd already works
around), so GameState.dev_mode's setter PUSHES the flag down. World
generation stays static and dependency-free, GameState stays the single
source of truth, and dev planting is applied after every rng draw so a dev
world and a normal one differ only in those four chunks.

FLEET SIZE. BASE_FLEET_SIZE was doing two jobs - how many slots exist AND
how many ships you start with - which forced a new game to open with a full
fleet, leaving the first ship crafter nothing to do. Split into
BASE_FLEET_SIZE (3 slots) and STARTING_SHIP_COUNT (1 vessel), so the opening
has two empty slots to fill. A third copy of the old value turned up in
PlanetAtmosphereController, hardcoded as spawn_ships(5) and silently
degraded into "however many slots exist" once max_fleet_size() clamped it.

The doctrine branch now runs 3 -> 10 slots. Wing (30 -> 15) and Squadron
(70 -> 35, 2 -> 1 points) are cut hardest: they are no longer a mid-game
luxury but the ordinary way out of the opening, and pricing them as a luxury
made the early game feel stuck. Armada follows at 90/2. Two new tiers,
Battlegroup and Grand Fleet, grant +2 each - seven single-vessel nodes in
one lane would be a long monotonous chain for a branch that only ever does
one thing. DEFAULT_FORMATION grows to 10 slots, because max_fleet_size()
clamps to its length and a doctrine past that would charge real resources
for nothing; test_fleet_growth.gd asserts that invariant directly.

TEST FALLOUT, all of it from the deliberate change rather than a defect:

- Tests that silently inherited the old 5-ship default now ask for the fleet
  they need. Several had to crew it from their first frame rather than
  _initialize(), where the engine's deferred tree-entry finalization undoes
  it - the same hazard those tests already documented for set_process(false).
- test_planet_landing_proximity and test_return_position_matches_planet take
  the FIRST chunk holding a planet and land there. The guaranteed gas giant
  had been masking that this can now be a black hole, which destroys ships on
  contact. They exclude it, and clear the asteroids the field already placed.
- test_pivot_controls measured rotation with angle_difference, which wraps at
  +/-pi, so a hold that turned a full circle read as no rotation. Latent
  flakiness that only ever showed under load; it now reads the controller's
  unwrapped pivot_hold_amount.
- test_research_points was failing on ore, not points: can_unlock() checks
  the ore price too, and a fresh game banks nothing now.
- test_gas_system's "a reachable gas source near the spawn" premise moved to
  dev mode, which is where that guarantee now lives.

Full suite green at 97/97.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-18 12:10:14 +02:00

231 lines
8.9 KiB
GDScript

extends SceneTree
# Headless gameplay test: run with
# godot --headless -s res://tests/test_resources_and_buildings.gd
# Covers the resource/building loop end to end:
# 1. flying a ship over a surface rock loads it into the fleet's hold
# (banking it needs a trip to the mothership - see #14 and
# tests/test_fleet_cargo.gd)
# 2. buildings are paid for, persist in GameState, and respawn with the
# space scene (they must survive the trip to a planet and back)
# 3. a ship crafter adds a vessel to the fleet for rocks
# 4. wiping out the fleet rebuilds one vessel at the mothership
var stage: int = 0
var elapsed: float = 0.0
var scene: Node
var fleet: Node
var game_state: Node
var rock: Area2D
var mothership_pos: Vector2 = Vector2(900.0, -400.0)
func _initialize() -> void:
game_state = root.get_node("GameState")
game_state.rocks = 0
game_state.fleet_cargo = 0
game_state.buildings.clear()
game_state.has_return_position = false
scene = load("res://PlanetSurface.tscn").instantiate()
root.add_child(scene)
current_scene = scene
fleet = scene.get_node("Fleet")
func _process(delta: float) -> bool:
elapsed += delta
match stage:
0:
# --- gather a rock -------------------------------------------
scene.get_node("SurfaceFeatures/ResourceProvider").set_process(false)
rock = load("res://Rock.tscn").instantiate()
rock.build_shape(1)
# Placed OUTSIDE the pickup shape but inside attract_radius, so
# this also proves the magnetic pull actually reels it in rather
# than the ship simply happening to overlap it on spawn.
rock.position = fleet.ships[0].position + Vector2(150.0, 0.0)
scene.add_child(rock)
elapsed = 0.0
stage = 1
return false
1:
if elapsed < 1.5:
return false
# Into the hold, not the bank: since #14 ore is cargo until the
# fleet delivers it to the mothership.
if game_state.fleet_cargo != 1:
print("fleet_cargo=%d after flying a ship onto one" % game_state.fleet_cargo)
print("TEST_RESOURCES_AND_BUILDINGS: FAIL (rock not gathered)")
quit(1)
return true
print("gathered rock -> hold=%d bank=%d" % [game_state.fleet_cargo, game_state.rocks])
# Bank enough to build with, then move to the space scene.
game_state.rocks = 60
change_scene_to_file("res://Main.tscn")
elapsed = 0.0
stage = 2
return false
2:
if elapsed < 0.4:
return false
if current_scene == null or current_scene.name != "Main":
return false
scene = current_scene
fleet = scene.get_node("Fleet")
scene.get_node("AsteroidField").set_process(false)
scene.get_node("EnemySpawner").set_process(false)
# --- place both buildings -------------------------------------
var field: Node = scene.get_node("BuildingField")
var before: int = game_state.rocks
for ship in fleet.ships:
ship.position = mothership_pos
fleet.move_to(mothership_pos)
field.place(GameState.BUILDING_MOTHERSHIP, mothership_pos)
game_state.spend(GameState.MOTHERSHIP_COST)
field.place(GameState.BUILDING_SHIP_CRAFTER, Vector2(1400.0, 300.0))
game_state.spend(GameState.SHIP_CRAFTER_COST)
var paid: bool = game_state.rocks == before - GameState.MOTHERSHIP_COST - GameState.SHIP_CRAFTER_COST
var stored: bool = game_state.buildings.size() == 2
print("buildings=%d rocks %d->%d paid=%s" % [
game_state.buildings.size(), before, game_state.rocks, paid
])
if not (paid and stored):
print("TEST_RESOURCES_AND_BUILDINGS: FAIL (placing/paying wrong)")
quit(1)
return true
elapsed = 0.0
stage = 3
return false
3:
if elapsed < 0.3:
return false
# --- crafter adds a ship --------------------------------------
# Crafting adds a FOURTH vessel below, one past the 3 slots a
# new game has, so make room first - the subject here is the
# crafter, not the fleet cap.
game_state.unlocked.append_array(["wing", "squadron", "armada"])
fleet.spawn_ships(3, Vector2.ZERO)
var before_ships: int = fleet.ships.size()
var before_rocks: int = game_state.rocks
game_state.spend(GameState.SHIP_COST)
var added: bool = fleet.add_ship()
print("craft: ships %d->%d rocks %d->%d" % [
before_ships, fleet.ships.size(), before_rocks, game_state.rocks
])
if not (added and fleet.ships.size() == before_ships + 1 and game_state.rocks == before_rocks - GameState.SHIP_COST):
print("TEST_RESOURCES_AND_BUILDINGS: FAIL (crafting a ship)")
quit(1)
return true
# --- command centre: drag a formation slot ---------------------
var editor: Control = scene.get_node("UI/FormationMenu/Editor")
var menu: Control = scene.get_node("UI/FormationMenu")
menu.visible = true
editor.size = Vector2(520, 520)
# The editor is driven from BuildController._unhandled_input,
# which never fires for Controls that consume touches through
# the GUI system first. The menu AND its full-screen backdrop
# must both be IGNORE, or dragging a slot silently does nothing
# on a real device - which is exactly what happened.
for node in [menu, menu.get_node("Backdrop"), editor]:
if node.mouse_filter != Control.MOUSE_FILTER_IGNORE:
print("%s has mouse_filter=%d, needs IGNORE" % [node.name, node.mouse_filter])
print("TEST_RESOURCES_AND_BUILDINGS: FAIL (formation menu would swallow drags)")
quit(1)
return true
var slot_before: Vector2 = game_state.formation[1]
var down: InputEventScreenTouch = InputEventScreenTouch.new()
down.index = 0
down.pressed = true
down.position = editor.global_position + editor._slot_screen_pos(1)
var grabbed: bool = editor.handle_touch(down)
var move: InputEventScreenDrag = InputEventScreenDrag.new()
move.index = 0
move.position = editor.global_position + editor._slot_screen_pos(1) + Vector2(40.0, 25.0)
editor.handle_drag(move)
var slot_after: Vector2 = game_state.formation[1]
menu.visible = false
print("formation slot 1: %s -> %s (grabbed=%s)" % [slot_before, slot_after, grabbed])
if not (grabbed and slot_after != slot_before):
print("TEST_RESOURCES_AND_BUILDINGS: FAIL (formation slot did not drag)")
quit(1)
return true
# Editing must re-seat the ships ALREADY in flight, not just
# ones built later - otherwise the edit reads as doing nothing.
var live_target: Vector2 = fleet.ships[1].move_target
var expected_target: Vector2 = fleet.fleet_target + slot_after
var live_ok: bool = live_target.distance_to(expected_target) < 0.01
print("live ship 1 target %s, expected %s (ok=%s)" % [
live_target, expected_target, live_ok
])
if not live_ok:
print("TEST_RESOURCES_AND_BUILDINGS: FAIL (existing ships ignored the edit)")
quit(1)
return true
# --- pivot the formation --------------------------------------
var before_pivot: Vector2 = game_state.formation[1]
game_state.rotate_formation(PI / 2.0)
fleet.apply_formation()
var after_pivot: Vector2 = game_state.formation[1]
var rotated_ok: bool = after_pivot.distance_to(before_pivot.rotated(PI / 2.0)) < 0.01
var length_kept: bool = abs(after_pivot.length() - before_pivot.length()) < 0.01
var ship_followed: bool = fleet.ships[1].move_target.distance_to(fleet.fleet_target + after_pivot) < 0.01
print("pivot: %s -> %s (rotated=%s shape_kept=%s ship_followed=%s)" % [
before_pivot, after_pivot, rotated_ok, length_kept, ship_followed
])
if not (rotated_ok and length_kept and ship_followed):
print("TEST_RESOURCES_AND_BUILDINGS: FAIL (pivot)")
quit(1)
return true
game_state.rotate_formation(-PI / 2.0)
slot_after = game_state.formation[1]
# A rebuilt fleet must adopt the edited layout, which is the
# whole point of storing the formation in GameState.
fleet.spawn_ships(2, Vector2.ZERO)
var uses_layout: bool = fleet.ships[1].position.distance_to(slot_after) < 0.01
print("respawned ship 1 at %s, formation says %s (matches=%s)" % [
fleet.ships[1].position, slot_after, uses_layout
])
if not uses_layout:
print("TEST_RESOURCES_AND_BUILDINGS: FAIL (fleet ignored the edited formation)")
quit(1)
return true
# --- wipe the fleet -------------------------------------------
for ship in fleet.ships.duplicate():
ship.queue_free()
fleet.ships.clear()
fleet.formation_offsets.clear()
elapsed = 0.0
stage = 4
return false
4:
if elapsed < 0.5:
return false
# BuildController._process should have rebuilt one vessel, at
# the mothership rather than the origin.
var respawned: bool = fleet.ships.size() == 1
var at_mothership: bool = respawned and fleet.ships[0].position.distance_to(mothership_pos) < 120.0
print("after wipe: ships=%d at_mothership=%s (pos=%s expected~%s)" % [
fleet.ships.size(), at_mothership,
fleet.ships[0].position if respawned else Vector2.ZERO, mothership_pos
])
if respawned and at_mothership:
print("TEST_RESOURCES_AND_BUILDINGS: PASS")
quit(0)
else:
print("TEST_RESOURCES_AND_BUILDINGS: FAIL (fleet not rebuilt at the mothership)")
quit(1)
return true
return false