Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ThreadFindSeal extends Thread
- {
- public World world;
- public Vector3 head;
- public boolean sealed;
- public List<TileSealer> sealers;
- public List<Vector3> oxygenReliantBlocks;
- public LinkedList<VecDirPair> checked;
- public int checkCount;
- public ThreadFindSeal()
- {
- super("Sealer Roomfinder Thread");
- }
- @Override
- public void run()
- {
- // Get initial time in nanoseconds, for performance testing
- long time1 = System.nanoTime();
- // Initialize as sealed
- this.sealed = true;
- // Start the loop right above the head tile
- loopThrough(this.head.clone().translate(new Vector3(0, 1, 0)));
- // If the loop found another sealer
- if (this.sealers.size() > 1)
- {
- this.checkCount = 0;
- // Loop through each found sealer, and increase loop size by it's oxygen level
- for (int i = 0; i < this.sealers.size(); i++)
- {
- TileSealer sealer = this.sealers.get(i);
- this.checkCount += sealer.getFindSealChecks();
- }
- // Restart check, with larger loop count this time
- this.sealed = true;
- this.checked.clear();
- loopThrough(this.head.clone().translate(new Vector3(0, 1, 0)));
- }
- int checkedSize1 = this.checked.size();
- if (!sealed)
- {
- // Loop through with second loop type, the "unseal" loop
- this.checked.clear();
- this.loopThroughD(this.head.clone().translate(new Vector3(0, 1, 0)));
- // Set each checked "breathable air" tile to a vacuum one
- for (VecDirPair checkedVec : checked)
- {
- int blockID = checkedVec.getPosition().getBlockID(world);
- if (blockID == GCCoreBlocks.breatheableAir.blockID)
- {
- world.setTile(checkedVec.getPosition().intX(), checkedVec.getPosition().intY(), checkedVec.getPosition().intZ(), 0, 0, 2);
- }
- }
- checkedSize1 += checked.size();
- }
- // Get second time in nanoseconds, for performance testing
- long time2 = System.nanoTime();
- for (VecDirPair checkedVec : checked)
- {
- int blockID = checkedVec.getPosition().getBlockID(world);
- // If sealed, replace vacuum tile with "breathable" air tile
- if (this.sealed && blockID == 0)
- {
- world.setTile(checkedVec.getPosition().intX(), checkedVec.getPosition().intY(), checkedVec.getPosition().intZ(), GCCoreBlocks.breatheableAir.blockID, 0, 2);
- }
- }
- // Get third time in nanoseconds, for performance testing
- long time3 = System.nanoTime();
- // Print out the result and the time each section took
- if (GCCoreConfigManager.enableDebug)
- {
- FMLLog.info("Oxygen Sealer Check Completed at x" + this.head.intX() + " y" + this.head.intY() + " z" + this.head.intZ());
- FMLLog.info(" Sealed: " + this.sealed);
- FMLLog.info(" Loop Time taken: " + ((time2 - time1) / 1000000.0D) + "ms");
- FMLLog.info(" Place Time taken: " + ((time3 - time2) / 1000000.0D) + "ms");
- FMLLog.info(" Total Time taken: " + ((time3 - time1) / 1000000.0D) + "ms");
- FMLLog.info(" Found: " + this.sealers.size() + " sealers");
- FMLLog.info(" Looped through: " + checkedSize1 + " blocks");
- }
- }
- private void loopThroughD(Vector3 vec)
- {
- for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS)
- {
- Vector3 sideVec = vec.clone().modifyPositionFromSide(dir);
- VecDirPair pair = new VecDirPair(sideVec, dir);
- if (!checked(pair))
- {
- check(pair);
- if (this.breathableAirAdjacent(pair))
- {
- this.loopThroughD(sideVec);
- }
- }
- }
- }
- private void loopThrough(Vector3 vec)
- {
- if (this.sealed)
- {
- if (this.checkCount > 0)
- {
- for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS)
- {
- Vector3 sideVec = vec.clone().modifyPositionFromSide(dir);
- VecDirPair pair = new VecDirPair(sideVec, dir);
- if (!checked(pair))
- {
- this.checkCount--;
- check(pair);
- if (Util.isUnsealedBlock(pair))
- {
- this.loopThrough(sideVec);
- }
- TileEntity tileAtVec = sideVec.getTileEntity(this.world);
- if (tileAtVec != null && tileAtVec instanceof TileSealer && !this.sealers.contains(tileAtVec))
- {
- TileSealer sealer = (TileSealer) tileAtVec;
- if (sealer.active)
- {
- this.sealers.add(sealer);
- }
- }
- }
- }
- }
- else
- {
- for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS)
- {
- Vector3 sideVec = vec.clone().modifyPositionFromSide(dir);
- if ((sideVec.getBlockID(this.world) == 0 || sideVec.getBlockID(this.world) == GCCoreBlocks.breatheableAir.blockID) && !checked(sideVec, dir))
- {
- this.sealed = false;
- }
- }
- }
- }
- }
- private boolean checked(Vector3 vec, ForgeDirection dir)
- {
- return this.checked(new VecDirPair(vec, dir));
- }
- private boolean checked(VecDirPair pair)
- {
- return this.checked.contains(pair);
- }
- private void check(VecDirPair pair)
- {
- this.checked.add(pair);
- }
- private boolean breathableAirAdjacent(VecDirPair pair)
- {
- for (final ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS)
- {
- Vector3 vec = pair.getPosition().clone().modifyPositionFromSide(dir);
- if (this.isBreathableAir(new VecDirPair(vec, dir)))
- {
- return true;
- }
- }
- return false;
- }
- private boolean isBreathableAir(VecDirPair pair)
- {
- return pair.getPosition().getBlockID(this.world) == GCCoreBlocks.breatheableAir.blockID;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement