-
Notifications
You must be signed in to change notification settings - Fork 497
/
Box.java
405 lines (321 loc) · 8.53 KB
/
Box.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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/**
* Copyright (c) 2011-2015, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.core;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import io.netty.buffer.ByteBuf;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import buildcraft.api.core.BlockIndex;
import buildcraft.api.core.IAreaProvider;
import buildcraft.api.core.IBox;
import buildcraft.api.core.ISerializable;
import buildcraft.api.core.Position;
import buildcraft.core.lib.utils.Utils;
public class Box implements IBox, ISerializable {
public enum Kind {
LASER_RED,
LASER_YELLOW,
LASER_GREEN,
LASER_BLUE,
STRIPES,
BLUE_STRIPES,
}
public Kind kind = Kind.LASER_RED;
public int xMin, yMin, zMin, xMax, yMax, zMax;
public boolean initialized;
public boolean isVisible = true;
public LaserData[] lasersData;
public Box() {
reset();
}
public Box(TileEntity e) {
initialize(e.xCoord, e.yCoord, e.zCoord, e.xCoord + 1, e.yCoord + 1,
e.zCoord + 1);
}
public Box(int xMin, int yMin, int zMin, int xMax, int yMax, int zMax) {
this ();
initialize(xMin, yMin, zMin, xMax, yMax, zMax);
}
public void reset() {
initialized = false;
xMin = Integer.MAX_VALUE;
yMin = Integer.MAX_VALUE;
zMin = Integer.MAX_VALUE;
xMax = Integer.MAX_VALUE;
yMax = Integer.MAX_VALUE;
zMax = Integer.MAX_VALUE;
}
public boolean isInitialized() {
return initialized;
}
public void initialize(int xMin, int yMin, int zMin, int xMax, int yMax, int zMax) {
if (xMin < xMax) {
this.xMin = xMin;
this.xMax = xMax;
} else {
this.xMin = xMax;
this.xMax = xMin;
}
if (yMin < yMax) {
this.yMin = yMin;
this.yMax = yMax;
} else {
this.yMin = yMax;
this.yMax = yMin;
}
if (zMin < zMax) {
this.zMin = zMin;
this.zMax = zMax;
} else {
this.zMin = zMax;
this.zMax = zMin;
}
initialized = !(xMin == Integer.MAX_VALUE || yMin == Integer.MAX_VALUE || zMin == Integer.MAX_VALUE || xMax == Integer.MAX_VALUE
|| yMax == Integer.MAX_VALUE || zMax == Integer.MAX_VALUE);
}
public void initialize(Box box) {
initialize(box.xMin, box.yMin, box.zMin, box.xMax, box.yMax, box.zMax);
}
public void initialize(IAreaProvider a) {
initialize(a.xMin(), a.yMin(), a.zMin(), a.xMax(), a.yMax(), a.zMax());
}
public void initialize(NBTTagCompound nbttagcompound) {
kind = Kind.values() [nbttagcompound.getShort("kind")];
initialize(nbttagcompound.getInteger("xMin"),
nbttagcompound.getInteger("yMin"),
nbttagcompound.getInteger("zMin"),
nbttagcompound.getInteger("xMax"),
nbttagcompound.getInteger("yMax"),
nbttagcompound.getInteger("zMax"));
}
public void initialize(int centerX, int centerY, int centerZ, int size) {
initialize(centerX - size, centerY - size, centerZ - size, centerX + size, centerY + size, centerZ + size);
}
public List<BlockIndex> getBlocksInArea() {
List<BlockIndex> blocks = new ArrayList<BlockIndex>();
for (float x = xMin; x <= xMax; x++) {
for (float y = yMin; y <= yMax; y++) {
for (float z = zMin; z <= zMax; z++) {
blocks.add(new BlockIndex((int) x, (int) y, (int) z));
}
}
}
return blocks;
}
@Override
public Box expand(int amount) {
xMin -= amount;
yMin -= amount;
zMin -= amount;
xMax += amount;
yMax += amount;
zMax += amount;
return this;
}
@Override
public IBox contract(int amount) {
return expand(-amount);
}
@Override
public boolean contains(double x, double y, double z) {
return x >= xMin && x <= xMax && y >= yMin && y <= yMax && z >= zMin && z <= zMax;
}
public boolean contains(Position p) {
return contains((int) p.x, (int) p.y, (int) p.z);
}
public boolean contains(BlockIndex i) {
return contains(i.x, i.y, i.z);
}
@Override
public Position pMin() {
return new Position(xMin, yMin, zMin);
}
@Override
public Position pMax() {
return new Position(xMax, yMax, zMax);
}
public int sizeX() {
return xMax - xMin + 1;
}
public int sizeY() {
return yMax - yMin + 1;
}
public int sizeZ() {
return zMax - zMin + 1;
}
public double centerX() {
return xMin + sizeX() / 2.0;
}
public double centerY() {
return yMin + sizeY() / 2.0;
}
public double centerZ() {
return zMin + sizeZ() / 2.0;
}
public Box rotateLeft() {
Box nBox = new Box();
nBox.xMin = (sizeZ() - 1) - zMin;
nBox.yMin = yMin;
nBox.zMin = xMin;
nBox.xMax = (sizeZ() - 1) - zMax;
nBox.yMax = yMax;
nBox.zMax = xMax;
nBox.reorder();
return nBox;
}
public void reorder() {
int tmp;
if (xMin > xMax) {
tmp = xMin;
xMin = xMax;
xMax = tmp;
}
if (yMin > yMax) {
tmp = yMin;
yMin = yMax;
yMax = tmp;
}
if (zMin > zMax) {
tmp = zMin;
zMin = zMax;
zMax = tmp;
}
}
@Override
public void createLaserData() {
lasersData = Utils.createLaserDataBox(xMin, yMin, zMin, xMax, yMax, zMax);
}
public void writeToNBT(NBTTagCompound nbttagcompound) {
nbttagcompound.setByte("kind", (byte) kind.ordinal());
nbttagcompound.setInteger("xMin", xMin);
nbttagcompound.setInteger("yMin", yMin);
nbttagcompound.setInteger("zMin", zMin);
nbttagcompound.setInteger("xMax", xMax);
nbttagcompound.setInteger("yMax", yMax);
nbttagcompound.setInteger("zMax", zMax);
}
@Override
public String toString() {
return "{" + xMin + ", " + xMax + "}, {" + yMin + ", " + yMax + "}, {" + zMin + ", " + zMax + "}";
}
public Box extendToEncompass (Box toBeContained) {
if (toBeContained == null || !toBeContained.initialized) {
return this;
}
if (toBeContained.xMin < xMin) {
xMin = toBeContained.xMin;
}
if (toBeContained.yMin < yMin) {
yMin = toBeContained.yMin;
}
if (toBeContained.zMin < zMin) {
zMin = toBeContained.zMin;
}
if (toBeContained.xMax > xMax) {
xMax = toBeContained.xMax;
}
if (toBeContained.yMax > yMax) {
yMax = toBeContained.yMax;
}
if (toBeContained.zMax > zMax) {
zMax = toBeContained.zMax;
}
return this;
}
public AxisAlignedBB getBoundingBox() {
return AxisAlignedBB.getBoundingBox(xMin, yMin, zMin,
xMax, yMax, zMax);
}
public Box extendToEncompass(Position toBeContained) {
if (toBeContained.x < xMin) {
xMin = (int) toBeContained.x - 1;
}
if (toBeContained.y < yMin) {
yMin = (int) toBeContained.y - 1;
}
if (toBeContained.z < zMin) {
zMin = (int) toBeContained.z - 1;
}
if (toBeContained.x > xMax) {
xMax = (int) toBeContained.x + 1;
}
if (toBeContained.y > yMax) {
yMax = (int) toBeContained.y + 1;
}
if (toBeContained.z > zMax) {
zMax = (int) toBeContained.z + 1;
}
return this;
}
public Box extendToEncompass(BlockIndex toBeContained) {
if (toBeContained.x < xMin) {
xMin = toBeContained.x - 1;
}
if (toBeContained.y < yMin) {
yMin = toBeContained.y - 1;
}
if (toBeContained.z < zMin) {
zMin = toBeContained.z - 1;
}
if (toBeContained.x > xMax) {
xMax = toBeContained.x + 1;
}
if (toBeContained.y > yMax) {
yMax = toBeContained.y + 1;
}
if (toBeContained.z > zMax) {
zMax = toBeContained.z + 1;
}
return this;
}
@Override
public double distanceTo(BlockIndex index) {
return Math.sqrt(distanceToSquared(index));
}
@Override
public double distanceToSquared(BlockIndex index) {
int dx = index.x - (xMin + (xMax - xMin + 1));
int dy = index.y - (yMin + (yMax - yMin + 1));
int dz = index.z - (zMin + (zMax - zMin + 1));
return dx * dx + dy * dy + dz * dz;
}
@Override
public BlockIndex getRandomBlockIndex(Random rand) {
int x = (xMax > xMin) ? xMin + rand.nextInt(xMax - xMin + 1) : xMin;
int y = (yMax > yMin) ? yMin + rand.nextInt(yMax - yMin + 1) : yMin;
int z = (zMax > zMin) ? zMin + rand.nextInt(zMax - zMin + 1) : zMin;
return new BlockIndex(x, y, z);
}
@Override
public void readData(ByteBuf stream) {
byte flags = stream.readByte();
xMin = stream.readInt();
yMin = stream.readShort();
zMin = stream.readInt();
xMax = stream.readInt();
yMax = stream.readShort();
zMax = stream.readInt();
kind = Kind.values()[flags & 31];
initialized = (flags & 64) != 0;
isVisible = (flags & 32) != 0;
}
@Override
public void writeData(ByteBuf stream) {
stream.writeByte((initialized ? 64 : 0) | (isVisible ? 32 : 0) | kind.ordinal());
stream.writeInt(xMin);
stream.writeShort(yMin);
stream.writeInt(zMin);
stream.writeInt(xMax);
stream.writeShort(yMax);
stream.writeInt(zMax);
}
}