public class Simulation {
Cell[][] map;
int width;
int height;
public void createCells() throws IOException{
int[][] grid = ImageHandler.loadImage("heightmap.png");
width = grid.length;
height = grid[0].length;
map = new Cell[width][height];
System.out.println("Creating cells...");
for(int x = 0; x < width; ++x){
for(int y = 0; y < height; ++y){
map[x][y] = new Cell();
map[x][y].setElevation(grid[x][y]);
if(grid[x][y] == 0){
map[x][y].setAbsorptionPotential(0);
map[x][y].addRain(1000);
}
int numNeighbours;
if((x == 0 && y == 0) || (x == 0 && y == height-1) || (x == width - 1 && y == height - 1) || (x == width - 1 && y == 0)){
numNeighbours = 3;
}
else if((x == 0) || (x == width - 1) || (y == 0) || (y == height - 1)){
numNeighbours = 5;
}
else {
numNeighbours = 8;
}
map[x][y].setNumNeighbours(numNeighbours);
}
}
System.out.println("Connecting cells...");
for(int x = 0; x < width; ++x){
for(int y = 0; y < height; ++y){
if(x == 0 && y == 0){
map[x][y].connectNeighbour(map[x][y+1], 0);
map[x][y].connectNeighbour(map[x+1][y], 1);
map[x][y].connectNeighbour(map[x+1][y+1], 2);
}
else if(x == 0 && y == height - 1){
map[x][y].connectNeighbour(map[x][y-1], 0);
map[x][y].connectNeighbour(map[x+1][y], 1);
map[x][y].connectNeighbour(map[x+1][y-1], 2);
}
else if(x == width - 1 && y == 0){
map[x][y].connectNeighbour(map[x-1][y], 0);
map[x][y].connectNeighbour(map[x][y+1], 1);
map[x][y].connectNeighbour(map[x-1][y+1], 2);
}
else if(x == width - 1 && y == height - 1){
map[x][y].connectNeighbour(map[x-1][y], 0);
map[x][y].connectNeighbour(map[x][y-1], 1);
map[x][y].connectNeighbour(map[x-1][y-1], 2);
}
else if(x == 0){
map[x][y].connectNeighbour(map[x][y-1], 0);
map[x][y].connectNeighbour(map[x+1][y-1], 1);
map[x][y].connectNeighbour(map[x+1][y], 2);
map[x][y].connectNeighbour(map[x+1][y+1], 3);
map[x][y].connectNeighbour(map[x][y+1], 4);
}
else if(x == width - 1){
map[x][y].connectNeighbour(map[x][y-1], 0);
map[x][y].connectNeighbour(map[x+-1][y-1], 1);
map[x][y].connectNeighbour(map[x-1][y], 2);
map[x][y].connectNeighbour(map[x-1][y+1], 3);
map[x][y].connectNeighbour(map[x][y+1], 4);
}
else if(y == 0){
map[x][y].connectNeighbour(map[x-1][y], 0);
map[x][y].connectNeighbour(map[x-1][y+1], 1);
map[x][y].connectNeighbour(map[x][y+1], 2);
map[x][y].connectNeighbour(map[x+1][y+1], 3);
map[x][y].connectNeighbour(map[x+1][y], 4);
}
else if(y == height - 1){
map[x][y].connectNeighbour(map[x-1][y], 0);
map[x][y].connectNeighbour(map[x-1][y-1], 1);
map[x][y].connectNeighbour(map[x][y-1], 2);
map[x][y].connectNeighbour(map[x+1][y-1], 3);
map[x][y].connectNeighbour(map[x+1][y], 4);
}
else {
map[x][y].connectNeighbour(map[x-1][y], 0);
map[x][y].connectNeighbour(map[x-1][y-1], 1);
map[x][y].connectNeighbour(map[x][y-1], 2);
map[x][y].connectNeighbour(map[x+1][y-1], 3);
map[x][y].connectNeighbour(map[x+1][y], 4);
map[x][y].connectNeighbour(map[x+1][y+1], 5);
map[x][y].connectNeighbour(map[x][y+1], 6);
map[x][y].connectNeighbour(map[x-1][y+1], 7);
}
}
}
}
}