Microdungeon placement now don't hog event loop

This commit is contained in:
DBotThePony 2024-04-20 22:58:30 +07:00
parent 5c13567fed
commit 151470f76d
Signed by: DBot
GPG Key ID: DCC23B5715498507
3 changed files with 12 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package ru.dbotthepony.kstarbound.server.world
import it.unimi.dsi.fastutil.objects.ObjectAVLTreeSet import it.unimi.dsi.fastutil.objects.ObjectAVLTreeSet
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.delay
import kotlinx.coroutines.future.await import kotlinx.coroutines.future.await
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.suspendCancellableCoroutine import kotlinx.coroutines.suspendCancellableCoroutine
@ -59,6 +60,7 @@ import kotlin.concurrent.withLock
import kotlin.coroutines.cancellation.CancellationException import kotlin.coroutines.cancellation.CancellationException
import kotlin.coroutines.resume import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException import kotlin.coroutines.resumeWithException
import kotlin.math.min
class ServerChunk(world: ServerWorld, pos: ChunkPos) : Chunk<ServerWorld, ServerChunk>(world, pos) { class ServerChunk(world: ServerWorld, pos: ChunkPos) : Chunk<ServerWorld, ServerChunk>(world, pos) {
override var state: ChunkState = ChunkState.FRESH override var state: ChunkState = ChunkState.FRESH
@ -758,6 +760,10 @@ class ServerChunk(world: ServerWorld, pos: ChunkPos) : Chunk<ServerWorld, Server
break break
} }
// some breathing room for other code, since placement checking is performance intense operation
if (!world.isInPreparation && world.clients.isNotEmpty())
delay(min(60L, anchor.reader.size.x * anchor.reader.size.y / 100L))
} }
} }
} }

View File

@ -306,8 +306,12 @@ class ServerWorld private constructor(
} }
} }
var isInPreparation = false
private set
private suspend fun prepare0() { private suspend fun prepare0() {
try { try {
isInPreparation = true
isBusy++ isBusy++
val random = random(template.seed) val random = random(template.seed)
@ -362,6 +366,7 @@ class ServerWorld private constructor(
} }
} finally { } finally {
isBusy-- isBusy--
isInPreparation = false
} }
} }

View File

@ -352,9 +352,9 @@ class ServerWorldTracker(val world: ServerWorld, val client: ServerConnection, p
// instantly added back because new world rejected us // instantly added back because new world rejected us
world.eventLoop.execute { remove0() } world.eventLoop.execute { remove0() }
scope.cancel()
damageTilesQueue.close() damageTilesQueue.close()
modifyTilesQueue.close() modifyTilesQueue.close()
scope.cancel()
} }
} }