Skip to content

Commit 700058f

Browse files
BoySanicIntegratedQuantum
authored andcommitted
Stop panic when blueprint doesn't exist for biome (#2195)
I just have it returning null if it can't find the blueprint and printing an error in the log. It should work the same as if the id was not found. Fixes #1898 Fixes #1932
1 parent b6caac3 commit 700058f

File tree

11 files changed

+38
-21
lines changed

11 files changed

+38
-21
lines changed

src/server/terrain/biomes.zig

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub const SimpleStructureModel = struct { // MARK: SimpleStructureModel
2020
water_surface,
2121
};
2222
const VTable = struct {
23-
loadModel: *const fn(arena: NeverFailingAllocator, parameters: ZonElement) *anyopaque,
23+
loadModel: *const fn(arena: NeverFailingAllocator, parameters: ZonElement) ?*anyopaque,
2424
generate: *const fn(self: *anyopaque, generationMode: GenerationMode, x: i32, y: i32, z: i32, chunk: *ServerChunk, caveMap: terrain.CaveMap.CaveMapView, biomeMap: terrain.CaveBiomeMap.CaveBiomeMapView, seed: *u64, isCeiling: bool) void,
2525
hashFunction: *const fn(self: *anyopaque) u64,
2626
generationMode: GenerationMode,
@@ -38,9 +38,13 @@ pub const SimpleStructureModel = struct { // MARK: SimpleStructureModel
3838
std.log.err("Couldn't find structure model with id {s}", .{id});
3939
return null;
4040
};
41+
const vtableModel = vtable.loadModel(arenaAllocator.allocator(), parameters) orelse {
42+
std.log.err("Error occurred while loading structure with id '{s}'. Dropping model from biome.", .{id});
43+
return null;
44+
};
4145
return SimpleStructureModel{
4246
.vtable = vtable,
43-
.data = vtable.loadModel(arenaAllocator.allocator(), parameters),
47+
.data = vtableModel,
4448
.chance = parameters.get(f32, "chance", 0.1),
4549
.priority = parameters.get(f32, "priority", 1),
4650
.generationMode = std.meta.stringToEnum(GenerationMode, parameters.get([]const u8, "generationMode", "")) orelse vtable.generationMode,
@@ -60,7 +64,7 @@ pub const SimpleStructureModel = struct { // MARK: SimpleStructureModel
6064

6165
pub fn registerGenerator(comptime Generator: type) void {
6266
var self: VTable = undefined;
63-
self.loadModel = main.utils.castFunctionReturnToAnyopaque(Generator.loadModel);
67+
self.loadModel = main.utils.castFunctionReturnToOptionalAnyopaque(Generator.loadModel);
6468
self.generate = main.utils.castFunctionSelfToAnyopaque(Generator.generate);
6569
self.hashFunction = main.utils.castFunctionSelfToAnyopaque(struct {
6670
fn hash(ptr: *Generator) u64 {

src/server/terrain/simple_structures/Boulder.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ block: main.blocks.Block,
2323
size: f32,
2424
sizeVariation: f32,
2525

26-
pub fn loadModel(arena: NeverFailingAllocator, parameters: ZonElement) *Boulder {
26+
pub fn loadModel(arena: NeverFailingAllocator, parameters: ZonElement) ?*Boulder {
2727
const self = arena.create(Boulder);
2828
self.* = .{
2929
.block = main.blocks.parseBlock(parameters.get([]const u8, "block", "cubyz:slate")),

src/server/terrain/simple_structures/FallenTree.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ topWoodBlock: u16,
2424
height0: u32,
2525
deltaHeight: u31,
2626

27-
pub fn loadModel(arena: NeverFailingAllocator, parameters: ZonElement) *FallenTree {
27+
pub fn loadModel(arena: NeverFailingAllocator, parameters: ZonElement) ?*FallenTree {
2828
const self = arena.create(FallenTree);
2929
self.* = .{
3030
.woodBlock = main.blocks.getTypeById(parameters.get([]const u8, "log", "cubyz:oak_log")),

src/server/terrain/simple_structures/FlowerPatch.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ width: f32,
2424
variation: f32,
2525
density: f32,
2626

27-
pub fn loadModel(arena: NeverFailingAllocator, parameters: ZonElement) *FlowerPatch {
27+
pub fn loadModel(arena: NeverFailingAllocator, parameters: ZonElement) ?*FlowerPatch {
2828
const self = arena.create(FlowerPatch);
2929
self.* = .{
3030
.block = main.blocks.parseBlock(parameters.get([]const u8, "block", "")),

src/server/terrain/simple_structures/GroundPatch.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ variation: f32,
2525
depth: i32,
2626
smoothness: f32,
2727

28-
pub fn loadModel(arena: NeverFailingAllocator, parameters: ZonElement) *GroundPatch {
28+
pub fn loadModel(arena: NeverFailingAllocator, parameters: ZonElement) ?*GroundPatch {
2929
const self = arena.create(GroundPatch);
3030
self.* = .{
3131
.block = main.blocks.parseBlock(parameters.get([]const u8, "block", "")),

src/server/terrain/simple_structures/SbbGen.zig

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ pub fn getHash(self: SbbGen) u64 {
2626
return std.hash.Wyhash.hash(@intFromEnum(self.placeMode), self.structureRef.id);
2727
}
2828

29-
pub fn loadModel(arena: NeverFailingAllocator, parameters: ZonElement) *SbbGen {
29+
pub fn loadModel(arena: NeverFailingAllocator, parameters: ZonElement) ?*SbbGen {
3030
const structureId = parameters.get(?[]const u8, "structure", null) orelse {
31-
main.utils.panicWithMessage("Error loading generator 'cubyz:sbb' structure field is mandatory.", .{});
31+
std.log.err("Error loading generator 'cubyz:sbb' structure field is mandatory.", .{});
32+
return null;
3233
};
3334
const structureRef = sbb.getByStringId(structureId) orelse {
34-
main.utils.panicWithMessage("Could not find structure building block with id '{s}'", .{structureId});
35+
std.log.err("Could not find blueprint with id {s}. Structure will not be added.", .{structureId});
36+
return null;
3537
};
3638
const rotationParam = parameters.getChild("rotation");
3739
const rotation = sbb.Rotation.fromZon(rotationParam) catch |err| blk: {

src/server/terrain/simple_structures/SimpleTreeModel.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ leafElongation: f32,
3636
deltaLeafElongation: f32,
3737
branched: bool,
3838

39-
pub fn loadModel(arena: NeverFailingAllocator, parameters: ZonElement) *SimpleTreeModel {
39+
pub fn loadModel(arena: NeverFailingAllocator, parameters: ZonElement) ?*SimpleTreeModel {
4040
const self = arena.create(SimpleTreeModel);
4141
self.* = .{
4242
.typ = std.meta.stringToEnum(Type, parameters.get([]const u8, "type", "")) orelse blk: {

src/server/terrain/simple_structures/SimpleVegetation.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ block: main.blocks.Block,
2323
height0: u31,
2424
deltaHeight: u31,
2525

26-
pub fn loadModel(arena: NeverFailingAllocator, parameters: ZonElement) *SimpleVegetation {
26+
pub fn loadModel(arena: NeverFailingAllocator, parameters: ZonElement) ?*SimpleVegetation {
2727
const self = arena.create(SimpleVegetation);
2828
self.* = .{
2929
.block = main.blocks.parseBlock(parameters.get([]const u8, "block", "")),

src/server/terrain/simple_structures/Stalagmite.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ block: main.blocks.Block,
2323
size: f32,
2424
sizeVariation: f32,
2525

26-
pub fn loadModel(arena: NeverFailingAllocator, parameters: ZonElement) *Stalagmite {
26+
pub fn loadModel(arena: NeverFailingAllocator, parameters: ZonElement) ?*Stalagmite {
2727
const self = arena.create(Stalagmite);
2828
self.* = .{
2929
.block = main.blocks.parseBlock(parameters.get([]const u8, "block", "cubyz:stalagmite")),

src/server/terrain/structure_building_blocks.zig

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -345,26 +345,25 @@ pub fn registerSBB(structures: *Assets.ZonHashMap) !void {
345345
std.debug.assert(structureList.items.len == 0);
346346
std.debug.assert(structureMap.capacity() == 0);
347347

348-
structureList.resize(arenaAllocator, structures.count());
348+
structureList.ensureCapacity(arenaAllocator, structures.count());
349349
structureMap.ensureTotalCapacity(arenaAllocator.allocator, structures.count()) catch unreachable;
350350

351351
childrenToResolve = .init(main.stackAllocator);
352352
defer childrenToResolve.deinit();
353353
{
354354
var iterator = structures.iterator();
355-
var index: u32 = 0;
355+
var loadedCount: u32 = 0;
356356
while(iterator.next()) |entry| {
357-
defer index += 1;
358-
359-
structureList.items[index] = StructureBuildingBlock.initFromZon(entry.key_ptr.*, entry.value_ptr.*) catch |err| {
357+
structureList.appendAssumeCapacity(StructureBuildingBlock.initFromZon(entry.key_ptr.*, entry.value_ptr.*) catch |err| {
360358
std.log.err("Could not register structure building block '{s}' ({s})", .{entry.key_ptr.*, @errorName(err)});
361359
continue;
362-
};
360+
});
363361

364362
const key = arenaAllocator.dupe(u8, entry.key_ptr.*);
365-
structureMap.put(arenaAllocator.allocator, key, @enumFromInt(index)) catch unreachable;
363+
structureMap.put(arenaAllocator.allocator, key, @enumFromInt(loadedCount)) catch unreachable;
366364

367365
std.log.debug("Registered structure building block: '{s}'", .{entry.key_ptr.*});
366+
loadedCount += 1;
368367
}
369368
}
370369
{

0 commit comments

Comments
 (0)