1 #include "sv_vehicles.qh"
3 #include <server/g_damage.qh>
4 #include <server/items/items.qh>
5 #include <common/mapobjects/defs.qh>
6 #include <common/mapobjects/teleporters.qh>
7 #include <server/sv_main.qh>
8 #include <server/weapons/common.qh>
10 bool SendAuxiliaryXhair(entity this, entity to, int sf)
12 WriteHeader(MSG_ENTITY, ENT_CLIENT_AUXILIARYXHAIR);
13 WriteByte(MSG_ENTITY, sf);
15 WriteByte(MSG_ENTITY, this.cnt);
19 WriteVector(MSG_ENTITY, this.origin);
24 WriteByte(MSG_ENTITY, rint(this.colormod_x * 255));
25 WriteByte(MSG_ENTITY, rint(this.colormod_y * 255));
26 WriteByte(MSG_ENTITY, rint(this.colormod_z * 255));
32 bool AuxiliaryXhair_customize(entity this, entity client)
34 entity e = WaypointSprite_getviewentity(client);
35 entity axh = e.(AuxiliaryXhair[this.cnt]);
36 return axh.owner == this.owner; // cheaply check if the client's axh owner is the same as our real owner
39 .vector axh_prevorigin;
40 .vector axh_prevcolors;
42 void UpdateAuxiliaryXhair(entity own, vector loc, vector clr, int axh_id)
44 if(!IS_REAL_CLIENT(own))
47 axh_id = bound(0, axh_id, MAX_AXH);
48 entity axh = own.(AuxiliaryXhair[axh_id]);
50 if(axh == NULL || wasfreed(axh)) // MADNESS? THIS IS QQQQCCCCCCCCC (wasfreed, why do you exsist? Mario: because of sloppy code like this)
52 axh = new(auxiliary_xhair);
54 //axh.drawonlytoclient = own; // not spectatable
55 setcefc(axh, AuxiliaryXhair_customize);
57 Net_LinkEntity(axh, false, 0, SendAuxiliaryXhair);
60 if(loc != axh.axh_prevorigin)
66 if(clr != axh.axh_prevcolors)
72 own.(AuxiliaryXhair[axh_id]) = axh; // set it anyway...?
75 void CSQCVehicleSetup(entity own, int vehicle_id)
77 if(!IS_REAL_CLIENT(own))
82 WriteHeader(MSG_ONE, TE_CSQC_VEHICLESETUP);
83 WriteByte(MSG_ONE, vehicle_id);
85 if(vehicle_id == 0 || vehicle_id == HUD_NORMAL)
87 for(int i = 0; i < MAX_AXH; ++i)
89 entity axh = own.(AuxiliaryXhair[i]);
90 own.(AuxiliaryXhair[i]) = NULL;
92 if(axh.owner == own && axh != NULL && !wasfreed(axh))
98 void vehicles_locktarget(entity this, float incr, float decr, float _lock_time)
100 if(this.lock_target && IS_DEAD(this.lock_target))
102 this.lock_target = NULL;
103 this.lock_strength = 0;
107 if(this.lock_time > time)
110 if(this.lock_soundtime < time)
112 this.lock_soundtime = time + 0.5;
113 play2(this.owner, "vehicles/locked.wav");
119 if(trace_ent != NULL)
121 if(SAME_TEAM(trace_ent, this))
124 if(IS_DEAD(trace_ent))
127 if(!(IS_VEHICLE(trace_ent) || IS_TURRET(trace_ent)))
130 if(trace_ent.alpha <= 0.5 && trace_ent.alpha != 0)
131 trace_ent = NULL; // invisible
134 if(this.lock_target == NULL && trace_ent != NULL)
135 this.lock_target = trace_ent;
137 if(this.lock_target && trace_ent == this.lock_target)
139 if(this.lock_strength != 1 && this.lock_strength + incr >= 1)
141 play2(this.owner, "vehicles/lock.wav");
142 this.lock_soundtime = time + 0.8;
144 else if (this.lock_strength != 1 && this.lock_soundtime < time)
146 play2(this.owner, "vehicles/locking.wav");
147 this.lock_soundtime = time + 0.3;
151 // Have a locking target
152 // Trace hit current target
153 if(trace_ent == this.lock_target && trace_ent != NULL)
155 this.lock_strength = min(this.lock_strength + incr, 1);
156 if(this.lock_strength == 1)
157 this.lock_time = time + _lock_time;
162 this.lock_strength = max(this.lock_strength - decr * 2, 0);
164 this.lock_strength = max(this.lock_strength - decr, 0);
166 if(this.lock_strength == 0)
167 this.lock_target = NULL;
171 float vehicle_altitude(entity this, float amax)
173 tracebox(this.origin, this.mins, this.maxs, this.origin - ('0 0 1' * amax), MOVE_WORLDONLY, this);
174 return vlen(this.origin - trace_endpos);
177 vector vehicles_force_fromtag_hover(entity this, string tag_name, float spring_length, float max_power)
179 force_fromtag_origin = gettaginfo(this, gettagindex(this, tag_name));
180 v_forward = normalize(v_forward) * -1;
181 traceline(force_fromtag_origin, force_fromtag_origin - (v_forward * spring_length), MOVE_NORMAL, this);
183 force_fromtag_power = (1 - trace_fraction) * max_power;
184 force_fromtag_normpower = force_fromtag_power / max_power;
186 return v_forward * force_fromtag_power;
189 vector vehicles_force_fromtag_maglev(entity this, string tag_name, float spring_length, float max_power)
191 force_fromtag_origin = gettaginfo(this, gettagindex(this, tag_name));
192 v_forward = normalize(v_forward) * -1;
193 traceline(force_fromtag_origin, force_fromtag_origin - (v_forward * spring_length), MOVE_NORMAL, this);
195 // TODO - this may NOT be compatible with wall/celing movement, unhardcode 0.25 (engine count multiplier)
196 if(trace_fraction == 1.0)
198 force_fromtag_normpower = -0.25;
202 force_fromtag_power = ((1 - trace_fraction) - trace_fraction) * max_power;
203 force_fromtag_normpower = force_fromtag_power / max_power;
205 return v_forward * force_fromtag_power;
208 // projectile handling
209 void vehicles_projectile_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
211 // Ignore damage from oterh projectiles from my owner (dont mess up volly's)
212 if(inflictor.owner == this.owner)
215 TakeResource(this, RES_HEALTH, damage);
216 this.velocity += force;
217 if(GetResource(this, RES_HEALTH) < 1)
219 this.takedamage = DAMAGE_NO;
220 this.event_damage = func_null;
221 setthink(this, adaptor_think2use);
222 this.nextthink = time;
226 void vehicles_projectile_explode(entity this, entity toucher)
228 if(this.owner && toucher != NULL)
230 if(toucher == this.owner.vehicle)
233 if(toucher == this.owner.vehicle.tur_head)
237 PROJECTILE_TOUCH(this, toucher);
239 this.event_damage = func_null;
240 RadiusDamage (this, this.realowner, this.shot_dmg, 0, this.shot_radius, this, NULL, this.shot_force, this.projectiledeathtype, DMG_NOWEP, toucher);
245 void vehicles_projectile_explode_think(entity this)
247 vehicles_projectile_explode(this, NULL);
250 void vehicles_projectile_explode_use(entity this, entity actor, entity trigger)
252 vehicles_projectile_explode(this, trigger);
255 entity vehicles_projectile(entity this, entity _mzlfx, Sound _mzlsound,
256 vector _org, vector _vel,
257 float _dmg, float _radi, float _force, float _size,
258 int _deahtype, float _projtype, float _health,
259 bool _cull, bool _clianim, entity _owner)
261 TC(Sound, _mzlsound);
266 PROJECTILE_MAKETRIGGER(proj);
267 setorigin(proj, _org);
269 proj.shot_dmg = _dmg;
270 proj.shot_radius = _radi;
271 proj.shot_force = _force;
272 proj.projectiledeathtype = _deahtype;
273 proj.solid = SOLID_BBOX;
274 set_movetype(proj, MOVETYPE_FLYMISSILE);
275 proj.flags = FL_PROJECTILE;
276 IL_PUSH(g_projectiles, proj);
277 IL_PUSH(g_bot_dodge, proj);
278 proj.bot_dodge = true;
279 proj.bot_dodgerating = _dmg;
280 proj.velocity = _vel;
281 settouch(proj, vehicles_projectile_explode);
282 proj.use = vehicles_projectile_explode_use;
284 proj.realowner = _owner;
285 setthink(proj, SUB_Remove);
286 proj.nextthink = time + 30;
290 proj.takedamage = DAMAGE_AIM;
291 proj.event_damage = vehicles_projectile_damage;
292 SetResourceExplicit(proj, RES_HEALTH, _health);
295 proj.flags |= FL_NOTARGET;
297 if(_mzlsound != SND_Null)
298 sound (this, CH_WEAPON_A, _mzlsound, VOL_BASE, ATTEN_NORM);
300 if(_mzlfx != EFFECT_Null)
301 Send_Effect(_mzlfx, proj.origin, proj.velocity, 1);
303 setsize (proj, '-1 -1 -1' * _size, '1 1 1' * _size);
305 CSQCProjectile(proj, _clianim, _projtype, _cull);
310 void vehicles_gib_explode(entity this)
312 sound (this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
313 Send_Effect(EFFECT_EXPLOSION_SMALL, randomvec() * 80 + (this.origin + '0 0 100'), '0 0 0', 1);
314 Send_Effect(EFFECT_EXPLOSION_SMALL, this.wp00.origin + '0 0 64', '0 0 0', 1);
318 void vehicles_gib_touch(entity this, entity toucher)
320 vehicles_gib_explode(this);
323 void vehicles_gib_think(entity this)
329 this.nextthink = time + 0.1;
332 entity vehicle_tossgib(entity this, entity _template, vector _vel, string _tag, bool _burn, bool _explode, float _maxtime, vector _rot)
334 entity _gib = spawn();
335 _setmodel(_gib, _template.model);
336 vector org = gettaginfo(this, gettagindex(this, _tag));
337 setorigin(_gib, org);
338 _gib.velocity = _vel;
339 set_movetype(_gib, MOVETYPE_TOSS);
340 _gib.solid = SOLID_CORPSE;
341 _gib.colormod = '-0.5 -0.5 -0.5';
342 _gib.effects = EF_LOWPRECISION;
343 _gib.avelocity = _rot;
346 _gib.effects |= EF_FLAME;
350 setthink(_gib, vehicles_gib_explode);
351 _gib.nextthink = time + random() * _explode;
352 settouch(_gib, vehicles_gib_touch);
356 _gib.cnt = time + _maxtime;
357 setthink(_gib, vehicles_gib_think);
358 _gib.nextthink = time + _maxtime - 1;
364 bool vehicle_addplayerslot( entity _owner,
368 bool(entity,float) _framefunc,
369 void(entity,bool) _exitfunc, float(entity, entity) _enterfunc)
371 if(!(_owner.vehicle_flags & VHF_MULTISLOT))
372 _owner.vehicle_flags |= VHF_MULTISLOT;
374 _slot.PlayerPhysplug = _framefunc;
375 _slot.vehicle_exit = _exitfunc;
376 _slot.vehicle_enter = _enterfunc;
377 STAT(HUD, _slot) = _hud;
378 _slot.vehicle_flags = VHF_PLAYERSLOT;
379 _slot.vehicle_viewport = spawn();
380 _slot.vehicle_hudmodel = spawn();
381 _slot.vehicle_hudmodel.viewmodelforclient = _slot;
382 _slot.vehicle_viewport.effects = (EF_ADDITIVE | EF_DOUBLESIDED | EF_FULLBRIGHT | EF_NODEPTHTEST | EF_NOGUNBOB | EF_NOSHADOW | EF_LOWPRECISION | EF_SELECTABLE | EF_TELEPORT_BIT);
384 setmodel(_slot.vehicle_hudmodel, _hud_model);
385 setmodel(_slot.vehicle_viewport, MDL_Null);
387 setattachment(_slot.vehicle_hudmodel, _slot, "");
388 setattachment(_slot.vehicle_viewport, _slot.vehicle_hudmodel, "");
393 vector vehicle_aimturret(entity _vehic, vector _target, entity _turrret, string _tagname,
394 float _pichlimit_min, float _pichlimit_max,
395 float _rotlimit_min, float _rotlimit_max, float _aimspeed, float dt)
399 vtag = gettaginfo(_turrret, gettagindex(_turrret, _tagname));
400 vtmp = vectoangles(normalize(_target - vtag));
401 vtmp = AnglesTransform_ToAngles(AnglesTransform_LeftDivide(AnglesTransform_FromAngles(_vehic.angles), AnglesTransform_FromAngles(vtmp))) - _turrret.angles;
402 vtmp = AnglesTransform_Normalize(vtmp, true);
403 ftmp = _aimspeed * dt;
404 vtmp_y = bound(-ftmp, vtmp_y, ftmp);
405 vtmp_x = bound(-ftmp, vtmp_x, ftmp);
406 _turrret.angles_y = bound(_rotlimit_min, _turrret.angles_y + vtmp_y, _rotlimit_max);
407 _turrret.angles_x = bound(_pichlimit_min, _turrret.angles_x + vtmp_x, _pichlimit_max);
411 void vehicles_reset_colors(entity this, entity player)
414 const vector cmod = '0 0 0';
415 if(this.team && teamplay)
416 cmap = 1024 + (this.team - 1) * 17;
418 cmap = player.colormap;
421 if(autocvar_g_nodepthtestplayers)
422 eff |= EF_NODEPTHTEST;
423 if(autocvar_g_fullbrightplayers)
424 eff |= EF_FULLBRIGHT;
426 // Find all ents attacked to main model and setup effects, colormod etc.
427 FOREACH_ENTITY_ENT(tag_entity, this,
429 if(it == this.vehicle_shieldent)
438 // Also check head tags
439 FOREACH_ENTITY_ENT(tag_entity, this.tur_head,
441 if(it == this.vehicle_shieldent)
450 this.vehicle_hudmodel.effects = this.effects = eff; // | EF_LOWPRECISION;
451 this.vehicle_hudmodel.colormod = this.colormod = cmod;
452 this.vehicle_hudmodel.colormap = this.colormap = cmap;
453 this.vehicle_viewport.effects = (EF_ADDITIVE | EF_DOUBLESIDED | EF_FULLBRIGHT | EF_NODEPTHTEST | EF_NOGUNBOB | EF_NOSHADOW | EF_LOWPRECISION | EF_SELECTABLE | EF_TELEPORT_BIT);
456 this.avelocity = '0 0 0';
457 this.velocity = '0 0 0';
460 Vehicle info = this.vehicledef; //REGISTRY_GET(Vehicles, this.vehicleid);
461 info.vr_setcolors(info, this);
464 void vehicles_clearreturn(entity veh)
466 // Remove "return helper" entities, if any.
467 IL_EACH(g_vehicle_returners, it.wp00 == veh,
470 setthink(it, SUB_Remove);
471 it.nextthink = time + 0.1;
472 IL_REMOVE(g_vehicle_returners, it);
474 if(it.waypointsprite_attached)
475 WaypointSprite_Kill(it.waypointsprite_attached);
479 void vehicles_spawn(entity this);
480 void vehicles_return(entity this)
482 Send_Effect(EFFECT_TELEPORT, this.wp00.origin + '0 0 64', '0 0 0', 1);
484 setthink(this.wp00, vehicles_spawn);
485 this.wp00.nextthink = time;
487 if(this.waypointsprite_attached)
488 WaypointSprite_Kill(this.waypointsprite_attached);
493 void vehicles_showwp_goaway(entity this)
495 if(this.waypointsprite_attached)
496 WaypointSprite_Kill(this.waypointsprite_attached);
501 void vehicles_showwp(entity this)
507 setthink(ent, vehicles_return);
508 ent.nextthink = ent.cnt;
512 setthink(ent, vehicles_return);
513 ent.nextthink = time + 1;
516 ent.team = this.wp00.team;
517 ent.wp00 = this.wp00;
518 setorigin(ent, this.wp00.pos1);
520 ent.nextthink = time + 5;
521 setthink(ent, vehicles_showwp_goaway);
525 if(teamplay && ent.team)
526 rgb = Team_ColorRGB(ent.team);
529 entity wp = WaypointSprite_Spawn(WP_Vehicle, 0, 0, ent, '0 0 64', NULL, 0, ent, waypointsprite_attached, true, RADARICON_Vehicle);
530 wp.wp_extra = ent.wp00.vehicleid;
532 if(ent.waypointsprite_attached)
534 WaypointSprite_UpdateRule(ent.waypointsprite_attached, ent.wp00.team, SPRITERULE_DEFAULT);
536 WaypointSprite_UpdateBuildFinished(ent.waypointsprite_attached, ent.nextthink);
537 WaypointSprite_Ping(ent.waypointsprite_attached);
541 void vehicles_setreturn(entity veh)
543 vehicles_clearreturn(veh);
545 entity ret = new(vehicle_return);
546 IL_PUSH(g_vehicle_returners, ret);
549 setthink(ret, vehicles_showwp);
553 ret.cnt = time + veh.respawntime;
554 ret.nextthink = min(time + veh.respawntime, time + veh.respawntime - 5);
558 ret.nextthink = min(time + veh.respawntime, time + veh.respawntime - 1);
561 setorigin(ret, veh.pos1 + '0 0 96');
564 void vehicle_use(entity this, entity actor, entity trigger)
566 LOG_DEBUG("vehicle ", this.netname, " used by ", actor.classname);
568 this.tur_head.team = actor.team;
570 if(this.tur_head.team == 0)
571 this.active = ACTIVE_NOT;
573 this.active = ACTIVE_ACTIVE;
575 if(this.active == ACTIVE_ACTIVE && !IS_DEAD(this) && !game_stopped)
577 LOG_DEBUG("Respawning vehicle: ", this.netname);
578 if(this.effects & EF_NODRAW)
580 setthink(this, vehicles_spawn);
581 this.nextthink = time + 3;
585 vehicles_setreturn(this);
586 vehicles_reset_colors(this, actor);
591 void vehicles_regen(entity this, float timer, .float regen_field, float field_max, float rpause, float regen, float delta_time, float _healthscale)
593 if(this.(regen_field) < field_max)
594 if(timer + rpause < time)
597 regen = regen * (GetResource(this, RES_HEALTH) / this.max_health);
599 this.(regen_field) = min(this.(regen_field) + regen * delta_time, field_max);
602 this.owner.(regen_field) = (this.(regen_field) / field_max) * 100;
606 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)
608 float resource_amount = GetResource(this, resource);
610 if(resource_amount < field_max)
611 if(timer + rpause < time)
614 regen = regen * (resource_amount / this.max_health);
616 SetResource(this, resource, min(resource_amount + regen * delta_time, field_max));
619 this.owner.(regen_field) = (GetResource(this, resource) / field_max) * 100;
623 void shieldhit_think(entity this)
628 // setmodel(this, MDL_Null);
630 this.effects |= EF_NODRAW;
634 this.nextthink = time + 0.1;
638 void vehicles_painframe(entity this)
640 int myhealth = ((this.owner) ? this.owner.vehicle_health : ((GetResource(this, RES_HEALTH) / this.max_health) * 100));
643 if(this.pain_frame < time)
645 float _ftmp = myhealth / 50;
646 this.pain_frame = time + max(0.1, 0.1 + (random() * 0.5 * _ftmp));
647 Send_Effect(EFFECT_SMOKE_SMALL, (this.origin + (randomvec() * 80)), '0 0 0', 1);
649 if(this.vehicle_flags & VHF_DMGSHAKE)
650 this.velocity += randomvec() * 30;
652 if(this.vehicle_flags & VHF_DMGROLL)
654 if(this.vehicle_flags & VHF_DMGHEADROLL)
655 this.tur_head.angles += randomvec();
657 this.angles += randomvec();
662 void vehicles_frame(entity this, entity actor)
664 vehicles_painframe(this);
667 void vehicles_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
669 this.dmg_time = time;
672 if(DEATH_ISWEAPON(deathtype, WEP_VORTEX))
673 damage *= autocvar_g_vehicles_vortex_damagerate;
674 else if(DEATH_ISWEAPON(deathtype, WEP_MACHINEGUN))
675 damage *= autocvar_g_vehicles_machinegun_damagerate;
676 else if(DEATH_ISWEAPON(deathtype, WEP_RIFLE))
677 damage *= autocvar_g_vehicles_rifle_damagerate;
678 else if(DEATH_ISWEAPON(deathtype, WEP_VAPORIZER))
679 damage *= autocvar_g_vehicles_vaporizer_damagerate;
680 else if(DEATH_ISWEAPON(deathtype, WEP_SEEKER))
681 damage *= autocvar_g_vehicles_tag_damagerate;
682 else if(DEATH_WEAPONOF(deathtype) != WEP_Null)
683 damage *= autocvar_g_vehicles_weapon_damagerate;
685 this.enemy = attacker;
687 this.pain_finished = time;
689 if((this.vehicle_flags & VHF_HASSHIELD) && (this.vehicle_shield > 0))
691 if (wasfreed(this.vehicle_shieldent) || this.vehicle_shieldent == NULL)
693 this.vehicle_shieldent = spawn();
694 this.vehicle_shieldent.effects = EF_LOWPRECISION;
696 setmodel(this.vehicle_shieldent, MDL_VEH_SHIELD);
697 setattachment(this.vehicle_shieldent, this, "");
698 setorigin(this.vehicle_shieldent, real_origin(this) - this.origin);
699 this.vehicle_shieldent.scale = 256 / vlen(this.maxs - this.mins);
700 setthink(this.vehicle_shieldent, shieldhit_think);
703 this.vehicle_shieldent.colormod = '1 1 1';
704 this.vehicle_shieldent.alpha = 0.45;
705 this.vehicle_shieldent.angles = vectoangles(normalize(hitloc - (this.origin + this.vehicle_shieldent.origin))) - this.angles;
706 this.vehicle_shieldent.nextthink = time;
707 this.vehicle_shieldent.effects &= ~EF_NODRAW;
709 this.vehicle_shield -= damage;
711 if(this.vehicle_shield < 0)
713 TakeResource(this, RES_HEALTH, fabs(this.vehicle_shield));
714 this.vehicle_shieldent.colormod = '2 0 0';
715 this.vehicle_shield = 0;
716 this.vehicle_shieldent.alpha = 0.75;
718 if(sound_allowed(MSG_BROADCAST, attacker))
719 spamsound (this, CH_PAIN, SND_ONS_HIT2, VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
722 if(sound_allowed(MSG_BROADCAST, attacker))
723 spamsound (this, CH_PAIN, SND_ONS_ELECTRICITY_EXPLODE, VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
728 TakeResource(this, RES_HEALTH, damage);
730 if(sound_allowed(MSG_BROADCAST, attacker))
731 spamsound (this, CH_PAIN, SND_ONS_HIT2, VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
734 if(this.damageforcescale < 1 && this.damageforcescale > 0)
735 this.velocity += force * this.damageforcescale;
737 this.velocity += force;
739 if(GetResource(this, RES_HEALTH) <= 0)
743 if(this.vehicle_flags & VHF_DEATHEJECT)
744 vehicles_exit(this, VHEF_EJECT);
746 vehicles_exit(this, VHEF_RELEASE);
749 antilag_clear(this, this);
751 Vehicle info = this.vehicledef; //REGISTRY_GET(Vehicles, this.vehicleid);
752 info.vr_death(info, this);
753 vehicles_setreturn(this);
757 bool vehicles_heal(entity targ, entity inflictor, float amount, float limit)
759 float true_limit = ((limit != RES_LIMIT_NONE) ? limit : targ.max_health);
760 if(GetResource(targ, RES_HEALTH) <= 0 || GetResource(targ, RES_HEALTH) >= true_limit)
763 GiveResourceWithLimit(targ, RES_HEALTH, amount, true_limit);
765 targ.owner.vehicle_health = (GetResource(targ, RES_HEALTH) / targ.max_health) * 100;
769 bool vehicles_crushable(entity e)
771 if(IS_PLAYER(e) && time >= e.vehicle_enter_delay)
780 void vehicles_impact(entity this, float _minspeed, float _speedfac, float _maxpain)
782 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
785 if(this.play_time < time)
787 if(vdist(this.velocity - this.oldvelocity, >, _minspeed))
789 float wc = vlen(this.velocity - this.oldvelocity);
790 float take = min(_speedfac * wc, _maxpain);
791 Damage(this, NULL, NULL, take, DEATH_FALL.m_id, DMG_NOWEP, this.origin, '0 0 0');
792 this.play_time = time + 0.25;
797 // vehicle enter/exit handling
798 vector vehicles_findgoodexit(entity this, entity player, vector prefer_spot)
800 // TODO: we actually want the player's size here
801 tracebox(this.origin + '0 0 32', PL_MIN_CONST, PL_MAX_CONST, prefer_spot, MOVE_NORMAL, player);
802 if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
805 float mysize = 1.5 * vlen(this.maxs - this.mins);
807 vector v2 = 0.5 * (this.absmin + this.absmax);
808 for(int i = 0; i < autocvar_g_vehicles_exit_attempts; ++i)
812 v = v2 + normalize(v) * mysize;
813 tracebox(v2, PL_MIN_CONST, PL_MAX_CONST, v, MOVE_NORMAL, player);
814 if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
821 .int old_vehicle_flags;
822 void vehicles_exit(entity vehic, bool eject)
824 entity player = vehic.owner;
826 if(vehicles_exit_running)
828 LOG_TRACE("^1vehicles_exit already running! this is not good...");
832 vehicles_exit_running = true;
834 if(vehic.vehicle_flags & VHF_PLAYERSLOT)
836 vehic.vehicle_exit(vehic, eject);
837 vehicles_exit_running = false;
843 if (IS_REAL_CLIENT(player))
846 WriteByte (MSG_ONE, SVC_SETVIEWPORT);
847 WriteEntity( MSG_ONE, player);
849 // NOTE: engine networked
850 WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
851 WriteAngle(MSG_ONE, 0);
852 WriteAngle(MSG_ONE, vehic.angles_y);
853 WriteAngle(MSG_ONE, 0);
856 setsize(player, STAT(PL_MIN, player), STAT(PL_MAX, player));
858 player.takedamage = DAMAGE_AIM;
859 player.solid = SOLID_SLIDEBOX;
860 set_movetype(player, MOVETYPE_WALK);
861 player.effects &= ~EF_NODRAW;
862 player.teleportable = TELEPORT_NORMAL;
863 player.alpha = default_player_alpha;
864 player.PlayerPhysplug = func_null;
865 player.vehicle = NULL;
866 player.view_ofs = STAT(PL_VIEW_OFS, player);
867 player.event_damage = PlayerDamage;
868 STAT(HUD, player) = HUD_NORMAL;
869 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++ slot)
871 .entity weaponentity = weaponentities[slot];
872 player.(weaponentity).m_switchweapon = vehic.(weaponentity).m_switchweapon;
873 delete(vehic.(weaponentity)); // no longer needed
875 player.last_vehiclecheck = time + 3;
876 player.vehicle_enter_delay = time + 2;
878 CSQCVehicleSetup(player, HUD_NORMAL);
880 Kill_Notification(NOTIF_ONE, player, MSG_CENTER, CPID_VEHICLES);
881 Kill_Notification(NOTIF_ONE, player, MSG_CENTER, CPID_VEHICLES_OTHER); // kill all vehicle notifications when exiting a vehicle?
884 vehic.flags |= FL_NOTARGET;
887 vehic.avelocity = '0 0 0';
889 vehic.tur_head.nodrawtoclient = NULL;
894 WaypointSprite_Kill(vehic.wps_intruder);
896 MUTATOR_CALLHOOK(VehicleExit, player, vehic);
898 vehic.team = vehic.tur_head.team;
900 if(vehic.old_vehicle_flags & VHF_SHIELDREGEN)
901 vehic.vehicle_flags |= VHF_SHIELDREGEN;
902 vehic.old_vehicle_flags = 0;
904 sound (vehic, CH_TRIGGER_SINGLE, SND_Null, 1, ATTEN_NORM);
905 vehic.vehicle_hudmodel.viewmodelforclient = vehic;
906 vehic.phase = time + 1;
908 vehic.vehicle_exit(vehic, eject);
910 vehicles_setreturn(vehic);
911 vehicles_reset_colors(vehic, NULL);
914 CSQCMODEL_AUTOINIT(vehic);
917 player.oldorigin = player.origin; // player's location is set by the exit functions, so we need to do this after everything
919 vehicles_exit_running = false;
922 void vehicles_touch(entity this, entity toucher)
924 if(MUTATOR_CALLHOOK(VehicleTouch, this, toucher))
927 // Vehicle currently in use
931 if((this.origin_z + this.maxs_z) > (toucher.origin_z))
932 if(vehicles_crushable(toucher))
933 if(!weaponLocked(this.owner))
935 if(vdist(this.velocity, >=, autocvar_g_vehicles_crush_minspeed))
936 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);
938 return; // Dont do selfdamage when hitting "soft targets".
941 if(this.play_time < time) {
942 Vehicle info = this.vehicledef; //REGISTRY_GET(Vehicles, this.vehicleid);
943 info.vr_impact(info, this);
949 if(autocvar_g_vehicles_enter)
952 vehicles_enter(toucher, this);
955 bool vehicle_impulse(entity this, int imp)
957 entity v = this.vehicle;
958 if (!v) return false;
959 if (IS_DEAD(v)) return false;
960 bool(entity,int) f = v.vehicles_impulse;
961 if (f && f(this,imp)) return true;
964 case IMP_weapon_drop.impulse:
966 stuffcmd(this, "\ntoggle cl_eventchase_vehicle\nset _vehicles_shownchasemessage 1\n");
973 void vehicles_enter(entity pl, entity veh)
975 // Remove this when bots know how to use vehicles
976 if((IS_BOT_CLIENT(pl) && !autocvar_g_vehicles_allow_bots))
980 || (veh.phase >= time)
981 || (pl.vehicle_enter_delay >= time)
982 || (STAT(FROZEN, pl))
987 Vehicle info = veh.vehicledef; //REGISTRY_GET(Vehicles, veh.vehicleid);
989 if(autocvar_g_vehicles_enter) // vehicle's touch function should handle this if entering via use key is disabled (TODO)
990 if(veh.vehicle_flags & VHF_MULTISLOT)
991 if(veh.owner && SAME_TEAM(pl, veh))
993 // we don't need a return value or anything here
994 // if successful the owner check below will prevent anything weird
995 info.vr_gunner_enter(info, veh, pl);
999 return; // got here and didn't enter the gunner, return
1003 if(DIFF_TEAM(pl, veh))
1004 if(autocvar_g_vehicles_steal)
1006 FOREACH_CLIENT(IS_PLAYER(it) && SAME_TEAM(it, veh), Send_Notification(NOTIF_ONE, it, MSG_CENTER, CENTER_VEHICLE_STEAL));
1008 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_VEHICLE_STEAL_SELF);
1010 veh.vehicle_shield = 0;
1011 veh.old_vehicle_flags = veh.vehicle_flags; // make a backup just so we're not permanently crippling this vehicle
1012 veh.vehicle_flags &= ~VHF_SHIELDREGEN;
1014 if (autocvar_g_vehicles_steal_show_waypoint) {
1015 entity wp = WaypointSprite_Spawn(WP_VehicleIntruder, 0, 0, pl, '0 0 68', NULL, veh.team, veh, wps_intruder, true, RADARICON_DANGER);
1016 wp.colormod = Team_ColorRGB(pl.team);
1021 RemoveGrapplingHooks(pl);
1023 veh.vehicle_ammo1 = 0;
1024 veh.vehicle_ammo2 = 0;
1025 veh.vehicle_reload1 = 0;
1026 veh.vehicle_reload2 = 0;
1027 veh.vehicle_energy = 0;
1032 // .viewmodelforclient works better.
1033 //veh.vehicle_hudmodel.drawonlytoclient = veh.owner;
1035 veh.vehicle_hudmodel.viewmodelforclient = pl;
1038 pl.view_ofs = STAT(PL_VIEW_OFS, pl);
1039 setsize(pl, STAT(PL_MIN, pl), STAT(PL_MAX, pl));
1041 veh.event_damage = vehicles_damage;
1042 veh.event_heal = vehicles_heal;
1044 pl.items &= ~IT_USING_JETPACK;
1045 pl.angles = veh.angles;
1046 pl.takedamage = DAMAGE_NO;
1047 pl.solid = SOLID_NOT;
1048 pl.disableclientprediction = 1; // physics is no longer run, so this won't be reset
1049 set_movetype(pl, MOVETYPE_NOCLIP);
1050 pl.teleportable = false;
1052 pl.event_damage = func_null;
1053 pl.view_ofs = '0 0 0';
1054 veh.colormap = pl.colormap;
1056 veh.tur_head.colormap = pl.colormap;
1057 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
1059 .entity weaponentity = weaponentities[slot];
1060 veh.(weaponentity) = new(temp_wepent);
1061 veh.(weaponentity).m_switchweapon = pl.(weaponentity).m_switchweapon;
1063 STAT(HUD, pl) = veh.vehicleid;
1064 pl.PlayerPhysplug = veh.PlayerPhysplug;
1066 pl.vehicle_ammo1 = veh.vehicle_ammo1;
1067 pl.vehicle_ammo2 = veh.vehicle_ammo2;
1068 pl.vehicle_reload1 = veh.vehicle_reload1;
1069 pl.vehicle_reload2 = veh.vehicle_reload2;
1070 pl.vehicle_energy = veh.vehicle_energy;
1072 // Cant do this, hides attached objects too.
1073 //veh.exteriormodeltoclient = veh.owner;
1074 //veh.tur_head.exteriormodeltoclient = veh.owner;
1077 UNSET_ONGROUND(veh);
1080 veh.flags -= FL_NOTARGET;
1082 vehicles_reset_colors(veh, pl);
1084 if (IS_REAL_CLIENT(pl))
1086 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_VEHICLE_ENTER);
1089 WriteByte (MSG_ONE, SVC_SETVIEWPORT);
1090 WriteEntity(MSG_ONE, veh.vehicle_viewport);
1092 // NOTE: engine networked
1093 WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
1096 WriteAngle(MSG_ONE, veh.tur_head.angles_x + veh.angles_x); // tilt
1097 WriteAngle(MSG_ONE, veh.tur_head.angles_y + veh.angles_y); // yaw
1098 WriteAngle(MSG_ONE, 0); // roll
1102 WriteAngle(MSG_ONE, veh.angles_x * -1); // tilt
1103 WriteAngle(MSG_ONE, veh.angles_y); // yaw
1104 WriteAngle(MSG_ONE, 0); // roll
1108 vehicles_clearreturn(veh);
1110 CSQCVehicleSetup(pl, veh.vehicleid);
1112 MUTATOR_CALLHOOK(VehicleEnter, pl, veh);
1114 CSQCModel_UnlinkEntity(veh);
1115 info.vr_enter(info, veh);
1117 antilag_clear(pl, CS(pl));
1120 void vehicles_think(entity this)
1122 this.nextthink = time + autocvar_g_vehicles_thinkrate;
1125 STAT(VEHICLESTAT_W2MODE, this.owner) = STAT(VEHICLESTAT_W2MODE, this);
1127 Vehicle info = this.vehicledef; //REGISTRY_GET(Vehicles, this.vehicleid);
1128 info.vr_think(info, this);
1130 vehicles_painframe(this);
1132 CSQCMODEL_AUTOUPDATE(this);
1135 void vehicles_reset(entity this)
1138 vehicles_exit(this, VHEF_RELEASE);
1140 vehicles_clearreturn(this);
1142 if(this.active != ACTIVE_NOT)
1143 vehicles_spawn(this);
1147 void vehicles_spawn(entity this)
1149 LOG_DEBUG("Spawning vehicle: ", this.classname);
1152 this.vehicle_hudmodel.viewmodelforclient = this;
1155 settouch(this, vehicles_touch);
1156 this.event_damage = vehicles_damage;
1157 this.event_heal = vehicles_heal;
1158 this.reset = vehicles_reset;
1159 this.iscreature = true;
1160 this.teleportable = false; // no teleporting for vehicles, too buggy
1161 this.damagedbycontents = true;
1162 set_movetype(this, MOVETYPE_WALK);
1163 this.solid = SOLID_SLIDEBOX;
1164 this.takedamage = DAMAGE_AIM;
1165 this.deadflag = DEAD_NO;
1166 if(!this.bot_attack)
1167 IL_PUSH(g_bot_targets, this);
1168 this.bot_attack = true;
1169 this.flags = FL_NOTARGET;
1170 this.avelocity = '0 0 0';
1171 this.velocity = '0 0 0';
1172 setthink(this, vehicles_think);
1173 this.nextthink = time;
1176 this.lock_strength = 0;
1177 this.lock_target = NULL;
1178 this.misc_bulletcounter = 0;
1181 this.angles = this.pos2;
1182 setorigin(this, this.pos1);
1184 Send_Effect(EFFECT_TELEPORT, this.origin + '0 0 64', '0 0 0', 1);
1186 if(this.vehicle_controller)
1187 this.team = this.vehicle_controller.team;
1189 FOREACH_CLIENT(IS_PLAYER(it),
1191 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
1193 .entity weaponentity = weaponentities[slot];
1194 if(it.(weaponentity).hook.aiment == this)
1195 RemoveHook(it.(weaponentity).hook);
1200 Vehicle info = this.vehicledef; //REGISTRY_GET(Vehicles, this.vehicleid);
1201 info.vr_spawn(info, this);
1203 vehicles_reset_colors(this, NULL);
1205 CSQCMODEL_AUTOINIT(this);
1208 bool vehicle_initialize(entity this, Vehicle info, bool nodrop)
1210 if(!autocvar_g_vehicles)
1218 info.vr_precache(info);
1219 IL_PUSH(g_vehicles, this);
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))