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