## 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