Skip to content
Open
Show file tree
Hide file tree
Changes from 15 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
27 changes: 6 additions & 21 deletions src/callbacks/block/server/check_support_blocks.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const Vec3d = vec.Vec3d;
const Vec3f = vec.Vec3f;
const ZonElement = main.ZonElement;
const server = main.server;
const BlockDrop = main.server.BlockDrop;

pub fn init(_: ZonElement, _: main.callbacks.Creator) ?*@This() {
return @as(*@This(), undefined);
Expand Down Expand Up @@ -43,27 +44,11 @@ pub fn run(_: *@This(), params: main.callbacks.ServerBlockCallback.Params) main.
if (newBlock == params.block) return .ignored;

if (main.server.world.?.cmpxchgBlock(wx, wy, wz, params.block, newBlock) == null) {
const dropAmount = params.block.mode().itemDropsOnChange(params.block, newBlock);
const drops = params.block.blockDrops();
for (0..dropAmount) |_| {
for (drops) |drop| {
if (!drop.isDroppedWhenBrokenWithItem(.null)) continue;
if (drop.chance == 1 or main.random.nextFloat(&main.seed) < drop.chance) {
for (drop.itemStacks) |stack| {
var dir = main.vec.normalize(main.random.nextFloatVectorSigned(3, &main.seed));
// Bias upwards
dir[2] += main.random.nextFloat(&main.seed)*4.0;
const model = params.block.mode().model(params.block).model();
const pos = Vec3f{
@as(f32, @floatFromInt(wx)) + model.min[0] + main.random.nextFloat(&main.seed)*(model.max[0] - model.min[0]),
@as(f32, @floatFromInt(wy)) + model.min[1] + main.random.nextFloat(&main.seed)*(model.max[1] - model.min[1]),
@as(f32, @floatFromInt(wz)) + model.min[2] + main.random.nextFloat(&main.seed)*(model.max[2] - model.min[2]),
};
main.server.world.?.drop(stack.clone(), pos, dir, 1);
}
}
}
}
const dropCtx = BlockDrop.Context{
.oldBlock = params.block,
.newBlock = newBlock,
};
dropCtx.dropNaturally(.{wx, wy, wz});
return .handled;
}
return .ignored;
Expand Down
16 changes: 2 additions & 14 deletions src/callbacks/block/server/decay.zig
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,9 @@ pub fn run(self: *@This(), params: main.callbacks.ServerBlockCallback.Params) ma

// no, there is no log in proximity
if (world.cmpxchgBlock(wx, wy, wz, leaf, self.decayReplacement) == null) {
const modelIndex = params.block.mode().model(params.block);
for (self.blockDrops) |drop| {
if (drop.chance == 1 or main.random.nextFloat(&main.seed) < drop.chance) {
for (drop.itemStacks) |stack| {
var dir = main.vec.normalize(main.random.nextFloatVectorSigned(3, &main.seed));
// Bias upwards
dir[2] += main.random.nextFloat(&main.seed)*4.0;
const model = leaf.mode().model(leaf).model();
const pos = Vec3f{
@as(f32, @floatFromInt(wx)) + model.min[0] + main.random.nextFloat(&main.seed)*(model.max[0] - model.min[0]),
@as(f32, @floatFromInt(wy)) + model.min[1] + main.random.nextFloat(&main.seed)*(model.max[1] - model.min[1]),
@as(f32, @floatFromInt(wz)) + model.min[2] + main.random.nextFloat(&main.seed)*(model.max[2] - model.min[2]),
};
main.server.world.?.drop(stack.clone(), pos, dir, 1);
}
}
drop.dropNaturally(modelIndex, .{wx, wy, wz});
}
return .handled;
}
Expand Down
48 changes: 44 additions & 4 deletions src/server/BlockDrop.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const Vec3f = vec.Vec3f;
const Vec3i = vec.Vec3i;
const blocks = main.blocks;
const Block = blocks.Block;
const ModelIndex = main.models.ModelIndex;

itemStacks: []const items.ItemStack,
chance: f32,
Expand All @@ -28,13 +29,34 @@ pub fn isDroppedWhenBrokenWithItem(self: @This(), item: Item) bool {
}

pub fn drop(self: @This(), pos: Vec3d, dir: Vec3f, velocity: f32) void {
if (self.chance == 1 or main.random.nextFloat(&main.seed) < self.chance) {
if (self.shouldDrop()) {
for (self.itemStacks) |itemStack| {
main.server.world.?.drop(itemStack.clone(), pos, dir, velocity);
}
}
}

pub fn dropNaturally(self: @This(), modelIndex: ModelIndex, pos: Vec3i) void {
if (self.shouldDrop()) {
const model = modelIndex.model();
for (self.itemStacks) |itemStack| {
var randomDir = main.vec.normalize(main.random.nextFloatVectorSigned(3, &main.seed));
// Bias upwards
randomDir[2] += main.random.nextFloat(&main.seed)*4.0;
const randomPos = Vec3f{
@as(f32, @floatFromInt(pos[0])) + model.min[0] + main.random.nextFloat(&main.seed)*(model.max[0] - model.min[0]),
@as(f32, @floatFromInt(pos[1])) + model.min[1] + main.random.nextFloat(&main.seed)*(model.max[1] - model.min[1]),
@as(f32, @floatFromInt(pos[2])) + model.min[2] + main.random.nextFloat(&main.seed)*(model.max[2] - model.min[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.

Could we maybe include this as another case into the Location?
I think it would be nicer to only have a single drop function, with everything else being configured through its parameters.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ohh I hadn't made that connection, I'll try it out

main.server.world.?.drop(itemStack.clone(), randomPos, randomDir, 1);
}
}
}

inline fn shouldDrop(self: @This()) bool {
return self.chance == 1 or main.random.nextFloat(&main.seed) < self.chance;
}

pub const Location = struct {
normalDir: Vec3f,
min: Vec3f,
Expand Down Expand Up @@ -100,19 +122,37 @@ pub const Context = struct {
item: Item = .null,

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

const dropPos = if (self.newBlock.collide()) location.outsidePos(pos) else location.insidePos(pos);
const dropDir = location.dropDir();
const dropVelocity = location.dropVelocity();

for (0..dropAmount) |_| {
for (0..amount) |_| {
for (self.oldBlock.blockDrops()) |blockDrop| {
if (blockDrop.isDroppedWhenBrokenWithItem(self.item)) {
blockDrop.drop(dropPos, dropDir, dropVelocity);
}
}
}
}

pub fn dropNaturally(self: Context, pos: Vec3i) void {
const amount = self.dropAmount();
if (amount == 0) return;

const modelIndex = self.oldBlock.mode().model(self.oldBlock);
for (0..amount) |_| {
for (self.oldBlock.blockDrops()) |blockDrop| {
if (blockDrop.isDroppedWhenBrokenWithItem(.null)) {
blockDrop.dropNaturally(modelIndex, pos);
}
}
}
}

inline fn dropAmount(self: Context) u16 {
return self.oldBlock.mode().itemDropsOnChange(self.oldBlock, self.newBlock);
}
};
Loading