-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
92 lines (76 loc) · 2.61 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
const Heuristics = require("./src/heuristics")
const Goals = require("./src/goals")
module.exports.plugin = function inject(bot) {
bot.movement = new Plugin(bot)
bot.movement.heuristic = new Heuristics(bot)
bot.movement.goals = new Goals(bot)
}
function Goal(heuristicMap) {
for (let label in heuristicMap) {
this[label] = heuristicMap[label]
}
}
function Plugin(bot) {
this.setGoal = setGoal
this.getYaw = getYaw
this.steer = steer
this.Goal = Goal
function steer(yaw, _force) {
const force = _force || true
return bot.look(yaw, bot.entity.pitch, force)
}
function setGoal(goal) {
this.heuristic.setGoal(goal)
}
function getYaw(_fov, _rotations, _blend) {
const fov = _fov || 240
const rotations = _rotations || 15
const blend = _blend || 1
let costs = new Float64Array(rotations)
// calculate the angular components
const total = fov * Math.PI / 180
const increment = total / rotations
const base = bot.entity.yaw + increment / 2 - total / 2
// add the total cost for each rotation
for (let i = 0; i < rotations; i++) {
for (let key in this.heuristic.active) {
costs[i] += this.heuristic.active[key].cost(base + increment * i)
}
}
// blend the adjacent costs together
{
const average = new Float64Array(rotations)
const total = blend * 2 + 1
const half = blend + 1
const max = rotations - blend - 1
for (let i = 0; i <= max; i++) {
for (let j = 1; j <= blend; j++)
average[i] += costs[i + j]
}
for (let i = blend; i < rotations; i++) {
for (let j = 1; j <= blend; j++)
average[i] += costs[i - j]
}
for (let i = 0; i < rotations; i++) {
average[i] += costs[i]
// use the correct mean total
if (blend <= i && i <= max) {
costs[i] = average[i] / total
} else {
costs[i] = average[i] / half
}
}
}
// get the cheapest cost
{
let cheapest = null
// find the rotation with the cheapest cost
for (let i = 0; i < rotations; i++) {
if (cheapest === null || costs[i] < costs[cheapest]) {
cheapest = i
}
}
return base + increment * cheapest
}
}
}