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