]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/triggers/func/train.qc
Merge branch 'master' into terencehill/menu_optimization
[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 REGISTER_NET_LINKED(ENT_CLIENT_TRAIN)
105
106 #ifdef SVQC
107 float train_send(entity to, float sf)
108 {SELFPARAM();
109         WriteHeader(MSG_ENTITY, ENT_CLIENT_TRAIN);
110         WriteByte(MSG_ENTITY, sf);
111
112         if(sf & SF_TRIGGER_INIT)
113         {
114                 WriteString(MSG_ENTITY, self.platmovetype);
115                 WriteByte(MSG_ENTITY, self.platmovetype_turn);
116                 WriteByte(MSG_ENTITY, self.spawnflags);
117
118                 WriteString(MSG_ENTITY, self.model);
119
120                 trigger_common_write(self, true);
121
122                 WriteString(MSG_ENTITY, self.curvetarget);
123
124                 WriteCoord(MSG_ENTITY, self.pos1_x);
125                 WriteCoord(MSG_ENTITY, self.pos1_y);
126                 WriteCoord(MSG_ENTITY, self.pos1_z);
127                 WriteCoord(MSG_ENTITY, self.pos2_x);
128                 WriteCoord(MSG_ENTITY, self.pos2_y);
129                 WriteCoord(MSG_ENTITY, self.pos2_z);
130
131                 WriteCoord(MSG_ENTITY, self.size_x);
132                 WriteCoord(MSG_ENTITY, self.size_y);
133                 WriteCoord(MSG_ENTITY, self.size_z);
134
135                 WriteCoord(MSG_ENTITY, self.view_ofs_x);
136                 WriteCoord(MSG_ENTITY, self.view_ofs_y);
137                 WriteCoord(MSG_ENTITY, self.view_ofs_z);
138
139                 WriteAngle(MSG_ENTITY, self.mangle_x);
140                 WriteAngle(MSG_ENTITY, self.mangle_y);
141                 WriteAngle(MSG_ENTITY, self.mangle_z);
142
143                 WriteShort(MSG_ENTITY, self.speed);
144                 WriteShort(MSG_ENTITY, self.height);
145                 WriteByte(MSG_ENTITY, self.lip);
146                 WriteByte(MSG_ENTITY, self.state);
147                 WriteByte(MSG_ENTITY, self.wait);
148
149                 WriteShort(MSG_ENTITY, self.dmg);
150                 WriteByte(MSG_ENTITY, self.dmgtime);
151         }
152
153         if(sf & SF_TRIGGER_RESET)
154         {
155                 // used on client
156         }
157
158         return true;
159 }
160
161 void train_link()
162 {
163         //Net_LinkEntity(self, 0, false, train_send);
164 }
165
166 void train_use()
167 {
168         self.SUB_NEXTTHINK = self.SUB_LTIME + 1;
169         self.SUB_THINK = train_next;
170         self.use = func_null; // not again
171 }
172
173 void func_train_find()
174 {SELFPARAM();
175         entity targ;
176         targ = find(world, targetname, self.target);
177         self.target = targ.target;
178         if (self.target == "")
179                 objerror("func_train_find: no next target");
180         SUB_SETORIGIN(self, targ.origin - self.view_ofs);
181
182         if(!(self.spawnflags & 4))
183         {
184                 self.SUB_NEXTTHINK = self.SUB_LTIME + 1;
185                 self.SUB_THINK = train_next;
186         }
187
188         train_link();
189 }
190
191 #endif
192
193 /*QUAKED spawnfunc_func_train (0 .5 .8) ?
194 Ridable platform, targets spawnfunc_path_corner path to follow.
195 speed : speed the train moves (can be overridden by each spawnfunc_path_corner)
196 target : targetname of first spawnfunc_path_corner (starts here)
197 */
198 #ifdef SVQC
199 spawnfunc(func_train)
200 {
201         if (self.noise != "")
202                 precache_sound(self.noise);
203
204         if (self.target == "")
205                 objerror("func_train without a target");
206         if (!self.speed)
207                 self.speed = 100;
208
209         if (!InitMovingBrushTrigger())
210                 return;
211         self.effects |= EF_LOWPRECISION;
212
213         if(self.spawnflags & 4)
214                 self.use = train_use;
215
216         if (self.spawnflags & 2)
217         {
218                 self.platmovetype_turn = true;
219                 self.view_ofs = '0 0 0'; // don't offset a rotating train, origin works differently now
220         }
221         else
222                 self.view_ofs = self.mins;
223
224         // wait for targets to spawn
225         InitializeEntity(self, func_train_find, INITPRIO_FINDTARGET);
226
227         self.blocked = generic_plat_blocked;
228         if(self.dmg && (self.message == ""))
229                 self.message = " was squished";
230     if(self.dmg && (self.message2 == ""))
231                 self.message2 = "was squished by";
232         if(self.dmg && (!self.dmgtime))
233                 self.dmgtime = 0.25;
234         self.dmgtime2 = time;
235
236         if(!set_platmovetype(self, self.platmovetype))
237                 return;
238         self.platmovetype_start_default = self.platmovetype_start;
239         self.platmovetype_end_default = self.platmovetype_end;
240
241         // TODO make a reset function for this one
242 }
243 #elif defined(CSQC)
244 void train_draw(entity this)
245 {
246         //Movetype_Physics_NoMatchServer();
247         Movetype_Physics_MatchServer(this, autocvar_cl_projectiles_sloppy);
248 }
249
250 NET_HANDLE(ENT_CLIENT_TRAIN, bool isnew)
251 {
252         float sf = ReadByte();
253
254         if(sf & SF_TRIGGER_INIT)
255         {
256                 self.platmovetype = strzone(ReadString());
257                 self.platmovetype_turn = ReadByte();
258                 self.spawnflags = ReadByte();
259
260                 self.model = strzone(ReadString());
261                 _setmodel(self, self.model);
262
263                 trigger_common_read(true);
264
265                 self.curvetarget = strzone(ReadString());
266
267                 self.pos1_x = ReadCoord();
268                 self.pos1_y = ReadCoord();
269                 self.pos1_z = ReadCoord();
270                 self.pos2_x = ReadCoord();
271                 self.pos2_y = ReadCoord();
272                 self.pos2_z = ReadCoord();
273
274                 self.size_x = ReadCoord();
275                 self.size_y = ReadCoord();
276                 self.size_z = ReadCoord();
277
278                 self.view_ofs_x = ReadCoord();
279                 self.view_ofs_y = ReadCoord();
280                 self.view_ofs_z = ReadCoord();
281
282                 self.mangle_x = ReadAngle();
283                 self.mangle_y = ReadAngle();
284                 self.mangle_z = ReadAngle();
285
286                 self.speed = ReadShort();
287                 self.height = ReadShort();
288                 self.lip = ReadByte();
289                 self.state = ReadByte();
290                 self.wait = ReadByte();
291
292                 self.dmg = ReadShort();
293                 self.dmgtime = ReadByte();
294
295                 self.classname = "func_train";
296                 self.solid = SOLID_BSP;
297                 self.movetype = MOVETYPE_PUSH;
298                 self.drawmask = MASK_NORMAL;
299                 self.draw = train_draw;
300                 self.entremove = trigger_remove_generic;
301
302                 if(set_platmovetype(self, self.platmovetype))
303                 {
304                         self.platmovetype_start_default = self.platmovetype_start;
305                         self.platmovetype_end_default = self.platmovetype_end;
306                 }
307
308                 // everything is set up by the time the train is linked, we shouldn't need this
309                 //func_train_find();
310
311                 // but we will need these
312                 //self.move_nextthink = self.move_ltime + 0.1;
313                 //self.move_think = train_next;
314                 train_next();
315
316                 self.move_movetype = MOVETYPE_PUSH;
317                 self.move_origin = self.origin;
318                 self.move_angles = self.angles;
319                 self.move_time = time;
320         }
321
322         if(sf & SF_TRIGGER_RESET)
323         {
324                 // TODO: make a reset function for trains
325         }
326
327         return true;
328 }
329
330 #endif