Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions src/entity.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const Mat4f = vec.Mat4f;
const Vec3d = vec.Vec3d;
const Vec3f = vec.Vec3f;
const Vec4f = vec.Vec4f;
const BinaryReader = main.utils.BinaryReader;

pub const components = @import("entityComponent/_list.zig");

Expand All @@ -15,6 +16,13 @@ pub const EntityNetworkData = struct {
rot: Vec3f,
};

pub const ComponentActionType = enum(u8) {
unload = 0,
load = 1,
modifyServer = 2,
modifyClient = 3,
};

pub const EntityComponentLoadError = error{
DecodingBase64,
UnreadableId,
Expand All @@ -29,10 +37,12 @@ pub const Entity = enum(u32) {
};
pub const EntityComponentId = u32;
const EntityComponentVTable = struct {
serverLoad: *const fn (entity: Entity, reader: *main.utils.BinaryReader, version: u32) EntityComponentLoadError!void,
clientLoad: *const fn (entity: Entity, reader: *main.utils.BinaryReader, version: u32) EntityComponentLoadError!void,
serverLoad: *const fn (entity: Entity, reader: *BinaryReader, version: u32) EntityComponentLoadError!void,
clientLoad: *const fn (entity: Entity, reader: *BinaryReader, version: u32) EntityComponentLoadError!void,
serverUnload: *const fn (entity: Entity) void,
clientUnload: *const fn (entity: Entity) void,
serverModifyComponent: *const fn (entity: Entity, reader: *BinaryReader) void,
clientModifyComponent: *const fn (entity: Entity, reader: *BinaryReader) void,
};
var componentList: []?EntityComponentVTable = undefined;

Expand All @@ -51,6 +61,8 @@ pub fn initComponents() void {
.clientLoad = @field(components, decl.name).client.load,
.serverUnload = @field(components, decl.name).server.unload,
.clientUnload = @field(components, decl.name).client.unload,
.serverModifyComponent = @field(components, decl.name).server.modifyComponent,
.clientModifyComponent = @field(components, decl.name).client.modifyComponent,
};
} else {
std.log.err("entity components: Duplicate list id {}.", .{componentId});
Expand Down Expand Up @@ -97,6 +109,23 @@ pub fn unloadComponent(comptime side: main.sync.Side, componentId: EntityCompone
}
}

pub fn modifyComponent(comptime side: main.sync.Side, componentId: EntityComponentId, entity: Entity, componentData: []const u8) EntityComponentLoadError!void {
if (componentId >= componentList.len) {
std.log.err("unknown Component Id {} ", .{componentId});
return error.UnknownComponentId;
}
var componentReader = BinaryReader.init(componentData);
if (componentList[componentId]) |vtable| {
switch (side) {
.server => vtable.serverModifyComponent(entity, &componentReader),
.client => vtable.clientModifyComponent(entity, &componentReader),
}
} else {
std.log.err("unknown Component Id {} ", .{componentId});
return error.UnknownComponentId;
}
}

pub const client = struct {
pub fn init() void {
inline for (@typeInfo(components).@"struct".decls) |decl| {
Expand Down
22 changes: 22 additions & 0 deletions src/entityComponent/_template.zig
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ pub const entityComponentVersion = 0;

// ############################# Client only stuff ################################
pub const client = struct {
pub const ExampleComponent = struct {
pub fn save(self: ExampleComponent, writer: *utils.BinaryWriter, audience: main.entity.AudienceInfo) main.entity.ComponentSaveBehaviour {
_ = self;
_ = writer;
_ = audience;
// do i want to be saved?
return .save;
}
};
pub fn load(entity: Entity, reader: *utils.BinaryReader, version: u32) main.entity.EntityComponentLoadError!void {
_ = entity;
_ = reader;
Expand All @@ -44,6 +53,15 @@ pub const client = struct {
pub fn init() void {}
pub fn deinit() void {}
pub fn clear() void {}
pub fn get(entity: Entity) ?ExampleComponent {
_ = entity;
return null;
}

pub fn modifyComponent(entity: Entity, reader: *utils.BinaryReader) void {
_ = entity;
_ = reader;
}
};
// ############################# Server only stuff ################################
pub const server = struct {
Expand All @@ -70,4 +88,8 @@ pub const server = struct {
pub fn unload(entity: Entity) void {
_ = entity;
}
pub fn modifyComponent(entity: Entity, reader: *utils.BinaryReader) void {
_ = entity;
_ = reader;
}
};
16 changes: 16 additions & 0 deletions src/entityComponent/bag.zig
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ const playerBagSizeLimit = 120;
pub const client = struct {
const Component = struct {
bag: items.Inventory.BagInventory,
pub fn save(self: Component, writer: *utils.BinaryWriter, audience: main.entity.AudienceInfo) main.entity.ComponentSaveBehaviour {
if (audience != .disk and audience != .playerHimself) return .discard;
self.bag.toBytes(writer);
return .save;
}
};
pub var components: main.utils.SparseSet(Component, Entity) = .{};

Expand All @@ -48,6 +53,9 @@ pub const client = struct {
components.clear();
}

pub fn get(entity: Entity) ?Component {
return (components.get(entity) orelse return null).*;
}
pub fn getBag(entity: Entity) ?*items.Inventory.BagInventory {
return &(components.get(entity) orelse return null).bag;
}
Expand All @@ -62,6 +70,10 @@ pub const client = struct {
const bag = components.fetchRemove(entity) catch return;
bag.bag.deinit();
}
pub fn modifyComponent(entity: Entity, reader: *utils.BinaryReader) void {
_ = entity;
_ = reader;
}
};

// ############################# Server only stuff ################################
Expand Down Expand Up @@ -103,4 +115,8 @@ pub const server = struct {
const bag = components.fetchRemove(entity) catch return;
bag.bag.deinit();
}
pub fn modifyComponent(entity: Entity, reader: *utils.BinaryReader) void {
_ = entity;
_ = reader;
}
};
13 changes: 13 additions & 0 deletions src/entityComponent/model.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ pub const client = struct {

main.systems.systems.modelRenderer.client.nodeBuffer.free(self.bufferAllocation);
}
pub fn save(self: Component, writer: *utils.BinaryWriter, audience: main.entity.AudienceInfo) main.entity.ComponentSaveBehaviour {
_ = audience;
writer.writeVarInt(u32, self.entityModel.index);
return .save;
}
};
pub var components: main.utils.SparseSet(Component, Entity) = .{};

Expand Down Expand Up @@ -75,6 +80,10 @@ pub const client = struct {
pub fn get(entity: Entity) ?*Component {
return components.get(entity);
}
pub fn modifyComponent(entity: Entity, reader: *utils.BinaryReader) void {
_ = entity;
_ = reader;
}
};

// ############################# Server only stuff ################################
Expand Down Expand Up @@ -114,4 +123,8 @@ pub const server = struct {
pub fn get(entity: Entity) ?*const Component {
return components.get(entity);
}
pub fn modifyComponent(entity: Entity, reader: *utils.BinaryReader) void {
_ = entity;
_ = reader;
}
};
87 changes: 35 additions & 52 deletions src/entityComponent/permissions.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ pub const entityComponentVersion = 0;

// ############################# Client only stuff ################################
pub const client = struct {
pub const Component = struct {
pub fn save(self: Component, writer: *utils.BinaryWriter, audience: main.entity.AudienceInfo) main.entity.ComponentSaveBehaviour {
_ = self;
_ = writer;
_ = audience;
// do i want to be saved?
return .save;
}
};
pub fn load(entity: Entity, reader: *BinaryReader, version: u32) main.entity.EntityComponentLoadError!void {
_ = entity;
_ = reader;
Expand All @@ -23,22 +32,24 @@ pub const client = struct {
pub fn init() void {}
pub fn deinit() void {}
pub fn clear() void {}
pub fn get(entity: Entity) ?*Component {
_ = entity;
return null;
}

pub fn modifyComponent(entity: Entity, reader: *utils.BinaryReader) void {
_ = entity;
_ = reader;
}
};
// ############################# Server only stuff ################################
pub const server = struct {
pub const server = struct { // MARK: server
pub const Component = struct {
permissions: main.server.permission.Permissions,
permissionGroups: std.AutoHashMapUnmanaged(main.server.permission.Group, void),

pub fn save(self: Component, writer: *BinaryWriter, audience: main.entity.AudienceInfo) main.entity.ComponentSaveBehaviour {
if (audience != .disk) return .discard;
self.permissions.toBytes(writer);

writer.writeVarInt(usize, self.permissionGroups.count());
var it = self.permissionGroups.keyIterator();
while (it.next()) |group| {
group.toBytes(writer);
}
return .save;
}
};
Expand All @@ -60,25 +71,11 @@ pub const server = struct {
return &(components.get(entity) orelse return null).permissions;
}

pub fn getPermissionGroups(entity: Entity) ?*std.AutoHashMapUnmanaged(main.server.permission.Group, void) {
return &(components.get(entity) orelse return null).permissionGroups;
}

pub fn hasPermission(entity: Entity, permissionPath: []const u8) bool {
switch ((getPermissions(entity) orelse return false).hasPermission(permissionPath)) {
.yes => return true,
.no => return false,
.neutral => {},
}
var groupIt = (getPermissionGroups(entity).?).keyIterator();
while (groupIt.next()) |group| {
const result = group.hasPermission(permissionPath) catch blk: {
std.debug.assert(removeFromGroup(entity, group.*) == true);
break :blk .no;
};
if (result == .yes) return true;
}
return false;
return switch ((getPermissions(entity) orelse return false).hasPermission(permissionPath)) {
.yes => true,
.no, .neutral => false,
};
}

pub fn addPermission(entity: Entity, listType: main.server.permission.Permissions.ListType, permissionPath: []const u8) void {
Expand All @@ -89,39 +86,25 @@ pub const server = struct {
return (getPermissions(entity) orelse return false).removePermission(listType, permissionPath);
}

pub fn addToGroup(entity: Entity, group: main.server.permission.Group) void {
(getPermissionGroups(entity) orelse return).put(main.globalAllocator.allocator, group, {}) catch unreachable;
}

pub fn removeFromGroup(entity: Entity, group: main.server.permission.Group) bool {
return getPermissionGroups(entity).?.remove(group);
}

pub fn loadFromData(entity: Entity, reader: *BinaryReader, version: u32) main.entity.EntityComponentLoadError!void {
if (version != entityComponentVersion) return error.InvalidComponentVersion;
const component = components.add(main.globalAllocator, entity);
component.permissions = .init(main.globalAllocator);
component.permissions.fromBytes(reader) catch return error.UnreadableComponentData;
component.permissionGroups = .empty;
const len = reader.readVarInt(usize) catch return;
for (0..len) |_| {
const group = main.server.permission.Group.fromBytes(reader) catch |err| {
if (err == error.GroupNotFound) continue; // if the group is not found we just skip it.
return error.UnreadableComponentData;
};
addToGroup(entity, group);
}
const permissions = &components.add(main.globalAllocator, entity).permissions;
permissions.* = .init(main.globalAllocator);
permissions.fromBytes(reader) catch return error.UnreadableComponentData;
}

pub fn loadEmpty(entity: Entity) void {
const component = components.add(main.globalAllocator, entity);
component.permissions = .init(main.globalAllocator);
component.permissionGroups = .empty;
const permissions = &components.add(main.globalAllocator, entity).permissions;
permissions.* = .init(main.globalAllocator);
}

pub fn unload(entity: Entity) void {
var component = components.fetchRemove(entity) catch return;
component.permissions.deinit();
component.permissionGroups.deinit(main.globalAllocator.allocator);
const permissions = components.fetchRemove(entity) catch return;
permissions.permissions.deinit();
}

pub fn modifyComponent(entity: Entity, reader: *utils.BinaryReader) void {
_ = entity;
_ = reader;
}
};
15 changes: 15 additions & 0 deletions src/entityComponent/player.zig
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ pub const entityComponentVersion = 0;
pub const client = struct {
const Component = struct {
playerIndex: u32,
pub fn save(self: Component, writer: *utils.BinaryWriter, audience: main.entity.AudienceInfo) main.entity.ComponentSaveBehaviour {
writer.writeVarInt(u32, self.playerIndex);
if (audience == .disk) return .discard;
return .save;
}
};
pub var components: main.utils.SparseSet(Component, Entity) = .{};

Expand All @@ -51,6 +56,11 @@ pub const client = struct {
pub fn get(entity: Entity) ?*Component {
return components.get(entity);
}

pub fn modifyComponent(entity: Entity, reader: *utils.BinaryReader) void {
_ = entity;
_ = reader;
}
};

// ############################# Server only stuff ################################
Expand Down Expand Up @@ -92,4 +102,9 @@ pub const server = struct {
pub fn get(entity: Entity) ?*Component {
return components.get(entity);
}

pub fn modifyComponent(entity: Entity, reader: *utils.BinaryReader) void {
_ = entity;
_ = reader;
}
};
Loading
Loading