2014-09-27 20:37:05 +02:00
package resonantinduction.mechanical.mech.gear
import codechicken.lib.vec.Rotation
2014-10-16 14:13:57 +02:00
import codechicken.multipart. { TMultiPart , TileMultipart }
2014-09-27 20:37:05 +02:00
import net.minecraft.tileentity.TileEntity
import net.minecraftforge.common.util.ForgeDirection
2014-11-06 07:05:42 +01:00
import resonant.api.grid.INodeProvider
import resonant.lib.transform.vector. { IVectorWorld , Vector3 , VectorWorld }
2014-11-10 12:28:36 +01:00
import resonant.lib.wrapper.ForgeDirectionWrapper._
2014-11-09 05:06:09 +01:00
import resonantinduction.core.interfaces.TMechanicalNode
2014-11-10 12:28:36 +01:00
import resonantinduction.mechanical.mech.gearshaft. { GearShaftNode , PartGearShaft }
2014-11-13 03:22:46 +01:00
import resonantinduction.mechanical.mech.grid.NodeMechanical
2014-09-27 20:37:05 +02:00
/* *
* Node for the gear
*
* @author Calclavia , Edited by : Darkguardsman
*/
2014-11-13 06:16:25 +01:00
class NodeGear ( parent : PartGear ) extends NodeMechanical ( parent : PartGear )
2014-09-27 20:37:05 +02:00
{
2014-11-10 14:20:42 +01:00
angleDisplacement = Math . PI / 12
2014-11-09 06:59:37 +01:00
2014-11-09 07:49:56 +01:00
protected def gear = getParent . asInstanceOf [ PartGear ]
2014-11-06 07:05:42 +01:00
2014-11-13 06:16:25 +01:00
override def getLoad : Double =
2014-11-06 07:05:42 +01:00
{
2014-11-09 05:06:09 +01:00
return gear . tier match
{
case 0 => 0.1
case 1 => 0.2
2014-11-09 07:49:56 +01:00
case 2 => 0.1
2014-11-09 05:06:09 +01:00
}
2014-11-06 07:05:42 +01:00
}
2014-11-13 14:34:16 +01:00
override def reconstruct ( )
2014-11-06 07:05:42 +01:00
{
2014-11-13 14:34:16 +01:00
if ( ! parent . getMultiBlock . isPrimary )
2014-11-09 05:06:09 +01:00
{
2014-11-13 14:34:16 +01:00
parent . getMultiBlock . getPrimary . mechanicalNode . reconstruct ( )
2014-11-09 05:06:09 +01:00
}
2014-11-13 14:34:16 +01:00
super . reconstruct ( )
2014-11-06 07:05:42 +01:00
}
2014-09-27 20:37:05 +02:00
2014-11-09 06:28:58 +01:00
override def rebuild ( )
2014-11-06 07:05:42 +01:00
{
if ( ! gear . getMultiBlock . isPrimary || world == null )
2014-09-27 20:37:05 +02:00
{
2014-11-06 07:05:42 +01:00
return
2014-09-27 20:37:05 +02:00
}
2014-11-09 06:28:58 +01:00
2014-11-06 07:05:42 +01:00
val tileBehind : TileEntity = new Vector3 ( gear . tile ) . add ( gear . placementSide ) . getTileEntity ( world )
if ( tileBehind . isInstanceOf [ INodeProvider ] )
2014-09-27 20:37:05 +02:00
{
2014-11-13 03:22:46 +01:00
val instance : NodeMechanical = ( tileBehind . asInstanceOf [ INodeProvider ] ) . getNode ( classOf [ NodeMechanical ] , gear . placementSide . getOpposite )
2014-11-06 07:05:42 +01:00
if ( instance != null && instance != this && ! ( instance . getParent . isInstanceOf [ PartGearShaft ] ) && instance . canConnect ( this , gear . placementSide . getOpposite ) )
{
connect ( instance , gear . placementSide )
}
2014-09-27 20:37:05 +02:00
}
2014-11-06 07:05:42 +01:00
for ( i <- 0 until 6 )
2014-09-27 20:37:05 +02:00
{
2014-11-06 07:05:42 +01:00
val checkDir : ForgeDirection = ForgeDirection . getOrientation ( i )
var tile : TileEntity = gear . tile
if ( gear . getMultiBlock . isConstructed && checkDir != gear . placementSide && checkDir != gear . placementSide . getOpposite )
{
tile = new Vector3 ( gear . tile ) . add ( checkDir ) . getTileEntity ( world )
}
if ( tile . isInstanceOf [ INodeProvider ] )
{
2014-11-13 03:22:46 +01:00
val instance : NodeMechanical = ( tile . asInstanceOf [ INodeProvider ] ) . getNode ( classOf [ NodeMechanical ] , if ( checkDir == gear . placementSide . getOpposite ) ForgeDirection . UNKNOWN else checkDir ) . asInstanceOf [ NodeMechanical ]
2014-11-06 07:05:42 +01:00
if ( ! directionMap . containsValue ( checkDir ) && instance != this && checkDir != gear . placementSide && instance != null && instance . canConnect ( this , checkDir . getOpposite ) )
2014-09-27 20:37:05 +02:00
{
2014-11-06 07:05:42 +01:00
connect ( instance , checkDir )
2014-09-27 20:37:05 +02:00
}
2014-11-06 07:05:42 +01:00
}
}
var displaceCheck : Int = 1
if ( gear . getMultiBlock . isPrimary && gear . getMultiBlock . isConstructed )
{
displaceCheck = 2
}
for ( i <- 0 until 4 )
{
val checkDir : ForgeDirection = ForgeDirection . getOrientation ( Rotation . rotateSide ( gear . placementSide . ordinal , i ) )
val checkTile : TileEntity = new Vector3 ( gear . tile ) . add ( checkDir ) . getTileEntity ( world )
if ( ! directionMap . containsValue ( checkDir ) && checkTile . isInstanceOf [ INodeProvider ] )
{
2014-11-13 03:22:46 +01:00
val instance : NodeMechanical = ( checkTile . asInstanceOf [ INodeProvider ] ) . getNode ( classOf [ NodeMechanical ] , gear . placementSide )
2014-11-06 07:05:42 +01:00
if ( instance != null && instance != this && instance . canConnect ( this , checkDir . getOpposite ) && ! ( instance . getParent . isInstanceOf [ PartGearShaft ] ) )
2014-09-27 20:37:05 +02:00
{
2014-11-06 07:05:42 +01:00
connect ( instance , checkDir )
2014-09-27 20:37:05 +02:00
}
2014-11-06 07:05:42 +01:00
}
}
}
/* *
* Can this gear be connected BY the source ?
*
* @param from - Direction source is coming from .
* @param other - The source of the connection .
* @return True is so .
*/
override def canConnect [ B ] ( other : B , from : ForgeDirection ) : Boolean =
{
if ( ! gear . getMultiBlock . isPrimary )
{
return false
}
2014-11-13 03:22:46 +01:00
if ( other . isInstanceOf [ NodeMechanical ] )
2014-11-06 07:05:42 +01:00
{
2014-11-13 03:22:46 +01:00
val parent : INodeProvider = other . asInstanceOf [ NodeMechanical ] . getParent
2014-11-06 07:05:42 +01:00
if ( from == gear . placementSide . getOpposite )
{
if ( parent . isInstanceOf [ PartGear ] || parent . isInstanceOf [ PartGearShaft ] )
2014-09-27 20:37:05 +02:00
{
2014-11-06 07:05:42 +01:00
if ( parent . isInstanceOf [ PartGearShaft ] )
{
2014-11-09 07:49:56 +01:00
//We are connecting to a shaft.
val shaft = parent . asInstanceOf [ PartGearShaft ]
//Check if the shaft is directing connected to the center of the gear (multiblock cases) and also its direction to make sure the shaft is facing the gear itself
2014-11-10 12:28:36 +01:00
return /* shaft.tile.partMap(from.getOpposite.ordinal) != gear && */ Math . abs ( shaft . placementSide . offsetX ) == Math . abs ( gear . placementSide . offsetX ) && Math . abs ( shaft . placementSide . offsetY ) == Math . abs ( gear . placementSide . offsetY ) && Math . abs ( shaft . placementSide . offsetZ ) == Math . abs ( gear . placementSide . offsetZ )
2014-11-06 07:05:42 +01:00
}
else if ( parent . isInstanceOf [ PartGear ] )
{
if ( ( parent . asInstanceOf [ PartGear ] ) . tile == gear . tile && ! gear . getMultiBlock . isConstructed )
2014-09-27 20:37:05 +02:00
{
2014-11-06 07:05:42 +01:00
return true
2014-10-16 14:13:57 +02:00
}
2014-11-06 07:05:42 +01:00
if ( ( parent . asInstanceOf [ PartGear ] ) . placementSide ne gear . placementSide )
2014-10-16 14:13:57 +02:00
{
2014-11-06 07:05:42 +01:00
val part : TMultiPart = gear . tile . partMap ( ( parent . asInstanceOf [ PartGear ] ) . placementSide . ordinal )
if ( part . isInstanceOf [ PartGear ] )
{
val sourceGear : PartGear = part . asInstanceOf [ PartGear ]
if ( sourceGear . isCenterMultiBlock && ! sourceGear . getMultiBlock . isPrimary )
2014-09-27 20:37:05 +02:00
{
2014-11-06 07:05:42 +01:00
return true
2014-09-27 20:37:05 +02:00
}
2014-11-06 07:05:42 +01:00
}
else
{
if ( gear . getMultiBlock . isConstructed )
2014-09-27 20:37:05 +02:00
{
2014-11-06 07:05:42 +01:00
val checkPart : TMultiPart = ( parent . asInstanceOf [ PartGear ] ) . tile . partMap ( gear . placementSide . ordinal )
if ( checkPart . isInstanceOf [ PartGear ] )
{
2014-11-09 05:06:09 +01:00
val requiredDirection : ForgeDirection = ( checkPart . asInstanceOf [ PartGear ] ) . getPosition . subtract ( toVectorWorld ) . toForgeDirection
2014-11-06 07:05:42 +01:00
return ( checkPart . asInstanceOf [ PartGear ] ) . isCenterMultiBlock && ( parent . asInstanceOf [ PartGear ] ) . placementSide == requiredDirection
}
2014-09-27 20:37:05 +02:00
}
2014-11-06 07:05:42 +01:00
}
2014-09-27 20:37:05 +02:00
}
2014-11-06 07:05:42 +01:00
}
2014-09-27 20:37:05 +02:00
}
2014-11-09 05:06:09 +01:00
val sourceTile : TileEntity = toVectorWorld . add ( from . getOpposite ) . getTileEntity ( world )
2014-11-06 07:05:42 +01:00
if ( sourceTile . isInstanceOf [ INodeProvider ] )
2014-09-27 20:37:05 +02:00
{
2014-11-13 03:22:46 +01:00
val sourceInstance : NodeMechanical = ( sourceTile . asInstanceOf [ INodeProvider ] ) . getNode ( classOf [ NodeMechanical ] , from )
2014-11-06 07:05:42 +01:00
return sourceInstance == other
2014-09-27 20:37:05 +02:00
}
2014-11-06 07:05:42 +01:00
}
else if ( from == gear . placementSide )
{
2014-11-09 05:06:09 +01:00
val sourceTile : TileEntity = toVectorWorld . add ( from ) . getTileEntity ( world )
2014-11-06 07:05:42 +01:00
if ( sourceTile . isInstanceOf [ INodeProvider ] )
2014-09-27 20:37:05 +02:00
{
2014-11-13 03:22:46 +01:00
val sourceInstance : NodeMechanical = ( sourceTile . asInstanceOf [ INodeProvider ] ) . getNode ( classOf [ NodeMechanical ] , from . getOpposite )
2014-11-06 07:05:42 +01:00
return sourceInstance == other
}
}
else
{
2014-11-13 03:22:46 +01:00
val destinationTile : TileEntity = other . asInstanceOf [ NodeMechanical ] . toVectorWorld . add ( from . getOpposite ) . getTileEntity ( world )
2014-11-06 07:05:42 +01:00
if ( destinationTile . isInstanceOf [ INodeProvider ] && destinationTile . isInstanceOf [ TileMultipart ] )
{
2014-11-09 07:49:56 +01:00
val destinationPart : TMultiPart = destinationTile . asInstanceOf [ TileMultipart ] . partMap ( gear . placementSide . ordinal )
2014-11-06 07:05:42 +01:00
if ( destinationPart . isInstanceOf [ PartGear ] )
{
if ( gear ne destinationPart )
2014-09-27 20:37:05 +02:00
{
2014-11-09 07:49:56 +01:00
return destinationPart . asInstanceOf [ PartGear ] . isCenterMultiBlock
2014-09-27 20:37:05 +02:00
}
else
{
2014-11-06 07:05:42 +01:00
return true
2014-09-27 20:37:05 +02:00
}
2014-11-06 07:05:42 +01:00
}
else
{
return true
}
2014-09-27 20:37:05 +02:00
}
2014-11-06 07:05:42 +01:00
}
2014-09-27 20:37:05 +02:00
}
2014-11-06 07:05:42 +01:00
return false
}
2014-09-27 20:37:05 +02:00
2014-11-10 12:28:36 +01:00
override def inverseRotation ( other : TMechanicalNode ) : Boolean = ! other . isInstanceOf [ GearShaftNode ] || ( other . isInstanceOf [ GearShaftNode ] && parent . placementSide . offset < Vector3 . zero )
2014-11-13 14:34:16 +01:00
override def momentOfInertia = if ( gear . getMultiBlock . isConstructed ) 1.5f else super . momentOfInertia
2014-11-13 07:17:05 +01:00
2014-11-13 14:34:16 +01:00
/*
2014-11-09 06:28:58 +01:00
override def getRadius ( dir : ForgeDirection , other : TMechanicalNode ) : Double =
2014-11-06 07:05:42 +01:00
{
2014-11-13 07:17:05 +01:00
//The ratio is the same if it is a gear placed back to back with each other OR a shaft
2014-11-09 06:28:58 +01:00
val deltaPos : Vector3 = new VectorWorld ( other . asInstanceOf [ IVectorWorld ] ) . subtract ( toVectorWorld )
val caseX = gear . placementSide . offsetX != 0 && deltaPos . y == 0 && deltaPos . z == 0
val caseY = gear . placementSide . offsetY != 0 && deltaPos . x == 0 && deltaPos . z == 0
val caseZ = gear . placementSide . offsetZ != 0 && deltaPos . x == 0 && deltaPos . y == 0
2014-11-06 07:05:42 +01:00
if ( caseX || caseY || caseZ )
2014-11-09 06:28:58 +01:00
return super . getRadius ( dir , other )
return if ( gear . getMultiBlock . isConstructed ) 1.5f else super . getRadius ( dir , other )
2014-11-13 14:34:16 +01:00
} */
2014-09-27 20:37:05 +02:00
}