Files
TinkersConstruct/src/tconstruct/library/component/TankLayerScan.java
T

509 lines
16 KiB
Java
Raw Normal View History

2013-09-29 19:38:20 -07:00
package tconstruct.library.component;
import java.util.ArrayList;
2013-10-05 22:49:39 -07:00
import java.util.Collections;
2013-09-29 19:38:20 -07:00
import java.util.Iterator;
2013-10-05 22:49:39 -07:00
import java.util.HashSet;
2013-09-29 19:38:20 -07:00
import net.minecraft.block.Block;
2013-10-05 11:21:32 -07:00
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagIntArray;
import net.minecraft.nbt.NBTTagList;
2013-09-29 19:38:20 -07:00
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.classloading.FMLForgePlugin;
2013-10-17 20:54:27 +01:00
import scala.compat.Platform$;
import tconstruct.TConstruct;
2013-09-29 19:38:20 -07:00
import tconstruct.library.util.CoordTuple;
2013-10-05 22:49:39 -07:00
import tconstruct.library.util.CoordTupleSort;
2013-09-29 19:38:20 -07:00
import tconstruct.library.util.IFacingLogic;
import tconstruct.library.util.IMasterLogic;
import tconstruct.library.util.IServantLogic;
public class TankLayerScan extends LogicComponent
{
2013-10-01 19:44:04 -07:00
protected TileEntity master;
protected IMasterLogic imaster;
protected Block[] scanBlocks;
2013-10-05 22:49:39 -07:00
protected CoordTuple masterCoord;
2013-09-29 19:38:20 -07:00
2013-10-01 19:44:04 -07:00
protected boolean completeStructure;
2013-09-29 19:38:20 -07:00
2013-10-01 19:44:04 -07:00
protected int bricks = 0;
protected int airBlocks = 0;
2013-10-05 22:49:39 -07:00
protected HashSet<CoordTuple> layerBlockCoords = new HashSet<CoordTuple>();
protected HashSet<CoordTuple> layerAirCoords = new HashSet<CoordTuple>();
2013-09-29 19:38:20 -07:00
2013-10-07 20:49:55 -07:00
public ArrayList<CoordTuple> blockCoords = new ArrayList<CoordTuple>();
public ArrayList<CoordTuple> airCoords = new ArrayList<CoordTuple>();
2013-10-01 19:44:04 -07:00
protected ArrayList<int[]> validAirCoords = new ArrayList<int[]>();
protected CoordTuple returnStone;
2013-10-05 11:21:32 -07:00
private static boolean debug = System.getenv("DBG_MANTLE_TankLayerScan") != null;
2013-10-17 20:54:27 +01:00
private static int MAX_LAYER_RECURSION_DEPTH = System.getProperty("os.arch").equals("amd64") ? 4000 : 2000; // Recursion causes overflows on 32-bit, so reduce if not 64-bit
2013-09-29 19:38:20 -07:00
2013-10-01 19:44:04 -07:00
public TankLayerScan(TileEntity te, Block... ids)
2013-09-29 19:38:20 -07:00
{
assert te instanceof IMasterLogic : "TileEntity must be an instance of IMasterLogic";
master = te;
imaster = (IMasterLogic) te;
2013-10-01 19:44:04 -07:00
scanBlocks = ids;
2013-10-05 22:49:39 -07:00
masterCoord = new CoordTuple(te.xCoord, te.yCoord, te.zCoord);
2013-09-29 19:38:20 -07:00
validAirCoords.add(new int[] { 1, 0 });
validAirCoords.add(new int[] { -1, 0 });
validAirCoords.add(new int[] { 0, 1 });
validAirCoords.add(new int[] { 0, -1 });
if (debug)
{
TConstruct.logger.info("In debug mode: " + this);
TConstruct.logger.info("Using recursion size " + MAX_LAYER_RECURSION_DEPTH + " on JVM arch " + System.getProperty("os.arch"));
}
2013-09-29 19:38:20 -07:00
}
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;
}
2013-10-01 19:44:04 -07:00
if (!world.isRemote && debug)
TConstruct.logger.info("Bricks in recursion: " + blockCoords.size());
2013-09-29 19:38:20 -07:00
blockCoords.clear();
bricks = 0;
//Does the actual adding of blocks in the ring
boolean sealed = floodTest(master.xCoord + xPos, master.yCoord, master.zCoord + zPos);
2013-10-01 19:44:04 -07:00
if (!world.isRemote && debug)
2013-09-29 19:38:20 -07:00
{
TConstruct.logger.info("Air in ring: " + airBlocks);
TConstruct.logger.info("Bricks in ring: " + bricks);
2013-09-29 19:38:20 -07:00
}
if (sealed)
{
2013-10-05 22:49:39 -07:00
blockCoords.add(new CoordTuple(master.xCoord, master.yCoord, master.zCoord)); //Don't forget me!
layerAirCoords = new HashSet<CoordTuple>(airCoords);
layerBlockCoords = new HashSet<CoordTuple>(blockCoords);
2013-09-29 19:38:20 -07:00
2013-10-05 11:21:32 -07:00
int lowY = recurseStructureDown(master.yCoord - 1);
if (lowY != -1)
2013-09-29 19:38:20 -07:00
{
completeStructure = true;
recurseStructureUp(master.yCoord + 1);
2013-10-06 09:24:58 -07:00
finalizeStructure();
2013-09-29 19:38:20 -07:00
2013-10-01 19:44:04 -07:00
if (!world.isRemote && debug)
2013-09-29 19:38:20 -07:00
{
TConstruct.logger.info("Air in structure: " + airCoords.size());
TConstruct.logger.info("Bricks in structure: " + blockCoords.size());
2013-09-29 19:38:20 -07:00
}
}
}
}
2013-10-01 19:44:04 -07:00
}
2013-09-29 19:38:20 -07:00
@SuppressWarnings({"unchecked"})
2013-10-06 09:24:58 -07:00
protected void finalizeStructure ()
2013-10-05 22:49:39 -07:00
{
Collections.sort(airCoords, new CoordTupleSort());
2013-10-09 22:21:19 -07:00
for (CoordTuple coord : blockCoords)
{
TileEntity servant = world.getBlockTileEntity(coord.x, coord.y, coord.z);
if (servant instanceof IServantLogic)
((IServantLogic) servant).verifyMaster(imaster, world, master.xCoord, master.yCoord, master.zCoord);
}
2013-10-05 22:49:39 -07:00
}
2013-10-01 19:44:04 -07:00
public boolean isComplete ()
{
return completeStructure;
}
2013-10-05 11:21:32 -07:00
2013-10-07 20:49:55 -07:00
public int getAirLayerSize ()
{
return layerAirCoords.size();
}
2013-10-05 11:21:32 -07:00
public int getAirSize ()
2013-10-01 19:44:04 -07:00
{
return airBlocks;
2013-09-29 19:38:20 -07:00
}
2013-10-07 20:49:55 -07:00
public CoordTuple getAirByIndex (int index)
{
if (index >= airCoords.size() || index < 0)
return null;
return airCoords.get(index);
}
2013-09-29 19:38:20 -07:00
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)];
2013-10-05 22:49:39 -07:00
if (block == null || block.isAirBlock(world, x, y, z))// || block == Block.glass)
2013-09-29 19:38:20 -07:00
return true;
return false;
}
protected boolean checkServant (int x, int y, int z)
{
Block block = Block.blocksList[world.getBlockId(x, y, z)];
2013-10-01 19:44:04 -07:00
if (block == null || block.isAirBlock(world, x, y, z) || !isValidBlock(x, y, z))
2013-09-29 19:38:20 -07:00
return false;
if (!block.hasTileEntity(world.getBlockMetadata(x, y, z)))
return false;
TileEntity be = world.getBlockTileEntity(x, y, z);
if (be instanceof IServantLogic)
2013-10-01 19:44:04 -07:00
return ((IServantLogic) be).setPotentialMaster(this.imaster, this.world, x, y, z);
2013-09-29 19:38:20 -07:00
return false;
}
protected boolean initialRecurseLayer (int x, int y, int z)
{
if (bricks > MAX_LAYER_RECURSION_DEPTH)
2013-09-29 19:38:20 -07:00
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 > MAX_LAYER_RECURSION_DEPTH)
2013-09-29 19:38:20 -07:00
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]))
{
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;
}
2013-10-05 11:21:32 -07:00
public int recurseStructureDown (int y)
2013-09-29 19:38:20 -07:00
{
2013-10-05 11:21:32 -07:00
Iterator i = layerAirCoords.iterator();
2013-10-01 19:44:04 -07:00
if (i.hasNext())
2013-09-29 19:38:20 -07:00
{
2013-10-01 19:44:04 -07:00
CoordTuple coord = (CoordTuple) i.next();
if (checkAir(coord.x, y, coord.z))
2013-09-29 19:38:20 -07:00
{
2013-10-01 19:44:04 -07:00
boolean valid = true;
2013-10-05 22:49:39 -07:00
addAirBlock(coord.x, y, coord.z); //Don't skip first one
2013-10-01 19:44:04 -07:00
//Air blocks
while (i.hasNext())
2013-09-29 19:38:20 -07:00
{
2013-10-01 19:44:04 -07:00
coord = (CoordTuple) i.next();
if (checkAir(coord.x, y, coord.z))
{
addAirBlock(coord.x, y, coord.z);
}
else
{
valid = false;
break;
}
2013-09-29 19:38:20 -07:00
}
2013-10-01 19:44:04 -07:00
//Bricks
2013-10-05 11:21:32 -07:00
i = layerBlockCoords.iterator();
2013-10-01 19:44:04 -07:00
while (i.hasNext())
2013-09-29 19:38:20 -07:00
{
2013-10-01 19:44:04 -07:00
coord = (CoordTuple) i.next();
if (checkServant(coord.x, y, coord.z))
blockCoords.add(new CoordTuple(coord.x, y, coord.z));
else
{
valid = false;
break;
}
2013-09-29 19:38:20 -07:00
}
2013-10-01 19:44:04 -07:00
if (valid)
return recurseStructureDown(y - 1);
}
else if (checkServant(coord.x, y, coord.z))
2013-09-29 19:38:20 -07:00
{
2013-10-01 19:44:04 -07:00
//Bottom floor. All blocks, please vacate the elevator
boolean valid = true;
while (i.hasNext())
{
coord = (CoordTuple) i.next();
2013-09-29 19:38:20 -07:00
2013-10-01 19:44:04 -07:00
if (checkServant(coord.x, y, coord.z))
blockCoords.add(new CoordTuple(coord.x, y, coord.z));
2013-09-29 19:38:20 -07:00
2013-10-01 19:44:04 -07:00
else
{
valid = false;
break;
}
2013-09-29 19:38:20 -07:00
}
2013-10-05 11:21:32 -07:00
if (valid)
2013-10-05 22:49:39 -07:00
return y + 1;
2013-09-29 19:38:20 -07:00
}
}
2013-10-05 11:21:32 -07:00
return -1;
2013-09-29 19:38:20 -07:00
}
public void recurseStructureUp (int y)
{
2013-10-05 11:21:32 -07:00
Iterator i = layerBlockCoords.iterator();
2013-10-01 19:44:04 -07:00
if (i.hasNext())
2013-09-29 19:38:20 -07:00
{
2013-10-01 19:44:04 -07:00
CoordTuple coord = (CoordTuple) i.next();
if (checkServant(coord.x, y, coord.z))
2013-09-29 19:38:20 -07:00
{
2013-10-01 19:44:04 -07:00
boolean valid = true;
2013-09-29 19:38:20 -07:00
2013-10-01 19:44:04 -07:00
//Bricks
2013-09-29 19:38:20 -07:00
while (i.hasNext())
{
coord = (CoordTuple) i.next();
2013-10-01 19:44:04 -07:00
if (checkServant(coord.x, y, coord.z))
blockCoords.add(new CoordTuple(coord.x, y, coord.z));
2013-09-29 19:38:20 -07:00
else
{
valid = false;
break;
}
}
2013-10-01 19:44:04 -07:00
//Air blocks
if (valid)
{
2013-10-05 11:21:32 -07:00
i = layerAirCoords.iterator();
2013-10-01 19:44:04 -07:00
while (i.hasNext())
{
coord = (CoordTuple) i.next();
if (checkAir(coord.x, y, coord.z))
{
addAirBlock(coord.x, y, coord.z);
}
else
{
valid = false;
break;
}
}
}
if (valid)
recurseStructureUp(y + 1);
}
2013-09-29 19:38:20 -07:00
}
}
protected void addAirBlock (int x, int y, int z)
{
airBlocks++;
2013-10-01 19:44:04 -07:00
airCoords.add(new CoordTuple(x, y, z));
}
/** Do any necessary cleanup here. Remove air blocks, invalidate servants, etc */
public void cleanup ()
{
Iterator i = blockCoords.iterator();
while (i.hasNext())
{
CoordTuple coord = (CoordTuple) i.next();
2013-10-05 22:49:39 -07:00
TileEntity te = world.getBlockTileEntity(coord.x, coord.y, coord.z);
if (te != null && te instanceof IServantLogic)
{
2013-10-07 20:49:55 -07:00
((IServantLogic) te).invalidateMaster(imaster, world, master.xCoord, master.yCoord, master.zCoord);
2013-10-05 22:49:39 -07:00
}
2013-10-01 19:44:04 -07:00
}
2013-09-29 19:38:20 -07:00
}
2013-10-05 11:21:32 -07:00
2013-10-10 00:51:38 -07:00
/* @Override
public void readFromNBT (NBTTagCompound tags)
{
super.readFromNBT(tags);
}*/
public void readNetworkNBT (NBTTagCompound tags)
2013-10-05 11:21:32 -07:00
{
2013-10-10 00:51:38 -07:00
completeStructure = tags.getBoolean("Complete");
2013-10-05 11:21:32 -07:00
NBTTagList layerAir = tags.getTagList("AirLayer");
if (layerAir != null)
{
layerAirCoords.clear();
for (int i = 0; i < layerAir.tagCount(); ++i)
{
NBTTagIntArray tag = (NBTTagIntArray) layerAir.tagAt(i);
int[] coord = tag.intArray;
layerAirCoords.add(new CoordTuple(coord[0], coord[1], coord[2]));
}
}
2013-10-05 22:49:39 -07:00
2013-10-18 21:05:59 +01:00
NBTTagList blocks = tags.getTagList("Blocks");
2013-10-05 11:21:32 -07:00
if (blocks != null)
{
blockCoords.clear();
for (int i = 0; i < blocks.tagCount(); ++i)
{
NBTTagIntArray tag = (NBTTagIntArray) blocks.tagAt(i);
int[] coord = tag.intArray;
blockCoords.add(new CoordTuple(coord[0], coord[1], coord[2]));
}
}
2013-10-05 22:49:39 -07:00
2013-10-18 21:05:59 +01:00
NBTTagList air = tags.getTagList("Air");
2013-10-05 11:21:32 -07:00
if (air != null)
{
airCoords.clear();
for (int i = 0; i < air.tagCount(); ++i)
{
NBTTagIntArray tag = (NBTTagIntArray) air.tagAt(i);
int[] coord = tag.intArray;
airCoords.add(new CoordTuple(coord[0], coord[1], coord[2]));
}
}
2013-10-07 20:49:55 -07:00
2013-10-05 22:49:39 -07:00
}
2013-10-10 00:51:38 -07:00
/*@Override
public void writeToNBT (NBTTagCompound tags)
2013-10-05 22:49:39 -07:00
{
2013-10-10 00:51:38 -07:00
super.writeToNBT(tags);
}*/
2013-10-05 11:21:32 -07:00
2013-10-10 00:51:38 -07:00
public void writeNetworkNBT (NBTTagCompound tags)
2013-10-05 11:21:32 -07:00
{
2013-10-10 00:51:38 -07:00
tags.setBoolean("Complete", completeStructure);
2013-10-05 11:21:32 -07:00
NBTTagList layerAir = new NBTTagList();
for (CoordTuple coord : layerAirCoords)
{
layerAir.appendTag(new NBTTagIntArray("coord", new int[] { coord.x, coord.y, coord.z }));
}
tags.setTag("AirLayer", layerAir);
2013-10-05 22:49:39 -07:00
2013-10-05 11:21:32 -07:00
NBTTagList blocks = new NBTTagList();
for (CoordTuple coord : blockCoords)
{
blocks.appendTag(new NBTTagIntArray("coord", new int[] { coord.x, coord.y, coord.z }));
}
tags.setTag("Blocks", blocks);
2013-10-05 22:49:39 -07:00
2013-10-05 11:21:32 -07:00
NBTTagList air = new NBTTagList();
for (CoordTuple coord : airCoords)
{
air.appendTag(new NBTTagIntArray("coord", new int[] { coord.x, coord.y, coord.z }));
}
tags.setTag("Air", air);
}
2013-09-29 19:38:20 -07:00
}