]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/triggers/func/train.qc
Also let the first target of func_train be random
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / triggers / func / train.qc
1 #include "train.qh"
2 .float train_wait_turning;
3 void train_next(entity this);
4 #ifdef SVQC
5 void train_use(entity this, entity actor, entity trigger);
6 #endif
7 void train_wait(entity this)
8 {
9         SUB_UseTargets(this.enemy, NULL, NULL);
10         this.enemy = NULL;
11
12         // if turning is enabled, the train will turn toward the next point while waiting
13         if(this.platmovetype_turn && !this.train_wait_turning)
14         {
15                 entity targ, cp;
16                 vector ang;
17                 targ = find(NULL, targetname, this.target);
18                 if((this.spawnflags & 1) && targ.curvetarget)
19                         cp = find(NULL, targetname, targ.curvetarget);
20                 else
21                         cp = NULL;
22
23                 if(cp) // bezier curves movement
24                         ang = cp.origin - (this.origin - this.view_ofs); // use the origin of the control point of the next path_corner
25                 else // linear movement
26                         ang = targ.origin - (this.origin - this.view_ofs); // use the origin of the next path_corner
27                 ang = vectoangles(ang);
28                 ang_x = -ang_x; // flip up / down orientation
29
30                 if(this.wait > 0) // slow turning
31                         SUB_CalcAngleMove(this, ang, TSPEED_TIME, this.SUB_LTIME - time + this.wait, train_wait);
32                 else // instant turning
33                         SUB_CalcAngleMove(this, ang, TSPEED_TIME, 0.0000001, train_wait);
34                 this.train_wait_turning = true;
35                 return;
36         }
37
38 #ifdef SVQC
39         if(this.noise != "")
40                 stopsoundto(MSG_BROADCAST, this, CH_TRIGGER_SINGLE); // send this as unreliable only, as the train will resume operation shortly anyway
41 #endif
42
43 #ifdef SVQC
44         entity tg = find(NULL, targetname, this.target);
45         if(tg.spawnflags & 4)
46         {
47                 this.use = train_use;
48                 SUB_THINK(this, func_null);
49                 this.SUB_NEXTTHINK = 0;
50         }
51         else
52 #endif
53              if(this.wait < 0 || this.train_wait_turning) // no waiting or we already waited while turning
54         {
55                 this.train_wait_turning = false;
56                 train_next(this);
57         }
58         else
59         {
60                 SUB_THINK(this, train_next);
61                 this.SUB_NEXTTHINK = this.SUB_LTIME + this.wait;
62         }
63 }
64
65 entity train_next_find(entity this)
66 {
67         if(this.target_random)
68         {
69                 RandomSelection_Init();
70                 for(entity t = NULL; (t = find(t, targetname, this.target));)
71                 {
72                         RandomSelection_AddEnt(t, 1, 0);
73                 }
74                 return = RandomSelection_chosen_ent;
75         }
76         else
77         {
78                 return find(NULL, targetname, this.target);
79         }
80 }
81
82 void train_next(entity this)
83 {
84         entity targ = NULL, cp = NULL;
85         vector cp_org = '0 0 0';
86
87         targ = train_next_find(this);
88
89         this.target = targ.target;
90         this.target_random = targ.target_random;
91
92         if (this.spawnflags & 1)
93         {
94                 if(targ.curvetarget)
95                 {
96                         cp = find(NULL, targetname, targ.curvetarget); // get its second target (the control point)
97                         cp_org = cp.origin - this.view_ofs; // no control point found, assume a straight line to the destination
98                 }
99         }
100         if (this.target == "")
101                 objerror(this, "train_next: no next target");
102         this.wait = targ.wait;
103         if (!this.wait)
104                 this.wait = 0.1;
105
106         if(targ.platmovetype)
107         {
108                 // this path_corner contains a movetype overrider, apply it
109                 this.platmovetype_start = targ.platmovetype_start;
110                 this.platmovetype_end = targ.platmovetype_end;
111         }
112         else
113         {
114                 // this path_corner doesn't contain a movetype overrider, use the train's defaults
115                 this.platmovetype_start = this.platmovetype_start_default;
116                 this.platmovetype_end = this.platmovetype_end_default;
117         }
118
119         if (targ.speed)
120         {
121                 if (cp)
122                         SUB_CalcMove_Bezier(this, cp_org, targ.origin - this.view_ofs, TSPEED_LINEAR, targ.speed, train_wait);
123                 else
124                         SUB_CalcMove(this, targ.origin - this.view_ofs, TSPEED_LINEAR, targ.speed, train_wait);
125         }
126         else
127         {
128                 if (cp)
129                         SUB_CalcMove_Bezier(this, cp_org, targ.origin - this.view_ofs, TSPEED_LINEAR, this.speed, train_wait);
130                 else
131                         SUB_CalcMove(this, targ.origin - this.view_ofs, TSPEED_LINEAR, this.speed, train_wait);
132         }
133
134         if(this.noise != "")
135                 _sound(this, CH_TRIGGER_SINGLE, this.noise, VOL_BASE, ATTEN_IDLE);
136 }
137
138 REGISTER_NET_LINKED(ENT_CLIENT_TRAIN)
139
140 #ifdef SVQC
141 float train_send(entity this, entity to, float sf)
142 {
143         WriteHeader(MSG_ENTITY, ENT_CLIENT_TRAIN);
144         WriteByte(MSG_ENTITY, sf);
145
146         if(sf & SF_TRIGGER_INIT)
147         {
148                 WriteString(MSG_ENTITY, this.platmovetype);
149                 WriteByte(MSG_ENTITY, this.platmovetype_turn);
150                 WriteByte(MSG_ENTITY, this.spawnflags);
151
152                 WriteString(MSG_ENTITY, this.model);
153
154                 trigger_common_write(this, true);
155
156                 WriteString(MSG_ENTITY, this.curvetarget);
157
158                 WriteCoord(MSG_ENTITY, this.pos1_x);
159                 WriteCoord(MSG_ENTITY, this.pos1_y);
160                 WriteCoord(MSG_ENTITY, this.pos1_z);
161                 WriteCoord(MSG_ENTITY, this.pos2_x);
162                 WriteCoord(MSG_ENTITY, this.pos2_y);
163                 WriteCoord(MSG_ENTITY, this.pos2_z);
164
165                 WriteCoord(MSG_ENTITY, this.size_x);
166                 WriteCoord(MSG_ENTITY, this.size_y);
167                 WriteCoord(MSG_ENTITY, this.size_z);
168
169                 WriteCoord(MSG_ENTITY, this.view_ofs_x);
170                 WriteCoord(MSG_ENTITY, this.view_ofs_y);
171                 WriteCoord(MSG_ENTITY, this.view_ofs_z);
172
173                 WriteAngle(MSG_ENTITY, this.mangle_x);
174                 WriteAngle(MSG_ENTITY, this.mangle_y);
175                 WriteAngle(MSG_ENTITY, this.mangle_z);
176
177                 WriteShort(MSG_ENTITY, this.speed);
178                 WriteShort(MSG_ENTITY, this.height);
179                 WriteByte(MSG_ENTITY, this.lip);
180                 WriteByte(MSG_ENTITY, this.state);
181                 WriteByte(MSG_ENTITY, this.wait);
182
183                 WriteShort(MSG_ENTITY, this.dmg);
184                 WriteByte(MSG_ENTITY, this.dmgtime);
185         }
186
187         if(sf & SF_TRIGGER_RESET)
188         {
189                 // used on client
190         }
191
192         return true;
193 }
194
195 void train_link(entity this)
196 {
197         //Net_LinkEntity(this, 0, false, train_send);
198 }
199
200 void train_use(entity this, entity actor, entity trigger)
201 {
202         this.SUB_NEXTTHINK = this.SUB_LTIME + 1;
203         SUB_THINK(this, train_next);
204         this.use = func_null; // not again
205 }
206
207 void func_train_find(entity this)
208 {
209         entity targ = train_next_find(this);
210         this.target = targ.target;
211         this.target_random = targ.target_random;
212         if (this.target == "")
213                 objerror(this, "func_train_find: no next target");
214         SUB_SETORIGIN(this, targ.origin - this.view_ofs);
215
216         if(!(this.spawnflags & 4))
217         {
218                 this.SUB_NEXTTHINK = this.SUB_LTIME + 1;
219                 SUB_THINK(this, train_next);
220         }
221
222         train_link(this);
223 }
224
225 #endif
226
227 /*QUAKED spawnfunc_func_train (0 .5 .8) ?
228 Ridable platform, targets spawnfunc_path_corner path to follow.
229 speed : speed the train moves (can be overridden by each spawnfunc_path_corner)
230 target : targetname of first spawnfunc_path_corner (starts here)
231 */
232 #ifdef SVQC
233 spawnfunc(func_train)
234 {
235         if (this.noise != "")
236                 precache_sound(this.noise);
237
238         if (this.target == "")
239                 objerror(this, "func_train without a target");
240         if (!this.speed)
241                 this.speed = 100;
242
243         if (!InitMovingBrushTrigger(this))
244                 return;
245         this.effects |= EF_LOWPRECISION;
246
247         if(this.spawnflags & 4)
248                 this.use = train_use;
249
250         if (this.spawnflags & 2)
251         {
252                 this.platmovetype_turn = true;
253                 this.view_ofs = '0 0 0'; // don't offset a rotating train, origin works differently now
254         }
255         else
256                 this.view_ofs = this.mins;
257
258         // wait for targets to spawn
259         InitializeEntity(this, func_train_find, INITPRIO_FINDTARGET);
260
261         setblocked(this, generic_plat_blocked);
262         if(this.dmg && (this.message == ""))
263                 this.message = " was squished";
264     if(this.dmg && (this.message2 == ""))
265                 this.message2 = "was squished by";
266         if(this.dmg && (!this.dmgtime))
267                 this.dmgtime = 0.25;
268         this.dmgtime2 = time;
269
270         if(!set_platmovetype(this, this.platmovetype))
271                 return;
272         this.platmovetype_start_default = this.platmovetype_start;
273         this.platmovetype_end_default = this.platmovetype_end;
274
275         // TODO make a reset function for this one
276 }
277 #elif defined(CSQC)
278 void train_draw(entity this)
279 {
280         //Movetype_Physics_NoMatchServer();
281         Movetype_Physics_MatchServer(this, autocvar_cl_projectiles_sloppy);
282 }
283
284 NET_HANDLE(ENT_CLIENT_TRAIN, bool isnew)
285 {
286         float sf = ReadByte();
287
288         if(sf & SF_TRIGGER_INIT)
289         {
290                 this.platmovetype = strzone(ReadString());
291                 this.platmovetype_turn = ReadByte();
292                 this.spawnflags = ReadByte();
293
294                 this.model = strzone(ReadString());
295                 _setmodel(this, this.model);
296
297                 trigger_common_read(this, true);
298
299                 this.curvetarget = strzone(ReadString());
300
301                 this.pos1_x = ReadCoord();
302                 this.pos1_y = ReadCoord();
303                 this.pos1_z = ReadCoord();
304                 this.pos2_x = ReadCoord();
305                 this.pos2_y = ReadCoord();
306                 this.pos2_z = ReadCoord();
307
308                 this.size_x = ReadCoord();
309                 this.size_y = ReadCoord();
310                 this.size_z = ReadCoord();
311
312                 this.view_ofs_x = ReadCoord();
313                 this.view_ofs_y = ReadCoord();
314                 this.view_ofs_z = ReadCoord();
315
316                 this.mangle_x = ReadAngle();
317                 this.mangle_y = ReadAngle();
318                 this.mangle_z = ReadAngle();
319
320                 this.speed = ReadShort();
321                 this.height = ReadShort();
322                 this.lip = ReadByte();
323                 this.state = ReadByte();
324                 this.wait = ReadByte();
325
326                 this.dmg = ReadShort();
327                 this.dmgtime = ReadByte();
328
329                 this.classname = "func_train";
330                 this.solid = SOLID_BSP;
331                 set_movetype(this, MOVETYPE_PUSH);
332                 this.drawmask = MASK_NORMAL;
333                 this.draw = train_draw;
334                 if (isnew) IL_PUSH(g_drawables, this);
335                 this.entremove = trigger_remove_generic;
336
337                 if(set_platmovetype(this, this.platmovetype))
338                 {
339                         this.platmovetype_start_default = this.platmovetype_start;
340                         this.platmovetype_end_default = this.platmovetype_end;
341                 }
342
343                 // everything is set up by the time the train is linked, we shouldn't need this
344                 //func_train_find();
345
346                 // but we will need these
347                 train_next(this);
348
349                 set_movetype(this, MOVETYPE_PUSH);
350                 this.move_time = time;
351         }
352
353         if(sf & SF_TRIGGER_RESET)
354         {
355                 // TODO: make a reset function for trains
356         }
357
358         return true;
359 }
360
361 #endif