Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions scripts/cmd/jobs.gd
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ static func scan(args: Args) -> int:
push_error("cannot write %s" % out)
return 1
Term.say("scan: %d non-empty chunks, y %d..%d, %.1f s" % [s.count(), s.min_y(), s.max_y(), (Time.get_ticks_msec() - t0) / 1000.0])
if s.unreadable > 0:
Term.say("warning: %d chunks could not be read and are not in the scan" % s.unreadable)
return 0


Expand Down
4 changes: 3 additions & 1 deletion scripts/job/renderer_pool.gd
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ func renderer(persp_name: String, region_dir: String) -> IsoRenderer:


## Renders each mega tile (mega x mega tiles) -> Vector2i -> null (blank) or {"img": Image, "hash": int}.
## A mega tile that could not be drawn adds nothing, so its existing rows stay as they are.
## A mega tile that could not be drawn adds nothing, so its existing rows stay as they are; the failure is
## logged, because a silent skip here is how a tile goes missing without a trace.
func render_megas(iso: IsoRenderer, megas: Array[Vector2i], mega: int, min_y: int, max_y: int) -> Dictionary:
var tiles := {}
iso.min_y = min_y
Expand All @@ -52,6 +53,7 @@ func render_megas(iso: IsoRenderer, megas: Array[Vector2i], mega: int, min_y: in
iso.next_generation()
var images := iso.render(m.x * mega, m.y * mega, mega, mega)
if images.is_empty():
Term.say("warning: mega tile %s could not be rendered; its tiles are unchanged" % m)
continue
var i := 0
for ty in mega:
Expand Down
2 changes: 2 additions & 0 deletions scripts/job/run_job.gd
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func run() -> int:
if not scan.save(cache_path):
return _fail("cannot write scan cache %s" % cache_path)
Term.say("scan: %d non-empty chunks, %.1f s" % [scan.count(), (Time.get_ticks_msec() - t0) / 1000.0])
if scan.unreadable > 0:
Term.say("warning: %d chunks could not be read; they are not in the plan and their tiles stay as they are" % scan.unreadable)
if scan.count() == 0:
return _fail("nothing to render in %s" % map.region_dir)

Expand Down
15 changes: 15 additions & 0 deletions scripts/map/scan_cache.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ extends RefCounted
## cache format: four int32 per chunk.

var quads := PackedInt32Array()
## Chunks that are present in a region file but could not be read (broken data, or a chunk the game
## stored in an external file). They are not in the plan, so their tiles are never rendered by a run
## job; the count is reported so the loss is not silent.
var unreadable := 0

var _files: Array = []
var _results: Array = []
var _skipped: Array = []


static func load_file(path: String) -> ScanCache:
Expand Down Expand Up @@ -54,25 +59,34 @@ static func build(region_dir: String) -> ScanCache:
c._files.append([rc[0], rc[1], region_dir.path_join(f)])
c._files.sort()
c._results.resize(c._files.size())
c._skipped.resize(c._files.size())
var gid := WorkerThreadPool.add_group_task(c._scan_region, c._files.size(), -1, true, "scan regions")
WorkerThreadPool.wait_for_group_task_completion(gid)
for r in c._results:
c.quads.append_array(r)
for s in c._skipped:
c.unreadable += int(s)
return c


func _scan_region(i: int) -> void:
var info: Array = _files[i]
var rg := WorldReader.read_region(info[2])
var out := PackedInt32Array()
var skipped := 0
if rg.size() >= 8192:
for lz in 32:
for lx in 32:
var raw := WorldReader.raw_chunk(rg, lx, lz)
if raw.is_empty():
# the location entry says a chunk is stored here but it could not be read
# (broken compression, or a chunk the game moved to an external file)
if rg[lx * 4 + lz * 128 + 3] != 0:
skipped += 1
continue
var root := Nbt.parse(raw)
if root.is_empty():
skipped += 1
continue
var status = root.get("Status", "")
if status is String and status != "minecraft:full" and status != "full" and status != "":
Expand All @@ -99,6 +113,7 @@ func _scan_region(i: int) -> void:
out.append(lo)
out.append(hi)
_results[i] = out
_skipped[i] = skipped


static func _is_air(v: Variant) -> bool:
Expand Down
53 changes: 40 additions & 13 deletions scripts/store/zoom_builder.gd
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,35 @@ func _ssbo(binding: int, id: RID) -> RDUniform:
return u


## Bookkeeping for one batch of parents at one level: per parent, whether its row is written ("write"), an
## existing row is dropped because the parent came out blank ("drop"), the parent is blank and has no row
## ("absent") or the stored row already matches ("same"). Every parent of the batch is in "next_keys": every
## processed parent propagates to the next level regardless of its outcome, because a missing parent can only
## be created while its children are being processed. If unchanged children stopped propagating, a parent that
## was never built (or was lost) would stay missing forever, and a higher parent would bake the absence in as a
## transparent quadrant. "hashes" carries the combined hash of every non-blank parent.
static func plan_level(batch: Array, combined: Array, old: Dictionary) -> Dictionary:
var action: Array[String] = []
var hashes: Array[int] = []
var next_keys := {}
for i in batch.size():
var p: Vector2i = batch[i]
next_keys[p] = true
var px: PackedByteArray = combined[i]
if TileCodec.is_blank(px):
action.append("drop" if old.has(p) else "absent")
hashes.append(0)
continue
var h := TileCodec.crc_hash(px)
hashes.append(h)
action.append("same" if old.get(p, -1) == h else "write")
return {"action": action, "hashes": hashes, "next_keys": next_keys}


## keys: Vector2i -> true, the zoom-0 tiles that changed. known: Vector2i -> Image (new content) or null (deleted).
## zoom_all: also re-check every parent that exists (scans whole levels; fixes stale quadrants and orphans).
## zoom_all: also re-check every parent that exists (scans whole levels; fixes stale quadrants and orphans)
## and, because every processed parent propagates (plan_level), creates the parents that are missing, so a
## zoom_all pass leaves no existing tile without its parent.
func rebuild(keys: Dictionary, known: Dictionary, zoom_max: int, zoom_all: bool, now_ms: int) -> void:
var level_keys := keys.duplicate()
var level_known := known.duplicate()
Expand Down Expand Up @@ -163,24 +190,24 @@ func rebuild(keys: Dictionary, known: Dictionary, zoom_max: int, zoom_all: bool,
kids.append(_bytes_of(level_known[c] if level_known.has(c) else fetched.get(c)))
kids_list.append(kids)
var combined := combine(kids_list)
var plan := plan_level(batch, combined, old)
var writes := []
var drops: Array[Vector2i] = []
for i in batch.size():
var p: Vector2i = batch[i]
var px: PackedByteArray = combined[i]
if TileCodec.is_blank(px):
if old.has(p):
next_keys[p] = true
match plan["action"][i]:
"write":
var webp := TileCodec.encode_webp(Image.create_from_data(T, T, false, Image.FORMAT_RGBA8, combined[i]))
writes.append({"x": p.x, "y": p.y, "hash": plan["hashes"][i], "webp": webp})
next_known[p] = webp
"drop":
drops.append(p)
next_keys[p] = true
next_known[p] = null
continue
var h := TileCodec.crc_hash(px)
if old.get(p, -1) == h:
continue
var webp := TileCodec.encode_webp(Image.create_from_data(T, T, false, Image.FORMAT_RGBA8, px))
writes.append({"x": p.x, "y": p.y, "hash": h, "webp": webp})
next_keys[p] = true
next_known[p] = webp
"absent":
next_known[p] = null
_: # "same": the stored row stands; the next level reads it back when it needs it
pass
store.write(zoom + 1, writes, now_ms)
store.delete(zoom + 1, drops)
written += writes.size()
Expand Down
40 changes: 40 additions & 0 deletions tests/run_tests.gd
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func _initialize() -> void:
_test_chunk()
_test_gpu_pack()
_test_scan()
_test_zoom_plan()
_test_jar()
_test_registry()
_test_config()
Expand Down Expand Up @@ -382,11 +383,50 @@ func _test_scan() -> void:
var deflated := nbt.compress(FileAccess.COMPRESSION_DEFLATE)
_write(dir.path_join("r.0.0.mca"), _region_with(2, deflated.size() + 1, 2, deflated))
_write(dir.path_join("r.999999.0.mca"), _region_with(2, deflated.size() + 1, 2, deflated))
var garbage := PackedByteArray([0x63, 0x00, 0x00, 0xFF, 0x42])
_write(dir.path_join("r.1.0.mca"), _region_with(2, garbage.size() + 1, 3, garbage))
var scan := ScanCache.build(ProjectSettings.globalize_path(dir))
_eq("scan keeps only region files inside the world border", scan.count(), 1)
_eq("scan counts chunks it cannot read instead of dropping them silently", scan.unreadable, 1)
_eq("scan ignores a section outside the Y range", [scan.min_y(), scan.max_y()], [0, 16])


func _solid_tile(r: int, g: int, b: int, a: int) -> PackedByteArray:
var px := PackedByteArray()
px.resize(128 * 128 * 4)
for i in range(0, px.size(), 4):
px[i] = r
px[i + 1] = g
px[i + 2] = b
px[i + 3] = a
return px


func _test_zoom_plan() -> void:
var blank := PackedByteArray()
blank.resize(128 * 128 * 4)
var red := _solid_tile(255, 0, 0, 255)
var blue := _solid_tile(0, 0, 255, 255)
var p_write := Vector2i(1, 0)
var p_same := Vector2i(2, 0)
var p_drop := Vector2i(3, 0)
var p_absent := Vector2i(4, 0)
var batch: Array = [p_write, p_same, p_drop, p_absent]
var combined: Array = [red, blue, blank, blank]
var old := {p_same: TileCodec.crc_hash(blue), p_drop: 12345}
var plan := ZoomBuilder.plan_level(batch, combined, old)
_eq("zoom plan writes a parent that has no row", plan["action"][0], "write")
_eq("zoom plan keeps a parent whose row matches", plan["action"][1], "same")
_eq("zoom plan drops a blank parent that has a row", plan["action"][2], "drop")
_eq("zoom plan marks a blank parent without a row absent", plan["action"][3], "absent")
_eq("zoom plan carries the hash of a written parent", plan["hashes"][0], TileCodec.crc_hash(red))
# Regression: every parent must propagate to the next level, whatever its outcome. When only changed
# parents propagated, a missing parent of unchanged children was never created and the absence was
# baked into higher levels as transparent quadrants (the black tiles of 2026-11-08).
for p in batch:
_ok("zoom plan propagates %s to the next level" % p, plan["next_keys"].has(p))


# --- build-data's jar reader -----------------------------------------------------------------------------------------


Expand Down
Loading