public static HexagonShape GetDirection(int dir) { HexagonShape h = new HexagonShape(); h.a = transLocation[RoundToDir(dir)].a; h.b = transLocation[RoundToDir(dir)].b; return(h); }
public static HexagonShape ToArrayPos(this HexagonShape self, int radius) { if (self.a == 0 && self.b == 0) { return(self); } return(new HexagonShape(self.a + radius - 1, self.b + radius - 1)); }
public static HexagonShape[] GetDirections() { HexagonShape[] h = new HexagonShape[totalDirections]; for (int i = 0; i < totalDirections; i++) { h[i] = new HexagonShape(); h[i].a = transLocation[i].a; h[i].b = transLocation[i].b; } return(h); }
public static float GetHeuristic(HexagonShape current, HexagonShape goal) { int distX = Mathf.Abs(current.a - goal.a); int distY = Mathf.Abs(current.b - goal.b); //x + y + z = 0 int current_z = current.a + current.b; int goal_z = goal.a + goal.b; int distZ = Mathf.Abs(current_z - goal_z); return((distX + distY + distZ) / 2.0f); }
public GridGeneration getBlocks(int x, int y) { if (shapeOfBlock == BlockGrid.SquareShape) { return(blockToGrid[y, x]); } else if (shapeOfBlock == BlockGrid.HexagonShape) { HexagonShape h = new HexagonShape(x, y); h = h.ToArrayPos(blockNumber); return(blockToGrid[h.b, h.a]); } return(null); }
public void GenerateHexagonRadiusGrid(int radius) //keeping the radius { if (radius <= 0) { return; } int prevRadius = 0; //hexagon shape board is null when 'prevRadius' <= 0 if (blockToGrid != null) { prevRadius = (blockToGrid.GetLength(0) - 1) / 2 + 1; } if (radius < prevRadius) { for (int i = 0; i < prevRadius; i++) { HexagonShape nextHexagonShape = HexagonAxis.GetDirection(4) * i; for (int dir = 0; dir < 6; dir++) { for (int j = 0; j < i; j++) { if (i >= radius) { Destroy(blockToGrid[nextHexagonShape.ToArrayPos(prevRadius).b, nextHexagonShape.ToArrayPos(prevRadius).a].gameObject); nextHexagonShape = nextHexagonShape.GetNeighbour(dir); } } } } } HexagonShapeScript[,] newHexagonShapeBoard = new HexagonShapeScript[(radius - 1) * 2 + 1, (radius - 1) * 2 + 1]; //generate blocks in the center if (0 < prevRadius) { //itinerate newHexagonShapeBoard[0, 0] = (HexagonShapeScript)blockToGrid[0, 0]; } else { //Instantiate newHexagonShapeBoard[0, 0] = Instantiate(savedBlocks[(int)shapeOfBlock]).GetComponent <HexagonShapeScript>(); newHexagonShapeBoard[0, 0].transform.parent = hexagonShapeBoardParent; } newHexagonShapeBoard[0, 0].transform.localScale = Vector3.one * blockSize; newHexagonShapeBoard[0, 0].transformValueHexagon = new HexagonShape(0, 0); newHexagonShapeBoard[0, 0]._isScannedArea = false; newHexagonShapeBoard[0, 0].isWall = false; newHexagonShapeBoard[0, 0].trackPos = Mathf.Infinity; newHexagonShapeBoard[0, 0].groupBlock = null; newHexagonShapeBoard[0, 0].currentSit = blockEnum.baseBlock; for (int i = 1; i < radius; i++) { HexagonShape nextHexagonShape = HexagonAxis.GetDirection(4) * i; for (int dir = 0; dir < 6; dir++) { for (int j = 0; j < i; j++) { HexagonShape arrayPos = nextHexagonShape.ToArrayPos(radius); HexagonShape oldArrayPos = nextHexagonShape.ToArrayPos(prevRadius); if (i < prevRadius) { //Itinerate newHexagonShapeBoard[arrayPos.b, arrayPos.a] = (HexagonShapeScript)blockToGrid[oldArrayPos.b, oldArrayPos.a]; } else { //Instantiating newHexagonShapeBoard[arrayPos.b, arrayPos.a] = Instantiate(savedBlocks[(int)shapeOfBlock]).GetComponent <HexagonShapeScript>(); newHexagonShapeBoard[arrayPos.b, arrayPos.a].transform.parent = hexagonShapeBoardParent; } newHexagonShapeBoard[arrayPos.b, arrayPos.a].transform.localScale = Vector3.one * blockSize; newHexagonShapeBoard[arrayPos.b, arrayPos.a].transformValueHexagon = new HexagonShape(nextHexagonShape.a, nextHexagonShape.b); newHexagonShapeBoard[arrayPos.b, arrayPos.a]._isScannedArea = false; newHexagonShapeBoard[arrayPos.b, arrayPos.a].isWall = false; newHexagonShapeBoard[arrayPos.b, arrayPos.a].trackPos = Mathf.Infinity; newHexagonShapeBoard[arrayPos.b, arrayPos.a].groupBlock = null; newHexagonShapeBoard[arrayPos.b, arrayPos.a].currentSit = blockEnum.baseBlock; nextHexagonShape = nextHexagonShape.GetNeighbour(dir); } } } //Save the new hexagon shape board blockToGrid = newHexagonShapeBoard; return; }
void GetAccess() { //If open list count is empty if (generationList.Count > 0 && !EndPosition._isScannedArea) { //It sets the current block as first on the list GridGeneration currentTile = generationList[0]; //The Block with the smallest movement cost is found for (int i = 1; i < generationList.Count; i++) { float newHeuristic = 0.0f; float curHeuristic = 0.0f; if (algorithmType == PathDecisionAlgorithm.AStarStyleAlgorithm) { if (BlockStyle == BlockGrid.SquareShape) { SquareShape newSquareShape = ((SquareShapeScript)generationList[i]).transformValue; SquareShape curSquareShape = ((SquareShapeScript)currentTile).transformValue; newHeuristic = newSquareShape.GetHeuristic(EndPosition.a, EndPosition.b); curHeuristic = curSquareShape.GetHeuristic(EndPosition.a, EndPosition.b); } else if (BlockStyle == BlockGrid.HexagonShape) { HexagonShape newHexagonShape = ((HexagonShapeScript)generationList[i]).transformValueHexagon; HexagonShape curHexagonShape = ((HexagonShapeScript)currentTile).transformValueHexagon; newHeuristic = newHexagonShape.GetHeuristic(EndPosition.a, EndPosition.b); curHeuristic = curHexagonShape.GetHeuristic(EndPosition.a, EndPosition.b); } newHeuristic *= weight; curHeuristic *= weight; } if (generationList[i].trackPos + newHeuristic < currentTile.trackPos + curHeuristic) { currentTile = generationList[i]; } } //It removes the current Block from open list currentTile._isScannedArea = true; generationList.Remove(currentTile); if (BlockStyle == BlockGrid.SquareShape) { //Neighbours of current block are taken SquareShape s = ((SquareShapeScript)currentTile).transformValue; SquareShape[] neighbours = s.getClosestBlock(); for (int i = 0; i < neighbours.Length; i++) { //Coordinate valid or not if ( neighbours[i].b < 0 || neighbours[i].b >= blockGrid.GetLength(0) || neighbours[i].a < 0 || neighbours[i].a >= blockGrid.GetLength(1) ) { continue; } //The coordinaes are transformed to grid piece script GridGeneration closestBlock = GridManager.type.getBlocks(neighbours[i].a, neighbours[i].b); //If neighbour does not exist, then skip if (closestBlock == null) { continue; } //If neighbour is obstacle, then skip if (closestBlock.isWall) { continue; } //If crossing diagonal gaps exist, then skip if (blockSidewayWall && !ignoreSidewayWall && i % 2 != 0) { GridGeneration prevNeighbour = GridManager.type.getBlocks(s.getClosestBlock(i - 1).a, s.getClosestBlock(i - 1).b); GridGeneration nextNeighbour = GridManager.type.getBlocks(s.getClosestBlock(i + 1).a, s.getClosestBlock(i + 1).b); if (prevNeighbour.isWall && nextNeighbour.isWall) //If diagonal gap, then do not cross { continue; } } //If neighbour is checked, then skip if (closestBlock._isScannedArea) { continue; } //Setting the new cost to the neighbour's mevement cost float newCost = currentTile.trackPos + s.GetCost(i); //If the new cost is smaller than the neighbour movement cost, then replace it and set it as parent tile if (newCost < closestBlock.trackPos) { closestBlock.trackPos = newCost; closestBlock.groupBlock = currentTile; if (!generationList.Contains(closestBlock)) { generationList.Add(closestBlock); } } } } else if (BlockStyle == BlockGrid.HexagonShape) { //Getting the neighbours of the current tile for the hexagon shape HexagonShape Hexagon = ((HexagonShapeScript)currentTile).transformValueHexagon; HexagonShape[] neighbours = Hexagon.GetNeighbours(); for (int i = 0; i < neighbours.Length; i++) { //Coordinates valid or not if ( neighbours[i].ToArrayPos(GridManager.type.blockNumber).b < 0 || neighbours[i].ToArrayPos(GridManager.type.blockNumber).b >= blockGrid.GetLength(0) || neighbours[i].ToArrayPos(GridManager.type.blockNumber).a < 0 || neighbours[i].ToArrayPos(GridManager.type.blockNumber).a >= blockGrid.GetLength(1) ) { continue; } //Coordinates a transfromed to grid piece script GridGeneration neighbour = GridManager.type.getBlocks(neighbours[i].a, neighbours[i].b); //If the neighbour does not exist, then skip if (neighbour == null) { continue; } //If the neighbour is an obstacle, then skip if (neighbour.isWall) { continue; } //also if is checked if (neighbour._isScannedArea) { continue; } //Setting the new cost to neighbour's movement cost float newCost = currentTile.trackPos + Hexagon.GetCost(i); // If the new cost is smaller, then replace it and set it as the parent tile if (newCost < neighbour.trackPos) { neighbour.trackPos = newCost; neighbour.groupBlock = currentTile; if (!generationList.Contains(neighbour)) { generationList.Add(neighbour); } } } } } else { //else, pathfinder is finding the next stage PathLocatingStyle = (pathLocatingProp)((int)PathLocatingStyle + 1); } }
public float GetHeuristic(int x, int y) { HexagonShape goal = new HexagonShape(x, y); return(GetHeuristic(goal)); }
public HexagonShape GetNeighbour(int dir) { HexagonShape neighDir = GetDirection(dir); return(this + neighDir); }
public float GetHeuristic(HexagonShape goal) { return(HexagonAxis.GetHeuristic(this, goal)); }