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;
235 if(data.y == FIELD_VECTOR)
236 value = strreplace("'", "", value); // why?!?
237 putentityfieldstring(data.x, e, value);
242 void target_spawn_useon(entity e)
244 self.target_spawn_activator = activator;
245 target_spawn_edit_entity(
248 find(world, targetname, self.killtarget),
249 find(world, targetname, self.target2),
250 find(world, targetname, self.target3),
251 find(world, targetname, self.target4),
256 float target_spawn_cancreate()
262 if(c == 0) // no limit?
265 ++c; // increase count to not include MYSELF
266 for(e = world; (e = findfloat(e, target_spawn_id, self.target_spawn_id)); --c)
269 // if c now is 0, we have AT LEAST the given count (maybe more), so don't spawn any more
275 void target_spawn_use()
279 if(self.target == "")
282 if(!target_spawn_cancreate())
285 target_spawn_useon(e);
286 e.target_spawn_id = self.target_spawn_id;
288 else if(self.target == "*activator")
292 target_spawn_useon(activator);
297 for(e = world; (e = find(e, targetname, self.target)); )
298 target_spawn_useon(e);
302 void target_spawn_spawnfirst()
304 activator = self.target_spawn_activator;
305 if(self.spawnflags & 2)
309 void initialize_field_db()
311 if(!target_spawn_initialized)
318 n = numentityfields();
319 for(i = 0; i < n; ++i)
321 fn = entityfieldname(i);
322 ft = entityfieldtype(i);
323 new = i * '1 0 0' + ft * '0 1 0' + '0 0 1';
324 prev = stov(db_get(TemporaryDB, strcat("/target_spawn/field/", fn)));
327 db_put(TemporaryDB, strcat("/target_spawn/field/", fn), vtos(new));
328 if(fn == "target_spawn_spawnfunc")
329 target_spawn_spawnfunc_field = i;
333 target_spawn_initialized = 1;
337 void spawnfunc_target_spawn()
339 initialize_field_db();
340 self.use = target_spawn_use;
341 self.message = strzone(strreplace("'", "\"", self.message));
342 self.target_spawn_id = ++target_spawn_count;
343 InitializeEntity(self, target_spawn_spawnfirst, INITPRIO_LAST);
347 void trigger_relay_if_use()
352 // TODO make this generic AND faster than nextent()ing through all, if somehow possible
353 n = (cvar_string(self.netname) == cvar_string(self.message));
354 if(self.spawnflags & 1)
361 void spawnfunc_trigger_relay_if()
363 self.use = trigger_relay_if_use;