working wire cutter

This commit is contained in:
minjaesong
2022-07-13 21:28:59 +09:00
parent 2a7e2b7ce4
commit c5360cdcb8
5 changed files with 38 additions and 24 deletions

View File

@@ -22,7 +22,7 @@ class GameCrashHandler(e: Throwable) : JFrame() {
private val outputStream = object : OutputStream() {
override fun write(p0: Int) {
htmlSB.appendCodePoint(p0)
htmlSB.append((p0 and 255).toChar())
}
}

View File

@@ -278,6 +278,15 @@ object Terrarum : Disposable {
INVALID, CENTRE, LEFT, BOTTOM, RIGHT, TOP
}
fun SubtileVector.toInt() = when (this) {
SubtileVector.INVALID -> throw IllegalArgumentException()
SubtileVector.RIGHT -> 1
SubtileVector.BOTTOM -> 2
SubtileVector.LEFT -> 4
SubtileVector.TOP -> 8
SubtileVector.CENTRE -> 0
}
data class MouseSubtile4(val x: Int, val y: Int, val vector: SubtileVector) {
val nx = when (vector) {
@@ -286,7 +295,7 @@ object Terrarum : Disposable {
SubtileVector.BOTTOM -> x
SubtileVector.LEFT -> x - 1
SubtileVector.TOP -> x
else -> throw IllegalArgumentException("Invalid vector index $vector for subtile $this")
else -> x
}
val ny = when (vector) {
@@ -295,7 +304,7 @@ object Terrarum : Disposable {
SubtileVector.BOTTOM -> y + 1
SubtileVector.LEFT -> y
SubtileVector.TOP -> y - 1
else -> throw IllegalArgumentException("Invalid vector index $vector for subtile $this")
else -> y
}
val currentTileCoord = x to y

View File

@@ -354,21 +354,16 @@ open class GameWorld() : Disposable {
Terrarum.ingame?.queueWireChangedEvent(tile, true, x, y)
Terrarum.ingame?.modified(LandUtil.LAYER_WIRE, x, y)
}
/*
// figure out wiring graphs
val matchingNeighbours = WireActor.WIRE_NEARBY.mapIndexed { index, (tx, ty) ->
(getAllWiresFrom(x + tx, y + ty)?.contains(tile) == true).toInt() shl index
}.sum()
for (i in 0..3) {
if (matchingNeighbours and WIRE_POS_MAP[i] > 0) {
val (tx, ty) = WireActor.WIRE_NEARBY[i]
val old = getWireGraphOf(x + tx, y + ty, tile) ?: 0
setWireGraphOf(x + tx, y + ty, tile, old and (15 - WIRE_ANTIPOS_MAP[i]))
}
}
// disconnect neighbouring nodes
/* RIGHT */getWireGraphOf(x+1, y, tile)?.let { setWireGraphOf(x+1, y, tile, it and 0b1011) }
/* BOTTOM */if (y+1 < height) getWireGraphOf(x, y+1, tile)?.let { setWireGraphOf(x, y+1, tile, it and 0b0111) }
/* LEFT */getWireGraphOf(x-1, y, tile)?.let { setWireGraphOf(x-1, y, tile, it and 0b1110) }
/* TOP */if (y-1 >= 0) getWireGraphOf(x, y-1, tile)?.let { setWireGraphOf(x, y-1, tile, it and 0b1101) }
// remove wire from this tile
wiringGraph[blockAddr]!!.remove(tile)
wirings[blockAddr]!!.ws.remove(tile)*/
wirings[blockAddr]!!.ws.remove(tile)
}
}

View File

@@ -76,6 +76,10 @@ object BlockBase {
(Terrarum.ingame!! as TerrarumIngame).selectedWireRenderClass = ""
}
private val wireVectorChars = arrayOf(
"· · · ·","· · · →","· · ↓ ·","· · ↓ →","· ← · ·","· ← · →","· ← ↓ ·","· ← ↓ →","↑ · · ·","↑ · · →","↑ · ↓ ·","↑ · ↓ →","↑ ← · ·","↑ ← · →","↑ ← ↓ ·","↑ ← ↓ →",
)
fun Int.toWireVectorBitsStr(): String = "[${wireVectorChars[this]}]($this)"
fun Int.wireNodeMirror() = this.shl(4).or(this).ushr(2).and(15)
fun wireNodesConnectedEachOther(oldToNewVector: Int, new: Int?, old: Int?): Boolean {
return if (new == null || old == null || oldToNewVector == 0) false
@@ -86,7 +90,7 @@ object BlockBase {
val q = newToOldVector and new
if (p == 0 && q == 0) false
else if (p > 0 && q > 0) true
else throw IllegalStateException("oldToNewVector = $oldToNewVector, new = $new, old == $old")
else throw IllegalStateException("oldToNewVector = ${oldToNewVector.toWireVectorBitsStr()}, new = ${new.toWireVectorBitsStr()}, old = ${old.toWireVectorBitsStr()}")
}
}

View File

@@ -3,17 +3,19 @@ package net.torvald.terrarum.modulebasegame.gameitems
import com.badlogic.gdx.graphics.g2d.TextureRegion
import net.torvald.terrarum.CommonResourcePool
import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.Terrarum.toInt
import net.torvald.terrarum.TerrarumAppConfiguration.TILE_SIZED
import net.torvald.terrarum.gameactors.ActorWithBody
import net.torvald.terrarum.gameitems.GameItem
import net.torvald.terrarum.gameitems.ItemID
import net.torvald.terrarum.gameitems.mouseInInteractableRangeTools
import net.torvald.terrarum.gameworld.GameWorld
import net.torvald.terrarum.itemproperties.Material
import net.torvald.terrarum.modulebasegame.TerrarumIngame
import net.torvald.terrarum.modulebasegame.gameactors.DroppedItem
import net.torvald.terrarum.modulebasegame.gameitems.BlockBase.wireNodeMirror
import net.torvald.terrarum.notEmptyOrNull
import net.torvald.terrarum.toInt
import net.torvald.terrarum.utils.WiringGraphMap
/**
* Modular approach to the wire cutter function
@@ -22,8 +24,12 @@ import net.torvald.terrarum.toInt
*/
object WireCutterBase {
private fun disconnect(world: GameWorld, item: ItemID, x1: Int, y1: Int, x2: Int, y2: Int) {
private fun disconnect(item: ItemID, mapP: WiringGraphMap, mapN: WiringGraphMap, subTile: Terrarum.SubtileVector) {
val pBitMask = subTile.toInt() // bit mask to turn off
val nBitMask = pBitMask.wireNodeMirror()
mapP[item]!!.cnx = mapP[item]!!.cnx and (15 - pBitMask) // (15 - pBitMask) -> bit mask to retain
mapN[item]!!.cnx = mapN[item]!!.cnx and (15 - nBitMask)
}
fun startPrimaryUse(item: GameItem, actor: ActorWithBody, delta: Float, wireFilter: (ItemID) -> Boolean) = mouseInInteractableRangeTools(actor, item) {
@@ -37,7 +43,7 @@ object WireCutterBase {
if (mvec == Terrarum.SubtileVector.CENTRE) {
val wireNet = ingame.world.getAllWiresFrom(mtx, mty)
val wireItems = wireNet.first?.cloneToList()
val wireItems = wireNet.first
wireItems?.filter(wireFilter)?.notEmptyOrNull()?.forEach {
ingame.world.removeTileWire(mtx, mty, it, false)
@@ -50,16 +56,16 @@ object WireCutterBase {
val (ntx, nty) = mouseTile.nextTileCoord
val wireNetP = ingame.world.getAllWiresFrom(mtx, mty)
val wireNetN = ingame.world.getAllWiresFrom(mtx, mty)
val wireItemsP = wireNetP.first?.cloneToList()
val wireItemsN = wireNetN.first?.cloneToList()
val wireNetN = ingame.world.getAllWiresFrom(ntx, nty)
val wireItemsP = wireNetP.first
val wireItemsN = wireNetN.first
// get intersection of wireItemsP and wireItemsN
if (wireItemsP != null && wireItemsN != null) {
val wireItems = wireItemsP intersect wireItemsN
wireItems.filter(wireFilter).notEmptyOrNull()?.forEach {
disconnect(ingame.world, it, mtx, mty, ntx, nty)
disconnect(it, wireNetP.second!!, wireNetN.second!!, mouseTile.vector)
} ?: return@mouseInInteractableRangeTools false
true