Advertisement
AlanGomes

Maze Generator

Aug 9th, 2015
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.03 KB | None | 0 0
  1. package me.Dablakbandit.MazeGenerator;
  2.  
  3. import java.util.Random;
  4. import org.bukkit.Location;
  5. import org.bukkit.Material;
  6. import org.bukkit.block.Block;
  7. import org.bukkit.block.BlockState;
  8.  
  9. public class MazeGenerator
  10. {
  11.   static int y1;
  12.   static int y2;
  13.  
  14.   public static void Gen(Material material, Location center, int Width, int Passage, int Height, Material roof, Material floor)
  15.   {
  16.     if (Width % 2 == 1) {
  17.       Width--;
  18.     }
  19.     Width *= 2;
  20.     Passage++;
  21.     Location l = new Location(center.getWorld(), center.getX(), center.getY(), center.getZ());
  22.     y1 = center.getBlockY();
  23.     y2 = center.getBlockY() + Height;
  24.     int minX = center.getBlockX() - Width / 4;
  25.     int minZ = center.getBlockZ() - Width / 4;
  26.     int maxX = center.getBlockX() + Width / 4;
  27.     int maxZ = center.getBlockZ() + Width / 4;
  28.     int minY = center.getBlockY() - 1;
  29.     int maxY = center.getBlockY() + Height;
  30.     if (Height == 1) {
  31.       maxY++;
  32.     }
  33.     for (int i = center.getBlockX() - Width / 4; i <= center
  34.           .getBlockX() + Width / 4; i++) {
  35.       for (int ii = y1; ii < y2; ii++)
  36.       {
  37.         l.setY(ii);
  38.         l.setX(i);
  39.         l.setZ(center.getBlockZ() - Width / 4);
  40.         Block b = l.getBlock();
  41.         b.setType(material);
  42.         b.getState().update(true);
  43.       }
  44.     }
  45.     for (int i = center.getBlockX() - Width / 4; i <= center
  46.           .getBlockX() + Width / 4; i++) {
  47.       for (int ii = y1; ii < y2; ii++)
  48.       {
  49.         l.setY(ii);
  50.         l.setX(i);
  51.         l.setZ(center.getBlockZ() + Width / 4);
  52.         Block b = l.getBlock();
  53.         b.setType(material);
  54.         b.getState().update(true);
  55.       }
  56.     }
  57.     for (int i = center.getBlockZ() - Width / 4; i <= center
  58.           .getBlockZ() + Width / 4; i++) {
  59.       for (int ii = y1; ii < y2; ii++)
  60.       {
  61.         l.setY(ii);
  62.         l.setZ(i);
  63.         l.setX(center.getBlockX() - Width / 4);
  64.         Block b = l.getBlock();
  65.         b.setType(material);
  66.         b.getState().update(true);
  67.       }
  68.     }
  69.     for (int i = center.getBlockZ() - Width / 4; i <= center
  70.           .getBlockZ() + Width / 4; i++) {
  71.       for (int ii = y1; ii < y2; ii++)
  72.       {
  73.         l.setY(ii);
  74.         l.setZ(i);
  75.         l.setX(center.getBlockX() + Width / 4);
  76.         Block b = l.getBlock();
  77.         b.setType(material);
  78.         b.getState().update(true);
  79.       }
  80.     }
  81.     if (floor != null) {
  82.       for (int i = minX; i <= maxX; i++) {
  83.         for (int ii = minZ; ii <= maxZ; ii++)
  84.         {
  85.           l.setY(minY);
  86.           l.setZ(ii);
  87.           l.setX(i);
  88.           Block b = l.getBlock();
  89.           b.setType(floor);
  90.           b.getState().update(true);
  91.         }
  92.       }
  93.     }
  94.     if (roof != null) {
  95.       for (int i = minX; i <= maxX; i++) {
  96.         for (int ii = minZ; ii <= maxZ; ii++)
  97.         {
  98.           l.setY(maxY);
  99.           l.setZ(ii);
  100.           l.setX(i);
  101.           Block b = l.getBlock();
  102.           b.setType(roof);
  103.           b.getState().update(true);
  104.         }
  105.       }
  106.     }
  107.     Chamber c = new Chamber(Width / 2, Width / 2, new Location(
  108.       center.getWorld(), center.getX() - 0.25D * Width, center.getY(),
  109.       center.getZ() - 0.25D * Width));
  110.     createWalls(c, Passage, material);
  111.   }
  112.  
  113.   private static void createWalls(Chamber chamber, int minWidth, Material material)
  114.   {
  115.     String orientation = chamber.getOrientation();
  116.    
  117.     Location l = new Location(chamber.getBL().getWorld(), chamber.getBL().getX(), chamber.getBL().getX(), chamber.getBL().getX());
  118.     Random r = new Random();
  119.     if ((chamber.getXDimension() > minWidth) &&
  120.       (chamber.getZDimension() > minWidth))
  121.     {
  122.       String str1;
  123.       switch ((str1 = orientation).hashCode())
  124.       {
  125.       case 120:
  126.         if (str1.equals("x")) {
  127.           break;
  128.         }
  129.         break;
  130.       case 122:
  131.         if (!str1.equals("z"))
  132.         {
  133.           return;
  134.           int hole;
  135.           int poss;
  136.           int hole;
  137.           if (0 + (chamber.getXDimension() / minWidth - 1) == 0)
  138.           {
  139.             int poss = r.nextInt(chamber.getXDimension() / minWidth) + 1;
  140.             hole = r.nextInt(chamber.getZDimension() / minWidth) + 1;
  141.           }
  142.           else
  143.           {
  144.             poss = r.nextInt(chamber.getXDimension() / minWidth - 1) + 1;
  145.             hole = r.nextInt(chamber.getZDimension() / minWidth) + 1;
  146.           }
  147.           for (int z = chamber.getDown(); z < chamber.getTop(); z++) {
  148.             if ((z <= chamber.getDown() + (hole - 1) * minWidth) ||
  149.               (z >= chamber.getDown() + hole * minWidth))
  150.             {
  151.               int random1 = r.nextInt(100);
  152.               int random2 = r.nextInt(90) + 1;
  153.               for (int y = y1; y < y2; y++)
  154.               {
  155.                 l.setY(y);
  156.                 l.setZ(z);
  157.                 int removed = 0;
  158.                 l.setX(chamber.getLeft() + minWidth * poss);
  159.                 if (l.getBlock().getType() != Material.AIR) {
  160.                   l.getBlock().setType(Material.AIR);
  161.                 }
  162.                 if ((random1 > removed - 1) ||
  163.                   (removed - 1 == -1)) {
  164.                   l.getBlock().setType(material);
  165.                 } else if ((y != random2) && (y != random2 + 1)) {
  166.                   l.getBlock().setType(material);
  167.                 }
  168.                 l.getBlock().getState().update(true);
  169.               }
  170.             }
  171.           }
  172.           int mid = chamber.getLeft() + minWidth * poss;
  173.           int right = chamber.getRight();
  174.           int left = chamber.getLeft();
  175.           createWalls(
  176.             new Chamber(right - mid, chamber.getTop() -
  177.             chamber.getDown(), new Location(l.getWorld(),
  178.             mid, 0.0D, chamber.getDown())), minWidth, material);
  179.           createWalls(
  180.             new Chamber(mid - left, chamber.getTop() -
  181.             chamber.getDown(), new Location(l.getWorld(),
  182.             left, 0.0D, chamber.getDown())), minWidth, material);
  183.         }
  184.         else
  185.         {
  186.           int hole;
  187.           int poss;
  188.           int hole;
  189.           if (0 + (chamber.getZDimension() / minWidth - 1) == 0)
  190.           {
  191.             int poss = r.nextInt(chamber.getZDimension() / minWidth) + 1;
  192.             hole = r.nextInt(chamber.getXDimension() / minWidth) + 1;
  193.           }
  194.           else
  195.           {
  196.             poss = r.nextInt(chamber.getZDimension() / minWidth - 1) + 1;
  197.             hole = r.nextInt(chamber.getXDimension() / minWidth) + 1;
  198.           }
  199.           for (int x = chamber.getLeft(); x < chamber.getRight(); x++) {
  200.             if ((x <= chamber.getLeft() + (hole - 1) * minWidth) ||
  201.               (x >= chamber.getLeft() + hole * minWidth))
  202.             {
  203.               int random1 = r.nextInt(100);
  204.               int random2 = r.nextInt(90) + 1;
  205.               for (int y = y1; y < y2; y++)
  206.               {
  207.                 l.setY(y);
  208.                 l.setX(x);
  209.                 int removed = 0;
  210.                 l.setZ(chamber.getDown() + minWidth * poss);
  211.                 if (l.getBlock().getType() != Material.AIR) {
  212.                   l.getBlock().setType(Material.AIR);
  213.                 }
  214.                 if ((random1 > removed - 1) ||
  215.                   (removed - 1 == -1)) {
  216.                   l.getBlock().setType(material);
  217.                 } else if ((y != random2) && (y != random2 + 1)) {
  218.                   l.getBlock().setType(material);
  219.                 }
  220.                 l.getBlock().getState().update(true);
  221.               }
  222.             }
  223.           }
  224.           int mid2 = chamber.getDown() + minWidth * poss;
  225.           int top = chamber.getTop();
  226.           int down = chamber.getDown();
  227.           createWalls(
  228.             new Chamber(chamber.getRight() - chamber.getLeft(), top -
  229.             mid2, new Location(l.getWorld(),
  230.             chamber.getLeft(), 0.0D, mid2)), minWidth, material);
  231.           createWalls(
  232.             new Chamber(chamber.getRight() - chamber.getLeft(),
  233.             mid2 - down, new Location(l.getWorld(),
  234.             chamber.getLeft(), 0.0D, down)), minWidth, material);
  235.         }
  236.         break;
  237.       }
  238.     }
  239.   }
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement