spaceship/tests/test_fleet_formation.gd
Loïc Wisniewski 2f161621c3 #84: move 215 root files into a folder hierarchy
Everything lived in one flat directory, so a diff gave no clue what area it
touched and finding the file you wanted meant knowing its exact name. Twelve
folders now, grouped by what a thing IS rather than what type it is - a
vessel's definition, its component scenes and its upgrade resources sit
together, because that is the set you read when you change one of them.

  buildings  core  controllers  enemies  logistics  planets
  resources  shaders  surface  ui  vessels  vfx  world

332 files moved and every res:// reference rewritten - 306 files touched,
including the autoload and main-scene paths in project.godot. Nothing renamed:
this is purely where files sit, so the diff is a pile of pure moves and no
behaviour is buried in it. ui_theme.tres, icon.svg and web_shell.html stay at
the root, where Godot and the export preset expect them.

Verified three ways, because a suite alone would not have caught all of it:
the full 117 pass, the web export builds clean, and the game renders.

The export is why that matters. The FIRST export after the move failed with
"Attempt to open script 'res://GameState.gd'" - the old path - while
project.godot on disk plainly said res://core/GameState.gd. That is this
project's documented Android FUSE mtime trap: Godot served a cached copy of a
file whose content had changed. A `touch` fixed it. Nothing in the test suite
would have found that, because the suite runs from a copy on a native
filesystem - the one place the bug cannot happen.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-21 19:44:14 +02:00

77 lines
2.4 KiB
GDScript

extends SceneTree
# Headless gameplay test: run with
# godot --headless -s res://tests/test_fleet_formation.gd
# Verifies that issuing a fleet move order makes every ship converge on its
# formation slot (offset preserved) around the ordered point AND actually
# stops there - a regression guard for the arrival-overshoot bug where ships
# oscillated back and forth around the target forever instead of settling.
const CHECKPOINT_TIME: float = 4.0
const TEST_DURATION: float = 7.0
const ARRIVE_TOLERANCE: float = 2.0
const STABLE_TOLERANCE: float = 1.0
const VELOCITY_TOLERANCE: float = 1.0
var main_scene: Node
var fleet: Node
var target: Vector2 = Vector2(600.0, 300.0)
var elapsed: float = 0.0
var started: bool = false
var checkpoint_taken: bool = false
var checkpoint_positions: Array = []
func _initialize() -> void:
var packed: PackedScene = load("res://world/Main.tscn")
main_scene = packed.instantiate()
root.add_child(main_scene)
fleet = main_scene.get_node("Fleet")
func _physics_process(delta: float) -> bool:
if not started:
# Wait one physics tick so Fleet/Ship _ready() has run and
# formation_offsets are computed before we issue the order.
fleet.move_to(target)
started = true
return false
elapsed += delta
if not checkpoint_taken and elapsed >= CHECKPOINT_TIME:
checkpoint_taken = true
for ship in fleet.ships:
checkpoint_positions.append(ship.position)
if elapsed >= TEST_DURATION:
_finish()
return true
return false
func _finish() -> void:
var all_ok: bool = true
for i in range(fleet.ships.size()):
var ship: Node = fleet.ships[i]
var expected: Vector2 = target + fleet.formation_offsets[i]
var dist: float = ship.position.distance_to(expected)
var drift_since_checkpoint: float = ship.position.distance_to(checkpoint_positions[i])
var speed: float = ship.velocity.length()
var arrived: bool = dist <= ARRIVE_TOLERANCE
var stable: bool = drift_since_checkpoint <= STABLE_TOLERANCE
var stopped: bool = speed <= VELOCITY_TOLERANCE
var ok: bool = arrived and stable and stopped
all_ok = all_ok and ok
print("Ship %d expected=%s actual=%s dist=%.3f drift_since_t%.0fs=%.3f speed=%.3f %s" % [
i, expected, ship.position, dist, CHECKPOINT_TIME, drift_since_checkpoint, speed, "OK" if ok else "FAIL"
])
if all_ok:
print("TEST_FLEET_FORMATION: PASS (ships arrived and settled, no oscillation)")
quit(0)
else:
print("TEST_FLEET_FORMATION: FAIL")
quit(1)