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