[Structure Building Block] Allow specifying empty / sentinel children (#1388)

* Allow sentinel child declarations

* Fix len check
This commit is contained in:
Krzysztof Wiśniewski 2025-05-03 14:23:57 +02:00 committed by GitHub
parent 1a1a503033
commit e1fc8deef8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 9 deletions

View File

@ -56,7 +56,7 @@ fn placeSbb(self: *SbbGen, structure: *const sbb.StructureBuildingBlock, placeme
rotated.blueprint.pasteInGeneration(pastePosition, chunk, self.placeMode);
for(rotated.childBlocks) |childBlock| {
const child = structure.pickChild(childBlock, seed);
const child = structure.pickChild(childBlock, seed) orelse continue;
placeSbb(self, child, pastePosition + childBlock.pos(), childBlock.direction(), chunk, seed);
}
}

View File

@ -137,7 +137,7 @@ pub const StructureBuildingBlock = struct {
pub fn getBlueprint(self: StructureBuildingBlock, rotation: Degrees) *BlueprintEntry {
return &self.blueprints[@intFromEnum(rotation)];
}
pub fn pickChild(self: StructureBuildingBlock, block: BlueprintEntry.StructureBlock, seed: *u64) *const StructureBuildingBlock {
pub fn pickChild(self: StructureBuildingBlock, block: BlueprintEntry.StructureBlock, seed: *u64) ?*const StructureBuildingBlock {
return self.children[block.index].sample(seed).structure;
}
};
@ -160,18 +160,16 @@ fn initChildTableFromZon(parentId: []const u8, colorName: []const u8, colorIndex
}
const Child = struct {
structure: *StructureBuildingBlock,
structure: ?*StructureBuildingBlock,
chance: f32,
fn initFromZon(parentId: []const u8, colorName: []const u8, colorIndex: usize, childIndex: usize, zon: ZonElement) !Child {
const structureId = zon.get([]const u8, "structure", "");
if(structureId.len == 0) {
std.log.err("['{s}'->'{s}'->'{d}'] Child node has empty structure field, parent structure will be discarded.", .{parentId, colorName, childIndex});
return error.EmptyStructureId;
const structureId = zon.get(?[]const u8, "structure", null);
if(structureId != null and structureId.?.len != 0) {
childrenToResolve.append(.{.parentId = parentId, .colorName = colorName, .colorIndex = colorIndex, .childIndex = childIndex, .structureId = structureId.?});
}
childrenToResolve.append(.{.parentId = parentId, .colorName = colorName, .colorIndex = colorIndex, .childIndex = childIndex, .structureId = structureId});
return .{
.structure = undefined,
.structure = null,
.chance = zon.get(f32, "chance", 1.0),
};
}