Skip to content

Instantly share code, notes, and snippets.

@ajweeks
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajweeks/ddbe5619ab93291dc37a to your computer and use it in GitHub Desktop.
Save ajweeks/ddbe5619ab93291dc37a to your computer and use it in GitHub Desktop.
private void updatePower() {
for (int y = 0; y < grid.height; y++) {
for (int x = 0; x < grid.width; x++) {
Tile curTile = getTileAt(x, y);
Tile north = getTileAt(x, y - 1);
Tile south = getTileAt(x, y + 1);
Tile east = getTileAt(x + 1, y);
Tile west = getTileAt(x - 1, y);
//First pass
switch (curTile.type) {
case BLANK:
case NULL:
continue;
case INVERTER:
switch (curTile.direction) {
case NORTH:
if (south.type != TileType.NULL) curTile.powered = south.powered;
break;
case EAST:
if (west.type != TileType.NULL) curTile.powered = west.powered;
break;
case SOUTH:
if (north.type != TileType.NULL) curTile.powered = north.powered;
break;
case WEST:
if (east.type != TileType.NULL) curTile.powered = east.powered;
break;
default:
new IllegalStateException("Inverter tile has invalid direction: " + curTile.direction + " @ x: " + x + ",y: " + y)
.printStackTrace();
}
break;
case WIRE:
curTile.powered = false;
break;
case POWER: //Power tiles don't need updating
break;
}
//Second pass
if ((curTile.type == TileType.POWER && curTile.powered) || (curTile.type == TileType.INVERTER && !curTile.powered)) {
floodfillAllExcept(x, y, Direction.NONE); //Start flood filling at power sources which are giving out power
}
grid.tiles[y * grid.width + x] = curTile.copy();
}
}
}
/** Calls floodfill on all this tile's neighbours (except the one towards direction comingFrom). <br/> If you want to update all neighbours, pass Direction.NONE */
private void floodfillAllExcept(int x, int y, Direction comingFrom) {
if (comingFrom != Direction.NORTH) floodfill(x, y - 1, Direction.SOUTH);
if (comingFrom != Direction.EAST) floodfill(x + 1, y, Direction.WEST);
if (comingFrom != Direction.SOUTH) floodfill(x, y + 1, Direction.NORTH);
if (comingFrom != Direction.WEST) floodfill(x - 1, y, Direction.EAST);
}
private void floodfill(int x, int y, Direction comingFrom) {
Tile t = getTileAt(x, y);
if (t.type != TileType.WIRE) return; //only update wires
if (grid.tiles[y * grid.width + x].powered) return;
grid.tiles[y * grid.width + x].powered = true;
checkNorth(t, x, y, comingFrom);
checkEast(t, x, y, comingFrom);
checkSouth(t, x, y, comingFrom);
checkWest(t, x, y, comingFrom);
}
private void checkNorth(Tile t, int x, int y, Direction comingFrom) {
if (t.neighbours[0] && comingFrom != Direction.NORTH) {
if (getTileAt(x, y - 1).type != TileType.NULL) {
grid.tiles[(y - 1) * grid.width + x].powered = true;
}
floodfillAllExcept(x, y - 1, Direction.SOUTH);
}
}
private void checkEast(Tile t, int x, int y, Direction comingFrom) {
if (t.neighbours[1] && comingFrom != Direction.EAST) {
if (getTileAt(x + 1, y).type != TileType.NULL) {
grid.tiles[y * grid.width + x + 1].powered = true;
}
floodfillAllExcept(x + 1, y, Direction.WEST);
}
}
private void checkSouth(Tile t, int x, int y, Direction comingFrom) {
if (t.neighbours[2] && comingFrom != Direction.SOUTH) {
if (getTileAt(x, y + 1).type != TileType.NULL) {
grid.tiles[(y + 1) * grid.width + x].powered = true;
}
floodfillAllExcept(x, y + 1, Direction.NORTH);
}
}
private void checkWest(Tile t, int x, int y, Direction comingFrom) {
if (t.neighbours[3] && comingFrom != Direction.WEST) {
if (getTileAt(x - 1, y).type != TileType.NULL) {
grid.tiles[y * grid.width + x - 1].powered = true;
}
floodfillAllExcept(x - 1, y, Direction.EAST);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment