Advertisement
Dev_S

Untitled

Jan 23rd, 2021
1,157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.22 KB | None | 0 0
  1. package net.devtech.orecentration.noise;
  2.  
  3. public class RegionNoise {
  4.     public static final int ORE_SCALE_BITS = 4;
  5.     public static final int ORE_SCALE = 1 << ORE_SCALE_BITS;
  6.     public static final int MASK = (1 << ORE_SCALE_BITS) - 1;
  7.     public static final int INVERSE_MASK = ~MASK;
  8.     private static final long multiplier = 0x5DEECE66DL;
  9.     private static final long addend = 0xBL;
  10.     private static final long mask = (1L << 48) - 1;
  11.  
  12.     /**
  13.      * the way this works, it splits up the world into 16x16x16 'cells', each 'cell' has a point inside it. When you generate a point, you check a
  14.      * 3x3x3 area of cells centered around the point to find the closest point. Whichever cell has the closest point, their seed is used to determine
  15.      * what the current 'type' (some int) is
  16.      */
  17.     public static void get(MutableResult result, int x, int y, int z) {
  18.         // find closest point
  19.         int minDistance = Integer.MAX_VALUE;
  20.         long currentSeed = 0;
  21.  
  22.         int orx = x & INVERSE_MASK, ory = y & INVERSE_MASK, orz = z & INVERSE_MASK;
  23.         for (int ox = -ORE_SCALE; ox <= ORE_SCALE; ox += ORE_SCALE) {
  24.             for (int oy = -ORE_SCALE; oy <= ORE_SCALE; oy+=ORE_SCALE) {
  25.                 for (int oz = -ORE_SCALE; oz <= ORE_SCALE; oz+=ORE_SCALE) {
  26.                     int sx = orx + ox, sy = ory + oy, sz = orz + oz;
  27.  
  28.                     // find point in octant
  29.                     long seed = seed(sx, sy, sz);
  30.                     sx += next(seed) & MASK;
  31.                     seed *= 181783497276652981L;
  32.                     sy += next(seed) & MASK;
  33.                     seed *= 181783497276652981L;
  34.                     sz += next(seed) & MASK;
  35.  
  36.                     int dx = sx - x, dy = sy - y, dz = sz - z;
  37.                     int distance = dx * dx + dy * dy + dz * dz;
  38.                     if (distance < minDistance) {
  39.                         minDistance = distance;
  40.                         currentSeed = seed * 181783497276652981L;
  41.                     }
  42.                 }
  43.             }
  44.         }
  45.  
  46.         result.value = next(currentSeed);
  47.         result.distance = minDistance;
  48.     }
  49.  
  50.     private static long seed(int sx, int sy, int sz) {
  51.         return ((long) sx << 32 | sy & 0xffffffffL) ^ (long) sz << 12;
  52.     }
  53.  
  54.     private static int next(long seed) {
  55.         return (int) (((seed * multiplier + addend) & mask) >>> 16);
  56.     }
  57.  
  58.     public static class MutableResult {
  59.         /**
  60.          * the squared distance to the nearest 'cell point'
  61.          */
  62.         public int distance;
  63.  
  64.         /**
  65.          * the value of that cell point
  66.          */
  67.         public int value;
  68.     }
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement