2 // "classname" "target_spawn"
3 // "message" "fieldname value fieldname value ..."
5 // 1 = call the spawn function
6 // 2 = trigger on map load
8 float target_spawn_initialized;
9 .void() target_spawn_spawnfunc;
10 float target_spawn_spawnfunc_field;
11 .entity target_spawn_activator;
12 .float target_spawn_id;
13 float target_spawn_count;
15 void target_spawn_helper_setmodel()
17 setmodel(self, self.model);
20 void target_spawn_helper_setsize()
22 setsize(self, self.mins, self.maxs);
25 void target_spawn_edit_entity(entity e, string msg, entity kt, entity t2, entity t3, entity t4, entity act)
27 float i, n, valuefieldpos;
28 string key, value, valuefield, valueoffset, valueoffsetrandom;
34 n = tokenize_console(msg);
36 for(i = 0; i < n-1; i += 2)
43 data_y = FIELD_STRING;
47 data = stov(db_get(TemporaryDB, strcat("/target_spawn/field/", key)));
48 if(data_y == 0) // undefined field, i.e., invalid type
50 print("target_spawn: invalid/unknown entity key ", key, " specified, ignored!\n");
54 if(substring(value, 0, 1) == "$")
56 value = substring(value, 1, strlen(value) - 1);
57 if(substring(value, 0, 1) == "$")
59 // deferred replacement
61 // useful for creating target_spawns with this!
66 valuefieldpos = strstrofs(value, "+", 0);
68 if(valuefieldpos != -1)
70 valueoffset = substring(value, valuefieldpos + 1, strlen(value) - valuefieldpos - 1);
71 value = substring(value, 0, valuefieldpos);
74 valuefieldpos = strstrofs(valueoffset, "+", 0);
75 valueoffsetrandom = "";
76 if(valuefieldpos != -1)
78 valueoffsetrandom = substring(valueoffset, valuefieldpos + 1, strlen(valueoffset) - valuefieldpos - 1);
79 valueoffset = substring(valueoffset, 0, valuefieldpos);
82 valuefieldpos = strstrofs(value, ".", 0);
84 if(valuefieldpos != -1)
86 valuefield = substring(value, valuefieldpos + 1, strlen(value) - valuefieldpos - 1);
87 value = substring(value, 0, valuefieldpos);
95 else if(value == "activator")
100 else if(value == "other")
105 else if(value == "pusher")
107 if(time < act.pushltime)
108 valueent = act.pusher;
113 else if(value == "target")
118 else if(value == "killtarget")
123 else if(value == "target2")
128 else if(value == "target3")
133 else if(value == "target4")
138 else if(value == "time")
145 print("target_spawn: invalid/unknown variable replacement ", value, " specified, ignored!\n");
152 value = ftos(num_for_edict(valueent));
158 print("target_spawn: try to get a field of a non-entity, ignored!\n");
161 data2 = stov(db_get(TemporaryDB, strcat("/target_spawn/field/", valuefield)));
162 if(data2_y == 0) // undefined field, i.e., invalid type
164 print("target_spawn: invalid/unknown entity key replacement ", valuefield, " specified, ignored!\n");
167 value = getentityfieldstring(data2_x, valueent);
170 if(valueoffset != "")
175 value = strcat(value, valueoffset);
178 value = ftos(stof(value) + stof(valueoffset));
181 value = vtos(stov(value) + stov(valueoffset));
184 print("target_spawn: only string, float and vector fields can do calculations, calculation ignored!\n");
189 if(valueoffsetrandom != "")
194 value = ftos(stof(value) + random() * stof(valueoffsetrandom));
197 data2 = stov(valueoffsetrandom);
198 value = vtos(stov(value) + random() * data2_x * '1 0 0' + random() * data2_y * '0 1 0' + random() * data2_z * '0 0 1');
201 print("target_spawn: only float and vector fields can do random calculations, calculation ignored!\n");
209 if(substring(value, 0, 1) == "_")
210 value = strcat("target_spawn_helper", value);
211 putentityfieldstring(target_spawn_spawnfunc_field, e, value);
214 oldactivator = activator;
219 if(self.target_spawn_spawnfunc)
220 self.target_spawn_spawnfunc();
223 activator = oldactivator;
227 if(data_y == FIELD_VECTOR)
228 value = strreplace("'", "", value); // why?!?
229 putentityfieldstring(data_x, e, value);
234 void target_spawn_useon(entity e)
236 self.target_spawn_activator = activator;
237 target_spawn_edit_entity(
240 find(world, targetname, self.killtarget),
241 find(world, targetname, self.target2),
242 find(world, targetname, self.target3),
243 find(world, targetname, self.target4),
248 float target_spawn_cancreate()
254 if(c == 0) // no limit?
257 ++c; // increase count to not include MYSELF
258 for(e = world; (e = findfloat(e, target_spawn_id, self.target_spawn_id)); --c)
261 // if c now is 0, we have AT LEAST the given count (maybe more), so don't spawn any more
267 void target_spawn_use()
271 if(self.target == "")
274 if(!target_spawn_cancreate())
277 target_spawn_useon(e);
278 e.target_spawn_id = self.target_spawn_id;
280 else if(self.target == "*activator")
284 target_spawn_useon(activator);
289 for(e = world; (e = find(e, targetname, self.target)); )
290 target_spawn_useon(e);
294 void target_spawn_spawnfirst()
296 activator = self.target_spawn_activator;
297 if(self.spawnflags & 2)
301 void initialize_field_db()
303 if(!target_spawn_initialized)
310 n = numentityfields();
311 for(i = 0; i < n; ++i)
313 fn = entityfieldname(i);
314 ft = entityfieldtype(i);
315 new = i * '1 0 0' + ft * '0 1 0' + '0 0 1';
316 prev = stov(db_get(TemporaryDB, strcat("/target_spawn/field/", fn)));
319 db_put(TemporaryDB, strcat("/target_spawn/field/", fn), vtos(new));
320 if(fn == "target_spawn_spawnfunc")
321 target_spawn_spawnfunc_field = i;
325 target_spawn_initialized = 1;
329 void spawnfunc_target_spawn()
331 initialize_field_db();
332 self.use = target_spawn_use;
333 self.message = strzone(strreplace("'", "\"", self.message));
334 self.target_spawn_id = ++target_spawn_count;
335 InitializeEntity(self, target_spawn_spawnfirst, INITPRIO_LAST);
339 void trigger_relay_if_use()
344 // TODO make this generic AND faster than nextent()ing through all, if somehow possible
345 n = (cvar_string(self.netname) == cvar_string(self.message));
346 if(self.spawnflags & 1)
353 void spawnfunc_trigger_relay_if()
355 self.use = trigger_relay_if_use;