157 lines
4.9 KiB
Plaintext
157 lines
4.9 KiB
Plaintext
#! Contains what a world knows of when each entity was given a component, and
|
|
#! when its value was last written: ticks it keeps of the components some
|
|
#! query asks what was `added` or `changed` of, and of no others, so passes
|
|
#! over the rest cost nothing more.
|
|
|
|
module ecs;
|
|
|
|
# Starts keeping the ticks of a component of each entity, where it doesn't. Of
|
|
# the entities each table has, each tick is taken as the table's own, which no
|
|
# entity's is past, so no query misses what was added or changed before. While
|
|
# threads share the world, it starts once the sharing ends, as tables can't
|
|
# change while threads pass over them; till then, a pass asks of tables alone.
|
|
def world::track(&mut this, id: u32): void {
|
|
return if id == absent || this.tracks(id);
|
|
if this.sharing {
|
|
for i in 0..this.to_track.length {
|
|
return if this.to_track[i] == id;
|
|
}
|
|
this.to_track.append(id);
|
|
return;
|
|
}
|
|
while this.tracked.length <= id as u64 {
|
|
this.tracked.append(false);
|
|
}
|
|
this.tracked[id as u64] = true;
|
|
for i in 0..this.tables.length {
|
|
val t := &mut this.tables[i];
|
|
val slot := t.slot_of(id);
|
|
continue if slot == absent;
|
|
t.track(id, t.added, slot == tagged ? t.added : t.columns[slot as u64].changed);
|
|
}
|
|
}
|
|
|
|
# Starts keeping the ticks queries asked for while threads shared the world.
|
|
def world::track_asked(&mut this): void {
|
|
for i in 0..this.to_track.length {
|
|
this.track(this.to_track[i]);
|
|
}
|
|
this.to_track.clear();
|
|
}
|
|
|
|
# Whether the world keeps the ticks of a component of each entity.
|
|
def world::tracks(&this, id: u32): bool {
|
|
return id < this.tracked.length && this.tracked[id as u64];
|
|
}
|
|
|
|
# Notes an entity's value of a component written, as a tick says: its
|
|
# column's, which no entity's is past, and its own where the world tracks it.
|
|
def table::wrote(&mut this, slot: u32, id: u32, row: u64, now: u64): void {
|
|
this.columns[slot as u64].changed = now;
|
|
if this.ticked.length > 0 {
|
|
val k := this.ticks_of(id);
|
|
if k != null {
|
|
k[0].changed[row] = now;
|
|
}
|
|
}
|
|
}
|
|
|
|
# Notes every entity's value of a component written.
|
|
def table::wrote_all(&mut this, slot: u32, id: u32, now: u64): void {
|
|
this.columns[slot as u64].changed = now;
|
|
if this.ticked.length > 0 {
|
|
val k := this.ticks_of(id);
|
|
if k != null {
|
|
for row in 0..this.count {
|
|
k[0].changed[row] = now;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
# Notes a row's entity given every component of the table, as it's spawned.
|
|
def table::entered(&mut this, row: u64, now: u64): void {
|
|
this.added = now;
|
|
for i in 0..this.ticked.length {
|
|
val k := &mut this.ticked[i];
|
|
k.added[row] = now;
|
|
k.changed[row] = now;
|
|
}
|
|
}
|
|
|
|
# Notes the ticks of a row's entity come from another table: what it had there
|
|
# it keeps the ticks of, and what it has newly, it was given now.
|
|
def table::came_from(&mut this, other: &table, row: u64, from: u64, now: u64): void {
|
|
for i in 0..this.ticked.length {
|
|
val k := &mut this.ticked[i];
|
|
val known := other.ticks_of(k.id);
|
|
if known != null {
|
|
k.added[row] = known[0].added[from];
|
|
k.changed[row] = known[0].changed[from];
|
|
} else {
|
|
k.added[row] = now;
|
|
k.changed[row] = now;
|
|
}
|
|
}
|
|
}
|
|
|
|
# A tick a pass reads or writes of each row of a table.
|
|
struct tick_at {
|
|
var at: *mut u64;
|
|
}
|
|
|
|
# What a pass asks of each entity of a table it matched, as its query asks what
|
|
# was added or changed of each: the ticks that must be past its last pass, and
|
|
# those of what it writes, which it stamps.
|
|
struct sieve {
|
|
var asks: tick_at[12];
|
|
var asked: u64;
|
|
var stamps: tick_at[8];
|
|
var stamped: u64;
|
|
}
|
|
|
|
# What a query asks of each entity of a table it matched. A component the
|
|
# world doesn't track yet, as threads share it, is asked of by its table alone.
|
|
def world::sieve_of(&this, at: *mut query_state, m: u64): sieve {
|
|
val st := &through(at);
|
|
val t := &this.tables[st.matched[m] as u64];
|
|
var made: sieve;
|
|
return made if t.ticked.length == 0;
|
|
for i in 0..st.terms.length {
|
|
val term := &st.terms[i];
|
|
continue if term.id == absent;
|
|
val kind := term.kind;
|
|
if kind == term_kind::changed || kind == term_kind::added {
|
|
val k := t.ticks_of(term.id);
|
|
if k != null {
|
|
made.asks[made.asked] = tick_at{ at = kind == term_kind::added ? k[0].added : k[0].changed };
|
|
made.asked++;
|
|
}
|
|
} else if kind == term_kind::writes || kind == term_kind::maybe_writes {
|
|
val k := t.ticks_of(term.id);
|
|
if k != null {
|
|
made.stamps[made.stamped] = tick_at{ at = k[0].changed };
|
|
made.stamped++;
|
|
}
|
|
}
|
|
}
|
|
return made;
|
|
}
|
|
|
|
# Whether a row passes: each tick asked of is past the query's last pass, or
|
|
# the query never passed.
|
|
def sieve::passes(&this, row: u64, since: u64): bool {
|
|
return true if since == 0;
|
|
for i in 0..this.asked {
|
|
return false if this.asks[i].at[row] <= since;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
# Stamps a row's values a pass writes.
|
|
def sieve::stamp(&this, row: u64, now: u64): void {
|
|
for i in 0..this.stamped {
|
|
this.stamps[i].at[row] = now;
|
|
}
|
|
}
|