4 #include "../../../dpdefs/progsdefs.qh"
5 #include "../../../dpdefs/dpextensions.qh"
6 #include "../../util.qh"
7 #include "../../../server/_all.qh"
13 // "classname" "target_spawn"
14 // "message" "fieldname value fieldname value ..."
16 // 1 = call the spawn function
17 // 2 = trigger on map load
19 float target_spawn_initialized;
20 .void() target_spawn_spawnfunc;
21 float target_spawn_spawnfunc_field;
22 .entity target_spawn_activator;
23 .float target_spawn_id;
24 float target_spawn_count;
26 void target_spawn_helper_setmodel()
28 setmodel(self, self.model);
31 void target_spawn_helper_setsize()
33 setsize(self, self.mins, self.maxs);
36 void target_spawn_edit_entity(entity e, string msg, entity kt, entity t2, entity t3, entity t4, entity act)
38 float i, n, valuefieldpos;
39 string key, value, valuefield, valueoffset, valueoffsetrandom;
45 n = tokenize_console(msg);
47 for(i = 0; i < n-1; i += 2)
54 data.y = FIELD_STRING;
58 data = stov(db_get(TemporaryDB, strcat("/target_spawn/field/", key)));
59 if(data.y == 0) // undefined field, i.e., invalid type
61 print("target_spawn: invalid/unknown entity key ", key, " specified, ignored!\n");
65 if(substring(value, 0, 1) == "$")
67 value = substring(value, 1, strlen(value) - 1);
68 if(substring(value, 0, 1) == "$")
70 // deferred replacement
72 // useful for creating target_spawns with this!
77 valuefieldpos = strstrofs(value, "+", 0);
79 if(valuefieldpos != -1)
81 valueoffset = substring(value, valuefieldpos + 1, strlen(value) - valuefieldpos - 1);
82 value = substring(value, 0, valuefieldpos);
85 valuefieldpos = strstrofs(valueoffset, "+", 0);
86 valueoffsetrandom = "";
87 if(valuefieldpos != -1)
89 valueoffsetrandom = substring(valueoffset, valuefieldpos + 1, strlen(valueoffset) - valuefieldpos - 1);
90 valueoffset = substring(valueoffset, 0, valuefieldpos);
93 valuefieldpos = strstrofs(value, ".", 0);
95 if(valuefieldpos != -1)
97 valuefield = substring(value, valuefieldpos + 1, strlen(value) - valuefieldpos - 1);
98 value = substring(value, 0, valuefieldpos);
106 else if(value == "activator")
111 else if(value == "other")
116 else if(value == "pusher")
118 if(time < act.pushltime)
119 valueent = act.pusher;
124 else if(value == "target")
129 else if(value == "killtarget")
134 else if(value == "target2")
139 else if(value == "target3")
144 else if(value == "target4")
149 else if(value == "time")
156 print("target_spawn: invalid/unknown variable replacement ", value, " specified, ignored!\n");
163 value = ftos(num_for_edict(valueent));
169 print("target_spawn: try to get a field of a non-entity, ignored!\n");
172 data2 = stov(db_get(TemporaryDB, strcat("/target_spawn/field/", valuefield)));
173 if(data2_y == 0) // undefined field, i.e., invalid type
175 print("target_spawn: invalid/unknown entity key replacement ", valuefield, " specified, ignored!\n");
178 value = getentityfieldstring(data2_x, valueent);
181 if(valueoffset != "")
186 value = strcat(value, valueoffset);
189 value = ftos(stof(value) + stof(valueoffset));
192 value = vtos(stov(value) + stov(valueoffset));
195 print("target_spawn: only string, float and vector fields can do calculations, calculation ignored!\n");
200 if(valueoffsetrandom != "")
205 value = ftos(stof(value) + random() * stof(valueoffsetrandom));
208 data2 = stov(valueoffsetrandom);
209 value = vtos(stov(value) + random() * data2_x * '1 0 0' + random() * data2_y * '0 1 0' + random() * data2_z * '0 0 1');
212 print("target_spawn: only float and vector fields can do random calculations, calculation ignored!\n");
220 if(substring(value, 0, 1) == "_")
221 value = strcat("target_spawn_helper", value);
222 putentityfieldstring(target_spawn_spawnfunc_field, e, value);
225 oldactivator = activator;
230 self.target_spawn_spawnfunc();
233 activator = oldactivator;
235 // We called an external function, so we have to re-tokenize msg.
236 n = tokenize_console(msg);
240 if(data.y == FIELD_VECTOR)
241 value = strreplace("'", "", value); // why?!?
242 putentityfieldstring(data.x, e, value);
247 void target_spawn_useon(entity e)
249 self.target_spawn_activator = activator;
250 target_spawn_edit_entity(
253 find(world, targetname, self.killtarget),
254 find(world, targetname, self.target2),
255 find(world, targetname, self.target3),
256 find(world, targetname, self.target4),
261 float target_spawn_cancreate()
267 if(c == 0) // no limit?
270 ++c; // increase count to not include MYSELF
271 for(e = world; (e = findfloat(e, target_spawn_id, self.target_spawn_id)); --c)
274 // if c now is 0, we have AT LEAST the given count (maybe more), so don't spawn any more
280 void target_spawn_use()
284 if(self.target == "")
287 if(!target_spawn_cancreate())
290 target_spawn_useon(e);
291 e.target_spawn_id = self.target_spawn_id;
293 else if(self.target == "*activator")
297 target_spawn_useon(activator);
302 for(e = world; (e = find(e, targetname, self.target)); )
303 target_spawn_useon(e);
307 void target_spawn_spawnfirst()
309 activator = self.target_spawn_activator;
310 if(self.spawnflags & 2)
314 void initialize_field_db()
316 if(!target_spawn_initialized)
323 n = numentityfields();
324 for(i = 0; i < n; ++i)
326 fn = entityfieldname(i);
327 ft = entityfieldtype(i);
328 next = i * '1 0 0' + ft * '0 1 0' + '0 0 1';
329 prev = stov(db_get(TemporaryDB, strcat("/target_spawn/field/", fn)));
332 db_put(TemporaryDB, strcat("/target_spawn/field/", fn), vtos(next));
333 if(fn == "target_spawn_spawnfunc")
334 target_spawn_spawnfunc_field = i;
338 target_spawn_initialized = 1;
342 void spawnfunc_target_spawn()
344 initialize_field_db();
345 self.use = target_spawn_use;
346 self.message = strzone(strreplace("'", "\"", self.message));
347 self.target_spawn_id = ++target_spawn_count;
348 InitializeEntity(self, target_spawn_spawnfirst, INITPRIO_LAST);