Skip to content

Commit

Permalink
Fixed lag spike when joining a world
Browse files Browse the repository at this point in the history
  • Loading branch information
Martomate committed Aug 9, 2024
1 parent 4a99172 commit f59dbca
Showing 1 changed file with 40 additions and 13 deletions.
53 changes: 40 additions & 13 deletions server/src/main/scala/hexacraft/server/ServerWorld.scala
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ class ServerWorld(worldProvider: WorldProvider, val worldInfo: WorldInfo)
}
}

def removeChunk(chunkCoords: ChunkRelWorld): Boolean = {
private def removeChunk(chunkCoords: ChunkRelWorld): Boolean = {
val columnCoords = chunkCoords.getColumnRelWorld

var chunkWasRemoved = false
Expand Down Expand Up @@ -312,6 +312,8 @@ class ServerWorld(worldProvider: WorldProvider, val worldInfo: WorldInfo)
requestedLoads: Seq[ChunkRelWorld],
requestedUnloads: Seq[ChunkRelWorld]
): WorldTickResult = {
handleLoadingColumns()

val (chunksAdded, chunksRemoved) = performChunkLoading(requestedLoads, requestedUnloads)

val blocksUpdated = if blockUpdateTimer.tick() then {
Expand Down Expand Up @@ -384,15 +386,20 @@ class ServerWorld(worldProvider: WorldProvider, val worldInfo: WorldInfo)
for coords <- requestedLoads do {
if loadsLeft > 0 then {
if !chunksLoading.contains(coords) && !chunksUnloading.contains(coords) then {
loadsLeft -= 1
chunksLoading(coords) = Future(worldProvider.loadChunkData(coords))(using fsAsync).flatMap {
case Some(loadedTag) =>
Future((Chunk.fromNbt(loadedTag), false))(using genAsync)
// make sure the columns around the chunk are getting loaded
startLoadingColumnIfNeeded(coords.getColumnRelWorld)

columns.get(coords.getColumnRelWorld.value) match { // the column is needed for the chunk to load
case Some(column) =>
loadsLeft -= 1
chunksLoading(coords) = Future(worldProvider.loadChunkData(coords))(using fsAsync).flatMap {
case Some(loadedTag) =>
Future((Chunk.fromNbt(loadedTag), false))(using genAsync)
case None =>
Future((Chunk.fromGenerator(coords, column, worldGenerator), true))(using genAsync)
}(using genAsync)
case None =>
Future(provideColumn(coords.getColumnRelWorld))(using fsAsync).flatMap(column =>
Future((Chunk.fromGenerator(coords, column, worldGenerator), true))(using genAsync)
)(using genAsync)
}(using genAsync)
}
}
}
}
Expand Down Expand Up @@ -429,7 +436,9 @@ class ServerWorld(worldProvider: WorldProvider, val worldInfo: WorldInfo)
result match {
case Failure(_) => // TODO: handle error
case Success((chunk, isNew)) =>
chunksToAdd += ((chunkCoords, chunk, isNew))
if ensurePlannerCanPlanSoon(chunkCoords) then {
chunksToAdd += ((chunkCoords, chunk, isNew))
}
}
}
}
Expand All @@ -440,6 +449,20 @@ class ServerWorld(worldProvider: WorldProvider, val worldInfo: WorldInfo)
finishedChunks
}

private def ensurePlannerCanPlanSoon(coords: ChunkRelWorld): Boolean = {
var readyToPlan = true
val neighbors = coords.extendedNeighbors(5)
val nIt = neighbors.iterator
while nIt.hasNext do {
val ch = nIt.next
if getColumn(ch.getColumnRelWorld).isEmpty then {
startLoadingColumnIfNeeded(ch.getColumnRelWorld)
readyToPlan = false
}
}
readyToPlan
}

private def chunksFinishedUnloading: Seq[ChunkRelWorld] = {
val chunksToRemove = mutable.ArrayBuffer.empty[ChunkRelWorld]
for (chunkCoords, future) <- chunksUnloading do {
Expand Down Expand Up @@ -533,9 +556,7 @@ class ServerWorld(worldProvider: WorldProvider, val worldInfo: WorldInfo)
}
}

private def ensureColumnExists(here: ColumnRelWorld): ChunkColumnTerrain = {
handleLoadingColumns()

private def startLoadingColumnIfNeeded(here: ColumnRelWorld): Unit = {
val columnsToLoad = mutable.ArrayBuffer.empty[ColumnRelWorld]
if !columns.contains(here.value) && !columnsLoading.contains(here.value) then {
columnsToLoad += here
Expand All @@ -553,6 +574,12 @@ class ServerWorld(worldProvider: WorldProvider, val worldInfo: WorldInfo)
)
)(using genAsync)
}
}

private def ensureColumnExists(here: ColumnRelWorld): ChunkColumnTerrain = {
handleLoadingColumns()

startLoadingColumnIfNeeded(here)

if !columns.contains(here.value) then {
Await.ready(columnsLoading(here.value), Duration.Inf)
Expand Down

0 comments on commit f59dbca

Please sign in to comment.