4 #include "../dpdefs/progsdefs.qh"
5 #include "../dpdefs/dpextensions.qh"
7 #include "../common/util.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;
236 if(data_y == FIELD_VECTOR)
237 value = strreplace("'", "", value); // why?!?
238 putentityfieldstring(data.x, e, value);
243 void target_spawn_useon(entity e)
245 self.target_spawn_activator = activator;
246 target_spawn_edit_entity(
249 find(world, targetname, self.killtarget),
250 find(world, targetname, self.target2),
251 find(world, targetname, self.target3),
252 find(world, targetname, self.target4),
257 float target_spawn_cancreate()
263 if(c == 0) // no limit?
266 ++c; // increase count to not include MYSELF
267 for(e = world; (e = findfloat(e, target_spawn_id, self.target_spawn_id)); --c)
270 // if c now is 0, we have AT LEAST the given count (maybe more), so don't spawn any more
276 void target_spawn_use()
280 if(self.target == "")
283 if(!target_spawn_cancreate())
286 target_spawn_useon(e);
287 e.target_spawn_id = self.target_spawn_id;
289 else if(self.target == "*activator")
293 target_spawn_useon(activator);
298 for(e = world; (e = find(e, targetname, self.target)); )
299 target_spawn_useon(e);
303 void target_spawn_spawnfirst()
305 activator = self.target_spawn_activator;
306 if(self.spawnflags & 2)
310 void initialize_field_db()
312 if(!target_spawn_initialized)
319 n = numentityfields();
320 for(i = 0; i < n; ++i)
322 fn = entityfieldname(i);
323 ft = entityfieldtype(i);
324 new = i * '1 0 0' + ft * '0 1 0' + '0 0 1';
325 prev = stov(db_get(TemporaryDB, strcat("/target_spawn/field/", fn)));
328 db_put(TemporaryDB, strcat("/target_spawn/field/", fn), vtos(new));
329 if(fn == "target_spawn_spawnfunc")
330 target_spawn_spawnfunc_field = i;
334 target_spawn_initialized = 1;
338 void spawnfunc_target_spawn()
340 initialize_field_db();
341 self.use = target_spawn_use;
342 self.message = strzone(strreplace("'", "\"", self.message));
343 self.target_spawn_id = ++target_spawn_count;
344 InitializeEntity(self, target_spawn_spawnfirst, INITPRIO_LAST);
348 void trigger_relay_if_use()
353 // TODO make this generic AND faster than nextent()ing through all, if somehow possible
354 n = (cvar_string(self.netname) == cvar_string(self.message));
355 if(self.spawnflags & 1)
362 void spawnfunc_trigger_relay_if()
364 self.use = trigger_relay_if_use;