diff --git a/assets/cubyz/blocks/glacite.json b/assets/cubyz/blocks/glacite.json index 171886af6..3a3780906 100644 --- a/assets/cubyz/blocks/glacite.json +++ b/assets/cubyz/blocks/glacite.json @@ -1,6 +1,6 @@ { "class" : "stone", - "hardness" : 25, + "hardness" : 20, "breakingPower" : 1, "drops" : [ "auto" diff --git a/assets/cubyz/blocks/glacite_bricks.json b/assets/cubyz/blocks/glacite_bricks.json index 15746763f..95e25f69b 100644 --- a/assets/cubyz/blocks/glacite_bricks.json +++ b/assets/cubyz/blocks/glacite_bricks.json @@ -1,6 +1,6 @@ { "class" : "stone", - "hardness" : 25, + "hardness" : 20, "breakingPower" : 1, "drops" : [ "auto" diff --git a/assets/cubyz/blocks/glacite_tile.json b/assets/cubyz/blocks/glacite_tile.json new file mode 100644 index 000000000..3a1b9c1fa --- /dev/null +++ b/assets/cubyz/blocks/glacite_tile.json @@ -0,0 +1,11 @@ +{ + "class" : "stone", + "hardness" : 20, + "breakingPower" : 1, + "drops" : [ + "auto" + ], + "rotation" : "stairs", + "model" : "cubyz:cube", + "texture" : "cubyz:glacite_tile" +} diff --git a/assets/cubyz/blocks/limestone_tiles.json b/assets/cubyz/blocks/limestone_tiles.json new file mode 100644 index 000000000..940bd7045 --- /dev/null +++ b/assets/cubyz/blocks/limestone_tiles.json @@ -0,0 +1,10 @@ +{ + "class": "stone", + "hardness": 12, + "drops": [ + "auto" + ], + "rotation": "stairs", + "model": "cubyz:cube", + "texture": "cubyz:limestone_tiles" +} \ No newline at end of file diff --git a/assets/cubyz/blocks/marble_tile.json b/assets/cubyz/blocks/marble_tile.json new file mode 100644 index 000000000..fde07b726 --- /dev/null +++ b/assets/cubyz/blocks/marble_tile.json @@ -0,0 +1,10 @@ +{ + "class": "stone", + "hardness": 12, + "drops": [ + "auto" + ], + "rotation": "stairs", + "model": "cubyz:cube", + "texture": "cubyz:marble_tile" +} \ No newline at end of file diff --git a/assets/cubyz/blocks/sandstone_tile.json b/assets/cubyz/blocks/sandstone_tile.json new file mode 100644 index 000000000..6d2be26bd --- /dev/null +++ b/assets/cubyz/blocks/sandstone_tile.json @@ -0,0 +1,10 @@ +{ + "class": "stone", + "hardness": 12, + "drops": [ + "auto" + ], + "rotation": "stairs", + "model": "cubyz:cube", + "texture": "cubyz:sandstone_tile" +} \ No newline at end of file diff --git a/assets/cubyz/blocks/stone_tile.json b/assets/cubyz/blocks/stone_tile.json new file mode 100644 index 000000000..c2311361c --- /dev/null +++ b/assets/cubyz/blocks/stone_tile.json @@ -0,0 +1,10 @@ +{ + "class": "stone", + "hardness": 12, + "drops": [ + "auto" + ], + "rotation": "stairs", + "model": "cubyz:cube", + "texture": "cubyz:stone_tile" +} \ No newline at end of file diff --git a/assets/cubyz/blocks/textures/chalk/dark_grey.png b/assets/cubyz/blocks/textures/chalk/dark_grey.png index a38bf7e6b..de54e2384 100644 Binary files a/assets/cubyz/blocks/textures/chalk/dark_grey.png and b/assets/cubyz/blocks/textures/chalk/dark_grey.png differ diff --git a/assets/cubyz/blocks/textures/chalk/grey.png b/assets/cubyz/blocks/textures/chalk/grey.png index d31cb061e..5372998a7 100644 Binary files a/assets/cubyz/blocks/textures/chalk/grey.png and b/assets/cubyz/blocks/textures/chalk/grey.png differ diff --git a/assets/cubyz/blocks/textures/glacite_bricks.png b/assets/cubyz/blocks/textures/glacite_bricks.png index f9eaf78e0..f6bec2e01 100644 Binary files a/assets/cubyz/blocks/textures/glacite_bricks.png and b/assets/cubyz/blocks/textures/glacite_bricks.png differ diff --git a/assets/cubyz/blocks/textures/glacite_tile.png b/assets/cubyz/blocks/textures/glacite_tile.png new file mode 100644 index 000000000..40c21b68a Binary files /dev/null and b/assets/cubyz/blocks/textures/glacite_tile.png differ diff --git a/assets/cubyz/blocks/textures/limestone_bricks.png b/assets/cubyz/blocks/textures/limestone_bricks.png index 01d7e9422..e46215f32 100644 Binary files a/assets/cubyz/blocks/textures/limestone_bricks.png and b/assets/cubyz/blocks/textures/limestone_bricks.png differ diff --git a/assets/cubyz/blocks/textures/limestone_tiles.png b/assets/cubyz/blocks/textures/limestone_tiles.png new file mode 100644 index 000000000..da8d8ec1a Binary files /dev/null and b/assets/cubyz/blocks/textures/limestone_tiles.png differ diff --git a/assets/cubyz/blocks/textures/marble_bricks.png b/assets/cubyz/blocks/textures/marble_bricks.png index 053f795eb..474cd3d3d 100644 Binary files a/assets/cubyz/blocks/textures/marble_bricks.png and b/assets/cubyz/blocks/textures/marble_bricks.png differ diff --git a/assets/cubyz/blocks/textures/marble_tile.png b/assets/cubyz/blocks/textures/marble_tile.png new file mode 100644 index 000000000..3ff6e0709 Binary files /dev/null and b/assets/cubyz/blocks/textures/marble_tile.png differ diff --git a/assets/cubyz/blocks/textures/sandstone_bricks.png b/assets/cubyz/blocks/textures/sandstone_bricks.png index ae1133d58..08ba38a3b 100644 Binary files a/assets/cubyz/blocks/textures/sandstone_bricks.png and b/assets/cubyz/blocks/textures/sandstone_bricks.png differ diff --git a/assets/cubyz/blocks/textures/sandstone_tile.png b/assets/cubyz/blocks/textures/sandstone_tile.png new file mode 100644 index 000000000..881b3a838 Binary files /dev/null and b/assets/cubyz/blocks/textures/sandstone_tile.png differ diff --git a/assets/cubyz/blocks/textures/stone_bricks.png b/assets/cubyz/blocks/textures/stone_bricks.png index 22cff0e21..04474588f 100644 Binary files a/assets/cubyz/blocks/textures/stone_bricks.png and b/assets/cubyz/blocks/textures/stone_bricks.png differ diff --git a/assets/cubyz/blocks/textures/stone_tile.png b/assets/cubyz/blocks/textures/stone_tile.png new file mode 100644 index 000000000..3b4c8df91 Binary files /dev/null and b/assets/cubyz/blocks/textures/stone_tile.png differ diff --git a/assets/cubyz/blocks/textures/void_stone.png b/assets/cubyz/blocks/textures/void_stone.png index e26f59ab2..7699b90cd 100644 Binary files a/assets/cubyz/blocks/textures/void_stone.png and b/assets/cubyz/blocks/textures/void_stone.png differ diff --git a/assets/cubyz/blocks/textures/void_stone_bricks.png b/assets/cubyz/blocks/textures/void_stone_bricks.png index 3fa56d515..7ff963302 100644 Binary files a/assets/cubyz/blocks/textures/void_stone_bricks.png and b/assets/cubyz/blocks/textures/void_stone_bricks.png differ diff --git a/assets/cubyz/blocks/textures/water.png b/assets/cubyz/blocks/textures/water.png index 5ec2b04cf..e76c6b224 100644 Binary files a/assets/cubyz/blocks/textures/water.png and b/assets/cubyz/blocks/textures/water.png differ diff --git a/assets/cubyz/blocks/textures/water.png_reflectivity.png b/assets/cubyz/blocks/textures/water.png_reflectivity.png index c7ef01b59..f445d6545 100644 Binary files a/assets/cubyz/blocks/textures/water.png_reflectivity.png and b/assets/cubyz/blocks/textures/water.png_reflectivity.png differ diff --git a/assets/cubyz/blocks/textures/water.png_textureInfo.json b/assets/cubyz/blocks/textures/water.png_textureInfo.json index 077a498f7..2bcd13a6f 100644 --- a/assets/cubyz/blocks/textures/water.png_textureInfo.json +++ b/assets/cubyz/blocks/textures/water.png_textureInfo.json @@ -1,4 +1,6 @@ { "fogDensity" : 0.02, "fogColor" : 0x203860, + "frames" : 3, + "time" : 500, } diff --git a/assets/cubyz/blocks/textures/water_1.png b/assets/cubyz/blocks/textures/water_1.png deleted file mode 100644 index 1413176a2..000000000 Binary files a/assets/cubyz/blocks/textures/water_1.png and /dev/null differ diff --git a/assets/cubyz/blocks/textures/water_1.png_absorption.png b/assets/cubyz/blocks/textures/water_1.png_absorption.png deleted file mode 100644 index 19cabb9b7..000000000 Binary files a/assets/cubyz/blocks/textures/water_1.png_absorption.png and /dev/null differ diff --git a/assets/cubyz/blocks/textures/water_1.png_reflectivity.png b/assets/cubyz/blocks/textures/water_1.png_reflectivity.png deleted file mode 100644 index 12267ec23..000000000 Binary files a/assets/cubyz/blocks/textures/water_1.png_reflectivity.png and /dev/null differ diff --git a/assets/cubyz/blocks/textures/water_1.png_textureInfo.json b/assets/cubyz/blocks/textures/water_1.png_textureInfo.json deleted file mode 100644 index 077a498f7..000000000 --- a/assets/cubyz/blocks/textures/water_1.png_textureInfo.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "fogDensity" : 0.02, - "fogColor" : 0x203860, -} diff --git a/assets/cubyz/blocks/textures/water_2.png b/assets/cubyz/blocks/textures/water_2.png deleted file mode 100644 index f1c0edc6d..000000000 Binary files a/assets/cubyz/blocks/textures/water_2.png and /dev/null differ diff --git a/assets/cubyz/blocks/textures/water_2.png_absorption.png b/assets/cubyz/blocks/textures/water_2.png_absorption.png deleted file mode 100644 index 76c9edd23..000000000 Binary files a/assets/cubyz/blocks/textures/water_2.png_absorption.png and /dev/null differ diff --git a/assets/cubyz/blocks/textures/water_2.png_reflectivity.png b/assets/cubyz/blocks/textures/water_2.png_reflectivity.png deleted file mode 100644 index f8f2bed1d..000000000 Binary files a/assets/cubyz/blocks/textures/water_2.png_reflectivity.png and /dev/null differ diff --git a/assets/cubyz/blocks/textures/water_2.png_textureInfo.json b/assets/cubyz/blocks/textures/water_2.png_textureInfo.json deleted file mode 100644 index 077a498f7..000000000 --- a/assets/cubyz/blocks/textures/water_2.png_textureInfo.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "fogDensity" : 0.02, - "fogColor" : 0x203860, -} diff --git a/assets/cubyz/blocks/water.json b/assets/cubyz/blocks/water.json index 36879e41f..eae96eab3 100644 --- a/assets/cubyz/blocks/water.json +++ b/assets/cubyz/blocks/water.json @@ -11,12 +11,5 @@ "collide": false, "absorbedLight" : 0x090501, "model" : "cubyz:cube", - "texture" : { - "time" : 500, - "textures": [ - "cubyz:water", - "cubyz:water_1", - "cubyz:water_2" - ] - } + "texture" : "cubyz:water", } diff --git a/assets/cubyz/shaders/animation_pre_processing.glsl b/assets/cubyz/shaders/animation_pre_processing.glsl index e409ad2b4..e6112afc5 100644 --- a/assets/cubyz/shaders/animation_pre_processing.glsl +++ b/assets/cubyz/shaders/animation_pre_processing.glsl @@ -3,6 +3,7 @@ layout (local_size_x = 64, local_size_y = 1, local_size_z = 1) in; struct AnimationData { + uint startFrame; uint frames; uint time; }; @@ -22,5 +23,5 @@ uniform uint size; void main() { uint textureIndex = gl_GlobalInvocationID.x; if(textureIndex >= size) return; - animatedTexture[textureIndex] = textureIndex + time / animation[textureIndex].time % animation[textureIndex].frames; + animatedTexture[textureIndex] = animation[textureIndex].startFrame + time / animation[textureIndex].time % animation[textureIndex].frames; } \ No newline at end of file diff --git a/src/blocks.zig b/src/blocks.zig index a1edb7456..831351015 100644 --- a/src/blocks.zig +++ b/src/blocks.zig @@ -331,8 +331,9 @@ pub const Block = packed struct { // MARK: Block pub const meshes = struct { // MARK: meshes const AnimationData = extern struct { - frames: i32, - time: i32, + startFrame: u32, + frames: u32, + time: u32, }; const TextureData = extern struct { @@ -474,104 +475,90 @@ pub const meshes = struct { // MARK: meshes return pathBuffer[0..path.len+ending.len]; } - fn readAuxillaryTexture(_path: []const u8, pathBuffer: []u8, ending: []const u8, list: *main.List(Image), default: Image) void { + fn readAuxillaryTexture(_path: []const u8, pathBuffer: []u8, ending: []const u8, default: Image) Image { const path = extendedPath(_path, pathBuffer, ending); const texture = Image.readFromFile(arenaForWorld.allocator(), path) catch default; - list.append(texture); + return texture; + } + + fn extractAnimationSlice(image: Image, frame: usize, frames: usize) Image { + if(image.height < frames) return image; + var startHeight = image.height/frames*frame; + if(image.height%frames > frame) startHeight += frame + else startHeight += image.height%frames; + var endHeight = image.height/frames*(frame + 1); + if(image.height%frames > frame + 1) endHeight += frame + 1 + else endHeight += image.height%frames; + var result = image; + result.height = @intCast(endHeight - startHeight); + result.imageData = result.imageData[startHeight*image.width..endHeight*image.width]; + return result; } fn readTextureData(_path: []const u8) void { var buffer: [1024]u8 = undefined; @memcpy(buffer[0.._path.len], _path); const path = buffer[0.._path.len]; - blockTextures.append(Image.readFromFile(arenaForWorld.allocator(), path) catch Image.defaultImage); - readAuxillaryTexture(path, &buffer, "_emission.png", &emissionTextures, Image.emptyImage); - readAuxillaryTexture(path, &buffer, "_reflectivity.png", &reflectivityTextures, Image.emptyImage); - readAuxillaryTexture(path, &buffer, "_absorption.png", &absorptionTextures, Image.whiteEmptyImage); const textureInfoPath = extendedPath(path, &buffer, "_textureInfo.json"); const textureInfoJson = main.files.readToJson(main.stackAllocator, textureInfoPath) catch .JsonNull; defer textureInfoJson.free(main.stackAllocator); - textureFogData.append(.{ - .fogDensity = textureInfoJson.get(f32, "fogDensity", 0.0), - .fogColor = textureInfoJson.get(u32, "fogColor", 0xffffff), - }); + const animationFrames = textureInfoJson.get(u32, "frames", 1); + const animationTime = textureInfoJson.get(u32, "time", 1); + animation.append(.{.startFrame = @intCast(blockTextures.items.len), .frames = animationFrames, .time = animationTime}); + const base = Image.readFromFile(arenaForWorld.allocator(), path) catch Image.defaultImage; + const emission = readAuxillaryTexture(path, &buffer, "_emission.png", Image.emptyImage); + const reflectivity = readAuxillaryTexture(path, &buffer, "_reflectivity.png", Image.emptyImage); + const absorption = readAuxillaryTexture(path, &buffer, "_absorption.png", Image.whiteEmptyImage); + for(0..animationFrames) |i| { + blockTextures.append(extractAnimationSlice(base, i, animationFrames)); + emissionTextures.append(extractAnimationSlice(emission, i, animationFrames)); + reflectivityTextures.append(extractAnimationSlice(reflectivity, i, animationFrames)); + absorptionTextures.append(extractAnimationSlice(absorption, i, animationFrames)); + textureFogData.append(.{ + .fogDensity = textureInfoJson.get(f32, "fogDensity", 0.0), + .fogColor = textureInfoJson.get(u32, "fogColor", 0xffffff), + }); + } } - pub fn readTexture(textureInfo: JsonElement, assetFolder: []const u8) !u16 { + pub fn readTexture(textureId: []const u8, assetFolder: []const u8) !u16 { var result: u16 = undefined; - if(textureInfo == .JsonString or textureInfo == .JsonStringOwned) { - const resource = textureInfo.as([]const u8, ""); - var splitter = std.mem.splitScalar(u8, resource, ':'); - const mod = splitter.first(); - const id = splitter.rest(); - var buffer: [1024]u8 = undefined; - var path = try std.fmt.bufPrint(&buffer, "{s}/{s}/blocks/textures/{s}.png", .{assetFolder, mod, id}); - // Test if it's already in the list: - for(textureIDs.items, 0..) |other, j| { - if(std.mem.eql(u8, other, path)) { - result = @intCast(j); - return result; - } + var splitter = std.mem.splitScalar(u8, textureId, ':'); + const mod = splitter.first(); + const id = splitter.rest(); + var buffer: [1024]u8 = undefined; + var path = try std.fmt.bufPrint(&buffer, "{s}/{s}/blocks/textures/{s}.png", .{assetFolder, mod, id}); + // Test if it's already in the list: + for(textureIDs.items, 0..) |other, j| { + if(std.mem.eql(u8, other, path)) { + result = @intCast(j); + return result; } - const file = std.fs.cwd().openFile(path, .{}) catch |err| blk: { - if(err != error.FileNotFound) { - std.log.err("Could not open file {s}: {s}", .{path, @errorName(err)}); - } - path = try std.fmt.bufPrint(&buffer, "assets/{s}/blocks/textures/{s}.png", .{mod, id}); // Default to global assets. - break :blk std.fs.cwd().openFile(path, .{}) catch |err2| { - std.log.err("File not found. Searched in \"{s}\" and also in the assetFolder \"{s}\"", .{path, assetFolder}); - return err2; - }; + } + const file = std.fs.cwd().openFile(path, .{}) catch |err| blk: { + if(err != error.FileNotFound) { + std.log.err("Could not open file {s}: {s}", .{path, @errorName(err)}); + } + path = try std.fmt.bufPrint(&buffer, "assets/{s}/blocks/textures/{s}.png", .{mod, id}); // Default to global assets. + break :blk std.fs.cwd().openFile(path, .{}) catch |err2| { + std.log.err("File not found. Searched in \"{s}\" and also in the assetFolder \"{s}\"", .{path, assetFolder}); + return err2; }; - file.close(); // It was only openend to check if it exists. - // Otherwise read it into the list: - result = @intCast(blockTextures.items.len); + }; + file.close(); // It was only openend to check if it exists. + // Otherwise read it into the list: + result = @intCast(textureIDs.items.len); - textureIDs.append(arenaForWorld.allocator().dupe(u8, path)); - animation.append(.{.frames = 1, .time = 1}); - readTextureData(path); - } else if(textureInfo == .JsonObject) { - const animationTime = textureInfo.get(i32, "time", 500); - const textures = textureInfo.getChild("textures").toSlice(); - // Add the new textures into the list. Since this is an animation all textures that weren't found need to be replaced with undefined. - result = @intCast(blockTextures.items.len); - for(textures, 0..) |item, i| { - if(i == 0) { - animation.append(.{.frames = @intCast(textures.len), .time = animationTime}); - } else { - animation.append(.{.frames = 1, .time = 1}); - } - var splitter = std.mem.splitScalar(u8, item.as([]const u8, "cubyz:undefined"), ':'); - const mod = splitter.first(); - const id = splitter.rest(); - var buffer: [1024]u8 = undefined; - var path = try std.fmt.bufPrint(&buffer, "{s}/{s}/blocks/textures/{s}.png", .{assetFolder, mod, id}); - const file = std.fs.cwd().openFile(path, .{}) catch |err| blk: { - if(err != error.FileNotFound) { - std.log.err("Could not open file {s}: {s}", .{path, @errorName(err)}); - } - path = try std.fmt.bufPrint(&buffer, "assets/{s}/blocks/textures/{s}.png", .{mod, id}); // Default to global assets. - break :blk std.fs.cwd().openFile(path, .{}) catch |err2| { - std.log.err("File not found. Searched in \"{s}\" and also in the assetFolder \"{s}\"", .{path, assetFolder}); - return err2; - }; - }; - file.close(); // It was only openend to check if it exists. - - textureIDs.append(arenaForWorld.allocator().dupe(u8, path)); - readTextureData(path); - } - } else { - return error.NotSpecified; - } + textureIDs.append(arenaForWorld.allocator().dupe(u8, path)); + readTextureData(path); return result; } pub fn getTextureIndices(json: JsonElement, assetFolder: []const u8, textureIndicesRef: []u16) void { - const defaultIndex = readTexture(json.getChild("texture"), assetFolder) catch 0; + const defaultIndex = readTexture(json.get([]const u8, "texture", ""), assetFolder) catch 0; for(textureIndicesRef, sideNames) |*ref, name| { - const textureInfo = json.getChild(name); - ref.* = readTexture(textureInfo, assetFolder) catch defaultIndex; + const textureId = json.get([]const u8, name, ""); + ref.* = readTexture(textureId, assetFolder) catch defaultIndex; } } @@ -591,8 +578,8 @@ pub const meshes = struct { // MARK: meshes pub fn preProcessAnimationData(time: u32) void { animationShader.bind(); graphics.c.glUniform1ui(animationUniforms.time, time); - graphics.c.glUniform1ui(animationUniforms.size, @intCast(blockTextures.items.len)); - graphics.c.glDispatchCompute(@intCast(@divFloor(blockTextures.items.len + 63, 64)), 1, 1); // TODO: Replace with @divCeil once available + graphics.c.glUniform1ui(animationUniforms.size, @intCast(animation.items.len)); + graphics.c.glDispatchCompute(@intCast(@divFloor(animation.items.len + 63, 64)), 1, 1); // TODO: Replace with @divCeil once available graphics.c.glMemoryBarrier(graphics.c.GL_SHADER_STORAGE_BARRIER_BIT); } @@ -647,7 +634,7 @@ pub const meshes = struct { // MARK: meshes animationSSBO = SSBO.initStatic(AnimationData, animation.items); animationSSBO.?.bind(0); - animatedTextureSSBO = SSBO.initStaticSize(u32, blockTextures.items.len); + animatedTextureSSBO = SSBO.initStaticSize(u32, animation.items.len); animatedTextureSSBO.?.bind(1); fogSSBO = SSBO.initStatic(FogData, textureFogData.items); fogSSBO.?.bind(7); diff --git a/src/game.zig b/src/game.zig index 426b16d98..a10de47b2 100644 --- a/src/game.zig +++ b/src/game.zig @@ -313,7 +313,7 @@ pub const Player = struct { // MARK: Player pub var isGhost: Atomic(bool) = .init(false); pub var hyperSpeed: Atomic(bool) = .init(false); pub var mutex: std.Thread.Mutex = std.Thread.Mutex{}; - pub var inventory__SEND_CHANGES_TO_SERVER: Inventory = undefined; + pub var inventory: Inventory = undefined; pub var selectedSlot: u32 = 0; pub var maxHealth: f32 = 8; @@ -337,7 +337,7 @@ pub const Player = struct { // MARK: Player fn loadFrom(json: JsonElement) void { super.loadFrom(json); - inventory__SEND_CHANGES_TO_SERVER.loadFromJson(json.getChild("inventory")); + inventory.loadFromJson(json.getChild("inventory")); } pub fn setPosBlocking(newPos: Vec3d) void { @@ -395,12 +395,12 @@ pub const Player = struct { // MARK: Player return; } } - main.renderer.MeshSelection.placeBlock(&inventory__SEND_CHANGES_TO_SERVER.items[selectedSlot]); + main.renderer.MeshSelection.placeBlock(&inventory.items[selectedSlot]); } pub fn breakBlock() void { // TODO: Breaking animation and tools if(!main.Window.grabbed) return; - main.renderer.MeshSelection.breakBlock(&inventory__SEND_CHANGES_TO_SERVER.items[selectedSlot]); + main.renderer.MeshSelection.breakBlock(&inventory.items[selectedSlot]); } pub fn acquireSelectedBlock() void { @@ -413,8 +413,8 @@ pub const Player = struct { // MARK: Player // Check if there is already a slot with that item type for (0..12) |slotIdx| { - if (std.meta.eql(inventory__SEND_CHANGES_TO_SERVER.items[slotIdx].item, item)) { - inventory__SEND_CHANGES_TO_SERVER.items[slotIdx] = items.ItemStack {.item = item, .amount = items.itemList[idx].stackSize}; + if (std.meta.eql(inventory.items[slotIdx].item, item)) { + inventory.items[slotIdx] = items.ItemStack {.item = item, .amount = items.itemList[idx].stackSize}; selectedSlot = @intCast(slotIdx); isDone = true; break; @@ -422,15 +422,15 @@ pub const Player = struct { // MARK: Player } if (isDone) break; - if (inventory__SEND_CHANGES_TO_SERVER.items[selectedSlot].empty()) { - inventory__SEND_CHANGES_TO_SERVER.items[selectedSlot] = items.ItemStack {.item = item, .amount = items.itemList[idx].stackSize}; + if (inventory.items[selectedSlot].empty()) { + inventory.items[selectedSlot] = items.ItemStack {.item = item, .amount = items.itemList[idx].stackSize}; break; } // Look for an empty slot for (0..12) |slotIdx| { - if (inventory__SEND_CHANGES_TO_SERVER.items[slotIdx].empty()) { - inventory__SEND_CHANGES_TO_SERVER.items[slotIdx] = items.ItemStack {.item = item, .amount = items.itemList[idx].stackSize}; + if (inventory.items[slotIdx].empty()) { + inventory.items[slotIdx] = items.ItemStack {.item = item, .amount = items.itemList[idx].stackSize}; selectedSlot = @intCast(slotIdx); isDone = true; break; @@ -438,7 +438,7 @@ pub const Player = struct { // MARK: Player } if (isDone) break; - inventory__SEND_CHANGES_TO_SERVER.items[selectedSlot] = items.ItemStack {.item = item, .amount = items.itemList[idx].stackSize}; + inventory.items[selectedSlot] = items.ItemStack {.item = item, .amount = items.itemList[idx].stackSize}; break; } } @@ -471,7 +471,7 @@ pub const World = struct { // MARK: World .milliTime = std.time.milliTimestamp(), }; self.itemDrops.init(main.globalAllocator, self); - Player.inventory__SEND_CHANGES_TO_SERVER = Inventory.init(main.globalAllocator, 32); + Player.inventory = Inventory.init(main.globalAllocator, 32); network.Protocols.handShake.clientSide(self.conn, settings.playerName); main.Window.setMouseGrabbed(true); @@ -502,7 +502,7 @@ pub const World = struct { // MARK: World renderer.mesh_storage.deinit(); renderer.mesh_storage.init(); assets.unloadAssets(); - Player.inventory__SEND_CHANGES_TO_SERVER.deinit(main.globalAllocator); + Player.inventory.deinit(main.globalAllocator); } pub fn finishHandshake(self: *World, json: JsonElement) !void { diff --git a/src/gui/gui.zig b/src/gui/gui.zig index 408900077..f970c7a01 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -565,8 +565,8 @@ pub fn toggleGameMenu() void { main.Window.setMouseGrabbed(!main.Window.grabbed); if(main.Window.grabbed) { // Take of the currently held item stack and close some windows if(inventory.carriedItemStack.item) |item| { - inventory.carriedItemStack.amount = main.game.Player.inventory__SEND_CHANGES_TO_SERVER.addItem(item, inventory.carriedItemStack.amount); - main.network.Protocols.genericUpdate.sendInventory_full(main.game.world.?.conn, main.game.Player.inventory__SEND_CHANGES_TO_SERVER); // TODO(post-java): Add better options to the protocol. + inventory.carriedItemStack.amount = main.game.Player.inventory.addItem(item, inventory.carriedItemStack.amount); + main.network.Protocols.genericUpdate.sendInventory_full(main.game.world.?.conn, main.game.Player.inventory); // TODO(post-java): Add better options to the protocol. if(inventory.carriedItemStack.amount != 0) { main.network.Protocols.genericUpdate.itemStackDrop(main.game.world.?.conn, inventory.carriedItemStack, @floatCast(main.game.Player.getPosBlocking()), main.camera.direction, 20); } diff --git a/src/gui/windows/hotbar.zig b/src/gui/windows/hotbar.zig index 2e3bf3648..53d1e5249 100644 --- a/src/gui/windows/hotbar.zig +++ b/src/gui/windows/hotbar.zig @@ -42,36 +42,36 @@ var itemSlots: [12]*ItemSlot = undefined; pub fn tryAddingItems(index: usize, source: *ItemStack, desiredAmount: u16) void { Player.mutex.lock(); defer Player.mutex.unlock(); - const destination = &Player.inventory__SEND_CHANGES_TO_SERVER.items[index]; + const destination = &Player.inventory.items[index]; if(destination.item != null and !std.meta.eql(source.item, destination.item)) return; const actual = destination.add(source.item.?, desiredAmount); source.amount -= actual; if(source.amount == 0) source.item = null; - main.network.Protocols.genericUpdate.sendInventory_full(main.game.world.?.conn, Player.inventory__SEND_CHANGES_TO_SERVER); // TODO(post-java): Add better options to the protocol. + main.network.Protocols.genericUpdate.sendInventory_full(main.game.world.?.conn, Player.inventory); // TODO(post-java): Add better options to the protocol. } pub fn tryTakingItems(index: usize, destination: *ItemStack, desiredAmount: u16) void { var amount = desiredAmount; Player.mutex.lock(); defer Player.mutex.unlock(); - const source = &Player.inventory__SEND_CHANGES_TO_SERVER.items[index]; + const source = &Player.inventory.items[index]; if(destination.item != null and !std.meta.eql(source.item, destination.item)) return; if(source.item == null) return; amount = @min(amount, source.amount); const actual = destination.add(source.item.?, amount); source.amount -= actual; if(source.amount == 0) source.item = null; - main.network.Protocols.genericUpdate.sendInventory_full(main.game.world.?.conn, Player.inventory__SEND_CHANGES_TO_SERVER); // TODO(post-java): Add better options to the protocol. + main.network.Protocols.genericUpdate.sendInventory_full(main.game.world.?.conn, Player.inventory); // TODO(post-java): Add better options to the protocol. } pub fn trySwappingItems(index: usize, source: *ItemStack) void { Player.mutex.lock(); defer Player.mutex.unlock(); - const destination = &Player.inventory__SEND_CHANGES_TO_SERVER.items[index]; + const destination = &Player.inventory.items[index]; const swap = destination.*; destination.* = source.*; source.* = swap; - main.network.Protocols.genericUpdate.sendInventory_full(main.game.world.?.conn, Player.inventory__SEND_CHANGES_TO_SERVER); // TODO(post-java): Add better options to the protocol. + main.network.Protocols.genericUpdate.sendInventory_full(main.game.world.?.conn, Player.inventory); // TODO(post-java): Add better options to the protocol. } const vtable = ItemSlot.VTable { @@ -83,7 +83,7 @@ const vtable = ItemSlot.VTable { pub fn onOpen() void { const list = HorizontalList.init(); for(0..12) |i| { - itemSlots[i] = ItemSlot.init(.{0, 0}, Player.inventory__SEND_CHANGES_TO_SERVER.items[i], &vtable, i, .{.custom = hotbarSlotTexture}, .normal); + itemSlots[i] = ItemSlot.init(.{0, 0}, Player.inventory.items[i], &vtable, i, .{.custom = hotbarSlotTexture}, .normal); list.add(itemSlots[i]); } list.finish(.{0, 0}, .center); @@ -102,7 +102,7 @@ pub fn update() void { Player.mutex.lock(); defer Player.mutex.unlock(); for(&itemSlots, 0..) |slot, i| { - slot.updateItemStack(Player.inventory__SEND_CHANGES_TO_SERVER.items[i]); + slot.updateItemStack(Player.inventory.items[i]); } itemSlots[Player.selectedSlot].hovered = true; } \ No newline at end of file diff --git a/src/gui/windows/inventory.zig b/src/gui/windows/inventory.zig index 72ba274ce..391f09e4b 100644 --- a/src/gui/windows/inventory.zig +++ b/src/gui/windows/inventory.zig @@ -42,36 +42,36 @@ var itemSlots: [20]*ItemSlot = undefined; pub fn tryAddingItems(index: usize, source: *ItemStack, desiredAmount: u16) void { Player.mutex.lock(); defer Player.mutex.unlock(); - const destination = &Player.inventory__SEND_CHANGES_TO_SERVER.items[index]; + const destination = &Player.inventory.items[index]; if(destination.item != null and !std.meta.eql(source.item, destination.item)) return; const actual = destination.add(source.item.?, desiredAmount); source.amount -= actual; if(source.amount == 0) source.item = null; - main.network.Protocols.genericUpdate.sendInventory_full(main.game.world.?.conn, Player.inventory__SEND_CHANGES_TO_SERVER); // TODO(post-java): Add better options to the protocol. + main.network.Protocols.genericUpdate.sendInventory_full(main.game.world.?.conn, Player.inventory); // TODO(post-java): Add better options to the protocol. } pub fn tryTakingItems(index: usize, destination: *ItemStack, desiredAmount: u16) void { var amount = desiredAmount; Player.mutex.lock(); defer Player.mutex.unlock(); - const source = &Player.inventory__SEND_CHANGES_TO_SERVER.items[index]; + const source = &Player.inventory.items[index]; if(destination.item != null and !std.meta.eql(source.item, destination.item)) return; if(source.item == null) return; amount = @min(amount, source.amount); const actual = destination.add(source.item.?, amount); source.amount -= actual; if(source.amount == 0) source.item = null; - main.network.Protocols.genericUpdate.sendInventory_full(main.game.world.?.conn, Player.inventory__SEND_CHANGES_TO_SERVER); // TODO(post-java): Add better options to the protocol. + main.network.Protocols.genericUpdate.sendInventory_full(main.game.world.?.conn, Player.inventory); // TODO(post-java): Add better options to the protocol. } pub fn trySwappingItems(index: usize, source: *ItemStack) void { Player.mutex.lock(); defer Player.mutex.unlock(); - const destination = &Player.inventory__SEND_CHANGES_TO_SERVER.items[index]; + const destination = &Player.inventory.items[index]; const swap = destination.*; destination.* = source.*; source.* = swap; - main.network.Protocols.genericUpdate.sendInventory_full(main.game.world.?.conn, Player.inventory__SEND_CHANGES_TO_SERVER); // TODO(post-java): Add better options to the protocol. + main.network.Protocols.genericUpdate.sendInventory_full(main.game.world.?.conn, Player.inventory); // TODO(post-java): Add better options to the protocol. } const vtable = ItemSlot.VTable { @@ -93,7 +93,7 @@ pub fn onOpen() void { const row = HorizontalList.init(); for(0..10) |x| { const index: usize = 12 + y*10 + x; - const slot = ItemSlot.init(.{0, 0}, Player.inventory__SEND_CHANGES_TO_SERVER.items[index], &vtable, index, .default, .normal); + const slot = ItemSlot.init(.{0, 0}, Player.inventory.items[index], &vtable, index, .default, .normal); itemSlots[index - 12] = slot; row.add(slot); } @@ -115,6 +115,6 @@ pub fn update() void { Player.mutex.lock(); defer Player.mutex.unlock(); for(&itemSlots, 12..) |slot, i| { - slot.updateItemStack(Player.inventory__SEND_CHANGES_TO_SERVER.items[i]); + slot.updateItemStack(Player.inventory.items[i]); } } \ No newline at end of file diff --git a/src/gui/windows/inventory_crafting.zig b/src/gui/windows/inventory_crafting.zig index 0f4e436ab..fec03ee32 100644 --- a/src/gui/windows/inventory_crafting.zig +++ b/src/gui/windows/inventory_crafting.zig @@ -66,7 +66,7 @@ fn tryTakingItems(recipeIndex: usize, destination: *ItemStack, _: u16) void { if(!destination.canAddAll(resultItem.item.?, resultItem.amount)) return; for(recipe.sourceItems, recipe.sourceAmounts) |item, _amount| { var amount = _amount; - for(main.game.Player.inventory__SEND_CHANGES_TO_SERVER.items) |*itemStack| { + for(main.game.Player.inventory.items) |*itemStack| { if(itemStack.item) |invItem| { if(invItem == .baseItem and invItem.baseItem == item) { if(amount >= itemStack.amount) { @@ -94,7 +94,7 @@ fn findAvailableRecipes(list: *VerticalList) bool { amount.* = 0; } // Figure out what items are available in the inventory: - for(main.game.Player.inventory__SEND_CHANGES_TO_SERVER.items) |itemStack| { + for(main.game.Player.inventory.items) |itemStack| { addItemStackToAvailable(itemStack); } addItemStackToAvailable(gui.inventory.carriedItemStack); diff --git a/src/gui/windows/workbench.zig b/src/gui/windows/workbench.zig index 128f06831..58ebf3185 100644 --- a/src/gui/windows/workbench.zig +++ b/src/gui/windows/workbench.zig @@ -162,14 +162,14 @@ pub fn onClose() void { availableItems = undefined; for(&craftingGrid) |*itemStack| { if(!itemStack.empty()) { - itemStack.amount = main.game.Player.inventory__SEND_CHANGES_TO_SERVER.addItem(itemStack.item.?, itemStack.amount); + itemStack.amount = main.game.Player.inventory.addItem(itemStack.item.?, itemStack.amount); if(!itemStack.empty()) { main.network.Protocols.genericUpdate.itemStackDrop(main.game.world.?.conn, itemStack.*, .{0, 0, 0}, main.camera.direction, 20); itemStack.clear(); } } } - main.network.Protocols.genericUpdate.sendInventory_full(main.game.world.?.conn, Player.inventory__SEND_CHANGES_TO_SERVER); // TODO(post-java): Add better options to the protocol. + main.network.Protocols.genericUpdate.sendInventory_full(main.game.world.?.conn, Player.inventory); // TODO(post-java): Add better options to the protocol. craftingGrid = undefined; } diff --git a/src/items.zig b/src/items.zig index d9b84d1ad..f6fb68c67 100644 --- a/src/items.zig +++ b/src/items.zig @@ -1238,8 +1238,8 @@ pub const Inventory = struct { // MARK: Inventory return self.items[slot].item; } - pub fn getStack(self: Inventory, slot: usize) *ItemStack { - return &self.items[slot]; + pub fn getStack(self: Inventory, slot: usize) ItemStack { + return self.items[slot]; } pub fn getAmount(self: Inventory, slot: usize) u16 { diff --git a/src/network.zig b/src/network.zig index 1ab60ec98..9afa5c6a9 100644 --- a/src/network.zig +++ b/src/network.zig @@ -1048,9 +1048,9 @@ pub const Protocols = struct { }; game.Player.mutex.lock(); defer game.Player.mutex.unlock(); - const remaining = game.Player.inventory__SEND_CHANGES_TO_SERVER.addItem(item, json.get(u16, "amount", 0)); + const remaining = game.Player.inventory.addItem(item, json.get(u16, "amount", 0)); - sendInventory_full(conn, game.Player.inventory__SEND_CHANGES_TO_SERVER); + sendInventory_full(conn, game.Player.inventory); if(remaining != 0) { // Couldn't collect everything → drop it again. itemStackDrop(conn, ItemStack{.item=item, .amount=remaining}, game.Player.super.pos, Vec3f{0, 0, 0}, 0); diff --git a/src/renderer.zig b/src/renderer.zig index 432fead5f..1c383344c 100644 --- a/src/renderer.zig +++ b/src/renderer.zig @@ -210,7 +210,7 @@ pub fn renderWorld(world: *World, ambientLight: Vec3f, skyColor: Vec3f) void { / gpu_performance_measuring.startQuery(.chunk_rendering_preparation); const direction = crosshairDirection(lookMatrix, lastFov, lastWidth, lastHeight); - MeshSelection.select(cameraPos, direction, game.Player.inventory__SEND_CHANGES_TO_SERVER.items[game.Player.selectedSlot]); + MeshSelection.select(cameraPos, direction, game.Player.inventory.items[game.Player.selectedSlot]); MeshSelection.render(game.projectionMatrix, camera.viewMatrix, viewPos); chunk_meshing.beginRender();