3 /** If this global exists, only functions with spawnfunc_ name prefix qualify as spawn functions */
4 noref bool require_spawnfunc_prefix;
5 .bool spawnfunc_checked;
7 // Optional type checking; increases compile time too much to be enabled by default
9 bool entityfieldassignablefromeditor(int i)
11 switch (entityfieldtype(i))
21 #define _spawnfunc_checktypes(fld) \
22 if (fieldname == #fld) \
23 if (!entityfieldassignablefromeditor(i)) LOG_FATALF("Entity field '%s' cannot be whitelisted", fieldname);
25 #define _spawnfunc_checktypes(fld)
27 #define _spawnfunc_check(fld) \
28 if (fieldname == #fld) continue;
30 noref int __spawnfunc_expecting;
31 noref entity __spawnfunc_expect;
32 noref bool __spawnfunc_unreachable_workaround = true;
34 .void(entity) __spawnfunc_constructor;
35 noref IntrusiveList g_spawn_queue;
36 .string targetname, __spawnfunc_targetname;
37 #define SPAWNFUNC_INTERNAL_FIELDS(X) \
38 X(targetname, string_null) \
40 void __spawnfunc_defer(entity prototype, void(entity) constructor)
42 IL_PUSH(g_spawn_queue, prototype);
43 #define X(fld, def) { prototype.__spawnfunc_##fld = prototype.fld; prototype.fld = def; }
44 SPAWNFUNC_INTERNAL_FIELDS(X);
46 prototype.__spawnfunc_constructor = constructor;
49 noref IntrusiveList g_map_entities;
50 #define __spawnfunc_spawn_all() MACRO_BEGIN \
51 g_map_entities = IL_NEW(); \
52 IL_EACH(g_spawn_queue, true, __spawnfunc_spawn(it)); \
55 void __spawnfunc_spawn(entity prototype)
57 entity e = new(clone);
58 copyentity(prototype, e);
59 IL_PUSH(g_map_entities, e);
60 #define X(fld, def) { e.fld = e.__spawnfunc_##fld; e.__spawnfunc_##fld = def; }
61 SPAWNFUNC_INTERNAL_FIELDS(X);
63 e.__spawnfunc_constructor(e);
66 #define spawnfunc_1(id) spawnfunc_2(id, FIELDS_UNION)
67 #define spawnfunc_2(id, whitelist) \
68 void __spawnfunc_##id(entity this); \
69 [[accumulate]] void spawnfunc_##id(entity this) \
71 bool dospawn = true; \
72 if (__spawnfunc_expecting > 1) { __spawnfunc_expecting = false; } \
73 else if (__spawnfunc_expecting) { \
75 if (!g_spawn_queue) { g_spawn_queue = IL_NEW(); } \
76 __spawnfunc_expecting = false; \
77 this = __spawnfunc_expect; \
78 __spawnfunc_expect = NULL; \
84 if (!this.sourceLoc) { \
85 this.sourceLoc = __FILE__ ":" STR(__LINE__); \
87 if (!this.spawnfunc_checked) { \
88 for (int i = 0, n = numentityfields(); i < n; ++i) { \
89 string value = getentityfieldstring(i, this); \
90 string fieldname = entityfieldname(i); \
91 whitelist(_spawnfunc_checktypes) \
92 if (value == "") continue; \
93 if (fieldname == "") continue; \
94 FIELDS_COMMON(_spawnfunc_check) \
95 whitelist(_spawnfunc_check) \
96 LOG_WARNF(_("Entity field %s.%s (%s) is not whitelisted. If you believe this is an error, please file an issue."), #id, fieldname, value); \
98 this.spawnfunc_checked = true; \
100 /* not worldspawn, delay spawn */ \
101 __spawnfunc_defer(this, __spawnfunc_##id); \
103 /* world might not be "worldspawn" */ \
104 this.__spawnfunc_constructor = __spawnfunc_##id; \
107 if (dospawn) { __spawnfunc_##id(this); } \
108 if (__spawnfunc_unreachable_workaround) return; \
110 void __spawnfunc_##id(entity this)
112 #define FIELD_SCALAR(fld, n) \
114 #define FIELD_VEC(fld, n) \
120 #define FIELDS_NONE(fld)
121 #define FIELDS_ALL(fld) if (false)
123 #define FIELDS_COMMON(fld) \
124 FIELD_SCALAR(fld, classname) \
125 FIELD_SCALAR(fld, sourceLoc) \
126 FIELD_SCALAR(fld, spawnfunc_checked) \
127 FIELD_VEC(fld, origin) \
130 #define FIELDS_UNION(fld) \
131 FIELD_SCALAR(fld, Version) \
132 FIELD_SCALAR(fld, ammo_cells) \
133 FIELD_SCALAR(fld, ammo_nails) \
134 FIELD_SCALAR(fld, ammo_rockets) \
135 FIELD_SCALAR(fld, armorvalue) \
136 FIELD_SCALAR(fld, atten) \
137 FIELD_SCALAR(fld, bgmscriptdecay) \
138 FIELD_SCALAR(fld, bgmscriptsustain) \
139 FIELD_SCALAR(fld, bgmscript) \
140 FIELD_SCALAR(fld, button0) \
141 FIELD_SCALAR(fld, cnt) \
142 FIELD_SCALAR(fld, colormap) \
143 FIELD_SCALAR(fld, count) \
144 FIELD_SCALAR(fld, curvetarget) \
145 FIELD_SCALAR(fld, cvarfilter) \
146 FIELD_SCALAR(fld, debrisdamageforcescale) \
147 FIELD_SCALAR(fld, debrisfadetime) \
148 FIELD_SCALAR(fld, debristimejitter) \
149 FIELD_SCALAR(fld, debristime) \
150 FIELD_SCALAR(fld, debris) \
151 FIELD_SCALAR(fld, delay) \
152 FIELD_SCALAR(fld, dmgtime) \
153 FIELD_SCALAR(fld, dmg) \
154 FIELD_SCALAR(fld, dmg_edge) \
155 FIELD_SCALAR(fld, dmg_force) \
156 FIELD_SCALAR(fld, dmg_radius) \
157 FIELD_SCALAR(fld, effects) \
158 FIELD_SCALAR(fld, flags) \
159 FIELD_SCALAR(fld, fog) \
160 FIELD_SCALAR(fld, frags) \
161 FIELD_SCALAR(fld, frame) \
162 FIELD_SCALAR(fld, gametypefilter) \
163 FIELD_SCALAR(fld, geomtype) \
164 FIELD_SCALAR(fld, gravity) \
165 FIELD_SCALAR(fld, health) \
166 FIELD_SCALAR(fld, height) \
167 FIELD_SCALAR(fld, impulse) \
168 FIELD_SCALAR(fld, killtarget) \
169 FIELD_SCALAR(fld, lerpfrac) \
170 FIELD_SCALAR(fld, light_lev) \
171 FIELD_SCALAR(fld, lip) \
172 FIELD_SCALAR(fld, loddistance1) \
173 FIELD_SCALAR(fld, lodmodel1) \
174 FIELD_SCALAR(fld, ltime) \
175 FIELD_SCALAR(fld, map) \
176 FIELD_SCALAR(fld, mdl) \
177 FIELD_SCALAR(fld, message2) \
178 FIELD_SCALAR(fld, message) \
179 FIELD_SCALAR(fld, modelindex) \
180 FIELD_SCALAR(fld, modelscale) \
181 FIELD_SCALAR(fld, model) \
182 FIELD_SCALAR(fld, monsterid) \
183 FIELD_SCALAR(fld, monster_moveflags) \
184 FIELD_SCALAR(fld, monster_name) \
185 FIELD_SCALAR(fld, movetype) \
186 FIELD_SCALAR(fld, move_movetype) \
187 FIELD_SCALAR(fld, netname) \
188 FIELD_SCALAR(fld, nextthink) \
189 FIELD_SCALAR(fld, noalign) \
190 FIELD_SCALAR(fld, noise1) \
191 FIELD_SCALAR(fld, noise2) \
192 FIELD_SCALAR(fld, noise) \
193 FIELD_SCALAR(fld, phase) \
194 FIELD_SCALAR(fld, platmovetype) \
195 FIELD_SCALAR(fld, race_place) \
196 FIELD_SCALAR(fld, radius) \
197 FIELD_SCALAR(fld, respawntimestart) \
198 FIELD_SCALAR(fld, respawntimejitter) \
199 FIELD_SCALAR(fld, respawntime) \
200 FIELD_SCALAR(fld, restriction) \
201 FIELD_SCALAR(fld, scale) \
202 FIELD_SCALAR(fld, skin) \
203 FIELD_SCALAR(fld, solid) \
204 FIELD_SCALAR(fld, sound1) \
205 FIELD_SCALAR(fld, sounds) \
206 FIELD_SCALAR(fld, spawnflags) \
207 FIELD_SCALAR(fld, spawnmob) \
208 FIELD_SCALAR(fld, speed) \
209 FIELD_SCALAR(fld, strength) \
210 FIELD_SCALAR(fld, style) \
211 FIELD_SCALAR(fld, target2) \
212 FIELD_SCALAR(fld, target3) \
213 FIELD_SCALAR(fld, target4) \
214 FIELD_SCALAR(fld, targetname) \
215 FIELD_SCALAR(fld, target) \
216 FIELD_SCALAR(fld, target_random) \
217 FIELD_SCALAR(fld, target_range) \
218 FIELD_SCALAR(fld, team) \
219 FIELD_SCALAR(fld, turret_scale_health) \
220 FIELD_SCALAR(fld, turret_scale_range) \
221 FIELD_SCALAR(fld, turret_scale_respawn) \
222 FIELD_SCALAR(fld, volume) \
223 FIELD_SCALAR(fld, wait) \
224 FIELD_SCALAR(fld, warpzone_fadeend) \
225 FIELD_SCALAR(fld, warpzone_fadestart) \
226 FIELD_SCALAR(fld, weapon) \
227 FIELD_SCALAR(fld, worldtype) \
228 FIELD_VEC(fld, absmax) \
229 FIELD_VEC(fld, absmin) \
230 FIELD_VEC(fld, angles) \
231 FIELD_VEC(fld, avelocity) \
232 FIELD_VEC(fld, color) \
233 FIELD_VEC(fld, mangle) \
234 FIELD_VEC(fld, maxs) \
235 FIELD_VEC(fld, maxs) \
236 FIELD_VEC(fld, mins) \
237 FIELD_VEC(fld, modelscale_vec) \
238 FIELD_VEC(fld, velocity) \
241 #define spawnfunc(...) EVAL_spawnfunc(OVERLOAD(spawnfunc, __VA_ARGS__))
242 #define EVAL_spawnfunc(...) __VA_ARGS__