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, self.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 == world || 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 = world;
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 != world)
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 = world; // invisible
130 if(this.lock_target == world && trace_ent != world)
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 != world)
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 = world;
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 this.think = this.use;
212 this.nextthink = time;
216 void vehicles_projectile_explode()
218 if(self.owner && other != world)
220 if(other == self.owner.vehicle)
223 if(other == self.owner.vehicle.tur_head)
229 self.event_damage = func_null;
230 RadiusDamage (self, self.realowner, self.shot_dmg, 0, self.shot_radius, self, world, self.shot_force, self.totalfrags, other);
235 entity vehicles_projectile(entity this, string _mzlfx, Sound _mzlsound,
236 vector _org, vector _vel,
237 float _dmg, float _radi, float _force, float _size,
238 int _deahtype, float _projtype, float _health,
239 bool _cull, bool _clianim, entity _owner)
241 TC(Sound, _mzlsound);
246 PROJECTILE_MAKETRIGGER(proj);
247 setorigin(proj, _org);
249 proj.shot_dmg = _dmg;
250 proj.shot_radius = _radi;
251 proj.shot_force = _force;
252 proj.totalfrags = _deahtype;
253 proj.solid = SOLID_BBOX;
254 proj.movetype = MOVETYPE_FLYMISSILE;
255 proj.flags = FL_PROJECTILE;
256 proj.bot_dodge = true;
257 proj.bot_dodgerating = _dmg;
258 proj.velocity = _vel;
259 proj.touch = vehicles_projectile_explode;
260 proj.use = vehicles_projectile_explode;
262 proj.realowner = _owner;
263 proj.think = SUB_Remove_self;
264 proj.nextthink = time + 30;
268 proj.takedamage = DAMAGE_AIM;
269 proj.event_damage = vehicles_projectile_damage;
270 proj.health = _health;
273 proj.flags = FL_PROJECTILE | FL_NOTARGET;
275 if(_mzlsound != SND_Null)
276 sound (this, CH_WEAPON_A, _mzlsound, VOL_BASE, ATTEN_NORM);
279 Send_Effect_(_mzlfx, proj.origin, proj.velocity, 1);
281 setsize (proj, '-1 -1 -1' * _size, '1 1 1' * _size);
283 CSQCProjectile(proj, _clianim, _projtype, _cull);
288 void vehicles_gib_explode()
290 sound (self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
291 Send_Effect(EFFECT_EXPLOSION_SMALL, randomvec() * 80 + (self.origin + '0 0 100'), '0 0 0', 1);
292 Send_Effect(EFFECT_EXPLOSION_SMALL, self.wp00.origin + '0 0 64', '0 0 0', 1);
296 void vehicles_gib_think()
302 self.nextthink = time + 0.1;
305 entity vehicle_tossgib(entity this, entity _template, vector _vel, string _tag, bool _burn, bool _explode, float _maxtime, vector _rot)
307 entity _gib = spawn();
308 _setmodel(_gib, _template.model);
309 vector org = gettaginfo(this, gettagindex(this, _tag));
310 setorigin(_gib, org);
311 _gib.velocity = _vel;
312 _gib.movetype = MOVETYPE_TOSS;
313 _gib.solid = SOLID_CORPSE;
314 _gib.colormod = '-0.5 -0.5 -0.5';
315 _gib.effects = EF_LOWPRECISION;
316 _gib.avelocity = _rot;
319 _gib.effects |= EF_FLAME;
323 _gib.think = vehicles_gib_explode;
324 _gib.nextthink = time + random() * _explode;
325 _gib.touch = vehicles_gib_explode;
329 _gib.cnt = time + _maxtime;
330 _gib.think = vehicles_gib_think;
331 _gib.nextthink = time + _maxtime - 1;
337 bool vehicle_addplayerslot( entity _owner,
341 bool(entity) _framefunc,
342 void(bool) _exitfunc, float(entity, entity) _enterfunc)
344 if(!(_owner.vehicle_flags & VHF_MULTISLOT))
345 _owner.vehicle_flags |= VHF_MULTISLOT;
347 _slot.PlayerPhysplug = _framefunc;
348 _slot.vehicle_exit = _exitfunc;
349 _slot.vehicle_enter = _enterfunc;
351 _slot.vehicle_flags = VHF_PLAYERSLOT;
352 _slot.vehicle_viewport = spawn();
353 _slot.vehicle_hudmodel = spawn();
354 _slot.vehicle_hudmodel.viewmodelforclient = _slot;
355 _slot.vehicle_viewport.effects = (EF_ADDITIVE | EF_DOUBLESIDED | EF_FULLBRIGHT | EF_NODEPTHTEST | EF_NOGUNBOB | EF_NOSHADOW | EF_LOWPRECISION | EF_SELECTABLE | EF_TELEPORT_BIT);
357 setmodel(_slot.vehicle_hudmodel, _hud_model);
358 setmodel(_slot.vehicle_viewport, MDL_Null);
360 setattachment(_slot.vehicle_hudmodel, _slot, "");
361 setattachment(_slot.vehicle_viewport, _slot.vehicle_hudmodel, "");
366 vector vehicle_aimturret(entity _vehic, vector _target, entity _turrret, string _tagname,
367 float _pichlimit_min, float _pichlimit_max,
368 float _rotlimit_min, float _rotlimit_max, float _aimspeed)
372 vtag = gettaginfo(_turrret, gettagindex(_turrret, _tagname));
373 vtmp = vectoangles(normalize(_target - vtag));
374 vtmp = AnglesTransform_ToAngles(AnglesTransform_LeftDivide(AnglesTransform_FromAngles(_vehic.angles), AnglesTransform_FromAngles(vtmp))) - _turrret.angles;
375 vtmp = AnglesTransform_Normalize(vtmp, true);
376 ftmp = _aimspeed * frametime;
377 vtmp_y = bound(-ftmp, vtmp_y, ftmp);
378 vtmp_x = bound(-ftmp, vtmp_x, ftmp);
379 _turrret.angles_y = bound(_rotlimit_min, _turrret.angles_y + vtmp_y, _rotlimit_max);
380 _turrret.angles_x = bound(_pichlimit_min, _turrret.angles_x + vtmp_x, _pichlimit_max);
384 void vehicles_reset_colors(entity this)
387 const vector cmod = '0 0 0';
388 if(this.team && teamplay)
389 cmap = 1024 + (this.team - 1) * 17;
392 if(autocvar_g_nodepthtestplayers)
393 eff |= EF_NODEPTHTEST;
394 if(autocvar_g_fullbrightplayers)
395 eff |= EF_FULLBRIGHT;
397 // Find all ents attacked to main model and setup effects, colormod etc.
398 FOREACH_ENTITY_ENT(tag_entity, this,
400 if(it != this.vehicle_shieldent)
409 // Also check head tags
410 FOREACH_ENTITY_ENT(tag_entity, this.tur_head,
412 if(it != this.vehicle_shieldent)
421 this.vehicle_hudmodel.effects = this.effects = eff; // | EF_LOWPRECISION;
422 this.vehicle_hudmodel.colormod = this.colormod = cmod;
423 this.vehicle_hudmodel.colormap = this.colormap = cmap;
424 this.vehicle_viewport.effects = (EF_ADDITIVE | EF_DOUBLESIDED | EF_FULLBRIGHT | EF_NODEPTHTEST | EF_NOGUNBOB | EF_NOSHADOW | EF_LOWPRECISION | EF_SELECTABLE | EF_TELEPORT_BIT);
427 this.avelocity = '0 0 0';
428 this.velocity = '0 0 0';
432 void vehicles_clearreturn(entity veh)
434 // Remove "return helper", if any.
435 for (entity ret = findchain(classname, "vehicle_return"); ret; ret = ret.chain)
440 ret.think = SUB_Remove_self;
441 ret.nextthink = time + 0.1;
443 if(ret.waypointsprite_attached)
444 WaypointSprite_Kill(ret.waypointsprite_attached);
451 void vehicles_spawn();
452 void vehicles_return()
454 Send_Effect(EFFECT_TELEPORT, self.wp00.origin + '0 0 64', '0 0 0', 1);
456 self.wp00.think = vehicles_spawn;
457 self.wp00.nextthink = time;
459 if(self.waypointsprite_attached)
460 WaypointSprite_Kill(self.waypointsprite_attached);
465 void vehicles_showwp_goaway()
467 if(self.waypointsprite_attached)
468 WaypointSprite_Kill(self.waypointsprite_attached);
474 void vehicles_showwp()
476 entity oldself = world;
481 self.think = vehicles_return;
482 self.nextthink = self.cnt;
486 self.think = vehicles_return;
487 self.nextthink = time +1;
491 setmodel(self, MDL_Null);
492 self.team = oldself.wp00.team;
493 self.wp00 = oldself.wp00;
494 setorigin(self, oldself.wp00.pos1);
496 self.nextthink = time + 5;
497 self.think = vehicles_showwp_goaway;
500 if(teamplay && self.team)
501 rgb = Team_ColorRGB(self.team);
504 entity wp = WaypointSprite_Spawn(WP_Vehicle, 0, 0, self, '0 0 64', world, 0, self, waypointsprite_attached, true, RADARICON_Vehicle);
506 if(self.waypointsprite_attached)
508 WaypointSprite_UpdateRule(self.waypointsprite_attached, self.wp00.team, SPRITERULE_DEFAULT);
510 WaypointSprite_UpdateBuildFinished(self.waypointsprite_attached, self.nextthink);
511 WaypointSprite_Ping(self.waypointsprite_attached);
518 void vehicles_setreturn(entity veh)
522 vehicles_clearreturn(veh);
524 ret = new(vehicle_return);
527 ret.think = vehicles_showwp;
531 ret.cnt = time + veh.respawntime;
532 ret.nextthink = min(time + veh.respawntime, time + veh.respawntime - 5);
536 ret.nextthink = min(time + veh.respawntime, time + veh.respawntime - 1);
539 setmodel(ret, MDL_Null);
540 setorigin(ret, veh.pos1 + '0 0 96');
546 LOG_TRACE("vehicle ",self.netname, " used by ", activator.classname, "\n");
548 self.tur_head.team = activator.team;
550 if(self.tur_head.team == 0)
551 self.active = ACTIVE_NOT;
553 self.active = ACTIVE_ACTIVE;
555 if(self.active == ACTIVE_ACTIVE && !IS_DEAD(self) && !gameover)
557 LOG_TRACE("Respawning vehicle: ", self.netname, "\n");
558 if(self.effects & EF_NODRAW)
560 self.think = vehicles_spawn;
561 self.nextthink = time + 3;
565 vehicles_setreturn(self);
566 vehicles_reset_colors(self);
571 void vehicles_regen(entity this, float timer, .float regen_field, float field_max, float rpause, float regen, float delta_time, float _healthscale)
573 if(this.(regen_field) < field_max)
574 if(timer + rpause < time)
577 regen = regen * (this.vehicle_health / this.max_health);
579 this.(regen_field) = min(this.(regen_field) + regen * delta_time, field_max);
582 this.owner.(regen_field) = (this.(regen_field) / field_max) * 100;
586 void shieldhit_think()
591 // setmodel(self, MDL_Null);
593 self.effects |= EF_NODRAW;
597 self.nextthink = time + 0.1;
601 void vehicles_painframe(entity this)
603 int myhealth = ((this.owner) ? this.owner.vehicle_health : ((this.vehicle_health / this.max_health) * 100));
606 if(this.pain_frame < time)
608 float _ftmp = myhealth / 50;
609 this.pain_frame = time + max(0.1, 0.1 + (random() * 0.5 * _ftmp));
610 Send_Effect(EFFECT_SMOKE_SMALL, (this.origin + (randomvec() * 80)), '0 0 0', 1);
612 if(this.vehicle_flags & VHF_DMGSHAKE)
613 this.velocity += randomvec() * 30;
615 if(this.vehicle_flags & VHF_DMGROLL)
616 if(this.vehicle_flags & VHF_DMGHEADROLL)
617 this.tur_head.angles += randomvec();
619 this.angles += randomvec();
623 void vehicles_frame(entity this, entity actor)
625 vehicles_painframe(this);
628 void vehicles_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
630 this.dmg_time = time;
633 if(DEATH_ISWEAPON(deathtype, WEP_VORTEX))
634 damage *= autocvar_g_vehicles_vortex_damagerate;
636 if(DEATH_ISWEAPON(deathtype, WEP_MACHINEGUN))
637 damage *= autocvar_g_vehicles_machinegun_damagerate;
639 if(DEATH_ISWEAPON(deathtype, WEP_RIFLE))
640 damage *= autocvar_g_vehicles_rifle_damagerate;
642 if(DEATH_ISWEAPON(deathtype, WEP_VAPORIZER))
643 damage *= autocvar_g_vehicles_vaporizer_damagerate;
645 if(DEATH_ISWEAPON(deathtype, WEP_SEEKER))
646 damage *= autocvar_g_vehicles_tag_damagerate;
648 if(DEATH_WEAPONOF(deathtype) != WEP_Null)
649 damage *= autocvar_g_vehicles_weapon_damagerate;
651 this.enemy = attacker;
653 this.pain_finished = time;
655 if((this.vehicle_flags & VHF_HASSHIELD) && (this.vehicle_shield > 0))
657 if (wasfreed(this.vehicle_shieldent) || this.vehicle_shieldent == world)
659 this.vehicle_shieldent = spawn();
660 this.vehicle_shieldent.effects = EF_LOWPRECISION;
662 setmodel(this.vehicle_shieldent, MDL_VEH_SHIELD);
663 setattachment(this.vehicle_shieldent, this, "");
664 setorigin(this.vehicle_shieldent, real_origin(this) - this.origin);
665 this.vehicle_shieldent.scale = 256 / vlen(this.maxs - this.mins);
666 this.vehicle_shieldent.think = shieldhit_think;
669 this.vehicle_shieldent.colormod = '1 1 1';
670 this.vehicle_shieldent.alpha = 0.45;
671 this.vehicle_shieldent.angles = vectoangles(normalize(hitloc - (this.origin + this.vehicle_shieldent.origin))) - this.angles;
672 this.vehicle_shieldent.nextthink = time;
673 this.vehicle_shieldent.effects &= ~EF_NODRAW;
675 this.vehicle_shield -= damage;
677 if(this.vehicle_shield < 0)
679 this.vehicle_health -= fabs(this.vehicle_shield);
680 this.vehicle_shieldent.colormod = '2 0 0';
681 this.vehicle_shield = 0;
682 this.vehicle_shieldent.alpha = 0.75;
684 if(sound_allowed(MSG_BROADCAST, attacker))
685 spamsound (this, CH_PAIN, "onslaught/ons_hit2.wav", VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
688 if(sound_allowed(MSG_BROADCAST, attacker))
689 spamsound (this, CH_PAIN, "onslaught/electricity_explode.wav", VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
694 this.vehicle_health -= damage;
696 if(sound_allowed(MSG_BROADCAST, attacker))
697 spamsound (this, CH_PAIN, "onslaught/ons_hit2.wav", VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
700 if(this.damageforcescale < 1 && this.damageforcescale > 0)
701 this.velocity += force * this.damageforcescale;
703 this.velocity += force;
705 if(this.vehicle_health <= 0)
708 if(this.vehicle_flags & VHF_DEATHEJECT)
709 WITHSELF(this, vehicles_exit(VHEF_EJECT));
711 WITHSELF(this, vehicles_exit(VHEF_RELEASE));
714 antilag_clear(this, this);
716 Vehicle info = Vehicles_from(this.vehicleid);
717 info.vr_death(info, this);
718 vehicles_setreturn(this);
722 float vehicles_crushable(entity e)
724 if(IS_PLAYER(e) && time >= e.vehicle_enter_delay)
733 void vehicles_impact(entity this, float _minspeed, float _speedfac, float _maxpain)
735 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
738 if(this.play_time < time)
740 float wc = vlen(this.velocity - this.oldvelocity);
741 //dprint("oldvel: ", vtos(this.oldvelocity), "\n");
742 //dprint("vel: ", vtos(this.velocity), "\n");
745 float take = min(_speedfac * wc, _maxpain);
746 Damage (this, world, world, take, DEATH_FALL.m_id, this.origin, '0 0 0');
747 this.play_time = time + 0.25;
749 //dprint("wc: ", ftos(wc), "\n");
750 //dprint("take: ", ftos(take), "\n");
755 // vehicle enter/exit handling
756 vector vehicles_findgoodexit(vector prefer_spot)
761 tracebox(self.origin + '0 0 32', STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), prefer_spot, MOVE_NORMAL, self.owner);
762 if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
765 mysize = 1.5 * vlen(self.maxs - self.mins);
768 v2 = 0.5 * (self.absmin + self.absmax);
769 for(i = 0; i < 100; ++i)
773 v = v2 + normalize(v) * mysize;
774 tracebox(v2, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), v, MOVE_NORMAL, self.owner);
775 if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
780 exitspot = (self.origin + '0 0 48') + v_forward * mysize;
781 tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, exitspot, MOVE_NORMAL, self.owner);
782 if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
785 exitspot = (self.origin + '0 0 48') - v_forward * mysize;
786 tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, exitspot, MOVE_NORMAL, self.owner);
787 if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
790 exitspot = (self.origin + '0 0 48') + v_right * mysize;
791 tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, exitspot, MOVE_NORMAL, self.owner);
792 if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
795 exitspot = (self.origin + '0 0 48') - v_right * mysize;
796 tracebox(self.origin + '0 0 32', PL_MIN, PL_MAX, exitspot, MOVE_NORMAL, self.owner);
797 if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
804 void vehicles_exit(bool eject)
809 if(vehicles_exit_running)
811 LOG_TRACE("^1vehicles_exit allready running! this is not good..\n");
815 vehicles_exit_running = true;
818 _vehicle = self.vehicle;
820 if (_vehicle.vehicle_flags & VHF_PLAYERSLOT)
822 _vehicle.vehicle_exit(eject);
824 vehicles_exit_running = false;
831 _player = _vehicle.owner;
837 if (IS_REAL_CLIENT(_player))
839 msg_entity = _player;
840 WriteByte (MSG_ONE, SVC_SETVIEWPORT);
841 WriteEntity( MSG_ONE, _player);
843 WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
844 WriteAngle(MSG_ONE, 0);
845 WriteAngle(MSG_ONE, _vehicle.angles_y);
846 WriteAngle(MSG_ONE, 0);
849 setsize(_player, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL));
851 _player.takedamage = DAMAGE_AIM;
852 _player.solid = SOLID_SLIDEBOX;
853 _player.movetype = MOVETYPE_WALK;
854 _player.effects &= ~EF_NODRAW;
855 _player.teleportable = TELEPORT_NORMAL;
857 _player.PlayerPhysplug = func_null;
858 _player.vehicle = world;
859 _player.view_ofs = STAT(PL_VIEW_OFS, NULL);
860 _player.event_damage = PlayerDamage;
861 _player.hud = HUD_NORMAL;
862 PS(_player).m_switchweapon = _vehicle.m_switchweapon;
863 _player.last_vehiclecheck = time + 3;
864 _player.vehicle_enter_delay = time + 2;
866 CSQCVehicleSetup(_player, HUD_NORMAL);
868 _vehicle.flags |= FL_NOTARGET;
870 if(!IS_DEAD(_vehicle))
871 _vehicle.avelocity = '0 0 0';
873 _vehicle.tur_head.nodrawtoclient = world;
878 Kill_Notification(NOTIF_ONE, _player, MSG_CENTER, CPID_VEHICLES);
879 Kill_Notification(NOTIF_ONE, _player, MSG_CENTER, CPID_VEHICLES_OTHER); // kill all vehicle notifications when exiting a vehicle?
881 WaypointSprite_Kill(_vehicle.wps_intruder);
883 MUTATOR_CALLHOOK(VehicleExit, _player, _vehicle);
885 _vehicle.team = _vehicle.tur_head.team;
887 sound (_vehicle, CH_TRIGGER_SINGLE, SND_Null, 1, ATTEN_NORM);
888 _vehicle.vehicle_hudmodel.viewmodelforclient = _vehicle;
889 _vehicle.phase = time + 1;
891 _vehicle.vehicle_exit(eject);
893 vehicles_setreturn(_vehicle);
894 vehicles_reset_colors(_vehicle);
895 _vehicle.owner = world;
897 CSQCMODEL_AUTOINIT(self);
900 vehicles_exit_running = false;
903 void vehicles_touch()
905 if(MUTATOR_CALLHOOK(VehicleTouch, self, other))
908 // Vehicle currently in use
911 if(!forbidWeaponUse(self.owner))
913 if((self.origin_z + self.maxs_z) > (other.origin_z))
914 if(vehicles_crushable(other))
916 if(vdist(self.velocity, >=, 30))
917 Damage(other, self, self.owner, autocvar_g_vehicles_crush_dmg, DEATH_VH_CRUSH.m_id, '0 0 0', normalize(other.origin - self.origin) * autocvar_g_vehicles_crush_force);
919 return; // Dont do selfdamage when hitting "soft targets".
922 if(self.play_time < time) {
923 Vehicle info = Vehicles_from(self.vehicleid);
924 info.vr_impact(info, self);
930 if(autocvar_g_vehicles_enter)
933 vehicles_enter(other, self);
936 bool vehicle_impulse(entity this, int imp)
938 entity v = this.vehicle;
939 if (!v) return false;
940 if (IS_DEAD(v)) return false;
941 bool(int) f = v.vehicles_impulse;
942 if (f && f(imp)) return true;
945 case IMP_weapon_drop.impulse:
947 stuffcmd(self, "\ntoggle cl_eventchase_vehicle\nset _vehicles_shownchasemessage 1\n");
954 void vehicles_enter(entity pl, entity veh)
956 entity oldself = self;
958 // Remove this when bots know how to use vehicles
959 if((IS_BOT_CLIENT(pl) && !autocvar_g_vehicles_allow_bots))
963 || (veh.phase >= time)
964 || (pl.vehicle_enter_delay >= time)
965 || (STAT(FROZEN, pl))
970 if(autocvar_g_vehicles_enter) // vehicle's touch function should handle this if entering via use key is disabled (TODO)
971 if(veh.vehicle_flags & VHF_MULTISLOT)
975 if(time >= veh.gun1.phase)
976 if(veh.gun1.vehicle_enter)
977 if(veh.gun1.vehicle_enter(veh, pl))
981 if(time >= veh.gun2.phase)
982 if(veh.gun2.vehicle_enter)
983 if(veh.gun2.vehicle_enter(veh, pl))
989 if(DIFF_TEAM(pl, veh))
990 if(autocvar_g_vehicles_steal)
992 FOREACH_CLIENT(IS_PLAYER(it) && SAME_TEAM(it, veh), Send_Notification(NOTIF_ONE, it, MSG_CENTER, CENTER_VEHICLE_STEAL));
994 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_VEHICLE_STEAL_SELF);
996 if (autocvar_g_vehicles_steal_show_waypoint) {
997 entity wp = WaypointSprite_Spawn(WP_VehicleIntruder, 0, 0, pl, '0 0 68', world, veh.team, veh, wps_intruder, true, RADARICON_DANGER);
998 wp.colormod = Team_ColorRGB(pl.team);
1003 RemoveGrapplingHook(pl);
1005 veh.vehicle_ammo1 = 0;
1006 veh.vehicle_ammo2 = 0;
1007 veh.vehicle_reload1 = 0;
1008 veh.vehicle_reload2 = 0;
1009 veh.vehicle_energy = 0;
1014 // .viewmodelforclient works better.
1015 //veh.vehicle_hudmodel.drawonlytoclient = veh.owner;
1017 veh.vehicle_hudmodel.viewmodelforclient = pl;
1019 tracebox(pl.origin, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), pl.origin, false, pl);
1021 pl.view_ofs = STAT(PL_VIEW_OFS, NULL);
1022 setsize (pl, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL));
1024 veh.event_damage = vehicles_damage;
1026 pl.angles = veh.angles;
1027 pl.takedamage = DAMAGE_NO;
1028 pl.solid = SOLID_NOT;
1029 pl.movetype = MOVETYPE_NOCLIP;
1030 pl.teleportable = false;
1032 pl.event_damage = func_null;
1033 pl.view_ofs = '0 0 0';
1034 veh.colormap = pl.colormap;
1036 veh.tur_head.colormap = pl.colormap;
1037 veh.m_switchweapon = PS(pl).m_switchweapon;
1038 pl.hud = veh.vehicleid;
1039 pl.PlayerPhysplug = veh.PlayerPhysplug;
1041 pl.vehicle_ammo1 = veh.vehicle_ammo1;
1042 pl.vehicle_ammo2 = veh.vehicle_ammo2;
1043 pl.vehicle_reload1 = veh.vehicle_reload1;
1044 pl.vehicle_reload2 = veh.vehicle_reload2;
1045 pl.vehicle_energy = veh.vehicle_energy;
1047 // Cant do this, hides attached objects too.
1048 //veh.exteriormodeltoclient = veh.owner;
1049 //veh.tur_head.exteriormodeltoclient = veh.owner;
1052 UNSET_ONGROUND(veh);
1055 veh.flags -= FL_NOTARGET;
1057 if (IS_REAL_CLIENT(pl))
1059 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_VEHICLE_ENTER);
1062 WriteByte (MSG_ONE, SVC_SETVIEWPORT);
1063 WriteEntity(MSG_ONE, veh.vehicle_viewport);
1065 WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
1068 WriteAngle(MSG_ONE, veh.tur_head.angles_x + veh.angles_x); // tilt
1069 WriteAngle(MSG_ONE, veh.tur_head.angles_y + veh.angles_y); // yaw
1070 WriteAngle(MSG_ONE, 0); // roll
1074 WriteAngle(MSG_ONE, veh.angles_x * -1); // tilt
1075 WriteAngle(MSG_ONE, veh.angles_y); // yaw
1076 WriteAngle(MSG_ONE, 0); // roll
1080 vehicles_clearreturn(veh);
1082 CSQCVehicleSetup(pl, veh.vehicleid);
1084 MUTATOR_CALLHOOK(VehicleEnter, pl, veh);
1087 CSQCModel_UnlinkEntity(veh);
1088 Vehicle info = Vehicles_from(veh.vehicleid);
1089 info.vr_enter(info, veh);
1092 antilag_clear(pl, CS(pl));
1095 void vehicles_think()
1097 self.nextthink = time;
1100 self.owner.vehicle_weapon2mode = self.vehicle_weapon2mode;
1102 Vehicle info = Vehicles_from(self.vehicleid);
1103 info.vr_think(info, self);
1105 vehicles_painframe(self);
1107 CSQCMODEL_AUTOUPDATE(self);
1111 void vehicles_spawn()
1113 LOG_TRACE("Spawning vehicle: ", self.classname, "\n");
1116 self.vehicle_hudmodel.viewmodelforclient = self;
1119 self.touch = vehicles_touch;
1120 self.event_damage = vehicles_damage;
1121 self.iscreature = true;
1122 self.teleportable = false; // no teleporting for vehicles, too buggy
1123 self.damagedbycontents = true;
1124 self.movetype = MOVETYPE_WALK;
1125 self.solid = SOLID_SLIDEBOX;
1126 self.takedamage = DAMAGE_AIM;
1127 self.deadflag = DEAD_NO;
1128 self.bot_attack = true;
1129 self.flags = FL_NOTARGET;
1130 self.avelocity = '0 0 0';
1131 self.velocity = '0 0 0';
1132 self.think = vehicles_think;
1133 self.nextthink = time;
1136 self.lock_strength = 0;
1137 self.lock_target = world;
1138 self.misc_bulletcounter = 0;
1141 self.angles = self.pos2;
1142 setorigin(self, self.pos1);
1144 Send_Effect(EFFECT_TELEPORT, self.origin + '0 0 64', '0 0 0', 1);
1146 if(self.vehicle_controller)
1147 self.team = self.vehicle_controller.team;
1149 FOREACH_CLIENT(IS_PLAYER(it) && it.hook.aiment == self, RemoveGrapplingHook(it));
1151 vehicles_reset_colors(self);
1153 Vehicle info = Vehicles_from(self.vehicleid);
1154 info.vr_spawn(info, self);
1156 CSQCMODEL_AUTOINIT(self);
1159 bool vehicle_initialize(entity this, Vehicle info, bool nodrop)
1161 if(!autocvar_g_vehicles)
1167 if(this.targetname && this.targetname != "")
1169 this.vehicle_controller = find(world, target, this.targetname);
1170 if(!this.vehicle_controller)
1172 bprint("^1WARNING: ^7Vehicle with invalid .targetname\n");
1173 this.active = ACTIVE_ACTIVE;
1177 this.team = this.vehicle_controller.team;
1178 this.use = vehicle_use;
1182 if(this.vehicle_controller.team == 0)
1183 this.active = ACTIVE_NOT;
1185 this.active = ACTIVE_ACTIVE;
1189 else { this.active = ACTIVE_ACTIVE; }
1191 if(this.team && (!teamplay || !autocvar_g_vehicles_teams))
1194 if(this.mdl == "" || !this.mdl)
1195 _setmodel(this, info.model);
1197 _setmodel(this, this.mdl);
1199 this.vehicle_flags |= VHF_ISVEHICLE;
1201 this.vehicle_viewport = new(vehicle_viewport);
1202 this.vehicle_hudmodel = new(vehicle_hudmodel);
1203 this.tur_head = new(tur_head);
1204 this.tur_head.owner = this;
1205 this.takedamage = DAMAGE_NO;
1206 this.bot_attack = true;
1207 this.iscreature = true;
1208 this.teleportable = false; // no teleporting for vehicles, too buggy
1209 this.damagedbycontents = true;
1210 this.vehicleid = info.vehicleid;
1211 this.PlayerPhysplug = info.PlayerPhysplug;
1212 this.event_damage = func_null;
1213 this.touch = vehicles_touch;
1214 this.think = vehicles_spawn;
1215 this.nextthink = time;
1216 this.effects = EF_NODRAW;
1217 this.dphitcontentsmask = DPCONTENTS_BODY | DPCONTENTS_SOLID;
1219 if(autocvar_g_playerclip_collisions)
1220 this.dphitcontentsmask |= DPCONTENTS_PLAYERCLIP;
1222 if(autocvar_g_nodepthtestplayers)
1223 this.effects |= EF_NODEPTHTEST;
1225 if(autocvar_g_fullbrightplayers)
1226 this.effects |= EF_FULLBRIGHT;
1228 _setmodel(this.vehicle_hudmodel, info.hud_model);
1229 setmodel(this.vehicle_viewport, MDL_Null);
1231 if(info.head_model != "")
1233 _setmodel(this.tur_head, info.head_model);
1234 setattachment(this.tur_head, this, info.tag_head);
1235 setattachment(this.vehicle_hudmodel, this.tur_head, info.tag_hud);
1236 setattachment(this.vehicle_viewport, this.vehicle_hudmodel, info.tag_view);
1240 setattachment(this.tur_head, this, "");
1241 setattachment(this.vehicle_hudmodel, this, info.tag_hud);
1242 setattachment(this.vehicle_viewport, this.vehicle_hudmodel, info.tag_view);
1245 setsize(this, info.mins, info.maxs);
1249 setorigin(this, this.origin);
1250 tracebox(this.origin + '0 0 100', info.mins, info.maxs, this.origin - '0 0 10000', MOVE_WORLDONLY, this);
1251 setorigin(this, trace_endpos);
1254 this.pos1 = this.origin;
1255 this.pos2 = this.angles;
1256 this.tur_head.team = this.team;
1258 info.vr_setup(info, this);
1260 if(this.active == ACTIVE_NOT)
1261 this.nextthink = 0; // wait until activated
1262 else if(autocvar_g_vehicles_delayspawn)
1263 this.nextthink = time + this.respawntime + (random() * autocvar_g_vehicles_delayspawn_jitter);
1265 this.nextthink = time + game_starttime;
1267 if(MUTATOR_CALLHOOK(VehicleSpawn, this))