#99 (#97a): buildings have health, and can be destroyed
Nothing can attack a structure that cannot be hurt, so this is the floor the rest of #97 stands on. max_hp is per definition - an extractor bolted to a rock is not the mothership - and the current value lives on the RECORD in GameState, not on the Building node. Main.tscn is torn down and rebuilt on every trip to a planet surface, so damage kept on a node would heal itself the moment the player landed. A record written before this carries no hp field at all. That reads as undamaged rather than as zero, or an existing save would open on a field of wrecks. The mothership cannot be destroyed - it floors at 1hp. The bank, the hangar and research all live there and a wiped fleet rebuilds there, so losing it would be a soft-lock with no route back. Keyed on the "bank" capability rather than on the type: whatever building holds the bank is the one that cannot be lost. Destroying a hub retires its transporters. Everything else keyed on a building id already copes with the building being gone - a transporter mid-delivery puts its cargo back rather than destroying it. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
88b53abf52
commit
c221584848
12 changed files with 293 additions and 0 deletions
195
tests/test_building_health.gd
Normal file
195
tests/test_building_health.gd
Normal file
|
|
@ -0,0 +1,195 @@
|
|||
extends SceneTree
|
||||
|
||||
# Headless test: run with
|
||||
# godot --headless -s res://tests/test_building_health.gd
|
||||
# Forgejo #99 (#97a): structures can be hurt, and can come down.
|
||||
#
|
||||
# Health lives on the RECORD in GameState rather than on the Building node,
|
||||
# because Main.tscn is destroyed and rebuilt on every trip to a planet
|
||||
# surface - damage kept on a node would heal itself the moment the player
|
||||
# landed. Most of this test therefore talks to GameState directly, and only
|
||||
# the last case looks at what the scene does with it.
|
||||
#
|
||||
# Covers:
|
||||
# A. every building definition declares a positive max_hp, and a fresh
|
||||
# record starts at full health
|
||||
# B. damage subtracts, reports survival, and enough of it destroys the
|
||||
# record outright
|
||||
# C. a record written before this existed reads as undamaged, not as
|
||||
# rubble - an existing save must not open on a field of wrecks
|
||||
# D. the mothership cannot be destroyed. The bank, the hangar and research
|
||||
# all live there and a wiped fleet rebuilds there, so losing it would
|
||||
# be a soft-lock; it bottoms out at 1hp instead
|
||||
# E. destroying a transport hub takes its transporters with it, and
|
||||
# nothing else keyed on the id is left pointing at a hole
|
||||
# F. the Building node follows its record out of existence, and shows
|
||||
# damage while it is still standing
|
||||
|
||||
var game_state: Node
|
||||
var scene: Node
|
||||
var stage: int = 0
|
||||
var elapsed: float = 0.0
|
||||
var destroyed_events: Array = []
|
||||
|
||||
|
||||
func _initialize() -> void:
|
||||
# GameState is an autoload: a bare GameState.<instance member> does not
|
||||
# compile in a SceneTree script, so the node is fetched by name instead.
|
||||
game_state = root.get_node("GameState")
|
||||
game_state.buildings.clear()
|
||||
game_state.extractors.clear()
|
||||
game_state.transporters.clear()
|
||||
game_state.reset_progression()
|
||||
game_state.rocks = 5000
|
||||
|
||||
|
||||
func _fail(message: String) -> bool:
|
||||
print("TEST_BUILDING_HEALTH: FAIL (%s)" % message)
|
||||
quit(1)
|
||||
return true
|
||||
|
||||
|
||||
func _run_state_cases() -> bool:
|
||||
var definitions: Script = load("res://buildings/BuildingDefinitions.gd")
|
||||
|
||||
# --- A. every structure declares what it can take -------------------
|
||||
for definition in definitions.all():
|
||||
if int(definition.max_hp) <= 0:
|
||||
return _fail("'%s' has max_hp %d - it would be rubble the moment it was placed" \
|
||||
% [definition.definition_id, definition.max_hp])
|
||||
var crafter_id: int = game_state.add_building(GameState.BUILDING_SHIP_CRAFTER, Vector2(300.0, 0.0))
|
||||
var full: int = game_state.building_max_hp(GameState.BUILDING_SHIP_CRAFTER)
|
||||
if game_state.building_hp(crafter_id) != full:
|
||||
return _fail("a new crafter opened at %d of %d hp" \
|
||||
% [game_state.building_hp(crafter_id), full])
|
||||
if not is_equal_approx(game_state.building_health_fraction(crafter_id), 1.0):
|
||||
return _fail("a new crafter is not at full health")
|
||||
print("%d definitions all declare max_hp; a new crafter starts at %d" \
|
||||
% [definitions.all().size(), full])
|
||||
|
||||
# --- B. damage, then death ------------------------------------------
|
||||
game_state.building_destroyed.connect(func(id: int, type: String):
|
||||
destroyed_events.append({"id": id, "type": type}))
|
||||
if game_state.damage_building(crafter_id, full - 1):
|
||||
return _fail("one hit point short of dead reported the crafter destroyed")
|
||||
if game_state.building_hp(crafter_id) != 1:
|
||||
return _fail("hp is %d after taking max_hp - 1" % game_state.building_hp(crafter_id))
|
||||
if game_state.building_health_fraction(crafter_id) >= 0.5:
|
||||
return _fail("a crafter on its last hit point still reads as healthy")
|
||||
if not game_state.damage_building(crafter_id, 1):
|
||||
return _fail("the last hit point did not destroy the crafter")
|
||||
if not game_state.find_building(crafter_id).is_empty():
|
||||
return _fail("the destroyed crafter is still in GameState.buildings")
|
||||
if destroyed_events.size() != 1 or int(destroyed_events[0].id) != crafter_id:
|
||||
return _fail("building_destroyed did not fire once for the crafter")
|
||||
# Fired AFTER the record went, so a listener sees the world without it.
|
||||
print("a crafter takes %d damage and comes down, announcing itself" % full)
|
||||
|
||||
# --- C. a record from before this existed ---------------------------
|
||||
var legacy_id: int = game_state.add_building(GameState.BUILDING_SHIP_LAB, Vector2(-300.0, 0.0))
|
||||
game_state.find_building(legacy_id).erase("hp")
|
||||
var lab_full: int = game_state.building_max_hp(GameState.BUILDING_SHIP_LAB)
|
||||
if game_state.building_hp(legacy_id) != lab_full:
|
||||
return _fail("a record with no hp field reads as %d, not the %d it should" \
|
||||
% [game_state.building_hp(legacy_id), lab_full])
|
||||
if game_state.damage_building(legacy_id, 1):
|
||||
return _fail("one point of damage levelled a building carried over from an old save")
|
||||
print("a pre-#99 record reads as undamaged (%d hp), not as rubble" % lab_full)
|
||||
|
||||
# --- D. the mothership survives anything ----------------------------
|
||||
var mothership_id: int = game_state.add_building(GameState.BUILDING_MOTHERSHIP, Vector2.ZERO)
|
||||
if not game_state.building_is_indestructible(game_state.find_building(mothership_id)):
|
||||
return _fail("the mothership is not marked indestructible")
|
||||
if game_state.damage_building(mothership_id, 999999):
|
||||
return _fail("the mothership was destroyed - the bank, hangar and research went with it")
|
||||
if game_state.building_hp(mothership_id) != 1:
|
||||
return _fail("an overwhelmed mothership sits at %d hp, expected to floor at 1" \
|
||||
% game_state.building_hp(mothership_id))
|
||||
if game_state.destroy_building(mothership_id):
|
||||
return _fail("destroy_building demolished the mothership outright")
|
||||
if game_state.repair_building(mothership_id, 999999) <= 0:
|
||||
return _fail("a battered mothership could not be repaired")
|
||||
if game_state.building_hp(mothership_id) != game_state.building_max_hp(GameState.BUILDING_MOTHERSHIP):
|
||||
return _fail("repair did not take the mothership back to full")
|
||||
print("the mothership floors at 1hp and repairs to full")
|
||||
|
||||
# --- E. a hub takes its transporters with it ------------------------
|
||||
var hub_id: int = game_state.add_building(GameState.BUILDING_TRANSPORT_HUB, Vector2(800.0, 0.0))
|
||||
var hub_transporters: int = 0
|
||||
for transporter in game_state.transporters:
|
||||
if int(transporter.get("hub_id", 0)) == hub_id:
|
||||
hub_transporters += 1
|
||||
if hub_transporters <= 0:
|
||||
return _fail("the hub provisioned no transporters, so this case proves nothing")
|
||||
var others: int = game_state.transporters.size() - hub_transporters
|
||||
if not game_state.destroy_building(hub_id):
|
||||
return _fail("the hub could not be destroyed")
|
||||
for transporter in game_state.transporters:
|
||||
if int(transporter.get("hub_id", 0)) == hub_id:
|
||||
return _fail("a transporter is still flying for a hub that no longer exists")
|
||||
if game_state.transporters.size() != others:
|
||||
return _fail("destroying the hub took %d unrelated transporters with it" \
|
||||
% (others - game_state.transporters.size()))
|
||||
print("destroying a hub retires its %d transporters and no others" % hub_transporters)
|
||||
return false
|
||||
|
||||
|
||||
func _process(delta: float) -> bool:
|
||||
elapsed += delta
|
||||
match stage:
|
||||
0:
|
||||
if _run_state_cases():
|
||||
return true
|
||||
# --- F. what the scene does with it -------------------------
|
||||
game_state.buildings.clear()
|
||||
game_state.transporters.clear()
|
||||
scene = load("res://world/Main.tscn").instantiate()
|
||||
root.add_child(scene)
|
||||
elapsed = 0.0
|
||||
stage = 1
|
||||
return false
|
||||
1:
|
||||
if elapsed < 0.3:
|
||||
return false
|
||||
scene.get_node("AsteroidField").set_process(false)
|
||||
scene.get_node("EnemySpawner").set_process(false)
|
||||
var field: Node = scene.get_node("BuildingField")
|
||||
field.place(GameState.BUILDING_SHIP_CRAFTER, Vector2(600.0, 0.0))
|
||||
elapsed = 0.0
|
||||
stage = 2
|
||||
return false
|
||||
2:
|
||||
if elapsed < 0.2:
|
||||
return false
|
||||
var field: Node = scene.get_node("BuildingField")
|
||||
var node: Node2D = null
|
||||
for b in field.spawned:
|
||||
if is_instance_valid(b) and b.building_type == GameState.BUILDING_SHIP_CRAFTER:
|
||||
node = b
|
||||
if node == null:
|
||||
return _fail("no crafter node was spawned")
|
||||
if node.is_damaged():
|
||||
return _fail("a freshly placed crafter already shows damage")
|
||||
game_state.damage_building(node.building_id, game_state.building_max_hp(
|
||||
GameState.BUILDING_SHIP_CRAFTER) / 2)
|
||||
node._process(0.0)
|
||||
if not node.is_damaged():
|
||||
return _fail("a crafter at half health does not show it")
|
||||
print("a half-wrecked crafter reads %.0f%%" % (node.health_fraction() * 100.0))
|
||||
game_state.damage_building(node.building_id, 999999)
|
||||
elapsed = 0.0
|
||||
stage = 3
|
||||
return false
|
||||
3:
|
||||
if elapsed < 0.2:
|
||||
return false
|
||||
var field: Node = scene.get_node("BuildingField")
|
||||
for b in field.spawned:
|
||||
if is_instance_valid(b) and b.building_type == GameState.BUILDING_SHIP_CRAFTER:
|
||||
return _fail("the crafter node outlived the record it was built from")
|
||||
print("the node follows its record out of existence")
|
||||
|
||||
print("TEST_BUILDING_HEALTH: PASS")
|
||||
quit(0)
|
||||
return true
|
||||
return false
|
||||
Loading…
Reference in a new issue