4 #include "../dpdefs/progsdefs.qh"
5 #include "../dpdefs/dpextensions.qh"
6 #include "../common/util.qh"
11 // "classname" "target_spawn"
12 // "message" "fieldname value fieldname value ..."
14 // 1 = call the spawn function
15 // 2 = trigger on map load
17 float target_spawn_initialized;
18 .void() target_spawn_spawnfunc;
19 float target_spawn_spawnfunc_field;
20 .entity target_spawn_activator;
21 .float target_spawn_id;
22 float target_spawn_count;
24 void target_spawn_helper_setmodel()
26 setmodel(self, self.model);
29 void target_spawn_helper_setsize()
31 setsize(self, self.mins, self.maxs);
34 void target_spawn_edit_entity(entity e, string msg, entity kt, entity t2, entity t3, entity t4, entity act)
36 float i, n, valuefieldpos;
37 string key, value, valuefield, valueoffset, valueoffsetrandom;
43 n = tokenize_console(msg);
45 for(i = 0; i < n-1; i += 2)
52 data.y = FIELD_STRING;
56 data = stov(db_get(TemporaryDB, strcat("/target_spawn/field/", key)));
57 if(data.y == 0) // undefined field, i.e., invalid type
59 print("target_spawn: invalid/unknown entity key ", key, " specified, ignored!\n");
63 if(substring(value, 0, 1) == "$")
65 value = substring(value, 1, strlen(value) - 1);
66 if(substring(value, 0, 1) == "$")
68 // deferred replacement
70 // useful for creating target_spawns with this!
75 valuefieldpos = strstrofs(value, "+", 0);
77 if(valuefieldpos != -1)
79 valueoffset = substring(value, valuefieldpos + 1, strlen(value) - valuefieldpos - 1);
80 value = substring(value, 0, valuefieldpos);
83 valuefieldpos = strstrofs(valueoffset, "+", 0);
84 valueoffsetrandom = "";
85 if(valuefieldpos != -1)
87 valueoffsetrandom = substring(valueoffset, valuefieldpos + 1, strlen(valueoffset) - valuefieldpos - 1);
88 valueoffset = substring(valueoffset, 0, valuefieldpos);
91 valuefieldpos = strstrofs(value, ".", 0);
93 if(valuefieldpos != -1)
95 valuefield = substring(value, valuefieldpos + 1, strlen(value) - valuefieldpos - 1);
96 value = substring(value, 0, valuefieldpos);
104 else if(value == "activator")
109 else if(value == "other")
114 else if(value == "pusher")
116 if(time < act.pushltime)
117 valueent = act.pusher;
122 else if(value == "target")
127 else if(value == "killtarget")
132 else if(value == "target2")
137 else if(value == "target3")
142 else if(value == "target4")
147 else if(value == "time")
154 print("target_spawn: invalid/unknown variable replacement ", value, " specified, ignored!\n");
161 value = ftos(num_for_edict(valueent));
167 print("target_spawn: try to get a field of a non-entity, ignored!\n");
170 data2 = stov(db_get(TemporaryDB, strcat("/target_spawn/field/", valuefield)));
171 if(data2_y == 0) // undefined field, i.e., invalid type
173 print("target_spawn: invalid/unknown entity key replacement ", valuefield, " specified, ignored!\n");
176 value = getentityfieldstring(data2_x, valueent);
179 if(valueoffset != "")
184 value = strcat(value, valueoffset);
187 value = ftos(stof(value) + stof(valueoffset));
190 value = vtos(stov(value) + stov(valueoffset));
193 print("target_spawn: only string, float and vector fields can do calculations, calculation ignored!\n");
198 if(valueoffsetrandom != "")
203 value = ftos(stof(value) + random() * stof(valueoffsetrandom));
206 data2 = stov(valueoffsetrandom);
207 value = vtos(stov(value) + random() * data2_x * '1 0 0' + random() * data2_y * '0 1 0' + random() * data2_z * '0 0 1');
210 print("target_spawn: only float and vector fields can do random calculations, calculation ignored!\n");
218 if(substring(value, 0, 1) == "_")
219 value = strcat("target_spawn_helper", value);
220 putentityfieldstring(target_spawn_spawnfunc_field, e, value);
223 oldactivator = activator;
228 self.target_spawn_spawnfunc();
231 activator = oldactivator;
233 // We called an external function, so we have to re-tokenize msg.
234 n = tokenize_console(msg);
238 if(data.y == FIELD_VECTOR)
239 value = strreplace("'", "", value); // why?!?
240 putentityfieldstring(data.x, e, value);
245 void target_spawn_useon(entity e)
247 self.target_spawn_activator = activator;
248 target_spawn_edit_entity(
251 find(world, targetname, self.killtarget),
252 find(world, targetname, self.target2),
253 find(world, targetname, self.target3),
254 find(world, targetname, self.target4),
259 float target_spawn_cancreate()
265 if(c == 0) // no limit?
268 ++c; // increase count to not include MYSELF
269 for(e = world; (e = findfloat(e, target_spawn_id, self.target_spawn_id)); --c)
272 // if c now is 0, we have AT LEAST the given count (maybe more), so don't spawn any more
278 void target_spawn_use()
282 if(self.target == "")
285 if(!target_spawn_cancreate())
288 target_spawn_useon(e);
289 e.target_spawn_id = self.target_spawn_id;
291 else if(self.target == "*activator")
295 target_spawn_useon(activator);
300 for(e = world; (e = find(e, targetname, self.target)); )
301 target_spawn_useon(e);
305 void target_spawn_spawnfirst()
307 activator = self.target_spawn_activator;
308 if(self.spawnflags & 2)
312 void initialize_field_db()
314 if(!target_spawn_initialized)
321 n = numentityfields();
322 for(i = 0; i < n; ++i)
324 fn = entityfieldname(i);
325 ft = entityfieldtype(i);
326 new = i * '1 0 0' + ft * '0 1 0' + '0 0 1';
327 prev = stov(db_get(TemporaryDB, strcat("/target_spawn/field/", fn)));
330 db_put(TemporaryDB, strcat("/target_spawn/field/", fn), vtos(new));
331 if(fn == "target_spawn_spawnfunc")
332 target_spawn_spawnfunc_field = i;
336 target_spawn_initialized = 1;
340 void spawnfunc_target_spawn()
342 initialize_field_db();
343 self.use = target_spawn_use;
344 self.message = strzone(strreplace("'", "\"", self.message));
345 self.target_spawn_id = ++target_spawn_count;
346 InitializeEntity(self, target_spawn_spawnfirst, INITPRIO_LAST);
350 void trigger_relay_if_use()
355 // TODO make this generic AND faster than nextent()ing through all, if somehow possible
356 n = (cvar_string(self.netname) == cvar_string(self.message));
357 if(self.spawnflags & 1)
364 void spawnfunc_trigger_relay_if()
366 self.use = trigger_relay_if_use;