Some renamings

This commit is contained in:
DBotThePony 2024-02-03 13:46:50 +07:00
parent 8c731366af
commit c65d3f1a88
Signed by: DBot
GPG Key ID: DCC23B5715498507
5 changed files with 52 additions and 44 deletions

View File

@ -46,7 +46,7 @@ abstract class Connection(val side: ConnectionSide, val type: ConnectionType, va
protected abstract fun inGame() protected abstract fun inGame()
fun helloReceived(helloPacket: HelloPacket) { fun helloReceived(helloPacket: HelloPacket) {
LOGGER.info("${side.opposite} HELLO Received from ${channel?.remoteAddress()}: $helloPacket") LOGGER.info("Handshake received on $side from ${channel?.remoteAddress()}: $helloPacket")
otherSide = helloPacket otherSide = helloPacket
if (side == ConnectionSide.SERVER) { if (side == ConnectionSide.SERVER) {

View File

@ -23,7 +23,7 @@ class ServerWorld(
val server: StarboundServer, val server: StarboundServer,
seed: Long, seed: Long,
geometry: WorldGeometry, geometry: WorldGeometry,
) : World<ServerWorld, ServerChunk>(seed, geometry), Closeable { ) : World<ServerWorld, ServerChunk>(seed, geometry) {
init { init {
server.worlds.add(this) server.worlds.add(this)
} }
@ -49,10 +49,8 @@ class ServerWorld(
private var nextThink = 0L private var nextThink = 0L
override fun close() { override fun close() {
if (!isStopped) { super.close()
isStopped = true isStopped = true
lock.withLock { onUnload() }
}
} }
fun startThread() { fun startThread() {
@ -60,10 +58,6 @@ class ServerWorld(
thread.start() thread.start()
} }
private fun onUnload() {
}
private fun runThread() { private fun runThread() {
while (!isStopped) { while (!isStopped) {
var diff = System.nanoTime() - nextThink var diff = System.nanoTime() - nextThink
@ -80,8 +74,7 @@ class ServerWorld(
try { try {
think() think()
} catch (err: Throwable) { } catch (err: Throwable) {
isStopped = true close()
onUnload()
throw err throw err
} }
} }
@ -127,7 +120,7 @@ class ServerWorld(
fun permanentChunkTicket(pos: ChunkPos): ITicket { fun permanentChunkTicket(pos: ChunkPos): ITicket {
lock.withLock { lock.withLock {
return getTicketList(pos).Ticket().init() return getTicketList(pos).Ticket()
} }
} }
@ -135,7 +128,7 @@ class ServerWorld(
require(time > 0) { "Invalid ticket time: $time" } require(time > 0) { "Invalid ticket time: $time" }
lock.withLock { lock.withLock {
return getTicketList(pos).TimedTicket(time).init() return getTicketList(pos).TimedTicket(time)
} }
} }
@ -181,13 +174,14 @@ class ServerWorld(
return temporary.isNotEmpty() || permanent.isNotEmpty() return temporary.isNotEmpty() || permanent.isNotEmpty()
} }
open inner class Ticket : ITicket { abstract inner class AbstractTicket : ITicket {
open fun init(): Ticket { final override val id: Int = nextTicketID.getAndIncrement()
if (this is TimedTicket) final override val pos: ChunkPos
temporary.add(this) get() = this@TicketList.pos
else
permanent.add(this)
final override var isCanceled: Boolean = false
fun init() {
if (first) { if (first) {
first = false first = false
@ -196,12 +190,12 @@ class ServerWorld(
if (chunkProviders.isNotEmpty()) { if (chunkProviders.isNotEmpty()) {
composeFutures(chunkProviders) composeFutures(chunkProviders)
{ if (!isValid) CompletableFuture.completedFuture(KOptional.empty()) else it.getTiles(pos) } { if (!isValid) CompletableFuture.completedFuture(KOptional.empty()) else it.getTiles(pos) }
.thenAccept(Consumer { tiles -> .thenAccept(Consumer { tiles ->
if (!isValid || !tiles.isPresent) return@Consumer if (!isValid || !tiles.isPresent) return@Consumer
composeFutures(chunkProviders) composeFutures(chunkProviders)
{ if (!isValid) CompletableFuture.completedFuture(KOptional.empty()) else it.getEntities(pos) } { if (!isValid) CompletableFuture.completedFuture(KOptional.empty()) else it.getEntities(pos) }
.thenAcceptAsync(Consumer { ents -> .thenAcceptAsync(Consumer { ents ->
if (!isValid) return@Consumer if (!isValid) return@Consumer
val chunk = chunkMap.compute(pos) ?: return@Consumer val chunk = chunkMap.compute(pos) ?: return@Consumer
@ -213,44 +207,49 @@ class ServerWorld(
} }
} }
}, mailbox) }, mailbox)
}) })
} }
} }
} }
return this
} }
final override val id: Int = nextTicketID.getAndIncrement()
final override val pos: ChunkPos
get() = this@TicketList.pos
final override fun cancel() { final override fun cancel() {
if (isCanceled) return if (isCanceled) return
lock.withLock { lock.withLock {
if (isCanceled) return if (isCanceled) return
isCanceled = true isCanceled = true
onCancel()
if (this is TimedTicket)
temporary.remove(this)
else
permanent.remove(this)
} }
} }
final override var isCanceled: Boolean = false protected abstract fun onCancel()
} }
inner class TimedTicket(expiresAt: Int) : Ticket(), ITimedTicket { inner class Ticket : AbstractTicket() {
init {
permanent.add(this)
init()
}
override fun onCancel() {
permanent.remove(this)
}
}
inner class TimedTicket(expiresAt: Int) : AbstractTicket(), ITimedTicket {
var expiresAt = expiresAt + ticks var expiresAt = expiresAt + ticks
override val timeRemaining: Int override val timeRemaining: Int
get() = (expiresAt - ticks).coerceAtLeast(0) get() = (expiresAt - ticks).coerceAtLeast(0)
override fun init(): TimedTicket { init {
super.init() temporary.add(this)
return this init()
}
override fun onCancel() {
temporary.remove(this)
} }
override fun prolong(ticks: Int) { override fun prolong(ticks: Int) {

View File

@ -46,7 +46,7 @@ abstract class Chunk<WorldType : World<WorldType, This>, This : Chunk<WorldType,
val entities = ReferenceOpenHashSet<Entity>() val entities = ReferenceOpenHashSet<Entity>()
val objects = ReferenceOpenHashSet<WorldObject>() val objects = ReferenceOpenHashSet<WorldObject>()
protected val subscribers = ObjectArraySet<IChunkSubscriber>() protected val subscribers = ObjectArraySet<IChunkListener>()
// local cells' tile access // local cells' tile access
val localBackgroundView = TileView.Background(this) val localBackgroundView = TileView.Background(this)
@ -135,6 +135,8 @@ abstract class Chunk<WorldType : World<WorldType, This>, This : Chunk<WorldType,
protected open fun cellChanges(x: Int, y: Int, cell: ImmutableCell) { protected open fun cellChanges(x: Int, y: Int, cell: ImmutableCell) {
changeset++ changeset++
cellChangeset++ cellChangeset++
subscribers.forEach { it.cellChanges(x, y, cell) }
} }
protected inline fun forEachNeighbour(block: (This) -> Unit) { protected inline fun forEachNeighbour(block: (This) -> Unit) {
@ -152,11 +154,11 @@ abstract class Chunk<WorldType : World<WorldType, This>, This : Chunk<WorldType,
return world.randomLongFor(x or pos.x shl CHUNK_SIZE_BITS, y or pos.y shl CHUNK_SIZE_BITS) return world.randomLongFor(x or pos.x shl CHUNK_SIZE_BITS, y or pos.y shl CHUNK_SIZE_BITS)
} }
fun addSubscriber(subscriber: IChunkSubscriber) { fun addListener(subscriber: IChunkListener) {
subscribers.add(subscriber) subscribers.add(subscriber)
} }
fun removeSubscriber(subscriber: IChunkSubscriber) { fun removeListener(subscriber: IChunkListener) {
subscribers.remove(subscriber) subscribers.remove(subscriber)
} }

View File

@ -1,11 +1,13 @@
package ru.dbotthepony.kstarbound.world package ru.dbotthepony.kstarbound.world
import ru.dbotthepony.kstarbound.world.api.ImmutableCell
import ru.dbotthepony.kstarbound.world.entities.Entity import ru.dbotthepony.kstarbound.world.entities.Entity
import ru.dbotthepony.kstarbound.world.entities.WorldObject import ru.dbotthepony.kstarbound.world.entities.WorldObject
interface IChunkSubscriber { interface IChunkListener {
fun onEntityAdded(entity: Entity) fun onEntityAdded(entity: Entity)
fun onEntityRemoved(entity: Entity) fun onEntityRemoved(entity: Entity)
fun onObjectAdded(obj: WorldObject) fun onObjectAdded(obj: WorldObject)
fun onObjectRemoved(obj: WorldObject) fun onObjectRemoved(obj: WorldObject)
fun cellChanges(x: Int, y: Int, cell: ImmutableCell)
} }

View File

@ -24,6 +24,7 @@ import ru.dbotthepony.kvector.api.IStruct2i
import ru.dbotthepony.kvector.arrays.Object2DArray import ru.dbotthepony.kvector.arrays.Object2DArray
import ru.dbotthepony.kvector.util2d.AABB import ru.dbotthepony.kvector.util2d.AABB
import ru.dbotthepony.kvector.vector.Vector2d import ru.dbotthepony.kvector.vector.Vector2d
import java.io.Closeable
import java.util.concurrent.ForkJoinPool import java.util.concurrent.ForkJoinPool
import java.util.concurrent.locks.ReentrantLock import java.util.concurrent.locks.ReentrantLock
import java.util.function.Predicate import java.util.function.Predicate
@ -34,7 +35,7 @@ import kotlin.concurrent.withLock
abstract class World<This : World<This, ChunkType>, ChunkType : Chunk<This, ChunkType>>( abstract class World<This : World<This, ChunkType>, ChunkType : Chunk<This, ChunkType>>(
val seed: Long, val seed: Long,
val geometry: WorldGeometry, val geometry: WorldGeometry,
) : ICellAccess { ) : ICellAccess, Closeable {
// whenever provided cell position is within actual world borders, ignoring wrapping // whenever provided cell position is within actual world borders, ignoring wrapping
fun inBounds(x: Int, y: Int) = geometry.x.inBoundsCell(x) && geometry.y.inBoundsCell(y) fun inBounds(x: Int, y: Int) = geometry.x.inBoundsCell(x) && geometry.y.inBoundsCell(y)
fun inBounds(value: IStruct2i) = geometry.x.inBoundsCell(value.component1()) && geometry.y.inBoundsCell(value.component2()) fun inBounds(value: IStruct2i) = geometry.x.inBoundsCell(value.component1()) && geometry.y.inBoundsCell(value.component2())
@ -279,6 +280,10 @@ abstract class World<This : World<This, ChunkType>, ChunkType : Chunk<This, Chun
protected abstract fun chunkFactory(pos: ChunkPos): ChunkType protected abstract fun chunkFactory(pos: ChunkPos): ChunkType
override fun close() {
mailbox.shutdownNow()
}
fun queryCollisions(aabb: AABB): MutableList<CollisionPoly> { fun queryCollisions(aabb: AABB): MutableList<CollisionPoly> {
val result = ArrayList<CollisionPoly>() val result = ArrayList<CollisionPoly>()
val tiles = aabb.encasingIntAABB() val tiles = aabb.encasingIntAABB()