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 first table phase above 28 m would drown it all, so it's # lowered; the phases before it keep their table levels. var safe: SafeGround = SafeGround.new(_ground(30.0)) var levels: PackedFloat32Array = safe.plan_levels() assert_eq(levels.size(), Tuning.TIDE_PHASES.size()) var lowered: int = -1 for index: int in Tuning.TIDE_PHASES.size(): if float(Tuning.TIDE_PHASES[index]["level"]) > 30.0 - Tuning.TIDE_SAFE_MARGIN_METRES: lowered = index break assert_true(lowered > 0 and lowered < levels.size() - 1, "the table has a phase to lower (%d)" % lowered) for index: int in lowered: assert_eq(levels[index], float(Tuning.TIDE_PHASES[index]["level"])) assert_eq(levels[lowered], 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[lowered])) 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)