]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/stats.qh
Macros: optimize
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / stats.qh
1 #ifndef LIB_STATS_H
2 #define LIB_STATS_H
3
4 // TODO: rename to 'netvars'
5
6 #include "registry.qh"
7 #include "sort.qh"
8
9 .int m_id;
10 typedef vector vectori;
11
12 #define REGISTER_STAT(...) EVAL(OVERLOAD(REGISTER_STAT, __VA_ARGS__))
13 #if defined(CSQC)
14         /** Get all stats and store them as globals, access with `STAT(ID)` */
15         void stats_get() {}
16         #define STAT(...) EVAL(OVERLOAD(STAT, __VA_ARGS__))
17     #define STAT_1(id) STAT_2(id, NULL)
18         #define STAT_2(id, cl) (0, _STAT(id))
19
20         #define getstat_int(id) getstati(id, 0, 24)
21         #define getstat_bool(id) boolean(getstati(id))
22         #define getstat_float(id) getstatf(id)
23         #define getstat_vector(id) vec3(getstat_float(id + 0), getstat_float(id + 1), getstat_float(id + 2))
24         #define getstat_vectori(id) vec3(getstat_int(id + 0), getstat_int(id + 1), getstat_int(id + 2))
25
26         #define _STAT(id) g_stat_##id
27         #define REGISTER_STAT_2(id, T) \
28                 T _STAT(id); \
29                 REGISTER(Stats, STAT_##id, m_id, new(stat)) \
30                 { \
31                         make_pure(this); \
32                         if (#T == "vector" || #T == "vectori") { \
33                                 REGISTRY_RESERVE(Stats, m_id, STAT_##id, y); \
34                                 REGISTRY_RESERVE(Stats, m_id, STAT_##id, z); \
35                         } \
36                 } \
37                 [[accumulate]] void stats_get() \
38                 { \
39                         _STAT(id) = getstat_##T(STAT_##id.m_id); \
40                 }
41         #define REGISTER_STAT_3(x, T, expr) REGISTER_STAT(x, T)
42 #elif defined(SVQC)
43         /** Add all registered stats, access with `STAT(ID, player)` or `.type stat = _STAT(ID); player.stat` */
44         void stats_add() {}
45         #define STAT(id, cl) (cl._STAT(id))
46
47         #define addstat_int(id, fld) addstat(id, AS_INT, fld)
48         #define addstat_bool(id, fld) addstat(id, AS_INT, fld)
49         #define addstat_float(id, fld) addstat(id, AS_FLOAT, fld)
50         #define addstat_vector(id, fld) MACRO_BEGIN { \
51                 addstat_float(id + 0, fld##_x); \
52                 addstat_float(id + 1, fld##_y); \
53                 addstat_float(id + 2, fld##_z); \
54         } MACRO_END
55         #define addstat_vectori(id, fld) MACRO_BEGIN { \
56                 addstat_int(id + 0, fld##_x); \
57                 addstat_int(id + 1, fld##_y); \
58                 addstat_int(id + 2, fld##_z); \
59         } MACRO_END
60         const int AS_STRING = 1;
61         const int AS_INT = 2;
62         const int AS_FLOAT = 8;
63
64         .int __stat_null;
65         /** Prevent engine stats being sent */
66         STATIC_INIT(stats_clear)
67         {
68                 int r = 32;
69                 for (int i = 0, n = 256 - r; i < n; ++i) {
70                         addstat(r + i, AS_INT, __stat_null);
71                 }
72         }
73
74         #define _STAT(id) stat_##id
75         #define REGISTER_STAT_2(id, T) \
76                 .T _STAT(id); \
77                 REGISTER(Stats, STAT_##id, m_id, new(stat)) \
78                 { \
79                         make_pure(this); \
80                         if (#T == "vector" || #T == "vectori") { \
81                                 REGISTRY_RESERVE(Stats, m_id, STAT_##id, y); \
82                                 REGISTRY_RESERVE(Stats, m_id, STAT_##id, z); \
83                         } \
84                 } \
85                 [[accumulate]] void stats_add() \
86                 { \
87                         addstat_##T(STAT_##id.m_id, _STAT(id)); \
88                 }
89         void GlobalStats_update(entity this) {}
90     #define REGISTER_STAT_3(x, T, expr) \
91         REGISTER_STAT(x, T); \
92         [[accumulate]] void GlobalStats_update(entity this) { STAT(x, this) = (expr); } \
93         STATIC_INIT(worldstat_##x) { entity this = world; STAT(x, this) = (expr); }
94 #else
95         #define REGISTER_STAT_2(id, type)
96     #define REGISTER_STAT_3(x, T, expr)
97 #endif
98
99 const int STATS_ENGINE_RESERVE = 32;
100
101 REGISTRY(Stats, 256 - STATS_ENGINE_RESERVE)
102 REGISTER_REGISTRY(Stats)
103 REGISTRY_SORT(Stats)
104 REGISTRY_CHECK(Stats)
105 STATIC_INIT(RegisterStats_renumber)
106 {
107         FOREACH(Stats, true, LAMBDA(it.m_id = STATS_ENGINE_RESERVE + i));
108 }
109 #ifdef SVQC
110 STATIC_INIT(stats_add) { stats_add(); }
111 #endif
112
113 #endif