## Counts crack pixels in a terrain_cracks capture. terrain_scene.gd draws the sky as a ## flat magenta that appears nowhere in the terrain. Magenta connected to the top of the ## frame is open sky above the world's edge; any other magenta is sky seen through a gap ## in the terrain. Prints the counts and exits 1 if there are any cracks. ## ## tools/godot.sh --script res://tools/crack_check.gd -- --image=/abs/path/cracks.png extends SceneTree func _init() -> void: var path: String = "" for arg: String in OS.get_cmdline_user_args(): if arg.begins_with("--image="): path = arg.trim_prefix("--image=") var image: Image = Image.load_from_file(path) if image == null or image.is_empty(): printerr("crack_check: can't read '%s'" % path) quit(2) return var width: int = image.get_width() var height: int = image.get_height() var magenta: PackedByteArray = PackedByteArray() magenta.resize(width * height) var total: int = 0 for y: int in height: for x: int in width: var pixel: Color = image.get_pixel(x, y) if pixel.r > 0.8 and pixel.b > 0.8 and pixel.g < 0.25: magenta[y * width + x] = 1 total += 1 # Flood the open sky from the top row; what's left is seen through cracks. var queue: PackedInt32Array = PackedInt32Array() for x: int in width: if magenta[x] == 1: magenta[x] = 2 queue.append(x) var head: int = 0 while head < queue.size(): var at: int = queue[head] head += 1 var x: int = at % width var y: int = at / width for next: Vector2i in [Vector2i(x + 1, y), Vector2i(x - 1, y), Vector2i(x, y + 1), Vector2i(x, y - 1)]: if next.x < 0 or next.y < 0 or next.x >= width or next.y >= height: continue var index: int = next.y * width + next.x if magenta[index] == 1: magenta[index] = 2 queue.append(index) var cracks: int = total - queue.size() print("crack_check: %d crack pixels (%d magenta, %d of it open sky)" % [cracks, total, queue.size()]) quit(1 if cracks > 0 else 0)