cleaned up redstone logic a bit and enforcing a short delay after synchronized calls to avoid tick times for computers going through the roof due to spammy synchronized calls (e.g. redstone.setOutput every frame). this is more of a workaround, i'll have to see if i can find a cleaner solution for this.

This commit is contained in:
Florian Nücke 2014-03-18 19:00:57 +01:00
parent 3b5bb9487b
commit 4e7bc78836
9 changed files with 72 additions and 79 deletions

View File

@ -57,14 +57,24 @@ trait BundledRedstoneAware extends RedstoneAware with IBundledEmitter with IBund
def bundledOutput(side: ForgeDirection, color: Int, value: Int): Unit = if (value != bundledOutput(side, color)) {
_bundledOutput(toLocal(side).ordinal())(color) = value
onRedstoneOutputChanged(side)
if (Loader.isModLoaded("MineFactoryReloaded")) {
val nx = x + side.offsetX
val ny = y + side.offsetY
val nz = z + side.offsetZ
Block.blocksList(world.getBlockId(nx, ny, nz)) match {
case block: IRedNetNetworkContainer => block.updateNetwork(world, nx, ny, nz)
case _ =>
}
}
onRedstoneOutputChanged()
}
// ----------------------------------------------------------------------- //
override def updateRedstoneInput() {
if (shouldUpdateInput) {
for (side <- ForgeDirection.VALID_DIRECTIONS) {
override protected def updateRedstoneInput(side: ForgeDirection) {
super.updateRedstoneInput(side)
val oldBundledInput = _bundledInput(side.ordinal())
val newBundledInput = computeBundledInput(side)
var changed = false
@ -80,9 +90,6 @@ trait BundledRedstoneAware extends RedstoneAware with IBundledEmitter with IBund
onRedstoneInputChanged(side)
}
}
}
super.updateRedstoneInput()
}
override def readFromNBT(nbt: NBTTagCompound) {
super.readFromNBT(nbt)
@ -149,8 +156,7 @@ trait BundledRedstoneAware extends RedstoneAware with IBundledEmitter with IBund
}
}
override protected def onRedstoneOutputChanged(side: ForgeDirection) {
if (side == ForgeDirection.UNKNOWN) {
override protected def onRedstoneOutputEnabledChanged() {
if (Loader.isModLoaded("MineFactoryReloaded")) {
for (side <- ForgeDirection.VALID_DIRECTIONS) {
val nx = x + side.offsetX
@ -162,19 +168,7 @@ trait BundledRedstoneAware extends RedstoneAware with IBundledEmitter with IBund
}
}
}
}
else {
val nx = x + side.offsetX
val ny = y + side.offsetY
val nz = z + side.offsetZ
if (Loader.isModLoaded("MineFactoryReloaded")) {
Block.blocksList(world.getBlockId(nx, ny, nz)) match {
case block: IRedNetNetworkContainer => block.updateNetwork(world, x, y, z)
case _ =>
}
}
}
super.onRedstoneOutputChanged(side)
super.onRedstoneOutputEnabledChanged()
}
// ----------------------------------------------------------------------- //

View File

