-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
385 lines (350 loc) · 11.2 KB
/
index.js
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
var aStar = require('a-star')
, EventEmitter = require('events').EventEmitter
module.exports = init;
// instantiated from init
var vec3;
var MONITOR_INTERVAL = 40;
var WATER_THRESHOLD = 20;
var DEFAULT_TIMEOUT = 10 * 1000; // 10 seconds
var DEFAULT_END_RADIUS = 0.1;
// if the distance is more than this number, navigator will opt to simply
// head in the correct direction and recalculate upon getting closer
var TOO_FAR_THRESHOLD = 150;
function init(mineflayer) {
vec3 = mineflayer.vec3;
return inject;
}
function inject(bot) {
var currentCourse = [];
var cardinalDirectionVectors = [
vec3(-1, 0, 0), // north
vec3( 1, 0, 0), // south
vec3( 0, 0, -1), // east
vec3( 0, 0, 1), // west
];
bot.navigate = new EventEmitter();
bot.navigate.to = navigateTo;
bot.navigate.stop = noop;
bot.navigate.walk = walk;
bot.navigate.findPathSync = findPathSync;
bot.navigate.blocksToAvoid = {
51: true, // fire
59: true, // crops
10: true, // lava
11: true, // lava
};
function onArrived() {
bot.navigate.emit("arrived");
}
function findPathSync(end, params) {
params = params || {};
end = end.floored()
var timeout = params.timeout == null ? DEFAULT_TIMEOUT : params.timeout;
var endRadius = params.endRadius == null ? DEFAULT_END_RADIUS : params.endRadius;
var tooFarThreshold = params.tooFarThreshold == null ? TOO_FAR_THRESHOLD : params.tooFarThreshold;
var actualIsEnd = params.isEnd || createIsEndWithRadius(end, endRadius);
var heuristic = createHeuristicFn(end);
var start = bot.entity.position.floored();
var tooFar = false;
if (start.distanceTo(end) > tooFarThreshold) {
// Too far to calculate reliably. Return 'tooFar' and a path to walk
// in the general direction of end.
actualIsEnd = function(node) {
// let's just go 100 meters (and not end in water)
return node.water === 0 && start.distanceTo(node.point) >= 100;
};
tooFar = true;
}
// search
var results = aStar({
start: new Node(start, 0),
isEnd: actualIsEnd,
neighbor: getNeighbors,
distance: distanceFunc,
heuristic: heuristic,
timeout: timeout,
});
results.status = tooFar ? 'tooFar' : results.status;
results.path = results.path.map(nodeCenterOffset);
return results;
}
function walk(currentCourse, callback) {
callback = callback || noop;
var lastNodeTime = new Date().getTime();
var monitorInterval = setInterval(monitorMovement, MONITOR_INTERVAL);
bot.navigate.stop('interrupted');
bot.navigate.stop = stop;
function monitorMovement() {
var nextPoint = currentCourse[0];
var currentPosition = bot.entity.position;
if (currentPosition.distanceTo(nextPoint) <= 0.2) {
// arrived at next point
lastNodeTime = new Date().getTime();
currentCourse.shift();
if (currentCourse.length === 0) {
// done
stop('arrived');
return;
}
// not done yet
nextPoint = currentCourse[0];
}
var delta = nextPoint.minus(currentPosition);
var gottaJump = false;
var horizontalDelta = Math.abs(delta.x + delta.z);
if (delta.y > 0.1) {
// gotta jump up when we're close enough
gottaJump = horizontalDelta < 1.75;
} else if (delta.y > -0.1) {
// possibly jump over a hole
gottaJump = 1.5 < horizontalDelta && horizontalDelta < 2.5;
}
bot.setControlState('jump', gottaJump);
// run toward next point
var lookAtY = currentPosition.y + bot.entity.height;
var lookAtPoint = vec3(nextPoint.x, lookAtY, nextPoint.z);
bot.lookAt(lookAtPoint);
bot.setControlState('forward', true);
// check for futility
if (new Date().getTime() - lastNodeTime > 1500) {
// should never take this long to go to the next node
bot.navigate.emit('obstructed');
stop('obstructed');
}
}
function stop(reason) {
bot.navigate.stop = noop;
clearInterval(monitorInterval);
bot.clearControlStates();
bot.navigate.emit("stop", reason);
callback(reason);
}
}
// publicly exposed
function navigateTo(end, params) {
params = params || {};
var onArrivedCb = params.onArrived ? params.onArrived : onArrived;
bot.navigate.stop('interrupted');
var results = findPathSync(end, params);
if (results.status === 'success') {
bot.navigate.emit("pathFound", results.path);
walk(results.path, function() {
onArrivedCb();
});
} else if (results.status === 'tooFar') {
// it's too far, just walk in the general direction and restart the search
bot.navigate.emit("pathPartFound", results.path);
walk(results.path, function() {
navigateTo(end, params);
});
} else if (results.status === 'noPath' || results.status === 'timeout') {
// can't find a path
bot.navigate.emit("cannotFind", results.path);
}
}
function getNeighbors(node) {
// for each cardinal direction:
// "." is head. "+" is feet and current location.
// "#" is initial floor which is always solid. "a"-"u" are blocks to check
//
// --0123-- horizontalOffset
// |
// +2 aho
// +1 .bip
// 0 +cjq
// -1 #dkr
// -2 els
// -3 fmt
// -4 gn
// |
// dz
//
var point = node.point;
var isSafeA = isSafe(bot.blockAt(point.offset(0, 2, 0)));
var result = [];
cardinalDirectionVectors.forEach(function(directionVector) {
var blockH, blockE;
var pointB = pointAt(1, 1);
var blockB = properties(pointB);
if (!blockB.safe) {
// we can do nothing in this direction
return;
}
var pointC = pointAt(1, 0);
var blockC = properties(pointC);
if (!blockC.safe) {
// can't walk forward
if (!blockC.physical) {
// too dangerous
return;
}
if (!isSafeA) {
// can't jump
return;
}
blockH = properties(pointAt(1, 2));
if (!blockH.safe) {
// no head room to stand on c
return;
}
// can jump up onto c
result.push(pointB);
return;
}
// c is open
var pointD = pointAt(1, -1);
var blockD = properties(pointD);
if (blockD.physical) {
// can walk onto d. this is the case of flat ground.
result.push(pointC);
return;
}
if (blockD.safe) {
// safe to drop through d
var pointE = pointAt(1, -2);
blockE = properties(pointE);
if (blockE.physical) {
// can drop onto e
result.push(pointD);
} else if (blockE.safe) {
// can drop through e
var pointF = pointAt(1, -3);
var blockF = properties(pointF);
if (blockF.physical) {
// can drop onto f
result.push(pointE);
} else if (blockF.safe) {
// can drop through f
var blockG = properties(pointAt(1, -4));
if (blockG.physical) {
result.push(pointF);
}
}
}
}
// might be able to jump over the d hole.
blockH = properties(pointAt(1, 2));
var blockO = properties(pointAt(2, 2));
var canJumpForward = isSafeA && blockH.safe && blockO.safe;
var pointI = pointAt(2, 1);
var blockI = properties(pointI);
var pointJ = pointAt(2, 0);
var blockJ = properties(pointJ);
if (canJumpForward && blockI.safe && blockJ.physical) {
// can jump over and up onto j
result.push(pointI);
}
var pointK = pointAt(2, -1);
var blockK = properties(pointK);
var canJumpPastJ = canJumpForward && blockJ.safe && blockI.safe;
if (canJumpPastJ && blockK.physical) {
// can jump over onto k
result.push(pointJ);
canJumpPastJ = false;
}
// might be able to walk and drop forward
var pointL = pointAt(2, -2);
var blockL = properties(pointL);
var canLandOnL = false;
if (blockI.safe && blockJ.safe && blockK.safe && blockL.physical) {
// can walk and drop onto l
canLandOnL = true;
result.push(pointK);
}
if (blockE === undefined) blockE = properties(pointAt(1, -2));
var canLandOnM = false;
if (blockE.safe) {
// can drop through e
var pointM = pointAt(2, -3);
var blockM = properties(pointM);
if (blockJ.safe && blockK.safe && blockL.safe && blockM.physical) {
// can walk and drop onto m
canLandOnM = true;
result.push(pointL);
}
var blockN = properties(pointAt(2, -4));
if (blockK.safe && blockL.safe && blockM.safe && blockN.physical) {
// can walk and drop onto n
result.push(pointM);
}
}
if (!canJumpPastJ) return;
// 3rd column
var blockP = properties(pointAt(3, 1));
var pointQ = pointAt(3, 0);
var blockQ = properties(pointQ);
var pointR = pointAt(3, -1);
var blockR = properties(pointR);
if (blockP.safe && blockQ.safe && blockR.physical) {
// can jump way over onto r
result.push(pointQ);
return;
}
var pointS = pointAt(3, -2);
var blockS = properties(pointS);
if (!canLandOnL && blockQ.safe && blockR.safe && blockS.physical) {
// can jump way over and down onto s
result.push(pointR);
return;
}
var blockT = properties(pointAt(3, -3));
if (!canLandOnM && blockR.safe && blockS.safe && blockT.physical) {
// can jump way over and down onto t
result.push(pointS);
return;
}
function pointAt(horizontalOffset, dy) {
return point.offset(directionVector.x * horizontalOffset, dy, directionVector.z * horizontalOffset);
}
function properties(point) {
var block = bot.blockAt(point);
return block ? {
safe: isSafe(block),
physical: block.boundingBox === 'block',
} : {
safe: false,
physical: false,
};
}
}); // cardinalDirectionVectors.forEach
return result.map(function(point) {
var faceBlock = bot.blockAt(point.offset(0, 1, 0));
var water = 0;
if (faceBlock.type === 0x08 || faceBlock.type === 0x09) {
water = node.water + 1;
}
return new Node(point, water);
}).filter(function(node) {
return node.water <= WATER_THRESHOLD;
});
}
function isSafe(block) {
return block.boundingBox === 'empty' &&
!bot.navigate.blocksToAvoid[block.type];
}
}
function createIsEndWithRadius(end, radius) {
return function(node) {
return node.point.distanceTo(end) <= radius;
};
}
function distanceFunc(nodeA, nodeB) {
return nodeA.point.distanceTo(nodeB.point);
}
function nodeCenterOffset(node) {
return node.point.offset(0.5, 0, 0.5);
}
function Node(point, water) {
this.point = point;
this.water = water;
}
Node.prototype.toString = function() {
// must declare a toString so that A* works.
return this.point.toString() + ":" + this.water;
};
function createHeuristicFn(end) {
return function(node) {
return node.point.distanceTo(end) + 5 * node.water;
};
}
function noop() {}