-
Notifications
You must be signed in to change notification settings - Fork 4
/
pathfinder.js
144 lines (122 loc) · 3.77 KB
/
pathfinder.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
/*
This needs a lot of work.
I'm not using mineflayer-pathfinder for "reasons"
*/
const vec3 = require('vec3');
function neighbours(bot, p) {
let points = [
p.offset( 0, 0,-1),
p.offset( 0, 0, 1),
p.offset(-1, 0, 0),
p.offset( 1, 0, 0),
];
for (i in points) {
if (!clean(bot, points[i])) {
points[i] = points[i].offset(0, 1, 0);
} else if (clean(bot, points[i], 0,-1, 0)) {
points[i] = points[i].offset(0,-1, 0);
}
}
return points.filter((n)=>{
let block = bot.blockAt(n.offset(0, -1, 0));
let height = block? block.shapes[0][4] : 1;
return height <= 1 && !clean(bot, n, 0,-1, 0) && clean(bot, n, 0, 0, 0) && clean(bot, n, 0, 1, 0);
});
}
function clean(bot, n, x=0, y=0, z=0) {
let b = bot.blockAt(n.offset(x, y, z));
return b && (b.displayName.includes('Air') || b.material == 'plant');
}
function gridWalk(bot, goal) {
let botPos = bot.entity.position;
goal = goal.offset(
-bot.entity.position.x,
-bot.entity.position.y,
-bot.entity.position.z
);
if (goal && botPos.distanceTo(botPos.offset(goal.x, goal.y, goal.z)) >= 0.1) {
bot.lookAt(botPos.offset(goal.x, 1.8, goal.z), true);
bot.setControlState('forward', true);
bot.setControlState('sprint', true);
bot.setControlState('jump', goal.y > 0);
} else {
bot.setControlState('forward', false);
bot.setControlState('sprint', false);
bot.setControlState('jump', false);
}
}
function pathfind(bot, start, end, range=1, maxLoops=100) {
let openList = [];
let closedList = [];
let initDist = start.distanceTo(end);
let loops = 0;
start = vec3(
Math.floor(start.x)+0.5,
Math.floor(start.y),
Math.floor(start.z)+0.5
);
openList.push({
position: start,
g: 0,
h: initDist,
f: 0,
});
while (openList.length && loops < maxLoops) {
loops++;
let point = openList.reduce((p, c)=>{
return p.f < c.f ? p : c;
});
openList.splice(openList.indexOf(point), 1);
closedList.push(point);
if (point.position.distanceTo(end) < range) {
let path = [];
while (point.root) {
path.push(point);
point = point.root;
}
return path;
}
for (neighbour of neighbours(bot, point.position)) {
let onClosedList = closedList.find((obj)=>{
return obj.position.distanceTo(neighbour) < 0.1;
});
if (!onClosedList) {
let g = point.g+1;
let h = neighbour.distanceTo(end);
let f = g+h;
let babyPoint = {
position: neighbour,
g: g,
h: h,
f: f,
root: point,
};
let previous = openList.find((obj)=>{
return obj.position.distanceTo(neighbour) < 0.1;
});
if (previous) {
if (g < previous.g) {
openList.splice(openList.indexOf(previous), 1);
openList.push(babyPoint);
}
} else {
openList.push(babyPoint);
}
}
}
}
if (openList.length) {
let point = openList.reduce((p, c)=>{
return p.f < c.f ? p : c;
});
let path = [];
while (point.root) {
path.push(point);
point = point.root;
}
return path;
}
return [];
}
exports.path = pathfind;
exports.walk = gridWalk;