This code may be difficult to understand but basically I'm just trying to do the diamond square algorithm to make some terrain for a tile based game. I never found an example of this for a tiled game so I painstakingly converted someones midpoint displacement, that was for values between 1.0 & 0.0, to all positive numbers and whole number. I also fixed it for diamond square but im not sure if im doing that right. The 4 points of the diamond are actually 3 but i added the midpoint twice cause I didn't want to wrap. I feel like its not doing all the steps properly and in the pixel mode that i have as default atm shows diamond and square artifacts and even can be seen sometimes from the more zoomed in version too. One thing anyone would notice if they are familiar would be the fact a don't have a starting noise value and having it decrease after each completion (I am having trouble in this area too). For each step it is adding plus or minus 1. At the way bottom of the code is the keyboard handler and it set noise to 4 for now, clears the array, and makes new corner values after pressing spacebar. If someone can see if I did the diamond square wrong and have any tips for generating these numbers for a tile based game that would be awesome. The decimal places were making it difficult for me but I might go back and use em again but just times the height value by 10 or 100 to bump it up to something I may find more usable.
package bttg; import java.awt.*; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.applet.*; @SuppressWarnings("serial") public class BTTG extends Applet implements KeyListener, MouseListener, FocusListener{ short i = 0; int worldSize = 499; //99 <------------------- int[][] map = new int[worldSize+1][worldSize+1]; boolean focussed = false; short rand; double noise = 0; public void init() { addKeyListener(this); } public boolean squareStep(int x, int y, int x1, int y1) //Diamond and Square Steps { if(x1-x<2 && y1-y<2) return false; int sideMidX = (x+x1)/2; int sideMidY = (y+y1)/2; rand = (byte) (Math.random()*noise - (noise/2)); // These values are locked at the moment random()*4 - (4/2); .... creates -1 to 1. 2 -1 wasn't working; Try doing 6 - (6/2) for more bumpy terrain if(map[sideMidX][sideMidY] == 0) map[sideMidX][sideMidY] = Math.abs((map[x][y] + map[x][y1] + map[x1][y] + map[x1][y1])/4 + (int)rand); // Diamond rand = (byte) (Math.random()*noise - (noise/2)); if(map[x][sideMidY] == 0) map[x][sideMidY] = Math.abs((map[x][y1] + map[x][y] + map[sideMidX][sideMidY] + (map[sideMidX][sideMidY])+rand)/4) + (int) rand); // Left side Squares or other way around\/\/\/ // Having these at + 0 noise make really flat terrain which isnt bad either rand = (byte) (Math.random()*noise - (noise/2)); if(map[sideMidX][y] == 0) map[sideMidX][y] = Math.abs((map[x1][y] + map[x][y] + map[sideMidX][sideMidY] + (map[sideMidX][sideMidY])+rand)/4) + (int) rand; // Top Side rand = (byte) (Math.random()*noise - (noise/2)); if(map[sideMidX][y1] == 0) map[sideMidX][y1] = Math.abs((map[x][y1] + map[x1][y1] + map[sideMidX][sideMidY] + (map[sideMidX][sideMidY])+rand)/4) + (int) rand; // Right Side rand = (byte) (Math.random()*noise - (noise/2)); if(map[x1][sideMidY] == 0) map[x1][sideMidY] = Math.abs((map[x1][y] + map[x1][y1] + map[sideMidX][sideMidY] + (map[sideMidX][sideMidY])+rand)/4) + (int) rand; squareStep(x,y,sideMidX,sideMidY); squareStep(sideMidX,y,x1,sideMidY); squareStep(x,sideMidY,sideMidX,y1); squareStep(sideMidX,sideMidY,x1,y1); return true; } public void paint(Graphics g) { int x=0; int y=0; g.setColor(Color.BLACK); g.fillRect(0,0,1920,1080); Font mapFont = new Font ("Monospaced", Font.BOLD, 8); // Not used this is to check the numbers below change to 12 if you want to see values g.setFont(mapFont); int xMap = 0; int yMap = 0; // for numbers change this to 15 while(y<worldSize+1) { Color bum = new Color(map[x][y], map[x][y],map[x][y]); // makes white shaded array values(times each one by 5 if displaying array values) g.setColor(bum); // sets that color g.fillRect(xMap, yMap,1,1); //change this g.draw("" + map[x][y] ,xMap, yMap); to change from pixels to numbers (dont forget to change the others too and also change mapSize to 99 at the top xMap += 1; // for numbers change this to 15 x++; if(x == worldSize+1) { x=0; y++; xMap =0; yMap += 1; // for numbers change this to 15 } } } public void keyPressed(KeyEvent evt) { int key = evt.getKeyCode(); if (key == KeyEvent.VK_SPACE) //Space makes a new map { int x = 0,y = 0; noise = 4; while(y<worldSize+1) { map[x][y] = 0; x++; if(x == worldSize+1) { x=0; y++; } } rand = (short) ((Math.random()*(worldSize/2))); // \/\/\/\/ These generate the starting corners map[0][0] = rand; rand = (short) ((Math.random()*(worldSize/2))); map[worldSize][0] = rand; rand = (short) ((Math.random()*(worldSize/2))); map[0][worldSize] = rand; rand = (short) ((Math.random()*(worldSize/2))); map[worldSize][worldSize] = rand; // rand = (short) ((Math.random()*(worldSize/2))); // Take comments of to also place one midpoint seed // map[worldSize/2][worldSize/2] = rand; squareStep(0,0,worldSize,worldSize); repaint(); } } public void keyReleased(KeyEvent evt) { } public void keyTyped(KeyEvent evt) { char ch = evt.getKeyChar();} public void mousePressed(MouseEvent evt) {requestFocus(); System.out.println("Mouse Works");} public void focusLost(FocusEvent evt) {focussed = false;} public void focusGained(FocusEvent evt) {focussed = true; System.out.println("We Have Focus");} public void mouseEntered(MouseEvent evt) { } public void mouseExited(MouseEvent evt) { } public void mouseReleased(MouseEvent evt) { } public void mouseClicked(MouseEvent evt) { } }