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