From 702abbb011c18249e56955528bf5cc85a1804f0d Mon Sep 17 00:00:00 2001 From: mDiyo Date: Sun, 29 Sep 2013 19:38:20 -0700 Subject: [PATCH] Smeltery Components --- src/tconstruct/blocks/SmelteryBlock.java | 4 +- .../blocks/logic/AdaptiveSmelteryLogic.java | 79 +++- .../blocks/logic/TowerFurnaceLogic.java | 23 +- .../blocks/AdaptiveInventoryLogic.java | 357 +----------------- .../library/component/IComponentHolder.java | 8 + .../library/component/LogicComponent.java | 6 + .../library/component/MultiFluidTank.java | 156 ++++++++ .../library/component/SmelteryComponent.java | 125 ++++++ .../library/component/TankLayerScan.java | 354 +++++++++++++++++ 9 files changed, 737 insertions(+), 375 deletions(-) create mode 100644 src/tconstruct/library/component/IComponentHolder.java create mode 100644 src/tconstruct/library/component/LogicComponent.java create mode 100644 src/tconstruct/library/component/MultiFluidTank.java create mode 100644 src/tconstruct/library/component/SmelteryComponent.java create mode 100644 src/tconstruct/library/component/TankLayerScan.java diff --git a/src/tconstruct/blocks/SmelteryBlock.java b/src/tconstruct/blocks/SmelteryBlock.java index 5c3414a4..d76bf092 100644 --- a/src/tconstruct/blocks/SmelteryBlock.java +++ b/src/tconstruct/blocks/SmelteryBlock.java @@ -41,11 +41,11 @@ public class SmelteryBlock extends InventoryBlock /* Rendering */ - @Override + /*@Override public int getRenderType () { return SmelteryRender.smelteryModel; - } + }*/ @Override public String[] getTextureNames () diff --git a/src/tconstruct/blocks/logic/AdaptiveSmelteryLogic.java b/src/tconstruct/blocks/logic/AdaptiveSmelteryLogic.java index 391848d7..aa85cfbe 100644 --- a/src/tconstruct/blocks/logic/AdaptiveSmelteryLogic.java +++ b/src/tconstruct/blocks/logic/AdaptiveSmelteryLogic.java @@ -1,51 +1,98 @@ package tconstruct.blocks.logic; +import java.util.ArrayList; +import java.util.List; + import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; +import net.minecraft.util.MathHelper; import net.minecraft.world.World; import net.minecraftforge.common.ForgeDirection; +import tconstruct.common.TContent; import tconstruct.library.blocks.AdaptiveInventoryLogic; +import tconstruct.library.component.IComponentHolder; +import tconstruct.library.component.LogicComponent; +import tconstruct.library.component.MultiFluidTank; +import tconstruct.library.component.SmelteryComponent; +import tconstruct.library.component.TankLayerScan; +import tconstruct.library.util.IActiveLogic; +import tconstruct.library.util.IMasterLogic; -public class AdaptiveSmelteryLogic extends AdaptiveInventoryLogic +public class AdaptiveSmelteryLogic extends AdaptiveInventoryLogic implements IActiveLogic, IMasterLogic, IComponentHolder { byte direction; + TankLayerScan structure = new TankLayerScan(this, TContent.smeltery, TContent.lavaTank); + MultiFluidTank multitank = new MultiFluidTank(); + SmelteryComponent smeltery = new SmelteryComponent(this, this.worldObj, multitank, 800); @Override public byte getRenderDirection () { - return 0; + return direction; } @Override public ForgeDirection getForgeDirection () { - return null; + return ForgeDirection.VALID_DIRECTIONS[direction]; } @Override - @Deprecated public void setDirection (int side) { - + } @Override - @Deprecated public void setDirection (float yaw, float pitch, EntityLivingBase player) { + int facing = MathHelper.floor_double((double) (yaw / 360) + 0.5D) & 3; + switch (facing) + { + case 0: + direction = 2; + break; + + case 1: + direction = 5; + break; + + case 2: + direction = 3; + break; + + case 3: + direction = 4; + break; + } + } + + @Override + public boolean getActive () + { + // TODO Auto-generated method stub + return false; + } + + @Override + public void setActive (boolean flag) + { + // TODO Auto-generated method stub } @Override - protected boolean isValidBlock (int x, int y, int z) + public void setNeedsUpdate () { - return false; + // TODO Auto-generated method stub + } @Override public Container getGuiContainer (InventoryPlayer inventoryplayer, World world, int x, int y, int z) { + // TODO Auto-generated method stub return null; } @@ -55,4 +102,20 @@ public class AdaptiveSmelteryLogic extends AdaptiveInventoryLogic return "crafters.Smeltery"; } + @Override + public List getComponents () + { + ArrayList ret = new ArrayList(1); + ret.add(structure); + ret.add(multitank); + ret.add(smeltery); + return ret; + } + + @Override + public void notifyChange (int x, int y, int z) + { + // TODO Auto-generated method stub + + } } diff --git a/src/tconstruct/blocks/logic/TowerFurnaceLogic.java b/src/tconstruct/blocks/logic/TowerFurnaceLogic.java index 338c7f69..2683f5a4 100644 --- a/src/tconstruct/blocks/logic/TowerFurnaceLogic.java +++ b/src/tconstruct/blocks/logic/TowerFurnaceLogic.java @@ -13,11 +13,6 @@ import tconstruct.library.util.IMasterLogic; public class TowerFurnaceLogic extends AdaptiveInventoryLogic implements IActiveLogic, IFacingLogic, IMasterLogic { - public TowerFurnaceLogic() - { - super(); - } - @Override public void notifyChange (int x, int y, int z) { @@ -40,6 +35,7 @@ public class TowerFurnaceLogic extends AdaptiveInventoryLogic implements IActive } @Override + @Deprecated public void setDirection (int side) { // TODO Auto-generated method stub @@ -47,6 +43,7 @@ public class TowerFurnaceLogic extends AdaptiveInventoryLogic implements IActive } @Override + @Deprecated public void setDirection (float yaw, float pitch, EntityLivingBase player) { // TODO Auto-generated method stub @@ -67,23 +64,25 @@ public class TowerFurnaceLogic extends AdaptiveInventoryLogic implements IActive } + @Override + public void setNeedsUpdate () + { + // TODO Auto-generated method stub + + } + @Override public Container getGuiContainer (InventoryPlayer inventoryplayer, World world, int x, int y, int z) { + // TODO Auto-generated method stub return null; } @Override protected String getDefaultName () { + // TODO Auto-generated method stub return null; } - @Override - protected boolean isValidBlock (int x, int y, int z) - { - // TODO Auto-generated method stub - return false; - } - } diff --git a/src/tconstruct/library/blocks/AdaptiveInventoryLogic.java b/src/tconstruct/library/blocks/AdaptiveInventoryLogic.java index a6cec5c9..90e3ffca 100644 --- a/src/tconstruct/library/blocks/AdaptiveInventoryLogic.java +++ b/src/tconstruct/library/blocks/AdaptiveInventoryLogic.java @@ -15,28 +15,21 @@ import tconstruct.library.util.IFacingLogic; import tconstruct.library.util.IMasterLogic; import tconstruct.library.util.IServantLogic; -public abstract class AdaptiveInventoryLogic extends InventoryLogic implements IFacingLogic, IMasterLogic +public abstract class AdaptiveInventoryLogic extends InventoryLogic implements IFacingLogic { Random random = new Random(); - protected boolean completeStructure; protected int inventorySize; - protected boolean needsUpdate; - protected int tick = 0; public AdaptiveInventoryLogic() { super(0); - validAirCoords.add(new int[] { 1, 0 }); - validAirCoords.add(new int[] { -1, 0 }); - validAirCoords.add(new int[] { 0, 1 }); - validAirCoords.add(new int[] { 0, -1 }); } protected void adjustInventory (int size, boolean forceAdjust) { if (size != inventorySize || forceAdjust) { - needsUpdate = true; + setNeedsUpdate(); inventorySize = size; ItemStack[] tempInv = inventory; @@ -109,353 +102,11 @@ public abstract class AdaptiveInventoryLogic extends InventoryLogic implements I } } } + + public abstract void setNeedsUpdate(); public int getTossDirection () { return getRenderDirection(); } - - @Override - public void updateEntity () - { - tick++; - - if (tick % 20 == 0) - { - /*if (!validStructure) - checkValidPlacement();*/ - - if (needsUpdate) - { - needsUpdate = false; - worldObj.markBlockForUpdate(xCoord, yCoord, zCoord); - } - } - } - - @Override - public void onInventoryChanged () - { - super.onInventoryChanged(); - needsUpdate = true; - } - - int bricks = 0; - int airBlocks = 0; - HashSet tempBlockCoords = new HashSet(); - HashSet tempAirCoords = new HashSet(); - - HashSet blockCoords = new HashSet(); - HashSet airCoords = new HashSet(); - ArrayList validAirCoords = new ArrayList(); - CoordTuple returnStone; - - public void checkValidStructure () - { - bricks = 0; - airBlocks = 0; - blockCoords.clear(); - airCoords.clear(); - boolean validAir = false; - //Check for air space in front of and behind the structure - byte dir = getRenderDirection(); - switch (getRenderDirection()) - { - case 2: // +z - case 3: // -z - if (checkAir(xCoord, yCoord, zCoord - 1) && checkAir(xCoord, yCoord, zCoord + 1)) - validAir = true; - break; - case 4: // +x - case 5: // -x - if (checkAir(xCoord - 1, yCoord, zCoord) && checkAir(xCoord + 1, yCoord, zCoord)) - validAir = true; - break; - } - - //Recurse the structure - boolean validBlocks = false; - - int xPos = 0, zPos = 0; - if (dir == 2) - xPos = 1; - if (dir == 3) - xPos = -1; - if (dir == 4) - zPos = -1; - if (dir == 5) - zPos = 1; - - returnStone = new CoordTuple(xCoord - xPos, yCoord, zCoord - zPos); - if (initialRecurseLayer(xCoord + xPos, yCoord, zCoord + zPos)) - { - xPos = 0; - zPos = 0; - switch (dir) - { - case 2: // +z - zPos = 1; - break; - case 3: // -z - zPos = -1; - break; - case 4: // +x - xPos = 1; - break; - case 5: // -x - xPos = -1; - break; - } - if (!worldObj.isRemote) - System.out.println("Bricks in recursion: " + blockCoords.size()); - blockCoords.clear(); - bricks = 0; - - //Does the actual adding of blocks in the ring - boolean sealed = floodTest(xCoord + xPos, yCoord, zCoord + zPos); - if (!worldObj.isRemote) - { - System.out.println("Air in ring: " + airBlocks); - System.out.println("Bricks in ring: " + bricks); - } - - if (sealed) - { - tempAirCoords = new HashSet(airCoords); - tempBlockCoords = new HashSet(blockCoords); - - if (recurseStructureDown(yCoord - 1)) - { - completeStructure = true; - recurseStructureUp(yCoord + 1); - - if (!worldObj.isRemote) - { - System.out.println("Air in structure: " + airCoords.size()); - System.out.println("Bricks in structure: " + blockCoords.size()); - } - } - } - } - - } - - protected boolean checkAir (int x, int y, int z) - { - Block block = Block.blocksList[worldObj.getBlockId(x, y, z)]; - if (block == null || block.isAirBlock(worldObj, x, y, z)) - return true; - - return false; - } - - protected boolean checkServant (int x, int y, int z) - { - Block block = Block.blocksList[worldObj.getBlockId(x, y, z)]; - if (block == null || block.isAirBlock(worldObj, x, y, z)) - return false; - - if (!block.hasTileEntity(worldObj.getBlockMetadata(x, y, z))) - return false; - - TileEntity be = worldObj.getBlockTileEntity(x, y, z); - if (be instanceof IServantLogic) - return ((IServantLogic) be).setPotentialMaster(this, x, y, z); - - return false; - } - - protected boolean initialRecurseLayer (int x, int y, int z) - { - if (bricks >= 4095) - return false; - - CoordTuple keystone = new CoordTuple(x, y, z); - if (keystone.equals(returnStone)) - return true; - - for (int xPos = -1; xPos <= 1; xPos++) - { - for (int zPos = -1; zPos <= 1; zPos++) - { - CoordTuple coord = new CoordTuple(x + xPos, y, z + zPos); - if (!blockCoords.contains(coord)) - { - if (isValidBlock(x + xPos, y, z + zPos)) - { - bricks++; - blockCoords.add(coord); - return initialRecurseLayer(x + xPos, y, z + zPos); - } - } - } - } - - return false; - } - - protected abstract boolean isValidBlock (int x, int y, int z); - - protected boolean floodTest (int x, int y, int z) - { - if (airBlocks >= 4095) - return false; - - for (int[] offset : validAirCoords) - { - CoordTuple coord = new CoordTuple(x + offset[0], y, z + offset[1]); - if (!airCoords.contains(coord)) - { - if (checkAir(x + offset[0], y, z + offset[1])) - { - airCoords.add(coord); - addAirBlock(coord.x, y, coord.z); - floodTest(x + offset[0], y, z + offset[1]); - } - else if (!blockCoords.contains(coord) && checkServant(x + offset[0], y, z + offset[1])) - { - bricks++; - blockCoords.add(coord); - } - } - } - return true; - } - - public boolean recurseStructureDown (int y) - { - Iterator i = tempAirCoords.iterator(); - CoordTuple coord = (CoordTuple) i.next(); - if (checkAir(coord.x, y, coord.z)) - { - boolean valid = true; - - //Air blocks - while (i.hasNext()) - { - coord = (CoordTuple) i.next(); - if (checkAir(coord.x, y, coord.z)) - { - airCoords.add(new CoordTuple(coord.x, y, coord.z)); - addAirBlock(coord.x, y, coord.z); - } - else - { - valid = false; - break; - } - } - - //Bricks - i = tempBlockCoords.iterator(); - while (i.hasNext()) - { - coord = (CoordTuple) i.next(); - if (checkServant(coord.x, y, coord.z)) - blockCoords.add(new CoordTuple(coord.x, y, coord.z)); - - else - { - valid = false; - break; - } - } - - if (valid) - return recurseStructureDown(y - 1); - } - else if (checkServant(coord.x, y, coord.z)) - { - //Bottom floor. All blocks, please vacate the elevator - boolean valid = true; - while (i.hasNext()) - { - coord = (CoordTuple) i.next(); - - if (checkServant(coord.x, y, coord.z)) - blockCoords.add(new CoordTuple(coord.x, y, coord.z)); - - else - { - valid = false; - break; - } - } - return valid; - } - - return false; - } - - public void recurseStructureUp (int y) - { - Iterator i = tempBlockCoords.iterator(); - CoordTuple coord = (CoordTuple) i.next(); - if (checkServant(coord.x, y, coord.z)) - { - boolean valid = true; - - //Bricks - while (i.hasNext()) - { - coord = (CoordTuple) i.next(); - if (checkServant(coord.x, y, coord.z)) - blockCoords.add(new CoordTuple(coord.x, y, coord.z)); - - else - { - valid = false; - break; - } - } - - //Air blocks - if (valid) - { - i = tempAirCoords.iterator(); - while (i.hasNext()) - { - coord = (CoordTuple) i.next(); - if (checkAir(coord.x, y, coord.z)) - { - airCoords.add(new CoordTuple(coord.x, y, coord.z)); - addAirBlock(coord.x, y, coord.z); - } - else - { - valid = false; - break; - } - } - } - - if (valid) - recurseStructureUp(y + 1); - } - } - - protected void addAirBlock(int x, int y, int z) - { - airBlocks++; - //worldObj.setBlock(x + offset[0], y, z + offset[1], Block.leaves.blockID); - } - - public void cleanup () - { - for (CoordTuple coord : airCoords) - { - worldObj.setBlockToAir(coord.x, coord.y, coord.z); - } - } - - void testReplaceBlocks () - { - - } - - @Override - public void notifyChange (int x, int y, int z) //The one method from IMasterLogic - { - - } } diff --git a/src/tconstruct/library/component/IComponentHolder.java b/src/tconstruct/library/component/IComponentHolder.java new file mode 100644 index 00000000..7a352af4 --- /dev/null +++ b/src/tconstruct/library/component/IComponentHolder.java @@ -0,0 +1,8 @@ +package tconstruct.library.component; + +import java.util.List; + +public interface IComponentHolder +{ + public List getComponents(); +} diff --git a/src/tconstruct/library/component/LogicComponent.java b/src/tconstruct/library/component/LogicComponent.java new file mode 100644 index 00000000..43cdac1f --- /dev/null +++ b/src/tconstruct/library/component/LogicComponent.java @@ -0,0 +1,6 @@ +package tconstruct.library.component; + +public class LogicComponent +{ + +} \ No newline at end of file diff --git a/src/tconstruct/library/component/MultiFluidTank.java b/src/tconstruct/library/component/MultiFluidTank.java new file mode 100644 index 00000000..b520ca7a --- /dev/null +++ b/src/tconstruct/library/component/MultiFluidTank.java @@ -0,0 +1,156 @@ +package tconstruct.library.component; + +import java.util.ArrayList; + +import tconstruct.library.crafting.Smeltery; +import net.minecraftforge.fluids.FluidStack; +import net.minecraftforge.fluids.FluidTankInfo; +import net.minecraftforge.fluids.IFluidTank; + +public class MultiFluidTank extends LogicComponent implements IFluidTank +{ + public ArrayList moltenMetal = new ArrayList(); + int maxLiquid; + int currentLiquid; + + @Override + public FluidStack drain (int maxDrain, boolean doDrain) + { + if (moltenMetal.size() == 0) + return null; + + FluidStack liquid = moltenMetal.get(0); + if (liquid != null) + { + if (liquid.amount - maxDrain <= 0) + { + FluidStack liq = liquid.copy(); + if (doDrain) + { + moltenMetal.remove(liquid); + currentLiquid = 0; + } + return liq; + } + else + { + if (doDrain) + { + liquid.amount -= maxDrain; + currentLiquid -= maxDrain; + } + return new FluidStack(liquid.fluidID, maxDrain, liquid.tag); + } + } + else + { + return new FluidStack(0, 0); + } + } + + @Override + public int fill (FluidStack resource, boolean doFill) + { + if (resource != null && currentLiquid < maxLiquid) + { + if (resource.amount + currentLiquid > maxLiquid) + resource.amount = maxLiquid - currentLiquid; + int amount = resource.amount; + + if (doFill) + { + if (addFluidToTank(resource, false)) + { + ArrayList alloys = Smeltery.mixMetals(moltenMetal); + for (int al = 0; al < alloys.size(); al++) + { + FluidStack liquid = (FluidStack) alloys.get(al); + addFluidToTank(liquid, true); + } + } + } + return amount; + } + else + return 0; + } + + boolean addFluidToTank (FluidStack liquid, boolean first) + { + if (moltenMetal.size() == 0) + { + moltenMetal.add(liquid.copy()); + currentLiquid += liquid.amount; + return true; + } + else + { + if (liquid.amount + currentLiquid > maxLiquid) + return false; + + currentLiquid += liquid.amount; + boolean added = false; + for (int i = 0; i < moltenMetal.size(); i++) + { + FluidStack l = moltenMetal.get(i); + if (l.isFluidEqual(liquid)) + { + l.amount += liquid.amount; + added = true; + } + if (l.amount <= 0) + { + moltenMetal.remove(l); + i--; + } + } + if (!added) + { + if (first) + moltenMetal.add(0, liquid.copy()); + else + moltenMetal.add(liquid.copy()); + } + return true; + } + } + + @Override + public FluidStack getFluid () + { + if (moltenMetal.size() == 0) + return null; + return moltenMetal.get(0); + } + + @Override + public FluidTankInfo getInfo () + { + return new FluidTankInfo(this); + } + + public FluidTankInfo[] getMultiTankInfo () + { + FluidTankInfo[] info = new FluidTankInfo[moltenMetal.size() + 1]; + for (int i = 0; i < moltenMetal.size(); i++) + { + FluidStack fluid = moltenMetal.get(i); + info[i] = new FluidTankInfo(fluid.copy(), fluid.amount); + } + info[moltenMetal.size()] = new FluidTankInfo(null, maxLiquid - currentLiquid); + return info; + } + + @Override + public int getFluidAmount () + { + return currentLiquid; + } + + @Override + public int getCapacity () + { + return maxLiquid; + } + +} diff --git a/src/tconstruct/library/component/SmelteryComponent.java b/src/tconstruct/library/component/SmelteryComponent.java new file mode 100644 index 00000000..7262ec65 --- /dev/null +++ b/src/tconstruct/library/component/SmelteryComponent.java @@ -0,0 +1,125 @@ +package tconstruct.library.component; + +import java.util.ArrayList; + +import net.minecraft.inventory.IInventory; +import net.minecraft.item.ItemStack; +import net.minecraft.world.World; +import net.minecraftforge.fluids.FluidStack; +import tconstruct.library.crafting.Smeltery; + +public class SmelteryComponent extends LogicComponent +{ + World world; + IInventory master; + MultiFluidTank multitank; + + int internalTemp; + public int useTime; + public int fuelGague; + public int fuelAmount; + boolean inUse; + + public int[] activeTemps; + public int[] meltingTemps; + + public SmelteryComponent(IInventory inventory, World world, MultiFluidTank multitank, int maxTemp) + { + master = inventory; + this.world = world; + this.multitank = multitank; + internalTemp = maxTemp; + } + + public void adjustSize (int size, boolean forceAdjust) + { + if (size != activeTemps.length || forceAdjust) + { + int[] tempActive = activeTemps; + activeTemps = new int[size]; + int activeLength = tempActive.length > activeTemps.length ? activeTemps.length : tempActive.length; + System.arraycopy(tempActive, 0, activeTemps, 0, activeLength); + + int[] tempMelting = meltingTemps; + meltingTemps = new int[size]; + int meltingLength = tempMelting.length > meltingTemps.length ? meltingTemps.length : tempMelting.length; + System.arraycopy(tempMelting, 0, meltingTemps, 0, meltingLength); + + if (activeTemps.length > 0 && activeTemps.length > tempActive.length) + { + for (int i = tempActive.length; i < activeTemps.length; i++) + { + activeTemps[i] = 20; + meltingTemps[i] = 20; + } + } + } + } + + void heatItems () + { + if (useTime > 0) + { + boolean hasUse = false; + for (int i = 0; i < meltingTemps.length; i++) + { + ItemStack slot = master.getStackInSlot(i); + if (meltingTemps[i] > 20 && slot != null) + { + hasUse = true; + if (activeTemps[i] < internalTemp && activeTemps[i] < meltingTemps[i]) + { + activeTemps[i] += 1; + } + else if (activeTemps[i] >= meltingTemps[i]) + { + if (!world.isRemote) + { + FluidStack result = getResultFor(slot); + if (result != null) + { + if (multitank.addFluidToTank(result, false)) + { + master.setInventorySlotContents(i, null); + activeTemps[i] = 20; + ArrayList alloys = Smeltery.mixMetals(multitank.moltenMetal); + for (int al = 0; al < alloys.size(); al++) + { + FluidStack liquid = (FluidStack) alloys.get(al); + multitank.addFluidToTank(liquid, true); + } + master.onInventoryChanged(); + } + } + } + } + + } + + else + activeTemps[i] = 20; + } + inUse = hasUse; + } + } + + public FluidStack getResultFor (ItemStack stack) + { + return Smeltery.instance.getSmelteryResult(stack); + } + + public int getInternalTemperature () + { + return internalTemp; + } + + public int getTempForSlot (int slot) + { + return activeTemps[slot]; + } + + public int getMeltingPointForSlot (int slot) + { + return meltingTemps[slot]; + } +} diff --git a/src/tconstruct/library/component/TankLayerScan.java b/src/tconstruct/library/component/TankLayerScan.java new file mode 100644 index 00000000..feff6201 --- /dev/null +++ b/src/tconstruct/library/component/TankLayerScan.java @@ -0,0 +1,354 @@ +package tconstruct.library.component; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Iterator; + +import net.minecraft.block.Block; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.World; +import tconstruct.library.util.CoordTuple; +import tconstruct.library.util.IFacingLogic; +import tconstruct.library.util.IMasterLogic; +import tconstruct.library.util.IServantLogic; + +public class TankLayerScan extends LogicComponent +{ + TileEntity master; + IMasterLogic imaster; + World world; + Block[] scanBlocks; + + public boolean completeStructure; + + int bricks = 0; + int airBlocks = 0; + HashSet tempBlockCoords = new HashSet(); + HashSet tempAirCoords = new HashSet(); + + public HashSet blockCoords = new HashSet(); + public HashSet airCoords = new HashSet(); + ArrayList validAirCoords = new ArrayList(); + CoordTuple returnStone; + + public TankLayerScan(TileEntity te, Block... id) + { + assert te instanceof IMasterLogic : "TileEntity must be an instance of IMasterLogic"; + master = te; + imaster = (IMasterLogic) te; + scanBlocks = id; + validAirCoords.add(new int[] { 1, 0 }); + validAirCoords.add(new int[] { -1, 0 }); + validAirCoords.add(new int[] { 0, 1 }); + validAirCoords.add(new int[] { 0, -1 }); + } + + public void checkValidStructure () + { + bricks = 0; + airBlocks = 0; + blockCoords.clear(); + airCoords.clear(); + boolean validAir = false; + //Check for air space in front of and behind the structure + byte dir = getDirection(); + switch (getDirection()) + { + case 2: // +z + case 3: // -z + if (checkAir(master.xCoord, master.yCoord, master.zCoord - 1) && checkAir(master.xCoord, master.yCoord, master.zCoord + 1)) + validAir = true; + break; + case 4: // +x + case 5: // -x + if (checkAir(master.xCoord - 1, master.yCoord, master.zCoord) && checkAir(master.xCoord + 1, master.yCoord, master.zCoord)) + validAir = true; + break; + } + + //Recurse the structure + boolean validBlocks = false; + + int xPos = 0, zPos = 0; + if (dir == 2) + xPos = 1; + if (dir == 3) + xPos = -1; + if (dir == 4) + zPos = -1; + if (dir == 5) + zPos = 1; + + returnStone = new CoordTuple(master.xCoord - xPos, master.yCoord, master.zCoord - zPos); + if (initialRecurseLayer(master.xCoord + xPos, master.yCoord, master.zCoord + zPos)) + { + xPos = 0; + zPos = 0; + switch (dir) + { + case 2: // +z + zPos = 1; + break; + case 3: // -z + zPos = -1; + break; + case 4: // +x + xPos = 1; + break; + case 5: // -x + xPos = -1; + break; + } + if (!world.isRemote) + System.out.println("Bricks in recursion: " + blockCoords.size()); + blockCoords.clear(); + bricks = 0; + + //Does the actual adding of blocks in the ring + boolean sealed = floodTest(master.xCoord + xPos, master.yCoord, master.zCoord + zPos); + if (!world.isRemote) + { + System.out.println("Air in ring: " + airBlocks); + System.out.println("Bricks in ring: " + bricks); + } + + if (sealed) + { + tempAirCoords = new HashSet(airCoords); + tempBlockCoords = new HashSet(blockCoords); + + if (recurseStructureDown(master.yCoord - 1)) + { + completeStructure = true; + recurseStructureUp(master.yCoord + 1); + + if (!world.isRemote) + { + System.out.println("Air in structure: " + airCoords.size()); + System.out.println("Bricks in structure: " + blockCoords.size()); + } + } + } + } + + } + + private byte getDirection () + { + if (master instanceof IFacingLogic) + return ((IFacingLogic) master).getRenderDirection(); + return 0; + } + + protected boolean checkAir (int x, int y, int z) + { + Block block = Block.blocksList[world.getBlockId(x, y, z)]; + if (block == null || block.isAirBlock(world, x, y, z)) + return true; + + return false; + } + + protected boolean checkServant (int x, int y, int z) + { + Block block = Block.blocksList[world.getBlockId(x, y, z)]; + if (block == null || block.isAirBlock(world, x, y, z)) + return false; + + if (!block.hasTileEntity(world.getBlockMetadata(x, y, z))) + return false; + + TileEntity be = world.getBlockTileEntity(x, y, z); + if (be instanceof IServantLogic) + return ((IServantLogic) be).setPotentialMaster(this.imaster, x, y, z); + + return false; + } + + protected boolean initialRecurseLayer (int x, int y, int z) + { + if (bricks >= 4095) + return false; + + CoordTuple keystone = new CoordTuple(x, y, z); + if (keystone.equals(returnStone)) + return true; + + for (int xPos = -1; xPos <= 1; xPos++) + { + for (int zPos = -1; zPos <= 1; zPos++) + { + CoordTuple coord = new CoordTuple(x + xPos, y, z + zPos); + if (!blockCoords.contains(coord)) + { + if (isValidBlock(x + xPos, y, z + zPos)) + { + bricks++; + blockCoords.add(coord); + return initialRecurseLayer(x + xPos, y, z + zPos); + } + } + } + } + + return false; + } + + protected boolean isValidBlock (int x, int y, int z) + { + Block block = Block.blocksList[world.getBlockId(x, y, z)]; + if (block != null) + { + for (int i = 0; i < scanBlocks.length; i++) + { + if (block == scanBlocks[i]) + return true; + } + } + return false; + } + + protected boolean floodTest (int x, int y, int z) + { + if (airBlocks >= 4095) + return false; + + for (int[] offset : validAirCoords) + { + CoordTuple coord = new CoordTuple(x + offset[0], y, z + offset[1]); + if (!airCoords.contains(coord)) + { + if (checkAir(x + offset[0], y, z + offset[1])) + { + airCoords.add(coord); + addAirBlock(coord.x, y, coord.z); + floodTest(x + offset[0], y, z + offset[1]); + } + else if (!blockCoords.contains(coord) && checkServant(x + offset[0], y, z + offset[1])) + { + bricks++; + blockCoords.add(coord); + } + } + } + return true; + } + + public boolean recurseStructureDown (int y) + { + Iterator i = tempAirCoords.iterator(); + CoordTuple coord = (CoordTuple) i.next(); + if (checkAir(coord.x, y, coord.z)) + { + boolean valid = true; + + //Air blocks + while (i.hasNext()) + { + coord = (CoordTuple) i.next(); + if (checkAir(coord.x, y, coord.z)) + { + airCoords.add(new CoordTuple(coord.x, y, coord.z)); + addAirBlock(coord.x, y, coord.z); + } + else + { + valid = false; + break; + } + } + + //Bricks + i = tempBlockCoords.iterator(); + while (i.hasNext()) + { + coord = (CoordTuple) i.next(); + if (checkServant(coord.x, y, coord.z)) + blockCoords.add(new CoordTuple(coord.x, y, coord.z)); + + else + { + valid = false; + break; + } + } + + if (valid) + return recurseStructureDown(y - 1); + } + else if (checkServant(coord.x, y, coord.z)) + { + //Bottom floor. All blocks, please vacate the elevator + boolean valid = true; + while (i.hasNext()) + { + coord = (CoordTuple) i.next(); + + if (checkServant(coord.x, y, coord.z)) + blockCoords.add(new CoordTuple(coord.x, y, coord.z)); + + else + { + valid = false; + break; + } + } + return valid; + } + + return false; + } + + public void recurseStructureUp (int y) + { + Iterator i = tempBlockCoords.iterator(); + CoordTuple coord = (CoordTuple) i.next(); + if (checkServant(coord.x, y, coord.z)) + { + boolean valid = true; + + //Bricks + while (i.hasNext()) + { + coord = (CoordTuple) i.next(); + if (checkServant(coord.x, y, coord.z)) + blockCoords.add(new CoordTuple(coord.x, y, coord.z)); + + else + { + valid = false; + break; + } + } + + //Air blocks + if (valid) + { + i = tempAirCoords.iterator(); + while (i.hasNext()) + { + coord = (CoordTuple) i.next(); + if (checkAir(coord.x, y, coord.z)) + { + airCoords.add(new CoordTuple(coord.x, y, coord.z)); + addAirBlock(coord.x, y, coord.z); + } + else + { + valid = false; + break; + } + } + } + + if (valid) + recurseStructureUp(y + 1); + } + } + + protected void addAirBlock (int x, int y, int z) + { + airBlocks++; + //world.setBlock(x + offset[0], y, z + offset[1], Block.leaves.blockID); + } +}