Skip to content

Commit

Permalink
Allow breaking single torches and carpets from a given block, instead…
Browse files Browse the repository at this point in the history
… of breaking all at once.

fixes #798
  • Loading branch information
IntegratedQuantum committed Nov 25, 2024
1 parent 0d50346 commit 994d2c9
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/rotation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -680,20 +680,33 @@ pub const RotationModes = struct {
return true;
}

pub fn rayIntersection(block: Block, _: ?main.items.Item, relativePlayerPos: Vec3f, playerDir: Vec3f) ?RayIntersectionResult {
fn closestRay(comptime typ: enum{bit, intersection}, block: Block, _: ?main.items.Item, relativePlayerPos: Vec3f, playerDir: Vec3f) if(typ == .intersection) ?RayIntersectionResult else u16 {
var result: ?RayIntersectionResult = null;
var resultBit: u16 = 0;
for([_]u16{1, 2, 4, 8, 16}) |bit| {
if(block.data & bit != 0) {
const modelIndex = blocks.meshes.modelIndexStart(block) + bit - 1;
if(RotationMode.DefaultFunctions.rayModelIntersection(modelIndex, relativePlayerPos, playerDir)) |intersection| {
if(result == null or result.?.distance > intersection.distance) {
result = intersection;
resultBit = bit;
}
}
}
}
if(typ == .bit) return resultBit;
return result;
}

pub fn rayIntersection(block: Block, item: ?main.items.Item, relativePlayerPos: Vec3f, playerDir: Vec3f) ?RayIntersectionResult {
return closestRay(.intersection, block, item, relativePlayerPos, playerDir);
}

pub fn onBlockBreaking(item: ?main.items.Item, relativePlayerPos: Vec3f, playerDir: Vec3f, currentData: *Block) void {
const bit = closestRay(.bit, currentData.*, item, relativePlayerPos, playerDir);
currentData.data &= ~bit;
if(currentData.data == 0) currentData.typ = 0;
}
};
pub const Carpet = struct { // MARK: Carpet
pub const id: []const u8 = "carpet";
Expand Down Expand Up @@ -823,20 +836,33 @@ pub const RotationModes = struct {
return true;
}

pub fn rayIntersection(block: Block, _: ?main.items.Item, relativePlayerPos: Vec3f, playerDir: Vec3f) ?RayIntersectionResult {
fn closestRay(comptime typ: enum{bit, intersection}, block: Block, _: ?main.items.Item, relativePlayerPos: Vec3f, playerDir: Vec3f) if(typ == .intersection) ?RayIntersectionResult else u16 {
var result: ?RayIntersectionResult = null;
var resultBit: u16 = 0;
for([_]u16{1, 2, 4, 8, 16, 32}) |bit| {
if(block.data & bit != 0) {
const modelIndex = blocks.meshes.modelIndexStart(block) + bit - 1;
if(RotationMode.DefaultFunctions.rayModelIntersection(modelIndex, relativePlayerPos, playerDir)) |intersection| {
if(result == null or result.?.distance > intersection.distance) {
result = intersection;
resultBit = bit;
}
}
}
}
if(typ == .bit) return resultBit;
return result;
}

pub fn rayIntersection(block: Block, item: ?main.items.Item, relativePlayerPos: Vec3f, playerDir: Vec3f) ?RayIntersectionResult {
return closestRay(.intersection, block, item, relativePlayerPos, playerDir);
}

pub fn onBlockBreaking(item: ?main.items.Item, relativePlayerPos: Vec3f, playerDir: Vec3f, currentData: *Block) void {
const bit = closestRay(.bit, currentData.*, item, relativePlayerPos, playerDir);
currentData.data &= ~bit;
if(currentData.data == 0) currentData.typ = 0;
}
};
};

Expand Down

0 comments on commit 994d2c9

Please sign in to comment.