main github
D&D Battle sim analyzer in python
Simple pathfinding in javascript
//// getdistance() example usages:
// getdistance($initOrder[0][3,] $initOrder[1][3]);
// getdistance([0,3], [4,7]);
// getdistance($playerCoords, $initOrder[1][3]);
setup.getdistance = function(origin, target) {
var xDist = Math.abs(origin[0] - target[0]);
var yDist = Math.abs(origin[1] - target[1]);
var totalDist = Math.max(xDist, yDist);
return totalDist;
}
setup.pathfind = function(origin, target) {
if (origin == target) {return false;}
// console.log(" ~~ Inputting: " + origin + " Targeting: " + target + " ~~ ");
var map = State.variables.battleMap;
var directions = ["N", "S", "E", "W", "NW", "NE", "SW", "SE"];
// first, check if the target is already our neighbor
for (var i = 0, j = directions.length; i < j; i++) {
var nCoords = getNeighbor(directions[i], origin);
if (nCoords[0] == target[0] && nCoords[1] == target[1]) {return false;}
}
var mapH = map.length;
var mapW = map[0].length;
var targetVal = map[target[1]][target[0]];
var queue = [origin];
var visited = {};
var count = 1;
// build our count array as a mirror of the map
var countArr = [];
for (var i = 0, j = map.length; i < j; i++) {
var thisRow = [];
for (var a = 0, b = map[0].length; a < b; a++) {thisRow.push(999);}
countArr.push(thisRow);
}
countArr[origin[1]][origin[0]] = 0;
while (queue.length > 0) {
var currentPos = queue.shift();
visited[currentPos] = true;
if (currentPos.toString() == target.toString()) {break;}
// check each neighbor
for (var i = 0, j = directions.length; i < j; i++) {
var nCoords = getNeighbor(directions[i], currentPos);
var nY = nCoords[1];
var nX = nCoords[0];
// make sure we're within the map bounds
if (nX < mapW && nY < mapH && nX >= 0 && nY >= 0) {
var nVal = map[nY][nX];
} else {continue;}
if ((nVal == " " || nVal == targetVal) && !visited[nCoords]) {
countArr[nY][nX] = count;
queue.push(nCoords);
count++;
visited[nCoords] = true;
}
}
} // end of mapping
// now get the shortest path based on our map, working backwards.
// console.log(countArr);
var startVal = countArr[target[1]][target[0]];
var queue = [ [target, startVal] ]; // spaces so twine doesn't think its a passage link
var path = [];
// console.log("Starting at our target, here: " + queue[0]);
while (queue.length > 0) {
var currentPos = queue.shift();
if (currentPos[0].toString() == origin.toString()) {
break;
}
var thisVal = countArr[currentPos[0][1]][currentPos[0][0]];
var lowestVal = currentPos;
// check each neighbor
for (var i = 0, j = directions.length; i < j; i++) {
// console.log(currentPos[0] + "'s neighbor: " + directions[i] + " ...");
var nCoords = getNeighbor(directions[i], currentPos[0]);
var nY = nCoords[1];
var nX = nCoords[0];
// make sure we're within the map bounds
if (nX < mapW && nY < mapH && nX >= 0 && nY >= 0) {
var nVal = countArr[nY][nX];
if (nVal < lowestVal[1]) { //get lowest val neighbor
lowestVal = [nCoords, nVal];
}
}
}
// console.log("Out of the 8 neighbors of: " + currentPos[0] + ", the lowest was: " + lowestVal);
queue.push(lowestVal); // our next cell to check
path.push(lowestVal[0]);
}
var nextStep = path[path.length-2];
var vector0 = nextStep[0] - origin[0];
var vector1 = nextStep[1] - origin[1];
var direction = getDirection(vector0, vector1);
return direction;
}
function getDirection(vector0, vector1) {
var vectorStr = vector0 + ", " + vector1;
var dirMap = {
"0, -1" : "N",
"0, 1" : "S",
"1, 0" : "E",
"-1, 0" : "W",
"-1, -1" : "NW",
"1, -1" : "NE",
"-1, 1" : "SW",
"1, 1" : "SE"
}
return dirMap[vectorStr];
}
function getNeighbor(direction, coords) {
switch (direction) {
case "N": return [coords[0], coords[1]-1];
case "S": return [coords[0], coords[1]+1];
case "E": return [coords[0]+1, coords[1]];
case "W": return [coords[0]-1, coords[1]];
case "NW": return [coords[0]-1, coords[1]-1];
case "NE": return [coords[0]+1, coords[1]-1];
case "SW": return [coords[0]-1, coords[1]+1];
case "SE": return [coords[0]+1, coords[1]+1];
}
}
// getdistance($initOrder[0][3,] $initOrder[1][3]);
// getdistance([0,3], [4,7]);
// getdistance($playerCoords, $initOrder[1][3]);
setup.getdistance = function(origin, target) {
var xDist = Math.abs(origin[0] - target[0]);
var yDist = Math.abs(origin[1] - target[1]);
var totalDist = Math.max(xDist, yDist);
return totalDist;
}
setup.pathfind = function(origin, target) {
if (origin == target) {return false;}
// console.log(" ~~ Inputting: " + origin + " Targeting: " + target + " ~~ ");
var map = State.variables.battleMap;
var directions = ["N", "S", "E", "W", "NW", "NE", "SW", "SE"];
// first, check if the target is already our neighbor
for (var i = 0, j = directions.length; i < j; i++) {
var nCoords = getNeighbor(directions[i], origin);
if (nCoords[0] == target[0] && nCoords[1] == target[1]) {return false;}
}
var mapH = map.length;
var mapW = map[0].length;
var targetVal = map[target[1]][target[0]];
var queue = [origin];
var visited = {};
var count = 1;
// build our count array as a mirror of the map
var countArr = [];
for (var i = 0, j = map.length; i < j; i++) {
var thisRow = [];
for (var a = 0, b = map[0].length; a < b; a++) {thisRow.push(999);}
countArr.push(thisRow);
}
countArr[origin[1]][origin[0]] = 0;
while (queue.length > 0) {
var currentPos = queue.shift();
visited[currentPos] = true;
if (currentPos.toString() == target.toString()) {break;}
// check each neighbor
for (var i = 0, j = directions.length; i < j; i++) {
var nCoords = getNeighbor(directions[i], currentPos);
var nY = nCoords[1];
var nX = nCoords[0];
// make sure we're within the map bounds
if (nX < mapW && nY < mapH && nX >= 0 && nY >= 0) {
var nVal = map[nY][nX];
} else {continue;}
if ((nVal == " " || nVal == targetVal) && !visited[nCoords]) {
countArr[nY][nX] = count;
queue.push(nCoords);
count++;
visited[nCoords] = true;
}
}
} // end of mapping
// now get the shortest path based on our map, working backwards.
// console.log(countArr);
var startVal = countArr[target[1]][target[0]];
var queue = [ [target, startVal] ]; // spaces so twine doesn't think its a passage link
var path = [];
// console.log("Starting at our target, here: " + queue[0]);
while (queue.length > 0) {
var currentPos = queue.shift();
if (currentPos[0].toString() == origin.toString()) {
break;
}
var thisVal = countArr[currentPos[0][1]][currentPos[0][0]];
var lowestVal = currentPos;
// check each neighbor
for (var i = 0, j = directions.length; i < j; i++) {
// console.log(currentPos[0] + "'s neighbor: " + directions[i] + " ...");
var nCoords = getNeighbor(directions[i], currentPos[0]);
var nY = nCoords[1];
var nX = nCoords[0];
// make sure we're within the map bounds
if (nX < mapW && nY < mapH && nX >= 0 && nY >= 0) {
var nVal = countArr[nY][nX];
if (nVal < lowestVal[1]) { //get lowest val neighbor
lowestVal = [nCoords, nVal];
}
}
}
// console.log("Out of the 8 neighbors of: " + currentPos[0] + ", the lowest was: " + lowestVal);
queue.push(lowestVal); // our next cell to check
path.push(lowestVal[0]);
}
var nextStep = path[path.length-2];
var vector0 = nextStep[0] - origin[0];
var vector1 = nextStep[1] - origin[1];
var direction = getDirection(vector0, vector1);
return direction;
}
function getDirection(vector0, vector1) {
var vectorStr = vector0 + ", " + vector1;
var dirMap = {
"0, -1" : "N",
"0, 1" : "S",
"1, 0" : "E",
"-1, 0" : "W",
"-1, -1" : "NW",
"1, -1" : "NE",
"-1, 1" : "SW",
"1, 1" : "SE"
}
return dirMap[vectorStr];
}
function getNeighbor(direction, coords) {
switch (direction) {
case "N": return [coords[0], coords[1]-1];
case "S": return [coords[0], coords[1]+1];
case "E": return [coords[0]+1, coords[1]];
case "W": return [coords[0]-1, coords[1]];
case "NW": return [coords[0]-1, coords[1]-1];
case "NE": return [coords[0]+1, coords[1]-1];
case "SW": return [coords[0]-1, coords[1]+1];
case "SE": return [coords[0]+1, coords[1]+1];
}
}