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