5 #include <common/util.qh>
6 #include <server/defs.qh>
12 // "classname" "target_spawn"
13 // "message" "fieldname value fieldname value ..."
15 // 1 = call the spawn function
16 // 2 = trigger on map load
18 float target_spawn_initialized;
19 .void(entity this) target_spawn_spawnfunc;
20 float target_spawn_spawnfunc_field;
21 .entity target_spawn_activator;
22 .float target_spawn_id;
23 float target_spawn_count;
25 void target_spawn_helper_setmodel(entity this)
27 _setmodel(this, this.model);
30 void target_spawn_helper_setsize(entity this)
32 setsize(this, this.mins, this.maxs);
35 void target_spawn_edit_entity(entity this, entity e, string msg, entity kt, entity t2, entity t3, entity t4, entity act, entity trigger)
37 float i, n, valuefieldpos;
38 string key, value, valuefield, valueoffset, valueoffsetrandom;
42 n = tokenize_console(msg);
44 for(i = 0; i < n-1; i += 2)
51 data.y = FIELD_STRING;
55 data = stov(db_get(TemporaryDB, strcat("/target_spawn/field/", key)));
56 if(data.y == 0) // undefined field, i.e., invalid type
58 LOG_INFO("target_spawn: invalid/unknown entity key ", key, " specified, ignored!\n");
62 if(substring(value, 0, 1) == "$")
64 value = substring(value, 1, strlen(value) - 1);
65 if(substring(value, 0, 1) == "$")
67 // deferred replacement
69 // useful for creating target_spawns with this!
74 valuefieldpos = strstrofs(value, "+", 0);
76 if(valuefieldpos != -1)
78 valueoffset = substring(value, valuefieldpos + 1, strlen(value) - valuefieldpos - 1);
79 value = substring(value, 0, valuefieldpos);
82 valuefieldpos = strstrofs(valueoffset, "+", 0);
83 valueoffsetrandom = "";
84 if(valuefieldpos != -1)
86 valueoffsetrandom = substring(valueoffset, valuefieldpos + 1, strlen(valueoffset) - valuefieldpos - 1);
87 valueoffset = substring(valueoffset, 0, valuefieldpos);
90 valuefieldpos = strstrofs(value, ".", 0);
92 if(valuefieldpos != -1)
94 valuefield = substring(value, valuefieldpos + 1, strlen(value) - valuefieldpos - 1);
95 value = substring(value, 0, valuefieldpos);
103 else if(value == "activator")
108 else if(value == "other")
113 else if(value == "pusher")
115 if(time < act.pushltime)
116 valueent = act.pusher;
121 else if(value == "target")
126 else if(value == "killtarget")
131 else if(value == "target2")
136 else if(value == "target3")
141 else if(value == "target4")
146 else if(value == "time")
153 LOG_INFO("target_spawn: invalid/unknown variable replacement ", value, " specified, ignored!\n");
160 value = ftos(etof(valueent));
166 LOG_INFO("target_spawn: try to get a field of a non-entity, ignored!\n");
169 data2 = stov(db_get(TemporaryDB, strcat("/target_spawn/field/", valuefield)));
170 if(data2_y == 0) // undefined field, i.e., invalid type
172 LOG_INFO("target_spawn: invalid/unknown entity key replacement ", valuefield, " specified, ignored!\n");
175 value = getentityfieldstring(data2_x, valueent);
178 if(valueoffset != "")
183 value = strcat(value, valueoffset);
186 value = ftos(stof(value) + stof(valueoffset));
189 value = vtos(stov(value) + stov(valueoffset));
192 LOG_INFO("target_spawn: only string, float and vector fields can do calculations, calculation ignored!\n");
197 if(valueoffsetrandom != "")
202 value = ftos(stof(value) + random() * stof(valueoffsetrandom));
205 data2 = stov(valueoffsetrandom);
206 value = vtos(stov(value) + random() * data2_x * '1 0 0' + random() * data2_y * '0 1 0' + random() * data2_z * '0 0 1');
209 LOG_INFO("target_spawn: only float and vector fields can do random calculations, calculation ignored!\n");
217 if(substring(value, 0, 1) == "_")
218 value = strcat("target_spawn_helper", value);
219 putentityfieldstring(target_spawn_spawnfunc_field, e, value);
221 e.target_spawn_spawnfunc(e);
223 // We called an external function, so we have to re-tokenize msg.
224 n = tokenize_console(msg);
228 if(data.y == FIELD_VECTOR)
229 value = strreplace("'", "", value); // why?!?
230 putentityfieldstring(data.x, e, value);
235 void target_spawn_useon(entity e, entity this, entity actor, entity trigger)
237 this.target_spawn_activator = actor;
238 target_spawn_edit_entity(
242 find(NULL, targetname, this.killtarget),
243 find(NULL, targetname, this.target2),
244 find(NULL, targetname, this.target3),
245 find(NULL, targetname, this.target4),
251 bool target_spawn_cancreate(entity this)
257 if(c == 0) // no limit?
260 ++c; // increase count to not include MYSELF
261 for(e = NULL; (e = findfloat(e, target_spawn_id, this.target_spawn_id)); --c)
264 // if c now is 0, we have AT LEAST the given count (maybe more), so don't spawn any more
270 void target_spawn_use(entity this, entity actor, entity trigger)
272 if(this.target == "")
275 if(!target_spawn_cancreate(this))
278 e.spawnfunc_checked = true;
279 target_spawn_useon(e, this, actor, trigger);
280 e.target_spawn_id = this.target_spawn_id;
282 else if(this.target == "*activator")
286 target_spawn_useon(actor, this, actor, trigger);
291 FOREACH_ENTITY_STRING(targetname, this.target,
293 target_spawn_useon(it, this, actor, trigger);
298 void target_spawn_spawnfirst(entity this)
300 entity act = this.target_spawn_activator;
301 if(this.spawnflags & 2)
302 target_spawn_use(this, act, NULL);
305 void initialize_field_db()
307 if(!target_spawn_initialized)
314 n = numentityfields();
315 for(i = 0; i < n; ++i)
317 fn = entityfieldname(i);
318 ft = entityfieldtype(i);
319 next = i * '1 0 0' + ft * '0 1 0' + '0 0 1';
320 prev = stov(db_get(TemporaryDB, strcat("/target_spawn/field/", fn)));
323 db_put(TemporaryDB, strcat("/target_spawn/field/", fn), vtos(next));
324 if(fn == "target_spawn_spawnfunc")
325 target_spawn_spawnfunc_field = i;
329 target_spawn_initialized = 1;
333 spawnfunc(target_spawn)
335 initialize_field_db();
336 this.use = target_spawn_use;
337 this.message = strzone(strreplace("'", "\"", this.message));
338 this.target_spawn_id = ++target_spawn_count;
339 InitializeEntity(this, target_spawn_spawnfirst, INITPRIO_LAST);