From e58d6140ccdb2aa13fc72ece4c25a81991379c23 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Nov 2026 17:19:45 -0500 Subject: [PATCH] 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 --- src/sim/inventory.gd | 245 +++++++++++++++++++++++++++++++++ src/sim/inventory.gd.uid | 1 + src/sim/item_catalog.gd | 105 ++++++++++++++ src/sim/item_catalog.gd.uid | 1 + src/sim/item_def.gd | 77 +++++++++++ src/sim/item_def.gd.uid | 1 + src/sim/player_command.gd | 2 + src/sim/sim_entity.gd | 35 +++++ src/sim/simulation.gd | 47 ++++++- src/sim/tuning.gd | 46 ++++++- test/sim/test_inventory.gd | 242 ++++++++++++++++++++++++++++++++ test/sim/test_inventory.gd.uid | 1 + 12 files changed, 800 insertions(+), 3 deletions(-) create mode 100644 src/sim/inventory.gd create mode 100644 src/sim/inventory.gd.uid create mode 100644 src/sim/item_catalog.gd create mode 100644 src/sim/item_catalog.gd.uid create mode 100644 src/sim/item_def.gd create mode 100644 src/sim/item_def.gd.uid create mode 100644 test/sim/test_inventory.gd create mode 100644 test/sim/test_inventory.gd.uid diff --git a/src/sim/inventory.gd b/src/sim/inventory.gd new file mode 100644 index 0000000..4c56072 --- /dev/null +++ b/src/sim/inventory.gd @@ -0,0 +1,245 @@ +## A player's five slots and carried ammunition, and items lying in the world as +## pickups. Every change happens on the server: a client only presses interact, drop +## or a slot number, and the server checks range, room and stacking itself. +## +## Selection 0 is the boathook, always carried. Selections 1 to Tuning.INVENTORY_SLOTS +## are slots. Weapons take a whole slot. Consumables and thrown weapons stack up to +## their ItemDef.stack. Ammunition doesn't use slots: each type has its own stock up +## to its cap. +## +## Interact picks up the nearest pickup within Tuning.PICKUP_RANGE of the feet. If +## there's no room and a slot item is held, the held item is swapped for it and left +## where the pickup was. Drop puts the held slot's item on the ground. An eliminated +## player drops everything, slots and ammunition, on a ring around where they fell. +class_name Inventory +extends RefCounted + +const EMPTY: int = -1 + + +static func slot_count() -> int: + return Tuning.INVENTORY_SLOTS + + +## Empties a player's slots and ammunition. +static func reset(player: SimEntity) -> void: + var slots: int = slot_count() + for array: PackedInt32Array in [player.slot_items, player.slot_counts, player.slot_rounds, player.slot_tiers]: + array.resize(slots) + player.slot_items.fill(EMPTY) + player.slot_counts.fill(0) + player.slot_rounds.fill(0) + player.slot_tiers.fill(0) + player.ammo.resize(Tuning.AMMO.size()) + player.ammo.fill(0) + player.selected = 0 + player.reload_ticks = 0 + + +## Index into SimEntity.ammo for an ammunition item code, or -1. +static func ammo_index(code: int) -> int: + var def: ItemDef = ItemCatalog.get_def(code) + if def == null or def.kind != ItemDef.AMMO: + return -1 + return Tuning.AMMO.keys().find(def.id) + + +## The ItemCatalog code of the ammunition in SimEntity.ammo[index]. +static func ammo_code(index: int) -> int: + return ItemCatalog.code_of(Tuning.AMMO.keys()[index]) + + +## The held slot index (0-based), or -1 when holding the boathook. +static func held_slot(player: SimEntity) -> int: + return player.selected - 1 + + +## The definition of the held item, or null for the boathook or an empty slot. +static func held_def(player: SimEntity) -> ItemDef: + var slot: int = held_slot(player) + if slot < 0 or slot >= player.slot_items.size(): + return null + return ItemCatalog.get_def(player.slot_items[slot]) + + +## Changes the held selection. Out-of-range selections are ignored. A change cancels +## a reload and costs Tuning.SWAP_SECONDS before the new item can be used. +static func select(player: SimEntity, selection: int) -> void: + if selection < 0 or selection > slot_count() or selection == player.selected: + return + player.selected = selection + player.reload_ticks = 0 + player.action_cooldown = maxi(player.action_cooldown, roundi(Tuning.SWAP_SECONDS * Tuning.TICK_RATE_HZ)) + + +## Gives the player up to `count` of an item. `rounds` is the loaded magazine for a +## weapon, -1 for a full one. Returns how many didn't fit. +static func give(player: SimEntity, code: int, count: int, rounds: int = -1, tier: int = 0) -> int: + var def: ItemDef = ItemCatalog.get_def(code) + if def == null or count <= 0: + return maxi(count, 0) + if def.kind == ItemDef.AMMO: + var index: int = ammo_index(code) + var taken: int = mini(count, def.cap - player.ammo[index]) + taken = maxi(taken, 0) + player.ammo[index] += taken + return count - taken + var left: int = count + # Top up matching stacks first, then fill empty slots. + if def.stack > 1: + for slot: int in slot_count(): + if left == 0: + break + if player.slot_items[slot] == code and player.slot_tiers[slot] == tier: + var room: int = def.stack - player.slot_counts[slot] + var moved: int = mini(room, left) + player.slot_counts[slot] += moved + left -= moved + for slot: int in slot_count(): + if left == 0: + break + if player.slot_items[slot] == EMPTY: + var moved: int = mini(def.stack, left) + _fill(player, slot, code, moved, rounds, tier) + left -= moved + return left + + +## How many of an item the player carries, in slots or as ammunition. +static func count_of(player: SimEntity, code: int) -> int: + var index: int = ammo_index(code) + if index >= 0: + return player.ammo[index] + var total: int = 0 + for slot: int in slot_count(): + if player.slot_items[slot] == code: + total += player.slot_counts[slot] + return total + + +## Takes `count` from a slot, emptying it when nothing is left. +static func take_from_slot(player: SimEntity, slot: int, count: int = 1) -> void: + player.slot_counts[slot] = maxi(player.slot_counts[slot] - count, 0) + if player.slot_counts[slot] == 0: + _clear(player, slot) + + +## The nearest pickup within reach of the player's feet, or 0. Ties go to the lower ID. +static func nearest_pickup(sim: Simulation, player: SimEntity) -> int: + var best: int = 0 + var best_distance: float = Tuning.PICKUP_RANGE + for pickup_id: int in sim.pickups: + var pickup: SimEntity = sim.entities.find(pickup_id) + var distance: float = pickup.position.distance_to(player.position) + if distance <= best_distance and (best == 0 or distance < best_distance): + best = pickup_id + best_distance = distance + return best + + +## Picks up the nearest pickup in reach. Returns the pickup's ID, or 0 if nothing was +## taken. Appends "pickup" and "dropped" events. +static func interact(sim: Simulation, player: SimEntity) -> int: + var pickup_id: int = nearest_pickup(sim, player) + if pickup_id == 0: + return 0 + var pickup: SimEntity = sim.entities.find(pickup_id) + var def: ItemDef = ItemCatalog.get_def(pickup.item) + var left: int = give(player, pickup.item, pickup.item_count, pickup.item_rounds, pickup.item_tier) + var taken: int = pickup.item_count - left + if taken == 0 and def.kind != ItemDef.AMMO: + # No room: swap with the held slot, leaving the held item where the pickup was. + var slot: int = held_slot(player) + if slot < 0: + return 0 + var spot: Vector3 = pickup.position + var old: Array[int] = [player.slot_items[slot], player.slot_counts[slot], player.slot_rounds[slot], player.slot_tiers[slot]] + _clear(player, slot) + var moved: int = mini(def.stack, pickup.item_count) + _fill(player, slot, pickup.item, moved, pickup.item_rounds, pickup.item_tier) + player.reload_ticks = 0 + taken = moved + _take_from_pickup(sim, pickup, taken) + var dropped: int = spawn_pickup(sim, spot, old[0], old[1], old[2], old[3]) + sim.events.append({"type": "pickup", "player": player.id, "item": def.code, "count": taken}) + sim.events.append({"type": "dropped", "player": player.id, "pickup": dropped, "item": old[0], "count": old[1]}) + return pickup_id + if taken == 0: + return 0 + _take_from_pickup(sim, pickup, taken) + sim.events.append({"type": "pickup", "player": player.id, "item": def.code, "count": taken}) + return pickup_id + + +## Drops the held slot's whole stack at the player's feet. Returns the new pickup's ID, +## or 0 if the boathook or an empty slot is held. +static func drop_held(sim: Simulation, player: SimEntity) -> int: + var slot: int = held_slot(player) + if slot < 0 or player.slot_items[slot] == EMPTY: + return 0 + var id: int = spawn_pickup(sim, player.position, player.slot_items[slot], player.slot_counts[slot], player.slot_rounds[slot], player.slot_tiers[slot]) + sim.events.append({"type": "dropped", "player": player.id, "pickup": id, "item": player.slot_items[slot], "count": player.slot_counts[slot]}) + _clear(player, slot) + player.reload_ticks = 0 + return id + + +## Drops every slot and all ammunition on a ring around the player. Returns the new +## pickups' IDs. +static func drop_all(sim: Simulation, player: SimEntity) -> Array[int]: + var stacks: Array[Array] = [] + for slot: int in slot_count(): + if player.slot_items[slot] != EMPTY: + stacks.append([player.slot_items[slot], player.slot_counts[slot], player.slot_rounds[slot], player.slot_tiers[slot]]) + for index: int in player.ammo.size(): + if player.ammo[index] > 0: + stacks.append([ammo_code(index), player.ammo[index], 0, 0]) + var ids: Array[int] = [] + for index: int in stacks.size(): + var angle: float = TAU * index / stacks.size() + var spot: Vector3 = player.position + Vector3(sin(angle), 0.0, cos(angle)) * Tuning.DROP_SCATTER_METRES + var stack: Array = stacks[index] + ids.append(spawn_pickup(sim, spot, stack[0], stack[1], stack[2], stack[3])) + reset(player) + return ids + + +## Puts an item in the world, resting on whatever is under `spot`. Returns its ID. +static func spawn_pickup(sim: Simulation, spot: Vector3, code: int, count: int, rounds: int = -1, tier: int = 0) -> int: + var def: ItemDef = ItemCatalog.get_def(code) + var pickup: SimEntity = sim.entities.create(SimEntity.ARCHETYPE_PICKUP) + var x: float = clampf(spot.x, 0.0, Tuning.WORLD_SIZE_METRES) + var z: float = clampf(spot.z, 0.0, Tuning.WORLD_SIZE_METRES) + # Rest on the highest floor at or a little above the spot: terrain, a deck or a floor. + pickup.position = Vector3(x, sim.ground.support_height(x, z, spot.y + 0.5), z) + pickup.item = code + pickup.item_count = count + pickup.item_rounds = def.magazine if rounds < 0 and def.uses_magazine() else maxi(rounds, 0) + pickup.item_tier = tier + sim.pickups.append(pickup.id) + return pickup.id + + +static func _take_from_pickup(sim: Simulation, pickup: SimEntity, taken: int) -> void: + pickup.item_count -= taken + if pickup.item_count <= 0: + sim.entities.remove(pickup.id) + sim.pickups.erase(pickup.id) + + +static func _fill(player: SimEntity, slot: int, code: int, count: int, rounds: int, tier: int) -> void: + var def: ItemDef = ItemCatalog.get_def(code) + player.slot_items[slot] = code + player.slot_counts[slot] = count + if def.uses_magazine(): + player.slot_rounds[slot] = def.magazine if rounds < 0 else clampi(rounds, 0, def.magazine) + else: + player.slot_rounds[slot] = 0 + player.slot_tiers[slot] = tier + + +static func _clear(player: SimEntity, slot: int) -> void: + player.slot_items[slot] = EMPTY + player.slot_counts[slot] = 0 + player.slot_rounds[slot] = 0 + player.slot_tiers[slot] = 0 diff --git a/src/sim/inventory.gd.uid b/src/sim/inventory.gd.uid new file mode 100644 index 0000000..21c8a85 --- /dev/null +++ b/src/sim/inventory.gd.uid @@ -0,0 +1 @@ +uid://cadhqhw4s4o6g diff --git a/src/sim/item_catalog.gd b/src/sim/item_catalog.gd new file mode 100644 index 0000000..c180f8d --- /dev/null +++ b/src/sim/item_catalog.gd @@ -0,0 +1,105 @@ +## Every carryable item, built once from Tuning.WEAPONS, Tuning.ITEMS and Tuning.AMMO. +## Codes run weapons first (in table order, without the boathook, which is always +## carried and never in a slot), then consumables, then ammunition. +class_name ItemCatalog +extends RefCounted + +const NONE: int = -1 +## The weapon every player always holds in selection 0. Not an inventory item. +const BOATHOOK: String = "boathook" + +static var _defs: Array[ItemDef] = [] +static var _by_id: Dictionary = {} + + +static func all() -> Array[ItemDef]: + if _defs.is_empty(): + _build() + return _defs + + +static func count() -> int: + return all().size() + + +## The definition for a code, or null if the code is out of range. +static func get_def(code: int) -> ItemDef: + var defs: Array[ItemDef] = all() + if code < 0 or code >= defs.size(): + return null + return defs[code] + + +## The code for an item ID, or NONE. +static func code_of(id: String) -> int: + all() + return int(_by_id.get(id, NONE)) + + +## The boathook's stats, built the same way as any other weapon. +static func boathook() -> ItemDef: + return _weapon_def(BOATHOOK, Tuning.WEAPONS[BOATHOOK], NONE) + + +static func _build() -> void: + var ammo_ids: Array = Tuning.AMMO.keys() + var first_ammo: int = Tuning.WEAPONS.size() - 1 + Tuning.ITEMS.size() + for id: String in Tuning.WEAPONS: + if id == BOATHOOK: + continue + var stats: Dictionary = Tuning.WEAPONS[id] + var ammo_code: int = NONE + if stats.has("ammo"): + ammo_code = first_ammo + ammo_ids.find(stats["ammo"]) + _add(_weapon_def(id, stats, ammo_code)) + for id: String in Tuning.ITEMS: + var stats: Dictionary = Tuning.ITEMS[id] + var def: ItemDef = ItemDef.new() + def.id = id + def.kind = ItemDef.CONSUMABLE + def.stack = int(stats.get("stack", 1)) + def.heal = int(stats.get("heal", 0)) + def.armor = int(stats.get("armor", 0)) + def.use_seconds = float(stats.get("use_seconds", 0.0)) + _add(def) + for id: String in ammo_ids: + var stats: Dictionary = Tuning.AMMO[id] + var def: ItemDef = ItemDef.new() + def.id = id + def.kind = ItemDef.AMMO + def.cap = int(stats["cap"]) + def.pickup_amount = int(stats["pickup"]) + def.stack = def.cap + _add(def) + + +static func _add(def: ItemDef) -> void: + def.code = _defs.size() + _defs.append(def) + _by_id[def.id] = def.code + + +static func _weapon_def(id: String, stats: Dictionary, ammo_code: int) -> ItemDef: + var def: ItemDef = ItemDef.new() + def.id = id + def.kind = ItemDef.WEAPON + def.stack = int(stats.get("stack", 1)) + def.thrown = def.stack > 1 + def.damage = int(stats["damage"]) + def.pellets = int(stats.get("pellets", 1)) + def.fire_rate = float(stats["fire_rate"]) + def.magazine = int(stats["magazine"]) + def.reload_seconds = float(stats.get("reload", 0.0)) + def.range_full = float(stats["range_full"]) + def.range_max = float(stats["range_max"]) + def.falloff_min = float(stats["falloff_min"]) + def.rarity = int(stats["rarity"]) + def.spread_degrees = float(stats.get("spread", 0.0)) + def.aim_spread_factor = float(stats.get("aim_spread", Tuning.AIM_SPREAD_FACTOR)) + def.bloom_degrees = float(stats.get("bloom", 0.0)) + def.headshot_multiplier = float(stats.get("headshot", Tuning.HEADSHOT_MULTIPLIER)) + def.structure_damage = int(stats.get("structure_damage", 0)) + def.projectile_speed = float(stats.get("projectile_speed", 0.0)) + def.projectile_gravity = float(stats.get("projectile_gravity", 0.0)) + def.ammo_code = ammo_code + return def diff --git a/src/sim/item_catalog.gd.uid b/src/sim/item_catalog.gd.uid new file mode 100644 index 0000000..fa95eac --- /dev/null +++ b/src/sim/item_catalog.gd.uid @@ -0,0 +1 @@ +uid://qmcnsyagx5yo diff --git a/src/sim/item_def.gd b/src/sim/item_def.gd new file mode 100644 index 0000000..c446136 --- /dev/null +++ b/src/sim/item_def.gd @@ -0,0 +1,77 @@ +## One kind of carried item: a weapon, a consumable or an ammunition type. Built by +## ItemCatalog from the tables in Tuning, so adding an item is a data change only. +class_name ItemDef +extends Resource + +const WEAPON: int = 0 +const CONSUMABLE: int = 1 +const AMMO: int = 2 + +## Index in ItemCatalog.all(). Stable for a given Tuning table. +@export var code: int = -1 +## Key in Tuning.WEAPONS, Tuning.ITEMS or Tuning.AMMO. +@export var id: String = "" +@export var kind: int = WEAPON +## How many fit in one slot. Weapons that aren't thrown stack to 1. +@export var stack: int = 1 + +@export_group("Weapon") +@export var damage: int = 0 +@export var pellets: int = 1 +@export var fire_rate: float = 1.0 +@export var magazine: int = 0 +@export var reload_seconds: float = 0.0 +@export var range_full: float = 0.0 +@export var range_max: float = 0.0 +@export var falloff_min: float = 1.0 +@export var rarity: int = 0 +@export var spread_degrees: float = 0.0 +@export var aim_spread_factor: float = 1.0 +@export var bloom_degrees: float = 0.0 +@export var headshot_multiplier: float = 1.0 +@export var structure_damage: int = 0 +## 0 means hitscan. +@export var projectile_speed: float = 0.0 +@export var projectile_gravity: float = 0.0 +## ItemCatalog code of the ammunition this weapon loads, or -1 if none. +@export var ammo_code: int = -1 +## Thrown weapons are their own ammunition: the slot count is what's left. +@export var thrown: bool = false + +@export_group("Consumable") +@export var heal: int = 0 +@export var armor: int = 0 +@export var use_seconds: float = 0.0 + +@export_group("Ammunition") +@export var cap: int = 0 +@export var pickup_amount: int = 0 + + +func is_weapon() -> bool: + return kind == WEAPON + + +func uses_magazine() -> bool: + return kind == WEAPON and magazine > 0 and not thrown + + +## Ticks between shots at this weapon's fire rate. +func fire_interval_ticks() -> int: + return maxi(ceili(Tuning.TICK_RATE_HZ / fire_rate), 1) + + +func reload_ticks() -> int: + return roundi(reload_seconds * Tuning.TICK_RATE_HZ) + + +func use_ticks() -> int: + return roundi(use_seconds * Tuning.TICK_RATE_HZ) + + +## Damage of one pellet at `distance` metres, before headshots and tiers. +func damage_at(distance: float) -> float: + if distance <= range_full or range_max <= range_full: + return float(damage) + var t: float = clampf((distance - range_full) / (range_max - range_full), 0.0, 1.0) + return float(damage) * lerpf(1.0, falloff_min, t) diff --git a/src/sim/item_def.gd.uid b/src/sim/item_def.gd.uid new file mode 100644 index 0000000..10c2cd1 --- /dev/null +++ b/src/sim/item_def.gd.uid @@ -0,0 +1 @@ +uid://c1y2cixxx5u17 diff --git a/src/sim/player_command.gd b/src/sim/player_command.gd index 6b2cd27..e2e5c76 100644 --- a/src/sim/player_command.gd +++ b/src/sim/player_command.gd @@ -13,6 +13,8 @@ const BUTTON_SPRINT: int = 1 << 4 const BUTTON_RELOAD: int = 1 << 5 const BUTTON_INTERACT: int = 1 << 6 const BUTTON_BUILD: int = 1 << 7 +## Drops the held slot's item. Acts once per press. +const BUTTON_DROP: int = 1 << 8 const AXIS_MAX: int = 127 const YAW_STEPS: int = 65536 diff --git a/src/sim/sim_entity.gd b/src/sim/sim_entity.gd index 79e7479..4c8e84f 100644 --- a/src/sim/sim_entity.gd +++ b/src/sim/sim_entity.gd @@ -3,6 +3,10 @@ class_name SimEntity extends RefCounted const ARCHETYPE_PLAYER: int = 1 +## An item lying in the world. See Inventory. +const ARCHETYPE_PICKUP: int = 2 +## A shot in flight. See Weapons. +const ARCHETYPE_PROJECTILE: int = 3 var id: int = 0 var archetype: int = 0 @@ -27,6 +31,37 @@ var materials: PackedInt32Array = PackedInt32Array([0, 0, 0]) ## Ticks until the player may swing, fire or place again. var action_cooldown: int = 0 +# Players: inventory and combat. See Inventory and Weapons. +var armor: int = 0 +## Held selection: 0 is the boathook, 1 to Tuning.INVENTORY_SLOTS a slot. +var selected: int = 0 +## Per slot: ItemCatalog code (-1 empty), stack count, loaded rounds and quality tier. +var slot_items: PackedInt32Array = PackedInt32Array() +var slot_counts: PackedInt32Array = PackedInt32Array() +var slot_rounds: PackedInt32Array = PackedInt32Array() +var slot_tiers: PackedInt32Array = PackedInt32Array() +## Carried ammunition, indexed like Tuning.AMMO's keys. +var ammo: PackedInt32Array = PackedInt32Array() +## Ticks left on a reload of the held weapon; 0 when not reloading. +var reload_ticks: int = 0 +## Extra spread from recent shots, in degrees. +var bloom: float = 0.0 +## Buttons held last tick, so pick up and drop act once per press. +var prev_buttons: int = 0 +## Set once health reaches 0. Eliminated players take no more commands. +var eliminated: bool = false +## The player who last damaged this one, or 0. +var last_attacker: int = 0 + +# Pickups and projectiles: the item they carry. +var item: int = -1 +var item_count: int = 0 +var item_rounds: int = 0 +var item_tier: int = 0 +## Projectiles: who fired it and where it started. +var owner_id: int = 0 +var origin: Vector3 = Vector3.ZERO + func _init(entity_id: int, entity_archetype: int) -> void: id = entity_id diff --git a/src/sim/simulation.gd b/src/sim/simulation.gd index 783d21f..9ff1e8d 100644 --- a/src/sim/simulation.gd +++ b/src/sim/simulation.gd @@ -33,6 +33,9 @@ var structures: StructureSet = StructureSet.new() ## The sky barge, from start_drop() on. Null in matches and tests that start on foot. var drop: Drop = null +## Items lying in the world, as entity IDs in ascending order. See Inventory. +var pickups: Array[int] = [] + ## What happened during the last step(): harvests, destroyed props, placed pieces. ## Each is a Dictionary with a "type". Cleared at the start of every step. var events: Array[Dictionary] = [] @@ -76,6 +79,7 @@ func add_player() -> int: player.on_ground = true player.health = Tuning.PLAYER_MAX_HEALTH Water.reset(player) + Inventory.reset(player) if drop != null and not drop.finished: Drop.board(player, drop.barge_position(tick)) _players.append(player.id) @@ -120,10 +124,13 @@ func step() -> void: var command: PlayerCommand = queued.get(tick) queued.erase(tick) var player: SimEntity = entities.find(player_id) + if player.eliminated: + continue if player.drop_state == Drop.ABOARD: _ride(player, command) else: _apply_command(player, command) + _check_eliminations() if drop != null and not drop.finished and drop.is_over(tick + 1): # The end of the route: everyone still aboard goes over the side. for player_id: int in _players: @@ -151,6 +158,16 @@ func _apply_command(player: SimEntity, command: PlayerCommand) -> void: Water.update(player, sea_level, drown_dps, sprinting) if player.action_cooldown > 0: player.action_cooldown -= 1 + var held: int = command.buttons if command != null else 0 + var pressed: int = held & ~player.prev_buttons + player.prev_buttons = held + if command != null and not command.is_pressed(PlayerCommand.BUTTON_BUILD): + Inventory.select(player, command.selection) + if Harvest.can_swing(player): + if pressed & PlayerCommand.BUTTON_INTERACT: + Inventory.interact(self, player) + if pressed & PlayerCommand.BUTTON_DROP: + Inventory.drop_held(self, player) if command == null or not command.is_pressed(PlayerCommand.BUTTON_FIRE): return if not Building.is_allowed(player): @@ -158,10 +175,22 @@ func _apply_command(player: SimEntity, command: PlayerCommand) -> void: if command.is_pressed(PlayerCommand.BUTTON_BUILD): Building.act(self, player, command) return - if command.selection == BOATHOOK_SLOT: + if player.selected == BOATHOOK_SLOT: Harvest.swing(player, ground, props, events, structures) +## Marks players whose health reached 0 as eliminated, and drops what they carried. +func _check_eliminations() -> void: + for player_id: int in _players: + var player: SimEntity = entities.find(player_id) + if player.eliminated or player.health > 0: + continue + player.eliminated = true + player.velocity = Vector3.ZERO + events.append({"type": "eliminated", "player": player_id, "by": player.last_attacker}) + Inventory.drop_all(self, player) + + ## Moves the sea surface. Takes effect on the next step. func set_sea_level(level: float) -> void: sea_level = level @@ -207,6 +236,22 @@ func state_hash() -> String: buffer.put_u8(entity.drop_state) buffer.put_data(entity.materials.to_byte_array()) buffer.put_32(entity.action_cooldown) + buffer.put_32(entity.armor) + buffer.put_32(entity.selected) + for array: PackedInt32Array in [entity.slot_items, entity.slot_counts, entity.slot_rounds, entity.slot_tiers, entity.ammo]: + buffer.put_data(array.to_byte_array()) + buffer.put_32(entity.reload_ticks) + buffer.put_64(roundi(entity.bloom * 1000.0)) + buffer.put_32(entity.prev_buttons) + buffer.put_u8(1 if entity.eliminated else 0) + buffer.put_32(entity.last_attacker) + buffer.put_32(entity.item) + buffer.put_32(entity.item_count) + buffer.put_32(entity.item_rounds) + buffer.put_32(entity.item_tier) + buffer.put_32(entity.owner_id) + for value: float in [entity.origin.x, entity.origin.y, entity.origin.z]: + buffer.put_64(roundi(value * 1000.0)) for value: float in [entity.position.x, entity.position.y, entity.position.z]: buffer.put_64(roundi(value * 1000.0)) for value: float in [entity.velocity.x, entity.velocity.y, entity.velocity.z]: diff --git a/src/sim/tuning.gd b/src/sim/tuning.gd index bba65b0..dff8425 100644 --- a/src/sim/tuning.gd +++ b/src/sim/tuning.gd @@ -232,39 +232,81 @@ const BREAKWATER_MAX_HEAD: float = 6.0 const BREAKWATER_WEAR_PER_METRE: float = 2.0 const BREACH_FLOW_METRES_PER_SECOND: float = 1.5 +# Inventory and pickups +## Items picked up or dropped must be within this distance of the player's feet. +const PICKUP_RANGE: float = 2.5 +## Changing the held slot takes this long before the new item can be used. +const SWAP_SECONDS: float = 0.25 +## Dropped items land on a ring this far from the player's feet. +const DROP_SCATTER_METRES: float = 1.2 +## Ammunition, carried apart from the five slots. `cap` is the most a player carries, +## `pickup` how much one ammunition pickup holds. +const AMMO: Dictionary = { + "birdshot": {"cap": 60, "pickup": 12}, + "cartridges": {"cap": 300, "pickup": 30}, + "heavy_balls": {"cap": 30, "pickup": 6}, + "lines": {"cap": 12, "pickup": 3}, +} + # Weapons. Falloff is linear from full damage at `range_full` to `falloff_min` -# (a fraction of damage) at `range_max`. +# (a fraction of damage) at `range_max`. `spread` is the cone half-angle in degrees +# from the hip; aiming multiplies it by AIM_SPREAD_FACTOR. Each shot adds `bloom` +# degrees, which decays at BLOOM_DECAY_DEGREES_PER_SECOND while the weapon is ready. `projectile_speed` 0 means +# hitscan; otherwise shots fly at that speed under `projectile_gravity`. `ammo` names +# an AMMO type; a weapon with `stack` above 1 is thrown and is its own ammunition. const WEAPONS: Dictionary = { "boathook": { "damage": 20, "fire_rate": 1.5, "magazine": 0, "reload": 0.0, "range_full": 2.5, "range_max": 2.5, "falloff_min": 1.0, "rarity": 0, + "spread": 0.0, "bloom": 0.0, }, "fowling_piece": { "damage": 9, "pellets": 9, "fire_rate": 1.1, "magazine": 6, "reload": 4.0, "range_full": 6.0, "range_max": 25.0, "falloff_min": 0.2, "rarity": 1, + "spread": 5.0, "bloom": 0.0, "ammo": "birdshot", "headshot": 1.5, }, "dock_carbine": { "damage": 27, "fire_rate": 6.0, "magazine": 30, "reload": 2.4, "range_full": 40.0, "range_max": 120.0, "falloff_min": 0.5, "rarity": 1, + "spread": 1.2, "bloom": 0.35, "ammo": "cartridges", "headshot": 1.75, }, "signal_revolver": { "damage": 38, "fire_rate": 2.2, "magazine": 6, "reload": 2.0, "range_full": 30.0, "range_max": 80.0, "falloff_min": 0.5, "rarity": 0, + "spread": 0.8, "bloom": 0.6, "ammo": "cartridges", "headshot": 2.0, }, "whalers_longgun": { "damage": 105, "fire_rate": 0.5, "magazine": 1, "reload": 2.6, "range_full": 120.0, "range_max": 350.0, "falloff_min": 0.7, "rarity": 2, + "spread": 3.0, "bloom": 0.0, "ammo": "heavy_balls", "headshot": 2.5, + "aim_spread": 0.0, "projectile_speed": 300.0, "projectile_gravity": 9.0, }, "line_thrower": { "damage": 0, "fire_rate": 0.6, "magazine": 3, "reload": 3.0, "range_full": 45.0, "range_max": 45.0, "falloff_min": 1.0, "rarity": 2, + "spread": 0.0, "bloom": 0.0, "ammo": "lines", + "projectile_speed": 90.0, "projectile_gravity": 4.0, }, "powder_keg": { "damage": 80, "structure_damage": 400, "blast_radius": 6.0, "fuse": 3.0, - "fire_rate": 0.8, "magazine": 1, "reload": 0.0, + "fire_rate": 0.8, "magazine": 1, "reload": 0.0, "stack": 3, "range_full": 0.0, "range_max": 30.0, "falloff_min": 1.0, "rarity": 2, + "spread": 0.0, "bloom": 0.0, }, } +## Spread multiplier while aiming, unless a weapon sets its own `aim_spread`. +const AIM_SPREAD_FACTOR: float = 0.4 +## Extra spread while moving, and while airborne or swimming, in degrees. +const MOVING_SPREAD_DEGREES: float = 1.5 +const AIRBORNE_SPREAD_DEGREES: float = 4.0 +const MAX_BLOOM_DEGREES: float = 6.0 +const BLOOM_DECAY_DEGREES_PER_SECOND: float = 6.0 +## Default damage multiplier for a hit on the head, unless a weapon sets `headshot`. +const HEADSHOT_MULTIPLIER: float = 1.5 +## The top of a player's body this tall counts as the head. +const HEAD_HEIGHT: float = 0.35 +## Shots against built pieces and props do this fraction of their player damage. +const WEAPON_STRUCTURE_FACTOR: float = 1.0 # Consumables const ITEMS: Dictionary = { diff --git a/test/sim/test_inventory.gd b/test/sim/test_inventory.gd new file mode 100644 index 0000000..dc4f702 --- /dev/null +++ b/test/sim/test_inventory.gd @@ -0,0 +1,242 @@ +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) diff --git a/test/sim/test_inventory.gd.uid b/test/sim/test_inventory.gd.uid new file mode 100644 index 0000000..0c904ac --- /dev/null +++ b/test/sim/test_inventory.gd.uid @@ -0,0 +1 @@ +uid://k2vsx000qhsb