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