B9: Loot tables and containers

Seeded loot at every point of interest's loot markers, rolled once per match
from the "loot" stream by LootSet.plan() and placed by Simulation.place_loot().

- Each marker holds a sea chest (chance by location) or floor loot (rolls on
  the location's weapon/consumable/ammo/nothing weights). Weapons roll a tier
  from the location's tier weights and come with their ammunition; chests
  hold a weapon (best of two tier rolls), a consumable and spare ammunition.
- Interact opens the nearest closed chest in reach unless a pickup is nearer,
  and spills its contents as pickups.
- Washed-up crate props hold one roll of the crate table and spill it when
  broken by any means.
- If the rolls leave fewer POI weapons than LOOT_MIN_POI_WEAPONS (one per
  player in a full lobby), extra weapon rolls top up floor spots.
- Chest and crate state is part of state_hash(). ServerLoop places loot.

Tests: test_loot (12), including a statistics run of 1,000 loot seeds over
four generated islands that checks tier shares against the tables, that
shrines beat hamlets, and that every match has a weapon per player at POIs
and 1.25 per player counting crates.

Closes #23

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-11 20:16:20 -05:00
co-authored by Claude Opus 5.5
parent 0c6ad36105
commit 0013a81658
9 changed files with 677 additions and 6 deletions
+313
View File
@@ -0,0 +1,313 @@
extends TestCase
## Loot from issue B9: seeded floor loot and sea chests at loot markers, crates that
## spill when broken, tables by location and tier, and a statistics run over 1,000 seeds.
## Islands whose markers and crates the statistics run rolls loot over. Generating a
## whole island takes most of a second, so 1,000 loot seeds share these four islands:
## loot seed N uses island ISLAND_SEEDS[N % 4].
const ISLAND_SEEDS: Array[int] = [1, 2, 3, 4]
const STAT_SEEDS: int = 1000
const FEET: Vector3 = Vector3(100.0, 0.0, 100.0)
static var _surveys: Array[Dictionary] = []
func _press(sim: Simulation, buttons: int) -> void:
var player: SimEntity = sim.entities.find(1)
sim.submit_command(1, PlayerCommand.from_input(sim.tick, Vector2.ZERO, 0.0, 0.0, buttons, player.selected))
sim.step()
func _interact(sim: Simulation) -> void:
_press(sim, PlayerCommand.BUTTON_INTERACT)
_press(sim, 0)
## A flat world with one player at FEET and one chest `offset` from the feet holding a
## carbine and some cartridges.
func _chest_world(offset: Vector3) -> Simulation:
var sim: Simulation = Simulation.new(5, SimGround.flat())
sim.add_player()
sim.entities.find(1).position = FEET
sim.loot.chest_positions.append(FEET + offset)
sim.loot.chest_sources.append("hamlet")
sim.loot.chest_contents.append([
PackedInt32Array([ItemCatalog.code_of("dock_carbine"), 1, -1, 2]),
PackedInt32Array([ItemCatalog.code_of("cartridges"), 30, -1, 0]),
])
sim.loot.chest_opened.append(0)
return sim
## Markers and crate spill points of each statistics island, generated once.
func _islands() -> Array[Dictionary]:
if _surveys.is_empty():
for seed_value: int in ISLAND_SEEDS:
var island: Island = Island.generate(seed_value)
var pois: PoiSet = PoiSet.generate(island)
var props: PropSet = PropSet.generate(island, pois)
_surveys.append({"markers": pois.loot_markers(), "crates": Simulation.crate_spill_points(props)})
return _surveys
func _is_weapon(stack: PackedInt32Array) -> bool:
return ItemCatalog.get_def(stack[0]).kind == ItemDef.WEAPON
func test_tables_cover_every_location_category_and_tier() -> void:
for kind: String in Tuning.POI_KINDS:
assert_true(Tuning.LOOT_SOURCES.has(kind), "%s has a loot table" % kind)
assert_true(Tuning.LOOT_SOURCES.has(LootSet.CRATE_KIND), "crates have a loot table")
for source: String in Tuning.LOOT_SOURCES:
var table: Dictionary = Tuning.LOOT_SOURCES[source]
for category: String in LootSet.CATEGORIES:
assert_true(int(table[category]) >= 0, "%s %s weight" % [source, category])
assert_true(int(table["weapon"]) > 0, "%s can hold weapons" % source)
assert_eq(table["tiers"].size(), Tuning.WEAPON_TIERS.size(), "%s has a weight per tier" % source)
assert_true(int(table["rolls"]) >= 1)
for id: String in Tuning.ITEMS:
var count: int = int(Tuning.ITEMS[id]["loot"])
assert_true(count >= 1 and count <= int(Tuning.ITEMS[id]["stack"]), "%s loot count fits a stack" % id)
for id: String in LootSet.loot_weapons():
assert_true(int(LootSet.loot_weapons()[id]["rarity"]) < Tuning.LOOT_RARITY_WEIGHTS.size(), "%s rarity has a weight" % id)
func test_loot_is_deterministic_for_a_seed() -> void:
var a: Simulation = Simulation.new(21)
var b: Simulation = Simulation.new(21)
a.place_loot()
b.place_loot()
assert_true(a.pickups.size() > 0, "floor loot was placed")
assert_true(a.loot.chest_positions.size() > 0, "chests were placed")
assert_eq(a.pickups.size(), b.pickups.size())
for index: int in a.pickups.size():
var pa: SimEntity = a.entities.find(a.pickups[index])
var pb: SimEntity = b.entities.find(b.pickups[index])
assert_eq([pa.item, pa.item_count, pa.item_rounds, pa.item_tier], [pb.item, pb.item_count, pb.item_rounds, pb.item_tier])
assert_eq(pa.position, pb.position)
assert_eq(a.loot.chest_contents, b.loot.chest_contents)
assert_eq(a.loot.crate_contents, b.loot.crate_contents)
assert_eq(a.state_hash(), b.state_hash())
# Placing twice changes nothing.
var before: String = a.state_hash()
a.place_loot()
assert_eq(a.state_hash(), before, "loot is placed once")
func test_different_seeds_roll_different_loot_on_the_same_markers() -> void:
var survey: Dictionary = _islands()[0]
var first: LootSet = LootSet.plan(SimRng.new(1).stream("loot"), survey["markers"], survey["crates"])
var again: LootSet = LootSet.plan(SimRng.new(1).stream("loot"), survey["markers"], survey["crates"])
var other: LootSet = LootSet.plan(SimRng.new(2).stream("loot"), survey["markers"], survey["crates"])
assert_eq(first.chest_contents, again.chest_contents)
assert_eq(first.floor_spots, again.floor_spots)
assert_true(first.floor_spots != other.floor_spots or first.chest_contents != other.chest_contents, "a different seed changes the loot")
func test_floor_loot_stands_at_its_markers() -> void:
var sim: Simulation = Simulation.new(3)
sim.place_loot()
var placed: int = 0
for spot: Dictionary in sim.loot.floor_spots:
placed += spot["stacks"].size()
assert_eq(sim.pickups.size(), placed, "every floor stack became a pickup")
for pickup_id: int in sim.pickups:
var pickup: SimEntity = sim.entities.find(pickup_id)
var nearest: float = INF
for spot: Dictionary in sim.loot.floor_spots:
nearest = minf(nearest, Vector2(pickup.position.x, pickup.position.z).distance_to(Vector2(spot["position"].x, spot["position"].z)))
assert_true(nearest <= Tuning.LOOT_SCATTER_METRES + 0.01, "pickup %d lies at its marker" % pickup_id)
var under: float = sim.ground.support_height(pickup.position.x, pickup.position.z, pickup.position.y + 0.1)
assert_near(pickup.position.y, under, 0.01, "pickup %d rests on a floor" % pickup_id)
func test_chests_hold_a_weapon_with_its_ammunition() -> void:
var survey: Dictionary = _islands()[1]
var loot: LootSet = LootSet.plan(SimRng.new(9).stream("loot"), survey["markers"], survey["crates"])
assert_true(loot.chest_contents.size() > 0)
for contents: Array in loot.chest_contents:
var weapons: int = 0
var consumables: int = 0
for stack: PackedInt32Array in contents:
var def: ItemDef = ItemCatalog.get_def(stack[0])
assert_not_null(def)
assert_true(stack[1] >= 1)
if def.kind == ItemDef.WEAPON:
weapons += 1
if def.ammo_code >= 0:
var has_ammo: bool = false
for other: PackedInt32Array in contents:
has_ammo = has_ammo or other[0] == def.ammo_code
assert_true(has_ammo, "%s comes with its ammunition" % def.id)
elif def.kind == ItemDef.CONSUMABLE:
consumables += 1
assert_eq(weapons, Tuning.LOOT_CHEST_WEAPONS)
assert_eq(consumables, Tuning.LOOT_CHEST_CONSUMABLES)
func test_interact_opens_a_chest_once_and_spills_it() -> void:
var sim: Simulation = _chest_world(Vector3(1.0, 0.0, 0.0))
_interact(sim)
assert_eq(sim.loot.chest_opened[0], 1, "the chest opened")
assert_eq(sim.pickups.size(), 2, "its two stacks spilled")
assert_eq(sim.entities.find(1).slot_items[0], Inventory.EMPTY, "opening doesn't also pick up")
# The next press picks up instead of opening again.
_interact(sim)
assert_eq(sim.loot.chest_opened.count(1), 1)
var player: SimEntity = sim.entities.find(1)
assert_true(player.slot_items[0] != Inventory.EMPTY or player.ammo[Inventory.ammo_index(ItemCatalog.code_of("cartridges"))] > 0, "a spilled stack was picked up")
func test_chest_out_of_reach_stays_shut() -> void:
var sim: Simulation = _chest_world(Vector3(Tuning.CHEST_RANGE + 0.5, 0.0, 0.0))
_interact(sim)
assert_eq(sim.loot.chest_opened[0], 0)
assert_eq(sim.pickups.size(), 0)
func test_a_nearer_pickup_is_taken_before_the_chest_is_opened() -> void:
var sim: Simulation = _chest_world(Vector3(2.0, 0.0, 0.0))
Inventory.spawn_pickup(sim, FEET + Vector3(0.5, 0.0, 0.0), ItemCatalog.code_of("tar_patch"), 2)
_interact(sim)
assert_eq(sim.loot.chest_opened[0], 0, "the nearer pickup wins")
assert_eq(Inventory.count_of(sim.entities.find(1), ItemCatalog.code_of("tar_patch")), 2)
_interact(sim)
assert_eq(sim.loot.chest_opened[0], 1, "then the chest opens")
func test_opening_a_chest_changes_the_state_hash() -> void:
var a: Simulation = _chest_world(Vector3(1.0, 0.0, 0.0))
var b: Simulation = _chest_world(Vector3(1.0, 0.0, 0.0))
_press(a, 0)
_press(b, 0)
assert_eq(a.state_hash(), b.state_hash())
_interact(a)
_press(b, 0)
_press(b, 0)
assert_ne(a.state_hash(), b.state_hash())
func test_a_broken_crate_spills_its_loot_where_it_stood() -> void:
var sim: Simulation = Simulation.new(4)
sim.place_loot()
var crate: int = -1
for prop: int in sim.loot.crate_contents:
if not sim.loot.crate_contents[prop].is_empty():
crate = prop
break
assert_true(crate >= 0, "some crate holds loot")
var stacks: int = sim.loot.crate_contents[crate].size()
var before: int = sim.pickups.size()
assert_true(sim.damage_prop(crate, 100000))
sim.step()
assert_eq(sim.pickups.size(), before + stacks, "the crate spilled")
assert_false(sim.loot.crate_contents.has(crate))
var spilled: bool = false
for event: Dictionary in sim.events:
spilled = spilled or (event["type"] == "crate_spilled" and event["prop"] == crate)
assert_true(spilled, "a crate_spilled event was raised")
var box: AABB = sim.props.boxes[crate]
var newest: SimEntity = sim.entities.find(sim.pickups[sim.pickups.size() - 1])
assert_true(Vector2(newest.position.x, newest.position.z).distance_to(Vector2(box.get_center().x, box.get_center().z)) <= Tuning.LOOT_SCATTER_METRES + 0.01)
sim.step()
assert_eq(sim.pickups.size(), before + stacks, "a crate spills once")
## The statistics run from the issue: 1,000 loot seeds, each over one of four real
## islands. It checks the tier distribution against the tables, that better places
## give better tiers, and that every match has a weapon for every player.
func test_statistics_over_1000_seeds() -> void:
var islands: Array[Dictionary] = _islands()
var tier_counts: PackedInt32Array = PackedInt32Array()
tier_counts.resize(Tuning.WEAPON_TIERS.size())
var expected: PackedFloat64Array = PackedFloat64Array()
expected.resize(Tuning.WEAPON_TIERS.size())
var shrine_tiers: Vector2 = Vector2.ZERO
var hamlet_tiers: Vector2 = Vector2.ZERO
var fewest_weapons: int = 1 << 30
var fewest_poi_weapons: int = 1 << 30
var fewest_weapon_sites: int = 1 << 30
var short_matches: int = 0
var topped_up_matches: int = 0
var total_weapons: int = 0
for seed_value: int in range(1, STAT_SEEDS + 1):
var survey: Dictionary = islands[seed_value % islands.size()]
var loot: LootSet = LootSet.plan(SimRng.new(seed_value).stream("loot"), survey["markers"], survey["crates"])
var poi_weapons: int = 0
var sites: Dictionary = {}
var groups: Array[Array] = []
for spot: Dictionary in loot.floor_spots:
groups.append([spot["source"], spot["stacks"], spot["position"], 1])
for index: int in loot.chest_contents.size():
groups.append([loot.chest_sources[index], loot.chest_contents[index], loot.chest_positions[index], Tuning.LOOT_CHEST_TIER_ROLLS])
for group: Array in groups:
for stack: PackedInt32Array in group[1]:
if not _is_weapon(stack):
continue
poi_weapons += 1
sites[group[2]] = true
tier_counts[stack[3]] += 1
_add_expected(expected, group[0], group[3])
if group[0] == "shrine":
shrine_tiers += Vector2(stack[3], 1)
elif group[0] == "hamlet":
hamlet_tiers += Vector2(stack[3], 1)
var crate_weapons: int = 0
for prop: int in loot.crate_contents:
for stack: PackedInt32Array in loot.crate_contents[prop]:
if _is_weapon(stack):
crate_weapons += 1
tier_counts[stack[3]] += 1
_add_expected(expected, LootSet.CRATE_KIND, 1)
var weapons: int = poi_weapons + crate_weapons
total_weapons += weapons
fewest_weapons = mini(fewest_weapons, weapons)
fewest_poi_weapons = mini(fewest_poi_weapons, poi_weapons)
fewest_weapon_sites = mini(fewest_weapon_sites, sites.size())
if poi_weapons < Tuning.SOLO_PLAYERS:
short_matches += 1
for spot: Dictionary in loot.floor_spots:
if spot.has("topped_up"):
topped_up_matches += 1
break
# Tier distribution: each tier's share within two percentage points of what the
# tables predict, and rarer the better it is.
var counted: int = 0
for count: int in tier_counts:
counted += count
for tier: int in tier_counts.size():
var share: float = float(tier_counts[tier]) / counted
var predicted: float = expected[tier] / counted
assert_near(share, predicted, 0.02, "tier %s share" % Tuning.WEAPON_TIERS[tier]["name"])
assert_true(tier_counts[tier] > 0, "tier %d appears" % tier)
if tier > 0:
assert_true(tier_counts[tier] < tier_counts[tier - 1], "tier %d is rarer than tier %d" % [tier, tier - 1])
assert_true(shrine_tiers.x / shrine_tiers.y > hamlet_tiers.x / hamlet_tiers.y + 0.5, "shrines hold better weapons than hamlets")
# Every player can find a weapon: points of interest alone hold one per player in a
# full lobby in every match (topped up when the rolls fall short, which should be
# rare), and counting crates there are at least a quarter more. Weapons are spread
# over at least eight places.
assert_eq(short_matches, 0, "matches whose POIs hold fewer weapons than players")
assert_true(fewest_poi_weapons >= Tuning.SOLO_PLAYERS, "fewest POI weapons: %d" % fewest_poi_weapons)
assert_true(fewest_weapons >= ceili(Tuning.SOLO_PLAYERS * 1.25), "fewest weapons in a match: %d" % fewest_weapons)
assert_true(topped_up_matches <= STAT_SEEDS / 10, "matches that needed topping up: %d" % topped_up_matches)
assert_true(fewest_weapon_sites >= 8, "fewest places with a weapon: %d" % fewest_weapon_sites)
print("loot stats: weapons per match mean %.1f, min %d; POI min %d; topped up %d; tiers %s" % [float(total_weapons) / STAT_SEEDS, fewest_weapons, fewest_poi_weapons, topped_up_matches, tier_counts])
## Adds one weapon's expected tier probabilities: the best of `rolls` rolls on the
## source's weights.
func _add_expected(expected: PackedFloat64Array, source: String, rolls: int) -> void:
var weights: Array = Tuning.LOOT_SOURCES[source]["tiers"]
var total: float = 0.0
for weight: int in weights:
total += weight
var below: float = 0.0
for tier: int in weights.size():
var at_most: float = pow((below + weights[tier]) / total, rolls)
var under: float = pow(below / total, rolls)
expected[tier] += at_most - under
below += weights[tier]
+1
View File
@@ -0,0 +1 @@
uid://br6rnqgyq2f67