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;
13 void target_spawn_useon(entity e)
15 float i, n, valuefieldpos, sPWNed;
16 string key, value, valuefield, valueoffset, valueoffsetrandom;
22 n = tokenize_sane(self.message);
26 for(i = 0; i < n-1; i += 2)
30 data = stov(db_get(TemporaryDB, strcat("/target_spawn/field/", key)));
31 if(data_y == 0) // undefined field, i.e., invalid type
33 print("target_spawn: invalid/unknown entity key ", key, " specified, ignored!\n");
36 if(substring(value, 0, 1) == "$")
38 value = substring(value, 1, strlen(value) - 1);
39 if(substring(value, 0, 1) == "$")
41 // deferred replacement
43 // useful for creating target_spawns with this!
48 valuefieldpos = strstrofs(value, ".", 0);
50 if(valuefieldpos != -1)
52 valuefield = substring(value, valuefieldpos + 1, strlen(value) - valuefieldpos - 1);
53 value = substring(value, 0, valuefieldpos);
56 valuefieldpos = strstrofs(value, "+", 0);
58 if(valuefieldpos != -1)
60 valueoffset = substring(valuefield, valuefieldpos + 1, strlen(valueoffset) - valuefieldpos - 1);
61 valuefield = substring(valuefield, 0, valuefieldpos);
64 valuefieldpos = strstrofs(valueoffset, "+", 0);
65 valueoffsetrandom = "";
66 if(valuefieldpos != -1)
68 valueoffsetrandom = substring(valueoffset, valuefieldpos + 1, strlen(valueoffset) - valuefieldpos - 1);
69 valueoffset = substring(valueoffset, 0, valuefieldpos);
77 else if(value == "activator")
82 else if(value == "pusher")
84 if(time < activator.pushltime)
85 valueent = activator.pusher;
90 else if(value == "time")
97 print("target_spawn: invalid/unknown variable replacement ", value, " specified, ignored!\n");
104 value = ftos(num_for_edict(valueent));
110 print("target_spawn: try to get a field of a non-entity, ignored!\n");
113 data2 = stov(db_get(TemporaryDB, strcat("/target_spawn/field/", valuefield)));
114 if(data2_y == 0) // undefined field, i.e., invalid type
116 print("target_spawn: invalid/unknown entity key replacement ", value, " specified, ignored!\n");
119 value = getentityfieldstring(data2_x, valueent);
122 if(valueoffset != "")
127 value = strcat(value, valueoffset);
130 value = ftos(stof(value) + stof(valueoffset));
133 value = vtos(stov(value) + stov(valueoffset));
136 print("target_spawn: only string, float and vector fields can do calculations, calculation ignored!\n");
141 if(valueoffsetrandom != "")
146 value = ftos(stof(value) + random() * stof(valueoffsetrandom));
149 data2 = stov(valueoffsetrandom);
150 value = vtos(stov(value) + random() * data2_x * '1 0 0' + random() * data2_y * '0 1 0' + random() * data2_z * '0 0 1');
153 print("target_spawn: only float and vector fields can do random calculations, calculation ignored!\n");
159 putentityfieldstring(data_x, e, value);
161 if(key == "classname" && !sPWNed)
163 if(self.spawnflags & 1)
165 if(!e.target_spawn_spawnfunc)
166 putentityfieldstring(target_spawn_spawnfunc_field, e, strcat("spawnfunc_", value));
169 oldactivator = activator;
172 activator = self.target_spawn_activator;
174 self.target_spawn_spawnfunc();
177 activator = oldactivator;
184 void target_spawn_use()
188 if(self.target == "")
192 target_spawn_useon(e);
197 for(e = world; (e = find(e, targetname, self.target)); )
198 target_spawn_useon(e);
202 void target_spawn_spawnfirst()
204 activator = self.target_spawn_activator;
208 void spawnfunc_target_spawn()
210 if(!target_spawn_initialized)
216 n = numentityfields();
217 for(i = 0; i < n; ++i)
219 fn = entityfieldname(i);
220 ft = entityfieldtype(i);
221 db_put(TemporaryDB, strcat("/target_spawn/field/", fn), vtos(i * '1 0 0' + ft * '0 1 0' + '0 0 1'));
222 if(fn == "target_spawn_spawnfunc")
223 target_spawn_spawnfunc_field = i;
226 target_spawn_initialized = 1;
228 self.use = target_spawn_use;
229 InitializeEntity(self, target_spawn_spawnfirst, INITPRIO_LAST);