forked from Minestom/Minestom
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathShapeImpl.java
More file actions
301 lines (269 loc) · 13.1 KB
/
ShapeImpl.java
File metadata and controls
301 lines (269 loc) · 13.1 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package net.minestom.server.collision;
import it.unimi.dsi.fastutil.doubles.DoubleArrayList;
import it.unimi.dsi.fastutil.doubles.DoubleList;
import net.minestom.server.coordinate.Point;
import net.minestom.server.coordinate.Vec;
import net.minestom.server.instance.block.BlockFace;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Unmodifiable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public record ShapeImpl(CollisionData collisionData, LightData lightData) implements Shape {
private static final Pattern PATTERN = Pattern.compile("\\d.\\d+", Pattern.MULTILINE);
record CollisionData(List<BoundingBox> collisionBoundingBoxes,
Point relativeStart, Point relativeEnd,
byte fullFaces) {
public CollisionData {
collisionBoundingBoxes = List.copyOf(collisionBoundingBoxes);
}
}
record LightData(List<BoundingBox> occlusionBoundingBoxes,
byte blockOcclusion, byte airOcclusion,
int lightEmission) {
public LightData {
occlusionBoundingBoxes = List.copyOf(occlusionBoundingBoxes);
}
}
/**
* Computes the occlusion for a given face.
*
* @param covering The rectangle set to check for covering.
* @return 0 if face is not covered, 1 if face is covered partially, 2 if face is fully covered.
*/
private static byte isFaceCovered(List<Rectangle> covering) {
if (covering.isEmpty()) return 0;
Rectangle r = new Rectangle(0, 0, 1, 1);
List<Rectangle> toCover = new ArrayList<>();
toCover.add(r);
for (Rectangle rect : covering) {
List<Rectangle> nextCovering = new ArrayList<>();
for (Rectangle toCoverRect : toCover) {
List<Rectangle> remaining = getRemaining(rect, toCoverRect);
nextCovering.addAll(remaining);
}
toCover = nextCovering;
if (toCover.isEmpty()) return 2;
}
return 1;
}
@Override
public @NotNull Point relativeStart() {
return collisionData.relativeStart;
}
@Override
public @NotNull Point relativeEnd() {
return collisionData.relativeEnd;
}
@Override
public boolean isOccluded(@NotNull Shape shape, @NotNull BlockFace face) {
final LightData lightData = this.lightData;
final LightData otherLightData = ((ShapeImpl) shape).lightData;
final boolean hasBlockOcclusion = (((lightData.blockOcclusion >> face.ordinal()) & 1) == 1);
final boolean hasBlockOcclusionOther = ((otherLightData.blockOcclusion >> face.getOppositeFace().ordinal()) & 1) == 1;
if (lightData.lightEmission > 0) return hasBlockOcclusionOther;
// If either face is full, return true
if (hasBlockOcclusion || hasBlockOcclusionOther) return true;
final boolean hasAirOcclusion = (((lightData.airOcclusion >> face.ordinal()) & 1) == 1);
final boolean hasAirOcclusionOther = ((otherLightData.airOcclusion >> face.getOppositeFace().ordinal()) & 1) == 1;
// If a single face is air, return false
if (hasAirOcclusion || hasAirOcclusionOther) return false;
// Comparing two partial faces. Computation needed
List<Rectangle> allRectangles = computeOcclusionSet(face.getOppositeFace(), otherLightData.occlusionBoundingBoxes);
allRectangles.addAll(computeOcclusionSet(face, lightData.occlusionBoundingBoxes));
return isFaceCovered(allRectangles) == 2;
}
@Override
public boolean isFaceFull(@NotNull BlockFace face) {
return (((collisionData.fullFaces >> face.ordinal()) & 1) == 1);
}
@Override
public boolean intersectBox(@NotNull Point position, @NotNull BoundingBox boundingBox) {
for (BoundingBox blockSection : collisionData.collisionBoundingBoxes) {
if (boundingBox.intersectBox(position, blockSection)) return true;
}
return false;
}
@Override
public boolean intersectBoxSwept(@NotNull Point rayStart, @NotNull Point rayDirection,
@NotNull Point shapePos, @NotNull BoundingBox moving, @NotNull SweepResult finalResult) {
boolean hitBlock = false;
for (BoundingBox blockSection : collisionData.collisionBoundingBoxes) {
// Update final result if the temp result collision is sooner than the current final result
if (RayUtils.BoundingBoxIntersectionCheck(moving, rayStart, rayDirection, blockSection, shapePos, finalResult)) {
finalResult.collidedPositionX = rayStart.x() + rayDirection.x() * finalResult.res;
finalResult.collidedPositionY = rayStart.y() + rayDirection.y() * finalResult.res;
finalResult.collidedPositionZ = rayStart.z() + rayDirection.z() * finalResult.res;
finalResult.collidedShapeX = shapePos.x();
finalResult.collidedShapeY = shapePos.y();
finalResult.collidedShapeZ = shapePos.z();
finalResult.collidedShape = this;
hitBlock = true;
}
}
return hitBlock;
}
/**
* Gets the collision bounding boxes for this block. There will be more than one bounds for more complex shapes e.g.
* stairs.
*
* @return the collision bounding boxes for this block
*/
public @NotNull @Unmodifiable List<BoundingBox> collisionBoundingBoxes() {
return collisionData.collisionBoundingBoxes;
}
/**
* Gets the occlusion bounding boxes for this block.
*
* @return the occlusion bounding boxes for this block
*/
public @NotNull @Unmodifiable List<BoundingBox> occlusionBoundingBoxes() {
return lightData.occlusionBoundingBoxes;
}
static final Map<ShapeImpl, ShapeImpl> SHAPES = new ConcurrentHashMap<>();
static ShapeImpl parseBlockFromRegistry(String collision, String occlusion, boolean occludes, int lightEmission) {
BoundingBox[] collisionBoundingBoxes = parseRegistryBoundingBoxString(collision);
BoundingBox[] occlusionBoundingBoxes = occludes ? parseRegistryBoundingBoxString(occlusion) : new BoundingBox[0];
final CollisionData collisionData = collisionData(List.of(collisionBoundingBoxes));
final LightData lightData = lightData(List.of(occlusionBoundingBoxes), lightEmission);
final ShapeImpl shape = new ShapeImpl(collisionData, lightData);
return SHAPES.computeIfAbsent(shape, k -> k);
}
private static BoundingBox[] parseRegistryBoundingBoxString(String str) {
final Matcher matcher = PATTERN.matcher(str);
DoubleList vals = new DoubleArrayList();
while (matcher.find()) {
double newVal = Double.parseDouble(matcher.group());
vals.add(newVal);
}
final int count = vals.size() / 6;
BoundingBox[] boundingBoxes = new BoundingBox[count];
for (int i = 0; i < count; ++i) {
final double minX = vals.getDouble(0 + 6 * i);
final double minY = vals.getDouble(1 + 6 * i);
final double minZ = vals.getDouble(2 + 6 * i);
final double boundXSize = vals.getDouble(3 + 6 * i) - minX;
final double boundYSize = vals.getDouble(4 + 6 * i) - minY;
final double boundZSize = vals.getDouble(5 + 6 * i) - minZ;
final Vec min = new Vec(minX, minY, minZ);
final Vec max = new Vec(minX + boundXSize, minY + boundYSize, minZ + boundZSize);
final BoundingBox bb = new BoundingBox(min, max);
if (bb.minX() != minX || bb.minY() != minY || bb.minZ() != minZ) {
throw new IllegalStateException("BoundingBox min values do not match the parsed values.");
}
boundingBoxes[i] = bb;
}
return boundingBoxes;
}
private static CollisionData collisionData(List<BoundingBox> collisionBoundingBoxes) {
// Find bounds of collision
Vec relativeStart;
Vec relativeEnd;
if (!collisionBoundingBoxes.isEmpty()) {
double minX = 1, minY = 1, minZ = 1;
double maxX = 0, maxY = 0, maxZ = 0;
for (BoundingBox blockSection : collisionBoundingBoxes) {
// Min
if (blockSection.minX() < minX) minX = blockSection.minX();
if (blockSection.minY() < minY) minY = blockSection.minY();
if (blockSection.minZ() < minZ) minZ = blockSection.minZ();
// Max
if (blockSection.maxX() > maxX) maxX = blockSection.maxX();
if (blockSection.maxY() > maxY) maxY = blockSection.maxY();
if (blockSection.maxZ() > maxZ) maxZ = blockSection.maxZ();
}
relativeStart = new Vec(minX, minY, minZ);
relativeEnd = new Vec(maxX, maxY, maxZ);
} else {
relativeStart = Vec.ZERO;
relativeEnd = Vec.ZERO;
}
byte fullCollisionFaces = 0;
for (BlockFace f : BlockFace.getValues()) {
final byte res = isFaceCovered(computeOcclusionSet(f, collisionBoundingBoxes));
fullCollisionFaces |= ((res == 2) ? 0b1 : 0b0) << (byte) f.ordinal();
}
return new CollisionData(collisionBoundingBoxes, relativeStart, relativeEnd, fullCollisionFaces);
}
private static LightData lightData(List<BoundingBox> occlusionBoundingBoxes, int lightEmission) {
byte fullFaces = 0;
byte airFaces = 0;
for (BlockFace f : BlockFace.getValues()) {
final byte res = isFaceCovered(computeOcclusionSet(f, occlusionBoundingBoxes));
fullFaces |= ((res == 2) ? 0b1 : 0b0) << (byte) f.ordinal();
airFaces |= ((res == 0) ? 0b1 : 0b0) << (byte) f.ordinal();
}
return new LightData(occlusionBoundingBoxes, fullFaces, airFaces, lightEmission);
}
private static @NotNull List<Rectangle> computeOcclusionSet(BlockFace face, List<BoundingBox> boundingBoxes) {
List<Rectangle> rSet = new ArrayList<>();
for (BoundingBox boundingBox : boundingBoxes) {
switch (face) {
case NORTH -> // negative Z
{
if (boundingBox.minZ() == 0)
rSet.add(new Rectangle(boundingBox.minX(), boundingBox.minY(), boundingBox.maxX(), boundingBox.maxY()));
}
case SOUTH -> // positive Z
{
if (boundingBox.maxZ() == 1)
rSet.add(new Rectangle(boundingBox.minX(), boundingBox.minY(), boundingBox.maxX(), boundingBox.maxY()));
}
case WEST -> // negative X
{
if (boundingBox.minX() == 0)
rSet.add(new Rectangle(boundingBox.minY(), boundingBox.minZ(), boundingBox.maxY(), boundingBox.maxZ()));
}
case EAST -> // positive X
{
if (boundingBox.maxX() == 1)
rSet.add(new Rectangle(boundingBox.minY(), boundingBox.minZ(), boundingBox.maxY(), boundingBox.maxZ()));
}
case BOTTOM -> // negative Y
{
if (boundingBox.minY() == 0)
rSet.add(new Rectangle(boundingBox.minX(), boundingBox.minZ(), boundingBox.maxX(), boundingBox.maxZ()));
}
case TOP -> // positive Y
{
if (boundingBox.maxY() == 1)
rSet.add(new Rectangle(boundingBox.minX(), boundingBox.minZ(), boundingBox.maxX(), boundingBox.maxZ()));
}
}
}
return rSet;
}
private static List<Rectangle> getRemaining(Rectangle covering, Rectangle toCover) {
List<Rectangle> remaining = new ArrayList<>();
covering = clipRectangle(covering, toCover);
// Up
if (covering.y1() > toCover.y1()) {
remaining.add(new Rectangle(toCover.x1(), toCover.y1(), toCover.x2(), covering.y1()));
}
// Down
if (covering.y2() < toCover.y2()) {
remaining.add(new Rectangle(toCover.x1(), covering.y2(), toCover.x2(), toCover.y2()));
}
// Left
if (covering.x1() > toCover.x1()) {
remaining.add(new Rectangle(toCover.x1(), covering.y1(), covering.x1(), covering.y2()));
}
//Right
if (covering.x2() < toCover.x2()) {
remaining.add(new Rectangle(covering.x2(), covering.y1(), toCover.x2(), covering.y2()));
}
return remaining;
}
private static Rectangle clipRectangle(Rectangle covering, Rectangle toCover) {
final double x1 = Math.max(covering.x1(), toCover.x1());
final double y1 = Math.max(covering.y1(), toCover.y1());
final double x2 = Math.min(covering.x2(), toCover.x2());
final double y2 = Math.min(covering.y2(), toCover.y2());
return new Rectangle(x1, y1, x2, y2);
}
private record Rectangle(double x1, double y1, double x2, double y2) {
}
}