Spawnfuncs: whitelist more fields
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / triggers / target / spawn.qc
1 #if defined(CSQC)
2 #elif defined(MENUQC)
3 #elif defined(SVQC)
4         #include "../../../server/_all.qh"
5     #include "../../util.qh"
6     #include "../../../server/defs.qh"
7 #endif
8
9 #ifdef SVQC
10
11 // spawner entity
12 // "classname" "target_spawn"
13 // "message" "fieldname value fieldname value ..."
14 // "spawnflags"
15 //   1 = call the spawn function
16 //   2 = trigger on map load
17
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;
24
25 void target_spawn_helper_setmodel()
26 {SELFPARAM();
27         _setmodel(self, self.model);
28 }
29
30 void target_spawn_helper_setsize()
31 {SELFPARAM();
32         setsize(self, self.mins, self.maxs);
33 }
34
35 void target_spawn_edit_entity(entity e, string msg, entity kt, entity t2, entity t3, entity t4, entity act)
36 {SELFPARAM();
37         float i, n, valuefieldpos;
38         string key, value, valuefield, valueoffset, valueoffsetrandom;
39         entity valueent;
40         vector data, data2;
41         entity oldactivator;
42
43         n = tokenize_console(msg);
44
45         for(i = 0; i < n-1; i += 2)
46         {
47                 key = argv(i);
48                 value = argv(i+1);
49                 if(key == "$")
50                 {
51                         data.x = -1;
52                         data.y = FIELD_STRING;
53                 }
54                 else
55                 {
56                         data = stov(db_get(TemporaryDB, strcat("/target_spawn/field/", key)));
57                         if(data.y == 0) // undefined field, i.e., invalid type
58                         {
59                                 LOG_INFO("target_spawn: invalid/unknown entity key ", key, " specified, ignored!\n");
60                                 continue;
61                         }
62                 }
63                 if(substring(value, 0, 1) == "$")
64                 {
65                         value = substring(value, 1, strlen(value) - 1);
66                         if(substring(value, 0, 1) == "$")
67                         {
68                                 // deferred replacement
69                                 // do nothing
70                                 // useful for creating target_spawns with this!
71                         }
72                         else
73                         {
74                                 // replace me!
75                                 valuefieldpos = strstrofs(value, "+", 0);
76                                 valueoffset = "";
77                                 if(valuefieldpos != -1)
78                                 {
79                                         valueoffset = substring(value, valuefieldpos + 1, strlen(value) - valuefieldpos - 1);
80                                         value = substring(value, 0, valuefieldpos);
81                                 }
82
83                                 valuefieldpos = strstrofs(valueoffset, "+", 0);
84                                 valueoffsetrandom = "";
85                                 if(valuefieldpos != -1)
86                                 {
87                                         valueoffsetrandom = substring(valueoffset, valuefieldpos + 1, strlen(valueoffset) - valuefieldpos - 1);
88                                         valueoffset = substring(valueoffset, 0, valuefieldpos);
89                                 }
90
91                                 valuefieldpos = strstrofs(value, ".", 0);
92                                 valuefield = "";
93                                 if(valuefieldpos != -1)
94                                 {
95                                         valuefield = substring(value, valuefieldpos + 1, strlen(value) - valuefieldpos - 1);
96                                         value = substring(value, 0, valuefieldpos);
97                                 }
98
99                                 if(value == "self")
100                                 {
101                                         valueent = self;
102                                         value = "";
103                                 }
104                                 else if(value == "activator")
105                                 {
106                                         valueent = act;
107                                         value = "";
108                                 }
109                                 else if(value == "other")
110                                 {
111                                         valueent = other;
112                                         value = "";
113                                 }
114                                 else if(value == "pusher")
115                                 {
116                                         if(time < act.pushltime)
117                                                 valueent = act.pusher;
118                                         else
119                                                 valueent = world;
120                                         value = "";
121                                 }
122                                 else if(value == "target")
123                                 {
124                                         valueent = e;
125                                         value = "";
126                                 }
127                                 else if(value == "killtarget")
128                                 {
129                                         valueent = kt;
130                                         value = "";
131                                 }
132                                 else if(value == "target2")
133                                 {
134                                         valueent = t2;
135                                         value = "";
136                                 }
137                                 else if(value == "target3")
138                                 {
139                                         valueent = t3;
140                                         value = "";
141                                 }
142                                 else if(value == "target4")
143                                 {
144                                         valueent = t4;
145                                         value = "";
146                                 }
147                                 else if(value == "time")
148                                 {
149                                         valueent = world;
150                                         value = ftos(time);
151                                 }
152                                 else
153                                 {
154                                         LOG_INFO("target_spawn: invalid/unknown variable replacement ", value, " specified, ignored!\n");
155                                         continue;
156                                 }
157
158                                 if(valuefield == "")
159                                 {
160                                         if(value == "")
161                                                 value = ftos(num_for_edict(valueent));
162                                 }
163                                 else
164                                 {
165                                         if(value != "")
166                                         {
167                                                 LOG_INFO("target_spawn: try to get a field of a non-entity, ignored!\n");
168                                                 continue;
169                                         }
170                                         data2 = stov(db_get(TemporaryDB, strcat("/target_spawn/field/", valuefield)));
171                                         if(data2_y == 0) // undefined field, i.e., invalid type
172                                         {
173                                                 LOG_INFO("target_spawn: invalid/unknown entity key replacement ", valuefield, " specified, ignored!\n");
174                                                 continue;
175                                         }
176                                         value = getentityfieldstring(data2_x, valueent);
177                                 }
178
179                                 if(valueoffset != "")
180                                 {
181                                         switch(data.y)
182                                         {
183                                                 case FIELD_STRING:
184                                                         value = strcat(value, valueoffset);
185                                                         break;
186                                                 case FIELD_FLOAT:
187                                                         value = ftos(stof(value) + stof(valueoffset));
188                                                         break;
189                                                 case FIELD_VECTOR:
190                                                         value = vtos(stov(value) + stov(valueoffset));
191                                                         break;
192                                                 default:
193                                                         LOG_INFO("target_spawn: only string, float and vector fields can do calculations, calculation ignored!\n");
194                                                         break;
195                                         }
196                                 }
197
198                                 if(valueoffsetrandom != "")
199                                 {
200                                         switch(data.y)
201                                         {
202                                                 case FIELD_FLOAT:
203                                                         value = ftos(stof(value) + random() * stof(valueoffsetrandom));
204                                                         break;
205                                                 case FIELD_VECTOR:
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');
208                                                         break;
209                                                 default:
210                                                         LOG_INFO("target_spawn: only float and vector fields can do random calculations, calculation ignored!\n");
211                                                         break;
212                                         }
213                                 }
214                         }
215                 }
216                 if(key == "$")
217                 {
218                         if(substring(value, 0, 1) == "_")
219                                 value = strcat("target_spawn_helper", value);
220                         putentityfieldstring(target_spawn_spawnfunc_field, e, value);
221
222                         oldactivator = activator;
223
224                         activator = act;
225                         WITH(entity, self, e, e.target_spawn_spawnfunc());
226                         activator = oldactivator;
227
228                         // We called an external function, so we have to re-tokenize msg.
229                         n = tokenize_console(msg);
230                 }
231                 else
232                 {
233                         if(data.y == FIELD_VECTOR)
234                                 value = strreplace("'", "", value); // why?!?
235                         putentityfieldstring(data.x, e, value);
236                 }
237         }
238 }
239
240 void target_spawn_useon(entity e)
241 {SELFPARAM();
242         self.target_spawn_activator = activator;
243         target_spawn_edit_entity(
244                 e,
245                 self.message,
246                 find(world, targetname, self.killtarget),
247                 find(world, targetname, self.target2),
248                 find(world, targetname, self.target3),
249                 find(world, targetname, self.target4),
250                 activator
251         );
252 }
253
254 float target_spawn_cancreate()
255 {SELFPARAM();
256         float c;
257         entity e;
258
259         c = self.count;
260         if(c == 0) // no limit?
261                 return 1;
262
263         ++c; // increase count to not include MYSELF
264         for(e = world; (e = findfloat(e, target_spawn_id, self.target_spawn_id)); --c)
265                 ;
266
267         // if c now is 0, we have AT LEAST the given count (maybe more), so don't spawn any more
268         if(c == 0)
269                 return 0;
270         return 1;
271 }
272
273 void target_spawn_use()
274 {SELFPARAM();
275         entity e;
276
277         if(self.target == "")
278         {
279                 // spawn new entity
280                 if(!target_spawn_cancreate())
281                         return;
282                 e = spawn();
283                 e.spawnfunc_checked = true;
284                 target_spawn_useon(e);
285                 e.target_spawn_id = self.target_spawn_id;
286         }
287         else if(self.target == "*activator")
288         {
289                 // edit entity
290                 if(activator)
291                         target_spawn_useon(activator);
292         }
293         else
294         {
295                 // edit entity
296                 for(e = world; (e = find(e, targetname, self.target)); )
297                         target_spawn_useon(e);
298         }
299 }
300
301 void target_spawn_spawnfirst()
302 {SELFPARAM();
303         activator = self.target_spawn_activator;
304         if(self.spawnflags & 2)
305                 target_spawn_use();
306 }
307
308 void initialize_field_db()
309 {
310         if(!target_spawn_initialized)
311         {
312                 float n, i;
313                 string fn;
314                 vector prev, next;
315                 float ft;
316
317                 n = numentityfields();
318                 for(i = 0; i < n; ++i)
319                 {
320                         fn = entityfieldname(i);
321                         ft = entityfieldtype(i);
322                         next = i * '1 0 0' + ft * '0 1 0' + '0 0 1';
323                         prev = stov(db_get(TemporaryDB, strcat("/target_spawn/field/", fn)));
324                         if(prev.y == 0)
325                         {
326                                 db_put(TemporaryDB, strcat("/target_spawn/field/", fn), vtos(next));
327                                 if(fn == "target_spawn_spawnfunc")
328                                         target_spawn_spawnfunc_field = i;
329                         }
330                 }
331
332                 target_spawn_initialized = 1;
333         }
334 }
335
336 spawnfunc(target_spawn)
337 {
338         initialize_field_db();
339         self.use = target_spawn_use;
340         self.message = strzone(strreplace("'", "\"", self.message));
341         self.target_spawn_id = ++target_spawn_count;
342         InitializeEntity(self, target_spawn_spawnfirst, INITPRIO_LAST);
343 }
344 #endif