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