]> git.rm.cloudns.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
Registry: fix incorrect sorting
authorTimePath <andrew.hardaker1995@gmail.com>
Fri, 6 Nov 2015 01:06:44 +0000 (12:06 +1100)
committerTimePath <andrew.hardaker1995@gmail.com>
Fri, 6 Nov 2015 01:06:44 +0000 (12:06 +1100)
qcsrc/common/command/all.qh
qcsrc/common/items/all.qh
qcsrc/common/weapons/all.qh
qcsrc/lib/net.qh
qcsrc/lib/registry.qh
qcsrc/lib/sort.qh
qcsrc/lib/static.qh

index b2e85dd87a33eaefdb3b2b4a40602bdfe23acbe5..05ccb464193d6670943eaefd822e337e513867d4 100644 (file)
@@ -5,7 +5,7 @@
 REGISTRY(GENERIC_COMMANDS, BITS(7))
 #define GENERIC_COMMANDS_from(i) _GENERIC_COMMANDS_from(i, NULL)
 REGISTER_REGISTRY(RegisterGENERIC_COMMANDS)
-REGISTRY_SORT(GENERIC_COMMANDS, m_name, 0)
+REGISTRY_SORT(GENERIC_COMMANDS, 0)
 
 #define GENERIC_COMMAND(id, description) \
        CLASS(genericcommand_##id, Command) \
index ca0189292ffa4b2dd4288b541541f988774c2ac0..69805a568cb992c7a9ce153bb4e7a49f4b68588d 100644 (file)
@@ -11,7 +11,7 @@ REGISTER_REGISTRY(RegisterItems)
 /** If you register a new item, make sure to add it to all.inc */
 #define REGISTER_ITEM(id, class) REGISTER(RegisterItems, ITEM, Items, id, m_id, NEW(class))
 
-REGISTRY_SORT(Items, m_name, 0)
+REGISTRY_SORT(Items, 0)
 STATIC_INIT(Items) { FOREACH(Items, true, LAMBDA(it.m_id = i)); }
 
 void Dump_Items();
index c8940898650f9f7b5f5692e15437ba2bb302d731..5844049176eabb9d5dd4ad747898ec191fb6c743 100644 (file)
@@ -133,13 +133,13 @@ REGISTER_WEAPON(Null, NEW(Weapon));
 #include "all.inc"
 
 // TODO: remove after 0.8.2. Retains impulse number compatibility because 0.8.1 clients don't reload the weapons.cfg
-#define WEP_HARDCODED_IMPULSES 22
+#define WEP_HARDCODED_IMPULSES 20
 
 // TODO: invert after 0.8.2. Will require moving 'best weapon' impulses
 #define WEP_IMPULSE_BEGIN 230
 #define WEP_IMPULSE_END bound(WEP_IMPULSE_BEGIN, WEP_IMPULSE_BEGIN + (Weapons_COUNT - 1) - 1, 253)
 
-REGISTRY_SORT(Weapons, netname, WEP_HARDCODED_IMPULSES + 1)
+REGISTRY_SORT(Weapons, WEP_HARDCODED_IMPULSES + 1)
 
 STATIC_INIT(register_weapons_done)
 {
index 1f8d27e80aedfebfd484c6d01374e7abf5dfd969..7e3a1f1cbcc5569e71aa4f081beced5b27ddb074 100644 (file)
 REGISTRY(LinkedEntities, BITS(8) - 1)
 #define LinkedEntities_from(i) _LinkedEntities_from(i, NULL)
 REGISTER_REGISTRY(RegisterLinkedEntities)
-REGISTRY_SORT(LinkedEntities, netname, 0)
+REGISTRY_SORT(LinkedEntities, 0)
 STATIC_INIT(RegisterLinkedEntities_renumber)
 {
        for (int i = 0; i < LinkedEntities_COUNT; ++i)
@@ -140,7 +140,7 @@ STATIC_INIT(RegisterLinkedEntities_renumber)
 REGISTRY(TempEntities, BITS(8) - 80)
 #define TempEntities_from(i) _TempEntities_from(i, NULL)
 REGISTER_REGISTRY(RegisterTempEntities)
-REGISTRY_SORT(TempEntities, netname, 0)
+REGISTRY_SORT(TempEntities, 0)
 STATIC_INIT(RegisterTempEntities_renumber)
 {
        for (int i = 0; i < TempEntities_COUNT; ++i)
index 7ff94fbaae4acafa030259db706e0f07948dede1..eb8372f7638c980791a48179988f8f4f4f6deb3a 100644 (file)
@@ -3,6 +3,8 @@
 
 #include "oo.qh"
 
+#define REGISTER_REGISTRY(func) ACCUMULATE_FUNCTION(__static_init, func)
+
 #define REGISTER_INIT(ns, id) [[accumulate]] void Register_##ns##_##id##_init(entity this)
 #define REGISTER_INIT_POST(ns, id) [[accumulate]] void Register_##ns##_##id##_init_post(entity this)
 
@@ -11,8 +13,7 @@
        const int id##_MAX = max; \
        noref entity _##id[id##_MAX], id##_first, id##_last; \
        int id##_COUNT; \
-       entity _##id##_from(int i, entity null) { if (i >= 0 && i < id##_COUNT) { entity e = _##id[i]; if (e) return e; } return null; } \
-       REGISTRY_CHECK(id)
+       entity _##id##_from(int i, entity null) { if (i >= 0 && i < id##_COUNT) { entity e = _##id[i]; if (e) return e; } return null; }
 
 /** registered item identifier */
 .string registered_id;
  * @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) {}                                  \
+#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.registered_id = #id;                                   \
-               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)             \
+               entity this = inst; \
+               ns##_##id = this; \
+               this.registered_id = #id; \
+               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)
 
 /** internal next pointer */
 #define REGISTRY_NEXT enemy
 .entity REGISTRY_NEXT;
 
-#define REGISTRY_SORT(id, field, skip)                              \
+#define REGISTRY_SORT(id, 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);                                    \
-       }                                                               \
+       { \
+               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; \
+       } \
+       int _REGISTRY_CMP_##id(int i, int j, entity pass) \
+       { \
+               i += skip; j += skip; \
+               string a = _##id[i].registered_id; \
+               string b = _##id[j].registered_id; \
+               return strcmp(a, b); \
+       } \
        STATIC_INIT(Registry_sort_##id) \
-       {                               \
+       { \
                heapsort(id##_COUNT - (skip), _REGISTRY_SWAP_##id, _REGISTRY_CMP_##id, NULL); \
-       }
+       } \
+       REGISTRY_CHECK(id)
 
 #define REGISTRY_CHECK(id) \
-       STATIC_INIT_LATE(Registry_check_##id) \
+       STATIC_INIT(Registry_check_##id) \
        { \
                string algo = "SHA256"; \
                string join = ":"; \
index 7f2b61fba4a0a59eb5a7072b9f69c58db2c4d03a..748bd2b2748efe82a071e4583771adbe338de146 100644 (file)
@@ -2,61 +2,43 @@
 #define SORT_H
 
 /** is only ever called for i1 < i2 */
-typedef void (float i1, float i2, entity pass) swapfunc_t;
+typedef void (int i1, int i2, entity pass) swapfunc_t;
 /** <0 for <, ==0 for ==, >0 for > (like strcmp) */
-typedef float (float i1, float i2, entity pass) comparefunc_t;
+typedef int (int i1, int i2, entity pass) comparefunc_t;
 
-void heapsort(float n, swapfunc_t swap, comparefunc_t cmp, entity pass)
+void heapsort(int n, swapfunc_t swap, comparefunc_t cmp, entity pass)
 {
-       int root, child;
+       #define heapify(_count) \
+               do \
+               { \
+                       for (int start = floor(((_count) - 2) / 2); start >= 0; --start) \
+                       { \
+                               siftdown(start, (_count) - 1); \
+                       } \
+               } \
+               while (0)
 
-       // heapify
-       int start = floor((n - 2) / 2);
-       while (start >= 0)
-       {
-               // siftdown(start, n - 1);
-               root = start;
-               while (root * 2 + 1 <= n - 1)
-               {
-                       child = root * 2 + 1;
-                       if (child < n - 1 && cmp(child, child + 1, pass) < 0) child += 1;
-                       if (cmp(root, child, pass) < 0)
-                       {
-                               swap(root, child, pass);
-                               root = child;
-                       }
-                       else
-                       {
-                               break;
-                       }
-               }
-               // end of siftdown
-               --start;
-       }
+       #define siftdown(_start, _end) \
+               do \
+               { \
+                       for (int root = (_start); root * 2 + 1 <= (_end); ) \
+                       { \
+                               int child = root * 2 + 1; \
+                               if (child < (_end) && cmp(child, child + 1, pass) < 0) child += 1; \
+                               if (cmp(root, child, pass) >= 0) break; \
+                               swap(root, child, pass); \
+                               root = child; \
+                       } \
+               } \
+               while (0)
 
-       // extract
+       heapify(n);
        int end = n - 1;
        while (end > 0)
        {
                swap(0, end, pass);
                end -= 1;
-               // siftdown(0, end);
-               root = 0;
-               while (root * 2 + 1 <= end)
-               {
-                       child = root * 2 + 1;
-                       if (child < end && cmp(child, child + 1, pass) < 0) child += 1;
-                       if (cmp(root, child, pass) < 0)
-                       {
-                               swap(root, child, pass);
-                               root = child;
-                       }
-                       else
-                       {
-                               break;
-                       }
-               }
-               // end of siftdown
+               siftdown(0, end);
        }
 }
 
index a9f7f53d678dddc5cbb5ffe1dfbc5eefa62aed3e..70eafd1b7d16ad89eb06e16d9d7d93bdae1dc983 100644 (file)
@@ -8,8 +8,6 @@ void __static_init_late() {}
 void __static_init_precache() {}
 #define static_init_precache() CALL_ACCUMULATED_FUNCTION(__static_init_precache)
 
-#define REGISTER_REGISTRY(func) ACCUMULATE_FUNCTION(__static_init, func)
-
 #define _STATIC_INIT(where, func) \
        void _static_##func(); \
        ACCUMULATE_FUNCTION(where, _static_##func) \