Skip to content

Commit

Permalink
Improved performance on not so fast computers
Browse files Browse the repository at this point in the history
  • Loading branch information
Martomate committed Aug 3, 2024
1 parent af378ba commit 6a4aed2
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 18 deletions.
9 changes: 0 additions & 9 deletions client/src/main/scala/hexacraft/client/ClientWorld.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@ import hexacraft.world.coord.*
import hexacraft.world.entity.{Entity, EntityFactory, EntityPhysicsSystem}

import java.util.UUID
import java.util.concurrent.TimeUnit
import scala.annotation.tailrec
import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
import scala.concurrent.{Await, Future}
import scala.concurrent.duration.Duration

object ClientWorld {
class WorldTickResult(
Expand All @@ -27,8 +24,6 @@ object ClientWorld {
class ClientWorld(val worldInfo: WorldInfo) extends BlockRepository with BlocksInWorld {
given size: CylinderSize = worldInfo.worldSize

private val backgroundTasks: mutable.ArrayBuffer[Future[Unit]] = mutable.ArrayBuffer.empty

private val columns: mutable.LongMap[ChunkColumnTerrain] = mutable.LongMap.empty
private val chunks: mutable.LongMap[Chunk] = mutable.LongMap.empty

Expand Down Expand Up @@ -359,10 +354,6 @@ class ClientWorld(val worldInfo: WorldInfo) extends BlockRepository with BlocksI
def unload(): Unit = {
chunks.clear()
columns.clear()

for t <- backgroundTasks do {
Await.result(t, Duration(10, TimeUnit.SECONDS))
}
}

private def onSetBlock(coords: BlockRelWorld, block: BlockState): Unit = {
Expand Down
18 changes: 15 additions & 3 deletions client/src/main/scala/hexacraft/client/GameClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ import org.joml.{Matrix4f, Vector2f, Vector3d, Vector3f}
import org.zeromq.*

import java.util.UUID
import java.util.concurrent.TimeUnit
import scala.collection.mutable
import scala.concurrent.{Await, Future}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.Duration
import scala.util.Random

object GameClient {
Expand Down Expand Up @@ -490,12 +494,20 @@ class GameClient(
.sum
}

private var tickFut: Option[Future[Seq[Nbt]]] = None

def tick(ctx: TickContext): Unit = {
// Act on the server info requested last tick, and send a new request to be used in the next tick
val currentTickFut = tickFut
tickFut = Some(Future {
val packets = Seq(NetworkPacket.GetPlayerState, NetworkPacket.GetEvents, NetworkPacket.GetWorldLoadingEvents(5))
socket.sendMultiplePacketsAndWait(packets)
})
if currentTickFut.isEmpty then return // the first tick has no server data to act on

try {
val Seq(playerNbt, worldEventsNbtPacket, worldLoadingEventsNbt) =
socket.sendMultiplePacketsAndWait(
Seq(NetworkPacket.GetPlayerState, NetworkPacket.GetEvents, NetworkPacket.GetWorldLoadingEvents(5))
)
Await.result(currentTickFut.get, Duration(1, TimeUnit.SECONDS))

val syncedPlayer = Player.fromNBT(player.id, playerNbt.asInstanceOf[Nbt.MapTag])
// println(syncedPlayer.position)
Expand Down
7 changes: 5 additions & 2 deletions client/src/main/scala/hexacraft/client/WorldRenderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ class WorldRenderer(
}

def tick(camera: Camera, renderDistance: Double, worldTickResult: WorldTickResult): Unit = {
// Step 1: Perform render updates using data calculated in the background since the previous frame
updateBlockData(futureRenderData.map((coords, fut) => (coords, Await.result(fut, Duration.Inf))).toSeq)
// Step 1: Collect render data calculated since the previous frame (used in step 3)
val blockDataToUpdate = futureRenderData.map((coords, fut) => (coords, Await.result(fut, Duration.Inf))).toSeq
futureRenderData.clear()

// Step 2: Start calculating render updates in the background
Expand Down Expand Up @@ -139,6 +139,9 @@ class WorldRenderer(
}
}

// Step 3: Perform render updates using data calculated in the background since the previous frame
updateBlockData(blockDataToUpdate)

// performTerrainUpdates(camera)
}

Expand Down
3 changes: 2 additions & 1 deletion main/src/test/scala/hexacraft/main/GameSceneTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ class GameSceneTest extends FunSuite {
gameScene.tick(tickContext)
Thread.sleep(20)
gameScene.tick(tickContext)
Thread.sleep(2)
Thread.sleep(20)
gameScene.tick(tickContext)

// start listening for audio events
val audioTracker = Tracker.withStorage[AudioSystem.Event]
Expand Down
2 changes: 1 addition & 1 deletion server/src/main/scala/hexacraft/server/GameServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ class GameServer(
}
}
}
println("Received login message from a new client")
// println("Received login message from a new client")
return Some(Nbt.makeMap("success" -> Nbt.ByteTag(true)))
} else {
println("Received login message from a logged in client")
Expand Down
4 changes: 2 additions & 2 deletions server/src/main/scala/hexacraft/server/ServerWorld.scala
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,8 @@ class ServerWorld(worldProvider: WorldProvider, val worldInfo: WorldInfo)
requestedLoads: Seq[ChunkRelWorld],
requestedUnloads: Seq[ChunkRelWorld]
): (Seq[ChunkRelWorld], Seq[ChunkRelWorld]) = {
val chunksToLoadPerTick = 8
val chunksToUnloadPerTick = 12
val chunksToLoadPerTick = if ServerWorld.shouldChillChunkLoader then 1 else 4
val chunksToUnloadPerTick = if ServerWorld.shouldChillChunkLoader then 2 else 6

var unloadsLeft = chunksToUnloadPerTick
for coords <- requestedUnloads do {
Expand Down

0 comments on commit 6a4aed2

Please sign in to comment.