-
Notifications
You must be signed in to change notification settings - Fork 79
/
BitLocation.java
171 lines (147 loc) · 4.44 KB
/
BitLocation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package mod.chiselsandbits.chiseledblock.data;
import mod.chiselsandbits.api.IBitLocation;
import mod.chiselsandbits.helpers.BitOperation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
public class BitLocation implements IBitLocation
{
private static final double One32nd = 0.5 / VoxelBlob.dim;
public final BlockPos blockPos;
public final int bitX, bitY, bitZ;
@Override
public BlockPos getBlockPos()
{
return blockPos;
}
@Override
public int getBitX()
{
return bitX;
}
@Override
public int getBitY()
{
return bitY;
}
@Override
public int getBitZ()
{
return bitZ;
}
public int snapToValid(
final int x )
{
// rounding can sometimes create -1 or 16, just snap int to the nearest
// valid position and move on.
return Math.min( Math.max( 0, x ), 15 );
}
public BitLocation(
final RayTraceResult mop,
final boolean absHit,
final BitOperation type )
{
final BlockPos absOffset = absHit ? mop.getBlockPos() : BlockPos.ORIGIN;
if ( !type.usePlacementOffset() )
{
blockPos = mop.getBlockPos();
final double xCoord = mop.hitVec.xCoord - absOffset.getX() - mop.sideHit.getFrontOffsetX() * One32nd;
final double yCoord = mop.hitVec.yCoord - absOffset.getY() - mop.sideHit.getFrontOffsetY() * One32nd;
final double zCoord = mop.hitVec.zCoord - absOffset.getZ() - mop.sideHit.getFrontOffsetZ() * One32nd;
bitX = snapToValid( (int) Math.floor( xCoord * VoxelBlob.dim ) );
bitY = snapToValid( (int) Math.floor( yCoord * VoxelBlob.dim ) );
bitZ = snapToValid( (int) Math.floor( zCoord * VoxelBlob.dim ) );
}
else
{
final double xCoord = mop.hitVec.xCoord - absOffset.getX() + mop.sideHit.getFrontOffsetX() * One32nd;
final double yCoord = mop.hitVec.yCoord - absOffset.getY() + mop.sideHit.getFrontOffsetY() * One32nd;
final double zCoord = mop.hitVec.zCoord - absOffset.getZ() + mop.sideHit.getFrontOffsetZ() * One32nd;
final int bitXi = (int) Math.floor( xCoord * VoxelBlob.dim );
final int bitYi = (int) Math.floor( yCoord * VoxelBlob.dim );
final int bitZi = (int) Math.floor( zCoord * VoxelBlob.dim );
if ( bitXi < 0 || bitYi < 0 || bitZi < 0 || bitXi >= VoxelBlob.dim || bitYi >= VoxelBlob.dim || bitZi >= VoxelBlob.dim )
{
blockPos = mop.getBlockPos().offset( mop.sideHit );
bitX = snapToValid( bitXi - mop.sideHit.getFrontOffsetX() * VoxelBlob.dim );
bitY = snapToValid( bitYi - mop.sideHit.getFrontOffsetY() * VoxelBlob.dim );
bitZ = snapToValid( bitZi - mop.sideHit.getFrontOffsetZ() * VoxelBlob.dim );
}
else
{
blockPos = mop.getBlockPos();
bitX = snapToValid( bitXi );
bitY = snapToValid( bitYi );
bitZ = snapToValid( bitZi );
}
}
}
public BitLocation(
final BlockPos pos,
final int x,
final int y,
final int z )
{
blockPos = pos;
bitX = x;
bitY = y;
bitZ = z;
}
public static BitLocation min(
final BitLocation from,
final BitLocation to )
{
final int bitX = Min( from.blockPos.getX(), to.blockPos.getX(), from.bitX, to.bitX );
final int bitY = Min( from.blockPos.getY(), to.blockPos.getY(), from.bitY, to.bitY );
final int bitZ = Min( from.blockPos.getZ(), to.blockPos.getZ(), from.bitZ, to.bitZ );
return new BitLocation( new BlockPos(
Math.min( from.blockPos.getX(), to.blockPos.getX() ),
Math.min( from.blockPos.getY(), to.blockPos.getY() ),
Math.min( from.blockPos.getZ(), to.blockPos.getZ() ) ),
bitX, bitY, bitZ );
}
public static BitLocation max(
final BitLocation from,
final BitLocation to )
{
final int bitX = Max( from.blockPos.getX(), to.blockPos.getX(), from.bitX, to.bitX );
final int bitY = Max( from.blockPos.getY(), to.blockPos.getY(), from.bitY, to.bitY );
final int bitZ = Max( from.blockPos.getZ(), to.blockPos.getZ(), from.bitZ, to.bitZ );
return new BitLocation( new BlockPos(
Math.max( from.blockPos.getX(), to.blockPos.getX() ),
Math.max( from.blockPos.getY(), to.blockPos.getY() ),
Math.max( from.blockPos.getZ(), to.blockPos.getZ() ) ),
bitX, bitY, bitZ );
}
private static int Min(
final int x,
final int x2,
final int bitX2,
final int bitX3 )
{
if ( x < x2 )
{
return bitX2;
}
if ( x2 == x )
{
return Math.min( bitX2, bitX3 );
}
return bitX3;
}
private static int Max(
final int x,
final int x2,
final int bitX2,
final int bitX3 )
{
if ( x > x2 )
{
return bitX2;
}
if ( x2 == x )
{
return Math.max( bitX2, bitX3 );
}
return bitX3;
}
}