X-Git-Url: https://de.git.xonotic.org/?a=blobdiff_plain;f=qcsrc%2Flib%2Fspawnfunc.qh;h=f1eb477a00195188c942ca3f7d5ce6b889343d1a;hb=58789ad3f9182d98bece66fb062b4fda6ff00563;hp=75f56a23d01fbaafb70140067e53088670b5748a;hpb=1e31381058a302273d1a3f907f1dd72885845b27;p=xonotic%2Fxonotic-data.pk3dir.git diff --git a/qcsrc/lib/spawnfunc.qh b/qcsrc/lib/spawnfunc.qh index 75f56a23d..f1eb477a0 100644 --- a/qcsrc/lib/spawnfunc.qh +++ b/qcsrc/lib/spawnfunc.qh @@ -1,5 +1,4 @@ -#ifndef SPAWNFUNC_H -#define SPAWNFUNC_H +#pragma once /** If this global exists, only functions with spawnfunc_ name prefix qualify as spawn functions */ noref bool require_spawnfunc_prefix; @@ -21,30 +20,92 @@ noref bool require_spawnfunc_prefix; #define _spawnfunc_checktypes(fld) \ if (fieldname == #fld) \ - if (!entityfieldassignablefromeditor(i)) LOG_FATALF("Entity field '%s' cannot be whitelisted\n", fieldname); + if (!entityfieldassignablefromeditor(i)) LOG_FATALF("Entity field '%s' cannot be whitelisted", fieldname); #else #define _spawnfunc_checktypes(fld) #endif #define _spawnfunc_check(fld) \ if (fieldname == #fld) continue; - bool __spawnfunc_unreachable_workaround = true; + noref int __spawnfunc_expecting; + noref entity __spawnfunc_expect; + noref bool __spawnfunc_unreachable_workaround = true; + + .void(entity) __spawnfunc_constructor; + noref IntrusiveList g_spawn_queue; + + #define SPAWNFUNC_INTERNAL_FIELDS(X) \ + X(string, classname, "spawnfunc") \ + X(string, targetname, string_null) \ + /**/ + + #define X(T, fld, def) .T fld, __spawnfunc_##fld; + SPAWNFUNC_INTERNAL_FIELDS(X) + #undef X + + void __spawnfunc_defer(entity prototype, void(entity) constructor) + { + IL_PUSH(g_spawn_queue, prototype); + #define X(T, fld, def) { prototype.__spawnfunc_##fld = prototype.fld; prototype.fld = def; } + SPAWNFUNC_INTERNAL_FIELDS(X); + #undef X + prototype.__spawnfunc_constructor = constructor; + } + + noref IntrusiveList g_map_entities; + #define __spawnfunc_spawn_all() MACRO_BEGIN \ + g_map_entities = IL_NEW(); \ + IL_EACH(g_spawn_queue, true, __spawnfunc_spawn(it)); \ + MACRO_END +#ifdef SVQC + void _SV_OnEntityPreSpawnFunction(entity this); +#endif + void __spawnfunc_spawn(entity prototype) + { + entity e = new(clone); + copyentity(prototype, e); + IL_PUSH(g_map_entities, e); + #define X(T, fld, def) { e.fld = e.__spawnfunc_##fld; e.__spawnfunc_##fld = def; } + SPAWNFUNC_INTERNAL_FIELDS(X); + #undef X +#ifdef SVQC + _SV_OnEntityPreSpawnFunction(e); + if (wasfreed(e)) { + return; + } +#endif + e.__spawnfunc_constructor(e); + } + + noref bool __spawnfunc_first; #define spawnfunc_1(id) spawnfunc_2(id, FIELDS_UNION) #define spawnfunc_2(id, whitelist) \ void __spawnfunc_##id(entity this); \ [[accumulate]] void spawnfunc_##id(entity this) \ { \ - this = self; \ - if (!this.sourceLocFile) \ - { \ - this.sourceLocFile = __FILE__; \ - this.sourceLocLine = __LINE__; \ + if (!__spawnfunc_first) { \ + __spawnfunc_first = true; \ + static_init_early(); \ + } \ + bool dospawn = true; \ + if (__spawnfunc_expecting > 1) { __spawnfunc_expecting = false; } \ + else if (__spawnfunc_expecting) { \ + /* engine call */ \ + if (!g_spawn_queue) { g_spawn_queue = IL_NEW(); } \ + __spawnfunc_expecting = false; \ + this = __spawnfunc_expect; \ + __spawnfunc_expect = NULL; \ + dospawn = false; \ + } else { \ + /* userland call */ \ + assert(this); \ + } \ + if (!this.sourceLoc) { \ + this.sourceLoc = __FILE__ ":" STR(__LINE__); \ } \ - if (!this.spawnfunc_checked) \ - { \ - for (int i = 0, n = numentityfields(); i < n; ++i) \ - { \ + if (!this.spawnfunc_checked) { \ + for (int i = 0, n = numentityfields(); i < n; ++i) { \ string value = getentityfieldstring(i, this); \ string fieldname = entityfieldname(i); \ whitelist(_spawnfunc_checktypes) \ @@ -52,11 +113,18 @@ noref bool require_spawnfunc_prefix; if (fieldname == "") continue; \ FIELDS_COMMON(_spawnfunc_check) \ whitelist(_spawnfunc_check) \ - LOG_WARNINGF(_("Entity field %s.%s (%s) is not whitelisted. If you believe this is an error, please file an issue.\n"), #id, fieldname, value); \ + LOG_WARNF(_("Entity field %s.%s (%s) is not whitelisted. If you believe this is an error, please file an issue."), #id, fieldname, value); \ } \ this.spawnfunc_checked = true; \ + if (this) { \ + /* not worldspawn, delay spawn */ \ + __spawnfunc_defer(this, __spawnfunc_##id); \ + } else { \ + /* world might not be "worldspawn" */ \ + this.__spawnfunc_constructor = __spawnfunc_##id; \ + } \ } \ - __spawnfunc_##id(this); \ + if (dospawn) { __spawnfunc_##id(this); } \ if (__spawnfunc_unreachable_workaround) return; \ } \ void __spawnfunc_##id(entity this) @@ -74,12 +142,12 @@ noref bool require_spawnfunc_prefix; #define FIELDS_COMMON(fld) \ FIELD_SCALAR(fld, classname) \ + FIELD_SCALAR(fld, sourceLoc) \ FIELD_SCALAR(fld, spawnfunc_checked) \ + FIELD_VEC(fld, origin) \ /**/ #define FIELDS_UNION(fld) \ - FIELD_SCALAR(fld, sourceLocFile) \ - FIELD_SCALAR(fld, sourceLocLine) \ FIELD_SCALAR(fld, Version) \ FIELD_SCALAR(fld, ammo_cells) \ FIELD_SCALAR(fld, ammo_nails) \ @@ -124,14 +192,18 @@ noref bool require_spawnfunc_prefix; FIELD_SCALAR(fld, loddistance1) \ FIELD_SCALAR(fld, lodmodel1) \ FIELD_SCALAR(fld, ltime) \ + FIELD_SCALAR(fld, map) \ FIELD_SCALAR(fld, mdl) \ FIELD_SCALAR(fld, message2) \ FIELD_SCALAR(fld, message) \ FIELD_SCALAR(fld, modelindex) \ FIELD_SCALAR(fld, modelscale) \ FIELD_SCALAR(fld, model) \ + FIELD_SCALAR(fld, monsterid) \ FIELD_SCALAR(fld, monster_moveflags) \ + FIELD_SCALAR(fld, monster_name) \ FIELD_SCALAR(fld, movetype) \ + FIELD_SCALAR(fld, move_movetype) \ FIELD_SCALAR(fld, netname) \ FIELD_SCALAR(fld, nextthink) \ FIELD_SCALAR(fld, noalign) \ @@ -142,6 +214,7 @@ noref bool require_spawnfunc_prefix; FIELD_SCALAR(fld, platmovetype) \ FIELD_SCALAR(fld, race_place) \ FIELD_SCALAR(fld, radius) \ + FIELD_SCALAR(fld, respawntimestart) \ FIELD_SCALAR(fld, respawntimejitter) \ FIELD_SCALAR(fld, respawntime) \ FIELD_SCALAR(fld, restriction) \ @@ -151,8 +224,10 @@ noref bool require_spawnfunc_prefix; FIELD_SCALAR(fld, sound1) \ FIELD_SCALAR(fld, sounds) \ FIELD_SCALAR(fld, spawnflags) \ + FIELD_SCALAR(fld, spawnmob) \ FIELD_SCALAR(fld, speed) \ FIELD_SCALAR(fld, strength) \ + FIELD_SCALAR(fld, style) \ FIELD_SCALAR(fld, target2) \ FIELD_SCALAR(fld, target3) \ FIELD_SCALAR(fld, target4) \ @@ -169,18 +244,19 @@ noref bool require_spawnfunc_prefix; FIELD_SCALAR(fld, warpzone_fadeend) \ FIELD_SCALAR(fld, warpzone_fadestart) \ FIELD_SCALAR(fld, weapon) \ + FIELD_SCALAR(fld, worldtype) \ FIELD_VEC(fld, absmax) \ FIELD_VEC(fld, absmin) \ FIELD_VEC(fld, angles) \ FIELD_VEC(fld, avelocity) \ + FIELD_VEC(fld, color) \ + FIELD_VEC(fld, mangle) \ FIELD_VEC(fld, maxs) \ FIELD_VEC(fld, maxs) \ FIELD_VEC(fld, mins) \ FIELD_VEC(fld, modelscale_vec) \ - FIELD_VEC(fld, origin) \ FIELD_VEC(fld, velocity) \ /**/ - #define spawnfunc(...) EVAL(OVERLOAD(spawnfunc, __VA_ARGS__)) - -#endif + #define spawnfunc(...) EVAL_spawnfunc(OVERLOAD(spawnfunc, __VA_ARGS__)) + #define EVAL_spawnfunc(...) __VA_ARGS__