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