Skip to content
Merged
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
3 changes: 3 additions & 0 deletions assets/cubyz/blocks/log/_defaults.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@
.rotation = "cubyz:log",
.model = "cubyz:cube",
.decayProhibitor = true,
.item = .{
.displayBlockData = 0b11,
},
}
6 changes: 6 additions & 0 deletions src/assets.zig
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,12 @@ pub fn loadWorldAssets(assetFolder: []const u8, blockPalette: *Palette, itemPale
try assignBlockItem(stringId);
}

for (items.itemList) |item| {
if (item.displayBlockData != null and item.block == null) {
std.log.err("displayBlockData field was set, but there is no block defined for item: '{s}'", .{item.id});
}
}

for (proceduralItemPalette.palette.items) |id| {
registerProceduralItem(assetFolder, id, worldAssets.proceduralItems.get(id) orelse .null);
}
Expand Down
6 changes: 4 additions & 2 deletions src/blocks.zig
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,12 @@ pub fn register(_: []const u8, id: []const u8, zon: ZonElement) u16 {
_id[size] = main.worldArena.dupe(u8, id);
reverseIndices.put(main.worldArena.allocator, _id[size], @intCast(size)) catch unreachable;

_mode[size] = rotation.getByID(zon.get([]const u8, "rotation") orelse "cubyz:no_rotation");
const rotationMode = rotation.getByID(zon.get([]const u8, "rotation") orelse "cubyz:no_rotation");

_mode[size] = rotationMode;
_blockHealth[size] = zon.get(f32, "blockHealth") orelse 1;
_blockResistance[size] = zon.get(f32, "blockResistance") orelse 0;
const rotation_tags = _mode[size].getBlockTags();
const rotation_tags = rotationMode.getBlockTags();
const block_tags = Tag.loadTagsFromZon(main.stackAllocator, zon.getChild("tags"));
defer main.stackAllocator.free(block_tags);
_tags[size] = std.mem.concat(main.worldArena.allocator, Tag, &.{rotation_tags, block_tags}) catch unreachable;
Expand Down
3 changes: 1 addition & 2 deletions src/graphics.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2319,8 +2319,7 @@ const block_texture = struct { // MARK: block_texture
}
};

