Removed mechanical default buffers and fixed mechanical recursion algorithm

This commit is contained in:
Calclavia 2014-11-22 18:13:04 +08:00
parent 81c89f7e64
commit f6746410ab
8 changed files with 52 additions and 51 deletions

View file

@ -66,11 +66,6 @@ object MechanicalContent extends ContentHolder
ResonantPartFactory.register(classOf[PartPipe])
}
override def init()
{
super.init()
}
/**
* Recipe registration
*/

View file

@ -49,7 +49,7 @@ abstract class PartMechanical extends PartAbstract with JNormalOcclusion with TF
save(tag)
packet.writeNBTTagCompound(tag)
case 1 => packet.writeFloat(mechanicalNode.angularVelocity.toFloat)
case 2 => packet.writeFloat(mechanicalNode.prevAngle.toFloat)
case 2 => packet.writeFloat(mechanicalNode.angle.toFloat)
}
}
@ -62,7 +62,9 @@ abstract class PartMechanical extends PartAbstract with JNormalOcclusion with TF
case 0 =>
load(packet.readNBTTagCompound())
case 1 => mechanicalNode.angularVelocity = packet.readFloat()
case 2 => mechanicalNode.prevAngle = packet.readFloat()
case 2 =>
mechanicalNode.prevAngle = packet.readFloat()
mechanicalNode.prevTime = System.currentTimeMillis()
}
}

View file

@ -9,7 +9,6 @@ import net.minecraft.item.ItemStack
import resonant.content.spatial.block.SpatialTile
import resonant.engine.ResonantEngine
import resonant.lib.content.prefab.TRotatable
import resonant.lib.grid.UpdateTicker
import resonant.lib.grid.node.TSpatialNodeProvider
import resonant.lib.network.ByteBufWrapper._
import resonant.lib.network.discriminator.PacketType
@ -30,7 +29,7 @@ abstract class TileMechanical(material: Material) extends SpatialTile(material:
def mechanicalNode_=(newNode: NodeMechanical)
{
_mechanicalNode = newNode
mechanicalNode.onVelocityChanged = () => UpdateTicker.world.enqueue(() => sendPacket(1))
mechanicalNode.onVelocityChanged = () => sendPacket(1)
nodes.add(mechanicalNode)
}

View file

@ -12,7 +12,6 @@ import net.minecraft.util.MovingObjectPosition
import net.minecraft.world.World
import net.minecraftforge.common.util.ForgeDirection
import resonant.api.grid.INode
import resonant.lib.grid.UpdateTicker
import resonant.lib.multiblock.reference.IMultiBlockStructure
import resonant.lib.utility.WrenchUtility
import resonantinduction.core.Reference
@ -38,10 +37,10 @@ class PartGear extends PartMechanical with IMultiBlockStructure[PartGear]
mechanicalNode.onVelocityChanged = () =>
{
if (getMultiBlock.isPrimary)
UpdateTicker.world.enqueue(() => if (world != null) sendPacket(1))
if (world != null) sendPacket(1)
// if (mechanicalNode.angularVelocity == 0)
// UpdateTicker.world.enqueue(() => if (world != null) sendPacket(2))
// if (mechanicalNode.angularVelocity == 0)
// if (world != null) sendPacket(2)
}
mechanicalNode.onGridReconstruct = () => if (world != null && !world.isRemote) sendPacket(2)
@ -59,8 +58,6 @@ class PartGear extends PartMechanical with IMultiBlockStructure[PartGear]
mechanicalNode.rotate((if (isClockwiseCrank) 2 else -2) * manualCrankTime, (if (isClockwiseCrank) 0.5 else -0.5) * manualCrankTime)
manualCrankTime -= 1
}
mechanicalNode.bufferDefaultTorque = (if (isClockwiseCrank) 2 else -2) * manualCrankTime
mechanicalNode.bufferDefaultAngularVelocity = (if (isClockwiseCrank) 0.5 else -0.5) * manualCrankTime
}
getMultiBlock.update()
@ -69,7 +66,7 @@ class PartGear extends PartMechanical with IMultiBlockStructure[PartGear]
override def activate(player: EntityPlayer, hit: MovingObjectPosition, itemStack: ItemStack): Boolean =
{
if (!world.isRemote)
println(mechanicalNode)
println(mechanicalNode.grid)
if (itemStack != null && itemStack.getItem.isInstanceOf[ItemHandCrank])
{

View file

@ -27,7 +27,7 @@ class MechanicalGrid extends GridNode[NodeMechanical](classOf[NodeMechanical]) w
override def reconstruct(first: NodeMechanical)
{
super.reconstruct(first)
UpdateTicker.threaded.addUpdater(this)
UpdateTicker.world.addUpdater(this)
load = getNodes.map(n => n.getLoad).foldLeft(0D)(_ + _)
}
@ -52,53 +52,62 @@ class MechanicalGrid extends GridNode[NodeMechanical](classOf[NodeMechanical]) w
getNodes.foreach(
n =>
{
n.prevTorque = n.torque
n.prevAngularVelocity = n.angularVelocity
n._torque = 0
n._angularVelocity = 0
}
)
getNodes.filter(n => n.bufferTorque != 0 && n.bufferAngularVelocity != 0).foreach(n => recurse(deltaTime, Seq(n)))
getNodes.filter(n => n.bufferTorque != 0 && n.bufferAngularVelocity != 0).foreach(n => recurse(deltaTime, n.bufferTorque, n.bufferAngularVelocity, Seq(n)))
getNodes.foreach(
n =>
{
if (n.prevTorque != n.torque)
n.onTorqueChanged()
// if (n.prevAngularVelocity != n.angularVelocity)
n.onVelocityChanged()
//TODO: Solve problem with "buffer" not syncing up with this thread.
n.bufferTorque = n.bufferDefaultTorque
n.bufferAngularVelocity = n.bufferDefaultAngularVelocity
}
)
//UpdateTicker world enqueue
resetNodes()
}
}
def recurse(deltaTime: Double, passed: Seq[NodeMechanical])
def resetNodes()
{
getNodes.foreach(
n =>
{
if (n.prevTorque != n.torque)
{
n.prevTorque = n.torque
n.onTorqueChanged()
}
if (n.prevAngularVelocity != n.angularVelocity)
{
n.prevAngularVelocity = n.angularVelocity
n.onVelocityChanged()
}
n.bufferTorque = 0
n.bufferAngularVelocity = 0
}
)
}
def recurse(deltaTime: Double, torque: Double, angularVelocity: Double, passed: Seq[NodeMechanical])
{
val curr = passed(passed.size - 1)
if (passed.size > 1)
{
val prev = passed(passed.size - 2)
val ratio = curr.radius / prev.radius
val ratio = curr.radius(prev) / prev.radius(curr)
val invert = if (curr.inverseRotation(prev)) -1 else 1
curr._torque += Math.abs(passed(0).bufferTorque) * ratio * invert * Math.signum(prev.torque)
curr._angularVelocity += Math.abs(passed(0).bufferAngularVelocity) * deltaTime / ratio * invert * Math.signum(prev.angularVelocity)
val addTorque = torque * ratio * invert
val addVel = angularVelocity / ratio * invert
curr._torque += addTorque
curr._angularVelocity += addVel * deltaTime
curr.connections.filter(!passed.contains(_)).foreach(c => recurse(deltaTime, addTorque, addVel, passed :+ c))
}
else
{
curr._torque += curr.bufferTorque
curr._angularVelocity += curr.bufferAngularVelocity * deltaTime
curr._torque += torque
curr._angularVelocity += angularVelocity * deltaTime
curr.connections.filter(!passed.contains(_)).foreach(c => recurse(deltaTime, torque, angularVelocity, passed :+ c))
}
if (curr.power > 0)
curr.connections.filter(!passed.contains(_)).foreach(c => recurse(deltaTime, passed :+ c))
}
override def continueUpdate = getNodes.size > 0

