@nxg-org/mineflayer-util-plugin 1.3.2 → 1.3.7

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.
@@ -19,6 +19,7 @@ export declare class AABB {
19
19
  0: Vec3;
20
20
  1: Vec3;
21
21
  };
22
+ toVertices(): Vec3[];
22
23
  floor(): void;
23
24
  extend(dx: number, dy: number, dz: number): this;
24
25
  contract(x: number, y: number, z: number): this;
@@ -28,13 +29,19 @@ export declare class AABB {
28
29
  computeOffsetY(other: AABB, offsetY: number): number;
29
30
  computeOffsetZ(other: AABB, offsetZ: number): number;
30
31
  intersects(other: AABB): boolean;
31
- xzIntersectsRay(origin: Vec3, direction: Vec3): {
32
+ xzIntersectsRay(org: Vec3, dir: Vec3): {
32
33
  x: number;
33
34
  z: number;
34
35
  } | null;
35
- intersectsRay(origin: Vec3, direction: Vec3): Vec3 | null;
36
+ intersectsRay(org: Vec3, dir: Vec3): Vec3 | null;
37
+ xzIntersectsSegment(org: Vec3, dest: Vec3): {
38
+ x: number;
39
+ z: number;
40
+ } | null;
41
+ intersectsSegment(org: Vec3, dest: Vec3): Vec3 | null;
36
42
  distanceFromRay(origin: Vec3, direction: Vec3, xz?: boolean): number;
37
43
  equals(other: AABB): boolean;
44
+ xzDistanceTo(pos: Vec3, heightOffset?: number): number;
38
45
  distanceTo(pos: Vec3, heightOffset?: number): number;
39
46
  }
40
47
  export default AABB;
package/lib/calcs/aabb.js CHANGED
@@ -34,6 +34,18 @@ class AABB {
34
34
  toVecs() {
35
35
  return { 0: new vec3_1.Vec3(this.minX, this.minY, this.minZ), 1: new vec3_1.Vec3(this.maxX, this.maxY, this.maxZ) };
36
36
  }
37
+ toVertices() {
38
+ return [
39
+ new vec3_1.Vec3(this.minX, this.minY, this.minZ),
40
+ new vec3_1.Vec3(this.minX, this.minY, this.maxZ),
41
+ new vec3_1.Vec3(this.minX, this.maxY, this.minZ),
42
+ new vec3_1.Vec3(this.minX, this.maxY, this.maxZ),
43
+ new vec3_1.Vec3(this.maxX, this.minY, this.minZ),
44
+ new vec3_1.Vec3(this.maxX, this.minY, this.maxZ),
45
+ new vec3_1.Vec3(this.maxX, this.maxY, this.minZ),
46
+ new vec3_1.Vec3(this.maxX, this.maxY, this.maxZ),
47
+ ];
48
+ }
37
49
  floor() {
38
50
  this.minX = Math.floor(this.minX);
39
51
  this.minY = Math.floor(this.minY);
@@ -125,17 +137,29 @@ class AABB {
125
137
  this.minZ < other.maxZ &&
126
138
  this.maxZ > other.minZ);
127
139
  }
128
- xzIntersectsRay(origin, direction) {
129
- const d = this.distanceFromRay(origin, direction, true);
130
- return d === Infinity ? null : { x: origin.x + direction.x * d, z: origin.z + direction.z * d };
140
+ xzIntersectsRay(org, dir) {
141
+ const d = this.distanceFromRay(org, dir, true);
142
+ return d === Infinity ? null : { x: org.x + dir.x * d, z: org.z + dir.z * d };
143
+ }
144
+ intersectsRay(org, dir) {
145
+ const d = this.distanceFromRay(org, dir);
146
+ return d === Infinity ? null : new vec3_1.Vec3(org.x + dir.x * d, org.y + dir.y * d, org.z + dir.z * d);
131
147
  }
132
- intersectsRay(origin, direction) {
133
- const d = this.distanceFromRay(origin, direction);
134
- return d === Infinity ? null : new vec3_1.Vec3(origin.x + direction.x * d, origin.y + direction.y * d, origin.z + direction.z * d);
148
+ //TODO: Better check for hit reg of PLANES.
149
+ xzIntersectsSegment(org, dest) {
150
+ const dir = dest.clone().subtract(org).normalize();
151
+ const d = this.distanceFromRay(org, dir, true);
152
+ return d > dest.distanceTo(org) || d < 0 ? null : { x: org.x + dir.x * d, z: org.z + dir.z * d };
153
+ }
154
+ //TODO: Better check for hit reg of PLANES.
155
+ intersectsSegment(org, dest) {
156
+ const dir = dest.clone().subtract(org).normalize();
157
+ const d = this.distanceFromRay(org, dir);
158
+ 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);
135
159
  }
136
160
  distanceFromRay(origin, direction, xz = false) {
137
161
  const ro = origin.toArray();
138
- const rd = direction.normalize().toArray();
162
+ const rd = direction.clone().normalize().toArray();
139
163
  const aabb = this.toMinAndMaxArrays();
140
164
  const dims = ro.length; // will change later.
141
165
  const dif = xz ? 2 : 1;
@@ -168,6 +192,12 @@ class AABB {
168
192
  this.maxY === other.maxY &&
169
193
  this.maxZ === other.maxZ);
170
194
  }
195
+ xzDistanceTo(pos, heightOffset = 0) {
196
+ const { x, y, z } = pos.offset(0, heightOffset, 0);
197
+ let dx = Math.max(this.minX - x, 0, x - this.maxX);
198
+ let dz = Math.max(this.minZ - z, 0, z - this.maxZ);
199
+ return Math.sqrt(dx * dx + dz * dz);
200
+ }
171
201
  distanceTo(pos, heightOffset = 0) {
172
202
  const { x, y, z } = pos.offset(0, heightOffset, 0);
173
203
  let dx = Math.max(this.minX - x, 0, x - this.maxX);
@@ -0,0 +1,25 @@
1
+ import { Bot } from "mineflayer";
2
+ import { Block } from "prismarine-block";
3
+ import { Vec3 } from "vec3";
4
+ export declare class InterceptFunctions {
5
+ bot: Bot;
6
+ constructor(bot: Bot);
7
+ check(from: Vec3, to: Vec3): {
8
+ block: Block | null;
9
+ iterations: {
10
+ x: number;
11
+ y: number;
12
+ z: number;
13
+ face: number;
14
+ }[];
15
+ };
16
+ raycast(from: Vec3, direction: Vec3, range: number): {
17
+ block: Block | null;
18
+ iterations: {
19
+ x: number;
20
+ y: number;
21
+ z: number;
22
+ face: number;
23
+ }[];
24
+ };
25
+ }
@@ -0,0 +1,158 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InterceptFunctions = void 0;
4
+ const vec3_1 = require("vec3");
5
+ class InterceptFunctions {
6
+ constructor(bot) {
7
+ this.bot = bot;
8
+ }
9
+ //Old ver grabs range between the two + direction from... from to... to.
10
+ //calls raycast.
11
+ check(from, to) {
12
+ //old code:
13
+ const range = from.distanceTo(to);
14
+ const direction = to.minus(from).normalize();
15
+ if (isNaN(range))
16
+ return { block: null, iterations: [] };
17
+ return this.raycast(from, direction, range);
18
+ }
19
+ raycast(from, direction, range) {
20
+ const iterations = [];
21
+ const iter = new RaycastIterator(from, direction, range);
22
+ let pos = iter.next();
23
+ while (pos) {
24
+ iterations.push(pos);
25
+ const position = new vec3_1.Vec3(pos.x, pos.y, pos.z);
26
+ const block = this.bot.blockAt(position);
27
+ if (block) {
28
+ const intersect = iter.intersect(block.shapes, position);
29
+ if (intersect) {
30
+ return {
31
+ block,
32
+ iterations,
33
+ };
34
+ }
35
+ }
36
+ pos = iter.next();
37
+ if (range > 20 || (iterations.length >= 1000 && iterations.length % 1000 === 0)) {
38
+ // console.trace("too much");
39
+ console.log(from, direction, range, iterations, block, position, pos);
40
+ }
41
+ // console.log(range, iterations.length);
42
+ }
43
+ return {
44
+ block: null,
45
+ iterations,
46
+ };
47
+ }
48
+ }
49
+ exports.InterceptFunctions = InterceptFunctions;
50
+ var BlockFace;
51
+ (function (BlockFace) {
52
+ BlockFace[BlockFace["UNKNOWN"] = -999] = "UNKNOWN";
53
+ BlockFace[BlockFace["BOTTOM"] = 0] = "BOTTOM";
54
+ BlockFace[BlockFace["TOP"] = 1] = "TOP";
55
+ BlockFace[BlockFace["NORTH"] = 2] = "NORTH";
56
+ BlockFace[BlockFace["SOUTH"] = 3] = "SOUTH";
57
+ BlockFace[BlockFace["WEST"] = 4] = "WEST";
58
+ BlockFace[BlockFace["EAST"] = 5] = "EAST";
59
+ })(BlockFace || (BlockFace = {}));
60
+ class RaycastIterator {
61
+ constructor(pos, dir, maxDistance) {
62
+ this.block = {
63
+ x: Math.floor(pos.x),
64
+ y: Math.floor(pos.y),
65
+ z: Math.floor(pos.z),
66
+ face: BlockFace.UNKNOWN,
67
+ };
68
+ this.blockVec = new vec3_1.Vec3(Math.floor(pos.x), Math.floor(pos.y), Math.floor(pos.z));
69
+ this.pos = pos;
70
+ this.dir = dir;
71
+ this.invDirX = dir.x === 0 ? Number.MAX_VALUE : 1 / dir.x;
72
+ this.invDirY = dir.y === 0 ? Number.MAX_VALUE : 1 / dir.y;
73
+ this.invDirZ = dir.z === 0 ? Number.MAX_VALUE : 1 / dir.z;
74
+ this.stepX = Math.sign(dir.x);
75
+ this.stepY = Math.sign(dir.y);
76
+ this.stepZ = Math.sign(dir.z);
77
+ this.tDeltaX = dir.x === 0 ? Number.MAX_VALUE : Math.abs(1 / dir.x);
78
+ this.tDeltaY = dir.y === 0 ? Number.MAX_VALUE : Math.abs(1 / dir.y);
79
+ this.tDeltaZ = dir.z === 0 ? Number.MAX_VALUE : Math.abs(1 / dir.z);
80
+ this.tMaxX = dir.x === 0 ? Number.MAX_VALUE : Math.abs((this.block.x + (dir.x > 0 ? 1 : 0) - pos.x) / dir.x);
81
+ this.tMaxY = dir.y === 0 ? Number.MAX_VALUE : Math.abs((this.block.y + (dir.y > 0 ? 1 : 0) - pos.y) / dir.y);
82
+ this.tMaxZ = dir.z === 0 ? Number.MAX_VALUE : Math.abs((this.block.z + (dir.z > 0 ? 1 : 0) - pos.z) / dir.z);
83
+ this.maxDistance = maxDistance;
84
+ }
85
+ // Returns null if none of the shapes is intersected, otherwise returns intersect pos and face
86
+ // shapes are translated by offset
87
+ //[x0: number,y0: number,z0: number,x1:number,y1:number,z1:number][]
88
+ intersect(shapes, offset) {
89
+ // Shapes is an array of shapes, each in the form of: [x0, y0, z0, x1, y1, z1]
90
+ let t = Number.MAX_VALUE;
91
+ let f = BlockFace.UNKNOWN;
92
+ const p = this.pos.minus(offset);
93
+ for (const shape of shapes) {
94
+ let tmin = (shape[this.invDirX > 0 ? 0 : 3] - p.x) * this.invDirX;
95
+ let tmax = (shape[this.invDirX > 0 ? 3 : 0] - p.x) * this.invDirX;
96
+ const tymin = (shape[this.invDirY > 0 ? 1 : 4] - p.y) * this.invDirY;
97
+ const tymax = (shape[this.invDirY > 0 ? 4 : 1] - p.y) * this.invDirY;
98
+ let face = this.stepX > 0 ? BlockFace.WEST : BlockFace.EAST;
99
+ if (tmin > tymax || tymin > tmax)
100
+ continue;
101
+ if (tymin > tmin) {
102
+ tmin = tymin;
103
+ face = this.stepY > 0 ? BlockFace.BOTTOM : BlockFace.TOP;
104
+ }
105
+ if (tymax < tmax)
106
+ tmax = tymax;
107
+ const tzmin = (shape[this.invDirZ > 0 ? 2 : 5] - p.z) * this.invDirZ;
108
+ const tzmax = (shape[this.invDirZ > 0 ? 5 : 2] - p.z) * this.invDirZ;
109
+ if (tmin > tzmax || tzmin > tmax)
110
+ continue;
111
+ if (tzmin > tmin) {
112
+ tmin = tzmin;
113
+ face = this.stepZ > 0 ? BlockFace.NORTH : BlockFace.SOUTH;
114
+ }
115
+ if (tzmax < tmax)
116
+ tmax = tzmax;
117
+ if (tmin < t) {
118
+ t = tmin;
119
+ f = face;
120
+ }
121
+ }
122
+ if (t === Number.MAX_VALUE)
123
+ return null;
124
+ return { pos: this.pos.plus(this.dir.scaled(t)), face: f };
125
+ }
126
+ next() {
127
+ if (Math.min(Math.min(this.tMaxX, this.tMaxY), this.tMaxZ) > this.maxDistance) {
128
+ return null;
129
+ }
130
+ if (this.tMaxX < this.tMaxY) {
131
+ if (this.tMaxX < this.tMaxZ) {
132
+ this.block.x += this.stepX;
133
+ this.tMaxX += this.tDeltaX;
134
+ this.block.face = this.stepX > 0 ? BlockFace.WEST : BlockFace.EAST;
135
+ }
136
+ else {
137
+ this.block.z += this.stepZ;
138
+ this.tMaxZ += this.tDeltaZ;
139
+ this.block.face = this.stepZ > 0 ? BlockFace.NORTH : BlockFace.SOUTH;
140
+ }
141
+ }
142
+ else {
143
+ if (this.tMaxY < this.tMaxZ) {
144
+ this.block.y += this.stepY;
145
+ this.tMaxY += this.tDeltaY;
146
+ this.block.face = this.stepY > 0 ? BlockFace.BOTTOM : BlockFace.TOP;
147
+ }
148
+ else {
149
+ this.block.z += this.stepZ;
150
+ this.tMaxZ += this.tDeltaZ;
151
+ this.block.face = this.stepZ > 0 ? BlockFace.NORTH : BlockFace.SOUTH;
152
+ }
153
+ }
154
+ if (isNaN(this.block.x) || isNaN(this.block.y) || isNaN(this.block.z))
155
+ return null;
156
+ return this.block;
157
+ }
158
+ }
package/lib/index.d.ts CHANGED
@@ -26,3 +26,4 @@ declare module "prismarine-entity" {
26
26
  }
27
27
  export default function inject(bot: Bot): void;
28
28
  export { AABB } from "./calcs/aabb";
29
+ export { InterceptFunctions } from "./calcs/intercept";
package/lib/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AABB = void 0;
3
+ exports.InterceptFunctions = exports.AABB = void 0;
4
4
  const utilFunctions_1 = require("./utilFunctions");
5
5
  function inject(bot) {
6
6
  bot.util = new utilFunctions_1.UtilFunctions(bot);
@@ -8,3 +8,5 @@ function inject(bot) {
8
8
  exports.default = inject;
9
9
  var aabb_1 = require("./calcs/aabb");
10
10
  Object.defineProperty(exports, "AABB", { enumerable: true, get: function () { return aabb_1.AABB; } });
11
+ var intercept_1 = require("./calcs/intercept");
12
+ Object.defineProperty(exports, "InterceptFunctions", { enumerable: true, get: function () { return intercept_1.InterceptFunctions; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxg-org/mineflayer-util-plugin",
3
- "version": "1.3.2",
3
+ "version": "1.3.7",
4
4
  "description": "mineflayer utils for NextGEN mineflayer plugins.",
5
5
  "keywords": [
6
6
  "mineflayer",