private Set<Pos> possiblesMovesAroundASpecificPosition(Pos pos) { HashSet<Pos> listOfCellAroundASpecificPosition = new HashSet<Pos>(); int x = pos.getXCoord(), y = pos.getYCoord(); // We must check if positions around the first one aren't out of the // grid and are navigable. if (x + 1 < this.gridWidth && getCellAtSpecificsCoords(x + 1, y).getCellSurface().canWalkOnSurface() && !listOfCellAroundASpecificPosition.contains(pos.newPos(1, 0))) { listOfCellAroundASpecificPosition.add(pos.newPos(1, 0)); } if (x - 1 >= 0 && getCellAtSpecificsCoords(x - 1, y).getCellSurface().canWalkOnSurface() && !listOfCellAroundASpecificPosition.contains(pos.newPos(-1, 0))) { listOfCellAroundASpecificPosition.add(pos.newPos(-1, 0)); } if (y + 1 < this.gridHeight && getCellAtSpecificsCoords(x, y + 1).getCellSurface().canWalkOnSurface() && !listOfCellAroundASpecificPosition.contains(pos.newPos(0, 1))) { listOfCellAroundASpecificPosition.add(pos.newPos(0, 1)); } if (y - 1 >= 0 && getCellAtSpecificsCoords(x, y - 1).getCellSurface().canWalkOnSurface() && !listOfCellAroundASpecificPosition.contains(pos.newPos(0, -1))) { listOfCellAroundASpecificPosition.add(pos.newPos(0, -1)); } return listOfCellAroundASpecificPosition; }
private List<Pos> cellsAroundACell(Pos unitPosition) { List<Pos> list = new LinkedList<Pos>(); if (unitPosition.getXCoord() + 1 <= this.gridWidth && getCellAtASpecificPos(unitPosition.newPos(1, 0)).getCellSurface().canWalkOnSurface()) { list.add(unitPosition.newPos(1, 0)); } if (unitPosition.getXCoord() - 1 >= 0 && getCellAtASpecificPos(unitPosition.newPos(-1, 0)).getCellSurface().canWalkOnSurface()) { list.add(unitPosition.newPos(-1, 0)); } if (unitPosition.getYCoord() + 1 <= this.gridHeight && getCellAtASpecificPos(unitPosition.newPos(0, 1)).getCellSurface().canWalkOnSurface()) { list.add(unitPosition.newPos(0, 1)); } if (unitPosition.getYCoord() - 1 >= 0 && getCellAtASpecificPos(unitPosition.newPos(0, -1)).getCellSurface().canWalkOnSurface()) { list.add(unitPosition.newPos(0, -1)); } return list; }
public Cell getCellAtASpecificPos(Pos pos) { return grid[pos.getXCoord()][pos.getYCoord()]; }
private List<Pos> possiblesAttackCellsAroundASpecificPosition(Pos unitPosition) { List<Pos> listOfPos = new LinkedList<Pos>(); List<Pos> listOfPosInRange = new LinkedList<Pos>(); int coordXErrorValue, coordYErrorValue, errorValue, counter = 0; listOfPosInRange = cellInRangeOfAttack(unitPosition); for (int index = 0; index < listOfPosInRange.size(); index++) { for (int index2 = index + 1; index2 < listOfPosInRange.size(); index2++) { if (listOfPosInRange.get(index2).equals(listOfPosInRange.get(index))) listOfPosInRange.remove(index2); } } // The following algorithm is pretty hard to understand and to explain. // If you want understand it : // http://fr.wikipedia.org/wiki/Algorithme_de_trac%C3%A9_de_segment_de_Bresenham // I had to set it, because it wasn't able to work for every situation. while (counter < listOfPosInRange.size()) { int unitXCoord = unitPosition.getXCoord(), unitYCoord = unitPosition.getYCoord(); int cellXCoord = listOfPosInRange.get(counter).getXCoord(), cellYCoord = listOfPosInRange.get(counter).getYCoord(); // Searching for case according to the X coordinates if // |unitXCoord-cellXCoord|>|unitYCorod-cellY|. if (Math.abs(unitXCoord - cellXCoord) > Math.abs(unitYCoord - cellYCoord)) { errorValue = Math.abs(unitXCoord - cellXCoord); coordXErrorValue = errorValue * 2; coordYErrorValue = Math.abs(unitYCoord - cellYCoord) * 2; // To avoid sign problems, we check if x2>=x1 if (cellXCoord >= unitXCoord) { while (unitXCoord <= cellXCoord && getCellAtSpecificsCoords(unitXCoord, unitYCoord) .getCellSurface() .canFireThrough()) { listOfPos.add(new Pos(unitXCoord, unitYCoord)); unitXCoord++; errorValue -= coordYErrorValue; if (errorValue <= 0 && unitXCoord <= this.gridHeight) { if (errorValue == 0 && getCellAtSpecificsCoords(cellXCoord, cellYCoord) .getCellSurface() .canFireThrough()) listOfPos.add(new Pos(unitXCoord, unitYCoord)); unitYCoord++; errorValue += coordXErrorValue; } if (unitYCoord > this.gridWidth || unitXCoord > this.gridHeight) break; } } else { while (cellXCoord <= unitXCoord && getCellAtSpecificsCoords(unitXCoord, unitYCoord) .getCellSurface() .canFireThrough()) { listOfPos.add(new Pos(unitXCoord, unitYCoord)); unitXCoord--; errorValue -= coordYErrorValue; if (errorValue <= 0 && unitXCoord >= 0) { if (errorValue == 0 && getCellAtSpecificsCoords(unitXCoord, unitYCoord) .getCellSurface() .canFireThrough()) listOfPos.add(new Pos(unitXCoord, unitYCoord)); unitYCoord--; errorValue += coordXErrorValue; } if (unitYCoord < 0 || unitXCoord < 0) break; } } } // Else, do according to the Y coordinate else { errorValue = Math.abs(unitYCoord - cellYCoord); coordXErrorValue = errorValue * 2; coordYErrorValue = Math.abs(unitXCoord - cellXCoord) * 2; // To avoid sign problems we check if y2>=y1 if (cellYCoord >= unitYCoord) { while (unitYCoord <= cellYCoord && getCellAtSpecificsCoords(unitXCoord, unitYCoord) .getCellSurface() .canFireThrough()) { listOfPos.add(new Pos(unitXCoord, unitYCoord)); unitYCoord++; errorValue -= coordYErrorValue; if (errorValue <= 0 && unitYCoord <= this.gridWidth) { if (errorValue == 0 && getCellAtSpecificsCoords(cellXCoord, cellYCoord) .getCellSurface() .canFireThrough()) listOfPos.add(new Pos(unitXCoord, unitYCoord)); unitXCoord++; errorValue += coordXErrorValue; } if (unitXCoord > this.gridHeight || unitYCoord > this.gridWidth) break; } } else { while (cellYCoord <= unitYCoord && getCellAtSpecificsCoords(unitXCoord, unitYCoord) .getCellSurface() .canFireThrough()) { listOfPos.add(new Pos(unitXCoord, unitYCoord)); unitYCoord--; errorValue -= coordYErrorValue; if (errorValue <= 0 && unitYCoord >= 0) { if (errorValue == 0 && getCellAtSpecificsCoords(unitXCoord, unitYCoord) .getCellSurface() .canFireThrough()) { listOfPos.add(new Pos(unitXCoord, unitYCoord)); } unitXCoord--; errorValue += coordXErrorValue; } if (unitXCoord < 0 || unitYCoord < 0) break; } } } counter++; } return listOfPos; }