Xonotic
stats.qh
Go to the documentation of this file.
1 #pragma once
2 
3 // TODO: rename to 'netvars'
4 
5 #include "registry.qh"
6 #include "sort.qh"
7 
8 .int m_id;
10 
11 const int STATS_ENGINE_RESERVE = 32;
12 // must be listed in ascending order
13 #define MAGIC_STATS(_, x) \
14  _(x, MOVEVARS_AIRACCEL_QW_STRETCHFACTOR, 220) \
15  _(x, MOVEVARS_AIRCONTROL_PENALTY, 221) \
16  _(x, MOVEVARS_AIRSPEEDLIMIT_NONQW, 222) \
17  _(x, MOVEVARS_AIRSTRAFEACCEL_QW, 223) \
18  _(x, MOVEVARS_AIRCONTROL_POWER, 224) \
19  _(x, MOVEFLAGS, 225) \
20  _(x, MOVEVARS_WARSOWBUNNY_AIRFORWARDACCEL, 226) \
21  _(x, MOVEVARS_WARSOWBUNNY_ACCEL, 227) \
22  _(x, MOVEVARS_WARSOWBUNNY_TOPSPEED, 228) \
23  _(x, MOVEVARS_WARSOWBUNNY_TURNACCEL, 229) \
24  _(x, MOVEVARS_WARSOWBUNNY_BACKTOSIDERATIO, 230) \
25  _(x, MOVEVARS_AIRSTOPACCELERATE, 231) \
26  _(x, MOVEVARS_AIRSTRAFEACCELERATE, 232) \
27  _(x, MOVEVARS_MAXAIRSTRAFESPEED, 233) \
28  _(x, MOVEVARS_AIRCONTROL, 234) \
29  _(x, FRAGLIMIT, 235) \
30  _(x, TIMELIMIT, 236) \
31  _(x, MOVEVARS_WALLFRICTION, 237) \
32  _(x, MOVEVARS_FRICTION, 238) \
33  _(x, MOVEVARS_WATERFRICTION, 239) \
34  _(x, MOVEVARS_TICRATE, 240) \
35  _(x, MOVEVARS_TIMESCALE, 241) \
36  _(x, MOVEVARS_GRAVITY, 242) \
37  _(x, MOVEVARS_STOPSPEED, 243) \
38  _(x, MOVEVARS_MAXSPEED, 244) \
39  _(x, MOVEVARS_SPECTATORMAXSPEED, 245) \
40  _(x, MOVEVARS_ACCELERATE, 246) \
41  _(x, MOVEVARS_AIRACCELERATE, 247) \
42  _(x, MOVEVARS_WATERACCELERATE, 248) \
43  _(x, MOVEVARS_ENTGRAVITY, 249) \
44  _(x, MOVEVARS_JUMPVELOCITY, 250) \
45  _(x, MOVEVARS_EDGEFRICTION, 251) \
46  _(x, MOVEVARS_MAXAIRSPEED, 252) \
47  _(x, MOVEVARS_STEPHEIGHT, 253) \
48  _(x, MOVEVARS_AIRACCEL_QW, 254) \
49  _(x, MOVEVARS_AIRACCEL_SIDEWAYS_FRICTION, 255) \
50 
51 
53 
54 #define MAGIC_STATS_FIX_MANUAL(it, var, id) \
55  if (it.registered_id == "STAT_" #var) { --g_magic_stats_hole; it.m_id = id; } else
56 
57 #define MAGIC_STATS_FIX_AUTO(it, var, id) \
58  if (it.m_id == id) { ++g_magic_stats_hole; ++it.m_id; }
59 
60 #define MAGIC_STATS_FIX(it) \
61  it.m_id += g_magic_stats_hole; \
62  MAGIC_STATS(MAGIC_STATS_FIX_MANUAL, it) { MAGIC_STATS(MAGIC_STATS_FIX_AUTO, it) }
63 
64 #define REGISTER_STAT(...) EVAL_REGISTER_STAT(OVERLOAD(REGISTER_STAT, __VA_ARGS__))
65 #define EVAL_REGISTER_STAT(...) __VA_ARGS__
66 #if defined(CSQC)
67 
68  void stats_get() {}
69  #define STAT(...) EVAL_STAT(OVERLOAD(STAT, __VA_ARGS__))
70  #define EVAL_STAT(...) __VA_ARGS__
71  #define STAT_1(id) (RVALUE, _STAT(id))
72  #define STAT_2(id, cl) STAT_1(id)
73 
74  #define getstat_int(id) getstati(id, 0, 24)
75  #define getstat_bool(id) boolean(getstati(id))
76  #define getstat_float(id) getstatf(id)
77  #define getstat_vector(id) vec3(getstat_float(id + 0), getstat_float(id + 1), getstat_float(id + 2))
78  #define getstat_vectori(id) vec3(getstat_int(id + 0), getstat_int(id + 1), getstat_int(id + 2))
79 
80  #define _STAT(id) g_stat_##id
81  #define REGISTER_STAT_2(id, T) \
82  T _STAT(id); \
83  /* T CAT(_STAT(id), _prev); */ \
84  REGISTER(Stats, STAT_##id, m_id, new_pure(stat)) \
85  { \
86  if (#T == "vector" || #T == "vectori") { \
87  REGISTRY_RESERVE(Stats, m_id, STAT_##id, y); \
88  REGISTRY_RESERVE(Stats, m_id, STAT_##id, z); \
89  } \
90  } \
91  ACCUMULATE void stats_get() \
92  { \
93  T it = getstat_##T(STAT_##id.m_id); \
94  /* if (it != CAT(_STAT(id), _prev)) \
95  CAT(_STAT(id), _prev) = */ _STAT(id) = it; \
96  }
97  #define REGISTER_STAT_3(x, T, expr) REGISTER_STAT_2(x, T)
98 #elif defined(SVQC)
99 
100  entity STATS;
102  void stats_add() {}
103  #define STAT(...) EVAL_STAT(OVERLOAD_(STAT, __VA_ARGS__))
104  #define EVAL_STAT(...) __VA_ARGS__
105  #define STAT_1(id) (RVALUE, STAT_2(id, STATS))
106  #define STAT_2(id, cl) (cl)._STAT(id)
107 
108  #define addstat_int(id, fld) addstat(id, AS_INT, fld)
109  #define addstat_bool(id, fld) addstat(id, AS_INT, fld)
110  #define addstat_float(id, fld) addstat(id, AS_FLOAT, fld)
111  #define addstat_vector(id, fld) MACRO_BEGIN \
112  addstat_float(id + 0, fld##_x); \
113  addstat_float(id + 1, fld##_y); \
114  addstat_float(id + 2, fld##_z); \
115  MACRO_END
116  #define addstat_vectori(id, fld) MACRO_BEGIN \
117  addstat_int(id + 0, fld##_x); \
118  addstat_int(id + 1, fld##_y); \
119  addstat_int(id + 2, fld##_z); \
120  MACRO_END
121  const int AS_STRING = 1;
122  const int AS_INT = 2;
123  const int AS_FLOAT = 8;
124 
125  .int __stat_null;
126  STATIC_INIT(stats)
127  {
128  STATS = new_pure(stats);
129  // Prevent engine stats being sent
130  int r = STATS_ENGINE_RESERVE;
131  for (int i = 0, n = 256 - r; i < n; ++i) {
132  #define X(_, name, id) if (i == id) continue;
133  MAGIC_STATS(X, );
134  #undef X
135  addstat(r + i, AS_INT, __stat_null);
136  }
137  }
138 
139  #define _STAT(id) stat_##id
140  #define REGISTER_STAT_2(id, T) \
141  .T _STAT(id); \
142  REGISTER(Stats, STAT_##id, m_id, new_pure(stat)) \
143  { \
144  if (#T == "vector" || #T == "vectori") { \
145  REGISTRY_RESERVE(Stats, m_id, STAT_##id, y); \
146  REGISTRY_RESERVE(Stats, m_id, STAT_##id, z); \
147  } \
148  } \
149  ACCUMULATE void stats_add() \
150  { \
151  .T fld = _STAT(id); \
152  addstat_##T(STAT_##id.m_id, fld); \
153  }
154  void GlobalStats_update(entity this) {}
155  void GlobalStats_updateglobal() {}
157  #define REGISTER_STAT_3(id, T, expr) \
158  REGISTER_STAT_2(id, T); \
159  ACCUMULATE void GlobalStats_update(entity this) { STAT(id, this) = (expr); } \
160  ACCUMULATE void GlobalStats_updateglobal() { entity this = STATS; STAT(id, this) = (expr); } \
161  STATIC_INIT(worldstat_##id) { entity this = STATS; STAT(id, this) = (expr); }
162 #else
163  #define REGISTER_STAT_2(id, type)
164  #define REGISTER_STAT_3(id, T, expr)
165 #endif
166 
167 REGISTRY(Stats, 256 - STATS_ENGINE_RESERVE)
168 REGISTER_REGISTRY(Stats)
169 REGISTRY_SORT(Stats)
170 REGISTRY_CHECK(Stats)
171 
173 STATIC_INIT(Stats_renumber)
174 {
175  FOREACH(Stats, true, {
176  it.m_id = STATS_ENGINE_RESERVE + i;
177  MAGIC_STATS_FIX(it);
178  });
179 }
180 #ifdef SVQC
181 STATIC_INIT(stats_add) { stats_add(); }
182 #endif
vector vectori
Definition: stats.qh:9
#define X(team)
#define MAGIC_STATS_FIX(it)
Definition: stats.qh:60
entity() spawn
#define REGISTRY_CHECK(id)
Definition: registry.qh:175
#define REGISTRY_SORT(...)
Definition: registry.qh:128
STATIC_INIT(Stats_renumber)
Definition: stats.qh:173
#define REGISTER_REGISTRY(id)
Definition: registry.qh:212
#define REGISTRY_DEFINE_GET(id, null)
Definition: registry.qh:40
int g_magic_stats_hole
Definition: stats.qh:52
#define REGISTRY(id, max)
Declare a new registry.
Definition: registry.qh:26
#define NULL
Definition: post.qh:17
#define MAGIC_STATS(_, x)
Definition: stats.qh:13
vector(float skel, float bonenum) _skel_get_boneabs_hidden
const int STATS_ENGINE_RESERVE
Definition: stats.qh:11
#define new_pure(class)
purely logical entities (.origin doesn&#39;t work)
Definition: oo.qh:62
int m_id
Definition: stats.qh:8
#define USING(name, T)
Definition: _all.inc:72
#define FOREACH(list, cond, body)
Definition: iter.qh:19