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