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