-
Notifications
You must be signed in to change notification settings - Fork 197
Biome-independent structure tables #2129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 9 commits
b6150b0
ea6b957
123d767
347c2c7
a49226e
a070cb1
8c9b230
60ca1aa
891bf44
9d12362
a9a51be
c98831f
52ff7e4
bbe77df
f28973e
8a0c751
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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, | ||
| }, | ||
| }, | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -622,6 +622,7 @@ pub const World = struct { // MARK: World | |
| itemPalette: *assets.Palette = undefined, | ||
| toolPalette: *assets.Palette = undefined, | ||
| biomePalette: *assets.Palette = undefined, | ||
| structureTablePalette: *assets.Palette = undefined, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. with the sorting, a palette isn't needed |
||
| itemDrops: ClientItemDropManager = undefined, | ||
| playerBiome: Atomic(*const main.server.terrain.biomes.Biome) = undefined, | ||
|
|
||
|
|
@@ -667,6 +668,7 @@ pub const World = struct { // MARK: World | |
| self.itemPalette.deinit(); | ||
| self.toolPalette.deinit(); | ||
| self.biomePalette.deinit(); | ||
| self.structureTablePalette.deinit(); | ||
| self.manager.deinit(); | ||
| main.server.stop(); | ||
| if (main.server.thread) |serverThread| { | ||
|
|
@@ -682,6 +684,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); | ||
|
|
@@ -693,7 +696,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 = ClientInventory.init(main.globalAllocator, Player.inventorySize, .normal, .serverShared, .{.playerInventory = Player.id}, .{}); | ||
| Player.loadFrom(zon.getChild("player")); | ||
|
|
||
BoySanic marked this conversation as resolved.
Show resolved
Hide resolved
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -71,3 +71,72 @@ pub const SimpleStructureModel = struct { // MARK: SimpleStructureModel | |
| return self.vtable.hashFunction(self.data); | ||
| } | ||
| }; | ||
|
|
||
| pub const StructureTable = struct { | ||
| id: []const u8, | ||
| biomeTags: [][]const u8, | ||
| structures: []SimpleStructureModel = &.{}, | ||
BoySanic marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| paletteId: u32, | ||
BoySanic marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| pub fn init(self: *StructureTable, id: []const u8, paletteId: u32, zon: ZonElement) void { | ||
| const biome_tags = zon.getChild("biomeTags"); | ||
| var tags_list = main.ListUnmanaged([]const u8){}; | ||
| for (biome_tags.toSlice()) |tag| { | ||
| tags_list.append(main.worldArena, tag.toString(main.worldArena)); | ||
| } | ||
|
|
||
| self.* = .{ | ||
| .id = main.worldArena.dupe(u8, id), | ||
| .paletteId = paletteId, | ||
| .biomeTags = tags_list.items, | ||
| }; | ||
|
|
||
| const structures = zon.getChild("structures"); | ||
| var structure_list = main.ListUnmanaged(SimpleStructureModel){}; | ||
| var total_chance: f32 = 0; | ||
| defer structure_list.deinit(main.stackAllocator); | ||
|
|
||
| for (structures.toSlice()) |elem| { | ||
| if (SimpleStructureModel.initModel(elem)) |model| { | ||
| structure_list.append(main.stackAllocator, model); | ||
| total_chance += model.chance; | ||
| } | ||
| } | ||
| if (total_chance > 1) { | ||
| for (structure_list.items) |*model| { | ||
| model.chance /= total_chance; | ||
| } | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need this normalization here? I think it could change the chances in unexpected ways (.chance = 1 in structure table has a different meaning from .chance = 1 in the host biome) when combined with the host biome normalization or other structure tables. |
||
| self.structures = main.worldArena.dupe(SimpleStructureModel, structure_list.items); | ||
| } | ||
| }; | ||
|
|
||
| var structureTables: main.ListUnmanaged(StructureTable) = .{}; | ||
|
|
||
| pub fn init() void { | ||
| structureTables = .{}; | ||
BoySanic marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| pub fn register(id: []const u8, paletteId: u32, zon: ZonElement) void { | ||
| var structure_table: StructureTable = undefined; | ||
| structure_table.init(id, paletteId, zon); | ||
BoySanic marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| structureTables.append(main.worldArena, structure_table); | ||
| std.log.debug("Registered structure table: {d: >5} '{s}'", .{paletteId, id}); | ||
| } | ||
| pub fn hasRegistered(id: []const u8) bool { | ||
| if (structureTables.items.len == 0) return false; | ||
| for (structureTables.items) |entry| { | ||
BoySanic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (std.mem.eql(u8, id, entry.id)) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| pub fn getSlice() []StructureTable { | ||
| return structureTables.items; | ||
| } | ||
|
|
||
| pub fn reset() void { | ||
| structureTables = .{}; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.