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