X-Git-Url: https://de.git.xonotic.org/?a=blobdiff_plain;f=qcsrc%2Fserver%2F_all.qh;h=1fabe4d0f965aa9abd6b564b180ad4e83637a96d;hb=7908f6be3a5bcdb29c7fc4ca663b919ed023b1f3;hp=a32f8b5832cf01e9ecccc534be2d794d6672118f;hpb=3bdee6303ce762a39c2ad67a70745668ba298043;p=xonotic%2Fxonotic-data.pk3dir.git diff --git a/qcsrc/server/_all.qh b/qcsrc/server/_all.qh index a32f8b583..1fabe4d0f 100644 --- a/qcsrc/server/_all.qh +++ b/qcsrc/server/_all.qh @@ -43,7 +43,38 @@ const string STR_OBSERVER = "observer"; #define FOREACH_CLIENT(cond, body) FOREACH_CLIENTSLOT(IS_CLIENT(it) && (cond), body) -// NOTE: FOR_EACH_MONSTER deprecated! Use the following instead: FOREACH_ENTITY_FLAGS(flags, FL_MONSTER, { code; }); +// using the "inside out" version of knuth-fisher-yates shuffle +// https://en.wikipedia.org/wiki/Fisher–Yates_shuffle +entity _FCR_clients[255]; +bool _FCR_entered = false; +#define FOREACH_CLIENT_RANDOM(cond, body) \ + MACRO_BEGIN { \ + 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) \ + { \ + _FCR_clients[_cnt] = it; \ + } \ + else \ + { \ + _FCR_clients[_cnt] = _FCR_clients[_j]; \ + _FCR_clients[_j] = it; \ + } \ + _cnt++; \ + )); \ + for (int _i = 0; _i < _cnt; ++_i) \ + { \ + const noref int i = _i; \ + ITER_CONST noref entity it = _FCR_clients[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; }); #include #include