Start working on items.

This commit is contained in:
IntegratedQuantum 2022-11-19 19:22:49 +01:00
parent b3ea5d517c
commit 0f0b0b6ac4
6 changed files with 1234 additions and 12 deletions

View File

@ -1,15 +1,17 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const blocks_zig = @import("blocks.zig");
const items_zig = @import("items.zig");
const json = @import("json.zig");
const JsonElement = json.JsonElement;
const blocks_zig = @import("blocks.zig");
const main = @import("main.zig");
var arena: std.heap.ArenaAllocator = undefined;
var arenaAllocator: Allocator = undefined;
var commonBlocks: std.StringHashMap(JsonElement) = undefined;
var commonBiomes: std.StringHashMap(JsonElement) = undefined;
var commonItems: std.StringHashMap(JsonElement) = undefined;
var commonRecipes: std.ArrayList([]const u8) = undefined;
/// Reads json files recursively from all subfolders.
@ -42,7 +44,7 @@ pub fn readAllJsonFilesInAddons(externalAllocator: Allocator, addons: std.ArrayL
}
}
pub fn readAssets(externalAllocator: Allocator, assetPath: []const u8, blocks: *std.StringHashMap(JsonElement), biomes: *std.StringHashMap(JsonElement)) !void {
pub fn readAssets(externalAllocator: Allocator, assetPath: []const u8, blocks: *std.StringHashMap(JsonElement), items: *std.StringHashMap(JsonElement), biomes: *std.StringHashMap(JsonElement)) !void {
var addons = std.ArrayList(std.fs.Dir).init(main.threadAllocator);
defer addons.deinit();
var addonNames = std.ArrayList([]const u8).init(main.threadAllocator);
@ -65,6 +67,7 @@ pub fn readAssets(externalAllocator: Allocator, assetPath: []const u8, blocks: *
};
try readAllJsonFilesInAddons(externalAllocator, addons, addonNames, "blocks", blocks);
try readAllJsonFilesInAddons(externalAllocator, addons, addonNames, "items", items);
try readAllJsonFilesInAddons(externalAllocator, addons, addonNames, "biomes", biomes);
}
@ -72,13 +75,18 @@ pub fn init() !void {
arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
arenaAllocator = arena.allocator();
commonBlocks = std.StringHashMap(JsonElement).init(arenaAllocator);
commonItems = std.StringHashMap(JsonElement).init(arenaAllocator);
commonBiomes = std.StringHashMap(JsonElement).init(arenaAllocator);
commonRecipes = std.ArrayList([]const u8).init(arenaAllocator);
try readAssets(arenaAllocator, "assets/", &commonBlocks, &commonBiomes);
try readAssets(arenaAllocator, "assets/", &commonBlocks, &commonItems, &commonBiomes);
}
pub fn registerBlock(assetFolder: []const u8, id: []const u8, info: JsonElement) !void {
fn registerItem(assetFolder: []const u8, id: []const u8, info: JsonElement) !void {
try items_zig.register(assetFolder, id, info);
}
fn registerBlock(assetFolder: []const u8, id: []const u8, info: JsonElement) !void {
try blocks_zig.register(assetFolder, id, info); // TODO: Modded block registries
try blocks_zig.meshes.register(assetFolder, id, info);
@ -185,11 +193,14 @@ pub const BlockPalette = struct {
pub fn loadWorldAssets(assetFolder: []const u8, palette: *BlockPalette) !void {
var blocks = try commonBlocks.cloneWithAllocator(main.threadAllocator);
defer blocks.clearAndFree();
var items = try commonItems.cloneWithAllocator(main.threadAllocator);
defer items.clearAndFree();
var biomes = try commonBiomes.cloneWithAllocator(main.threadAllocator);
defer biomes.clearAndFree();
try readAssets(arenaAllocator, assetFolder, &blocks, &biomes);
try readAssets(arenaAllocator, assetFolder, &blocks, &items, &biomes);
// blocks:
var block: u32 = 0;
for(palette.palette.items) |id| {
var nullKeyValue = blocks.fetchRemove(id);
@ -213,6 +224,12 @@ pub fn loadWorldAssets(assetFolder: []const u8, palette: *BlockPalette) !void {
block += 1;
}
// items:
iterator = items.iterator();
while(iterator.next()) |entry| {
try registerItem(assetFolder, entry.key_ptr.*, entry.value_ptr.*);
}
// public void registerBlocks(Registry<DataOrientedRegistry> registries, NoIDRegistry<Ore> oreRegistry, BlockPalette palette) {
// HashMap<Resource, JsonObject> perWorldBlocks = new HashMap<>(commonBlocks);
// readAllJsonObjects("blocks", (json, id) -> {

View File

@ -638,6 +638,16 @@ pub const Image = struct {
width: u31,
height: u31,
imageData: []Color,
pub fn init(allocator: Allocator, width: u31, height: u31) !Image {
return Image{
.width = width,
.height = height,
.imageData = try allocator.alloc(Color, width*height),
};
}
pub fn deinit(self: Image, allocator: Allocator) void {
allocator.free(self.imageData);
}
pub fn readFromFile(allocator: Allocator, path: []const u8) !Image {
var result: Image = undefined;
var channel: c_int = undefined;

1118
src/items.zig Normal file

File diff suppressed because it is too large Load Diff

View File

@ -25,7 +25,41 @@ pub const JsonElement = union(JsonType) {
JsonArray: *ArrayList(JsonElement),
JsonObject: *std.StringHashMap(JsonElement),
// TODO: Add simple array access.
pub fn initObject(allocator: Allocator) !JsonElement {
var map: *std.StringHashMap(JsonElement) = try allocator.create(std.StringHashMap(JsonElement));
map.* = std.StringHashMap(JsonElement).init(allocator);
return JsonElement{.JsonObject=map};
}
pub fn initArray(allocator: Allocator) !JsonElement {
const list: *ArrayList(JsonElement) = try allocator.create(ArrayList(JsonElement));
list.* = ArrayList(JsonElement).init(allocator);
return JsonElement{.JsonArray=list};
}
pub fn getAtIndex(self: *const JsonElement, comptime _type: type, index: usize, replacement: _type) @TypeOf(replacement) {
if(self.* != JsonType.JsonArray) {
return replacement;
} else {
if(index < self.JsonArray.items.len) {
return self.JsonArray.items[index].as(_type, replacement);
} else {
return replacement;
}
}
}
pub fn getChildAtIndex(self: *const JsonElement, index: usize) JsonElement {
if(self.* != JsonType.JsonArray) {
return JsonElement{.JsonNull={}};
} else {
if(index < self.JsonArray.items.len) {
return self.JsonArray.items[index];
} else {
return JsonElement{.JsonNull={}};
}
}
}
pub fn get(self: *const JsonElement, comptime _type: type, key: []const u8, replacement: _type) @TypeOf(replacement) {
if(self.* != JsonType.JsonObject) {
@ -52,23 +86,29 @@ pub const JsonElement = union(JsonType) {
}
pub fn as(self: *const JsonElement, comptime _type: type, replacement: _type) _type {
switch(@typeInfo(_type)) {
comptime var typeInfo = @typeInfo(_type);
comptime var innerType = _type;
inline while(typeInfo == .Optional) {
innerType = typeInfo.Optional.child;
typeInfo = @typeInfo(innerType);
}
switch(typeInfo) {
.Int => {
switch(self.*) {
JsonType.JsonInt => return std.math.cast(_type, self.JsonInt) orelse replacement,
JsonType.JsonFloat => return std.math.lossyCast(_type, std.math.round(self.JsonFloat)),
JsonType.JsonInt => return std.math.cast(innerType, self.JsonInt) orelse replacement,
JsonType.JsonFloat => return std.math.lossyCast(innerType, std.math.round(self.JsonFloat)),
else => return replacement,
}
},
.Float => {
switch(self.*) {
JsonType.JsonInt => return @intToFloat(_type, self.JsonInt),
JsonType.JsonFloat => return @floatCast(_type, self.JsonFloat),
JsonType.JsonInt => return @intToFloat(innerType, self.JsonInt),
JsonType.JsonFloat => return @floatCast(innerType, self.JsonFloat),
else => return replacement,
}
},
else => {
switch(_type) {
switch(innerType) {
[]const u8 => {
switch(self.*) {
JsonType.JsonString => return self.JsonString,
@ -90,6 +130,31 @@ pub const JsonElement = union(JsonType) {
}
}
fn createElementFromRandomType(value: anytype) JsonElement {
switch(@typeInfo(@TypeOf(value))) {
.Void => return JsonElement{.JsonNull={}},
.Null => return JsonElement{.JsonNull={}},
.Bool => return JsonElement{.JsonBool=value},
.Int, .ComptimeInt => return JsonElement{.JsonInt=@intCast(i64, value)},
.Float, .ComptimeFloat => return JsonElement{.JsonInt=@floatCast(f64, value)},
.Union => {
if(@TypeOf(value) == JsonElement) {
return value;
} else {
@compileError("Unknown value type.");
}
},
else => {
@compileError("Unknown value type.");
},
}
}
pub fn put(self: *const JsonElement, key: []const u8, value: anytype) !void {
const result = createElementFromRandomType(value);
try self.JsonObject.put(key, result);
}
pub fn free(self: *const JsonElement, allocator: Allocator) void {
switch(self.*) {
JsonType.JsonInt, JsonType.JsonFloat, JsonType.JsonBool, JsonType.JsonNull, JsonType.JsonString => return,

View File

@ -6,6 +6,7 @@ const chunk = @import("chunk.zig");
const entity = @import("entity.zig");
const game = @import("game.zig");
const graphics = @import("graphics.zig");
const items = @import("items.zig");
const models = @import("models.zig");
const renderer = @import("renderer.zig");
const network = @import("network.zig");
@ -239,6 +240,9 @@ pub fn main() !void {
try models.init();
defer models.deinit();
items.globalInit();
defer items.deinit();
try assets.init();
defer assets.deinit();

View File

@ -18,6 +18,14 @@ pub fn dot(self: anytype, other: @TypeOf(self)) @typeInfo(@TypeOf(self)).Vector.
return @reduce(.Add, self*other);
}
pub fn length(self: anytype) @typeInfo(@TypeOf(self)).Vector.child {
return @sqrt(@reduce(.Add, self*self));
}
pub fn normalize(self: anytype) @typeInfo(@TypeOf(self)).Vector.child {
return self/length(self);
}
pub fn cross(self: anytype, other: @TypeOf(self)) @TypeOf(self) {
if(@typeInfo(@TypeOf(self)).Vector.len != 3) @compileError("Only available for vectors of length 3.");
return @TypeOf(self) {