Guest User

Completely generated chunk finder

a guest
May 16th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.41 KB | None | 0 0
  1. import java.awt.Point;
  2. import java.io.FileNotFoundException;
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.List;
  6. import java.util.Random;
  7. import java.util.Scanner;
  8.  
  9. public class GenTest {
  10.    
  11.     static final int SIZE_X = 15; // 41 for file
  12.     static final int SIZE_Z = 10; // 34
  13.     static boolean[][] generated = new boolean[SIZE_Z][SIZE_X];
  14.     static boolean[][] decoratedFlag = new boolean[SIZE_Z][SIZE_X];
  15.     static boolean[][] decorated = new boolean[SIZE_Z * 2][SIZE_X * 2];
  16.     static boolean[][] processed = new boolean[SIZE_Z][SIZE_X];
  17.  
  18.     public static void main(String[] args) throws InterruptedException, FileNotFoundException {
  19.  
  20. //      Scanner in = new Scanner(new FileInputStream("C:\\Users\\javaponyportable\\Documents\\Программы\\Разработка\\Eclipse\\ForgeMods\\run\\log.log"));
  21. //      int dx = 14, dz = 0;
  22.         Scanner in = new Scanner(System.in);
  23.         Random random = new Random(in.nextLong());
  24.        
  25.         List<Point> points = new ArrayList<>((SIZE_X - 2) * (SIZE_Z - 2));
  26.         for (int x = 1; x < SIZE_X - 1; ++x) {
  27.             for (int z = 1; z < SIZE_Z - 1; ++z) {
  28.                 points.add(new Point(x, z));
  29.             }
  30.         }
  31.        
  32.         Collections.shuffle(points, random);
  33.        
  34.         while (!points.isEmpty()) {
  35. //      while (in.hasNext()) {
  36. //          int x = dx + in.nextInt();
  37. //          int z = dz + in.nextInt();
  38. //          minecraft(x, z);
  39.            
  40.             Point pnt = points.remove(points.size() - 1);
  41.             minecraft(pnt.x, pnt.y);
  42.            
  43.             render();
  44.             System.out.println("-----------------------------------");
  45.             Thread.sleep(250);
  46.         }
  47.        
  48.     }
  49.  
  50.     private static void minecraft(int x, int z) {
  51.         generated[z][x] = true;
  52.        
  53.         for (int dx = -1; dx < 2; ++dx) {
  54.             for (int dz = -1; dz < 2; ++dz) {
  55.                 tryDecorate(x + dx, z + dz);
  56.             }
  57.         }
  58.     }
  59.  
  60.     private static void tryDecorate(int x, int z) {
  61.         if (decoratedFlag[z][x]) {
  62.             return;
  63.         }
  64.        
  65.         for (int dx = 0; dx < 2; ++dx) {
  66.             for (int dz = 0; dz < 2; ++dz) {
  67.                 if (!generated[z + dz][x + dx]) {
  68.                     return;
  69.                 }
  70.             }
  71.         }
  72.        
  73.         decoratedFlag[z][x] = true;
  74.         mg(x, z);
  75.         decorated[z*2+1][x*2+1] = true;
  76.         decorated[z*2+2][x*2+1] = true;
  77.         decorated[z*2+1][x*2+2] = true;
  78.         decorated[z*2+2][x*2+2] = true;
  79.     }
  80.  
  81.     private static void mg(int x, int z) {
  82.         for (int dx = 0; dx <= 1; ++dx) {
  83.             for (int dz = 0; dz <= 1; ++dz) {
  84.                 if (isChunkReady(x + dx, z + dz)) {
  85.                     processed[z + dz][x + dx] = true;
  86.                 }
  87.             }
  88.         }
  89.     }
  90.  
  91.     private static boolean isChunkReady(int x, int z) {
  92.         for (int dx = -1; dx <= 0; ++dx) {
  93.             for (int dz = -1; dz <= 0; ++dz) {
  94.                 if (!generated[z + dz][x + dx] || !decoratedFlag[z + dz][x + dx]) {
  95.                     return false;
  96.                 }
  97.             }
  98.         }
  99.        
  100.         return true;
  101.     }
  102.  
  103.     private static void render() {
  104.         StringBuilder sb = new StringBuilder();
  105.        
  106.         for (int z = 0; z < SIZE_Z * 3; ++z) {
  107.             if (z % 3 == 2) {
  108.                 sb.append('\n');
  109.                 continue;
  110.             }
  111.             for (int x = 0; x < SIZE_X * 3; ++x) {
  112.                 char c = '·';
  113.                
  114.                 if (x % 3 == 2) {
  115.                     c = ' ';
  116.                 } else if (generated[z / 3][x / 3]) {
  117.                     if (decorated[z / 3 * 2 + z % 3][x / 3 * 2 + x % 3]) {
  118.                         if (processed[z / 3][x / 3]) {
  119.                             c = '█';
  120.                         } else {
  121.                             c = '▓';
  122.                         }
  123.                     } else {
  124.                         if (processed[z / 3][x / 3]) {
  125.                             c = '@';
  126.                         } else {
  127.                             c = '░';
  128.                         }
  129.                     }
  130.                 } else if (processed[z / 3][x / 3]) {
  131.                     c = '!';
  132.                 }
  133.                
  134.                 sb.append(c).append(c);
  135.             }
  136.             sb.append('\n');
  137.         }
  138.        
  139.         System.out.print(sb.toString());
  140.     }
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment