Files
tidewall/test/sim/test_inventory.gd
T
claudeandClaude Opus 5.5 e58d6140cc B5: Inventory and pickups
Five slots plus the always-carried boathook (selection 0), ammunition carried
apart from the slots, and items lying in the world as pickup entities. All
changes are made by the server from commands: interact picks up the nearest
pickup in reach, drop (new BUTTON_DROP) leaves the held stack at the feet, and
both act once per press. A full inventory swaps the held item. Consumables and
thrown weapons stack; ammunition stops at its cap and leaves the rest on the
ground. An eliminated player drops everything on a ring around where they fell,
and the simulation now emits an "eliminated" event when health reaches 0.

Item definitions are ItemDef resources built by ItemCatalog from the tables in
Tuning (WEAPONS, ITEMS and the new AMMO), so a new item is a data change only.

Closes #19

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 17:19:45 -05:00

243 lines
9.1 KiB
GDScript

extends TestCase
## Inventory and pickups from issue B5: slots, stacking, ammunition caps, picking up,
## swapping, dropping, and dropping everything on elimination. All through commands, so
## the server's own checks are what's tested.
const FEET: Vector3 = Vector3(100.0, 0.0, 100.0)
func _sim() -> Simulation:
var sim: Simulation = Simulation.new(11, SimGround.flat())
var id: int = sim.add_player()
sim.entities.find(id).position = FEET
return sim
func _code(id: String) -> int:
var code: int = ItemCatalog.code_of(id)
assert_true(code >= 0, "%s should be in the catalog" % id)
return code
## Runs one tick with the given buttons and selection.
func _press(sim: Simulation, buttons: int, selection: int = -1, player_id: int = 1) -> void:
var player: SimEntity = sim.entities.find(player_id)
var chosen: int = player.selected if selection < 0 else selection
sim.submit_command(player_id, PlayerCommand.from_input(sim.tick, Vector2.ZERO, 0.0, 0.0, buttons, chosen))
sim.step()
## Presses interact once: a press on one tick and a release on the next.
func _interact(sim: Simulation) -> void:
_press(sim, PlayerCommand.BUTTON_INTERACT)
_press(sim, 0)
func test_catalog_codes_are_stable_and_every_table_entry_is_present() -> void:
var expected: int = Tuning.WEAPONS.size() - 1 + Tuning.ITEMS.size() + Tuning.AMMO.size()
assert_eq(ItemCatalog.count(), expected)
for index: int in ItemCatalog.count():
var def: ItemDef = ItemCatalog.get_def(index)
assert_eq(def.code, index)
assert_eq(ItemCatalog.code_of(def.id), index)
assert_true(def is Resource, "definitions are resources")
assert_eq(ItemCatalog.code_of("boathook"), ItemCatalog.NONE, "the boathook is never a slot item")
assert_null(ItemCatalog.get_def(-1))
assert_null(ItemCatalog.get_def(ItemCatalog.count()))
func test_weapons_name_real_ammunition() -> void:
for def: ItemDef in ItemCatalog.all():
if def.uses_magazine():
var ammo: ItemDef = ItemCatalog.get_def(def.ammo_code)
assert_not_null(ammo, "%s needs ammunition" % def.id)
assert_eq(ammo.kind, ItemDef.AMMO)
func test_new_players_carry_only_the_boathook() -> void:
var sim: Simulation = _sim()
var player: SimEntity = sim.entities.find(1)
assert_eq(player.selected, 0)
assert_eq(player.slot_items.size(), Tuning.INVENTORY_SLOTS)
for slot: int in Tuning.INVENTORY_SLOTS:
assert_eq(player.slot_items[slot], Inventory.EMPTY)
for amount: int in player.ammo:
assert_eq(amount, 0)
func test_picking_up_a_weapon_fills_a_slot_with_a_full_magazine() -> void:
var sim: Simulation = _sim()
var carbine: int = _code("dock_carbine")
var pickup: int = Inventory.spawn_pickup(sim, FEET + Vector3(1.0, 0.0, 0.0), carbine, 1)
_interact(sim)
var player: SimEntity = sim.entities.find(1)
assert_eq(player.slot_items[0], carbine)
assert_eq(player.slot_rounds[0], 30)
assert_false(sim.entities.contains(pickup), "the pickup is gone")
assert_eq(sim.pickups.size(), 0)
func test_pickups_out_of_reach_are_ignored() -> void:
var sim: Simulation = _sim()
var pickup: int = Inventory.spawn_pickup(sim, FEET + Vector3(Tuning.PICKUP_RANGE + 0.5, 0.0, 0.0), _code("dock_carbine"), 1)
_interact(sim)
assert_eq(sim.entities.find(1).slot_items[0], Inventory.EMPTY)
assert_true(sim.entities.contains(pickup))
func test_the_nearest_pickup_is_taken_first() -> void:
var sim: Simulation = _sim()
Inventory.spawn_pickup(sim, FEET + Vector3(2.0, 0.0, 0.0), _code("dock_carbine"), 1)
Inventory.spawn_pickup(sim, FEET + Vector3(0.5, 0.0, 0.0), _code("signal_revolver"), 1)
_interact(sim)
assert_eq(sim.entities.find(1).slot_items[0], _code("signal_revolver"))
func test_holding_interact_takes_one_item_per_press() -> void:
var sim: Simulation = _sim()
for _i: int in 3:
Inventory.spawn_pickup(sim, FEET, _code("dock_carbine"), 1)
for _i: int in 10:
_press(sim, PlayerCommand.BUTTON_INTERACT)
assert_eq(sim.pickups.size(), 2, "only the first tick of a held press acts")
func test_consumables_stack_up_to_their_limit_then_take_a_new_slot() -> void:
var sim: Simulation = _sim()
var player: SimEntity = sim.entities.find(1)
var bandage: int = _code("sailcloth_bandage")
var stack: int = ItemCatalog.get_def(bandage).stack
assert_eq(Inventory.give(player, bandage, stack - 2), 0)
assert_eq(Inventory.give(player, bandage, 5), 0)
assert_eq(player.slot_counts[0], stack, "the first stack is topped up")
assert_eq(player.slot_counts[1], 3, "the rest starts a new stack")
assert_eq(Inventory.count_of(player, bandage), stack + 3)
func test_ammunition_stops_at_its_cap_and_leaves_the_rest_on_the_ground() -> void:
var sim: Simulation = _sim()
var player: SimEntity = sim.entities.find(1)
var shot: int = _code("birdshot")
var cap: int = int(Tuning.AMMO["birdshot"]["cap"])
assert_eq(Inventory.give(player, shot, cap - 5), 0)
var pickup: int = Inventory.spawn_pickup(sim, FEET, shot, 12)
_interact(sim)
assert_eq(Inventory.count_of(player, shot), cap)
assert_true(sim.entities.contains(pickup), "what didn't fit stays")
assert_eq(sim.entities.find(pickup).item_count, 7)
for slot: int in Tuning.INVENTORY_SLOTS:
assert_eq(player.slot_items[slot], Inventory.EMPTY, "ammunition never takes a slot")
func test_a_full_inventory_swaps_the_held_item() -> void:
var sim: Simulation = _sim()
var player: SimEntity = sim.entities.find(1)
var revolver: int = _code("signal_revolver")
for _i: int in Tuning.INVENTORY_SLOTS:
Inventory.give(player, revolver, 1)
player.slot_rounds[2] = 4
var longgun: int = _code("whalers_longgun")
Inventory.spawn_pickup(sim, FEET + Vector3(0.0, 0.0, 1.0), longgun, 1)
_press(sim, 0, 3)
_interact(sim)
assert_eq(player.slot_items[2], longgun, "the held slot takes the new item")
assert_eq(sim.pickups.size(), 1, "the old item is left behind")
var left: SimEntity = sim.entities.find(sim.pickups[0])
assert_eq(left.item, revolver)
assert_eq(left.item_rounds, 4, "a dropped weapon keeps its loaded rounds")
assert_near(left.position.z, FEET.z + 1.0, 0.001, "left where the pickup was")
func test_a_full_inventory_holding_the_boathook_takes_nothing() -> void:
var sim: Simulation = _sim()
var player: SimEntity = sim.entities.find(1)
for _i: int in Tuning.INVENTORY_SLOTS:
Inventory.give(player, _code("signal_revolver"), 1)
var pickup: int = Inventory.spawn_pickup(sim, FEET, _code("dock_carbine"), 1)
_interact(sim)
assert_true(sim.entities.contains(pickup))
assert_eq(Inventory.count_of(player, _code("dock_carbine")), 0)
func test_dropping_the_held_slot_and_picking_it_back_up_round_trips() -> void:
var sim: Simulation = _sim()
var player: SimEntity = sim.entities.find(1)
var bandage: int = _code("sailcloth_bandage")
Inventory.give(player, bandage, 4)
_press(sim, 0, 1)
_press(sim, PlayerCommand.BUTTON_DROP)
assert_eq(player.slot_items[0], Inventory.EMPTY)
assert_eq(sim.pickups.size(), 1)
assert_eq(sim.entities.find(sim.pickups[0]).item_count, 4)
_press(sim, 0)
_interact(sim)
assert_eq(Inventory.count_of(player, bandage), 4)
assert_eq(sim.pickups.size(), 0)
func test_dropping_the_boathook_does_nothing() -> void:
var sim: Simulation = _sim()
_press(sim, PlayerCommand.BUTTON_DROP, 0)
assert_eq(sim.pickups.size(), 0)
func test_selection_outside_the_slots_is_ignored() -> void:
var sim: Simulation = _sim()
_press(sim, 0, 2)
_press(sim, 0, Tuning.INVENTORY_SLOTS + 1)
assert_eq(sim.entities.find(1).selected, 2)
func test_swimmers_and_passengers_cannot_pick_up() -> void:
var sim: Simulation = _sim()
var player: SimEntity = sim.entities.find(1)
var pickup: int = Inventory.spawn_pickup(sim, FEET, _code("dock_carbine"), 1)
sim.set_sea_level(5.0)
_press(sim, 0)
assert_true(player.swimming)
_interact(sim)
assert_true(sim.entities.contains(pickup))
func test_elimination_drops_everything_carried() -> void:
var sim: Simulation = _sim()
var player: SimEntity = sim.entities.find(1)
Inventory.give(player, _code("dock_carbine"), 1)
Inventory.give(player, _code("sailcloth_bandage"), 3)
Inventory.give(player, _code("cartridges"), 45)
player.health = 0
_press(sim, 0)
assert_true(player.eliminated)
assert_eq(sim.pickups.size(), 3, "two slots and one ammunition type")
var total_cartridges: int = 0
for pickup_id: int in sim.pickups:
var pickup: SimEntity = sim.entities.find(pickup_id)
assert_near(pickup.position.distance_to(FEET), Tuning.DROP_SCATTER_METRES, 0.01)
if pickup.item == _code("cartridges"):
total_cartridges += pickup.item_count
assert_eq(total_cartridges, 45)
assert_eq(Inventory.count_of(player, _code("dock_carbine")), 0)
var eliminated: int = 0
for event: Dictionary in sim.events:
if event["type"] == "eliminated":
eliminated += 1
assert_eq(eliminated, 1)
_press(sim, 0)
assert_eq(sim.pickups.size(), 3, "nothing is dropped twice")
func test_inventory_state_is_in_the_hash() -> void:
var a: Simulation = _sim()
var b: Simulation = _sim()
assert_eq(a.state_hash(), b.state_hash())
Inventory.give(b.entities.find(1), _code("cartridges"), 1)
assert_ne(a.state_hash(), b.state_hash())
func test_pickups_rest_on_a_floor_above_the_ground() -> void:
var sim: Simulation = _sim()
sim.ground.add_solid(AABB(Vector3(96.0, 0.0, 96.0), Vector3(8.0, 3.0, 8.0)))
var pickup: int = Inventory.spawn_pickup(sim, Vector3(100.0, 3.2, 100.0), _code("dock_carbine"), 1)
assert_near(sim.entities.find(pickup).position.y, 3.0, 0.001)