1 #include "sv_vehicles.qh"
3 #include <common/items/_mod.qh>
4 #include <common/mapobjects/defs.qh>
5 #include <common/mapobjects/teleporters.qh>
6 #include <common/mapobjects/triggers.qh>
7 #include <server/bot/api.qh>
8 #include <server/damage.qh>
9 #include <server/items/items.qh>
10 #include <server/weapons/common.qh>
11 #include <server/world.qh>
13 bool SendAuxiliaryXhair(entity this, entity to, int sf)
15 WriteHeader(MSG_ENTITY, ENT_CLIENT_AUXILIARYXHAIR);
16 WriteByte(MSG_ENTITY, sf);
18 WriteByte(MSG_ENTITY, this.cnt);
22 WriteVector(MSG_ENTITY, this.origin);
27 WriteByte(MSG_ENTITY, rint(this.colormod_x * 255));
28 WriteByte(MSG_ENTITY, rint(this.colormod_y * 255));
29 WriteByte(MSG_ENTITY, rint(this.colormod_z * 255));
35 bool AuxiliaryXhair_customize(entity this, entity client)
37 entity e = WaypointSprite_getviewentity(client);
38 entity axh = e.(AuxiliaryXhair[this.cnt]);
39 return axh.owner == this.owner; // cheaply check if the client's axh owner is the same as our real owner
42 .vector axh_prevorigin;
43 .vector axh_prevcolors;
45 void UpdateAuxiliaryXhair(entity own, vector loc, vector clr, int axh_id)
47 if(!IS_REAL_CLIENT(own))
50 axh_id = bound(0, axh_id, MAX_AXH);
51 entity axh = own.(AuxiliaryXhair[axh_id]);
53 if(axh == NULL || wasfreed(axh)) // MADNESS? THIS IS QQQQCCCCCCCCC (wasfreed, why do you exsist? Mario: because of sloppy code like this)
55 axh = new(auxiliary_xhair);
57 //axh.drawonlytoclient = own; // not spectatable
58 setcefc(axh, AuxiliaryXhair_customize);
60 Net_LinkEntity(axh, false, 0, SendAuxiliaryXhair);
63 if(loc != axh.axh_prevorigin)
69 if(clr != axh.axh_prevcolors)
75 own.(AuxiliaryXhair[axh_id]) = axh; // set it anyway...?
78 void CSQCVehicleSetup(entity own, int vehicle_id)
80 if(!IS_REAL_CLIENT(own))
85 WriteHeader(MSG_ONE, TE_CSQC_VEHICLESETUP);
86 WriteByte(MSG_ONE, vehicle_id);
88 if(vehicle_id == 0 || vehicle_id == HUD_NORMAL)
90 for(int i = 0; i < MAX_AXH; ++i)
92 entity axh = own.(AuxiliaryXhair[i]);
93 own.(AuxiliaryXhair[i]) = NULL;
95 if(axh.owner == own && axh != NULL && !wasfreed(axh))
101 void vehicles_locktarget(entity this, float incr, float decr, float _lock_time)
103 if(this.lock_target && IS_DEAD(this.lock_target))
105 this.lock_target = NULL;
106 this.lock_strength = 0;
110 if(this.lock_time > time)
113 if(this.lock_soundtime < time)
115 this.lock_soundtime = time + 0.5;
116 play2(this.owner, "vehicles/locked.wav");
122 if(trace_ent != NULL)
124 if(SAME_TEAM(trace_ent, this))
127 if(IS_DEAD(trace_ent))
130 if(!(IS_VEHICLE(trace_ent) || IS_TURRET(trace_ent)))
133 if(trace_ent.alpha <= 0.5 && trace_ent.alpha != 0)
134 trace_ent = NULL; // invisible
137 if(this.lock_target == NULL && trace_ent != NULL)
138 this.lock_target = trace_ent;
140 if(this.lock_target && trace_ent == this.lock_target)
142 if(this.lock_strength != 1 && this.lock_strength + incr >= 1)
144 play2(this.owner, "vehicles/lock.wav");
145 this.lock_soundtime = time + 0.8;
147 else if (this.lock_strength != 1 && this.lock_soundtime < time)
149 play2(this.owner, "vehicles/locking.wav");
150 this.lock_soundtime = time + 0.3;
154 // Have a locking target
155 // Trace hit current target
156 if(trace_ent == this.lock_target && trace_ent != NULL)
158 this.lock_strength = min(this.lock_strength + incr, 1);
159 if(this.lock_strength == 1)
160 this.lock_time = time + _lock_time;
165 this.lock_strength = max(this.lock_strength - decr * 2, 0);
167 this.lock_strength = max(this.lock_strength - decr, 0);
169 if(this.lock_strength == 0)
170 this.lock_target = NULL;
174 float vehicle_altitude(entity this, float amax)
176 tracebox(this.origin, this.mins, this.maxs, this.origin - ('0 0 1' * amax), MOVE_WORLDONLY, this);
177 return vlen(this.origin - trace_endpos);
180 vector vehicles_force_fromtag_hover(entity this, string tag_name, float spring_length, float max_power)
182 force_fromtag_origin = gettaginfo(this, gettagindex(this, tag_name));
183 v_forward = normalize(v_forward) * -1;
184 traceline(force_fromtag_origin, force_fromtag_origin - (v_forward * spring_length), MOVE_NORMAL, this);
186 force_fromtag_power = (1 - trace_fraction) * max_power;
187 force_fromtag_normpower = force_fromtag_power / max_power;
189 return v_forward * force_fromtag_power;
192 vector vehicles_force_fromtag_maglev(entity this, string tag_name, float spring_length, float max_power)
194 force_fromtag_origin = gettaginfo(this, gettagindex(this, tag_name));
195 v_forward = normalize(v_forward) * -1;
196 traceline(force_fromtag_origin, force_fromtag_origin - (v_forward * spring_length), MOVE_NORMAL, this);
198 // TODO - this may NOT be compatible with wall/celing movement, unhardcode 0.25 (engine count multiplier)
199 if(trace_fraction == 1.0)
201 force_fromtag_normpower = -0.25;
205 force_fromtag_power = ((1 - trace_fraction) - trace_fraction) * max_power;
206 force_fromtag_normpower = force_fromtag_power / max_power;
208 return v_forward * force_fromtag_power;
211 // projectile handling
212 void vehicles_projectile_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
214 // Ignore damage from oterh projectiles from my owner (dont mess up volly's)
215 if(inflictor.owner == this.owner)
218 TakeResource(this, RES_HEALTH, damage);
219 this.velocity += force;
220 if(GetResource(this, RES_HEALTH) < 1)
222 this.takedamage = DAMAGE_NO;
223 this.event_damage = func_null;
224 setthink(this, adaptor_think2use);
225 this.nextthink = time;
229 void vehicles_projectile_explode(entity this, entity toucher)
231 if(this.owner && toucher != NULL)
233 if(toucher == this.owner.vehicle)
236 if(toucher == this.owner.vehicle.tur_head)
240 PROJECTILE_TOUCH(this, toucher);
242 this.event_damage = func_null;
243 RadiusDamage (this, this.realowner, this.shot_dmg, 0, this.shot_radius, this, NULL, this.shot_force, this.projectiledeathtype, DMG_NOWEP, toucher);
248 void vehicles_projectile_explode_think(entity this)
250 vehicles_projectile_explode(this, NULL);
253 void vehicles_projectile_explode_use(entity this, entity actor, entity trigger)
255 vehicles_projectile_explode(this, trigger);
258 entity vehicles_projectile(entity this, entity _mzlfx, Sound _mzlsound,
259 vector _org, vector _vel,
260 float _dmg, float _radi, float _force, float _size,
261 int _deahtype, float _projtype, float _health,
262 bool _cull, bool _clianim, entity _owner)
264 TC(Sound, _mzlsound);
265 entity proj = new(vehicles_projectile);
267 PROJECTILE_MAKETRIGGER(proj);
268 setorigin(proj, _org);
270 proj.shot_dmg = _dmg;
271 proj.shot_radius = _radi;
272 proj.shot_force = _force;
273 proj.projectiledeathtype = _deahtype;
274 proj.solid = SOLID_BBOX;
275 set_movetype(proj, MOVETYPE_FLYMISSILE);
276 proj.flags = FL_PROJECTILE;
277 IL_PUSH(g_projectiles, proj);
278 IL_PUSH(g_bot_dodge, proj);
279 proj.bot_dodge = true;
280 proj.bot_dodgerating = _dmg;
281 proj.velocity = _vel;
282 settouch(proj, vehicles_projectile_explode);
283 proj.use = vehicles_projectile_explode_use;
285 proj.realowner = _owner;
286 setthink(proj, SUB_Remove);
287 proj.nextthink = time + 30;
291 proj.takedamage = DAMAGE_AIM;
292 proj.event_damage = vehicles_projectile_damage;
293 SetResourceExplicit(proj, RES_HEALTH, _health);
296 proj.flags |= FL_NOTARGET;
298 if(_mzlsound != SND_Null)
299 sound (this, CH_WEAPON_A, _mzlsound, VOL_BASE, ATTEN_NORM);
301 if(_mzlfx != EFFECT_Null)
302 Send_Effect(_mzlfx, proj.origin, proj.velocity, 1);
304 setsize (proj, '-1 -1 -1' * _size, '1 1 1' * _size);
306 CSQCProjectile(proj, _clianim, _projtype, _cull);
311 void vehicles_gib_explode(entity this)
313 sound (this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
314 Send_Effect(EFFECT_EXPLOSION_SMALL, randomvec() * 80 + (this.origin + '0 0 100'), '0 0 0', 1);
315 Send_Effect(EFFECT_EXPLOSION_SMALL, this.wp00.origin + '0 0 64', '0 0 0', 1);
319 void vehicles_gib_touch(entity this, entity toucher)
321 vehicles_gib_explode(this);
324 void vehicles_gib_think(entity this)
330 this.nextthink = time + 0.1;
333 entity vehicle_tossgib(entity this, entity _template, vector _vel, string _tag, bool _burn, bool _explode, float _maxtime, vector _rot)
335 entity _gib = new(vehicle_gib);
336 _setmodel(_gib, _template.model);
337 vector org = gettaginfo(this, gettagindex(this, _tag));
338 setorigin(_gib, org);
339 _gib.velocity = _vel;
340 set_movetype(_gib, MOVETYPE_TOSS);
341 _gib.solid = SOLID_CORPSE;
342 _gib.colormod = '-0.5 -0.5 -0.5';
343 _gib.effects = EF_LOWPRECISION;
344 _gib.avelocity = _rot;
347 _gib.effects |= EF_FLAME;
351 setthink(_gib, vehicles_gib_explode);
352 _gib.nextthink = time + random() * _explode;
353 settouch(_gib, vehicles_gib_touch);
357 _gib.cnt = time + _maxtime;
358 setthink(_gib, vehicles_gib_think);
359 _gib.nextthink = time + _maxtime - 1;
365 bool vehicle_addplayerslot( entity _owner,
369 bool(entity,float) _framefunc,
370 void(entity,bool) _exitfunc, float(entity, entity) _enterfunc)
372 if(!(_owner.vehicle_flags & VHF_MULTISLOT))
373 _owner.vehicle_flags |= VHF_MULTISLOT;
375 _slot.PlayerPhysplug = _framefunc;
376 _slot.vehicle_exit = _exitfunc;
377 _slot.vehicle_enter = _enterfunc;
378 STAT(HUD, _slot) = _hud;
379 _slot.vehicle_flags = VHF_PLAYERSLOT;
380 _slot.vehicle_viewport = new(vehicle_viewport);
381 _slot.vehicle_hudmodel = new(vehicle_hudmodel);
382 _slot.vehicle_hudmodel.viewmodelforclient = _slot;
383 _slot.vehicle_viewport.effects = (EF_ADDITIVE | EF_DOUBLESIDED | EF_FULLBRIGHT | EF_NODEPTHTEST | EF_NOGUNBOB | EF_NOSHADOW | EF_LOWPRECISION | EF_SELECTABLE | EF_TELEPORT_BIT);
385 setmodel(_slot.vehicle_hudmodel, _hud_model);
386 setmodel(_slot.vehicle_viewport, MDL_Null);
388 setattachment(_slot.vehicle_hudmodel, _slot, "");
389 setattachment(_slot.vehicle_viewport, _slot.vehicle_hudmodel, "");
394 vector vehicle_aimturret(entity _vehic, vector _target, entity _turrret, string _tagname,
395 float _pichlimit_min, float _pichlimit_max,
396 float _rotlimit_min, float _rotlimit_max, float _aimspeed, float dt)
400 vtag = gettaginfo(_turrret, gettagindex(_turrret, _tagname));
401 vtmp = vectoangles(normalize(_target - vtag));
402 vtmp = AnglesTransform_ToAngles(AnglesTransform_LeftDivide(AnglesTransform_FromAngles(_vehic.angles), AnglesTransform_FromAngles(vtmp))) - _turrret.angles;
403 vtmp = AnglesTransform_Normalize(vtmp, true);
404 ftmp = _aimspeed * dt;
405 vtmp_y = bound(-ftmp, vtmp_y, ftmp);
406 vtmp_x = bound(-ftmp, vtmp_x, ftmp);
407 _turrret.angles_y = bound(_rotlimit_min, _turrret.angles_y + vtmp_y, _rotlimit_max);
408 _turrret.angles_x = bound(_pichlimit_min, _turrret.angles_x + vtmp_x, _pichlimit_max);
412 void vehicles_reset_colors(entity this, entity player)
415 const vector cmod = '0 0 0';
416 if(this.team && teamplay)
417 cmap = 1024 + (this.team - 1) * 17;
419 cmap = player.colormap;
422 if(autocvar_g_nodepthtestplayers)
423 eff |= EF_NODEPTHTEST;
424 if(autocvar_g_fullbrightplayers)
425 eff |= EF_FULLBRIGHT;
427 // Find all ents attacked to main model and setup effects, colormod etc.
428 FOREACH_ENTITY_ENT(tag_entity, this,
430 if(it == this.vehicle_shieldent)
439 // Also check head tags
440 FOREACH_ENTITY_ENT(tag_entity, this.tur_head,
442 if(it == this.vehicle_shieldent)
451 this.vehicle_hudmodel.effects = this.effects = eff; // | EF_LOWPRECISION;
452 this.vehicle_hudmodel.colormod = this.colormod = cmod;
453 this.vehicle_hudmodel.colormap = this.colormap = cmap;
454 this.vehicle_viewport.effects = (EF_ADDITIVE | EF_DOUBLESIDED | EF_FULLBRIGHT | EF_NODEPTHTEST | EF_NOGUNBOB | EF_NOSHADOW | EF_LOWPRECISION | EF_SELECTABLE | EF_TELEPORT_BIT);
457 this.avelocity = '0 0 0';
458 this.velocity = '0 0 0';
461 Vehicle info = this.vehicledef; //REGISTRY_GET(Vehicles, this.vehicleid);
462 info.vr_setcolors(info, this);
465 void vehicles_clearreturn(entity veh)
467 // Remove "return helper" entities, if any.
468 IL_EACH(g_vehicle_returners, it.wp00 == veh,
471 setthink(it, SUB_Remove);
472 it.nextthink = time + 0.1;
473 IL_REMOVE(g_vehicle_returners, it);
475 if(it.waypointsprite_attached)
476 WaypointSprite_Kill(it.waypointsprite_attached);
480 void vehicles_spawn(entity this);
481 void vehicles_return(entity this)
483 Send_Effect(EFFECT_TELEPORT, this.wp00.origin + '0 0 64', '0 0 0', 1);
485 setthink(this.wp00, vehicles_spawn);
486 this.wp00.nextthink = time;
488 if(this.waypointsprite_attached)
489 WaypointSprite_Kill(this.waypointsprite_attached);
494 void vehicles_showwp_goaway(entity this)
496 if(this.waypointsprite_attached)
497 WaypointSprite_Kill(this.waypointsprite_attached);
502 void vehicles_showwp(entity this)
508 setthink(ent, vehicles_return);
509 ent.nextthink = ent.cnt;
513 setthink(ent, vehicles_return);
514 ent.nextthink = time + 1;
517 ent.team = this.wp00.team;
518 ent.wp00 = this.wp00;
519 setorigin(ent, this.wp00.pos1);
521 ent.nextthink = time + 5;
522 setthink(ent, vehicles_showwp_goaway);
526 if(teamplay && ent.team)
527 rgb = Team_ColorRGB(ent.team);
530 entity wp = WaypointSprite_Spawn(WP_Vehicle, 0, 0, ent, '0 0 64', NULL, 0, ent, waypointsprite_attached, true, RADARICON_Vehicle);
531 wp.wp_extra = ent.wp00.vehicleid;
533 if(ent.waypointsprite_attached)
535 WaypointSprite_UpdateRule(ent.waypointsprite_attached, ent.wp00.team, SPRITERULE_DEFAULT);
537 WaypointSprite_UpdateBuildFinished(ent.waypointsprite_attached, ent.nextthink);
538 WaypointSprite_Ping(ent.waypointsprite_attached);
542 void vehicles_setreturn(entity veh)
544 vehicles_clearreturn(veh);
546 entity ret = new(vehicle_return);
547 IL_PUSH(g_vehicle_returners, ret);
550 setthink(ret, vehicles_showwp);
554 ret.cnt = time + veh.respawntime;
555 ret.nextthink = min(time + veh.respawntime, time + veh.respawntime - 5);
559 ret.nextthink = min(time + veh.respawntime, time + veh.respawntime - 1);
562 setorigin(ret, veh.pos1 + '0 0 96');
565 void vehicle_use(entity this, entity actor, entity trigger)
567 LOG_DEBUG("vehicle ", this.netname, " used by ", actor.classname);
569 this.tur_head.team = actor.team;
571 if(this.tur_head.team == 0)
572 this.active = ACTIVE_NOT;
574 this.active = ACTIVE_ACTIVE;
576 if(this.active == ACTIVE_ACTIVE && !IS_DEAD(this) && !game_stopped)
578 LOG_DEBUG("Respawning vehicle: ", this.netname);
579 if(this.effects & EF_NODRAW)
581 setthink(this, vehicles_spawn);
582 this.nextthink = time + 3;
586 vehicles_setreturn(this);
587 vehicles_reset_colors(this, actor);
592 void vehicles_regen(entity this, float timer, .float regen_field, float field_max, float rpause, float regen, float delta_time, float _healthscale)
594 if(this.(regen_field) < field_max)
595 if(timer + rpause < time)
598 regen = regen * (GetResource(this, RES_HEALTH) / this.max_health);
600 this.(regen_field) = min(this.(regen_field) + regen * delta_time, field_max);
603 this.owner.(regen_field) = (this.(regen_field) / field_max) * 100;
607 void vehicles_regen_resource(entity this, float timer, .float regen_field, float field_max, float rpause, float regen, float delta_time, float _healthscale, int resource)
609 float resource_amount = GetResource(this, resource);
611 if(resource_amount < field_max)
612 if(timer + rpause < time)
615 regen = regen * (resource_amount / this.max_health);
617 SetResource(this, resource, min(resource_amount + regen * delta_time, field_max));
620 this.owner.(regen_field) = (GetResource(this, resource) / field_max) * 100;
624 void shieldhit_think(entity this)
629 // setmodel(this, MDL_Null);
631 this.effects |= EF_NODRAW;
635 this.nextthink = time + 0.1;
639 void vehicles_painframe(entity this)
641 int myhealth = ((this.owner) ? this.owner.vehicle_health : ((GetResource(this, RES_HEALTH) / this.max_health) * 100));
644 if(this.pain_frame < time)
646 float _ftmp = myhealth / 50;
647 this.pain_frame = time + max(0.1, 0.1 + (random() * 0.5 * _ftmp));
648 Send_Effect(EFFECT_SMOKE_SMALL, (this.origin + (randomvec() * 80)), '0 0 0', 1);
650 if(this.vehicle_flags & VHF_DMGSHAKE)
651 this.velocity += randomvec() * 30;
653 if(this.vehicle_flags & VHF_DMGROLL)
655 if(this.vehicle_flags & VHF_DMGHEADROLL)
656 this.tur_head.angles += randomvec();
658 this.angles += randomvec();
663 void vehicles_frame(entity this, entity actor)
665 vehicles_painframe(this);
668 void vehicles_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
670 this.dmg_time = time;
673 if(DEATH_ISWEAPON(deathtype, WEP_VORTEX))
674 damage *= autocvar_g_vehicles_vortex_damagerate;
675 else if(DEATH_ISWEAPON(deathtype, WEP_MACHINEGUN))
676 damage *= autocvar_g_vehicles_machinegun_damagerate;
677 else if(DEATH_ISWEAPON(deathtype, WEP_RIFLE))
678 damage *= autocvar_g_vehicles_rifle_damagerate;
679 else if(DEATH_ISWEAPON(deathtype, WEP_VAPORIZER))
680 damage *= autocvar_g_vehicles_vaporizer_damagerate;
681 else if(DEATH_ISWEAPON(deathtype, WEP_SEEKER))
682 damage *= autocvar_g_vehicles_tag_damagerate;
683 else if(DEATH_WEAPONOF(deathtype) != WEP_Null)
684 damage *= autocvar_g_vehicles_weapon_damagerate;
686 this.enemy = attacker;
688 this.pain_finished = time;
690 if((this.vehicle_flags & VHF_HASSHIELD) && (this.vehicle_shield > 0))
692 if (wasfreed(this.vehicle_shieldent) || this.vehicle_shieldent == NULL)
694 this.vehicle_shieldent = new(vehicle_shieldent);
695 this.vehicle_shieldent.effects = EF_LOWPRECISION;
697 setmodel(this.vehicle_shieldent, MDL_VEH_SHIELD);
698 setattachment(this.vehicle_shieldent, this, "");
699 setorigin(this.vehicle_shieldent, real_origin(this) - this.origin);
700 this.vehicle_shieldent.scale = 256 / vlen(this.maxs - this.mins);
701 setthink(this.vehicle_shieldent, shieldhit_think);
704 this.vehicle_shieldent.colormod = '1 1 1';
705 this.vehicle_shieldent.alpha = 0.45;
706 this.vehicle_shieldent.angles = vectoangles(normalize(hitloc - (this.origin + this.vehicle_shieldent.origin))) - this.angles;
707 this.vehicle_shieldent.nextthink = time;
708 this.vehicle_shieldent.effects &= ~EF_NODRAW;
710 this.vehicle_shield -= damage;
712 if(this.vehicle_shield < 0)
714 TakeResource(this, RES_HEALTH, fabs(this.vehicle_shield));
715 this.vehicle_shieldent.colormod = '2 0 0';
716 this.vehicle_shield = 0;
717 this.vehicle_shieldent.alpha = 0.75;
719 if(sound_allowed(MSG_BROADCAST, attacker))
720 spamsound (this, CH_PAIN, SND_ONS_HIT2, VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
723 if(sound_allowed(MSG_BROADCAST, attacker))
724 spamsound (this, CH_PAIN, SND_ONS_ELECTRICITY_EXPLODE, VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
729 TakeResource(this, RES_HEALTH, damage);
731 if(sound_allowed(MSG_BROADCAST, attacker))
732 spamsound (this, CH_PAIN, SND_ONS_HIT2, VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
735 if(this.damageforcescale < 1 && this.damageforcescale > 0)
736 this.velocity += force * this.damageforcescale;
738 this.velocity += force;
740 if(GetResource(this, RES_HEALTH) <= 0)
744 if(this.vehicle_flags & VHF_DEATHEJECT)
745 vehicles_exit(this, VHEF_EJECT);
747 vehicles_exit(this, VHEF_RELEASE);
750 antilag_clear(this, this);
752 Vehicle info = this.vehicledef; //REGISTRY_GET(Vehicles, this.vehicleid);
753 info.vr_death(info, this);
754 vehicles_setreturn(this);
758 bool vehicles_heal(entity targ, entity inflictor, float amount, float limit)
760 float true_limit = ((limit != RES_LIMIT_NONE) ? limit : targ.max_health);
761 if(GetResource(targ, RES_HEALTH) <= 0 || GetResource(targ, RES_HEALTH) >= true_limit)
764 GiveResourceWithLimit(targ, RES_HEALTH, amount, true_limit);
766 targ.owner.vehicle_health = (GetResource(targ, RES_HEALTH) / targ.max_health) * 100;
770 bool vehicles_crushable(entity e)
772 if(IS_PLAYER(e) && time >= e.vehicle_enter_delay)
781 void vehicles_impact(entity this, float _minspeed, float _speedfac, float _maxpain)
783 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
786 if(this.play_time < time)
788 if(vdist(this.velocity - this.oldvelocity, >, _minspeed))
790 float wc = vlen(this.velocity - this.oldvelocity);
791 float take = min(_speedfac * wc, _maxpain);
792 Damage(this, NULL, NULL, take, DEATH_FALL.m_id, DMG_NOWEP, this.origin, '0 0 0');
793 this.play_time = time + 0.25;
798 // vehicle enter/exit handling
799 vector vehicles_findgoodexit(entity this, entity player, vector prefer_spot)
801 // TODO: we actually want the player's size here
802 tracebox(this.origin + '0 0 32', PL_MIN_CONST, PL_MAX_CONST, prefer_spot, MOVE_NORMAL, player);
803 if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
806 float mysize = 1.5 * vlen(this.maxs - this.mins);
808 vector v2 = 0.5 * (this.absmin + this.absmax);
809 for(int i = 0; i < autocvar_g_vehicles_exit_attempts; ++i)
813 v = v2 + normalize(v) * mysize;
814 tracebox(v2, PL_MIN_CONST, PL_MAX_CONST, v, MOVE_NORMAL, player);
815 if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
822 .int old_vehicle_flags;
823 void vehicles_exit(entity vehic, bool eject)
825 entity player = vehic.owner;
827 if(vehicles_exit_running)
829 LOG_TRACE("^1vehicles_exit already running! this is not good...");
833 vehicles_exit_running = true;
835 if(vehic.vehicle_flags & VHF_PLAYERSLOT)
837 vehic.vehicle_exit(vehic, eject);
838 vehicles_exit_running = false;
844 if (IS_REAL_CLIENT(player))
847 WriteByte (MSG_ONE, SVC_SETVIEWPORT);
848 WriteEntity( MSG_ONE, player);
850 // NOTE: engine networked
851 WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
852 WriteAngle(MSG_ONE, 0);
853 WriteAngle(MSG_ONE, vehic.angles_y);
854 WriteAngle(MSG_ONE, 0);
857 setsize(player, STAT(PL_MIN, player), STAT(PL_MAX, player));
859 player.takedamage = DAMAGE_AIM;
860 player.solid = SOLID_SLIDEBOX;
861 set_movetype(player, MOVETYPE_WALK);
862 player.effects &= ~EF_NODRAW;
863 player.teleportable = TELEPORT_NORMAL;
864 player.alpha = default_player_alpha;
865 player.PlayerPhysplug = func_null;
866 player.vehicle = NULL;
867 player.view_ofs = STAT(PL_VIEW_OFS, player);
868 player.event_damage = PlayerDamage;
869 STAT(HUD, player) = HUD_NORMAL;
870 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++ slot)
872 .entity weaponentity = weaponentities[slot];
873 player.(weaponentity).m_switchweapon = vehic.(weaponentity).m_switchweapon;
874 delete(vehic.(weaponentity)); // no longer needed
876 player.last_vehiclecheck = time + 3;
877 player.vehicle_enter_delay = time + 2;
879 CSQCVehicleSetup(player, HUD_NORMAL);
881 Kill_Notification(NOTIF_ONE, player, MSG_CENTER, CPID_VEHICLES);
882 Kill_Notification(NOTIF_ONE, player, MSG_CENTER, CPID_VEHICLES_OTHER); // kill all vehicle notifications when exiting a vehicle?
885 vehic.flags |= FL_NOTARGET;
888 vehic.avelocity = '0 0 0';
890 vehic.tur_head.nodrawtoclient = NULL;
895 WaypointSprite_Kill(vehic.wps_intruder);
897 MUTATOR_CALLHOOK(VehicleExit, player, vehic);
899 vehic.team = vehic.tur_head.team;
901 if(vehic.old_vehicle_flags & VHF_SHIELDREGEN)
902 vehic.vehicle_flags |= VHF_SHIELDREGEN;
903 vehic.old_vehicle_flags = 0;
905 sound (vehic, CH_TRIGGER_SINGLE, SND_Null, 1, ATTEN_NORM);
906 vehic.vehicle_hudmodel.viewmodelforclient = vehic;
907 vehic.phase = time + 1;
909 vehic.vehicle_exit(vehic, eject);
911 vehicles_setreturn(vehic);
912 vehicles_reset_colors(vehic, NULL);
915 CSQCMODEL_AUTOINIT(vehic);
918 player.oldorigin = player.origin; // player's location is set by the exit functions, so we need to do this after everything
920 vehicles_exit_running = false;
923 void vehicles_touch(entity this, entity toucher)
925 if(MUTATOR_CALLHOOK(VehicleTouch, this, toucher))
928 // Vehicle currently in use
932 if((this.origin_z + this.maxs_z) > (toucher.origin_z))
933 if(vehicles_crushable(toucher))
934 if(!weaponLocked(this.owner))
936 if(vdist(this.velocity, >=, autocvar_g_vehicles_crush_minspeed))
937 Damage(toucher, this, this.owner, autocvar_g_vehicles_crush_dmg, DEATH_VH_CRUSH.m_id, DMG_NOWEP, '0 0 0', normalize(toucher.origin - this.origin) * autocvar_g_vehicles_crush_force);
939 return; // Dont do selfdamage when hitting "soft targets".
942 if(this.play_time < time) {
943 Vehicle info = this.vehicledef; //REGISTRY_GET(Vehicles, this.vehicleid);
944 info.vr_impact(info, this);
950 if(autocvar_g_vehicles_enter)
953 vehicles_enter(toucher, this);
956 bool vehicle_impulse(entity this, int imp)
958 entity v = this.vehicle;
959 if (!v) return false;
960 if (IS_DEAD(v)) return false;
961 bool(entity,int) f = v.vehicles_impulse;
962 if (f && f(this,imp)) return true;
965 case IMP_weapon_drop.impulse:
967 stuffcmd(this, "\ntoggle cl_eventchase_vehicle\nset _vehicles_shownchasemessage 1\n");
974 void vehicles_enter(entity pl, entity veh)
976 // Remove this when bots know how to use vehicles
977 if((IS_BOT_CLIENT(pl) && !autocvar_g_vehicles_allow_bots))
981 || (veh.phase >= time)
982 || (pl.vehicle_enter_delay >= time)
983 || (STAT(FROZEN, pl))
988 Vehicle info = veh.vehicledef; //REGISTRY_GET(Vehicles, veh.vehicleid);
990 if(autocvar_g_vehicles_enter) // vehicle's touch function should handle this if entering via use key is disabled (TODO)
991 if(veh.vehicle_flags & VHF_MULTISLOT)
992 if(veh.owner && SAME_TEAM(pl, veh))
994 // we don't need a return value or anything here
995 // if successful the owner check below will prevent anything weird
996 info.vr_gunner_enter(info, veh, pl);
1000 return; // got here and didn't enter the gunner, return
1004 if(DIFF_TEAM(pl, veh))
1005 if(autocvar_g_vehicles_steal)
1007 FOREACH_CLIENT(IS_PLAYER(it) && SAME_TEAM(it, veh), Send_Notification(NOTIF_ONE, it, MSG_CENTER, CENTER_VEHICLE_STEAL));
1009 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_VEHICLE_STEAL_SELF);
1011 veh.vehicle_shield = 0;
1012 veh.old_vehicle_flags = veh.vehicle_flags; // make a backup just so we're not permanently crippling this vehicle
1013 veh.vehicle_flags &= ~VHF_SHIELDREGEN;
1015 if (autocvar_g_vehicles_steal_show_waypoint) {
1016 entity wp = WaypointSprite_Spawn(WP_VehicleIntruder, 0, 0, pl, '0 0 68', NULL, veh.team, veh, wps_intruder, true, RADARICON_DANGER);
1017 wp.colormod = Team_ColorRGB(pl.team);
1022 RemoveGrapplingHooks(pl);
1024 veh.vehicle_ammo1 = 0;
1025 veh.vehicle_ammo2 = 0;
1026 veh.vehicle_reload1 = 0;
1027 veh.vehicle_reload2 = 0;
1028 veh.vehicle_energy = 0;
1033 // .viewmodelforclient works better.
1034 //veh.vehicle_hudmodel.drawonlytoclient = veh.owner;
1036 veh.vehicle_hudmodel.viewmodelforclient = pl;
1039 pl.view_ofs = STAT(PL_VIEW_OFS, pl);
1040 setsize(pl, STAT(PL_MIN, pl), STAT(PL_MAX, pl));
1042 veh.event_damage = vehicles_damage;
1043 veh.event_heal = vehicles_heal;
1045 pl.items &= ~IT_USING_JETPACK;
1046 pl.angles = veh.angles;
1047 pl.takedamage = DAMAGE_NO;
1048 pl.solid = SOLID_NOT;
1049 pl.disableclientprediction = 1; // physics is no longer run, so this won't be reset
1050 set_movetype(pl, MOVETYPE_NOCLIP);
1051 pl.teleportable = false;
1053 pl.event_damage = func_null;
1054 pl.view_ofs = '0 0 0';
1055 veh.colormap = pl.colormap;
1057 veh.tur_head.colormap = pl.colormap;
1058 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
1060 .entity weaponentity = weaponentities[slot];
1061 veh.(weaponentity) = new(temp_wepent);
1062 veh.(weaponentity).m_switchweapon = pl.(weaponentity).m_switchweapon;
1064 STAT(HUD, pl) = veh.vehicleid;
1065 pl.PlayerPhysplug = veh.PlayerPhysplug;
1067 pl.vehicle_ammo1 = veh.vehicle_ammo1;
1068 pl.vehicle_ammo2 = veh.vehicle_ammo2;
1069 pl.vehicle_reload1 = veh.vehicle_reload1;
1070 pl.vehicle_reload2 = veh.vehicle_reload2;
1071 pl.vehicle_energy = veh.vehicle_energy;
1073 // Cant do this, hides attached objects too.
1074 //veh.exteriormodeltoclient = veh.owner;
1075 //veh.tur_head.exteriormodeltoclient = veh.owner;
1078 UNSET_ONGROUND(veh);
1081 veh.flags -= FL_NOTARGET;
1083 vehicles_reset_colors(veh, pl);
1085 if (IS_REAL_CLIENT(pl))
1087 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_VEHICLE_ENTER);
1090 WriteByte (MSG_ONE, SVC_SETVIEWPORT);
1091 WriteEntity(MSG_ONE, veh.vehicle_viewport);
1093 // NOTE: engine networked
1094 WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
1097 WriteAngle(MSG_ONE, veh.tur_head.angles_x + veh.angles_x); // tilt
1098 WriteAngle(MSG_ONE, veh.tur_head.angles_y + veh.angles_y); // yaw
1099 WriteAngle(MSG_ONE, 0); // roll
1103 WriteAngle(MSG_ONE, veh.angles_x * -1); // tilt
1104 WriteAngle(MSG_ONE, veh.angles_y); // yaw
1105 WriteAngle(MSG_ONE, 0); // roll
1109 vehicles_clearreturn(veh);
1111 CSQCVehicleSetup(pl, veh.vehicleid);
1113 MUTATOR_CALLHOOK(VehicleEnter, pl, veh);
1115 CSQCModel_UnlinkEntity(veh);
1116 info.vr_enter(info, veh);
1118 antilag_clear(pl, CS(pl));
1121 void vehicles_think(entity this)
1123 this.nextthink = time + autocvar_g_vehicles_thinkrate;
1126 STAT(VEHICLESTAT_W2MODE, this.owner) = STAT(VEHICLESTAT_W2MODE, this);
1128 Vehicle info = this.vehicledef; //REGISTRY_GET(Vehicles, this.vehicleid);
1129 info.vr_think(info, this);
1131 vehicles_painframe(this);
1133 CSQCMODEL_AUTOUPDATE(this);
1136 void vehicles_reset(entity this)
1139 vehicles_exit(this, VHEF_RELEASE);
1141 vehicles_clearreturn(this);
1143 if(this.active != ACTIVE_NOT)
1144 vehicles_spawn(this);
1148 void vehicles_spawn(entity this)
1150 LOG_DEBUG("Spawning vehicle: ", this.classname);
1153 this.vehicle_hudmodel.viewmodelforclient = this;
1156 settouch(this, vehicles_touch);
1157 this.event_damage = vehicles_damage;
1158 this.event_heal = vehicles_heal;
1159 this.reset = vehicles_reset;
1160 this.iscreature = true;
1161 this.teleportable = false; // no teleporting for vehicles, too buggy
1162 this.damagedbycontents = true;
1163 set_movetype(this, MOVETYPE_WALK);
1164 this.solid = SOLID_SLIDEBOX;
1165 this.takedamage = DAMAGE_AIM;
1166 this.deadflag = DEAD_NO;
1167 if(!this.bot_attack)
1168 IL_PUSH(g_bot_targets, this);
1169 this.bot_attack = true;
1170 this.flags = FL_NOTARGET;
1171 this.avelocity = '0 0 0';
1172 this.velocity = '0 0 0';
1173 setthink(this, vehicles_think);
1174 this.nextthink = time;
1177 this.lock_strength = 0;
1178 this.lock_target = NULL;
1179 this.misc_bulletcounter = 0;
1182 this.angles = this.pos2;
1183 setorigin(this, this.pos1);
1185 Send_Effect(EFFECT_TELEPORT, this.origin + '0 0 64', '0 0 0', 1);
1187 if(this.vehicle_controller)
1188 this.team = this.vehicle_controller.team;
1190 FOREACH_CLIENT(IS_PLAYER(it),
1192 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
1194 .entity weaponentity = weaponentities[slot];
1195 if(it.(weaponentity).hook.aiment == this)
1196 RemoveHook(it.(weaponentity).hook);
1201 Vehicle info = this.vehicledef; //REGISTRY_GET(Vehicles, this.vehicleid);
1202 info.vr_spawn(info, this);
1204 vehicles_reset_colors(this, NULL);
1206 CSQCMODEL_AUTOINIT(this);
1209 bool vehicle_initialize(entity this, Vehicle info, bool nodrop)
1211 if(!autocvar_g_vehicles)
1218 info.vr_precache(info);
1220 if(this.targetname && this.targetname != "")
1222 this.vehicle_controller = find(NULL, target, this.targetname);
1223 if(!this.vehicle_controller)
1225 LOG_DEBUG("^1WARNING: ^7Vehicle with invalid .targetname");
1226 this.active = ACTIVE_ACTIVE;
1230 this.team = this.vehicle_controller.team;
1231 this.use = vehicle_use;
1235 if(this.vehicle_controller.team == 0)
1236 this.active = ACTIVE_NOT;
1238 this.active = ACTIVE_ACTIVE;
1242 else { this.active = ACTIVE_ACTIVE; }
1244 if(this.team && (!teamplay || !autocvar_g_vehicles_teams))
1247 if(this.mdl == "" || !this.mdl)
1248 _setmodel(this, info.model);
1250 _setmodel(this, this.mdl);
1252 this.vehicle_flags |= VHF_ISVEHICLE;
1254 this.vehicle_viewport = new(vehicle_viewport);
1255 this.vehicle_hudmodel = new(vehicle_hudmodel);
1256 this.tur_head = new(tur_head);
1257 this.tur_head.owner = this;
1258 this.takedamage = DAMAGE_NO;
1259 this.bot_attack = true;
1260 IL_PUSH(g_bot_targets, this);
1261 this.iscreature = true;
1262 this.teleportable = false; // no teleporting for vehicles, too buggy
1263 this.damagedbycontents = true;
1264 IL_PUSH(g_damagedbycontents, this);
1265 this.vehicleid = info.vehicleid;
1266 this.vehicledef = info;
1267 this.PlayerPhysplug = info.PlayerPhysplug;
1268 this.event_damage = func_null;
1269 this.event_heal = func_null;
1270 settouch(this, vehicles_touch);
1271 setthink(this, vehicles_spawn);
1272 this.nextthink = time;
1273 this.effects = EF_NODRAW;
1274 this.dphitcontentsmask = DPCONTENTS_BODY | DPCONTENTS_SOLID;
1276 if(autocvar_g_playerclip_collisions)
1277 this.dphitcontentsmask |= DPCONTENTS_PLAYERCLIP;
1279 if(autocvar_g_nodepthtestplayers)
1280 this.effects |= EF_NODEPTHTEST;
1282 if(autocvar_g_fullbrightplayers)
1283 this.effects |= EF_FULLBRIGHT;
1285 _setmodel(this.vehicle_hudmodel, info.hud_model);
1286 setmodel(this.vehicle_viewport, MDL_Null);
1288 if(info.head_model != "")
1290 _setmodel(this.tur_head, info.head_model);
1291 setattachment(this.tur_head, this, info.tag_head);
1292 setattachment(this.vehicle_hudmodel, this.tur_head, info.tag_hud);
1293 setattachment(this.vehicle_viewport, this.vehicle_hudmodel, info.tag_view);
1297 setattachment(this.tur_head, this, "");
1298 setattachment(this.vehicle_hudmodel, this, info.tag_hud);
1299 setattachment(this.vehicle_viewport, this.vehicle_hudmodel, info.tag_view);
1302 setsize(this, info.m_mins, info.m_maxs);
1304 info.vr_setup(info, this);
1308 setorigin(this, this.origin);
1309 tracebox(this.origin + '0 0 100', info.m_mins, info.m_maxs, this.origin - '0 0 10000', MOVE_WORLDONLY, this);
1310 setorigin(this, trace_endpos);
1313 this.pos1 = this.origin;
1314 this.pos2 = this.angles;
1315 this.tur_head.team = this.team;
1317 if(this.active == ACTIVE_NOT)
1318 this.nextthink = 0; // wait until activated
1319 else if(autocvar_g_vehicles_delayspawn)
1320 this.nextthink = time + this.respawntime + (random() * autocvar_g_vehicles_delayspawn_jitter);
1322 this.nextthink = time + game_starttime;
1324 if(MUTATOR_CALLHOOK(VehicleInit, this))