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