spaceship/tests/test_gas_bomber_ammo.gd
Loïc Wisniewski 17d0cf7e66 Gas bombers consume gas from the fleet hold when firing (#60)
Shots cost GAS_PER_SHOT (5) from GameState.fleet_inventory, not the
bank - the fleet has to be carrying its own ammunition. The button's
enabled rule now also requires affordability, and a press on a ready-
but-unaffordable bomber still consumes the touch to explain itself via
a new status label (GasBombStatus, IconButton's own "greyed but still
pressable" convention) rather than either silently doing nothing or
consuming the bomber's cooldown for a shot that never fires.

Also documents a real, reproducible GDScript trap hit while writing
tests/test_gas_bomber_ammo.gd: a bare `ClassName.SOME_CONST` reference
anywhere in a -s entry test script can corrupt method resolution for
the WHOLE script, making an unrelated, earlier `some_node._process()`
call fail with "Nonexistent function '_process' in base 'Nil'" despite
the node being valid and the method existing (confirmed via
has_method()). Root cause unconfirmed; fixed by mirroring the constant
's value into a local const instead of referencing the class directly,
in all three gas-bomber tests.

tests/test_gas_bomber.gd and test_gas_bomber_cooldowns.gd updated to
stock fleet gas first, since shots are no longer free.
2026-08-16 18:54:44 +02:00

142 lines
5.3 KiB
GDScript

extends SceneTree
# Headless gameplay test: run with
# godot --headless -s res://tests/test_gas_bomber_ammo.gd
# Forgejo #60: gas bomb shots draw ammunition from the FLEET HOLD
# (GameState.fleet_inventory), not the bank - the fleet has to be carrying
# its own gas.
#
# Covers:
# A. gas sitting in the bank does not enable firing - only the fleet
# hold counts
# B. with insufficient fleet gas the button is disabled, pressing it
# explains why, and no projectile is created / no cooldown consumed
# C. firing drains exactly GAS_PER_SHOT from the fleet hold and nothing
# from the bank
var game_state: Node
var main: Node
var fleet: Node
var skill: Node
var bomber_component: Node
var stage: int = 0
var elapsed: float = 0.0
# Mirrors GasBombController.GAS_PER_SHOT's value rather than referencing
# the class constant directly - a bare ClassName.CONST reference elsewhere
# in a test
# script has been observed to corrupt method resolution for the WHOLE
# script (an unrelated skill._process() call earlier in the same file then
# fails with "Nonexistent function '_process' in base 'Nil'"), root cause
# unconfirmed. See tests/test_gas_bomber_cooldowns.gd's identical fix.
const GAS_PER_SHOT: int = 5
func _initialize() -> void:
game_state = root.get_node("GameState")
game_state.buildings.clear()
game_state.extractors.clear()
game_state.transporters.clear()
game_state.stored_vessels.clear()
game_state.reset_progression()
game_state.vessel_levels["unlock_gas_bomber"] = 1
game_state.rocks = 0
game_state.gas = 0
game_state.fleet_gas_cargo = 0
main = load("res://Main.tscn").instantiate()
root.add_child(main)
fleet = main.get_node("Fleet")
skill = main.get_node("GasBombController")
func _process(delta: float) -> bool:
elapsed += delta
match stage:
0:
if elapsed < 0.2:
return false
main.get_node("EnemySpawner").set_process(false)
main.get_node("AsteroidField").set_process(false)
# An interceptor rides along purely for its "cargo" capability
# (#56): a fleet made only of a gas bomber has zero cargo
# capacity, which would silently clamp every fleet_gas_cargo
# assignment below to 0.
fleet.spawn_ships(2, Vector2.ZERO, PackedStringArray([
GameState.VESSEL_INTERCEPTOR, GameState.VESSEL_GAS_BOMBER]))
bomber_component = fleet.ships[1].get_component("gas_bomber")
skill._process(0.0)
# --- A. bank gas does not count -----------------------------------
game_state.gas = 500
game_state.fleet_gas_cargo = 0
skill._process(0.0)
if skill.button.enabled:
return _fail_now("500 banked gas (0 in the fleet hold) left the control enabled")
print("500 banked gas, 0 fleet gas: control correctly disabled")
# --- B. insufficient fleet gas --------------------------------------
game_state.fleet_gas_cargo = GAS_PER_SHOT - 1
skill._process(0.0)
if skill.button.enabled:
return _fail_now("fleet gas one short of GAS_PER_SHOT left the control enabled")
var press := InputEventScreenTouch.new()
press.index = 3
press.position = skill.button.global_position + skill.button.size * 0.5
press.pressed = true
main._handle_touch(press)
if skill.aiming:
return _fail_now("aiming began despite insufficient fleet gas")
if skill.status.text == "":
return _fail_now("an unaffordable press did not explain itself")
print("insufficient fleet gas: disabled, press explains itself, no aiming began")
if not bomber_component.is_ready():
return _fail_now("a refused press consumed the bomber's cooldown")
if game_state.fleet_resource(GameState.RESOURCE_GAS) != GAS_PER_SHOT - 1:
return _fail_now("a refused press spent fleet gas anyway")
print("no cooldown consumed, no gas spent on a refused press")
# --- C. exact ammo cost on a real shot ------------------------------
game_state.fleet_gas_cargo = GAS_PER_SHOT + 7
skill._process(0.0)
if not skill.button.enabled:
return _fail_now("enough fleet gas still left the control disabled")
var press2 := InputEventScreenTouch.new()
press2.index = 4
press2.position = skill.button.global_position + skill.button.size * 0.5
press2.pressed = true
main._handle_touch(press2)
if not skill.aiming:
return _fail_now("an affordable press did not begin aiming")
var drag := InputEventScreenDrag.new()
drag.index = 4
drag.position = press2.position + Vector2(-300.0, 0.0)
main._handle_drag(drag)
var release := InputEventScreenTouch.new()
release.index = 4
release.position = drag.position
release.pressed = false
main._handle_touch(release)
if skill.projectiles.size() != 1:
return _fail_now("a paid-for shot did not launch a projectile")
if bomber_component.is_ready():
return _fail_now("firing did not consume the bomber's cooldown")
if game_state.fleet_resource(GameState.RESOURCE_GAS) != 7:
return _fail_now("fleet gas after firing is %d, expected 7 (paid exactly %d)" \
% [game_state.fleet_resource(GameState.RESOURCE_GAS), GAS_PER_SHOT])
if game_state.gas != 500:
return _fail_now("firing touched the bank (%d), expected it untouched at 500" % game_state.gas)
print("firing drained exactly %d from the fleet hold, bank untouched" % GAS_PER_SHOT)
print("TEST_GAS_BOMBER_AMMO: PASS")
quit(0)
return true
return false
func _fail_now(message: String) -> bool:
print("TEST_GAS_BOMBER_AMMO: FAIL (%s)" % message)
quit(1)
return true