]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/lib/registry.qh
Items: use Model references instead of strings
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / registry.qh
index 688a711912cd1e43694b1856d03a0493b09c10b5..4c39a9dec6e59379f719e24b865857e9503c5ebe 100644 (file)
  * @param fld       The field to store the current count into
  * @param inst      An expression to create a new instance, invoked for every registration
  */
-#define REGISTER(initfunc, ns, array, id, fld, inst)            \
-    entity ns##_##id;                                           \
-    REGISTER_INIT(ns, id) { }                                   \
-    REGISTER_INIT_POST(ns, id) { }                              \
-    .entity enemy; /* internal next pointer */                  \
-    void Register_##ns##_##id() {                               \
+#define REGISTER(initfunc, ns, array, id, fld, inst)                \
+    entity ns##_##id;                                               \
+    REGISTER_INIT(ns, id) { }                                       \
+    REGISTER_INIT_POST(ns, id) { }                                  \
+    void Register_##ns##_##id() {                                   \
         if (array##_COUNT >= array##_MAX) LOG_FATALF("Registry capacity exceeded (%s)", ftos(array##_MAX)); \
-        entity this = inst;                                     \
-        ns##_##id = this;                                       \
-        this.fld = array##_COUNT;                               \
-        array[array##_COUNT++] = this;                          \
-        if (!array##_first)    array##_first = this;            \
-        if ( array##_last)     array##_last.enemy = this;       \
-        array##_last = this;                                    \
-        Register_##ns##_##id##_init(this);                      \
-        Register_##ns##_##id##_init_post(this);                 \
-    }                                                           \
-    ACCUMULATE_FUNCTION(initfunc, Register_##ns##_##id)         \
+        entity this = inst;                                         \
+        ns##_##id = this;                                           \
+        this.fld = array##_COUNT;                                   \
+        array[array##_COUNT++] = this;                              \
+        if (!array##_first)    array##_first = this;                \
+        if ( array##_last)     array##_last.REGISTRY_NEXT = this;   \
+        array##_last = this;                                        \
+        Register_##ns##_##id##_init(this);                          \
+        Register_##ns##_##id##_init_post(this);                     \
+    }                                                               \
+    ACCUMULATE_FUNCTION(initfunc, Register_##ns##_##id)             \
     REGISTER_INIT(ns, id)
 
-#define REGISTRY_SORT(id, field, skip)                          \
-    void _REGISTRY_SWAP_##id(int i, int j, entity pass) {       \
-        i += skip; j += skip;                                   \
-        entity e = id[i];                                       \
-        id[i] = id[j];                                          \
-        id[j] = e;                                              \
-    }                                                           \
-    float _REGISTRY_CMP_##id(int i, int j, entity pass) {       \
-        i += skip; j += skip;                                   \
-        string a = id[i].field;                                 \
-        string b = id[j].field;                                 \
-        return strcasecmp(a, b);                                \
-    }                                                           \
-    STATIC_INIT(Registry_sort_##id) {                           \
+/** internal next pointer */
+#define REGISTRY_NEXT enemy
+.entity REGISTRY_NEXT;
+
+#define REGISTRY_SORT(id, field, skip)                              \
+    void _REGISTRY_SWAP_##id(int i, int j, entity pass) {           \
+        i += skip; j += skip;                                       \
+                                                                    \
+        entity a = id[i], b = id[j];                                \
+        id[i] = b;                                                  \
+        id[j] = a;                                                  \
+                                                                    \
+        entity a_next = a.REGISTRY_NEXT, b_next = b.REGISTRY_NEXT;  \
+        a.REGISTRY_NEXT = b_next;                                   \
+        b.REGISTRY_NEXT = a_next;                                   \
+                                                                    \
+        if (i == 0) id##_first = b;                                 \
+        else id[i - 1].REGISTRY_NEXT = b;                           \
+                                                                    \
+        if (j == 0) id##_first = a;                                 \
+        else id[j - 1].REGISTRY_NEXT = a;                           \
+    }                                                               \
+    float _REGISTRY_CMP_##id(int i, int j, entity pass) {           \
+        i += skip; j += skip;                                       \
+        string a = id[i].field;                                     \
+        string b = id[j].field;                                     \
+        return strcasecmp(a, b);                                    \
+    }                                                               \
+    STATIC_INIT(Registry_sort_##id) {                               \
         heapsort(id##_COUNT - (skip), _REGISTRY_SWAP_##id, _REGISTRY_CMP_##id, NULL); \
     }