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_useon(entity e)
27 float i, n, valuefieldpos;
28 string key, value, valuefield, valueoffset, valueoffsetrandom;
33 entity kt, t2, t3, t4;
35 n = tokenize_console(self.message);
36 self.target_spawn_activator = activator;
38 kt = find(world, targetname, self.killtarget);
39 t2 = find(world, targetname, self.target2);
40 t3 = find(world, targetname, self.target3);
41 t4 = find(world, targetname, self.target4);
43 for(i = 0; i < n-1; i += 2)
50 data_y = FIELD_STRING;
54 data = stov(db_get(TemporaryDB, strcat("/target_spawn/field/", key)));
55 if(data_y == 0) // undefined field, i.e., invalid type
57 print("target_spawn: invalid/unknown entity key ", key, " specified, ignored!\n");
61 if(substring(value, 0, 1) == "$")
63 value = substring(value, 1, strlen(value) - 1);
64 if(substring(value, 0, 1) == "$")
66 // deferred replacement
68 // useful for creating target_spawns with this!
73 valuefieldpos = strstrofs(value, "+", 0);
75 if(valuefieldpos != -1)
77 valueoffset = substring(value, valuefieldpos + 1, strlen(value) - valuefieldpos - 1);
78 value = substring(value, 0, valuefieldpos);
81 valuefieldpos = strstrofs(valueoffset, "+", 0);
82 valueoffsetrandom = "";
83 if(valuefieldpos != -1)
85 valueoffsetrandom = substring(valueoffset, valuefieldpos + 1, strlen(valueoffset) - valuefieldpos - 1);
86 valueoffset = substring(valueoffset, 0, valuefieldpos);
89 valuefieldpos = strstrofs(value, ".", 0);
91 if(valuefieldpos != -1)
93 valuefield = substring(value, valuefieldpos + 1, strlen(value) - valuefieldpos - 1);
94 value = substring(value, 0, valuefieldpos);
102 else if(value == "activator")
104 valueent = activator;
107 else if(value == "other")
112 else if(value == "pusher")
114 if(time < activator.pushltime)
115 valueent = activator.pusher;
120 else if(value == "target")
125 else if(value == "killtarget")
130 else if(value == "target2")
135 else if(value == "target3")
140 else if(value == "target4")
145 else if(value == "time")
152 print("target_spawn: invalid/unknown variable replacement ", value, " specified, ignored!\n");
159 value = ftos(num_for_edict(valueent));
165 print("target_spawn: try to get a field of a non-entity, ignored!\n");
168 data2 = stov(db_get(TemporaryDB, strcat("/target_spawn/field/", valuefield)));
169 if(data2_y == 0) // undefined field, i.e., invalid type
171 print("target_spawn: invalid/unknown entity key replacement ", valuefield, " specified, ignored!\n");
174 value = getentityfieldstring(data2_x, valueent);
177 if(valueoffset != "")
182 value = strcat(value, valueoffset);
185 value = ftos(stof(value) + stof(valueoffset));
188 value = vtos(stov(value) + stov(valueoffset));
191 print("target_spawn: only string, float and vector fields can do calculations, calculation ignored!\n");
196 if(valueoffsetrandom != "")
201 value = ftos(stof(value) + random() * stof(valueoffsetrandom));
204 data2 = stov(valueoffsetrandom);
205 value = vtos(stov(value) + random() * data2_x * '1 0 0' + random() * data2_y * '0 1 0' + random() * data2_z * '0 0 1');
208 print("target_spawn: only float and vector fields can do random calculations, calculation ignored!\n");
216 if(substring(value, 0, 1) == "_")
217 value = strcat("target_spawn_helper", value);
218 putentityfieldstring(target_spawn_spawnfunc_field, e, value);
221 oldactivator = activator;
224 activator = oldself.target_spawn_activator;
226 self.target_spawn_spawnfunc();
229 activator = oldactivator;
233 if(data_y == FIELD_VECTOR)
234 value = strreplace("'", "", value); // why?!?
235 putentityfieldstring(data_x, e, value);
240 float target_spawn_cancreate()
246 if(c == 0) // no limit?
249 ++c; // increase count to not include MYSELF
250 for(e = world; (e = findfloat(e, target_spawn_id, self.target_spawn_id)); --c)
253 // if c now is 0, we have AT LEAST the given count (maybe more), so don't spawn any more
259 void target_spawn_use()
263 if(self.target == "")
266 if(!target_spawn_cancreate())
269 target_spawn_useon(e);
270 e.target_spawn_id = self.target_spawn_id;
275 for(e = world; (e = find(e, targetname, self.target)); )
276 target_spawn_useon(e);
280 void target_spawn_spawnfirst()
282 activator = self.target_spawn_activator;
283 if(self.spawnflags & 2)
287 void initialize_field_db()
289 if(!target_spawn_initialized)
296 n = numentityfields();
297 for(i = 0; i < n; ++i)
299 fn = entityfieldname(i);
300 ft = entityfieldtype(i);
301 new = i * '1 0 0' + ft * '0 1 0' + '0 0 1';
302 prev = stov(db_get(TemporaryDB, strcat("/target_spawn/field/", fn)));
305 db_put(TemporaryDB, strcat("/target_spawn/field/", fn), vtos(new));
306 if(fn == "target_spawn_spawnfunc")
307 target_spawn_spawnfunc_field = i;
311 target_spawn_initialized = 1;
315 void spawnfunc_target_spawn()
317 initialize_field_db();
318 self.use = target_spawn_use;
319 self.message = strzone(strreplace("'", "\"", self.message));
320 self.target_spawn_id = ++target_spawn_count;
321 InitializeEntity(self, target_spawn_spawnfirst, INITPRIO_LAST);
325 void trigger_relay_if_use()
330 // TODO make this generic AND faster than nextent()ing through all, if somehow possible
331 n = (cvar_string(self.netname) == cvar_string(self.message));
332 if(self.spawnflags & 1)
339 void spawnfunc_trigger_relay_if()
341 self.use = trigger_relay_if_use;