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