TinkersConstruct/mods/tinker/tconstruct/blocks/LiquidMetalFlowing.java

329 lines
7.1 KiB
Java
Raw Normal View History

2013-03-16 23:54:39 -07:00
package mods.tinker.tconstruct.blocks;
2013-02-06 18:19:14 -08:00
import java.util.Random;
2013-04-28 02:25:06 -07:00
import mods.tinker.tconstruct.blocks.logic.LiquidTextureLogic;
import mods.tinker.tconstruct.common.TContent;
2013-02-06 18:19:14 -08:00
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
2013-03-01 13:35:24 -08:00
import net.minecraft.tileentity.TileEntity;
2013-02-06 18:19:14 -08:00
import net.minecraft.world.World;
import net.minecraftforge.liquids.ILiquid;
2013-03-01 13:35:24 -08:00
public class LiquidMetalFlowing extends LiquidMetalBase
implements ILiquid
2013-02-06 18:19:14 -08:00
{
boolean isOptimalFlowDirection[] = new boolean[4];
int flowCost[] = new int[4];
public LiquidMetalFlowing(int id)
{
2013-03-01 13:35:24 -08:00
super(id, TContent.liquidMetal);
2013-02-06 18:19:14 -08:00
}
2013-03-01 13:35:24 -08:00
/*@Override
public int getBlockTexture(IBlockAccess world, int x, int y, int z, int side)
{
LiquidTextureLogic logic = (LiquidTextureLogic) world.getBlockTileEntity(x, y, z);
return logic.getTexturePos() + 1;
}*/
2013-02-06 18:19:14 -08:00
private void updateFlow (World world, int x, int y, int z)
{
2013-02-11 12:25:40 -08:00
//System.out.println("x: "+x+", y: "+y+", z: "+z);
2013-03-01 13:35:24 -08:00
int tex = ((LiquidTextureLogic) world.getBlockTileEntity(x, y, z)).getLiquidType();
2013-02-11 12:25:40 -08:00
int meta = world.getBlockMetadata(x, y, z);
2013-03-21 17:38:04 -07:00
world.setBlock(x, y, z, stillLiquidId(), meta, 3);
2013-02-06 18:19:14 -08:00
world.markBlockRangeForRenderUpdate(x, y, z, x, y, z);
world.markBlockForUpdate(x, y, z);
2013-03-16 23:54:39 -07:00
((LiquidTextureLogic) world.getBlockTileEntity(x, y, z)).setLiquidType(tex);
2013-02-06 18:19:14 -08:00
}
@Override
2013-02-11 12:25:40 -08:00
public void updateTick (World world, int x, int y, int z, Random random)
2013-02-06 18:19:14 -08:00
{
2013-02-11 12:25:40 -08:00
//System.out.println("x: "+x+", y: "+y+", z: "+z);
2013-03-01 13:35:24 -08:00
int tex = ((LiquidTextureLogic) world.getBlockTileEntity(x, y, z)).getLiquidType();
2013-03-16 23:54:39 -07:00
int flow = getFlowDecay(world, x, y, z);
2013-02-06 18:19:14 -08:00
byte byte0 = 1;
boolean flag = true;
2013-03-16 23:54:39 -07:00
if (flow > 0)
2013-02-06 18:19:14 -08:00
{
int i1 = -100;
2013-02-11 12:25:40 -08:00
i1 = getSmallestFlowDecay(world, x - 1, y, z, i1);
i1 = getSmallestFlowDecay(world, x + 1, y, z, i1);
i1 = getSmallestFlowDecay(world, x, y, z - 1, i1);
i1 = getSmallestFlowDecay(world, x, y, z + 1, i1);
2013-02-06 18:19:14 -08:00
int j1 = i1 + byte0;
if (j1 >= 8 || i1 < 0)
{
j1 = -1;
}
2013-02-11 12:25:40 -08:00
if (getFlowDecay(world, x, y + 1, z) >= 0)
2013-02-06 18:19:14 -08:00
{
2013-02-11 12:25:40 -08:00
int l1 = getFlowDecay(world, x, y + 1, z);
2013-02-06 18:19:14 -08:00
if (l1 >= 8)
{
j1 = l1;
}
else
{
j1 = l1 + 8;
}
}
2013-03-16 23:54:39 -07:00
if (j1 != flow)
2013-02-06 18:19:14 -08:00
{
2013-03-16 23:54:39 -07:00
flow = j1;
if (flow < 0)
2013-02-06 18:19:14 -08:00
{
2013-03-21 17:38:04 -07:00
world.setBlockToAir(x, y, z);
2013-02-06 18:19:14 -08:00
}
else
{
2013-03-16 23:54:39 -07:00
world.setBlockMetadataWithNotify(x, y, z, flow, 2);
2013-02-11 12:25:40 -08:00
world.scheduleBlockUpdate(x, y, z, blockID, tickRate());
world.notifyBlocksOfNeighborChange(x, y, z, blockID);
2013-02-06 18:19:14 -08:00
}
}
else if (flag)
{
2013-02-11 12:25:40 -08:00
updateFlow(world, x, y, z);
2013-02-06 18:19:14 -08:00
}
}
else
{
2013-02-11 12:25:40 -08:00
updateFlow(world, x, y, z);
2013-02-06 18:19:14 -08:00
}
2013-02-11 12:25:40 -08:00
if (liquidCanDisplaceBlock(world, x, y - 1, z))
2013-02-06 18:19:14 -08:00
{
2013-03-16 23:54:39 -07:00
if (flow >= 8)
2013-02-06 18:19:14 -08:00
{
2013-03-21 17:38:04 -07:00
world.setBlock(x, y - 1, z, blockID, flow, 3);
2013-02-06 18:19:14 -08:00
}
else
{
2013-03-21 17:38:04 -07:00
world.setBlock(x, y - 1, z, blockID, flow + 8, 3);
2013-02-06 18:19:14 -08:00
}
2013-03-16 23:54:39 -07:00
((LiquidTextureLogic) world.getBlockTileEntity(x, y - 1, z)).setLiquidType(tex);
2013-02-06 18:19:14 -08:00
}
2013-03-16 23:54:39 -07:00
else if (flow >= 0 && (flow == 0 || blockBlocksFlow(world, x, y - 1, z)))
2013-02-06 18:19:14 -08:00
{
2013-02-11 12:25:40 -08:00
boolean aflag[] = getOptimalFlowDirections(world, x, y, z);
2013-03-16 23:54:39 -07:00
int k1 = flow + byte0;
if (flow >= 8)
2013-02-06 18:19:14 -08:00
{
k1 = 1;
}
if (k1 >= 8)
return;
if (aflag[0])
{
2013-03-01 13:35:24 -08:00
flowIntoBlock(world, x - 1, y, z, k1, tex);
2013-02-06 18:19:14 -08:00
}
if (aflag[1])
{
2013-03-01 13:35:24 -08:00
flowIntoBlock(world, x + 1, y, z, k1, tex);
2013-02-06 18:19:14 -08:00
}
if (aflag[2])
{
2013-03-01 13:35:24 -08:00
flowIntoBlock(world, x, y, z - 1, k1, tex);
2013-02-06 18:19:14 -08:00
}
if (aflag[3])
{
2013-03-01 13:35:24 -08:00
flowIntoBlock(world, x, y, z + 1, k1, tex);
2013-02-06 18:19:14 -08:00
}
}
}
2013-03-01 13:35:24 -08:00
private void flowIntoBlock (World world, int x, int y, int z, int meta, int tex)
2013-02-06 18:19:14 -08:00
{
2013-02-11 12:25:40 -08:00
if (liquidCanDisplaceBlock(world, x, y, z))
2013-02-06 18:19:14 -08:00
{
2013-02-11 12:25:40 -08:00
int bID = world.getBlockId(x, y, z);
if (bID > 0)
2013-02-06 18:19:14 -08:00
{
2013-02-11 12:25:40 -08:00
Block.blocksList[bID].dropBlockAsItem(world, x, y, z, world.getBlockMetadata(x, y, z), 0);
2013-02-06 18:19:14 -08:00
}
2013-03-21 17:38:04 -07:00
world.setBlock(x, y, z, blockID, meta, 3);
2013-03-01 13:35:24 -08:00
2013-03-16 23:54:39 -07:00
((LiquidTextureLogic) world.getBlockTileEntity(x, y, z)).setLiquidType(tex);
2013-02-06 18:19:14 -08:00
}
}
2013-02-11 12:25:40 -08:00
private int calculateFlowCost (World world, int x, int y, int z, int l, int i1)
2013-02-06 18:19:14 -08:00
{
int j1 = 1000;
for (int k1 = 0; k1 < 4; k1++)
{
if (k1 == 0 && i1 == 1 || k1 == 1 && i1 == 0 || k1 == 2 && i1 == 3 || k1 == 3 && i1 == 2)
{
continue;
}
2013-02-11 12:25:40 -08:00
int posX = x;
int posY = y;
int posZ = z;
2013-02-06 18:19:14 -08:00
if (k1 == 0)
{
2013-02-11 12:25:40 -08:00
posX--;
2013-02-06 18:19:14 -08:00
}
if (k1 == 1)
{
2013-02-11 12:25:40 -08:00
posX++;
2013-02-06 18:19:14 -08:00
}
if (k1 == 2)
{
2013-02-11 12:25:40 -08:00
posZ--;
2013-02-06 18:19:14 -08:00
}
if (k1 == 3)
{
2013-02-11 12:25:40 -08:00
posZ++;
2013-02-06 18:19:14 -08:00
}
2013-02-11 12:25:40 -08:00
if (blockBlocksFlow(world, posX, posY, posZ) || world.getBlockMaterial(posX, posY, posZ) == blockMaterial && world.getBlockMetadata(posX, posY, posZ) == 0)
2013-02-06 18:19:14 -08:00
{
continue;
}
2013-02-11 12:25:40 -08:00
if (!blockBlocksFlow(world, posX, posY - 1, posZ))
2013-02-06 18:19:14 -08:00
return l;
if (l >= 4)
{
continue;
}
2013-02-11 12:25:40 -08:00
int k2 = calculateFlowCost(world, posX, posY, posZ, l + 1, k1);
2013-02-06 18:19:14 -08:00
if (k2 < j1)
{
j1 = k2;
}
}
return j1;
}
private boolean[] getOptimalFlowDirections (World world, int x, int y, int z)
{
2013-02-11 12:25:40 -08:00
for (int iter = 0; iter < 4; iter++)
2013-02-06 18:19:14 -08:00
{
2013-02-11 12:25:40 -08:00
flowCost[iter] = 1000;
int posX = x;
int posY = y;
int posZ = z;
if (iter == 0)
2013-02-06 18:19:14 -08:00
{
2013-02-11 12:25:40 -08:00
posX--;
2013-02-06 18:19:14 -08:00
}
2013-02-11 12:25:40 -08:00
if (iter == 1)
2013-02-06 18:19:14 -08:00
{
2013-02-11 12:25:40 -08:00
posX++;
2013-02-06 18:19:14 -08:00
}
2013-02-11 12:25:40 -08:00
if (iter == 2)
2013-02-06 18:19:14 -08:00
{
2013-02-11 12:25:40 -08:00
posZ--;
2013-02-06 18:19:14 -08:00
}
2013-02-11 12:25:40 -08:00
if (iter == 3)
2013-02-06 18:19:14 -08:00
{
2013-02-11 12:25:40 -08:00
posZ++;
2013-02-06 18:19:14 -08:00
}
2013-02-11 12:25:40 -08:00
if (blockBlocksFlow(world, posX, posY, posZ) || world.getBlockMaterial(posX, posY, posZ) == blockMaterial && world.getBlockMetadata(posX, posY, posZ) == 0)
2013-02-06 18:19:14 -08:00
{
continue;
}
2013-02-11 12:25:40 -08:00
if (!blockBlocksFlow(world, posX, posY - 1, posZ))
2013-02-06 18:19:14 -08:00
{
2013-02-11 12:25:40 -08:00
flowCost[iter] = 0;
2013-02-06 18:19:14 -08:00
}
else
{
2013-02-11 12:25:40 -08:00
flowCost[iter] = calculateFlowCost(world, posX, posY, posZ, 1, iter);
2013-02-06 18:19:14 -08:00
}
}
2013-02-11 12:25:40 -08:00
int cost = flowCost[0];
2013-02-06 18:19:14 -08:00
for (int k1 = 1; k1 < 4; k1++)
{
2013-02-11 12:25:40 -08:00
if (flowCost[k1] < cost)
2013-02-06 18:19:14 -08:00
{
2013-02-11 12:25:40 -08:00
cost = flowCost[k1];
2013-02-06 18:19:14 -08:00
}
}
for (int l1 = 0; l1 < 4; l1++)
{
2013-02-11 12:25:40 -08:00
isOptimalFlowDirection[l1] = flowCost[l1] == cost;
2013-02-06 18:19:14 -08:00
}
return isOptimalFlowDirection;
}
private boolean blockBlocksFlow (World world, int x, int y, int z)
{
int l = world.getBlockId(x, y, z);
2013-04-17 22:52:40 -07:00
if (l == Block.doorWood.blockID || l == Block.doorIron.blockID || l == Block.signPost.blockID || l == Block.ladder.blockID || l == Block.reed.blockID)
2013-02-06 18:19:14 -08:00
return true;
if (l == 0)
return false;
Material material = Block.blocksList[l].blockMaterial;
return material.isSolid();
}
2013-02-11 12:25:40 -08:00
protected int getSmallestFlowDecay (World world, int x, int y, int z, int l)
2013-02-06 18:19:14 -08:00
{
2013-02-11 12:25:40 -08:00
int i1 = getFlowDecay(world, x, y, z);
2013-02-06 18:19:14 -08:00
if (i1 < 0)
return l;
if (i1 >= 8)
{
i1 = 0;
}
return l >= 0 && i1 >= l ? l : i1;
}
2013-02-11 12:25:40 -08:00
private boolean liquidCanDisplaceBlock (World world, int x, int y, int z)
2013-02-06 18:19:14 -08:00
{
2013-02-11 12:25:40 -08:00
Material material = world.getBlockMaterial(x, y, z);
2013-02-06 18:19:14 -08:00
if (material == blockMaterial)
return false;
else
2013-02-11 12:25:40 -08:00
return !blockBlocksFlow(world, x, y, z);
2013-02-06 18:19:14 -08:00
}
@Override
2013-02-11 12:25:40 -08:00
public void onBlockAdded (World world, int x, int y, int z)
2013-02-06 18:19:14 -08:00
{
2013-02-11 12:25:40 -08:00
//System.out.println("x: "+x+", y: "+y+", z: "+z);
super.onBlockAdded(world, x, y, z);
if (world.getBlockId(x, y, z) == blockID)
2013-02-06 18:19:14 -08:00
{
2013-02-11 12:25:40 -08:00
world.scheduleBlockUpdate(x, y, z, blockID, tickRate());
2013-02-06 18:19:14 -08:00
}
}
@Override
public int stillLiquidMeta ()
{
return 0;
}
@Override
public boolean isMetaSensitive ()
{
return false;
}
2013-03-01 13:35:24 -08:00
@Override
public int stillLiquidId ()
{
return TContent.liquidMetalStill.blockID;
}
@Override
public boolean hasTileEntity(int metadata)
{
return true;
}
@Override
public TileEntity createTileEntity(World world, int metadata)
{
return new LiquidTextureLogic();
}
2013-02-06 18:19:14 -08:00
}