diff --git a/assets/cubyz/blocks/basalt.zig.zon b/assets/cubyz/blocks/basalt.zig.zon index 1d4f6ea1..3cff526a 100644 --- a/assets/cubyz/blocks/basalt.zig.zon +++ b/assets/cubyz/blocks/basalt.zig.zon @@ -15,6 +15,7 @@ }, }, }, + .allowOres = true, .breakingPower = 1, .rotation = .stairs, .model = "cubyz:cube", diff --git a/assets/cubyz/blocks/coal_ore.zig.zon b/assets/cubyz/blocks/coal_ore.zig.zon index 1db34ec8..29360b63 100644 --- a/assets/cubyz/blocks/coal_ore.zig.zon +++ b/assets/cubyz/blocks/coal_ore.zig.zon @@ -14,6 +14,7 @@ .drops = .{ .{.items = .{"cubyz:coal"}}, }, + .rotation = .ore, .model = "cubyz:cube", .texture = "cubyz:coal_ore", } diff --git a/assets/cubyz/blocks/diamond_ore.zig.zon b/assets/cubyz/blocks/diamond_ore.zig.zon index 9d4e9962..f58a22e0 100644 --- a/assets/cubyz/blocks/diamond_ore.zig.zon +++ b/assets/cubyz/blocks/diamond_ore.zig.zon @@ -14,6 +14,7 @@ .drops = .{ .{.items = .{"cubyz:diamond"}}, }, + .rotation = .ore, .model = "cubyz:cube", .texture = "cubyz:diamond_ore", } diff --git a/assets/cubyz/blocks/ferrock.zig.zon b/assets/cubyz/blocks/ferrock.zig.zon index 03b6598f..a17a219e 100644 --- a/assets/cubyz/blocks/ferrock.zig.zon +++ b/assets/cubyz/blocks/ferrock.zig.zon @@ -16,6 +16,7 @@ }, }, }, + .allowOres = true, .model = "cubyz:cube", .rotation = .stairs, .texture = "cubyz:ferrock", diff --git a/assets/cubyz/blocks/glacite.zig.zon b/assets/cubyz/blocks/glacite.zig.zon index 96ed5da8..160f2c23 100644 --- a/assets/cubyz/blocks/glacite.zig.zon +++ b/assets/cubyz/blocks/glacite.zig.zon @@ -16,6 +16,7 @@ }, }, }, + .allowOres = true, .rotation = .stairs, .model = "cubyz:cube", .texture = "cubyz:glacite", diff --git a/assets/cubyz/blocks/iron_ore.zig.zon b/assets/cubyz/blocks/iron_ore.zig.zon index 08e7b570..9705dd2f 100644 --- a/assets/cubyz/blocks/iron_ore.zig.zon +++ b/assets/cubyz/blocks/iron_ore.zig.zon @@ -14,6 +14,7 @@ .drops = .{ .{.items = .{.auto}}, }, + .rotation = .ore, .model = "cubyz:cube", .texture = "cubyz:iron_ore", } diff --git a/assets/cubyz/blocks/limestone.zig.zon b/assets/cubyz/blocks/limestone.zig.zon index 468e87db..09ac8116 100644 --- a/assets/cubyz/blocks/limestone.zig.zon +++ b/assets/cubyz/blocks/limestone.zig.zon @@ -16,6 +16,7 @@ }, }, }, + .allowOres = true, .rotation = .stairs, .model = "cubyz:cube", .texture = "cubyz:limestone", diff --git a/assets/cubyz/blocks/marble.zig.zon b/assets/cubyz/blocks/marble.zig.zon index 3b593527..1f4e2ccb 100644 --- a/assets/cubyz/blocks/marble.zig.zon +++ b/assets/cubyz/blocks/marble.zig.zon @@ -15,6 +15,7 @@ }, }, }, + .allowOres = true, .rotation= .stairs, .model= "cubyz:cube", .texture= "cubyz:marble", diff --git a/assets/cubyz/blocks/sandstone.zig.zon b/assets/cubyz/blocks/sandstone.zig.zon index f8ac9f43..fcc92e4f 100644 --- a/assets/cubyz/blocks/sandstone.zig.zon +++ b/assets/cubyz/blocks/sandstone.zig.zon @@ -15,6 +15,7 @@ }, }, }, + .allowOres = true, .rotation = .stairs, .model = "cubyz:cube", .texture = "cubyz:sandstone", diff --git a/assets/cubyz/blocks/stone.zig.zon b/assets/cubyz/blocks/stone.zig.zon index e1cea097..8f9e5e99 100644 --- a/assets/cubyz/blocks/stone.zig.zon +++ b/assets/cubyz/blocks/stone.zig.zon @@ -16,6 +16,7 @@ }, }, }, + .allowOres = true, .rotation = .stairs, .model = "cubyz:cube", .texture = "cubyz:stone", diff --git a/assets/cubyz/blocks/void_stone.zig.zon b/assets/cubyz/blocks/void_stone.zig.zon index 4ad08b8b..d0b018a9 100644 --- a/assets/cubyz/blocks/void_stone.zig.zon +++ b/assets/cubyz/blocks/void_stone.zig.zon @@ -16,6 +16,7 @@ }, }, }, + .allowOres = true, .rotation = .stairs, .model = "cubyz:cube", .texture = "cubyz:void_stone", diff --git a/src/blocks.zig b/src/blocks.zig index a8c79287..ede5ac15 100644 --- a/src/blocks.zig +++ b/src/blocks.zig @@ -47,15 +47,6 @@ pub const Ore = struct { maxHeight: i32, blockType: u16, - - sources: []u16, - - pub fn canCreateVeinInBlock(self: Ore, blockType: u16) bool { - for(self.sources) |source| { - if(blockType == source) return true; - } - return false; - } }; var _transparent: [maxBlockCount]bool = undefined; @@ -84,20 +75,18 @@ var _lodReplacement: [maxBlockCount]u16 = undefined; var _opaqueVariant: [maxBlockCount]u16 = undefined; var _friction: [maxBlockCount]f32 = undefined; +var _allowOres: [maxBlockCount]bool = undefined; + var reverseIndices = std.StringHashMap(u16).init(allocator.allocator); var size: u32 = 0; pub var ores: main.List(Ore) = .init(allocator); -var unfinishedOreSourceBlockIds: main.List([][]const u8) = undefined; - pub fn init() void { - unfinishedOreSourceBlockIds = .init(main.globalAllocator); } pub fn deinit() void { - unfinishedOreSourceBlockIds.deinit(); } pub fn register(_: []const u8, id: []const u8, zon: ZonElement) u16 { @@ -124,23 +113,20 @@ pub fn register(_: []const u8, id: []const u8, zon: ZonElement) u16 { _viewThrough[size] = zon.get(bool, "viewThrough", false) or _transparent[size] or _alwaysViewThrough[size]; _hasBackFace[size] = zon.get(bool, "hasBackFace", false); _friction[size] = zon.get(f32, "friction", 20); + _allowOres[size] = zon.get(bool, "allowOres", false); const oreProperties = zon.getChild("ore"); - if (oreProperties != .null) { - // Extract the ids: - const sourceBlocks = oreProperties.getChild("sources").toSlice(); - const oreIds = main.globalAllocator.alloc([]const u8, sourceBlocks.len); - for(sourceBlocks, oreIds) |source, *oreId| { - oreId.* = main.globalAllocator.dupe(u8, source.as([]const u8, "")); + if (oreProperties != .null) blk: { + if(!std.mem.eql(u8, zon.get([]const u8, "rotation", "no_rotation"), "ore")) { + std.log.err("Ore must have rotation mode \"ore\"!", .{}); + break :blk; } - unfinishedOreSourceBlockIds.append(oreIds); ores.append(Ore { .veins = oreProperties.get(f32, "veins", 0), .size = oreProperties.get(f32, "size", 0), .maxHeight = oreProperties.get(i32, "height", 0), .density = oreProperties.get(f32, "density", 0.5), .blockType = @intCast(size), - .sources = &.{}, }); } @@ -209,15 +195,6 @@ pub fn finishBlocks(zonElements: std.StringHashMap(ZonElement)) void { registerLodReplacement(i, zonElements.get(_id[i]) orelse continue); registerOpaqueVariant(i, zonElements.get(_id[i]) orelse continue); } - for(ores.items, unfinishedOreSourceBlockIds.items) |*ore, oreIds| { - ore.sources = allocator.alloc(u16, oreIds.len); - for(ore.sources, oreIds) |*source, id| { - source.* = getTypeById(id); - main.globalAllocator.free(id); - } - main.globalAllocator.free(oreIds); - } - unfinishedOreSourceBlockIds.clearRetainingCapacity(); } pub fn reset() void { @@ -226,7 +203,6 @@ pub fn reset() void { meshes.reset(); _ = arena.reset(.free_all); reverseIndices = .init(arena.allocator().allocator); - std.debug.assert(unfinishedOreSourceBlockIds.items.len == 0); } pub fn getTypeById(id: []const u8) u16 { @@ -349,6 +325,10 @@ pub const Block = packed struct { // MARK: Block return _friction[self.typ]; } + pub inline fn allowOres(self: Block) bool { + return _allowOres[self.typ]; + } + pub fn canBeChangedInto(self: Block, newBlock: Block, item: main.items.ItemStack) main.rotation.RotationMode.CanBeChangedInto { return newBlock.mode().canBeChangedInto(self, newBlock, item); } @@ -501,7 +481,11 @@ pub const meshes = struct { // MARK: meshes } pub inline fn textureIndex(block: Block, orientation: usize) u16 { - return textureData[block.typ].textureIndices[orientation]; + if(orientation < 16) { + return textureData[block.typ].textureIndices[orientation]; + } else { + return textureData[block.data].textureIndices[orientation - 16]; + } } fn extendedPath(_allocator: main.utils.NeverFailingAllocator, path: []const u8, ending: []const u8) []const u8 { diff --git a/src/models.zig b/src/models.zig index 6348efbd..7aa1b922 100644 --- a/src/models.zig +++ b/src/models.zig @@ -351,7 +351,7 @@ pub const Model = struct { main.globalAllocator.free(self.internalQuads); } - fn getRawFaces(model: Model, quadList: *main.List(QuadInfo)) void { + pub fn getRawFaces(model: Model, quadList: *main.List(QuadInfo)) void { for(model.internalQuads) |quadIndex| { quadList.append(quads.items[quadIndex]); } diff --git a/src/rotation.zig b/src/rotation.zig index 99f1fe5c..8d68c77b 100644 --- a/src/rotation.zig +++ b/src/rotation.zig @@ -953,6 +953,34 @@ pub const RotationModes = struct { return Torch.canBeChangedInto(oldBlock, newBlock, item); } }; + pub const Ore = struct { // MARK: Ore + pub const id: []const u8 = "ore"; + var modelCache: ?u16 = null; + + fn init() void {} + fn deinit() void {} + + pub fn createBlockModel(modelId: []const u8) u16 { + if(!std.mem.eql(u8, modelId, "cubyz:cube")) { + std.log.err("Ores can only be use on cube models.", .{modelId}); + } + if(modelCache) |modelIndex| return modelIndex; + + const baseModelIndex = main.models.getModelIndex("cubyz:cube"); + const baseModel = main.models.models.items[baseModelIndex]; + var quadList = main.List(main.models.QuadInfo).init(main.stackAllocator); + defer quadList.deinit(); + baseModel.getRawFaces(&quadList); + const len = quadList.items.len; + for(0..len) |i| { + quadList.append(quadList.items[i]); + quadList.items[i + len].textureSlot += 16; + } + const modelIndex = main.models.Model.init(quadList.items); + modelCache = modelIndex; + return modelIndex; + } + }; }; // MARK: init/register diff --git a/src/server/terrain/chunkgen/OreGenerator.zig b/src/server/terrain/chunkgen/OreGenerator.zig index d7291cf0..99f8f58e 100644 --- a/src/server/terrain/chunkgen/OreGenerator.zig +++ b/src/server/terrain/chunkgen/OreGenerator.zig @@ -105,8 +105,9 @@ fn considerCoordinates(ore: *const main.blocks.Ore, relX: f32, relY: f32, relZ: if(distSqr < 1) { // Add some roughness. The ore density gets smaller at the edges: if((1 - distSqr)*ore.density >= random.nextFloat(&veinSeed)) { - if(ore.canCreateVeinInBlock(chunk.getBlock(curX, curY, curZ).typ)) { - chunk.updateBlockInGeneration(curX, curY, curZ, .{.typ = ore.blockType, .data = 0}); + const stoneBlock = chunk.getBlock(curX, curY, curZ); + if(chunk.getBlock(curX, curY, curZ).allowOres()) { + chunk.updateBlockInGeneration(curX, curY, curZ, .{.typ = ore.blockType, .data = stoneBlock.typ}); } } }