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 <common/vehicles/vehicles.qh>
8 #include <server/bot/api.qh>
9 #include <server/client.qh>
10 #include <server/damage.qh>
11 #include <server/items/items.qh>
12 #include <server/weapons/common.qh>
13 #include <server/world.qh>
15 bool SendAuxiliaryXhair(entity this, entity to, int sf)
17 WriteHeader(MSG_ENTITY, ENT_CLIENT_AUXILIARYXHAIR);
18 WriteByte(MSG_ENTITY, sf);
20 WriteByte(MSG_ENTITY, this.cnt);
24 WriteVector(MSG_ENTITY, this.origin);
29 WriteByte(MSG_ENTITY, rint(this.colormod_x * 255));
30 WriteByte(MSG_ENTITY, rint(this.colormod_y * 255));
31 WriteByte(MSG_ENTITY, rint(this.colormod_z * 255));
37 bool AuxiliaryXhair_customize(entity this, entity client)
39 entity e = WaypointSprite_getviewentity(client);
40 entity axh = e.(AuxiliaryXhair[this.cnt]);
41 return axh.owner == this.owner; // cheaply check if the client's axh owner is the same as our real owner
44 .vector axh_prevorigin;
45 .vector axh_prevcolors;
47 void UpdateAuxiliaryXhair(entity own, vector loc, vector clr, int axh_id)
49 if(!IS_REAL_CLIENT(own))
52 axh_id = bound(0, axh_id, MAX_AXH);
53 entity axh = own.(AuxiliaryXhair[axh_id]);
55 if(axh == NULL || wasfreed(axh)) // MADNESS? THIS IS QQQQCCCCCCCCC (wasfreed, why do you exsist? Mario: because of sloppy code like this)
57 axh = new(auxiliary_xhair);
59 //axh.drawonlytoclient = own; // not spectatable
60 setcefc(axh, AuxiliaryXhair_customize);
62 Net_LinkEntity(axh, false, 0, SendAuxiliaryXhair);
65 if(loc != axh.axh_prevorigin)
71 if(clr != axh.axh_prevcolors)
77 own.(AuxiliaryXhair[axh_id]) = axh; // set it anyway...?
80 void CSQCVehicleSetup(entity own, int vehicle_id)
82 if(!IS_REAL_CLIENT(own))
87 WriteHeader(MSG_ONE, TE_CSQC_VEHICLESETUP);
88 WriteByte(MSG_ONE, vehicle_id);
90 if(vehicle_id == 0 || vehicle_id == HUD_NORMAL)
92 for(int i = 0; i < MAX_AXH; ++i)
94 entity axh = own.(AuxiliaryXhair[i]);
95 own.(AuxiliaryXhair[i]) = NULL;
97 if(axh.owner == own && axh != NULL && !wasfreed(axh))
103 void vehicles_locktarget(entity this, float incr, float decr, float _lock_time)
105 if(this.lock_target && IS_DEAD(this.lock_target))
107 this.lock_target = NULL;
108 this.lock_strength = 0;
112 if(this.lock_time > time)
115 if(this.lock_soundtime < time)
117 this.lock_soundtime = time + 0.5;
118 play2(this.owner, "vehicles/locked.wav");
124 if(trace_ent != NULL)
126 if(SAME_TEAM(trace_ent, this))
129 if(IS_DEAD(trace_ent))
132 if(!(IS_VEHICLE(trace_ent) || IS_TURRET(trace_ent)))
135 if(trace_ent.alpha <= 0.5 && trace_ent.alpha != 0)
136 trace_ent = NULL; // invisible
139 if(this.lock_target == NULL && trace_ent != NULL)
140 this.lock_target = trace_ent;
142 if(this.lock_target && trace_ent == this.lock_target)
144 if(this.lock_strength != 1 && this.lock_strength + incr >= 1)
146 play2(this.owner, "vehicles/lock.wav");
147 this.lock_soundtime = time + 0.8;
149 else if (this.lock_strength != 1 && this.lock_soundtime < time)
151 play2(this.owner, "vehicles/locking.wav");
152 this.lock_soundtime = time + 0.3;
156 // Have a locking target
157 // Trace hit current target
158 if(trace_ent == this.lock_target && trace_ent != NULL)
160 this.lock_strength = min(this.lock_strength + incr, 1);
161 if(this.lock_strength == 1)
162 this.lock_time = time + _lock_time;
167 this.lock_strength = max(this.lock_strength - decr * 2, 0);
169 this.lock_strength = max(this.lock_strength - decr, 0);
171 if(this.lock_strength == 0)
172 this.lock_target = NULL;
176 // projectile handling
177 void vehicles_projectile_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
179 // Ignore damage from oterh projectiles from my owner (dont mess up volly's)
180 if(inflictor.owner == this.owner)
183 TakeResource(this, RES_HEALTH, damage);
184 this.velocity += force;
185 if(GetResource(this, RES_HEALTH) < 1)
187 this.takedamage = DAMAGE_NO;
188 this.event_damage = func_null;
189 setthink(this, adaptor_think2use);
190 this.nextthink = time;
194 void vehicles_projectile_explode(entity this, entity toucher)
196 if(this.owner && toucher != NULL)
198 if(toucher == this.owner.vehicle)
201 if(toucher == this.owner.vehicle.tur_head)
205 PROJECTILE_TOUCH(this, toucher);
207 this.event_damage = func_null;
208 RadiusDamage (this, this.realowner, this.shot_dmg, 0, this.shot_radius, this, NULL, this.shot_force, this.projectiledeathtype, DMG_NOWEP, toucher);
213 void vehicles_projectile_explode_think(entity this)
215 vehicles_projectile_explode(this, NULL);
218 void vehicles_projectile_explode_use(entity this, entity actor, entity trigger)
220 vehicles_projectile_explode(this, trigger);
223 entity vehicles_projectile(entity this, entity _mzlfx, Sound _mzlsound,
224 vector _org, vector _vel,
225 float _dmg, float _radi, float _force, float _size,
226 int _deahtype, float _projtype, float _health,
227 bool _cull, bool _clianim, entity _owner)
229 TC(Sound, _mzlsound);
230 entity proj = new(vehicles_projectile);
232 PROJECTILE_MAKETRIGGER(proj);
233 setorigin(proj, _org);
235 proj.shot_dmg = _dmg;
236 proj.shot_radius = _radi;
237 proj.shot_force = _force;
238 proj.projectiledeathtype = _deahtype;
239 proj.solid = SOLID_BBOX;
240 set_movetype(proj, MOVETYPE_FLYMISSILE);
241 proj.flags = FL_PROJECTILE;
242 IL_PUSH(g_projectiles, proj);
243 IL_PUSH(g_bot_dodge, proj);
244 proj.bot_dodge = true;
245 proj.bot_dodgerating = _dmg;
246 proj.velocity = _vel;
247 settouch(proj, vehicles_projectile_explode);
248 proj.use = vehicles_projectile_explode_use;
250 proj.realowner = _owner;
251 setthink(proj, SUB_Remove);
252 proj.nextthink = time + 30;
256 proj.takedamage = DAMAGE_AIM;
257 proj.event_damage = vehicles_projectile_damage;
258 SetResourceExplicit(proj, RES_HEALTH, _health);
261 proj.flags |= FL_NOTARGET;
263 if(_mzlsound != SND_Null)
264 sound (this, CH_WEAPON_A, _mzlsound, VOL_BASE, ATTEN_NORM);
266 if(_mzlfx != EFFECT_Null)
267 Send_Effect(_mzlfx, proj.origin, proj.velocity, 1);
269 setsize (proj, '-1 -1 -1' * _size, '1 1 1' * _size);
271 CSQCProjectile(proj, _clianim, _projtype, _cull);
276 void vehicles_gib_explode(entity this)
278 sound (this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
279 Send_Effect(EFFECT_EXPLOSION_SMALL, randomvec() * 80 + (this.origin + '0 0 100'), '0 0 0', 1);
280 Send_Effect(EFFECT_EXPLOSION_SMALL, this.wp00.origin + '0 0 64', '0 0 0', 1);
284 void vehicles_gib_touch(entity this, entity toucher)
286 vehicles_gib_explode(this);
289 void vehicles_gib_think(entity this)
295 this.nextthink = time + 0.1;
298 entity vehicle_tossgib(entity this, entity _template, vector _vel, string _tag, bool _burn, bool _explode, float _maxtime, vector _rot)
300 entity _gib = new(vehicle_gib);
301 _setmodel(_gib, _template.model);
302 vector org = gettaginfo(this, gettagindex(this, _tag));
303 setorigin(_gib, org);
304 _gib.velocity = _vel;
305 set_movetype(_gib, MOVETYPE_TOSS);
306 _gib.solid = SOLID_CORPSE;
307 _gib.colormod = '-0.5 -0.5 -0.5';
308 _gib.effects = EF_LOWPRECISION;
309 _gib.avelocity = _rot;
312 _gib.effects |= EF_FLAME;
316 setthink(_gib, vehicles_gib_explode);
317 _gib.nextthink = time + random() * _explode;
318 settouch(_gib, vehicles_gib_touch);
322 _gib.cnt = time + _maxtime;
323 setthink(_gib, vehicles_gib_think);
324 _gib.nextthink = time + _maxtime - 1;
330 bool vehicle_addplayerslot( entity _owner,
334 bool(entity,float) _framefunc,
335 void(entity,bool) _exitfunc, float(entity, entity) _enterfunc)
337 if(!(_owner.vehicle_flags & VHF_MULTISLOT))
338 _owner.vehicle_flags |= VHF_MULTISLOT;
340 _slot.PlayerPhysplug = _framefunc;
341 _slot.vehicle_exit = _exitfunc;
342 _slot.vehicle_enter = _enterfunc;
343 STAT(HUD, _slot) = _hud;
344 _slot.vehicle_flags = VHF_PLAYERSLOT;
345 _slot.vehicle_viewport = new(vehicle_viewport);
346 _slot.vehicle_hudmodel = new(vehicle_hudmodel);
347 _slot.vehicle_hudmodel.viewmodelforclient = _slot;
348 _slot.vehicle_viewport.effects = (EF_ADDITIVE | EF_DOUBLESIDED | EF_FULLBRIGHT | EF_NODEPTHTEST | EF_NOGUNBOB | EF_NOSHADOW | EF_LOWPRECISION | EF_SELECTABLE | EF_TELEPORT_BIT);
350 setmodel(_slot.vehicle_hudmodel, _hud_model);
351 setmodel(_slot.vehicle_viewport, MDL_Null);
353 setattachment(_slot.vehicle_hudmodel, _slot, "");
354 setattachment(_slot.vehicle_viewport, _slot.vehicle_hudmodel, "");
359 vector vehicle_aimturret(entity _vehic, vector _target, entity _turrret, string _tagname,
360 float _pichlimit_min, float _pichlimit_max,
361 float _rotlimit_min, float _rotlimit_max, float _aimspeed, float dt)
365 vtag = gettaginfo(_turrret, gettagindex(_turrret, _tagname));
366 vtmp = vectoangles(normalize(_target - vtag));
367 vtmp = AnglesTransform_ToAngles(AnglesTransform_LeftDivide(AnglesTransform_FromAngles(_vehic.angles), AnglesTransform_FromAngles(vtmp))) - _turrret.angles;
368 vtmp = AnglesTransform_Normalize(vtmp, true);
369 ftmp = _aimspeed * dt;
370 vtmp_y = bound(-ftmp, vtmp_y, ftmp);
371 vtmp_x = bound(-ftmp, vtmp_x, ftmp);
372 _turrret.angles_y = bound(_rotlimit_min, _turrret.angles_y + vtmp_y, _rotlimit_max);
373 _turrret.angles_x = bound(_pichlimit_min, _turrret.angles_x + vtmp_x, _pichlimit_max);
377 void vehicles_reset_colors(entity this, entity player)
380 const vector cmod = '0 0 0';
381 if(this.team && teamplay)
382 cmap = 1024 + (this.team - 1) * 17;
384 cmap = player.colormap;
387 if(autocvar_g_nodepthtestplayers)
388 eff |= EF_NODEPTHTEST;
389 if(autocvar_g_fullbrightplayers)
390 eff |= EF_FULLBRIGHT;
392 // Find all ents attacked to main model and setup effects, colormod etc.
393 FOREACH_ENTITY_ENT(tag_entity, this,
395 if(it == this.vehicle_shieldent)
404 // Also check head tags
405 FOREACH_ENTITY_ENT(tag_entity, this.tur_head,
407 if(it == this.vehicle_shieldent)
416 this.vehicle_hudmodel.effects = this.effects = eff; // | EF_LOWPRECISION;
417 this.vehicle_hudmodel.colormod = this.colormod = cmod;
418 this.vehicle_hudmodel.colormap = this.colormap = cmap;
419 this.vehicle_viewport.effects = (EF_ADDITIVE | EF_DOUBLESIDED | EF_FULLBRIGHT | EF_NODEPTHTEST | EF_NOGUNBOB | EF_NOSHADOW | EF_LOWPRECISION | EF_SELECTABLE | EF_TELEPORT_BIT);
422 this.avelocity = '0 0 0';
423 this.velocity = '0 0 0';
426 Vehicle info = this.vehicledef; //REGISTRY_GET(Vehicles, this.vehicleid);
427 info.vr_setcolors(info, this);
430 void vehicles_clearreturn(entity veh)
432 // Remove "return helper" entities, if any.
433 IL_EACH(g_vehicle_returners, it.wp00 == veh,
436 setthink(it, SUB_Remove);
437 it.nextthink = time + 0.1;
438 IL_REMOVE(g_vehicle_returners, it);
440 if(it.waypointsprite_attached)
441 WaypointSprite_Kill(it.waypointsprite_attached);
445 void vehicles_spawn(entity this);
446 void vehicles_return(entity this)
448 Send_Effect(EFFECT_TELEPORT, this.wp00.origin + '0 0 64', '0 0 0', 1);
450 setthink(this.wp00, vehicles_spawn);
451 this.wp00.nextthink = time;
453 if(this.waypointsprite_attached)
454 WaypointSprite_Kill(this.waypointsprite_attached);
459 void vehicles_showwp_goaway(entity this)
461 if(this.waypointsprite_attached)
462 WaypointSprite_Kill(this.waypointsprite_attached);
467 void vehicles_showwp(entity this)
473 setthink(ent, vehicles_return);
474 ent.nextthink = ent.cnt;
478 setthink(ent, vehicles_return);
479 ent.nextthink = time + 1;
482 ent.team = this.wp00.team;
483 ent.wp00 = this.wp00;
484 setorigin(ent, this.wp00.pos1);
486 ent.nextthink = time + 5;
487 setthink(ent, vehicles_showwp_goaway);
491 if(teamplay && ent.team)
492 rgb = Team_ColorRGB(ent.team);
495 entity wp = WaypointSprite_Spawn(WP_Vehicle, 0, 0, ent, '0 0 64', NULL, 0, ent, waypointsprite_attached, true, RADARICON_Vehicle);
496 wp.wp_extra = ent.wp00.vehicleid;
498 if(ent.waypointsprite_attached)
500 WaypointSprite_UpdateRule(ent.waypointsprite_attached, ent.wp00.team, SPRITERULE_DEFAULT);
502 WaypointSprite_UpdateBuildFinished(ent.waypointsprite_attached, ent.nextthink);
503 WaypointSprite_Ping(ent.waypointsprite_attached);
507 void vehicles_setreturn(entity veh)
509 vehicles_clearreturn(veh);
511 entity ret = new(vehicle_return);
512 IL_PUSH(g_vehicle_returners, ret);
515 setthink(ret, vehicles_showwp);
519 ret.cnt = time + veh.respawntime;
520 ret.nextthink = min(time + veh.respawntime, time + veh.respawntime - 5);
524 ret.nextthink = min(time + veh.respawntime, time + veh.respawntime - 1);
527 setorigin(ret, veh.pos1 + '0 0 96');
530 void vehicle_use(entity this, entity actor, entity trigger)
532 LOG_DEBUG("vehicle ", this.netname, " used by ", actor.classname);
534 this.tur_head.team = actor.team;
536 if(this.tur_head.team == 0)
537 this.active = ACTIVE_NOT;
539 this.active = ACTIVE_ACTIVE;
541 if(this.active == ACTIVE_ACTIVE && !IS_DEAD(this) && !game_stopped)
543 LOG_DEBUG("Respawning vehicle: ", this.netname);
544 if(this.effects & EF_NODRAW)
546 setthink(this, vehicles_spawn);
547 this.nextthink = time + 3;
551 vehicles_setreturn(this);
552 vehicles_reset_colors(this, actor);
557 void vehicles_regen(entity this, float timer, .float regen_field, float field_max, float rpause, float regen, float delta_time, float _healthscale)
559 if(this.(regen_field) < field_max)
560 if(timer + rpause < time)
563 regen = regen * (GetResource(this, RES_HEALTH) / this.max_health);
565 this.(regen_field) = min(this.(regen_field) + regen * delta_time, field_max);
568 this.owner.(regen_field) = (this.(regen_field) / field_max) * 100;
572 void vehicles_regen_resource(entity this, float timer, .float regen_field, float field_max, float rpause, float regen, float delta_time, float _healthscale, Resource resource)
574 float resource_amount = GetResource(this, resource);
576 if(resource_amount < field_max)
577 if(timer + rpause < time)
580 regen = regen * (resource_amount / this.max_health);
582 SetResource(this, resource, min(resource_amount + regen * delta_time, field_max));
585 this.owner.(regen_field) = (GetResource(this, resource) / field_max) * 100;
589 void shieldhit_think(entity this)
594 // setmodel(this, MDL_Null);
596 this.effects |= EF_NODRAW;
600 this.nextthink = time + 0.1;
604 void vehicles_painframe(entity this)
606 int myhealth = ((this.owner) ? this.owner.vehicle_health : ((GetResource(this, RES_HEALTH) / this.max_health) * 100));
609 if(this.pain_frame < time)
611 float _ftmp = myhealth / 50;
612 this.pain_frame = time + max(0.1, 0.1 + (random() * 0.5 * _ftmp));
613 Send_Effect(EFFECT_SMOKE_SMALL, (this.origin + (randomvec() * 80)), '0 0 0', 1);
615 if(this.vehicle_flags & VHF_DMGSHAKE)
616 this.velocity += randomvec() * 30;
618 if(this.vehicle_flags & VHF_DMGROLL)
620 if(this.vehicle_flags & VHF_DMGHEADROLL)
621 this.tur_head.angles += randomvec();
623 this.angles += randomvec();
628 void vehicles_frame(entity this, entity actor)
630 vehicles_painframe(this);
633 void vehicles_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
635 this.dmg_time = time;
638 if(DEATH_ISWEAPON(deathtype, WEP_VORTEX))
639 damage *= autocvar_g_vehicles_vortex_damagerate;
640 else if(DEATH_ISWEAPON(deathtype, WEP_MACHINEGUN))
641 damage *= autocvar_g_vehicles_machinegun_damagerate;
642 else if(DEATH_ISWEAPON(deathtype, WEP_RIFLE))
643 damage *= autocvar_g_vehicles_rifle_damagerate;
644 else if(DEATH_ISWEAPON(deathtype, WEP_VAPORIZER))
645 damage *= autocvar_g_vehicles_vaporizer_damagerate;
646 else if(DEATH_ISWEAPON(deathtype, WEP_SEEKER))
647 damage *= autocvar_g_vehicles_tag_damagerate;
648 else if(DEATH_WEAPONOF(deathtype) != WEP_Null)
649 damage *= autocvar_g_vehicles_weapon_damagerate;
651 this.enemy = attacker;
653 this.pain_finished = time;
655 if((this.vehicle_flags & VHF_HASSHIELD) && (this.vehicle_shield > 0))
657 if (wasfreed(this.vehicle_shieldent) || this.vehicle_shieldent == NULL)
659 this.vehicle_shieldent = new(vehicle_shieldent);
660 this.vehicle_shieldent.effects = EF_LOWPRECISION;
662 setmodel(this.vehicle_shieldent, MDL_VEH_SHIELD);
663 setattachment(this.vehicle_shieldent, this, "");
664 setorigin(this.vehicle_shieldent, real_origin(this) - this.origin);
665 this.vehicle_shieldent.scale = 256 / vlen(this.maxs - this.mins);
666 setthink(this.vehicle_shieldent, shieldhit_think);
669 this.vehicle_shieldent.colormod = '1 1 1';
670 this.vehicle_shieldent.alpha = 0.45;
671 this.vehicle_shieldent.angles = vectoangles(normalize(hitloc - (this.origin + this.vehicle_shieldent.origin))) - this.angles;
672 this.vehicle_shieldent.nextthink = time;
673 this.vehicle_shieldent.effects &= ~EF_NODRAW;
675 this.vehicle_shield -= damage;
677 if(this.vehicle_shield < 0)
679 TakeResource(this, RES_HEALTH, fabs(this.vehicle_shield));
680 this.vehicle_shieldent.colormod = '2 0 0';
681 this.vehicle_shield = 0;
682 this.vehicle_shieldent.alpha = 0.75;
684 if(sound_allowed(MSG_BROADCAST, attacker))
685 spamsound (this, CH_PAIN, SND_ONS_HIT2, VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
688 if(sound_allowed(MSG_BROADCAST, attacker))
689 spamsound (this, CH_PAIN, SND_ONS_ELECTRICITY_EXPLODE, VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
694 TakeResource(this, RES_HEALTH, damage);
696 if(sound_allowed(MSG_BROADCAST, attacker))
697 spamsound (this, CH_PAIN, SND_ONS_HIT2, VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
700 if(this.damageforcescale < 1 && this.damageforcescale > 0)
701 this.velocity += force * this.damageforcescale;
703 this.velocity += force;
705 if(GetResource(this, RES_HEALTH) <= 0)
709 if(this.vehicle_flags & VHF_DEATHEJECT)
710 vehicles_exit(this, VHEF_EJECT);
712 vehicles_exit(this, VHEF_RELEASE);
715 antilag_clear(this, this);
717 Vehicle info = this.vehicledef; //REGISTRY_GET(Vehicles, this.vehicleid);
718 info.vr_death(info, this);
719 vehicles_setreturn(this);
723 bool vehicles_heal(entity targ, entity inflictor, float amount, float limit)
725 float true_limit = ((limit != RES_LIMIT_NONE) ? limit : targ.max_health);
726 if(GetResource(targ, RES_HEALTH) <= 0 || GetResource(targ, RES_HEALTH) >= true_limit)
729 GiveResourceWithLimit(targ, RES_HEALTH, amount, true_limit);
731 targ.owner.vehicle_health = (GetResource(targ, RES_HEALTH) / targ.max_health) * 100;
735 bool vehicles_crushable(entity e)
737 if(IS_PLAYER(e) && time >= e.vehicle_enter_delay)
746 void vehicles_impact(entity this, float _minspeed, float _speedfac, float _maxpain)
748 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
751 if(this.play_time < time)
753 if(vdist(this.velocity - this.oldvelocity, >, _minspeed))
755 float wc = vlen(this.velocity - this.oldvelocity);
756 float take = min(_speedfac * wc, _maxpain);
757 Damage(this, NULL, NULL, take, DEATH_FALL.m_id, DMG_NOWEP, this.origin, '0 0 0');
758 this.play_time = time + 0.25;
763 // vehicle enter/exit handling
764 vector vehicles_findgoodexit(entity this, entity player, vector prefer_spot)
766 // TODO: we actually want the player's size here
767 tracebox(this.origin + '0 0 32', PL_MIN_CONST, PL_MAX_CONST, prefer_spot, MOVE_NORMAL, player);
768 if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
771 float mysize = 1.5 * vlen(this.maxs - this.mins);
773 vector v2 = 0.5 * (this.absmin + this.absmax);
774 for(int i = 0; i < autocvar_g_vehicles_exit_attempts; ++i)
778 v = v2 + normalize(v) * mysize;
779 tracebox(v2, PL_MIN_CONST, PL_MAX_CONST, v, MOVE_NORMAL, player);
780 if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
787 .int old_vehicle_flags;
788 void vehicles_exit(entity vehic, bool eject)
790 entity player = vehic.owner;
792 if(vehicles_exit_running)
794 LOG_TRACE("^1vehicles_exit already running! this is not good...");
798 vehicles_exit_running = true;
800 if(vehic.vehicle_flags & VHF_PLAYERSLOT)
802 vehic.vehicle_exit(vehic, eject);
803 vehicles_exit_running = false;
809 if (IS_REAL_CLIENT(player))
812 WriteByte (MSG_ONE, SVC_SETVIEWPORT);
813 WriteEntity( MSG_ONE, player);
815 // NOTE: engine networked
816 WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
817 WriteAngle(MSG_ONE, 0);
818 WriteAngle(MSG_ONE, vehic.angles_y);
819 WriteAngle(MSG_ONE, 0);
822 player.takedamage = DAMAGE_AIM;
823 player.solid = SOLID_SLIDEBOX;
824 set_movetype(player, MOVETYPE_WALK);
825 player.effects &= ~EF_NODRAW;
826 player.teleportable = TELEPORT_NORMAL;
827 player.alpha = default_player_alpha;
828 player.PlayerPhysplug = func_null;
829 player.vehicle = NULL;
830 player.view_ofs = STAT(PL_VIEW_OFS, player);
831 player.event_damage = PlayerDamage;
832 STAT(HUD, player) = HUD_NORMAL;
833 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++ slot)
835 .entity weaponentity = weaponentities[slot];
836 player.(weaponentity).m_switchweapon = vehic.(weaponentity).m_switchweapon;
837 delete(vehic.(weaponentity)); // no longer needed
839 player.last_vehiclecheck = time + 3;
840 player.vehicle_enter_delay = time + 2;
841 setsize(player, STAT(PL_MIN, player), STAT(PL_MAX, player));
843 CSQCVehicleSetup(player, HUD_NORMAL);
845 Kill_Notification(NOTIF_ONE, player, MSG_CENTER, CPID_VEHICLES);
846 Kill_Notification(NOTIF_ONE, player, MSG_CENTER, CPID_VEHICLES_OTHER); // kill all vehicle notifications when exiting a vehicle?
849 vehic.flags |= FL_NOTARGET;
852 vehic.avelocity = '0 0 0';
854 vehic.tur_head.nodrawtoclient = NULL;
859 WaypointSprite_Kill(vehic.wps_intruder);
861 MUTATOR_CALLHOOK(VehicleExit, player, vehic);
863 vehic.team = vehic.tur_head.team;
865 if(vehic.old_vehicle_flags & VHF_SHIELDREGEN)
866 vehic.vehicle_flags |= VHF_SHIELDREGEN;
867 vehic.old_vehicle_flags = 0;
869 sound (vehic, CH_TRIGGER_SINGLE, SND_Null, 1, ATTEN_NORM);
870 vehic.vehicle_hudmodel.viewmodelforclient = vehic;
871 vehic.phase = time + 1;
873 vehic.vehicle_exit(vehic, eject);
875 vehicles_setreturn(vehic);
876 vehicles_reset_colors(vehic, NULL);
879 CSQCMODEL_AUTOINIT(vehic);
882 player.oldorigin = player.origin; // player's location is set by the exit functions, so we need to do this after everything
884 vehicles_exit_running = false;
887 void vehicles_touch(entity this, entity toucher)
889 if(MUTATOR_CALLHOOK(VehicleTouch, this, toucher))
892 // Vehicle currently in use
896 if((this.origin_z + this.maxs_z) > (toucher.origin_z))
897 if(vehicles_crushable(toucher))
898 if(!weaponLocked(this.owner))
900 if(vdist(this.velocity, >=, autocvar_g_vehicles_crush_minspeed))
901 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);
903 return; // Dont do selfdamage when hitting "soft targets".
906 if(this.play_time < time) {
907 Vehicle info = this.vehicledef; //REGISTRY_GET(Vehicles, this.vehicleid);
908 info.vr_impact(info, this);
914 if(autocvar_g_vehicles_enter)
917 vehicles_enter(toucher, this);
920 bool vehicle_impulse(entity this, int imp)
922 entity v = this.vehicle;
923 if (!v) return false;
924 if (IS_DEAD(v)) return false;
925 bool(entity,int) f = v.vehicles_impulse;
926 if (f && f(this,imp)) return true;
929 case IMP_weapon_drop.impulse:
931 stuffcmd(this, "\ntoggle cl_eventchase_vehicle\nset _vehicles_shownchasemessage 1\n");
938 void vehicles_enter(entity pl, entity veh)
940 // Remove this when bots know how to use vehicles
941 if((IS_BOT_CLIENT(pl) && !autocvar_g_vehicles_allow_bots))
945 || (veh.phase >= time)
946 || (pl.vehicle_enter_delay >= time)
947 || (STAT(FROZEN, pl))
952 Vehicle info = veh.vehicledef; //REGISTRY_GET(Vehicles, veh.vehicleid);
954 if(autocvar_g_vehicles_enter) // vehicle's touch function should handle this if entering via use key is disabled (TODO)
955 if(veh.vehicle_flags & VHF_MULTISLOT)
956 if(veh.owner && SAME_TEAM(pl, veh))
958 // we don't need a return value or anything here
959 // if successful the owner check below will prevent anything weird
960 info.vr_gunner_enter(info, veh, pl);
964 return; // got here and didn't enter the gunner, return
968 if(DIFF_TEAM(pl, veh))
969 if(autocvar_g_vehicles_steal)
971 FOREACH_CLIENT(IS_PLAYER(it) && SAME_TEAM(it, veh), Send_Notification(NOTIF_ONE, it, MSG_CENTER, CENTER_VEHICLE_STEAL));
973 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_VEHICLE_STEAL_SELF);
975 veh.vehicle_shield = 0;
976 veh.old_vehicle_flags = veh.vehicle_flags; // make a backup just so we're not permanently crippling this vehicle
977 veh.vehicle_flags &= ~VHF_SHIELDREGEN;
979 if (autocvar_g_vehicles_steal_show_waypoint) {
980 entity wp = WaypointSprite_Spawn(WP_VehicleIntruder, 0, 0, pl, '0 0 68', NULL, veh.team, veh, wps_intruder, true, RADARICON_DANGER);
981 wp.colormod = Team_ColorRGB(pl.team);
986 RemoveGrapplingHooks(pl);
988 veh.vehicle_ammo1 = 0;
989 veh.vehicle_ammo2 = 0;
990 veh.vehicle_reload1 = 0;
991 veh.vehicle_reload2 = 0;
992 veh.vehicle_energy = 0;
997 // .viewmodelforclient works better.
998 //veh.vehicle_hudmodel.drawonlytoclient = veh.owner;
1000 veh.vehicle_hudmodel.viewmodelforclient = pl;
1003 pl.view_ofs = STAT(PL_VIEW_OFS, pl);
1004 setsize(pl, STAT(PL_MIN, pl), STAT(PL_MAX, pl));
1006 veh.event_damage = vehicles_damage;
1007 veh.event_heal = vehicles_heal;
1009 pl.items &= ~IT_USING_JETPACK;
1010 pl.angles = veh.angles;
1011 pl.takedamage = DAMAGE_NO;
1012 pl.solid = SOLID_NOT;
1013 pl.disableclientprediction = 1; // physics is no longer run, so this won't be reset
1014 set_movetype(pl, MOVETYPE_NOCLIP);
1015 pl.teleportable = false;
1017 pl.event_damage = func_null;
1018 pl.view_ofs = '0 0 0';
1019 veh.colormap = pl.colormap;
1021 veh.tur_head.colormap = pl.colormap;
1022 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
1024 .entity weaponentity = weaponentities[slot];
1025 veh.(weaponentity) = new(temp_wepent);
1026 veh.(weaponentity).m_switchweapon = pl.(weaponentity).m_switchweapon;
1028 STAT(HUD, pl) = veh.vehicleid;
1029 pl.PlayerPhysplug = veh.PlayerPhysplug;
1031 pl.vehicle_ammo1 = veh.vehicle_ammo1;
1032 pl.vehicle_ammo2 = veh.vehicle_ammo2;
1033 pl.vehicle_reload1 = veh.vehicle_reload1;
1034 pl.vehicle_reload2 = veh.vehicle_reload2;
1035 pl.vehicle_energy = veh.vehicle_energy;
1037 // Cant do this, hides attached objects too.
1038 //veh.exteriormodeltoclient = veh.owner;
1039 //veh.tur_head.exteriormodeltoclient = veh.owner;
1042 UNSET_ONGROUND(veh);
1045 veh.flags -= FL_NOTARGET;
1047 vehicles_reset_colors(veh, pl);
1049 if (IS_REAL_CLIENT(pl))
1051 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_VEHICLE_ENTER);
1054 WriteByte (MSG_ONE, SVC_SETVIEWPORT);
1055 WriteEntity(MSG_ONE, veh.vehicle_viewport);
1057 // NOTE: engine networked
1058 WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
1061 WriteAngle(MSG_ONE, veh.tur_head.angles_x + veh.angles_x); // tilt
1062 WriteAngle(MSG_ONE, veh.tur_head.angles_y + veh.angles_y); // yaw
1063 WriteAngle(MSG_ONE, 0); // roll
1067 WriteAngle(MSG_ONE, veh.angles_x * -1); // tilt
1068 WriteAngle(MSG_ONE, veh.angles_y); // yaw
1069 WriteAngle(MSG_ONE, 0); // roll
1073 vehicles_clearreturn(veh);
1075 CSQCVehicleSetup(pl, veh.vehicleid);
1077 MUTATOR_CALLHOOK(VehicleEnter, pl, veh);
1079 CSQCModel_UnlinkEntity(veh);
1080 info.vr_enter(info, veh);
1082 antilag_clear(pl, CS(pl));
1085 void vehicles_think(entity this)
1087 this.nextthink = time + autocvar_g_vehicles_thinkrate;
1090 STAT(VEHICLESTAT_W2MODE, this.owner) = STAT(VEHICLESTAT_W2MODE, this);
1092 Vehicle info = this.vehicledef; //REGISTRY_GET(Vehicles, this.vehicleid);
1093 info.vr_think(info, this);
1095 vehicles_painframe(this);
1097 CSQCMODEL_AUTOUPDATE(this);
1100 void vehicles_reset(entity this)
1103 vehicles_exit(this, VHEF_RELEASE);
1105 vehicles_clearreturn(this);
1107 if(this.active != ACTIVE_NOT)
1108 vehicles_spawn(this);
1112 void vehicles_spawn(entity this)
1114 LOG_DEBUG("Spawning vehicle: ", this.classname);
1117 this.vehicle_hudmodel.viewmodelforclient = this;
1120 settouch(this, vehicles_touch);
1121 this.event_damage = vehicles_damage;
1122 this.event_heal = vehicles_heal;
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 = this.vehicledef; //REGISTRY_GET(Vehicles, 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)
1182 info.vr_precache(info);
1184 if(this.targetname && this.targetname != "")
1186 this.vehicle_controller = find(NULL, target, this.targetname);
1187 if(!this.vehicle_controller)
1189 LOG_DEBUG("^1WARNING: ^7Vehicle with invalid .targetname");
1190 this.active = ACTIVE_ACTIVE;
1194 this.team = this.vehicle_controller.team;
1195 this.use = vehicle_use;
1199 if(this.vehicle_controller.team == 0)
1200 this.active = ACTIVE_NOT;
1202 this.active = ACTIVE_ACTIVE;
1206 else { this.active = ACTIVE_ACTIVE; }
1208 if(this.team && (!teamplay || !autocvar_g_vehicles_teams))
1211 if(this.mdl == "" || !this.mdl)
1212 _setmodel(this, info.model);
1214 _setmodel(this, this.mdl);
1216 this.vehicle_flags |= VHF_ISVEHICLE;
1218 this.vehicle_viewport = new(vehicle_viewport);
1219 this.vehicle_hudmodel = new(vehicle_hudmodel);
1220 this.tur_head = new(tur_head);
1221 this.tur_head.owner = this;
1222 this.takedamage = DAMAGE_NO;
1223 this.bot_attack = true;
1224 IL_PUSH(g_bot_targets, this);
1225 this.iscreature = true;
1226 this.teleportable = false; // no teleporting for vehicles, too buggy
1227 this.damagedbycontents = true;
1228 IL_PUSH(g_damagedbycontents, this);
1229 this.vehicleid = info.vehicleid;
1230 this.vehicledef = info;
1231 this.PlayerPhysplug = info.PlayerPhysplug;
1232 this.event_damage = func_null;
1233 this.event_heal = func_null;
1234 settouch(this, vehicles_touch);
1235 setthink(this, vehicles_spawn);
1236 this.nextthink = time;
1237 this.effects = EF_NODRAW;
1238 this.dphitcontentsmask = DPCONTENTS_BODY | DPCONTENTS_SOLID;
1240 if(autocvar_g_playerclip_collisions)
1241 this.dphitcontentsmask |= DPCONTENTS_PLAYERCLIP;
1243 if(autocvar_g_nodepthtestplayers)
1244 this.effects |= EF_NODEPTHTEST;
1246 if(autocvar_g_fullbrightplayers)
1247 this.effects |= EF_FULLBRIGHT;
1249 _setmodel(this.vehicle_hudmodel, info.hud_model);
1250 setmodel(this.vehicle_viewport, MDL_Null);
1252 if(info.head_model != "")
1254 _setmodel(this.tur_head, info.head_model);
1255 setattachment(this.tur_head, this, info.tag_head);
1256 setattachment(this.vehicle_hudmodel, this.tur_head, info.tag_hud);
1257 setattachment(this.vehicle_viewport, this.vehicle_hudmodel, info.tag_view);
1261 setattachment(this.tur_head, this, "");
1262 setattachment(this.vehicle_hudmodel, this, info.tag_hud);
1263 setattachment(this.vehicle_viewport, this.vehicle_hudmodel, info.tag_view);
1266 setsize(this, info.m_mins, info.m_maxs);
1268 info.vr_setup(info, this);
1272 setorigin(this, this.origin);
1273 tracebox(this.origin + '0 0 100', info.m_mins, info.m_maxs, this.origin - '0 0 10000', MOVE_WORLDONLY, this);
1274 setorigin(this, trace_endpos);
1277 this.pos1 = this.origin;
1278 this.pos2 = this.angles;
1279 this.tur_head.team = this.team;
1281 if(this.active == ACTIVE_NOT)
1282 this.nextthink = 0; // wait until activated
1283 else if(autocvar_g_vehicles_delayspawn)
1284 this.nextthink = time + this.respawntime + (random() * autocvar_g_vehicles_delayspawn_jitter);
1286 this.nextthink = time + game_starttime;
1288 if(MUTATOR_CALLHOOK(VehicleInit, this))