/** Approximate action delay between two locations. */ public static int heuristic(MapLocation loc1, MapLocation loc2) { int dx = Math.abs(loc1.x - loc2.x); int dy = Math.abs(loc1.y - loc2.y); int min, diff; if (dx > dy) { min = dy; diff = dx - dy; } else { min = dx; diff = dy - dx; } return (min * NORMAL_DIAGONAL + diff * NORMAL_ORTHOGONAL); }
public NoiseTowerBehavior() { for (int i = 7; i >= 0; i--) { int lastdir = (i + 4) % 8; int at = 1; paths[i][0] = currentLocation; int lastcow = 0; for (int j = 1; j < 30; j++) { if (i < 7 && RC.isActive()) { try { makeSomeNoise(); } catch (GameActionException e1) { e1.printStackTrace(); } } int k = lastdir + 2; k %= 8; int bestscore = -1; MapLocation bestplace = currentLocation; while (k != (lastdir + 6) % 8) { int score = Math.abs(i - k); if (score < 4) score = 8 - score; score *= score * score; MapLocation here = paths[i][j - 1].add(directions[k]); double cows; try { cows = Utils.COW_GROWTH[here.x][here.y]; if (cows > 0) lastcow = j; score = (here.add(directions[k])).distanceSquaredTo(currentLocation) > 300 || RC.senseTerrainTile(here) == TerrainTile.VOID ? -10 : cows == 0.0 ? 30 : score + (int) (cows) * 0 + 30; } catch (Exception e) { score = -2; } if (score > bestscore) { bestscore = score; bestplace = here; } k++; if (k == 8) k = 0; } if (!bestplace.equals(currentLocation)) { paths[i][j] = bestplace; } else { break; } } pathat[i] = lastcow; while (paths[i][pathat[i]] == null && pathat[i] > 0) pathat[i]--; if (lastcow < 29) lastcow++; } skip[0] = false; double[] d = new double[8]; int[] dist = new int[8]; MapLocation[] toconsider = new MapLocation[8]; for (int i = 7; i >= 0; i--) { if (pathat[i] == 0) { d[i] = 0; dist[i] = 0; } else { toconsider[i] = paths[i][pathat[i] - 1]; d[i] = Math.atan2(toconsider[i].y - curY, toconsider[i].x - curX); dist[i] = currentLocation.distanceSquaredTo(toconsider[i]); } } for (int i = 7; i >= 0; i--) { if (dist[i] == 0) skip[i] = true; else { for (int j = i - 1; j >= 0; j--) { if (i == j || dist[j] == 0) continue; if (Math.abs(d[i] - d[j]) < 0.4 && pathbetween(toconsider[i], toconsider[j])) { if (dist[i] < dist[j]) skip[i] = true; else skip[j] = true; } } } } }
public static double atan3(int a, int b) { if (a == 0 && b == 0) return 20.0; return Math.atan2(a, b); }