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