Refactor star name generation
authorTheSaminator <thesaminator@users.noreply.github.com>
Tue, 5 Jul 2022 18:27:23 +0000 (14:27 -0400)
committerTheSaminator <thesaminator@users.noreply.github.com>
Tue, 5 Jul 2022 19:15:22 +0000 (15:15 -0400)
src/jvmMain/kotlin/net/starshipfights/campaign/cluster_gen.kt
src/jvmMain/kotlin/net/starshipfights/data/space/star_names.kt

index c96f6b46f5c5aca96ef2575cced9c9a9a1e71458..8e547bc02be70acc0a5899d3e7bf0481963697c8 100644 (file)
@@ -4,7 +4,7 @@ import kotlinx.coroutines.*
 import kotlinx.coroutines.flow.*
 import net.starshipfights.data.Id
 import net.starshipfights.data.invoke
-import net.starshipfights.data.space.randomStarName
+import net.starshipfights.data.space.newStarName
 import net.starshipfights.data.space.toRomanNumerals
 import net.starshipfights.game.*
 import net.starshipfights.game.ai.mean
@@ -216,8 +216,7 @@ class ClusterGenerator(val settings: ClusterGenerationSettings) {
                val usedNames = mutableSetOf<String>()
                
                while (currentCoroutineContext().isActive) {
-                       val name = generateSequence { randomStarName() }.dropWhile { it in usedNames }.first()
-                       usedNames += name
+                       val name = newStarName(usedNames) ?: break
                        
                        val unnamedCelestialObjects = createCelestialObjects()
                                .takeWhile { it.position.vector.magnitude + it.size + SYSTEM_MARGIN < MAX_SYSTEM_SIZE }
index e7d207b8e9b02134c88e0041f081f21600fe9ab7..cb186de046a76e6d23253266b39c6c6f949008ea 100644 (file)
@@ -5,6 +5,10 @@ import net.starshipfights.data.admiralty.LatinNoun
 import net.starshipfights.data.admiralty.LatinNounForm
 import net.starshipfights.data.admiralty.describedBy
 
+fun newStarName(existingNames: MutableSet<String>) = generateSequence {
+       randomStarName()
+}.take(20).dropWhile { it in existingNames }.firstOrNull()?.also { existingNames.add(it) }
+
 fun Int.toRomanNumerals(): String {
        require(this in 1..3999) { "Roman numerals must be in the range [1, 4000)!" }