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