121 lines
4.6 KiB
Plaintext
121 lines
4.6 KiB
Plaintext
#! Contains how `std::serial` writes and reads an `entity`: as a number, its
|
|
#! bits, which name it only in its own world while the program runs; or, while
|
|
#! a program writes or reads a world as data of its own, as a scene file does,
|
|
#! by an `entity_numbering`, as the number the program gave it there, so a
|
|
#! component naming an entity names the one it was read as.
|
|
|
|
import std::serial;
|
|
module malunal::ecs;
|
|
|
|
#! The numbers a program writes entities as, where it writes a world's entities
|
|
#! as data of its own, as a scene file writes each by an id, and the entities
|
|
#! the numbers it reads stand for. While a numbering is applied, `std::serial`
|
|
#! writes and reads every entity by it.
|
|
#!
|
|
#! Each entity is given one number, and each number names one entity; giving
|
|
#! either another replaces what it had.
|
|
pub struct entity_numbering : owning {
|
|
var by_entity: idmap; # Each entity's place in `entries`, by its bits.
|
|
var by_number: idmap; # Each number's place in `entries`, by the number.
|
|
var entries: vector<numbered>;
|
|
}
|
|
|
|
# An entity, and the number it's given.
|
|
struct numbered {
|
|
var bits: u64;
|
|
var number: u64;
|
|
}
|
|
|
|
#! A numbering of no entities.
|
|
pub def entity_numbering::new(): entity_numbering {
|
|
return entity_numbering{ by_entity = idmap::new(), by_number = idmap::new(), entries = vector::<numbered>::new() };
|
|
}
|
|
|
|
pub def entity_numbering::close(&mut this): void {
|
|
this.by_entity.close();
|
|
this.by_number.close();
|
|
this.entries.close();
|
|
}
|
|
|
|
#! Gives an entity a number, which names it from now on.
|
|
pub def entity_numbering::set(&mut this, e: entity, number: u64): void {
|
|
val at := this.entries.length as u32;
|
|
this.entries.append(numbered{ bits = e.bits, number = number });
|
|
this.by_entity.set(e.bits, at);
|
|
this.by_number.set(number, at);
|
|
}
|
|
|
|
#! The number an entity was given, where it was given one.
|
|
pub def entity_numbering::number_of(&this, e: entity): maybe<u64> {
|
|
val at := this.by_entity.get(e.bits);
|
|
return nothing{} if at == absent;
|
|
return this.entries[at as u64].number;
|
|
}
|
|
|
|
#! The entity a number names, where it names one.
|
|
pub def entity_numbering::entity_of(&this, number: u64): maybe<entity> {
|
|
val at := this.by_number.get(number);
|
|
return nothing{} if at == absent;
|
|
return entity{ bits = this.entries[at as u64].bits };
|
|
}
|
|
|
|
# The numbering `std::serial` writes and reads entities by, or `null` where
|
|
# none is applied.
|
|
var applied_numbering: *entity_numbering = null;
|
|
|
|
#! Writes and reads every entity by the numbering, until what this gives closes,
|
|
#! as its scope ends, when the numbering applied before it is again. A numbering
|
|
#! is applied on one thread, and the program writes and reads entities on that
|
|
#! thread while it is.
|
|
#! @param this The numbering, which stays where it is while it's applied.
|
|
#! @returns What applies it while it's held.
|
|
pub def entity_numbering::apply(&this): numbering_applied {
|
|
val made := numbering_applied{ before = applied_numbering, held = true };
|
|
applied_numbering = this as *entity_numbering;
|
|
return made;
|
|
}
|
|
|
|
#! A numbering applied, until it closes.
|
|
pub struct numbering_applied : owning {
|
|
var before: *entity_numbering; # The numbering applied before, applied again as this closes.
|
|
var held: bool; # Whether it applies one, which a value moved from doesn't.
|
|
}
|
|
|
|
pub def numbering_applied::close(&mut this): void {
|
|
return if !this.held;
|
|
applied_numbering = this.before;
|
|
this.held = false;
|
|
}
|
|
|
|
#! Writes an entity as its number in the numbering applied, or as nothing where
|
|
#! it has none there, as one the data leaves out; and as its bits where no
|
|
#! numbering is applied. `entity::none` is nothing either way.
|
|
pub def entity::encode(&this, out: &mut serial::encoder): void {
|
|
if this.bits == 0 {
|
|
out.empty();
|
|
return;
|
|
}
|
|
if applied_numbering == null {
|
|
out.unsigned(this.bits);
|
|
return;
|
|
}
|
|
if through(applied_numbering).number_of(entity{ bits = this.bits }) is some(number) {
|
|
out.unsigned(number);
|
|
} else {
|
|
out.empty();
|
|
}
|
|
}
|
|
|
|
#! Reads an entity `encode` wrote: nothing as `entity::none`, and a number as
|
|
#! the entity it names in the numbering applied, which must name one, or as its
|
|
#! bits where none is applied.
|
|
pub def entity::decode(from: &serial::decoder, at: u32, given: serial::options): result<entity, serial::error> {
|
|
return entity::none if from.kind_of(at) == serial::kind::empty;
|
|
val number := from.as_u64(at) else (e) { return e; };
|
|
return entity{ bits = number } if applied_numbering == null;
|
|
val found := through(applied_numbering).entity_of(number) else {
|
|
return serial::error::at(serial::error_kind::out_of_range, from.place(at));
|
|
};
|
|
return found;
|
|
}
|