]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/target_spawn.qc
fix turrets for onslaught: default them to have no team and attack nobody if not...
[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
13 void target_spawn_useon(entity e)
14 {
15         float i, n, valuefieldpos;
16         string key, value, valuefield, valueoffset, valueoffsetrandom;
17         entity valueent;
18         vector data, data2;
19         entity oldself;
20         entity oldactivator;
21
22         n = tokenize_sane(self.message);
23
24         for(i = 0; i < n-1; i += 2)
25         {
26                 key = argv(i);
27                 value = argv(i+1);
28                 if(key == "$")
29                 {
30                         data_x = -1;
31                         data_y = FIELD_STRING;
32                 }
33                 else
34                 {
35                         data = stov(db_get(TemporaryDB, strcat("/target_spawn/field/", key)));
36                         if(data_y == 0) // undefined field, i.e., invalid type
37                         {
38                                 print("target_spawn: invalid/unknown entity key ", key, " specified, ignored!\n");
39                                 continue;
40                         }
41                 }
42                 if(substring(value, 0, 1) == "$")
43                 {
44                         value = substring(value, 1, strlen(value) - 1);
45                         if(substring(value, 0, 1) == "$")
46                         {
47                                 // deferred replacement
48                                 // do nothing
49                                 // useful for creating target_spawns with this!
50                         }
51                         else
52                         {
53                                 // replace me!
54                                 valuefieldpos = strstrofs(value, "+", 0);
55                                 valueoffset = "";
56                                 if(valuefieldpos != -1)
57                                 {
58                                         valueoffset = substring(value, valuefieldpos + 1, strlen(value) - valuefieldpos - 1);
59                                         value = substring(value, 0, valuefieldpos);
60                                 }
61
62                                 valuefieldpos = strstrofs(valueoffset, "+", 0);
63                                 valueoffsetrandom = "";
64                                 if(valuefieldpos != -1)
65                                 {
66                                         valueoffsetrandom = substring(valueoffset, valuefieldpos + 1, strlen(valueoffset) - valuefieldpos - 1);
67                                         valueoffset = substring(valueoffset, 0, valuefieldpos);
68                                 }
69
70                                 valuefieldpos = strstrofs(value, ".", 0);
71                                 valuefield = "";
72                                 if(valuefieldpos != -1)
73                                 {
74                                         valuefield = substring(value, valuefieldpos + 1, strlen(value) - valuefieldpos - 1);
75                                         value = substring(value, 0, valuefieldpos);
76                                 }
77
78                                 if(value == "self")
79                                 {
80                                         valueent = self;
81                                         value = "";
82                                 }
83                                 else if(value == "activator")
84                                 {
85                                         valueent = activator;
86                                         value = "";
87                                 }
88                                 else if(value == "pusher")
89                                 {
90                                         if(time < activator.pushltime)
91                                                 valueent = activator.pusher;
92                                         else
93                                                 valueent = world;
94                                         value = "";
95                                 }
96                                 else if(value == "target")
97                                 {
98                                         valueent = e;
99                                         value = "";
100                                 }
101                                 else if(value == "time")
102                                 {
103                                         valueent = world;
104                                         value = ftos(time);
105                                 }
106                                 else
107                                 {
108                                         print("target_spawn: invalid/unknown variable replacement ", value, " specified, ignored!\n");
109                                         continue;
110                                 }
111
112                                 if(valuefield == "")
113                                 {
114                                         if(value == "")
115                                                 value = ftos(num_for_edict(valueent));
116                                 }
117                                 else
118                                 {
119                                         if(value != "")
120                                         {
121                                                 print("target_spawn: try to get a field of a non-entity, ignored!\n");
122                                                 continue;
123                                         }
124                                         data2 = stov(db_get(TemporaryDB, strcat("/target_spawn/field/", valuefield)));
125                                         if(data2_y == 0) // undefined field, i.e., invalid type
126                                         {
127                                                 print("target_spawn: invalid/unknown entity key replacement ", valuefield, " specified, ignored!\n");
128                                                 continue;
129                                         }
130                                         value = getentityfieldstring(data2_x, valueent);
131                                 }
132
133                                 if(valueoffset != "")
134                                 {
135                                         switch(data_y)
136                                         {
137                                                 case FIELD_STRING:
138                                                         value = strcat(value, valueoffset);
139                                                         break;
140                                                 case FIELD_FLOAT:
141                                                         value = ftos(stof(value) + stof(valueoffset));
142                                                         break;
143                                                 case FIELD_VECTOR:
144                                                         value = vtos(stov(value) + stov(valueoffset));
145                                                         break;
146                                                 default:
147                                                         print("target_spawn: only string, float and vector fields can do calculations, calculation ignored!\n");
148                                                         break;
149                                         }
150                                 }
151
152                                 if(valueoffsetrandom != "")
153                                 {
154                                         switch(data_y)
155                                         {
156                                                 case FIELD_FLOAT:
157                                                         value = ftos(stof(value) + random() * stof(valueoffsetrandom));
158                                                         break;
159                                                 case FIELD_VECTOR:
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');
162                                                         break;
163                                                 default:
164                                                         print("target_spawn: only float and vector fields can do random calculations, calculation ignored!\n");
165                                                         break;
166                                         }
167                                 }
168                         }
169                 }
170                 if(key == "$")
171                 {
172                         putentityfieldstring(target_spawn_spawnfunc_field, e, value);
173
174                         oldself = self;
175                         oldactivator = activator;
176
177                         self = e;
178                         activator = self.target_spawn_activator;
179
180                         self.target_spawn_spawnfunc();
181
182                         self = oldself;
183                         activator = oldactivator;
184                 }
185                 else
186                 {
187                         if(data_y == FIELD_VECTOR)
188                                 value = strreplace("'", "", value); // why?!?
189                         putentityfieldstring(data_x, e, value);
190                 }
191         }
192 }
193
194 void target_spawn_use()
195 {
196         entity e;
197
198         if(self.target == "")
199         {
200                 // spawn new entity
201                 e = spawn();
202                 target_spawn_useon(e);
203         }
204         else
205         {
206                 // edit entity
207                 for(e = world; (e = find(e, targetname, self.target)); )
208                         target_spawn_useon(e);
209         }
210 }
211
212 void target_spawn_spawnfirst()
213 {
214         activator = self.target_spawn_activator;
215         if(self.spawnflags & 2)
216                 target_spawn_use();
217 }
218
219 void spawnfunc_target_spawn()
220 {
221         if(!target_spawn_initialized)
222         {
223                 float n, i;
224                 string fn;
225                 vector prev, new;
226                 float ft;
227
228                 n = numentityfields();
229                 for(i = 0; i < n; ++i)
230                 {
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)));
235                         if(prev_y == 0)
236                         {
237                                 db_put(TemporaryDB, strcat("/target_spawn/field/", fn), vtos(new));
238                                 if(fn == "target_spawn_spawnfunc")
239                                         target_spawn_spawnfunc_field = i;
240                         }
241                 }
242
243                 target_spawn_initialized = 1;
244         }
245         self.use = target_spawn_use;
246         self.message = strzone(strreplace("'", "\"", self.message));
247         InitializeEntity(self, target_spawn_spawnfirst, INITPRIO_LAST);
248 }