pub fn generateBlockTexture(blockType: u16) Texture {
const block = main.blocks.Block{.typ = blockType, .data = 0}; // TODO: Use natural standard data.
pub fn generateBlockTexture(block: main.blocks.Block) Texture {
const textureSize = block_texture.textureSize;
c.glViewport(0, 0, textureSize, textureSize);

Expand Down
4 changes: 2 additions & 2 deletions src/itemdrop.zig
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ pub const ItemDropManager = struct { // MARK: ItemDropManager
const properties = .{
zon.get(Vec3d, "pos") orelse .{0, 0, 0},
zon.get(Vec3d, "vel") orelse .{0, 0, 0},

random.nextFloatVector(3, &main.seed)*@as(Vec3f, @splat(2*std.math.pi)),
items.ItemStack{.item = item, .amount = zon.get(u16, "amount") orelse 1},
zon.get(i32, "despawnTime") orelse 60,
Expand Down Expand Up @@ -546,8 +547,7 @@ pub const ItemDropRenderer = struct { // MARK: ItemDropRenderer
};
if (self.item == .baseItem and self.item.baseItem.block() != null and self.item.baseItem.image().imageData.ptr == graphics.Image.defaultImage.imageData.ptr) {
// Find sizes and free index:
var block = blocks.Block{.typ = self.item.baseItem.block().?, .data = 0};
block.data = block.mode().naturalStandard;
const block = self.item.baseItem.getDisplayBlock().?;
const model = blocks.meshes.model(block).model();
var data: main.ListManaged(u32) = .init(main.stackAllocator);
defer data.deinit();
Expand Down
20 changes: 17 additions & 3 deletions src/items.zig
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,9 @@ pub const BaseItemIndex = enum(u16) { // MARK: BaseItemIndex
pub fn getTooltip(self: BaseItemIndex) []const u8 {
return itemList[@intFromEnum(self)].getTooltip();
}
pub fn getDisplayBlock(self: BaseItemIndex) ?Block {
return itemList[@intFromEnum(self)].getDisplayBlock();
}
};

pub const BaseItem = struct { // MARK: BaseItem
Expand All @@ -319,6 +322,7 @@ pub const BaseItem = struct { // MARK: BaseItem
stackSize: u16,
material: ?Material,
block: ?u16,
displayBlockData: ?u16,
foodValue: f32, // TODO: Effects.

fn init(self: *BaseItem, allocator: NeverFailingAllocator, texturePath: []const u8, replacementTexturePath: []const u8, colorTexturePath: []const u8, colorReplacementTexturePath: []const u8, id: []const u8, zon: ZonElement) void {
Expand All @@ -344,6 +348,8 @@ pub const BaseItem = struct { // MARK: BaseItem
self.block = blk: {
break :blk blocks.getTypeById(zon.get([]const u8, "block") orelse break :blk null);
};
self.displayBlockData = zon.get(u16, "displayBlockData");

self.texture = null;
self.foodValue = zon.get(f32, "food") orelse 0;

Expand Down Expand Up @@ -378,8 +384,8 @@ pub const BaseItem = struct { // MARK: BaseItem
pub fn getTexture(self: *BaseItem) graphics.Texture {
if (self.texture == null) {
if (self.image.imageData.ptr == graphics.Image.defaultImage.imageData.ptr) {
if (self.block) |blockType| {
self.texture = graphics.generateBlockTexture(blockType);
if (self.getDisplayBlock()) |block| {
self.texture = graphics.generateBlockTexture(block);
} else {
self.texture = graphics.Texture.init();
self.texture.?.generate(self.image);
Expand All @@ -402,6 +408,14 @@ pub const BaseItem = struct { // MARK: BaseItem
}
return false;
}

pub fn getDisplayBlock(self: *const BaseItem) ?Block {
if (self.block) |blockType| {
const data = if (self.displayBlockData) |d| d else (Block{.typ = blockType, .data = 0}).mode().naturalStandard;
return .{.typ = blockType, .data = data};
}
return null;
}
};

const allNeighborOffsets = [_][2]i8{.{-1, -1}, .{0, -1}, .{1, -1}, .{-1, 0}, .{1, 0}, .{-1, 1}, .{0, 1}, .{1, 1}};
Expand Down Expand Up @@ -1381,8 +1395,8 @@ var proceduralItemTypeIdToIndex: std.StringHashMapUnmanaged(ProceduralItemTypeIn
var reverseIndices: std.StringHashMapUnmanaged(BaseItemIndex) = .{};
var modifiers: std.StringHashMapUnmanaged(*const Modifier.VTable) = .{};
var modifierRestrictions: std.StringHashMapUnmanaged(*const ModifierRestriction.VTable) = .{};
pub var itemList: [65536]BaseItem = undefined;
pub var itemListSize: u16 = 0;
pub var itemList: [65536]BaseItem = undefined;

// Due to migrations multiple indices can map to the same item. This must be resolved during inventory loading using this map.
var itemDeduplicationMap: [65536]BaseItemIndex = undefined;
Expand Down
17 changes: 17 additions & 0 deletions src/zon.zig
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,21 @@ const Parser = struct { // MARK: Parser
}
return .{.int = sign*intPart};
}
if (index.* + 1 < chars.len and chars[index.*] == '0' and chars[index.* + 1] == 'b') {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We probably should switch to stdlib parsing at some point, or at least deduplicate the loops into a more generic function. (I made an issue for this)

// Parse binary int
index.* += 2;
while (index.* < chars.len) : (index.* += 1) {
switch (chars[index.*]) {
'0', '1' => {
intPart = (chars[index.*] - '0') +% intPart*%2;
},
else => {
break;
},
}
}
return .{.int = sign*intPart};
}
while (index.* < chars.len) : (index.* += 1) {
switch (chars[index.*]) {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9' => {
Expand Down Expand Up @@ -850,6 +865,8 @@ test "number parsing" {
try std.testing.expectEqual(Parser.parseNumber(" abcd185473896", &index), ZonElement{.int = 185473896});
index = 0;
try std.testing.expectEqual(Parser.parseNumber("0xff34786056.0", &index), ZonElement{.int = 0xff34786056});
index = 0;
try std.testing.expectEqual(Parser.parseNumber("0b01010010", &index), ZonElement{.int = 0b01010010});
// Floats:
index = 0;
try std.testing.expectEqual(Parser.parseNumber("0.0", &index), ZonElement{.float = 0.0});
Expand Down
Loading