spenk

CraftBuilder

Mar 4th, 2012
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.51 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.Iterator;
  4. import java.util.Stack;
  5.  
  6. public class CraftBuilder
  7. {
  8.   private static Craft craft;
  9.   private static Stack<BlockLoc> blocksStack;
  10.   private static HashMap<BlockLoc, BlockLoc> blocksList = null;
  11.   private static HashMap<Integer, HashMap<Integer, HashMap<Integer, Short>>> dmatrix;
  12.   private static Short nullBlock = Short.valueOf("-1");
  13.  
  14.   private static boolean isFree(int x, int y, int z)
  15.   {
  16.     if ((x < 0) || (x >= craft.sizeX) || (y < 0) || (y >= craft.sizeY) || (z < 0) || (z >= craft.sizeZ))
  17.     {
  18.       return true;
  19.     }
  20.     int blockId = craft.matrix[x][y][z];
  21.  
  22.     return (blockId == 0) || (blockId == -1);
  23.   }
  24.  
  25.   private static Short get(int x, int y, int z)
  26.   {
  27.     HashMap<Integer,HashMap<Integer,Short>> xRow = (HashMap<Integer,HashMap<Integer,Short>>)dmatrix.get(new Integer(x));
  28.     if (xRow != null)
  29.     {
  30.       HashMap<Integer,Short> yRow = (HashMap<Integer,Short>)xRow.get(new Integer(y));
  31.  
  32.       if (yRow != null) {
  33.         return (Short)yRow.get(new Integer(z));
  34.       }
  35.     }
  36.  
  37.     return null;
  38.   }
  39.  
  40.   private static void set(short blockType, int x, int y, int z)
  41.   {
  42.     HashMap<Integer,HashMap<Integer,Short>> xRow = (HashMap<Integer,HashMap<Integer,Short>>)dmatrix.get(new Integer(x));
  43.     if (xRow == null) {
  44.       xRow = new HashMap<Integer,HashMap<Integer,Short>>();
  45.       dmatrix.put(new Integer(x), xRow);
  46.     }
  47.  
  48.     HashMap<Integer,Short> yRow = (HashMap<Integer,Short>)xRow.get(new Integer(y));
  49.  
  50.     if (yRow == null) {
  51.       yRow = new HashMap<Integer,Short>();
  52.       xRow.put(new Integer(y), yRow);
  53.     }
  54.  
  55.     Short type = (Short)yRow.get(new Integer(z));
  56.  
  57.     if (type == null)
  58.       yRow.put(new Integer(z), new Short(blockType));
  59.   }
  60.  
  61.   private static void detectWater(int x, int y, int z)
  62.   {
  63.     if ((x >= 0) && (x < craft.sizeX) && (y >= 0) && (y < craft.sizeY) && (z >= 0) && (z < craft.sizeZ) && (craft.matrix[x][y][z] != -1)) {
  64.       return;
  65.     }
  66.     int blockId = Craft.player.getWorld().getBlockIdAt(craft.posX + x, craft.posY + y, craft.posZ + z);
  67.  
  68.     if ((blockId == 8) || (blockId == 9)) {
  69.       if (y > craft.waterLevel) craft.waterLevel = y;
  70.       craft.waterType = 8;
  71.       return;
  72.     }
  73.  
  74.     if ((blockId == 10) || (blockId == 11)) {
  75.       if (y > craft.waterLevel) craft.waterLevel = y;
  76.       craft.waterType = 10;
  77.       return;
  78.     }
  79.   }
  80.  
  81.   private static void removeWater()
  82.   {
  83.     boolean updated;
  84.     do
  85.     {
  86.       updated = false;
  87.  
  88.       for (int x = 0; x < craft.sizeX; x++) {
  89.         for (int z = 0; z < craft.sizeZ; z++) {
  90.           for (int y = 0; y < craft.sizeY; y++)
  91.           {
  92.             if ((craft.matrix[x][y][z] < 8) || (craft.matrix[x][y][z] > 11))
  93.               continue;
  94.             if ((!isFree(x + 1, y, z)) && (!isFree(x - 1, y, z)) && (!isFree(x, y, z + 1)) && (!isFree(x, y, z - 1)) && (!isFree(x, y - 1, z)))
  95.             {
  96.               continue;
  97.             }
  98.  
  99.             craft.matrix[x][y][z] = -1;
  100.             updated = true;
  101.           }
  102.         }
  103.  
  104.       }
  105.  
  106.     }
  107.  
  108.     while (updated);
  109.   }
  110.  
  111.   private static boolean createAirBubble()
  112.   {
  113.     BlockLoc block = (BlockLoc)blocksStack.pop();
  114.  
  115.     if (blocksList.get(block) != null) {
  116.       return true;
  117.     }
  118.  
  119.     blocksList.put(block, block);
  120.  
  121.     if ((block.x < 0) || (block.x > craft.maxX - craft.posX) || (block.y < 0) || (block.y > craft.maxY - craft.posY) || (block.z < 0) || (block.z > craft.maxZ - craft.posZ))
  122.     {
  123.       return false;
  124.     }
  125.  
  126.     if (craft.matrix[block.x][block.y][block.z] == -1)
  127.     {
  128.       if ((block.x == 0) || (block.x == craft.maxX - craft.posX) || (block.y == 0) || (block.y == craft.maxY - craft.posY) || (block.z == 0) || (block.z == craft.maxZ - craft.posZ))
  129.       {
  130.         return false;
  131.       }
  132.  
  133.       craft.matrix[block.x][block.y][block.z] = 0;
  134.     }
  135.     else
  136.     {
  137.       return true;
  138.     }
  139.  
  140.     blocksStack.push(new BlockLoc(block.x + 1, block.y, block.z));
  141.     blocksStack.push(new BlockLoc(block.x - 1, block.y, block.z));
  142.     blocksStack.push(new BlockLoc(block.x, block.y + 1, block.z));
  143.     blocksStack.push(new BlockLoc(block.x, block.y - 1, block.z));
  144.     blocksStack.push(new BlockLoc(block.x, block.y, block.z + 1));
  145.     blocksStack.push(new BlockLoc(block.x, block.y, block.z - 1));
  146.  
  147.     return true;
  148.   }
  149.  
  150.   private static boolean secondPassDetection()
  151.   {
  152.     for (int x = 0; x < craft.sizeX; x++) {
  153.       for (int z = 0; z < craft.sizeZ; z++)
  154.       {
  155.         boolean floor = false;
  156.  
  157.         for (int y = 0; y < craft.sizeY; y++)
  158.         {
  159.           if ((!floor) && (craft.matrix[x][y][z] != -1)) {
  160.             floor = true;
  161.           }
  162.           else
  163.           {
  164.             if ((!floor) || (craft.matrix[x][y][z] != -1))
  165.               continue;
  166.             int blockId = Craft.player.getWorld().getBlockIdAt(craft.posX + x, craft.posY + y, craft.posZ + z);
  167.  
  168.             craft.matrix[x][y][z] = (short)blockId;
  169.  
  170.             if (BlocksInfo.isDataBlock(blockId)) {
  171.               addDataBlock(craft.posX + x, craft.posY + y, craft.posZ + z);
  172.             }
  173.  
  174.             if (BlocksInfo.isComplexBlock(blockId)) {
  175.               addComplexBlock(craft.posX + x, craft.posY + y, craft.posZ + z);
  176.             }
  177.  
  178.             if (blockId == 79) {
  179.               Craft.player.sendMessage("§cSorry, you can't have ice in the " + craft.name);
  180.               return false;
  181.             }
  182.           }
  183.         }
  184.  
  185.       }
  186.  
  187.     }
  188.  
  189.     if (craft.waterType != 0)
  190.     {
  191.       craft.waterLevel = -1;
  192.  
  193.       for (int x = 0; x < craft.sizeX; x++) {
  194.         for (int z = 0; z < craft.sizeZ; z++) {
  195.           for (int y = 0; y < craft.sizeY; y++) {
  196.             if (craft.matrix[x][y][z] != -1) {
  197.               detectWater(x + 1, y, z);
  198.               detectWater(x - 1, y, z);
  199.               detectWater(x, y, z + 1);
  200.               detectWater(x, y, z - 1);
  201.             }
  202.           }
  203.         }
  204.  
  205.       }
  206.  
  207.       removeWater();
  208.     }
  209.  
  210.     if (craft.waterLevel != -1)
  211.     {
  212.       for (int x = 0; x < craft.sizeX; x++) {
  213.         for (int z = 0; z < craft.sizeZ; z++) {
  214.           for (int y = craft.waterLevel + 1; y < craft.sizeY; y++) {
  215.             if (craft.matrix[x][y][z] == 0)
  216.               craft.matrix[x][y][z] = -1;
  217.           }
  218.         }
  219.       }
  220.     }
  221.     else {
  222.       for (int x = 0; x < craft.sizeX; x++) {
  223.         for (int z = 0; z < craft.sizeZ; z++) {
  224.           for (int y = 0; y < craft.sizeY; y++) {
  225.             if (craft.matrix[x][y][z] == 0) {
  226.               craft.matrix[x][y][z] = -1;
  227.             }
  228.           }
  229.         }
  230.       }
  231.  
  232.     }
  233.  
  234.     if (craft.type.canDive)
  235.     {
  236.       blocksList = new HashMap<BlockLoc,BlockLoc>();
  237.       blocksStack = new Stack<BlockLoc>();
  238.  
  239.       blocksStack.push(new BlockLoc((int)Math.floor(Craft.player.getX()) - craft.posX, (int)Math.floor(Craft.player.getY() + 1.0D - craft.posY), (int)Math.floor(Craft.player.getZ()) - craft.posZ));
  240.       do
  241.       {
  242.         if (createAirBubble())
  243.           continue;
  244.         Craft.player.sendMessage("§eThis " + craft.type.name + " have holes, it needs to be waterproof");
  245.         return false;
  246.       }
  247.  
  248.       while (!blocksStack.isEmpty());
  249.  
  250.       blocksStack = null;
  251.       blocksList = null;
  252.     }
  253.  
  254.     return true;
  255.   }
  256.  
  257.   private static void addDataBlock(int x, int y, int z)
  258.   {
  259.     craft.dataBlocks.add(new Craft.DataBlock(x - craft.posX, y - craft.posY, z - craft.posZ, Craft.player.getWorld().getBlockData(x, y, z)));
  260.   }
  261.  
  262.   private static void addComplexBlock(int x, int y, int z)
  263.   {
  264.     craft.complexBlocks.add(new Craft.CraftComplexBlock(x - craft.posX, y - craft.posY, z - craft.posZ, null));
  265.   }
  266.  
  267.   private static void createMatrix()
  268.   {
  269.     craft.matrix = new short[craft.sizeX][craft.sizeY][craft.sizeZ];
  270.     craft.dataBlocks = new ArrayList<Craft.DataBlock>();
  271.     craft.complexBlocks = new ArrayList<Craft.CraftComplexBlock>();
  272.  
  273.     for (int x = 0; x < craft.sizeX; x++) {
  274.       for (int z = 0; z < craft.sizeZ; z++) {
  275.         for (int y = 0; y < craft.sizeY; y++) {
  276.           craft.matrix[x][y][z] = -1;
  277.         }
  278.       }
  279.     }
  280.     Integer x;
  281.     HashMap<Integer,HashMap<Integer,Short>> xRow;
  282.     Integer y;
  283.     HashMap<Integer, Short> yRow;
  284.     dmatrix = null;
  285.     for (Iterator<Integer> i = dmatrix.keySet().iterator(); i.hasNext(); ) { x = (Integer)i.next();
  286.       xRow = (HashMap<Integer,HashMap<Integer,Short>>)dmatrix.get(x);
  287.       for (i = xRow.keySet().iterator(); i.hasNext(); ) { y = (Integer)i.next();
  288.         yRow = (HashMap<Integer,Short>)xRow.get(y);
  289.         for (Integer z : yRow.keySet())
  290.         {
  291.           short blockId = ((Short)yRow.get(z)).shortValue();
  292.  
  293.           if (blockId == -1) {
  294.             continue;
  295.           }
  296.           craft.matrix[(x.intValue() - craft.posX)][(y.intValue() - craft.posY)][(z.intValue() - craft.posZ)] = blockId;
  297.  
  298.           if (BlocksInfo.isDataBlock(blockId)) {
  299.             addDataBlock(x.intValue(), y.intValue(), z.intValue());
  300.           }
  301.           if (BlocksInfo.isComplexBlock(blockId))
  302.             addComplexBlock(x.intValue(), y.intValue(), z.intValue());
  303.         }
  304.       }
  305.     }
  306.   }
  307.  
  308.   private static void detectBlock(int x, int y, int z, int dir)
  309.   {
  310.     Short blockType = get(x, y, z);
  311.  
  312.     if (blockType != null) return;
  313.  
  314.     blockType = new Short((short)Craft.player.getWorld().getBlockIdAt(x, y, z));
  315.     if ((blockType.shortValue() == 8) || (blockType.shortValue() == 9)) {
  316.       if (y > craft.waterLevel) craft.waterLevel = y;
  317.       craft.waterType = 8;
  318.       set(nullBlock.shortValue(), x, y, z);
  319.       return;
  320.     }
  321.  
  322.     if ((blockType.shortValue() == 10) || (blockType.shortValue() == 11)) {
  323.       if (y > craft.waterLevel) craft.waterLevel = y;
  324.       craft.waterType = 10;
  325.       set(nullBlock.shortValue(), x, y, z);
  326.       return;
  327.     }
  328.  
  329.     if (blockType.shortValue() == 0) {
  330.       set(nullBlock.shortValue(), x, y, z);
  331.       return;
  332.     }
  333.  
  334.     if (blockType.shortValue() == 55) {
  335.       if (dir != 1) {
  336.         set(nullBlock.shortValue(), x, y, z);
  337.         return;
  338.       }
  339.  
  340.     }
  341.     else if (craft.type.structureBlocks == null)
  342.     {
  343.       if ((blockType.shortValue() != 4) && (blockType.shortValue() != 5) && (blockType.shortValue() != 17) && (blockType.shortValue() != 19) && (blockType.shortValue() != 20) && (blockType.shortValue() != 35) && ((blockType.shortValue() < 41) || (blockType.shortValue() > 50)) && (blockType.shortValue() != 53) && (blockType.shortValue() != 55) && (blockType.shortValue() != 57) && (blockType.shortValue() != 65) && (blockType.shortValue() != 67) && (blockType.shortValue() != 68) && (blockType.shortValue() != 69) && (blockType.shortValue() != 75) && (blockType.shortValue() != 76) && (blockType.shortValue() != 77) && (blockType.shortValue() != 85) && (blockType.shortValue() != 87) && (blockType.shortValue() != 88) && (blockType.shortValue() != 89))
  344.       {
  345.         set(nullBlock.shortValue(), x, y, z);
  346.         return;
  347.       }
  348.  
  349.     }
  350.     else
  351.     {
  352.       boolean found = false;
  353.       for (short blockId : craft.type.structureBlocks) {
  354.         if (blockType.shortValue() != blockId) continue; found = true;
  355.       }
  356.       if (!found) {
  357.         set(nullBlock.shortValue(), x, y, z);
  358.         return;
  359.       }
  360.  
  361.     }
  362.  
  363.     set(blockType.shortValue(), x, y, z);
  364.  
  365.     craft.blockCount += 1;
  366.     if (craft.blockCount > craft.type.maxBlocks) {
  367.       return;
  368.     }
  369.  
  370.     if (blockType.shortValue() == craft.type.flyBlockType) {
  371.       craft.flyBlockCount += 1;
  372.     }
  373.  
  374.     if (x < craft.minX) craft.minX = x;
  375.     if (x > craft.maxX) craft.maxX = x;
  376.     if (y < craft.minY) craft.minY = y;
  377.     if (y > craft.maxY) craft.maxY = y;
  378.     if (z < craft.minZ) craft.minZ = z;
  379.     if (z > craft.maxZ) craft.maxZ = z;
  380.  
  381.     if (BlocksInfo.needsSupport(blockType.shortValue())) return;
  382.  
  383.     blocksStack.push(new BlockLoc(x, y, z));
  384.   }
  385.  
  386.   private static void detectBlock(BlockLoc block)
  387.   {
  388.     detectBlock(block.x + 1, block.y, block.z, 1);
  389.     detectBlock(block.x - 1, block.y, block.z, 2);
  390.     detectBlock(block.x, block.y + 1, block.z, 1);
  391.     detectBlock(block.x, block.y - 1, block.z, 6);
  392.     detectBlock(block.x, block.y, block.z + 1, 3);
  393.     detectBlock(block.x, block.y, block.z - 1, 4);
  394.  
  395.     detectBlock(block.x + 1, block.y - 1, block.z, -1);
  396.     detectBlock(block.x - 1, block.y - 1, block.z, -1);
  397.     detectBlock(block.x, block.y - 1, block.z + 1, -1);
  398.     detectBlock(block.x, block.y - 1, block.z - 1, -1);
  399.     detectBlock(block.x + 1, block.y + 1, block.z, -1);
  400.     detectBlock(block.x - 1, block.y + 1, block.z, -1);
  401.     detectBlock(block.x, block.y + 1, block.z + 1, -1);
  402.     detectBlock(block.x, block.y + 1, block.z - 1, -1);
  403.   }
  404.  
  405.   public static boolean detect(Craft craft, int X, int Y, int Z)
  406.   {
  407.     CraftBuilder.craft = craft;
  408.  
  409.     dmatrix = new HashMap<Integer, HashMap<Integer, HashMap<Integer, Short>>>();
  410.  
  411.     craft.blockCount = 0;
  412.  
  413.     craft.minX = (craft.maxX = X);
  414.     craft.minY = (craft.maxY = Y);
  415.     craft.minZ = (craft.maxZ = Z);
  416.  
  417.     blocksStack = new Stack<BlockLoc>();
  418.     blocksStack.push(new BlockLoc(X, Y, Z));
  419.     do
  420.     {
  421.       detectBlock((BlockLoc)blocksStack.pop());
  422.     }
  423.     while (!blocksStack.isEmpty());
  424.  
  425.     blocksStack = null;
  426.  
  427.     if (craft.blockCount > craft.type.maxBlocks) {
  428.       Craft.player.sendMessage("§cUnable to detect the " + craft.name + ", be sure it is not connected");
  429.       Craft.player.sendMessage("§c to the ground, or maybe it is too big for this type of craft");
  430.       Craft.player.sendMessage("§cThe maximum size is " + craft.type.maxBlocks + " blocks");
  431.       return false;
  432.     }
  433.  
  434.     if (craft.blockCount < craft.type.minBlocks)
  435.     {
  436.       if (craft.blockCount == 0) {
  437.         Craft.player.sendMessage("§cThere is no " + craft.name + " here");
  438.         Craft.player.sendMessage("§cBe sure you are standing on a block");
  439.       }
  440.       else {
  441.         Craft.player.sendMessage("§cThis " + craft.name + " is too small !");
  442.         Craft.player.sendMessage("§cYou need to add " + (craft.type.minBlocks - craft.blockCount) + " blocks");
  443.       }
  444.  
  445.       return false;
  446.     }
  447.  
  448.     for (Craft c : Craft.craftList) {
  449.       if ((c != craft) && (c.isOnBoard))
  450.       {
  451.         if (((craft.minX >= craft.minX) || (craft.maxX >= craft.minX)) && ((craft.minX >= craft.minX) || (craft.maxX >= craft.minX)) &&
  452.           ((craft.minY >= craft.minY) || (craft.maxY >= craft.minY)) && ((craft.minY >= craft.minY) || (craft.maxY >= craft.minY)) &&
  453.           ((craft.minZ >= craft.minZ) || (craft.maxZ >= craft.minZ)) && (
  454.           (craft.minZ >= craft.minZ) || (craft.maxZ >= craft.minZ))) {
  455.           Craft.player.sendMessage("§c" + Craft.player.getName() + " is already controling this " + craft.name);
  456.           return false;
  457.         }
  458.       }
  459.  
  460.     }
  461.  
  462.     craft.sizeX = (craft.maxX - craft.minX + 1);
  463.     craft.sizeY = (craft.maxY - craft.minY + 1);
  464.  
  465.     craft.sizeZ = (craft.maxZ - craft.minZ + 1);
  466.  
  467.     craft.posX = craft.minX;
  468.     craft.posY = craft.minY;
  469.     craft.posZ = craft.minZ;
  470.  
  471.     if (craft.waterLevel != -1) {
  472.       craft.waterLevel -= craft.posY;
  473.     }
  474.  
  475.     createMatrix();
  476.  
  477.     if (!secondPassDetection()) {
  478.       return false;
  479.     }
  480.  
  481.     if ((craft.type.canNavigate) && (!craft.type.canFly) && (craft.waterType == 0)) {
  482.       Craft.player.sendMessage("§cThis " + craft.name + " is not on water...");
  483.       return false;
  484.     }
  485.  
  486.     if ((craft.type.canDive) && (!craft.type.canFly) && (craft.waterType == 0)) {
  487.       Craft.player.sendMessage("§cThis " + craft.name + " is not into water...");
  488.       return false;
  489.     }
  490.  
  491.     if ((craft.type.canFly) && (!craft.type.canNavigate) && (!craft.type.canDive) && (craft.waterLevel > -1)) {
  492.       Craft.player.sendMessage("§cThis " + craft.name + " is into water...");
  493.       return false;
  494.     }
  495.  
  496.     if ((craft.type.canFly) && (craft.type.flyBlockType != 0))
  497.     {
  498.       int flyBlocksNeeded = (int)Math.floor((craft.blockCount - craft.flyBlockCount) * (craft.type.flyBlockPercent * 0.01D) / (1.0D - craft.type.flyBlockPercent * 0.01D));
  499.  
  500.       if (flyBlocksNeeded < 1) {
  501.         flyBlocksNeeded = 1;
  502.       }
  503.       if (craft.flyBlockCount < flyBlocksNeeded) {
  504.         Craft.player.sendMessage("§cNot enough " + craft.type.flyBlockName + " to make this " + craft.name + " move");
  505.         Craft.player.sendMessage("§cYou need to add " + (flyBlocksNeeded - craft.flyBlockCount) + " more");
  506.         return false;
  507.       }
  508.     }
  509.  
  510.     if (craft.customName == null)
  511.       Craft.player.sendMessage("§e" + craft.type.sayOnControl);
  512.     else {
  513.       Craft.player.sendMessage("§eWelcome on " + craft.customName + " !");
  514.     }
  515.  
  516.     return true;
  517.   }
  518. }
Advertisement
Add Comment
Please, Sign In to add comment