M2: The rising tide

The tide now runs every match, from the drop on, and water flows along the
terrain instead of standing everywhere under one level.

- Tide (src/sim/tide.gd): Tuning.TIDE_PHASES as a schedule of holds and
  rises. Level, forecast (each hold announces its level), stage, drowning
  rate, surge timing and the time limit are pure functions of the tick.
- SafeGround (src/sim/safe_ground.gd): safe ground from elevation on an 8 m
  grid, and per-island planned levels. A phase's level is lowered, when it
  must be, to just under the top of the largest piece of land still dry when
  the phase begins; every phase still rises at least TIDE_MIN_RISE_METRES and
  the last keeps its table level.
- Flood (src/sim/flood.gd): a 4 m grid aligned with building cells. Water
  reaches cells from the world's edge through ground under the sea and edges
  no breakwater stack seals. Cut-off ground stays dry until overtopped or
  breached, then fills at BREACH_FLOW_METRES_PER_SECOND. Incremental: only
  the edges where water stopped are re-tested, once a second.
- Players, drowning and structure wear use the local water. Breakwaters wear
  by head and fail past BREAKWATER_MAX_HEAD; a breach floods what's behind.
- Storm surges: each phase's `surge` damage hits pieces near the water when
  its rise begins.
- Sudden death after the last rise, then a time limit. Match breaks ties
  (most health, highest feet, earliest join) at the limit and when the last
  players fall together, so every match ends with a winner.

Tests: test_tide (18) covers the schedule and forecasts, planned levels,
overtopping, a sealed ring, breach and fill rate, spill-over, pressure wear
and head failure, dry pieces, surges, drowning under the real tide, sudden
death, the time limit and simultaneous eliminations. test_island's 100-seed
sweep now checks that every phase leaves safe ground reachable on foot from
the whole main landmass within the phase's time (closest margin 14 s).
A full rise with 24 bots measured p99 1.5 ms, max 1.9 ms per tick.

Closes #25

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-11 20:48:41 -05:00
co-authored by Claude Opus 5.5
parent c6f3c19004
commit 8a6fb93b50
18 changed files with 1194 additions and 21 deletions
+104
View File
@@ -0,0 +1,104 @@
## Checks that safe ground stays reachable as the tide rises, for the tide tests' sweep,
## on SafeGround's 8 m grid. For each phase of the island's planned tide whose level
## some ground stands above, every cell of the main landmass (the largest dry piece when
## the phase's hold begins) must reach ground that stays dry after the rise within the
## phase's hold and rise: walking at WALK_SPEED over dry land, and swimming at
## SWIM_SPEED where the way crosses water.
class_name SafeGroundSurvey
extends RefCounted
var safe: SafeGround
var levels: PackedFloat32Array
func _init(island: Island) -> void:
safe = SafeGround.new(island.ground)
levels = safe.plan_levels()
## One result per phase checked: {"phase", "level", "worst_seconds", "allowed_seconds",
## "unreached", "swimmers"}: the longest any main-landmass cell takes to reach safe
## ground, the time the phase allows, how many cells can't reach it at all, and how many
## must swim.
func check() -> Array[Dictionary]:
var results: Array[Dictionary] = []
var highest: float = safe.highest()
var before: float = Tuning.SEA_LEVEL_START
for index: int in levels.size():
var phase: Dictionary = Tuning.TIDE_PHASES[index]
var level: float = levels[index]
if level >= highest:
break
var main: PackedByteArray = safe.largest_dry_piece(before)
var timed: PackedFloat32Array = _time_to(level, before, false, main)
var walked: PackedFloat32Array = _time_to(level, before, true, main)
var worst: float = 0.0
var unreached: int = 0
var swimmers: int = 0
for cell: int in main.size():
if main[cell] == 0:
continue
if walked[cell] == INF:
swimmers += 1
if timed[cell] == INF:
unreached += 1
else:
worst = maxf(worst, timed[cell])
results.append({
"phase": index, "level": level, "worst_seconds": worst,
"allowed_seconds": float(phase["hold"]) + float(phase["rise"]),
"unreached": unreached, "swimmers": swimmers,
})
before = level
return results
## Seconds from every cell to the nearest cell above `level`: a grid step at walking
## speed into a cell above `water`, at swimming speed into one below it (or not at all
## with `walk_only`, which also keeps to `area`). Dial's algorithm over whole steps.
func _time_to(level: float, water: float, walk_only: bool, area: PackedByteArray) -> PackedFloat32Array:
var side: int = safe.side
var heights: PackedFloat32Array = safe.heights
var swim_cost: int = maxi(roundi(Tuning.WALK_SPEED / Tuning.SWIM_SPEED), 1)
var unit: float = SafeGround.STRIDE_METRES / Tuning.WALK_SPEED
var cost: PackedInt32Array = PackedInt32Array()
cost.resize(side * side)
cost.fill(-1)
var buckets: Array[PackedInt32Array] = [PackedInt32Array()]
for cell: int in side * side:
if heights[cell] > level and (not walk_only or area[cell] != 0):
cost[cell] = 0
buckets[0].append(cell)
var at: int = 0
while at < buckets.size():
var bucket: PackedInt32Array = buckets[at]
var head: int = 0
while head < bucket.size():
var cell: int = bucket[head]
head += 1
if cost[cell] != at:
continue
var x: int = cell % side
var z: int = cell / side
for step: Vector2i in SafeGround.STEPS:
var nx: int = x + step.x
var nz: int = z + step.y
if nx < 0 or nz < 0 or nx >= side or nz >= side:
continue
var next: int = nz * side + nx
var dry: bool = heights[next] > water
if walk_only and (not dry or area[next] == 0):
continue
var total: int = at + (1 if dry else swim_cost)
if cost[next] >= 0 and cost[next] <= total:
continue
cost[next] = total
while buckets.size() <= total:
buckets.append(PackedInt32Array())
buckets[total].append(next)
at += 1
var seconds: PackedFloat32Array = PackedFloat32Array()
seconds.resize(side * side)
for cell: int in side * side:
seconds[cell] = INF if cost[cell] < 0 else cost[cell] * unit
return seconds
+1
View File
@@ -0,0 +1 @@
uid://dugkjoornrh08
+20
View File
@@ -91,6 +91,8 @@ func test_one_hundred_seeds_make_playable_islands() -> void:
var worst_stranded: float = 0.0
var land_range: Vector2 = Vector2(INF, -INF)
var peak_counts: Dictionary = {}
var safe_margin: float = INF
var lowered_phases: int = 0
for seed_value: int in range(1, SWEEP_SEEDS + 1):
var started: int = Time.get_ticks_msec()
var island: Island = Island.generate(seed_value)
@@ -116,6 +118,23 @@ func test_one_hundred_seeds_make_playable_islands() -> void:
assert_true(stranded <= MAX_STRANDED_FRACTION,
"%s: at %.0f m, %.3f of dry land is cut off from the main landmass" % [label, level, stranded])
# Safe ground (issue M2): every phase of the island's planned tide that leaves
# land leaves it within reach of the whole main landmass, on foot, in time.
var safe: SafeGroundSurvey = SafeGroundSurvey.new(island)
var checked: Array[Dictionary] = safe.check()
assert_true(checked.size() >= Tuning.TIDE_PHASES.size() - 2, "%s: only %d phases leave safe ground" % [label, checked.size()])
for result: Dictionary in checked:
var phase_label: String = "%s phase %d (%.1f m)" % [label, result["phase"], result["level"]]
assert_eq(result["unreached"], 0, "%s: main landmass cells with no way to safe ground" % phase_label)
assert_eq(result["swimmers"], 0, "%s: main landmass cells that must swim to safe ground" % phase_label)
assert_true(result["worst_seconds"] <= result["allowed_seconds"],
"%s: %.0f s to safe ground, %.0f s allowed" % [phase_label, result["worst_seconds"], result["allowed_seconds"]])
safe_margin = minf(safe_margin, result["allowed_seconds"] - result["worst_seconds"])
for index: int in safe.levels.size():
if not is_equal_approx(safe.levels[index], float(Tuning.TIDE_PHASES[index]["level"])):
lowered_phases += 1
assert_eq(safe.levels[safe.levels.size() - 1], float(Tuning.TIDE_PHASES[-1]["level"]), "%s: the last phase keeps its level" % label)
var peaks: Array[Vector3] = survey.peaks(PEAK_MIN_HEIGHT, PEAK_RADIUS, PEAK_DROP)
assert_true(peaks.size() >= 2, "%s: %d distinct high points" % [label, peaks.size()])
@@ -134,6 +153,7 @@ func test_one_hundred_seeds_make_playable_islands() -> void:
assert_true(marsh_islands >= SWEEP_SEEDS * 0.8, "only %d islands have marsh" % marsh_islands)
print(" island sweep: slowest %d ms, land %.2f to %.2f of the world, worst stranded %.4f," % [
slowest, land_range.x, land_range.y, worst_stranded])
print(" island sweep: safe ground reached with at least %.0f s to spare; %d phase levels lowered by planning" % [safe_margin, lowered_phases])
print(" island sweep: islands by distinct high points %s, %d of %d with marsh" % [
peak_counts, marsh_islands, SWEEP_SEEDS])
+1
View File
@@ -51,6 +51,7 @@ func _reference_hash(m: Match, index: int, players: int) -> String:
for _index: int in players:
sim.add_player()
sim.start_drop()
sim.start_tide()
sim.place_loot()
return sim.state_hash()
+353
View File
@@ -0,0 +1,353 @@
extends TestCase
## The rising tide from issue M2: the phase schedule and forecasts, island-planned
## levels, flooding along the terrain, breakwaters that seal, wear, overflow and breach,
## storm surges, drowning, and sudden death with a deterministic winner. Safe ground over
## 100 seeds is checked in test_island's sweep, which already generates those islands.
const SIDE: int = 513
const SPACING: float = 2.0
## A low square of land, 200 m across, in the middle of open sea.
const LAND_MIN: float = 400.0
const LAND_MAX: float = 600.0
## The breakwater ring: cells RING_MIN to RING_MAX inclusive, 12 m square.
const RING_MIN: int = 120
const RING_MAX: int = 122
const INSIDE: Vector3 = Vector3(486.0, 1.0, 486.0)
const OUTSIDE: Vector3 = Vector3(450.0, 1.0, 450.0)
## A heightfield world: open sea floor everywhere, `land` height over the land square,
## and `basin` height in a 40 m square in its middle (no basin if NAN).
func _ground(land: float, basin: float = NAN) -> SimGround:
var values: PackedFloat32Array = PackedFloat32Array()
values.resize(SIDE * SIDE)
for gz: int in SIDE:
for gx: int in SIDE:
var x: float = gx * SPACING
var z: float = gz * SPACING
var height: float = -Tuning.ISLAND_SEA_DEPTH
if x >= LAND_MIN and x <= LAND_MAX and z >= LAND_MIN and z <= LAND_MAX:
height = land
if not is_nan(basin) and x >= 480.0 and x <= 520.0 and z >= 480.0 and z <= 520.0:
height = basin
values[gz * SIDE + gx] = height
return SimGround.from_heights(values, SIDE, SPACING)
## A simulation over `ground` with a flood grid, as an island match has.
func _sim(ground: SimGround) -> Simulation:
var sim: Simulation = Simulation.new(3, ground)
sim.flood = Flood.create(ground)
return sim
## Seals the ring's twelve edges with breakwaters `stack` pieces high, the lowest based
## at build level `base`, and lets them finish building.
func _ring(sim: Simulation, stack: int = 1, base: int = 0, material: String = "stone") -> Array[int]:
var ids: Array[int] = []
var stone: int = Materials.code_of(material)
for level_index: int in stack:
var level: int = base + level_index * 2
for along: int in range(RING_MIN, RING_MAX + 1):
for slot: Vector4i in [
Vector4i(RING_MIN, level, along, StructureSet.SLOT_WALL_X),
Vector4i(RING_MAX + 1, level, along, StructureSet.SLOT_WALL_X),
Vector4i(along, level, RING_MIN, StructureSet.SLOT_WALL_Z),
Vector4i(along, level, RING_MAX + 1, StructureSet.SLOT_WALL_Z),
]:
ids.append(sim.structures.add(StructureSet.BREAKWATER, stone, slot, 0, 0, sim.tick, sim.ground).id)
for _index: int in roundi(float(Tuning.MATERIALS[material]["build_seconds"]) * Tuning.TICK_RATE_HZ) + 1:
sim.structures.advance_building()
return ids
## Steps the simulation for `seconds`, so newly flooded ground fills to the sea.
func _settle(sim: Simulation, seconds: float = 3.0) -> void:
for _index: int in roundi(seconds * Tuning.TICK_RATE_HZ):
sim.step()
func _tick_for(tide: Tide, seconds: float) -> int:
return tide.started_tick + roundi(seconds * Tuning.TICK_RATE_HZ)
func test_the_schedule_follows_the_table_and_forecasts_each_level() -> void:
var tide: Tide = Tide.new(100)
var phases: Array[Dictionary] = Tuning.TIDE_PHASES
assert_eq(tide.level_at(100), Tuning.SEA_LEVEL_START)
assert_eq(tide.stage_at(100), Tide.HOLD)
assert_eq(tide.forecast_at(100), float(phases[0]["level"]), "the first level is announced from the start")
var hold: float = phases[0]["hold"]
var rise: float = phases[0]["rise"]
assert_eq(tide.level_at(_tick_for(tide, hold - 1.0)), Tuning.SEA_LEVEL_START, "still during the hold")
assert_true(tide.ticks_until_rise(100) == roundi(hold * Tuning.TICK_RATE_HZ))
assert_true(tide.surge_starts(_tick_for(tide, hold)), "the surge comes with the rise")
assert_false(tide.surge_starts(_tick_for(tide, hold) + 1))
assert_eq(tide.stage_at(_tick_for(tide, hold + 1.0)), Tide.RISE)
assert_near(tide.level_at(_tick_for(tide, hold + rise * 0.5)), float(phases[0]["level"]) * 0.5, 0.01, "halfway up")
var end_first: int = _tick_for(tide, hold + rise)
assert_near(tide.level_at(end_first), float(phases[0]["level"]), 0.001)
assert_eq(tide.phase_at(end_first), 1)
assert_eq(tide.forecast_at(end_first), float(phases[1]["level"]), "each hold forecasts the next level")
assert_eq(tide.drown_dps_at(end_first), int(phases[1]["drown_dps"]))
# The whole schedule, then sudden death, then the time limit.
var total: float = 0.0
for phase: Dictionary in phases:
total += float(phase["hold"]) + float(phase["rise"])
assert_eq(tide.sudden_death_offset(), roundi(total * Tuning.TICK_RATE_HZ))
assert_eq(tide.stage_at(100 + tide.sudden_death_offset()), Tide.SUDDEN_DEATH)
assert_eq(tide.level_at(100 + tide.sudden_death_offset() + 5), float(phases[-1]["level"]))
assert_eq(tide.stage_at(100 + tide.time_limit_offset()), Tide.OVER)
assert_eq(tide.time_limit_offset() - tide.sudden_death_offset(), roundi(Tuning.TIDE_SUDDEN_DEATH_SECONDS * Tuning.TICK_RATE_HZ))
# Levels only rise.
var last: float = -INF
for tick: int in range(100, 100 + tide.time_limit_offset(), 97):
assert_true(tide.level_at(tick) >= last)
last = tide.level_at(tick)
func test_planned_levels_keep_safe_ground_on_the_main_land() -> void:
# A 30 m plateau: the table's 30 m phase would drown it all, so it's lowered.
var safe: SafeGround = SafeGround.new(_ground(30.0))
var levels: PackedFloat32Array = safe.plan_levels()
assert_eq(levels.size(), Tuning.TIDE_PHASES.size())
assert_eq(levels[0], float(Tuning.TIDE_PHASES[0]["level"]))
assert_eq(levels[1], float(Tuning.TIDE_PHASES[1]["level"]))
assert_eq(levels[2], 30.0 - Tuning.TIDE_SAFE_MARGIN_METRES, "lowered to just under the land's top")
for index: int in range(1, levels.size()):
assert_true(levels[index] >= levels[index - 1] + Tuning.TIDE_MIN_RISE_METRES - 0.001, "phase %d still rises" % index)
assert_eq(levels[levels.size() - 1], float(Tuning.TIDE_PHASES[-1]["level"]), "the last phase covers everything")
assert_true(safe.is_safe(500.0, 500.0, levels[2]))
assert_false(safe.is_safe(100.0, 100.0, 0.0), "open sea is never safe")
func test_low_ground_cut_off_from_the_sea_stays_dry_until_overtopped() -> void:
var sim: Simulation = _sim(_ground(3.0, -2.0))
var basin: Vector3 = Vector3(500.0, -2.0, 500.0)
assert_eq(sim.water_level_at(Vector3(100.0, 0.0, 100.0)), 0.0, "the open sea")
assert_eq(sim.water_level_at(basin), Flood.NO_WATER, "the basin is below the sea but cut off")
sim.set_sea_level(2.5)
assert_eq(sim.water_level_at(basin), Flood.NO_WATER, "the land around it still holds")
sim.set_sea_level(4.0)
assert_true(sim.flood.is_flooded(basin.x, basin.z), "the sea poured over the land")
assert_near(sim.water_level_at(basin), -2.0, 0.01, "and starts filling it from the bottom")
for _index: int in 2 * Tuning.TICK_RATE_HZ:
sim.step()
assert_near(sim.water_level_at(basin), -2.0 + 2.0 * Tuning.BREACH_FLOW_METRES_PER_SECOND, 0.06, "it fills at the breach rate")
for _index: int in 3 * Tuning.TICK_RATE_HZ:
sim.step()
assert_eq(sim.water_level_at(basin), 4.0, "until it stands level with the sea")
func test_a_breakwater_ring_keeps_its_ground_dry() -> void:
var sim: Simulation = _sim(_ground(1.0))
_ring(sim)
sim.set_sea_level(3.0)
assert_true(sim.flood.is_flooded(OUTSIDE.x, OUTSIDE.z), "the land outside floods")
assert_near(sim.water_level_at(OUTSIDE), 1.0, 0.01, "filling from its floor")
_settle(sim)
assert_eq(sim.water_level_at(OUTSIDE), 3.0, "up to the sea")
assert_eq(sim.water_level_at(INSIDE), Flood.NO_WATER, "inside the ring stays dry")
assert_eq(sim.flood.barrier_between(sim.flood.cell_of(INSIDE.x, INSIDE.z), sim.flood.cell_of(INSIDE.x - 4.0, INSIDE.z)), -INF, "no barrier inside the ring")
func test_a_breached_breakwater_floods_everything_behind_it() -> void:
var sim: Simulation = _sim(_ground(1.0))
var ids: Array[int] = _ring(sim)
sim.set_sea_level(3.0)
_settle(sim)
assert_eq(sim.water_level_at(INSIDE), Flood.NO_WATER)
assert_true(sim.damage_piece(ids[0], 100000), "the breakwater breaks")
sim.set_sea_level(3.0)
assert_true(sim.flood.is_flooded(INSIDE.x, INSIDE.z), "the breach lets the sea in")
assert_near(sim.water_level_at(INSIDE), 1.0, 0.01, "filling from the floor")
for _index: int in Tuning.TICK_RATE_HZ:
sim.step()
assert_near(sim.water_level_at(INSIDE), 1.0 + Tuning.BREACH_FLOW_METRES_PER_SECOND, 0.06, "rising at the breach rate")
_settle(sim, 1.0)
assert_eq(sim.water_level_at(INSIDE), 3.0, "until it's level with the sea")
func test_water_spills_over_a_breakwater_that_is_too_low() -> void:
var sim: Simulation = _sim(_ground(1.0))
_ring(sim)
sim.set_sea_level(3.9)
assert_eq(sim.water_level_at(INSIDE), Flood.NO_WATER)
sim.set_sea_level(4.5)
assert_true(sim.flood.is_flooded(INSIDE.x, INSIDE.z), "over the top of a 4 m breakwater")
func test_pressure_wears_breakwaters_and_too_much_head_breaks_them() -> void:
var sim: Simulation = _sim(_ground(1.0))
var ids: Array[int] = _ring(sim, 2)
var piece: StructureSet.Piece = sim.structures.find(ids[0])
var full: int = piece.health
sim.set_sea_level(3.0)
for _index: int in 3:
sim.flood.flow(3.0, 1.0)
sim.structures.tide_damage(sim.sea_level, sim.ground, sim.events, sim.flood)
assert_eq(piece.health, full - roundi(Tuning.BREAKWATER_WEAR_PER_METRE * 3.0), "worn per metre of head")
# 7 m of water on a breakwater based at 0 is past BREAKWATER_MAX_HEAD.
sim.set_sea_level(7.0)
for _index: int in 5:
sim.flood.flow(7.0, 1.0)
assert_eq(sim.water_level_at(INSIDE), Flood.NO_WATER, "the 8 m stack still holds 7 m")
sim.structures.tide_damage(sim.sea_level, sim.ground, sim.events, sim.flood)
assert_null(sim.structures.find(ids[0]), "too much head breaks it at once")
sim.set_sea_level(7.0)
assert_true(sim.flood.is_flooded(INSIDE.x, INSIDE.z), "and the sea comes through")
func test_pieces_in_dry_ground_take_no_tide_damage() -> void:
var sim: Simulation = _sim(_ground(1.0))
_ring(sim)
var floor_piece: StructureSet.Piece = sim.structures.add(StructureSet.FLOOR, Materials.code_of("stone"), Vector4i(121, 0, 121, StructureSet.SLOT_FLOOR), 0, 0, 0, sim.ground)
var outside_piece: StructureSet.Piece = sim.structures.add(StructureSet.FLOOR, Materials.code_of("stone"), Vector4i(112, 0, 112, StructureSet.SLOT_FLOOR), 0, 0, 0, sim.ground)
var inside_health: int = floor_piece.health
var outside_health: int = outside_piece.health
sim.set_sea_level(3.0)
for _index: int in 3:
sim.flood.flow(3.0, 1.0)
sim.structures.tide_damage(sim.sea_level, sim.ground, sim.events, sim.flood)
assert_eq(floor_piece.health, inside_health, "behind the breakwater, the sea can't reach it")
assert_eq(outside_piece.health, outside_health - Tuning.TIDE_STRUCTURE_DPS)
func test_a_storm_surge_hits_low_pieces_where_the_water_reaches() -> void:
var sim: Simulation = _sim(_ground(1.0))
_ring(sim)
var stone: int = Materials.code_of("stone")
var low: StructureSet.Piece = sim.structures.add(StructureSet.FLOOR, stone, Vector4i(112, 2, 112, StructureSet.SLOT_FLOOR), 0, 0, 0, sim.ground)
var high: StructureSet.Piece = sim.structures.add(StructureSet.FLOOR, stone, Vector4i(110, 6, 110, StructureSet.SLOT_FLOOR), 0, 0, 0, sim.ground)
var sheltered: StructureSet.Piece = sim.structures.add(StructureSet.FLOOR, stone, Vector4i(121, 0, 121, StructureSet.SLOT_FLOOR), 0, 0, 0, sim.ground)
var before: Array[int] = [low.health, high.health, sheltered.health]
sim.set_sea_level(3.0)
for _index: int in 3:
sim.flood.flow(3.0, 1.0)
sim.structures.surge(20, sim.sea_level, sim.ground, sim.events, sim.flood)
assert_eq(low.health, before[0] - 20, "a 4 m base is within the surge over 3 m of water")
assert_eq(high.health, before[1], "a 12 m base is above it")
assert_eq(sheltered.health, before[2], "the ring shelters what's inside")
func test_the_tide_raises_the_sea_floods_the_land_and_drowns() -> void:
# Land at 3 m, and a salvage iron ring based at 2 m, two pieces (10 m) high: the
# first phase's 8 m puts 6 m of head on it, which it holds while it wears. (Stone
# wears through during the rise: about 330 damage against 300 health.)
var sim: Simulation = _sim(_ground(3.0))
_ring(sim, 2, 1, "salvage_iron")
var outside: int = sim.add_player()
var inside: int = sim.add_player()
sim.entities.find(outside).position = OUTSIDE + Vector3(0.0, 2.0, 0.0)
sim.entities.find(inside).position = INSIDE + Vector3(0.0, 2.0, 0.0)
sim.start_tide()
# Jump to just before the first rise: the sea is still at its start.
var phase: Dictionary = Tuning.TIDE_PHASES[0]
sim.tide = Tide.new(sim.tick - roundi(float(phase["hold"]) * Tuning.TICK_RATE_HZ) + 1)
var surged: bool = false
for _index: int in roundi(float(phase["rise"]) * Tuning.TICK_RATE_HZ) + Tuning.TICK_RATE_HZ:
sim.step()
for event: Dictionary in sim.events:
surged = surged or event["type"] == "surge"
assert_true(surged, "the rise brought a surge")
assert_near(sim.sea_level, float(phase["level"]), 0.001, "the first phase raised the sea")
assert_eq(sim.drown_dps, int(Tuning.TIDE_PHASES[1]["drown_dps"]))
assert_eq(sim.water_level_at(OUTSIDE), sim.sea_level, "the land flooded")
assert_eq(sim.water_level_at(INSIDE), Flood.NO_WATER, "a 10 m ring holds 8 m")
# An exhausted swimmer with no breath goes under and drowns at the phase's rate.
var swimmer: SimEntity = sim.entities.find(outside)
swimmer.stamina_ticks = 0
swimmer.breath_ticks = 0
var dry: SimEntity = sim.entities.find(inside)
var health: int = swimmer.health
for _index: int in 5 * Tuning.TICK_RATE_HZ:
sim.step()
assert_true(Water.head_under(swimmer, sim.water_level_at(swimmer.position)), "the exhausted swimmer went under")
assert_true(health - swimmer.health >= sim.drown_dps and (health - swimmer.health) % sim.drown_dps == 0, "drowning at the phase's rate: %d -> %d" % [health, swimmer.health])
assert_eq(dry.health, Tuning.PLAYER_MAX_HEALTH, "the player behind the ring is dry")
assert_false(dry.swimming)
func _match(players: int) -> Match:
var rules: MatchRules = MatchRules.immediate()
rules.max_players = players
rules.fill_with_bots = false
var m: Match = Match.new(77, rules)
for handle: int in range(1, players + 1):
m.join(handle)
m.step()
assert_eq(m.phase, Match.DROP)
return m
func _land_everyone(m: Match) -> void:
for handle: int in m.entity_of:
var player: SimEntity = m.sim.entities.find(m.entity_of[handle])
player.drop_state = Drop.NONE
player.position = Vector3(512.0, 200.0 + handle, 512.0)
func test_sudden_death_wears_everyone_down() -> void:
var m: Match = _match(3)
_land_everyone(m)
m.sim.tide = Tide.new(m.sim.tick - m.sim.tide.sudden_death_offset(), m.sim.tide.levels)
for _index: int in Tuning.TICK_RATE_HZ:
m.step()
for handle: int in m.entity_of:
assert_eq(m.sim.entities.find(m.entity_of[handle]).health, Tuning.PLAYER_MAX_HEALTH - Tuning.TIDE_SUDDEN_DEATH_DPS)
assert_true(m.in_match())
func test_the_time_limit_ends_the_match_with_a_tie_break_winner() -> void:
var m: Match = _match(3)
_land_everyone(m)
var players: Array[SimEntity] = []
for handle: int in [1, 2, 3]:
players.append(m.sim.entities.find(m.entity_of[handle]))
players[0].health = 50
players[1].health = 90
players[2].health = 90
m.sim.tide = Tide.new(m.sim.tick - m.sim.tide.time_limit_offset() + 1, m.sim.tide.levels)
m.step()
assert_eq(m.phase, Match.RESULTS)
assert_eq(m.winners, [3] as Array[int], "most health, then highest feet")
var places: Dictionary = {}
for record: Dictionary in m.placements:
places[record["handle"]] = record["place"]
assert_eq(places, {1: 3, 2: 2, 3: 1})
var timed_out: bool = false
for event: Dictionary in m.events:
if event["type"] == "match_ended":
timed_out = event["timed_out"]
assert_true(timed_out)
func test_when_the_last_players_fall_together_one_still_wins() -> void:
var m: Match = _match(3)
_land_everyone(m)
m.leave(1)
for handle: int in [2, 3]:
m.sim.entities.find(m.entity_of[handle]).health = 0
m.step()
assert_eq(m.phase, Match.RESULTS)
assert_eq(m.winners, [3] as Array[int], "the higher of the two")
var places: Dictionary = {}
for record: Dictionary in m.placements:
places[record["handle"]] = record["place"]
assert_eq(places[3], 1)
assert_eq(places[2], 2)
assert_eq(places[1], 3, "the leaver keeps last place")
func test_every_match_ends_by_the_time_limit() -> void:
# The bound itself: nothing can keep a match going past the tide's time limit.
var tide: Tide = Tide.new(0)
var limit_seconds: float = tide.time_limit_offset() / float(Tuning.TICK_RATE_HZ)
var expected: float = Tuning.TIDE_SUDDEN_DEATH_SECONDS
for phase: Dictionary in Tuning.TIDE_PHASES:
expected += float(phase["hold"]) + float(phase["rise"])
assert_near(limit_seconds, expected, 0.05)
# Sudden death alone takes a full-health, full-armor player out before the limit.
assert_true(Tuning.TIDE_SUDDEN_DEATH_DPS * Tuning.TIDE_SUDDEN_DEATH_SECONDS >= Tuning.PLAYER_MAX_HEALTH)
assert_true(Tuning.TIDE_PHASES[-1]["level"] > Tuning.ISLAND_MAX_HEIGHT)
+1
View File
@@ -0,0 +1 @@
uid://bqbrkt5tq1qf3