1 #include "sv_vehicles.qh"
3 #include <common/items/_mod.qh>
4 #include <common/mapobjects/defs.qh>
5 #include <common/mapobjects/teleporters.qh>
6 #include <common/mapobjects/triggers.qh>
7 #include <server/bot/api.qh>
8 #include <server/client.qh>
9 #include <server/damage.qh>
10 #include <server/items/items.qh>
11 #include <server/weapons/common.qh>
12 #include <server/world.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);
266 entity proj = new(vehicles_projectile);
268 PROJECTILE_MAKETRIGGER(proj);
269 setorigin(proj, _org);
271 proj.shot_dmg = _dmg;
272 proj.shot_radius = _radi;
273 proj.shot_force = _force;
274 proj.projectiledeathtype = _deahtype;
275 proj.solid = SOLID_BBOX;
276 set_movetype(proj, MOVETYPE_FLYMISSILE);
277 proj.flags = FL_PROJECTILE;
278 IL_PUSH(g_projectiles, proj);
279 IL_PUSH(g_bot_dodge, proj);
280 proj.bot_dodge = true;
281 proj.bot_dodgerating = _dmg;
282 proj.velocity = _vel;
283 settouch(proj, vehicles_projectile_explode);
284 proj.use = vehicles_projectile_explode_use;
286 proj.realowner = _owner;
287 setthink(proj, SUB_Remove);
288 proj.nextthink = time + 30;
292 proj.takedamage = DAMAGE_AIM;
293 proj.event_damage = vehicles_projectile_damage;
294 SetResourceExplicit(proj, RES_HEALTH, _health);
297 proj.flags |= FL_NOTARGET;
299 if(_mzlsound != SND_Null)
300 sound (this, CH_WEAPON_A, _mzlsound, VOL_BASE, ATTEN_NORM);
302 if(_mzlfx != EFFECT_Null)
303 Send_Effect(_mzlfx, proj.origin, proj.velocity, 1);
305 setsize (proj, '-1 -1 -1' * _size, '1 1 1' * _size);
307 CSQCProjectile(proj, _clianim, _projtype, _cull);
312 void vehicles_gib_explode(entity this)
314 sound (this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
315 Send_Effect(EFFECT_EXPLOSION_SMALL, randomvec() * 80 + (this.origin + '0 0 100'), '0 0 0', 1);
316 Send_Effect(EFFECT_EXPLOSION_SMALL, this.wp00.origin + '0 0 64', '0 0 0', 1);
320 void vehicles_gib_touch(entity this, entity toucher)
322 vehicles_gib_explode(this);
325 void vehicles_gib_think(entity this)
331 this.nextthink = time + 0.1;
334 entity vehicle_tossgib(entity this, entity _template, vector _vel, string _tag, bool _burn, bool _explode, float _maxtime, vector _rot)
336 entity _gib = new(vehicle_gib);
337 _setmodel(_gib, _template.model);
338 vector org = gettaginfo(this, gettagindex(this, _tag));
339 setorigin(_gib, org);
340 _gib.velocity = _vel;
341 set_movetype(_gib, MOVETYPE_TOSS);
342 _gib.solid = SOLID_CORPSE;
343 _gib.colormod = '-0.5 -0.5 -0.5';
344 _gib.effects = EF_LOWPRECISION;
345 _gib.avelocity = _rot;
348 _gib.effects |= EF_FLAME;
352 setthink(_gib, vehicles_gib_explode);
353 _gib.nextthink = time + random() * _explode;
354 settouch(_gib, vehicles_gib_touch);
358 _gib.cnt = time + _maxtime;
359 setthink(_gib, vehicles_gib_think);
360 _gib.nextthink = time + _maxtime - 1;
366 bool vehicle_addplayerslot( entity _owner,
370 bool(entity,float) _framefunc,
371 void(entity,bool) _exitfunc, float(entity, entity) _enterfunc)
373 if(!(_owner.vehicle_flags & VHF_MULTISLOT))
374 _owner.vehicle_flags |= VHF_MULTISLOT;
376 _slot.PlayerPhysplug = _framefunc;
377 _slot.vehicle_exit = _exitfunc;
378 _slot.vehicle_enter = _enterfunc;
379 STAT(HUD, _slot) = _hud;
380 _slot.vehicle_flags = VHF_PLAYERSLOT;
381 _slot.vehicle_viewport = new(vehicle_viewport);
382 _slot.vehicle_hudmodel = new(vehicle_hudmodel);
383 _slot.vehicle_hudmodel.viewmodelforclient = _slot;
384 _slot.vehicle_viewport.effects = (EF_ADDITIVE | EF_DOUBLESIDED | EF_FULLBRIGHT | EF_NODEPTHTEST | EF_NOGUNBOB | EF_NOSHADOW | EF_LOWPRECISION | EF_SELECTABLE | EF_TELEPORT_BIT);
386 setmodel(_slot.vehicle_hudmodel, _hud_model);
387 setmodel(_slot.vehicle_viewport, MDL_Null);
389 setattachment(_slot.vehicle_hudmodel, _slot, "");
390 setattachment(_slot.vehicle_viewport, _slot.vehicle_hudmodel, "");
395 vector vehicle_aimturret(entity _vehic, vector _target, entity _turrret, string _tagname,
396 float _pichlimit_min, float _pichlimit_max,
397 float _rotlimit_min, float _rotlimit_max, float _aimspeed, float dt)
401 vtag = gettaginfo(_turrret, gettagindex(_turrret, _tagname));
402 vtmp = vectoangles(normalize(_target - vtag));
403 vtmp = AnglesTransform_ToAngles(AnglesTransform_LeftDivide(AnglesTransform_FromAngles(_vehic.angles), AnglesTransform_FromAngles(vtmp))) - _turrret.angles;
404 vtmp = AnglesTransform_Normalize(vtmp, true);
405 ftmp = _aimspeed * dt;
406 vtmp_y = bound(-ftmp, vtmp_y, ftmp);
407 vtmp_x = bound(-ftmp, vtmp_x, ftmp);
408 _turrret.angles_y = bound(_rotlimit_min, _turrret.angles_y + vtmp_y, _rotlimit_max);
409 _turrret.angles_x = bound(_pichlimit_min, _turrret.angles_x + vtmp_x, _pichlimit_max);
413 void vehicles_reset_colors(entity this, entity player)
416 const vector cmod = '0 0 0';
417 if(this.team && teamplay)
418 cmap = 1024 + (this.team - 1) * 17;
420 cmap = player.colormap;
423 if(autocvar_g_nodepthtestplayers)
424 eff |= EF_NODEPTHTEST;
425 if(autocvar_g_fullbrightplayers)
426 eff |= EF_FULLBRIGHT;
428 // Find all ents attacked to main model and setup effects, colormod etc.
429 FOREACH_ENTITY_ENT(tag_entity, this,
431 if(it == this.vehicle_shieldent)
440 // Also check head tags
441 FOREACH_ENTITY_ENT(tag_entity, this.tur_head,
443 if(it == this.vehicle_shieldent)
452 this.vehicle_hudmodel.effects = this.effects = eff; // | EF_LOWPRECISION;
453 this.vehicle_hudmodel.colormod = this.colormod = cmod;
454 this.vehicle_hudmodel.colormap = this.colormap = cmap;
455 this.vehicle_viewport.effects = (EF_ADDITIVE | EF_DOUBLESIDED | EF_FULLBRIGHT | EF_NODEPTHTEST | EF_NOGUNBOB | EF_NOSHADOW | EF_LOWPRECISION | EF_SELECTABLE | EF_TELEPORT_BIT);
458 this.avelocity = '0 0 0';
459 this.velocity = '0 0 0';
462 Vehicle info = this.vehicledef; //REGISTRY_GET(Vehicles, this.vehicleid);
463 info.vr_setcolors(info, this);
466 void vehicles_clearreturn(entity veh)
468 // Remove "return helper" entities, if any.
469 IL_EACH(g_vehicle_returners, it.wp00 == veh,
472 setthink(it, SUB_Remove);
473 it.nextthink = time + 0.1;
474 IL_REMOVE(g_vehicle_returners, it);
476 if(it.waypointsprite_attached)
477 WaypointSprite_Kill(it.waypointsprite_attached);
481 void vehicles_spawn(entity this);
482 void vehicles_return(entity this)
484 Send_Effect(EFFECT_TELEPORT, this.wp00.origin + '0 0 64', '0 0 0', 1);
486 setthink(this.wp00, vehicles_spawn);
487 this.wp00.nextthink = time;
489 if(this.waypointsprite_attached)
490 WaypointSprite_Kill(this.waypointsprite_attached);
495 void vehicles_showwp_goaway(entity this)
497 if(this.waypointsprite_attached)
498 WaypointSprite_Kill(this.waypointsprite_attached);
503 void vehicles_showwp(entity this)
509 setthink(ent, vehicles_return);
510 ent.nextthink = ent.cnt;
514 setthink(ent, vehicles_return);
515 ent.nextthink = time + 1;
518 ent.team = this.wp00.team;
519 ent.wp00 = this.wp00;
520 setorigin(ent, this.wp00.pos1);
522 ent.nextthink = time + 5;
523 setthink(ent, vehicles_showwp_goaway);
527 if(teamplay && ent.team)
528 rgb = Team_ColorRGB(ent.team);
531 entity wp = WaypointSprite_Spawn(WP_Vehicle, 0, 0, ent, '0 0 64', NULL, 0, ent, waypointsprite_attached, true, RADARICON_Vehicle);
532 wp.wp_extra = ent.wp00.vehicleid;
534 if(ent.waypointsprite_attached)
536 WaypointSprite_UpdateRule(ent.waypointsprite_attached, ent.wp00.team, SPRITERULE_DEFAULT);
538 WaypointSprite_UpdateBuildFinished(ent.waypointsprite_attached, ent.nextthink);
539 WaypointSprite_Ping(ent.waypointsprite_attached);
543 void vehicles_setreturn(entity veh)
545 vehicles_clearreturn(veh);
547 entity ret = new(vehicle_return);
548 IL_PUSH(g_vehicle_returners, ret);
551 setthink(ret, vehicles_showwp);
555 ret.cnt = time + veh.respawntime;
556 ret.nextthink = min(time + veh.respawntime, time + veh.respawntime - 5);
560 ret.nextthink = min(time + veh.respawntime, time + veh.respawntime - 1);
563 setorigin(ret, veh.pos1 + '0 0 96');
566 void vehicle_use(entity this, entity actor, entity trigger)
568 LOG_DEBUG("vehicle ", this.netname, " used by ", actor.classname);
570 this.tur_head.team = actor.team;
572 if(this.tur_head.team == 0)
573 this.active = ACTIVE_NOT;
575 this.active = ACTIVE_ACTIVE;
577 if(this.active == ACTIVE_ACTIVE && !IS_DEAD(this) && !game_stopped)
579 LOG_DEBUG("Respawning vehicle: ", this.netname);
580 if(this.effects & EF_NODRAW)
582 setthink(this, vehicles_spawn);
583 this.nextthink = time + 3;
587 vehicles_setreturn(this);
588 vehicles_reset_colors(this, actor);
593 void vehicles_regen(entity this, float timer, .float regen_field, float field_max, float rpause, float regen, float delta_time, float _healthscale)
595 if(this.(regen_field) < field_max)
596 if(timer + rpause < time)
599 regen = regen * (GetResource(this, RES_HEALTH) / this.max_health);
601 this.(regen_field) = min(this.(regen_field) + regen * delta_time, field_max);
604 this.owner.(regen_field) = (this.(regen_field) / field_max) * 100;
608 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)
610 float resource_amount = GetResource(this, resource);
612 if(resource_amount < field_max)
613 if(timer + rpause < time)
616 regen = regen * (resource_amount / this.max_health);
618 SetResource(this, resource, min(resource_amount + regen * delta_time, field_max));
621 this.owner.(regen_field) = (GetResource(this, resource) / field_max) * 100;
625 void shieldhit_think(entity this)
630 // setmodel(this, MDL_Null);
632 this.effects |= EF_NODRAW;
636 this.nextthink = time + 0.1;
640 void vehicles_painframe(entity this)
642 int myhealth = ((this.owner) ? this.owner.vehicle_health : ((GetResource(this, RES_HEALTH) / this.max_health) * 100));
645 if(this.pain_frame < time)
647 float _ftmp = myhealth / 50;
648 this.pain_frame = time + max(0.1, 0.1 + (random() * 0.5 * _ftmp));
649 Send_Effect(EFFECT_SMOKE_SMALL, (this.origin + (randomvec() * 80)), '0 0 0', 1);
651 if(this.vehicle_flags & VHF_DMGSHAKE)
652 this.velocity += randomvec() * 30;
654 if(this.vehicle_flags & VHF_DMGROLL)
656 if(this.vehicle_flags & VHF_DMGHEADROLL)
657 this.tur_head.angles += randomvec();
659 this.angles += randomvec();
664 void vehicles_frame(entity this, entity actor)
666 vehicles_painframe(this);
669 void vehicles_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
671 this.dmg_time = time;
674 if(DEATH_ISWEAPON(deathtype, WEP_VORTEX))
675 damage *= autocvar_g_vehicles_vortex_damagerate;
676 else if(DEATH_ISWEAPON(deathtype, WEP_MACHINEGUN))
677 damage *= autocvar_g_vehicles_machinegun_damagerate;
678 else if(DEATH_ISWEAPON(deathtype, WEP_RIFLE))
679 damage *= autocvar_g_vehicles_rifle_damagerate;
680 else if(DEATH_ISWEAPON(deathtype, WEP_VAPORIZER))
681 damage *= autocvar_g_vehicles_vaporizer_damagerate;
682 else if(DEATH_ISWEAPON(deathtype, WEP_SEEKER))
683 damage *= autocvar_g_vehicles_tag_damagerate;
684 else if(DEATH_WEAPONOF(deathtype) != WEP_Null)
685 damage *= autocvar_g_vehicles_weapon_damagerate;
687 this.enemy = attacker;
689 this.pain_finished = time;
691 if((this.vehicle_flags & VHF_HASSHIELD) && (this.vehicle_shield > 0))
693 if (wasfreed(this.vehicle_shieldent) || this.vehicle_shieldent == NULL)
695 this.vehicle_shieldent = new(vehicle_shieldent);
696 this.vehicle_shieldent.effects = EF_LOWPRECISION;
698 setmodel(this.vehicle_shieldent, MDL_VEH_SHIELD);
699 setattachment(this.vehicle_shieldent, this, "");
700 setorigin(this.vehicle_shieldent, real_origin(this) - this.origin);
701 this.vehicle_shieldent.scale = 256 / vlen(this.maxs - this.mins);
702 setthink(this.vehicle_shieldent, shieldhit_think);
705 this.vehicle_shieldent.colormod = '1 1 1';
706 this.vehicle_shieldent.alpha = 0.45;
707 this.vehicle_shieldent.angles = vectoangles(normalize(hitloc - (this.origin + this.vehicle_shieldent.origin))) - this.angles;
708 this.vehicle_shieldent.nextthink = time;
709 this.vehicle_shieldent.effects &= ~EF_NODRAW;
711 this.vehicle_shield -= damage;
713 if(this.vehicle_shield < 0)
715 TakeResource(this, RES_HEALTH, fabs(this.vehicle_shield));
716 this.vehicle_shieldent.colormod = '2 0 0';
717 this.vehicle_shield = 0;
718 this.vehicle_shieldent.alpha = 0.75;
720 if(sound_allowed(MSG_BROADCAST, attacker))
721 spamsound (this, CH_PAIN, SND_ONS_HIT2, VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
724 if(sound_allowed(MSG_BROADCAST, attacker))
725 spamsound (this, CH_PAIN, SND_ONS_ELECTRICITY_EXPLODE, VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
730 TakeResource(this, RES_HEALTH, damage);
732 if(sound_allowed(MSG_BROADCAST, attacker))
733 spamsound (this, CH_PAIN, SND_ONS_HIT2, VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
736 if(this.damageforcescale < 1 && this.damageforcescale > 0)
737 this.velocity += force * this.damageforcescale;
739 this.velocity += force;
741 if(GetResource(this, RES_HEALTH) <= 0)
745 if(this.vehicle_flags & VHF_DEATHEJECT)
746 vehicles_exit(this, VHEF_EJECT);
748 vehicles_exit(this, VHEF_RELEASE);
751 antilag_clear(this, this);
753 Vehicle info = this.vehicledef; //REGISTRY_GET(Vehicles, this.vehicleid);
754 info.vr_death(info, this);
755 vehicles_setreturn(this);
759 bool vehicles_heal(entity targ, entity inflictor, float amount, float limit)
761 float true_limit = ((limit != RES_LIMIT_NONE) ? limit : targ.max_health);
762 if(GetResource(targ, RES_HEALTH) <= 0 || GetResource(targ, RES_HEALTH) >= true_limit)
765 GiveResourceWithLimit(targ, RES_HEALTH, amount, true_limit);
767 targ.owner.vehicle_health = (GetResource(targ, RES_HEALTH) / targ.max_health) * 100;
771 bool vehicles_crushable(entity e)
773 if(IS_PLAYER(e) && time >= e.vehicle_enter_delay)
782 void vehicles_impact(entity this, float _minspeed, float _speedfac, float _maxpain)
784 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
787 if(this.play_time < time)
789 if(vdist(this.velocity - this.oldvelocity, >, _minspeed))
791 float wc = vlen(this.velocity - this.oldvelocity);
792 float take = min(_speedfac * wc, _maxpain);
793 Damage(this, NULL, NULL, take, DEATH_FALL.m_id, DMG_NOWEP, this.origin, '0 0 0');
794 this.play_time = time + 0.25;
799 // vehicle enter/exit handling
800 vector vehicles_findgoodexit(entity this, entity player, vector prefer_spot)
802 // TODO: we actually want the player's size here
803 tracebox(this.origin + '0 0 32', PL_MIN_CONST, PL_MAX_CONST, prefer_spot, MOVE_NORMAL, player);
804 if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
807 float mysize = 1.5 * vlen(this.maxs - this.mins);
809 vector v2 = 0.5 * (this.absmin + this.absmax);
810 for(int i = 0; i < autocvar_g_vehicles_exit_attempts; ++i)
814 v = v2 + normalize(v) * mysize;
815 tracebox(v2, PL_MIN_CONST, PL_MAX_CONST, v, MOVE_NORMAL, player);
816 if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
823 .int old_vehicle_flags;
824 void vehicles_exit(entity vehic, bool eject)
826 entity player = vehic.owner;
828 if(vehicles_exit_running)
830 LOG_TRACE("^1vehicles_exit already running! this is not good...");
834 vehicles_exit_running = true;
836 if(vehic.vehicle_flags & VHF_PLAYERSLOT)
838 vehic.vehicle_exit(vehic, eject);
839 vehicles_exit_running = false;
845 if (IS_REAL_CLIENT(player))
848 WriteByte (MSG_ONE, SVC_SETVIEWPORT);
849 WriteEntity( MSG_ONE, player);
851 // NOTE: engine networked
852 WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
853 WriteAngle(MSG_ONE, 0);
854 WriteAngle(MSG_ONE, vehic.angles_y);
855 WriteAngle(MSG_ONE, 0);
858 setsize(player, STAT(PL_MIN, player), STAT(PL_MAX, player));
860 player.takedamage = DAMAGE_AIM;
861 player.solid = SOLID_SLIDEBOX;
862 set_movetype(player, MOVETYPE_WALK);
863 player.effects &= ~EF_NODRAW;
864 player.teleportable = TELEPORT_NORMAL;
865 player.alpha = default_player_alpha;
866 player.PlayerPhysplug = func_null;
867 player.vehicle = NULL;
868 player.view_ofs = STAT(PL_VIEW_OFS, player);
869 player.event_damage = PlayerDamage;
870 STAT(HUD, player) = HUD_NORMAL;
871 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++ slot)
873 .entity weaponentity = weaponentities[slot];
874 player.(weaponentity).m_switchweapon = vehic.(weaponentity).m_switchweapon;
875 delete(vehic.(weaponentity)); // no longer needed
877 player.last_vehiclecheck = time + 3;
878 player.vehicle_enter_delay = time + 2;
880 CSQCVehicleSetup(player, HUD_NORMAL);
882 Kill_Notification(NOTIF_ONE, player, MSG_CENTER, CPID_VEHICLES);
883 Kill_Notification(NOTIF_ONE, player, MSG_CENTER, CPID_VEHICLES_OTHER); // kill all vehicle notifications when exiting a vehicle?
886 vehic.flags |= FL_NOTARGET;
889 vehic.avelocity = '0 0 0';
891 vehic.tur_head.nodrawtoclient = NULL;
896 WaypointSprite_Kill(vehic.wps_intruder);
898 MUTATOR_CALLHOOK(VehicleExit, player, vehic);
900 vehic.team = vehic.tur_head.team;
902 if(vehic.old_vehicle_flags & VHF_SHIELDREGEN)
903 vehic.vehicle_flags |= VHF_SHIELDREGEN;
904 vehic.old_vehicle_flags = 0;
906 sound (vehic, CH_TRIGGER_SINGLE, SND_Null, 1, ATTEN_NORM);
907 vehic.vehicle_hudmodel.viewmodelforclient = vehic;
908 vehic.phase = time + 1;
910 vehic.vehicle_exit(vehic, eject);
912 vehicles_setreturn(vehic);
913 vehicles_reset_colors(vehic, NULL);
916 CSQCMODEL_AUTOINIT(vehic);
919 player.oldorigin = player.origin; // player's location is set by the exit functions, so we need to do this after everything
921 vehicles_exit_running = false;
924 void vehicles_touch(entity this, entity toucher)
926 if(MUTATOR_CALLHOOK(VehicleTouch, this, toucher))
929 // Vehicle currently in use
933 if((this.origin_z + this.maxs_z) > (toucher.origin_z))
934 if(vehicles_crushable(toucher))
935 if(!weaponLocked(this.owner))
937 if(vdist(this.velocity, >=, autocvar_g_vehicles_crush_minspeed))
938 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);
940 return; // Dont do selfdamage when hitting "soft targets".
943 if(this.play_time < time) {
944 Vehicle info = this.vehicledef; //REGISTRY_GET(Vehicles, this.vehicleid);
945 info.vr_impact(info, this);
951 if(autocvar_g_vehicles_enter)
954 vehicles_enter(toucher, this);
957 bool vehicle_impulse(entity this, int imp)
959 entity v = this.vehicle;
960 if (!v) return false;
961 if (IS_DEAD(v)) return false;
962 bool(entity,int) f = v.vehicles_impulse;
963 if (f && f(this,imp)) return true;
966 case IMP_weapon_drop.impulse:
968 stuffcmd(this, "\ntoggle cl_eventchase_vehicle\nset _vehicles_shownchasemessage 1\n");
975 void vehicles_enter(entity pl, entity veh)
977 // Remove this when bots know how to use vehicles
978 if((IS_BOT_CLIENT(pl) && !autocvar_g_vehicles_allow_bots))
982 || (veh.phase >= time)
983 || (pl.vehicle_enter_delay >= time)
984 || (STAT(FROZEN, pl))
989 Vehicle info = veh.vehicledef; //REGISTRY_GET(Vehicles, veh.vehicleid);
991 if(autocvar_g_vehicles_enter) // vehicle's touch function should handle this if entering via use key is disabled (TODO)
992 if(veh.vehicle_flags & VHF_MULTISLOT)
993 if(veh.owner && SAME_TEAM(pl, veh))
995 // we don't need a return value or anything here
996 // if successful the owner check below will prevent anything weird
997 info.vr_gunner_enter(info, veh, pl);
1001 return; // got here and didn't enter the gunner, return
1005 if(DIFF_TEAM(pl, veh))
1006 if(autocvar_g_vehicles_steal)
1008 FOREACH_CLIENT(IS_PLAYER(it) && SAME_TEAM(it, veh), Send_Notification(NOTIF_ONE, it, MSG_CENTER, CENTER_VEHICLE_STEAL));
1010 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_VEHICLE_STEAL_SELF);
1012 veh.vehicle_shield = 0;
1013 veh.old_vehicle_flags = veh.vehicle_flags; // make a backup just so we're not permanently crippling this vehicle
1014 veh.vehicle_flags &= ~VHF_SHIELDREGEN;
1016 if (autocvar_g_vehicles_steal_show_waypoint) {
1017 entity wp = WaypointSprite_Spawn(WP_VehicleIntruder, 0, 0, pl, '0 0 68', NULL, veh.team, veh, wps_intruder, true, RADARICON_DANGER);
1018 wp.colormod = Team_ColorRGB(pl.team);
1023 RemoveGrapplingHooks(pl);
1025 veh.vehicle_ammo1 = 0;
1026 veh.vehicle_ammo2 = 0;
1027 veh.vehicle_reload1 = 0;
1028 veh.vehicle_reload2 = 0;
1029 veh.vehicle_energy = 0;
1034 // .viewmodelforclient works better.
1035 //veh.vehicle_hudmodel.drawonlytoclient = veh.owner;
1037 veh.vehicle_hudmodel.viewmodelforclient = pl;
1040 pl.view_ofs = STAT(PL_VIEW_OFS, pl);
1041 setsize(pl, STAT(PL_MIN, pl), STAT(PL_MAX, pl));
1043 veh.event_damage = vehicles_damage;
1044 veh.event_heal = vehicles_heal;
1046 pl.items &= ~IT_USING_JETPACK;
1047 pl.angles = veh.angles;
1048 pl.takedamage = DAMAGE_NO;
1049 pl.solid = SOLID_NOT;
1050 pl.disableclientprediction = 1; // physics is no longer run, so this won't be reset
1051 set_movetype(pl, MOVETYPE_NOCLIP);
1052 pl.teleportable = false;
1054 pl.event_damage = func_null;
1055 pl.view_ofs = '0 0 0';
1056 veh.colormap = pl.colormap;
1058 veh.tur_head.colormap = pl.colormap;
1059 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
1061 .entity weaponentity = weaponentities[slot];
1062 veh.(weaponentity) = new(temp_wepent);
1063 veh.(weaponentity).m_switchweapon = pl.(weaponentity).m_switchweapon;
1065 STAT(HUD, pl) = veh.vehicleid;
1066 pl.PlayerPhysplug = veh.PlayerPhysplug;
1068 pl.vehicle_ammo1 = veh.vehicle_ammo1;
1069 pl.vehicle_ammo2 = veh.vehicle_ammo2;
1070 pl.vehicle_reload1 = veh.vehicle_reload1;
1071 pl.vehicle_reload2 = veh.vehicle_reload2;
1072 pl.vehicle_energy = veh.vehicle_energy;
1074 // Cant do this, hides attached objects too.
1075 //veh.exteriormodeltoclient = veh.owner;
1076 //veh.tur_head.exteriormodeltoclient = veh.owner;
1079 UNSET_ONGROUND(veh);
1082 veh.flags -= FL_NOTARGET;
1084 vehicles_reset_colors(veh, pl);
1086 if (IS_REAL_CLIENT(pl))
1088 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_VEHICLE_ENTER);
1091 WriteByte (MSG_ONE, SVC_SETVIEWPORT);
1092 WriteEntity(MSG_ONE, veh.vehicle_viewport);
1094 // NOTE: engine networked
1095 WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
1098 WriteAngle(MSG_ONE, veh.tur_head.angles_x + veh.angles_x); // tilt
1099 WriteAngle(MSG_ONE, veh.tur_head.angles_y + veh.angles_y); // yaw
1100 WriteAngle(MSG_ONE, 0); // roll
1104 WriteAngle(MSG_ONE, veh.angles_x * -1); // tilt
1105 WriteAngle(MSG_ONE, veh.angles_y); // yaw
1106 WriteAngle(MSG_ONE, 0); // roll
1110 vehicles_clearreturn(veh);
1112 CSQCVehicleSetup(pl, veh.vehicleid);
1114 MUTATOR_CALLHOOK(VehicleEnter, pl, veh);
1116 CSQCModel_UnlinkEntity(veh);
1117 info.vr_enter(info, veh);
1119 antilag_clear(pl, CS(pl));
1122 void vehicles_think(entity this)
1124 this.nextthink = time + autocvar_g_vehicles_thinkrate;
1127 STAT(VEHICLESTAT_W2MODE, this.owner) = STAT(VEHICLESTAT_W2MODE, this);
1129 Vehicle info = this.vehicledef; //REGISTRY_GET(Vehicles, this.vehicleid);
1130 info.vr_think(info, this);
1132 vehicles_painframe(this);
1134 CSQCMODEL_AUTOUPDATE(this);
1137 void vehicles_reset(entity this)
1140 vehicles_exit(this, VHEF_RELEASE);
1142 vehicles_clearreturn(this);
1144 if(this.active != ACTIVE_NOT)
1145 vehicles_spawn(this);
1149 void vehicles_spawn(entity this)
1151 LOG_DEBUG("Spawning vehicle: ", this.classname);
1154 this.vehicle_hudmodel.viewmodelforclient = this;
1157 settouch(this, vehicles_touch);
1158 this.event_damage = vehicles_damage;
1159 this.event_heal = vehicles_heal;
1160 this.reset = vehicles_reset;
1161 this.iscreature = true;
1162 this.teleportable = false; // no teleporting for vehicles, too buggy
1163 this.damagedbycontents = true;
1164 set_movetype(this, MOVETYPE_WALK);
1165 this.solid = SOLID_SLIDEBOX;
1166 this.takedamage = DAMAGE_AIM;
1167 this.deadflag = DEAD_NO;
1168 if(!this.bot_attack)
1169 IL_PUSH(g_bot_targets, this);
1170 this.bot_attack = true;
1171 this.flags = FL_NOTARGET;
1172 this.avelocity = '0 0 0';
1173 this.velocity = '0 0 0';
1174 setthink(this, vehicles_think);
1175 this.nextthink = time;
1178 this.lock_strength = 0;
1179 this.lock_target = NULL;
1180 this.misc_bulletcounter = 0;
1183 this.angles = this.pos2;
1184 setorigin(this, this.pos1);
1186 Send_Effect(EFFECT_TELEPORT, this.origin + '0 0 64', '0 0 0', 1);
1188 if(this.vehicle_controller)
1189 this.team = this.vehicle_controller.team;
1191 FOREACH_CLIENT(IS_PLAYER(it),
1193 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
1195 .entity weaponentity = weaponentities[slot];
1196 if(it.(weaponentity).hook.aiment == this)
1197 RemoveHook(it.(weaponentity).hook);
1202 Vehicle info = this.vehicledef; //REGISTRY_GET(Vehicles, this.vehicleid);
1203 info.vr_spawn(info, this);
1205 vehicles_reset_colors(this, NULL);
1207 CSQCMODEL_AUTOINIT(this);
1210 bool vehicle_initialize(entity this, Vehicle info, bool nodrop)
1212 if(!autocvar_g_vehicles)
1219 info.vr_precache(info);
1221 if(this.targetname && this.targetname != "")
1223 this.vehicle_controller = find(NULL, target, this.targetname);
1224 if(!this.vehicle_controller)
1226 LOG_DEBUG("^1WARNING: ^7Vehicle with invalid .targetname");
1227 this.active = ACTIVE_ACTIVE;
1231 this.team = this.vehicle_controller.team;
1232 this.use = vehicle_use;
1236 if(this.vehicle_controller.team == 0)
1237 this.active = ACTIVE_NOT;
1239 this.active = ACTIVE_ACTIVE;
1243 else { this.active = ACTIVE_ACTIVE; }
1245 if(this.team && (!teamplay || !autocvar_g_vehicles_teams))
1248 if(this.mdl == "" || !this.mdl)
1249 _setmodel(this, info.model);
1251 _setmodel(this, this.mdl);
1253 this.vehicle_flags |= VHF_ISVEHICLE;
1255 this.vehicle_viewport = new(vehicle_viewport);
1256 this.vehicle_hudmodel = new(vehicle_hudmodel);
1257 this.tur_head = new(tur_head);
1258 this.tur_head.owner = this;
1259 this.takedamage = DAMAGE_NO;
1260 this.bot_attack = true;
1261 IL_PUSH(g_bot_targets, this);
1262 this.iscreature = true;
1263 this.teleportable = false; // no teleporting for vehicles, too buggy
1264 this.damagedbycontents = true;
1265 IL_PUSH(g_damagedbycontents, this);
1266 this.vehicleid = info.vehicleid;
1267 this.vehicledef = info;
1268 this.PlayerPhysplug = info.PlayerPhysplug;
1269 this.event_damage = func_null;
1270 this.event_heal = func_null;
1271 settouch(this, vehicles_touch);
1272 setthink(this, vehicles_spawn);
1273 this.nextthink = time;
1274 this.effects = EF_NODRAW;
1275 this.dphitcontentsmask = DPCONTENTS_BODY | DPCONTENTS_SOLID;
1277 if(autocvar_g_playerclip_collisions)
1278 this.dphitcontentsmask |= DPCONTENTS_PLAYERCLIP;
1280 if(autocvar_g_nodepthtestplayers)
1281 this.effects |= EF_NODEPTHTEST;
1283 if(autocvar_g_fullbrightplayers)
1284 this.effects |= EF_FULLBRIGHT;
1286 _setmodel(this.vehicle_hudmodel, info.hud_model);
1287 setmodel(this.vehicle_viewport, MDL_Null);
1289 if(info.head_model != "")
1291 _setmodel(this.tur_head, info.head_model);
1292 setattachment(this.tur_head, this, info.tag_head);
1293 setattachment(this.vehicle_hudmodel, this.tur_head, info.tag_hud);
1294 setattachment(this.vehicle_viewport, this.vehicle_hudmodel, info.tag_view);
1298 setattachment(this.tur_head, this, "");
1299 setattachment(this.vehicle_hudmodel, this, info.tag_hud);
1300 setattachment(this.vehicle_viewport, this.vehicle_hudmodel, info.tag_view);
1303 setsize(this, info.m_mins, info.m_maxs);
1305 info.vr_setup(info, this);
1309 setorigin(this, this.origin);
1310 tracebox(this.origin + '0 0 100', info.m_mins, info.m_maxs, this.origin - '0 0 10000', MOVE_WORLDONLY, this);
1311 setorigin(this, trace_endpos);
1314 this.pos1 = this.origin;
1315 this.pos2 = this.angles;
1316 this.tur_head.team = this.team;
1318 if(this.active == ACTIVE_NOT)
1319 this.nextthink = 0; // wait until activated
1320 else if(autocvar_g_vehicles_delayspawn)
1321 this.nextthink = time + this.respawntime + (random() * autocvar_g_vehicles_delayspawn_jitter);
1323 this.nextthink = time + game_starttime;
1325 if(MUTATOR_CALLHOOK(VehicleInit, this))