From 190d57bf452b54b1401347f42d621958b57d9193 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Thu, 25 Sep 2014 12:22:49 +0200 Subject: [PATCH] Fixed a bunch of *potentially* derpy coordinate computations. Made sign upgrade in tablet also work when standing "inside" the sign. --- .../scala/li/cil/oc/common/SaveHandler.scala | 2 +- .../cil/oc/common/component/TextBuffer.scala | 2 +- .../event/ChunkloaderUpgradeHandler.scala | 2 +- .../li/cil/oc/server/component/Geolyzer.scala | 2 +- .../UpgradeInventoryController.scala | 8 +++---- .../cil/oc/server/component/UpgradeSign.scala | 24 ++++++++++++------- .../component/UpgradeSolarGenerator.scala | 2 +- .../component/WirelessNetworkCard.scala | 6 ++--- 8 files changed, 28 insertions(+), 20 deletions(-) diff --git a/src/main/scala/li/cil/oc/common/SaveHandler.scala b/src/main/scala/li/cil/oc/common/SaveHandler.scala index 6d7aa69f4..38984a9b8 100644 --- a/src/main/scala/li/cil/oc/common/SaveHandler.scala +++ b/src/main/scala/li/cil/oc/common/SaveHandler.scala @@ -41,7 +41,7 @@ object SaveHandler { } def scheduleSave(container: Container, nbt: NBTTagCompound, name: String, save: NBTTagCompound => Unit) { - scheduleSave(container.world, math.round(container.xPosition - 0.5).toInt, math.round(container.zPosition - 0.5).toInt, nbt, name, writeNBT(save)) + scheduleSave(container.world, math.floor(container.xPosition).toInt, math.floor(container.zPosition).toInt, nbt, name, writeNBT(save)) } def scheduleSave(world: World, x: Int, z: Int, nbt: NBTTagCompound, name: String, data: Array[Byte]) { diff --git a/src/main/scala/li/cil/oc/common/component/TextBuffer.scala b/src/main/scala/li/cil/oc/common/component/TextBuffer.scala index fdc4fff0f..67129b34c 100644 --- a/src/main/scala/li/cil/oc/common/component/TextBuffer.scala +++ b/src/main/scala/li/cil/oc/common/component/TextBuffer.scala @@ -422,7 +422,7 @@ object TextBuffer { def onChunkUnload(e: ChunkEvent.Unload) { val chunk = e.getChunk clientBuffers = clientBuffers.filter(t => { - val keep = t.owner.world != e.world || !chunk.isAtLocation(math.round(t.owner.xPosition - 0.5).toInt << 4, math.round(t.owner.zPosition - 0.5).toInt << 4) + val keep = t.owner.world != e.world || !chunk.isAtLocation(math.floor(t.owner.xPosition).toInt << 4, math.floor(t.owner.zPosition).toInt << 4) if (!keep) { ClientComponentTracker.remove(t.proxy.nodeAddress) } diff --git a/src/main/scala/li/cil/oc/common/event/ChunkloaderUpgradeHandler.scala b/src/main/scala/li/cil/oc/common/event/ChunkloaderUpgradeHandler.scala index 1c38f65d3..9ed821022 100644 --- a/src/main/scala/li/cil/oc/common/event/ChunkloaderUpgradeHandler.scala +++ b/src/main/scala/li/cil/oc/common/event/ChunkloaderUpgradeHandler.scala @@ -59,7 +59,7 @@ object ChunkloaderUpgradeHandler extends LoadingCallback { } def updateLoadedChunk(loader: UpgradeChunkloader) { - val robotChunk = new ChunkCoordIntPair(math.round(loader.owner.xPosition - 0.5).toInt >> 4, math.round(loader.owner.zPosition - 0.5).toInt >> 4) + val robotChunk = new ChunkCoordIntPair(math.floor(loader.owner.xPosition).toInt >> 4, math.floor(loader.owner.zPosition).toInt >> 4) loader.ticket.foreach(ticket => { ticket.getChunkList.collect { case chunk: ChunkCoordIntPair if chunk != robotChunk => ForgeChunkManager.unforceChunk(ticket, chunk) diff --git a/src/main/scala/li/cil/oc/server/component/Geolyzer.scala b/src/main/scala/li/cil/oc/server/component/Geolyzer.scala index d9e7d15ab..b1bf94b0a 100644 --- a/src/main/scala/li/cil/oc/server/component/Geolyzer.scala +++ b/src/main/scala/li/cil/oc/server/component/Geolyzer.scala @@ -21,7 +21,7 @@ class Geolyzer(val owner: Container) extends component.ManagedComponent { if (math.abs(rx) > Settings.get.geolyzerRange || math.abs(rz) > Settings.get.geolyzerRange) { throw new IllegalArgumentException("location out of bounds") } - val (x, y, z) = ((owner.xPosition - 0.5).toInt, (owner.yPosition - 0.5).toInt, (owner.zPosition - 0.5).toInt) + val (x, y, z) = (math.floor(owner.xPosition).toInt, math.floor(owner.yPosition).toInt, math.floor(owner.zPosition).toInt) val bx = x + rx val bz = z + rz diff --git a/src/main/scala/li/cil/oc/server/component/UpgradeInventoryController.scala b/src/main/scala/li/cil/oc/server/component/UpgradeInventoryController.scala index 2ed33c19f..dbfd81f16 100644 --- a/src/main/scala/li/cil/oc/server/component/UpgradeInventoryController.scala +++ b/src/main/scala/li/cil/oc/server/component/UpgradeInventoryController.scala @@ -22,7 +22,7 @@ class UpgradeInventoryController(val owner: Container with Robot) extends compon def getInventorySize(context: Context, args: Arguments): Array[AnyRef] = { val facing = checkSideForInventory(args, 0) if (facing == owner.facing.getOpposite) result(owner.inventorySize) - else InventoryUtils.inventoryAt(owner.world, math.round(owner.xPosition - 0.5).toInt + facing.offsetX, math.round(owner.yPosition - 0.5).toInt + facing.offsetY, math.round(owner.zPosition - 0.5).toInt + facing.offsetZ) match { + else InventoryUtils.inventoryAt(owner.world, math.floor(owner.xPosition).toInt + facing.offsetX, math.floor(owner.yPosition).toInt + facing.offsetY, math.floor(owner.zPosition).toInt + facing.offsetZ) match { case Some(inventory) => result(inventory.getSizeInventory) case _ => result(Unit, "no inventory") } @@ -36,7 +36,7 @@ class UpgradeInventoryController(val owner: Container with Robot) extends compon if (slot < 0 || slot >= owner.inventorySize) result(Unit) else result(owner.getStackInSlot(slot + 1 + owner.containerCount)) } - else InventoryUtils.inventoryAt(owner.world, math.round(owner.xPosition - 0.5).toInt + facing.offsetX, math.round(owner.yPosition - 0.5).toInt + facing.offsetY, math.round(owner.zPosition - 0.5).toInt + facing.offsetZ) match { + else InventoryUtils.inventoryAt(owner.world, math.floor(owner.xPosition).toInt + facing.offsetX, math.floor(owner.yPosition).toInt + facing.offsetY, math.floor(owner.zPosition).toInt + facing.offsetZ) match { case Some(inventory) => if (slot < 0 || slot > inventory.getSizeInventory) result(Unit) else result(inventory.getStackInSlot(slot)) @@ -51,7 +51,7 @@ class UpgradeInventoryController(val owner: Container with Robot) extends compon val selectedSlot = owner.selectedSlot val stack = owner.getStackInSlot(selectedSlot) if (stack != null && stack.stackSize > 0) { - InventoryUtils.inventoryAt(owner.world, math.round(owner.xPosition - 0.5).toInt + facing.offsetX, math.round(owner.yPosition - 0.5).toInt + facing.offsetY, math.round(owner.zPosition - 0.5).toInt + facing.offsetZ) match { + InventoryUtils.inventoryAt(owner.world, math.floor(owner.xPosition).toInt + facing.offsetX, math.floor(owner.yPosition).toInt + facing.offsetY, math.floor(owner.zPosition).toInt + facing.offsetZ) match { case Some(inventory) if inventory.isUseableByPlayer(owner.player) => val slot = args.checkSlot(inventory, 1) if (!InventoryUtils.insertIntoInventorySlot(stack, inventory, facing.getOpposite, slot, count)) { @@ -81,7 +81,7 @@ class UpgradeInventoryController(val owner: Container with Robot) extends compon val facing = checkSideForAction(args, 0) val count = args.optionalItemCount(2) - InventoryUtils.inventoryAt(owner.world, math.round(owner.xPosition - 0.5).toInt + facing.offsetX, math.round(owner.yPosition - 0.5).toInt + facing.offsetY, math.round(owner.zPosition - 0.5).toInt + facing.offsetZ) match { + InventoryUtils.inventoryAt(owner.world, math.floor(owner.xPosition).toInt + facing.offsetX, math.floor(owner.yPosition).toInt + facing.offsetY, math.floor(owner.zPosition).toInt + facing.offsetZ) match { case Some(inventory) if inventory.isUseableByPlayer(owner.player) => val slot = args.checkSlot(inventory, 1) if (InventoryUtils.extractFromInventorySlot(owner.player.inventory.addItemStackToInventory, inventory, facing.getOpposite, slot, count)) { diff --git a/src/main/scala/li/cil/oc/server/component/UpgradeSign.scala b/src/main/scala/li/cil/oc/server/component/UpgradeSign.scala index 45fd6b94c..fe7befbb8 100644 --- a/src/main/scala/li/cil/oc/server/component/UpgradeSign.scala +++ b/src/main/scala/li/cil/oc/server/component/UpgradeSign.scala @@ -16,9 +16,8 @@ class UpgradeSign(val owner: Container with Rotatable) extends component.Managed @Callback(doc = """function():string -- Get the text on the sign in front of the robot.""") def getValue(context: Context, args: Arguments): Array[AnyRef] = { - val facing = owner.facing - owner.world.getBlockTileEntity(math.round(owner.xPosition - 0.5).toInt + facing.offsetX, math.round(owner.yPosition - 0.5).toInt + facing.offsetY, math.round(owner.zPosition - 0.5).toInt + facing.offsetZ) match { - case sign: TileEntitySign => result(sign.signText.mkString("\n")) + findSign match { + case Some(sign) => result(sign.signText.mkString("\n")) case _ => result(Unit, "no sign") } } @@ -26,14 +25,23 @@ class UpgradeSign(val owner: Container with Rotatable) extends component.Managed @Callback(doc = """function(value:string):string -- Set the text on the sign in front of the robot.""") def setValue(context: Context, args: Arguments): Array[AnyRef] = { val text = args.checkString(0).lines.padTo(4, "").map(line => if (line.length > 15) line.substring(0, 15) else line) - val facing = owner.facing - val (sx, sy, sz) = (math.round(owner.xPosition - 0.5).toInt + facing.offsetX, math.round(owner.yPosition - 0.5).toInt + facing.offsetY, math.round(owner.zPosition - 0.5).toInt + facing.offsetZ) - owner.world.getBlockTileEntity(sx, sy, sz) match { - case sign: TileEntitySign => + findSign match { + case Some(sign) => text.copyToArray(sign.signText) - owner.world.markBlockForUpdate(sx, sy, sz) + owner.world.markBlockForUpdate(sign.xCoord, sign.yCoord, sign.zCoord) result(sign.signText.mkString("\n")) case _ => result(Unit, "no sign") } } + + private def findSign = { + val (x, y, z) = (math.floor(owner.xPosition).toInt, math.floor(owner.yPosition).toInt, math.floor(owner.zPosition).toInt) + owner.world.getBlockTileEntity(x, y, z) match { + case sign: TileEntitySign => Option(sign) + case _ => owner.world.getBlockTileEntity(x + owner.facing.offsetX, y + owner.facing.offsetY, z + owner.facing.offsetZ) match { + case sign: TileEntitySign => Option(sign) + case _ => None + } + } + } } diff --git a/src/main/scala/li/cil/oc/server/component/UpgradeSolarGenerator.scala b/src/main/scala/li/cil/oc/server/component/UpgradeSolarGenerator.scala index 39c974893..bc055407f 100644 --- a/src/main/scala/li/cil/oc/server/component/UpgradeSolarGenerator.scala +++ b/src/main/scala/li/cil/oc/server/component/UpgradeSolarGenerator.scala @@ -27,7 +27,7 @@ class UpgradeSolarGenerator(val owner: Container) extends component.ManagedCompo ticksUntilCheck -= 1 if (ticksUntilCheck <= 0) { ticksUntilCheck = 100 - isSunShining = isSunVisible(owner.world, math.round(owner.xPosition - 0.5).toInt, math.round(owner.yPosition - 0.5).toInt + 1, math.round(owner.zPosition - 0.5).toInt) + isSunShining = isSunVisible(owner.world, math.floor(owner.xPosition).toInt, math.floor(owner.yPosition).toInt + 1, math.floor(owner.zPosition).toInt) } if (isSunShining) { node.changeBuffer(Settings.get.solarGeneratorEfficiency) diff --git a/src/main/scala/li/cil/oc/server/component/WirelessNetworkCard.scala b/src/main/scala/li/cil/oc/server/component/WirelessNetworkCard.scala index 0ff508d2f..154434d2d 100644 --- a/src/main/scala/li/cil/oc/server/component/WirelessNetworkCard.scala +++ b/src/main/scala/li/cil/oc/server/component/WirelessNetworkCard.scala @@ -20,11 +20,11 @@ class WirelessNetworkCard(val owner: Container) extends NetworkCard with Wireles // ----------------------------------------------------------------------- // - override def x = math.round(owner.xPosition - 0.5).toInt + override def x = math.floor(owner.xPosition).toInt - override def y = math.round(owner.yPosition - 0.5).toInt + override def y = math.floor(owner.yPosition).toInt - override def z = math.round(owner.zPosition - 0.5).toInt + override def z = math.floor(owner.zPosition).toInt override def world = owner.world