4 #define ITER_CONST const
9 #define FOREACH_ARRAY(arr, start, end, cond, body) \
11 for (int _i = start; _i < end; ++_i) \
13 const noref int i = _i; \
14 ITER_CONST noref entity it = arr[i]; \
15 if (cond) { LAMBDA(body) } \
19 #define FOREACH(list, cond, body) FOREACH_LIST(list, enemy, cond, body)
21 #define FOREACH_LIST(list, next, cond, body) \
24 for (entity _it = list##_first, _next = NULL; _it; (_it = _next, ++_i)) \
26 const noref int i = _i; \
27 ITER_CONST noref entity it = _it; \
29 if (cond) { LAMBDA(body) } \
33 #define FOREACH_WORD(words, cond, body) \
35 string _words = words; \
37 for (string _it; (_it = car(_words)); (_words = cdr(_words), ++_i)) \
39 const noref int i = _i; \
40 const noref string it = _it; \
41 if (cond) { LAMBDA(body) } \
45 #define STRING_ITERATOR(this, s, i) \
46 string this##_s = s; \
49 #define STRING_ITERATOR_SET(this, s, i) \
55 #define STRING_ITERATOR_GET(this) str2chr(this##_s, this##_i++)
56 #define STRING_ITERATOR_PEEK(this) str2chr(this##_s, this##_i)
57 #define STRING_ITERATOR_NEXT(this) MACRO_BEGIN ++this##_i; MACRO_END
58 #define STRING_ITERATOR_UNGET(this) MACRO_BEGIN --this##_i; MACRO_END
59 #define STRING_ITERATOR_SAVE(this) this##_i
60 #define STRING_ITERATOR_LOAD(this, n) MACRO_BEGIN this##_i = n; MACRO_END
62 #define FOREACH_CHAR(s, cond, body) \
64 STRING_ITERATOR(iter, s, 0); \
66 while ((_it = STRING_ITERATOR_GET(iter)) > 0) \
68 const noref int it = _it; \
69 if (cond) { LAMBDA(body) } \
74 entity(entity start, .string fld, string match) _findstring = #18;
75 entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #402;
77 entity(entity start, .entity fld, entity match) _findentity = #98;
78 entity(.entity fld, entity match, .entity tofield) _findchainentity_tofield = #403;
80 entity(entity start, .float fld, float match) _findfloat = #98;
81 entity(.float fld, float match, .entity tofield) _findchainfloat_tofield = #403;
83 entity(entity start, .float fld, float match) _findflags = #449;
84 entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #450;
86 entity(entity start, .string fld, string match) _findstring = #18;
87 entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #402;
89 entity(entity start, .entity fld, entity match) _findentity = #98;
90 entity(.entity fld, entity match, .entity tofield) _findchainentity_tofield = #403;
92 entity(entity start, .float fld, float match) _findfloat = #98;
93 entity(.float fld, float match, .entity tofield) _findchainfloat_tofield = #403;
95 entity(entity start, .float fld, float match) _findflags = #449;
96 entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #450;
98 entity(entity start, .string fld, string match) _findstring = #24;
99 entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #26;
101 entity(entity start, .entity fld, entity match) _findentity = #25;
102 entity(.entity fld, entity match, .entity tofield) _findchainentity_tofield = #27;
104 entity(entity start, .float fld, float match) _findfloat = #25;
105 entity(.float fld, float match, .entity tofield) _findchainfloat_tofield = #27;
107 entity(entity start, .float fld, float match) _findflags = #87;
108 entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #88;
111 #define ORDERED(F) F##_UNORDERED
112 #define _FOREACH_ENTITY_FIND_ORDERED(T, fld, match, cond, body) \
115 for (entity _it = NULL; (_it = _find##T(_it, fld, match)); ++_i) \
117 const noref int i = _i; \
118 ITER_CONST noref entity it = _it; \
119 if (cond) LAMBDA(body) \
122 #define MUTEX_LOCK(this) MACRO_BEGIN \
123 if (this) LOG_SEVEREF("Loop mutex held by %s", this); \
126 #define MUTEX_UNLOCK(this) MACRO_BEGIN \
127 this = string_null; \
129 #define _FOREACH_ENTITY_FIND_UNORDERED(id, T, fld, match, cond, body) \
131 MUTEX_LOCK(_FOREACH_ENTITY_FIND_##T##_##id##mutex); \
132 entity _foundchain_first = _findchain##T##_tofield(fld, match, _FOREACH_ENTITY_FIND_##T##_next##id); \
133 FOREACH_LIST(_foundchain, _FOREACH_ENTITY_FIND_##T##_next##id, cond, body); \
134 MUTEX_UNLOCK(_FOREACH_ENTITY_FIND_##T##_##id##mutex); \
137 #define FOREACH_ENTITY(cond, body) ORDERED(FOREACH_ENTITY)(cond, body)
138 #define FOREACH_ENTITY_ORDERED(cond, body) \
141 for (entity _it = NULL; (_it = nextent(_it)); ++_i) \
143 const noref int i = _i; \
144 ITER_CONST noref entity it = _it; \
145 if (cond) LAMBDA(body) \
148 /** marker field, always NULL */
149 .entity _FOREACH_ENTITY_fld;
150 .entity _FOREACH_ENTITY_FIND_entity_nextall; noref string _FOREACH_ENTITY_FIND_entity_allmutex;
151 #define FOREACH_ENTITY_UNORDERED(cond, body) _FOREACH_ENTITY_FIND_UNORDERED(all, entity, _FOREACH_ENTITY_fld, NULL, cond, body)
153 #define FOREACH_ENTITY_FLAGS(fld, match, body) ORDERED(FOREACH_ENTITY_FLAGS)(fld, match, body)
154 #define FOREACH_ENTITY_FLAGS_ORDERED(fld, match, body) _FOREACH_ENTITY_FIND_ORDERED(flags, fld, match, true, body)
155 .entity _FOREACH_ENTITY_FIND_flags_next; noref string _FOREACH_ENTITY_FIND_flags_mutex;
156 #define FOREACH_ENTITY_FLAGS_UNORDERED(fld, match, body) _FOREACH_ENTITY_FIND_UNORDERED(, flags, fld, match, true, body)
159 entity(vector org, float rad, .entity tofield) _findchainradius_tofield = #22;
160 #define FOREACH_ENTITY_RADIUS(org, dist, cond, body) ORDERED(FOREACH_ENTITY_RADIUS)(org, dist, cond, body)
161 .entity _FOREACH_ENTITY_FIND_radius_next; noref string _FOREACH_ENTITY_FIND_radius_mutex;
162 #define FOREACH_ENTITY_RADIUS_UNORDERED(org, dist, cond, body) _FOREACH_ENTITY_FIND_UNORDERED(, radius, org, dist, cond, body)
163 .entity _FOREACH_ENTITY_FIND_radius_nexttmp; noref string _FOREACH_ENTITY_FIND_radius_tmpmutex;
164 #define FOREACH_ENTITY_RADIUS_ORDERED(org, dist, cond, body) \
166 entity _rev_first = NULL; \
167 _FOREACH_ENTITY_FIND_UNORDERED(tmp, radius, org, dist, cond, (it._FOREACH_ENTITY_FIND_radius_nexttmp = _rev_first, _rev_first = it)); \
168 MUTEX_LOCK(_FOREACH_ENTITY_FIND_radius_tmpmutex); \
169 FOREACH_LIST(_rev, _FOREACH_ENTITY_FIND_radius_nexttmp, true, body); \
170 MUTEX_UNLOCK(_FOREACH_ENTITY_FIND_radius_tmpmutex); \
174 #define FOREACH_ENTITY_FLOAT(fld, match, body) ORDERED(FOREACH_ENTITY_FLOAT)(fld, match, body)
175 #define FOREACH_ENTITY_FLOAT_ORDERED(fld, match, body) _FOREACH_ENTITY_FIND_ORDERED(float, fld, match, true, body)
176 .entity _FOREACH_ENTITY_FIND_float_next; noref string _FOREACH_ENTITY_FIND_float_mutex;
177 #define FOREACH_ENTITY_FLOAT_UNORDERED(fld, match, body) _FOREACH_ENTITY_FIND_UNORDERED(, float, fld, match, true, body)
179 #define FOREACH_ENTITY_ENT(fld, match, body) ORDERED(FOREACH_ENTITY_ENT)(fld, match, body)
180 #define FOREACH_ENTITY_ENT_ORDERED(fld, match, body) _FOREACH_ENTITY_FIND_ORDERED(entity, fld, match, true, body)
181 .entity _FOREACH_ENTITY_FIND_entity_next; noref string _FOREACH_ENTITY_FIND_entity_mutex;
182 #define FOREACH_ENTITY_ENT_UNORDERED(fld, match, body) _FOREACH_ENTITY_FIND_UNORDERED(, entity, fld, match, true, body)
184 #define FOREACH_ENTITY_STRING(fld, match, body) ORDERED(FOREACH_ENTITY_STRING)(fld, match, body)
185 #define FOREACH_ENTITY_STRING_ORDERED(fld, match, body) _FOREACH_ENTITY_FIND_ORDERED(string, fld, match, true, body)
186 .entity _FOREACH_ENTITY_FIND_string_next; noref string _FOREACH_ENTITY_FIND_string_mutex;
187 #define FOREACH_ENTITY_STRING_UNORDERED(fld, match, body) _FOREACH_ENTITY_FIND_UNORDERED(, string, fld, match, true, body)
189 #define FOREACH_ENTITY_CLASS(class, cond, body) ORDERED(FOREACH_ENTITY_CLASS)(class, cond, body)
190 #define FOREACH_ENTITY_CLASS_ORDERED(class, cond, body) _FOREACH_ENTITY_FIND_ORDERED(string, classname, class, cond, body)
191 .entity _FOREACH_ENTITY_FIND_string_nextclazz; noref string _FOREACH_ENTITY_FIND_string_clazzmutex;
192 #define FOREACH_ENTITY_CLASS_UNORDERED(class, cond, body) _FOREACH_ENTITY_FIND_UNORDERED(clazz, string, classname, class, cond, body)