// Michael Ryan - 59568611
/*
Approach:
Taking the previous 2D A* code and the CodeTrain videos, I implemented A* for
the enemy in the MoveLogicalEnemy method.
To make it a little more interesting, I decided to see if I could compare the original algorithm with our new A*
implementation.
This required a large refactor of the code, mainly removing all dependencies on global variables, and to rather depend
on variables passed around as parameters.
You can immediately tell that A* performs better at finding the Agent, however one problem it had was finishing the game.
As A* only brings us to our destination, it can end up keeping the Agent pinned against a position in a stalemate.
To help with this, if we have reached our destination (Agent next to us), I default to the original algorithm to try and lock
the Agent into a corner - or at least give the Agent a chance to leave and find a potentially better spot to get caught in.
To view the improvement, I've updated the score message to show the performance of each of the algorithms.
As before, a higher % means a worse enemy.
Spent a couple of days trying to figure out an annoying bug where the enemy would sometimes "jump" to a seemingly random location.
Turns out - when I was setting the original random locations for characters, the grid reference was being passed around rather than
its coordinates. Long story short, I had to re-write a lot of methods to make sure I was using values rather than references...
Result:
After several runs, it's very clear that in terms of pathfinding, the A* algorithm performs far better than the simple follow algorithm.
Incorporating the simple algorithm as a "finishing" move greatly improves the A* algorithm at catching the Agent.
Bugs:
So far one of the only remaining bugs I've noticed is that the simple agent can sometimes get "stuck" in a position.
This seems to hit infrequently - presumably it's only trying to move to a position that is blocking its path.
For the vast majority of cases, it doesn't impact the results of our runs so I didn't consider it a high
priority to fix.
*/
// ==== Starter World ===============================================================================================
// (c) Ancient Brain Ltd. All rights reserved.
// This code is only for use on the Ancient Brain site.
// This code may be freely copied and edited by anyone on the Ancient Brain site.
// This code may not be copied, re-published or used on any other website.
// To include a run of this code on another website, see the "Embed code" links provided on the Ancient Brain site.
// ==================================================================================================================
// =============================================================================================
// More complex starter World
// 3d-effect Maze World (really a 2-D problem)
// Movement is on a semi-visible grid of squares
//
// This more complex World shows:
// - Skybox
// - Internal maze (randomly drawn each time)
// - Enemy actively chases agent
// - Music/audio
// - 2D world (clone this and set show3d = false)
// - User keyboard control (clone this and comment out Mind actions to see)
// =============================================================================================
// =============================================================================================
// Scoring:
// Bad steps = steps where enemy is within one step of agent.
// Good steps = steps where enemy is further away.
// Score = good steps as percentage of all steps.
//
// There are situations where agent is trapped and cannot move.
// If this happens, you score zero.
// =============================================================================================
// ===================================================================================================================
// === Start of tweaker's box ========================================================================================
// ===================================================================================================================
// The easiest things to modify are in this box.
// You should be able to change things in this box without being a JavaScript programmer.
// Go ahead and change some of these. What's the worst that could happen?
AB.clockTick = 100;
// Speed of run: Step every n milliseconds. Default 100.
AB.maxSteps = 1000;
// Length of run: Maximum length of run in steps. Default 1000.
AB.screenshotStep = 50;
// Take screenshot on this step. (All resources should have finished loading.) Default 50.
//---- global constants: -------------------------------------------------------
const show3d = false; // Switch between 3d and 2d view (both using Three.js)
const TEXTURE_WALL = '/uploads/starter/door.jpg';
const TEXTURE_MAZE = '/uploads/starter/latin.jpg';
const TEXTURE_AGENT = '/uploads/starter/pacman.jpg';
// Taken from the game Undertale, which I've never played but whose OST I enjoy
// https://en.wikipedia.org/wiki/Undertale
const TEXTURE_ENEMY = 'uploads/michaelryan/sans_undertale.jpg';
const MUSIC_BACK = 'uploads/michaelryan/UndertaleMegolovania.mp3' ;
// credits:
// http://commons.wikimedia.org/wiki/File:Old_door_handles.jpg
// https://commons.wikimedia.org/wiki/Category:Pac-Man_icons
// https://commons.wikimedia.org/wiki/Category:Skull_and_crossbone_icons
// http://en.wikipedia.org/wiki/File:Inscription_displaying_apices_(from_the_shrine_of_the_Augustales_at_Herculaneum).jpg
const gridSize = 20; // number of squares along side of world
const NOBOXES = Math.trunc((gridSize * gridSize) / 10); // density of maze - number of internal boxes
const squaresize = 100; // size of square in pixels
const MAXPOS = gridSize * squaresize; // length of one side in pixels
const SKYCOLOR = 0xddffdd; // a number, not a string
const startRadiusConst = MAXPOS * 1.3; // distance from centre to start the camera at
const maxRadiusConst = MAXPOS * 10; // maximum distance from camera we will render things
//--- change ABWorld defaults: -------------------------------
ABHandler.MAXCAMERAPOS = maxRadiusConst;
ABHandler.GROUNDZERO = true; // "ground" exists at altitude zero
//--- skybox: -------------------------------
// skybox is a collection of 6 files
// x,y,z positive and negative faces have to be in certain order in the array
// https://threejs.org/docs/#api/en/loaders/CubeTextureLoader
// mountain skybox, credit:
// http://stemkoski.github.io/Three.js/Skybox.html
const SKYBOX_ARRAY = [
"/uploads/starter/dawnmountain-xpos.png",
"/uploads/starter/dawnmountain-xneg.png",
"/uploads/starter/dawnmountain-ypos.png",
"/uploads/starter/dawnmountain-yneg.png",
"/uploads/starter/dawnmountain-zpos.png",
"/uploads/starter/dawnmountain-zneg.png"
];
// space skybox, credit:
// http://en.spaceengine.org/forum/21-514-1
// x,y,z labelled differently
// ===================================================================================================================
// === End of tweaker's box ==========================================================================================
// ===================================================================================================================
// You will need to be some sort of JavaScript programmer to change things below the tweaker's box.
//--- Mind can pick one of these actions -----------------
const ACTION_LEFT = 0;
const ACTION_RIGHT = 1;
const ACTION_UP = 2;
const ACTION_DOWN = 3;
const ACTION_STAYSTILL = 4;
// contents of a grid square
const GRID_BLANK = 0;
const GRID_WALL = 1;
const GRID_MAZE = 2;
var BOXHEIGHT; // 3d or 2d box height
var wall_texture, agent_texture, enemy_texture, maze_texture;
// Need to keep a global collection of these as we lose the reference in takeAction()
var mazes = [];
function loadResources() // asynchronous file loads - call initScene() when all finished
{
var loader1 = new THREE.TextureLoader();
var loader2 = new THREE.TextureLoader();
var loader3 = new THREE.TextureLoader();
var loader4 = new THREE.TextureLoader();
loader1.load(TEXTURE_WALL, function(thetexture)
{
thetexture.minFilter = THREE.LinearFilter;
wall_texture = thetexture;
if (asynchFinished()) initScene(); // if all file loads have returned
});
loader2.load(TEXTURE_AGENT, function(thetexture)
{
thetexture.minFilter = THREE.LinearFilter;
agent_texture = thetexture;
if (asynchFinished()) initScene();
});
loader3.load(TEXTURE_ENEMY, function(thetexture)
{
thetexture.minFilter = THREE.LinearFilter;
enemy_texture = thetexture;
if (asynchFinished()) initScene();
});
loader4.load(TEXTURE_MAZE, function(thetexture)
{
thetexture.minFilter = THREE.LinearFilter;
maze_texture = thetexture;
if (asynchFinished()) initScene();
});
}
function asynchFinished() // all file loads returned
{
if (wall_texture && agent_texture && enemy_texture && maze_texture) return true;
else return false;
}
//--- grid system -------------------------------------------------------------------------------
// my numbering is 0 to gridsize-1
function occupied(maze, i, j) // is this square occupied
{
if ((maze.enemyPosition.i == i) && (maze.enemyPosition.j == j)) return true; // variable objects
if ((maze.agentPosition.i == i) && (maze.agentPosition.j == j)) return true;
if (maze.grid[i][j].type == GRID_WALL) return true;
if (maze.grid[i][j].type == GRID_MAZE) return true;
return false;
}
// translate my (i,j) grid coordinates to three.js (x,y,z) coordinates
// logically, coordinates are: y=0, x and z all positive (no negative)
// logically my dimensions are all positive 0 to MAXPOS
// to centre everything on origin, subtract (MAXPOS/2) from all dimensions
function translate(i, j)
{
var v = new THREE.Vector3();
v.y = 0;
v.x = (i * squaresize) - (MAXPOS / 2);
v.z = (j * squaresize) - (MAXPOS / 2);
return v;
}
// Given a grid and gridsize, set the outside of the grid to be walls
function createWalls(grid, gridSize)
{
// set up walls
for (i = 0; i < gridSize; i++)
for (j = 0; j < gridSize; j++)
if ((i === 0) || (i === gridSize - 1) || (j === 0) || (j === gridSize - 1))
{
grid[i][j] = new Spot(i, j, GRID_WALL);
}
else
{
grid[i][j] = new Spot(i, j, GRID_BLANK);
}
return grid;
}
// Given a grid and gridSize, create the internal maze
function createMaze(grid, gridSize)
{
// set up maze
for (var c = 1; c <= NOBOXES; c++)
{
i = AB.randomIntAtoB(1, gridSize - 2); // inner squares are 1 to gridsize-2
j = AB.randomIntAtoB(1, gridSize - 2);
grid[i][j] = new Spot(i, j, GRID_MAZE);
}
return grid;
}
// Set all of a grids neighboring squares
function addGridNeighbors(grid, gridSize)
{
// Maze complete, add neighbors
for (var i = 0; i < gridSize; i++)
{
for (var j = 0; j < gridSize; j++)
{
grid[i][j].addNeighbors(grid);
}
}
}
// Function to create a grid + maze
function createGrid(gridSize)
{
// Create Grid
var grid = new Array(gridSize);
// set up GRID as 2D array
for (i = 0; i < gridSize; i++)
grid[i] = new Array(gridSize);
// Create Walls
grid = createWalls(grid, gridSize);
// Create Maze
grid = createMaze(grid, gridSize);
// Add cell neighbors
addGridNeighbors(grid, gridSize);
return grid;
}
// Find a free square without walls or characters
function getRandomFreeLocation(maze)
{
do {
i = AB.randomIntAtoB(1, maze.gridSize - 2);
j = AB.randomIntAtoB(1, maze.gridSize - 2);
}
while (occupied(maze, i, j)); // search for empty square
// Returning this reference caused me a headache that took too long to find...
return maze.grid[i][j];
}
// Create a shape object
function newShape(texture)
{
shape = new THREE.BoxGeometry(squaresize, BOXHEIGHT, squaresize);
mesh = new THREE.Mesh(shape);
mesh.material = new THREE.MeshBasicMaterial(
{
map: texture
});
return mesh;
}
// Define a Maze object with a size, an offset (where to draw it) and if it should use our heuristic
function Maze(gridSize, offset, useHeuristic)
{
// Cleaned up Grid creation a bit
var gridArray = new Array(gridSize);
this.grid = createGrid(gridSize);
this.gridSize = gridSize;
this.offset = offset;
this.useHeuristic = useHeuristic;
// Set up characters
this.enemyPosition = {
i: -1,
j: -1
};
this.agentPosition = {
i: -1,
j: -1
};
this.score = 0;
this.goodSteps = 0;
this.badSteps = 0;
addCharacters(this);
}
// Add an enemy and agent to the given maze
function addCharacters(maze)
{
// Find a free location for the characters
var enemyLocation = getRandomFreeLocation(maze);
// Get value of square rather than reference -_-
maze.enemyPosition.i = enemyLocation.i;
maze.enemyPosition.j = enemyLocation.j;
var agentLocation = getRandomFreeLocation(maze);
maze.agentPosition.i = agentLocation.i;
maze.agentPosition.j = agentLocation.j;
// Draw Characters
maze.enemyShape = newShape(enemy_texture);
ABWorld.scene.add(maze.enemyShape);
draw(maze.enemyPosition, maze.enemyShape, maze.offset);
maze.agentShape = newShape(agent_texture);
ABWorld.scene.add(maze.agentShape);
draw(maze.agentPosition, maze.agentShape, maze.offset);
}
// Draw a given maze on the scene
function drawMaze(maze)
{
for (var i = 0; i < maze.gridSize; i++)
{
for (var j = 0; j < maze.gridSize; j++)
{
if (maze.grid[i][j].type != GRID_BLANK)
{
shape = new THREE.BoxGeometry(squaresize, BOXHEIGHT, squaresize);
thecube = new THREE.Mesh(shape);
thecube.material = new THREE.MeshBasicMaterial(
{
map: maze_texture
});
thecube.position.copy(translate(i + maze.offset, j));
ABWorld.scene.add(thecube);
}
}
}
}
/*Create two mazes with a shared grid structure, run one with A*, one without*/
function initScene()
{
// Play sound at lower volume
var listener = new THREE.AudioListener();
var sound = new THREE.Audio( listener );
var audioLoader = new THREE.AudioLoader();
audioLoader.load( 'uploads/michaelryan/UndertaleMegolovania.mp3', function( buffer ) {
sound.setBuffer( buffer );
sound.setLoop( true );
sound.setVolume( 0.25 );
sound.play();
});
// Get grid for mazes to use
var normalMaze = new Maze(gridSize, -10, false);
var aStarMaze = new Maze(gridSize, 10, true);
drawMaze(normalMaze);
drawMaze(aStarMaze);
// Add to our global list of grids
this.mazes.push(normalMaze);
this.mazes.push(aStarMaze);
ABWorld.scene.background = new THREE.Color(0x000000);
ABWorld.render();
AB.removeLoading();
AB.runReady = true; // start the run loop
}
// --- draw moving objects -----------------------------------
// Simplified draw method based on supplied parameters
function draw(position, shape, offset)
{
shape.position.copy(translate(position.i + offset, position.j)); // translate my (i,j) grid coordinates to three.js (x,y,z) coordinates
ABWorld.lookat.copy(shape.position); // if camera moving, look back at where the enemy is
}
// --- take actions -----------------------------------
/* Introduce our A* changes */
function heuristic(a, b)
{
// Init our Vector object used by threejs
var positionA = new THREE.Vector3(a.i, 0, a.j);
var positionB = new THREE.Vector3(b.i, 0, b.j);
return positionA.distanceTo(positionB);
}
// Daniel Shiffman
// Nature of Code: Intelligence and Learning
// https://github.com/shiffman/NOC-S17-2-Intelligence-Learning
// Part 1: https://youtu.be/aKYlikFAV4k
// Part 2: https://youtu.be/EaZxUCWAjb0
// Part 3: https://youtu.be/jwRT4PCT6RU
// An object to describe a spot in the grid
function Spot(i, j, type)
{
// Location
this.i = i;
this.j = j;
// f, g, and h values for A*
this.f = 0;
this.g = 0;
this.h = 0;
// Neighbors
this.neighbors = [];
// Spot Type
this.type = type;
// Figure out who my neighbors are
this.addNeighbors = function(grid)
{
var i = this.i;
var j = this.j;
if (i < gridSize - 1) this.neighbors.push(grid[i + 1][j]);
if (i > 0) this.neighbors.push(grid[i - 1][j]);
if (j < gridSize - 1) this.neighbors.push(grid[i][j + 1]);
if (j > 0) this.neighbors.push(grid[i][j - 1]);
if (i > 0 && j > 0) this.neighbors.push(grid[i - 1][j - 1]);
if (i < gridSize - 1 && j > 0) this.neighbors.push(grid[i + 1][j - 1]);
if (i > 0 && j < gridSize - 1) this.neighbors.push(grid[i - 1][j + 1]);
if (i < gridSize - 1 && j < gridSize - 1) this.neighbors.push(grid[i + 1][j + 1]);
};
}
// Remove an element from array
function removeFromArray(arr, elt)
{
// Could use indexOf here instead to be more efficient
for (var i = arr.length - 1; i >= 0; i--)
if (arr[i] == elt)
arr.splice(i, 1);
}
// Reset the heuristic score for the given grid
function resetHeuristicScores(maze)
{
// Maze complete, add neighbors
for (var i = 0; i < maze.gridSize; i++)
{
for (var j = 0; j < maze.gridSize; j++)
{
maze.grid[i][j].f = 0;
maze.grid[i][j].g = 0;
maze.grid[i][j].h = 0;
}
}
}
// Given end and start node, follow end node back to startnode + 1
function findNodeAfterStart(end, start)
{
var temp = end;
while (temp.previous)
{
if (temp.previous === start)
{
break;
}
temp = temp.previous;
}
return temp;
}
// Check our current node against a neighbor
function checkNodeNeighborValues(currentNode, neighborNode, destinationNode, openSet, closedSet)
{
if (!closedSet.includes(neighborNode) && neighborNode.type == GRID_BLANK)
{
var tempG = currentNode.g + heuristic(neighborNode, currentNode);
// Is this a better path than before?
var newPath = false;
if (openSet.includes(neighborNode))
{
if (tempG < neighborNode.g)
{
neighborNode.g = tempG;
newPath = true;
}
}
else
{
neighborNode.g = tempG;
newPath = true;
openSet.push(neighborNode);
}
// Yes, it's a better path
if (newPath)
{
currentNode.h = heuristic(neighborNode, destinationNode);
neighborNode.f = neighborNode.g + neighborNode.h;
neighborNode.previous = currentNode;
}
}
return neighborNode;
}
// The main A* algorithm implementation
function findNextSquare(start, destination)
{
// Set up A* sets
var openSet = [];
var closedSet = [];
openSet.push(start);
var current;
while (openSet.length > 0)
{
// Best next option
var winner = 0;
// Sort OpenSet by f
openSet.sort(function(a, b)
{
return a.f - b.f;
});
current = openSet[0];
removeFromArray(openSet, current);
closedSet.push(current);
var neighbors = current.neighbors;
//--- start of for loop -----------
for (i = 0; i < neighbors.length; i++)
{
var neighbor = checkNodeNeighborValues(current, neighbors[i], destination, openSet, closedSet);
// Check if this is our goal
if (neighbor === destination)
{
return findNodeAfterStart(neighbor, start);
}
}
}
// We couldn't find a path, skip
console.log("No path found...");
return start;
}
// The original algorithm for moving the enemy
function simpleEnemyMove(maze)
{
var i, j;
ei = maze.enemyPosition.i;
ej = maze.enemyPosition.j;
ai = maze.agentPosition.i;
aj = maze.agentPosition.j;
if (ei < ai) i = AB.randomIntAtoB(ei, ei + 1);
if (ei == ai) i = ei;
if (ei > ai) i = AB.randomIntAtoB(ei - 1, ei);
if (ej < aj) j = AB.randomIntAtoB(ej, ej + 1);
if (ej == aj) j = ej;
if (ej > aj) j = AB.randomIntAtoB(ej - 1, ej);
if (!occupied(maze, i, j))
{
// Bit yuck but trying to find reference bug was torture
return new Spot(i, j, GRID_BLANK);
}
// Skip if occupied
return maze.enemyPosition;
}
// Here is where our enemy takes their move
// Choose move based on maze algorithm type (A* or simple)
function moveLogicalEnemy(maze)
{
// Get Enemy and Agent locations
var agentSpot = maze.grid[maze.agentPosition.i][maze.agentPosition.j];
var enemySpot = maze.grid[maze.enemyPosition.i][maze.enemyPosition.j];
if (maze.useHeuristic === true)
{
// Reset Grid Heuristic score
resetHeuristicScores(maze);
// Find square for us to move to
var heuristicNext = findNextSquare(enemySpot, agentSpot);
//Can we try catch him?
if (heuristicNext == agentSpot)
{
heuristicNext = simpleEnemyMove(maze);
}
maze.enemyPosition.i = heuristicNext.i;
maze.enemyPosition.j = heuristicNext.j;
}
else
{
var simpleNextSquare = simpleEnemyMove(maze);
maze.enemyPosition.i = simpleNextSquare.i;
maze.enemyPosition.j = simpleNextSquare.j;
}
}
// Move the Agent
function moveLogicalAgent(maze, action)
{
var i = maze.agentPosition.i;
var j = maze.agentPosition.j;
if (action == ACTION_LEFT) i--;
else if (action == ACTION_RIGHT) i++;
else if (action == ACTION_UP) j++;
else if (action == ACTION_DOWN) j--;
if (!occupied(maze, i, j))
{
maze.agentPosition.i = i;
maze.agentPosition.j = j;
}
}
// --- score: -----------------------------------
function badstep(agentPosition, enemyPosition) // is the enemy within one square of the agent
{
if ((Math.abs(enemyPosition.i - agentPosition.i) < 2) && (Math.abs(enemyPosition.j - agentPosition.j) < 2)) return true;
else return false;
}
// Check if the given point is surrounded
function blocked(maze, i, j) // agent is blocked on all sides, run over
{
return (occupied(maze, i - 1, j) &&
occupied(maze, i + 1, j) &&
occupied(maze, i, j + 1) &&
occupied(maze, i, j - 1));
}
// Display the scores of the two algorithms
function updateStatusAfter()
{
// new state after both have moved
var scoreMsg = "";
for (var i = 0; i < mazes.length; i++)
{
scoreMsg +=
"Maze: " + i +
"<br>Using Heuristic: " + mazes[i].useHeuristic.toString() +
"<br>Score: " + mazes[i].score +
"<br>";
}
AB.msg(scoreMsg);
}
AB.world.newRun = function()
{
AB.loadingScreen();
AB.runReady = false;
if (show3d)
{
BOXHEIGHT = squaresize;
ABWorld.init3d(startRadiusConst, maxRadiusConst, SKYCOLOR);
}
else
{
BOXHEIGHT = 1;
ABWorld.init2d(startRadiusConst, maxRadiusConst, SKYCOLOR);
}
loadResources(); // aynch file loads
// calls initScene() when it returns
};
// Pass our list of mazes to the mind
AB.world.getState = function()
{
return (mazes);
};
// Take a move here - could potentially move enemy mind
AB.world.takeAction = function(actions)
{
// Take an action for each of our mazes
for (var i = 0; i < mazes.length; i++)
{
moveLogicalAgent(mazes[i], actions[i]);
draw(mazes[i].agentPosition, mazes[i].agentShape, mazes[i].offset);
if ((AB.step % 2) === 0) // slow the enemy down to every nth step
{
moveLogicalEnemy(mazes[i]);
draw(mazes[i].enemyPosition, mazes[i].enemyShape, mazes[i].offset);
}
if (badstep(mazes[i].agentPosition, mazes[i].enemyPosition))
{
mazes[i].badSteps++;
}
else
{
mazes[i].goodSteps++;
}
mazes[i].score = (mazes[i].goodSteps / (mazes[i].goodSteps + mazes[i].badSteps) * 100).toFixed(2);
updateStatusAfter();
if (blocked(mazes[i], mazes[i].agentPosition.i, mazes[i].agentPosition.j)) // if agent blocked in, run over
{
AB.abortRun = true;
}
}
};
AB.world.endRun = function()
{
// musicPause();
if (AB.abortRun) AB.msg(" <br> <font color=red> <B> Agent trapped. Final score zero. </B> </font> ", 3);
else AB.msg(" <br> <font color=green> <B> Run over. </B> </font> ", 3);
};