Files
ecs/ecs/keys.sakura
T
2026-10-29 11:27:53 -05:00

146 lines
5.3 KiB
Plaintext

#! Contains reaching components and resources by their types' keys rather than
#! their types, as a program does for code it runs that names a type the
#! program doesn't, as an engine running scripts does: a key, and the
#! bytes of a value, laid out as both build the type from one declaration.
module ecs;
#! The key a type of component is known by: a hash of its `path_of`, the same in
#! every world, and in every program declaring the type.
pub def component_key<T>(): u64 {
return info_of::<T>()[0].key;
}
#! What a world knows of a type of plain data, which closes nothing, made as a
#! program runs rather than while it compiles, as a program makes one for a
#! type of component a script declares. A world keeps its address, which, and
#! the texts it views, must outlast every world meeting it.
pub def component_info::plain(key: u64, name: strview, path: strview, size: u64, align: u64): component_info {
return component_info{
key = key,
name = name,
path = path,
size = size,
align = align,
closes = false,
dispose = close_nothing,
};
}
# Closes nothing, as plain data holds nothing to close.
def close_nothing(at: *mut u8): void {}
#! What the world knows of the type of a key, or `null` where it never met it.
pub def world::info_by_key(&this, key: u64): *component_info {
val id := this.by_key.get(key);
return id == absent ? null : this.infos[id as u64];
}
#! An entity's component of the type of a key, or `null` where it lacks one, or
#! it's a tag. It lasts until the entity next moves, as `get`'s does.
pub def world::get_by_key(&this, e: entity, key: u64): *mut u8 {
return this.value_of(e, this.by_key.get(key));
}
#! Whether an entity has a component of the type of a key.
pub def world::has_by_key(&this, e: entity, key: u64): bool {
return this.has_id(e, this.by_key.get(key));
}
#! Gives an entity a component, moving the bytes of a value in, which the
#! caller lets go of, or replaces the one it has, closing it: as `set` does, so
#! it's given once a pass over the world ends. The world may not have met the
#! type, whose `info` it keeps; one it met is the same size.
pub def world::set_by_info(&mut this, e: entity, info: *component_info, from: *mut u8): void {
if !this.alive(e) {
fault($"`set_by_info` was given {e.show()}, which isn't alive.");
}
if this.deferring > 0 {
# One it has is replaced where it is, at once.
val id := this.find_id(info);
val r: record = this.records[e.index as u64];
if id != absent && r.table != pending {
val t := &mut this.tables[r.table as u64];
val slot := t.slot_of(id);
if slot != absent {
if slot != tagged {
val at := t.at(slot, r.row as u64);
if info[0].closes {
info[0].dispose(at);
}
memcpy(at, from, info[0].size);
t.columns[slot as u64].changed = this.stamp();
}
return;
}
}
this.defer_put(e, info, from);
return;
}
this.set_bytes(e, info, from);
}
#! Takes an entity's component of the type of a key, closing it, once a pass
#! over the world ends, as `remove` does.
#! @returns Whether the entity had it.
pub def world::remove_by_key(&mut this, e: entity, key: u64): bool {
val id := this.by_key.get(key);
return false if !this.has_id(e, id);
if this.deferring > 0 {
this.defer(op::remove, e, this.infos[id as u64]);
} else {
this.remove_now(e, id);
}
return true;
}
#! The world's resource of the type of a key, or `null` where it holds none.
pub def world::resource_by_key(&mut this, key: u64): *mut u8 {
val found := this.by_resource.get(key);
return null if found == absent;
return this.resources[found as u64].data;
}
#! What the world knows of the type of a resource's key, or `null` where it
#! holds none.
pub def world::resource_info_by_key(&this, key: u64): *component_info {
val found := this.by_resource.get(key);
return found == absent ? null : this.resources[found as u64].info;
}
#! Gives the world a resource, moving the bytes of a value in, which the caller
#! lets go of, or replaces the one it holds, closing it, as `set_resource` does.
#! The world may not have met the type, whose `info` it keeps; one it met is the
#! same size.
pub def world::set_resource_by_info(&mut this, info: *component_info, from: *mut u8): void {
var found := this.by_resource.get(info[0].key);
if found == absent {
found = this.resources.length as u32;
this.resources.append(resource{ info = info });
this.by_resource.set(info[0].key, found);
}
val held := &mut this.resources[found as u64];
if held.info[0].size != info[0].size {
fault($"`set_resource_by_info` was given `{info[0].path}` of {info[0].size} bytes, which the world holds of {held.info[0].size}.");
}
if held.data == null {
held.data = raw_allocate(info[0].size == 0 ? 1 : info[0].size);
} else if held.info[0].closes {
held.info[0].dispose(held.data);
}
memcpy(held.data, from, info[0].size);
}
#! Appends each entity having a component of the type of a key, table by table.
pub def world::entities_by_key(&this, key: u64, into: &mut vector<entity>): void {
val id := this.by_key.get(key);
return if id == absent;
for i in 0..this.tables.length {
val t := &this.tables[i];
continue if !t.has(id);
for row in 0..t.count {
into.append(t.entities[row]);
}
}
}