Skip to content

Instantly share code, notes, and snippets.

Created November 9, 2012 23:28
Show Gist options
  • Save anonymous/4049004 to your computer and use it in GitHub Desktop.
Save anonymous/4049004 to your computer and use it in GitHub Desktop.
Generator
package main;
import java.util.ArrayList;
import java.util.Random;
public class Generator {
Random random = new Random();
int SIZE;
MapTile[][] world;
ArrayList<Coord> toDo = new ArrayList<Coord>();
public Generator(int size){
SIZE = size;
}
public void start(){
System.out.println("start");
int mid = (SIZE - 1)/2;
//init
world = new MapTile[SIZE][SIZE];
world[0][0] = new MapTile(0,0,random.nextInt(30) - 15);
world[0][SIZE - 1] = new MapTile(0, SIZE - 1, random.nextInt(30) - 15);
world[SIZE - 1][0] = new MapTile(SIZE - 1, 0, random.nextInt(30) - 15);
world[SIZE - 1][SIZE - 1] = new MapTile(SIZE - 1, SIZE - 1, random.nextInt(30) - 15);
//initiate generation
toDo.add(new Coord(mid, mid));
diamond(SIZE);
}
public void regenerate(){
start();
}
public void diamond(int width){
Coord[] toDoArray = toDo.toArray(new Coord[0]);
toDo.clear();
int count = toDoArray.length;
int mid = (width - 1) / 2;
if(width >= 3){
while(count > 0){
int tempx = toDoArray[count - 1].x;
int tempy = toDoArray[count - 1].y;
if(withinBounds(tempx, tempy, world)){
if(world[tempx][tempy] == null){
addToMap(tempx, tempy, (getFromMap(tempx + mid, tempy + mid) + getFromMap(tempx - mid, tempy + mid) + getFromMap(tempx + mid, tempy - mid) + getFromMap(tempx - mid, tempy - mid)/4) + (random.nextDouble() - .5) / 32);
squareFind(tempx, tempy, width);
}
}
count--;
}
square(width);
}
else{
System.out.println("DONE");
}
}
public void diamondFind(int x, int y, int width){
int mid = (width - 1)/2;
toDo.add(new Coord(x + mid,y + mid));
toDo.add(new Coord(x - mid,y + mid));
toDo.add(new Coord(x + mid,y - mid));
toDo.add(new Coord(x - mid,y - mid));
}
public void square(int width){
Coord[] toDoArray = toDo.toArray(new Coord[0]);
toDo.clear();
int count = toDoArray.length;
int mid = (width - 1)/2;
if(width >= 3){
while(count > 0){
int tempx = toDoArray[count - 1].x;
int tempy = toDoArray[count - 1].y;
if(withinBounds(tempx, tempy, world)){
if(world[tempx][tempy] == null){
addToMap(tempx, tempy, (getFromMap(tempx - mid, tempy) + getFromMap(tempx + mid, tempy) + getFromMap(tempx, tempy + mid) + getFromMap(tempx, tempy - mid))/4 + ((random.nextDouble()) - .5) / 32);
diamondFind(tempx, tempy, (width + 1) / 2);
}
}
count--;
}
}
diamond((width + 1) / 2);
}
public void squareFind(int x, int y, int width){
int mid = (width - 1)/2;
toDo.add(new Coord(x + mid,y));
toDo.add(new Coord(x, y + mid));
toDo.add(new Coord(x - mid, y));
toDo.add(new Coord(x, y - mid));
}
public void addToMap(int x, int y, double height){
if(withinBounds(x, y, world)){
world[x][y] = new MapTile(x,y,height);
System.out.println(x + ", " + y + " height: " + height);
}
}
public double getFromMap(int x, int y){
double height;
if(withinBounds(x, y, world)){
//System.out.println(x + ", " + y);
height = world[x][y].height;
}
else{
height = 0;
}
return height;
}
public boolean withinBounds(int x, int y, MapTile[][] world){
if(x >= 0 && x < world.length && y >= 0 && y < world[0].length){
return true;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment