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