Skip to content

Commit

Permalink
Recaluclating the roadnetwork creates 1 snapshot per world instead of…
Browse files Browse the repository at this point in the history
… 1 per node
  • Loading branch information
gabber235 committed Nov 13, 2024
1 parent 0a34cf8 commit de9e0af
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ import com.typewritermc.engine.paper.entry.roadnetwork.gps.roadNetworkFindPath
import com.typewritermc.engine.paper.entry.roadnetwork.pathfinding.PFInstanceSpace
import com.typewritermc.engine.paper.plugin
import com.typewritermc.engine.paper.utils.ThreadType.DISPATCHERS_ASYNC
import kotlinx.coroutines.Job
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.*
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import java.util.concurrent.Executors
import kotlin.math.max
import kotlin.math.min

class RoadNetworkEditor(
private val ref: Ref<out RoadNetworkEntry>,
Expand Down Expand Up @@ -64,16 +63,17 @@ class RoadNetworkEditor(
fun recalculateEdges() {
jobRecalculateEdges?.cancel()

val nrOfThreads = Runtime.getRuntime().availableProcessors()
val nrOfThreads = max(Runtime.getRuntime().availableProcessors() / 2, 1)
jobRecalculateEdges = plugin.launch(Executors.newFixedThreadPool(nrOfThreads).asCoroutineDispatcher()) {
update {
it.copy(edges = emptyList())
}
recalculateEdges.set(network.nodes.size)
val instancesSpaces = network.nodes.associate { it.location.world.uid to PFInstanceSpace(it.location.world) }
coroutineScope {
network.nodes.map {
launch {
recalculateEdgesForNode(it)
recalculateEdgesForNode(it, instancesSpaces[it.location.world.uid]!!)
recalculateEdges.decrementAndGet()
}
}
Expand All @@ -82,8 +82,7 @@ class RoadNetworkEditor(
}
}

private suspend fun recalculateEdgesForNode(node: RoadNode) {
val instance = PFInstanceSpace(node.location.world)
private suspend fun recalculateEdgesForNode(node: RoadNode, instance: PFInstanceSpace) {
val interestingNodes = network.nodes
.filter { it != node && it.location.world == node.location.world && it.location.distanceSquared(node.location) < roadNetworkMaxDistance * roadNetworkMaxDistance }
val generatedEdges =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ class RoadNetworkContentMode(context: ContentContext, player: Player) : ContentM
private fun addRoadNode() = DISPATCHERS_ASYNC.launch {
val node = createNode()
editorComponent.update { it.copy(nodes = it.nodes + node) }
editorComponent.recalculateEdges()
ContentModeTrigger(
context,
SelectedRoadNodeContentMode(context, player, ref, node.id, true)
Expand Down

0 comments on commit de9e0af

Please sign in to comment.