4 #include "../../../server/_all.qh"
5 #include "../../util.qh"
6 #include "../../../server/defs.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;
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 LOG_INFO("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 LOG_INFO("target_spawn: invalid/unknown variable replacement ", value, " specified, ignored!\n");
161 value = ftos(num_for_edict(valueent));
167 LOG_INFO("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 LOG_INFO("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 LOG_INFO("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 LOG_INFO("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);
222 oldactivator = activator;
225 WITH(entity, self, e, e.target_spawn_spawnfunc());
226 activator = oldactivator;
228 // We called an external function, so we have to re-tokenize msg.
229 n = tokenize_console(msg);
233 if(data.y == FIELD_VECTOR)
234 value = strreplace("'", "", value); // why?!?
235 putentityfieldstring(data.x, e, value);
240 void target_spawn_useon(entity e)
242 self.target_spawn_activator = activator;
243 target_spawn_edit_entity(
246 find(world, targetname, self.killtarget),
247 find(world, targetname, self.target2),
248 find(world, targetname, self.target3),
249 find(world, targetname, self.target4),
254 float target_spawn_cancreate()
260 if(c == 0) // no limit?
263 ++c; // increase count to not include MYSELF
264 for(e = world; (e = findfloat(e, target_spawn_id, self.target_spawn_id)); --c)
267 // if c now is 0, we have AT LEAST the given count (maybe more), so don't spawn any more
273 void target_spawn_use()
277 if(self.target == "")
280 if(!target_spawn_cancreate())
283 target_spawn_useon(e);
284 e.target_spawn_id = self.target_spawn_id;
286 else if(self.target == "*activator")
290 target_spawn_useon(activator);
295 for(e = world; (e = find(e, targetname, self.target)); )
296 target_spawn_useon(e);
300 void target_spawn_spawnfirst()
302 activator = self.target_spawn_activator;
303 if(self.spawnflags & 2)
307 void initialize_field_db()
309 if(!target_spawn_initialized)
316 n = numentityfields();
317 for(i = 0; i < n; ++i)
319 fn = entityfieldname(i);
320 ft = entityfieldtype(i);
321 next = i * '1 0 0' + ft * '0 1 0' + '0 0 1';
322 prev = stov(db_get(TemporaryDB, strcat("/target_spawn/field/", fn)));
325 db_put(TemporaryDB, strcat("/target_spawn/field/", fn), vtos(next));
326 if(fn == "target_spawn_spawnfunc")
327 target_spawn_spawnfunc_field = i;
331 target_spawn_initialized = 1;
335 void spawnfunc_target_spawn()
337 initialize_field_db();
338 self.use = target_spawn_use;
339 self.message = strzone(strreplace("'", "\"", self.message));
340 self.target_spawn_id = ++target_spawn_count;
341 InitializeEntity(self, target_spawn_spawnfirst, INITPRIO_LAST);