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) \
/** 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();
#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)
{
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)
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)
#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)
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 = ":"; \
#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);
}
}
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) \