1 #include "sv_vehicles.qh"
3 bool SendAuxiliaryXhair(entity this, entity to, int sf)
5 WriteHeader(MSG_ENTITY, ENT_CLIENT_AUXILIARYXHAIR);
6 WriteByte(MSG_ENTITY, sf);
8 WriteByte(MSG_ENTITY, this.cnt);
12 WriteCoord(MSG_ENTITY, this.origin_x);
13 WriteCoord(MSG_ENTITY, this.origin_y);
14 WriteCoord(MSG_ENTITY, this.origin_z);
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));
27 .vector axh_prevorigin;
28 .vector axh_prevcolors;
30 void UpdateAuxiliaryXhair(entity own, vector loc, vector clr, int axh_id)
32 if(!IS_REAL_CLIENT(own))
35 axh_id = bound(0, axh_id, MAX_AXH);
36 entity axh = own.(AuxiliaryXhair[axh_id]);
38 if(axh == NULL || wasfreed(axh)) // MADNESS? THIS IS QQQQCCCCCCCCC (wasfreed, why do you exsist? Mario: because of sloppy code like this)
40 axh = new(auxiliary_xhair);
42 axh.drawonlytoclient = own;
44 Net_LinkEntity(axh, false, 0, SendAuxiliaryXhair);
47 if(loc != axh.axh_prevorigin)
53 if(clr != axh.axh_prevcolors)
59 own.(AuxiliaryXhair[axh_id]) = axh; // set it anyway...?
62 void CSQCVehicleSetup(entity own, int vehicle_id)
64 if(!IS_REAL_CLIENT(own))
69 WriteHeader(MSG_ONE, TE_CSQC_VEHICLESETUP);
70 WriteByte(MSG_ONE, vehicle_id);
73 void vehicles_locktarget(entity this, float incr, float decr, float _lock_time)
75 if(this.lock_target && IS_DEAD(this.lock_target))
77 this.lock_target = NULL;
78 this.lock_strength = 0;
82 if(this.lock_time > time)
85 if(this.lock_soundtime < time)
87 this.lock_soundtime = time + 0.5;
88 play2(this.owner, "vehicles/locked.wav");
96 if(SAME_TEAM(trace_ent, this))
99 if(IS_DEAD(trace_ent))
102 if(!(IS_VEHICLE(trace_ent) || IS_TURRET(trace_ent)))
105 if(trace_ent.alpha <= 0.5 && trace_ent.alpha != 0)
106 trace_ent = NULL; // invisible
109 if(this.lock_target == NULL && trace_ent != NULL)
110 this.lock_target = trace_ent;
112 if(this.lock_target && trace_ent == this.lock_target)
114 if(this.lock_strength != 1 && this.lock_strength + incr >= 1)
116 play2(this.owner, "vehicles/lock.wav");
117 this.lock_soundtime = time + 0.8;
119 else if (this.lock_strength != 1 && this.lock_soundtime < time)
121 play2(this.owner, "vehicles/locking.wav");
122 this.lock_soundtime = time + 0.3;
126 // Have a locking target
127 // Trace hit current target
128 if(trace_ent == this.lock_target && trace_ent != NULL)
130 this.lock_strength = min(this.lock_strength + incr, 1);
131 if(this.lock_strength == 1)
132 this.lock_time = time + _lock_time;
137 this.lock_strength = max(this.lock_strength - decr * 2, 0);
139 this.lock_strength = max(this.lock_strength - decr, 0);
141 if(this.lock_strength == 0)
142 this.lock_target = NULL;
146 float vehicle_altitude(entity this, float amax)
148 tracebox(this.origin, this.mins, this.maxs, this.origin - ('0 0 1' * amax), MOVE_WORLDONLY, this);
149 return vlen(this.origin - trace_endpos);
152 vector vehicles_force_fromtag_hover(entity this, string tag_name, float spring_length, float max_power)
154 force_fromtag_origin = gettaginfo(this, gettagindex(this, tag_name));
155 v_forward = normalize(v_forward) * -1;
156 traceline(force_fromtag_origin, force_fromtag_origin - (v_forward * spring_length), MOVE_NORMAL, this);
158 force_fromtag_power = (1 - trace_fraction) * max_power;
159 force_fromtag_normpower = force_fromtag_power / max_power;
161 return v_forward * force_fromtag_power;
164 vector vehicles_force_fromtag_maglev(entity this, string tag_name, float spring_length, float max_power)
166 force_fromtag_origin = gettaginfo(this, gettagindex(this, tag_name));
167 v_forward = normalize(v_forward) * -1;
168 traceline(force_fromtag_origin, force_fromtag_origin - (v_forward * spring_length), MOVE_NORMAL, this);
170 // TODO - this may NOT be compatible with wall/celing movement, unhardcode 0.25 (engine count multiplier)
171 if(trace_fraction == 1.0)
173 force_fromtag_normpower = -0.25;
177 force_fromtag_power = ((1 - trace_fraction) - trace_fraction) * max_power;
178 force_fromtag_normpower = force_fromtag_power / max_power;
180 return v_forward * force_fromtag_power;
183 // projectile handling
184 void vehicles_projectile_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
186 // Ignore damage from oterh projectiles from my owner (dont mess up volly's)
187 if(inflictor.owner == this.owner)
190 this.health -= damage;
191 this.velocity += force;
194 this.takedamage = DAMAGE_NO;
195 this.event_damage = func_null;
196 setthink(this, adaptor_think2use);
197 this.nextthink = time;
201 void vehicles_projectile_explode(entity this, entity toucher)
203 if(this.owner && toucher != NULL)
205 if(toucher == this.owner.vehicle)
208 if(toucher == this.owner.vehicle.tur_head)
212 PROJECTILE_TOUCH(this, toucher);
214 this.event_damage = func_null;
215 RadiusDamage (this, this.realowner, this.shot_dmg, 0, this.shot_radius, this, NULL, this.shot_force, this.totalfrags, toucher);
220 void vehicles_projectile_explode_think(entity this)
222 vehicles_projectile_explode(this, NULL);
225 void vehicles_projectile_explode_use(entity this, entity actor, entity trigger)
227 vehicles_projectile_explode(this, trigger);
230 entity vehicles_projectile(entity this, string _mzlfx, Sound _mzlsound,
231 vector _org, vector _vel,
232 float _dmg, float _radi, float _force, float _size,
233 int _deahtype, float _projtype, float _health,
234 bool _cull, bool _clianim, entity _owner)
236 TC(Sound, _mzlsound);
241 PROJECTILE_MAKETRIGGER(proj);
242 setorigin(proj, _org);
244 proj.shot_dmg = _dmg;
245 proj.shot_radius = _radi;
246 proj.shot_force = _force;
247 proj.totalfrags = _deahtype;
248 proj.solid = SOLID_BBOX;
249 set_movetype(proj, MOVETYPE_FLYMISSILE);
250 proj.flags = FL_PROJECTILE;
251 IL_PUSH(g_projectiles, proj);
252 proj.bot_dodge = true;
253 proj.bot_dodgerating = _dmg;
254 proj.velocity = _vel;
255 settouch(proj, vehicles_projectile_explode);
256 proj.use = vehicles_projectile_explode_use;
258 proj.realowner = _owner;
259 setthink(proj, SUB_Remove);
260 proj.nextthink = time + 30;
264 proj.takedamage = DAMAGE_AIM;
265 proj.event_damage = vehicles_projectile_damage;
266 proj.health = _health;
269 proj.flags |= FL_NOTARGET;
271 if(_mzlsound != SND_Null)
272 sound (this, CH_WEAPON_A, _mzlsound, VOL_BASE, ATTEN_NORM);
275 Send_Effect_(_mzlfx, proj.origin, proj.velocity, 1);
277 setsize (proj, '-1 -1 -1' * _size, '1 1 1' * _size);
279 CSQCProjectile(proj, _clianim, _projtype, _cull);
284 void vehicles_gib_explode(entity this)
286 sound (this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
287 Send_Effect(EFFECT_EXPLOSION_SMALL, randomvec() * 80 + (this.origin + '0 0 100'), '0 0 0', 1);
288 Send_Effect(EFFECT_EXPLOSION_SMALL, this.wp00.origin + '0 0 64', '0 0 0', 1);
292 void vehicles_gib_touch(entity this, entity toucher)
294 vehicles_gib_explode(this);
297 void vehicles_gib_think(entity this)
303 this.nextthink = time + 0.1;
306 entity vehicle_tossgib(entity this, entity _template, vector _vel, string _tag, bool _burn, bool _explode, float _maxtime, vector _rot)
308 entity _gib = spawn();
309 _setmodel(_gib, _template.model);
310 vector org = gettaginfo(this, gettagindex(this, _tag));
311 setorigin(_gib, org);
312 _gib.velocity = _vel;
313 set_movetype(_gib, MOVETYPE_TOSS);
314 _gib.solid = SOLID_CORPSE;
315 _gib.colormod = '-0.5 -0.5 -0.5';
316 _gib.effects = EF_LOWPRECISION;
317 _gib.avelocity = _rot;
320 _gib.effects |= EF_FLAME;
324 setthink(_gib, vehicles_gib_explode);
325 _gib.nextthink = time + random() * _explode;
326 settouch(_gib, vehicles_gib_touch);
330 _gib.cnt = time + _maxtime;
331 setthink(_gib, vehicles_gib_think);
332 _gib.nextthink = time + _maxtime - 1;
338 bool vehicle_addplayerslot( entity _owner,
342 bool(entity,float) _framefunc,
343 void(entity,bool) _exitfunc, float(entity, entity) _enterfunc)
345 if(!(_owner.vehicle_flags & VHF_MULTISLOT))
346 _owner.vehicle_flags |= VHF_MULTISLOT;
348 _slot.PlayerPhysplug = _framefunc;
349 _slot.vehicle_exit = _exitfunc;
350 _slot.vehicle_enter = _enterfunc;
352 _slot.vehicle_flags = VHF_PLAYERSLOT;
353 _slot.vehicle_viewport = spawn();
354 _slot.vehicle_hudmodel = spawn();
355 _slot.vehicle_hudmodel.viewmodelforclient = _slot;
356 _slot.vehicle_viewport.effects = (EF_ADDITIVE | EF_DOUBLESIDED | EF_FULLBRIGHT | EF_NODEPTHTEST | EF_NOGUNBOB | EF_NOSHADOW | EF_LOWPRECISION | EF_SELECTABLE | EF_TELEPORT_BIT);
358 setmodel(_slot.vehicle_hudmodel, _hud_model);
359 setmodel(_slot.vehicle_viewport, MDL_Null);
361 setattachment(_slot.vehicle_hudmodel, _slot, "");
362 setattachment(_slot.vehicle_viewport, _slot.vehicle_hudmodel, "");
367 vector vehicle_aimturret(entity _vehic, vector _target, entity _turrret, string _tagname,
368 float _pichlimit_min, float _pichlimit_max,
369 float _rotlimit_min, float _rotlimit_max, float _aimspeed)
373 vtag = gettaginfo(_turrret, gettagindex(_turrret, _tagname));
374 vtmp = vectoangles(normalize(_target - vtag));
375 vtmp = AnglesTransform_ToAngles(AnglesTransform_LeftDivide(AnglesTransform_FromAngles(_vehic.angles), AnglesTransform_FromAngles(vtmp))) - _turrret.angles;
376 vtmp = AnglesTransform_Normalize(vtmp, true);
377 ftmp = _aimspeed * frametime;
378 vtmp_y = bound(-ftmp, vtmp_y, ftmp);
379 vtmp_x = bound(-ftmp, vtmp_x, ftmp);
380 _turrret.angles_y = bound(_rotlimit_min, _turrret.angles_y + vtmp_y, _rotlimit_max);
381 _turrret.angles_x = bound(_pichlimit_min, _turrret.angles_x + vtmp_x, _pichlimit_max);
385 void vehicles_reset_colors(entity this)
388 const vector cmod = '0 0 0';
389 if(this.team && teamplay)
390 cmap = 1024 + (this.team - 1) * 17;
393 if(autocvar_g_nodepthtestplayers)
394 eff |= EF_NODEPTHTEST;
395 if(autocvar_g_fullbrightplayers)
396 eff |= EF_FULLBRIGHT;
398 // Find all ents attacked to main model and setup effects, colormod etc.
399 FOREACH_ENTITY_ENT(tag_entity, this,
401 if(it != this.vehicle_shieldent)
410 // Also check head tags
411 FOREACH_ENTITY_ENT(tag_entity, this.tur_head,
413 if(it != this.vehicle_shieldent)
422 this.vehicle_hudmodel.effects = this.effects = eff; // | EF_LOWPRECISION;
423 this.vehicle_hudmodel.colormod = this.colormod = cmod;
424 this.vehicle_hudmodel.colormap = this.colormap = cmap;
425 this.vehicle_viewport.effects = (EF_ADDITIVE | EF_DOUBLESIDED | EF_FULLBRIGHT | EF_NODEPTHTEST | EF_NOGUNBOB | EF_NOSHADOW | EF_LOWPRECISION | EF_SELECTABLE | EF_TELEPORT_BIT);
428 this.avelocity = '0 0 0';
429 this.velocity = '0 0 0';
433 void vehicles_clearreturn(entity veh)
435 // Remove "return helper" entities, if any.
436 IL_EACH(g_vehicle_returners, it.wp00 == veh,
439 setthink(it, SUB_Remove);
440 it.nextthink = time + 0.1;
441 IL_REMOVE(g_vehicle_returners, it);
443 if(it.waypointsprite_attached)
444 WaypointSprite_Kill(it.waypointsprite_attached);
448 void vehicles_spawn(entity this);
449 void vehicles_return(entity this)
451 Send_Effect(EFFECT_TELEPORT, this.wp00.origin + '0 0 64', '0 0 0', 1);
453 setthink(this.wp00, vehicles_spawn);
454 this.wp00.nextthink = time;
456 if(this.waypointsprite_attached)
457 WaypointSprite_Kill(this.waypointsprite_attached);
462 void vehicles_showwp_goaway(entity this)
464 if(this.waypointsprite_attached)
465 WaypointSprite_Kill(this.waypointsprite_attached);
470 void vehicles_showwp(entity this)
476 setthink(ent, vehicles_return);
477 ent.nextthink = ent.cnt;
481 setthink(ent, vehicles_return);
482 ent.nextthink = time + 1;
485 ent.team = this.wp00.team;
486 ent.wp00 = this.wp00;
487 setorigin(ent, this.wp00.pos1);
489 ent.nextthink = time + 5;
490 setthink(ent, vehicles_showwp_goaway);
494 if(teamplay && ent.team)
495 rgb = Team_ColorRGB(ent.team);
498 entity wp = WaypointSprite_Spawn(WP_Vehicle, 0, 0, ent, '0 0 64', NULL, 0, ent, waypointsprite_attached, true, RADARICON_Vehicle);
500 if(ent.waypointsprite_attached)
502 WaypointSprite_UpdateRule(ent.waypointsprite_attached, ent.wp00.team, SPRITERULE_DEFAULT);
504 WaypointSprite_UpdateBuildFinished(ent.waypointsprite_attached, ent.nextthink);
505 WaypointSprite_Ping(ent.waypointsprite_attached);
509 void vehicles_setreturn(entity veh)
511 vehicles_clearreturn(veh);
513 entity ret = new(vehicle_return);
514 IL_PUSH(g_vehicle_returners, ret);
517 setthink(ret, vehicles_showwp);
521 ret.cnt = time + veh.respawntime;
522 ret.nextthink = min(time + veh.respawntime, time + veh.respawntime - 5);
526 ret.nextthink = min(time + veh.respawntime, time + veh.respawntime - 1);
529 setorigin(ret, veh.pos1 + '0 0 96');
532 void vehicle_use(entity this, entity actor, entity trigger)
534 LOG_DEBUG("vehicle ", this.netname, " used by ", actor.classname);
536 this.tur_head.team = actor.team;
538 if(this.tur_head.team == 0)
539 this.active = ACTIVE_NOT;
541 this.active = ACTIVE_ACTIVE;
543 if(this.active == ACTIVE_ACTIVE && !IS_DEAD(this) && !gameover)
545 LOG_DEBUG("Respawning vehicle: ", this.netname);
546 if(this.effects & EF_NODRAW)
548 setthink(this, vehicles_spawn);
549 this.nextthink = time + 3;
553 vehicles_setreturn(this);
554 vehicles_reset_colors(this);
559 void vehicles_regen(entity this, float timer, .float regen_field, float field_max, float rpause, float regen, float delta_time, float _healthscale)
561 if(this.(regen_field) < field_max)
562 if(timer + rpause < time)
565 regen = regen * (this.vehicle_health / this.max_health);
567 this.(regen_field) = min(this.(regen_field) + regen * delta_time, field_max);
570 this.owner.(regen_field) = (this.(regen_field) / field_max) * 100;
574 void shieldhit_think(entity this)
579 // setmodel(this, MDL_Null);
581 this.effects |= EF_NODRAW;
585 this.nextthink = time + 0.1;
589 void vehicles_painframe(entity this)
591 int myhealth = ((this.owner) ? this.owner.vehicle_health : ((this.vehicle_health / this.max_health) * 100));
594 if(this.pain_frame < time)
596 float _ftmp = myhealth / 50;
597 this.pain_frame = time + max(0.1, 0.1 + (random() * 0.5 * _ftmp));
598 Send_Effect(EFFECT_SMOKE_SMALL, (this.origin + (randomvec() * 80)), '0 0 0', 1);
600 if(this.vehicle_flags & VHF_DMGSHAKE)
601 this.velocity += randomvec() * 30;
603 if(this.vehicle_flags & VHF_DMGROLL)
604 if(this.vehicle_flags & VHF_DMGHEADROLL)
605 this.tur_head.angles += randomvec();
607 this.angles += randomvec();
611 void vehicles_frame(entity this, entity actor)
613 vehicles_painframe(this);
616 void vehicles_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
618 this.dmg_time = time;
621 if(DEATH_ISWEAPON(deathtype, WEP_VORTEX))
622 damage *= autocvar_g_vehicles_vortex_damagerate;
623 else if(DEATH_ISWEAPON(deathtype, WEP_MACHINEGUN))
624 damage *= autocvar_g_vehicles_machinegun_damagerate;
625 else if(DEATH_ISWEAPON(deathtype, WEP_RIFLE))
626 damage *= autocvar_g_vehicles_rifle_damagerate;
627 else if(DEATH_ISWEAPON(deathtype, WEP_VAPORIZER))
628 damage *= autocvar_g_vehicles_vaporizer_damagerate;
629 else if(DEATH_ISWEAPON(deathtype, WEP_SEEKER))
630 damage *= autocvar_g_vehicles_tag_damagerate;
631 else if(DEATH_WEAPONOF(deathtype) != WEP_Null)
632 damage *= autocvar_g_vehicles_weapon_damagerate;
634 this.enemy = attacker;
636 this.pain_finished = time;
638 if((this.vehicle_flags & VHF_HASSHIELD) && (this.vehicle_shield > 0))
640 if (wasfreed(this.vehicle_shieldent) || this.vehicle_shieldent == NULL)
642 this.vehicle_shieldent = spawn();
643 this.vehicle_shieldent.effects = EF_LOWPRECISION;
645 setmodel(this.vehicle_shieldent, MDL_VEH_SHIELD);
646 setattachment(this.vehicle_shieldent, this, "");
647 setorigin(this.vehicle_shieldent, real_origin(this) - this.origin);
648 this.vehicle_shieldent.scale = 256 / vlen(this.maxs - this.mins);
649 setthink(this.vehicle_shieldent, shieldhit_think);
652 this.vehicle_shieldent.colormod = '1 1 1';
653 this.vehicle_shieldent.alpha = 0.45;
654 this.vehicle_shieldent.angles = vectoangles(normalize(hitloc - (this.origin + this.vehicle_shieldent.origin))) - this.angles;
655 this.vehicle_shieldent.nextthink = time;
656 this.vehicle_shieldent.effects &= ~EF_NODRAW;
658 this.vehicle_shield -= damage;
660 if(this.vehicle_shield < 0)
662 this.vehicle_health -= fabs(this.vehicle_shield);
663 this.vehicle_shieldent.colormod = '2 0 0';
664 this.vehicle_shield = 0;
665 this.vehicle_shieldent.alpha = 0.75;
667 if(sound_allowed(MSG_BROADCAST, attacker))
668 spamsound (this, CH_PAIN, "onslaught/ons_hit2.wav", VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
671 if(sound_allowed(MSG_BROADCAST, attacker))
672 spamsound (this, CH_PAIN, "onslaught/electricity_explode.wav", VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
677 this.vehicle_health -= damage;
679 if(sound_allowed(MSG_BROADCAST, attacker))
680 spamsound (this, CH_PAIN, "onslaught/ons_hit2.wav", VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
683 if(this.damageforcescale < 1 && this.damageforcescale > 0)
684 this.velocity += force * this.damageforcescale;
686 this.velocity += force;
688 if(this.vehicle_health <= 0)
691 if(this.vehicle_flags & VHF_DEATHEJECT)
692 vehicles_exit(this, VHEF_EJECT);
694 vehicles_exit(this, VHEF_RELEASE);
697 antilag_clear(this, this);
699 Vehicle info = Vehicles_from(this.vehicleid);
700 info.vr_death(info, this);
701 vehicles_setreturn(this);
705 bool vehicles_crushable(entity e)
707 if(IS_PLAYER(e) && time >= e.vehicle_enter_delay)
716 void vehicles_impact(entity this, float _minspeed, float _speedfac, float _maxpain)
718 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
721 if(this.play_time < time)
723 float wc = vlen(this.velocity - this.oldvelocity);
724 //dprint("oldvel: ", vtos(this.oldvelocity), "\n");
725 //dprint("vel: ", vtos(this.velocity), "\n");
728 float take = min(_speedfac * wc, _maxpain);
729 Damage (this, NULL, NULL, take, DEATH_FALL.m_id, this.origin, '0 0 0');
730 this.play_time = time + 0.25;
732 //dprint("wc: ", ftos(wc), "\n");
733 //dprint("take: ", ftos(take), "\n");
738 // vehicle enter/exit handling
739 vector vehicles_findgoodexit(entity this, vector prefer_spot)
741 tracebox(this.origin + '0 0 32', STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), prefer_spot, MOVE_NORMAL, this.owner);
742 if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
745 float mysize = 1.5 * vlen(this.maxs - this.mins);
747 vector v2 = 0.5 * (this.absmin + this.absmax);
748 for(int i = 0; i < autocvar_g_vehicles_exit_attempts; ++i)
752 v = v2 + normalize(v) * mysize;
753 tracebox(v2, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), v, MOVE_NORMAL, this.owner);
754 if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
761 void vehicles_exit(entity vehic, bool eject)
763 entity player = vehic.owner;
765 if(vehicles_exit_running)
767 LOG_TRACE("^1vehicles_exit already running! this is not good...");
771 vehicles_exit_running = true;
773 // TODO: this was in an IS_CLIENT check, make sure it isn't actually needed!
774 if(vehic.vehicle_flags & VHF_PLAYERSLOT)
776 vehic.vehicle_exit(vehic, eject);
777 vehicles_exit_running = false;
783 if (IS_REAL_CLIENT(player))
786 WriteByte (MSG_ONE, SVC_SETVIEWPORT);
787 WriteEntity( MSG_ONE, player);
789 WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
790 WriteAngle(MSG_ONE, 0);
791 WriteAngle(MSG_ONE, vehic.angles_y);
792 WriteAngle(MSG_ONE, 0);
795 setsize(player, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL));
797 player.takedamage = DAMAGE_AIM;
798 player.solid = SOLID_SLIDEBOX;
799 set_movetype(player, MOVETYPE_WALK);
800 player.effects &= ~EF_NODRAW;
801 player.teleportable = TELEPORT_NORMAL;
803 player.PlayerPhysplug = func_null;
804 player.vehicle = NULL;
805 player.view_ofs = STAT(PL_VIEW_OFS, NULL);
806 player.event_damage = PlayerDamage;
807 player.hud = HUD_NORMAL;
808 PS(player).m_switchweapon = vehic.m_switchweapon;
809 player.last_vehiclecheck = time + 3;
810 player.vehicle_enter_delay = time + 2;
812 CSQCVehicleSetup(player, HUD_NORMAL);
814 Kill_Notification(NOTIF_ONE, player, MSG_CENTER, CPID_VEHICLES);
815 Kill_Notification(NOTIF_ONE, player, MSG_CENTER, CPID_VEHICLES_OTHER); // kill all vehicle notifications when exiting a vehicle?
818 vehic.flags |= FL_NOTARGET;
821 vehic.avelocity = '0 0 0';
823 vehic.tur_head.nodrawtoclient = NULL;
828 WaypointSprite_Kill(vehic.wps_intruder);
830 MUTATOR_CALLHOOK(VehicleExit, player, vehic);
832 vehic.team = vehic.tur_head.team;
834 sound (vehic, CH_TRIGGER_SINGLE, SND_Null, 1, ATTEN_NORM);
835 vehic.vehicle_hudmodel.viewmodelforclient = vehic;
836 vehic.phase = time + 1;
838 vehic.vehicle_exit(vehic, eject);
840 vehicles_setreturn(vehic);
841 vehicles_reset_colors(vehic);
844 CSQCMODEL_AUTOINIT(vehic);
846 vehicles_exit_running = false;
849 void vehicles_touch(entity this, entity toucher)
851 if(MUTATOR_CALLHOOK(VehicleTouch, this, toucher))
854 // Vehicle currently in use
857 if(!forbidWeaponUse(this.owner))
859 if((this.origin_z + this.maxs_z) > (toucher.origin_z))
860 if(vehicles_crushable(toucher))
862 if(vdist(this.velocity, >=, 30))
863 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);
865 return; // Dont do selfdamage when hitting "soft targets".
868 if(this.play_time < time) {
869 Vehicle info = Vehicles_from(this.vehicleid);
870 info.vr_impact(info, this);
876 if(autocvar_g_vehicles_enter)
879 vehicles_enter(toucher, this);
882 bool vehicle_impulse(entity this, int imp)
884 entity v = this.vehicle;
885 if (!v) return false;
886 if (IS_DEAD(v)) return false;
887 bool(entity,int) f = v.vehicles_impulse;
888 if (f && f(this,imp)) return true;
891 case IMP_weapon_drop.impulse:
893 stuffcmd(this, "\ntoggle cl_eventchase_vehicle\nset _vehicles_shownchasemessage 1\n");
900 void vehicles_enter(entity pl, entity veh)
902 // Remove this when bots know how to use vehicles
903 if((IS_BOT_CLIENT(pl) && !autocvar_g_vehicles_allow_bots))
907 || (veh.phase >= time)
908 || (pl.vehicle_enter_delay >= time)
909 || (STAT(FROZEN, pl))
914 Vehicle info = Vehicles_from(veh.vehicleid);
916 if(autocvar_g_vehicles_enter) // vehicle's touch function should handle this if entering via use key is disabled (TODO)
917 if(veh.vehicle_flags & VHF_MULTISLOT)
918 if(veh.owner && SAME_TEAM(pl, veh))
920 // we don't need a return value or anything here
921 // if successful the owner check below will prevent anything weird
922 info.vr_gunner_enter(info, veh, pl);
926 return; // got here and didn't enter the gunner, return
930 if(DIFF_TEAM(pl, veh))
931 if(autocvar_g_vehicles_steal)
933 FOREACH_CLIENT(IS_PLAYER(it) && SAME_TEAM(it, veh), Send_Notification(NOTIF_ONE, it, MSG_CENTER, CENTER_VEHICLE_STEAL));
935 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_VEHICLE_STEAL_SELF);
937 if (autocvar_g_vehicles_steal_show_waypoint) {
938 entity wp = WaypointSprite_Spawn(WP_VehicleIntruder, 0, 0, pl, '0 0 68', NULL, veh.team, veh, wps_intruder, true, RADARICON_DANGER);
939 wp.colormod = Team_ColorRGB(pl.team);
944 RemoveGrapplingHook(pl);
946 veh.vehicle_ammo1 = 0;
947 veh.vehicle_ammo2 = 0;
948 veh.vehicle_reload1 = 0;
949 veh.vehicle_reload2 = 0;
950 veh.vehicle_energy = 0;
955 // .viewmodelforclient works better.
956 //veh.vehicle_hudmodel.drawonlytoclient = veh.owner;
958 veh.vehicle_hudmodel.viewmodelforclient = pl;
961 pl.view_ofs = STAT(PL_VIEW_OFS, NULL);
962 setsize (pl, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL));
964 veh.event_damage = vehicles_damage;
966 pl.angles = veh.angles;
967 pl.takedamage = DAMAGE_NO;
968 pl.solid = SOLID_NOT;
969 pl.disableclientprediction = 1; // physics is no longer run, so this won't be reset
970 set_movetype(pl, MOVETYPE_NOCLIP);
971 pl.teleportable = false;
973 pl.event_damage = func_null;
974 pl.view_ofs = '0 0 0';
975 veh.colormap = pl.colormap;
977 veh.tur_head.colormap = pl.colormap;
978 veh.m_switchweapon = PS(pl).m_switchweapon;
979 pl.hud = veh.vehicleid;
980 pl.PlayerPhysplug = veh.PlayerPhysplug;
982 pl.vehicle_ammo1 = veh.vehicle_ammo1;
983 pl.vehicle_ammo2 = veh.vehicle_ammo2;
984 pl.vehicle_reload1 = veh.vehicle_reload1;
985 pl.vehicle_reload2 = veh.vehicle_reload2;
986 pl.vehicle_energy = veh.vehicle_energy;
988 // Cant do this, hides attached objects too.
989 //veh.exteriormodeltoclient = veh.owner;
990 //veh.tur_head.exteriormodeltoclient = veh.owner;
996 veh.flags -= FL_NOTARGET;
998 if (IS_REAL_CLIENT(pl))
1000 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_VEHICLE_ENTER);
1003 WriteByte (MSG_ONE, SVC_SETVIEWPORT);
1004 WriteEntity(MSG_ONE, veh.vehicle_viewport);
1006 WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
1009 WriteAngle(MSG_ONE, veh.tur_head.angles_x + veh.angles_x); // tilt
1010 WriteAngle(MSG_ONE, veh.tur_head.angles_y + veh.angles_y); // yaw
1011 WriteAngle(MSG_ONE, 0); // roll
1015 WriteAngle(MSG_ONE, veh.angles_x * -1); // tilt
1016 WriteAngle(MSG_ONE, veh.angles_y); // yaw
1017 WriteAngle(MSG_ONE, 0); // roll
1021 vehicles_clearreturn(veh);
1023 CSQCVehicleSetup(pl, veh.vehicleid);
1025 MUTATOR_CALLHOOK(VehicleEnter, pl, veh);
1027 CSQCModel_UnlinkEntity(veh);
1028 info.vr_enter(info, veh);
1030 antilag_clear(pl, CS(pl));
1033 void vehicles_think(entity this)
1035 this.nextthink = time + autocvar_g_vehicles_thinkrate;
1038 this.owner.vehicle_weapon2mode = this.vehicle_weapon2mode;
1040 Vehicle info = Vehicles_from(this.vehicleid);
1041 info.vr_think(info, this);
1043 vehicles_painframe(this);
1045 CSQCMODEL_AUTOUPDATE(this);
1048 void vehicles_reset(entity this)
1051 vehicles_exit(this, VHEF_RELEASE);
1053 vehicles_clearreturn(this);
1055 if(this.active != ACTIVE_NOT)
1056 vehicles_spawn(this);
1060 void vehicles_spawn(entity this)
1062 LOG_DEBUG("Spawning vehicle: ", this.classname);
1065 this.vehicle_hudmodel.viewmodelforclient = this;
1068 settouch(this, vehicles_touch);
1069 this.event_damage = vehicles_damage;
1070 this.reset = vehicles_reset;
1071 this.iscreature = true;
1072 this.teleportable = false; // no teleporting for vehicles, too buggy
1073 this.damagedbycontents = true;
1074 set_movetype(this, MOVETYPE_WALK);
1075 this.solid = SOLID_SLIDEBOX;
1076 this.takedamage = DAMAGE_AIM;
1077 this.deadflag = DEAD_NO;
1078 if(!this.bot_attack)
1079 IL_PUSH(g_bot_targets, this);
1080 this.bot_attack = true;
1081 this.flags = FL_NOTARGET;
1082 this.avelocity = '0 0 0';
1083 this.velocity = '0 0 0';
1084 setthink(this, vehicles_think);
1085 this.nextthink = time;
1088 this.lock_strength = 0;
1089 this.lock_target = NULL;
1090 this.misc_bulletcounter = 0;
1093 this.angles = this.pos2;
1094 setorigin(this, this.pos1);
1096 Send_Effect(EFFECT_TELEPORT, this.origin + '0 0 64', '0 0 0', 1);
1098 if(this.vehicle_controller)
1099 this.team = this.vehicle_controller.team;
1101 FOREACH_CLIENT(IS_PLAYER(it) && it.hook.aiment == this, RemoveGrapplingHook(it));
1103 vehicles_reset_colors(this);
1105 Vehicle info = Vehicles_from(this.vehicleid);
1106 info.vr_spawn(info, this);
1108 CSQCMODEL_AUTOINIT(this);
1111 bool vehicle_initialize(entity this, Vehicle info, bool nodrop)
1113 if(!autocvar_g_vehicles)
1121 info.vr_precache(info);
1122 IL_PUSH(g_vehicles, this);
1125 if(this.targetname && this.targetname != "")
1127 this.vehicle_controller = find(NULL, target, this.targetname);
1128 if(!this.vehicle_controller)
1130 LOG_DEBUG("^1WARNING: ^7Vehicle with invalid .targetname");
1131 this.active = ACTIVE_ACTIVE;
1135 this.team = this.vehicle_controller.team;
1136 this.use = vehicle_use;
1140 if(this.vehicle_controller.team == 0)
1141 this.active = ACTIVE_NOT;
1143 this.active = ACTIVE_ACTIVE;
1147 else { this.active = ACTIVE_ACTIVE; }
1149 if(this.team && (!teamplay || !autocvar_g_vehicles_teams))
1152 if(this.mdl == "" || !this.mdl)
1153 _setmodel(this, info.model);
1155 _setmodel(this, this.mdl);
1157 this.vehicle_flags |= VHF_ISVEHICLE;
1159 this.vehicle_viewport = new(vehicle_viewport);
1160 this.vehicle_hudmodel = new(vehicle_hudmodel);
1161 this.tur_head = new(tur_head);
1162 this.tur_head.owner = this;
1163 this.takedamage = DAMAGE_NO;
1164 this.bot_attack = true;
1165 IL_PUSH(g_bot_targets, this);
1166 this.iscreature = true;
1167 this.teleportable = false; // no teleporting for vehicles, too buggy
1168 this.damagedbycontents = true;
1169 this.vehicleid = info.vehicleid;
1170 this.PlayerPhysplug = info.PlayerPhysplug;
1171 this.event_damage = func_null;
1172 settouch(this, vehicles_touch);
1173 setthink(this, vehicles_spawn);
1174 this.nextthink = time;
1175 this.effects = EF_NODRAW;
1176 this.dphitcontentsmask = DPCONTENTS_BODY | DPCONTENTS_SOLID;
1178 if(autocvar_g_playerclip_collisions)
1179 this.dphitcontentsmask |= DPCONTENTS_PLAYERCLIP;
1181 if(autocvar_g_nodepthtestplayers)
1182 this.effects |= EF_NODEPTHTEST;
1184 if(autocvar_g_fullbrightplayers)
1185 this.effects |= EF_FULLBRIGHT;
1187 _setmodel(this.vehicle_hudmodel, info.hud_model);
1188 setmodel(this.vehicle_viewport, MDL_Null);
1190 if(info.head_model != "")
1192 _setmodel(this.tur_head, info.head_model);
1193 setattachment(this.tur_head, this, info.tag_head);
1194 setattachment(this.vehicle_hudmodel, this.tur_head, info.tag_hud);
1195 setattachment(this.vehicle_viewport, this.vehicle_hudmodel, info.tag_view);
1199 setattachment(this.tur_head, this, "");
1200 setattachment(this.vehicle_hudmodel, this, info.tag_hud);
1201 setattachment(this.vehicle_viewport, this.vehicle_hudmodel, info.tag_view);
1204 setsize(this, info.mins, info.maxs);
1206 info.vr_setup(info, this);
1210 setorigin(this, this.origin);
1211 tracebox(this.origin + '0 0 100', info.mins, info.maxs, this.origin - '0 0 10000', MOVE_WORLDONLY, this);
1212 setorigin(this, trace_endpos);
1215 this.pos1 = this.origin;
1216 this.pos2 = this.angles;
1217 this.tur_head.team = this.team;
1219 if(this.active == ACTIVE_NOT)
1220 this.nextthink = 0; // wait until activated
1221 else if(autocvar_g_vehicles_delayspawn)
1222 this.nextthink = time + this.respawntime + (random() * autocvar_g_vehicles_delayspawn_jitter);
1224 this.nextthink = time + game_starttime;
1226 if(MUTATOR_CALLHOOK(VehicleSpawn, this))