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 bool AuxiliaryXhair_customize(entity this, entity client)
29 entity e = WaypointSprite_getviewentity(client);
30 entity axh = e.(AuxiliaryXhair[this.cnt]);
31 return axh.owner == this.owner; // cheaply check if the client's axh owner is the same as our real owner
34 .vector axh_prevorigin;
35 .vector axh_prevcolors;
37 void UpdateAuxiliaryXhair(entity own, vector loc, vector clr, int axh_id)
39 if(!IS_REAL_CLIENT(own))
42 axh_id = bound(0, axh_id, MAX_AXH);
43 entity axh = own.(AuxiliaryXhair[axh_id]);
45 if(axh == NULL || wasfreed(axh)) // MADNESS? THIS IS QQQQCCCCCCCCC (wasfreed, why do you exsist? Mario: because of sloppy code like this)
47 axh = new(auxiliary_xhair);
49 //axh.drawonlytoclient = own; // not spectatable
50 setcefc(axh, AuxiliaryXhair_customize);
52 Net_LinkEntity(axh, false, 0, SendAuxiliaryXhair);
55 if(loc != axh.axh_prevorigin)
61 if(clr != axh.axh_prevcolors)
67 own.(AuxiliaryXhair[axh_id]) = axh; // set it anyway...?
70 void CSQCVehicleSetup(entity own, int vehicle_id)
72 if(!IS_REAL_CLIENT(own))
77 WriteHeader(MSG_ONE, TE_CSQC_VEHICLESETUP);
78 WriteByte(MSG_ONE, vehicle_id);
80 if(vehicle_id == 0 || vehicle_id == HUD_NORMAL)
82 for(int i = 0; i < MAX_AXH; ++i)
84 entity axh = own.(AuxiliaryXhair[i]);
85 own.(AuxiliaryXhair[i]) = NULL;
87 if(axh.owner == own && axh != NULL && !wasfreed(axh))
93 void vehicles_locktarget(entity this, float incr, float decr, float _lock_time)
95 if(this.lock_target && IS_DEAD(this.lock_target))
97 this.lock_target = NULL;
98 this.lock_strength = 0;
102 if(this.lock_time > time)
105 if(this.lock_soundtime < time)
107 this.lock_soundtime = time + 0.5;
108 play2(this.owner, "vehicles/locked.wav");
114 if(trace_ent != NULL)
116 if(SAME_TEAM(trace_ent, this))
119 if(IS_DEAD(trace_ent))
122 if(!(IS_VEHICLE(trace_ent) || IS_TURRET(trace_ent)))
125 if(trace_ent.alpha <= 0.5 && trace_ent.alpha != 0)
126 trace_ent = NULL; // invisible
129 if(this.lock_target == NULL && trace_ent != NULL)
130 this.lock_target = trace_ent;
132 if(this.lock_target && trace_ent == this.lock_target)
134 if(this.lock_strength != 1 && this.lock_strength + incr >= 1)
136 play2(this.owner, "vehicles/lock.wav");
137 this.lock_soundtime = time + 0.8;
139 else if (this.lock_strength != 1 && this.lock_soundtime < time)
141 play2(this.owner, "vehicles/locking.wav");
142 this.lock_soundtime = time + 0.3;
146 // Have a locking target
147 // Trace hit current target
148 if(trace_ent == this.lock_target && trace_ent != NULL)
150 this.lock_strength = min(this.lock_strength + incr, 1);
151 if(this.lock_strength == 1)
152 this.lock_time = time + _lock_time;
157 this.lock_strength = max(this.lock_strength - decr * 2, 0);
159 this.lock_strength = max(this.lock_strength - decr, 0);
161 if(this.lock_strength == 0)
162 this.lock_target = NULL;
166 float vehicle_altitude(entity this, float amax)
168 tracebox(this.origin, this.mins, this.maxs, this.origin - ('0 0 1' * amax), MOVE_WORLDONLY, this);
169 return vlen(this.origin - trace_endpos);
172 vector vehicles_force_fromtag_hover(entity this, string tag_name, float spring_length, float max_power)
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);
178 force_fromtag_power = (1 - trace_fraction) * max_power;
179 force_fromtag_normpower = force_fromtag_power / max_power;
181 return v_forward * force_fromtag_power;
184 vector vehicles_force_fromtag_maglev(entity this, string tag_name, float spring_length, float max_power)
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);
190 // TODO - this may NOT be compatible with wall/celing movement, unhardcode 0.25 (engine count multiplier)
191 if(trace_fraction == 1.0)
193 force_fromtag_normpower = -0.25;
197 force_fromtag_power = ((1 - trace_fraction) - trace_fraction) * max_power;
198 force_fromtag_normpower = force_fromtag_power / max_power;
200 return v_forward * force_fromtag_power;
203 // projectile handling
204 void vehicles_projectile_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
206 // Ignore damage from oterh projectiles from my owner (dont mess up volly's)
207 if(inflictor.owner == this.owner)
210 this.health -= damage;
211 this.velocity += force;
214 this.takedamage = DAMAGE_NO;
215 this.event_damage = func_null;
216 setthink(this, adaptor_think2use);
217 this.nextthink = time;
221 void vehicles_projectile_explode(entity this, entity toucher)
223 if(this.owner && toucher != NULL)
225 if(toucher == this.owner.vehicle)
228 if(toucher == this.owner.vehicle.tur_head)
232 PROJECTILE_TOUCH(this, toucher);
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);
240 void vehicles_projectile_explode_think(entity this)
242 vehicles_projectile_explode(this, NULL);
245 void vehicles_projectile_explode_use(entity this, entity actor, entity trigger)
247 vehicles_projectile_explode(this, trigger);
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)
256 TC(Sound, _mzlsound);
261 PROJECTILE_MAKETRIGGER(proj);
262 setorigin(proj, _org);
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;
279 proj.realowner = _owner;
280 setthink(proj, SUB_Remove);
281 proj.nextthink = time + 30;
285 proj.takedamage = DAMAGE_AIM;
286 proj.event_damage = vehicles_projectile_damage;
287 proj.health = _health;
290 proj.flags |= FL_NOTARGET;
292 if(_mzlsound != SND_Null)
293 sound (this, CH_WEAPON_A, _mzlsound, VOL_BASE, ATTEN_NORM);
296 Send_Effect_(_mzlfx, proj.origin, proj.velocity, 1);
298 setsize (proj, '-1 -1 -1' * _size, '1 1 1' * _size);
300 CSQCProjectile(proj, _clianim, _projtype, _cull);
305 void vehicles_gib_explode(entity this)
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);
313 void vehicles_gib_touch(entity this, entity toucher)
315 vehicles_gib_explode(this);
318 void vehicles_gib_think(entity this)
324 this.nextthink = time + 0.1;
327 entity vehicle_tossgib(entity this, entity _template, vector _vel, string _tag, bool _burn, bool _explode, float _maxtime, vector _rot)
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;
341 _gib.effects |= EF_FLAME;
345 setthink(_gib, vehicles_gib_explode);
346 _gib.nextthink = time + random() * _explode;
347 settouch(_gib, vehicles_gib_touch);
351 _gib.cnt = time + _maxtime;
352 setthink(_gib, vehicles_gib_think);
353 _gib.nextthink = time + _maxtime - 1;
359 bool vehicle_addplayerslot( entity _owner,
363 bool(entity,float) _framefunc,
364 void(entity,bool) _exitfunc, float(entity, entity) _enterfunc)
366 if(!(_owner.vehicle_flags & VHF_MULTISLOT))
367 _owner.vehicle_flags |= VHF_MULTISLOT;
369 _slot.PlayerPhysplug = _framefunc;
370 _slot.vehicle_exit = _exitfunc;
371 _slot.vehicle_enter = _enterfunc;
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);
379 setmodel(_slot.vehicle_hudmodel, _hud_model);
380 setmodel(_slot.vehicle_viewport, MDL_Null);
382 setattachment(_slot.vehicle_hudmodel, _slot, "");
383 setattachment(_slot.vehicle_viewport, _slot.vehicle_hudmodel, "");
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)
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);
406 void vehicles_reset_colors(entity this, entity player)
409 const vector cmod = '0 0 0';
410 if(this.team && teamplay)
411 cmap = 1024 + (this.team - 1) * 17;
413 cmap = player.colormap;
416 if(autocvar_g_nodepthtestplayers)
417 eff |= EF_NODEPTHTEST;
418 if(autocvar_g_fullbrightplayers)
419 eff |= EF_FULLBRIGHT;
421 // Find all ents attacked to main model and setup effects, colormod etc.
422 FOREACH_ENTITY_ENT(tag_entity, this,
424 if(it == this.vehicle_shieldent)
433 // Also check head tags
434 FOREACH_ENTITY_ENT(tag_entity, this.tur_head,
436 if(it == this.vehicle_shieldent)
445 this.vehicle_hudmodel.effects = this.effects = eff; // | EF_LOWPRECISION;
446 this.vehicle_hudmodel.colormod = this.colormod = cmod;
447 this.vehicle_hudmodel.colormap = this.colormap = cmap;
448 this.vehicle_viewport.effects = (EF_ADDITIVE | EF_DOUBLESIDED | EF_FULLBRIGHT | EF_NODEPTHTEST | EF_NOGUNBOB | EF_NOSHADOW | EF_LOWPRECISION | EF_SELECTABLE | EF_TELEPORT_BIT);
451 this.avelocity = '0 0 0';
452 this.velocity = '0 0 0';
455 Vehicle info = Vehicles_from(this.vehicleid);
456 info.vr_setcolors(info, this);
459 void vehicles_clearreturn(entity veh)
461 // Remove "return helper" entities, if any.
462 IL_EACH(g_vehicle_returners, it.wp00 == veh,
465 setthink(it, SUB_Remove);
466 it.nextthink = time + 0.1;
467 IL_REMOVE(g_vehicle_returners, it);
469 if(it.waypointsprite_attached)
470 WaypointSprite_Kill(it.waypointsprite_attached);
474 void vehicles_spawn(entity this);
475 void vehicles_return(entity this)
477 Send_Effect(EFFECT_TELEPORT, this.wp00.origin + '0 0 64', '0 0 0', 1);
479 setthink(this.wp00, vehicles_spawn);
480 this.wp00.nextthink = time;
482 if(this.waypointsprite_attached)
483 WaypointSprite_Kill(this.waypointsprite_attached);
488 void vehicles_showwp_goaway(entity this)
490 if(this.waypointsprite_attached)
491 WaypointSprite_Kill(this.waypointsprite_attached);
496 void vehicles_showwp(entity this)
502 setthink(ent, vehicles_return);
503 ent.nextthink = ent.cnt;
507 setthink(ent, vehicles_return);
508 ent.nextthink = time + 1;
511 ent.team = this.wp00.team;
512 ent.wp00 = this.wp00;
513 setorigin(ent, this.wp00.pos1);
515 ent.nextthink = time + 5;
516 setthink(ent, vehicles_showwp_goaway);
520 if(teamplay && ent.team)
521 rgb = Team_ColorRGB(ent.team);
524 entity wp = WaypointSprite_Spawn(WP_Vehicle, 0, 0, ent, '0 0 64', NULL, 0, ent, waypointsprite_attached, true, RADARICON_Vehicle);
525 wp.wp_extra = ent.wp00.vehicleid;
527 if(ent.waypointsprite_attached)
529 WaypointSprite_UpdateRule(ent.waypointsprite_attached, ent.wp00.team, SPRITERULE_DEFAULT);
531 WaypointSprite_UpdateBuildFinished(ent.waypointsprite_attached, ent.nextthink);
532 WaypointSprite_Ping(ent.waypointsprite_attached);
536 void vehicles_setreturn(entity veh)
538 vehicles_clearreturn(veh);
540 entity ret = new(vehicle_return);
541 IL_PUSH(g_vehicle_returners, ret);
544 setthink(ret, vehicles_showwp);
548 ret.cnt = time + veh.respawntime;
549 ret.nextthink = min(time + veh.respawntime, time + veh.respawntime - 5);
553 ret.nextthink = min(time + veh.respawntime, time + veh.respawntime - 1);
556 setorigin(ret, veh.pos1 + '0 0 96');
559 void vehicle_use(entity this, entity actor, entity trigger)
561 LOG_DEBUG("vehicle ", this.netname, " used by ", actor.classname);
563 this.tur_head.team = actor.team;
565 if(this.tur_head.team == 0)
566 this.active = ACTIVE_NOT;
568 this.active = ACTIVE_ACTIVE;
570 if(this.active == ACTIVE_ACTIVE && !IS_DEAD(this) && !game_stopped)
572 LOG_DEBUG("Respawning vehicle: ", this.netname);
573 if(this.effects & EF_NODRAW)
575 setthink(this, vehicles_spawn);
576 this.nextthink = time + 3;
580 vehicles_setreturn(this);
581 vehicles_reset_colors(this, actor);
586 void vehicles_regen(entity this, float timer, .float regen_field, float field_max, float rpause, float regen, float delta_time, float _healthscale)
588 if(this.(regen_field) < field_max)
589 if(timer + rpause < time)
592 regen = regen * (this.vehicle_health / this.max_health);
594 this.(regen_field) = min(this.(regen_field) + regen * delta_time, field_max);
597 this.owner.(regen_field) = (this.(regen_field) / field_max) * 100;
601 void shieldhit_think(entity this)
606 // setmodel(this, MDL_Null);
608 this.effects |= EF_NODRAW;
612 this.nextthink = time + 0.1;
616 void vehicles_painframe(entity this)
618 int myhealth = ((this.owner) ? this.owner.vehicle_health : ((this.vehicle_health / this.max_health) * 100));
621 if(this.pain_frame < time)
623 float _ftmp = myhealth / 50;
624 this.pain_frame = time + max(0.1, 0.1 + (random() * 0.5 * _ftmp));
625 Send_Effect(EFFECT_SMOKE_SMALL, (this.origin + (randomvec() * 80)), '0 0 0', 1);
627 if(this.vehicle_flags & VHF_DMGSHAKE)
628 this.velocity += randomvec() * 30;
630 if(this.vehicle_flags & VHF_DMGROLL)
631 if(this.vehicle_flags & VHF_DMGHEADROLL)
632 this.tur_head.angles += randomvec();
634 this.angles += randomvec();
638 void vehicles_frame(entity this, entity actor)
640 vehicles_painframe(this);
643 void vehicles_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
645 this.dmg_time = time;
648 if(DEATH_ISWEAPON(deathtype, WEP_VORTEX))
649 damage *= autocvar_g_vehicles_vortex_damagerate;
650 else if(DEATH_ISWEAPON(deathtype, WEP_MACHINEGUN))
651 damage *= autocvar_g_vehicles_machinegun_damagerate;
652 else if(DEATH_ISWEAPON(deathtype, WEP_RIFLE))
653 damage *= autocvar_g_vehicles_rifle_damagerate;
654 else if(DEATH_ISWEAPON(deathtype, WEP_VAPORIZER))
655 damage *= autocvar_g_vehicles_vaporizer_damagerate;
656 else if(DEATH_ISWEAPON(deathtype, WEP_SEEKER))
657 damage *= autocvar_g_vehicles_tag_damagerate;
658 else if(DEATH_WEAPONOF(deathtype) != WEP_Null)
659 damage *= autocvar_g_vehicles_weapon_damagerate;
661 this.enemy = attacker;
663 this.pain_finished = time;
665 if((this.vehicle_flags & VHF_HASSHIELD) && (this.vehicle_shield > 0))
667 if (wasfreed(this.vehicle_shieldent) || this.vehicle_shieldent == NULL)
669 this.vehicle_shieldent = spawn();
670 this.vehicle_shieldent.effects = EF_LOWPRECISION;
672 setmodel(this.vehicle_shieldent, MDL_VEH_SHIELD);
673 setattachment(this.vehicle_shieldent, this, "");
674 setorigin(this.vehicle_shieldent, real_origin(this) - this.origin);
675 this.vehicle_shieldent.scale = 256 / vlen(this.maxs - this.mins);
676 setthink(this.vehicle_shieldent, shieldhit_think);
679 this.vehicle_shieldent.colormod = '1 1 1';
680 this.vehicle_shieldent.alpha = 0.45;
681 this.vehicle_shieldent.angles = vectoangles(normalize(hitloc - (this.origin + this.vehicle_shieldent.origin))) - this.angles;
682 this.vehicle_shieldent.nextthink = time;
683 this.vehicle_shieldent.effects &= ~EF_NODRAW;
685 this.vehicle_shield -= damage;
687 if(this.vehicle_shield < 0)
689 this.vehicle_health -= fabs(this.vehicle_shield);
690 this.vehicle_shieldent.colormod = '2 0 0';
691 this.vehicle_shield = 0;
692 this.vehicle_shieldent.alpha = 0.75;
694 if(sound_allowed(MSG_BROADCAST, attacker))
695 spamsound (this, CH_PAIN, SND_ONS_HIT2, VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
698 if(sound_allowed(MSG_BROADCAST, attacker))
699 spamsound (this, CH_PAIN, SND_ONS_ELECTRICITY_EXPLODE, VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
704 this.vehicle_health -= damage;
706 if(sound_allowed(MSG_BROADCAST, attacker))
707 spamsound (this, CH_PAIN, SND_ONS_HIT2, VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
710 if(this.damageforcescale < 1 && this.damageforcescale > 0)
711 this.velocity += force * this.damageforcescale;
713 this.velocity += force;
715 if(this.vehicle_health <= 0)
718 if(this.vehicle_flags & VHF_DEATHEJECT)
719 vehicles_exit(this, VHEF_EJECT);
721 vehicles_exit(this, VHEF_RELEASE);
724 antilag_clear(this, this);
726 Vehicle info = Vehicles_from(this.vehicleid);
727 info.vr_death(info, this);
728 vehicles_setreturn(this);
732 bool vehicles_crushable(entity e)
734 if(IS_PLAYER(e) && time >= e.vehicle_enter_delay)
743 void vehicles_impact(entity this, float _minspeed, float _speedfac, float _maxpain)
745 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
748 if(this.play_time < time)
750 float wc = vlen(this.velocity - this.oldvelocity);
751 //dprint("oldvel: ", vtos(this.oldvelocity), "\n");
752 //dprint("vel: ", vtos(this.velocity), "\n");
755 float take = min(_speedfac * wc, _maxpain);
756 Damage (this, NULL, NULL, take, DEATH_FALL.m_id, this.origin, '0 0 0');
757 this.play_time = time + 0.25;
759 //dprint("wc: ", ftos(wc), "\n");
760 //dprint("take: ", ftos(take), "\n");
765 // vehicle enter/exit handling
766 vector vehicles_findgoodexit(entity this, entity player, vector prefer_spot)
768 // TODO: we actually want the player's size here
769 tracebox(this.origin + '0 0 32', PL_MIN_CONST, PL_MAX_CONST, prefer_spot, MOVE_NORMAL, player);
770 if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
773 float mysize = 1.5 * vlen(this.maxs - this.mins);
775 vector v2 = 0.5 * (this.absmin + this.absmax);
776 for(int i = 0; i < autocvar_g_vehicles_exit_attempts; ++i)
780 v = v2 + normalize(v) * mysize;
781 tracebox(v2, PL_MIN_CONST, PL_MAX_CONST, v, MOVE_NORMAL, player);
782 if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
789 .int old_vehicle_flags;
790 void vehicles_exit(entity vehic, bool eject)
792 entity player = vehic.owner;
794 if(vehicles_exit_running)
796 LOG_TRACE("^1vehicles_exit already running! this is not good...");
800 vehicles_exit_running = true;
802 // TODO: this was in an IS_CLIENT check, make sure it isn't actually needed!
803 if(vehic.vehicle_flags & VHF_PLAYERSLOT)
805 vehic.vehicle_exit(vehic, eject);
806 vehicles_exit_running = false;
812 if (IS_REAL_CLIENT(player))
815 WriteByte (MSG_ONE, SVC_SETVIEWPORT);
816 WriteEntity( MSG_ONE, player);
818 WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
819 WriteAngle(MSG_ONE, 0);
820 WriteAngle(MSG_ONE, vehic.angles_y);
821 WriteAngle(MSG_ONE, 0);
824 setsize(player, STAT(PL_MIN, player), STAT(PL_MAX, player));
826 player.takedamage = DAMAGE_AIM;
827 player.solid = SOLID_SLIDEBOX;
828 set_movetype(player, MOVETYPE_WALK);
829 player.effects &= ~EF_NODRAW;
830 player.teleportable = TELEPORT_NORMAL;
832 player.PlayerPhysplug = func_null;
833 player.vehicle = NULL;
834 player.view_ofs = STAT(PL_VIEW_OFS, player);
835 player.event_damage = PlayerDamage;
836 player.hud = HUD_NORMAL;
837 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++ slot)
839 .entity weaponentity = weaponentities[slot];
840 player.(weaponentity).m_switchweapon = vehic.(weaponentity).m_switchweapon;
841 delete(vehic.(weaponentity)); // no longer needed
843 player.last_vehiclecheck = time + 3;
844 player.vehicle_enter_delay = time + 2;
846 CSQCVehicleSetup(player, HUD_NORMAL);
848 Kill_Notification(NOTIF_ONE, player, MSG_CENTER, CPID_VEHICLES);
849 Kill_Notification(NOTIF_ONE, player, MSG_CENTER, CPID_VEHICLES_OTHER); // kill all vehicle notifications when exiting a vehicle?
852 vehic.flags |= FL_NOTARGET;
855 vehic.avelocity = '0 0 0';
857 vehic.tur_head.nodrawtoclient = NULL;
862 WaypointSprite_Kill(vehic.wps_intruder);
864 MUTATOR_CALLHOOK(VehicleExit, player, vehic);
866 vehic.team = vehic.tur_head.team;
868 if(vehic.old_vehicle_flags & VHF_SHIELDREGEN)
869 vehic.vehicle_flags |= VHF_SHIELDREGEN;
870 vehic.old_vehicle_flags = 0;
872 sound (vehic, CH_TRIGGER_SINGLE, SND_Null, 1, ATTEN_NORM);
873 vehic.vehicle_hudmodel.viewmodelforclient = vehic;
874 vehic.phase = time + 1;
876 vehic.vehicle_exit(vehic, eject);
878 vehicles_setreturn(vehic);
879 vehicles_reset_colors(vehic, NULL);
882 CSQCMODEL_AUTOINIT(vehic);
885 player.oldorigin = player.origin; // player's location is set by the exit functions, so we need to do this after everything
887 vehicles_exit_running = false;
890 void vehicles_touch(entity this, entity toucher)
892 if(MUTATOR_CALLHOOK(VehicleTouch, this, toucher))
895 // Vehicle currently in use
898 if(!forbidWeaponUse(this.owner))
900 if((this.origin_z + this.maxs_z) > (toucher.origin_z))
901 if(vehicles_crushable(toucher))
903 if(vdist(this.velocity, >=, 30))
904 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);
906 return; // Dont do selfdamage when hitting "soft targets".
909 if(this.play_time < time) {
910 Vehicle info = Vehicles_from(this.vehicleid);
911 info.vr_impact(info, this);
917 if(autocvar_g_vehicles_enter)
920 vehicles_enter(toucher, this);
923 bool vehicle_impulse(entity this, int imp)
925 entity v = this.vehicle;
926 if (!v) return false;
927 if (IS_DEAD(v)) return false;
928 bool(entity,int) f = v.vehicles_impulse;
929 if (f && f(this,imp)) return true;
932 case IMP_weapon_drop.impulse:
934 stuffcmd(this, "\ntoggle cl_eventchase_vehicle\nset _vehicles_shownchasemessage 1\n");
941 void vehicles_enter(entity pl, entity veh)
943 // Remove this when bots know how to use vehicles
944 if((IS_BOT_CLIENT(pl) && !autocvar_g_vehicles_allow_bots))
948 || (veh.phase >= time)
949 || (pl.vehicle_enter_delay >= time)
950 || (STAT(FROZEN, pl))
955 Vehicle info = Vehicles_from(veh.vehicleid);
957 if(autocvar_g_vehicles_enter) // vehicle's touch function should handle this if entering via use key is disabled (TODO)
958 if(veh.vehicle_flags & VHF_MULTISLOT)
959 if(veh.owner && SAME_TEAM(pl, veh))
961 // we don't need a return value or anything here
962 // if successful the owner check below will prevent anything weird
963 info.vr_gunner_enter(info, veh, pl);
967 return; // got here and didn't enter the gunner, return
971 if(DIFF_TEAM(pl, veh))
972 if(autocvar_g_vehicles_steal)
974 FOREACH_CLIENT(IS_PLAYER(it) && SAME_TEAM(it, veh), Send_Notification(NOTIF_ONE, it, MSG_CENTER, CENTER_VEHICLE_STEAL));
976 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_VEHICLE_STEAL_SELF);
978 veh.vehicle_shield = 0;
979 veh.old_vehicle_flags = veh.vehicle_flags; // make a backup just so we're not permanently crippling this vehicle
980 veh.vehicle_flags &= ~VHF_SHIELDREGEN;
982 if (autocvar_g_vehicles_steal_show_waypoint) {
983 entity wp = WaypointSprite_Spawn(WP_VehicleIntruder, 0, 0, pl, '0 0 68', NULL, veh.team, veh, wps_intruder, true, RADARICON_DANGER);
984 wp.colormod = Team_ColorRGB(pl.team);
989 RemoveGrapplingHooks(pl);
991 veh.vehicle_ammo1 = 0;
992 veh.vehicle_ammo2 = 0;
993 veh.vehicle_reload1 = 0;
994 veh.vehicle_reload2 = 0;
995 veh.vehicle_energy = 0;
1000 // .viewmodelforclient works better.
1001 //veh.vehicle_hudmodel.drawonlytoclient = veh.owner;
1003 veh.vehicle_hudmodel.viewmodelforclient = pl;
1006 pl.view_ofs = STAT(PL_VIEW_OFS, pl);
1007 setsize(pl, STAT(PL_MIN, pl), STAT(PL_MAX, pl));
1009 veh.event_damage = vehicles_damage;
1011 pl.items &= ~IT_USING_JETPACK;
1012 pl.angles = veh.angles;
1013 pl.takedamage = DAMAGE_NO;
1014 pl.solid = SOLID_NOT;
1015 pl.disableclientprediction = 1; // physics is no longer run, so this won't be reset
1016 set_movetype(pl, MOVETYPE_NOCLIP);
1017 pl.teleportable = false;
1019 pl.event_damage = func_null;
1020 pl.view_ofs = '0 0 0';
1021 veh.colormap = pl.colormap;
1023 veh.tur_head.colormap = pl.colormap;
1024 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
1026 .entity weaponentity = weaponentities[slot];
1027 veh.(weaponentity) = new(temp_wepent);
1028 veh.(weaponentity).m_switchweapon = pl.(weaponentity).m_switchweapon;
1030 pl.hud = veh.vehicleid;
1031 pl.PlayerPhysplug = veh.PlayerPhysplug;
1033 pl.vehicle_ammo1 = veh.vehicle_ammo1;
1034 pl.vehicle_ammo2 = veh.vehicle_ammo2;
1035 pl.vehicle_reload1 = veh.vehicle_reload1;
1036 pl.vehicle_reload2 = veh.vehicle_reload2;
1037 pl.vehicle_energy = veh.vehicle_energy;
1039 // Cant do this, hides attached objects too.
1040 //veh.exteriormodeltoclient = veh.owner;
1041 //veh.tur_head.exteriormodeltoclient = veh.owner;
1044 UNSET_ONGROUND(veh);
1047 veh.flags -= FL_NOTARGET;
1049 vehicles_reset_colors(veh, pl);
1051 if (IS_REAL_CLIENT(pl))
1053 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_VEHICLE_ENTER);
1056 WriteByte (MSG_ONE, SVC_SETVIEWPORT);
1057 WriteEntity(MSG_ONE, veh.vehicle_viewport);
1059 WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
1062 WriteAngle(MSG_ONE, veh.tur_head.angles_x + veh.angles_x); // tilt
1063 WriteAngle(MSG_ONE, veh.tur_head.angles_y + veh.angles_y); // yaw
1064 WriteAngle(MSG_ONE, 0); // roll
1068 WriteAngle(MSG_ONE, veh.angles_x * -1); // tilt
1069 WriteAngle(MSG_ONE, veh.angles_y); // yaw
1070 WriteAngle(MSG_ONE, 0); // roll
1074 vehicles_clearreturn(veh);
1076 CSQCVehicleSetup(pl, veh.vehicleid);
1078 MUTATOR_CALLHOOK(VehicleEnter, pl, veh);
1080 CSQCModel_UnlinkEntity(veh);
1081 info.vr_enter(info, veh);
1083 antilag_clear(pl, CS(pl));
1086 void vehicles_think(entity this)
1088 this.nextthink = time + autocvar_g_vehicles_thinkrate;
1091 this.owner.vehicle_weapon2mode = this.vehicle_weapon2mode;
1093 Vehicle info = Vehicles_from(this.vehicleid);
1094 info.vr_think(info, this);
1096 vehicles_painframe(this);
1098 CSQCMODEL_AUTOUPDATE(this);
1101 void vehicles_reset(entity this)
1104 vehicles_exit(this, VHEF_RELEASE);
1106 vehicles_clearreturn(this);
1108 if(this.active != ACTIVE_NOT)
1109 vehicles_spawn(this);
1113 void vehicles_spawn(entity this)
1115 LOG_DEBUG("Spawning vehicle: ", this.classname);
1118 this.vehicle_hudmodel.viewmodelforclient = this;
1121 settouch(this, vehicles_touch);
1122 this.event_damage = vehicles_damage;
1123 this.reset = vehicles_reset;
1124 this.iscreature = true;
1125 this.teleportable = false; // no teleporting for vehicles, too buggy
1126 this.damagedbycontents = true;
1127 set_movetype(this, MOVETYPE_WALK);
1128 this.solid = SOLID_SLIDEBOX;
1129 this.takedamage = DAMAGE_AIM;
1130 this.deadflag = DEAD_NO;
1131 if(!this.bot_attack)
1132 IL_PUSH(g_bot_targets, this);
1133 this.bot_attack = true;
1134 this.flags = FL_NOTARGET;
1135 this.avelocity = '0 0 0';
1136 this.velocity = '0 0 0';
1137 setthink(this, vehicles_think);
1138 this.nextthink = time;
1141 this.lock_strength = 0;
1142 this.lock_target = NULL;
1143 this.misc_bulletcounter = 0;
1146 this.angles = this.pos2;
1147 setorigin(this, this.pos1);
1149 Send_Effect(EFFECT_TELEPORT, this.origin + '0 0 64', '0 0 0', 1);
1151 if(this.vehicle_controller)
1152 this.team = this.vehicle_controller.team;
1154 FOREACH_CLIENT(IS_PLAYER(it),
1156 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
1158 .entity weaponentity = weaponentities[slot];
1159 if(it.(weaponentity).hook.aiment == this)
1160 RemoveHook(it.(weaponentity).hook);
1165 Vehicle info = Vehicles_from(this.vehicleid);
1166 info.vr_spawn(info, this);
1168 vehicles_reset_colors(this, NULL);
1170 CSQCMODEL_AUTOINIT(this);
1173 bool vehicle_initialize(entity this, Vehicle info, bool nodrop)
1175 if(!autocvar_g_vehicles)
1183 info.vr_precache(info);
1184 IL_PUSH(g_vehicles, this);
1187 if(this.targetname && this.targetname != "")
1189 this.vehicle_controller = find(NULL, target, this.targetname);
1190 if(!this.vehicle_controller)
1192 LOG_DEBUG("^1WARNING: ^7Vehicle with invalid .targetname");
1193 this.active = ACTIVE_ACTIVE;
1197 this.team = this.vehicle_controller.team;
1198 this.use = vehicle_use;
1202 if(this.vehicle_controller.team == 0)
1203 this.active = ACTIVE_NOT;
1205 this.active = ACTIVE_ACTIVE;
1209 else { this.active = ACTIVE_ACTIVE; }
1211 if(this.team && (!teamplay || !autocvar_g_vehicles_teams))
1214 if(this.mdl == "" || !this.mdl)
1215 _setmodel(this, info.model);
1217 _setmodel(this, this.mdl);
1219 this.vehicle_flags |= VHF_ISVEHICLE;
1221 this.vehicle_viewport = new(vehicle_viewport);
1222 this.vehicle_hudmodel = new(vehicle_hudmodel);
1223 this.tur_head = new(tur_head);
1224 this.tur_head.owner = this;
1225 this.takedamage = DAMAGE_NO;
1226 this.bot_attack = true;
1227 IL_PUSH(g_bot_targets, this);
1228 this.iscreature = true;
1229 this.teleportable = false; // no teleporting for vehicles, too buggy
1230 this.damagedbycontents = true;
1231 IL_PUSH(g_damagedbycontents, this);
1232 this.vehicleid = info.vehicleid;
1233 this.PlayerPhysplug = info.PlayerPhysplug;
1234 this.event_damage = func_null;
1235 settouch(this, vehicles_touch);
1236 setthink(this, vehicles_spawn);
1237 this.nextthink = time;
1238 this.effects = EF_NODRAW;
1239 this.dphitcontentsmask = DPCONTENTS_BODY | DPCONTENTS_SOLID;
1241 if(autocvar_g_playerclip_collisions)
1242 this.dphitcontentsmask |= DPCONTENTS_PLAYERCLIP;
1244 if(autocvar_g_nodepthtestplayers)
1245 this.effects |= EF_NODEPTHTEST;
1247 if(autocvar_g_fullbrightplayers)
1248 this.effects |= EF_FULLBRIGHT;
1250 _setmodel(this.vehicle_hudmodel, info.hud_model);
1251 setmodel(this.vehicle_viewport, MDL_Null);
1253 if(info.head_model != "")
1255 _setmodel(this.tur_head, info.head_model);
1256 setattachment(this.tur_head, this, info.tag_head);
1257 setattachment(this.vehicle_hudmodel, this.tur_head, info.tag_hud);
1258 setattachment(this.vehicle_viewport, this.vehicle_hudmodel, info.tag_view);
1262 setattachment(this.tur_head, this, "");
1263 setattachment(this.vehicle_hudmodel, this, info.tag_hud);
1264 setattachment(this.vehicle_viewport, this.vehicle_hudmodel, info.tag_view);
1267 setsize(this, info.m_mins, info.m_maxs);
1269 info.vr_setup(info, this);
1273 setorigin(this, this.origin);
1274 tracebox(this.origin + '0 0 100', info.m_mins, info.m_maxs, this.origin - '0 0 10000', MOVE_WORLDONLY, this);
1275 setorigin(this, trace_endpos);
1278 this.pos1 = this.origin;
1279 this.pos2 = this.angles;
1280 this.tur_head.team = this.team;
1282 if(this.active == ACTIVE_NOT)
1283 this.nextthink = 0; // wait until activated
1284 else if(autocvar_g_vehicles_delayspawn)
1285 this.nextthink = time + this.respawntime + (random() * autocvar_g_vehicles_delayspawn_jitter);
1287 this.nextthink = time + game_starttime;
1289 if(MUTATOR_CALLHOOK(VehicleInit, this))