Advertisement
Guest User

Planet

a guest
May 5th, 2014
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.83 KB | None | 0 0
  1. public class Planet {
  2.     static PlanetType DIRT = new PlanetType(Blocks.dirt, 20, "Dirt").setTopBlock(Blocks.grass);
  3.     static PlanetType WOOD = new PlanetType(Blocks.leaves, Blocks.log, 10, "Wood");
  4.     static PlanetType WATER = new PlanetType(Blocks.glass, Blocks.water, 5, "Water");
  5.     static PlanetType SAND = new PlanetType(Blocks.sand, 10, "Sand").setBottomBlock(Blocks.sandstone);
  6.     static PlanetType GLOWSTONE = new PlanetType(Blocks.glowstone, 3, "Glowstone");
  7.     static PlanetType STONE = new PlanetType(Blocks.stone, 20, "Stone");
  8.  
  9.     static PlanetType GRAVEL = new PlanetType(Blocks.stone, Blocks.gravel, 40, "Gravel");
  10.     static PlanetType COBBLESTONE = new PlanetType(Blocks.stone, Blocks.cobblestone, 60, "Cobblestone");
  11.     static PlanetType LAVA = new PlanetType(Blocks.stone, Blocks.lava, 60, "Lava");
  12.     static PlanetType COAL = new PlanetType(Blocks.stone, Blocks.coal_ore, 60, "Coal");
  13.     static PlanetType IRON = new PlanetType(Blocks.stone, Blocks.iron_ore, 60, "Iron");
  14.     static PlanetType GOLD = new PlanetType(Blocks.stone, Blocks.gold_ore, 30, "Gold");
  15.     static PlanetType REDSTONE = new PlanetType(Blocks.stone, Blocks.redstone_ore, 30, "Redstone");
  16.     static PlanetType LAPISLAZULI = new PlanetType(Blocks.stone, Blocks.lapis_ore, 15, "Lapislazuli");
  17.     static PlanetType TNT = new PlanetType(Blocks.stone, Blocks.tnt, 2, "TNT");
  18.     static PlanetType DIAMOND = new PlanetType(Blocks.stone, Blocks.diamond_ore, 2, "Diamond");
  19.     static PlanetType EMERALD = new PlanetType(Blocks.stone, Blocks.emerald_ore, 1, "Emerald");
  20.  
  21.     static ArrayList<PlanetType> stonetypes = new ArrayList<PlanetType>();
  22.     static ArrayList<PlanetType> types = initTypes();
  23.  
  24.     Random rand = new Random();
  25.     PlanetoidChunkProvider chunkprovider;
  26.     World world2;
  27.     int x;
  28.     int y;
  29.     int z;
  30.     int radius;
  31.     PlanetType type;
  32.     ArrayList<Point> unfinished = new ArrayList<Point>();
  33.     ArrayList<Point> finished = new ArrayList<Point>();
  34.  
  35.     public Planet(PlanetoidChunkProvider provider, World w, int x2, int y2, int z2, int r) {
  36.         this.chunkprovider = provider;
  37.         this.world2 = w;
  38.         this.x = x2;
  39.         this.y = y2;
  40.         this.z = z2;
  41.         this.radius = r;
  42.  
  43.         this.type = getRandomPlanet();
  44.     }
  45.  
  46.     private static ArrayList<PlanetType> initTypes() {
  47.         ArrayList<PlanetType> list = new ArrayList<PlanetType>();
  48.         list.add(DIRT);
  49.         // list.add(WOOD);
  50.         // list.add(WATER);
  51.         // list.add(SAND);
  52.         // list.add(GLOWSTONE);
  53.         // list.add(STONE);
  54.         // stonetypes.add(GRAVEL);
  55.         // stonetypes.add(COBBLESTONE);
  56.         // stonetypes.add(LAVA);
  57.         // stonetypes.add(COAL);
  58.         // stonetypes.add(IRON);
  59.         // stonetypes.add(GOLD);
  60.         // stonetypes.add(REDSTONE);
  61.         // stonetypes.add(LAPISLAZULI);
  62.         // stonetypes.add(TNT);
  63.         // stonetypes.add(DIAMOND);
  64.         // stonetypes.add(EMERALD);
  65.         return list;
  66.     }
  67.  
  68.     public Planet(PlanetoidChunkProvider provider, World w, double x, double y, double z, double r) {
  69.         this(provider, w, round(x), round(y), round(z), round(r));
  70.     }
  71.  
  72.     public static void print() {
  73.         System.out.println("---PREGENERATION: ---");
  74.         for (PlanetType p : types) {
  75.             System.out.println(p.name + ":\t" + p.total);
  76.         }
  77.  
  78.         for (PlanetType p : stonetypes) {
  79.             System.out.println("-" + p.name + ":\t" + p.total);
  80.         }
  81.         System.out.println("---PREGENERATION END---");
  82.     }
  83.  
  84.     public PlanetType getRandomPlanet() {
  85.         this.rand.setSeed(this.x * 341873128712L + this.z * 132897987541L);
  86.  
  87.         ArrayList<PlanetType> list = new ArrayList<PlanetType>();
  88.  
  89.         list.addAll(types);
  90.  
  91.         PlanetType type = (PlanetType) WeightedRandom.getRandomItem(this.rand, list);
  92.  
  93.         if (type == STONE) {
  94.             list.clear();
  95.             list.addAll(stonetypes);
  96.  
  97.             type = (PlanetType) WeightedRandom.getRandomItem(this.rand, list);
  98.         }
  99.  
  100.         type.total += 1;
  101.         if (type.out == Blocks.stone)
  102.             STONE.total += 1;
  103.         return type;
  104.     }
  105.  
  106.     public void generateChunk(int chunkX, int chunkZ, Block[] ablock, byte[] ameta) {
  107.         this.rand.setSeed(chunkX * 341873128712L + chunkZ * 132897987541L);
  108.         TimeAnalyzer.start("generateChunk");
  109.         for (int x2 = Math.max(chunkX * 16, this.x - this.radius); x2 <= Math.min(chunkX * 16 + 15, this.x + this.radius); x2++) {
  110.             for (int y2 = this.y - this.radius; y2 <= this.y + this.radius; y2++) {
  111.                 for (int z2 = Math.max(chunkZ * 16, this.z - this.radius); z2 <= Math.min(chunkZ * 16 + 15, this.z + this.radius); z2++) {
  112.                     int d = round(distance(this.x, this.y, this.z, x2, y2, z2));
  113.                     if (d == this.radius) {
  114.                         if (isBottomBlock(x2, y2, z2))
  115.                             setBlock(x2, y2, z2, this.type.getBottomBlock(), 0, ablock, ameta);
  116.                         else if (isTopBlock(x2, y2, z2))
  117.                             setBlock(x2, y2, z2, this.type.getTopBlock(), 0, ablock, ameta);
  118.                         else
  119.                             setBlock(x2, y2, z2, this.type.out, 0, ablock, ameta);
  120.                     } else if (d < this.radius) {
  121.                         setBlock(x2, y2, z2, this.type.in, 0, ablock, ameta);
  122.                     }
  123.                     generateSpecial(x2, y2, z2, ablock, ameta);
  124.                 }
  125.             }
  126.         }
  127.  
  128.         if (!this.finished.contains(new Point(chunkX, chunkZ)))
  129.             this.finished.add(new Point(chunkX, chunkZ));
  130.         if (this.unfinished.contains(new Point(chunkX, chunkZ)))
  131.             this.unfinished.remove(new Point(chunkX, chunkZ));
  132.         TimeAnalyzer.end("generateChunk");
  133.     }
  134.  
  135.     public void decorateChunk(World world, int chunkX, int chunkZ) {
  136.         this.rand.setSeed(chunkX * 341873128712L + chunkZ * 132897987541L);
  137.         TimeAnalyzer.start("decorateChunk");
  138.         for (int x2 = Math.max(chunkX * 16, this.x - this.radius); x2 <= Math.min(chunkX * 16 + 15, this.x + this.radius); x2++) {
  139.             for (int y2 = this.y - this.radius; y2 <= this.y + this.radius; y2++) {
  140.                 for (int z2 = Math.max(chunkZ * 16, this.z - this.radius); z2 <= Math.min(chunkZ * 16 + 15, this.z + this.radius); z2++) {
  141.                     if (isTopBlock(x2, y2, z2)) {
  142.                         if ((this.type == SAND) && (this.rand.nextDouble() <= 0.05D) && (Blocks.cactus.canPlaceBlockAt(world, x2, y2 + 1, z2))) {
  143.                             for (int i = 1; i <= 1 + this.rand.nextInt(3); i++) {
  144.                                 world.setBlock(x2, y2 + i, z2, Blocks.cactus);
  145.                             }
  146.                         } else if (this.type == DIRT) {
  147.                             if ((this.rand.nextDouble() <= 0.1D) && (Blocks.tallgrass.canPlaceBlockAt(world, x2, y2 + 1, z2))) {
  148.                                 world.setBlock(x2, y2 + 1, z2, Blocks.tallgrass, 1, 3);
  149.                             } else if (this.rand.nextDouble() <= 0.004D) { // sugar cane
  150.                                 boolean flag1 = (world.getBlock(x2 + 1, y2, z2) == Blocks.grass) && (world.getBlock(x2 - 1, y2, z2) == Blocks.grass)
  151.                                         && (world.getBlock(x2, y2, z2 + 1) == Blocks.grass) && (world.getBlock(x2, y2, z2 - 1) == Blocks.grass);
  152.                                 boolean flag2 = (world.getBlock(x2 + 1, y2 + 1, z2) == Blocks.air) && (world.getBlock(x2 - 1, y2 + 1, z2) == Blocks.air)
  153.                                         && (world.getBlock(x2, y2 + 1, z2 + 1) == Blocks.air) && (world.getBlock(x2, y2 + 1, z2 - 1) == Blocks.air);
  154.  
  155.                                 if ((flag1) && (flag2)) {
  156.                                     world.setBlock(x2, y2, z2, Blocks.water);
  157.                                     for (int i = 1; i <= 1 + this.rand.nextInt(3); i++)
  158.                                         world.setBlock(x2 + 1, y2 + i, z2, Blocks.reeds);
  159.                                     for (int i = 1; i <= 1 + this.rand.nextInt(3); i++)
  160.                                         world.setBlock(x2 - 1, y2 + i, z2, Blocks.reeds);
  161.                                     for (int i = 1; i <= 1 + this.rand.nextInt(3); i++)
  162.                                         world.setBlock(x2, y2 + i, z2 + 1, Blocks.reeds);
  163.                                     for (int i = 1; i <= 1 + this.rand.nextInt(3); i++)
  164.                                         world.setBlock(x2, y2 + i, z2 - 1, Blocks.reeds);
  165.                                 }
  166.                             }
  167.                         }
  168.                     } else if (isBottomBlock(x2, y2, z2)) {
  169.                         if (this.type == WATER) {
  170.                             if (x2 == this.x && z2 == this.z) {
  171.                                 world.setBlockToAir(x2, y2, z2);
  172.                             } else {
  173.                                 if (world.getBlock(x2, y2 + 1, z2) == Blocks.water || world.getBlock(x2, y2 + 1, z2) == Blocks.flowing_water) {
  174.                                     for (int i = 1; i <= 1 + this.rand.nextInt(2); i++)
  175.                                         world.setBlock(x2, y2 + i, z2, Blocks.clay);
  176.                                 } else if (world.getBlock(x2, y2 + 2, z2) == Blocks.water || world.getBlock(x2, y2 + 2, z2) == Blocks.flowing_water) {
  177.                                     for (int i = 2; i <= 2 + this.rand.nextInt(2); i++)
  178.                                         world.setBlock(x2, y2 + i, z2, Blocks.clay);
  179.                                 }
  180.                             }
  181.                         }
  182.                     }
  183.                 }
  184.             }
  185.         }
  186.         TimeAnalyzer.end("decorateChunk");
  187.     }
  188.  
  189.     private void generateSpecial(int x, int y, int z, Block[] ablock, byte[] data) {
  190.     }
  191.  
  192.     private boolean isTopBlock(int x2, int y2, int z2) {
  193.         return (round(distance(this.x, this.y, this.z, x2, y2, z2)) == this.radius) && (round(distance(this.x, this.y, this.z, x2, y2 + 1, z2)) > this.radius);
  194.     }
  195.  
  196.     private boolean isBottomBlock(int x2, int y2, int z2) {
  197.         return (round(distance(this.x, this.y, this.z, x2, y2, z2)) == this.radius) && (round(distance(this.x, this.y, this.z, x2, y2 - 1, z2)) > this.radius);
  198.     }
  199.  
  200.     public boolean shouldFinishChunk(int cx, int cz) {
  201.         return this.unfinished.contains(new Point(cx, cz));
  202.     }
  203.  
  204.     public boolean shouldDecorateChunk(int cx, int cz) {
  205.         return this.finished.contains(new Point(cx, cz));
  206.     }
  207.  
  208.     public boolean isAreaClear() {
  209.         for (Planet p : this.chunkprovider.unfinished) {
  210.             if (p.intersects(this))
  211.                 return false;
  212.         }
  213.         for (Planet p : this.chunkprovider.finished) {
  214.             if (p.intersects(this))
  215.                 return false;
  216.         }
  217.         return true;
  218.     }
  219.  
  220.     private boolean intersects(Planet planet) {
  221.         return distance(planet.x, planet.y, planet.z, this.x, this.y, this.z) <= planet.radius + this.radius + 1;
  222.     }
  223.  
  224.     public boolean isFinished() {
  225.         return this.unfinished.size() == 0;
  226.     }
  227.  
  228.     public static int getBlockNum(int x, int y, int z) {
  229.         if (x < 0)
  230.             x = 16 + x;
  231.         if (z < 0)
  232.             z = 16 + z;
  233.         return y + z * 256 + x * 256 * 16;
  234.     }
  235.  
  236.     public static void setBlock(int x, int y, int z, Block block, int meta, Block[] ablock, byte[] ameta) {
  237.         if ((y < 0) || (y >= 256)) {
  238.             return;
  239.         }
  240.         ablock[getBlockNum(x % 16, y, z % 16)] = block;
  241.         ameta[getBlockNum(x % 16, y, z % 16)] = (byte) meta;
  242.     }
  243.  
  244.     public static double distance(double x1, double y1, double z1, double x2, double y2, double z2) {
  245.         return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2));
  246.     }
  247.  
  248.     public static int round(double d) {
  249.         return (int) Math.round(d);
  250.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement