#ifndef LIB_STATS_H #define LIB_STATS_H // TODO: rename to 'netvars' #include "registry.qh" #include "sort.qh" .int m_id; #define REGISTER_STAT(...) EVAL(OVERLOAD(REGISTER_STAT, __VA_ARGS__)) #if defined(CSQC) /** Get all stats and store them as globals, access with `STAT(ID)` */ void stats_get() {} #define STAT(...) EVAL(OVERLOAD(STAT, __VA_ARGS__)) #define STAT_1(id) STAT_2(id, NULL) #define STAT_2(id, cl) (0, _STAT(id)) #define getstat_int(id) getstati(id, 0, 24) #define getstat_bool(id) boolean(getstati(id)) #define getstat_float(id) getstatf(id) #define _STAT(id) g_stat_##id #define REGISTER_STAT_2(id, type) \ type _STAT(id); \ REGISTER(RegisterStats, STAT, Stats, id, m_id, new(stat)) \ { \ make_pure(this); \ } \ [[accumulate]] void stats_get() \ { \ _STAT(id) = getstat_##type(STAT_##id.m_id); \ } #define REGISTER_STAT_3(x, T, expr) REGISTER_STAT(x, T) #elif defined(SVQC) /** Add all registered stats, access with `STAT(ID, player)` or `.type stat = _STAT(ID); player.stat` */ void stats_add() {} #define STAT(id, cl) (cl._STAT(id)) #define addstat_int(id, fld) addstat(id, AS_INT, fld) #define addstat_bool(id, fld) addstat(id, AS_INT, fld) #define addstat_float(id, fld) addstat(id, AS_FLOAT, fld) const int AS_STRING = 1; const int AS_INT = 2; const int AS_FLOAT = 8; .int __stat_null; /** Prevent engine stats being sent */ STATIC_INIT(stats_clear) { int r = 32; for (int i = 0, n = 256 - r; i < n; ++i) { addstat(r + i, AS_INT, __stat_null); } } #define _STAT(id) stat_##id #define REGISTER_STAT_2(id, type) \ .type _STAT(id); \ REGISTER(RegisterStats, STAT, Stats, id, m_id, new(stat)) \ { \ make_pure(this); \ } \ [[accumulate]] void stats_add() \ { \ addstat_##type(STAT_##id.m_id, _STAT(id)); \ } void GlobalStats_update(entity this) {} #define REGISTER_STAT_3(x, T, expr) REGISTER_STAT(x, T); [[accumulate]] void GlobalStats_update(entity this) { STAT(x, this) = (expr); } #else #define REGISTER_STAT_2(id, type) #define REGISTER_STAT_3(x, T, expr) #endif const int STATS_ENGINE_RESERVE = 32 + (8 * 3); // Not sure how to handle vector stats yet, reserve them too REGISTRY(Stats, 256 - STATS_ENGINE_RESERVE) REGISTER_REGISTRY(RegisterStats) REGISTRY_SORT(Stats, 0) REGISTRY_CHECK(Stats) STATIC_INIT(RegisterStats_renumber) { FOREACH(Stats, true, LAMBDA(it.m_id = STATS_ENGINE_RESERVE + i)); } #ifdef SVQC STATIC_INIT(stats_add) { stats_add(); } #endif #endif