-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from halotukozak/todos
Todos
- Loading branch information
Showing
27 changed files
with
679 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,31 @@ | ||
package backend.map | ||
|
||
import backend.config.Config | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.flow.* | ||
import kotlinx.coroutines.flow.update | ||
import shared.takeRandom | ||
import kotlin.math.roundToInt | ||
|
||
|
||
class JungleMap(config: Config) : AbstractMap(config) { | ||
@OptIn(ExperimentalCoroutinesApi::class) | ||
|
||
override suspend fun growPlants(plantsCount: Int) { | ||
_plants.update { plants -> | ||
val preferredPositions = plants | ||
.asFlow() | ||
.flatMapMerge { it.surroundingPositions } | ||
.filter { it.inMap() } | ||
.toSet() | ||
|
||
val otherPositions = (fields - preferredPositions) | ||
val preferredPositions = _preferredFields.value | ||
val otherPositions = fields - preferredPositions - plants | ||
|
||
val plantsOnPreferredPositions = minOf(preferredPositions.size, (plantsCount * 0.8).roundToInt()) | ||
val plantsOnOtherPositions = minOf(otherPositions.size, plantsCount - plantsOnPreferredPositions) | ||
|
||
(plants + seedRandomly(preferredPositions.toList(), plantsOnPreferredPositions) + seedRandomly( | ||
otherPositions, | ||
plantsOnOtherPositions, | ||
)).also { fields -> | ||
(plants + | ||
preferredPositions.toList().takeRandom(plantsOnPreferredPositions, random) + | ||
otherPositions.takeRandom(plantsOnOtherPositions, random)).also { fields -> | ||
_preferredFields.update { | ||
fields | ||
.asFlow() | ||
.flatMapMerge { it.surroundingPositions } | ||
.filter { it.inMap() } | ||
.flatMap { it.surroundingPositions } | ||
.filter { it.x in 0..<config.mapWidth && it.y in 0..<config.mapHeight } | ||
.toSet() - fields | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun Vector.inMap() = x in 0..<config.mapWidth && y in 0..<config.mapHeight | ||
|
||
private val Vector.surroundingPositions | ||
get() = flowOf( | ||
Vector(x - 1, y - 1), | ||
Vector(x, y - 1), | ||
Vector(x + 1, y - 1), | ||
Vector(x - 1, y), | ||
Vector(x + 1, y), | ||
Vector(x - 1, y + 1), | ||
Vector(x, y + 1), | ||
Vector(x + 1, y + 1) | ||
) | ||
|
||
|
||
private fun seedRandomly(emptyFields: List<Vector>, numberOfSeeds: Int) = | ||
generateSequence { emptyFields[random.nextInt(emptyFields.size)] } | ||
.distinct() | ||
.take(numberOfSeeds) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.