Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-10-30 09:55:40 -05:00
commit 42fdcf16e7
22 files changed
+5122

No files matched your search

+187
View File
@@ -0,0 +1,187 @@
#! Contains `table`: the entities having one set of components, an archetype,
#! each component a column of their values in the entities' order.
module ecs;
# A component's column of a table: a value for each of the table's entities.
struct column {
var data: *mut u8;
var size: u64;
var id: u32;
var info: *component_info;
var changed: u64; # The world's tick as it was last written, which `changed` filters read.
}
# What a table's `slot_of` gives for a component it has without a column: a
# tag, which holds nothing.
const tagged: u32 = 0xFFFFFFFE;
# The ticks of a component a table keeps of each of its entities, once a
# query asks what was added or changed of it: as each was given it, and as
# each's was last written.
struct ticks {
var id: u32;
var added: *mut u64;
var changed: *mut u64;
}
# The entities having one set of components. A row is an entity, and its
# values, one in each column; taking a row out moves the last into its place,
# so the rows stay side by side. What spawning, moving, and despawning reach
# comes first, so it's read together.
struct table : owning {
var columns: vector<column>;
var entities: *mut entity;
var count: u64;
var capacity: u64;
var ticked: vector<ticks>; # The ticks of each entity, of each component the world tracks.
var added: u64; # The world's tick as an entity last came into it.
var kin: bool; # Whether its entities are children or parents.
var ids: vector<u32>; # Its components, in order.
var slots: vector<u32>; # Each one's column, or `tagged`.
var hash: u64; # Of `ids`.
var next: u32; # The next table of the same hash, or `absent`.
var edges: idmap; # The tables an entity goes to as it gains or loses a component.
}
# The column of a component, `tagged` where it's a tag, or `absent` where the
# table hasn't the component.
def table::slot_of(&this, id: u32): u32 {
var low := 0u;
var high := this.ids.length;
while low < high {
val middle := (low + high) / 2;
val at := this.ids[middle];
if at == id {
return this.slots[middle];
}
if at < id {
low = middle + 1;
} else {
high = middle;
}
}
return absent;
}
def table::has(&this, id: u32): bool {
return this.slot_of(id) != absent;
}
# Where a row's value of a column is.
def table::at(&this, slot: u32, row: u64): *mut u8 {
val c := &this.columns[slot as u64];
return c.data + row * c.size;
}
# Gives the table room for a number of rows, doubling what it has.
def table::reserve(&mut this, wanted: u64): void {
return if wanted <= this.capacity;
var size := this.capacity == 0 ? 8u : this.capacity * 2;
while size < wanted {
size *= 2;
}
for i in 0..this.columns.length {
val c := &mut this.columns[i];
c.data = resized(c.data, size * c.size);
}
for i in 0..this.ticked.length {
val k := &mut this.ticked[i];
k.added = resized(k.added as *mut u8, size * 8) as *mut u64;
k.changed = resized(k.changed as *mut u8, size * 8) as *mut u64;
}
this.entities = resized(this.entities as *mut u8, size * 8) as *mut entity;
this.capacity = size;
}
# The ticks it keeps of a component, or `null` where the world doesn't track
# it, or the table hasn't it.
def table::ticks_of(&this, id: u32): *ticks {
for i in 0..this.ticked.length {
return address(&this.ticked[i]) if this.ticked[i].id == id;
}
return null;
}
# Starts keeping the ticks of a component of each entity, each the tick given,
# as the world starts tracking it.
def table::track(&mut this, id: u32, added: u64, changed: u64): void {
var made := ticks{ id = id };
if this.capacity > 0 {
made.added = raw_allocate(this.capacity * 8) as *mut u64;
made.changed = raw_allocate(this.capacity * 8) as *mut u64;
}
for row in 0..this.count {
made.added[row] = added;
made.changed[row] = changed;
}
this.ticked.append(made);
}
# Adds a row for an entity, whose values the caller writes, and gives it.
def table::push(&mut this, e: entity): u64 {
if this.count == this.capacity {
this.reserve(this.count + 1);
}
this.entities[this.count] = e;
this.count++;
return this.count - 1;
}
# Takes a row out, whose values were moved or closed already, moving the last
# row into its place. Gives the entity moved, or `entity::none` where the row
# was the last.
def table::take_out(&mut this, row: u64): entity {
val last := this.count - 1;
this.count = last;
return entity::none if row == last;
for i in 0..this.columns.length {
val c := &this.columns[i];
memcpy(c.data + row * c.size, c.data + last * c.size, c.size);
}
for i in 0..this.ticked.length {
val k := &this.ticked[i];
k.added[row] = k.added[last];
k.changed[row] = k.changed[last];
}
val moved := this.entities[last];
this.entities[row] = moved;
return moved;
}
# Closes the values of a row that close what they hold.
def table::close_row(&mut this, row: u64): void {
for i in 0..this.columns.length {
val c := &this.columns[i];
if c.info[0].closes {
c.info[0].dispose(c.data + row * c.size);
}
}
}
pub def table::close(&mut this): void {
for row in 0..this.count {
this.close_row(row);
}
for i in 0..this.columns.length {
raw_release(this.columns[i].data);
}
for i in 0..this.ticked.length {
raw_release(this.ticked[i].added as *mut u8);
raw_release(this.ticked[i].changed as *mut u8);
}
this.ticked.close();
raw_release(this.entities as *mut u8);
this.entities = null;
this.count = 0;
this.capacity = 0;
this.columns.close();
this.ids.close();
this.slots.close();
this.edges.close();
}
# A block made larger, or made where there was none.
def resized(at: *mut u8, size: u64): *mut u8 {
return at == null ? raw_allocate(size) : raw_resize(at, size);
}