Advertisement
Earthcomputer

end island location source code

Aug 21st, 2018
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.61 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Comparator;
  3. import java.util.HashSet;
  4. import java.util.List;
  5. import java.util.Random;
  6. import java.util.Set;
  7.  
  8. public class Main {
  9.  
  10.     private static int max = 0;
  11.     private static List<BlockPos> startPortals = new ArrayList<>();
  12.    
  13.     public static void main(String[] args) {
  14.         final int MIN_RADIUS = 768 - 16;
  15.         final int MAX_RADIUS = 1024 + 16;
  16.         for (int x = -MAX_RADIUS; x <= MAX_RADIUS; x++) {
  17.             System.out.println(x);
  18.             for (int z = -MAX_RADIUS; z <= MAX_RADIUS; z++) {
  19.                 if (x * x + z * z <= MAX_RADIUS * MAX_RADIUS && x * x + z * z >= MIN_RADIUS * MIN_RADIUS) {
  20.                     doStuffWithPortal(x, z);
  21.                 }
  22.             }
  23.         }
  24.        
  25.         startPortals.sort(Comparator.<BlockPos>comparingDouble(portal -> {
  26.             double angle = Math.atan2(portal.z, portal.x);
  27.             angle *= 20 / (Math.PI * 2);
  28.             double rounded = Math.round(angle);
  29.             return Math.abs(rounded - angle);
  30.         }));
  31.        
  32.         System.out.println("Found " + startPortals.size() + " max portals");
  33.         System.out.println("Max size: " + max);
  34.         startPortals.forEach(pos -> System.out.println(pos.x + ", " + pos.z));
  35.     }
  36.  
  37.     public static void doStuffWithPortal(int portalX, int portalZ) {
  38.         double distance = Math.sqrt(portalX * portalX + portalZ * portalZ);
  39.  
  40.         double normX = portalX / distance;
  41.         double normZ = portalZ / distance;
  42.  
  43.         double x = normX * 768;
  44.         double z = normZ * 768;
  45.  
  46.         doStuffWithExitPortal(portalX, portalZ, (int) Math.round(x), (int) Math.round(z));
  47.     }
  48.  
  49.     public static void doStuffWithExitPortal(int startX, int startZ, int portalX, int portalZ) {
  50.         //System.out.println(portalX + ", " + portalZ);
  51.        
  52.         final int NUM_X_BITS = 26;
  53.         final int NUM_Z_BITS = NUM_X_BITS;
  54.         final int NUM_Y_BITS = 64 - NUM_X_BITS - NUM_Z_BITS;
  55.         final int Y_SHIFT = 0 + NUM_Z_BITS;
  56.         final int X_SHIFT = Y_SHIFT + NUM_Y_BITS;
  57.         final long X_MASK = (1L << NUM_X_BITS) - 1L;
  58.         final long Y_MASK = (1L << NUM_Y_BITS) - 1L;
  59.         final long Z_MASK = (1L << NUM_Z_BITS) - 1L;
  60.         long seed = ((long)portalX & X_MASK) << X_SHIFT | ((long)75 & Y_MASK) << Y_SHIFT | ((long)portalZ & Z_MASK) << 0;
  61.         //System.out.println(seed);
  62.         Set<BlockPos> endStone = new HashSet<>();
  63.         generate(new Random(seed), endStone);
  64.         int count = endStone.size();
  65.         if (count > max) {
  66.             max = count;
  67.             startPortals.clear();
  68.         }
  69.         if (count == max) {
  70.             startPortals.add(new BlockPos(startX, 0, startZ));
  71.         }
  72.     }
  73.  
  74.     public static boolean generate(Random rand, Set<BlockPos> endStone) {
  75.         float f = (float) (rand.nextInt(3) + 4);
  76.  
  77.         for (int i = 0; f > 0.5F; --i) {
  78.             for (int j = (int) Math.floor(-f); j <= Math.ceil(f); ++j) {
  79.                 for (int k = (int) Math.floor(-f); k <= Math.ceil(f); ++k) {
  80.                     if ((float) (j * j + k * k) <= (f + 1.0F) * (f + 1.0F)) {
  81.                         endStone.add(new BlockPos(j, i, k));
  82.                     }
  83.                 }
  84.             }
  85.  
  86.             f = (float) ((double) f - ((double) rand.nextInt(2) + 0.5D));
  87.         }
  88.  
  89.         return true;
  90.     }
  91.  
  92.     public static class BlockPos {
  93.         private int x, y, z;
  94.  
  95.         public BlockPos(int x, int y, int z) {
  96.             this.x = x;
  97.             this.y = y;
  98.             this.z = z;
  99.         }
  100.  
  101.         @Override
  102.         public int hashCode() {
  103.             final int prime = 31;
  104.             int result = 1;
  105.             result = prime * result + x;
  106.             result = prime * result + y;
  107.             result = prime * result + z;
  108.             return result;
  109.         }
  110.  
  111.         @Override
  112.         public boolean equals(Object obj) {
  113.             if (this == obj)
  114.                 return true;
  115.             if (obj == null)
  116.                 return false;
  117.             if (getClass() != obj.getClass())
  118.                 return false;
  119.             BlockPos other = (BlockPos) obj;
  120.             if (x != other.x)
  121.                 return false;
  122.             if (y != other.y)
  123.                 return false;
  124.             if (z != other.z)
  125.                 return false;
  126.             return true;
  127.         }
  128.     }
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement