mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-10 18:44:05 +09:00
cut wires are also power-cut
This commit is contained in:
@@ -410,7 +410,7 @@ open class GameWorld() : Disposable {
|
|||||||
if (wiringGraph[blockAddr]!![itemID] == null)
|
if (wiringGraph[blockAddr]!![itemID] == null)
|
||||||
wiringGraph[blockAddr]!![itemID] = WiringSimCell(0, vector)
|
wiringGraph[blockAddr]!![itemID] = WiringSimCell(0, vector)
|
||||||
|
|
||||||
wiringGraph[blockAddr]!![itemID]!!.emt = vector
|
wiringGraph[blockAddr]!![itemID]!!.emt.set(vector)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun addWireRecvStateOf(x: Int, y: Int, itemID: ItemID, state: WireRecvState) {
|
fun addWireRecvStateOf(x: Int, y: Int, itemID: ItemID, state: WireRecvState) {
|
||||||
@@ -649,8 +649,8 @@ open class GameWorld() : Disposable {
|
|||||||
*/
|
*/
|
||||||
data class WiringSimCell(
|
data class WiringSimCell(
|
||||||
var cnx: Int = 0, // connections
|
var cnx: Int = 0, // connections
|
||||||
var emt: Vector2 = Vector2(0.0, 0.0), // i'm emitting this much power
|
val emt: Vector2 = Vector2(0.0, 0.0), // i'm emitting this much power
|
||||||
var rcv: ArrayList<WireRecvState> = ArrayList() // how far away are the power sources
|
val rcv: ArrayList<WireRecvState> = ArrayList() // how far away are the power sources
|
||||||
)
|
)
|
||||||
|
|
||||||
fun getTemperature(worldTileX: Int, worldTileY: Int): Float? {
|
fun getTemperature(worldTileX: Int, worldTileY: Int): Float? {
|
||||||
|
|||||||
@@ -11,8 +11,6 @@ import net.torvald.terrarum.modulebasegame.gameactors.ActorHumanoid
|
|||||||
import net.torvald.terrarum.modulebasegame.gameactors.Electric
|
import net.torvald.terrarum.modulebasegame.gameactors.Electric
|
||||||
import net.torvald.terrarum.modulebasegame.gameactors.FixtureBase
|
import net.torvald.terrarum.modulebasegame.gameactors.FixtureBase
|
||||||
import org.dyn4j.geometry.Vector2
|
import org.dyn4j.geometry.Vector2
|
||||||
import java.util.*
|
|
||||||
import kotlin.collections.ArrayList
|
|
||||||
import kotlin.math.roundToInt
|
import kotlin.math.roundToInt
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -442,17 +440,31 @@ object WorldSimulator {
|
|||||||
it is FixtureBase && it is Electric && it.inUpdateRange(world) && it.wireEmitterTypes.isNotEmpty()
|
it is FixtureBase && it is Electric && it.inUpdateRange(world) && it.wireEmitterTypes.isNotEmpty()
|
||||||
} as List<FixtureBase>
|
} as List<FixtureBase>
|
||||||
|
|
||||||
|
private val wireSimMarked = HashSet<Long>()
|
||||||
|
private val wireSimPoints = Queue<WireGraphCursor>()
|
||||||
|
private val oldTraversedNodes = ArrayList<WireGraphCursor>()
|
||||||
|
|
||||||
private fun simulateWires(delta: Float) {
|
private fun simulateWires(delta: Float) {
|
||||||
|
// unset old wires before we begin
|
||||||
|
oldTraversedNodes.forEach { (x, y, _, _, wire) ->
|
||||||
|
world.getAllWiringGraph(x, y)?.get(wire)?.emt?.set(0.0, 0.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
oldTraversedNodes.clear()
|
||||||
|
|
||||||
wiresimGetSourceBlocks().let { sources ->
|
wiresimGetSourceBlocks().let { sources ->
|
||||||
// signal-emitting fixtures must set emitState of its own tiles via update()
|
// signal-emitting fixtures must set emitState of its own tiles via update()
|
||||||
sources.forEach {
|
sources.forEach {
|
||||||
(it as Electric).wireEmitterTypes.forEach { wireType, bbi ->
|
(it as Electric).wireEmitterTypes.forEach { wireType, bbi ->
|
||||||
|
|
||||||
val startingPoint = WireGraphCursor(it.worldBlockPos!! + it.blockBoxIndexToPoint2i(bbi))
|
val startingPoint = it.worldBlockPos!! + it.blockBoxIndexToPoint2i(bbi)
|
||||||
val signal = (it as Electric).wireEmission[bbi] ?: Vector2(0.0, 0.0)
|
val signal = (it as Electric).wireEmission[bbi] ?: Vector2(0.0, 0.0)
|
||||||
|
|
||||||
world.getAllWiringGraph(startingPoint.x, startingPoint.y)?.keys?.filter { WireCodex[it].accepts == wireType }?.forEach { wire ->
|
world.getAllWiringGraph(startingPoint.x, startingPoint.y)?.keys?.filter { WireCodex[it].accepts == wireType }?.forEach { wire ->
|
||||||
traverseWireGraph(world, wire, startingPoint, signal)
|
val simStartingPoint = WireGraphCursor(startingPoint, wire)
|
||||||
|
wireSimMarked.clear()
|
||||||
|
wireSimPoints.clear()
|
||||||
|
traverseWireGraph(world, wire, simStartingPoint, signal)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -470,23 +482,22 @@ object WorldSimulator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var point = startingPoint.copy()
|
var point = startingPoint.copy()
|
||||||
val points = Queue<WireGraphCursor>() // a queue, enqueued at the end
|
|
||||||
val marked = HashSet<Long>()
|
|
||||||
|
|
||||||
fun mark(point: WireGraphCursor) {
|
fun mark(point: WireGraphCursor) {
|
||||||
marked.add(point.longHash())
|
wireSimMarked.add(point.longHash())
|
||||||
|
oldTraversedNodes.add(point.copy())
|
||||||
// do some signal action
|
// do some signal action
|
||||||
world.setWireEmitStateOf(point.x, point.y, wire, signal)
|
world.setWireEmitStateOf(point.x, point.y, wire, signal)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isMarked(point: WireGraphCursor) = marked.contains(point.longHash())
|
fun isMarked(point: WireGraphCursor) = wireSimMarked.contains(point.longHash())
|
||||||
fun enq(point: WireGraphCursor) = points.addFirst(point.copy())
|
fun enq(point: WireGraphCursor) = wireSimPoints.addFirst(point.copy())
|
||||||
fun deq() = points.removeLast()
|
fun deq() = wireSimPoints.removeLast()
|
||||||
|
|
||||||
enq(point)
|
enq(point)
|
||||||
mark(point)
|
mark(point)
|
||||||
|
|
||||||
while (points.notEmpty()) {
|
while (wireSimPoints.notEmpty()) {
|
||||||
point = deq()
|
point = deq()
|
||||||
// TODO if we found a power receiver, do something to it
|
// TODO if we found a power receiver, do something to it
|
||||||
world.getWireGraphOf(point.x, point.y, wire)?.let { connections ->
|
world.getWireGraphOf(point.x, point.y, wire)?.let { connections ->
|
||||||
@@ -519,9 +530,10 @@ object WorldSimulator {
|
|||||||
var x: Int,
|
var x: Int,
|
||||||
var y: Int,
|
var y: Int,
|
||||||
var fromWhere: Int, //1: right, 2: down, 4: left, 8: up, 0: *shrug*
|
var fromWhere: Int, //1: right, 2: down, 4: left, 8: up, 0: *shrug*
|
||||||
var len: Int
|
var len: Int,
|
||||||
|
var wire: ItemID
|
||||||
) {
|
) {
|
||||||
constructor(point2i: Point2i): this(point2i.x, point2i.y, 0, 0)
|
constructor(point2i: Point2i, wire: ItemID): this(point2i.x, point2i.y, 0, 0, wire)
|
||||||
|
|
||||||
fun moveOneCell(dir: Int): WireGraphCursor {
|
fun moveOneCell(dir: Int): WireGraphCursor {
|
||||||
when (dir) {
|
when (dir) {
|
||||||
|
|||||||
Reference in New Issue
Block a user