View file

@ -43,13 +43,11 @@ class NodeMechanical(parent: INodeProvider) extends NodeGrid[NodeMechanical](par
*/
protected[grid] var bufferTorque = 0D
protected[grid] var bufferAngularVelocity = 0D
var bufferDefaultTorque = 0D
var bufferDefaultAngularVelocity = 0D
/**
* Angle calculations
*/
protected var prevTime = 0L
var prevTime = System.currentTimeMillis()
var prevAngle = 0D
/**

View file

@ -1,6 +1,7 @@
package resonantinduction.mechanical.mech.turbine
import net.minecraftforge.common.util.ForgeDirection
import resonantinduction.core.interfaces.TNodeMechanical
import resonantinduction.mechanical.mech.grid.NodeMechanical
/**
@ -22,7 +23,7 @@ class NodeTurbine(parent: TileTurbine) extends NodeMechanical(parent)
* Moment of inertia = m * r * r
* Where "m" is the mass and "r" is the radius of the object.
*/
override def radius = parent.multiBlockRadius * parent.multiBlockRadius
override def radius(other: TNodeMechanical) = parent.multiBlockRadius * parent.multiBlockRadius
def turbine: TileTurbine =
{

View file

@ -30,7 +30,7 @@ class TileWindTurbine extends TileTurbine with IBoilHandler
private final val openBlockCache = new Array[Byte](224)
private var checkCount = 0
private var efficiency = 0f
private var windTorque = 0d
private var windPower = 0d
/**
* Steam simulations
@ -64,7 +64,7 @@ class TileWindTurbine extends TileTurbine with IBoilHandler
computePower()
}
getMultiBlock.get.mechanicalNode.bufferDefaultTorque = windTorque
getMultiBlock.get.mechanicalNode.rotate(windPower * multiBlockRadius, windPower / multiBlockRadius)
}
else
{
@ -124,7 +124,7 @@ class TileWindTurbine extends TileTurbine with IBoilHandler
val hasBonus = biome.isInstanceOf[BiomeGenOcean] || biome.isInstanceOf[BiomeGenPlains] || biome == BiomeGenBase.river
val windSpeed = (worldObj.rand.nextFloat / 8) + (yCoord / 256f) * (if (hasBonus) 1.2f else 1) + worldObj.getRainStrength(1.5f)
windTorque = materialMultiplier * multiblockMultiplier * windSpeed * efficiency * Settings.WIND_POWER_RATIO / 20
windPower = materialMultiplier * multiblockMultiplier * windSpeed * efficiency * Settings.WIND_POWER_RATIO / 20
}
override def getSubBlocks(par1: Item, par2CreativeTabs: CreativeTabs, par3List: List[_])