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

which provide the following methods:


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;
	}
	/**
	* method to get entrance cost
	*
	* @method getEntranceMoveCost
	* @param index {integer}
	* @param hexagonPoint {point}
	* @return {integer} moveCost
	*/
	MoveCalculator.prototype.getEntranceMoveCost = function (index, hexagonPoint) {
		var moveCost = 1;
		if(this.terrain.hasTerrainType(hexagonPoint, "clear") == true) {
			moveCost = 2;
		}
		if(this.terrain.hasTerrainType(hexagonPoint, "rough") == true) {
			moveCost = 3;
		}
		if(this.terrain.hasTerrainType(hexagonPoint, "forest") == true) {
			moveCost = 3;
		}
		return moveCost;
	}