electrodynamics/src/main/scala/edx/mechanical/mech/turbine/TileWaterTurbine.scala

129 lines
4.4 KiB
Scala
Raw Normal View History

2015-01-14 12:06:03 +01:00
package edx.mechanical.mech.turbine
import java.util.List
import cpw.mods.fml.relauncher.ReflectionHelper
2015-01-14 12:06:03 +01:00
import edx.core.Settings
import edx.mechanical.mech.grid.NodeMechanical
import net.minecraft.block.BlockLiquid
import net.minecraft.creativetab.CreativeTabs
import net.minecraft.init.Blocks
import net.minecraft.item.{Item, ItemStack}
import net.minecraft.tileentity.TileEntity
import net.minecraft.util.Vec3
import net.minecraft.world.IBlockAccess
import net.minecraftforge.common.util.ForgeDirection
2015-01-26 13:17:04 +01:00
import resonantengine.api.graph.INodeProvider
2015-01-26 12:40:32 +01:00
import resonantengine.lib.transform.vector.Vector3
import resonantengine.lib.wrapper.CollectionWrapper._
2015-01-26 13:28:38 +01:00
import resonantengine.prefab.block.itemblock.ItemBlockMetadata
/**
* The vertical water turbine collects flowing water flowing on X axis.
* The horizontal water turbine collects flowing water on Z axis.
*
* @author Calclavia
*
*/
class TileWaterTurbine extends TileTurbine
{
var powerTicks = 0
2014-10-26 15:41:40 +01:00
//Constructor
itemBlock = classOf[ItemBlockMetadata]
mechanicalNode = new NodeTurbine(this)
2014-10-26 15:41:40 +01:00
{
2014-11-04 15:06:08 +01:00
override def canConnect[B](other: B, from: ForgeDirection): Boolean =
{
2014-12-11 03:18:09 +01:00
if (other.isInstanceOf[NodeMechanical] && !other.isInstanceOf[TileTurbine])
2014-10-26 15:41:40 +01:00
{
2021-04-05 14:41:30 +02:00
val sourceTile: TileEntity = toVectorWorld.add(from).getTileEntity
2014-10-26 15:41:40 +01:00
if (sourceTile.isInstanceOf[INodeProvider])
{
2014-11-13 03:22:46 +01:00
val sourceInstance: NodeMechanical = sourceTile.asInstanceOf[INodeProvider].getNode(classOf[NodeMechanical], from.getOpposite).asInstanceOf[NodeMechanical]
2014-11-04 15:06:08 +01:00
return sourceInstance == other && (from == getDirection.getOpposite || from == getDirection)
}
2014-10-26 15:41:40 +01:00
}
2014-10-26 15:41:40 +01:00
return false
}
2014-10-26 15:41:40 +01:00
}
override def update()
2014-10-26 15:41:40 +01:00
{
super.update()
2014-10-26 15:41:40 +01:00
if (getDirection.offsetY != 0)
{
if (powerTicks > 0)
{
2015-01-23 02:18:36 +01:00
getMultiBlock.get.mechanicalNode.accelerate(getWaterPower)
2014-10-26 15:41:40 +01:00
powerTicks -= 1
}
2014-10-26 15:41:40 +01:00
if (ticks % 20 == 0)
{
val blockAbove = worldObj.getBlock(xCoord, yCoord + 1, zCoord)
val blockBelow = worldObj.getBlock(xCoord, yCoord - 1, zCoord)
val metadata = worldObj.getBlockMetadata(xCoord, yCoord + 1, zCoord)
val isWater = blockAbove == Blocks.water || blockAbove == Blocks.flowing_water
2021-04-05 14:41:30 +02:00
if (isWater && metadata == 0 && blockBelow.isReplaceable(world, x.toInt, y.toInt - 1, z.toInt))
{
2014-10-26 15:41:40 +01:00
powerTicks = 20
worldObj.setBlockToAir(xCoord, yCoord + 1, zCoord)
worldObj.setBlock(xCoord, yCoord - 1, zCoord, Blocks.flowing_water)
2015-01-23 02:18:36 +01:00
getMultiBlock.get.mechanicalNode.accelerate(10000)
}
2014-10-26 15:41:40 +01:00
}
}
else
{
val currentDir: ForgeDirection = getDirection
2014-10-26 15:41:40 +01:00
for (dir <- ForgeDirection.VALID_DIRECTIONS)
{
if (dir != currentDir && dir != currentDir.getOpposite)
{
2021-04-05 14:41:30 +02:00
val check: Vector3 = toVectorWorld.add(dir)
val block = worldObj.getBlock(check.xi, check.yi, check.zi)
2014-10-26 15:41:40 +01:00
val metadata: Int = worldObj.getBlockMetadata(check.xi, check.yi, check.zi)
if (block == Blocks.water || block == Blocks.flowing_water)
{
2021-04-05 14:41:30 +02:00
val m = ReflectionHelper.findMethod(classOf[BlockLiquid], null, Array[String]("getFlowVector", "func_149800_f"), classOf[IBlockAccess], Integer.TYPE, Integer.TYPE, Integer.TYPE)
2014-12-10 14:20:56 +01:00
val vector = new Vector3(m.invoke(Blocks.water, worldObj, check.xi: Integer, check.yi: Integer, check.zi: Integer).asInstanceOf[Vec3])
val invert = (currentDir.offsetZ > 0 && vector.x < 0) || (currentDir.offsetZ < 0 && vector.x > 0) || (currentDir.offsetX > 0 && vector.z > 0) || (currentDir.offsetX < 0 && vector.z < 0)
if (getDirection.offsetX != 0)
{
2015-01-23 02:18:36 +01:00
getMultiBlock.get.mechanicalNode.accelerate(if (invert) -1 else 1 * Math.abs(getWaterPower * vector.z * (7 - metadata) / 7f))
powerTicks = 20
}
if (getDirection.offsetZ != 0)
{
2015-01-23 02:18:36 +01:00
getMultiBlock.get.mechanicalNode.accelerate(if (invert) -1 else 1 * Math.abs(getWaterPower * vector.x * (7 - metadata) / 7f))
powerTicks = 20
}
2014-10-26 15:41:40 +01:00
}
}
2014-10-26 15:41:40 +01:00
}
}
2014-10-26 15:41:40 +01:00
}
2014-10-26 15:41:40 +01:00
/**
* Gravitation Potential Energy:
* PE = mgh
*/
private def getWaterPower = (10000 / (2 - tier + 1)) * Settings.WATER_POWER_RATIO
2014-10-26 15:41:40 +01:00
override def getSubBlocks(par1: Item, par2CreativeTabs: CreativeTabs, par3List: List[_])
{
for (i <- 0 to 2)
{
2014-10-26 15:41:40 +01:00
par3List.add(new ItemStack(par1, 1, i))
}
2014-10-26 15:41:40 +01:00
}
2021-04-05 14:41:30 +02:00
}