1 #include "sv_vehicles.qh"
3 #include <server/bot/api.qh>
4 #include <server/g_damage.qh>
5 #include <server/g_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/sv_main.qh>
12 #include <server/weapons/common.qh>
14 bool SendAuxiliaryXhair(entity this, entity to, int sf)
16 WriteHeader(MSG_ENTITY, ENT_CLIENT_AUXILIARYXHAIR);
17 WriteByte(MSG_ENTITY, sf);
19 WriteByte(MSG_ENTITY, this.cnt);
23 WriteVector(MSG_ENTITY, this.origin);
28 WriteByte(MSG_ENTITY, rint(this.colormod_x * 255));
29 WriteByte(MSG_ENTITY, rint(this.colormod_y * 255));
30 WriteByte(MSG_ENTITY, rint(this.colormod_z * 255));
36 bool AuxiliaryXhair_customize(entity this, entity client)
38 entity e = WaypointSprite_getviewentity(client);
39 entity axh = e.(AuxiliaryXhair[this.cnt]);
40 return axh.owner == this.owner; // cheaply check if the client's axh owner is the same as our real owner
43 .vector axh_prevorigin;
44 .vector axh_prevcolors;
46 void UpdateAuxiliaryXhair(entity own, vector loc, vector clr, int axh_id)
48 if(!IS_REAL_CLIENT(own))
51 axh_id = bound(0, axh_id, MAX_AXH);
52 entity axh = own.(AuxiliaryXhair[axh_id]);
54 if(axh == NULL || wasfreed(axh)) // MADNESS? THIS IS QQQQCCCCCCCCC (wasfreed, why do you exsist? Mario: because of sloppy code like this)
56 axh = new(auxiliary_xhair);
58 //axh.drawonlytoclient = own; // not spectatable
59 setcefc(axh, AuxiliaryXhair_customize);
61 Net_LinkEntity(axh, false, 0, SendAuxiliaryXhair);
64 if(loc != axh.axh_prevorigin)
70 if(clr != axh.axh_prevcolors)
76 own.(AuxiliaryXhair[axh_id]) = axh; // set it anyway...?
79 void CSQCVehicleSetup(entity own, int vehicle_id)
81 if(!IS_REAL_CLIENT(own))
86 WriteHeader(MSG_ONE, TE_CSQC_VEHICLESETUP);
87 WriteByte(MSG_ONE, vehicle_id);
89 if(vehicle_id == 0 || vehicle_id == HUD_NORMAL)
91 for(int i = 0; i < MAX_AXH; ++i)
93 entity axh = own.(AuxiliaryXhair[i]);
94 own.(AuxiliaryXhair[i]) = NULL;
96 if(axh.owner == own && axh != NULL && !wasfreed(axh))
102 void vehicles_locktarget(entity this, float incr, float decr, float _lock_time)
104 if(this.lock_target && IS_DEAD(this.lock_target))
106 this.lock_target = NULL;
107 this.lock_strength = 0;
111 if(this.lock_time > time)
114 if(this.lock_soundtime < time)
116 this.lock_soundtime = time + 0.5;
117 play2(this.owner, "vehicles/locked.wav");
123 if(trace_ent != NULL)
125 if(SAME_TEAM(trace_ent, this))
128 if(IS_DEAD(trace_ent))
131 if(!(IS_VEHICLE(trace_ent) || IS_TURRET(trace_ent)))
134 if(trace_ent.alpha <= 0.5 && trace_ent.alpha != 0)
135 trace_ent = NULL; // invisible
138 if(this.lock_target == NULL && trace_ent != NULL)
139 this.lock_target = trace_ent;
141 if(this.lock_target && trace_ent == this.lock_target)
143 if(this.lock_strength != 1 && this.lock_strength + incr >= 1)
145 play2(this.owner, "vehicles/lock.wav");
146 this.lock_soundtime = time + 0.8;
148 else if (this.lock_strength != 1 && this.lock_soundtime < time)
150 play2(this.owner, "vehicles/locking.wav");
151 this.lock_soundtime = time + 0.3;
155 // Have a locking target
156 // Trace hit current target
157 if(trace_ent == this.lock_target && trace_ent != NULL)
159 this.lock_strength = min(this.lock_strength + incr, 1);
160 if(this.lock_strength == 1)
161 this.lock_time = time + _lock_time;
166 this.lock_strength = max(this.lock_strength - decr * 2, 0);
168 this.lock_strength = max(this.lock_strength - decr, 0);
170 if(this.lock_strength == 0)
171 this.lock_target = NULL;
175 float vehicle_altitude(entity this, float amax)
177 tracebox(this.origin, this.mins, this.maxs, this.origin - ('0 0 1' * amax), MOVE_WORLDONLY, this);
178 return vlen(this.origin - trace_endpos);
181 vector vehicles_force_fromtag_hover(entity this, string tag_name, float spring_length, float max_power)
183 force_fromtag_origin = gettaginfo(this, gettagindex(this, tag_name));
184 v_forward = normalize(v_forward) * -1;
185 traceline(force_fromtag_origin, force_fromtag_origin - (v_forward * spring_length), MOVE_NORMAL, this);
187 force_fromtag_power = (1 - trace_fraction) * max_power;
188 force_fromtag_normpower = force_fromtag_power / max_power;
190 return v_forward * force_fromtag_power;
193 vector vehicles_force_fromtag_maglev(entity this, string tag_name, float spring_length, float max_power)
195 force_fromtag_origin = gettaginfo(this, gettagindex(this, tag_name));
196 v_forward = normalize(v_forward) * -1;
197 traceline(force_fromtag_origin, force_fromtag_origin - (v_forward * spring_length), MOVE_NORMAL, this);
199 // TODO - this may NOT be compatible with wall/celing movement, unhardcode 0.25 (engine count multiplier)
200 if(trace_fraction == 1.0)
202 force_fromtag_normpower = -0.25;
206 force_fromtag_power = ((1 - trace_fraction) - trace_fraction) * max_power;
207 force_fromtag_normpower = force_fromtag_power / max_power;
209 return v_forward * force_fromtag_power;
212 // projectile handling
213 void vehicles_projectile_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
215 // Ignore damage from oterh projectiles from my owner (dont mess up volly's)
216 if(inflictor.owner == this.owner)
219 TakeResource(this, RES_HEALTH, damage);
220 this.velocity += force;
221 if(GetResource(this, RES_HEALTH) < 1)
223 this.takedamage = DAMAGE_NO;
224 this.event_damage = func_null;
225 setthink(this, adaptor_think2use);
226 this.nextthink = time;
230 void vehicles_projectile_explode(entity this, entity toucher)
232 if(this.owner && toucher != NULL)
234 if(toucher == this.owner.vehicle)
237 if(toucher == this.owner.vehicle.tur_head)
241 PROJECTILE_TOUCH(this, toucher);
243 this.event_damage = func_null;
244 RadiusDamage (this, this.realowner, this.shot_dmg, 0, this.shot_radius, this, NULL, this.shot_force, this.projectiledeathtype, DMG_NOWEP, toucher);
249 void vehicles_projectile_explode_think(entity this)
251 vehicles_projectile_explode(this, NULL);
254 void vehicles_projectile_explode_use(entity this, entity actor, entity trigger)
256 vehicles_projectile_explode(this, trigger);
259 entity vehicles_projectile(entity this, entity _mzlfx, Sound _mzlsound,
260 vector _org, vector _vel,
261 float _dmg, float _radi, float _force, float _size,
262 int _deahtype, float _projtype, float _health,
263 bool _cull, bool _clianim, entity _owner)
265 TC(Sound, _mzlsound);
270 PROJECTILE_MAKETRIGGER(proj);
271 setorigin(proj, _org);
273 proj.shot_dmg = _dmg;
274 proj.shot_radius = _radi;
275 proj.shot_force = _force;
276 proj.projectiledeathtype = _deahtype;
277 proj.solid = SOLID_BBOX;
278 set_movetype(proj, MOVETYPE_FLYMISSILE);
279 proj.flags = FL_PROJECTILE;
280 IL_PUSH(g_projectiles, proj);
281 IL_PUSH(g_bot_dodge, proj);
282 proj.bot_dodge = true;
283 proj.bot_dodgerating = _dmg;
284 proj.velocity = _vel;
285 settouch(proj, vehicles_projectile_explode);
286 proj.use = vehicles_projectile_explode_use;
288 proj.realowner = _owner;
289 setthink(proj, SUB_Remove);
290 proj.nextthink = time + 30;
294 proj.takedamage = DAMAGE_AIM;
295 proj.event_damage = vehicles_projectile_damage;
296 SetResourceExplicit(proj, RES_HEALTH, _health);
299 proj.flags |= FL_NOTARGET;
301 if(_mzlsound != SND_Null)
302 sound (this, CH_WEAPON_A, _mzlsound, VOL_BASE, ATTEN_NORM);
304 if(_mzlfx != EFFECT_Null)
305 Send_Effect(_mzlfx, proj.origin, proj.velocity, 1);
307 setsize (proj, '-1 -1 -1' * _size, '1 1 1' * _size);
309 CSQCProjectile(proj, _clianim, _projtype, _cull);
314 void vehicles_gib_explode(entity this)
316 sound (this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
317 Send_Effect(EFFECT_EXPLOSION_SMALL, randomvec() * 80 + (this.origin + '0 0 100'), '0 0 0', 1);
318 Send_Effect(EFFECT_EXPLOSION_SMALL, this.wp00.origin + '0 0 64', '0 0 0', 1);
322 void vehicles_gib_touch(entity this, entity toucher)
324 vehicles_gib_explode(this);
327 void vehicles_gib_think(entity this)
333 this.nextthink = time + 0.1;
336 entity vehicle_tossgib(entity this, entity _template, vector _vel, string _tag, bool _burn, bool _explode, float _maxtime, vector _rot)
338 entity _gib = spawn();
339 _setmodel(_gib, _template.model);
340 vector org = gettaginfo(this, gettagindex(this, _tag));
341 setorigin(_gib, org);
342 _gib.velocity = _vel;
343 set_movetype(_gib, MOVETYPE_TOSS);
344 _gib.solid = SOLID_CORPSE;
345 _gib.colormod = '-0.5 -0.5 -0.5';
346 _gib.effects = EF_LOWPRECISION;
347 _gib.avelocity = _rot;
350 _gib.effects |= EF_FLAME;
354 setthink(_gib, vehicles_gib_explode);
355 _gib.nextthink = time + random() * _explode;
356 settouch(_gib, vehicles_gib_touch);
360 _gib.cnt = time + _maxtime;
361 setthink(_gib, vehicles_gib_think);
362 _gib.nextthink = time + _maxtime - 1;
368 bool vehicle_addplayerslot( entity _owner,
372 bool(entity,float) _framefunc,
373 void(entity,bool) _exitfunc, float(entity, entity) _enterfunc)
375 if(!(_owner.vehicle_flags & VHF_MULTISLOT))
376 _owner.vehicle_flags |= VHF_MULTISLOT;
378 _slot.PlayerPhysplug = _framefunc;
379 _slot.vehicle_exit = _exitfunc;
380 _slot.vehicle_enter = _enterfunc;
381 STAT(HUD, _slot) = _hud;
382 _slot.vehicle_flags = VHF_PLAYERSLOT;
383 _slot.vehicle_viewport = spawn();
384 _slot.vehicle_hudmodel = spawn();
385 _slot.vehicle_hudmodel.viewmodelforclient = _slot;
386 _slot.vehicle_viewport.effects = (EF_ADDITIVE | EF_DOUBLESIDED | EF_FULLBRIGHT | EF_NODEPTHTEST | EF_NOGUNBOB | EF_NOSHADOW | EF_LOWPRECISION | EF_SELECTABLE | EF_TELEPORT_BIT);
388 setmodel(_slot.vehicle_hudmodel, _hud_model);
389 setmodel(_slot.vehicle_viewport, MDL_Null);
391 setattachment(_slot.vehicle_hudmodel, _slot, "");
392 setattachment(_slot.vehicle_viewport, _slot.vehicle_hudmodel, "");
397 vector vehicle_aimturret(entity _vehic, vector _target, entity _turrret, string _tagname,
398 float _pichlimit_min, float _pichlimit_max,
399 float _rotlimit_min, float _rotlimit_max, float _aimspeed, float dt)
403 vtag = gettaginfo(_turrret, gettagindex(_turrret, _tagname));
404 vtmp = vectoangles(normalize(_target - vtag));
405 vtmp = AnglesTransform_ToAngles(AnglesTransform_LeftDivide(AnglesTransform_FromAngles(_vehic.angles), AnglesTransform_FromAngles(vtmp))) - _turrret.angles;
406 vtmp = AnglesTransform_Normalize(vtmp, true);
407 ftmp = _aimspeed * dt;
408 vtmp_y = bound(-ftmp, vtmp_y, ftmp);
409 vtmp_x = bound(-ftmp, vtmp_x, ftmp);
410 _turrret.angles_y = bound(_rotlimit_min, _turrret.angles_y + vtmp_y, _rotlimit_max);
411 _turrret.angles_x = bound(_pichlimit_min, _turrret.angles_x + vtmp_x, _pichlimit_max);
415 void vehicles_reset_colors(entity this, entity player)
418 const vector cmod = '0 0 0';
419 if(this.team && teamplay)
420 cmap = 1024 + (this.team - 1) * 17;
422 cmap = player.colormap;
425 if(autocvar_g_nodepthtestplayers)
426 eff |= EF_NODEPTHTEST;
427 if(autocvar_g_fullbrightplayers)
428 eff |= EF_FULLBRIGHT;
430 // Find all ents attacked to main model and setup effects, colormod etc.
431 FOREACH_ENTITY_ENT(tag_entity, this,
433 if(it == this.vehicle_shieldent)
442 // Also check head tags
443 FOREACH_ENTITY_ENT(tag_entity, this.tur_head,
445 if(it == this.vehicle_shieldent)
454 this.vehicle_hudmodel.effects = this.effects = eff; // | EF_LOWPRECISION;
455 this.vehicle_hudmodel.colormod = this.colormod = cmod;
456 this.vehicle_hudmodel.colormap = this.colormap = cmap;
457 this.vehicle_viewport.effects = (EF_ADDITIVE | EF_DOUBLESIDED | EF_FULLBRIGHT | EF_NODEPTHTEST | EF_NOGUNBOB | EF_NOSHADOW | EF_LOWPRECISION | EF_SELECTABLE | EF_TELEPORT_BIT);
460 this.avelocity = '0 0 0';
461 this.velocity = '0 0 0';
464 Vehicle info = this.vehicledef; //REGISTRY_GET(Vehicles, this.vehicleid);
465 info.vr_setcolors(info, this);
468 void vehicles_clearreturn(entity veh)
470 // Remove "return helper" entities, if any.
471 IL_EACH(g_vehicle_returners, it.wp00 == veh,
474 setthink(it, SUB_Remove);
475 it.nextthink = time + 0.1;
476 IL_REMOVE(g_vehicle_returners, it);
478 if(it.waypointsprite_attached)
479 WaypointSprite_Kill(it.waypointsprite_attached);
483 void vehicles_spawn(entity this);
484 void vehicles_return(entity this)
486 Send_Effect(EFFECT_TELEPORT, this.wp00.origin + '0 0 64', '0 0 0', 1);
488 setthink(this.wp00, vehicles_spawn);
489 this.wp00.nextthink = time;
491 if(this.waypointsprite_attached)
492 WaypointSprite_Kill(this.waypointsprite_attached);
497 void vehicles_showwp_goaway(entity this)
499 if(this.waypointsprite_attached)
500 WaypointSprite_Kill(this.waypointsprite_attached);
505 void vehicles_showwp(entity this)
511 setthink(ent, vehicles_return);
512 ent.nextthink = ent.cnt;
516 setthink(ent, vehicles_return);
517 ent.nextthink = time + 1;
520 ent.team = this.wp00.team;
521 ent.wp00 = this.wp00;
522 setorigin(ent, this.wp00.pos1);
524 ent.nextthink = time + 5;
525 setthink(ent, vehicles_showwp_goaway);
529 if(teamplay && ent.team)
530 rgb = Team_ColorRGB(ent.team);
533 entity wp = WaypointSprite_Spawn(WP_Vehicle, 0, 0, ent, '0 0 64', NULL, 0, ent, waypointsprite_attached, true, RADARICON_Vehicle);
534 wp.wp_extra = ent.wp00.vehicleid;
536 if(ent.waypointsprite_attached)
538 WaypointSprite_UpdateRule(ent.waypointsprite_attached, ent.wp00.team, SPRITERULE_DEFAULT);
540 WaypointSprite_UpdateBuildFinished(ent.waypointsprite_attached, ent.nextthink);
541 WaypointSprite_Ping(ent.waypointsprite_attached);
545 void vehicles_setreturn(entity veh)
547 vehicles_clearreturn(veh);
549 entity ret = new(vehicle_return);
550 IL_PUSH(g_vehicle_returners, ret);
553 setthink(ret, vehicles_showwp);
557 ret.cnt = time + veh.respawntime;
558 ret.nextthink = min(time + veh.respawntime, time + veh.respawntime - 5);
562 ret.nextthink = min(time + veh.respawntime, time + veh.respawntime - 1);
565 setorigin(ret, veh.pos1 + '0 0 96');
568 void vehicle_use(entity this, entity actor, entity trigger)
570 LOG_DEBUG("vehicle ", this.netname, " used by ", actor.classname);
572 this.tur_head.team = actor.team;
574 if(this.tur_head.team == 0)
575 this.active = ACTIVE_NOT;
577 this.active = ACTIVE_ACTIVE;
579 if(this.active == ACTIVE_ACTIVE && !IS_DEAD(this) && !game_stopped)
581 LOG_DEBUG("Respawning vehicle: ", this.netname);
582 if(this.effects & EF_NODRAW)
584 setthink(this, vehicles_spawn);
585 this.nextthink = time + 3;
589 vehicles_setreturn(this);
590 vehicles_reset_colors(this, actor);
595 void vehicles_regen(entity this, float timer, .float regen_field, float field_max, float rpause, float regen, float delta_time, float _healthscale)
597 if(this.(regen_field) < field_max)
598 if(timer + rpause < time)
601 regen = regen * (GetResource(this, RES_HEALTH) / this.max_health);
603 this.(regen_field) = min(this.(regen_field) + regen * delta_time, field_max);
606 this.owner.(regen_field) = (this.(regen_field) / field_max) * 100;
610 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)
612 float resource_amount = GetResource(this, resource);
614 if(resource_amount < field_max)
615 if(timer + rpause < time)
618 regen = regen * (resource_amount / this.max_health);
620 SetResource(this, resource, min(resource_amount + regen * delta_time, field_max));
623 this.owner.(regen_field) = (GetResource(this, resource) / field_max) * 100;
627 void shieldhit_think(entity this)
632 // setmodel(this, MDL_Null);
634 this.effects |= EF_NODRAW;
638 this.nextthink = time + 0.1;
642 void vehicles_painframe(entity this)
644 int myhealth = ((this.owner) ? this.owner.vehicle_health : ((GetResource(this, RES_HEALTH) / this.max_health) * 100));
647 if(this.pain_frame < time)
649 float _ftmp = myhealth / 50;
650 this.pain_frame = time + max(0.1, 0.1 + (random() * 0.5 * _ftmp));
651 Send_Effect(EFFECT_SMOKE_SMALL, (this.origin + (randomvec() * 80)), '0 0 0', 1);
653 if(this.vehicle_flags & VHF_DMGSHAKE)
654 this.velocity += randomvec() * 30;
656 if(this.vehicle_flags & VHF_DMGROLL)
658 if(this.vehicle_flags & VHF_DMGHEADROLL)
659 this.tur_head.angles += randomvec();
661 this.angles += randomvec();
666 void vehicles_frame(entity this, entity actor)
668 vehicles_painframe(this);
671 void vehicles_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
673 this.dmg_time = time;
676 if(DEATH_ISWEAPON(deathtype, WEP_VORTEX))
677 damage *= autocvar_g_vehicles_vortex_damagerate;
678 else if(DEATH_ISWEAPON(deathtype, WEP_MACHINEGUN))
679 damage *= autocvar_g_vehicles_machinegun_damagerate;
680 else if(DEATH_ISWEAPON(deathtype, WEP_RIFLE))
681 damage *= autocvar_g_vehicles_rifle_damagerate;
682 else if(DEATH_ISWEAPON(deathtype, WEP_VAPORIZER))
683 damage *= autocvar_g_vehicles_vaporizer_damagerate;
684 else if(DEATH_ISWEAPON(deathtype, WEP_SEEKER))
685 damage *= autocvar_g_vehicles_tag_damagerate;
686 else if(DEATH_WEAPONOF(deathtype) != WEP_Null)
687 damage *= autocvar_g_vehicles_weapon_damagerate;
689 this.enemy = attacker;
691 this.pain_finished = time;
693 if((this.vehicle_flags & VHF_HASSHIELD) && (this.vehicle_shield > 0))
695 if (wasfreed(this.vehicle_shieldent) || this.vehicle_shieldent == NULL)
697 this.vehicle_shieldent = spawn();
698 this.vehicle_shieldent.effects = EF_LOWPRECISION;
700 setmodel(this.vehicle_shieldent, MDL_VEH_SHIELD);
701 setattachment(this.vehicle_shieldent, this, "");
702 setorigin(this.vehicle_shieldent, real_origin(this) - this.origin);
703 this.vehicle_shieldent.scale = 256 / vlen(this.maxs - this.mins);
704 setthink(this.vehicle_shieldent, shieldhit_think);
707 this.vehicle_shieldent.colormod = '1 1 1';
708 this.vehicle_shieldent.alpha = 0.45;
709 this.vehicle_shieldent.angles = vectoangles(normalize(hitloc - (this.origin + this.vehicle_shieldent.origin))) - this.angles;
710 this.vehicle_shieldent.nextthink = time;
711 this.vehicle_shieldent.effects &= ~EF_NODRAW;
713 this.vehicle_shield -= damage;
715 if(this.vehicle_shield < 0)
717 TakeResource(this, RES_HEALTH, fabs(this.vehicle_shield));
718 this.vehicle_shieldent.colormod = '2 0 0';
719 this.vehicle_shield = 0;
720 this.vehicle_shieldent.alpha = 0.75;
722 if(sound_allowed(MSG_BROADCAST, attacker))
723 spamsound (this, CH_PAIN, SND_ONS_HIT2, VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
726 if(sound_allowed(MSG_BROADCAST, attacker))
727 spamsound (this, CH_PAIN, SND_ONS_ELECTRICITY_EXPLODE, VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
732 TakeResource(this, RES_HEALTH, damage);
734 if(sound_allowed(MSG_BROADCAST, attacker))
735 spamsound (this, CH_PAIN, SND_ONS_HIT2, VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
738 if(this.damageforcescale < 1 && this.damageforcescale > 0)
739 this.velocity += force * this.damageforcescale;
741 this.velocity += force;
743 if(GetResource(this, RES_HEALTH) <= 0)
747 if(this.vehicle_flags & VHF_DEATHEJECT)
748 vehicles_exit(this, VHEF_EJECT);
750 vehicles_exit(this, VHEF_RELEASE);
753 antilag_clear(this, this);
755 Vehicle info = this.vehicledef; //REGISTRY_GET(Vehicles, this.vehicleid);
756 info.vr_death(info, this);
757 vehicles_setreturn(this);
761 bool vehicles_heal(entity targ, entity inflictor, float amount, float limit)
763 float true_limit = ((limit != RES_LIMIT_NONE) ? limit : targ.max_health);
764 if(GetResource(targ, RES_HEALTH) <= 0 || GetResource(targ, RES_HEALTH) >= true_limit)
767 GiveResourceWithLimit(targ, RES_HEALTH, amount, true_limit);
769 targ.owner.vehicle_health = (GetResource(targ, RES_HEALTH) / targ.max_health) * 100;
773 bool vehicles_crushable(entity e)
775 if(IS_PLAYER(e) && time >= e.vehicle_enter_delay)
784 void vehicles_impact(entity this, float _minspeed, float _speedfac, float _maxpain)
786 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
789 if(this.play_time < time)
791 if(vdist(this.velocity - this.oldvelocity, >, _minspeed))
793 float wc = vlen(this.velocity - this.oldvelocity);
794 float take = min(_speedfac * wc, _maxpain);
795 Damage(this, NULL, NULL, take, DEATH_FALL.m_id, DMG_NOWEP, this.origin, '0 0 0');
796 this.play_time = time + 0.25;
801 // vehicle enter/exit handling
802 vector vehicles_findgoodexit(entity this, entity player, vector prefer_spot)
804 // TODO: we actually want the player's size here
805 tracebox(this.origin + '0 0 32', PL_MIN_CONST, PL_MAX_CONST, prefer_spot, MOVE_NORMAL, player);
806 if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
809 float mysize = 1.5 * vlen(this.maxs - this.mins);
811 vector v2 = 0.5 * (this.absmin + this.absmax);
812 for(int i = 0; i < autocvar_g_vehicles_exit_attempts; ++i)
816 v = v2 + normalize(v) * mysize;
817 tracebox(v2, PL_MIN_CONST, PL_MAX_CONST, v, MOVE_NORMAL, player);
818 if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
825 .int old_vehicle_flags;
826 void vehicles_exit(entity vehic, bool eject)
828 entity player = vehic.owner;
830 if(vehicles_exit_running)
832 LOG_TRACE("^1vehicles_exit already running! this is not good...");
836 vehicles_exit_running = true;
838 if(vehic.vehicle_flags & VHF_PLAYERSLOT)
840 vehic.vehicle_exit(vehic, eject);
841 vehicles_exit_running = false;
847 if (IS_REAL_CLIENT(player))
850 WriteByte (MSG_ONE, SVC_SETVIEWPORT);
851 WriteEntity( MSG_ONE, player);
853 // NOTE: engine networked
854 WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
855 WriteAngle(MSG_ONE, 0);
856 WriteAngle(MSG_ONE, vehic.angles_y);
857 WriteAngle(MSG_ONE, 0);
860 setsize(player, STAT(PL_MIN, player), STAT(PL_MAX, player));
862 player.takedamage = DAMAGE_AIM;
863 player.solid = SOLID_SLIDEBOX;
864 set_movetype(player, MOVETYPE_WALK);
865 player.effects &= ~EF_NODRAW;
866 player.teleportable = TELEPORT_NORMAL;
867 player.alpha = default_player_alpha;
868 player.PlayerPhysplug = func_null;
869 player.vehicle = NULL;
870 player.view_ofs = STAT(PL_VIEW_OFS, player);
871 player.event_damage = PlayerDamage;
872 STAT(HUD, player) = HUD_NORMAL;
873 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++ slot)
875 .entity weaponentity = weaponentities[slot];
876 player.(weaponentity).m_switchweapon = vehic.(weaponentity).m_switchweapon;
877 delete(vehic.(weaponentity)); // no longer needed
879 player.last_vehiclecheck = time + 3;
880 player.vehicle_enter_delay = time + 2;
882 CSQCVehicleSetup(player, HUD_NORMAL);
884 Kill_Notification(NOTIF_ONE, player, MSG_CENTER, CPID_VEHICLES);
885 Kill_Notification(NOTIF_ONE, player, MSG_CENTER, CPID_VEHICLES_OTHER); // kill all vehicle notifications when exiting a vehicle?
888 vehic.flags |= FL_NOTARGET;
891 vehic.avelocity = '0 0 0';
893 vehic.tur_head.nodrawtoclient = NULL;
898 WaypointSprite_Kill(vehic.wps_intruder);
900 MUTATOR_CALLHOOK(VehicleExit, player, vehic);
902 vehic.team = vehic.tur_head.team;
904 if(vehic.old_vehicle_flags & VHF_SHIELDREGEN)
905 vehic.vehicle_flags |= VHF_SHIELDREGEN;
906 vehic.old_vehicle_flags = 0;
908 sound (vehic, CH_TRIGGER_SINGLE, SND_Null, 1, ATTEN_NORM);
909 vehic.vehicle_hudmodel.viewmodelforclient = vehic;
910 vehic.phase = time + 1;
912 vehic.vehicle_exit(vehic, eject);
914 vehicles_setreturn(vehic);
915 vehicles_reset_colors(vehic, NULL);
918 CSQCMODEL_AUTOINIT(vehic);
921 player.oldorigin = player.origin; // player's location is set by the exit functions, so we need to do this after everything
923 vehicles_exit_running = false;
926 void vehicles_touch(entity this, entity toucher)
928 if(MUTATOR_CALLHOOK(VehicleTouch, this, toucher))
931 // Vehicle currently in use
935 if((this.origin_z + this.maxs_z) > (toucher.origin_z))
936 if(vehicles_crushable(toucher))
937 if(!weaponLocked(this.owner))
939 if(vdist(this.velocity, >=, autocvar_g_vehicles_crush_minspeed))
940 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);
942 return; // Dont do selfdamage when hitting "soft targets".
945 if(this.play_time < time) {
946 Vehicle info = this.vehicledef; //REGISTRY_GET(Vehicles, this.vehicleid);
947 info.vr_impact(info, this);
953 if(autocvar_g_vehicles_enter)
956 vehicles_enter(toucher, this);
959 bool vehicle_impulse(entity this, int imp)
961 entity v = this.vehicle;
962 if (!v) return false;
963 if (IS_DEAD(v)) return false;
964 bool(entity,int) f = v.vehicles_impulse;
965 if (f && f(this,imp)) return true;
968 case IMP_weapon_drop.impulse:
970 stuffcmd(this, "\ntoggle cl_eventchase_vehicle\nset _vehicles_shownchasemessage 1\n");
977 void vehicles_enter(entity pl, entity veh)
979 // Remove this when bots know how to use vehicles
980 if((IS_BOT_CLIENT(pl) && !autocvar_g_vehicles_allow_bots))
984 || (veh.phase >= time)
985 || (pl.vehicle_enter_delay >= time)
986 || (STAT(FROZEN, pl))
991 Vehicle info = veh.vehicledef; //REGISTRY_GET(Vehicles, veh.vehicleid);
993 if(autocvar_g_vehicles_enter) // vehicle's touch function should handle this if entering via use key is disabled (TODO)
994 if(veh.vehicle_flags & VHF_MULTISLOT)
995 if(veh.owner && SAME_TEAM(pl, veh))
997 // we don't need a return value or anything here
998 // if successful the owner check below will prevent anything weird
999 info.vr_gunner_enter(info, veh, pl);
1003 return; // got here and didn't enter the gunner, return
1007 if(DIFF_TEAM(pl, veh))
1008 if(autocvar_g_vehicles_steal)
1010 FOREACH_CLIENT(IS_PLAYER(it) && SAME_TEAM(it, veh), Send_Notification(NOTIF_ONE, it, MSG_CENTER, CENTER_VEHICLE_STEAL));
1012 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_VEHICLE_STEAL_SELF);
1014 veh.vehicle_shield = 0;
1015 veh.old_vehicle_flags = veh.vehicle_flags; // make a backup just so we're not permanently crippling this vehicle
1016 veh.vehicle_flags &= ~VHF_SHIELDREGEN;
1018 if (autocvar_g_vehicles_steal_show_waypoint) {
1019 entity wp = WaypointSprite_Spawn(WP_VehicleIntruder, 0, 0, pl, '0 0 68', NULL, veh.team, veh, wps_intruder, true, RADARICON_DANGER);
1020 wp.colormod = Team_ColorRGB(pl.team);
1025 RemoveGrapplingHooks(pl);
1027 veh.vehicle_ammo1 = 0;
1028 veh.vehicle_ammo2 = 0;
1029 veh.vehicle_reload1 = 0;
1030 veh.vehicle_reload2 = 0;
1031 veh.vehicle_energy = 0;
1036 // .viewmodelforclient works better.
1037 //veh.vehicle_hudmodel.drawonlytoclient = veh.owner;
1039 veh.vehicle_hudmodel.viewmodelforclient = pl;
1042 pl.view_ofs = STAT(PL_VIEW_OFS, pl);
1043 setsize(pl, STAT(PL_MIN, pl), STAT(PL_MAX, pl));
1045 veh.event_damage = vehicles_damage;
1046 veh.event_heal = vehicles_heal;
1048 pl.items &= ~IT_USING_JETPACK;
1049 pl.angles = veh.angles;
1050 pl.takedamage = DAMAGE_NO;
1051 pl.solid = SOLID_NOT;
1052 pl.disableclientprediction = 1; // physics is no longer run, so this won't be reset
1053 set_movetype(pl, MOVETYPE_NOCLIP);
1054 pl.teleportable = false;
1056 pl.event_damage = func_null;
1057 pl.view_ofs = '0 0 0';
1058 veh.colormap = pl.colormap;
1060 veh.tur_head.colormap = pl.colormap;
1061 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
1063 .entity weaponentity = weaponentities[slot];
1064 veh.(weaponentity) = new(temp_wepent);
1065 veh.(weaponentity).m_switchweapon = pl.(weaponentity).m_switchweapon;
1067 STAT(HUD, pl) = veh.vehicleid;
1068 pl.PlayerPhysplug = veh.PlayerPhysplug;
1070 pl.vehicle_ammo1 = veh.vehicle_ammo1;
1071 pl.vehicle_ammo2 = veh.vehicle_ammo2;
1072 pl.vehicle_reload1 = veh.vehicle_reload1;
1073 pl.vehicle_reload2 = veh.vehicle_reload2;
1074 pl.vehicle_energy = veh.vehicle_energy;
1076 // Cant do this, hides attached objects too.
1077 //veh.exteriormodeltoclient = veh.owner;
1078 //veh.tur_head.exteriormodeltoclient = veh.owner;
1081 UNSET_ONGROUND(veh);
1084 veh.flags -= FL_NOTARGET;
1086 vehicles_reset_colors(veh, pl);
1088 if (IS_REAL_CLIENT(pl))
1090 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_VEHICLE_ENTER);
1093 WriteByte (MSG_ONE, SVC_SETVIEWPORT);
1094 WriteEntity(MSG_ONE, veh.vehicle_viewport);
1096 // NOTE: engine networked
1097 WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
1100 WriteAngle(MSG_ONE, veh.tur_head.angles_x + veh.angles_x); // tilt
1101 WriteAngle(MSG_ONE, veh.tur_head.angles_y + veh.angles_y); // yaw
1102 WriteAngle(MSG_ONE, 0); // roll
1106 WriteAngle(MSG_ONE, veh.angles_x * -1); // tilt
1107 WriteAngle(MSG_ONE, veh.angles_y); // yaw
1108 WriteAngle(MSG_ONE, 0); // roll
1112 vehicles_clearreturn(veh);
1114 CSQCVehicleSetup(pl, veh.vehicleid);
1116 MUTATOR_CALLHOOK(VehicleEnter, pl, veh);
1118 CSQCModel_UnlinkEntity(veh);
1119 info.vr_enter(info, veh);
1121 antilag_clear(pl, CS(pl));
1124 void vehicles_think(entity this)
1126 this.nextthink = time + autocvar_g_vehicles_thinkrate;
1129 STAT(VEHICLESTAT_W2MODE, this.owner) = STAT(VEHICLESTAT_W2MODE, this);
1131 Vehicle info = this.vehicledef; //REGISTRY_GET(Vehicles, this.vehicleid);
1132 info.vr_think(info, this);
1134 vehicles_painframe(this);
1136 CSQCMODEL_AUTOUPDATE(this);
1139 void vehicles_reset(entity this)
1142 vehicles_exit(this, VHEF_RELEASE);
1144 vehicles_clearreturn(this);
1146 if(this.active != ACTIVE_NOT)
1147 vehicles_spawn(this);
1151 void vehicles_spawn(entity this)
1153 LOG_DEBUG("Spawning vehicle: ", this.classname);
1156 this.vehicle_hudmodel.viewmodelforclient = this;
1159 settouch(this, vehicles_touch);
1160 this.event_damage = vehicles_damage;
1161 this.event_heal = vehicles_heal;
1162 this.reset = vehicles_reset;
1163 this.iscreature = true;
1164 this.teleportable = false; // no teleporting for vehicles, too buggy
1165 this.damagedbycontents = true;
1166 set_movetype(this, MOVETYPE_WALK);
1167 this.solid = SOLID_SLIDEBOX;
1168 this.takedamage = DAMAGE_AIM;
1169 this.deadflag = DEAD_NO;
1170 if(!this.bot_attack)
1171 IL_PUSH(g_bot_targets, this);
1172 this.bot_attack = true;
1173 this.flags = FL_NOTARGET;
1174 this.avelocity = '0 0 0';
1175 this.velocity = '0 0 0';
1176 setthink(this, vehicles_think);
1177 this.nextthink = time;
1180 this.lock_strength = 0;
1181 this.lock_target = NULL;
1182 this.misc_bulletcounter = 0;
1185 this.angles = this.pos2;
1186 setorigin(this, this.pos1);
1188 Send_Effect(EFFECT_TELEPORT, this.origin + '0 0 64', '0 0 0', 1);
1190 if(this.vehicle_controller)
1191 this.team = this.vehicle_controller.team;
1193 FOREACH_CLIENT(IS_PLAYER(it),
1195 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
1197 .entity weaponentity = weaponentities[slot];
1198 if(it.(weaponentity).hook.aiment == this)
1199 RemoveHook(it.(weaponentity).hook);
1204 Vehicle info = this.vehicledef; //REGISTRY_GET(Vehicles, this.vehicleid);
1205 info.vr_spawn(info, this);
1207 vehicles_reset_colors(this, NULL);
1209 CSQCMODEL_AUTOINIT(this);
1212 bool vehicle_initialize(entity this, Vehicle info, bool nodrop)
1214 if(!autocvar_g_vehicles)
1221 info.vr_precache(info);
1223 if(this.targetname && this.targetname != "")
1225 this.vehicle_controller = find(NULL, target, this.targetname);
1226 if(!this.vehicle_controller)
1228 LOG_DEBUG("^1WARNING: ^7Vehicle with invalid .targetname");
1229 this.active = ACTIVE_ACTIVE;
1233 this.team = this.vehicle_controller.team;
1234 this.use = vehicle_use;
1238 if(this.vehicle_controller.team == 0)
1239 this.active = ACTIVE_NOT;
1241 this.active = ACTIVE_ACTIVE;
1245 else { this.active = ACTIVE_ACTIVE; }
1247 if(this.team && (!teamplay || !autocvar_g_vehicles_teams))
1250 if(this.mdl == "" || !this.mdl)
1251 _setmodel(this, info.model);
1253 _setmodel(this, this.mdl);
1255 this.vehicle_flags |= VHF_ISVEHICLE;
1257 this.vehicle_viewport = new(vehicle_viewport);
1258 this.vehicle_hudmodel = new(vehicle_hudmodel);
1259 this.tur_head = new(tur_head);
1260 this.tur_head.owner = this;
1261 this.takedamage = DAMAGE_NO;
1262 this.bot_attack = true;
1263 IL_PUSH(g_bot_targets, this);
1264 this.iscreature = true;
1265 this.teleportable = false; // no teleporting for vehicles, too buggy
1266 this.damagedbycontents = true;
1267 IL_PUSH(g_damagedbycontents, this);
1268 this.vehicleid = info.vehicleid;
1269 this.vehicledef = info;
1270 this.PlayerPhysplug = info.PlayerPhysplug;
1271 this.event_damage = func_null;
1272 this.event_heal = func_null;
1273 settouch(this, vehicles_touch);
1274 setthink(this, vehicles_spawn);
1275 this.nextthink = time;
1276 this.effects = EF_NODRAW;
1277 this.dphitcontentsmask = DPCONTENTS_BODY | DPCONTENTS_SOLID;
1279 if(autocvar_g_playerclip_collisions)
1280 this.dphitcontentsmask |= DPCONTENTS_PLAYERCLIP;
1282 if(autocvar_g_nodepthtestplayers)
1283 this.effects |= EF_NODEPTHTEST;
1285 if(autocvar_g_fullbrightplayers)
1286 this.effects |= EF_FULLBRIGHT;
1288 _setmodel(this.vehicle_hudmodel, info.hud_model);
1289 setmodel(this.vehicle_viewport, MDL_Null);
1291 if(info.head_model != "")
1293 _setmodel(this.tur_head, info.head_model);
1294 setattachment(this.tur_head, this, info.tag_head);
1295 setattachment(this.vehicle_hudmodel, this.tur_head, info.tag_hud);
1296 setattachment(this.vehicle_viewport, this.vehicle_hudmodel, info.tag_view);
1300 setattachment(this.tur_head, this, "");
1301 setattachment(this.vehicle_hudmodel, this, info.tag_hud);
1302 setattachment(this.vehicle_viewport, this.vehicle_hudmodel, info.tag_view);
1305 setsize(this, info.m_mins, info.m_maxs);
1307 info.vr_setup(info, this);
1311 setorigin(this, this.origin);
1312 tracebox(this.origin + '0 0 100', info.m_mins, info.m_maxs, this.origin - '0 0 10000', MOVE_WORLDONLY, this);
1313 setorigin(this, trace_endpos);
1316 this.pos1 = this.origin;
1317 this.pos2 = this.angles;
1318 this.tur_head.team = this.team;
1320 if(this.active == ACTIVE_NOT)
1321 this.nextthink = 0; // wait until activated
1322 else if(autocvar_g_vehicles_delayspawn)
1323 this.nextthink = time + this.respawntime + (random() * autocvar_g_vehicles_delayspawn_jitter);
1325 this.nextthink = time + game_starttime;
1327 if(MUTATOR_CALLHOOK(VehicleInit, this))