]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/iter.qh
Move MOVETYPE_FLY to ecs
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / iter.qh
1 #pragma once
2
3 #if 1
4 #define ITER_CONST const
5 #else
6 #define ITER_CONST
7 #endif
8
9 #define FOREACH_ARRAY(arr, start, end, cond, body) \
10         MACRO_BEGIN \
11         { \
12                 for (int _i = start; _i < end; ++_i) \
13                 { \
14                         const noref int i = _i; \
15                         ITER_CONST noref entity it = arr[i]; \
16                         if (cond) { LAMBDA(body) } \
17                 } \
18         } MACRO_END
19
20 #define FOREACH(list, cond, body) FOREACH_LIST(list, enemy, cond, body)
21
22 #define FOREACH_LIST(list, next, cond, body) \
23         MACRO_BEGIN \
24         { \
25                 int _i = 0; \
26                 for (entity _it = list##_first; _it; (_it = _it.next, ++_i)) \
27                 { \
28                         const noref int i = _i; \
29                         ITER_CONST noref entity it = _it; \
30                         if (cond) { LAMBDA(body) } \
31                 } \
32         } MACRO_END
33
34 #define FOREACH_WORD(words, cond, body) \
35         MACRO_BEGIN \
36         { \
37                 string _words = words; \
38                 int _i = 0; \
39                 for (string _it; (_it = car(_words)); (_words = cdr(_words), ++_i)) \
40                 { \
41                         const noref int i = _i; \
42                         const noref string it = _it; \
43                         if (cond) { LAMBDA(body) } \
44                 } \
45         } MACRO_END
46
47 #define STRING_ITERATOR(this, s, i) \
48         string this##_s = s; \
49         int this##_i = i
50
51 #define STRING_ITERATOR_SET(this, s, i) \
52         MACRO_BEGIN { \
53                 this##_s = s; \
54                 this##_i = i; \
55         } MACRO_END
56
57 #define STRING_ITERATOR_GET(this) str2chr(this##_s, this##_i++)
58 #define STRING_ITERATOR_PEEK(this) str2chr(this##_s, this##_i)
59 #define STRING_ITERATOR_NEXT(this) MACRO_BEGIN ++this##_i; MACRO_END
60 #define STRING_ITERATOR_UNGET(this) MACRO_BEGIN --this##_i; MACRO_END
61 #define STRING_ITERATOR_SAVE(this) this##_i
62 #define STRING_ITERATOR_LOAD(this, n) MACRO_BEGIN this##_i = n; MACRO_END
63
64 #define FOREACH_CHAR(s, cond, body) \
65         MACRO_BEGIN \
66         { \
67                 STRING_ITERATOR(iter, s, 0); \
68                 int _it; \
69                 while ((_it = STRING_ITERATOR_GET(iter)) > 0) \
70                 { \
71                         const noref int it = _it; \
72                         if (cond) { LAMBDA(body) } \
73                 } \
74         } MACRO_END
75
76 #if defined(CSQC)
77     entity(entity start, .string fld, string match) _findstring = #18;
78         entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #402;
79
80     entity(entity start, .entity fld, entity match) _findentity = #98;
81         entity(.entity fld, entity match, .entity tofield) _findchainentity_tofield = #403;
82
83     entity(entity start, .float fld, float match) _findfloat = #98;
84         entity(.float fld, float match, .entity tofield) _findchainfloat_tofield = #403;
85
86         entity(entity start, .float fld, float match) _findflags = #449;
87         entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #450;
88 #elif defined(SVQC)
89     entity(entity start, .string fld, string match) _findstring = #18;
90         entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #402;
91
92     entity(entity start, .entity fld, entity match) _findentity = #98;
93         entity(.entity fld, entity match, .entity tofield) _findchainentity_tofield = #403;
94
95     entity(entity start, .float fld, float match) _findfloat = #98;
96         entity(.float fld, float match, .entity tofield) _findchainfloat_tofield = #403;
97
98         entity(entity start, .float fld, float match) _findflags = #449;
99         entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #450;
100 #elif defined(MENUQC)
101     entity(entity start, .string fld, string match) _findstring = #24;
102         entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #26;
103
104     entity(entity start, .entity fld, entity match) _findentity = #25;
105         entity(.entity fld, entity match, .entity tofield) _findchainentity_tofield = #27;
106
107     entity(entity start, .float fld, float match) _findfloat = #25;
108         entity(.float fld, float match, .entity tofield) _findchainfloat_tofield = #27;
109
110         entity(entity start, .float fld, float match) _findflags = #87;
111         entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #88;
112 #endif
113
114 #define ORDERED(F) F##_UNORDERED
115 #define _FOREACH_ENTITY_FIND_ORDERED(T, fld, match, cond, body) \
116     MACRO_BEGIN { \
117         int _i = 0; \
118         for (entity _it = NULL; (_it = _find##T(_it, fld, match)); ++_i) \
119         { \
120             const noref int i = _i; \
121             ITER_CONST noref entity it = _it; \
122             if (cond) LAMBDA(body) \
123         } \
124     } MACRO_END
125 #define MUTEX_LOCK(this) MACRO_BEGIN \
126         if (this) LOG_SEVEREF("Loop mutex held by %s", this); \
127         this = __FUNC__; \
128 MACRO_END
129 #define MUTEX_UNLOCK(this) MACRO_BEGIN \
130         this = string_null; \
131 MACRO_END
132 #define _FOREACH_ENTITY_FIND_UNORDERED(id, T, fld, match, cond, body) \
133         MACRO_BEGIN { \
134                 MUTEX_LOCK(_FOREACH_ENTITY_FIND_##T##_##id##mutex); \
135                 entity _foundchain_first = _findchain##T##_tofield(fld, match, _FOREACH_ENTITY_FIND_##T##_next##id); \
136                 FOREACH_LIST(_foundchain, _FOREACH_ENTITY_FIND_##T##_next##id, cond, body); \
137                 MUTEX_UNLOCK(_FOREACH_ENTITY_FIND_##T##_##id##mutex); \
138         } MACRO_END
139
140 #define FOREACH_ENTITY(cond, body) ORDERED(FOREACH_ENTITY)(cond, body)
141 #define FOREACH_ENTITY_ORDERED(cond, body) \
142         MACRO_BEGIN { \
143                 int _i = 0; \
144                 for (entity _it = NULL; (_it = nextent(_it)); ++_i) \
145                 { \
146                         const noref int i = _i; \
147                         ITER_CONST noref entity it = _it; \
148                         if (cond) LAMBDA(body) \
149                 } \
150         } MACRO_END
151 /** marker field, always NULL */
152 .entity _FOREACH_ENTITY_fld;
153 .entity _FOREACH_ENTITY_FIND_entity_nextall; noref string _FOREACH_ENTITY_FIND_entity_allmutex;
154 #define FOREACH_ENTITY_UNORDERED(cond, body) _FOREACH_ENTITY_FIND_UNORDERED(all, entity, _FOREACH_ENTITY_fld, NULL, cond, body)
155
156 #define FOREACH_ENTITY_FLAGS(fld, match, body) ORDERED(FOREACH_ENTITY_FLAGS)(fld, match, body)
157 #define FOREACH_ENTITY_FLAGS_ORDERED(fld, match, body) _FOREACH_ENTITY_FIND_ORDERED(flags, fld, match, true, body)
158 .entity _FOREACH_ENTITY_FIND_flags_next; noref string _FOREACH_ENTITY_FIND_flags_mutex;
159 #define FOREACH_ENTITY_FLAGS_UNORDERED(fld, match, body) _FOREACH_ENTITY_FIND_UNORDERED(, flags, fld, match, true, body)
160
161 #ifndef MENUQC
162 entity(vector org, float rad, .entity tofield) _findchainradius_tofield = #22;
163 #define FOREACH_ENTITY_RADIUS(org, dist, cond, body) ORDERED(FOREACH_ENTITY_RADIUS)(org, dist, cond, body)
164 .entity _FOREACH_ENTITY_FIND_radius_next; noref string _FOREACH_ENTITY_FIND_radius_mutex;
165 #define FOREACH_ENTITY_RADIUS_UNORDERED(org, dist, cond, body) _FOREACH_ENTITY_FIND_UNORDERED(, radius, org, dist, cond, body)
166 .entity _FOREACH_ENTITY_FIND_radius_nexttmp; noref string _FOREACH_ENTITY_FIND_radius_tmpmutex;
167 #define FOREACH_ENTITY_RADIUS_ORDERED(org, dist, cond, body) \
168 MACRO_BEGIN \
169         entity _rev_first = NULL; \
170         _FOREACH_ENTITY_FIND_UNORDERED(tmp, radius, org, dist, cond, (it._FOREACH_ENTITY_FIND_radius_nexttmp = _rev_first, _rev_first = it)); \
171         MUTEX_LOCK(_FOREACH_ENTITY_FIND_radius_tmpmutex); \
172         FOREACH_LIST(_rev, _FOREACH_ENTITY_FIND_radius_nexttmp, true, body); \
173         MUTEX_UNLOCK(_FOREACH_ENTITY_FIND_radius_tmpmutex); \
174 MACRO_END
175 #endif
176
177 #define FOREACH_ENTITY_FLOAT(fld, match, body) ORDERED(FOREACH_ENTITY_FLOAT)(fld, match, body)
178 #define FOREACH_ENTITY_FLOAT_ORDERED(fld, match, body) _FOREACH_ENTITY_FIND_ORDERED(float, fld, match, true, body)
179 .entity _FOREACH_ENTITY_FIND_float_next; noref string _FOREACH_ENTITY_FIND_float_mutex;
180 #define FOREACH_ENTITY_FLOAT_UNORDERED(fld, match, body) _FOREACH_ENTITY_FIND_UNORDERED(, float, fld, match, true, body)
181
182 #define FOREACH_ENTITY_ENT(fld, match, body) ORDERED(FOREACH_ENTITY_ENT)(fld, match, body)
183 #define FOREACH_ENTITY_ENT_ORDERED(fld, match, body) _FOREACH_ENTITY_FIND_ORDERED(entity, fld, match, true, body)
184 .entity _FOREACH_ENTITY_FIND_entity_next; noref string _FOREACH_ENTITY_FIND_entity_mutex;
185 #define FOREACH_ENTITY_ENT_UNORDERED(fld, match, body) _FOREACH_ENTITY_FIND_UNORDERED(, entity, fld, match, true, body)
186
187 #define FOREACH_ENTITY_STRING(fld, match, body) ORDERED(FOREACH_ENTITY_STRING)(fld, match, body)
188 #define FOREACH_ENTITY_STRING_ORDERED(fld, match, body) _FOREACH_ENTITY_FIND_ORDERED(string, fld, match, true, body)
189 .entity _FOREACH_ENTITY_FIND_string_next; noref string _FOREACH_ENTITY_FIND_string_mutex;
190 #define FOREACH_ENTITY_STRING_UNORDERED(fld, match, body) _FOREACH_ENTITY_FIND_UNORDERED(, string, fld, match, true, body)
191
192 #define FOREACH_ENTITY_CLASS(class, cond, body) ORDERED(FOREACH_ENTITY_CLASS)(class, cond, body)
193 #define FOREACH_ENTITY_CLASS_ORDERED(class, cond, body) _FOREACH_ENTITY_FIND_ORDERED(string, classname, class, cond, body)
194 .entity _FOREACH_ENTITY_FIND_string_nextclazz; noref string _FOREACH_ENTITY_FIND_string_clazzmutex;
195 #define FOREACH_ENTITY_CLASS_UNORDERED(class, cond, body) _FOREACH_ENTITY_FIND_UNORDERED(clazz, string, classname, class, cond, body)