61 lines
1.8 KiB
Text
61 lines
1.8 KiB
Text
# ludic.base/queue.ludic - a mechanic reports what HAPPENED as facts in a queue it owns, and the
|
|
# game drains it in a later phase. First in, first out; nothing here rolls dice or reorders.
|
|
export property Queue<T> {
|
|
items: []T = null
|
|
id: int = -1
|
|
}
|
|
|
|
# every named queue's pending count, so a frame's end can say which facts nobody read
|
|
export state BaseState {
|
|
qb_names: []string = null
|
|
qb_pending: []int = null
|
|
cs_list: []System = null
|
|
}
|
|
|
|
function qb_init(base_st: mut BaseState) -> void {
|
|
if base_st.qb_names != null { return }
|
|
base_st.qb_names = new []string
|
|
base_st.qb_pending = new []int
|
|
}
|
|
|
|
# a queue; the name is only for core_undrained
|
|
export function queue_new<T>(base_st: mut BaseState, name: string) -> Queue<T> {
|
|
qb_init(base_st)
|
|
let q = new Queue<T>
|
|
q.items = new []T
|
|
q.id = len(base_st.qb_names)
|
|
push(base_st.qb_names, name)
|
|
push(base_st.qb_pending, 0)
|
|
return q
|
|
}
|
|
|
|
export function q_push<T>(base_st: mut BaseState, q: Queue<T>, v: T) -> void {
|
|
push(q.items, v)
|
|
base_st.qb_pending[q.id] = len(q.items)
|
|
}
|
|
|
|
# the facts in the order they were pushed; the queue is empty afterwards
|
|
export function q_drain<T>(base_st: mut BaseState, q: Queue<T>) -> []T {
|
|
let out = q.items
|
|
q.items = new []T
|
|
base_st.qb_pending[q.id] = 0
|
|
return out
|
|
}
|
|
|
|
export function q_len<T>(q: Queue<T>) -> int { return len(q.items) }
|
|
|
|
export function q_clear<T>(base_st: mut BaseState, q: Queue<T>) -> void {
|
|
q.items = new []T
|
|
base_st.qb_pending[q.id] = 0
|
|
}
|
|
|
|
# the names of the queues still holding facts; call it at the end of a frame, where a fact
|
|
# left behind is a route nobody wrote
|
|
export function core_undrained(base_st: mut BaseState) -> []string {
|
|
qb_init(base_st)
|
|
let out = new []string
|
|
for i in 0 .. len(base_st.qb_names) {
|
|
if base_st.qb_pending[i] > 0 { push(out, base_st.qb_names[i]) }
|
|
}
|
|
return out
|
|
}
|