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