package biz.pavonis.hexameter.internal.impl;

import static java.lang.Math.abs;
import static java.lang.Math.max;
import static java.lang.Math.min;

import java.util.HashSet;
import java.util.Set;

import biz.pavonis.hexameter.api.Hexagon;
import biz.pavonis.hexameter.api.HexagonalGrid;
import biz.pavonis.hexameter.api.HexagonalGridCalculator;

public final class HexagonalGridCalculatorImpl implements
        HexagonalGridCalculator {

    private final HexagonalGrid hexagonalGrid;

    public HexagonalGridCalculatorImpl(HexagonalGrid hexagonalGrid) {
        this.hexagonalGrid = hexagonalGrid;
    }

    public int calculateDistanceBetween(Hexagon hex0, Hexagon hex1) {
        double absX = abs(hex0.getGridX() - hex1.getGridX());
        double absY = abs(hex0.getGridY() - hex1.getGridY());
        double absZ = abs(hex0.getGridZ() - hex1.getGridZ());
        return (int) max(max(absX, absY), absZ);
    }

    public Set<Hexagon> calculateMovementRangeFrom(Hexagon hexagon, int distance) {
        Set<Hexagon> ret = new HashSet<Hexagon>();
        for (int x = -distance; x <= distance; x++) {
            for (int y = max(-distance, -x - distance); y <= min(distance, -x
                    + distance); y++) {
                int z = -x - y;
                ret.add(hexagonalGrid.getByGridCoordinate(hexagon.getGridX()
                        + x, hexagon.getGridZ() + z));
            }
        }
        return ret;
    }
}

@@ -914,24 +914,25 @@
 %0A    %7D%0A%0A    
+%09
 public Set%3CH
@@ -999,24 +999,18 @@
 ance) %7B%0A
-        
+%09%09
 Set%3CHexa
@@ -1044,24 +1044,18 @@
 gon%3E();%0A
-        
+%09%09
 for (int
@@ -1092,28 +1092,19 @@
  x++) %7B%0A
-            
+%09%09%09
 for (int
@@ -1160,36 +1160,16 @@
 ance, -x
-%0A                   
  + dista
@@ -1185,63 +1185,113 @@
 ) %7B%0A
-                int z = -x - y;%0A                ret.add
+%09%09%09%09int z = -x - y;%0A%09%09%09%09int tmpX = hexagon.getGridX() + x;%0A%09%09%09%09int tmpZ = hexagon.getGridZ() + z;%0A%09%09%09%09if 
 (hex
@@ -1305,17 +1305,16 @@
 rid.
-getByGrid
+contains
 Coor
@@ -1324,63 +1324,49 @@
 ate(
-hexagon.getGridX()%0A                        + x,
+tmpX, tmpZ)) %7B%0A%09%09%09%09%09Hexagon hex =
  hexagon
 .get
@@ -1365,59 +1365,84 @@
 agon
+alGrid
 .get
+By
 Grid
-Z() + z));%0A            %7D%0A        %7D%0A        
+Coordinate(tmpX, tmpZ);%0A%09%09%09%09%09ret.add(hex);%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A%09%09
 retu
@@ -1449,16 +1449,13 @@
 rn ret;%0A
-    
+%09
 %7D%0A%7D%0A

Update HexagonalGridCalculatorImpl.java