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;
16 string key, value, valuefield, valueoffset, valueoffsetrandom;
22 n = tokenize_sane(self.message);
24 for(i = 0; i < n-1; i += 2)
31 data_y = FIELD_STRING;
35 data = stov(db_get(TemporaryDB, strcat("/target_spawn/field/", key)));
36 if(data_y == 0) // undefined field, i.e., invalid type
38 print("target_spawn: invalid/unknown entity key ", key, " specified, ignored!\n");
42 if(substring(value, 0, 1) == "$")
44 value = substring(value, 1, strlen(value) - 1);
45 if(substring(value, 0, 1) == "$")
47 // deferred replacement
49 // useful for creating target_spawns with this!
54 valuefieldpos = strstrofs(value, "+", 0);
56 if(valuefieldpos != -1)
58 valueoffset = substring(value, valuefieldpos + 1, strlen(value) - valuefieldpos - 1);
59 value = substring(value, 0, valuefieldpos);
62 valuefieldpos = strstrofs(valueoffset, "+", 0);
63 valueoffsetrandom = "";
64 if(valuefieldpos != -1)
66 valueoffsetrandom = substring(valueoffset, valuefieldpos + 1, strlen(valueoffset) - valuefieldpos - 1);
67 valueoffset = substring(valueoffset, 0, valuefieldpos);
70 valuefieldpos = strstrofs(value, ".", 0);
72 if(valuefieldpos != -1)
74 valuefield = substring(value, valuefieldpos + 1, strlen(value) - valuefieldpos - 1);
75 value = substring(value, 0, valuefieldpos);
83 else if(value == "activator")
88 else if(value == "pusher")
90 if(time < activator.pushltime)
91 valueent = activator.pusher;
96 else if(value == "time")
103 print("target_spawn: invalid/unknown variable replacement ", value, " specified, ignored!\n");
110 value = ftos(num_for_edict(valueent));
116 print("target_spawn: try to get a field of a non-entity, ignored!\n");
119 data2 = stov(db_get(TemporaryDB, strcat("/target_spawn/field/", valuefield)));
120 if(data2_y == 0) // undefined field, i.e., invalid type
122 print("target_spawn: invalid/unknown entity key replacement ", valuefield, " specified, ignored!\n");
125 value = getentityfieldstring(data2_x, valueent);
128 if(valueoffset != "")
133 value = strcat(value, valueoffset);
136 value = ftos(stof(value) + stof(valueoffset));
139 value = vtos(stov(value) + stov(valueoffset));
142 print("target_spawn: only string, float and vector fields can do calculations, calculation ignored!\n");
147 if(valueoffsetrandom != "")
152 value = ftos(stof(value) + random() * stof(valueoffsetrandom));
155 data2 = stov(valueoffsetrandom);
156 value = vtos(stov(value) + random() * data2_x * '1 0 0' + random() * data2_y * '0 1 0' + random() * data2_z * '0 0 1');
159 print("target_spawn: only float and vector fields can do random calculations, calculation ignored!\n");
167 putentityfieldstring(target_spawn_spawnfunc_field, e, value);
170 oldactivator = activator;
173 activator = self.target_spawn_activator;
175 self.target_spawn_spawnfunc();
178 activator = oldactivator;
182 if(data_y == FIELD_VECTOR)
183 value = strreplace("'", "", value); // why?!?
184 putentityfieldstring(data_x, e, value);
189 void target_spawn_use()
193 if(self.target == "")
197 target_spawn_useon(e);
202 for(e = world; (e = find(e, targetname, self.target)); )
203 target_spawn_useon(e);
207 void target_spawn_spawnfirst()
209 activator = self.target_spawn_activator;
210 if(self.spawnflags & 2)
214 void spawnfunc_target_spawn()
216 if(!target_spawn_initialized)
223 n = numentityfields();
224 for(i = 0; i < n; ++i)
226 fn = entityfieldname(i);
227 ft = entityfieldtype(i);
228 new = i * '1 0 0' + ft * '0 1 0' + '0 0 1';
229 prev = stov(db_get(TemporaryDB, strcat("/target_spawn/field/", fn)));
232 db_put(TemporaryDB, strcat("/target_spawn/field/", fn), vtos(new));
233 if(fn == "target_spawn_spawnfunc")
234 target_spawn_spawnfunc_field = i;
238 target_spawn_initialized = 1;
240 self.use = target_spawn_use;
241 self.message = strzone(strreplace("'", "\"", self.message));
242 InitializeEntity(self, target_spawn_spawnfirst, INITPRIO_LAST);