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>
This commit is contained in:
@@ -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
|
||||
@@ -0,0 +1 @@
|
||||
uid://cadhqhw4s4o6g
|
||||
@@ -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
|
||||
@@ -0,0 +1 @@
|
||||
uid://qmcnsyagx5yo
|
||||
@@ -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)
|
||||
@@ -0,0 +1 @@
|
||||
uid://c1y2cixxx5u17
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
+46
-1
@@ -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]:
|
||||
|
||||
+44
-2
@@ -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 = {
|
||||
|
||||
Reference in New Issue
Block a user