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
2 changes: 1 addition & 1 deletion src/callbacks/block/server/check_support_blocks.zig
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn run(_: *@This(), params: main.callbacks.ServerBlockCallback.Params) main.
.newBlock = newBlock,
};
const model = params.block.mode().model(params.block).model();
dropCtx.drop(.natural(model.min, model.max), .{wx, wy, wz});
dropCtx.drop(.natural(.{wx, wy, wz}, model.min, model.max));
return .handled;
}
return .ignored;
Expand Down
2 changes: 1 addition & 1 deletion src/callbacks/block/server/decay.zig
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ pub fn run(self: *@This(), params: main.callbacks.ServerBlockCallback.Params) ma
.newBlock = self.decayReplacement,
};
const model = params.block.mode().model(params.block).model();
dropCtx.drop(.natural(model.min, model.max), .{wx, wy, wz});
dropCtx.drop(.natural(.{wx, wy, wz}, model.min, model.max));
return .handled;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/renderer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1142,8 +1142,8 @@ pub const MeshSelection = struct { // MARK: MeshSelection
main.sync.client.executeCommand(.{
.updateBlock = .{
.source = .{.inv = source.super, .slot = slot},
.pos = pos,
.dropLocation = .{
.worldPos = pos,
.normalDir = selectionNormal,
.min = selectionMin,
.max = selectionMax,
Expand Down
26 changes: 14 additions & 12 deletions src/server/BlockDrop.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@ pub fn isDroppedWhenBrokenWithItem(self: @This(), item: Item) bool {
return true;
}

pub fn drop(self: @This(), loc: Location, pos: Vec3i, spread: Location.Spread) void {
pub fn drop(self: @This(), loc: Location, spread: Location.Spread) void {
if (self.chance == 1 or main.random.nextFloat(&main.seed) < self.chance) {
for (self.itemStacks) |itemStack| {
main.server.world.?.drop(itemStack.clone(), spread.dropPos(loc, pos), loc.dropDir(), loc.dropVelocity());
main.server.world.?.drop(itemStack.clone(), spread.dropPos(loc), loc.dropDir(), loc.dropVelocity());
}
}
}

pub const Location = struct {
worldPos: Vec3i,
normalDir: Vec3f,
min: Vec3f,
max: Vec3f,
Expand All @@ -44,8 +45,9 @@ pub const Location = struct {
const itemHitBoxMargin: f32 = @floatCast(main.itemdrop.ItemDropManager.radius);
const itemHitBoxMarginVec: Vec3f = @splat(itemHitBoxMargin);

pub inline fn natural(modelMin: Vec3f, modelMax: Vec3f) Location {
pub inline fn natural(worldPos: Vec3i, modelMin: Vec3f, modelMax: Vec3f) Location {
return .{
.worldPos = worldPos,
.normalDir = .{0, 0, 1},
.min = modelMin,
.max = modelMax,
Expand All @@ -56,16 +58,16 @@ pub const Location = struct {
inside,
outside,

pub inline fn dropPos(self: Spread, loc: Location, pos: Vec3i) Vec3d {
pub inline fn dropPos(self: Spread, loc: Location) Vec3d {
return switch (self) {
.inside => loc.insidePos(pos),
.outside => loc.outsidePos(pos),
.inside => loc.insidePos(),
.outside => loc.outsidePos(),
};
}
};

fn insidePos(self: Location, _pos: Vec3i) Vec3d {
const pos: Vec3d = @floatFromInt(_pos);
fn insidePos(self: Location) Vec3d {
const pos: Vec3d = @floatFromInt(self.worldPos);
return pos + self.randomOffset();
}
fn randomOffset(self: Location) Vec3f {
Expand All @@ -75,8 +77,8 @@ pub const Location = struct {
const width = (max - min)*half;
return center + width*main.random.nextFloatVectorSigned(3, &main.seed)*half;
}
fn outsidePos(self: Location, _pos: Vec3i) Vec3d {
const pos: Vec3d = @floatFromInt(_pos);
fn outsidePos(self: Location) Vec3d {
const pos: Vec3d = @floatFromInt(self.worldPos);
const random = self.randomOffset();
const minorVectors = minors(self);
const minor1Offset = @as(Vec3f, @splat(vec.dot(random, minorVectors[0])))*minorVectors[0];
Expand Down Expand Up @@ -119,7 +121,7 @@ pub const Context = struct {
newBlock: Block,
item: Item = .null,

pub fn drop(self: Context, location: Location, pos: Vec3i) void {
pub fn drop(self: Context, location: Location) void {
const dropAmount = self.oldBlock.mode().itemDropsOnChange(self.oldBlock, self.newBlock);
if (dropAmount == 0) return;

Expand All @@ -128,7 +130,7 @@ pub const Context = struct {
for (0..dropAmount) |_| {
for (self.oldBlock.blockDrops()) |blockDrop| {
if (blockDrop.isDroppedWhenBrokenWithItem(self.item)) {
blockDrop.drop(location, pos, spread);
blockDrop.drop(location, spread);
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/sync.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1573,13 +1573,13 @@ pub const Command = struct { // MARK: Command

const UpdateBlock = struct { // MARK: UpdateBlock
source: InventoryAndSlot,
pos: Vec3i,
dropLocation: BlockDrop.Location,
oldBlock: Block,
newBlock: Block,

fn run(self: UpdateBlock, ctx: Context) error{serverFailure}!void {
const stack = self.source.ref();
const pos = self.dropLocation.worldPos;

var shouldDropSourceBlockOnSuccess: bool = true;
const costOfChange = if (ctx.gamemode != .creative) self.oldBlock.canBeChangedInto(self.newBlock, stack.*, &shouldDropSourceBlockOnSuccess) else .yes;
Expand All @@ -1596,20 +1596,20 @@ pub const Command = struct { // MARK: Command
var writer = BinaryWriter.init(main.stackAllocator);
defer writer.deinit();

const actualBlock = main.server.world.?.getBlockAndBlockEntityData(self.pos[0], self.pos[1], self.pos[2], &writer) orelse return;
main.network.protocols.blockUpdate.send(ctx.user.?.conn, &.{.init(self.pos, actualBlock, writer.data.items)});
const actualBlock = main.server.world.?.getBlockAndBlockEntityData(pos[0], pos[1], pos[2], &writer) orelse return;
main.network.protocols.blockUpdate.send(ctx.user.?.conn, &.{.init(pos, actualBlock, writer.data.items)});
}
return;
}

if (ctx.side == .server) {
if (main.server.world.?.cmpxchgBlock(self.pos[0], self.pos[1], self.pos[2], self.oldBlock, self.newBlock) != null) {
if (main.server.world.?.cmpxchgBlock(pos[0], pos[1], pos[2], self.oldBlock, self.newBlock) != null) {
// Inform the client of the actual block:
var writer = BinaryWriter.init(main.stackAllocator);
defer writer.deinit();

const actualBlock = main.server.world.?.getBlockAndBlockEntityData(self.pos[0], self.pos[1], self.pos[2], &writer) orelse return;
main.network.protocols.blockUpdate.send(ctx.user.?.conn, &.{.init(self.pos, actualBlock, writer.data.items)});
const actualBlock = main.server.world.?.getBlockAndBlockEntityData(pos[0], pos[1], pos[2], &writer) orelse return;
main.network.protocols.blockUpdate.send(ctx.user.?.conn, &.{.init(pos, actualBlock, writer.data.items)});
return error.serverFailure;
}
}
Expand Down Expand Up @@ -1638,13 +1638,13 @@ pub const Command = struct { // MARK: Command
.newBlock = self.newBlock,
.item = handItem,
};
dropCtx.drop(self.dropLocation, self.pos);
dropCtx.drop(self.dropLocation);
}
}

fn serialize(self: UpdateBlock, writer: *BinaryWriter) void {
self.source.write(writer);
writer.writeVec(Vec3i, self.pos);
writer.writeVec(Vec3i, self.dropLocation.worldPos);
writer.writeVec(Vec3f, self.dropLocation.normalDir);
writer.writeVec(Vec3f, self.dropLocation.min);
writer.writeVec(Vec3f, self.dropLocation.max);
Expand All @@ -1655,8 +1655,8 @@ pub const Command = struct { // MARK: Command
fn deserialize(reader: *BinaryReader, side: Side, user: ?*main.server.User) !UpdateBlock {
return .{
.source = try InventoryAndSlot.read(reader, side, user),
.pos = try reader.readVec(Vec3i),
.dropLocation = .{
.worldPos = try reader.readVec(Vec3i),
.normalDir = try reader.readVec(Vec3f),
.min = try reader.readVec(Vec3f),
.max = try reader.readVec(Vec3f),
Expand Down
Loading