From beeb8aba065704711b41828bbe79c518adfdeda0 Mon Sep 17 00:00:00 2001 From: otta8634 Date: Wed, 12 Feb 2025 17:51:03 +0800 Subject: [PATCH] Optimize random nade selection code Use RandomSelection rather than looping through the Nades registry twice. --- .../common/mutators/mutator/nades/sv_nades.qc | 23 ++++--------------- 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/qcsrc/common/mutators/mutator/nades/sv_nades.qc b/qcsrc/common/mutators/mutator/nades/sv_nades.qc index 34786c9b6..0ca068264 100644 --- a/qcsrc/common/mutators/mutator/nades/sv_nades.qc +++ b/qcsrc/common/mutators/mutator/nades/sv_nades.qc @@ -490,25 +490,12 @@ bool nade_customize(entity this, entity client) int nade_choose_random() { - // loops: first determine how many allowed options there are (A), then randomly select one of them (B) - int total_allowed = 0; - FOREACH(Nades, it != NADE_TYPE_Null, { // (A) - if (nades_CheckTypes(it).m_id == it.m_id) // this nade type is allowed - ++total_allowed; + RandomSelection_Init(); + FOREACH(Nades, it != NADE_TYPE_Null, { + if (nades_CheckTypes(it) == it) // this nade type is allowed + RandomSelection_AddEnt(it, 1, 1); }); - int choose_nth = floor(random() * total_allowed); - int n_i = 0; - int ntype = 0; - FOREACH(Nades, true, { // (B) - if (it != NADE_TYPE_Null && nades_CheckTypes(it).m_id == it.m_id) - { - if (n_i == choose_nth) - break; - ++n_i; - } - ++ntype; - }); - return ntype; + return RandomSelection_chosen_ent.m_id; } Nade Nades_FromString(string ntype) -- 2.39.5