@nxg-org/mineflayer-util-plugin 1.3.3 → 1.3.8
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.d.ts +1 -0
- package/lib/calcs/aabb.js +12 -0
- package/lib/calcs/intercept.d.ts +20 -0
- package/lib/calcs/intercept.js +158 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +3 -1
- package/package.json +1 -1
package/lib/calcs/aabb.d.ts
CHANGED
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);
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Bot } from "mineflayer";
|
|
2
|
+
import { Block } from "prismarine-block";
|
|
3
|
+
import { Vec3 } from "vec3";
|
|
4
|
+
declare type Iteration = {
|
|
5
|
+
x: number;
|
|
6
|
+
y: number;
|
|
7
|
+
z: number;
|
|
8
|
+
face: number;
|
|
9
|
+
};
|
|
10
|
+
declare type BlockAndIterations = {
|
|
11
|
+
block: Block | null;
|
|
12
|
+
iterations: Iteration[];
|
|
13
|
+
};
|
|
14
|
+
export declare class InterceptFunctions {
|
|
15
|
+
bot: Bot;
|
|
16
|
+
constructor(bot: Bot);
|
|
17
|
+
check(from: Vec3, to: Vec3): BlockAndIterations;
|
|
18
|
+
raycast(from: Vec3, direction: Vec3, range: number): BlockAndIterations;
|
|
19
|
+
}
|
|
20
|
+
export {};
|
|
@@ -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
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; } });
|