- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
public static function findNeighbourEmpty(cx:int, cy:int, checkDebris:Boolean = true):Cell {
var cell:Cell = Cell.getCell(cx, cy);
var i:int = 0;
var distance:int = 1;
var n:int = 8;
while(!cell || !cell.accessible || (checkDebris && !isEmptyTile(cell))) {
if (i < n * distance / 4) {
cell = Cell.getCell(cx + Math.min(i, distance), cy + Math.min(distance * 2 - i, distance));
} else if (i < n * distance / 2) {
cell = Cell.getCell(cx + Math.min(distance * 2 - i + n * distance / 4, distance), cy + Math.max(n * distance / 4 - i, -distance));
} else if (i < 3 * n * distance / 4) {
cell = Cell.getCell(cx + Math.max(n * distance / 2 - i, -distance), cy + Math.max(-distance * 2 + i - n * distance / 2, -distance));
} else if (i < n * distance) {
cell = Cell.getCell(cx + Math.max(-distance * 2 + i - 3 * n * distance / 4, -distance), cy + Math.min(i - 3 * n * distance / 4, distance));
} else {
i = 0;
distance++;
if (distance > MAX_NEIGHBOUR_CELL_DISTANCE) {
return findNeighbourEmpty(cx, cy, false);
}
continue;
}
i++;
}
return cell;
}