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 self.queue.qualityOfService = .userInitiated
} }
public mutating func generate(chunkID: SIMD3<Int>) { public mutating func cancelAndClearAll() {
if !generatingChunkSet.insert(chunkID).inserted { self.queue.cancelAllOperations()
return 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>) { 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 { fileprivate func locked<X>(_ perform: () -> X) -> X {
self.lock.lock() self.lock.lock()
defer { defer {

View File

@ -32,6 +32,8 @@ class Game: GameDelegate {
} }
private func generateWorld() { private func generateWorld() {
self.world.removeAllChunks()
self.renderChunks.removeAll()
let seed = UInt64(Arc4Random.instance.next()) | UInt64(Arc4Random.instance.next()) << 32 let seed = UInt64(Arc4Random.instance.next()) | UInt64(Arc4Random.instance.next()) << 32
printErr(seed) printErr(seed)
#if DEBUG #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) { func generate(width: Int, height: Int, depth: Int, seed: UInt64) {
self._generator.reset(seed: seed) self._generator.reset(seed: seed)
let orig = SIMD3(width, height, depth) / 2 let orig = SIMD3(width, height, depth) / 2