Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.devtech.orecentration.noise;
- public class RegionNoise {
- public static final int ORE_SCALE_BITS = 4;
- public static final int ORE_SCALE = 1 << ORE_SCALE_BITS;
- public static final int MASK = (1 << ORE_SCALE_BITS) - 1;
- public static final int INVERSE_MASK = ~MASK;
- private static final long multiplier = 0x5DEECE66DL;
- private static final long addend = 0xBL;
- private static final long mask = (1L << 48) - 1;
- /**
- * 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
- * 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
- * what the current 'type' (some int) is
- */
- public static void get(MutableResult result, int x, int y, int z) {
- // find closest point
- int minDistance = Integer.MAX_VALUE;
- long currentSeed = 0;
- int orx = x & INVERSE_MASK, ory = y & INVERSE_MASK, orz = z & INVERSE_MASK;
- for (int ox = -ORE_SCALE; ox <= ORE_SCALE; ox += ORE_SCALE) {
- for (int oy = -ORE_SCALE; oy <= ORE_SCALE; oy+=ORE_SCALE) {
- for (int oz = -ORE_SCALE; oz <= ORE_SCALE; oz+=ORE_SCALE) {
- int sx = orx + ox, sy = ory + oy, sz = orz + oz;
- // find point in octant
- long seed = seed(sx, sy, sz);
- sx += next(seed) & MASK;
- seed *= 181783497276652981L;
- sy += next(seed) & MASK;
- seed *= 181783497276652981L;
- sz += next(seed) & MASK;
- int dx = sx - x, dy = sy - y, dz = sz - z;
- int distance = dx * dx + dy * dy + dz * dz;
- if (distance < minDistance) {
- minDistance = distance;
- currentSeed = seed * 181783497276652981L;
- }
- }
- }
- }
- result.value = next(currentSeed);
- result.distance = minDistance;
- }
- private static long seed(int sx, int sy, int sz) {
- return ((long) sx << 32 | sy & 0xffffffffL) ^ (long) sz << 12;
- }
- private static int next(long seed) {
- return (int) (((seed * multiplier + addend) & mask) >>> 16);
- }
- public static class MutableResult {
- /**
- * the squared distance to the nearest 'cell point'
- */
- public int distance;
- /**
- * the value of that cell point
- */
- public int value;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement