diff --git a/src/main.zig b/src/main.zig index 3a02099..9db5e35 100644 --- a/src/main.zig +++ b/src/main.zig @@ -5,6 +5,10 @@ const std = @import("std"); const spacetime = @import("spacetime.zig"); comptime { _ = spacetime; } +const stdb_math = @import("spacetime/math.zig"); +const DbVector2 = stdb_math.DbVector2; +const DbVector3 = stdb_math.DbVector3; + const START_PLAYER_MASS: u32 = 15; const START_PLAYER_SPEED: u32 = 10; const FOOD_MASS_MIN: u32 = 2; @@ -196,59 +200,6 @@ pub const spacespec = spacetime.Spec{ } }; -pub const DbVector2 = struct { - x: f32, - y: f32, - - pub fn sqr_magnitude(self: @This()) f32 { - return self.x * self.x + self.y * self.y; - } - - pub fn magnitude(self: @This()) f32 { - return @sqrt(self.sqr_magnitude()); - } - - pub fn normalized(self: @This()) DbVector2 { - const length = self.magnitude(); - return .{ - .x = self.x / length, - .y = self.y / length, - }; - } - - pub fn scale(self: @This(), val: f32) DbVector2 { - return .{ - .x = self.x * val, - .y = self.y * val, - }; - } - - pub fn add(self: @This(), other: DbVector2) DbVector2 { - return .{ - .x = self.x + other.x, - .y = self.y + other.y, - }; - } - - pub fn add_to(self: *@This(), other: DbVector2) void { - self.x += other.x; - self.y += other.y; - } - - pub fn sub(self: @This(), other: DbVector2) DbVector2 { - return .{ - .x = self.x - other.x, - .y = self.y - other.y, - }; - } - - pub fn sub_from(self: *@This(), other: DbVector2) void { - self.x -= other.x; - self.y -= other.y; - } - -}; - pub const Config = struct { id: u32, world_size: u64, diff --git a/src/spacetime.zig b/src/spacetime.zig index cc6a5f0..6352982 100644 --- a/src/spacetime.zig +++ b/src/spacetime.zig @@ -255,7 +255,7 @@ pub fn readArg(allocator: std.mem.Allocator, args: BytesSource, comptime t: type const string_buf = try allocator.alloc(u8, len); return try read_bytes_source(args, string_buf); }, - i8, u8, i16, u16, i32, u32, + bool, i8, u8, i16, u16, i32, u32, i64, u64, i128, u128, i256, u256, f32, f64 => { const read_type = t; @@ -299,6 +299,7 @@ pub fn zigTypeToSpacetimeType(comptime param: ?type) AlgebraicType { if(param == null) @compileError("Null parameter type passed to zigParamsToSpacetimeParams"); return switch(param.?) { []const u8 => .{ .String = {} }, + bool => .{ .Bool = {}, }, i32 => .{ .I32 = {}, }, i64 => .{ .I64 = {}, }, i128 => .{ .I128 = {}, }, @@ -597,6 +598,30 @@ pub const Spec = struct { reducers: []const SpecReducer, row_level_security: []const []const u8, includes: []const Spec = &.{}, + + pub fn getAllTable(self: @This()) []const Table { + var tables: []const Table = self.tables; + for(self.includes) |include| { + tables = tables ++ include.getAllTable(); + } + return tables; + } + + pub fn getAllReducers(self: @This()) []const SpecReducer { + var reducers: []const SpecReducer = self.reducers; + for(self.includes) |include| { + reducers = reducers ++ include.getAllReducers(); + } + return reducers; + } + + pub fn getAllRLS(self: @This()) []const []const u8 { + var row_level_security: []const []const u8 = self.row_level_security; + for(self.includes) |include| { + row_level_security = row_level_security ++ include.getAllRLS(); + } + return row_level_security; + } }; pub fn SpecBuilder(comptime spec: Spec) RawModuleDefV9 { @@ -612,7 +637,7 @@ pub fn SpecBuilder(comptime spec: Spec) RawModuleDefV9 { var structDecls: []const StructImpl = &[_]StructImpl{}; - for(spec.tables) |table| { + for(spec.getAllTable()) |table| { const table_name: []const u8 = table.name; const table_type: TableType = table.attribs.type; const table_access: TableAccess = table.attribs.access; @@ -760,7 +785,7 @@ pub fn SpecBuilder(comptime spec: Spec) RawModuleDefV9 { }; } - for(spec.reducers) |reducer| { + for(spec.getAllReducers()) |reducer| { const name: []const u8 = reducer.name; const lifecycle: Lifecycle = reducer.lifecycle; diff --git a/src/spacetime/math.zig b/src/spacetime/math.zig new file mode 100644 index 0000000..53ad992 --- /dev/null +++ b/src/spacetime/math.zig @@ -0,0 +1,112 @@ +pub const DbVector3 = struct { + x: f32, + y: f32, + z: f32, + + pub fn sqr_magnitude(self: @This()) f32 { + return self.x * self.x + self.y * self.y + self.z * self.z; + } + + pub fn magnitude(self: @This()) f32 { + return @sqrt(self.sqr_magnitude()); + } + + pub fn normalized(self: @This()) DbVector3 { + const length = self.magnitude(); + return .{ + .x = self.x / length, + .y = self.y / length, + .z = self.z / length, + }; + } + + pub fn scale(self: @This(), val: f32) DbVector3 { + return .{ + .x = self.x * val, + .y = self.y * val, + .z = self.z * val, + }; + } + + pub fn add(self: @This(), other: DbVector3) DbVector3 { + return .{ + .x = self.x + other.x, + .y = self.y + other.y, + .z = self.z + other.z, + }; + } + + pub fn add_to(self: *@This(), other: DbVector3) void { + self.x += other.x; + self.y += other.y; + self.z += other.z; + } + + pub fn sub(self: @This(), other: DbVector3) DbVector3 { + return .{ + .x = self.x - other.x, + .y = self.y - other.y, + .z = self.z - other.z, + }; + } + + pub fn sub_from(self: *@This(), other: DbVector3) void { + self.x -= other.x; + self.y -= other.y; + self.z -= other.z; + } + +}; + +pub const DbVector2 = struct { + x: f32, + y: f32, + + pub fn sqr_magnitude(self: @This()) f32 { + return self.x * self.x + self.y * self.y; + } + + pub fn magnitude(self: @This()) f32 { + return @sqrt(self.sqr_magnitude()); + } + + pub fn normalized(self: @This()) DbVector2 { + const length = self.magnitude(); + return .{ + .x = self.x / length, + .y = self.y / length, + }; + } + + pub fn scale(self: @This(), val: f32) DbVector2 { + return .{ + .x = self.x * val, + .y = self.y * val, + }; + } + + pub fn add(self: @This(), other: DbVector2) DbVector2 { + return .{ + .x = self.x + other.x, + .y = self.y + other.y, + }; + } + + pub fn add_to(self: *@This(), other: DbVector2) void { + self.x += other.x; + self.y += other.y; + } + + pub fn sub(self: @This(), other: DbVector2) DbVector2 { + return .{ + .x = self.x - other.x, + .y = self.y - other.y, + }; + } + + pub fn sub_from(self: *@This(), other: DbVector2) void { + self.x -= other.x; + self.y -= other.y; + } + +};