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

146 lines
4.9 KiB
Scala
Raw Normal View History

package resonantinduction.mechanical.mech.turbine
import java.lang.reflect.Method
import java.util.List
import cpw.mods.fml.relauncher.ReflectionHelper
import net.minecraft.block.{Block, BlockDynamicLiquid}
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
import resonant.content.prefab.itemblock.ItemBlockMetadata
2014-10-26 15:41:40 +01:00
import resonant.lib.wrapper.WrapList._
import resonantinduction.core.Settings
import resonant.api.grid.INodeProvider
import resonant.lib.transform.vector.Vector3
2014-11-09 05:06:09 +01:00
import resonantinduction.mechanical.mech.grid.MechanicalNode
/**
* 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
{
2014-10-26 15:41:40 +01:00
var powerTicks: Int = 0
2014-10-26 15:41:40 +01:00
//Constructor
this.itemBlock_$eq(classOf[ItemBlockMetadata])
mechanicalNode.torque = defaultTorque
mechanicalNode = new TurbineNode((this))
{
2014-11-04 15:06:08 +01:00
override def canConnect[B](other: B, from: ForgeDirection): Boolean =
{
2014-11-04 15:06:08 +01:00
if (other.isInstanceOf[MechanicalNode] && !(other.isInstanceOf[TileTurbine]))
2014-10-26 15:41:40 +01:00
{
val sourceTile: TileEntity = position.add(from).getTileEntity(getWorld)
if (sourceTile.isInstanceOf[INodeProvider])
{
2014-10-26 15:41:40 +01:00
val sourceInstance: MechanicalNode = sourceTile.asInstanceOf[INodeProvider].getNode(classOf[MechanicalNode], from.getOpposite).asInstanceOf[MechanicalNode]
2014-11-04 15:06:08 +01:00
return sourceInstance == other && (from == getDirection.getOpposite || from == getDirection)
}
2014-10-26 15:41:40 +01:00
}
return false
}
2014-10-26 15:41:40 +01:00
}
override def update
{
super.update
if (getMultiBlock.isConstructed)
{
2014-10-26 15:41:40 +01:00
mechanicalNode.torque = (defaultTorque / (1d / multiBlockRadius)).asInstanceOf[Long]
}
else
{
mechanicalNode.torque = defaultTorque / 12
}
if (getDirection.offsetY != 0)
{
maxPower = 10000
if (powerTicks > 0)
{
getMultiBlock.get.power += getWaterPower
powerTicks -= 1
}
if (ticks % 20 == 0)
{
val blockIDAbove: Block = worldObj.getBlock(xCoord, yCoord + 1, zCoord)
val metadata: Int = worldObj.getBlockMetadata(xCoord, yCoord + 1, zCoord)
val isWater: Boolean = (blockIDAbove == Blocks.water || blockIDAbove == Blocks.flowing_water)
if (isWater && worldObj.isAirBlock(xCoord, yCoord - 1, zCoord) && metadata == 0)
{
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)
}
2014-10-26 15:41:40 +01:00
}
}
else
{
maxPower = 2500
val currentDir: ForgeDirection = getDirection
for (dir <- ForgeDirection.VALID_DIRECTIONS)
{
if (dir != currentDir && dir != currentDir.getOpposite)
{
2014-11-08 13:58:31 +01:00
val check: Vector3 = toVector3.add(dir)
2014-10-26 15:41:40 +01:00
val blockID: Block = worldObj.getBlock(check.xi, check.yi, check.zi)
val metadata: Int = worldObj.getBlockMetadata(check.xi, check.yi, check.zi)
if (blockID == Blocks.water || blockID == Blocks.flowing_water)
{
try
{
2014-10-26 15:41:40 +01:00
val m: Method = ReflectionHelper.findMethod(classOf[BlockDynamicLiquid], null, Array[String]("getFlowVector", "func_72202_i"), classOf[IBlockAccess], Integer.TYPE, Integer.TYPE, Integer.TYPE)
val vector: Vector3 = new Vector3(m.invoke(Blocks.water, Array(worldObj, check.xi, check.yi, check.zi)).asInstanceOf[Vec3])
if ((currentDir.offsetZ > 0 && vector.x < 0) || (currentDir.offsetZ < 0 && vector.x > 0) || (currentDir.offsetX > 0 && vector.z > 0) || (currentDir.offsetX < 0 && vector.z < 0))
{
mechanicalNode.torque = -mechanicalNode.torque
}
if (getDirection.offsetX != 0)
{
getMultiBlock.get.power += Math.abs(getWaterPower * vector.z * (7 - metadata) / 7f)
powerTicks = 20
}
if (getDirection.offsetZ != 0)
{
getMultiBlock.get.power += Math.abs(getWaterPower * vector.x * (7 - metadata) / 7f)
powerTicks = 20
}
}
2014-10-26 15:41:40 +01:00
catch
{
case e: Exception =>
{
2014-10-26 15:41:40 +01:00
e.printStackTrace
}
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: Long =
{
return (maxPower / (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
}
}