From 9af7eec1425c0bfef69bee1209997cd61d8ac2e4 Mon Sep 17 00:00:00 2001 From: Halonexus Date: Thu, 15 Feb 2024 02:05:04 +0300 Subject: [PATCH] Abno queue null entry fix (#1966) new fixed picked weight proc --- code/__HELPERS/_lists.dm | 17 +++++++++++++++++ code/controllers/subsystem/abnormality_queue.dm | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/code/__HELPERS/_lists.dm b/code/__HELPERS/_lists.dm index b25a1b158847..9ea57bd3c839 100644 --- a/code/__HELPERS/_lists.dm +++ b/code/__HELPERS/_lists.dm @@ -316,6 +316,23 @@ return null +/proc/PickWeightRealNumber(list/L) + var/total = 0.0 + var/item + for (item in L) + if (!L[item]) + L[item] = 1.0 + total += L[item] + var/generator/G = generator("num", 0.0, total, UNIFORM_RAND) + total = G.Rand() + for (item in L) + total -= L[item] + if (total <= 0.000001) + return item + if(LAZYLEN(L)) + log_runtime("PickWeightRealNumber failed with leftover total = [total]") + return null + /proc/pickweightAllowZero(list/L) //The original pickweight proc will sometimes pick entries with zero weight. I'm not sure if changing the original will break anything, so I left it be. var/total = 0 var/item diff --git a/code/controllers/subsystem/abnormality_queue.dm b/code/controllers/subsystem/abnormality_queue.dm index ce48bee8b2c9..c3c8fae27148 100644 --- a/code/controllers/subsystem/abnormality_queue.dm +++ b/code/controllers/subsystem/abnormality_queue.dm @@ -141,7 +141,7 @@ SUBSYSTEM_DEF(abnormality_queue) picked_levs |= lev if(!LAZYLEN(possible_abnormalities[lev] - picking_abnormalities)) continue - var/chosen_abno = pickweight(possible_abnormalities[lev] - picking_abnormalities) + var/chosen_abno = PickWeightRealNumber(possible_abnormalities[lev] - picking_abnormalities) picking_abnormalities += chosen_abno if(!LAZYLEN(picking_abnormalities)) return FALSE