]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/_all.qh
iterate through clients in random order
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / _all.qh
1 #pragma once
2
3 int maxclients;
4
5 const string STR_PLAYER = "player";
6 const string STR_SPECTATOR = "spectator";
7 const string STR_OBSERVER = "observer";
8
9 #define IS_PLAYER(v) ((v).classname == STR_PLAYER)
10 #define IS_SPEC(v) ((v).classname == STR_SPECTATOR)
11 #define IS_OBSERVER(v) ((v).classname == STR_OBSERVER)
12
13 #define IS_CLIENT(v) (v.flags & FL_CLIENT)
14 /** want: (IS_CLIENT(v) && !IS_REAL_CLIENT(v)) */
15 #define IS_BOT_CLIENT(v) (clienttype(v) == CLIENTTYPE_BOT)
16 #define IS_FAKE_CLIENT(v) (clienttype(v) == CLIENTTYPE_NOTACLIENT)
17 #define IS_REAL_CLIENT(v) (clienttype(v) == CLIENTTYPE_REAL)
18 /** was: (clienttype(v) == CLIENTTYPE_NOTACLIENT) */
19 #define IS_NOT_A_CLIENT(v) (!IS_CLIENT(v))
20
21 #define IS_MONSTER(v) (v.flags & FL_MONSTER)
22 #define IS_VEHICLE(v) (v.vehicle_flags & VHF_ISVEHICLE)
23 #define IS_TURRET(v) (v.turret_flags & TUR_FLAG_ISTURRET)
24
25 // NOTE: FOR_EACH_CLIENTSLOT deprecated! Use the following instead: FOREACH_CLIENTSLOT(true, { code; });
26 // NOTE: FOR_EACH_CLIENT deprecated! Use the following instead: FOREACH_CLIENT(true, { code; });
27 // NOTE: FOR_EACH_REALCLIENT deprecated! Use the following instead: FOREACH_CLIENT(IS_REAL_CLIENT(it), { code; });
28
29 // NOTE: FOR_EACH_PLAYER deprecated! Use the following instead: FOREACH_CLIENT(IS_PLAYER(it), { code; });
30 // NOTE: FOR_EACH_SPEC deprecated! Use the following instead: FOREACH_CLIENT(IS_SPEC(it), { code; });
31 // NOTE: FOR_EACH_OBSERVER deprecated! Use the following instead: FOREACH_CLIENT(IS_OBSERVER(it), { code; });
32 // NOTE: FOR_EACH_REALPLAYER deprecated! Use the following instead: FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it), { code; });
33
34 #define FOREACH_CLIENTSLOT(cond, body) \
35         MACRO_BEGIN { \
36                 for(int _i = 1; _i <= maxclients; ++_i) \
37                 { \
38                         const noref int i = _i; \
39                         ITER_CONST noref entity it = ftoe(i); \
40                         if(cond) { LAMBDA(body) } \
41                 } \
42         } MACRO_END
43
44 #define FOREACH_CLIENT(cond, body) FOREACH_CLIENTSLOT(IS_CLIENT(it) && (cond), body)
45
46 // using the "inside out" version of knuth-fisher-yates shuffle
47 // https://en.wikipedia.org/wiki/Fisher–Yates_shuffle
48 #define FOREACH_CLIENT_RANDOM(cond, body) \
49         MACRO_BEGIN { \
50                 float _clients[255]; \
51                 int _cnt = 0; \
52                 FOREACH_CLIENT(cond, LAMBDA( \
53                         int _j = floor(random() * (_cnt + 1)); \
54                         if (_j == _cnt) \
55                         { \
56                                 _clients[_cnt] = etof(it); \
57                         } \
58                         else \
59                         { \
60                                 _clients[_cnt] = _clients[_j]; \
61                                 _clients[_j] = etof(it); \
62                         } \
63                         _cnt++; \
64                 )); \
65                 for (int _i = 0; _i < _cnt; ++_i) \
66                 { \
67                         const noref int i = _clients[_i]; \
68                         ITER_CONST noref entity it = ftoe(i); \
69                         if (cond) { LAMBDA(body) } \
70                 } \
71         } MACRO_END
72
73 // NOTE: FOR_EACH_MONSTER deprecated! Use the following instead: IL_EACH(g_monsters, true, { code; });
74
75 #include <common/effects/all.qh>
76 #include <common/models/all.qh>
77 #include <common/sounds/all.qh>
78
79 #include "autocvars.qh"
80 #include "constants.qh"
81 #include "defs.qh"
82 #include "miscfunctions.qh"