Advertisement
Earthcomputer

end island code

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