From: TimePath Date: Fri, 6 Nov 2015 01:06:44 +0000 (+1100) Subject: Registry: fix incorrect sorting X-Git-Tag: xonotic-v0.8.2~1701 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=8f450f9a25325203da1e502a319c64fdd51b43ef;p=xonotic%2Fxonotic-data.pk3dir.git Registry: fix incorrect sorting --- diff --git a/qcsrc/common/command/all.qh b/qcsrc/common/command/all.qh index b2e85dd87..05ccb4641 100644 --- a/qcsrc/common/command/all.qh +++ b/qcsrc/common/command/all.qh @@ -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) \ diff --git a/qcsrc/common/items/all.qh b/qcsrc/common/items/all.qh index ca0189292..69805a568 100644 --- a/qcsrc/common/items/all.qh +++ b/qcsrc/common/items/all.qh @@ -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(); diff --git a/qcsrc/common/weapons/all.qh b/qcsrc/common/weapons/all.qh index c89408986..584404917 100644 --- a/qcsrc/common/weapons/all.qh +++ b/qcsrc/common/weapons/all.qh @@ -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) { diff --git a/qcsrc/lib/net.qh b/qcsrc/lib/net.qh index 1f8d27e80..7e3a1f1cb 100644 --- a/qcsrc/lib/net.qh +++ b/qcsrc/lib/net.qh @@ -111,7 +111,7 @@ 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) diff --git a/qcsrc/lib/registry.qh b/qcsrc/lib/registry.qh index 7ff94fbaa..eb8372f76 100644 --- a/qcsrc/lib/registry.qh +++ b/qcsrc/lib/registry.qh @@ -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; @@ -39,64 +40,65 @@ * @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 = ":"; \ diff --git a/qcsrc/lib/sort.qh b/qcsrc/lib/sort.qh index 7f2b61fba..748bd2b27 100644 --- a/qcsrc/lib/sort.qh +++ b/qcsrc/lib/sort.qh @@ -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); } } diff --git a/qcsrc/lib/static.qh b/qcsrc/lib/static.qh index a9f7f53d6..70eafd1b7 100644 --- a/qcsrc/lib/static.qh +++ b/qcsrc/lib/static.qh @@ -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) \