1 #include "sv_vehicles.qh"
4 bool vehicle_send(entity to, int sf)
6 WriteByte(MSG_ENTITY, ENT_CLIENT_VEHICLE);
7 WriteByte(MSG_ENTITY, sf);
11 WriteByte(MSG_ENTITY, this.vehicleid);
21 // player handles the .vehicle stuff, we need only set ourselves up for driving
40 bool SendAuxiliaryXhair(entity this, entity to, int sf)
43 WriteHeader(MSG_ENTITY, ENT_CLIENT_AUXILIARYXHAIR);
45 WriteByte(MSG_ENTITY, this.cnt);
47 WriteCoord(MSG_ENTITY, this.origin_x);
48 WriteCoord(MSG_ENTITY, this.origin_y);
49 WriteCoord(MSG_ENTITY, this.origin_z);
51 WriteByte(MSG_ENTITY, rint(this.colormod_x * 255));
52 WriteByte(MSG_ENTITY, rint(this.colormod_y * 255));
53 WriteByte(MSG_ENTITY, rint(this.colormod_z * 255));
58 void UpdateAuxiliaryXhair(entity own, vector loc, vector clr, int axh_id)
60 if(!IS_REAL_CLIENT(own))
65 axh_id = bound(0, axh_id, MAX_AXH);
66 axh = own.(AuxiliaryXhair[axh_id]);
68 if(axh == NULL || wasfreed(axh)) // MADNESS? THIS IS QQQQCCCCCCCCC (wasfreed, why do you exsist?)
72 axh.drawonlytoclient = own;
74 Net_LinkEntity(axh, false, 0, SendAuxiliaryXhair);
80 own.(AuxiliaryXhair[axh_id]) = axh;
83 void CSQCVehicleSetup(entity own, int vehicle_id)
85 if(!IS_REAL_CLIENT(own))
90 WriteHeader(MSG_ONE, TE_CSQC_VEHICLESETUP);
91 WriteByte(MSG_ONE, vehicle_id);
94 void vehicles_locktarget(entity this, float incr, float decr, float _lock_time)
96 if(this.lock_target && IS_DEAD(this.lock_target))
98 this.lock_target = NULL;
99 this.lock_strength = 0;
103 if(this.lock_time > time)
106 if(this.lock_soundtime < time)
108 this.lock_soundtime = time + 0.5;
109 play2(this.owner, "vehicles/locked.wav");
115 if(trace_ent != NULL)
117 if(SAME_TEAM(trace_ent, this))
120 if(IS_DEAD(trace_ent))
123 if(!(IS_VEHICLE(trace_ent) || IS_TURRET(trace_ent)))
126 if(trace_ent.alpha <= 0.5 && trace_ent.alpha != 0)
127 trace_ent = NULL; // invisible
130 if(this.lock_target == NULL && trace_ent != NULL)
131 this.lock_target = trace_ent;
133 if(this.lock_target && trace_ent == this.lock_target)
135 if(this.lock_strength != 1 && this.lock_strength + incr >= 1)
137 play2(this.owner, "vehicles/lock.wav");
138 this.lock_soundtime = time + 0.8;
140 else if (this.lock_strength != 1 && this.lock_soundtime < time)
142 play2(this.owner, "vehicles/locking.wav");
143 this.lock_soundtime = time + 0.3;
147 // Have a locking target
148 // Trace hit current target
149 if(trace_ent == this.lock_target && trace_ent != NULL)
151 this.lock_strength = min(this.lock_strength + incr, 1);
152 if(this.lock_strength == 1)
153 this.lock_time = time + _lock_time;
158 this.lock_strength = max(this.lock_strength - decr * 2, 0);
160 this.lock_strength = max(this.lock_strength - decr, 0);
162 if(this.lock_strength == 0)
163 this.lock_target = NULL;
167 vector vehicles_force_fromtag_hover(entity this, string tag_name, float spring_length, float max_power)
169 force_fromtag_origin = gettaginfo(this, gettagindex(this, tag_name));
170 v_forward = normalize(v_forward) * -1;
171 traceline(force_fromtag_origin, force_fromtag_origin - (v_forward * spring_length), MOVE_NORMAL, this);
173 force_fromtag_power = (1 - trace_fraction) * max_power;
174 force_fromtag_normpower = force_fromtag_power / max_power;
176 return v_forward * force_fromtag_power;
179 vector vehicles_force_fromtag_maglev(entity this, string tag_name, float spring_length, float max_power)
181 force_fromtag_origin = gettaginfo(this, gettagindex(this, tag_name));
182 v_forward = normalize(v_forward) * -1;
183 traceline(force_fromtag_origin, force_fromtag_origin - (v_forward * spring_length), MOVE_NORMAL, this);
185 // TODO - this may NOT be compatible with wall/celing movement, unhardcode 0.25 (engine count multiplier)
186 if(trace_fraction == 1.0)
188 force_fromtag_normpower = -0.25;
192 force_fromtag_power = ((1 - trace_fraction) - trace_fraction) * max_power;
193 force_fromtag_normpower = force_fromtag_power / max_power;
195 return v_forward * force_fromtag_power;
198 // projectile handling
199 void vehicles_projectile_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
201 // Ignore damage from oterh projectiles from my owner (dont mess up volly's)
202 if(inflictor.owner == this.owner)
205 this.health -= damage;
206 this.velocity += force;
209 this.takedamage = DAMAGE_NO;
210 this.event_damage = func_null;
211 setthink(this, adaptor_think2use);
212 this.nextthink = time;
216 void vehicles_projectile_explode(entity this)
218 if(this.owner && other != NULL)
220 if(other == this.owner.vehicle)
223 if(other == this.owner.vehicle.tur_head)
227 PROJECTILE_TOUCH(this);
229 this.event_damage = func_null;
230 RadiusDamage (this, this.realowner, this.shot_dmg, 0, this.shot_radius, this, NULL, this.shot_force, this.totalfrags, other);
235 void vehicles_projectile_explode_use(entity this, entity actor, entity trigger)
237 vehicles_projectile_explode(this);
240 entity vehicles_projectile(entity this, string _mzlfx, Sound _mzlsound,
241 vector _org, vector _vel,
242 float _dmg, float _radi, float _force, float _size,
243 int _deahtype, float _projtype, float _health,
244 bool _cull, bool _clianim, entity _owner)
246 TC(Sound, _mzlsound);
251 PROJECTILE_MAKETRIGGER(proj);
252 setorigin(proj, _org);
254 proj.shot_dmg = _dmg;
255 proj.shot_radius = _radi;
256 proj.shot_force = _force;
257 proj.totalfrags = _deahtype;
258 proj.solid = SOLID_BBOX;
259 proj.movetype = MOVETYPE_FLYMISSILE;
260 proj.flags = FL_PROJECTILE;
261 proj.bot_dodge = true;
262 proj.bot_dodgerating = _dmg;
263 proj.velocity = _vel;
264 settouch(proj, vehicles_projectile_explode);
265 proj.use = vehicles_projectile_explode_use;
267 proj.realowner = _owner;
268 setthink(proj, SUB_Remove);
269 proj.nextthink = time + 30;
273 proj.takedamage = DAMAGE_AIM;
274 proj.event_damage = vehicles_projectile_damage;
275 proj.health = _health;
278 proj.flags = FL_PROJECTILE | FL_NOTARGET;
280 if(_mzlsound != SND_Null)
281 sound (this, CH_WEAPON_A, _mzlsound, VOL_BASE, ATTEN_NORM);
284 Send_Effect_(_mzlfx, proj.origin, proj.velocity, 1);
286 setsize (proj, '-1 -1 -1' * _size, '1 1 1' * _size);
288 CSQCProjectile(proj, _clianim, _projtype, _cull);
293 void vehicles_gib_explode(entity this)
295 sound (this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
296 Send_Effect(EFFECT_EXPLOSION_SMALL, randomvec() * 80 + (this.origin + '0 0 100'), '0 0 0', 1);
297 Send_Effect(EFFECT_EXPLOSION_SMALL, this.wp00.origin + '0 0 64', '0 0 0', 1);
301 void vehicles_gib_think(entity this)
307 this.nextthink = time + 0.1;
310 entity vehicle_tossgib(entity this, entity _template, vector _vel, string _tag, bool _burn, bool _explode, float _maxtime, vector _rot)
312 entity _gib = spawn();
313 _setmodel(_gib, _template.model);
314 vector org = gettaginfo(this, gettagindex(this, _tag));
315 setorigin(_gib, org);
316 _gib.velocity = _vel;
317 _gib.movetype = MOVETYPE_TOSS;
318 _gib.solid = SOLID_CORPSE;
319 _gib.colormod = '-0.5 -0.5 -0.5';
320 _gib.effects = EF_LOWPRECISION;
321 _gib.avelocity = _rot;
324 _gib.effects |= EF_FLAME;
328 setthink(_gib, vehicles_gib_explode);
329 _gib.nextthink = time + random() * _explode;
330 settouch(_gib, vehicles_gib_explode);
334 _gib.cnt = time + _maxtime;
335 setthink(_gib, vehicles_gib_think);
336 _gib.nextthink = time + _maxtime - 1;
342 bool vehicle_addplayerslot( entity _owner,
346 bool(entity) _framefunc,
347 void(entity,bool) _exitfunc, float(entity, entity) _enterfunc)
349 if(!(_owner.vehicle_flags & VHF_MULTISLOT))
350 _owner.vehicle_flags |= VHF_MULTISLOT;
352 _slot.PlayerPhysplug = _framefunc;
353 _slot.vehicle_exit = _exitfunc;
354 _slot.vehicle_enter = _enterfunc;
356 _slot.vehicle_flags = VHF_PLAYERSLOT;
357 _slot.vehicle_viewport = spawn();
358 _slot.vehicle_hudmodel = spawn();
359 _slot.vehicle_hudmodel.viewmodelforclient = _slot;
360 _slot.vehicle_viewport.effects = (EF_ADDITIVE | EF_DOUBLESIDED | EF_FULLBRIGHT | EF_NODEPTHTEST | EF_NOGUNBOB | EF_NOSHADOW | EF_LOWPRECISION | EF_SELECTABLE | EF_TELEPORT_BIT);
362 setmodel(_slot.vehicle_hudmodel, _hud_model);
363 setmodel(_slot.vehicle_viewport, MDL_Null);
365 setattachment(_slot.vehicle_hudmodel, _slot, "");
366 setattachment(_slot.vehicle_viewport, _slot.vehicle_hudmodel, "");
371 vector vehicle_aimturret(entity _vehic, vector _target, entity _turrret, string _tagname,
372 float _pichlimit_min, float _pichlimit_max,
373 float _rotlimit_min, float _rotlimit_max, float _aimspeed)
377 vtag = gettaginfo(_turrret, gettagindex(_turrret, _tagname));
378 vtmp = vectoangles(normalize(_target - vtag));
379 vtmp = AnglesTransform_ToAngles(AnglesTransform_LeftDivide(AnglesTransform_FromAngles(_vehic.angles), AnglesTransform_FromAngles(vtmp))) - _turrret.angles;
380 vtmp = AnglesTransform_Normalize(vtmp, true);
381 ftmp = _aimspeed * frametime;
382 vtmp_y = bound(-ftmp, vtmp_y, ftmp);
383 vtmp_x = bound(-ftmp, vtmp_x, ftmp);
384 _turrret.angles_y = bound(_rotlimit_min, _turrret.angles_y + vtmp_y, _rotlimit_max);
385 _turrret.angles_x = bound(_pichlimit_min, _turrret.angles_x + vtmp_x, _pichlimit_max);
389 void vehicles_reset_colors(entity this)
392 const vector cmod = '0 0 0';
393 if(this.team && teamplay)
394 cmap = 1024 + (this.team - 1) * 17;
397 if(autocvar_g_nodepthtestplayers)
398 eff |= EF_NODEPTHTEST;
399 if(autocvar_g_fullbrightplayers)
400 eff |= EF_FULLBRIGHT;
402 // Find all ents attacked to main model and setup effects, colormod etc.
403 FOREACH_ENTITY_ENT(tag_entity, this,
405 if(it != this.vehicle_shieldent)
414 // Also check head tags
415 FOREACH_ENTITY_ENT(tag_entity, this.tur_head,
417 if(it != this.vehicle_shieldent)
426 this.vehicle_hudmodel.effects = this.effects = eff; // | EF_LOWPRECISION;
427 this.vehicle_hudmodel.colormod = this.colormod = cmod;
428 this.vehicle_hudmodel.colormap = this.colormap = cmap;
429 this.vehicle_viewport.effects = (EF_ADDITIVE | EF_DOUBLESIDED | EF_FULLBRIGHT | EF_NODEPTHTEST | EF_NOGUNBOB | EF_NOSHADOW | EF_LOWPRECISION | EF_SELECTABLE | EF_TELEPORT_BIT);
432 this.avelocity = '0 0 0';
433 this.velocity = '0 0 0';
437 void vehicles_clearreturn(entity veh)
439 // Remove "return helper", if any.
440 for (entity ret = findchain(classname, "vehicle_return"); ret; ret = ret.chain)
445 setthink(ret, SUB_Remove);
446 ret.nextthink = time + 0.1;
448 if(ret.waypointsprite_attached)
449 WaypointSprite_Kill(ret.waypointsprite_attached);
456 void vehicles_spawn(entity this);
457 void vehicles_return(entity this)
459 Send_Effect(EFFECT_TELEPORT, this.wp00.origin + '0 0 64', '0 0 0', 1);
461 setthink(this.wp00, vehicles_spawn);
462 this.wp00.nextthink = time;
464 if(this.waypointsprite_attached)
465 WaypointSprite_Kill(this.waypointsprite_attached);
470 void vehicles_showwp_goaway(entity this)
472 if(this.waypointsprite_attached)
473 WaypointSprite_Kill(this.waypointsprite_attached);
479 void vehicles_showwp(entity this)
487 setthink(ent, vehicles_return);
488 ent.nextthink = ent.cnt;
492 setthink(ent, vehicles_return);
493 ent.nextthink = time + 1;
496 setmodel(ent, MDL_Null);
497 ent.team = this.wp00.team;
498 ent.wp00 = this.wp00;
499 setorigin(ent, this.wp00.pos1);
501 ent.nextthink = time + 5;
502 setthink(ent, vehicles_showwp_goaway);
505 if(teamplay && ent.team)
506 rgb = Team_ColorRGB(ent.team);
509 entity wp = WaypointSprite_Spawn(WP_Vehicle, 0, 0, ent, '0 0 64', NULL, 0, ent, waypointsprite_attached, true, RADARICON_Vehicle);
511 if(ent.waypointsprite_attached)
513 WaypointSprite_UpdateRule(ent.waypointsprite_attached, ent.wp00.team, SPRITERULE_DEFAULT);
515 WaypointSprite_UpdateBuildFinished(ent.waypointsprite_attached, ent.nextthink);
516 WaypointSprite_Ping(ent.waypointsprite_attached);
520 void vehicles_setreturn(entity veh)
524 vehicles_clearreturn(veh);
526 ret = new(vehicle_return);
529 setthink(ret, vehicles_showwp);
533 ret.cnt = time + veh.respawntime;
534 ret.nextthink = min(time + veh.respawntime, time + veh.respawntime - 5);
538 ret.nextthink = min(time + veh.respawntime, time + veh.respawntime - 1);
541 setmodel(ret, MDL_Null);
542 setorigin(ret, veh.pos1 + '0 0 96');
546 void vehicle_use(entity this, entity actor, entity trigger)
548 LOG_TRACE("vehicle ",this.netname, " used by ", actor.classname, "\n");
550 this.tur_head.team = actor.team;
552 if(this.tur_head.team == 0)
553 this.active = ACTIVE_NOT;
555 this.active = ACTIVE_ACTIVE;
557 if(this.active == ACTIVE_ACTIVE && !IS_DEAD(this) && !gameover)
559 LOG_TRACE("Respawning vehicle: ", this.netname, "\n");
560 if(this.effects & EF_NODRAW)
562 setthink(this, vehicles_spawn);
563 this.nextthink = time + 3;
567 vehicles_setreturn(this);
568 vehicles_reset_colors(this);
573 void vehicles_regen(entity this, float timer, .float regen_field, float field_max, float rpause, float regen, float delta_time, float _healthscale)
575 if(this.(regen_field) < field_max)
576 if(timer + rpause < time)
579 regen = regen * (this.vehicle_health / this.max_health);
581 this.(regen_field) = min(this.(regen_field) + regen * delta_time, field_max);
584 this.owner.(regen_field) = (this.(regen_field) / field_max) * 100;
588 void shieldhit_think(entity this)
593 // setmodel(this, MDL_Null);
595 this.effects |= EF_NODRAW;
599 this.nextthink = time + 0.1;
603 void vehicles_painframe(entity this)
605 int myhealth = ((this.owner) ? this.owner.vehicle_health : ((this.vehicle_health / this.max_health) * 100));
608 if(this.pain_frame < time)
610 float _ftmp = myhealth / 50;
611 this.pain_frame = time + max(0.1, 0.1 + (random() * 0.5 * _ftmp));
612 Send_Effect(EFFECT_SMOKE_SMALL, (this.origin + (randomvec() * 80)), '0 0 0', 1);
614 if(this.vehicle_flags & VHF_DMGSHAKE)
615 this.velocity += randomvec() * 30;
617 if(this.vehicle_flags & VHF_DMGROLL)
618 if(this.vehicle_flags & VHF_DMGHEADROLL)
619 this.tur_head.angles += randomvec();
621 this.angles += randomvec();
625 void vehicles_frame(entity this, entity actor)
627 vehicles_painframe(this);
630 void vehicles_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
632 this.dmg_time = time;
635 if(DEATH_ISWEAPON(deathtype, WEP_VORTEX))
636 damage *= autocvar_g_vehicles_vortex_damagerate;
638 if(DEATH_ISWEAPON(deathtype, WEP_MACHINEGUN))
639 damage *= autocvar_g_vehicles_machinegun_damagerate;
641 if(DEATH_ISWEAPON(deathtype, WEP_RIFLE))
642 damage *= autocvar_g_vehicles_rifle_damagerate;
644 if(DEATH_ISWEAPON(deathtype, WEP_VAPORIZER))
645 damage *= autocvar_g_vehicles_vaporizer_damagerate;
647 if(DEATH_ISWEAPON(deathtype, WEP_SEEKER))
648 damage *= autocvar_g_vehicles_tag_damagerate;
650 if(DEATH_WEAPONOF(deathtype) != WEP_Null)
651 damage *= autocvar_g_vehicles_weapon_damagerate;
653 this.enemy = attacker;
655 this.pain_finished = time;
657 if((this.vehicle_flags & VHF_HASSHIELD) && (this.vehicle_shield > 0))
659 if (wasfreed(this.vehicle_shieldent) || this.vehicle_shieldent == NULL)
661 this.vehicle_shieldent = spawn();
662 this.vehicle_shieldent.effects = EF_LOWPRECISION;
664 setmodel(this.vehicle_shieldent, MDL_VEH_SHIELD);
665 setattachment(this.vehicle_shieldent, this, "");
666 setorigin(this.vehicle_shieldent, real_origin(this) - this.origin);
667 this.vehicle_shieldent.scale = 256 / vlen(this.maxs - this.mins);
668 setthink(this.vehicle_shieldent, shieldhit_think);
671 this.vehicle_shieldent.colormod = '1 1 1';
672 this.vehicle_shieldent.alpha = 0.45;
673 this.vehicle_shieldent.angles = vectoangles(normalize(hitloc - (this.origin + this.vehicle_shieldent.origin))) - this.angles;
674 this.vehicle_shieldent.nextthink = time;
675 this.vehicle_shieldent.effects &= ~EF_NODRAW;
677 this.vehicle_shield -= damage;
679 if(this.vehicle_shield < 0)
681 this.vehicle_health -= fabs(this.vehicle_shield);
682 this.vehicle_shieldent.colormod = '2 0 0';
683 this.vehicle_shield = 0;
684 this.vehicle_shieldent.alpha = 0.75;
686 if(sound_allowed(MSG_BROADCAST, attacker))
687 spamsound (this, CH_PAIN, "onslaught/ons_hit2.wav", VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
690 if(sound_allowed(MSG_BROADCAST, attacker))
691 spamsound (this, CH_PAIN, "onslaught/electricity_explode.wav", VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
696 this.vehicle_health -= damage;
698 if(sound_allowed(MSG_BROADCAST, attacker))
699 spamsound (this, CH_PAIN, "onslaught/ons_hit2.wav", VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
702 if(this.damageforcescale < 1 && this.damageforcescale > 0)
703 this.velocity += force * this.damageforcescale;
705 this.velocity += force;
707 if(this.vehicle_health <= 0)
710 if(this.vehicle_flags & VHF_DEATHEJECT)
711 vehicles_exit(this, VHEF_EJECT);
713 vehicles_exit(this, VHEF_RELEASE);
716 antilag_clear(this, this);
718 Vehicle info = Vehicles_from(this.vehicleid);
719 info.vr_death(info, this);
720 vehicles_setreturn(this);
724 float vehicles_crushable(entity e)
726 if(IS_PLAYER(e) && time >= e.vehicle_enter_delay)
735 void vehicles_impact(entity this, float _minspeed, float _speedfac, float _maxpain)
737 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
740 if(this.play_time < time)
742 float wc = vlen(this.velocity - this.oldvelocity);
743 //dprint("oldvel: ", vtos(this.oldvelocity), "\n");
744 //dprint("vel: ", vtos(this.velocity), "\n");
747 float take = min(_speedfac * wc, _maxpain);
748 Damage (this, NULL, NULL, take, DEATH_FALL.m_id, this.origin, '0 0 0');
749 this.play_time = time + 0.25;
751 //dprint("wc: ", ftos(wc), "\n");
752 //dprint("take: ", ftos(take), "\n");
757 // vehicle enter/exit handling
758 vector vehicles_findgoodexit(entity this, vector prefer_spot)
763 tracebox(this.origin + '0 0 32', STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), prefer_spot, MOVE_NORMAL, this.owner);
764 if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
767 mysize = 1.5 * vlen(this.maxs - this.mins);
770 v2 = 0.5 * (this.absmin + this.absmax);
771 for(i = 0; i < 100; ++i)
775 v = v2 + normalize(v) * mysize;
776 tracebox(v2, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), v, MOVE_NORMAL, this.owner);
777 if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
782 exitspot = (this.origin + '0 0 48') + v_forward * mysize;
783 tracebox(this.origin + '0 0 32', PL_MIN, PL_MAX, exitspot, MOVE_NORMAL, this.owner);
784 if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
787 exitspot = (this.origin + '0 0 48') - v_forward * mysize;
788 tracebox(this.origin + '0 0 32', PL_MIN, PL_MAX, exitspot, MOVE_NORMAL, this.owner);
789 if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
792 exitspot = (this.origin + '0 0 48') + v_right * mysize;
793 tracebox(this.origin + '0 0 32', PL_MIN, PL_MAX, exitspot, MOVE_NORMAL, this.owner);
794 if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
797 exitspot = (this.origin + '0 0 48') - v_right * mysize;
798 tracebox(this.origin + '0 0 32', PL_MIN, PL_MAX, exitspot, MOVE_NORMAL, this.owner);
799 if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
806 void vehicles_exit(entity vehic, bool eject)
808 entity player = vehic.owner;
810 if(vehicles_exit_running)
812 LOG_TRACE("^1vehicles_exit already running! this is not good...\n");
816 vehicles_exit_running = true;
818 // TODO: this was in an IS_CLIENT check, make sure it isn't actually needed!
819 if(vehic.vehicle_flags & VHF_PLAYERSLOT)
821 vehic.vehicle_exit(vehic, eject);
822 vehicles_exit_running = false;
828 if (IS_REAL_CLIENT(player))
831 WriteByte (MSG_ONE, SVC_SETVIEWPORT);
832 WriteEntity( MSG_ONE, player);
834 WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
835 WriteAngle(MSG_ONE, 0);
836 WriteAngle(MSG_ONE, vehic.angles_y);
837 WriteAngle(MSG_ONE, 0);
840 setsize(player, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL));
842 player.takedamage = DAMAGE_AIM;
843 player.solid = SOLID_SLIDEBOX;
844 player.movetype = MOVETYPE_WALK;
845 player.effects &= ~EF_NODRAW;
846 player.teleportable = TELEPORT_NORMAL;
848 player.PlayerPhysplug = func_null;
849 player.vehicle = NULL;
850 player.view_ofs = STAT(PL_VIEW_OFS, NULL);
851 player.event_damage = PlayerDamage;
852 player.hud = HUD_NORMAL;
853 PS(player).m_switchweapon = vehic.m_switchweapon;
854 player.last_vehiclecheck = time + 3;
855 player.vehicle_enter_delay = time + 2;
857 CSQCVehicleSetup(player, HUD_NORMAL);
859 Kill_Notification(NOTIF_ONE, player, MSG_CENTER, CPID_VEHICLES);
860 Kill_Notification(NOTIF_ONE, player, MSG_CENTER, CPID_VEHICLES_OTHER); // kill all vehicle notifications when exiting a vehicle?
863 vehic.flags |= FL_NOTARGET;
866 vehic.avelocity = '0 0 0';
868 vehic.tur_head.nodrawtoclient = NULL;
873 WaypointSprite_Kill(vehic.wps_intruder);
875 MUTATOR_CALLHOOK(VehicleExit, player, vehic);
877 vehic.team = vehic.tur_head.team;
879 sound (vehic, CH_TRIGGER_SINGLE, SND_Null, 1, ATTEN_NORM);
880 vehic.vehicle_hudmodel.viewmodelforclient = vehic;
881 vehic.phase = time + 1;
883 vehic.vehicle_exit(vehic, eject);
885 vehicles_setreturn(vehic);
886 vehicles_reset_colors(vehic);
889 CSQCMODEL_AUTOINIT(vehic);
891 vehicles_exit_running = false;
894 void vehicles_touch(entity this)
896 if(MUTATOR_CALLHOOK(VehicleTouch, this, other))
899 // Vehicle currently in use
902 if(!forbidWeaponUse(this.owner))
904 if((this.origin_z + this.maxs_z) > (other.origin_z))
905 if(vehicles_crushable(other))
907 if(vdist(this.velocity, >=, 30))
908 Damage(other, this, this.owner, autocvar_g_vehicles_crush_dmg, DEATH_VH_CRUSH.m_id, '0 0 0', normalize(other.origin - this.origin) * autocvar_g_vehicles_crush_force);
910 return; // Dont do selfdamage when hitting "soft targets".
913 if(this.play_time < time) {
914 Vehicle info = Vehicles_from(this.vehicleid);
915 info.vr_impact(info, this);
921 if(autocvar_g_vehicles_enter)
924 vehicles_enter(other, this);
927 bool vehicle_impulse(entity this, int imp)
929 entity v = this.vehicle;
930 if (!v) return false;
931 if (IS_DEAD(v)) return false;
932 bool(entity,int) f = v.vehicles_impulse;
933 if (f && f(this,imp)) return true;
936 case IMP_weapon_drop.impulse:
938 stuffcmd(this, "\ntoggle cl_eventchase_vehicle\nset _vehicles_shownchasemessage 1\n");
945 void vehicles_enter(entity pl, entity veh)
947 // Remove this when bots know how to use vehicles
948 if((IS_BOT_CLIENT(pl) && !autocvar_g_vehicles_allow_bots))
952 || (veh.phase >= time)
953 || (pl.vehicle_enter_delay >= time)
954 || (STAT(FROZEN, pl))
959 if(autocvar_g_vehicles_enter) // vehicle's touch function should handle this if entering via use key is disabled (TODO)
960 if(veh.vehicle_flags & VHF_MULTISLOT)
964 if(time >= veh.gun1.phase)
965 if(veh.gun1.vehicle_enter)
966 if(veh.gun1.vehicle_enter(veh, pl))
970 if(time >= veh.gun2.phase)
971 if(veh.gun2.vehicle_enter)
972 if(veh.gun2.vehicle_enter(veh, pl))
978 if(DIFF_TEAM(pl, veh))
979 if(autocvar_g_vehicles_steal)
981 FOREACH_CLIENT(IS_PLAYER(it) && SAME_TEAM(it, veh), Send_Notification(NOTIF_ONE, it, MSG_CENTER, CENTER_VEHICLE_STEAL));
983 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_VEHICLE_STEAL_SELF);
985 if (autocvar_g_vehicles_steal_show_waypoint) {
986 entity wp = WaypointSprite_Spawn(WP_VehicleIntruder, 0, 0, pl, '0 0 68', NULL, veh.team, veh, wps_intruder, true, RADARICON_DANGER);
987 wp.colormod = Team_ColorRGB(pl.team);
992 RemoveGrapplingHook(pl);
994 veh.vehicle_ammo1 = 0;
995 veh.vehicle_ammo2 = 0;
996 veh.vehicle_reload1 = 0;
997 veh.vehicle_reload2 = 0;
998 veh.vehicle_energy = 0;
1003 // .viewmodelforclient works better.
1004 //veh.vehicle_hudmodel.drawonlytoclient = veh.owner;
1006 veh.vehicle_hudmodel.viewmodelforclient = pl;
1008 tracebox(pl.origin, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), pl.origin, false, pl);
1010 pl.view_ofs = STAT(PL_VIEW_OFS, NULL);
1011 setsize (pl, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL));
1013 veh.event_damage = vehicles_damage;
1015 pl.angles = veh.angles;
1016 pl.takedamage = DAMAGE_NO;
1017 pl.solid = SOLID_NOT;
1018 pl.movetype = MOVETYPE_NOCLIP;
1019 pl.teleportable = false;
1021 pl.event_damage = func_null;
1022 pl.view_ofs = '0 0 0';
1023 veh.colormap = pl.colormap;
1025 veh.tur_head.colormap = pl.colormap;
1026 veh.m_switchweapon = PS(pl).m_switchweapon;
1027 pl.hud = veh.vehicleid;
1028 pl.PlayerPhysplug = veh.PlayerPhysplug;
1030 pl.vehicle_ammo1 = veh.vehicle_ammo1;
1031 pl.vehicle_ammo2 = veh.vehicle_ammo2;
1032 pl.vehicle_reload1 = veh.vehicle_reload1;
1033 pl.vehicle_reload2 = veh.vehicle_reload2;
1034 pl.vehicle_energy = veh.vehicle_energy;
1036 // Cant do this, hides attached objects too.
1037 //veh.exteriormodeltoclient = veh.owner;
1038 //veh.tur_head.exteriormodeltoclient = veh.owner;
1041 UNSET_ONGROUND(veh);
1044 veh.flags -= FL_NOTARGET;
1046 if (IS_REAL_CLIENT(pl))
1048 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_VEHICLE_ENTER);
1051 WriteByte (MSG_ONE, SVC_SETVIEWPORT);
1052 WriteEntity(MSG_ONE, veh.vehicle_viewport);
1054 WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
1057 WriteAngle(MSG_ONE, veh.tur_head.angles_x + veh.angles_x); // tilt
1058 WriteAngle(MSG_ONE, veh.tur_head.angles_y + veh.angles_y); // yaw
1059 WriteAngle(MSG_ONE, 0); // roll
1063 WriteAngle(MSG_ONE, veh.angles_x * -1); // tilt
1064 WriteAngle(MSG_ONE, veh.angles_y); // yaw
1065 WriteAngle(MSG_ONE, 0); // roll
1069 vehicles_clearreturn(veh);
1071 CSQCVehicleSetup(pl, veh.vehicleid);
1073 MUTATOR_CALLHOOK(VehicleEnter, pl, veh);
1075 CSQCModel_UnlinkEntity(veh);
1076 Vehicle info = Vehicles_from(veh.vehicleid);
1077 info.vr_enter(info, veh);
1079 antilag_clear(pl, CS(pl));
1082 void vehicles_think(entity this)
1084 this.nextthink = time;
1087 this.owner.vehicle_weapon2mode = this.vehicle_weapon2mode;
1089 Vehicle info = Vehicles_from(this.vehicleid);
1090 info.vr_think(info, this);
1092 vehicles_painframe(this);
1094 CSQCMODEL_AUTOUPDATE(this);
1098 void vehicles_spawn(entity this)
1100 LOG_TRACE("Spawning vehicle: ", this.classname, "\n");
1103 this.vehicle_hudmodel.viewmodelforclient = this;
1106 settouch(this, vehicles_touch);
1107 this.event_damage = vehicles_damage;
1108 this.iscreature = true;
1109 this.teleportable = false; // no teleporting for vehicles, too buggy
1110 this.damagedbycontents = true;
1111 this.movetype = MOVETYPE_WALK;
1112 this.solid = SOLID_SLIDEBOX;
1113 this.takedamage = DAMAGE_AIM;
1114 this.deadflag = DEAD_NO;
1115 this.bot_attack = true;
1116 this.flags = FL_NOTARGET;
1117 this.avelocity = '0 0 0';
1118 this.velocity = '0 0 0';
1119 setthink(this, vehicles_think);
1120 this.nextthink = time;
1123 this.lock_strength = 0;
1124 this.lock_target = NULL;
1125 this.misc_bulletcounter = 0;
1128 this.angles = this.pos2;
1129 setorigin(this, this.pos1);
1131 Send_Effect(EFFECT_TELEPORT, this.origin + '0 0 64', '0 0 0', 1);
1133 if(this.vehicle_controller)
1134 this.team = this.vehicle_controller.team;
1136 FOREACH_CLIENT(IS_PLAYER(it) && it.hook.aiment == this, RemoveGrapplingHook(it));
1138 vehicles_reset_colors(this);
1140 Vehicle info = Vehicles_from(this.vehicleid);
1141 info.vr_spawn(info, this);
1143 CSQCMODEL_AUTOINIT(this);
1146 bool vehicle_initialize(entity this, Vehicle info, bool nodrop)
1148 if(!autocvar_g_vehicles)
1155 info.vr_precache(info);
1157 if(this.targetname && this.targetname != "")
1159 this.vehicle_controller = find(NULL, target, this.targetname);
1160 if(!this.vehicle_controller)
1162 bprint("^1WARNING: ^7Vehicle with invalid .targetname\n");
1163 this.active = ACTIVE_ACTIVE;
1167 this.team = this.vehicle_controller.team;
1168 this.use = vehicle_use;
1172 if(this.vehicle_controller.team == 0)
1173 this.active = ACTIVE_NOT;
1175 this.active = ACTIVE_ACTIVE;
1179 else { this.active = ACTIVE_ACTIVE; }
1181 if(this.team && (!teamplay || !autocvar_g_vehicles_teams))
1184 if(this.mdl == "" || !this.mdl)
1185 _setmodel(this, info.model);
1187 _setmodel(this, this.mdl);
1189 this.vehicle_flags |= VHF_ISVEHICLE;
1191 this.vehicle_viewport = new(vehicle_viewport);
1192 this.vehicle_hudmodel = new(vehicle_hudmodel);
1193 this.tur_head = new(tur_head);
1194 this.tur_head.owner = this;
1195 this.takedamage = DAMAGE_NO;
1196 this.bot_attack = true;
1197 this.iscreature = true;
1198 this.teleportable = false; // no teleporting for vehicles, too buggy
1199 this.damagedbycontents = true;
1200 this.vehicleid = info.vehicleid;
1201 this.PlayerPhysplug = info.PlayerPhysplug;
1202 this.event_damage = func_null;
1203 settouch(this, vehicles_touch);
1204 setthink(this, vehicles_spawn);
1205 this.nextthink = time;
1206 this.effects = EF_NODRAW;
1207 this.dphitcontentsmask = DPCONTENTS_BODY | DPCONTENTS_SOLID;
1209 if(autocvar_g_playerclip_collisions)
1210 this.dphitcontentsmask |= DPCONTENTS_PLAYERCLIP;
1212 if(autocvar_g_nodepthtestplayers)
1213 this.effects |= EF_NODEPTHTEST;
1215 if(autocvar_g_fullbrightplayers)
1216 this.effects |= EF_FULLBRIGHT;
1218 _setmodel(this.vehicle_hudmodel, info.hud_model);
1219 setmodel(this.vehicle_viewport, MDL_Null);
1221 if(info.head_model != "")
1223 _setmodel(this.tur_head, info.head_model);
1224 setattachment(this.tur_head, this, info.tag_head);
1225 setattachment(this.vehicle_hudmodel, this.tur_head, info.tag_hud);
1226 setattachment(this.vehicle_viewport, this.vehicle_hudmodel, info.tag_view);
1230 setattachment(this.tur_head, this, "");
1231 setattachment(this.vehicle_hudmodel, this, info.tag_hud);
1232 setattachment(this.vehicle_viewport, this.vehicle_hudmodel, info.tag_view);
1235 setsize(this, info.mins, info.maxs);
1239 setorigin(this, this.origin);
1240 tracebox(this.origin + '0 0 100', info.mins, info.maxs, this.origin - '0 0 10000', MOVE_WORLDONLY, this);
1241 setorigin(this, trace_endpos);
1244 this.pos1 = this.origin;
1245 this.pos2 = this.angles;
1246 this.tur_head.team = this.team;
1248 info.vr_setup(info, this);
1250 if(this.active == ACTIVE_NOT)
1251 this.nextthink = 0; // wait until activated
1252 else if(autocvar_g_vehicles_delayspawn)
1253 this.nextthink = time + this.respawntime + (random() * autocvar_g_vehicles_delayspawn_jitter);
1255 this.nextthink = time + game_starttime;
1257 if(MUTATOR_CALLHOOK(VehicleSpawn, this))