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 - focus on damaged ships #91

Merged
merged 1 commit into from Jul 30, 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
89 changes: 67 additions & 22 deletions automove.js
Expand Up @@ -7,16 +7,21 @@ let computer = {

computerShipsTurn: [],

teamHome: { 'Red Team': {returnRow: 0, returnCol: 15},
'Green Team': {returnRow: 30, returnCol: 15},
'Blue Team': {returnRow: 15, returnCol: 0},
'Orange Team': {returnRow: 15, returnCol: 30},
teamHome: { 'Red Team': {homeRow: 0, homeCol: 15},
'Green Team': {homeRow: 30, homeCol: 15},
'Blue Team': {homeRow: 15, homeCol: 0},
'Orange Team': {homeRow: 15, homeCol: 29},
},

computerShipsTurnCount: -1,

targetHarbour: [],

telescopeHarbour: [],

computerShipsTurnCount: -1,
maxDistanceTiles: [],

minCostTiles: [],

// Method to manage automated movement of computer opponent ships
// --------------------------------------------------------------
Expand All @@ -38,7 +43,7 @@ let computer = {
computer.computerMove();

} else {
if(workFlow == 1) {console.log('All computer opponent ships have moved. Update dashboards and reset pirate info: ' + (Date.now() - launchTime)); }
if(workFlow == 1) {console.log('All computer opponent ships have moved. Update dashboards and event listeners: ' + (Date.now() - launchTime)); }
// Update the stock dashboard
stockDashboard.stockTake();
stockDashboard.drawStock();
Expand All @@ -56,50 +61,95 @@ let computer = {
computerMove: function() {
if(workFlow == 1) {console.log('Computer player: ' + computer.computerShipsTurn[computer.computerShipsTurnCount].team + ' ' + computer.computerShipsTurn[computer.computerShipsTurnCount].type + ' moves: '+ (Date.now() - launchTime)); }
if(computer.computerShipsTurnCount < computer.computerShipsTurn.length) {

// -------------- SET UP -----------------
// Resets movement array
pieceMovement.movementArray = {start: {row: '', col: ''}, end: {row: '', col: ''}};

let pathDistance = 0;
// Starting tile for pirate ship move taken from array of pirate ships
pieceMovement.movementArray.start = computer.computerShipsTurn[computer.computerShipsTurnCount].start;

// Pulling maxMove from pieces array
maxMove = 0;
let arrayPosition = stockDashboard.pieceTypes.findIndex(k => k.type == pieceMovement.movementArray.start.pieces.type);
if (arrayPosition != -1) {
maxMove = stockDashboard.pieceTypes[arrayPosition].maxMove;
}

// -------------- FINDING PATHS AND ACTIVATING TILES (equivalent of START of move) -----------------
// Tiles activated which also finds path for moves and information on reachable area
// true / false allow red boundaries to be highlighted or not
let searchRange = 10;
//let searchRange = 0;
let searchRange = 0;
// Activating tiles and findPath for damaged ships at sea (damageStatus is set to 0 after battle loss)
if (pieceMovement.movementArray.start.pieces.damageStatus == 0) {
if(workFlow == 1) {console.log('Damaged ship - find paths: '+ (Date.now() - launchTime)); }
// TBC - Add search range calculation
searchRange = 10; // Consider processing requirement and determine whether this needs to be restricted to 10
pieceMovement.activateTiles(pieceMovement.movementArray.start.row, pieceMovement.movementArray.start.col, 2.1, searchRange, true, 0);
// Activating tiles and findPath for undamaged ships (damageStatus is 5 for healthy ships)
} else if (pieceMovement.movementArray.start.pieces.damageStatus == 5) {
if(workFlow == 1) {console.log('Good ship - find paths: '+ (Date.now() - launchTime)); }
// TBC - Add search range calculation
searchRange = 30;
pieceMovement.activateTiles(pieceMovement.movementArray.start.row, pieceMovement.movementArray.start.col, maxMove, searchRange, true, 5);
// Setting findPath for ships under repair (damageStatus between 0 and 5) to prevent ships moving
} else {
pieceMovement.initialisefindPath(pieceMovement.movementArray.start.row, pieceMovement.movementArray.start.col);
}

// Redraw active tile layer after activation to show activated tiles
gameBoard.drawActiveTiles();

// -------------- DECIDING MOVE (equivalent of END of move) -----------------
// Deciding move for damaged ships at sea (damageStatus is set to 0 after battle loss)
if (pieceMovement.movementArray.start.pieces.damageStatus == 0) {
// To be completed
if(workFlow == 1) {console.log('Damaged ship - decide move: '+ (Date.now() - launchTime)); }

computer.targetHarbour = pirates.findTarget('All', 'harbour');
if(workFlow == 1) {console.log('targetHarbour', computer.targetHarbour);}
computer.telescopeHarbour = pirates.useTelescope('All', 'harbour', maxMove);
if(workFlow == 1) {console.log('telescopeHarbour', computer.telescopeHarbour);}

if (computer.targetHarbour.length > 0) {
// 1 - Move to safe harbour within wind range
if(workFlow == 1) {console.log('Move to safe harbour within wind range: ' + (Date.now() - launchTime)); }
computer.targetHarbour = pirates.minArray(computer.targetHarbour, 'distance');
computer.targetHarbour = pirates.minArray(computer.targetHarbour, 'moveCost');
if(workFlow == 1) {console.log('targetCargo - min', computer.targetHarbour);}
// Attacks targetable Transports if in range (currently just uses first Transport in array - to improve in future)
// Keep - useful for debugging - console.log('found target: ' + pirates.targetCargo[0].row + ' ' + pirates.targetCargo[0].col);
lastTile = pirates.findLastActive(pieceMovement.findPath[computer.targetHarbour[0].row][computer.targetHarbour[0].col].path, 0);
computer.computerShipsTurn[computer.computerShipsTurnCount].end.row = pieceMovement.findPath[computer.targetHarbour[0].row][computer.targetHarbour[0].col].path[lastTile].fromRow;
computer.computerShipsTurn[computer.computerShipsTurnCount].end.col = pieceMovement.findPath[computer.targetHarbour[0].row][computer.targetHarbour[0].col].path[lastTile].fromCol;
} else if (computer.telescopeHarbour.length > 0) {
// 2 - Search for nearest safe Harbour outside wind range
if(workFlow == 1) {console.log('Move towards safe harbour outside wind range: ' + (Date.now() - launchTime)); }
// Finds safe harbours within the findPath then cuts down array based on minimum distance and move cost
computer.telescopeHarbour = pirates.minArray(computer.telescopeHarbour, 'distance');
computer.telescopeHarbour = pirates.minArray(computer.telescopeHarbour, 'moveCost');
if(workFlow == 1) {console.log('telescopeHarbour - min', computer.telescopeHarbour);}
lastTile = pirates.findLastActive(pieceMovement.findPath[computer.telescopeHarbour[0].row][computer.telescopeHarbour[0].col].path, 0);
computer.computerShipsTurn[computer.computerShipsTurnCount].end.row = pieceMovement.findPath[computer.telescopeHarbour[0].row][computer.telescopeHarbour[0].col].path[lastTile].fromRow;
computer.computerShipsTurn[computer.computerShipsTurnCount].end.col = pieceMovement.findPath[computer.telescopeHarbour[0].row][computer.telescopeHarbour[0].col].path[lastTile].fromCol;
} else {
// 3 - Moves towards home - should almost never be called
if(workFlow == 1) {console.log('Moves towards home: ' + (Date.now() - launchTime)); }
lastTile = pirates.findLastActive(pieceMovement.findPath[computer.computerShipsTurn[computer.computerShipsTurnCount].manifest.homeRow][computer.computerShipsTurn[computer.computerShipsTurnCount].manifest.homeCol].path, 0);
computer.computerShipsTurn[computer.computerShipsTurnCount].end.row = pieceMovement.findPath[computer.computerShipsTurn[computer.computerShipsTurnCount].manifest.homeRow][computer.computerShipsTurn[computer.computerShipsTurnCount].manifest.homeCol].path[lastTile].fromRow;
computer.computerShipsTurn[computer.computerShipsTurnCount].end.col = pieceMovement.findPath[computer.computerShipsTurn[computer.computerShipsTurnCount].manifest.homeRow][computer.computerShipsTurn[computer.computerShipsTurnCount].manifest.homeCol].path[lastTile].fromCol;
}

// Deciding move for undamaged ships (damageStatus is 5 for healthy ships)
} else if (pieceMovement.movementArray.start.pieces.damageStatus == 5) {
if(workFlow == 1) {console.log('Good ship - decide move: '+ (Date.now() - launchTime)); }

// Move maximum distance at minimum wind cost
if(workFlow == 1) {console.log('Finds max distance move at minimum cost: ' + (Date.now() - launchTime)); }
computer.maxDistanceTiles = pirates.maxPathDistance();
computer.minCostTiles = pirates.minArray(computer.maxDistanceTiles, 'moveCost');
computer.computerShipsTurn[computer.computerShipsTurnCount].end.row = computer.minCostTiles[0].row;
computer.computerShipsTurn[computer.computerShipsTurnCount].end.col = computer.minCostTiles[0].col;

// Catching move for ships under repair (damageStatus between 0 and 5)
} else {
// No action currently necessary - included for future development
}

// -------------- GRAPHICS / TRANSITION OF MOVE -----------------
// End position for pirate ship confirmed with movement array then move activated and dashboard recalculated
pieceMovement.movementArray.end = computer.computerShipsTurn[computer.computerShipsTurnCount].end;
if(workFlow == 1) {
Expand All @@ -111,11 +161,6 @@ let computer = {
}
},

maxDistanceTiles: [],

minCostTiles: [],


// Method to generate a list of computer opponent ships to move
// ------------------------------------------------------------
populateComputerShipsArray: function() {
Expand All @@ -133,7 +178,7 @@ let computer = {
}
}
}
console.log(this.computerShipsAll);
if(workFlow == 1) {console.log(this.computerShipsAll);}
},


Expand Down
20 changes: 6 additions & 14 deletions board.js
Expand Up @@ -225,21 +225,13 @@ let gameBoard = {
// TEST AREA


// Ships
this.boardArray[boardCenter-2][col-1].pieces = {populatedSquare: true, category: 'Transport', type: 'cargo ship', direction: '-90', used: 'unused', damageStatus: 5, team: 'Orange Team', goods: 'none', stock: 0, production: 0, homeRow: boardCenter-1, homeCol: col-1};
this.boardArray[0][boardCenter-2].pieces = {populatedSquare: true, category: 'Transport', type: 'warship', direction: '180', used: 'unused', damageStatus: 5, team: 'Red Team', goods: 'none', stock: 0, production: 0, homeRow: 0, homeCol: boardCenter-1};

// Extra Ships
/*
// Clay
this.boardArray[row-3][boardCenter+1] = {xpos: row-3, ypos: boardCenter+1, terrain: 'land', subTerrain: 'none', activeStatus: 'inactive', pieces: {populatedSquare: true, category: 'Resources', type: 'clay', direction: '0', used: 'unused', damageStatus: 5, team: 'Unclaimed', goods: 'pottery', stock: 18, production: 1}};

// Flax
this.boardArray[row-3][boardCenter] = {xpos: row-3, ypos: boardCenter, terrain: 'land', subTerrain: 'none', activeStatus: 'inactive', pieces: {populatedSquare: true, category: 'Resources', type: 'flax', direction: '0', used: 'unused', damageStatus: 5, team: 'Unclaimed', goods: 'cloth', stock: 18, production: 1}};

// Iron
this.boardArray[6][boardCenter] = {xpos: row-3, ypos: boardCenter, terrain: 'land', subTerrain: 'none', activeStatus: 'inactive', pieces: {populatedSquare: true, category: 'Resources', type: 'ironworks', direction: '0', used: 'unused', damageStatus: 5, team: 'Unclaimed', goods: 'iron', stock: 18, production: 1}};

this.boardArray[5][boardCenter].pieces = {populatedSquare: true, category: 'Transport', type: 'catamaran', direction: '45', used: 'unused', damageStatus: 5, team: 'Green Team', goods: 'none', stock: 0};
this.boardArray[boardCenter-3][col-9].pieces = {populatedSquare: true, category: 'Transport', type: 'cargo ship', direction: '-90', used: 'unused', damageStatus: 5, team: 'Orange Team', goods: 'none', stock: 0, production: 0, homeRow: boardCenter-1, homeCol: col-1};
this.boardArray[boardCenter-2][col-9].pieces = {populatedSquare: true, category: 'Transport', type: 'catamaran', direction: '-90', used: 'unused', damageStatus: 5, team: 'Orange Team', goods: 'none', stock: 0, production: 0, homeRow: boardCenter-1, homeCol: col-1};
this.boardArray[boardCenter-3][col-10].pieces = {populatedSquare: true, category: 'Transport', type: 'warship', direction: '-90', used: 'unused', damageStatus: 5, team: 'Orange Team', goods: 'none', stock: 0, production: 0, homeRow: boardCenter-1, homeCol: col-1};
this.boardArray[boardCenter-2][col-10].pieces = {populatedSquare: true, category: 'Transport', type: 'catamaran', direction: '180', used: 'unused', damageStatus: 5, team: 'Red Team', goods: 'none', stock: 0, production: 0, homeRow: boardCenter-1, homeCol: col-1};
this.boardArray[10][boardCenter-2].pieces = {populatedSquare: true, category: 'Transport', type: 'warship', direction: '180', used: 'unused', damageStatus: 0, team: 'Red Team', goods: 'none', stock: 0, production: 0, homeRow: 0, homeCol: boardCenter-1};

// Battle Royale
this.boardArray[row-4][boardCenter].pieces = {populatedSquare: true, category: 'Transport', type: 'cargo ship', direction: '45', used: 'unused', damageStatus: 5, team: 'Pirate', goods: 'none', stock: 0};
Expand Down