Battle Analysis: Move Calculator

Move Calculator

As the units move, their position is saved as a hexagon point. The hexagon name is calculated from the hexagon point when it is to be displayed.

The move rules will check if the move is valid using the unique moveCalculator for each wargame.

MoveRules.prototype.moveUnitToHexagon = function(index, hexagonPoint) {
  var moveAmount;
  var currentHexagonPoint = this.force.getUnitHexagonPoint(index);
  if (this.force.unitIsMoving(index) && this.moveCalculator.moveIsValid(index, hexagonPoint)) {
    moveAmount = this.moveCalculator.calculateMoveCost(index, currentHexagonPoint, hexagonPoint);
    this.updateMove(index, hexagonPoint, moveAmount);
    }
  }

moveCalculator.moveIsValid(index, hexagonPoint) will :

The following code is an example of calculating the move cost

Since the grid is a cartesian coordinate, the hexside coordinate can be calculated with ease.

	MoveCalculator.prototype.calculateMoveCost = function (index, startHexagonPoint, endHexagonPoint) {
		// get entrance cost
		var moveCost = this.getEntranceMoveCost(index, endHexagonPoint);
	
		 var hexsideX = ( startHexagonPoint.getX() + endHexagonPoint.getX() ) / 2;
		 var hexsideY = ( startHexagonPoint.getY() + endHexagonPoint.getY() ) / 2;
		var hexside = new Point(hexsideX, hexsideY)
	
		if(this.terrain.hasTerrainType(hexside, "river") == true) {
			moveCost += 1;
		}
	
		// road override
		if(this.moveIsTraverse(startHexagonPoint, endHexagonPoint, "road")){
			moveCost = 1;
		}
	
		// move cost on exit is the entrance cost of the leaving hexagonPoint
		if ( this.terrain.hasTerrainType(endHexagonPoint, "exit") == true ) {
		// if leaving road, exit cost is road
			if (this.terrain.hasTerrainType(hexside, "road") == true) {
				moveCost = 1;
			}
		}
	
		return moveCost;
	}