world regen clears all chunks

This commit is contained in:
a dinosaur 2024-09-07 04:05:50 +10:00
parent c80e456d3e
commit ee54e011a1
4 changed files with 23 additions and 5 deletions

View File

@ -14,12 +14,17 @@ public struct ChunkGeneration {
self.queue.qualityOfService = .userInitiated
}
public mutating func generate(chunkID: SIMD3<Int>) {
if !generatingChunkSet.insert(chunkID).inserted {
return
}
public mutating func cancelAndClearAll() {
self.queue.cancelAllOperations()
self.queue.waitUntilAllOperationsAreFinished()
self.localReadyChunks.removeAll()
self.generatingChunkSet.removeAll()
}
self.queueGenerateJob(chunkID: chunkID)
public mutating func generate(chunkID: SIMD3<Int>) {
if generatingChunkSet.insert(chunkID).inserted {
self.queueGenerateJob(chunkID: chunkID)
}
}
func queueGenerateJob(chunkID: SIMD3<Int>) {

View File

@ -70,6 +70,12 @@ public class ConcurrentDictionary<V: Hashable, T>: Collection {
}
}
public func removeAll(keepingCapacity keep: Bool = false) {
self.locked {
self.inner.removeAll(keepingCapacity: keep)
}
}
fileprivate func locked<X>(_ perform: () -> X) -> X {
self.lock.lock()
defer {

View File

@ -32,6 +32,8 @@ class Game: GameDelegate {
}
private func generateWorld() {
self.world.removeAllChunks()
self.renderChunks.removeAll()
let seed = UInt64(Arc4Random.instance.next()) | UInt64(Arc4Random.instance.next()) << 32
printErr(seed)
#if DEBUG

View File

@ -68,6 +68,11 @@ public class World {
}
}
func removeAllChunks() {
self._chunkGeneration.cancelAndClearAll()
self._chunks.removeAll()
}
func generate(width: Int, height: Int, depth: Int, seed: UInt64) {
self._generator.reset(seed: seed)
let orig = SIMD3(width, height, depth) / 2