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
24 changes: 23 additions & 1 deletion scripts/job/publisher.gd
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,35 @@ var store: TileStore
## sorted tile keys of the plan; empty = every rendered tile is a candidate
var candidates := PackedInt64Array()
var only_missing := false
## mega tile size of the caller's plan; only used to group still-missing tiles in the result
var mega := 8


func _init(store_: TileStore) -> void:
store = store_


## Mega tile -> how many of its tiles are in `wanted_blank` (candidate tiles without a row that rendered
## blank in an only-missing upload). Pure bookkeeping for upload(); see its result.
static func still_megas(wanted_blank: Array, mega_: int) -> Dictionary:
var out := {}
for xy: Vector2i in wanted_blank:
var m := Vector2i(ZoomBuilder.floor_div(xy.x, mega_), ZoomBuilder.floor_div(xy.y, mega_))
out[m] = int(out.get(m, 0)) + 1
return out


## tiles: Vector2i -> null | {"img", "hash"}. Returns {"changed": Vector2i -> Image|null, "text": summary}.
## In only-missing mode the result also carries "still_megas" (see still_megas()): mega tiles whose missing
## candidates rendered blank, so they are still missing. A tile can render blank because its chunk could not
## be read this time, not because it has no content; the caller leaves those mega tiles out of its done set
## so a later run tries them again instead of treating the hole as repaired.
func upload(tiles: Dictionary, now_ms: int) -> Dictionary:
var existing := store.hashes(0, tiles.keys())
var changed := {}
var writes := []
var drops: Array[Vector2i] = []
var wanted_blank: Array[Vector2i] = []
var same := 0
var blank := 0
for xy: Vector2i in tiles:
Expand All @@ -34,6 +51,8 @@ func upload(tiles: Dictionary, now_ms: int) -> Dictionary:
if existing.has(xy):
drops.append(xy)
changed[xy] = null
elif only_missing:
wanted_blank.append(xy)
continue
if existing.get(xy, -1) == t["hash"]:
same += 1
Expand All @@ -42,4 +61,7 @@ func upload(tiles: Dictionary, now_ms: int) -> Dictionary:
changed[xy] = t["img"]
store.write(0, writes, now_ms)
store.delete(0, drops)
return {"changed": changed, "text": "zoom 0: wrote %d, deleted %d, unchanged %d, blank %d" % [writes.size(), drops.size(), same, blank]}
var res := {"changed": changed, "text": "zoom 0: wrote %d, deleted %d, unchanged %d, blank %d" % [writes.size(), drops.size(), same, blank]}
if only_missing and not wanted_blank.is_empty():
res["still_megas"] = still_megas(wanted_blank, mega)
return res
7 changes: 6 additions & 1 deletion scripts/job/run_job.gd
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func run() -> int:
var pub := Publisher.new(store)
pub.candidates = keys
pub.only_missing = only_missing
pub.mega = map.mega
var changed_path := state_dir.path_join("changed-%d.txt" % map.id)
var per_run := args.integer("per-run", 8)
var limit := args.integer("limit")
Expand All @@ -97,8 +98,12 @@ func run() -> int:
return _fail("upload failed")
_record(changed_path, res["changed"])
_keep_png(args.text("keep-png"), tiles)
var still: Dictionary = res.get("still_megas", {})
for m in group:
done["%d,%d" % [m.x, m.y]] = true
if still.has(m):
Term.say("warning: mega tile %s still has %d missing tiles after rendering; not marked done" % [m, still[m]])
else:
done["%d,%d" % [m.x, m.y]] = true
_save_done(state_path, done)
n_done += group.size()
Term.say("megas %s..%s [%d/%d] render %.1fs upload %.1fs | %s" % [group[0], group[-1], n_done, todo.size(), (t1 - t0) / 1000.0, (Time.get_ticks_msec() - t1) / 1000.0, res["text"]])
Expand Down
8 changes: 7 additions & 1 deletion scripts/render/world_reader.gd
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,13 @@ func chunk(cx: int, cz: int) -> ChunkData:
c = ChunkData.decode(raw, cx, cz, table, registry)
stats["chunks"] += 1
stats["chunk_ms"] += (Time.get_ticks_usec() - t0) / 1000.0
_chunks[key] = c
# A chunk that could not be read or decoded is NOT cached. The world is live: a read can race the
# game saving the region file, and pinning that failure would blank the chunk's tiles for the rest
# of the run -- the publisher then treats them as legitimately empty and the hole survives every
# later pass over the same data. Absent chunks cost one header lookup per retry, which is cheap
# against the cached region image.
if c != null:
_chunks[key] = c
return c


Expand Down
49 changes: 49 additions & 0 deletions tests/run_tests.gd
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func _initialize() -> void:
_test_gpu_pack()
_test_scan()
_test_zoom_plan()
_test_world_reader_cache()
_test_still_megas()
_test_jar()
_test_registry()
_test_config()
Expand Down Expand Up @@ -427,6 +429,53 @@ func _test_zoom_plan() -> void:
_ok("zoom plan propagates %s to the next level" % p, plan["next_keys"].has(p))


func _region_two_chunks(body0: PackedByteArray, body1: PackedByteArray) -> PackedByteArray:
var rg := PackedByteArray()
rg.resize(8192 + 8192)
rg.fill(0)
rg[2] = 2
rg[3] = 1 # chunk (0,0) at sector 2
rg[6] = 3
rg[7] = 1 # chunk (1,0) at sector 3
for i in 2:
var body: PackedByteArray = [body0, body1][i]
var off := 8192 + i * 4096
var sp := _be()
sp.put_32(body.size() + 1)
sp.put_u8(3)
sp.put_data(body)
var frame := sp.data_array
for j in frame.size():
rg[off + j] = frame[j]
return rg


func _test_world_reader_cache() -> void:
var dir := TMP.path_join("reader")
DirAccess.make_dir_recursive_absolute(dir)
var garbage := PackedByteArray([0x63, 0x00, 0x00, 0xFF, 0x42])
var valid := _chunk_nbt([_section(0, ["minecraft:stone"])])
_write(dir.path_join("r.0.0.mca"), _region_two_chunks(garbage, valid))
var reader := WorldReader.new(ProjectSettings.globalize_path(dir), BlockTable.new())
reader.registry = _registry()
_eq("unreadable chunk reads as absent", reader.chunk(0, 0), null)
# Regression: a failed read must not be pinned in the cache. On a live world a read can race the
# game saving the region file; caching the failure blanked the chunk's tiles for the rest of a run
# and the holes survived every later pass (2026-11-10).
_ok("a failed chunk read is not cached", not reader._chunks.has(Vector2i(0, 0)))
_ok("valid chunk decodes", reader.chunk(1, 0) != null)
_ok("a decoded chunk is cached", reader._chunks.has(Vector2i(1, 0)))
_eq("absent chunk reads as absent", reader.chunk(5, 5), null)
_ok("an absent chunk is not cached either", not reader._chunks.has(Vector2i(5, 5)))


func _test_still_megas() -> void:
var blank_missing: Array[Vector2i] = [Vector2i(512, -21), Vector2i(513, -20), Vector2i(520, -21), Vector2i(-1, -1)]
var still := Publisher.still_megas(blank_missing, 8)
_eq("still-missing tiles group by mega tile", still, {Vector2i(64, -3): 2, Vector2i(65, -3): 1, Vector2i(-1, -1): 1})
_eq("no still-missing tiles, no mega tiles", Publisher.still_megas([], 8), {})


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


Expand Down
Loading