@nxg-org/mineflayer-util-plugin 1.8.3 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/calcs/aabb.js CHANGED
@@ -1,330 +1,330 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AABB = void 0;
4
- const vec3_1 = require("vec3");
5
- function lerp(f, f2, f3) {
6
- return f2 + f * (f3 - f2);
7
- }
8
- const emptyVec = new vec3_1.Vec3(0, 0, 0);
9
- class AABB {
10
- constructor(x0, y0, z0, x1, y1, z1) {
11
- this.minX = x0;
12
- this.minY = y0;
13
- this.minZ = z0;
14
- this.maxX = x1;
15
- this.maxY = y1;
16
- this.maxZ = z1;
17
- }
18
- static fromVecs(min, max) {
19
- return new AABB(min.x, min.y, min.z, max.x, max.y, max.z);
20
- }
21
- static fromBlock(min) {
22
- return new AABB(min.x, min.y, min.z, min.x + 1.0, min.y + 1.0, min.z + 1.0);
23
- }
24
- static fromBlockPos(min) {
25
- return new AABB(Math.floor(min.x), Math.floor(min.y), Math.floor(min.z), Math.floor(min.x) + 1.0, Math.floor(min.y) + 1.0, Math.floor(min.z) + 1.0);
26
- }
27
- static fromShape(pts, offset = emptyVec) {
28
- return new AABB(pts[0], pts[1], pts[2], pts[3], pts[4], pts[5]).translateVec(offset);
29
- }
30
- set(x0, y0, z0, x1, y1, z1) {
31
- this.minX = x0;
32
- this.minY = y0;
33
- this.minZ = z0;
34
- this.maxX = x1;
35
- this.maxY = y1;
36
- this.maxZ = z1;
37
- }
38
- clone() {
39
- return new AABB(this.minX, this.minY, this.minZ, this.maxX, this.maxY, this.maxZ);
40
- }
41
- minPoint() {
42
- return new vec3_1.Vec3(this.minX, this.minY, this.minZ);
43
- }
44
- maxPoint() {
45
- return new vec3_1.Vec3(this.maxX, this.maxY, this.maxZ);
46
- }
47
- bottomMiddlePoint() {
48
- return new vec3_1.Vec3((this.maxX - this.minX) / 2, this.minY, (this.maxZ - this.minZ) / 2);
49
- }
50
- heightAndWidths() {
51
- return new vec3_1.Vec3(this.maxX - this.minX, this.maxY - this.minY, this.maxZ - this.minZ);
52
- }
53
- toArray() {
54
- return [this.minX, this.minY, this.minZ, this.maxX, this.maxY, this.maxZ];
55
- }
56
- minAndMaxArrays() {
57
- return [
58
- [this.minX, this.minY, this.minZ],
59
- [this.maxX, this.maxY, this.maxZ],
60
- ];
61
- }
62
- toVecs() {
63
- return [new vec3_1.Vec3(this.minX, this.minY, this.minZ), new vec3_1.Vec3(this.maxX, this.maxY, this.maxZ)];
64
- }
65
- /**
66
- * Compatible with Iterators from prismarine-world.
67
- * Used like a block for prismarine-world.
68
- * @returns {number[][]} single element long array of shapes.
69
- */
70
- toShapeFromMin() {
71
- return [[0, 0, 0, this.maxX - this.minX, this.maxY - this.minY, this.maxZ - this.minZ]];
72
- }
73
- /**
74
- * Compatible with Iterators from prismarine-world.
75
- * Used with the entity's actual position for prismarine-world.
76
- * @returns {number[][]} single element long array of shapes.
77
- */
78
- toShapeFromBottomMiddle() {
79
- const wx = (this.maxX - this.minX) / 2;
80
- const wz = (this.maxZ - this.minZ) / 2;
81
- return [[-wx, 0, -wz, wx, this.maxY - this.minY, wz]];
82
- }
83
- toVertices() {
84
- return [
85
- new vec3_1.Vec3(this.minX, this.minY, this.minZ),
86
- new vec3_1.Vec3(this.minX, this.minY, this.maxZ),
87
- new vec3_1.Vec3(this.minX, this.maxY, this.minZ),
88
- new vec3_1.Vec3(this.minX, this.maxY, this.maxZ),
89
- new vec3_1.Vec3(this.maxX, this.minY, this.minZ),
90
- new vec3_1.Vec3(this.maxX, this.minY, this.maxZ),
91
- new vec3_1.Vec3(this.maxX, this.maxY, this.minZ),
92
- new vec3_1.Vec3(this.maxX, this.maxY, this.maxZ),
93
- ];
94
- }
95
- floor() {
96
- this.minX = Math.floor(this.minX);
97
- this.minY = Math.floor(this.minY);
98
- this.minZ = Math.floor(this.minZ);
99
- this.maxX = Math.floor(this.maxX);
100
- this.maxY = Math.floor(this.maxY);
101
- this.maxZ = Math.floor(this.maxZ);
102
- return this;
103
- }
104
- extend(dx, dy, dz) {
105
- if (dx < 0)
106
- this.minX += dx;
107
- else
108
- this.maxX += dx;
109
- if (dy < 0)
110
- this.minY += dy;
111
- else
112
- this.maxY += dy;
113
- if (dz < 0)
114
- this.minZ += dz;
115
- else
116
- this.maxZ += dz;
117
- return this;
118
- }
119
- contract(x, y, z) {
120
- this.minX += x;
121
- this.minY += y;
122
- this.minZ += z;
123
- this.maxX -= x;
124
- this.maxY -= y;
125
- this.maxZ -= z;
126
- return this;
127
- }
128
- expand(x, y, z) {
129
- this.minX -= x;
130
- this.minY -= y;
131
- this.minZ -= z;
132
- this.maxX += x;
133
- this.maxY += y;
134
- this.maxZ += z;
135
- return this;
136
- }
137
- translate(x, y, z) {
138
- this.minX += x;
139
- this.minY += y;
140
- this.minZ += z;
141
- this.maxX += x;
142
- this.maxY += y;
143
- this.maxZ += z;
144
- return this;
145
- }
146
- translateVec(vec) {
147
- this.minX += vec.x;
148
- this.minY += vec.y;
149
- this.minZ += vec.z;
150
- this.maxX += vec.x;
151
- this.maxY += vec.y;
152
- this.maxZ += vec.z;
153
- return this;
154
- }
155
- computeOffsetX(other, offsetX) {
156
- if (other.maxY > this.minY && other.minY < this.maxY && other.maxZ > this.minZ && other.minZ < this.maxZ) {
157
- if (offsetX > 0.0 && other.maxX <= this.minX) {
158
- offsetX = Math.min(this.minX - other.maxX, offsetX);
159
- }
160
- else if (offsetX < 0.0 && other.minX >= this.maxX) {
161
- offsetX = Math.max(this.maxX - other.minX, offsetX);
162
- }
163
- }
164
- return offsetX;
165
- }
166
- computeOffsetY(other, offsetY) {
167
- if (other.maxX > this.minX && other.minX < this.maxX && other.maxZ > this.minZ && other.minZ < this.maxZ) {
168
- if (offsetY > 0.0 && other.maxY <= this.minY) {
169
- offsetY = Math.min(this.minY - other.maxY, offsetY);
170
- }
171
- else if (offsetY < 0.0 && other.minY >= this.maxY) {
172
- offsetY = Math.max(this.maxY - other.minY, offsetY);
173
- }
174
- }
175
- return offsetY;
176
- }
177
- computeOffsetZ(other, offsetZ) {
178
- if (other.maxX > this.minX && other.minX < this.maxX && other.maxY > this.minY && other.minY < this.maxY) {
179
- if (offsetZ > 0.0 && other.maxZ <= this.minZ) {
180
- offsetZ = Math.min(this.minZ - other.maxZ, offsetZ);
181
- }
182
- else if (offsetZ < 0.0 && other.minZ >= this.maxZ) {
183
- offsetZ = Math.max(this.maxZ - other.minZ, offsetZ);
184
- }
185
- }
186
- return offsetZ;
187
- }
188
- intersects(other) {
189
- return (this.minX < other.maxX &&
190
- this.maxX > other.minX &&
191
- this.minY < other.maxY &&
192
- this.maxY > other.minY &&
193
- this.minZ < other.maxZ &&
194
- this.maxZ > other.minZ);
195
- }
196
- xzIntersectsRay(org, dir) {
197
- const d = this.distanceFromRay(org, dir, true);
198
- return d === Infinity ? null : { x: org.x + dir.x * d, z: org.z + dir.z * d };
199
- }
200
- intersectsRay(org, dir) {
201
- const d = this.distanceFromRay(org, dir);
202
- return d === Infinity ? null : new vec3_1.Vec3(org.x + dir.x * d, org.y + dir.y * d, org.z + dir.z * d);
203
- }
204
- //TODO: Better check for hit reg of PLANES.
205
- xzIntersectsSegment(org, dest) {
206
- const dir = dest.clone().subtract(org).normalize();
207
- const d = this.distanceFromRay(org, dir, true);
208
- return d > dest.distanceTo(org) || d < 0 ? null : { x: org.x + dir.x * d, z: org.z + dir.z * d };
209
- }
210
- //TODO: Better check for hit reg of PLANES.
211
- intersectsSegment(org, dest) {
212
- const dir = dest.clone().subtract(org).normalize();
213
- const d = this.distanceFromRay(org, dir);
214
- return d > dest.distanceTo(org) || d < 0 ? null : new vec3_1.Vec3(org.x + dir.x * d, org.y + dir.y * d, org.z + dir.z * d);
215
- }
216
- distanceFromRay(origin, direction, xz = false) {
217
- const ro = origin.toArray();
218
- const rd = direction.clone().normalize().toArray();
219
- const aabb = this.minAndMaxArrays();
220
- const dims = ro.length; // will change later.
221
- const dif = xz ? 2 : 1;
222
- let lo = -Infinity;
223
- let hi = +Infinity;
224
- // let test = origin.clone()
225
- for (let i = 0; i < dims; i += dif) {
226
- let dimLo = (aabb[0][i] - ro[i]) / rd[i];
227
- let dimHi = (aabb[1][i] - ro[i]) / rd[i];
228
- if (dimLo > dimHi) {
229
- let tmp = dimLo;
230
- dimLo = dimHi;
231
- dimHi = tmp;
232
- }
233
- if (dimHi < lo || dimLo > hi) {
234
- return Infinity;
235
- }
236
- if (dimLo > lo)
237
- lo = dimLo;
238
- if (dimHi < hi)
239
- hi = dimHi;
240
- }
241
- return lo > hi ? Infinity : lo;
242
- }
243
- intersect(aABB) {
244
- const d = Math.max(this.minX, aABB.minX);
245
- const d2 = Math.max(this.minY, aABB.minY);
246
- const d3 = Math.max(this.minZ, aABB.minZ);
247
- const d4 = Math.min(this.maxX, aABB.maxX);
248
- const d5 = Math.min(this.maxY, aABB.maxY);
249
- const d6 = Math.min(this.maxZ, aABB.maxZ);
250
- return new AABB(d, d2, d3, d4, d5, d6);
251
- }
252
- equals(other) {
253
- return (this.minX === other.minX &&
254
- this.minY === other.minY &&
255
- this.minZ === other.minZ &&
256
- this.maxX === other.maxX &&
257
- this.maxY === other.maxY &&
258
- this.maxZ === other.maxZ);
259
- }
260
- xzDistanceToVec(pos) {
261
- let dx = Math.max(this.minX - pos.x, 0, pos.x - this.maxX);
262
- let dz = Math.max(this.minZ - pos.z, 0, pos.z - this.maxZ);
263
- return Math.sqrt(dx * dx + dz * dz);
264
- }
265
- distanceToVec(pos) {
266
- let dx = Math.max(this.minX - pos.x, 0, pos.x - this.maxX);
267
- let dy = Math.max(this.minY - pos.y, 0, pos.y - this.maxY);
268
- let dz = Math.max(this.minZ - pos.z, 0, pos.z - this.maxZ);
269
- return Math.sqrt(dx * dx + dy * dy + dz * dz);
270
- }
271
- expandTowards(vec3) {
272
- return this.expandTowardsCoords(vec3.x, vec3.y, vec3.z);
273
- }
274
- expandTowardsCoords(d, d2, d3) {
275
- let d4 = this.minX;
276
- let d5 = this.minY;
277
- let d6 = this.minZ;
278
- let d7 = this.maxX;
279
- let d8 = this.maxY;
280
- let d9 = this.maxZ;
281
- if (d < 0.0) {
282
- d4 += d;
283
- }
284
- else if (d > 0.0) {
285
- d7 += d;
286
- }
287
- if (d2 < 0.0) {
288
- d5 += d2;
289
- }
290
- else if (d2 > 0.0) {
291
- d8 += d2;
292
- }
293
- if (d3 < 0.0) {
294
- d6 += d3;
295
- }
296
- else if (d3 > 0.0) {
297
- d9 += d3;
298
- }
299
- return new AABB(d4, d5, d6, d7, d8, d9);
300
- }
301
- moveCoords(d, d2, d3) {
302
- return new AABB(this.minX + d, this.minY + d2, this.minZ + d3, this.maxX + d, this.maxY + d2, this.maxZ + d3);
303
- }
304
- move(vec3) {
305
- return new AABB(this.minX + vec3.x, this.minY + vec3.y, this.minZ + vec3.z, this.maxX + vec3.x, this.maxY + vec3.y, this.maxZ + vec3.z);
306
- }
307
- intersectsCoords(d, d2, d3, d4, d5, d6) {
308
- return this.minX < d4 && this.maxX > d && this.minY < d5 && this.maxY > d2 && this.minZ < d6 && this.maxZ > d3;
309
- }
310
- collides(aABB) {
311
- return this.collidesCoords(aABB.minX, aABB.minY, aABB.minZ, aABB.maxX, aABB.maxY, aABB.maxZ);
312
- }
313
- collidesCoords(d, d2, d3, d4, d5, d6) {
314
- return this.minX <= d4 && this.maxX >= d && this.minY <= d5 && this.maxY >= d2 && this.minZ <= d6 && this.maxZ >= d3;
315
- }
316
- contains(aABB) {
317
- return this.containsCoords(aABB.minX, aABB.minY, aABB.minZ) && this.containsCoords(aABB.maxX, aABB.maxY, aABB.maxZ);
318
- }
319
- containsVec(vec) {
320
- return this.minX <= vec.x && this.maxX >= vec.x && this.minY <= vec.y && this.maxY >= vec.y && this.minZ <= vec.z && this.maxZ >= vec.z;
321
- }
322
- containsCoords(x, y, z) {
323
- return this.minX <= x && this.maxX >= x && this.minY <= y && this.maxY >= y && this.minZ <= z && this.maxZ >= z;
324
- }
325
- getCenter() {
326
- return new vec3_1.Vec3(lerp(0.5, this.minX, this.maxX), lerp(0.5, this.minY, this.maxY), lerp(0.5, this.minZ, this.maxZ));
327
- }
328
- }
329
- exports.AABB = AABB;
330
- exports.default = AABB;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AABB = void 0;
4
+ const vec3_1 = require("vec3");
5
+ function lerp(f, f2, f3) {
6
+ return f2 + f * (f3 - f2);
7
+ }
8
+ const emptyVec = new vec3_1.Vec3(0, 0, 0);
9
+ class AABB {
10
+ constructor(x0, y0, z0, x1, y1, z1) {
11
+ this.minX = x0;
12
+ this.minY = y0;
13
+ this.minZ = z0;
14
+ this.maxX = x1;
15
+ this.maxY = y1;
16
+ this.maxZ = z1;
17
+ }
18
+ static fromVecs(min, max) {
19
+ return new AABB(min.x, min.y, min.z, max.x, max.y, max.z);
20
+ }
21
+ static fromBlock(min) {
22
+ return new AABB(min.x, min.y, min.z, min.x + 1.0, min.y + 1.0, min.z + 1.0);
23
+ }
24
+ static fromBlockPos(min) {
25
+ return new AABB(Math.floor(min.x), Math.floor(min.y), Math.floor(min.z), Math.floor(min.x) + 1.0, Math.floor(min.y) + 1.0, Math.floor(min.z) + 1.0);
26
+ }
27
+ static fromShape(pts, offset = emptyVec) {
28
+ return new AABB(pts[0], pts[1], pts[2], pts[3], pts[4], pts[5]).translateVec(offset);
29
+ }
30
+ set(x0, y0, z0, x1, y1, z1) {
31
+ this.minX = x0;
32
+ this.minY = y0;
33
+ this.minZ = z0;
34
+ this.maxX = x1;
35
+ this.maxY = y1;
36
+ this.maxZ = z1;
37
+ }
38
+ clone() {
39
+ return new AABB(this.minX, this.minY, this.minZ, this.maxX, this.maxY, this.maxZ);
40
+ }
41
+ minPoint() {
42
+ return new vec3_1.Vec3(this.minX, this.minY, this.minZ);
43
+ }
44
+ maxPoint() {
45
+ return new vec3_1.Vec3(this.maxX, this.maxY, this.maxZ);
46
+ }
47
+ bottomMiddlePoint() {
48
+ return new vec3_1.Vec3((this.maxX - this.minX) / 2, this.minY, (this.maxZ - this.minZ) / 2);
49
+ }
50
+ heightAndWidths() {
51
+ return new vec3_1.Vec3(this.maxX - this.minX, this.maxY - this.minY, this.maxZ - this.minZ);
52
+ }
53
+ toArray() {
54
+ return [this.minX, this.minY, this.minZ, this.maxX, this.maxY, this.maxZ];
55
+ }
56
+ minAndMaxArrays() {
57
+ return [
58
+ [this.minX, this.minY, this.minZ],
59
+ [this.maxX, this.maxY, this.maxZ],
60
+ ];
61
+ }
62
+ toVecs() {
63
+ return [new vec3_1.Vec3(this.minX, this.minY, this.minZ), new vec3_1.Vec3(this.maxX, this.maxY, this.maxZ)];
64
+ }
65
+ /**
66
+ * Compatible with Iterators from prismarine-world.
67
+ * Used like a block for prismarine-world.
68
+ * @returns {number[][]} single element long array of shapes.
69
+ */
70
+ toShapeFromMin() {
71
+ return [[0, 0, 0, this.maxX - this.minX, this.maxY - this.minY, this.maxZ - this.minZ]];
72
+ }
73
+ /**
74
+ * Compatible with Iterators from prismarine-world.
75
+ * Used with the entity's actual position for prismarine-world.
76
+ * @returns {number[][]} single element long array of shapes.
77
+ */
78
+ toShapeFromBottomMiddle() {
79
+ const wx = (this.maxX - this.minX) / 2;
80
+ const wz = (this.maxZ - this.minZ) / 2;
81
+ return [[-wx, 0, -wz, wx, this.maxY - this.minY, wz]];
82
+ }
83
+ toVertices() {
84
+ return [
85
+ new vec3_1.Vec3(this.minX, this.minY, this.minZ),
86
+ new vec3_1.Vec3(this.minX, this.minY, this.maxZ),
87
+ new vec3_1.Vec3(this.minX, this.maxY, this.minZ),
88
+ new vec3_1.Vec3(this.minX, this.maxY, this.maxZ),
89
+ new vec3_1.Vec3(this.maxX, this.minY, this.minZ),
90
+ new vec3_1.Vec3(this.maxX, this.minY, this.maxZ),
91
+ new vec3_1.Vec3(this.maxX, this.maxY, this.minZ),
92
+ new vec3_1.Vec3(this.maxX, this.maxY, this.maxZ),
93
+ ];
94
+ }
95
+ floor() {
96
+ this.minX = Math.floor(this.minX);
97
+ this.minY = Math.floor(this.minY);
98
+ this.minZ = Math.floor(this.minZ);
99
+ this.maxX = Math.floor(this.maxX);
100
+ this.maxY = Math.floor(this.maxY);
101
+ this.maxZ = Math.floor(this.maxZ);
102
+ return this;
103
+ }
104
+ extend(dx, dy, dz) {
105
+ if (dx < 0)
106
+ this.minX += dx;
107
+ else
108
+ this.maxX += dx;
109
+ if (dy < 0)
110
+ this.minY += dy;
111
+ else
112
+ this.maxY += dy;
113
+ if (dz < 0)
114
+ this.minZ += dz;
115
+ else
116
+ this.maxZ += dz;
117
+ return this;
118
+ }
119
+ contract(x, y, z) {
120
+ this.minX += x;
121
+ this.minY += y;
122
+ this.minZ += z;
123
+ this.maxX -= x;
124
+ this.maxY -= y;
125
+ this.maxZ -= z;
126
+ return this;
127
+ }
128
+ expand(x, y, z) {
129
+ this.minX -= x;
130
+ this.minY -= y;
131
+ this.minZ -= z;
132
+ this.maxX += x;
133
+ this.maxY += y;
134
+ this.maxZ += z;
135
+ return this;
136
+ }
137
+ translate(x, y, z) {
138
+ this.minX += x;
139
+ this.minY += y;
140
+ this.minZ += z;
141
+ this.maxX += x;
142
+ this.maxY += y;
143
+ this.maxZ += z;
144
+ return this;
145
+ }
146
+ translateVec(vec) {
147
+ this.minX += vec.x;
148
+ this.minY += vec.y;
149
+ this.minZ += vec.z;
150
+ this.maxX += vec.x;
151
+ this.maxY += vec.y;
152
+ this.maxZ += vec.z;
153
+ return this;
154
+ }
155
+ computeOffsetX(other, offsetX) {
156
+ if (other.maxY > this.minY && other.minY < this.maxY && other.maxZ > this.minZ && other.minZ < this.maxZ) {
157
+ if (offsetX > 0.0 && other.maxX <= this.minX) {
158
+ offsetX = Math.min(this.minX - other.maxX, offsetX);
159
+ }
160
+ else if (offsetX < 0.0 && other.minX >= this.maxX) {
161
+ offsetX = Math.max(this.maxX - other.minX, offsetX);
162
+ }
163
+ }
164
+ return offsetX;
165
+ }
166
+ computeOffsetY(other, offsetY) {
167
+ if (other.maxX > this.minX && other.minX < this.maxX && other.maxZ > this.minZ && other.minZ < this.maxZ) {
168
+ if (offsetY > 0.0 && other.maxY <= this.minY) {
169
+ offsetY = Math.min(this.minY - other.maxY, offsetY);
170
+ }
171
+ else if (offsetY < 0.0 && other.minY >= this.maxY) {
172
+ offsetY = Math.max(this.maxY - other.minY, offsetY);
173
+ }
174
+ }
175
+ return offsetY;
176
+ }
177
+ computeOffsetZ(other, offsetZ) {
178
+ if (other.maxX > this.minX && other.minX < this.maxX && other.maxY > this.minY && other.minY < this.maxY) {
179
+ if (offsetZ > 0.0 && other.maxZ <= this.minZ) {
180
+ offsetZ = Math.min(this.minZ - other.maxZ, offsetZ);
181
+ }
182
+ else if (offsetZ < 0.0 && other.minZ >= this.maxZ) {
183
+ offsetZ = Math.max(this.maxZ - other.minZ, offsetZ);
184
+ }
185
+ }
186
+ return offsetZ;
187
+ }
188
+ intersects(other) {
189
+ return (this.minX < other.maxX &&
190
+ this.maxX > other.minX &&
191
+ this.minY < other.maxY &&
192
+ this.maxY > other.minY &&
193
+ this.minZ < other.maxZ &&
194
+ this.maxZ > other.minZ);
195
+ }
196
+ xzIntersectsRay(org, dir) {
197
+ const d = this.distanceFromRay(org, dir, true);
198
+ return d === Infinity ? null : { x: org.x + dir.x * d, z: org.z + dir.z * d };
199
+ }
200
+ intersectsRay(org, dir) {
201
+ const d = this.distanceFromRay(org, dir);
202
+ return d === Infinity ? null : new vec3_1.Vec3(org.x + dir.x * d, org.y + dir.y * d, org.z + dir.z * d);
203
+ }
204
+ //TODO: Better check for hit reg of PLANES.
205
+ xzIntersectsSegment(org, dest) {
206
+ const dir = dest.clone().subtract(org).normalize();
207
+ const d = this.distanceFromRay(org, dir, true);
208
+ return d > dest.distanceTo(org) || d < 0 ? null : { x: org.x + dir.x * d, z: org.z + dir.z * d };
209
+ }
210
+ //TODO: Better check for hit reg of PLANES.
211
+ intersectsSegment(org, dest) {
212
+ const dir = dest.clone().subtract(org).normalize();
213
+ const d = this.distanceFromRay(org, dir);
214
+ return d > dest.distanceTo(org) || d < 0 ? null : new vec3_1.Vec3(org.x + dir.x * d, org.y + dir.y * d, org.z + dir.z * d);
215
+ }
216
+ distanceFromRay(origin, direction, xz = false) {
217
+ const ro = origin.toArray();
218
+ const rd = direction.clone().normalize().toArray();
219
+ const aabb = this.minAndMaxArrays();
220
+ const dims = ro.length; // will change later.
221
+ const dif = xz ? 2 : 1;
222
+ let lo = -Infinity;
223
+ let hi = +Infinity;
224
+ // let test = origin.clone()
225
+ for (let i = 0; i < dims; i += dif) {
226
+ let dimLo = (aabb[0][i] - ro[i]) / rd[i];
227
+ let dimHi = (aabb[1][i] - ro[i]) / rd[i];
228
+ if (dimLo > dimHi) {
229
+ let tmp = dimLo;
230
+ dimLo = dimHi;
231
+ dimHi = tmp;
232
+ }
233
+ if (dimHi < lo || dimLo > hi) {
234
+ return Infinity;
235
+ }
236
+ if (dimLo > lo)
237
+ lo = dimLo;
238
+ if (dimHi < hi)
239
+ hi = dimHi;
240
+ }
241
+ return lo > hi ? Infinity : lo;
242
+ }
243
+ intersect(aABB) {
244
+ const d = Math.max(this.minX, aABB.minX);
245
+ const d2 = Math.max(this.minY, aABB.minY);
246
+ const d3 = Math.max(this.minZ, aABB.minZ);
247
+ const d4 = Math.min(this.maxX, aABB.maxX);
248
+ const d5 = Math.min(this.maxY, aABB.maxY);
249
+ const d6 = Math.min(this.maxZ, aABB.maxZ);
250
+ return new AABB(d, d2, d3, d4, d5, d6);
251
+ }
252
+ equals(other) {
253
+ return (this.minX === other.minX &&
254
+ this.minY === other.minY &&
255
+ this.minZ === other.minZ &&
256
+ this.maxX === other.maxX &&
257
+ this.maxY === other.maxY &&
258
+ this.maxZ === other.maxZ);
259
+ }
260
+ xzDistanceToVec(pos) {
261
+ let dx = Math.max(this.minX - pos.x, 0, pos.x - this.maxX);
262
+ let dz = Math.max(this.minZ - pos.z, 0, pos.z - this.maxZ);
263
+ return Math.sqrt(dx * dx + dz * dz);
264
+ }
265
+ distanceToVec(pos) {
266
+ let dx = Math.max(this.minX - pos.x, 0, pos.x - this.maxX);
267
+ let dy = Math.max(this.minY - pos.y, 0, pos.y - this.maxY);
268
+ let dz = Math.max(this.minZ - pos.z, 0, pos.z - this.maxZ);
269
+ return Math.sqrt(dx * dx + dy * dy + dz * dz);
270
+ }
271
+ expandTowards(vec3) {
272
+ return this.expandTowardsCoords(vec3.x, vec3.y, vec3.z);
273
+ }
274
+ expandTowardsCoords(d, d2, d3) {
275
+ let d4 = this.minX;
276
+ let d5 = this.minY;
277
+ let d6 = this.minZ;
278
+ let d7 = this.maxX;
279
+ let d8 = this.maxY;
280
+ let d9 = this.maxZ;
281
+ if (d < 0.0) {
282
+ d4 += d;
283
+ }
284
+ else if (d > 0.0) {
285
+ d7 += d;
286
+ }
287
+ if (d2 < 0.0) {
288
+ d5 += d2;
289
+ }
290
+ else if (d2 > 0.0) {
291
+ d8 += d2;
292
+ }
293
+ if (d3 < 0.0) {
294
+ d6 += d3;
295
+ }
296
+ else if (d3 > 0.0) {
297
+ d9 += d3;
298
+ }
299
+ return new AABB(d4, d5, d6, d7, d8, d9);
300
+ }
301
+ moveCoords(d, d2, d3) {
302
+ return new AABB(this.minX + d, this.minY + d2, this.minZ + d3, this.maxX + d, this.maxY + d2, this.maxZ + d3);
303
+ }
304
+ move(vec3) {
305
+ return new AABB(this.minX + vec3.x, this.minY + vec3.y, this.minZ + vec3.z, this.maxX + vec3.x, this.maxY + vec3.y, this.maxZ + vec3.z);
306
+ }
307
+ intersectsCoords(d, d2, d3, d4, d5, d6) {
308
+ return this.minX < d4 && this.maxX > d && this.minY < d5 && this.maxY > d2 && this.minZ < d6 && this.maxZ > d3;
309
+ }
310
+ collides(aABB) {
311
+ return this.collidesCoords(aABB.minX, aABB.minY, aABB.minZ, aABB.maxX, aABB.maxY, aABB.maxZ);
312
+ }
313
+ collidesCoords(d, d2, d3, d4, d5, d6) {
314
+ return this.minX <= d4 && this.maxX >= d && this.minY <= d5 && this.maxY >= d2 && this.minZ <= d6 && this.maxZ >= d3;
315
+ }
316
+ contains(aABB) {
317
+ return this.containsCoords(aABB.minX, aABB.minY, aABB.minZ) && this.containsCoords(aABB.maxX, aABB.maxY, aABB.maxZ);
318
+ }
319
+ containsVec(vec) {
320
+ return this.minX <= vec.x && this.maxX >= vec.x && this.minY <= vec.y && this.maxY >= vec.y && this.minZ <= vec.z && this.maxZ >= vec.z;
321
+ }
322
+ containsCoords(x, y, z) {
323
+ return this.minX <= x && this.maxX >= x && this.minY <= y && this.maxY >= y && this.minZ <= z && this.maxZ >= z;
324
+ }
325
+ getCenter() {
326
+ return new vec3_1.Vec3(lerp(0.5, this.minX, this.maxX), lerp(0.5, this.minY, this.maxY), lerp(0.5, this.minZ, this.maxZ));
327
+ }
328
+ }
329
+ exports.AABB = AABB;
330
+ exports.default = AABB;