Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automated ship movement - claiming resources #92

Merged
merged 1 commit into from Aug 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 50 additions & 2 deletions automove.js
Expand Up @@ -27,12 +27,12 @@ let computer = {
// --------------------------------------------------------------
automatePlayer: function() {
if(workFlow == 1) {console.log('Automate computer opponent: ' + (Date.now() - launchTime)); }

// Update array of all computer opponent ships
if (computer.computerShipsTurnCount == -1) {
this.populateComputerShipsArray();
// Separate array of ships for current turn
this.computerShipsTurn = this.computerShipsAll.filter(fI => fI.team == gameManagement.turn);
if(arrayFlow == 1) {console.log('computerShipsTurn', this.computerShipsTurn);}
}

// Increases counter to move on to next ship
Expand Down Expand Up @@ -178,7 +178,55 @@ let computer = {
}
}
}
if(workFlow == 1) {console.log(this.computerShipsAll);}
if(arrayFlow == 1) {console.log('computerShipsAll', this.computerShipsAll);}
},

// Method to check whether there are resources to be claimed and decide whether a resource should be claimed
// ---------------------------------------------------------------------------------------------------------
decideClaimResource: function() {
// Array of resources to be claimed
let claimableResources = [];
let teamPosition = stockDashboard.pieceTotals.findIndex(fI => fI.team == gameManagement.turn);
let resourceType = '';

// Check whether there are resources to be claimed
for (var k = 0; k < computer.computerShipsTurn.length; k+=1) {

for (var i = -1; i < 2; i+=1) {
if(computer.computerShipsTurn[k].end.row+i >=0 && computer.computerShipsTurn[k].end.row+i <row) {
for (var j = -1; j < 2; j+=1) {
if(computer.computerShipsTurn[k].end.col+j >=0 && computer.computerShipsTurn[k].end.col+j <col) {
// Reduces search to exclude diagonals
if(i == 0 || j == 0) {
// Checks if tile is land and unpopulated
if (gameBoard.boardArray[computer.computerShipsTurn[k].end.row+i][computer.computerShipsTurn[k].end.col+j].pieces.category == 'Resources' && gameBoard.boardArray[computer.computerShipsTurn[k].end.row+i][computer.computerShipsTurn[k].end.col+j].pieces.type != 'desert' && gameBoard.boardArray[computer.computerShipsTurn[k].end.row+i][computer.computerShipsTurn[k].end.col+j].pieces.team == 'Unclaimed') {
resourceType = gameBoard.boardArray[computer.computerShipsTurn[k].end.row+i][computer.computerShipsTurn[k].end.col+j].pieces.type;
// Exclude resource types that have already been claimed
if (stockDashboard.pieceTotals[teamPosition].pieces[resourceType].quantity == 0) {
claimableResources.push({row: computer.computerShipsTurn[k].end.row+i, col: computer.computerShipsTurn[k].end.col+j, type: gameBoard.boardArray[computer.computerShipsTurn[k].end.row+i][computer.computerShipsTurn[k].end.col+j].pieces.type, production: gameBoard.boardArray[computer.computerShipsTurn[k].end.row+i][computer.computerShipsTurn[k].end.col+j].pieces.production});
}
}
}
}
}
}
}
}
if(arrayFlow == 1) {console.log('claimableResources', claimableResources);}

// Reduce down array / sort order of claiming according to criteria - ignore for first implementation

// Decide whether a resource should be claimed - simple version of decision for first implementation
for (var l = 0; l < claimableResources.length; l+=1) {
// Need to check whether player has already claimed this resource in this loop - can happen that claimableResource includes 2 or 3 of same resource type
if (stockDashboard.pieceTotals[teamPosition].pieces[claimableResources[l].type].quantity == 0) {
// Claim resource (already checked that have resource in construction of claimable resource)
resourceManagement.claimResource(claimableResources[l].row, claimableResources[l].col, gameManagement.turn);
// Update stock take and stock dashboard before next claimable resource checked
stockDashboard.stockTake();
stockDashboard.drawStock();
}
}
},


