extends TestCase ## A11: the chart's flood forecast (Flood.forecast) shows exactly the cells each phase's ## rise will flood, on a real island through every phase, and behind a breakwater ring ## that holds or doesn't. const SIDE: int = 513 const SPACING: float = 2.0 ## Skips the rest of the current hold, so the rise starts on the next step. func _skip_hold(sim: Simulation) -> void: var wait: int = sim.tide.ticks_until_rise(sim.tick) if wait > 1: sim.tide = Tide.new(sim.tide.started_tick - wait + 1, sim.tide.levels) ## Steps through the current rise and a second after, so every pour has happened. func _ride_out_rise(sim: Simulation) -> void: var phase: int = sim.tide.phase_at(sim.tick) var guard: int = 0 while sim.tide.stage_at(sim.tick) != Tide.HOLD or sim.tide.phase_at(sim.tick) == phase: sim.step() guard += 1 if sim.tide.stage_at(sim.tick) >= Tide.SUDDEN_DEATH or guard > 200 * Tuning.TICK_RATE_HZ: break for _index: int in Tuning.TICK_RATE_HZ + 1: sim.step() func _differing(a: PackedByteArray, b: PackedByteArray) -> int: var count: int = 0 for index: int in a.size(): if a[index] != b[index]: count += 1 return count func test_the_forecast_matches_every_phase_on_a_real_island() -> void: var sim: Simulation = Simulation.new(11) assert_not_null(sim.flood) sim.start_tide() var phases_checked: int = 0 for phase: int in Tide.phase_count(): var before: PackedByteArray = sim.flood.flooded.duplicate() var level: float = sim.tide.forecast_at(sim.tick) var forecast: PackedByteArray = sim.flood.forecast(level, sim.structures) assert_eq(sim.flood.flooded, before, "forecasting changes nothing") _skip_hold(sim) _ride_out_rise(sim) assert_near(sim.sea_level, level, 0.001, "phase %d reached its forecast level" % phase) assert_eq(_differing(forecast, sim.flood.flooded), 0, "phase %d floods what was forecast" % phase) assert_true(_differing(before, forecast) > 0 or phase == 0, "phase %d floods something new" % phase) phases_checked += 1 if sim.tide.stage_at(sim.tick) >= Tide.SUDDEN_DEATH: break assert_eq(phases_checked, Tide.phase_count()) func test_the_forecast_respects_a_breakwater_ring() -> void: 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 values[gz * SIDE + gx] = 1.0 if x >= 400.0 and x <= 600.0 and z >= 400.0 and z <= 600.0 else -Tuning.ISLAND_SEA_DEPTH var ground: SimGround = SimGround.from_heights(values, SIDE, SPACING) var sim: Simulation = Simulation.new(3, ground) sim.flood = Flood.create(ground) var stone: int = Materials.code_of("stone") var ids: Array[int] = [] for along: int in range(120, 123): for slot: Vector4i in [Vector4i(120, 0, along, StructureSet.SLOT_WALL_X), Vector4i(123, 0, along, StructureSet.SLOT_WALL_X), Vector4i(along, 0, 120, StructureSet.SLOT_WALL_Z), Vector4i(along, 0, 123, StructureSet.SLOT_WALL_Z)]: ids.append(sim.structures.add(StructureSet.BREAKWATER, stone, slot, 0, 0, sim.tick, sim.ground).id) for _index: int in 200: sim.structures.advance_building() var inside: int = sim.flood.cell_of(486.0, 486.0) var outside: int = sim.flood.cell_of(450.0, 450.0) var forecast: PackedByteArray = sim.flood.forecast(3.0, sim.structures) assert_eq(forecast[outside], 1, "the land outside will flood") assert_eq(forecast[inside], 0, "the ring will hold") sim.set_sea_level(3.0) assert_eq(_differing(forecast, sim.flood.flooded), 0, "and so it did") # Too high for the ring: the forecast says it spills over. forecast = sim.flood.forecast(9.0, sim.structures) assert_eq(forecast[inside], 1, "over the top") # With the ring gone, the forecast changes with it. for piece_id: int in ids: sim.structures.remove(piece_id, sim.ground) forecast = sim.flood.forecast(3.0, sim.structures) assert_eq(forecast[inside], 1, "nothing holds it back now") sim.set_sea_level(3.0) assert_eq(_differing(forecast, sim.flood.flooded), 0)