]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/target_spawn.qc
bugfix target_spawn, and change docs to match it
[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 == "time")
97                                 {
98                                         valueent = world;
99                                         value = ftos(time);
100                                 }
101                                 else
102                                 {
103                                         print("target_spawn: invalid/unknown variable replacement ", value, " specified, ignored!\n");
104                                         continue;
105                                 }
106
107                                 if(valuefield == "")
108                                 {
109                                         if(value == "")
110                                                 value = ftos(num_for_edict(valueent));
111                                 }
112                                 else
113                                 {
114                                         if(value != "")
115                                         {
116                                                 print("target_spawn: try to get a field of a non-entity, ignored!\n");
117                                                 continue;
118                                         }
119                                         data2 = stov(db_get(TemporaryDB, strcat("/target_spawn/field/", valuefield)));
120                                         if(data2_y == 0) // undefined field, i.e., invalid type
121                                         {
122                                                 print("target_spawn: invalid/unknown entity key replacement ", valuefield, " specified, ignored!\n");
123                                                 continue;
124                                         }
125                                         value = getentityfieldstring(data2_x, valueent);
126                                 }
127
128                                 if(valueoffset != "")
129                                 {
130                                         switch(data_y)
131                                         {
132                                                 case FIELD_STRING:
133                                                         value = strcat(value, valueoffset);
134                                                         break;
135                                                 case FIELD_FLOAT:
136                                                         value = ftos(stof(value) + stof(valueoffset));
137                                                         break;
138                                                 case FIELD_VECTOR:
139                                                         value = vtos(stov(value) + stov(valueoffset));
140                                                         break;
141                                                 default:
142                                                         print("target_spawn: only string, float and vector fields can do calculations, calculation ignored!\n");
143                                                         break;
144                                         }
145                                 }
146
147                                 if(valueoffsetrandom != "")
148                                 {
149                                         switch(data_y)
150                                         {
151                                                 case FIELD_FLOAT:
152                                                         value = ftos(stof(value) + random() * stof(valueoffsetrandom));
153                                                         break;
154                                                 case FIELD_VECTOR:
155                                                         data2 = stov(valueoffsetrandom);
156                                                         value = vtos(stov(value) + random() * data2_x * '1 0 0' + random() * data2_y * '0 1 0' + random() * data2_z * '0 0 1');
157                                                         break;
158                                                 default:
159                                                         print("target_spawn: only float and vector fields can do random calculations, calculation ignored!\n");
160                                                         break;
161                                         }
162                                 }
163                         }
164                 }
165                 if(key == "$")
166                 {
167                         putentityfieldstring(target_spawn_spawnfunc_field, e, value);
168
169                         oldself = self;
170                         oldactivator = activator;
171
172                         self = e;
173                         activator = self.target_spawn_activator;
174
175                         self.target_spawn_spawnfunc();
176
177                         self = oldself;
178                         activator = oldactivator;
179                 }
180                 else
181                 {
182                         if(data_y == FIELD_VECTOR)
183                                 value = strreplace("'", "", value); // why?!?
184                         putentityfieldstring(data_x, e, value);
185                 }
186         }
187 }
188
189 void target_spawn_use()
190 {
191         entity e;
192
193         if(self.target == "")
194         {
195                 // spawn new entity
196                 e = spawn();
197                 target_spawn_useon(e);
198         }
199         else
200         {
201                 // edit entity
202                 for(e = world; (e = find(e, targetname, self.target)); )
203                         target_spawn_useon(e);
204         }
205 }
206
207 void target_spawn_spawnfirst()
208 {
209         activator = self.target_spawn_activator;
210         if(self.spawnflags & 2)
211                 target_spawn_use();
212 }
213
214 void spawnfunc_target_spawn()
215 {
216         if(!target_spawn_initialized)
217         {
218                 float n, i;
219                 string fn;
220                 vector prev, new;
221                 float ft;
222
223                 n = numentityfields();
224                 for(i = 0; i < n; ++i)
225                 {
226                         fn = entityfieldname(i);
227                         ft = entityfieldtype(i);
228                         new = i * '1 0 0' + ft * '0 1 0' + '0 0 1';
229                         prev = stov(db_get(TemporaryDB, strcat("/target_spawn/field/", fn)));
230                         if(prev_y == 0)
231                         {
232                                 db_put(TemporaryDB, strcat("/target_spawn/field/", fn), vtos(new));
233                                 if(fn == "target_spawn_spawnfunc")
234                                         target_spawn_spawnfunc_field = i;
235                         }
236                 }
237
238                 target_spawn_initialized = 1;
239         }
240         self.use = target_spawn_use;
241         self.message = strzone(strreplace("'", "\"", self.message));
242         InitializeEntity(self, target_spawn_spawnfirst, INITPRIO_LAST);
243 }