Expand Down
2 changes: 1 addition & 1 deletion dashboard.js
Expand Up @@ -56,7 +56,7 @@ let stockDashboard = {
stockDashboard.pieceTotals[h].pieces[stockDashboard.pieceTypes[k].type] = {quantity: counter, goods: piecesGoods, stock: piecesStock};
}
}
//console.log('pieceTotals', stockDashboard.pieceTotals);
console.log('pieceTotals', stockDashboard.pieceTotals);
},

// Method to populate stock dashboard on left-hand panel
Expand Down
11 changes: 7 additions & 4 deletions gamemanagement.js
Expand Up @@ -461,7 +461,7 @@ let gameManagement = {

optionsArray: [
{ variable: 'speed', active: 'fast', options: [{text: 'slow', active: false, constant: 1.5}, {text: 'medium', active: false, constant: 1}, {text: 'fast', active: true, constant: 0.6}] },
{ variable: 'dev', options: [{text: 'workflow', active: false}, {text: 'transitions', active: false}] },
{ variable: 'dev', options: [{text: 'workflow', active: false}, {text: 'arrays', active: true}, {text: 'transitions', active: false}] },
],


Expand Down Expand Up @@ -498,7 +498,8 @@ let gameManagement = {
indexNew = this.optionsArray[1].options.findIndex(item => item.text == e.target.classList.item(1));
this.optionsArray[1].options[indexNew].active = !this.optionsArray[1].options[indexNew].active;
workFlow = this.optionsArray[1].options[0].active;
transitionMonitor = this.optionsArray[1].options[1].active;
arrayFlow = this.optionsArray[1].options[1].active;
transitionMonitor = this.optionsArray[1].options[2].active;
this.clearPanel();
this.devPanel(localScale);
}
Expand Down Expand Up @@ -533,8 +534,10 @@ let gameManagement = {
// Panel buttons - make into a loop?
workFlowCheck = settingsPanel.appendChild(this.optionBox(localScale, this.optionsArray[1].options[0], this.optionsArray[1].variable, 18));
workFlowCheck.setAttribute('transform', 'translate(' + (25 * localScale - 9 * localScale) + ', ' + (25 * localScale - 2 * localScale) + ')');
transitionsCheck = settingsPanel.appendChild(this.optionBox(localScale, this.optionsArray[1].options[1], this.optionsArray[1].variable, 18));
transitionsCheck.setAttribute('transform', 'translate(' + (25 * localScale - 9 * localScale) + ', ' + (32 * localScale - 2 * localScale) + ')');
arrayCheck = settingsPanel.appendChild(this.optionBox(localScale, this.optionsArray[1].options[1], this.optionsArray[1].variable, 18));
arrayCheck.setAttribute('transform', 'translate(' + (25 * localScale - 9 * localScale) + ', ' + (32 * localScale - 2 * localScale) + ')');
transitionsCheck = settingsPanel.appendChild(this.optionBox(localScale, this.optionsArray[1].options[2], this.optionsArray[1].variable, 18));
transitionsCheck.setAttribute('transform', 'translate(' + (25 * localScale - 9 * localScale) + ', ' + (39 * localScale - 2 * localScale) + ')');

},

Expand Down
11 changes: 3 additions & 8 deletions main.js
Expand Up @@ -13,7 +13,8 @@ const launchTime = Date.now();

let workFlow = gameManagement.optionsArray[1].options[0].active;
let gameBoardTrack = 0;
let transitionMonitor = gameManagement.optionsArray[1].options[1].active;
let arrayFlow = gameManagement.optionsArray[1].options[1].active;
let transitionMonitor = gameManagement.optionsArray[1].options[2].active;



Expand Down Expand Up @@ -537,14 +538,8 @@ function boardHandler(event) {
if (pieceMovement.movementArray.end.pieces.category == 'Transport' && pieceMovement.movementArray.start.pieces.type != 'desert' && pieceMovement.movementArray.start.pieces.team == 'Unclaimed') {
pieceMovement.deactivateTiles(1);
gameBoard.drawActiveTiles();
// Calculate placement on board of resource tile to be altered
let IDPiece = 'tile' + Number(pieceMovement.movementArray.start.row*1000 + pieceMovement.movementArray.start.col);
document.getElementById(IDPiece).remove();
gameBoard.boardArray[pieceMovement.movementArray.start.row][pieceMovement.movementArray.start.col].pieces.team = gameManagement.turn;
boardMarkNode.appendChild(gameBoard.createActionTile(pieceMovement.movementArray.start.row, pieceMovement.movementArray.start.col, gameBoard.boardArray[pieceMovement.movementArray.start.row][pieceMovement.movementArray.start.col].pieces.type, gameBoard.boardArray[pieceMovement.movementArray.start.row][pieceMovement.movementArray.start.col].pieces.team,
'tile' + Number((pieceMovement.movementArray.start.row)*1000 + (pieceMovement.movementArray.start.col)), boardSurround + tileBorder/2 + (gridSize + tileBorder * 2) * pieceMovement.movementArray.start.row, boardSurround + tileBorder/2 + (gridSize + tileBorder * 2) * pieceMovement.movementArray.start.col, 1, gameBoard.boardArray[pieceMovement.movementArray.start.row][pieceMovement.movementArray.start.col].pieces.direction));
resourceManagement.claimResource(pieceMovement.movementArray.start.row, pieceMovement.movementArray.start.col, gameManagement.turn);
startEnd = 'start';
gameScore.workScores('Exploring', gameManagement.turn, pieceMovement.movementArray.start.pieces.type);

// Loading of goods
} else if ((pieceMovement.movementArray.start.pieces.category == 'Resources' || pieceMovement.movementArray.start.pieces.category == 'Settlements') && pieceMovement.movementArray.end.pieces.category == 'Transport') {
Expand Down
44 changes: 34 additions & 10 deletions movement.js
Expand Up @@ -375,12 +375,12 @@ let pieceMovement = {
gameBoard.boardArray[pieceMovement.movementArray['start'].row][pieceMovement.movementArray['start'].col].pieces = {populatedSquare: false, category: '', type: 'no piece', direction: '', used: 'unused', damageStatus: 5, team: '', goods: 'none', stock: 0};
gameBoard.boardArray[pieceMovement.movementArray.end.row][pieceMovement.movementArray.end.col].pieces = {populatedSquare: true, category: pieceMovement.movementArray.start.pieces.category, type: pieceMovement.movementArray.start.pieces.type, direction: rotateDirection, used: 'used', damageStatus: pieceMovement.movementArray.start.pieces.damageStatus, team: pieceMovement.movementArray.start.pieces.team, goods: pieceMovement.movementArray.start.pieces.goods, stock: pieceMovement.movementArray.start.pieces.stock, ref: pieceMovement.movementArray.start.pieces.ref};

//Updating piece information
// Updating piece information
chosenPiece.setAttribute('id', 'tile' + Number(pieceMovement.movementArray.end.row*1000 + pieceMovement.movementArray.end.col));
chosenPiece.style.transition = '';
gameBoard.drawActiveTiles();
pieceMovement.harbourRepairArrival(chosenPiece);
pieceMovement.shipConflict(rotateDirection);

pieceMovement.postTransition(rotateDirection, chosenPiece);
}
} else {
if(transitionMonitor == 1) {console.log('TM: Transition ignored - rotation not translation');}
Expand Down Expand Up @@ -419,15 +419,38 @@ let pieceMovement = {

},

// Method for post ship movement actions
// -------------------------------------
postTransition: function(rotateDirection, chosenPiece) {
// Pirates, human, computer opponent treated separately
// Move completion called to reset once post transition complete
// Pirate ship conflict has transitions so move completion needs to be called once this is finished (from shipConflict)
// May need to do this for computer opponents as well once actions with transitions implemented
if (gameManagement.type == 'Pirate') {
pieceMovement.harbourRepairArrival(chosenPiece);
pieceMovement.shipConflict(rotateDirection);
} else if (gameManagement.type == 'human') {
if(this.movementArray.start.pieces.damageStatus == 5) {
pieceMovement.landDiscovery();
}
pieceMovement.harbourRepairArrival(chosenPiece);
pieceMovement.moveCompletion();
} else { // 'computer'
if(this.movementArray.start.pieces.damageStatus == 5) {
pieceMovement.landDiscovery();
}
computer.decideClaimResource();
pieceMovement.harbourRepairArrival(chosenPiece);
pieceMovement.moveCompletion();
}
},

// Method to complete ship movement once all transitions have been cycled through
// ------------------------------------------------------------------------------
moveCompletion: function(chosenPiece) {
// Applying moves to game board array
if(workFlow == 1) {console.log('----- Move Completion activated ----- ' + (Date.now() - launchTime)); }

if (gameManagement.type == 'human') {
pieceMovement.landDiscovery();

// Resetting movement array once second click has been made (if move valid)
pieceMovement.movementArray = {start: {row: '', col: ''}, end: {row: '', col: ''}};
startEnd = 'start';
Expand All @@ -442,6 +465,8 @@ let pieceMovement = {
startEnd = 'start';
pirates.automatePirates();
} else if (gameManagement.type == 'computer') {

// Resetting movement array once second click has been made (if move valid)
pieceMovement.movementArray = {start: {row: '', col: ''}, end: {row: '', col: ''}};
startEnd = 'start';
computer.automatePlayer();
Expand Down Expand Up @@ -480,7 +505,7 @@ let pieceMovement = {
if(this.movementArray.end.row+i >=0 && this.movementArray.end.row+i <row) {
for (var j = -searchDistance; j < searchDistance + 1; j++) {
if(this.movementArray.end.col+j >=0 && this.movementArray.end.col+j <col) {
// Reduces seacrh to exclude diagonals
// Reduces search to exclude diagonals
if(i == 0 || j == 0) {
// Checks if tile is land and unpopulated
if(gameBoard.boardArray[this.movementArray.end.row+i][this.movementArray.end.col+j].terrain == 'land' && !gameBoard.boardArray[this.movementArray.end.row+i][this.movementArray.end.col+j].pieces.populatedSquare) {
Expand All @@ -498,8 +523,6 @@ let pieceMovement = {
}
}
}

//console.log('valid cargo - start');
},

// Method to check a ship is nearby to allow resource to be settled
Expand Down Expand Up @@ -577,7 +600,8 @@ let pieceMovement = {
let endCannon = 0;
if (pirates.conflictArray.conflict == true) {
if(workFlow == 1) {console.log('Ship conflict - battle: ' + (Date.now() - launchTime)); }
//console.log(pirates.conflictArray);
console.log(pirates.conflictArray);
console.log(startDirection);
// Obtains ID and element of pirates
IDPirate = 'tile' + Number(pirates.conflictArray.pirate.row*1000 + pirates.conflictArray.pirate.col);
let piratePiece = document.getElementById(IDPirate);
Expand Down
14 changes: 14 additions & 0 deletions resource.js
Expand Up @@ -67,5 +67,19 @@ let resourceManagement = {
return(cardType);
},

// Method to claim resource tile - used by both human and computer opponent game logic
// -----------------------------------------------------------------------------------
claimResource: function(localRow, localCol, localTeam) {
// Calculate placement on board of resource tile to be altered and remove it
let IDPiece = 'tile' + Number(localRow*1000 + localCol);
document.getElementById(IDPiece).remove();
// Change board array and add new SVG piece with team colour
gameBoard.boardArray[localRow][localCol].pieces.team = localTeam;
boardMarkNode.appendChild(gameBoard.createActionTile(localRow, localCol, gameBoard.boardArray[localRow][localCol].pieces.type, localTeam,
'tile' + Number(localRow*1000 + localCol), boardSurround + tileBorder/2 + (gridSize + tileBorder * 2) * localRow, boardSurround + tileBorder/2 + (gridSize + tileBorder * 2) * localCol, 1, gameBoard.boardArray[localRow][localCol].pieces.direction));
// Update score as necessary
gameScore.workScores('Exploring', localTeam, gameBoard.boardArray[localRow][localCol].pieces.type);
},

// LAST BRACKET OF OBJECT
}