Advertisement
Guest User

Untitled

a guest
Nov 2nd, 2013
573
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.96 KB | None | 0 0
  1. public class ThreadFindSeal extends Thread
  2. {
  3.     public World world;
  4.     public Vector3 head;
  5.     public boolean sealed;
  6.     public List<TileSealer> sealers;
  7.     public List<Vector3> oxygenReliantBlocks;
  8.     public LinkedList<VecDirPair> checked;
  9.     public int checkCount;
  10.    
  11.     public ThreadFindSeal()
  12.     {
  13.         super("Sealer Roomfinder Thread");
  14.     }
  15.    
  16.     @Override
  17.     public void run()
  18.     {
  19.         // Get initial time in nanoseconds, for performance testing
  20.         long time1 = System.nanoTime();
  21.        
  22.         // Initialize as sealed
  23.         this.sealed = true;
  24.         // Start the loop right above the head tile
  25.         loopThrough(this.head.clone().translate(new Vector3(0, 1, 0)));
  26.        
  27.         // If the loop found another sealer
  28.         if (this.sealers.size() > 1)
  29.         {
  30.             this.checkCount = 0;
  31.            
  32.             // Loop through each found sealer, and increase loop size by it's oxygen level
  33.             for (int i = 0; i < this.sealers.size(); i++)
  34.             {
  35.                 TileSealer sealer = this.sealers.get(i);
  36.                 this.checkCount += sealer.getFindSealChecks();
  37.             }
  38.            
  39.             // Restart check, with larger loop count this time
  40.             this.sealed = true;
  41.             this.checked.clear();
  42.             loopThrough(this.head.clone().translate(new Vector3(0, 1, 0)));
  43.         }
  44.        
  45.         int checkedSize1 = this.checked.size();
  46.        
  47.         if (!sealed)
  48.         {
  49.             // Loop through with second loop type, the "unseal" loop
  50.             this.checked.clear();
  51.             this.loopThroughD(this.head.clone().translate(new Vector3(0, 1, 0)));
  52.            
  53.             // Set each checked "breathable air" tile to a vacuum one
  54.             for (VecDirPair checkedVec : checked)
  55.             {
  56.                 int blockID = checkedVec.getPosition().getBlockID(world);
  57.                
  58.                 if (blockID == GCCoreBlocks.breatheableAir.blockID)
  59.                 {
  60.                     world.setTile(checkedVec.getPosition().intX(), checkedVec.getPosition().intY(), checkedVec.getPosition().intZ(), 0, 0, 2);
  61.                 }
  62.             }
  63.            
  64.             checkedSize1 += checked.size();
  65.         }
  66.  
  67.         // Get second time in nanoseconds, for performance testing
  68.         long time2 = System.nanoTime();
  69.        
  70.         for (VecDirPair checkedVec : checked)
  71.         {
  72.             int blockID = checkedVec.getPosition().getBlockID(world);
  73.  
  74.             // If sealed, replace vacuum tile with "breathable" air tile
  75.             if (this.sealed && blockID == 0)
  76.             {
  77.                 world.setTile(checkedVec.getPosition().intX(), checkedVec.getPosition().intY(), checkedVec.getPosition().intZ(), GCCoreBlocks.breatheableAir.blockID, 0, 2);
  78.             }
  79.         }
  80.  
  81.         // Get third time in nanoseconds, for performance testing
  82.         long time3 = System.nanoTime();
  83.  
  84.         // Print out the result and the time each section took
  85.         if (GCCoreConfigManager.enableDebug)
  86.         {
  87.             FMLLog.info("Oxygen Sealer Check Completed at x" + this.head.intX() + " y" + this.head.intY() + " z" + this.head.intZ());
  88.             FMLLog.info("   Sealed: " + this.sealed);
  89.             FMLLog.info("   Loop Time taken: " + ((time2 - time1) / 1000000.0D) + "ms");
  90.             FMLLog.info("   Place Time taken: " + ((time3 - time2) / 1000000.0D) + "ms");
  91.             FMLLog.info("   Total Time taken: " + ((time3 - time1) / 1000000.0D) + "ms");
  92.             FMLLog.info("   Found: " + this.sealers.size() + " sealers");
  93.             FMLLog.info("   Looped through: " + checkedSize1 + " blocks");
  94.         }
  95.     }
  96.    
  97.     private void loopThroughD(Vector3 vec)
  98.     {
  99.         for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS)
  100.         {
  101.             Vector3 sideVec = vec.clone().modifyPositionFromSide(dir);
  102.             VecDirPair pair = new VecDirPair(sideVec, dir);
  103.            
  104.             if (!checked(pair))
  105.             {
  106.                 check(pair);
  107.                
  108.                 if (this.breathableAirAdjacent(pair))
  109.                 {
  110.                     this.loopThroughD(sideVec);
  111.                 }
  112.             }
  113.         }
  114.     }
  115.    
  116.     private void loopThrough(Vector3 vec)
  117.     {
  118.         if (this.sealed)
  119.         {
  120.             if (this.checkCount > 0)
  121.             {
  122.                 for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS)
  123.                 {
  124.                     Vector3 sideVec = vec.clone().modifyPositionFromSide(dir);
  125.                     VecDirPair pair = new VecDirPair(sideVec, dir);
  126.                    
  127.                     if (!checked(pair))
  128.                     {
  129.                         this.checkCount--;
  130.                         check(pair);
  131.                        
  132.                         if (Util.isUnsealedBlock(pair))
  133.                         {
  134.                             this.loopThrough(sideVec);
  135.                         }
  136.                        
  137.                         TileEntity tileAtVec = sideVec.getTileEntity(this.world);
  138.                        
  139.                         if (tileAtVec != null && tileAtVec instanceof TileSealer && !this.sealers.contains(tileAtVec))
  140.                         {
  141.                             TileSealer sealer = (TileSealer) tileAtVec;
  142.                            
  143.                             if (sealer.active)
  144.                             {
  145.                                 this.sealers.add(sealer);
  146.                             }
  147.                         }
  148.                     }
  149.                 }
  150.             }
  151.             else
  152.             {
  153.                 for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS)
  154.                 {
  155.                     Vector3 sideVec = vec.clone().modifyPositionFromSide(dir);
  156.  
  157.                     if ((sideVec.getBlockID(this.world) == 0 || sideVec.getBlockID(this.world) == GCCoreBlocks.breatheableAir.blockID) && !checked(sideVec, dir))
  158.                     {
  159.                         this.sealed = false;
  160.                     }
  161.                 }
  162.             }
  163.         }
  164.     }
  165.    
  166.     private boolean checked(Vector3 vec, ForgeDirection dir)
  167.     {
  168.         return this.checked(new VecDirPair(vec, dir));
  169.     }
  170.    
  171.     private boolean checked(VecDirPair pair)
  172.     {
  173.         return this.checked.contains(pair);
  174.     }
  175.    
  176.     private void check(VecDirPair pair)
  177.     {
  178.         this.checked.add(pair);
  179.     }
  180.    
  181.     private boolean breathableAirAdjacent(VecDirPair pair)
  182.     {
  183.         for (final ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS)
  184.         {
  185.             Vector3 vec = pair.getPosition().clone().modifyPositionFromSide(dir);
  186.  
  187.             if (this.isBreathableAir(new VecDirPair(vec, dir)))
  188.             {
  189.                 return true;
  190.             }
  191.         }
  192.  
  193.         return false;
  194.     }
  195.    
  196.     private boolean isBreathableAir(VecDirPair pair)
  197.     {
  198.         return pair.getPosition().getBlockID(this.world) == GCCoreBlocks.breatheableAir.blockID;
  199.     }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement