From 67cb88252b3161a08555080032dae9bce3f37228 Mon Sep 17 00:00:00 2001 From: TheSaminator Date: Mon, 6 Jun 2022 13:37:27 -0400 Subject: [PATCH] Refactor weighted random --- plan/campaign/map.svg | 466 ++++++++++++++++++ .../kotlin/starshipfights/game/ai/util.kt | 3 +- 2 files changed, 467 insertions(+), 2 deletions(-) create mode 100644 plan/campaign/map.svg 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) -- 2.25.1