Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Adapted from C source code found at https://www.reddit.com/r/gamedev/comments/3g89el/portable_procedural_land_generation/
- // The C source was licensed under http://creativecommons.org/licenses/by/4.0/
- // and this Java version is made available under the same license.
- edit: above C source was released into the public domain, and this source file is made available under the same terms, without warranty for any use or purpose.
- Java version by Charles Griffiths, August 10, 2015
- This version refers to classes in libgdx (com.badlogic.gdx).
- */
- import java.io.BufferedWriter;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.util.Arrays;
- import java.util.Random;
- import com.badlogic.gdx.graphics.Pixmap;
- import com.badlogic.gdx.graphics.Pixmap.Format;
- public class ValueNoise
- {
- protected Random r;
- public final int width, height;
- private int size;
- private int elevation[][];
- public ValueNoise( int width, int height )
- {
- int i, max = Math.max( width, height );
- this.width = width;
- this.height = height;
- for (i=1; Math.pow( 2, i )+1 <= max; i++);
- size = (int) Math.pow( 2, i );
- }
- public int getElevation( int x, int y )
- {
- if (x >= 0 && x < width && y >= 0 && y < height)
- return elevation[x][y];
- return 0;
- }
- public void generate()
- {
- generate( System.currentTimeMillis() );
- }
- public void generate( long seed )
- {
- int noise[][] = getArray();
- r = new Random( seed );
- elevation = getArray();
- for (int a=1, t=2; t<=size/4; a*=2, t*=2)
- octave( noise, a, t );
- }
- private int[][] getArray()
- {
- int ia[][] = new int[size+1][size+1];
- for (int i=0; i<=size; i++)
- Arrays.fill( ia[i], 0 );
- return ia;
- }
- protected void octave( int noise[][], int a, int t )
- {
- for (int i=0; i<size; i+=t)
- for (int j=0; j<size; j+=t)
- noise[i][j] = r.nextInt( a ) - a/2;
- for (int i=0; i<size; i+=t)
- for (int j=0; j<size; j+=t)
- binterp( noise, i, j, t );
- for (int i=0; i<=size; i++)
- for (int j=0; j<=size; j++)
- elevation[i][j] += noise[i][j];
- }
- protected void binterp( int noise[][], int i0, int j0, int t )
- {
- int a = t*t;
- for (int i=i0; i<=i0+t; i++)
- for (int j=j0; j<=j0+t; j++)
- {
- int b = (int) (noise[ i0][ j0] * (t-j+j0) * (t-i+i0));
- int c = (int) (noise[ i0][t+j0] * ( j-j0) * (t-i+i0));
- int d = (int) (noise[t+i0][ j0] * (t-j+j0) * ( i-i0));
- int e = (int) (noise[t+i0][t+j0] * ( j-j0) * ( i-i0));
- noise[i][j]=(b+c+d+e)/a;
- }
- }
- public void save( String filename )
- {
- try
- {
- FileOutputStream fos = new FileOutputStream( filename );
- writeAscii( fos );
- fos.close();
- }
- catch (FileNotFoundException e)
- {
- e.printStackTrace();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- private void writeAscii( OutputStream os ) throws IOException
- {
- BufferedWriter bw = new BufferedWriter( new OutputStreamWriter( os ));
- for (int i=0; i<height; i++)
- {
- StringBuffer sb = new StringBuffer();
- for (int j=0; j<width; j++)
- {
- if (j > 0)
- sb.append( "\t" );
- sb.append( elevation[i][j] );
- }
- sb.append( System.lineSeparator() );
- bw.write( sb.toString() );
- }
- bw.flush();
- }
- public Pixmap getPixmap()
- {
- Pixmap p = new Pixmap( width, height, Format.RGB888 );
- int max = 0, min = 0;
- for (int i=0; i<height; i++)
- for (int j=0; j<width; j++)
- {
- int z = elevation[i][j];
- if (z > max)
- max = z;
- else if (z < min)
- min = z;
- }
- for (int i=0; i<height; i++)
- for (int j=0; j<width; j++)
- {
- float value = (float)(elevation[i][j] - min) / (max-min+1);
- // greyscale
- p.setColor( value, value, value, 1 );
- p.drawPixel( j, i );
- }
- return p;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement