-#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)
-
-#define REGISTER(initfunc, ns, array, counter, id, fld, inst) \
- entity ns##_##id; \
- REGISTER_INIT(ns, id) { } \
- REGISTER_INIT_POST(ns, id) { } \
- .entity enemy; /* internal next pointer */ \
- void Register_##ns##_##id() { \
- entity this = inst; \
- ns##_##id = this; \
- this.fld = counter; \
- array[counter++] = 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) \
- REGISTER_INIT(ns, id)
+#if 1
+ #define _R_MAP(r, max) ArrayList r; STATIC_INIT(r) { AL_NEW(r, max, NULL, e); }
+ #define _R_GET(r, i) AL_gete(r, i)
+ #define _R_SET(r, i, e) AL_sete(r, i, e)
+ #define _R_DEL(r) AL_DELETE(r)
+#else
+ #define _R_MAP(r, max) entity r[max]
+ #define _R_GET(r, i) r[i]
+ #define _R_SET(r, i, e) r[i] = e
+ #define _R_DEL(r)
+#endif
+
+/**
+ * Declare a new registry.
+ *
+ * Don't forget to call `REGISTER_REGISTRY`:
+ * REGISTER_REGISTRY(Foos)
+ */
+#define REGISTRY(id, max) \
+ void Register##id(); \
+ [[accumulate]] REGISTRY_BEGIN(id) {} \
+ [[accumulate]] REGISTRY_END(id) {} \
+ void _Register##id() {} \
+ void Register##id() { REGISTRY_BEGIN_(id); _Register##id(); REGISTRY_END_(id); } \
+ const int id##_MAX = max; \
+ int id##_COUNT; \
+ noref entity id##_first, id##_last; \
+ _R_MAP(_##id, id##_MAX); \
+ SHUTDOWN(id) { _R_DEL(_##id); } \
+ entity _##id##_from(int i, entity null) { if (i >= 0 && i < id##_COUNT) { entity e = _R_GET(_##id, i); if (e) return e; } return null; }
+
+/** Called before initializing a registry. */
+#define REGISTRY_BEGIN(id) [[accumulate]] void REGISTRY_BEGIN_(id) { noref void() f = Register##id; } void REGISTRY_BEGIN_(id)
+#define REGISTRY_BEGIN_(id) Register##id##_First()
+
+/** Called after initializing a registry. */
+#define REGISTRY_END(id) [[accumulate]] void REGISTRY_END_(id) { noref void() f = Register##id; } void REGISTRY_END_(id)
+#define REGISTRY_END_(id) Register##id##_Done()
+
+REGISTRY(Registries, BITS(8))
+
+/** registered item identifier */
+.string registered_id;
+
+/**
+ * Register a new entity with a registry.
+ * Must be followed by a semicolon or a function body with a `this` parameter.
+ * Wrapper macros may perform actions after user initialization like so:
+ * #define REGISTER_FOO(id) \
+ * REGISTER(Foos, FOO, id, m_id, NEW(Foo)) { \
+ * print("Registering foo #", this.m_id + 1, "\n"); \
+ * } \
+ * REGISTER_INIT(FOO, id)
+ *
+ *
+ * @param registry The registry to add each entity to.
+ * @param ns Short for namespace, prefix for each global (ns##_##id)
+ * @param id The identifier of the current entity being registered
+ * @param fld The field to store the locally unique unique entity id
+ * @param inst An expression to create a new instance, invoked for every registration
+ */
+#define REGISTER(...) EVAL_REGISTER(OVERLOAD_(REGISTER, __VA_ARGS__))
+#define EVAL_REGISTER(...) __VA_ARGS__
+#define REGISTER_5(registry, ns, id, fld, inst) REGISTER_4(registry, ns##_##id, fld, inst)
+#define REGISTER_4(registry, id, fld, inst) \
+ entity id; \
+ REGISTER_INIT(id) {} \
+ void Register_##id() \
+ { \
+ if (registry##_COUNT >= registry##_MAX) LOG_FATALF("Registry capacity exceeded (%d)", registry##_MAX); \
+ entity this = id = inst; \
+ this.registered_id = #id; \
+ REGISTRY_PUSH(registry, fld, this); \
+ Register_##id##_init(this); \
+ } \
+ ACCUMULATE_FUNCTION(_Register##registry, Register_##id) \
+ REGISTER_INIT(id)
+
+#define REGISTRY_PUSH(registry, fld, it) MACRO_BEGIN { \
+ it.fld = registry##_COUNT; \
+ _R_SET(_##registry, registry##_COUNT, it); \
+ ++registry##_COUNT; \
+ if (!registry##_first) registry##_first = it; \
+ if (registry##_last) registry##_last.REGISTRY_NEXT = it; \
+ registry##_last = it; \
+} MACRO_END