Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.57 KB | None | 0 0
  1. package fr.bretzel.challenge.thechallenge;
  2.  
  3. import org.bukkit.util.noise.NoiseGenerator;
  4.  
  5. import java.util.Random;
  6.  
  7. public class DiamondNoise extends NoiseGenerator
  8. {
  9.     private Random random;
  10.  
  11.     private int[][] DATA;
  12.  
  13.     public DiamondNoise()
  14.     {
  15.         this.random = new Random();
  16.  
  17.         int size = 512;
  18.  
  19.         DATA = new int[size][size];
  20.  
  21.         DATA[0][0] = random.nextInt(256);
  22.         DATA[0][size - 1] = random.nextInt(256);
  23.         DATA[size - 1][size - 1] = random.nextInt(256);
  24.         DATA[size - 1][0] = random.nextInt(256);
  25.  
  26.         int i = size - 1;
  27.  
  28.         while (i > 1)
  29.         {
  30.             int id = i / 2;
  31.  
  32.             for (int x = id; x <= size; x += i)
  33.             {
  34.                 for (int y = id; y <= size; y += i)
  35.                 {
  36.                     int moyenne = (DATA[x - id][y - id] +
  37.                             DATA[x - id][y + id] +
  38.                             DATA[x + id][y + id] +
  39.                             DATA[x + id][y - id]) / 4;
  40.  
  41.                     DATA[x][y] = moyenne + random.nextInt(2 * id) + id;
  42.                 }
  43.             }
  44.  
  45.             int decalage = 0;
  46.  
  47.             for (int x = 0; x <= size - 1; x += id)
  48.             {
  49.                 if (x % i == 0)
  50.                     decalage = id;
  51.                 else decalage = 0;
  52.  
  53.                 for (int y = decalage; y <= decalage; y++)
  54.                 {
  55.                     int somme = 0;
  56.                     int n = 0;
  57.  
  58.                     if (x >= id)
  59.                     {
  60.                         somme = somme + DATA[x - id][y];
  61.                         n++;
  62.                     }
  63.  
  64.                     if ((x + id) < size)
  65.                     {
  66.                         somme = somme + DATA[x + id][y];
  67.                         n++;
  68.                     }
  69.  
  70.                     if (y >= id)
  71.                     {
  72.                         somme = (somme + DATA[x][y - id]);
  73.                         n++;
  74.                     }
  75.  
  76.                     if ((y + id) < size)
  77.                     {
  78.                         somme = somme + DATA[x][y + id];
  79.                         n++;
  80.                     }
  81.  
  82.                     DATA[x][y] = (somme / n + random.nextInt(2 * id) - id);
  83.                 }
  84.             }
  85.             i = id;
  86.         }
  87.     }
  88.  
  89.     @Override
  90.     public double noise(double x, double y)
  91.     {
  92.         if (DATA.length <= 0)
  93.             return 0;
  94.         return DATA[(int) x][(int) y];
  95.     }
  96.  
  97.     @Override
  98.     public double noise(double x, double y, double z)
  99.     {
  100.         return noise(x, y);
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement