]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/lib/stats.qh
Stats: port vectors
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / stats.qh
index 929c5902e8335ddd0ada8a66675edf9ab0cbd05d..32927f4bd8b7793eecfcdb0fad49908662e53254 100644 (file)
@@ -7,6 +7,7 @@
 #include "sort.qh"
 
 .int m_id;
+typedef vector vectori;
 
 #define REGISTER_STAT(...) EVAL(OVERLOAD(REGISTER_STAT, __VA_ARGS__))
 #if defined(CSQC)
        #define getstat_int(id) getstati(id, 0, 24)
        #define getstat_bool(id) boolean(getstati(id))
        #define getstat_float(id) getstatf(id)
+       #define getstat_vector(id) vec3(getstat_float(id + 0), getstat_float(id + 1), getstat_float(id + 2))
+       #define getstat_vectori(id) vec3(getstat_int(id + 0), getstat_int(id + 1), getstat_int(id + 2))
 
        #define _STAT(id) g_stat_##id
-       #define REGISTER_STAT_2(id, type) \
-               type _STAT(id); \
+       #define REGISTER_STAT_2(id, T) \
+               T _STAT(id); \
                REGISTER(RegisterStats, STAT, Stats, id, m_id, new(stat)) \
                { \
                        make_pure(this); \
+                       if (#T == "vector" || #T == "vectori") { \
+                               REGISTRY_RESERVE(Stats, m_id, id, _y); \
+                               REGISTRY_RESERVE(Stats, m_id, id, _z); \
+                       } \
                } \
                [[accumulate]] void stats_get() \
                { \
-                       _STAT(id) = getstat_##type(STAT_##id.m_id); \
+                       _STAT(id) = getstat_##T(STAT_##id.m_id); \
                }
        #define REGISTER_STAT_3(x, T, expr) REGISTER_STAT(x, T)
 #elif defined(SVQC)
        #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)
+       #define addstat_vector(id, fld) do { \
+               addstat_float(id + 0, fld##_x); \
+               addstat_float(id + 1, fld##_y); \
+               addstat_float(id + 2, fld##_z); \
+       } while (0)
+       #define addstat_vectori(id, fld) do { \
+               addstat_int(id + 0, fld##_x); \
+               addstat_int(id + 1, fld##_y); \
+               addstat_int(id + 2, fld##_z); \
+       } while (0)
        const int AS_STRING = 1;
        const int AS_INT = 2;
        const int AS_FLOAT = 8;
        }
 
        #define _STAT(id) stat_##id
-       #define REGISTER_STAT_2(id, type) \
-               .type _STAT(id); \
+       #define REGISTER_STAT_2(id, T) \
+               .T _STAT(id); \
                REGISTER(RegisterStats, STAT, Stats, id, m_id, new(stat)) \
                { \
                        make_pure(this); \
+                       if (#T == "vector" || #T == "vectori") { \
+                               REGISTRY_RESERVE(Stats, m_id, id, _y); \
+                               REGISTRY_RESERVE(Stats, m_id, id, _z); \
+                       } \
                } \
                [[accumulate]] void stats_add() \
                { \
-                       addstat_##type(STAT_##id.m_id, _STAT(id)); \
+                       addstat_##T(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); }
+    #define REGISTER_STAT_3(x, T, expr) \
+       REGISTER_STAT(x, T); \
+       [[accumulate]] void GlobalStats_update(entity this) { STAT(x, this) = (expr); } \
+       STATIC_INIT(worldstat_##x) { entity this = world; 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
+const int STATS_ENGINE_RESERVE = 32;
 
 REGISTRY(Stats, 256 - STATS_ENGINE_RESERVE)
 REGISTER_REGISTRY(RegisterStats)