Walls placement algorithm overhaul.

master
Ben Burlingham 5 years ago
parent 9b62ec7336
commit 5fbe9b29ff
  1. 7
      README.md
  2. 110
      server/ricochet.js

@ -13,6 +13,9 @@ A victory state can be stored by taking a snapshot of the current stack.
Icons from [https://game-icons.net](https://game-icons.net) Icons from [https://game-icons.net](https://game-icons.net)
## TODO ## TODO
- walls and winstate algorithm
- tutorial - tutorial
- restore name prompt1 - restore name prompt
- home button
- reset robots? on server reset...avoid multiple shadows
- Fix 9 things

@ -134,63 +134,81 @@ Ricochet.prototype.freshRobots = function() {
}; };
Ricochet.prototype.freshWalls = function() { Ricochet.prototype.freshWalls = function() {
// Edge IDs are of the form [i1-j1-i2-j2]. Top left is 0, 0. // Corner count relates quadratically to squares per side.
const numberOfCorners = Math.ceil(Math.pow((this.squaresPerSide / 4), 2));
// Leave here for testing. const corners = [];
// return [ let shortCircuitCounter = 0;
// "1-9-1-10",
// "9-1-10-1", while (corners.length < numberOfCorners && shortCircuitCounter < numberOfCorners * 5) {
// "9-19-10-19", const { i, j } = this.randomSquare();
// "19-9-19-10"
// ]; const isTooClose = corners.reduce((acc, v) => {
const delta = Math.abs(v.i - i) + Math.abs(v.j - j);
// console.log("Generating walls.");
return acc || (delta < 3);
// Squares per side has quadratic relationship with wall/corner requirements. }, false);
const numberOfCorners = Math.ceil(Math.pow((this.squaresPerSide / 10), 2));
const numberOfWalls = Math.ceil(Math.pow((this.squaresPerSide / 5), 2)); if (isTooClose === false) {
corners.push({ i, j });
const edges = []; }
}
// DO NUMBER OF CORNERS FIRST AFTER TESTING
for (let n = 0; n < numberOfWalls; n++) {
const { i: ri, j: rj } = this.randomSquare();
const isHorizontal = Math.random() < 0.5;
const isBackward = Math.random() < 0.5;
let i1, j1, i2, j2; // Edge IDs are of the form [i1-j1-i2-j2]. Top left is 0, 0.
const horizontalEdges = corners.map(({ i, j }) => {
const isLeftward = i > 0 && i < this.squaresPerSide && (Math.random() < 0.5);
const i1 = isLeftward ? i - 1 : i;
const i2 = isLeftward ? i : i + 1;
return { i1, j1: j, i2, j2: j};
});
const verticalEdges = corners.map(({ i, j }) => {
const isUpward = j > 0 && j < this.squaresPerSide && (Math.random() < 0.5);
const j1 = isUpward ? j - 1 : j;
const j2 = isUpward ? j : j + 1;
return { i1: i, j1, i2: i, j2 };
});
const edges = horizontalEdges.concat(verticalEdges);
return edges.reduce((acc, { i1, j1, i2, j2 }) => {
// Remove walls along all edges
const onLeft = (i1 === 0 && i2 === 0);
const onRight = (i1 === this.squaresPerSide && i2 === this.squaresPerSide);
const onTop = (j1 === 0 && j2 === 0);
const onBottom = (j1 === this.squaresPerSide && j2 === this.squaresPerSide);
if (onLeft || onRight || onTop || onBottom) {
return acc;
}
if (isHorizontal) { // Remove some walls randomly
i1 = isBackward ? ri - 1 : ri; if (Math.random() < 0.3) {
i2 = isBackward ? ri : ri + 1; return acc;
}
j1 = rj; return acc.concat(`${i1}-${j1}-${i2}-${j2}`);
j2 = rj; }, []);
} else { };
i1 = ri;
i2 = ri;
j1 = isBackward ? rj - 1 : rj; Ricochet.prototype.freshObjective = function() {
j2 = isBackward ? rj : rj + 1; const getRandomWall = () => {
if (!this.walls.length) {
this.randomUnoccupiedSquare();
} }
const edge = `${i1}-${j1}-${i2}-${j2}`; const wall = this.walls[Math.floor((Math.random() * this.walls.length))];
if (edges.includes(edge)) { const [i1, j1, i2, j2] = wall.split('-');
n--;
} else {
edges.push(edge);
}
}
return edges; return (Math.random() < 0.5) ? { i: i1, j: j1 } : { i: i2, j: j2 };
}; };
Ricochet.prototype.freshObjective = function() {
const rand = Math.floor(Math.random() * this.robotIds.length); const rand = Math.floor(Math.random() * this.robotIds.length);
const id = this.robotIds[rand]; const id = this.robotIds[rand];
const { i, j } = this.randomUnoccupiedSquare();
const { i, j } = getRandomWall();
return { return {
i, i,

Loading…
Cancel
Save