@ -30,8 +30,6 @@ class Charger extends Environment with RedstoneAware with Analyzable {
override def updateEntity() {
super.updateEntity()
if (isServer) {
updateRedstoneInput()
val charge = Settings.get.chargeRate * chargeSpeed
robots.collect {
case Some(proxy) => node.changeBuffer(proxy.robot.bot.node.changeBuffer(charge + node.changeBuffer(-charge)))

View File

@ -124,7 +124,6 @@ trait Computer extends Environment with ComponentInventory with Rotatable with B
ServerPacketSender.sendComputerState(this)
}
updateRedstoneInput()
updateComponents()
}
}

View File

@ -217,7 +217,6 @@ class Rack extends PowerAcceptor with Hub with PowerBalancer with Inventory with
isOutputEnabled = hasRedstoneCard
isAbstractBusAvailable = hasAbstractBusCard
updateRedstoneInput()
servers collect {
case Some(server) => server.inventory.updateComponents()
}

View File

@ -16,13 +16,6 @@ class Redstone extends Environment with BundledRedstoneAware {
_isOutputEnabled = true
}
override def updateEntity() {
super.updateEntity()
if (isServer) {
updateRedstoneInput()
}
}
override def readFromNBT(nbt: NBTTagCompound) {
super.readFromNBT(nbt)
instance.load(nbt.getCompoundTag(Settings.namespace + "redstone"))

View File

@ -33,7 +33,7 @@ trait RedstoneAware extends RotationAware with IConnectable with IRedstoneEmitte
_output(i) = 0
}
}
onRedstoneOutputChanged(ForgeDirection.UNKNOWN)
onRedstoneOutputEnabledChanged()
}
this
}
@ -44,9 +44,16 @@ trait RedstoneAware extends RotationAware with IConnectable with IRedstoneEmitte
def output(side: ForgeDirection) = _output(toLocal(side).ordinal())
def output(side: ForgeDirection, value: Int): Unit = if (value != output(side)) {
def output(side: ForgeDirection, value: Int): Unit = if (value != output(side)) this.synchronized {
_output(toLocal(side).ordinal()) = value
onRedstoneOutputChanged(side)
val nx = x + side.offsetX
val ny = y + side.offsetY
val nz = z + side.offsetZ
world.notifyBlockOfNeighborChange(nx, ny, nz, block.blockID)
world.notifyBlocksOfNeighborChange(nx, ny, nz, world.getBlockId(nx, ny, nz), side.getOpposite.ordinal)
onRedstoneOutputChanged()
}
def checkRedstoneInputChanged() {
@ -55,10 +62,19 @@ trait RedstoneAware extends RotationAware with IConnectable with IRedstoneEmitte
// ----------------------------------------------------------------------- //
def updateRedstoneInput() {
override def updateEntity() {
super.updateEntity()
if (isServer) {
if (shouldUpdateInput) {
shouldUpdateInput = false
for (side <- ForgeDirection.VALID_DIRECTIONS) {
updateRedstoneInput(side)
}
}
}
}
protected def updateRedstoneInput(side: ForgeDirection) {
val oldInput = _input(side.ordinal())
val newInput = computeInput(side)
_input(side.ordinal()) = newInput
@ -66,8 +82,6 @@ trait RedstoneAware extends RotationAware with IConnectable with IRedstoneEmitte
onRedstoneInputChanged(side)
}
}
}
}
// ----------------------------------------------------------------------- //
@ -124,17 +138,13 @@ trait RedstoneAware extends RotationAware with IConnectable with IRedstoneEmitte
protected def onRedstoneInputChanged(side: ForgeDirection) {}
protected def onRedstoneOutputChanged(side: ForgeDirection) {
if (side == ForgeDirection.UNKNOWN) {
protected def onRedstoneOutputEnabledChanged() = this.synchronized {
world.notifyBlocksOfNeighborChange(x, y, z, block.blockID)
if (isServer) ServerPacketSender.sendRedstoneState(this)
else world.markBlockForRenderUpdate(x, y, z)
}
else {
val nx = x + side.offsetX
val ny = y + side.offsetY
val nz = z + side.offsetZ
world.notifyBlockOfNeighborChange(nx, ny, nz, block.blockID)
world.notifyBlocksOfNeighborChange(nx, ny, nz, world.getBlockId(nx, ny, nz))
}
protected def onRedstoneOutputChanged() {
if (isServer) ServerPacketSender.sendRedstoneState(this)
else world.markBlockForRenderUpdate(x, y, z)
}

View File

@ -154,8 +154,6 @@ class RobotProxy(val robot: Robot) extends Computer with ISidedInventory with Bu
override def checkRedstoneInputChanged() = robot.checkRedstoneInputChanged()
override def updateRedstoneInput() = robot.updateRedstoneInput()
@Optional.Method(modid = "RedLogic")
override def connects(wire: IWire, blockFace: Int, fromDirection: Int) = robot.connects(wire, blockFace, fromDirection)

View File

@ -201,9 +201,6 @@ class Screen(var tier: Int) extends Buffer with SidedEnvironment with Rotatable
override def updateEntity() {
super.updateEntity()
if (isServer) {
updateRedstoneInput()
}
if (isServer && isOn && isOrigin && world.getWorldTime % Settings.get.tickFrequency == 0) {
if (relativeLitArea < 0) {
// The relative lit area is the number of pixels that are not blank

View File

@ -380,6 +380,11 @@ class Machine(val owner: Owner, val rom: Option[ManagedEnvironment], constructor
switchTo(Machine.State.Running)
try {
architecture.runSynchronized()
// This sleep is used to avoid spammy synchronized calls to increase
// the tick time the computer eats unduly. This is a very... rough
// workaround for that problem, and may have to be addressed with
// more care at some point... aka when I have more time.
pause(0.1)
// Check if the callback called pause() or stop().
state.top match {
case Machine.State.Running =>