mirror of
				https://github.com/GayPizzaSpecifications/voxelotl-engine.git
				synced 2025-11-04 02:59:37 +00:00 
			
		
		
		
	split worldgen
This commit is contained in:
		@ -51,6 +51,7 @@ add_executable(Voxelotl MACOSX_BUNDLE
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  # Game logic classes
 | 
					  # Game logic classes
 | 
				
			||||||
  Chunk.swift
 | 
					  Chunk.swift
 | 
				
			||||||
 | 
					  WorldGenerator.swift
 | 
				
			||||||
  World.swift
 | 
					  World.swift
 | 
				
			||||||
  Raycast.swift
 | 
					  Raycast.swift
 | 
				
			||||||
  Player.swift
 | 
					  Player.swift
 | 
				
			||||||
 | 
				
			|||||||
@ -40,20 +40,12 @@ class Game: GameDelegate {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  private func generateWorld() {
 | 
					  private func generateWorld() {
 | 
				
			||||||
    var random: any RandomProvider
 | 
					    let seed = UInt64(Arc4Random.instance.next()) | UInt64(Arc4Random.instance.next()) << 32
 | 
				
			||||||
#if true
 | 
					    printErr(seed)
 | 
				
			||||||
    let newSeed = UInt64(Arc4Random.instance.next()) | UInt64(Arc4Random.instance.next()) << 32
 | 
					 | 
				
			||||||
    printErr(newSeed)
 | 
					 | 
				
			||||||
    random = Xoroshiro128PlusPlus(seed: newSeed)
 | 
					 | 
				
			||||||
#else
 | 
					 | 
				
			||||||
    random = PCG32Random(state: (
 | 
					 | 
				
			||||||
      UInt64(Arc4Random.instance.next()) | UInt64(Arc4Random.instance.next()) << 32,
 | 
					 | 
				
			||||||
      UInt64(Arc4Random.instance.next()) | UInt64(Arc4Random.instance.next()) << 32))
 | 
					 | 
				
			||||||
#endif
 | 
					 | 
				
			||||||
#if DEBUG
 | 
					#if DEBUG
 | 
				
			||||||
    self.world.generate(width: 2, height: 1, depth: 1, random: &random)
 | 
					    self.world.generate(width: 2, height: 1, depth: 1, seed: seed)
 | 
				
			||||||
#else
 | 
					#else
 | 
				
			||||||
    self.world.generate(width: 5, height: 3, depth: 5, random: &random)
 | 
					    self.world.generate(width: 5, height: 3, depth: 5, seed: seed)
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -2,11 +2,11 @@ import Foundation
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
public class World {
 | 
					public class World {
 | 
				
			||||||
  private var _chunks: Dictionary<SIMD3<Int>, Chunk>
 | 
					  private var _chunks: Dictionary<SIMD3<Int>, Chunk>
 | 
				
			||||||
  private var noise: ImprovedPerlin<Float>!
 | 
					  private var _generator: WorldGenerator
 | 
				
			||||||
  private var noise2: SimplexNoise<Float>!
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  public init() {
 | 
					  public init() {
 | 
				
			||||||
    self._chunks = [:]
 | 
					    self._chunks = [:]
 | 
				
			||||||
 | 
					    self._generator = WorldGenerator()
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  func getBlock(at position: SIMD3<Int>) -> Block {
 | 
					  func getBlock(at position: SIMD3<Int>) -> Block {
 | 
				
			||||||
@ -19,40 +19,21 @@ public class World {
 | 
				
			|||||||
    self._chunks[position &>> Chunk.shift]?.setBlock(at: position, type: type)
 | 
					    self._chunks[position &>> Chunk.shift]?.setBlock(at: position, type: type)
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  func generate(width: Int, height: Int, depth: Int, random: inout any RandomProvider) {
 | 
					  func generate(width: Int, height: Int, depth: Int, seed: UInt64) {
 | 
				
			||||||
    self.noise = ImprovedPerlin<Float>(random: &random)
 | 
					    self._generator.reset(seed: seed)
 | 
				
			||||||
    self.noise2 = SimplexNoise<Float>(random: &random)
 | 
					    let orig = SIMD3(width, height, depth) / 2
 | 
				
			||||||
 | 
					    for z in 0..<depth {
 | 
				
			||||||
    for x in 0..<width {
 | 
					 | 
				
			||||||
      for y in 0..<height {
 | 
					      for y in 0..<height {
 | 
				
			||||||
        for z in 0..<depth {
 | 
					        for x in 0..<width {
 | 
				
			||||||
          let chunkID = SIMD3(x, y, z) &- SIMD3(width, height, depth) / 2
 | 
					          let chunkID = SIMD3(x, y, z) &- orig
 | 
				
			||||||
          self.generate(chunkID: chunkID)
 | 
					          self._chunks[chunkID] = self._generator.makeChunk(id: chunkID)
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  func generate(chunkID: SIMD3<Int>) {
 | 
					  func generate(chunkID: SIMD3<Int>) {
 | 
				
			||||||
    let chunkOrigin = chunkID &<< Chunk.shift
 | 
					    self._chunks[chunkID] = self._generator.makeChunk(id: chunkID)
 | 
				
			||||||
    var chunk = Chunk(position: chunkOrigin)
 | 
					 | 
				
			||||||
    chunk.fill(allBy: { position in
 | 
					 | 
				
			||||||
      let fpos = SIMD3<Float>(position)
 | 
					 | 
				
			||||||
        let threshold: Float = 0.6
 | 
					 | 
				
			||||||
        let value = fpos.y / Float(Chunk.size)
 | 
					 | 
				
			||||||
          + self.noise.get(fpos * 0.05) * 1.1
 | 
					 | 
				
			||||||
          + self.noise.get(fpos * 0.10) * 0.5
 | 
					 | 
				
			||||||
          + self.noise.get(fpos * 0.30) * 0.23
 | 
					 | 
				
			||||||
      return if value < threshold {
 | 
					 | 
				
			||||||
        .solid(.init(
 | 
					 | 
				
			||||||
          hue:        Float16(180 + self.noise2.get(fpos * 0.05) * 180),
 | 
					 | 
				
			||||||
          saturation: Float16(0.5 + self.noise2.get(SIMD4(fpos * 0.05, 4)) * 0.5),
 | 
					 | 
				
			||||||
          value:      Float16(0.5 + self.noise2.get(SIMD4(fpos * 0.05, 9)) * 0.5).lerp(0.5, 1)).linear)
 | 
					 | 
				
			||||||
      } else {
 | 
					 | 
				
			||||||
        .air
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
    })
 | 
					 | 
				
			||||||
    self._chunks[chunkID] = chunk
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  var instances: [Instance] {
 | 
					  var instances: [Instance] {
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										40
									
								
								Sources/Voxelotl/WorldGenerator.swift
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								Sources/Voxelotl/WorldGenerator.swift
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,40 @@
 | 
				
			|||||||
 | 
					struct WorldGenerator {
 | 
				
			||||||
 | 
					  var noise: ImprovedPerlin<Float>!, noise2: SimplexNoise<Float>!
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public mutating func reset(seed: UInt64) {
 | 
				
			||||||
 | 
					    var random: any RandomProvider
 | 
				
			||||||
 | 
					#if true
 | 
				
			||||||
 | 
					    random = Xoroshiro128PlusPlus(seed: seed)
 | 
				
			||||||
 | 
					#else
 | 
				
			||||||
 | 
					    //TODO: Fill seed with a hash
 | 
				
			||||||
 | 
					    random = PCG32Random(state: (
 | 
				
			||||||
 | 
					      UInt64(Arc4Random.instance.next()) | UInt64(Arc4Random.instance.next()) << 32,
 | 
				
			||||||
 | 
					      UInt64(Arc4Random.instance.next()) | UInt64(Arc4Random.instance.next()) << 32))
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    self.noise = ImprovedPerlin<Float>(random: &random)
 | 
				
			||||||
 | 
					    self.noise2 = SimplexNoise<Float>(random: &random)
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public func makeChunk(id chunkID: SIMD3<Int>) -> Chunk {
 | 
				
			||||||
 | 
					    let chunkOrigin = chunkID &<< Chunk.shift
 | 
				
			||||||
 | 
					    var chunk = Chunk(position: chunkOrigin)
 | 
				
			||||||
 | 
					    chunk.fill(allBy: { position in
 | 
				
			||||||
 | 
					      let fpos = SIMD3<Float>(position)
 | 
				
			||||||
 | 
					        let threshold: Float = 0.6
 | 
				
			||||||
 | 
					        let value = fpos.y / Float(Chunk.size)
 | 
				
			||||||
 | 
					          + self.noise.get(fpos * 0.05) * 1.1
 | 
				
			||||||
 | 
					          + self.noise.get(fpos * 0.10) * 0.5
 | 
				
			||||||
 | 
					          + self.noise.get(fpos * 0.30) * 0.23
 | 
				
			||||||
 | 
					      return if value < threshold {
 | 
				
			||||||
 | 
					        .solid(.init(
 | 
				
			||||||
 | 
					          hue:        Float16(180 + self.noise2.get(fpos * 0.05) * 180),
 | 
				
			||||||
 | 
					          saturation: Float16(0.5 + self.noise2.get(SIMD4(fpos * 0.05, 4)) * 0.5),
 | 
				
			||||||
 | 
					          value:      Float16(0.5 + self.noise2.get(SIMD4(fpos * 0.05, 9)) * 0.5).lerp(0.5, 1)).linear)
 | 
				
			||||||
 | 
					      } else {
 | 
				
			||||||
 | 
					        .air
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    })
 | 
				
			||||||
 | 
					    return chunk
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user