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