DEADSOFTWARE

Update README
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / world / GameWorldGenerator.kt
1 package ru.deadsoftware.cavedroid.game.world
3 import com.google.common.primitives.Ints.min
4 import ru.deadsoftware.cavedroid.game.GameItemsHolder
5 import ru.deadsoftware.cavedroid.game.model.block.Block
6 import ru.deadsoftware.cavedroid.game.model.world.Biome
7 import ru.deadsoftware.cavedroid.game.model.world.generator.WorldGeneratorConfig
8 import kotlin.math.abs
9 import kotlin.math.max
10 import kotlin.random.Random
12 class GameWorldGenerator(
13 private val config: WorldGeneratorConfig,
14 private val gameItemsHolder: GameItemsHolder,
15 ) {
17 private val random = Random(config.seed)
19 private val foreMap by lazy { Array(config.width) { Array(config.height) { gameItemsHolder.fallbackBlock } } }
20 private val backMap by lazy { Array(config.width) { Array(config.height) { gameItemsHolder.fallbackBlock } } }
22 private val heights by lazy { generateHeights() }
23 private val biomesMap by lazy { generateBiomes() }
25 private val plainsPlants = listOf("dandelion", "rose", "tallgrass")
26 private val mushrooms = listOf("mushroom_brown", "mushroom_red",)
28 private fun generateHeights(): IntArray {
29 val surfaceHeightRange = config.minSurfaceHeight .. config.maxSurfaceHeight
30 val result = IntArray(config.width)
32 result[0] = surfaceHeightRange.random(random)
34 for (x in 1 ..< config.width) {
35 val previous = result[x - 1]
36 var d = random.nextInt(-5, 6).let { if (it !in -4..4) it / abs(it) else 0 }
38 if (previous + d !in surfaceHeightRange) { d = -d }
40 if (result.lastIndex - x < abs(result[0] - previous) * 3) {
41 d = result[0].compareTo(previous).let { if (it != 0) it / abs(it) else 0 }
42 }
44 result[x] = result[x - 1] + d
45 }
47 return result
48 }
50 private fun generateBiomes(): Map<Int, Biome> {
51 val xSequence = sequence {
52 var lastX = 0
53 var count = 0
55 while (lastX < config.width - config.minBiomeSize - 1) {
56 yield(lastX)
58 lastX = random.nextInt(lastX + config.minBiomeSize, config.width)
59 count++
60 }
61 }
63 return xSequence.associateWith { config.biomes.random(random) }
64 }
66 private fun winterBiome(x: Int) {
67 assert(x in 0 ..< config.width) { "x not in range of world width" }
69 val surfaceHeight = heights[x]
71 val grass = gameItemsHolder.getBlock("grass_snowed")
72 val bedrock = gameItemsHolder.getBlock("bedrock")
73 val dirt = gameItemsHolder.getBlock("dirt")
74 val stone = gameItemsHolder.getBlock("stone")
76 foreMap[x][surfaceHeight] = grass
77 foreMap[x][config.height - 1] = bedrock
78 backMap[x][surfaceHeight] = grass
79 backMap[x][config.height - 1] = bedrock
81 for (y in min(surfaceHeight + 1, config.seaLevel) ..< config.height - 1) {
82 if (y <= surfaceHeight) {
83 backMap[x][y] = dirt
84 continue
85 }
87 foreMap[x][y] = when {
88 y < surfaceHeight + random.nextInt(5, 8) -> dirt
89 else -> stone
90 }
91 backMap[x][y] = foreMap[x][y]
92 }
93 }
95 private fun plainsBiome(x: Int) {
96 assert(x in 0 ..< config.width) { "x not in range of world width" }
98 val surfaceHeight = heights[x]
100 val grass = gameItemsHolder.getBlock("grass")
101 val bedrock = gameItemsHolder.getBlock("bedrock")
102 val dirt = gameItemsHolder.getBlock("dirt")
103 val stone = gameItemsHolder.getBlock("stone")
105 foreMap[x][surfaceHeight] = grass
106 foreMap[x][config.height - 1] = bedrock
107 backMap[x][surfaceHeight] = grass
108 backMap[x][config.height - 1] = bedrock
110 for (y in min(surfaceHeight + 1, config.seaLevel) ..< config.height - 1) {
111 if (y <= surfaceHeight) {
112 backMap[x][y] = dirt
113 continue
116 foreMap[x][y] = when {
117 y < surfaceHeight + random.nextInt(5, 8) -> dirt
118 else -> stone
120 backMap[x][y] = foreMap[x][y]
123 val plant = random.nextInt(100)
124 if (surfaceHeight < config.seaLevel) {
125 if (plant < 10) {
126 generateOak(x)
127 } else if (plant < 40) {
128 generateTallGrass(x)
133 private fun desertBiome(x: Int) {
134 assert(x in 0 ..< config.width) { "x not in range of world width" }
136 val surfaceHeight = heights[x]
138 val sand = gameItemsHolder.getBlock("sand")
139 val bedrock = gameItemsHolder.getBlock("bedrock")
140 val sandstone = gameItemsHolder.getBlock("sandstone")
141 val stone = gameItemsHolder.getBlock("stone")
144 foreMap[x][surfaceHeight] = sand
145 foreMap[x][config.height - 1] = bedrock
146 backMap[x][surfaceHeight] = sand
147 backMap[x][config.height - 1] = bedrock
149 for (y in min(surfaceHeight + 1, config.seaLevel) ..< config.height - 1) {
150 if (y <= surfaceHeight) {
151 backMap[x][y] = sand
152 continue
155 foreMap[x][y] = when {
156 y < surfaceHeight + random.nextInt(5, 8) -> sand
157 y < surfaceHeight + random.nextInt(0, 2) -> sandstone
158 else -> stone
160 backMap[x][y] = foreMap[x][y]
163 val plant = random.nextInt(100)
164 if (surfaceHeight < config.seaLevel) {
165 if (plant < 5) {
166 generateCactus(x)
167 } else if (plant < 10) {
168 generateDeadBush(x)
173 private fun fillWater() {
174 val water = gameItemsHolder.getBlock("water")
176 for (x in 0 ..< config.width) {
177 for (y in config.seaLevel ..< config.height) {
178 if (foreMap[x][y] != gameItemsHolder.fallbackBlock) {
179 break
182 foreMap[x][y] = water
187 private fun generateCactus(x: Int) {
188 val cactus = gameItemsHolder.getBlock("cactus")
189 val cactusHeight = random.nextInt(3)
190 val h = heights[x] - 1
192 for (y in h downTo max(0, h - cactusHeight)) {
193 foreMap[x][y] = cactus
197 private fun generateOak(x: Int) {
198 val log = gameItemsHolder.getBlock("log_oak")
199 val leaves = gameItemsHolder.getBlock("leaves_oak")
200 val h = heights[x] - 1
201 val treeH = random.nextInt(5, 7)
202 val height = max(0, h - treeH)
204 val top = height - 1
205 if (top >= 0) {
206 foreMap[x][top] = leaves
207 backMap[x][top] = leaves
210 for (x1 in max(0, x - 1) .. min(config.width - 1, x + 1)) {
211 for (y in height .. height + treeH - 4) {
212 foreMap[x1][y] = leaves
213 backMap[x1][y] = leaves
215 if (random.nextInt(15) < 3) {
216 foreMap[x1][heights[x1] - 1] = gameItemsHolder.getBlock(mushrooms.random(random))
220 for (y in h downTo height) {
221 backMap[x][y] = log
225 private fun generateTallGrass(x: Int) {
226 val tallGrass = gameItemsHolder.getBlock(plainsPlants.random(random))
227 val h = heights[x] - 1
228 if (h > 0) {
229 foreMap[x][h] = tallGrass
233 private fun generateDeadBush(x: Int) {
234 val bush = gameItemsHolder.getBlock("deadbush")
235 val h = heights[x] - 1
236 if (h > 0) {
237 foreMap[x][h] = bush
241 private fun generateOres(x : Int) {
242 val stone = gameItemsHolder.getBlock("stone")
243 val coal = gameItemsHolder.getBlock("coal_ore")
244 val iron = gameItemsHolder.getBlock("iron_ore")
245 val gold = gameItemsHolder.getBlock("gold_ore")
246 val diamond = gameItemsHolder.getBlock("diamond_ore")
247 val lapis = gameItemsHolder.getBlock("lapis_ore")
249 for (y in heights[x] ..< config.height) {
250 val res = random.nextInt(10000)
252 val h = config.height - y
253 val block = when {
254 res in 0..<25 && h < 16 -> diamond
255 res in 25 ..< 50 && h < 32 -> gold
256 res in 50 ..< 250 && h < 64 -> iron
257 res in 250 ..< 450 && h < 128 -> coal
258 res in 450 ..< (450 + (25 - (abs(h - 16) * (25 / 16)))) -> lapis
259 else -> null
262 if (block != null && foreMap[x][y] == stone) {
263 foreMap[x][y] = block
268 /**
269 * Generate world
270 */
271 fun generate(): Pair<Array<Array<Block>>, Array<Array<Block>>> {
272 var biome = Biome.PLAINS
274 for (x in 0 until config.width) {
275 biome = biomesMap[x] ?: biome
277 when (biome) {
278 Biome.PLAINS -> plainsBiome(x)
279 Biome.DESERT -> desertBiome(x)
280 Biome.WINTER -> winterBiome(x)
283 generateOres(x)
286 fillWater()
288 return Pair(foreMap, backMap)