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 == "target")
101 else if(value == "time")
108 print("target_spawn: invalid/unknown variable replacement ", value, " specified, ignored!\n");
115 value = ftos(num_for_edict(valueent));
121 print("target_spawn: try to get a field of a non-entity, ignored!\n");
124 data2 = stov(db_get(TemporaryDB, strcat("/target_spawn/field/", valuefield)));
125 if(data2_y == 0) // undefined field, i.e., invalid type
127 print("target_spawn: invalid/unknown entity key replacement ", valuefield, " specified, ignored!\n");
130 value = getentityfieldstring(data2_x, valueent);
133 if(valueoffset != "")
138 value = strcat(value, valueoffset);
141 value = ftos(stof(value) + stof(valueoffset));
144 value = vtos(stov(value) + stov(valueoffset));
147 print("target_spawn: only string, float and vector fields can do calculations, calculation ignored!\n");
152 if(valueoffsetrandom != "")
157 value = ftos(stof(value) + random() * stof(valueoffsetrandom));
160 data2 = stov(valueoffsetrandom);
161 value = vtos(stov(value) + random() * data2_x * '1 0 0' + random() * data2_y * '0 1 0' + random() * data2_z * '0 0 1');
164 print("target_spawn: only float and vector fields can do random calculations, calculation ignored!\n");
172 putentityfieldstring(target_spawn_spawnfunc_field, e, value);
175 oldactivator = activator;
178 activator = self.target_spawn_activator;
180 self.target_spawn_spawnfunc();
183 activator = oldactivator;
187 if(data_y == FIELD_VECTOR)
188 value = strreplace("'", "", value); // why?!?
189 putentityfieldstring(data_x, e, value);
194 void target_spawn_use()
198 if(self.target == "")
202 target_spawn_useon(e);
207 for(e = world; (e = find(e, targetname, self.target)); )
208 target_spawn_useon(e);
212 void target_spawn_spawnfirst()
214 activator = self.target_spawn_activator;
215 if(self.spawnflags & 2)
219 void spawnfunc_target_spawn()
221 if(!target_spawn_initialized)
228 n = numentityfields();
229 for(i = 0; i < n; ++i)
231 fn = entityfieldname(i);
232 ft = entityfieldtype(i);
233 new = i * '1 0 0' + ft * '0 1 0' + '0 0 1';
234 prev = stov(db_get(TemporaryDB, strcat("/target_spawn/field/", fn)));
237 db_put(TemporaryDB, strcat("/target_spawn/field/", fn), vtos(new));
238 if(fn == "target_spawn_spawnfunc")
239 target_spawn_spawnfunc_field = i;
243 target_spawn_initialized = 1;
245 self.use = target_spawn_use;
246 self.message = strzone(strreplace("'", "\"", self.message));
247 InitializeEntity(self, target_spawn_spawnfirst, INITPRIO_LAST);