|
| 1 | +const std = @import("std"); |
| 2 | + |
| 3 | +const main = @import("main"); |
| 4 | +const Block = main.blocks.Block; |
| 5 | +const blocks = main.blocks; |
| 6 | +const Neighbor = main.chunk.Neighbor; |
| 7 | +const vec = main.vec; |
| 8 | +const Vec3i = vec.Vec3i; |
| 9 | +const Vec3d = vec.Vec3d; |
| 10 | +const Vec3f = vec.Vec3f; |
| 11 | +const ZonElement = main.ZonElement; |
| 12 | +const server = main.server; |
| 13 | + |
| 14 | +pub fn init(_: ZonElement) ?*@This() { |
| 15 | + return @as(*@This(), undefined); |
| 16 | +} |
| 17 | + |
| 18 | +pub fn run(_: *@This(), params: main.callbacks.ServerBlockCallback.Params) main.callbacks.Result { |
| 19 | + const wx = params.chunk.super.pos.wx + params.blockPos.x; |
| 20 | + const wy = params.chunk.super.pos.wy + params.blockPos.y; |
| 21 | + const wz = params.chunk.super.pos.wz + params.blockPos.z; |
| 22 | + |
| 23 | + var neighborSupportive: [6]bool = undefined; |
| 24 | + |
| 25 | + for(Neighbor.iterable) |neighbor| { |
| 26 | + const neighborBlock: Block = main.server.world.?.getBlock(wx +% neighbor.relX(), wy +% neighbor.relY(), wz +% neighbor.relZ()) orelse .{.typ = 0, .data = 0}; |
| 27 | + const neighborModel = main.blocks.meshes.model(neighborBlock).model(); |
| 28 | + neighborSupportive[neighbor.toInt()] = !neighborBlock.replacable() and neighborModel.neighborFacingQuads[neighbor.reverse().toInt()].len != 0; |
| 29 | + } |
| 30 | + |
| 31 | + var newBlock: Block = params.block; |
| 32 | + |
| 33 | + if(params.block.mode() == main.rotation.getByID("cubyz:torch")) { |
| 34 | + main.rotation.list.@"cubyz:torch".updateBlockFromNeighborConnectivity(&newBlock, neighborSupportive); |
| 35 | + } else { |
| 36 | + std.log.err("Expected {s} to have cubyz:torch as rotation", .{params.block.id()}); |
| 37 | + } |
| 38 | + |
| 39 | + if(newBlock == params.block) return .ignored; |
| 40 | + |
| 41 | + if(main.server.world.?.cmpxchgBlock(wx, wy, wz, params.block, newBlock) == null) { |
| 42 | + const drops = params.block.blockDrops(); |
| 43 | + for(drops) |drop| { |
| 44 | + if(drop.chance == 1 or main.random.nextFloat(&main.seed) < drop.chance) { |
| 45 | + for(drop.items) |stack| { |
| 46 | + var dir = main.vec.normalize(main.random.nextFloatVectorSigned(3, &main.seed)); |
| 47 | + // Bias upwards |
| 48 | + dir[2] += main.random.nextFloat(&main.seed)*4.0; |
| 49 | + const model = params.block.mode().model(params.block).model(); |
| 50 | + const pos = Vec3f{ |
| 51 | + @as(f32, @floatFromInt(wx)) + model.min[0] + main.random.nextFloat(&main.seed)*(model.max[0] - model.min[0]), |
| 52 | + @as(f32, @floatFromInt(wy)) + model.min[1] + main.random.nextFloat(&main.seed)*(model.max[1] - model.min[1]), |
| 53 | + @as(f32, @floatFromInt(wz)) + model.min[2] + main.random.nextFloat(&main.seed)*(model.max[2] - model.min[2]), |
| 54 | + }; |
| 55 | + main.server.world.?.drop(stack.clone(), pos, dir, 1); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + return .handled; |
| 60 | + } |
| 61 | + return .ignored; |
| 62 | +} |
0 commit comments