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