Impulses: migration pathway
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / vehicles / sv_vehicles.qc
1 #include "sv_vehicles.qh"
2
3 #if 0
4 bool vehicle_send(entity to, int sf)
5 {
6         WriteByte(MSG_ENTITY, ENT_CLIENT_VEHICLE);
7         WriteByte(MSG_ENTITY, sf);
8
9         if(sf & VSF_SPAWN)
10         {
11                 WriteByte(MSG_ENTITY, self.vehicleid);
12         }
13
14         if(sf & VSF_SETUP)
15         {
16                 // send stuff?
17         }
18
19         if(sf & VSF_ENTER)
20         {
21                 // player handles the .vehicle stuff, we need only set ourselves up for driving
22
23                 // send stuff?
24         }
25
26         if(sf & VSF_EXIT)
27         {
28                 // senf stuff?
29         }
30
31         if(sf & VSF_PRECACHE)
32         {
33                 // send stuff?!
34         }
35
36         return true;
37 }
38 #endif
39
40 bool SendAuxiliaryXhair(entity this, entity to, int sf)
41 {
42
43         WriteHeader(MSG_ENTITY, ENT_CLIENT_AUXILIARYXHAIR);
44
45         WriteByte(MSG_ENTITY, self.cnt);
46
47         WriteCoord(MSG_ENTITY, self.origin_x);
48         WriteCoord(MSG_ENTITY, self.origin_y);
49         WriteCoord(MSG_ENTITY, self.origin_z);
50
51         WriteByte(MSG_ENTITY, rint(self.colormod_x * 255));
52         WriteByte(MSG_ENTITY, rint(self.colormod_y * 255));
53         WriteByte(MSG_ENTITY, rint(self.colormod_z * 255));
54
55         return true;
56 }
57
58 void UpdateAuxiliaryXhair(entity own, vector loc, vector clr, int axh_id)
59 {
60         if(!IS_REAL_CLIENT(own))
61                 return;
62
63         entity axh;
64
65         axh_id = bound(0, axh_id, MAX_AXH);
66         axh = own.(AuxiliaryXhair[axh_id]);
67
68         if(axh == world || wasfreed(axh))  // MADNESS? THIS IS QQQQCCCCCCCCC (wasfreed, why do you exsist?)
69         {
70                 axh                                      = spawn();
71                 axh.cnt                          = axh_id;
72                 axh.drawonlytoclient    = own;
73                 axh.owner                          = own;
74                 Net_LinkEntity(axh, false, 0, SendAuxiliaryXhair);
75         }
76
77         setorigin(axh, loc);
78         axh.colormod                    = clr;
79         axh.SendFlags              = 0x01;
80         own.(AuxiliaryXhair[axh_id]) = axh;
81 }
82
83 void CSQCVehicleSetup(entity own, int vehicle_id)
84 {
85         if(!IS_REAL_CLIENT(own))
86                 return;
87
88         msg_entity = own;
89
90         WriteHeader(MSG_ONE, TE_CSQC_VEHICLESETUP);
91         WriteByte(MSG_ONE, vehicle_id);
92 }
93
94 vector targetdrone_getnewspot()
95 {SELFPARAM();
96         vector spot;
97         int i;
98         for(i = 0; i < 100; ++i)
99         {
100                 spot = self.origin + randomvec() * 1024;
101                 tracebox(spot, self.mins, self.maxs, spot, MOVE_NORMAL, self);
102                 if(trace_fraction == 1.0 && trace_startsolid == 0 && trace_allsolid == 0)
103                         return spot;
104         }
105         return self.origin;
106 }
107
108 void vehicles_locktarget(float incr, float decr, float _lock_time)
109 {SELFPARAM();
110         if(self.lock_target && self.lock_target.deadflag != DEAD_NO)
111         {
112                 self.lock_target        = world;
113                 self.lock_strength  = 0;
114                 self.lock_time    = 0;
115         }
116
117         if(self.lock_time > time)
118         {
119                 if(self.lock_target)
120                 if(self.lock_soundtime < time)
121                 {
122                         self.lock_soundtime = time + 0.5;
123                         play2(self.owner, "vehicles/locked.wav");
124                 }
125
126                 return;
127         }
128
129         if(trace_ent != world)
130         {
131                 if(SAME_TEAM(trace_ent, self))
132                         trace_ent = world;
133
134                 if(trace_ent.deadflag != DEAD_NO)
135                         trace_ent = world;
136
137                 if(!(IS_VEHICLE(trace_ent) || IS_TURRET(trace_ent)))
138                         trace_ent = world;
139
140                 if(trace_ent.alpha <= 0.5 && trace_ent.alpha != 0)
141                         trace_ent = world; // invisible
142         }
143
144         if(self.lock_target == world && trace_ent != world)
145                 self.lock_target = trace_ent;
146
147         if(self.lock_target && trace_ent == self.lock_target)
148         {
149                 if(self.lock_strength != 1 && self.lock_strength + incr >= 1)
150                 {
151                         play2(self.owner, "vehicles/lock.wav");
152                         self.lock_soundtime = time + 0.8;
153                 }
154                 else if (self.lock_strength != 1 && self.lock_soundtime < time)
155                 {
156                         play2(self.owner, "vehicles/locking.wav");
157                         self.lock_soundtime = time + 0.3;
158                 }
159         }
160
161         // Have a locking target
162         // Trace hit current target
163         if(trace_ent == self.lock_target && trace_ent != world)
164         {
165                 self.lock_strength = min(self.lock_strength + incr, 1);
166                 if(self.lock_strength == 1)
167                         self.lock_time = time + _lock_time;
168         }
169         else
170         {
171                 if(trace_ent)
172                         self.lock_strength = max(self.lock_strength - decr * 2, 0);
173                 else
174                         self.lock_strength = max(self.lock_strength - decr, 0);
175
176                 if(self.lock_strength == 0)
177                         self.lock_target = world;
178         }
179 }
180
181 vector vehicles_force_fromtag_hover(string tag_name, float spring_length, float max_power)
182 {SELFPARAM();
183         force_fromtag_origin = gettaginfo(self, gettagindex(self, tag_name));
184         v_forward  = normalize(v_forward) * -1;
185         traceline(force_fromtag_origin, force_fromtag_origin - (v_forward  * spring_length), MOVE_NORMAL, self);
186
187         force_fromtag_power = (1 - trace_fraction) * max_power;
188         force_fromtag_normpower = force_fromtag_power / max_power;
189
190         return v_forward  * force_fromtag_power;
191 }
192
193 vector vehicles_force_fromtag_maglev(string tag_name, float spring_length, float max_power)
194 {SELFPARAM();
195
196         force_fromtag_origin = gettaginfo(self, gettagindex(self, tag_name));
197         v_forward  = normalize(v_forward) * -1;
198         traceline(force_fromtag_origin, force_fromtag_origin - (v_forward  * spring_length), MOVE_NORMAL, self);
199
200         // TODO - this may NOT be compatible with wall/celing movement, unhardcode 0.25 (engine count multiplier)
201         if(trace_fraction == 1.0)
202         {
203                 force_fromtag_normpower = -0.25;
204                 return '0 0 -200';
205         }
206
207         force_fromtag_power = ((1 - trace_fraction) - trace_fraction) * max_power;
208         force_fromtag_normpower = force_fromtag_power / max_power;
209
210         return v_forward  * force_fromtag_power;
211 }
212
213 // projectile handling
214 void vehicles_projectile_damage(entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
215 {SELFPARAM();
216         // Ignore damage from oterh projectiles from my owner (dont mess up volly's)
217         if(inflictor.owner == self.owner)
218                 return;
219
220         self.health -= damage;
221         self.velocity += force;
222         if(self.health < 1)
223         {
224                 self.takedamage = DAMAGE_NO;
225                 self.event_damage = func_null;
226                 self.think = self.use;
227                 self.nextthink = time;
228         }
229 }
230
231 void vehicles_projectile_explode()
232 {SELFPARAM();
233         if(self.owner && other != world)
234         {
235                 if(other == self.owner.vehicle)
236                         return;
237
238                 if(other == self.owner.vehicle.tur_head)
239                         return;
240         }
241
242         PROJECTILE_TOUCH;
243
244         self.event_damage = func_null;
245         RadiusDamage (self, self.realowner, self.shot_dmg, 0, self.shot_radius, self, world, self.shot_force, self.totalfrags, other);
246
247         remove (self);
248 }
249
250 entity vehicles_projectile(string _mzlfx, string _mzlsound,
251                                                    vector _org, vector _vel,
252                                                    float _dmg, float _radi, float _force,  float _size,
253                                                    int _deahtype, float _projtype, float _health,
254                                                    bool _cull, bool _clianim, entity _owner)
255 {SELFPARAM();
256         entity proj;
257
258         proj = spawn();
259
260         PROJECTILE_MAKETRIGGER(proj);
261         setorigin(proj, _org);
262
263         proj.shot_dmg            = _dmg;
264         proj.shot_radius          = _radi;
265         proj.shot_force    = _force;
266         proj.totalfrags    = _deahtype;
267         proj.solid                      = SOLID_BBOX;
268         proj.movetype            = MOVETYPE_FLYMISSILE;
269         proj.flags                      = FL_PROJECTILE;
270         proj.bot_dodge          = true;
271         proj.bot_dodgerating  = _dmg;
272         proj.velocity            = _vel;
273         proj.touch                      = vehicles_projectile_explode;
274         proj.use                          = vehicles_projectile_explode;
275         proj.owner                      = self;
276         proj.realowner          = _owner;
277         proj.think                      = SUB_Remove_self;
278         proj.nextthink          = time + 30;
279
280         if(_health)
281         {
282                 proj.takedamage    = DAMAGE_AIM;
283                 proj.event_damage        = vehicles_projectile_damage;
284                 proj.health                = _health;
285         }
286         else
287                 proj.flags                 = FL_PROJECTILE | FL_NOTARGET;
288
289         if(_mzlsound)
290                 _sound (self, CH_WEAPON_A, _mzlsound, VOL_BASE, ATTEN_NORM);
291
292         if(_mzlfx)
293                 Send_Effect_(_mzlfx, proj.origin, proj.velocity, 1);
294
295         setsize (proj, '-1 -1 -1' * _size, '1 1 1' * _size);
296
297         CSQCProjectile(proj, _clianim, _projtype, _cull);
298
299         return proj;
300 }
301
302 void vehicles_gib_explode()
303 {SELFPARAM();
304         sound (self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
305         Send_Effect(EFFECT_EXPLOSION_SMALL, randomvec() * 80 + (self.origin + '0 0 100'), '0 0 0', 1);
306         Send_Effect(EFFECT_EXPLOSION_SMALL, self.wp00.origin + '0 0 64', '0 0 0', 1);
307         remove(self);
308 }
309
310 void vehicles_gib_think()
311 {SELFPARAM();
312         self.alpha -= 0.1;
313         if(self.cnt >= time)
314                 remove(self);
315         else
316                 self.nextthink = time + 0.1;
317 }
318
319 entity vehicle_tossgib(entity _template, vector _vel, string _tag, bool _burn, bool _explode, float _maxtime, vector _rot)
320 {SELFPARAM();
321         entity _gib = spawn();
322         _setmodel(_gib, _template.model);
323         setorigin(_gib, gettaginfo(self, gettagindex(self, _tag)));
324         _gib.velocity = _vel;
325         _gib.movetype = MOVETYPE_TOSS;
326         _gib.solid = SOLID_CORPSE;
327         _gib.colormod = '-0.5 -0.5 -0.5';
328         _gib.effects = EF_LOWPRECISION;
329         _gib.avelocity = _rot;
330
331         if(_burn)
332                 _gib.effects |= EF_FLAME;
333
334         if(_explode)
335         {
336                 _gib.think = vehicles_gib_explode;
337                 _gib.nextthink = time + random() * _explode;
338                 _gib.touch = vehicles_gib_explode;
339         }
340         else
341         {
342                 _gib.cnt = time + _maxtime;
343                 _gib.think = vehicles_gib_think;
344                 _gib.nextthink = time + _maxtime - 1;
345                 _gib.alpha = 1;
346         }
347         return _gib;
348 }
349
350 bool vehicle_addplayerslot(     entity _owner,
351                                                                 entity _slot,
352                                                                 int _hud,
353                                                                 string _hud_model,
354                                                                 bool() _framefunc,
355                                                                 void(bool) _exitfunc, float() _enterfunc)
356 {
357         if(!(_owner.vehicle_flags & VHF_MULTISLOT))
358                 _owner.vehicle_flags |= VHF_MULTISLOT;
359
360         _slot.PlayerPhysplug = _framefunc;
361         _slot.vehicle_exit = _exitfunc;
362         _slot.vehicle_enter = _enterfunc;
363         _slot.hud = _hud;
364         _slot.vehicle_flags = VHF_PLAYERSLOT;
365         _slot.vehicle_viewport = spawn();
366         _slot.vehicle_hudmodel = spawn();
367         _slot.vehicle_hudmodel.viewmodelforclient = _slot;
368         _slot.vehicle_viewport.effects = (EF_ADDITIVE | EF_DOUBLESIDED | EF_FULLBRIGHT | EF_NODEPTHTEST | EF_NOGUNBOB | EF_NOSHADOW | EF_LOWPRECISION | EF_SELECTABLE | EF_TELEPORT_BIT);
369
370         _setmodel(_slot.vehicle_hudmodel, _hud_model);
371         setmodel(_slot.vehicle_viewport, MDL_Null);
372
373         setattachment(_slot.vehicle_hudmodel, _slot, "");
374         setattachment(_slot.vehicle_viewport, _slot.vehicle_hudmodel, "");
375
376         return true;
377 }
378
379 vector vehicle_aimturret(entity _vehic, vector _target, entity _turrret, string _tagname,
380                                                  float _pichlimit_min, float _pichlimit_max,
381                                                  float _rotlimit_min, float _rotlimit_max, float _aimspeed)
382 {
383         vector vtmp, vtag;
384         float ftmp;
385         vtag = gettaginfo(_turrret, gettagindex(_turrret, _tagname));
386         vtmp = vectoangles(normalize(_target - vtag));
387         vtmp = AnglesTransform_ToAngles(AnglesTransform_LeftDivide(AnglesTransform_FromAngles(_vehic.angles), AnglesTransform_FromAngles(vtmp))) - _turrret.angles;
388         vtmp = AnglesTransform_Normalize(vtmp, true);
389         ftmp = _aimspeed * frametime;
390         vtmp_y = bound(-ftmp, vtmp_y, ftmp);
391         vtmp_x = bound(-ftmp, vtmp_x, ftmp);
392         _turrret.angles_y = bound(_rotlimit_min, _turrret.angles_y + vtmp_y, _rotlimit_max);
393         _turrret.angles_x = bound(_pichlimit_min, _turrret.angles_x + vtmp_x, _pichlimit_max);
394         return vtag;
395 }
396
397 void vehicles_reset_colors()
398 {SELFPARAM();
399         entity e;
400         float _effects = 0, _colormap;
401         vector _glowmod, _colormod;
402
403         if(autocvar_g_nodepthtestplayers)
404                 _effects |= EF_NODEPTHTEST;
405
406         if(autocvar_g_fullbrightplayers)
407                 _effects |= EF_FULLBRIGHT;
408
409         if(self.team)
410                 _colormap = 1024 + (self.team - 1) * 17;
411         else
412                 _colormap = 1024;
413
414         _glowmod  = '0 0 0';
415         _colormod = '0 0 0';
416
417         // Find all ents attacked to main model and setup effects, colormod etc.
418         e = findchainentity(tag_entity, self);
419         while(e)
420         {
421                 if(e != self.vehicle_shieldent)
422                 {
423                         e.effects   = _effects; //  | EF_LOWPRECISION;
424                         e.colormod  = _colormod;
425                         e.colormap  = _colormap;
426                         e.alpha  = 1;
427                 }
428                 e = e.chain;
429         }
430         // Also check head tags
431         e = findchainentity(tag_entity, self.tur_head);
432         while(e)
433         {
434                 if(e != self.vehicle_shieldent)
435                 {
436                         e.effects   = _effects; //  | EF_LOWPRECISION;
437                         e.colormod  = _colormod;
438                         e.colormap  = _colormap;
439                         e.alpha  = 1;
440                 }
441                 e = e.chain;
442         }
443
444         self.vehicle_hudmodel.effects  = self.effects  = _effects; // | EF_LOWPRECISION;
445         self.vehicle_hudmodel.colormod = self.colormod = _colormod;
446         self.vehicle_hudmodel.colormap = self.colormap = _colormap;
447         self.vehicle_viewport.effects = (EF_ADDITIVE | EF_DOUBLESIDED | EF_FULLBRIGHT | EF_NODEPTHTEST | EF_NOGUNBOB | EF_NOSHADOW | EF_LOWPRECISION | EF_SELECTABLE | EF_TELEPORT_BIT);
448
449         self.alpha       = 1;
450         self.avelocity = '0 0 0';
451         self.velocity  = '0 0 0';
452         self.effects   = _effects;
453 }
454
455 void vehicles_clearreturn(entity veh)
456 {
457         // Remove "return helper", if any.
458         for (entity ret = findchain(classname, "vehicle_return"); ret; ret = ret.chain)
459         {
460                 if(ret.wp00 == veh)
461                 {
462                         ret.classname   = "";
463                         ret.think          = SUB_Remove_self;
464                         ret.nextthink   = time + 0.1;
465
466                         if(ret.waypointsprite_attached)
467                                 WaypointSprite_Kill(ret.waypointsprite_attached);
468
469                         return;
470                 }
471         }
472 }
473
474 void vehicles_spawn();
475 void vehicles_return()
476 {SELFPARAM();
477         Send_Effect(EFFECT_TELEPORT, self.wp00.origin + '0 0 64', '0 0 0', 1);
478
479         self.wp00.think  = vehicles_spawn;
480         self.wp00.nextthink = time;
481
482         if(self.waypointsprite_attached)
483                 WaypointSprite_Kill(self.waypointsprite_attached);
484
485         remove(self);
486 }
487
488 void vehicles_showwp_goaway()
489 {SELFPARAM();
490         if(self.waypointsprite_attached)
491                 WaypointSprite_Kill(self.waypointsprite_attached);
492
493         remove(self);
494
495 }
496
497 void vehicles_showwp()
498 {SELFPARAM();
499         entity oldself = world;
500         vector rgb;
501
502         if(self.cnt)
503         {
504                 self.think        = vehicles_return;
505                 self.nextthink  = self.cnt;
506         }
507         else
508         {
509                 self.think        = vehicles_return;
510                 self.nextthink  = time +1;
511
512                 oldself = self;
513                 setself(spawn());
514                 setmodel(self, MDL_Null);
515                 self.team = oldself.wp00.team;
516                 self.wp00 = oldself.wp00;
517                 setorigin(self, oldself.wp00.pos1);
518
519                 self.nextthink = time + 5;
520                 self.think = vehicles_showwp_goaway;
521         }
522
523         if(teamplay && self.team)
524                 rgb = Team_ColorRGB(self.team);
525         else
526                 rgb = '1 1 1';
527         entity wp = WaypointSprite_Spawn(WP_Vehicle, 0, 0, self, '0 0 64', world, 0, self, waypointsprite_attached, true, RADARICON_Vehicle);
528         wp.colormod = rgb;
529         if(self.waypointsprite_attached)
530         {
531                 WaypointSprite_UpdateRule(self.waypointsprite_attached, self.wp00.team, SPRITERULE_DEFAULT);
532                 if(this == NULL)
533                         WaypointSprite_UpdateBuildFinished(self.waypointsprite_attached, self.nextthink);
534                 WaypointSprite_Ping(self.waypointsprite_attached);
535         }
536
537         if(this)
538                 setself(this);
539 }
540
541 void vehicles_setreturn(entity veh)
542 {
543         entity ret;
544
545         vehicles_clearreturn(veh);
546
547         ret = new(vehicle_return);
548         ret.wp00           = veh;
549         ret.team                = veh.team;
550         ret.think          = vehicles_showwp;
551
552         if(veh.deadflag != DEAD_NO)
553         {
554                 ret.cnt          = time + veh.respawntime;
555                 ret.nextthink   = min(time + veh.respawntime, time + veh.respawntime - 5);
556         }
557         else
558         {
559                 ret.nextthink   = min(time + veh.respawntime, time + veh.respawntime - 1);
560         }
561
562         setmodel(ret, MDL_Null);
563         setorigin(ret, veh.pos1 + '0 0 96');
564
565 }
566
567 void vehicle_use()
568 {SELFPARAM();
569         LOG_TRACE("vehicle ",self.netname, " used by ", activator.classname, "\n");
570
571         self.tur_head.team = activator.team;
572
573         if(self.tur_head.team == 0)
574                 self.active = ACTIVE_NOT;
575         else
576                 self.active = ACTIVE_ACTIVE;
577
578         if(self.active == ACTIVE_ACTIVE && self.deadflag == DEAD_NO && !gameover)
579         {
580                 LOG_TRACE("Respawning vehicle: ", self.netname, "\n");
581                 if(self.effects & EF_NODRAW)
582                 {
583                         self.think = vehicles_spawn;
584                         self.nextthink = time + 3;
585                 }
586                 else
587                 {
588                         vehicles_setreturn(self);
589                         vehicles_reset_colors();
590                 }
591         }
592 }
593
594 void vehicles_regen(float timer, .float regen_field, float field_max, float rpause, float regen, float delta_time, float _healthscale)
595 {SELFPARAM();
596         if(self.(regen_field) < field_max)
597         if(timer + rpause < time)
598         {
599                 if(_healthscale)
600                         regen = regen * (self.vehicle_health / self.max_health);
601
602                 self.(regen_field) = min(self.(regen_field) + regen * delta_time, field_max);
603
604                 if(self.owner)
605                         self.owner.(regen_field) = (self.(regen_field) / field_max) * 100;
606         }
607 }
608
609 void shieldhit_think()
610 {SELFPARAM();
611         self.alpha -= 0.1;
612         if (self.alpha <= 0)
613         {
614                 // setmodel(self, MDL_Null);
615                 self.alpha = -1;
616                 self.effects |= EF_NODRAW;
617         }
618         else
619         {
620                 self.nextthink = time + 0.1;
621         }
622 }
623
624 void vehicles_painframe()
625 {SELFPARAM();
626         if(self.owner.vehicle_health <= 50)
627         if(self.pain_frame < time)
628         {
629                 float _ftmp;
630                 _ftmp = self.owner.vehicle_health / 50;
631                 self.pain_frame = time + 0.1 + (random() * 0.5 * _ftmp);
632                 pointparticles(EFFECT_SMOKE_SMALL, (self.origin + (randomvec() * 80)), '0 0 0', 1);
633
634                 if(self.vehicle_flags & VHF_DMGSHAKE)
635                         self.velocity += randomvec() * 30;
636
637                 if(self.vehicle_flags & VHF_DMGROLL)
638                         if(self.vehicle_flags & VHF_DMGHEADROLL)
639                                 self.tur_head.angles += randomvec();
640                         else
641                                 self.angles += randomvec();
642
643         }
644 }
645
646 void vehicles_damage(entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
647 {SELFPARAM();
648         self.dmg_time = time;
649
650         // WEAPONTODO
651         if(DEATH_ISWEAPON(deathtype, WEP_VORTEX))
652                 damage *= autocvar_g_vehicles_vortex_damagerate;
653
654         if(DEATH_ISWEAPON(deathtype, WEP_MACHINEGUN))
655                 damage *= autocvar_g_vehicles_machinegun_damagerate;
656
657         if(DEATH_ISWEAPON(deathtype, WEP_RIFLE))
658                 damage *= autocvar_g_vehicles_rifle_damagerate;
659
660         if(DEATH_ISWEAPON(deathtype, WEP_VAPORIZER))
661                 damage *= autocvar_g_vehicles_vaporizer_damagerate;
662
663         if(DEATH_ISWEAPON(deathtype, WEP_SEEKER))
664                 damage *= autocvar_g_vehicles_tag_damagerate;
665
666         if(DEATH_WEAPONOF(deathtype) != WEP_Null)
667                 damage *= autocvar_g_vehicles_weapon_damagerate;
668
669         self.enemy = attacker;
670
671         self.pain_finished = time;
672
673         if((self.vehicle_flags & VHF_HASSHIELD) && (self.vehicle_shield > 0))
674         {
675                 if (wasfreed(self.vehicle_shieldent) || self.vehicle_shieldent == world)
676                 {
677                         self.vehicle_shieldent = spawn();
678                         self.vehicle_shieldent.effects = EF_LOWPRECISION;
679
680                         setmodel(self.vehicle_shieldent, MDL_VEH_SHIELD);
681                         setattachment(self.vehicle_shieldent, self, "");
682                         setorigin(self.vehicle_shieldent, real_origin(self) - self.origin);
683                         self.vehicle_shieldent.scale       = 256 / vlen(self.maxs - self.mins);
684                         self.vehicle_shieldent.think       = shieldhit_think;
685                 }
686
687                 self.vehicle_shieldent.colormod = '1 1 1';
688                 self.vehicle_shieldent.alpha = 0.45;
689                 self.vehicle_shieldent.angles = vectoangles(normalize(hitloc - (self.origin + self.vehicle_shieldent.origin))) - self.angles;
690                 self.vehicle_shieldent.nextthink = time;
691                 self.vehicle_shieldent.effects &= ~EF_NODRAW;
692
693                 self.vehicle_shield -= damage;
694
695                 if(self.vehicle_shield < 0)
696                 {
697                         self.vehicle_health -= fabs(self.vehicle_shield);
698                         self.vehicle_shieldent.colormod = '2 0 0';
699                         self.vehicle_shield = 0;
700                         self.vehicle_shieldent.alpha = 0.75;
701
702                         if(sound_allowed(MSG_BROADCAST, attacker))
703                                 spamsound (self, CH_PAIN, "onslaught/ons_hit2.wav", VOL_BASE, ATTEN_NORM);   // FIXME: PLACEHOLDER
704                 }
705                 else
706                         if(sound_allowed(MSG_BROADCAST, attacker))
707                                 spamsound (self, CH_PAIN, "onslaught/electricity_explode.wav", VOL_BASE, ATTEN_NORM);  // FIXME: PLACEHOLDER
708
709         }
710         else
711         {
712                 self.vehicle_health -= damage;
713
714                 if(sound_allowed(MSG_BROADCAST, attacker))
715                         spamsound (self, CH_PAIN, "onslaught/ons_hit2.wav", VOL_BASE, ATTEN_NORM);  // FIXME: PLACEHOLDER
716         }
717
718         if(self.damageforcescale < 1 && self.damageforcescale > 0)
719                 self.velocity += force * self.damageforcescale;
720         else
721                 self.velocity += force;
722
723         if(self.vehicle_health <= 0)
724         {
725                 if(self.owner)
726                         if(self.vehicle_flags & VHF_DEATHEJECT)
727                                 vehicles_exit(VHEF_EJECT);
728                         else
729                                 vehicles_exit(VHEF_RELEASE);
730
731
732                 antilag_clear(self);
733
734                 Vehicle info = get_vehicleinfo(self.vehicleid);
735                 info.vr_death(info);
736                 vehicles_setreturn(self);
737         }
738 }
739
740 float vehicles_crushable(entity e)
741 {
742         if(IS_PLAYER(e) && time >= e.vehicle_enter_delay)
743                 return true;
744
745         if(IS_MONSTER(e))
746                 return true;
747
748         return false;
749 }
750
751 void vehicles_impact(float _minspeed, float _speedfac, float _maxpain)
752 {SELFPARAM();
753         if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
754                 return;
755
756         if(self.play_time < time)
757         {
758                 float wc = vlen(self.velocity - self.oldvelocity);
759                 //dprint("oldvel: ", vtos(self.oldvelocity), "\n");
760                 //dprint("vel: ", vtos(self.velocity), "\n");
761                 if(_minspeed < wc)
762                 {
763                         float take = min(_speedfac * wc, _maxpain);
764                         Damage (self, world, world, take, DEATH_FALL.m_id, self.origin, '0 0 0');
765                         self.play_time = time + 0.25;
766
767                         //dprint("wc: ", ftos(wc), "\n");
768                         //dprint("take: ", ftos(take), "\n");
769                 }
770         }
771 }
772
773 // vehicle enter/exit handling
774 vector vehicles_findgoodexit(vector prefer_spot)
775 {SELFPARAM();
776         //vector exitspot;
777         float mysize;
778
779         tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, prefer_spot, MOVE_NORMAL, self.owner);
780         if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
781                 return prefer_spot;
782
783         mysize = 1.5 * vlen(self.maxs - self.mins);
784         float i;
785         vector v, v2;
786         v2 = 0.5 * (self.absmin + self.absmax);
787         for(i = 0; i < 100; ++i)
788         {
789                 v = randomvec();
790                 v_z = 0;
791                 v = v2 + normalize(v) * mysize;
792                 tracebox(v2, PL_MIN, PL_MAX, v, MOVE_NORMAL, self.owner);
793                 if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
794                         return v;
795         }
796
797         /*
798         exitspot = (self.origin + '0 0 48') + v_forward * mysize;
799         tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, exitspot, MOVE_NORMAL, self.owner);
800         if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
801                 return exitspot;
802
803         exitspot = (self.origin + '0 0 48') - v_forward * mysize;
804         tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, exitspot, MOVE_NORMAL, self.owner);
805         if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
806                 return exitspot;
807
808         exitspot = (self.origin + '0 0 48') + v_right * mysize;
809         tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, exitspot, MOVE_NORMAL, self.owner);
810         if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
811                 return exitspot;
812
813         exitspot = (self.origin + '0 0 48') - v_right * mysize;
814         tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, exitspot, MOVE_NORMAL, self.owner);
815         if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
816                 return exitspot;
817         */
818
819         return self.origin;
820 }
821
822 void vehicles_exit(bool eject)
823 {SELFPARAM();
824         entity _vehicle;
825         entity _player;
826
827         if(vehicles_exit_running)
828         {
829                 LOG_TRACE("^1vehicles_exit allready running! this is not good..\n");
830                 return;
831         }
832
833         vehicles_exit_running = true;
834         if(IS_CLIENT(self))
835         {
836                 _vehicle = self.vehicle;
837
838                 if (_vehicle.vehicle_flags & VHF_PLAYERSLOT)
839                 {
840                         _vehicle.vehicle_exit(eject);
841                         setself(this);
842                         vehicles_exit_running = false;
843                         return;
844                 }
845         }
846         else
847                 _vehicle = self;
848
849         _player = _vehicle.owner;
850
851         setself(_vehicle);
852
853         if (_player)
854         {
855                 if (IS_REAL_CLIENT(_player))
856                 {
857                         msg_entity = _player;
858                         WriteByte (MSG_ONE, SVC_SETVIEWPORT);
859                         WriteEntity( MSG_ONE, _player);
860
861                         WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
862                         WriteAngle(MSG_ONE, 0);
863                         WriteAngle(MSG_ONE, _vehicle.angles_y);
864                         WriteAngle(MSG_ONE, 0);
865                 }
866
867                 setsize(_player, PL_MIN,PL_MAX);
868
869                 _player.takedamage              = DAMAGE_AIM;
870                 _player.solid                   = SOLID_SLIDEBOX;
871                 _player.movetype                = MOVETYPE_WALK;
872                 _player.effects            &= ~EF_NODRAW;
873                 _player.teleportable    = TELEPORT_NORMAL;
874                 _player.alpha                   = 1;
875                 _player.PlayerPhysplug  = func_null;
876                 _player.vehicle                 = world;
877                 _player.view_ofs                = PL_VIEW_OFS;
878                 _player.event_damage    = PlayerDamage;
879                 _player.hud                             = HUD_NORMAL;
880                 _player.switchweapon    = _vehicle.switchweapon;
881                 _player.last_vehiclecheck = time + 3;
882                 _player.vehicle_enter_delay = time + 2;
883
884                 CSQCVehicleSetup(_player, HUD_NORMAL);
885         }
886         _vehicle.flags |= FL_NOTARGET;
887
888         if(_vehicle.deadflag == DEAD_NO)
889                 _vehicle.avelocity = '0 0 0';
890
891         _vehicle.tur_head.nodrawtoclient = world;
892
893         if(!teamplay)
894                 _vehicle.team = 0;
895
896         Kill_Notification(NOTIF_ONE, _player, MSG_CENTER_CPID, CPID_VEHICLES);
897         Kill_Notification(NOTIF_ONE, _player, MSG_CENTER_CPID, CPID_VEHICLES_OTHER); // kill all vehicle notifications when exiting a vehicle?
898
899         WaypointSprite_Kill(_vehicle.wps_intruder);
900
901         MUTATOR_CALLHOOK(VehicleExit, _player, _vehicle);
902
903         _vehicle.team = _vehicle.tur_head.team;
904
905         sound (_vehicle, CH_TRIGGER_SINGLE, SND_Null, 1, ATTEN_NORM);
906         _vehicle.vehicle_hudmodel.viewmodelforclient = _vehicle;
907         _vehicle.phase = time + 1;
908
909         _vehicle.vehicle_exit(eject);
910
911         vehicles_setreturn(_vehicle);
912         vehicles_reset_colors();
913         _vehicle.owner = world;
914
915         CSQCMODEL_AUTOINIT(self);
916
917         setself(this);
918         vehicles_exit_running = false;
919 }
920
921 void vehicles_touch()
922 {SELFPARAM();
923         if(MUTATOR_CALLHOOK(VehicleTouch, self, other))
924                 return;
925
926         // Vehicle currently in use
927         if(self.owner)
928         {
929                 if(!forbidWeaponUse(self.owner))
930                 if(other != world)
931                 if((self.origin_z + self.maxs_z) > (other.origin_z))
932                 if(vehicles_crushable(other))
933                 {
934                         if(vlen(self.velocity) >= 30)
935                                 Damage(other, self, self.owner, autocvar_g_vehicles_crush_dmg, DEATH_VH_CRUSH.m_id, '0 0 0', normalize(other.origin - self.origin) * autocvar_g_vehicles_crush_force);
936
937                         return; // Dont do selfdamage when hitting "soft targets".
938                 }
939
940                 if(self.play_time < time) {
941                         Vehicle info = get_vehicleinfo(self.vehicleid);
942                         info.vr_impact(info);
943                 }
944
945                 return;
946         }
947
948         if(autocvar_g_vehicles_enter)
949                 return;
950
951         vehicles_enter(other, self);
952 }
953
954 bool vehicle_impulse(entity this, int imp)
955 {
956         entity v = this.vehicle;
957         if (!v) return false;
958         if (v.deadflag != DEAD_NO) return false;
959         bool(int) f = v.vehicles_impulse;
960         if (f && f(imp)) return true;
961         switch (imp)
962         {
963                 case 17:
964                 {
965                         stuffcmd(self, "\ntoggle cl_eventchase_vehicle\nset _vehicles_shownchasemessage 1\n");
966                         return true;
967                 }
968         }
969         return false;
970 }
971
972 void vehicles_enter(entity pl, entity veh)
973 {SELFPARAM();
974    // Remove this when bots know how to use vehicles
975         if((IS_BOT_CLIENT(pl) && !autocvar_g_vehicles_allow_bots))
976                 return;
977
978         if((!IS_PLAYER(pl))
979         || (veh.phase >= time)
980         || (pl.vehicle_enter_delay >= time)
981         || (pl.frozen)
982         || (pl.deadflag != DEAD_NO)
983         || (pl.vehicle)
984         ) { return; }
985
986         if(autocvar_g_vehicles_enter) // vehicle's touch function should handle this if entering via use key is disabled (TODO)
987         if(veh.vehicle_flags & VHF_MULTISLOT)
988         if(veh.owner)
989         {
990                 setself(veh);
991                 other = pl; // TODO: fix
992
993                 if(!veh.gunner1)
994                 if(time >= veh.gun1.phase)
995                 if(veh.gun1.vehicle_enter)
996                 if(veh.gun1.vehicle_enter())
997                 {
998                         setself(this);
999                         return;
1000                 }
1001
1002                 if(!veh.gunner2)
1003                 if(time >= veh.gun2.phase)
1004                 if(veh.gun2.vehicle_enter)
1005                 if(veh.gun2.vehicle_enter())
1006                 {
1007                         setself(this);
1008                         return;
1009                 }
1010
1011                 setself(this);
1012         }
1013
1014         if(teamplay)
1015         if(veh.team)
1016         if(DIFF_TEAM(pl, veh))
1017         if(autocvar_g_vehicles_steal)
1018         {
1019                 entity head;
1020                 FOR_EACH_PLAYER(head) if(SAME_TEAM(head, veh))
1021                         Send_Notification(NOTIF_ONE, head, MSG_CENTER, CENTER_VEHICLE_STEAL);
1022
1023                 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_VEHICLE_STEAL_SELF);
1024
1025                 if (autocvar_g_vehicles_steal_show_waypoint) {
1026                         entity wp = WaypointSprite_Spawn(WP_VehicleIntruder, 0, 0, pl, '0 0 68', world, veh.team, veh, wps_intruder, true, RADARICON_DANGER);
1027                         wp.colormod = Team_ColorRGB(pl.team);
1028                 }
1029         }
1030         else return;
1031
1032         RemoveGrapplingHook(pl);
1033
1034         veh.vehicle_ammo1 = 0;
1035         veh.vehicle_ammo2 = 0;
1036         veh.vehicle_reload1 = 0;
1037         veh.vehicle_reload2 = 0;
1038         veh.vehicle_energy = 0;
1039
1040         veh.owner = pl;
1041         pl.vehicle = veh;
1042
1043         // .viewmodelforclient works better.
1044         //veh.vehicle_hudmodel.drawonlytoclient = veh.owner;
1045
1046         veh.vehicle_hudmodel.viewmodelforclient = pl;
1047
1048         tracebox(pl.origin, PL_MIN, PL_MAX, pl.origin, false, pl);
1049         pl.crouch = false;
1050         pl.view_ofs = PL_VIEW_OFS;
1051         setsize (pl, PL_MIN, PL_MAX);
1052
1053         veh.event_damage        = vehicles_damage;
1054         veh.nextthink           = 0;
1055         pl.angles                       = veh.angles;
1056         pl.takedamage           = DAMAGE_NO;
1057         pl.solid                        = SOLID_NOT;
1058         pl.movetype                     = MOVETYPE_NOCLIP;
1059         pl.teleportable         = false;
1060         pl.alpha                        = -1;
1061         pl.event_damage         = func_null;
1062         pl.view_ofs                     = '0 0 0';
1063         veh.colormap            = pl.colormap;
1064         if(veh.tur_head)
1065                 veh.tur_head.colormap = pl.colormap;
1066         veh.switchweapon = pl.switchweapon;
1067         pl.hud = veh.vehicleid;
1068         pl.PlayerPhysplug = veh.PlayerPhysplug;
1069
1070         pl.vehicle_ammo1 = veh.vehicle_ammo1;
1071         pl.vehicle_ammo2 = veh.vehicle_ammo2;
1072         pl.vehicle_reload1 = veh.vehicle_reload1;
1073         pl.vehicle_reload2 = veh.vehicle_reload2;
1074         pl.vehicle_energy = veh.vehicle_energy;
1075
1076         // Cant do this, hides attached objects too.
1077         //veh.exteriormodeltoclient = veh.owner;
1078         //veh.tur_head.exteriormodeltoclient = veh.owner;
1079
1080         pl.flags &= ~FL_ONGROUND;
1081         veh.flags &= ~FL_ONGROUND;
1082
1083         veh.team = pl.team;
1084         veh.flags -= FL_NOTARGET;
1085
1086         if (IS_REAL_CLIENT(pl))
1087         {
1088                 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_VEHICLE_ENTER);
1089
1090                 msg_entity = pl;
1091                 WriteByte (MSG_ONE, SVC_SETVIEWPORT);
1092                 WriteEntity(MSG_ONE, veh.vehicle_viewport);
1093
1094                 WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
1095                 if(veh.tur_head)
1096                 {
1097                         WriteAngle(MSG_ONE, veh.tur_head.angles_x + veh.angles_x); // tilt
1098                         WriteAngle(MSG_ONE, veh.tur_head.angles_y + veh.angles_y); // yaw
1099                         WriteAngle(MSG_ONE, 0);                                                                   // roll
1100                 }
1101                 else
1102                 {
1103                         WriteAngle(MSG_ONE, veh.angles_x * -1); // tilt
1104                         WriteAngle(MSG_ONE, veh.angles_y);        // yaw
1105                         WriteAngle(MSG_ONE, 0);                           // roll
1106                 }
1107         }
1108
1109         vehicles_clearreturn(veh);
1110
1111         CSQCVehicleSetup(pl, veh.vehicleid);
1112
1113         MUTATOR_CALLHOOK(VehicleEnter, pl, veh);
1114
1115         setself(veh);
1116         CSQCModel_UnlinkEntity(veh);
1117         Vehicle info = get_vehicleinfo(veh.vehicleid);
1118         info.vr_enter(info);
1119         setself(this);
1120
1121         antilag_clear(pl);
1122 }
1123
1124 void vehicles_think()
1125 {SELFPARAM();
1126         self.nextthink = time;
1127
1128         if(self.owner)
1129                 self.owner.vehicle_weapon2mode = self.vehicle_weapon2mode;
1130
1131         Vehicle info = get_vehicleinfo(self.vehicleid);
1132         info.vr_think(info);
1133
1134         CSQCMODEL_AUTOUPDATE(self);
1135 }
1136
1137 // initialization
1138 void vehicles_spawn()
1139 {SELFPARAM();
1140         LOG_TRACE("Spawning vehicle: ", self.classname, "\n");
1141
1142         // disown & reset
1143         self.vehicle_hudmodel.viewmodelforclient = self;
1144
1145         self.owner                              = world;
1146         self.touch                              = vehicles_touch;
1147         self.event_damage               = vehicles_damage;
1148         self.iscreature                 = true;
1149         self.teleportable               = false; // no teleporting for vehicles, too buggy
1150         self.damagedbycontents  = true;
1151         self.movetype                   = MOVETYPE_WALK;
1152         self.solid                              = SOLID_SLIDEBOX;
1153         self.takedamage                 = DAMAGE_AIM;
1154         self.deadflag                   = DEAD_NO;
1155         self.bot_attack                 = true;
1156         self.flags                              = FL_NOTARGET;
1157         self.avelocity                  = '0 0 0';
1158         self.velocity                   = '0 0 0';
1159         self.think                              = vehicles_think;
1160         self.nextthink                  = time;
1161
1162         // Reset locking
1163         self.lock_strength = 0;
1164         self.lock_target = world;
1165         self.misc_bulletcounter = 0;
1166
1167         // Return to spawn
1168         self.angles = self.pos2;
1169         setorigin(self, self.pos1);
1170         // Show it
1171         Send_Effect(EFFECT_TELEPORT, self.origin + '0 0 64', '0 0 0', 1);
1172
1173         if(self.vehicle_controller)
1174                 self.team = self.vehicle_controller.team;
1175
1176         entity head; // remove hooks (if any)
1177         FOR_EACH_PLAYER(head)
1178         if(head.hook.aiment == self)
1179                 RemoveGrapplingHook(head);
1180
1181         vehicles_reset_colors();
1182
1183         Vehicle info = get_vehicleinfo(self.vehicleid);
1184         info.vr_spawn(info);
1185
1186         CSQCMODEL_AUTOINIT(self);
1187 }
1188
1189 bool vehicle_initialize(entity veh, bool nodrop)
1190 {SELFPARAM();
1191         if(!autocvar_g_vehicles)
1192                 return false;
1193
1194         if(!veh.vehicleid)
1195                 return false;
1196
1197         if(!veh.tur_head) {
1198                 Vehicle info = get_vehicleinfo(veh.vehicleid);
1199                 info.vr_precache(info);
1200         }
1201
1202         if(self.targetname && self.targetname != "")
1203         {
1204                 self.vehicle_controller = find(world, target, self.targetname);
1205                 if(!self.vehicle_controller)
1206                 {
1207                         bprint("^1WARNING: ^7Vehicle with invalid .targetname\n");
1208                         self.active = ACTIVE_ACTIVE;
1209                 }
1210                 else
1211                 {
1212                         self.team = self.vehicle_controller.team;
1213                         self.use = vehicle_use;
1214
1215                         if(teamplay)
1216                         {
1217                                 if(self.vehicle_controller.team == 0)
1218                                         self.active = ACTIVE_NOT;
1219                                 else
1220                                         self.active = ACTIVE_ACTIVE;
1221                         }
1222                 }
1223         }
1224         else { self.active = ACTIVE_ACTIVE; }
1225
1226         if(self.team && (!teamplay || !autocvar_g_vehicles_teams))
1227                 self.team = 0;
1228
1229         if(self.mdl == "" || !self.mdl)
1230                 _setmodel(self, veh.model);
1231         else
1232                 _setmodel(self, self.mdl);
1233
1234         self.vehicle_flags |= VHF_ISVEHICLE;
1235
1236         self.vehicle_viewport           = new(vehicle_viewport);
1237         self.vehicle_hudmodel           = new(vehicle_hudmodel);
1238         self.tur_head                           = new(tur_head);
1239         self.tur_head.owner                     = self;
1240         self.takedamage                         = DAMAGE_NO;
1241         self.bot_attack                         = true;
1242         self.iscreature                         = true;
1243         self.teleportable                       = false; // no teleporting for vehicles, too buggy
1244         self.damagedbycontents          = true;
1245         self.vehicleid                          = veh.vehicleid;
1246         self.PlayerPhysplug                     = veh.PlayerPhysplug;
1247         self.event_damage                       = func_null;
1248         self.touch                                      = vehicles_touch;
1249         self.think                                      = vehicles_spawn;
1250         self.nextthink                          = time;
1251         self.effects                            = EF_NODRAW;
1252         self.dphitcontentsmask          = DPCONTENTS_BODY | DPCONTENTS_SOLID;
1253
1254         if(autocvar_g_playerclip_collisions)
1255                 self.dphitcontentsmask |= DPCONTENTS_PLAYERCLIP;
1256
1257         if(autocvar_g_nodepthtestplayers)
1258                 self.effects |= EF_NODEPTHTEST;
1259
1260         if(autocvar_g_fullbrightplayers)
1261                 self.effects |= EF_FULLBRIGHT;
1262
1263         _setmodel(self.vehicle_hudmodel, veh.hud_model);
1264         setmodel(self.vehicle_viewport, MDL_Null);
1265
1266         if(veh.head_model != "")
1267         {
1268                 _setmodel(self.tur_head, veh.head_model);
1269                 setattachment(self.tur_head, self, veh.tag_head);
1270                 setattachment(self.vehicle_hudmodel, self.tur_head, veh.tag_hud);
1271                 setattachment(self.vehicle_viewport, self.vehicle_hudmodel, veh.tag_view);
1272         }
1273         else
1274         {
1275                 setattachment(self.tur_head, self, "");
1276                 setattachment(self.vehicle_hudmodel, self, veh.tag_hud);
1277                 setattachment(self.vehicle_viewport, self.vehicle_hudmodel, veh.tag_view);
1278         }
1279
1280         setsize(self, veh.mins, veh.maxs);
1281
1282         if(!nodrop)
1283         {
1284                 setorigin(self, self.origin);
1285                 tracebox(self.origin + '0 0 100', veh.mins, veh.maxs, self.origin - '0 0 10000', MOVE_WORLDONLY, self);
1286                 setorigin(self, trace_endpos);
1287         }
1288
1289         self.pos1 = self.origin;
1290         self.pos2 = self.angles;
1291         self.tur_head.team = self.team;
1292
1293         Vehicle info = get_vehicleinfo(veh.vehicleid);
1294         info.vr_setup(info);
1295
1296         if(self.active == ACTIVE_NOT)
1297                 self.nextthink = 0; // wait until activated
1298         else if(autocvar_g_vehicles_delayspawn)
1299                 self.nextthink = time + self.respawntime + (random() * autocvar_g_vehicles_delayspawn_jitter);
1300         else
1301                 self.nextthink = time + game_starttime;
1302
1303         if(MUTATOR_CALLHOOK(VehicleSpawn))
1304                 return false;
1305
1306         return true;
1307 }