4 // TODO: rename to 'netvars'
10 typedef vector vectori;
12 #define REGISTER_STAT(...) EVAL(OVERLOAD(REGISTER_STAT, __VA_ARGS__))
14 /** Get all stats and store them as globals, access with `STAT(ID)` */
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))
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))
26 #define _STAT(id) g_stat_##id
27 #define REGISTER_STAT_2(id, T) \
29 REGISTER(Stats, STAT_##id, m_id, new(stat)) \
32 if (#T == "vector" || #T == "vectori") { \
33 REGISTRY_RESERVE(Stats, m_id, STAT_##id, y); \
34 REGISTRY_RESERVE(Stats, m_id, STAT_##id, z); \
37 [[accumulate]] void stats_get() \
39 _STAT(id) = getstat_##T(STAT_##id.m_id); \
41 #define REGISTER_STAT_3(x, T, expr) REGISTER_STAT(x, T)
43 /** Add all registered stats, access with `STAT(ID, player)` or `.type stat = _STAT(ID); player.stat` */
45 #define STAT(id, cl) (cl._STAT(id))
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) do { \
51 addstat_float(id + 0, fld##_x); \
52 addstat_float(id + 1, fld##_y); \
53 addstat_float(id + 2, fld##_z); \
55 #define addstat_vectori(id, fld) do { \
56 addstat_int(id + 0, fld##_x); \
57 addstat_int(id + 1, fld##_y); \
58 addstat_int(id + 2, fld##_z); \
60 const int AS_STRING = 1;
62 const int AS_FLOAT = 8;
65 /** Prevent engine stats being sent */
66 STATIC_INIT(stats_clear)
69 for (int i = 0, n = 256 - r; i < n; ++i) {
70 addstat(r + i, AS_INT, __stat_null);
74 #define _STAT(id) stat_##id
75 #define REGISTER_STAT_2(id, T) \
77 REGISTER(Stats, STAT_##id, m_id, new(stat)) \
80 if (#T == "vector" || #T == "vectori") { \
81 REGISTRY_RESERVE(Stats, m_id, STAT_##id, y); \
82 REGISTRY_RESERVE(Stats, m_id, STAT_##id, z); \
85 [[accumulate]] void stats_add() \
87 addstat_##T(STAT_##id.m_id, _STAT(id)); \
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); }
95 #define REGISTER_STAT_2(id, type)
96 #define REGISTER_STAT_3(x, T, expr)
99 const int STATS_ENGINE_RESERVE = 32;
101 REGISTRY(Stats, 256 - STATS_ENGINE_RESERVE)
102 REGISTER_REGISTRY(Stats)
104 REGISTRY_CHECK(Stats)
105 STATIC_INIT(RegisterStats_renumber)
107 FOREACH(Stats, true, LAMBDA(it.m_id = STATS_ENGINE_RESERVE + i));
110 STATIC_INIT(stats_add) { stats_add(); }