protected void createGrids() { emptySpaces.clear(); neighbors = new IntGrid2D(gridWidth, gridHeight, 0); int[][] g = neighbors.field; for (int x = 0; x < gridWidth; x++) for (int y = 0; y < gridHeight; y++) { double d = random.nextDouble(); if (d < redProbability) g[x][y] = RED; else if (d < redProbability + blueProbability) g[x][y] = BLUE; else if (d < redProbability + blueProbability + emptyProbability) { g[x][y] = EMPTY; emptySpaces.add(new Int2D(x, y)); } else g[x][y] = UNAVAILABLE; } }
/** * For each <xPos,yPos,zPos> location, puts all such objects into the result bag. Returns the * result bag. If the provided result bag is null, one will be created and returned. */ public Bag getObjectsAtLocations( final IntBag xPos, final IntBag yPos, final IntBag zPos, Bag result) { if (result == null) result = new Bag(); else result.clear(); final int len = xPos.numObjs; final int[] xs = xPos.objs; final int[] ys = yPos.objs; final int[] zs = zPos.objs; for (int i = 0; i < len; i++) { // a little efficiency: add if we're 1, addAll if we're > 1, // do nothing if we're 0 Bag temp = getObjectsAtLocation(xs[i], ys[i], zs[i]); if (temp != null) { int n = temp.numObjs; if (n == 1) result.add(temp.objs[0]); else if (n > 1) result.addAll(temp); } } return result; }
private static void updateTargets(IntGrid2D baseMapGrid, int scale, Int2D zeroPos) { if (targets == null) { targets = new Bag(); } else { targets.clear(); } java.util.HashMap<Integer, java.util.HashSet<Integer>> starget = new java.util.HashMap<Integer, java.util.HashSet<Integer>>(); for (int i = 0; i < baseMapGrid.getWidth(); ++i) { for (int j = 0; j < baseMapGrid.getHeight(); ++j) { int val = baseMapGrid.get(i, j); if (val == 0) { Int2D locPos = posToLocalMapPos(new Int2D(i, j), scale, zeroPos); if (!starget.containsKey(locPos.x)) { starget.put(locPos.x, new java.util.HashSet<Integer>()); } boolean ok = starget.get(locPos.x).add(locPos.y); if (ok) { targets.add(locPos); } } } } }