Skip to content
Draft
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
6 changes: 6 additions & 0 deletions assets/cubyz/blocks/torch.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
.onUpdate = .{
.type = .check_support_blocks,
},
.onInteract = .{
.type = .torch_update_particles,
},
.onClientUpdate = .{
.type = .torch_update_particles,
},
.model = .{
.base = "cubyz:torch",
.side = "cubyz:torch_side",
Expand Down
2 changes: 1 addition & 1 deletion assets/cubyz/particles/flame.zig.zon
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.{
.texture = "cubyz:flame",
.density = .{0.00065, 0.00085},
.rotationVelocity = .{20, 35},
.rotationVelocity = .{5, 10},
.dragCoefficient = .{0.2, 0.3},
}
11 changes: 11 additions & 0 deletions src/blocks.zig
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ var _mobility: [maxBlockCount]f32 = undefined;
var _allowOres: [maxBlockCount]bool = undefined;
var _onTick: [maxBlockCount]ServerBlockCallback = undefined;
var _onTouch: [maxBlockCount]BlockTouchCallback = undefined;
var _onClientUpdate: [maxBlockCount]ClientBlockCallback = undefined;
var _blockEntity: [maxBlockCount]?*const BlockEntityType = undefined;

var reverseIndices: std.StringHashMapUnmanaged(u16) = .{};
Expand Down Expand Up @@ -329,6 +330,12 @@ fn registerCallbacks(typ: u16, zon: ZonElement) void {
break :blk .noop;
};
};
_onClientUpdate[typ] = blk: {
break :blk ClientBlockCallback.init(zon.getChildOrNull("onClientUpdate") orelse break :blk .noop, .{.block = .{.typ = typ, .data = 0}}) orelse {
std.log.err("Failed to load onClientUpdate event for block {s}", .{_id[typ]});
break :blk .noop;
};
};
}

