From 11ebce62a33674dc6264bac91b7cc5b66a2fc0c2 Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Tue, 7 May 2024 16:35:27 -0700 Subject: [PATCH] module: rename Entities -> Database Signed-off-by: Stephen Gutekanst --- src/module/Archetype.zig | 4 ++-- src/module/entities.zig | 32 ++++++++++++++++---------------- src/module/main.zig | 2 +- src/module/module.zig | 20 ++++++++++---------- 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/module/Archetype.zig b/src/module/Archetype.zig index a86cc67a..bd96a57d 100644 --- a/src/module/Archetype.zig +++ b/src/module/Archetype.zig @@ -53,8 +53,8 @@ component_names: *StringTable, /// A hash composed of all Column.name's, effectively acting as the unique name of this table. hash: u64, -/// An index to Entities.archetypes, used in the event of a *bucket* hash collision (not a collision -/// of the .hash field) - see Entities.archetypeOrPut for details. +/// An index to Database.archetypes, used in the event of a *bucket* hash collision (not a collision +/// of the .hash field) - see Database.archetypeOrPut for details. next: ?u32 = null, pub fn deinit(storage: *Archetype, gpa: Allocator) void { diff --git a/src/module/entities.zig b/src/module/entities.zig index ad0889c3..b26d3062 100644 --- a/src/module/entities.zig +++ b/src/module/entities.zig @@ -14,7 +14,7 @@ const ModuleName = @import("module.zig").ModuleName; const ComponentNameM = @import("module.zig").ComponentNameM; const ComponentName = @import("module.zig").ComponentName; -/// An entity ID uniquely identifies an entity globally within an Entities set. +/// An entity ID uniquely identifies an entity globally within a Database. pub const EntityID = u64; fn byTypeId(context: void, lhs: Archetype.Column, rhs: Archetype.Column) bool { @@ -25,7 +25,7 @@ fn byTypeId(context: void, lhs: Archetype.Column, rhs: Archetype.Column) bool { /// A database of entities. For example, all player, monster, etc. entities in a game world. /// /// ``` -/// const world = Entities.init(allocator); // all entities in our world. +/// const world = Database.init(allocator); // all entities in our world. /// defer world.deinit(); /// /// const player1 = world.new(); // our first "player" entity @@ -67,13 +67,13 @@ fn byTypeId(context: void, lhs: Archetype.Column, rhs: Archetype.Column) bool { /// deleted are merely marked as "unused" and recycled /// /// Database equivalents: -/// * Entities is a database of tables, where each table represents a single archetype. +/// * Database is a database of tables, where each table represents a single archetype. /// * Archetype is a table, whose rows are entities and columns are components. /// * EntityID is a mere 32-bit array index, pointing to a 16-bit archetype table index and 32-bit /// row index, enabling entities to "move" from one archetype table to another seamlessly and /// making lookup by entity ID a few cheap array indexing operations. /// * ComponentStorage(T) is a column of data within a table for a single type of component `T`. -pub fn Entities(comptime modules: anytype) type { +pub fn Database(comptime modules: anytype) type { const component_types_by_name = ComponentTypesByName(modules){}; return struct { allocator: Allocator, @@ -104,7 +104,7 @@ pub fn Entities(comptime modules: anytype) type { /// of that table. That is, the entity's component values are stored at: /// /// ``` - /// Entities.archetypes.items[ptr.archetype_index].rows[ptr.row_index] + /// Database.archetypes.items[ptr.archetype_index].rows[ptr.row_index] /// ``` /// pub const Pointer = struct { @@ -271,9 +271,9 @@ pub fn Entities(comptime modules: anytype) type { /// Given a component name, returns its ID. A new ID is created if needed. /// - /// The set of components used is expected to be static for the lifetime of the Entities, + /// The set of components used is expected to be static for the lifetime of the Database, /// and as such this function allocates component names but there is no way to release that - /// memory until Entities.deinit() is called. + /// memory until Database.deinit() is called. pub fn componentNameString(entities: *Self, name_str: []const u8) StringTable.Index { return entities.component_names.indexOrPut(entities.allocator, name_str) catch @panic("TODO: implement stateful OOM"); } @@ -871,15 +871,15 @@ pub fn Entities(comptime modules: anytype) type { // TODO: move this type somewhere else pub fn ArchetypeIterator(comptime modules: anytype) type { - const EntitiesT = Entities(modules); + const DatabaseT = Database(modules); return struct { - entities: *EntitiesT, - query: EntitiesT.QueryDeprecated, + entities: *DatabaseT, + query: DatabaseT.QueryDeprecated, index: usize, const Self = @This(); - pub fn init(entities: *EntitiesT, query: EntitiesT.QueryDeprecated) Self { + pub fn init(entities: *DatabaseT, query: DatabaseT.QueryDeprecated) Self { return Self{ .entities = entities, .query = query, @@ -935,7 +935,7 @@ test { const modules = merge(.{ builtin_modules, }); - std.testing.refAllDeclsRecursive(Entities(modules)); + std.testing.refAllDeclsRecursive(Database(modules)); } // TODO: require "one big registration of components" even when using dynamic API? Would alleviate @@ -954,7 +954,7 @@ test "dynamic" { const Rotation = struct { degrees: f32 }; // Create a world. - var world = try Entities(merge(.{builtin_modules})).init(allocator); + var world = try Database(merge(.{builtin_modules})).init(allocator); defer world.deinit(); // Create an entity and add dynamic components. @@ -1002,7 +1002,7 @@ test "example" { //------------------------------------------------------------------------- // Create a world. - var world = try Entities(modules).init(allocator); + var world = try Database(modules).init(allocator); defer world.deinit(); //------------------------------------------------------------------------- @@ -1112,7 +1112,7 @@ test "example" { test "empty_world" { const allocator = testing.allocator; //------------------------------------------------------------------------- - var world = try Entities(merge(.{builtin_modules})).init(allocator); + var world = try Database(merge(.{builtin_modules})).init(allocator); // Create a world. defer world.deinit(); } @@ -1141,7 +1141,7 @@ test "many entities" { }); // Create many entities - var world = try Entities(modules).init(allocator); + var world = try Database(modules).init(allocator); defer world.deinit(); for (0..8192) |_| { const player = try world.new(); diff --git a/src/module/main.zig b/src/module/main.zig index 0784f086..fd4d2099 100644 --- a/src/module/main.zig +++ b/src/module/main.zig @@ -3,7 +3,7 @@ const mach = @import("../main.zig"); const testing = std.testing; pub const EntityID = @import("entities.zig").EntityID; -pub const Entities = @import("entities.zig").Entities; +pub const Database = @import("entities.zig").Database; pub const Archetype = @import("Archetype.zig"); pub const ModSet = @import("module.zig").ModSet; pub const Modules = @import("module.zig").Modules; diff --git a/src/module/module.zig b/src/module/module.zig index 3edf1c03..8388aaba 100644 --- a/src/module/module.zig +++ b/src/module/module.zig @@ -63,7 +63,7 @@ const builtin = @import("builtin"); const std = @import("std"); const testing = @import("../testing.zig"); -const Entities = @import("entities.zig").Entities; +const Database = @import("entities.zig").Database; const EntityID = @import("entities.zig").EntityID; const is_debug = @import("Archetype.zig").is_debug; const builtin_modules = @import("main.zig").builtin_modules; @@ -201,12 +201,12 @@ pub fn Modules(comptime modules: anytype) type { events: EventQueue, mod: ModsByName(modules), // TODO: pass mods directly instead of ComponentTypesByName? - entities: Entities(modules), + entities: Database(modules), debug_trace: bool, pub fn init(m: *@This(), allocator: std.mem.Allocator) !void { - // TODO: switch Entities to stack allocation like Modules is - var entities = try Entities(modules).init(allocator); + // TODO: switch Database to stack allocation like Modules is + var entities = try Database(modules).init(allocator); errdefer entities.deinit(); const debug_trace_str = std.process.getEnvVarOwned( @@ -579,13 +579,13 @@ pub fn ModSet(comptime modules: anytype) type { // The .entity module is a special builtin module, with its own unique API. return struct { /// Private/internal fields - __entities: *Entities(modules), + __entities: *Database(modules), __is_initialized: bool, __state: void, pub const IsInjectedArgument = void; - pub inline fn read(comptime component_name: ComponentNameM(M)) Entities(modules).ComponentQuery { + pub inline fn read(comptime component_name: ComponentNameM(M)) Database(modules).ComponentQuery { return .{ .read = .{ .module = M.name, .component = comptime stringToEnum(ComponentName(modules), @tagName(component_name)).?, @@ -634,7 +634,7 @@ pub fn ModSet(comptime modules: anytype) type { /// /// Whether you use `Foo.Mod.read()` or `Foo.Mod.write()` determines whether the /// slice in each iterator value will be mutable or not. - pub inline fn query(m: *@This(), comptime q: anytype) !Entities(modules).QueryResult(q) { + pub inline fn query(m: *@This(), comptime q: anytype) !Database(modules).QueryResult(q) { return m.__entities.query(q); } }; @@ -642,20 +642,20 @@ pub fn ModSet(comptime modules: anytype) type { return struct { /// Private/internal fields - __entities: *Entities(modules), + __entities: *Database(modules), __is_initialized: bool, __state: M, pub const IsInjectedArgument = void; - pub inline fn read(comptime component_name: ComponentNameM(M)) Entities(modules).ComponentQuery { + pub inline fn read(comptime component_name: ComponentNameM(M)) Database(modules).ComponentQuery { return .{ .read = .{ .module = M.name, .component = comptime stringToEnum(ComponentName(modules), @tagName(component_name)).?, } }; } - pub inline fn write(comptime component_name: ComponentNameM(M)) Entities(modules).ComponentQuery { + pub inline fn write(comptime component_name: ComponentNameM(M)) Database(modules).ComponentQuery { return .{ .write = .{ .module = M.name, .component = comptime stringToEnum(ComponentName(modules), @tagName(component_name)).?,