4 #include "../../../server/_all.qh"
5 #include "../../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() 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()
27 setmodel(self, self.model);
30 void target_spawn_helper_setsize()
32 setsize(self, self.mins, self.maxs);
35 void target_spawn_edit_entity(entity e, string msg, entity kt, entity t2, entity t3, entity t4, entity act)
37 float i, n, valuefieldpos;
38 string key, value, valuefield, valueoffset, valueoffsetrandom;
44 n = tokenize_console(msg);
46 for(i = 0; i < n-1; i += 2)
53 data.y = FIELD_STRING;
57 data = stov(db_get(TemporaryDB, strcat("/target_spawn/field/", key)));
58 if(data.y == 0) // undefined field, i.e., invalid type
60 print("target_spawn: invalid/unknown entity key ", key, " specified, ignored!\n");
64 if(substring(value, 0, 1) == "$")
66 value = substring(value, 1, strlen(value) - 1);
67 if(substring(value, 0, 1) == "$")
69 // deferred replacement
71 // useful for creating target_spawns with this!
76 valuefieldpos = strstrofs(value, "+", 0);
78 if(valuefieldpos != -1)
80 valueoffset = substring(value, valuefieldpos + 1, strlen(value) - valuefieldpos - 1);
81 value = substring(value, 0, valuefieldpos);
84 valuefieldpos = strstrofs(valueoffset, "+", 0);
85 valueoffsetrandom = "";
86 if(valuefieldpos != -1)
88 valueoffsetrandom = substring(valueoffset, valuefieldpos + 1, strlen(valueoffset) - valuefieldpos - 1);
89 valueoffset = substring(valueoffset, 0, valuefieldpos);
92 valuefieldpos = strstrofs(value, ".", 0);
94 if(valuefieldpos != -1)
96 valuefield = substring(value, valuefieldpos + 1, strlen(value) - valuefieldpos - 1);
97 value = substring(value, 0, valuefieldpos);
105 else if(value == "activator")
110 else if(value == "other")
115 else if(value == "pusher")
117 if(time < act.pushltime)
118 valueent = act.pusher;
123 else if(value == "target")
128 else if(value == "killtarget")
133 else if(value == "target2")
138 else if(value == "target3")
143 else if(value == "target4")
148 else if(value == "time")
155 print("target_spawn: invalid/unknown variable replacement ", value, " specified, ignored!\n");
162 value = ftos(num_for_edict(valueent));
168 print("target_spawn: try to get a field of a non-entity, ignored!\n");
171 data2 = stov(db_get(TemporaryDB, strcat("/target_spawn/field/", valuefield)));
172 if(data2_y == 0) // undefined field, i.e., invalid type
174 print("target_spawn: invalid/unknown entity key replacement ", valuefield, " specified, ignored!\n");
177 value = getentityfieldstring(data2_x, valueent);
180 if(valueoffset != "")
185 value = strcat(value, valueoffset);
188 value = ftos(stof(value) + stof(valueoffset));
191 value = vtos(stov(value) + stov(valueoffset));
194 print("target_spawn: only string, float and vector fields can do calculations, calculation ignored!\n");
199 if(valueoffsetrandom != "")
204 value = ftos(stof(value) + random() * stof(valueoffsetrandom));
207 data2 = stov(valueoffsetrandom);
208 value = vtos(stov(value) + random() * data2_x * '1 0 0' + random() * data2_y * '0 1 0' + random() * data2_z * '0 0 1');
211 print("target_spawn: only float and vector fields can do random calculations, calculation ignored!\n");
219 if(substring(value, 0, 1) == "_")
220 value = strcat("target_spawn_helper", value);
221 putentityfieldstring(target_spawn_spawnfunc_field, e, value);
224 oldactivator = activator;
229 self.target_spawn_spawnfunc();
232 activator = oldactivator;
234 // We called an external function, so we have to re-tokenize msg.
235 n = tokenize_console(msg);
239 if(data.y == FIELD_VECTOR)
240 value = strreplace("'", "", value); // why?!?
241 putentityfieldstring(data.x, e, value);
246 void target_spawn_useon(entity e)
248 self.target_spawn_activator = activator;
249 target_spawn_edit_entity(
252 find(world, targetname, self.killtarget),
253 find(world, targetname, self.target2),
254 find(world, targetname, self.target3),
255 find(world, targetname, self.target4),
260 float target_spawn_cancreate()
266 if(c == 0) // no limit?
269 ++c; // increase count to not include MYSELF
270 for(e = world; (e = findfloat(e, target_spawn_id, self.target_spawn_id)); --c)
273 // if c now is 0, we have AT LEAST the given count (maybe more), so don't spawn any more
279 void target_spawn_use()
283 if(self.target == "")
286 if(!target_spawn_cancreate())
289 target_spawn_useon(e);
290 e.target_spawn_id = self.target_spawn_id;
292 else if(self.target == "*activator")
296 target_spawn_useon(activator);
301 for(e = world; (e = find(e, targetname, self.target)); )
302 target_spawn_useon(e);
306 void target_spawn_spawnfirst()
308 activator = self.target_spawn_activator;
309 if(self.spawnflags & 2)
313 void initialize_field_db()
315 if(!target_spawn_initialized)
322 n = numentityfields();
323 for(i = 0; i < n; ++i)
325 fn = entityfieldname(i);
326 ft = entityfieldtype(i);
327 next = i * '1 0 0' + ft * '0 1 0' + '0 0 1';
328 prev = stov(db_get(TemporaryDB, strcat("/target_spawn/field/", fn)));
331 db_put(TemporaryDB, strcat("/target_spawn/field/", fn), vtos(next));
332 if(fn == "target_spawn_spawnfunc")
333 target_spawn_spawnfunc_field = i;
337 target_spawn_initialized = 1;
341 void spawnfunc_target_spawn()
343 initialize_field_db();
344 self.use = target_spawn_use;
345 self.message = strzone(strreplace("'", "\"", self.message));
346 self.target_spawn_id = ++target_spawn_count;
347 InitializeEntity(self, target_spawn_spawnfirst, INITPRIO_LAST);