4 #define ITER_CONST const
9 #define FOREACH_ARRAY(arr, start, end, cond, body) \
12 for (int _i = start; _i < end; ++_i) \
14 const noref int i = _i; \
15 ITER_CONST noref entity it = arr[i]; \
16 if (cond) { LAMBDA(body) } \
20 #define FOREACH(list, cond, body) FOREACH_LIST(list, enemy, cond, body)
22 #define FOREACH_LIST(list, next, cond, body) \
26 for (entity _it = list##_first, _next = NULL; _it; (_it = _next, ++_i)) \
28 const noref int i = _i; \
29 ITER_CONST noref entity it = _it; \
31 if (cond) { LAMBDA(body) } \
35 #define FOREACH_WORD(words, cond, body) \
38 string _words = words; \
40 for (string _it; (_it = car(_words)); (_words = cdr(_words), ++_i)) \
42 const noref int i = _i; \
43 const noref string it = _it; \
44 if (cond) { LAMBDA(body) } \
48 #define STRING_ITERATOR(this, s, i) \
49 string this##_s = s; \
52 #define STRING_ITERATOR_SET(this, s, i) \
58 #define STRING_ITERATOR_GET(this) str2chr(this##_s, this##_i++)
59 #define STRING_ITERATOR_PEEK(this) str2chr(this##_s, this##_i)
60 #define STRING_ITERATOR_NEXT(this) MACRO_BEGIN ++this##_i; MACRO_END
61 #define STRING_ITERATOR_UNGET(this) MACRO_BEGIN --this##_i; MACRO_END
62 #define STRING_ITERATOR_SAVE(this) this##_i
63 #define STRING_ITERATOR_LOAD(this, n) MACRO_BEGIN this##_i = n; MACRO_END
65 #define FOREACH_CHAR(s, cond, body) \
68 STRING_ITERATOR(iter, s, 0); \
70 while ((_it = STRING_ITERATOR_GET(iter)) > 0) \
72 const noref int it = _it; \
73 if (cond) { LAMBDA(body) } \
78 entity(entity start, .string fld, string match) _findstring = #18;
79 entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #402;
81 entity(entity start, .entity fld, entity match) _findentity = #98;
82 entity(.entity fld, entity match, .entity tofield) _findchainentity_tofield = #403;
84 entity(entity start, .float fld, float match) _findfloat = #98;
85 entity(.float fld, float match, .entity tofield) _findchainfloat_tofield = #403;
87 entity(entity start, .float fld, float match) _findflags = #449;
88 entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #450;
90 entity(entity start, .string fld, string match) _findstring = #18;
91 entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #402;
93 entity(entity start, .entity fld, entity match) _findentity = #98;
94 entity(.entity fld, entity match, .entity tofield) _findchainentity_tofield = #403;
96 entity(entity start, .float fld, float match) _findfloat = #98;
97 entity(.float fld, float match, .entity tofield) _findchainfloat_tofield = #403;
99 entity(entity start, .float fld, float match) _findflags = #449;
100 entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #450;
101 #elif defined(MENUQC)
102 entity(entity start, .string fld, string match) _findstring = #24;
103 entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #26;
105 entity(entity start, .entity fld, entity match) _findentity = #25;
106 entity(.entity fld, entity match, .entity tofield) _findchainentity_tofield = #27;
108 entity(entity start, .float fld, float match) _findfloat = #25;
109 entity(.float fld, float match, .entity tofield) _findchainfloat_tofield = #27;
111 entity(entity start, .float fld, float match) _findflags = #87;
112 entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #88;
115 #define ORDERED(F) F##_UNORDERED
116 #define _FOREACH_ENTITY_FIND_ORDERED(T, fld, match, cond, body) \
119 for (entity _it = NULL; (_it = _find##T(_it, fld, match)); ++_i) \
121 const noref int i = _i; \
122 ITER_CONST noref entity it = _it; \
123 if (cond) LAMBDA(body) \
126 #define _FOREACH_ENTITY_FIND_UNORDERED(id, T, fld, match, cond, body) \
128 if (_FOREACH_ENTITY_FIND_##T##_##id##mutex) LOG_SEVEREF("Loop mutex held by %s", _FOREACH_ENTITY_FIND_##T##_##id##mutex); \
129 _FOREACH_ENTITY_FIND_##T##_##id##mutex = __FUNC__; \
130 entity _foundchain_first = _findchain##T##_tofield(fld, match, _FOREACH_ENTITY_FIND_##T##_next##id); \
131 FOREACH_LIST(_foundchain, _FOREACH_ENTITY_FIND_##T##_next##id, cond, body); \
132 _FOREACH_ENTITY_FIND_##T##_##id##mutex = string_null; \
135 #define FOREACH_ENTITY(cond, body) ORDERED(FOREACH_ENTITY)(cond, body)
136 #define FOREACH_ENTITY_ORDERED(cond, body) \
139 for (entity _it = NULL; (_it = nextent(_it)); ++_i) \
141 const noref int i = _i; \
142 ITER_CONST noref entity it = _it; \
143 if (cond) LAMBDA(body) \
146 /** marker field, always NULL */
147 .entity _FOREACH_ENTITY_fld;
148 .entity _FOREACH_ENTITY_FIND_entity_nextall; noref string _FOREACH_ENTITY_FIND_entity_allmutex;
149 #define FOREACH_ENTITY_UNORDERED(cond, body) _FOREACH_ENTITY_FIND_UNORDERED(all, entity, _FOREACH_ENTITY_fld, NULL, cond, body)
151 #define FOREACH_ENTITY_FLAGS(fld, match, body) ORDERED(FOREACH_ENTITY_FLAGS)(fld, match, body)
152 #define FOREACH_ENTITY_FLAGS_ORDERED(fld, match, body) _FOREACH_ENTITY_FIND_ORDERED(flags, fld, match, true, body)
153 .entity _FOREACH_ENTITY_FIND_flags_next; noref string _FOREACH_ENTITY_FIND_flags_mutex;
154 #define FOREACH_ENTITY_FLAGS_UNORDERED(fld, match, body) _FOREACH_ENTITY_FIND_UNORDERED(, flags, fld, match, true, body)
157 entity(vector org, float rad, .entity tofield) _findchainradius_tofield = #22;
158 #define FOREACH_ENTITY_RADIUS(org, dist, cond, body) FOREACH_ENTITY_RADIUS_UNORDERED(org, dist, cond, body)
159 .entity _FOREACH_ENTITY_FIND_radius_next; noref string _FOREACH_ENTITY_FIND_radius_mutex;
160 #define FOREACH_ENTITY_RADIUS_UNORDERED(org, dist, cond, body) _FOREACH_ENTITY_FIND_UNORDERED(, radius, org, dist, cond, body)
163 #define FOREACH_ENTITY_FLOAT(fld, match, body) ORDERED(FOREACH_ENTITY_FLOAT)(fld, match, body)
164 #define FOREACH_ENTITY_FLOAT_ORDERED(fld, match, body) _FOREACH_ENTITY_FIND_ORDERED(float, fld, match, true, body)
165 .entity _FOREACH_ENTITY_FIND_float_next; noref string _FOREACH_ENTITY_FIND_float_mutex;
166 #define FOREACH_ENTITY_FLOAT_UNORDERED(fld, match, body) _FOREACH_ENTITY_FIND_UNORDERED(, float, fld, match, true, body)
168 #define FOREACH_ENTITY_ENT(fld, match, body) ORDERED(FOREACH_ENTITY_ENT)(fld, match, body)
169 #define FOREACH_ENTITY_ENT_ORDERED(fld, match, body) _FOREACH_ENTITY_FIND_ORDERED(entity, fld, match, true, body)
170 .entity _FOREACH_ENTITY_FIND_entity_next; noref string _FOREACH_ENTITY_FIND_entity_mutex;
171 #define FOREACH_ENTITY_ENT_UNORDERED(fld, match, body) _FOREACH_ENTITY_FIND_UNORDERED(, entity, fld, match, true, body)
173 #define FOREACH_ENTITY_STRING(fld, match, body) ORDERED(FOREACH_ENTITY_STRING)(fld, match, body)
174 #define FOREACH_ENTITY_STRING_ORDERED(fld, match, body) _FOREACH_ENTITY_FIND_ORDERED(string, fld, match, true, body)
175 .entity _FOREACH_ENTITY_FIND_string_next; noref string _FOREACH_ENTITY_FIND_string_mutex;
176 #define FOREACH_ENTITY_STRING_UNORDERED(fld, match, body) _FOREACH_ENTITY_FIND_UNORDERED(, string, fld, match, true, body)
178 #define FOREACH_ENTITY_CLASS(class, cond, body) ORDERED(FOREACH_ENTITY_CLASS)(class, cond, body)
179 #define FOREACH_ENTITY_CLASS_ORDERED(class, cond, body) _FOREACH_ENTITY_FIND_ORDERED(string, classname, class, cond, body)
180 .entity _FOREACH_ENTITY_FIND_string_nextclazz; noref string _FOREACH_ENTITY_FIND_string_clazzmutex;
181 #define FOREACH_ENTITY_CLASS_UNORDERED(class, cond, body) _FOREACH_ENTITY_FIND_UNORDERED(clazz, string, classname, class, cond, body)