Skip to content
Draft
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
14 changes: 14 additions & 0 deletions assets/cubyz/structure_tables/torches_everywhere.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.{
.id = "cubyz:torches_everywhere",
.biomeTags = .{},

.structures = .{
.{
.id = "cubyz:simple_vegetation",
.chance = 0.1,
.block = "cubyz:torch",
.height = 1,
.height_variation = 0,
},
},
}
35 changes: 32 additions & 3 deletions src/assets.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const NeverFailingAllocator = main.heap.NeverFailingAllocator;
const NeverFailingArenaAllocator = main.heap.NeverFailingArenaAllocator;
const ListUnmanaged = main.ListUnmanaged;
const files = main.files;
const structures_zig = main.server.terrain.structures;

var common: Assets = undefined;

Expand All @@ -29,6 +30,8 @@ pub const Assets = struct {
tools: ZonHashMap,
biomes: ZonHashMap,
biomeMigrations: AddonNameToZonMap,
structureTables: ZonHashMap,
structureTableMigrations: AddonNameToZonMap,
recipes: ZonHashMap,
models: BytesHashMap,
structureBuildingBlocks: ZonHashMap,
Expand All @@ -44,6 +47,8 @@ pub const Assets = struct {
.tools = .{},
.biomes = .{},
.biomeMigrations = .{},
.structureTables = .{},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding a new asset type, why not just add the structures directly to the biomes when loading the structure table?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that could work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm investigating this a little and it might be easiest to use the approach I took with a new asset type.

At least this way we can have assets load in the structure tables and have a place to put them.
Otherwise, I'm not sure how you'd load structure tables without adding another way to load assets that isn't the standard flow in assets.zig, which may not be preferable due to diverging ways to load assets.

.structureTableMigrations = .{},
.recipes = .{},
.models = .{},
.structureBuildingBlocks = .{},
Expand All @@ -59,6 +64,8 @@ pub const Assets = struct {
self.tools.deinit(allocator.allocator);
self.biomes.deinit(allocator.allocator);
self.biomeMigrations.deinit(allocator.allocator);
self.structureTables.deinit(allocator.allocator);
self.structureTableMigrations.deinit(allocator.allocator);
self.recipes.deinit(allocator.allocator);
self.models.deinit(allocator.allocator);
self.structureBuildingBlocks.deinit(allocator.allocator);
Expand All @@ -74,6 +81,8 @@ pub const Assets = struct {
.tools = self.tools.clone(allocator.allocator) catch unreachable,
.biomes = self.biomes.clone(allocator.allocator) catch unreachable,
.biomeMigrations = self.biomeMigrations.clone(allocator.allocator) catch unreachable,
.structureTables = self.structureTables.clone(allocator.allocator) catch unreachable,
.structureTableMigrations = self.structureTables.clone(allocator.allocator) catch unreachable,
.recipes = self.recipes.clone(allocator.allocator) catch unreachable,
.models = self.models.clone(allocator.allocator) catch unreachable,
.structureBuildingBlocks = self.structureBuildingBlocks.clone(allocator.allocator) catch unreachable,
Expand All @@ -90,6 +99,7 @@ pub const Assets = struct {
addon.readAllZon(allocator, "blocks", true, &self.blocks, &self.blockMigrations);
addon.readAllZon(allocator, "items", true, &self.items, &self.itemMigrations);
addon.readAllZon(allocator, "tools", true, &self.tools, null);
addon.readAllZon(allocator, "structure_tables", true, &self.structureTables, &self.structureTableMigrations);
addon.readAllZon(allocator, "biomes", true, &self.biomes, &self.biomeMigrations);
addon.readAllZon(allocator, "recipes", false, &self.recipes, null);
addon.readAllZon(allocator, "sbb", true, &self.structureBuildingBlocks, null);
Expand All @@ -100,8 +110,8 @@ pub const Assets = struct {
}
fn log(self: *Assets, typ: enum {common, world}) void {
std.log.info(
"Finished {s} assets reading with {} blocks, {} items, {} tools, {} biomes, {} recipes, {} structure building blocks, {} blueprints and {} particles",
.{@tagName(typ), self.blocks.count(), self.items.count(), self.tools.count(), self.biomes.count(), self.recipes.count(), self.structureBuildingBlocks.count(), self.blueprints.count(), self.particles.count()},
"Finished {s} assets reading with {} blocks, {} items, {} tools, {} biomes, {} structure tables, {} recipes, {} structure building blocks, {} blueprints and {} particles",
.{@tagName(typ), self.blocks.count(), self.items.count(), self.tools.count(), self.biomes.count(), self.structureTables.count(), self.recipes.count(), self.structureBuildingBlocks.count(), self.blueprints.count(), self.particles.count()},
);
}

Expand Down Expand Up @@ -320,6 +330,7 @@ fn createAssetStringID(
}

pub fn init() void {
structures_zig.init();
biomes_zig.init();

common = .init();
Expand Down Expand Up @@ -365,6 +376,10 @@ fn registerBiome(numericId: u32, stringId: []const u8, zon: ZonElement) void {
biomes_zig.register(stringId, numericId, zon);
}

fn registerStructureTable(numericId: u32, stringId: []const u8, zon: ZonElement) void {
if(zon == .null) std.log.err("Missing StructureTable: {s}. Will not replace.", .{stringId});
structures_zig.register(stringId, numericId, zon);
}
fn registerRecipesFromZon(zon: ZonElement) void {
items_zig.registerRecipes(zon);
}
Expand Down Expand Up @@ -473,7 +488,7 @@ pub const Palette = struct { // MARK: Palette

var loadedAssets: bool = false;

pub fn loadWorldAssets(assetFolder: []const u8, blockPalette: *Palette, itemPalette: *Palette, toolPalette: *Palette, biomePalette: *Palette) !void { // MARK: loadWorldAssets()
pub fn loadWorldAssets(assetFolder: []const u8, blockPalette: *Palette, itemPalette: *Palette, toolPalette: *Palette, biomePalette: *Palette, structureTablePalette: *Palette) !void { // MARK: loadWorldAssets()
if(loadedAssets) return; // The assets already got loaded by the server.
loadedAssets = true;

Expand Down Expand Up @@ -605,6 +620,20 @@ pub fn loadWorldAssets(assetFolder: []const u8, blockPalette: *Palette, itemPale
while(iterator.next()) |entry| {
particles_zig.ParticleManager.register(assetFolder, entry.key_ptr.*, entry.value_ptr.*);
}

// StructureTables:
var nextStructureTableNumericId: u32 = 0;
for(structureTablePalette.palette.items) |id| {
registerStructureTable(nextStructureTableNumericId, id, worldAssets.structureTables.get(id) orelse .null);
nextStructureTableNumericId += 1;
}
iterator = worldAssets.structureTables.iterator();
while(iterator.next()) |entry| {
if(structures_zig.hasRegistered(entry.key_ptr.*)) continue;
registerStructureTable(nextStructureTableNumericId, entry.key_ptr.*, entry.value_ptr.*);
structureTablePalette.add(entry.key_ptr.*);
nextStructureTableNumericId += 1;
}

// Biomes:
var nextBiomeNumericId: u32 = 0;
Expand Down
4 changes: 3 additions & 1 deletion src/game.zig
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,7 @@ pub const World = struct { // MARK: World
itemPalette: *assets.Palette = undefined,
toolPalette: *assets.Palette = undefined,
biomePalette: *assets.Palette = undefined,
structureTablePalette: *assets.Palette = undefined,
itemDrops: ClientItemDropManager = undefined,
playerBiome: Atomic(*const main.server.terrain.biomes.Biome) = undefined,

Expand Down Expand Up @@ -671,6 +672,7 @@ pub const World = struct { // MARK: World

pub fn finishHandshake(self: *World, zon: ZonElement) !void {
// TODO: Consider using a per-world allocator.
self.structureTablePalette = try assets.Palette.init(main.globalAllocator, zon.getChild("structureTablePalette"), null);
self.blockPalette = try assets.Palette.init(main.globalAllocator, zon.getChild("blockPalette"), "cubyz:air");
errdefer self.blockPalette.deinit();
self.biomePalette = try assets.Palette.init(main.globalAllocator, zon.getChild("biomePalette"), null);
Expand All @@ -683,7 +685,7 @@ pub const World = struct { // MARK: World

const path = std.fmt.allocPrint(main.stackAllocator.allocator, "{s}/serverAssets", .{main.files.cubyzDirStr()}) catch unreachable;
defer main.stackAllocator.free(path);
try assets.loadWorldAssets(path, self.blockPalette, self.itemPalette, self.toolPalette, self.biomePalette);
try assets.loadWorldAssets(path, self.blockPalette, self.itemPalette, self.toolPalette, self.biomePalette, self.structureTablePalette);
Player.id = zon.get(u32, "player_id", std.math.maxInt(u32));
Player.inventory = Inventory.init(main.globalAllocator, Player.inventorySize, .normal, .{.playerInventory = Player.id}, .{});
Player.loadFrom(zon.getChild("player"));
Expand Down
Loading
Loading