Code viewer for World:
astar
function translateBox(e, t) {
var n = new THREE.Vector3;
console.log("nnnnnnnnnnnnn " + e*squaresize);
return n.y = -50, n.x = e * squaresize - MAXPOS / 2, n.z = t * squaresize - MAXPOS / 2, n
}
//var t, n, o, a = [];
function showDistance(e) {
var t, n, o, a = [];
if (e) {
for (let t = 0; t < e.length - 1; t++) a.push(translateBox(e[t].x, e[t].y));
console.log("a" + a);
t = (new THREE.BufferGeometry).setFromPoints(a), o = new THREE.LineBasicMaterial({
color: 255255255
}), n = new THREE.Line(t, o), ABWorld.scene.add(n), setTimeout(() => {
t.dispose(), o.dispose(), ABWorld.scene.remove(n)
}, AB.clockTick)
}
}
// check if its wall
function isWall(e) {
return GRID[e.x][e.y] === GRID_WALL || GRID[e.x][e.y] === GRID_MAZE;
}
// calculate the heuristic (direct) distance between agent and enemy
function distance(ei, ej, ai, aj) {
return (Math.abs(ei - ai) + Math.abs(ej - aj))
}
function heuristic(e, n) {
return distance(e.x, e.y, n.x, n.y)
}
// creates the point objects
function Point(ei, ej) {
this.x = ei, this.y = ej, this.f = 0, this.g = 0, this.h = 0, this.previous = null;
var t = this;
this.toString = function() {
return "(" + t.x + ", " + t.y + ")"
}
}
// check if agent and enemy are on same block
function pointsAreEqual(e, n) {
return e.x == n.x && e.y == n.y
}
// removes the visited points
function removePointFromArray(e, n) {
for (var t = -1, o = 0; o < e.length; o++) pointsAreEqual(e[o], n) && (t = o);
t >= 0 && (e.splice(t, 1), console.log("Removed " + n.toString() + "from array"))
}
function arrayContainsPoint(e, n) {
for (var t = 0; t < e.length; t++)
if (pointsAreEqual(e[t], n)) return !0;
return !1
}
function getNeighborsOfPoint(e) {
var n = new Point(e.x, e.y + 1),
t = new Point(e.x, e.y - 1),
o = new Point(e.x - 1, e.y),
r = new Point(e.x + 1, e.y),
a = [];
return isWall(n) || a.push(n), isWall(t) || a.push(t), isWall(o) || a.push(o), isWall(r) || a.push(r), a
}
function getNextPosition(e, n) {
console.log("hahaha Enemy position: ", e);
console.log("RUN Agent position: ", n);
for (var t = [e], o = [], r = []; t.length > 0;) {
for (var a = 0, i = 0; i < t.length; i++) t[i].f < t[a].f && (a = i);
var s = t[a];
if (console.log("Current", s), console.log("Removing " + s.toString() + "from open."), removePointFromArray(t, s), pointsAreEqual(s, n)) {
console.log("Shortest path found to agent found.");
var u = s;
for (r.push(u); u.previous;) r.unshift(u.previous), u = u.previous;
console.log("Path", r);
break
}
var d = getNeighborsOfPoint(s);
console.log("Neighbors", d);
for (i = 0; i < d.length; i++) {
var l = d[i];
if (!arrayContainsPoint(o, l) && !isWall(l)) {
var c = s.g + heuristic(l, s),
A = !1;
arrayContainsPoint(t, l) ? c < l.g && (l.g = c, A = !0) : (l.g = c, A = !0, t.push(l)), A && (l.h = heuristic(l, n), l.f = l.g + l.h, l.previous = s)
}
}
console.log("Adding current to closed.");
o.push(s)
console.log("o: " + o);
}
console.log("r: " + r);
console.log("r1: " + r[1]);
console.log("r0: " + r[0]);
console.log("r: " + r);
showDistance(r);
return r.length > 1 ? r[1] : r[0]
//return r;
}