From: TheSaminator Date: Mon, 6 Jun 2022 17:37:27 +0000 (-0400) Subject: Refactor weighted random X-Git-Url: https://gitweb.starshipfights.net/?a=commitdiff_plain;h=67cb88252b3161a08555080032dae9bce3f37228;p=starship-fights Refactor weighted random --- diff --git a/plan/campaign/map.svg b/plan/campaign/map.svg new file mode 100644 index 0000000..36fb7aa --- /dev/null +++ b/plan/campaign/map.svg @@ -0,0 +1,466 @@ + + + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/commonMain/kotlin/starshipfights/game/ai/util.kt b/src/commonMain/kotlin/starshipfights/game/ai/util.kt index 92bb8a8..ddd1425 100644 --- a/src/commonMain/kotlin/starshipfights/game/ai/util.kt +++ b/src/commonMain/kotlin/starshipfights/game/ai/util.kt @@ -24,10 +24,9 @@ fun Map.weightedRandom(random: Random = Random): T { } fun Map.weightedRandomOrNull(random: Random = Random): T? { - if (isEmpty()) return null + if (values.none { it >= EPSILON }) return null val total = values.sum() - if (total < EPSILON) return null var hasChoice = false var choice = random.nextDouble(total)