spaceship/tests/test_building_redraw_cost.gd
Loïc Wisniewski 39ad050dc6 #117: the crafter and the lab animate only while they are working
A factory whose cranes swing over an empty cradle reads as working on
nothing, and a lab sweeping instruments over a vessel it is not studying
reads the same way.

The CLOCK stops rather than the drawing. Every moving element is a NeonPiece
whose position is pushed from that clock, so a frozen clock leaves the
building standing with its cranes parked; stopping apply_animation() instead
would leave them wherever the last frame happened to put them, which is not
the same thing as idle.

The state is read by tree lookup rather than through the bare `GameState`
identifier. These visual scripts are reached through a BuildingDefinition's
visual_scene, which is preloaded while the definition catalog is still
loading - early enough that the autoload is not in the global class table
yet, and a bare reference fails to COMPILE the whole file. That failure also
hung test_building_redraw_cost, because a visual that never built its pieces
left a null crane being dereferenced every frame.

Both animation tests now say what they mean: the redraw-cost test queues an
order first, because its point is that animating is cheap rather than that
it happens unprompted, and the crafter test additionally pins that an idle
crafter really is still.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-23 21:58:59 +02:00

122 lines
4.2 KiB
GDScript

extends SceneTree
# Headless gameplay test: run with
# godot --headless -s res://tests/test_building_redraw_cost.gd
# Animated buildings must not re-run _draw() to animate (Forgejo issues #6
# and #10).
#
# Every NeonDraw call expands into four antialiased polylines, so redrawing
# a whole shipyard each frame to advance a crane re-tessellates all of it.
# The moving elements are now separate NeonPiece children, rasterised once
# and thereafter only moved, scaled, faded or hidden - operations that reuse
# the cached geometry. Ship.gd and Asteroid.gd already worked this way.
#
# Verifies:
# 1. an animated building's geometry is rasterised exactly ONCE, however
# long it animates
# 2. it is genuinely still animating (so this cannot be "passed" by
# freezing the animation)
# 3. the moving parts really do move - the crane travels and the
# mothership's satellites orbit - proving the animation reaches the
# nodes rather than being silently dropped
# 4. the static command centre needs no per-frame work either
var scene: Node
var field: Node
var crafter: Node2D
var mothership: Node2D
var command: Node2D
var stage: int = 0
var elapsed: float = 0.0
var crane_x_start: float = 0.0
var satellite_rotation_start: float = 0.0
var game_state: Node
func _initialize() -> void:
game_state = root.get_node("GameState")
game_state.buildings.clear()
game_state.extractors.clear()
scene = load("res://world/Main.tscn").instantiate()
root.add_child(scene)
field = scene.get_node("BuildingField")
func _process(delta: float) -> bool:
elapsed += delta
match stage:
0:
scene.get_node("AsteroidField").set_process(false)
scene.get_node("EnemySpawner").set_process(false)
field.place(GameState.BUILDING_SHIP_CRAFTER, Vector2(600.0, 0.0))
field.place(GameState.BUILDING_MOTHERSHIP, Vector2(-600.0, 0.0))
field.place(GameState.BUILDING_COMMAND_CENTER, Vector2(0.0, 600.0))
elapsed = 0.0
stage = 1
return false
1:
if elapsed < 0.3:
return false
for b in field.spawned:
if not is_instance_valid(b):
continue
match b.building_type:
GameState.BUILDING_SHIP_CRAFTER:
crafter = b.visual
GameState.BUILDING_MOTHERSHIP:
mothership = b.visual
GameState.BUILDING_COMMAND_CENTER:
command = b.visual
if crafter == null or mothership == null or command == null:
print("TEST_BUILDING_REDRAW_COST: FAIL (buildings never spawned)")
quit(1)
return true
# #117: an idle crafter holds its clock, so there would be no
# animation to measure the cost of. The point of this test is
# that animating is CHEAP, not that it happens unprompted.
game_state.queue_order(int(_crafter_id(field)), GameState.VESSEL_INTERCEPTOR)
crane_x_start = crafter._crane.position.x
satellite_rotation_start = mothership._satellites.rotation
elapsed = 0.0
stage = 2
return false
2:
# Long enough to cross several assembly phases and a good arc of
# the satellites' orbit.
if elapsed < 2.5:
return false
print("rasterisations after %.1fs of animation - crafter:%d mothership:%d command:%d" % [
elapsed, crafter.draw_count, mothership.draw_count, command.draw_count
])
for b in [crafter, mothership, command]:
if b.draw_count != 1:
print("TEST_BUILDING_REDRAW_COST: FAIL (%s redrew its geometry %d times)" % [
b.name, b.draw_count
])
quit(1)
return true
var animating: bool = crafter.anim_time > 0.0 and mothership.phase_time > 0.0
var crane_moved: bool = absf(crafter._crane.position.x - crane_x_start) > 1.0
var orbit_moved: bool = absf(mothership._satellites.rotation - satellite_rotation_start) > 0.05
print("still animating=%s crane travelled=%s satellites orbited=%s" % [
animating, crane_moved, orbit_moved
])
if not (animating and crane_moved and orbit_moved):
print("TEST_BUILDING_REDRAW_COST: FAIL (nothing actually moved - not a fix)")
quit(1)
return true
print("TEST_BUILDING_REDRAW_COST: PASS")
quit(0)
return true
return false
func _crafter_id(building_field: Node) -> int:
for b in building_field.spawned:
if is_instance_valid(b) and b.building_type == GameState.BUILDING_SHIP_CRAFTER:
return int(b.building_id)
return 0