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