Advertisement
charlesg

ValueNoise

Aug 9th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 4.13 KB | None | 0 0
  1. /*
  2. Adapted from C source code found at https://www.reddit.com/r/gamedev/comments/3g89el/portable_procedural_land_generation/
  3. // The C source was licensed under http://creativecommons.org/licenses/by/4.0/
  4. // and this Java version is made available under the same license.
  5. 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.
  6.  
  7. Java version by Charles Griffiths, August 10, 2015
  8. This version refers to classes in libgdx (com.badlogic.gdx).
  9. */
  10.  
  11. import java.io.BufferedWriter;
  12. import java.io.FileNotFoundException;
  13. import java.io.FileOutputStream;
  14. import java.io.IOException;
  15. import java.io.OutputStream;
  16. import java.io.OutputStreamWriter;
  17. import java.util.Arrays;
  18. import java.util.Random;
  19.  
  20. import com.badlogic.gdx.graphics.Pixmap;
  21. import com.badlogic.gdx.graphics.Pixmap.Format;
  22.  
  23.  
  24.  
  25. public class ValueNoise
  26. {
  27. protected Random r;
  28. public final int width, height;
  29. private int size;
  30. private int elevation[][];
  31.  
  32.  
  33.   public ValueNoise( int width, int height )
  34.   {
  35.   int i, max = Math.max( width, height );
  36.  
  37.     this.width = width;
  38.     this.height = height;
  39.     for (i=1; Math.pow( 2, i )+1 <= max; i++);
  40.     size = (int) Math.pow( 2, i );
  41.   }
  42.  
  43.  
  44.   public int getElevation( int x, int y )
  45.   {
  46.     if (x >= 0 && x < width && y >= 0 && y < height)
  47.       return elevation[x][y];
  48.  
  49.     return 0;
  50.   }
  51.  
  52.  
  53.   public void generate()
  54.   {
  55.     generate( System.currentTimeMillis() );
  56.   }
  57.  
  58.   public void generate( long seed )
  59.   {
  60.   int noise[][] = getArray();
  61.  
  62.     r = new Random( seed );
  63.     elevation = getArray();
  64.    
  65.     for (int a=1, t=2; t<=size/4; a*=2, t*=2)
  66.       octave( noise, a, t );
  67.   }
  68.  
  69.  
  70.   private int[][] getArray()
  71.   {
  72.   int ia[][] = new int[size+1][size+1];
  73.  
  74.     for (int i=0; i<=size; i++)
  75.       Arrays.fill( ia[i], 0 );
  76.  
  77.     return ia;
  78.   }
  79.  
  80.  
  81.   protected void octave( int noise[][], int a, int t )
  82.   {
  83.     for (int i=0; i<size; i+=t)
  84.       for (int j=0; j<size; j+=t)
  85.         noise[i][j] = r.nextInt( a ) - a/2;
  86.  
  87.     for (int i=0; i<size; i+=t)
  88.       for (int j=0; j<size; j+=t)
  89.         binterp( noise, i, j, t );
  90.  
  91.     for (int i=0; i<=size; i++)
  92.       for (int j=0; j<=size; j++)
  93.         elevation[i][j] += noise[i][j];
  94.   }
  95.  
  96.  
  97.   protected void binterp( int noise[][], int i0, int j0, int t )
  98.   {
  99.   int a = t*t;
  100.  
  101.     for (int i=i0; i<=i0+t; i++)
  102.       for (int j=j0; j<=j0+t; j++)
  103.       {
  104.       int b = (int) (noise[  i0][  j0] * (t-j+j0) * (t-i+i0));
  105.       int c = (int) (noise[  i0][t+j0] * (  j-j0) * (t-i+i0));
  106.       int d = (int) (noise[t+i0][  j0] * (t-j+j0) * (  i-i0));
  107.       int e = (int) (noise[t+i0][t+j0] * (  j-j0) * (  i-i0));
  108.  
  109.         noise[i][j]=(b+c+d+e)/a;
  110.     }
  111.   }
  112.  
  113.  
  114.   public void save( String filename )
  115.   {
  116.     try
  117.     {
  118.     FileOutputStream fos = new FileOutputStream( filename );
  119.      
  120.       writeAscii( fos );
  121.       fos.close();
  122.     }
  123.     catch (FileNotFoundException e)
  124.     {
  125.       e.printStackTrace();
  126.     }
  127.     catch (IOException e)
  128.     {
  129.       e.printStackTrace();
  130.     }
  131.   }
  132.  
  133.  
  134.   private void writeAscii( OutputStream os ) throws IOException
  135.   {
  136.   BufferedWriter bw = new BufferedWriter( new OutputStreamWriter( os ));
  137.  
  138.     for (int i=0; i<height; i++)
  139.     {
  140.     StringBuffer sb = new StringBuffer();
  141.  
  142.       for (int j=0; j<width; j++)
  143.       {
  144.         if (j > 0)
  145.           sb.append( "\t" );
  146.         sb.append( elevation[i][j] );
  147.       }
  148.       sb.append( System.lineSeparator() );
  149.  
  150.       bw.write( sb.toString() );
  151.     }
  152.  
  153.     bw.flush();
  154.   }
  155.  
  156.  
  157.   public Pixmap getPixmap()
  158.   {
  159.   Pixmap p = new Pixmap( width, height, Format.RGB888 );
  160.   int max = 0, min = 0;
  161.  
  162.     for (int i=0; i<height; i++)
  163.       for (int j=0; j<width; j++)
  164.       {
  165.       int z = elevation[i][j];
  166.  
  167.         if (z > max)
  168.           max = z;
  169.         else if (z < min)
  170.           min = z;
  171.       }
  172.  
  173.     for (int i=0; i<height; i++)
  174.       for (int j=0; j<width; j++)
  175.       {
  176.       float value = (float)(elevation[i][j] - min) / (max-min+1);
  177.  
  178.         // greyscale
  179.         p.setColor( value, value, value, 1 );
  180.         p.drawPixel( j, i );
  181.       }
  182.  
  183.     return p;
  184.   }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement