ea9f18fd26bc29e0e52fd82917ce18ce6bbea452
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / vehicles / sv_vehicles.qc
1 #include "sv_vehicles.qh"
2 #include "../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 to, int sf)
42 {SELFPARAM();
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, W_Sound("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, "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, "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_POWERUP);
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, "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, "");
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, "models/vhshield.md3");
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                 VEH_ACTION(self.vehicleid, VR_DEATH);
741                 vehicles_setreturn(self);
742         }
743 }
744
745 float vehicles_crushable(entity e)
746 {
747         if(IS_PLAYER(e) && time >= e.vehicle_enter_delay)
748                 return true;
749
750         if(IS_MONSTER(e))
751                 return true;
752
753         return false;
754 }
755
756 void vehicles_impact(float _minspeed, float _speedfac, float _maxpain)
757 {SELFPARAM();
758         if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
759                 return;
760
761         if(self.play_time < time)
762         {
763                 float wc = vlen(self.velocity - self.oldvelocity);
764                 //dprint("oldvel: ", vtos(self.oldvelocity), "\n");
765                 //dprint("vel: ", vtos(self.velocity), "\n");
766                 if(_minspeed < wc)
767                 {
768                         float take = min(_speedfac * wc, _maxpain);
769                         Damage (self, world, world, take, DEATH_FALL, self.origin, '0 0 0');
770                         self.play_time = time + 0.25;
771
772                         //dprint("wc: ", ftos(wc), "\n");
773                         //dprint("take: ", ftos(take), "\n");
774                 }
775         }
776 }
777
778 // vehicle enter/exit handling
779 vector vehicles_findgoodexit(vector prefer_spot)
780 {SELFPARAM();
781         //vector exitspot;
782         float mysize;
783
784         tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, prefer_spot, MOVE_NORMAL, self.owner);
785         if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
786                 return prefer_spot;
787
788         mysize = 1.5 * vlen(self.maxs - self.mins);
789         float i;
790         vector v, v2;
791         v2 = 0.5 * (self.absmin + self.absmax);
792         for(i = 0; i < 100; ++i)
793         {
794                 v = randomvec();
795                 v_z = 0;
796                 v = v2 + normalize(v) * mysize;
797                 tracebox(v2, PL_MIN, PL_MAX, v, MOVE_NORMAL, self.owner);
798                 if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
799                         return v;
800         }
801
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_forward * 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         exitspot = (self.origin + '0 0 48') - v_right * mysize;
819         tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, exitspot, MOVE_NORMAL, self.owner);
820         if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
821                 return exitspot;
822         */
823
824         return self.origin;
825 }
826
827 void vehicles_exit(bool eject)
828 {SELFPARAM();
829         entity _vehicle;
830         entity _player;
831
832         if(vehicles_exit_running)
833         {
834                 LOG_TRACE("^1vehicles_exit allready running! this is not good..\n");
835                 return;
836         }
837
838         vehicles_exit_running = true;
839         if(IS_CLIENT(self))
840         {
841                 _vehicle = self.vehicle;
842
843                 if (_vehicle.vehicle_flags & VHF_PLAYERSLOT)
844                 {
845                         _vehicle.vehicle_exit(eject);
846                         setself(this);
847                         vehicles_exit_running = false;
848                         return;
849                 }
850         }
851         else
852                 _vehicle = self;
853
854         _player = _vehicle.owner;
855
856         setself(_vehicle);
857
858         if (_player)
859         {
860                 if (IS_REAL_CLIENT(_player))
861                 {
862                         msg_entity = _player;
863                         WriteByte (MSG_ONE, SVC_SETVIEWPORT);
864                         WriteEntity( MSG_ONE, _player);
865
866                         WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
867                         WriteAngle(MSG_ONE, 0);
868                         WriteAngle(MSG_ONE, _vehicle.angles_y);
869                         WriteAngle(MSG_ONE, 0);
870                 }
871
872                 setsize(_player, PL_MIN,PL_MAX);
873
874                 _player.takedamage              = DAMAGE_AIM;
875                 _player.solid                   = SOLID_SLIDEBOX;
876                 _player.movetype                = MOVETYPE_WALK;
877                 _player.effects            &= ~EF_NODRAW;
878                 _player.teleportable    = TELEPORT_NORMAL;
879                 _player.alpha                   = 1;
880                 _player.PlayerPhysplug  = func_null;
881                 _player.vehicle                 = world;
882                 _player.view_ofs                = PL_VIEW_OFS;
883                 _player.event_damage    = PlayerDamage;
884                 _player.hud                             = HUD_NORMAL;
885                 _player.switchweapon    = _vehicle.switchweapon;
886                 _player.last_vehiclecheck = time + 3;
887                 _player.vehicle_enter_delay = time + 2;
888
889                 CSQCVehicleSetup(_player, HUD_NORMAL);
890         }
891         _vehicle.flags |= FL_NOTARGET;
892
893         if(_vehicle.deadflag == DEAD_NO)
894                 _vehicle.avelocity = '0 0 0';
895
896         _vehicle.tur_head.nodrawtoclient = world;
897
898         if(!teamplay)
899                 _vehicle.team = 0;
900
901         Kill_Notification(NOTIF_ONE, _player, MSG_CENTER_CPID, CPID_VEHICLES);
902         Kill_Notification(NOTIF_ONE, _player, MSG_CENTER_CPID, CPID_VEHICLES_OTHER); // kill all vehicle notifications when exiting a vehicle?
903
904         WaypointSprite_Kill(_vehicle.wps_intruder);
905
906         MUTATOR_CALLHOOK(VehicleExit, _player, _vehicle);
907
908         _vehicle.team = _vehicle.tur_head.team;
909
910         sound (_vehicle, CH_TRIGGER_SINGLE, "misc/null.wav", 1, ATTEN_NORM);
911         _vehicle.vehicle_hudmodel.viewmodelforclient = _vehicle;
912         _vehicle.phase = time + 1;
913
914         _vehicle.vehicle_exit(eject);
915
916         vehicles_setreturn(_vehicle);
917         vehicles_reset_colors();
918         _vehicle.owner = world;
919
920         CSQCMODEL_AUTOINIT(self);
921
922         setself(this);
923         vehicles_exit_running = false;
924 }
925
926 void vehicles_touch()
927 {SELFPARAM();
928         if(MUTATOR_CALLHOOK(VehicleTouch, self, other))
929                 return;
930
931         // Vehicle currently in use
932         if(self.owner)
933         {
934                 if(!forbidWeaponUse(self.owner))
935                 if(other != world)
936                 if((self.origin_z + self.maxs_z) > (other.origin_z))
937                 if(vehicles_crushable(other))
938                 {
939                         if(vlen(self.velocity) >= 30)
940                                 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);
941
942                         return; // Dont do selfdamage when hitting "soft targets".
943                 }
944
945                 if(self.play_time < time)
946                         VEH_ACTION(self.vehicleid, VR_IMPACT);
947
948                 return;
949         }
950
951         if(autocvar_g_vehicles_enter)
952                 return;
953
954         vehicles_enter(other, self);
955 }
956
957 bool vehicle_impulse(int imp)
958 {SELFPARAM();
959         switch(imp)
960         {
961                 case 17:
962                 {
963                         stuffcmd(self, "\ntoggle cl_eventchase_vehicle\nset _vehicles_shownchasemessage 1\n");
964                         return true;
965                 }
966         }
967
968         return false;
969 }
970
971 void vehicles_enter(entity pl, entity veh)
972 {SELFPARAM();
973    // Remove this when bots know how to use vehicles
974         if((IS_BOT_CLIENT(pl) && !autocvar_g_vehicles_allow_bots))
975                 return;
976
977         if((!IS_PLAYER(pl))
978         || (veh.phase >= time)
979         || (pl.vehicle_enter_delay >= time)
980         || (pl.frozen)
981         || (pl.deadflag != DEAD_NO)
982         || (pl.vehicle)
983         ) { return; }
984
985         if(autocvar_g_vehicles_enter) // vehicle's touch function should handle this if entering via use key is disabled (TODO)
986         if(veh.vehicle_flags & VHF_MULTISLOT)
987         if(veh.owner)
988         {
989                 setself(veh);
990                 other = pl; // TODO: fix
991
992                 if(!veh.gunner1)
993                 if(time >= veh.gun1.phase)
994                 if(veh.gun1.vehicle_enter)
995                 if(veh.gun1.vehicle_enter())
996                 {
997                         setself(this);
998                         return;
999                 }
1000
1001                 if(!veh.gunner2)
1002                 if(time >= veh.gun2.phase)
1003                 if(veh.gun2.vehicle_enter)
1004                 if(veh.gun2.vehicle_enter())
1005                 {
1006                         setself(this);
1007                         return;
1008                 }
1009
1010                 setself(this);
1011         }
1012
1013         if(teamplay)
1014         if(veh.team)
1015         if(DIFF_TEAM(pl, veh))
1016         if(autocvar_g_vehicles_steal)
1017         {
1018                 entity head;
1019                 FOR_EACH_PLAYER(head) if(SAME_TEAM(head, veh))
1020                         Send_Notification(NOTIF_ONE, head, MSG_CENTER, CENTER_VEHICLE_STEAL);
1021
1022                 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_VEHICLE_STEAL_SELF);
1023
1024                 if (autocvar_g_vehicles_steal_show_waypoint) {
1025                         entity wp = WaypointSprite_Spawn(WP_VehicleIntruder, 0, 0, pl, '0 0 68', world, veh.team, veh, wps_intruder, true, RADARICON_DANGER);
1026                         wp.colormod = Team_ColorRGB(pl.team);
1027                 }
1028         }
1029         else return;
1030
1031         RemoveGrapplingHook(pl);
1032
1033         veh.vehicle_ammo1 = 0;
1034         veh.vehicle_ammo2 = 0;
1035         veh.vehicle_reload1 = 0;
1036         veh.vehicle_reload2 = 0;
1037         veh.vehicle_energy = 0;
1038
1039         veh.owner = pl;
1040         pl.vehicle = veh;
1041
1042         // .viewmodelforclient works better.
1043         //veh.vehicle_hudmodel.drawonlytoclient = veh.owner;
1044
1045         veh.vehicle_hudmodel.viewmodelforclient = pl;
1046
1047         tracebox(pl.origin, PL_MIN, PL_MAX, pl.origin, false, pl);
1048         pl.crouch = false;
1049         pl.view_ofs = PL_VIEW_OFS;
1050         setsize (pl, PL_MIN, PL_MAX);
1051
1052         veh.event_damage        = vehicles_damage;
1053         veh.nextthink           = 0;
1054         pl.angles                       = veh.angles;
1055         pl.takedamage           = DAMAGE_NO;
1056         pl.solid                        = SOLID_NOT;
1057         pl.movetype                     = MOVETYPE_NOCLIP;
1058         pl.teleportable         = false;
1059         pl.alpha                        = -1;
1060         pl.event_damage         = func_null;
1061         pl.view_ofs                     = '0 0 0';
1062         veh.colormap            = pl.colormap;
1063         if(veh.tur_head)
1064                 veh.tur_head.colormap = pl.colormap;
1065         veh.switchweapon = pl.switchweapon;
1066         pl.hud = veh.vehicleid;
1067         pl.PlayerPhysplug = veh.PlayerPhysplug;
1068
1069         pl.vehicle_ammo1 = veh.vehicle_ammo1;
1070         pl.vehicle_ammo2 = veh.vehicle_ammo2;
1071         pl.vehicle_reload1 = veh.vehicle_reload1;
1072         pl.vehicle_reload2 = veh.vehicle_reload2;
1073         pl.vehicle_energy = veh.vehicle_energy;
1074
1075         // Cant do this, hides attached objects too.
1076         //veh.exteriormodeltoclient = veh.owner;
1077         //veh.tur_head.exteriormodeltoclient = veh.owner;
1078
1079         pl.flags &= ~FL_ONGROUND;
1080         veh.flags &= ~FL_ONGROUND;
1081
1082         veh.team = pl.team;
1083         veh.flags -= FL_NOTARGET;
1084
1085         if (IS_REAL_CLIENT(pl))
1086         {
1087                 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_VEHICLE_ENTER);
1088
1089                 msg_entity = pl;
1090                 WriteByte (MSG_ONE, SVC_SETVIEWPORT);
1091                 WriteEntity(MSG_ONE, veh.vehicle_viewport);
1092
1093                 WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
1094                 if(veh.tur_head)
1095                 {
1096                         WriteAngle(MSG_ONE, veh.tur_head.angles_x + veh.angles_x); // tilt
1097                         WriteAngle(MSG_ONE, veh.tur_head.angles_y + veh.angles_y); // yaw
1098                         WriteAngle(MSG_ONE, 0);                                                                   // roll
1099                 }
1100                 else
1101                 {
1102                         WriteAngle(MSG_ONE, veh.angles_x * -1); // tilt
1103                         WriteAngle(MSG_ONE, veh.angles_y);        // yaw
1104                         WriteAngle(MSG_ONE, 0);                           // roll
1105                 }
1106         }
1107
1108         vehicles_clearreturn(veh);
1109
1110         CSQCVehicleSetup(pl, veh.vehicleid);
1111
1112         MUTATOR_CALLHOOK(VehicleEnter, pl, veh);
1113
1114         setself(veh);
1115         CSQCModel_UnlinkEntity();
1116         VEH_ACTION(veh.vehicleid, VR_ENTER);
1117         setself(this);
1118
1119         antilag_clear(pl);
1120 }
1121
1122 void vehicles_think()
1123 {SELFPARAM();
1124         self.nextthink = time;
1125
1126         if(self.owner)
1127                 self.owner.vehicle_weapon2mode = self.vehicle_weapon2mode;
1128
1129         VEH_ACTION(self.vehicleid, VR_THINK);
1130
1131         CSQCMODEL_AUTOUPDATE(self);
1132 }
1133
1134 // initialization
1135 void vehicles_spawn()
1136 {SELFPARAM();
1137         LOG_TRACE("Spawning vehicle: ", self.classname, "\n");
1138
1139         // disown & reset
1140         self.vehicle_hudmodel.viewmodelforclient = self;
1141
1142         self.owner                              = world;
1143         self.touch                              = vehicles_touch;
1144         self.event_damage               = vehicles_damage;
1145         self.iscreature                 = true;
1146         self.teleportable               = false; // no teleporting for vehicles, too buggy
1147         self.damagedbycontents  = true;
1148         self.movetype                   = MOVETYPE_WALK;
1149         self.solid                              = SOLID_SLIDEBOX;
1150         self.takedamage                 = DAMAGE_AIM;
1151         self.deadflag                   = DEAD_NO;
1152         self.bot_attack                 = true;
1153         self.flags                              = FL_NOTARGET;
1154         self.avelocity                  = '0 0 0';
1155         self.velocity                   = '0 0 0';
1156         self.think                              = vehicles_think;
1157         self.nextthink                  = time;
1158
1159         // Reset locking
1160         self.lock_strength = 0;
1161         self.lock_target = world;
1162         self.misc_bulletcounter = 0;
1163
1164         // Return to spawn
1165         self.angles = self.pos2;
1166         setorigin(self, self.pos1);
1167         // Show it
1168         Send_Effect(EFFECT_TELEPORT, self.origin + '0 0 64', '0 0 0', 1);
1169
1170         if(self.vehicle_controller)
1171                 self.team = self.vehicle_controller.team;
1172
1173         entity head; // remove hooks (if any)
1174         FOR_EACH_PLAYER(head)
1175         if(head.hook.aiment == self)
1176                 RemoveGrapplingHook(head);
1177
1178         vehicles_reset_colors();
1179
1180         VEH_ACTION(self.vehicleid, VR_SPAWN);
1181
1182         CSQCMODEL_AUTOINIT(self);
1183 }
1184
1185 bool vehicle_initialize(entity veh, bool nodrop)
1186 {SELFPARAM();
1187         if(!autocvar_g_vehicles)
1188                 return false;
1189
1190         if(!veh.vehicleid)
1191                 return false;
1192
1193         if(!veh.tur_head) { VEH_ACTION(veh.vehicleid, VR_PRECACHE); }
1194
1195         if(self.targetname && self.targetname != "")
1196         {
1197                 self.vehicle_controller = find(world, target, self.targetname);
1198                 if(!self.vehicle_controller)
1199                 {
1200                         bprint("^1WARNING: ^7Vehicle with invalid .targetname\n");
1201                         self.active = ACTIVE_ACTIVE;
1202                 }
1203                 else
1204                 {
1205                         self.team = self.vehicle_controller.team;
1206                         self.use = vehicle_use;
1207
1208                         if(teamplay)
1209                         {
1210                                 if(self.vehicle_controller.team == 0)
1211                                         self.active = ACTIVE_NOT;
1212                                 else
1213                                         self.active = ACTIVE_ACTIVE;
1214                         }
1215                 }
1216         }
1217         else { self.active = ACTIVE_ACTIVE; }
1218
1219         if(self.team && (!teamplay || !autocvar_g_vehicles_teams))
1220                 self.team = 0;
1221
1222         if(self.mdl == "" || !self.mdl)
1223                 setmodel(self, veh.model);
1224         else
1225                 setmodel(self, self.mdl);
1226
1227         self.vehicle_flags |= VHF_ISVEHICLE;
1228
1229         self.vehicle_viewport           = spawn();
1230         self.vehicle_hudmodel           = spawn();
1231         self.tur_head                           = spawn();
1232         self.tur_head.owner                     = self;
1233         self.takedamage                         = DAMAGE_NO;
1234         self.bot_attack                         = true;
1235         self.iscreature                         = true;
1236         self.teleportable                       = false; // no teleporting for vehicles, too buggy
1237         self.damagedbycontents          = true;
1238         self.vehicleid                          = veh.vehicleid;
1239         self.PlayerPhysplug                     = veh.PlayerPhysplug;
1240         self.event_damage                       = func_null;
1241         self.touch                                      = vehicles_touch;
1242         self.think                                      = vehicles_spawn;
1243         self.nextthink                          = time;
1244         self.effects                            = EF_NODRAW;
1245         self.dphitcontentsmask          = DPCONTENTS_BODY | DPCONTENTS_SOLID;
1246
1247         if(autocvar_g_playerclip_collisions)
1248                 self.dphitcontentsmask |= DPCONTENTS_PLAYERCLIP;
1249
1250         if(autocvar_g_nodepthtestplayers)
1251                 self.effects |= EF_NODEPTHTEST;
1252
1253         if(autocvar_g_fullbrightplayers)
1254                 self.effects |= EF_FULLBRIGHT;
1255
1256         setmodel(self.vehicle_hudmodel, veh.hud_model);
1257         setmodel(self.vehicle_viewport, "null");
1258
1259         if(veh.head_model != "")
1260         {
1261                 setmodel(self.tur_head, veh.head_model);
1262                 setattachment(self.tur_head, self, veh.tag_head);
1263                 setattachment(self.vehicle_hudmodel, self.tur_head, veh.tag_hud);
1264                 setattachment(self.vehicle_viewport, self.vehicle_hudmodel, veh.tag_view);
1265         }
1266         else
1267         {
1268                 setattachment(self.tur_head, self, "");
1269                 setattachment(self.vehicle_hudmodel, self, veh.tag_hud);
1270                 setattachment(self.vehicle_viewport, self.vehicle_hudmodel, veh.tag_view);
1271         }
1272
1273         setsize(self, veh.mins, veh.maxs);
1274
1275         if(!nodrop)
1276         {
1277                 setorigin(self, self.origin);
1278                 tracebox(self.origin + '0 0 100', veh.mins, veh.maxs, self.origin - '0 0 10000', MOVE_WORLDONLY, self);
1279                 setorigin(self, trace_endpos);
1280         }
1281
1282         self.pos1 = self.origin;
1283         self.pos2 = self.angles;
1284         self.tur_head.team = self.team;
1285
1286         VEH_ACTION(veh.vehicleid, VR_SETUP);
1287
1288         if(self.active == ACTIVE_NOT)
1289                 self.nextthink = 0; // wait until activated
1290         else if(autocvar_g_vehicles_delayspawn)
1291                 self.nextthink = time + self.respawntime + (random() * autocvar_g_vehicles_delayspawn_jitter);
1292         else
1293                 self.nextthink = time + game_starttime;
1294
1295         if(MUTATOR_CALLHOOK(VehicleSpawn))
1296                 return false;
1297
1298         return true;
1299 }