1 #include "sv_vehicles.qh"
3 #include <server/bot/api.qh>
4 #include <server/damage.qh>
5 #include <server/world.qh>
6 #include <server/items/items.qh>
7 #include <common/items/_mod.qh>
8 #include <common/mapobjects/defs.qh>
9 #include <common/mapobjects/teleporters.qh>
10 #include <common/mapobjects/triggers.qh>
11 #include <server/weapons/common.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);
269 PROJECTILE_MAKETRIGGER(proj);
270 setorigin(proj, _org);
272 proj.shot_dmg = _dmg;
273 proj.shot_radius = _radi;
274 proj.shot_force = _force;
275 proj.projectiledeathtype = _deahtype;
276 proj.solid = SOLID_BBOX;
277 set_movetype(proj, MOVETYPE_FLYMISSILE);
278 proj.flags = FL_PROJECTILE;
279 IL_PUSH(g_projectiles, proj);
280 IL_PUSH(g_bot_dodge, proj);
281 proj.bot_dodge = true;
282 proj.bot_dodgerating = _dmg;
283 proj.velocity = _vel;
284 settouch(proj, vehicles_projectile_explode);
285 proj.use = vehicles_projectile_explode_use;
287 proj.realowner = _owner;
288 setthink(proj, SUB_Remove);
289 proj.nextthink = time + 30;
293 proj.takedamage = DAMAGE_AIM;
294 proj.event_damage = vehicles_projectile_damage;
295 SetResourceExplicit(proj, RES_HEALTH, _health);
298 proj.flags |= FL_NOTARGET;
300 if(_mzlsound != SND_Null)
301 sound (this, CH_WEAPON_A, _mzlsound, VOL_BASE, ATTEN_NORM);
303 if(_mzlfx != EFFECT_Null)
304 Send_Effect(_mzlfx, proj.origin, proj.velocity, 1);
306 setsize (proj, '-1 -1 -1' * _size, '1 1 1' * _size);
308 CSQCProjectile(proj, _clianim, _projtype, _cull);
313 void vehicles_gib_explode(entity this)
315 sound (this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
316 Send_Effect(EFFECT_EXPLOSION_SMALL, randomvec() * 80 + (this.origin + '0 0 100'), '0 0 0', 1);
317 Send_Effect(EFFECT_EXPLOSION_SMALL, this.wp00.origin + '0 0 64', '0 0 0', 1);
321 void vehicles_gib_touch(entity this, entity toucher)
323 vehicles_gib_explode(this);
326 void vehicles_gib_think(entity this)
332 this.nextthink = time + 0.1;
335 entity vehicle_tossgib(entity this, entity _template, vector _vel, string _tag, bool _burn, bool _explode, float _maxtime, vector _rot)
337 entity _gib = spawn();
338 _setmodel(_gib, _template.model);
339 vector org = gettaginfo(this, gettagindex(this, _tag));
340 setorigin(_gib, org);
341 _gib.velocity = _vel;
342 set_movetype(_gib, MOVETYPE_TOSS);
343 _gib.solid = SOLID_CORPSE;
344 _gib.colormod = '-0.5 -0.5 -0.5';
345 _gib.effects = EF_LOWPRECISION;
346 _gib.avelocity = _rot;
349 _gib.effects |= EF_FLAME;
353 setthink(_gib, vehicles_gib_explode);
354 _gib.nextthink = time + random() * _explode;
355 settouch(_gib, vehicles_gib_touch);
359 _gib.cnt = time + _maxtime;
360 setthink(_gib, vehicles_gib_think);
361 _gib.nextthink = time + _maxtime - 1;
367 bool vehicle_addplayerslot( entity _owner,
371 bool(entity,float) _framefunc,
372 void(entity,bool) _exitfunc, float(entity, entity) _enterfunc)
374 if(!(_owner.vehicle_flags & VHF_MULTISLOT))
375 _owner.vehicle_flags |= VHF_MULTISLOT;
377 _slot.PlayerPhysplug = _framefunc;
378 _slot.vehicle_exit = _exitfunc;
379 _slot.vehicle_enter = _enterfunc;
380 STAT(HUD, _slot) = _hud;
381 _slot.vehicle_flags = VHF_PLAYERSLOT;
382 _slot.vehicle_viewport = spawn();
383 _slot.vehicle_hudmodel = spawn();
384 _slot.vehicle_hudmodel.viewmodelforclient = _slot;
385 _slot.vehicle_viewport.effects = (EF_ADDITIVE | EF_DOUBLESIDED | EF_FULLBRIGHT | EF_NODEPTHTEST | EF_NOGUNBOB | EF_NOSHADOW | EF_LOWPRECISION | EF_SELECTABLE | EF_TELEPORT_BIT);
387 setmodel(_slot.vehicle_hudmodel, _hud_model);
388 setmodel(_slot.vehicle_viewport, MDL_Null);
390 setattachment(_slot.vehicle_hudmodel, _slot, "");
391 setattachment(_slot.vehicle_viewport, _slot.vehicle_hudmodel, "");
396 vector vehicle_aimturret(entity _vehic, vector _target, entity _turrret, string _tagname,
397 float _pichlimit_min, float _pichlimit_max,
398 float _rotlimit_min, float _rotlimit_max, float _aimspeed, float dt)
402 vtag = gettaginfo(_turrret, gettagindex(_turrret, _tagname));
403 vtmp = vectoangles(normalize(_target - vtag));
404 vtmp = AnglesTransform_ToAngles(AnglesTransform_LeftDivide(AnglesTransform_FromAngles(_vehic.angles), AnglesTransform_FromAngles(vtmp))) - _turrret.angles;
405 vtmp = AnglesTransform_Normalize(vtmp, true);
406 ftmp = _aimspeed * dt;
407 vtmp_y = bound(-ftmp, vtmp_y, ftmp);
408 vtmp_x = bound(-ftmp, vtmp_x, ftmp);
409 _turrret.angles_y = bound(_rotlimit_min, _turrret.angles_y + vtmp_y, _rotlimit_max);
410 _turrret.angles_x = bound(_pichlimit_min, _turrret.angles_x + vtmp_x, _pichlimit_max);
414 void vehicles_reset_colors(entity this, entity player)
417 const vector cmod = '0 0 0';
418 if(this.team && teamplay)
419 cmap = 1024 + (this.team - 1) * 17;
421 cmap = player.colormap;
424 if(autocvar_g_nodepthtestplayers)
425 eff |= EF_NODEPTHTEST;
426 if(autocvar_g_fullbrightplayers)
427 eff |= EF_FULLBRIGHT;
429 // Find all ents attacked to main model and setup effects, colormod etc.
430 FOREACH_ENTITY_ENT(tag_entity, this,
432 if(it == this.vehicle_shieldent)
441 // Also check head tags
442 FOREACH_ENTITY_ENT(tag_entity, this.tur_head,
444 if(it == this.vehicle_shieldent)
453 this.vehicle_hudmodel.effects = this.effects = eff; // | EF_LOWPRECISION;
454 this.vehicle_hudmodel.colormod = this.colormod = cmod;
455 this.vehicle_hudmodel.colormap = this.colormap = cmap;
456 this.vehicle_viewport.effects = (EF_ADDITIVE | EF_DOUBLESIDED | EF_FULLBRIGHT | EF_NODEPTHTEST | EF_NOGUNBOB | EF_NOSHADOW | EF_LOWPRECISION | EF_SELECTABLE | EF_TELEPORT_BIT);
459 this.avelocity = '0 0 0';
460 this.velocity = '0 0 0';
463 Vehicle info = this.vehicledef; //REGISTRY_GET(Vehicles, this.vehicleid);
464 info.vr_setcolors(info, this);
467 void vehicles_clearreturn(entity veh)
469 // Remove "return helper" entities, if any.
470 IL_EACH(g_vehicle_returners, it.wp00 == veh,
473 setthink(it, SUB_Remove);
474 it.nextthink = time + 0.1;
475 IL_REMOVE(g_vehicle_returners, it);
477 if(it.waypointsprite_attached)
478 WaypointSprite_Kill(it.waypointsprite_attached);
482 void vehicles_spawn(entity this);
483 void vehicles_return(entity this)
485 Send_Effect(EFFECT_TELEPORT, this.wp00.origin + '0 0 64', '0 0 0', 1);
487 setthink(this.wp00, vehicles_spawn);
488 this.wp00.nextthink = time;
490 if(this.waypointsprite_attached)
491 WaypointSprite_Kill(this.waypointsprite_attached);
496 void vehicles_showwp_goaway(entity this)
498 if(this.waypointsprite_attached)
499 WaypointSprite_Kill(this.waypointsprite_attached);
504 void vehicles_showwp(entity this)
510 setthink(ent, vehicles_return);
511 ent.nextthink = ent.cnt;
515 setthink(ent, vehicles_return);
516 ent.nextthink = time + 1;
519 ent.team = this.wp00.team;
520 ent.wp00 = this.wp00;
521 setorigin(ent, this.wp00.pos1);
523 ent.nextthink = time + 5;
524 setthink(ent, vehicles_showwp_goaway);
528 if(teamplay && ent.team)
529 rgb = Team_ColorRGB(ent.team);
532 entity wp = WaypointSprite_Spawn(WP_Vehicle, 0, 0, ent, '0 0 64', NULL, 0, ent, waypointsprite_attached, true, RADARICON_Vehicle);
533 wp.wp_extra = ent.wp00.vehicleid;
535 if(ent.waypointsprite_attached)
537 WaypointSprite_UpdateRule(ent.waypointsprite_attached, ent.wp00.team, SPRITERULE_DEFAULT);
539 WaypointSprite_UpdateBuildFinished(ent.waypointsprite_attached, ent.nextthink);
540 WaypointSprite_Ping(ent.waypointsprite_attached);
544 void vehicles_setreturn(entity veh)
546 vehicles_clearreturn(veh);
548 entity ret = new(vehicle_return);
549 IL_PUSH(g_vehicle_returners, ret);
552 setthink(ret, vehicles_showwp);
556 ret.cnt = time + veh.respawntime;
557 ret.nextthink = min(time + veh.respawntime, time + veh.respawntime - 5);
561 ret.nextthink = min(time + veh.respawntime, time + veh.respawntime - 1);
564 setorigin(ret, veh.pos1 + '0 0 96');
567 void vehicle_use(entity this, entity actor, entity trigger)
569 LOG_DEBUG("vehicle ", this.netname, " used by ", actor.classname);
571 this.tur_head.team = actor.team;
573 if(this.tur_head.team == 0)
574 this.active = ACTIVE_NOT;
576 this.active = ACTIVE_ACTIVE;
578 if(this.active == ACTIVE_ACTIVE && !IS_DEAD(this) && !game_stopped)
580 LOG_DEBUG("Respawning vehicle: ", this.netname);
581 if(this.effects & EF_NODRAW)
583 setthink(this, vehicles_spawn);
584 this.nextthink = time + 3;
588 vehicles_setreturn(this);
589 vehicles_reset_colors(this, actor);
594 void vehicles_regen(entity this, float timer, .float regen_field, float field_max, float rpause, float regen, float delta_time, float _healthscale)
596 if(this.(regen_field) < field_max)
597 if(timer + rpause < time)
600 regen = regen * (GetResource(this, RES_HEALTH) / this.max_health);
602 this.(regen_field) = min(this.(regen_field) + regen * delta_time, field_max);
605 this.owner.(regen_field) = (this.(regen_field) / field_max) * 100;
609 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)
611 float resource_amount = GetResource(this, resource);
613 if(resource_amount < field_max)
614 if(timer + rpause < time)
617 regen = regen * (resource_amount / this.max_health);
619 SetResource(this, resource, min(resource_amount + regen * delta_time, field_max));
622 this.owner.(regen_field) = (GetResource(this, resource) / field_max) * 100;
626 void shieldhit_think(entity this)
631 // setmodel(this, MDL_Null);
633 this.effects |= EF_NODRAW;
637 this.nextthink = time + 0.1;
641 void vehicles_painframe(entity this)
643 int myhealth = ((this.owner) ? this.owner.vehicle_health : ((GetResource(this, RES_HEALTH) / this.max_health) * 100));
646 if(this.pain_frame < time)
648 float _ftmp = myhealth / 50;
649 this.pain_frame = time + max(0.1, 0.1 + (random() * 0.5 * _ftmp));
650 Send_Effect(EFFECT_SMOKE_SMALL, (this.origin + (randomvec() * 80)), '0 0 0', 1);
652 if(this.vehicle_flags & VHF_DMGSHAKE)
653 this.velocity += randomvec() * 30;
655 if(this.vehicle_flags & VHF_DMGROLL)
657 if(this.vehicle_flags & VHF_DMGHEADROLL)
658 this.tur_head.angles += randomvec();
660 this.angles += randomvec();
665 void vehicles_frame(entity this, entity actor)
667 vehicles_painframe(this);
670 void vehicles_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
672 this.dmg_time = time;
675 if(DEATH_ISWEAPON(deathtype, WEP_VORTEX))
676 damage *= autocvar_g_vehicles_vortex_damagerate;
677 else if(DEATH_ISWEAPON(deathtype, WEP_MACHINEGUN))
678 damage *= autocvar_g_vehicles_machinegun_damagerate;
679 else if(DEATH_ISWEAPON(deathtype, WEP_RIFLE))
680 damage *= autocvar_g_vehicles_rifle_damagerate;
681 else if(DEATH_ISWEAPON(deathtype, WEP_VAPORIZER))
682 damage *= autocvar_g_vehicles_vaporizer_damagerate;
683 else if(DEATH_ISWEAPON(deathtype, WEP_SEEKER))
684 damage *= autocvar_g_vehicles_tag_damagerate;
685 else if(DEATH_WEAPONOF(deathtype) != WEP_Null)
686 damage *= autocvar_g_vehicles_weapon_damagerate;
688 this.enemy = attacker;
690 this.pain_finished = time;
692 if((this.vehicle_flags & VHF_HASSHIELD) && (this.vehicle_shield > 0))
694 if (wasfreed(this.vehicle_shieldent) || this.vehicle_shieldent == NULL)
696 this.vehicle_shieldent = spawn();
697 this.vehicle_shieldent.effects = EF_LOWPRECISION;
699 setmodel(this.vehicle_shieldent, MDL_VEH_SHIELD);
700 setattachment(this.vehicle_shieldent, this, "");
701 setorigin(this.vehicle_shieldent, real_origin(this) - this.origin);
702 this.vehicle_shieldent.scale = 256 / vlen(this.maxs - this.mins);
703 setthink(this.vehicle_shieldent, shieldhit_think);
706 this.vehicle_shieldent.colormod = '1 1 1';
707 this.vehicle_shieldent.alpha = 0.45;
708 this.vehicle_shieldent.angles = vectoangles(normalize(hitloc - (this.origin + this.vehicle_shieldent.origin))) - this.angles;
709 this.vehicle_shieldent.nextthink = time;
710 this.vehicle_shieldent.effects &= ~EF_NODRAW;
712 this.vehicle_shield -= damage;
714 if(this.vehicle_shield < 0)
716 TakeResource(this, RES_HEALTH, fabs(this.vehicle_shield));
717 this.vehicle_shieldent.colormod = '2 0 0';
718 this.vehicle_shield = 0;
719 this.vehicle_shieldent.alpha = 0.75;
721 if(sound_allowed(MSG_BROADCAST, attacker))
722 spamsound (this, CH_PAIN, SND_ONS_HIT2, VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
725 if(sound_allowed(MSG_BROADCAST, attacker))
726 spamsound (this, CH_PAIN, SND_ONS_ELECTRICITY_EXPLODE, VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
731 TakeResource(this, RES_HEALTH, damage);
733 if(sound_allowed(MSG_BROADCAST, attacker))
734 spamsound (this, CH_PAIN, SND_ONS_HIT2, VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
737 if(this.damageforcescale < 1 && this.damageforcescale > 0)
738 this.velocity += force * this.damageforcescale;
740 this.velocity += force;
742 if(GetResource(this, RES_HEALTH) <= 0)
746 if(this.vehicle_flags & VHF_DEATHEJECT)
747 vehicles_exit(this, VHEF_EJECT);
749 vehicles_exit(this, VHEF_RELEASE);
752 antilag_clear(this, this);
754 Vehicle info = this.vehicledef; //REGISTRY_GET(Vehicles, this.vehicleid);
755 info.vr_death(info, this);
756 vehicles_setreturn(this);
760 bool vehicles_heal(entity targ, entity inflictor, float amount, float limit)
762 float true_limit = ((limit != RES_LIMIT_NONE) ? limit : targ.max_health);
763 if(GetResource(targ, RES_HEALTH) <= 0 || GetResource(targ, RES_HEALTH) >= true_limit)
766 GiveResourceWithLimit(targ, RES_HEALTH, amount, true_limit);
768 targ.owner.vehicle_health = (GetResource(targ, RES_HEALTH) / targ.max_health) * 100;
772 bool vehicles_crushable(entity e)
774 if(IS_PLAYER(e) && time >= e.vehicle_enter_delay)
783 void vehicles_impact(entity this, float _minspeed, float _speedfac, float _maxpain)
785 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
788 if(this.play_time < time)
790 if(vdist(this.velocity - this.oldvelocity, >, _minspeed))
792 float wc = vlen(this.velocity - this.oldvelocity);
793 float take = min(_speedfac * wc, _maxpain);
794 Damage(this, NULL, NULL, take, DEATH_FALL.m_id, DMG_NOWEP, this.origin, '0 0 0');
795 this.play_time = time + 0.25;
800 // vehicle enter/exit handling
801 vector vehicles_findgoodexit(entity this, entity player, vector prefer_spot)
803 // TODO: we actually want the player's size here
804 tracebox(this.origin + '0 0 32', PL_MIN_CONST, PL_MAX_CONST, prefer_spot, MOVE_NORMAL, player);
805 if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
808 float mysize = 1.5 * vlen(this.maxs - this.mins);
810 vector v2 = 0.5 * (this.absmin + this.absmax);
811 for(int i = 0; i < autocvar_g_vehicles_exit_attempts; ++i)
815 v = v2 + normalize(v) * mysize;
816 tracebox(v2, PL_MIN_CONST, PL_MAX_CONST, v, MOVE_NORMAL, player);
817 if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
824 .int old_vehicle_flags;
825 void vehicles_exit(entity vehic, bool eject)
827 entity player = vehic.owner;
829 if(vehicles_exit_running)
831 LOG_TRACE("^1vehicles_exit already running! this is not good...");
835 vehicles_exit_running = true;
837 if(vehic.vehicle_flags & VHF_PLAYERSLOT)
839 vehic.vehicle_exit(vehic, eject);
840 vehicles_exit_running = false;
846 if (IS_REAL_CLIENT(player))
849 WriteByte (MSG_ONE, SVC_SETVIEWPORT);
850 WriteEntity( MSG_ONE, player);
852 // NOTE: engine networked
853 WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
854 WriteAngle(MSG_ONE, 0);
855 WriteAngle(MSG_ONE, vehic.angles_y);
856 WriteAngle(MSG_ONE, 0);
859 setsize(player, STAT(PL_MIN, player), STAT(PL_MAX, player));
861 player.takedamage = DAMAGE_AIM;
862 player.solid = SOLID_SLIDEBOX;
863 set_movetype(player, MOVETYPE_WALK);
864 player.effects &= ~EF_NODRAW;
865 player.teleportable = TELEPORT_NORMAL;
866 player.alpha = default_player_alpha;
867 player.PlayerPhysplug = func_null;
868 player.vehicle = NULL;
869 player.view_ofs = STAT(PL_VIEW_OFS, player);
870 player.event_damage = PlayerDamage;
871 STAT(HUD, player) = HUD_NORMAL;
872 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++ slot)
874 .entity weaponentity = weaponentities[slot];
875 player.(weaponentity).m_switchweapon = vehic.(weaponentity).m_switchweapon;
876 delete(vehic.(weaponentity)); // no longer needed
878 player.last_vehiclecheck = time + 3;
879 player.vehicle_enter_delay = time + 2;
881 CSQCVehicleSetup(player, HUD_NORMAL);
883 Kill_Notification(NOTIF_ONE, player, MSG_CENTER, CPID_VEHICLES);
884 Kill_Notification(NOTIF_ONE, player, MSG_CENTER, CPID_VEHICLES_OTHER); // kill all vehicle notifications when exiting a vehicle?
887 vehic.flags |= FL_NOTARGET;
890 vehic.avelocity = '0 0 0';
892 vehic.tur_head.nodrawtoclient = NULL;
897 WaypointSprite_Kill(vehic.wps_intruder);
899 MUTATOR_CALLHOOK(VehicleExit, player, vehic);
901 vehic.team = vehic.tur_head.team;
903 if(vehic.old_vehicle_flags & VHF_SHIELDREGEN)
904 vehic.vehicle_flags |= VHF_SHIELDREGEN;
905 vehic.old_vehicle_flags = 0;
907 sound (vehic, CH_TRIGGER_SINGLE, SND_Null, 1, ATTEN_NORM);
908 vehic.vehicle_hudmodel.viewmodelforclient = vehic;
909 vehic.phase = time + 1;
911 vehic.vehicle_exit(vehic, eject);
913 vehicles_setreturn(vehic);
914 vehicles_reset_colors(vehic, NULL);
917 CSQCMODEL_AUTOINIT(vehic);
920 player.oldorigin = player.origin; // player's location is set by the exit functions, so we need to do this after everything
922 vehicles_exit_running = false;
925 void vehicles_touch(entity this, entity toucher)
927 if(MUTATOR_CALLHOOK(VehicleTouch, this, toucher))
930 // Vehicle currently in use
934 if((this.origin_z + this.maxs_z) > (toucher.origin_z))
935 if(vehicles_crushable(toucher))
936 if(!weaponLocked(this.owner))
938 if(vdist(this.velocity, >=, autocvar_g_vehicles_crush_minspeed))
939 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);
941 return; // Dont do selfdamage when hitting "soft targets".
944 if(this.play_time < time) {
945 Vehicle info = this.vehicledef; //REGISTRY_GET(Vehicles, this.vehicleid);
946 info.vr_impact(info, this);
952 if(autocvar_g_vehicles_enter)
955 vehicles_enter(toucher, this);
958 bool vehicle_impulse(entity this, int imp)
960 entity v = this.vehicle;
961 if (!v) return false;
962 if (IS_DEAD(v)) return false;
963 bool(entity,int) f = v.vehicles_impulse;
964 if (f && f(this,imp)) return true;
967 case IMP_weapon_drop.impulse:
969 stuffcmd(this, "\ntoggle cl_eventchase_vehicle\nset _vehicles_shownchasemessage 1\n");
976 void vehicles_enter(entity pl, entity veh)
978 // Remove this when bots know how to use vehicles
979 if((IS_BOT_CLIENT(pl) && !autocvar_g_vehicles_allow_bots))
983 || (veh.phase >= time)
984 || (pl.vehicle_enter_delay >= time)
985 || (STAT(FROZEN, pl))
990 Vehicle info = veh.vehicledef; //REGISTRY_GET(Vehicles, veh.vehicleid);
992 if(autocvar_g_vehicles_enter) // vehicle's touch function should handle this if entering via use key is disabled (TODO)
993 if(veh.vehicle_flags & VHF_MULTISLOT)
994 if(veh.owner && SAME_TEAM(pl, veh))
996 // we don't need a return value or anything here
997 // if successful the owner check below will prevent anything weird
998 info.vr_gunner_enter(info, veh, pl);
1002 return; // got here and didn't enter the gunner, return
1006 if(DIFF_TEAM(pl, veh))
1007 if(autocvar_g_vehicles_steal)
1009 FOREACH_CLIENT(IS_PLAYER(it) && SAME_TEAM(it, veh), Send_Notification(NOTIF_ONE, it, MSG_CENTER, CENTER_VEHICLE_STEAL));
1011 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_VEHICLE_STEAL_SELF);
1013 veh.vehicle_shield = 0;
1014 veh.old_vehicle_flags = veh.vehicle_flags; // make a backup just so we're not permanently crippling this vehicle
1015 veh.vehicle_flags &= ~VHF_SHIELDREGEN;
1017 if (autocvar_g_vehicles_steal_show_waypoint) {
1018 entity wp = WaypointSprite_Spawn(WP_VehicleIntruder, 0, 0, pl, '0 0 68', NULL, veh.team, veh, wps_intruder, true, RADARICON_DANGER);
1019 wp.colormod = Team_ColorRGB(pl.team);
1024 RemoveGrapplingHooks(pl);
1026 veh.vehicle_ammo1 = 0;
1027 veh.vehicle_ammo2 = 0;
1028 veh.vehicle_reload1 = 0;
1029 veh.vehicle_reload2 = 0;
1030 veh.vehicle_energy = 0;
1035 // .viewmodelforclient works better.
1036 //veh.vehicle_hudmodel.drawonlytoclient = veh.owner;
1038 veh.vehicle_hudmodel.viewmodelforclient = pl;
1041 pl.view_ofs = STAT(PL_VIEW_OFS, pl);
1042 setsize(pl, STAT(PL_MIN, pl), STAT(PL_MAX, pl));
1044 veh.event_damage = vehicles_damage;
1045 veh.event_heal = vehicles_heal;
1047 pl.items &= ~IT_USING_JETPACK;
1048 pl.angles = veh.angles;
1049 pl.takedamage = DAMAGE_NO;
1050 pl.solid = SOLID_NOT;
1051 pl.disableclientprediction = 1; // physics is no longer run, so this won't be reset
1052 set_movetype(pl, MOVETYPE_NOCLIP);
1053 pl.teleportable = false;
1055 pl.event_damage = func_null;
1056 pl.view_ofs = '0 0 0';
1057 veh.colormap = pl.colormap;
1059 veh.tur_head.colormap = pl.colormap;
1060 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
1062 .entity weaponentity = weaponentities[slot];
1063 veh.(weaponentity) = new(temp_wepent);
1064 veh.(weaponentity).m_switchweapon = pl.(weaponentity).m_switchweapon;
1066 STAT(HUD, pl) = veh.vehicleid;
1067 pl.PlayerPhysplug = veh.PlayerPhysplug;
1069 pl.vehicle_ammo1 = veh.vehicle_ammo1;
1070 pl.vehicle_ammo2 = veh.vehicle_ammo2;
1071 pl.vehicle_reload1 = veh.vehicle_reload1;
1072 pl.vehicle_reload2 = veh.vehicle_reload2;
1073 pl.vehicle_energy = veh.vehicle_energy;
1075 // Cant do this, hides attached objects too.
1076 //veh.exteriormodeltoclient = veh.owner;
1077 //veh.tur_head.exteriormodeltoclient = veh.owner;
1080 UNSET_ONGROUND(veh);
1083 veh.flags -= FL_NOTARGET;
1085 vehicles_reset_colors(veh, pl);
1087 if (IS_REAL_CLIENT(pl))
1089 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_VEHICLE_ENTER);
1092 WriteByte (MSG_ONE, SVC_SETVIEWPORT);
1093 WriteEntity(MSG_ONE, veh.vehicle_viewport);
1095 // NOTE: engine networked
1096 WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
1099 WriteAngle(MSG_ONE, veh.tur_head.angles_x + veh.angles_x); // tilt
1100 WriteAngle(MSG_ONE, veh.tur_head.angles_y + veh.angles_y); // yaw
1101 WriteAngle(MSG_ONE, 0); // roll
1105 WriteAngle(MSG_ONE, veh.angles_x * -1); // tilt
1106 WriteAngle(MSG_ONE, veh.angles_y); // yaw
1107 WriteAngle(MSG_ONE, 0); // roll
1111 vehicles_clearreturn(veh);
1113 CSQCVehicleSetup(pl, veh.vehicleid);
1115 MUTATOR_CALLHOOK(VehicleEnter, pl, veh);
1117 CSQCModel_UnlinkEntity(veh);
1118 info.vr_enter(info, veh);
1120 antilag_clear(pl, CS(pl));
1123 void vehicles_think(entity this)
1125 this.nextthink = time + autocvar_g_vehicles_thinkrate;
1128 STAT(VEHICLESTAT_W2MODE, this.owner) = STAT(VEHICLESTAT_W2MODE, this);
1130 Vehicle info = this.vehicledef; //REGISTRY_GET(Vehicles, this.vehicleid);
1131 info.vr_think(info, this);
1133 vehicles_painframe(this);
1135 CSQCMODEL_AUTOUPDATE(this);
1138 void vehicles_reset(entity this)
1141 vehicles_exit(this, VHEF_RELEASE);
1143 vehicles_clearreturn(this);
1145 if(this.active != ACTIVE_NOT)
1146 vehicles_spawn(this);
1150 void vehicles_spawn(entity this)
1152 LOG_DEBUG("Spawning vehicle: ", this.classname);
1155 this.vehicle_hudmodel.viewmodelforclient = this;
1158 settouch(this, vehicles_touch);
1159 this.event_damage = vehicles_damage;
1160 this.event_heal = vehicles_heal;
1161 this.reset = vehicles_reset;
1162 this.iscreature = true;
1163 this.teleportable = false; // no teleporting for vehicles, too buggy
1164 this.damagedbycontents = true;
1165 set_movetype(this, MOVETYPE_WALK);
1166 this.solid = SOLID_SLIDEBOX;
1167 this.takedamage = DAMAGE_AIM;
1168 this.deadflag = DEAD_NO;
1169 if(!this.bot_attack)
1170 IL_PUSH(g_bot_targets, this);
1171 this.bot_attack = true;
1172 this.flags = FL_NOTARGET;
1173 this.avelocity = '0 0 0';
1174 this.velocity = '0 0 0';
1175 setthink(this, vehicles_think);
1176 this.nextthink = time;
1179 this.lock_strength = 0;
1180 this.lock_target = NULL;
1181 this.misc_bulletcounter = 0;
1184 this.angles = this.pos2;
1185 setorigin(this, this.pos1);
1187 Send_Effect(EFFECT_TELEPORT, this.origin + '0 0 64', '0 0 0', 1);
1189 if(this.vehicle_controller)
1190 this.team = this.vehicle_controller.team;
1192 FOREACH_CLIENT(IS_PLAYER(it),
1194 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
1196 .entity weaponentity = weaponentities[slot];
1197 if(it.(weaponentity).hook.aiment == this)
1198 RemoveHook(it.(weaponentity).hook);
1203 Vehicle info = this.vehicledef; //REGISTRY_GET(Vehicles, this.vehicleid);
1204 info.vr_spawn(info, this);
1206 vehicles_reset_colors(this, NULL);
1208 CSQCMODEL_AUTOINIT(this);
1211 bool vehicle_initialize(entity this, Vehicle info, bool nodrop)
1213 if(!autocvar_g_vehicles)
1220 info.vr_precache(info);
1222 if(this.targetname && this.targetname != "")
1224 this.vehicle_controller = find(NULL, target, this.targetname);
1225 if(!this.vehicle_controller)
1227 LOG_DEBUG("^1WARNING: ^7Vehicle with invalid .targetname");
1228 this.active = ACTIVE_ACTIVE;
1232 this.team = this.vehicle_controller.team;
1233 this.use = vehicle_use;
1237 if(this.vehicle_controller.team == 0)
1238 this.active = ACTIVE_NOT;
1240 this.active = ACTIVE_ACTIVE;
1244 else { this.active = ACTIVE_ACTIVE; }
1246 if(this.team && (!teamplay || !autocvar_g_vehicles_teams))
1249 if(this.mdl == "" || !this.mdl)
1250 _setmodel(this, info.model);
1252 _setmodel(this, this.mdl);
1254 this.vehicle_flags |= VHF_ISVEHICLE;
1256 this.vehicle_viewport = new(vehicle_viewport);
1257 this.vehicle_hudmodel = new(vehicle_hudmodel);
1258 this.tur_head = new(tur_head);
1259 this.tur_head.owner = this;
1260 this.takedamage = DAMAGE_NO;
1261 this.bot_attack = true;
1262 IL_PUSH(g_bot_targets, this);
1263 this.iscreature = true;
1264 this.teleportable = false; // no teleporting for vehicles, too buggy
1265 this.damagedbycontents = true;
1266 IL_PUSH(g_damagedbycontents, this);
1267 this.vehicleid = info.vehicleid;
1268 this.vehicledef = info;
1269 this.PlayerPhysplug = info.PlayerPhysplug;
1270 this.event_damage = func_null;
1271 this.event_heal = func_null;
1272 settouch(this, vehicles_touch);
1273 setthink(this, vehicles_spawn);
1274 this.nextthink = time;
1275 this.effects = EF_NODRAW;
1276 this.dphitcontentsmask = DPCONTENTS_BODY | DPCONTENTS_SOLID;
1278 if(autocvar_g_playerclip_collisions)
1279 this.dphitcontentsmask |= DPCONTENTS_PLAYERCLIP;
1281 if(autocvar_g_nodepthtestplayers)
1282 this.effects |= EF_NODEPTHTEST;
1284 if(autocvar_g_fullbrightplayers)
1285 this.effects |= EF_FULLBRIGHT;
1287 _setmodel(this.vehicle_hudmodel, info.hud_model);
1288 setmodel(this.vehicle_viewport, MDL_Null);
1290 if(info.head_model != "")
1292 _setmodel(this.tur_head, info.head_model);
1293 setattachment(this.tur_head, this, info.tag_head);
1294 setattachment(this.vehicle_hudmodel, this.tur_head, info.tag_hud);
1295 setattachment(this.vehicle_viewport, this.vehicle_hudmodel, info.tag_view);
1299 setattachment(this.tur_head, this, "");
1300 setattachment(this.vehicle_hudmodel, this, info.tag_hud);
1301 setattachment(this.vehicle_viewport, this.vehicle_hudmodel, info.tag_view);
1304 setsize(this, info.m_mins, info.m_maxs);
1306 info.vr_setup(info, this);
1310 setorigin(this, this.origin);
1311 tracebox(this.origin + '0 0 100', info.m_mins, info.m_maxs, this.origin - '0 0 10000', MOVE_WORLDONLY, this);
1312 setorigin(this, trace_endpos);
1315 this.pos1 = this.origin;
1316 this.pos2 = this.angles;
1317 this.tur_head.team = this.team;
1319 if(this.active == ACTIVE_NOT)
1320 this.nextthink = 0; // wait until activated
1321 else if(autocvar_g_vehicles_delayspawn)
1322 this.nextthink = time + this.respawntime + (random() * autocvar_g_vehicles_delayspawn_jitter);
1324 this.nextthink = time + game_starttime;
1326 if(MUTATOR_CALLHOOK(VehicleInit, this))