2 .float train_wait_turning;
4 void train_next(entity this);
6 void train_use(entity this, entity actor, entity trigger);
8 void train_wait(entity this)
10 SUB_UseTargets(this.enemy, NULL, NULL);
13 // if turning is enabled, the train will turn toward the next point while waiting
14 if(this.platmovetype_turn && !this.train_wait_turning)
18 targ = this.future_target;
19 if((this.spawnflags & TRAIN_CURVE) && targ.curvetarget)
20 cp = find(NULL, targetname, targ.curvetarget);
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 ang = vectoangles(ang);
29 ang_x = -ang_x; // flip up / down orientation
31 if(this.wait > 0) // slow turning
32 SUB_CalcAngleMove(this, ang, TSPEED_TIME, this.ltime - time + this.wait, train_wait);
33 else // instant turning
34 SUB_CalcAngleMove(this, ang, TSPEED_TIME, 0.0000001, train_wait);
35 this.train_wait_turning = true;
41 stopsoundto(MSG_BROADCAST, this, CH_TRIGGER_SINGLE); // send this as unreliable only, as the train will resume operation shortly anyway
45 entity tg = this.future_target;
46 if(tg.spawnflags & TRAIN_NEEDACTIVATION)
49 setthink(this, func_null);
54 if(this.wait < 0 || this.train_wait_turning) // no waiting or we already waited while turning
56 this.train_wait_turning = false;
61 setthink(this, train_next);
62 this.nextthink = this.ltime + this.wait;
66 entity train_next_find(entity this)
68 if(this.target_random)
70 RandomSelection_Init();
71 for(entity t = NULL; (t = find(t, targetname, this.target));)
73 RandomSelection_AddEnt(t, 1, 0);
75 return RandomSelection_chosen_ent;
79 return find(NULL, targetname, this.target);
83 void train_next(entity this)
85 entity targ = NULL, cp = NULL;
86 vector cp_org = '0 0 0';
88 targ = this.future_target;
90 this.target = targ.target;
91 this.target_random = targ.target_random;
92 this.future_target = train_next_find(targ);
94 if (this.spawnflags & TRAIN_CURVE)
98 cp = find(NULL, targetname, targ.curvetarget); // get its second target (the control point)
99 cp_org = cp.origin - this.view_ofs; // no control point found, assume a straight line to the destination
102 if (this.target == "")
103 objerror(this, "train_next: no next target");
104 this.wait = targ.wait;
108 if(targ.platmovetype)
110 // this path_corner contains a movetype overrider, apply it
111 this.platmovetype_start = targ.platmovetype_start;
112 this.platmovetype_end = targ.platmovetype_end;
116 // this path_corner doesn't contain a movetype overrider, use the train's defaults
117 this.platmovetype_start = this.platmovetype_start_default;
118 this.platmovetype_end = this.platmovetype_end_default;
124 SUB_CalcMove_Bezier(this, cp_org, targ.origin - this.view_ofs, TSPEED_LINEAR, targ.speed, train_wait);
126 SUB_CalcMove(this, targ.origin - this.view_ofs, TSPEED_LINEAR, targ.speed, train_wait);
131 SUB_CalcMove_Bezier(this, cp_org, targ.origin - this.view_ofs, TSPEED_LINEAR, this.speed, train_wait);
133 SUB_CalcMove(this, targ.origin - this.view_ofs, TSPEED_LINEAR, this.speed, train_wait);
137 _sound(this, CH_TRIGGER_SINGLE, this.noise, VOL_BASE, ATTEN_IDLE);
140 REGISTER_NET_LINKED(ENT_CLIENT_TRAIN)
143 float train_send(entity this, entity to, float sf)
145 WriteHeader(MSG_ENTITY, ENT_CLIENT_TRAIN);
146 WriteByte(MSG_ENTITY, sf);
148 if(sf & SF_TRIGGER_INIT)
150 WriteString(MSG_ENTITY, this.platmovetype);
151 WriteByte(MSG_ENTITY, this.platmovetype_turn);
152 WriteByte(MSG_ENTITY, this.spawnflags);
154 WriteString(MSG_ENTITY, this.model);
156 trigger_common_write(this, true);
158 WriteString(MSG_ENTITY, this.curvetarget);
160 WriteVector(MSG_ENTITY, this.pos1);
161 WriteVector(MSG_ENTITY, this.pos2);
163 WriteVector(MSG_ENTITY, this.size);
165 WriteVector(MSG_ENTITY, this.view_ofs);
167 WriteAngleVector(MSG_ENTITY, this.mangle);
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);
175 WriteShort(MSG_ENTITY, this.dmg);
176 WriteByte(MSG_ENTITY, this.dmgtime);
179 if(sf & SF_TRIGGER_RESET)
187 void train_link(entity this)
189 //Net_LinkEntity(this, 0, false, train_send);
192 void train_use(entity this, entity actor, entity trigger)
194 this.nextthink = this.ltime + 1;
195 setthink(this, train_next);
196 this.use = func_null; // not again, next target can set it again if needed
197 if(trigger.target2 && trigger.target2 != "")
198 this.future_target = find(NULL, targetname, trigger.target2);
201 void func_train_find(entity this)
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 setorigin(this, targ.origin - this.view_ofs);
212 if(!(this.spawnflags & TRAIN_NEEDACTIVATION))
214 this.nextthink = this.ltime + 1;
215 setthink(this, train_next);
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)
229 spawnfunc(func_train)
231 if (this.noise != "")
232 precache_sound(this.noise);
234 if (this.target == "")
235 objerror(this, "func_train without a target");
239 if (!InitMovingBrushTrigger(this))
241 this.effects |= EF_LOWPRECISION;
243 if(this.spawnflags & TRAIN_NEEDACTIVATION)
244 this.use = train_use;
246 if (this.spawnflags & TRAIN_TURN)
248 this.platmovetype_turn = true;
249 this.view_ofs = '0 0 0'; // don't offset a rotating train, origin works differently now
252 this.view_ofs = this.mins;
254 // wait for targets to spawn
255 InitializeEntity(this, func_train_find, INITPRIO_FINDTARGET);
257 setblocked(this, generic_plat_blocked);
258 if(this.dmg && (this.message == ""))
259 this.message = " was squished";
260 if(this.dmg && (this.message2 == ""))
261 this.message2 = "was squished by";
262 if(this.dmg && (!this.dmgtime))
264 this.dmgtime2 = time;
266 if(!set_platmovetype(this, this.platmovetype))
268 this.platmovetype_start_default = this.platmovetype_start;
269 this.platmovetype_end_default = this.platmovetype_end;
271 // TODO make a reset function for this one
274 void train_draw(entity this)
276 //Movetype_Physics_NoMatchServer();
277 Movetype_Physics_MatchServer(this, autocvar_cl_projectiles_sloppy);
280 NET_HANDLE(ENT_CLIENT_TRAIN, bool isnew)
282 float sf = ReadByte();
284 if(sf & SF_TRIGGER_INIT)
286 this.platmovetype = strzone(ReadString());
287 this.platmovetype_turn = ReadByte();
288 this.spawnflags = ReadByte();
290 this.model = strzone(ReadString());
291 _setmodel(this, this.model);
293 trigger_common_read(this, true);
295 this.curvetarget = strzone(ReadString());
297 this.pos1 = ReadVector();
298 this.pos2 = ReadVector();
300 this.size = ReadVector();
302 this.view_ofs = ReadVector();
304 this.mangle = ReadAngleVector();
306 this.speed = ReadShort();
307 this.height = ReadShort();
308 this.lip = ReadByte();
309 this.state = ReadByte();
310 this.wait = ReadByte();
312 this.dmg = ReadShort();
313 this.dmgtime = ReadByte();
315 this.classname = "func_train";
316 this.solid = SOLID_BSP;
317 set_movetype(this, MOVETYPE_PUSH);
318 this.drawmask = MASK_NORMAL;
319 this.draw = train_draw;
320 if (isnew) IL_PUSH(g_drawables, this);
321 this.entremove = trigger_remove_generic;
323 if(set_platmovetype(this, this.platmovetype))
325 this.platmovetype_start_default = this.platmovetype_start;
326 this.platmovetype_end_default = this.platmovetype_end;
329 // everything is set up by the time the train is linked, we shouldn't need this
332 // but we will need these
335 set_movetype(this, MOVETYPE_PUSH);
336 this.move_time = time;
339 if(sf & SF_TRIGGER_RESET)
341 // TODO: make a reset function for trains