spaceship/tests/test_planet_identity_persists.gd

122 lines
3.9 KiB
GDScript3
Raw Permalink Normal View History

extends SceneTree
# Headless gameplay test: run with
# godot --headless -s res://tests/test_planet_identity_persists.gd
# Regression test for: leaving a planet's surface and coming back
# respawned Main.tscn's PlanetField from scratch, and since planet
# name/position/shape were picked with the unseeded global RNG at spawn
# time, the "same" planet (same chunk) would come back with a different
# name, a different position within the chunk, and a different outline
# shape every single time. Fixed by seeding planet identity purely from
# the chunk's own coordinates (mirrors WorldGen's own deterministic noise
# pattern), so this loads Main.tscn twice and checks the planet at the
# same chunk is identical both times.
var target_chunk: Vector2i
var first_name: String
var first_pos: Vector2
var first_shape: PackedVector2Array
var stage: int = 0
var main_scene: Node
var elapsed: float = 0.0
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)
if WorldGen.has_planet_at_chunk(chunk):
target_chunk = chunk
found = true
break
if found:
break
if not found:
print("FAIL: could not find any chunk with a planet to test with")
quit(1)
return
_load_main()
func _load_main() -> void:
# GameState (an autoload) isn't accessible as a bare global identifier
# from a script that extends SceneTree directly - fetch it via root.
var game_state: Node = root.get_node("GameState")
game_state.has_return_position = true
game_state.surviving_ship_count = 5
game_state.return_world_position = WorldGen.chunk_origin(target_chunk) + Vector2.ONE * WorldGen.CHUNK_SIZE * 0.5
game_state.return_camera_zoom = 1.0
#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
var packed: PackedScene = load("res://world/Main.tscn")
main_scene = packed.instantiate()
root.add_child(main_scene)
main_scene.get_node("AsteroidField").set_process(false)
main_scene.get_node("EnemySpawner").set_process(false)
func _process(_delta: float) -> bool:
match stage:
0:
# Wait one tick for _ready() (and the forced synchronous
# update_chunks() call inside it) to have actually run.
stage = 1
return false
1:
var planet_field: Node = main_scene.get_node("PlanetField")
if not planet_field.populated_chunks.has(target_chunk):
print("FAIL: planet never spawned in target chunk on first load")
quit(1)
return true
var planet: Node2D = planet_field.populated_chunks[target_chunk][0]
first_name = planet.planet_name
first_pos = planet.position
first_shape = planet.shape_points.duplicate()
main_scene.queue_free()
stage = 2
return false
2:
# Give queue_free() a moment to actually clear the old tree
# before loading a fresh Main.tscn on top of it.
elapsed += 1.0
if elapsed < 3.0:
return false
_load_main()
elapsed = 0.0
stage = 3
return false
3:
stage = 4
return false
4:
var planet_field: Node = main_scene.get_node("PlanetField")
if not planet_field.populated_chunks.has(target_chunk):
print("FAIL: planet never spawned in target chunk on second load")
quit(1)
return true
var planet: Node2D = planet_field.populated_chunks[target_chunk][0]
var name_ok: bool = planet.planet_name == first_name
var pos_ok: bool = planet.position.distance_to(first_pos) < 0.01
var shape_ok: bool = planet.shape_points.size() == first_shape.size()
if shape_ok:
for i in range(first_shape.size()):
if planet.shape_points[i].distance_to(first_shape[i]) > 0.01:
shape_ok = false
break
print("name: %s vs %s (ok=%s)" % [first_name, planet.planet_name, name_ok])
print("pos: %s vs %s (ok=%s)" % [first_pos, planet.position, pos_ok])
print("shape_ok=%s" % shape_ok)
if name_ok and pos_ok and shape_ok:
print("TEST_PLANET_IDENTITY_PERSISTS: PASS")
quit(0)
else:
print("TEST_PLANET_IDENTITY_PERSISTS: FAIL")
quit(1)
return true
return false