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