Draw: purge SELFPARAM
[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         WriteByte(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 func_train_find()
165 {SELFPARAM();
166         entity targ;
167         targ = find(world, targetname, self.target);
168         self.target = targ.target;
169         if (self.target == "")
170                 objerror("func_train_find: no next target");
171         SUB_SETORIGIN(self, targ.origin - self.view_ofs);
172         self.SUB_NEXTTHINK = self.SUB_LTIME + 1;
173         self.SUB_THINK = train_next;
174
175         train_link();
176 }
177
178 #endif
179
180 /*QUAKED spawnfunc_func_train (0 .5 .8) ?
181 Ridable platform, targets spawnfunc_path_corner path to follow.
182 speed : speed the train moves (can be overridden by each spawnfunc_path_corner)
183 target : targetname of first spawnfunc_path_corner (starts here)
184 */
185 #ifdef SVQC
186 spawnfunc(func_train)
187 {
188         if (self.noise != "")
189                 precache_sound(self.noise);
190
191         if (self.target == "")
192                 objerror("func_train without a target");
193         if (!self.speed)
194                 self.speed = 100;
195
196         if (!InitMovingBrushTrigger())
197                 return;
198         self.effects |= EF_LOWPRECISION;
199
200         if (self.spawnflags & 2)
201         {
202                 self.platmovetype_turn = true;
203                 self.view_ofs = '0 0 0'; // don't offset a rotating train, origin works differently now
204         }
205         else
206                 self.view_ofs = self.mins;
207
208         // wait for targets to spawn
209         InitializeEntity(self, func_train_find, INITPRIO_FINDTARGET);
210
211         self.blocked = generic_plat_blocked;
212         if(self.dmg && (self.message == ""))
213                 self.message = " was squished";
214     if(self.dmg && (self.message2 == ""))
215                 self.message2 = "was squished by";
216         if(self.dmg && (!self.dmgtime))
217                 self.dmgtime = 0.25;
218         self.dmgtime2 = time;
219
220         if(!set_platmovetype(self, self.platmovetype))
221                 return;
222         self.platmovetype_start_default = self.platmovetype_start;
223         self.platmovetype_end_default = self.platmovetype_end;
224
225         // TODO make a reset function for this one
226 }
227 #elif defined(CSQC)
228 void train_draw(entity this)
229 {
230         //Movetype_Physics_NoMatchServer();
231         Movetype_Physics_MatchServer(autocvar_cl_projectiles_sloppy);
232 }
233
234 void ent_train()
235 {SELFPARAM();
236         float sf = ReadByte();
237
238         if(sf & SF_TRIGGER_INIT)
239         {
240                 self.platmovetype = strzone(ReadString());
241                 self.platmovetype_turn = ReadByte();
242                 self.spawnflags = ReadByte();
243
244                 self.model = strzone(ReadString());
245                 _setmodel(self, self.model);
246
247                 trigger_common_read(true);
248
249                 self.curvetarget = strzone(ReadString());
250
251                 self.pos1_x = ReadCoord();
252                 self.pos1_y = ReadCoord();
253                 self.pos1_z = ReadCoord();
254                 self.pos2_x = ReadCoord();
255                 self.pos2_y = ReadCoord();
256                 self.pos2_z = ReadCoord();
257
258                 self.size_x = ReadCoord();
259                 self.size_y = ReadCoord();
260                 self.size_z = ReadCoord();
261
262                 self.view_ofs_x = ReadCoord();
263                 self.view_ofs_y = ReadCoord();
264                 self.view_ofs_z = ReadCoord();
265
266                 self.mangle_x = ReadAngle();
267                 self.mangle_y = ReadAngle();
268                 self.mangle_z = ReadAngle();
269
270                 self.speed = ReadShort();
271                 self.height = ReadShort();
272                 self.lip = ReadByte();
273                 self.state = ReadByte();
274                 self.wait = ReadByte();
275
276                 self.dmg = ReadShort();
277                 self.dmgtime = ReadByte();
278
279                 self.classname = "func_train";
280                 self.solid = SOLID_BSP;
281                 self.movetype = MOVETYPE_PUSH;
282                 self.drawmask = MASK_NORMAL;
283                 self.draw = train_draw;
284                 self.entremove = trigger_remove_generic;
285
286                 if(set_platmovetype(self, self.platmovetype))
287                 {
288                         self.platmovetype_start_default = self.platmovetype_start;
289                         self.platmovetype_end_default = self.platmovetype_end;
290                 }
291
292                 // everything is set up by the time the train is linked, we shouldn't need this
293                 //func_train_find();
294
295                 // but we will need these
296                 //self.move_nextthink = self.move_ltime + 0.1;
297                 //self.move_think = train_next;
298                 train_next();
299
300                 self.move_movetype = MOVETYPE_PUSH;
301                 self.move_origin = self.origin;
302                 self.move_angles = self.angles;
303                 self.move_time = time;
304         }
305
306         if(sf & SF_TRIGGER_RESET)
307         {
308                 // TODO: make a reset function for trains
309         }
310 }
311
312 #endif