3 #define FOREACH_ARRAY(arr, start, end, cond, body) \
6 for (int _i = start; _i < end; ++_i) \
8 const noref int i = _i; \
9 const noref entity it = arr[i]; \
10 if (cond) { LAMBDA(body) } \
14 #define FOREACH(list, cond, body) FOREACH_LIST(list, enemy, cond, body)
16 #define FOREACH_LIST(list, next, cond, body) \
20 for (entity _it = list##_first; _it; (_it = _it.next, ++_i)) \
22 const noref int i = _i; \
23 const noref entity it = _it; \
24 if (cond) { LAMBDA(body) } \
28 #define FOREACH_WORD(words, cond, body) \
31 string _words = words; \
33 for (string _it; (_it = car(_words)); (_words = cdr(_words), ++_i)) \
35 const noref int i = _i; \
36 const noref string it = _it; \
37 if (cond) { LAMBDA(body) } \
41 #define STRING_ITERATOR(this, s, i) \
42 string this##_s = s; \
45 #define STRING_ITERATOR_SET(this, s, i) \
51 #define STRING_ITERATOR_GET(this) str2chr(this##_s, this##_i++)
53 #define FOREACH_CHAR(s, cond, body) \
56 STRING_ITERATOR(iter, s, 0); \
58 while ((_it = STRING_ITERATOR_GET(iter)) > 0) \
60 const noref int it = _it; \
61 if (cond) { LAMBDA(body) } \
66 entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #402;
67 entity(.entity fld, entity match, .entity tofield) _findchainentity_tofield = #403;
68 entity(.float fld, float match, .entity tofield) _findchainfloat_tofield = #403;
69 entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #450;
71 entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #402;
72 entity(.entity fld, entity match, .entity tofield) _findchainentity_tofield = #403;
73 entity(.float fld, float match, .entity tofield) _findchainfloat_tofield = #403;
74 entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #450;
76 entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #26;
77 entity(.entity fld, entity match, .entity tofield) _findchainentity_tofield = #27;
78 entity(.float fld, float match, .entity tofield) _findchainfloat_tofield = #27;
79 entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #88;
82 #define ORDERED(F) F##_UNORDERED
84 #define FOREACH_ENTITY(cond, body) ORDERED(FOREACH_ENTITY)(cond, body)
85 #define FOREACH_ENTITY_ORDERED(cond, body) \
88 for (entity _it = NULL; (_it = nextent(_it)); ++_i) \
90 const noref int i = _i; \
91 const noref entity it = _it; \
92 if (cond) { LAMBDA(body) } \
95 /** marker field, always NULL */
96 .entity _FOREACH_ENTITY_fld;
98 .entity _FOREACH_ENTITY_next;
99 noref string _FOREACH_ENTITY_mutex;
100 #define FOREACH_ENTITY_UNORDERED(cond, body) \
102 if (_FOREACH_ENTITY_mutex) LOG_SEVEREF("Loop mutex held by %s", _FOREACH_ENTITY_mutex); \
103 _FOREACH_ENTITY_mutex = __FUNC__; \
104 entity _foundchain_first = _findchainentity_tofield(_FOREACH_ENTITY_fld, NULL, _FOREACH_ENTITY_next); \
105 FOREACH_LIST(_foundchain, _FOREACH_ENTITY_next, cond, body); \
106 _FOREACH_ENTITY_mutex = string_null; \
111 #define FOREACH_ENTITY_CLASS(class, cond, body) ORDERED(FOREACH_ENTITY_CLASS)(class, cond, body)
112 #define FOREACH_ENTITY_CLASS_ORDERED(class, cond, body) FOREACH_ENTITY_ORDERED(it.classname == class && (cond), body)
113 .entity _FOREACH_ENTITY_CLASS_next;
114 noref string _FOREACH_ENTITY_CLASS_mutex;
115 #define FOREACH_ENTITY_CLASS_UNORDERED(class, cond, body) \
117 if (_FOREACH_ENTITY_CLASS_mutex) LOG_SEVEREF("Loop mutex held by %s", _FOREACH_ENTITY_CLASS_mutex); \
118 _FOREACH_ENTITY_CLASS_mutex = __FUNC__; \
119 entity _foundchain_first = _findchainstring_tofield(classname, class, _FOREACH_ENTITY_CLASS_next); \
120 FOREACH_LIST(_foundchain, _FOREACH_ENTITY_CLASS_next, cond, body); \
121 _FOREACH_ENTITY_CLASS_mutex = string_null; \
126 #define FOREACH_ENTITY_FLOAT(fld, match, body) ORDERED(FOREACH_ENTITY_FLOAT)(fld, match, body)
127 #define FOREACH_ENTITY_FLOAT_ORDERED(fld, match, body) FOREACH_ENTITY_ORDERED(it.fld == match, body)
128 .entity _FOREACH_ENTITY_FLOAT_next;
129 noref string _FOREACH_ENTITY_FLOAT_mutex;
130 #define FOREACH_ENTITY_FLOAT_UNORDERED(fld, match, body) \
132 if (_FOREACH_ENTITY_FLOAT_mutex) LOG_SEVEREF("Loop mutex held by %s", _FOREACH_ENTITY_FLOAT_mutex); \
133 _FOREACH_ENTITY_FLOAT_mutex = __FUNC__; \
134 entity _foundchain_first = _findchainfloat_tofield(fld, match, _FOREACH_ENTITY_FLOAT_next); \
135 FOREACH_LIST(_foundchain, _FOREACH_ENTITY_FLOAT_next, true, body); \
136 _FOREACH_ENTITY_FLOAT_mutex = string_null; \
141 #define FOREACH_ENTITY_FLAGS(fld, match, body) ORDERED(FOREACH_ENTITY_FLAGS)(fld, match, body)
142 #define FOREACH_ENTITY_FLAGS_ORDERED(fld, match, body) FOREACH_ENTITY_ORDERED(it.fld & match, body)
143 .entity _FOREACH_ENTITY_FLAGS_next;
144 noref string _FOREACH_ENTITY_FLAGS_mutex;
145 #define FOREACH_ENTITY_FLAGS_UNORDERED(fld, match, body) \
147 if (_FOREACH_ENTITY_FLAGS_mutex) LOG_SEVEREF("Loop mutex held by %s", _FOREACH_ENTITY_FLAGS_mutex); \
148 _FOREACH_ENTITY_FLAGS_mutex = __FUNC__; \
149 entity _foundchain_first = _findchainflags_tofield(fld, match, _FOREACH_ENTITY_FLAGS_next); \
150 FOREACH_LIST(_foundchain, _FOREACH_ENTITY_FLAGS_next, true, body); \
151 _FOREACH_ENTITY_FLAGS_mutex = string_null; \
156 #define FOREACH_ENTITY_ENT(fld, match, body) ORDERED(FOREACH_ENTITY_ENT)(fld, match, body)
157 #define FOREACH_ENTITY_ENT_ORDERED(fld, match, body) FOREACH_ENTITY_ORDERED(it.fld == match, body)
158 .entity _FOREACH_ENTITY_ENT_next;
159 noref string _FOREACH_ENTITY_ENT_mutex;
160 #define FOREACH_ENTITY_ENT_UNORDERED(fld, match, body) \
162 if (_FOREACH_ENTITY_ENT_mutex) LOG_SEVEREF("Loop mutex held by %s", _FOREACH_ENTITY_ENT_mutex); \
163 _FOREACH_ENTITY_ENT_mutex = __FUNC__; \
164 entity _foundchain_first = _findchainentity_tofield(fld, match, _FOREACH_ENTITY_ENT_next); \
165 FOREACH_LIST(_foundchain, _FOREACH_ENTITY_ENT_next, true, body); \
166 _FOREACH_ENTITY_ENT_mutex = string_null; \
171 #define FOREACH_ENTITY_STRING(fld, match, body) ORDERED(FOREACH_ENTITY_STRING)(fld, match, body)
172 #define FOREACH_ENTITY_STRING_ORDERED(fld, match, body) FOREACH_ENTITY_ORDERED(it.fld == match, body)
173 .entity _FOREACH_ENTITY_STRING_next;
174 noref string _FOREACH_ENTITY_STRING_mutex;
175 #define FOREACH_ENTITY_STRING_UNORDERED(fld, match, body) \
177 if (_FOREACH_ENTITY_STRING_mutex) LOG_SEVEREF("Loop mutex held by %s", _FOREACH_ENTITY_STRING_mutex); \
178 _FOREACH_ENTITY_STRING_mutex = __FUNC__; \
179 entity _foundchain_first = _findchainstring_tofield(fld, match, _FOREACH_ENTITY_STRING_next); \
180 FOREACH_LIST(_foundchain, _FOREACH_ENTITY_STRING_next, true, body); \
181 _FOREACH_ENTITY_STRING_mutex = string_null; \