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