From 1393aa8e53dcd644bef4bec541d320f2bc268c2b Mon Sep 17 00:00:00 2001 From: Martin Taibr Date: Wed, 2 Nov 2016 23:30:31 +0100 Subject: [PATCH] less bad FOREACH_CLIENT_RANDOM --- qcsrc/server/_all.qh | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/qcsrc/server/_all.qh b/qcsrc/server/_all.qh index 794638dea..369ef199d 100644 --- a/qcsrc/server/_all.qh +++ b/qcsrc/server/_all.qh @@ -45,29 +45,33 @@ const string STR_OBSERVER = "observer"; // using the "inside out" version of knuth-fisher-yates shuffle // https://en.wikipedia.org/wiki/Fisher–Yates_shuffle +float _FCR_clients[255]; +bool _FCR_entered = false; #define FOREACH_CLIENT_RANDOM(cond, body) \ MACRO_BEGIN { \ - float _clients[255]; \ + if (_FCR_entered) LOG_FATAL("FOREACH_CLIENT_RANDOM must not be nested"); \ + _FCR_entered = true; \ int _cnt = 0; \ FOREACH_CLIENT(cond, LAMBDA( \ int _j = floor(random() * (_cnt + 1)); \ if (_j == _cnt) \ { \ - _clients[_cnt] = etof(it); \ + _FCR_clients[_cnt] = etof(it); \ } \ else \ { \ - _clients[_cnt] = _clients[_j]; \ - _clients[_j] = etof(it); \ + _FCR_clients[_cnt] = _FCR_clients[_j]; \ + _FCR_clients[_j] = etof(it); \ } \ _cnt++; \ )); \ for (int _i = 0; _i < _cnt; ++_i) \ { \ - const noref int i = _clients[_i]; \ + const noref int i = _FCR_clients[_i]; \ ITER_CONST noref entity it = ftoe(i); \ if (cond) { LAMBDA(body) } \ } \ + _FCR_entered = false; \ } MACRO_END // NOTE: FOR_EACH_MONSTER deprecated! Use the following instead: IL_EACH(g_monsters, true, { code; }); -- 2.39.2