-
-
Notifications
You must be signed in to change notification settings - Fork 235
Refactor natural block dropping #3532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
545b16c
a9616a6
7a69014
bf107d7
248ad25
944286b
6337e1a
971f468
4b26a8b
664b465
fbe14a4
23083ce
ef9efb6
c9a6302
3c49964
2b8d9f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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, | ||
|
|
@@ -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]), | ||
| }; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we maybe include this as another case into the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
|
@@ -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); | ||
| } | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally all item stacks should have a randomized direction, not one direction for all of them.