ludic/packages/ludic.things/chunks.ludic

48 lines
2 KiB
Text

# chunks.ludic - the Things of one chunk of the world, counted and let go together: what a streamed
# world asks when a chunk is loaded or dropped. A chunk is chunk_of(x, z, TH_CHUNK_M).
export function things_chunk(x: float, z: float) -> int { return chunk_of(x, z, TH_CHUNK_M) }
export function things_chunk_count(things_st: ThingsState, chunk: int) -> int { return ix_count(things_st.th.chunks, chunk) }
# every Thing standing in the chunk removed for good, the port and the facts told of each as
# thing_remove tells them; how many went
export function things_drop_chunk(things_st: mut ThingsState, chunk: int) -> int {
var n = 0
let rows = ix_rows(things_st.th.chunks, chunk)
while len(rows) > 0 {
thing_remove(things_st, things_st.th.tab.rec[rows[len(rows) - 1]])
n += 1
}
return n
}
# the chunk's Things that missed a dawn and a morning would now reach, restocked as that morning
# would have (no dice: a restock rolls none) and filed into the caller's list; how many
export function things_catch_up(things_st: ThingsState, chunk: int, day: int, out: []Thing) -> int {
List.clear(out)
let rows = ix_rows(things_st.th.chunks, chunk)
for i in 0 .. len(rows) {
let t = things_st.th.tab.rec[rows[i]]
if t.dawn >= day or thing_is_put_down(t) or not ThingsWorld.restocks(t.x, t.z) { continue }
th_restock_one(t, day)
kept_push(out, t)
}
return len(out)
}
# change detection: the tick now, the table moving on to the next - a consumer keeps what this
# returned and asks things_changed(since: it) for what was written after
export function things_now(things_st: mut ThingsState) -> int {
let t = tb_tick(things_st.th.tab)
tb_advance(things_st.th.tab)
return t
}
# every Thing a verb wrote after `since`, into the caller's list (untouched 64-row blocks skipped)
export function things_changed(things_st: ThingsState, since: int, rows: words, out: []Thing) -> int {
List.clear(out)
let tb = things_st.th.tab
let n = tb_changed_since(tb, since, rows)
for k in 0 .. n { kept_push(out, tb.rec[rows[k]]) }
return n
}