pub fn finishBlocks(zonElements: Assets.ZonHashMap) void {
Expand Down Expand Up @@ -569,6 +576,10 @@ pub const Block = packed struct(u32) { // MARK: Block
return _onTouch[self.typ];
}

pub inline fn onClientUpdate(self: Block) ClientBlockCallback {
return _onClientUpdate[self.typ];
}

pub fn blockEntity(self: Block) ?*const BlockEntityType {
return _blockEntity[self.typ];
}
Expand Down
1 change: 1 addition & 0 deletions src/callbacks/block/client/_list.zig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub const edit_sign = @import("edit_sign.zig");
pub const open_chest = @import("open_chest.zig");
pub const open_window = @import("open_window.zig");
pub const torch_update_particles = @import("torch_update_particles.zig");
24 changes: 24 additions & 0 deletions src/callbacks/block/client/torch_update_particles.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const std = @import("std");

const main = @import("main");
const Block = main.blocks.Block;
const vec = main.vec;
const Vec3i = vec.Vec3i;
const ZonElement = main.ZonElement;
const particles = main.particles;

pub fn init(_: ZonElement, _: main.callbacks.Creator) ?*anyopaque {
return @as(*anyopaque, undefined);
}

pub fn run(_: *anyopaque, params: main.callbacks.ClientBlockCallback.Params) main.callbacks.Result {
var emitter: particles.Emitter = undefined;
const emitterProperties = particles.EmitterProperties{
.speed = .init(0.15, 0.2),
.lifeTime = .init(0.25, 0.5),
.randomizeRotation = false,
};
emitter = .init("cubyz:flame", false, .{.point = .{}}, emitterProperties, .spread);
emitter.spawnParticles(@as(vec.Vec3f, @floatFromInt(params.blockPos)) + vec.Vec3f{0.5, 0.5, 0.85}, 1);
return .handled;
}
31 changes: 31 additions & 0 deletions src/game.zig
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ pub const World = struct { // MARK: World
entityComponentPalette: *assets.Palette = undefined,
itemDrops: ClientItemDropManager = undefined,
playerBiome: Atomic(*const main.server.terrain.biomes.Biome) = undefined,
randomTicksTime: f64 = 0,

shouldRestart: std.atomic.Value(bool) = .init(false),
shouldReload: bool = false,
Expand Down Expand Up @@ -412,6 +413,36 @@ pub const World = struct { // MARK: World
}
network.protocols.playerPosition.send(self.conn, Player.getPosBlocking(), Player.getVelBlocking(), @intCast(newTime & 65535));
self.dayTime.update(deltaTime);

self.randomTicksTime += deltaTime;
const tickrate: f32 = 1.0/20.0;
if (self.randomTicksTime >= tickrate) {

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.

To catch lag spikes, this should be a while loop, below it should subtract the tickRate instead of setting it to 0

Suggested change
if (self.randomTicksTime >= tickrate) {
while (self.randomTicksTime >= tickrate) {

updateRandomTickBlocks(self);
self.randomTicksTime = 0;
}
}

pub fn updateRandomTickBlocks(_: *World) void {
const chunkRadius = settings.visualRandomTickRadius;
const playerBlockPos: Vec3i = @intFromFloat(@floor(Player.getPosBlocking()));
const thing: i32 = @intCast(if (@mod(chunkRadius, 2) == 0) @divFloor(chunkRadius, 2) else @divFloor(chunkRadius - 1, 2));

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.

It's not a radius if you half it

for (0..chunkRadius) |cx| {
for (0..chunkRadius) |cy| {
for (0..chunkRadius) |cz| {
const chunkMultiplierPos: Vec3i = playerBlockPos +% @as(Vec3i, @splat(32))*Vec3i{@as(i32, @intCast(cx)) - thing, @as(i32, @intCast(cy)) - thing, @as(i32, @intCast(cz)) - thing};
const chunkMesh = main.renderer.mesh_storage.getMesh(.initFromWorldPos(chunkMultiplierPos, 1));

if (chunkMesh) |mesh| {
for (0..20) |_| {

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.

Should be configurable

const curBlockPos = Vec3i{main.random.nextIntBounded(i32, &main.seed, 32), main.random.nextIntBounded(i32, &main.seed, 32), main.random.nextIntBounded(i32, &main.seed, 32)};

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.

You can create a random BlockPos directly from a u15

const block = mesh.chunk.getBlock(curBlockPos[0], curBlockPos[1], curBlockPos[2]);
const onClientUpdate = block.onClientUpdate();

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.

Suggested change
const onClientUpdate = block.onClientUpdate();
const onClientUpdate = block.onClientRandomTick();

if (onClientUpdate.run(.{.blockPos = curBlockPos + Vec3i{mesh.pos.wx, mesh.pos.wy, mesh.pos.wz}, .block = block, .chunk = mesh.chunk}) == .handled) continue;
}
}
}
}
}
}

pub const DayTime = struct { // MARK: DayTime
Expand Down
8 changes: 8 additions & 0 deletions src/gui/windows/graphics.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const fpsPresetsText = blk: {
break :blk strings;
};

const visualRandomTickRadius = [_]u8{1, 2, 3, 4, 5, 6};

fn fpsCapGetIndex(fpsOptional: ?u32) u16 {
const fps: u16 = @truncate(fpsOptional orelse return fpsPresetsValue.len);
return @intCast(std.sort.lowerBound(u16, &fpsPresetsValue, fps, struct {
Expand Down Expand Up @@ -128,6 +130,11 @@ fn resolutionScaleCallback(newValue: u16) void {
main.Window.GLFWCallbacks.framebufferSize(null, main.Window.width, main.Window.height);
}

fn visualRandomTickRadiusCallback(newValue: u16) void {
settings.visualRandomTickRadius = newValue;
settings.save();
}

pub fn onOpen() void {
const list = VerticalList.init(.{padding, 16 + padding}, 300, 16);
list.add(DiscreteSlider.init(.{0, 0}, 128, "#ffffffFPS Limit:\n", "{s}", &fpsPresetsText, fpsCapGetIndex(settings.fpsCap), &fpsCapCallback));
Expand All @@ -151,6 +158,7 @@ pub fn onOpen() void {
else => 2,
}, &anisotropicFilteringCallback));
list.add(DiscreteSlider.init(.{0, 0}, 128, "#ffffffResolution Scale: ", "{}%", &resolutions, @as(u16, @trunc(@log2(settings.resolutionScale) + 2.0)), &resolutionScaleCallback));
list.add(DiscreteSlider.init(.{0, 0}, 128, "#ffffffVisual random tick radius: ", "{} chunks", &visualRandomTickRadius, settings.visualRandomTickRadius, &visualRandomTickRadiusCallback));
list.finish(.center);
window.rootComponent = list.toComponent();
window.contentSize = window.rootComponent.?.pos() + window.rootComponent.?.size() + @as(Vec2f, @splat(padding));
Expand Down
10 changes: 7 additions & 3 deletions src/particles.zig
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,8 @@ pub const DirectionMode = union(enum) {

pub const Emitter = struct {
typ: u16 = 0,
particleTypeLocal: ParticleTypeLocal,
particleType: ParticleType,
collides: bool,
spawnShape: SpawnShape,
properties: EmitterProperties,
Expand Down Expand Up @@ -506,6 +508,8 @@ pub const Emitter = struct {

return Emitter{
.typ = typ,
.particleTypeLocal = ParticleManager.typesLocal.items[typ],
.particleType = ParticleManager.types.items[typ],
.collides = collides,
.spawnShape = spawnShape,
.properties = properties,
Expand All @@ -526,6 +530,8 @@ pub const Emitter = struct {

return Emitter{
.typ = typ,
.particleTypeLocal = ParticleManager.typesLocal.items[typ],
.particleType = ParticleManager.types.items[typ],
.collides = collides,
.spawnShape = spawnShape,
.properties = EmitterProperties.parse(zon),
Expand All @@ -538,9 +544,7 @@ pub const Emitter = struct {
for (0..count) |_| {
const particlePos, const particleVel = self.spawnShape.spawn(pos, self.properties, self.mode);

const particleTypeLocal = ParticleManager.typesLocal.items[self.typ];
const particleType = ParticleManager.types.items[self.typ];
ParticleSystem.addParticle(self.typ, particleTypeLocal, particleType, particlePos, particleVel, self.collides, self.properties);
ParticleSystem.addParticle(self.typ, self.particleTypeLocal, self.particleType, particlePos, particleVel, self.collides, self.properties);
}
}
};
Expand Down
2 changes: 2 additions & 0 deletions src/settings.zig
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ pub var updateRepeatDelay: std.Io.Duration = .fromMilliseconds(500);

pub var controllerAxisDeadzone: f32 = 0.2;

pub var visualRandomTickRadius: u16 = 5;

const settingsFile = if (builtin.mode == .Debug) "debug_settings.zig.zon" else "settings.zig.zon";

pub fn init() void {
Expand Down
Loading