Skip to content

Commit

Permalink
Whirlpools - interaction with other pieces / tiles
Browse files Browse the repository at this point in the history
movement.js
* Adds restriction to prevent player moving ship into a whirlpool.
* Adds restriction to prevent piece movement path passing through a whirlpool.

contracts.js
* Adds restriction to prevent trade route path from passing through an existing whirlpool.

whirlpool.js
* Adds restriction to prevent whirlpool being created in team harbour or pirate harbour.
* Adds restriction to prevent whirlpool being created next to land.

board.js
* Add helper function (checkNextTo) to allow check on whether a tile is next to land (or any terrain type).
  • Loading branch information
miniature-tiger committed Jan 14, 2019
1 parent b3b2ef2 commit 96480c7
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 47 deletions.
24 changes: 21 additions & 3 deletions board.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Board.prototype.setupBoardArray = function() {
for (let y = 0; y < this.cols; y+=1) {
let rowArray = [];
for (let x = 0; x < this.rows; x+=1) {
rowArray.push({tile: {terrain: 'sea', subTerrain: 'none', subTerrainTeam: 'none', activeStatus: 'inactive'}, piece: {populatedSquare: false}});
rowArray.push({tile: {terrain: 'sea', subTerrain: 'none', subTerrainTeam: 'none', activeStatus: 'inactive'}, piece: {populatedSquare: false, category: 'none'}});
}
this.boardArray.push(rowArray);
}
Expand Down Expand Up @@ -256,8 +256,8 @@ Board.prototype.movePiece = function(coordStart, coordEnd) {

// Method to remove piece from boardArray
// -------------------------------------------
Board.prototype.removePiece = function(coordStart) {
this.boardArray[coordStart[0]][coordStart[1]].piece = {populatedSquare: false};
Board.prototype.removePiece = function(coords) {
this.boardArray[coords[0]][coords[1]].piece = {populatedSquare: false, category: 'none'};
}

// Method to reset pieces from 'used' to 'unused' once a turn has ended
Expand All @@ -273,6 +273,24 @@ Board.prototype.usedPiecesReset = function() {
}
}

// Method to check whether a tile is next to a terrain type
// ------------------------------------------------------------------------------
Board.prototype.checkNextTo = function(coords, terrain) {
let result = false;
for (let i=-1; i<=1; i+=1) {
if ((coords[0] + i >= 0) && (coords[0] + i < this.rows)) {
for (let j=-1; j<=1; j+=1) {
if ((coords[1] + j >= 0) && (coords[1] + j < this.cols)) {
if (this.boardArray[coords[0] + i][coords[1] + j].tile.terrain === terrain) {
result = true;
}
}
}
}
}
return result;
}

// Method to get array of tiles in findPath which are within defined distance range
// --------------------------------------------------------------------------------
Board.prototype.useTelescope = function(startMove, item, searchRange) {
Expand Down
21 changes: 10 additions & 11 deletions contracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,23 +310,22 @@ let tradeContracts = {

let k = 0;
let found = false;
while (found == false && k < localMaxMove) {
while (found === false && k < localMaxMove) {

// Loops through i rows and j columns to form the 3x3 etc grids
for (var i = -k; i < k+1; i++) {
for (let i = -k; i < k+1; i+=1) {
// Restrict by map size for rows so not searching off edge of board
if(harbour.harbourStartRow+i>=0 && harbour.harbourStartRow+i < game.rows) {
for (var j = -k; j < k+1; j++) {
if(harbour.harbourStartRow+i >= 0 && harbour.harbourStartRow+i < game.rows) {
for (let j = -k; j < k+1; j+=1) {
// Restrict by map size for columns so not searching off edge of board
if(harbour.harbourStartCol+j >=0 && harbour.harbourStartCol+j < game.cols) {
// Checks if tile is active. If so runs pathTiles to search for potential tiles to activate around it
if (this.tradePath[harbour.harbourStartRow+i][harbour.harbourStartCol+j].activeStatus == 'active') {
if (this.tradePath[harbour.harbourStartRow+i][harbour.harbourStartCol+j].activeStatus === 'active') {
//keep for debugging - console.log('run: ' + k);
//keep for debugging - console.log('starting from: row: ' + (localStartRow+i) + ' col: ' + (localStartCol+j) + ' prior cost: ' + this.tradePath[localStartRow+i][localStartCol+j].moveCost);
searchFound = this.pathTiles(harbour.harbourStartRow+i, harbour.harbourStartCol+j, this.tradePath[harbour.harbourStartRow+i][harbour.harbourStartCol+j].moveCost, localMaxMove, directionAngle, k, harbour.harbourEndRow, harbour.harbourEndCol);
if (searchFound == true) {
if (searchFound === true) {
found = true;

}
}
}
Expand Down Expand Up @@ -354,15 +353,15 @@ let tradeContracts = {
// Loop through rows
for (var i = -1; i <= 1; i++) {
// Restrict by map size for rows
if(localStartRowI+i>=0 && localStartRowI+i < game.rows) {
if(localStartRowI+i >= 0 && localStartRowI+i < game.rows) {
// Loop through columns
for (var j = -1; j <= 1; j++) {
// Restrict by map size for columns
if(localStartColJ+j >=0 && localStartColJ+j < game.cols) {
// Restrict for land squares
if (game.boardArray[localStartRowI+i][localStartColJ+j].tile.terrain == 'sea' || (localStartRowI+i == localEndRow && localStartColJ+j == localEndCol)) {
// Restrict to avoid passing through land squares and whirlpools
if ( (game.boardArray[localStartRowI+i][localStartColJ+j].tile.terrain === 'sea' && game.boardArray[localStartRowI+i][localStartColJ+j].piece.category !== 'Hazards') || (localStartRowI+i === localEndRow && localStartColJ+j === localEndCol)) {
// Checks for reaching destination
if (localStartRowI+i == localEndRow && localStartColJ+j == localEndCol) {
if (localStartRowI+i === localEndRow && localStartColJ+j === localEndCol) {
localFound = true;
}
// Aggregate cost of reaching tile in tileCumulMoveCost - add the existing cost to the cost for reaching the new tile from moveCost
Expand Down
3 changes: 2 additions & 1 deletion gamemanagement.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ Game.prototype.prePlayerEvents = function() {

} else { // pirates turn
// Create and move whirlpools
if (this.gameDate > 0 * this.phaseCount) { // start at end of first moon along with contracts
//if (this.gameDate > 1 * this.phaseCount) { // start at end of first moon along with contracts
if (this.gameDate > 0 * this.phaseCount ) { // testing
whirlpool.manageWhirlpools();
}
}
Expand Down
74 changes: 43 additions & 31 deletions movement.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,22 +161,30 @@ let pieceMovement = {
//Keep useful for debugging - console.log('row: ' + (startRow+i) + ' col: ' + (startCol+j) + ' set to: ' + this.findPath[startRow+i][startCol+j].activeStatus + ' with cost: ' + this.findPath[startRow+i][startCol+j].moveCost + ' and distance: ' + this.findPath[startRow+i][startCol+j].distance);
}

// Sets Transport tile to inactive to prevent moving there
if (game.boardArray[startRow+i][startCol+j].piece.category == 'Transport') {
if (game.turn != 'Pirate') {
// Restrictions on Activation of tiles to prevent certain moves
if (game.boardArray[startRow+i][startCol+j].piece.category === 'Transport') {
// Player moves - sets Transport tile to inactive to prevent players moving there
if (game.turn !== 'Pirate') {
this.findPath[startRow+i][startCol+j].activeStatus = 'inactive';
game.boardArray[startRow+i][startCol+j].tile.activeStatus = 'inactive';
// Prevents pirate ships being activated on pirate ship moves
} else if (this.findPath[startRow+i][startCol+j].team == 'Pirate') {
} else if (this.findPath[startRow+i][startCol+j].team === 'Pirate') {
this.findPath[startRow+i][startCol+j].activeStatus = 'inactive';
game.boardArray[startRow+i][startCol+j].tile.activeStatus = 'inactive';
} else if (game.boardArray[startRow+i][startCol+j].piece.damageStatus == 0) {
// Prevents damaged ships being activated on pirate ship moves
} else if (game.boardArray[startRow+i][startCol+j].piece.damageStatus === 0) {
this.findPath[startRow+i][startCol+j].activeStatus = 'inactive';
game.boardArray[startRow+i][startCol+j].tile.activeStatus = 'inactive';
} else if (game.boardArray[startRow][startCol].piece.damageStatus == 0) {
// Prevents damaged pirate ships targeting other ships
} else if (game.boardArray[startRow][startCol].piece.damageStatus === 0) {
console.log('here2')
this.findPath[startRow+i][startCol+j].activeStatus = 'inactive';
game.boardArray[startRow+i][startCol+j].tile.activeStatus = 'inactive';
}
// Prevents players or pirates moving into a whirlpool
} else if (game.boardArray[startRow+i][startCol+j].piece.category === 'Hazards') {
this.findPath[startRow+i][startCol+j].activeStatus = 'inactive';
game.boardArray[startRow+i][startCol+j].tile.activeStatus = 'inactive';
}
}
}
Expand All @@ -189,26 +197,26 @@ let pieceMovement = {
// Method adds detail of targets and pieces to tiles within findPath - this information can be generated before findPath
// ---------------------------------------------------------------------------------------------------------------------
paintFindPath: function() {
for (var i = 0; i < game.cols; i++) {
for (var j = 0; j < game.rows; j++) {
for (let i = 0; i < game.cols; i+=1) {
for (let j = 0; j < game.rows; j+=1) {

// Target transport ships for pirate attack
if (game.boardArray[i][j].piece.category == 'Transport' && game.boardArray[i][j].piece.team != 'Pirate' && game.boardArray[i][j].piece.damageStatus == 5) {
if (game.boardArray[i][j].piece.category === 'Transport' && game.boardArray[i][j].piece.team !== 'Pirate' && game.boardArray[i][j].piece.damageStatus === 5) {
this.findPath[i][j].target = [{type: [game.boardArray[i][j].piece.type], team: game.boardArray[i][j].piece.team}];
}

// Resource harbours and virgin island harbours
if ((game.boardArray[i][j].tile.terrain == 'land' && !game.boardArray[i][j].piece.populatedSquare) ||
(game.boardArray[i][j].piece.category == 'Resources' && game.boardArray[i][j].piece.type != 'desert')) {
if ((game.boardArray[i][j].tile.terrain === 'land' && !game.boardArray[i][j].piece.populatedSquare) ||
(game.boardArray[i][j].piece.category === 'Resources' && game.boardArray[i][j].piece.type !== 'desert')) {
// Single tile search around the island
for (let k = -1; k < 2; k+=1) {
if(i + k >=0 && i + k <game.rows) {
for (let l = -1; l < 2; l+=1) {
if(j + l >=0 && j + l <game.cols) {
for (let k = -1; k <= 1; k+=1) {
if(i + k >=0 && i + k < game.rows) {
for (let l = -1; l <= 1; l+=1) {
if(j + l >=0 && j + l < game.cols) {
// Reduces search to exclude diagonals
if(k == 0 || l == 0) {
if(game.boardArray[i+k][j+l].tile.terrain == 'sea') {
if (game.boardArray[i][j].piece.category == 'Resources' && game.boardArray[i][j].piece.type != 'desert') {
if(game.boardArray[i+k][j+l].tile.terrain === 'sea') {
if (game.boardArray[i][j].piece.category === 'Resources' && game.boardArray[i][j].piece.type !== 'desert') {
this.findPath[i+k][j+l].resourceHarbour.push({type: game.boardArray[i][j].piece.type, detail: game.boardArray[i][j].piece.team, ref: i+'-'+j});
} else {
this.findPath[i+k][j+l].resourceHarbour.push({type: 'virgin', detail: 'Unclaimed', ref: i+'-'+j});
Expand All @@ -222,13 +230,13 @@ let pieceMovement = {
}

// Safe harbour for ship repair or hiding
if (game.boardArray[i][j].tile.subTerrain == 'harbour') {
if (game.boardArray[i][j].tile.subTerrain === 'harbour') {
// Single tile search around the harbour for fort reference
for (let k = -1; k < 2; k+=1) {
if(i + k >=0 && i + k <game.rows) {
for (let l = -1; l < 2; l+=1) {
if(j + l >=0 && j + l <game.cols) {
if (game.boardArray[i+k][j+l].piece.type == 'fort') {
for (let k = -1; k <= 1; k+=1) {
if(i + k >=0 && i + k < game.rows) {
for (let l = -1; l <= 1; l+=1) {
if(j + l >=0 && j + l < game.cols) {
if (game.boardArray[i+k][j+l].piece.type === 'fort') {
this.findPath[i][j].harbour.push({type: game.boardArray[i][j].tile.subTerrain, team: game.boardArray[i][j].tile.subTerrainTeam, ref: (i+k)+'-'+(j+l)});
}
}
Expand All @@ -237,16 +245,20 @@ let pieceMovement = {
}
}

// Tiles where path must end
if (game.boardArray[i][j].piece.category == 'Transport') {
// Tiles where path must end - cannot move through a ship
if (game.boardArray[i][j].piece.category === 'Transport') {
this.findPath[i][j].pathStop = [{type: game.boardArray[i][j].piece.type, team: game.boardArray[i][j].piece.team}];
// cannot move through a whirlpool
} else if (game.boardArray[i][j].piece.category === 'Hazards') {
this.findPath[i][j].pathStop = [{type: game.boardArray[i][j].piece.type, team: game.boardArray[i][j].piece.team}];
} else if (game.boardArray[i][j].tile.subTerrain == 'harbour') {
// cannot move through a harbour
} else if (game.boardArray[i][j].tile.subTerrain === 'harbour') {
// Single tile search around the harbour for fort reference
for (let k = -1; k < 2; k+=1) {
if(i + k >=0 && i + k <game.rows) {
for (let l = -1; l < 2; l+=1) {
if(j + l >=0 && j + l <game.cols) {
if (game.boardArray[i+k][j+l].piece.type == 'fort') {
for (let k = -1; k <= 1; k+=1) {
if(i + k >=0 && i + k < game.rows) {
for (let l = -1; l <= 1; l+=1) {
if(j + l >=0 && j + l < game.cols) {
if (game.boardArray[i+k][j+l].piece.type === 'fort') {
this.findPath[i][j].pathStop = [{type: game.boardArray[i][j].tile.subTerrain, team: game.boardArray[i][j].tile.subTerrainTeam, ref: (i+k)+'-'+(j+l)}];
}
}
Expand Down
9 changes: 8 additions & 1 deletion whirlpool.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,23 @@ let whirlpool = {
if (settings.workFlow === true) {console.log('Managing whirlpools: ' + (Date.now() - settings.launchTime)); }
const stockTotalPosition = stockDashboard.pieceTotals.findIndex(fI => fI.team == 'total');


// Choose whirlpool position for a quadrant
let whirlpoolPosition = (rowAdd, colAdd) => {

let valid = false
let row = 0;
let col = 0;
while (valid === false) {
row = Math.floor(Math.random()*15);
col = Math.floor(Math.random()*15);

//console.log(this.boardArray[row + rowAdd][col + colAdd].piece.populatedSquare, this.boardArray[row + rowAdd][col + colAdd].tile.terrain)
if (game.boardArray[row + rowAdd][col + colAdd].piece.populatedSquare === false && game.boardArray[row + rowAdd][col + colAdd].tile.terrain === 'sea') {
if (game.boardArray[row + rowAdd][col + colAdd].piece.populatedSquare === false
&& game.boardArray[row + rowAdd][col + colAdd].tile.terrain === 'sea'
&& game.boardArray[row + rowAdd][col + colAdd].tile.subTerrain === 'none'
&& game.board.checkNextTo([row + rowAdd, col + colAdd], 'land') === false
){
valid = true;
}
}
Expand Down

0 comments on commit 96480c7

Please sign in to comment.