Advertisement
Guest User

Untitled

a guest
Nov 26th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.07 KB | None | 0 0
  1. package cubicchunks.worldgen.generator.custom.builder;
  2.  
  3. import com.google.common.collect.AbstractIterator;
  4.  
  5. import net.minecraft.util.math.MathHelper;
  6. import net.minecraft.util.math.Vec3i;
  7.  
  8. import static java.lang.Math.max;
  9. import static java.lang.Math.min;
  10.  
  11. class ScalingLerpIBuilderIterator extends AbstractIterator<IBuilder.IEntry> {
  12.     // TODO: explain how it works
  13.     private final int minX;
  14.     private final int minY;
  15.     private final int minZ;
  16.     private final int maxX;
  17.     private final int maxY;
  18.     private final int maxZ;
  19.  
  20.     private final double xStep;
  21.     private final double yStep;
  22.     private final double zStep;
  23.  
  24.     private final int minGridX;
  25.     private final int minGridY;
  26.     private final int minGridZ;
  27.     private final int maxGridX;
  28.     private final int maxGridY;
  29.     private final int maxGridZ;
  30.  
  31.     private final int scaleX;
  32.     private final int scaleY;
  33.     private final int scaleZ;
  34.  
  35.     private final IBuilder builder;
  36.  
  37.     private int nextGridX;
  38.     private int nextGridY;
  39.     private int nextGridZ;
  40.  
  41.     private int nextRelX;
  42.     private int nextRelY;
  43.     private int nextRelZ;
  44.  
  45.     private double v000, v001, v010, v011;
  46.     private double vx00, vx01, vx10, vx11;
  47.     private double vxy0, vxy1;
  48.     private double vxyz;
  49.  
  50.     private double dx00, dx01, dx10, dx11;
  51.     private double dxy0, dxy1;
  52.     private double dxyz;
  53.  
  54.     ScalingLerpIBuilderIterator(IBuilder builder, Vec3i start, Vec3i end, Vec3i scale) {
  55.         this.builder = builder;
  56.         this.scaleX = scale.getX();
  57.         this.scaleY = scale.getY();
  58.         this.scaleZ = scale.getZ();
  59.  
  60.         minX = min(start.getX(), end.getX());
  61.         minY = min(start.getY(), end.getY());
  62.         minZ = min(start.getZ(), end.getZ());
  63.         maxX = max(start.getX(), end.getX());
  64.         maxY = max(start.getY(), end.getY());
  65.         maxZ = max(start.getZ(), end.getZ());
  66.  
  67.         minGridX = MathHelper.intFloorDiv(minX, scaleX);
  68.         minGridY = MathHelper.intFloorDiv(minY, scaleY);
  69.         minGridZ = MathHelper.intFloorDiv(minZ, scaleZ);
  70.         maxGridX = MathHelper.intFloorDiv(maxX, scaleX);
  71.         maxGridY = MathHelper.intFloorDiv(maxY, scaleY);
  72.         maxGridZ = MathHelper.intFloorDiv(maxZ, scaleZ);
  73.  
  74.         nextGridX = minGridX;
  75.         nextGridY = minGridY;
  76.         nextGridZ = minGridZ;
  77.  
  78.         xStep = 1.0/scaleX;
  79.         yStep = 1.0/scaleY;
  80.         zStep = 1.0/scaleZ;
  81.  
  82.         nextRelX = 0;
  83.         nextRelY = 0;
  84.         nextRelZ = 0;
  85.     }
  86.  
  87.     @Override public IBuilder.IEntry computeNext() {
  88.         // if out of bounds...
  89.         if (nextGridX > maxGridX) {
  90.             endOfData();
  91.             return null;
  92.         }
  93.  
  94.         if (nextRelZ == 0) {
  95.             if (nextRelY == 0) {
  96.                 if (nextRelX == 0) {
  97.                     onResetX();
  98.                 }
  99.                 onResetY();
  100.             }
  101.             onResetZ();
  102.         }
  103.         vxyz += dxyz;
  104.  
  105.         IBuilder.IEntry entry = new IBuilder.ImmutbleEntry(
  106.             global(nextGridX, scaleX, nextRelX),
  107.             global(nextGridY, scaleY, nextRelY),
  108.             global(nextGridZ, scaleZ, nextRelZ),
  109.             vxyz
  110.         );
  111.  
  112.         incrementPos();
  113.         return entry;
  114.     }
  115.  
  116.     private void onResetX() {
  117.         nextRelX = boundClampRelPosMin(nextGridX, scaleX, minX);
  118.  
  119.         // get corners
  120.         v000 = builder.get(nextGridX, nextGridY, nextGridZ);
  121.         v001 = builder.get(nextGridX, nextGridY, nextGridZ + 1);
  122.         v010 = builder.get(nextGridX, nextGridY + 1, nextGridZ);
  123.         v011 = builder.get(nextGridX, nextGridY + 1, nextGridZ + 1);
  124.         double v100 = builder.get(nextGridX + 1, nextGridY, nextGridZ);
  125.         double v101 = builder.get(nextGridX + 1, nextGridY, nextGridZ + 1);
  126.         double v110 = builder.get(nextGridX + 1, nextGridY + 1, nextGridZ);
  127.         double v111 = builder.get(nextGridX + 1, nextGridY + 1, nextGridZ + 1);
  128.  
  129.         // step 1
  130.         dx00 = (v100 - v000)*xStep;
  131.         dx01 = (v101 - v001)*xStep;
  132.         dx10 = (v110 - v010)*xStep;
  133.         dx11 = (v111 - v011)*xStep;
  134.  
  135.         vx00 = v000 + dx00*nextRelX;
  136.         vx01 = v001 + dx01*nextRelX;
  137.         vx10 = v010 + dx10*nextRelX;
  138.         vx11 = v011 + dx11*nextRelX;
  139.     }
  140.  
  141.     private void onResetY() {
  142.         nextRelY = boundClampRelPosMin(nextGridY, scaleY, minY);
  143.         // step 2
  144.         dxy0 = (vx10 - vx00)*yStep;
  145.         dxy1 = (vx11 - vx01)*yStep;
  146.  
  147.         vxy0 = vx00 + dxy0*nextRelY;
  148.         vxy1 = vx01 + dxy1*nextRelY;
  149.  
  150.         vx00 += dx00;
  151.         vx01 += dx01;
  152.         vx10 += dx10;
  153.         vx11 += dx11;
  154.     }
  155.  
  156.     private void onResetZ() {
  157.         nextRelZ = boundClampRelPosMin(nextGridZ, scaleZ, minZ);
  158.  
  159.         dxyz = (vxy1 - vxy0)*zStep;
  160.  
  161.         vxyz = vxy0 + dxyz*nextRelZ - dxyz;
  162.  
  163.         vxy0 += dxy0;
  164.         vxy1 += dxy1;
  165.     }
  166.  
  167.     private void incrementPos() {
  168.         nextRelZ++;
  169.         if (nextRelZ >= scaleZ || global(nextGridZ, scaleZ, nextRelZ) > maxZ) {
  170.             nextRelZ = 0;
  171.             nextRelY++;
  172.             if (nextRelY >= scaleY || global(nextGridY, scaleY, nextRelY) > maxY) {
  173.                 nextRelY = 0;
  174.                 nextRelX++;
  175.                 if (nextRelX >= scaleX || global(nextGridX, scaleX, nextRelX) > maxX) {
  176.                     nextRelX = 0;
  177.                     incrementGridPos();
  178.                 }
  179.             }
  180.         }
  181.     }
  182.  
  183.     private void incrementGridPos() {
  184.         nextGridZ++;
  185.         if (nextGridZ > maxGridZ) {
  186.             nextGridZ = 0;
  187.             nextGridY++;
  188.             if (nextGridY > maxGridY) {
  189.                 nextGridY = 0;
  190.                 nextGridX++;
  191.             }
  192.         }
  193.     }
  194.  
  195.     private static int boundClampRelPosMin(int gridPos, int scale, int minPos) {
  196.         int globPos = gridPos*scale;//+localPos
  197.         if (globPos < minPos) {
  198.             return minPos - globPos;
  199.         }
  200.         return 0;
  201.     }
  202.  
  203.     private static int global(int gridPos, int scale, int relPos) {
  204.         return gridPos*scale + relPos;
  205.     }
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement