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