1 #include "sv_vehicles.qh"
3 bool SendAuxiliaryXhair(entity this, entity to, int sf)
5 WriteHeader(MSG_ENTITY, ENT_CLIENT_AUXILIARYXHAIR);
6 WriteByte(MSG_ENTITY, sf);
8 WriteByte(MSG_ENTITY, this.cnt);
12 WriteVector(MSG_ENTITY, this.origin);
17 WriteByte(MSG_ENTITY, rint(this.colormod_x * 255));
18 WriteByte(MSG_ENTITY, rint(this.colormod_y * 255));
19 WriteByte(MSG_ENTITY, rint(this.colormod_z * 255));
25 bool AuxiliaryXhair_customize(entity this, entity client)
27 entity e = WaypointSprite_getviewentity(client);
28 entity axh = e.(AuxiliaryXhair[this.cnt]);
29 return axh.owner == this.owner; // cheaply check if the client's axh owner is the same as our real owner
32 .vector axh_prevorigin;
33 .vector axh_prevcolors;
35 void UpdateAuxiliaryXhair(entity own, vector loc, vector clr, int axh_id)
37 if(!IS_REAL_CLIENT(own))
40 axh_id = bound(0, axh_id, MAX_AXH);
41 entity axh = own.(AuxiliaryXhair[axh_id]);
43 if(axh == NULL || wasfreed(axh)) // MADNESS? THIS IS QQQQCCCCCCCCC (wasfreed, why do you exsist? Mario: because of sloppy code like this)
45 axh = new(auxiliary_xhair);
47 //axh.drawonlytoclient = own; // not spectatable
48 setcefc(axh, AuxiliaryXhair_customize);
50 Net_LinkEntity(axh, false, 0, SendAuxiliaryXhair);
53 if(loc != axh.axh_prevorigin)
59 if(clr != axh.axh_prevcolors)
65 own.(AuxiliaryXhair[axh_id]) = axh; // set it anyway...?
68 void CSQCVehicleSetup(entity own, int vehicle_id)
70 if(!IS_REAL_CLIENT(own))
75 WriteHeader(MSG_ONE, TE_CSQC_VEHICLESETUP);
76 WriteByte(MSG_ONE, vehicle_id);
78 if(vehicle_id == 0 || vehicle_id == HUD_NORMAL)
80 for(int i = 0; i < MAX_AXH; ++i)
82 entity axh = own.(AuxiliaryXhair[i]);
83 own.(AuxiliaryXhair[i]) = NULL;
85 if(axh.owner == own && axh != NULL && !wasfreed(axh))
91 void vehicles_locktarget(entity this, float incr, float decr, float _lock_time)
93 if(this.lock_target && IS_DEAD(this.lock_target))
95 this.lock_target = NULL;
96 this.lock_strength = 0;
100 if(this.lock_time > time)
103 if(this.lock_soundtime < time)
105 this.lock_soundtime = time + 0.5;
106 play2(this.owner, "vehicles/locked.wav");
112 if(trace_ent != NULL)
114 if(SAME_TEAM(trace_ent, this))
117 if(IS_DEAD(trace_ent))
120 if(!(IS_VEHICLE(trace_ent) || IS_TURRET(trace_ent)))
123 if(trace_ent.alpha <= 0.5 && trace_ent.alpha != 0)
124 trace_ent = NULL; // invisible
127 if(this.lock_target == NULL && trace_ent != NULL)
128 this.lock_target = trace_ent;
130 if(this.lock_target && trace_ent == this.lock_target)
132 if(this.lock_strength != 1 && this.lock_strength + incr >= 1)
134 play2(this.owner, "vehicles/lock.wav");
135 this.lock_soundtime = time + 0.8;
137 else if (this.lock_strength != 1 && this.lock_soundtime < time)
139 play2(this.owner, "vehicles/locking.wav");
140 this.lock_soundtime = time + 0.3;
144 // Have a locking target
145 // Trace hit current target
146 if(trace_ent == this.lock_target && trace_ent != NULL)
148 this.lock_strength = min(this.lock_strength + incr, 1);
149 if(this.lock_strength == 1)
150 this.lock_time = time + _lock_time;
155 this.lock_strength = max(this.lock_strength - decr * 2, 0);
157 this.lock_strength = max(this.lock_strength - decr, 0);
159 if(this.lock_strength == 0)
160 this.lock_target = NULL;
164 float vehicle_altitude(entity this, float amax)
166 tracebox(this.origin, this.mins, this.maxs, this.origin - ('0 0 1' * amax), MOVE_WORLDONLY, this);
167 return vlen(this.origin - trace_endpos);
170 vector vehicles_force_fromtag_hover(entity this, string tag_name, float spring_length, float max_power)
172 force_fromtag_origin = gettaginfo(this, gettagindex(this, tag_name));
173 v_forward = normalize(v_forward) * -1;
174 traceline(force_fromtag_origin, force_fromtag_origin - (v_forward * spring_length), MOVE_NORMAL, this);
176 force_fromtag_power = (1 - trace_fraction) * max_power;
177 force_fromtag_normpower = force_fromtag_power / max_power;
179 return v_forward * force_fromtag_power;
182 vector vehicles_force_fromtag_maglev(entity this, string tag_name, float spring_length, float max_power)
184 force_fromtag_origin = gettaginfo(this, gettagindex(this, tag_name));
185 v_forward = normalize(v_forward) * -1;
186 traceline(force_fromtag_origin, force_fromtag_origin - (v_forward * spring_length), MOVE_NORMAL, this);
188 // TODO - this may NOT be compatible with wall/celing movement, unhardcode 0.25 (engine count multiplier)
189 if(trace_fraction == 1.0)
191 force_fromtag_normpower = -0.25;
195 force_fromtag_power = ((1 - trace_fraction) - trace_fraction) * max_power;
196 force_fromtag_normpower = force_fromtag_power / max_power;
198 return v_forward * force_fromtag_power;
201 // projectile handling
202 void vehicles_projectile_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
204 // Ignore damage from oterh projectiles from my owner (dont mess up volly's)
205 if(inflictor.owner == this.owner)
208 TakeResource(this, RESOURCE_HEALTH, damage);
209 this.velocity += force;
210 if(GetResourceAmount(this, RESOURCE_HEALTH) < 1)
212 this.takedamage = DAMAGE_NO;
213 this.event_damage = func_null;
214 setthink(this, adaptor_think2use);
215 this.nextthink = time;
219 void vehicles_projectile_explode(entity this, entity toucher)
221 if(this.owner && toucher != NULL)
223 if(toucher == this.owner.vehicle)
226 if(toucher == this.owner.vehicle.tur_head)
230 PROJECTILE_TOUCH(this, toucher);
232 this.event_damage = func_null;
233 RadiusDamage (this, this.realowner, this.shot_dmg, 0, this.shot_radius, this, NULL, this.shot_force, this.totalfrags, DMG_NOWEP, toucher);
238 void vehicles_projectile_explode_think(entity this)
240 vehicles_projectile_explode(this, NULL);
243 void vehicles_projectile_explode_use(entity this, entity actor, entity trigger)
245 vehicles_projectile_explode(this, trigger);
248 entity vehicles_projectile(entity this, string _mzlfx, Sound _mzlsound,
249 vector _org, vector _vel,
250 float _dmg, float _radi, float _force, float _size,
251 int _deahtype, float _projtype, float _health,
252 bool _cull, bool _clianim, entity _owner)
254 TC(Sound, _mzlsound);
259 PROJECTILE_MAKETRIGGER(proj);
260 setorigin(proj, _org);
262 proj.shot_dmg = _dmg;
263 proj.shot_radius = _radi;
264 proj.shot_force = _force;
265 proj.totalfrags = _deahtype;
266 proj.solid = SOLID_BBOX;
267 set_movetype(proj, MOVETYPE_FLYMISSILE);
268 proj.flags = FL_PROJECTILE;
269 IL_PUSH(g_projectiles, proj);
270 IL_PUSH(g_bot_dodge, proj);
271 proj.bot_dodge = true;
272 proj.bot_dodgerating = _dmg;
273 proj.velocity = _vel;
274 settouch(proj, vehicles_projectile_explode);
275 proj.use = vehicles_projectile_explode_use;
277 proj.realowner = _owner;
278 setthink(proj, SUB_Remove);
279 proj.nextthink = time + 30;
283 proj.takedamage = DAMAGE_AIM;
284 proj.event_damage = vehicles_projectile_damage;
285 SetResourceAmountExplicit(proj, RESOURCE_HEALTH, _health);
288 proj.flags |= FL_NOTARGET;
290 if(_mzlsound != SND_Null)
291 sound (this, CH_WEAPON_A, _mzlsound, VOL_BASE, ATTEN_NORM);
294 Send_Effect_(_mzlfx, proj.origin, proj.velocity, 1);
296 setsize (proj, '-1 -1 -1' * _size, '1 1 1' * _size);
298 CSQCProjectile(proj, _clianim, _projtype, _cull);
303 void vehicles_gib_explode(entity this)
305 sound (this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
306 Send_Effect(EFFECT_EXPLOSION_SMALL, randomvec() * 80 + (this.origin + '0 0 100'), '0 0 0', 1);
307 Send_Effect(EFFECT_EXPLOSION_SMALL, this.wp00.origin + '0 0 64', '0 0 0', 1);
311 void vehicles_gib_touch(entity this, entity toucher)
313 vehicles_gib_explode(this);
316 void vehicles_gib_think(entity this)
322 this.nextthink = time + 0.1;
325 entity vehicle_tossgib(entity this, entity _template, vector _vel, string _tag, bool _burn, bool _explode, float _maxtime, vector _rot)
327 entity _gib = spawn();
328 _setmodel(_gib, _template.model);
329 vector org = gettaginfo(this, gettagindex(this, _tag));
330 setorigin(_gib, org);
331 _gib.velocity = _vel;
332 set_movetype(_gib, MOVETYPE_TOSS);
333 _gib.solid = SOLID_CORPSE;
334 _gib.colormod = '-0.5 -0.5 -0.5';
335 _gib.effects = EF_LOWPRECISION;
336 _gib.avelocity = _rot;
339 _gib.effects |= EF_FLAME;
343 setthink(_gib, vehicles_gib_explode);
344 _gib.nextthink = time + random() * _explode;
345 settouch(_gib, vehicles_gib_touch);
349 _gib.cnt = time + _maxtime;
350 setthink(_gib, vehicles_gib_think);
351 _gib.nextthink = time + _maxtime - 1;
357 bool vehicle_addplayerslot( entity _owner,
361 bool(entity,float) _framefunc,
362 void(entity,bool) _exitfunc, float(entity, entity) _enterfunc)
364 if(!(_owner.vehicle_flags & VHF_MULTISLOT))
365 _owner.vehicle_flags |= VHF_MULTISLOT;
367 _slot.PlayerPhysplug = _framefunc;
368 _slot.vehicle_exit = _exitfunc;
369 _slot.vehicle_enter = _enterfunc;
370 STAT(HUD, _slot) = _hud;
371 _slot.vehicle_flags = VHF_PLAYERSLOT;
372 _slot.vehicle_viewport = spawn();
373 _slot.vehicle_hudmodel = spawn();
374 _slot.vehicle_hudmodel.viewmodelforclient = _slot;
375 _slot.vehicle_viewport.effects = (EF_ADDITIVE | EF_DOUBLESIDED | EF_FULLBRIGHT | EF_NODEPTHTEST | EF_NOGUNBOB | EF_NOSHADOW | EF_LOWPRECISION | EF_SELECTABLE | EF_TELEPORT_BIT);
377 setmodel(_slot.vehicle_hudmodel, _hud_model);
378 setmodel(_slot.vehicle_viewport, MDL_Null);
380 setattachment(_slot.vehicle_hudmodel, _slot, "");
381 setattachment(_slot.vehicle_viewport, _slot.vehicle_hudmodel, "");
386 vector vehicle_aimturret(entity _vehic, vector _target, entity _turrret, string _tagname,
387 float _pichlimit_min, float _pichlimit_max,
388 float _rotlimit_min, float _rotlimit_max, float _aimspeed)
392 vtag = gettaginfo(_turrret, gettagindex(_turrret, _tagname));
393 vtmp = vectoangles(normalize(_target - vtag));
394 vtmp = AnglesTransform_ToAngles(AnglesTransform_LeftDivide(AnglesTransform_FromAngles(_vehic.angles), AnglesTransform_FromAngles(vtmp))) - _turrret.angles;
395 vtmp = AnglesTransform_Normalize(vtmp, true);
396 ftmp = _aimspeed * frametime;
397 vtmp_y = bound(-ftmp, vtmp_y, ftmp);
398 vtmp_x = bound(-ftmp, vtmp_x, ftmp);
399 _turrret.angles_y = bound(_rotlimit_min, _turrret.angles_y + vtmp_y, _rotlimit_max);
400 _turrret.angles_x = bound(_pichlimit_min, _turrret.angles_x + vtmp_x, _pichlimit_max);
404 void vehicles_reset_colors(entity this, entity player)
407 const vector cmod = '0 0 0';
408 if(this.team && teamplay)
409 cmap = 1024 + (this.team - 1) * 17;
411 cmap = player.colormap;
414 if(autocvar_g_nodepthtestplayers)
415 eff |= EF_NODEPTHTEST;
416 if(autocvar_g_fullbrightplayers)
417 eff |= EF_FULLBRIGHT;
419 // Find all ents attacked to main model and setup effects, colormod etc.
420 FOREACH_ENTITY_ENT(tag_entity, this,
422 if(it == this.vehicle_shieldent)
431 // Also check head tags
432 FOREACH_ENTITY_ENT(tag_entity, this.tur_head,
434 if(it == this.vehicle_shieldent)
443 this.vehicle_hudmodel.effects = this.effects = eff; // | EF_LOWPRECISION;
444 this.vehicle_hudmodel.colormod = this.colormod = cmod;
445 this.vehicle_hudmodel.colormap = this.colormap = cmap;
446 this.vehicle_viewport.effects = (EF_ADDITIVE | EF_DOUBLESIDED | EF_FULLBRIGHT | EF_NODEPTHTEST | EF_NOGUNBOB | EF_NOSHADOW | EF_LOWPRECISION | EF_SELECTABLE | EF_TELEPORT_BIT);
449 this.avelocity = '0 0 0';
450 this.velocity = '0 0 0';
453 Vehicle info = Vehicles_from(this.vehicleid);
454 info.vr_setcolors(info, this);
457 void vehicles_clearreturn(entity veh)
459 // Remove "return helper" entities, if any.
460 IL_EACH(g_vehicle_returners, it.wp00 == veh,
463 setthink(it, SUB_Remove);
464 it.nextthink = time + 0.1;
465 IL_REMOVE(g_vehicle_returners, it);
467 if(it.waypointsprite_attached)
468 WaypointSprite_Kill(it.waypointsprite_attached);
472 void vehicles_spawn(entity this);
473 void vehicles_return(entity this)
475 Send_Effect(EFFECT_TELEPORT, this.wp00.origin + '0 0 64', '0 0 0', 1);
477 setthink(this.wp00, vehicles_spawn);
478 this.wp00.nextthink = time;
480 if(this.waypointsprite_attached)
481 WaypointSprite_Kill(this.waypointsprite_attached);
486 void vehicles_showwp_goaway(entity this)
488 if(this.waypointsprite_attached)
489 WaypointSprite_Kill(this.waypointsprite_attached);
494 void vehicles_showwp(entity this)
500 setthink(ent, vehicles_return);
501 ent.nextthink = ent.cnt;
505 setthink(ent, vehicles_return);
506 ent.nextthink = time + 1;
509 ent.team = this.wp00.team;
510 ent.wp00 = this.wp00;
511 setorigin(ent, this.wp00.pos1);
513 ent.nextthink = time + 5;
514 setthink(ent, vehicles_showwp_goaway);
518 if(teamplay && ent.team)
519 rgb = Team_ColorRGB(ent.team);
522 entity wp = WaypointSprite_Spawn(WP_Vehicle, 0, 0, ent, '0 0 64', NULL, 0, ent, waypointsprite_attached, true, RADARICON_Vehicle);
523 wp.wp_extra = ent.wp00.vehicleid;
525 if(ent.waypointsprite_attached)
527 WaypointSprite_UpdateRule(ent.waypointsprite_attached, ent.wp00.team, SPRITERULE_DEFAULT);
529 WaypointSprite_UpdateBuildFinished(ent.waypointsprite_attached, ent.nextthink);
530 WaypointSprite_Ping(ent.waypointsprite_attached);
534 void vehicles_setreturn(entity veh)
536 vehicles_clearreturn(veh);
538 entity ret = new(vehicle_return);
539 IL_PUSH(g_vehicle_returners, ret);
542 setthink(ret, vehicles_showwp);
546 ret.cnt = time + veh.respawntime;
547 ret.nextthink = min(time + veh.respawntime, time + veh.respawntime - 5);
551 ret.nextthink = min(time + veh.respawntime, time + veh.respawntime - 1);
554 setorigin(ret, veh.pos1 + '0 0 96');
557 void vehicle_use(entity this, entity actor, entity trigger)
559 LOG_DEBUG("vehicle ", this.netname, " used by ", actor.classname);
561 this.tur_head.team = actor.team;
563 if(this.tur_head.team == 0)
564 this.active = ACTIVE_NOT;
566 this.active = ACTIVE_ACTIVE;
568 if(this.active == ACTIVE_ACTIVE && !IS_DEAD(this) && !game_stopped)
570 LOG_DEBUG("Respawning vehicle: ", this.netname);
571 if(this.effects & EF_NODRAW)
573 setthink(this, vehicles_spawn);
574 this.nextthink = time + 3;
578 vehicles_setreturn(this);
579 vehicles_reset_colors(this, actor);
584 void vehicles_regen(entity this, float timer, .float regen_field, float field_max, float rpause, float regen, float delta_time, float _healthscale)
586 if(this.(regen_field) < field_max)
587 if(timer + rpause < time)
590 regen = regen * (this.vehicle_health / this.max_health);
592 this.(regen_field) = min(this.(regen_field) + regen * delta_time, field_max);
595 this.owner.(regen_field) = (this.(regen_field) / field_max) * 100;
599 void shieldhit_think(entity this)
604 // setmodel(this, MDL_Null);
606 this.effects |= EF_NODRAW;
610 this.nextthink = time + 0.1;
614 void vehicles_painframe(entity this)
616 int myhealth = ((this.owner) ? this.owner.vehicle_health : ((this.vehicle_health / this.max_health) * 100));
619 if(this.pain_frame < time)
621 float _ftmp = myhealth / 50;
622 this.pain_frame = time + max(0.1, 0.1 + (random() * 0.5 * _ftmp));
623 Send_Effect(EFFECT_SMOKE_SMALL, (this.origin + (randomvec() * 80)), '0 0 0', 1);
625 if(this.vehicle_flags & VHF_DMGSHAKE)
626 this.velocity += randomvec() * 30;
628 if(this.vehicle_flags & VHF_DMGROLL)
629 if(this.vehicle_flags & VHF_DMGHEADROLL)
630 this.tur_head.angles += randomvec();
632 this.angles += randomvec();
636 void vehicles_frame(entity this, entity actor)
638 vehicles_painframe(this);
641 void vehicles_damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
643 this.dmg_time = time;
646 if(DEATH_ISWEAPON(deathtype, WEP_VORTEX))
647 damage *= autocvar_g_vehicles_vortex_damagerate;
648 else if(DEATH_ISWEAPON(deathtype, WEP_MACHINEGUN))
649 damage *= autocvar_g_vehicles_machinegun_damagerate;
650 else if(DEATH_ISWEAPON(deathtype, WEP_RIFLE))
651 damage *= autocvar_g_vehicles_rifle_damagerate;
652 else if(DEATH_ISWEAPON(deathtype, WEP_VAPORIZER))
653 damage *= autocvar_g_vehicles_vaporizer_damagerate;
654 else if(DEATH_ISWEAPON(deathtype, WEP_SEEKER))
655 damage *= autocvar_g_vehicles_tag_damagerate;
656 else if(DEATH_WEAPONOF(deathtype) != WEP_Null)
657 damage *= autocvar_g_vehicles_weapon_damagerate;
659 this.enemy = attacker;
661 this.pain_finished = time;
663 if((this.vehicle_flags & VHF_HASSHIELD) && (this.vehicle_shield > 0))
665 if (wasfreed(this.vehicle_shieldent) || this.vehicle_shieldent == NULL)
667 this.vehicle_shieldent = spawn();
668 this.vehicle_shieldent.effects = EF_LOWPRECISION;
670 setmodel(this.vehicle_shieldent, MDL_VEH_SHIELD);
671 setattachment(this.vehicle_shieldent, this, "");
672 setorigin(this.vehicle_shieldent, real_origin(this) - this.origin);
673 this.vehicle_shieldent.scale = 256 / vlen(this.maxs - this.mins);
674 setthink(this.vehicle_shieldent, shieldhit_think);
677 this.vehicle_shieldent.colormod = '1 1 1';
678 this.vehicle_shieldent.alpha = 0.45;
679 this.vehicle_shieldent.angles = vectoangles(normalize(hitloc - (this.origin + this.vehicle_shieldent.origin))) - this.angles;
680 this.vehicle_shieldent.nextthink = time;
681 this.vehicle_shieldent.effects &= ~EF_NODRAW;
683 this.vehicle_shield -= damage;
685 if(this.vehicle_shield < 0)
687 this.vehicle_health -= fabs(this.vehicle_shield);
688 this.vehicle_shieldent.colormod = '2 0 0';
689 this.vehicle_shield = 0;
690 this.vehicle_shieldent.alpha = 0.75;
692 if(sound_allowed(MSG_BROADCAST, attacker))
693 spamsound (this, CH_PAIN, SND_ONS_HIT2, VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
696 if(sound_allowed(MSG_BROADCAST, attacker))
697 spamsound (this, CH_PAIN, SND_ONS_ELECTRICITY_EXPLODE, VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
702 this.vehicle_health -= damage;
704 if(sound_allowed(MSG_BROADCAST, attacker))
705 spamsound (this, CH_PAIN, SND_ONS_HIT2, VOL_BASE, ATTEN_NORM); // FIXME: PLACEHOLDER
708 if(this.damageforcescale < 1 && this.damageforcescale > 0)
709 this.velocity += force * this.damageforcescale;
711 this.velocity += force;
713 if(this.vehicle_health <= 0)
716 if(this.vehicle_flags & VHF_DEATHEJECT)
717 vehicles_exit(this, VHEF_EJECT);
719 vehicles_exit(this, VHEF_RELEASE);
722 antilag_clear(this, this);
724 Vehicle info = Vehicles_from(this.vehicleid);
725 info.vr_death(info, this);
726 vehicles_setreturn(this);
730 bool vehicles_heal(entity targ, entity inflictor, float amount, float limit)
732 float true_limit = ((limit) ? limit : targ.max_health);
733 //if(GetResourceAmount(targ, RESOURCE_HEALTH) <= 0 || GetResourceAmount(targ, RESOURCE_HEALTH) >= true_limit)
734 if(targ.vehicle_health <= 0 || targ.vehicle_health >= true_limit)
737 targ.vehicle_health = min(targ.vehicle_health + amount, true_limit);
738 //GiveResourceWithLimit(targ, RESOURCE_HEALTH, amount, true_limit);
740 //targ.owner.vehicle_health = (targ.vehicle_health / targ.max_health) * 100;
744 bool vehicles_crushable(entity e)
746 if(IS_PLAYER(e) && time >= e.vehicle_enter_delay)
755 void vehicles_impact(entity this, float _minspeed, float _speedfac, float _maxpain)
757 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
760 if(this.play_time < time)
762 float wc = vlen(this.velocity - this.oldvelocity);
763 //dprint("oldvel: ", vtos(this.oldvelocity), "\n");
764 //dprint("vel: ", vtos(this.velocity), "\n");
767 float take = min(_speedfac * wc, _maxpain);
768 Damage (this, NULL, NULL, take, DEATH_FALL.m_id, DMG_NOWEP, this.origin, '0 0 0');
769 this.play_time = time + 0.25;
771 //dprint("wc: ", ftos(wc), "\n");
772 //dprint("take: ", ftos(take), "\n");
777 // vehicle enter/exit handling
778 vector vehicles_findgoodexit(entity this, entity player, vector prefer_spot)
780 // TODO: we actually want the player's size here
781 tracebox(this.origin + '0 0 32', PL_MIN_CONST, PL_MAX_CONST, prefer_spot, MOVE_NORMAL, player);
782 if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
785 float mysize = 1.5 * vlen(this.maxs - this.mins);
787 vector v2 = 0.5 * (this.absmin + this.absmax);
788 for(int i = 0; i < autocvar_g_vehicles_exit_attempts; ++i)
792 v = v2 + normalize(v) * mysize;
793 tracebox(v2, PL_MIN_CONST, PL_MAX_CONST, v, MOVE_NORMAL, player);
794 if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
801 .int old_vehicle_flags;
802 void vehicles_exit(entity vehic, bool eject)
804 entity player = vehic.owner;
806 if(vehicles_exit_running)
808 LOG_TRACE("^1vehicles_exit already running! this is not good...");
812 vehicles_exit_running = true;
814 // TODO: this was in an IS_CLIENT check, make sure it isn't actually needed!
815 if(vehic.vehicle_flags & VHF_PLAYERSLOT)
817 vehic.vehicle_exit(vehic, eject);
818 vehicles_exit_running = false;
824 if (IS_REAL_CLIENT(player))
827 WriteByte (MSG_ONE, SVC_SETVIEWPORT);
828 WriteEntity( MSG_ONE, player);
830 WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
831 WriteAngle(MSG_ONE, 0);
832 WriteAngle(MSG_ONE, vehic.angles_y);
833 WriteAngle(MSG_ONE, 0);
836 setsize(player, STAT(PL_MIN, player), STAT(PL_MAX, player));
838 player.takedamage = DAMAGE_AIM;
839 player.solid = SOLID_SLIDEBOX;
840 set_movetype(player, MOVETYPE_WALK);
841 player.effects &= ~EF_NODRAW;
842 player.teleportable = TELEPORT_NORMAL;
844 player.PlayerPhysplug = func_null;
845 player.vehicle = NULL;
846 player.view_ofs = STAT(PL_VIEW_OFS, player);
847 player.event_damage = PlayerDamage;
848 STAT(HUD, player) = HUD_NORMAL;
849 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++ slot)
851 .entity weaponentity = weaponentities[slot];
852 player.(weaponentity).m_switchweapon = vehic.(weaponentity).m_switchweapon;
853 delete(vehic.(weaponentity)); // no longer needed
855 player.last_vehiclecheck = time + 3;
856 player.vehicle_enter_delay = time + 2;
858 CSQCVehicleSetup(player, HUD_NORMAL);
860 Kill_Notification(NOTIF_ONE, player, MSG_CENTER, CPID_VEHICLES);
861 Kill_Notification(NOTIF_ONE, player, MSG_CENTER, CPID_VEHICLES_OTHER); // kill all vehicle notifications when exiting a vehicle?
864 vehic.flags |= FL_NOTARGET;
867 vehic.avelocity = '0 0 0';
869 vehic.tur_head.nodrawtoclient = NULL;
874 WaypointSprite_Kill(vehic.wps_intruder);
876 MUTATOR_CALLHOOK(VehicleExit, player, vehic);
878 vehic.team = vehic.tur_head.team;
880 if(vehic.old_vehicle_flags & VHF_SHIELDREGEN)
881 vehic.vehicle_flags |= VHF_SHIELDREGEN;
882 vehic.old_vehicle_flags = 0;
884 sound (vehic, CH_TRIGGER_SINGLE, SND_Null, 1, ATTEN_NORM);
885 vehic.vehicle_hudmodel.viewmodelforclient = vehic;
886 vehic.phase = time + 1;
888 vehic.vehicle_exit(vehic, eject);
890 vehicles_setreturn(vehic);
891 vehicles_reset_colors(vehic, NULL);
894 CSQCMODEL_AUTOINIT(vehic);
897 player.oldorigin = player.origin; // player's location is set by the exit functions, so we need to do this after everything
899 vehicles_exit_running = false;
902 void vehicles_touch(entity this, entity toucher)
904 if(MUTATOR_CALLHOOK(VehicleTouch, this, toucher))
907 // Vehicle currently in use
910 if(!forbidWeaponUse(this.owner))
912 if((this.origin_z + this.maxs_z) > (toucher.origin_z))
913 if(vehicles_crushable(toucher))
915 if(vdist(this.velocity, >=, 30))
916 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);
918 return; // Dont do selfdamage when hitting "soft targets".
921 if(this.play_time < time) {
922 Vehicle info = Vehicles_from(this.vehicleid);
923 info.vr_impact(info, this);
929 if(autocvar_g_vehicles_enter)
932 vehicles_enter(toucher, this);
935 bool vehicle_impulse(entity this, int imp)
937 entity v = this.vehicle;
938 if (!v) return false;
939 if (IS_DEAD(v)) return false;
940 bool(entity,int) f = v.vehicles_impulse;
941 if (f && f(this,imp)) return true;
944 case IMP_weapon_drop.impulse:
946 stuffcmd(this, "\ntoggle cl_eventchase_vehicle\nset _vehicles_shownchasemessage 1\n");
953 void vehicles_enter(entity pl, entity veh)
955 // Remove this when bots know how to use vehicles
956 if((IS_BOT_CLIENT(pl) && !autocvar_g_vehicles_allow_bots))
960 || (veh.phase >= time)
961 || (pl.vehicle_enter_delay >= time)
962 || (STAT(FROZEN, pl))
967 Vehicle info = Vehicles_from(veh.vehicleid);
969 if(autocvar_g_vehicles_enter) // vehicle's touch function should handle this if entering via use key is disabled (TODO)
970 if(veh.vehicle_flags & VHF_MULTISLOT)
971 if(veh.owner && SAME_TEAM(pl, veh))
973 // we don't need a return value or anything here
974 // if successful the owner check below will prevent anything weird
975 info.vr_gunner_enter(info, veh, pl);
979 return; // got here and didn't enter the gunner, return
983 if(DIFF_TEAM(pl, veh))
984 if(autocvar_g_vehicles_steal)
986 FOREACH_CLIENT(IS_PLAYER(it) && SAME_TEAM(it, veh), Send_Notification(NOTIF_ONE, it, MSG_CENTER, CENTER_VEHICLE_STEAL));
988 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_VEHICLE_STEAL_SELF);
990 veh.vehicle_shield = 0;
991 veh.old_vehicle_flags = veh.vehicle_flags; // make a backup just so we're not permanently crippling this vehicle
992 veh.vehicle_flags &= ~VHF_SHIELDREGEN;
994 if (autocvar_g_vehicles_steal_show_waypoint) {
995 entity wp = WaypointSprite_Spawn(WP_VehicleIntruder, 0, 0, pl, '0 0 68', NULL, veh.team, veh, wps_intruder, true, RADARICON_DANGER);
996 wp.colormod = Team_ColorRGB(pl.team);
1001 RemoveGrapplingHooks(pl);
1003 veh.vehicle_ammo1 = 0;
1004 veh.vehicle_ammo2 = 0;
1005 veh.vehicle_reload1 = 0;
1006 veh.vehicle_reload2 = 0;
1007 veh.vehicle_energy = 0;
1012 // .viewmodelforclient works better.
1013 //veh.vehicle_hudmodel.drawonlytoclient = veh.owner;
1015 veh.vehicle_hudmodel.viewmodelforclient = pl;
1018 pl.view_ofs = STAT(PL_VIEW_OFS, pl);
1019 setsize(pl, STAT(PL_MIN, pl), STAT(PL_MAX, pl));
1021 veh.event_damage = vehicles_damage;
1022 veh.event_heal = vehicles_heal;
1024 pl.items &= ~IT_USING_JETPACK;
1025 pl.angles = veh.angles;
1026 pl.takedamage = DAMAGE_NO;
1027 pl.solid = SOLID_NOT;
1028 pl.disableclientprediction = 1; // physics is no longer run, so this won't be reset
1029 set_movetype(pl, MOVETYPE_NOCLIP);
1030 pl.teleportable = false;
1032 pl.event_damage = func_null;
1033 pl.view_ofs = '0 0 0';
1034 veh.colormap = pl.colormap;
1036 veh.tur_head.colormap = pl.colormap;
1037 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
1039 .entity weaponentity = weaponentities[slot];
1040 veh.(weaponentity) = new(temp_wepent);
1041 veh.(weaponentity).m_switchweapon = pl.(weaponentity).m_switchweapon;
1043 STAT(HUD, pl) = veh.vehicleid;
1044 pl.PlayerPhysplug = veh.PlayerPhysplug;
1046 pl.vehicle_ammo1 = veh.vehicle_ammo1;
1047 pl.vehicle_ammo2 = veh.vehicle_ammo2;
1048 pl.vehicle_reload1 = veh.vehicle_reload1;
1049 pl.vehicle_reload2 = veh.vehicle_reload2;
1050 pl.vehicle_energy = veh.vehicle_energy;
1052 // Cant do this, hides attached objects too.
1053 //veh.exteriormodeltoclient = veh.owner;
1054 //veh.tur_head.exteriormodeltoclient = veh.owner;
1057 UNSET_ONGROUND(veh);
1060 veh.flags -= FL_NOTARGET;
1062 vehicles_reset_colors(veh, pl);
1064 if (IS_REAL_CLIENT(pl))
1066 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_VEHICLE_ENTER);
1069 WriteByte (MSG_ONE, SVC_SETVIEWPORT);
1070 WriteEntity(MSG_ONE, veh.vehicle_viewport);
1072 WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
1075 WriteAngle(MSG_ONE, veh.tur_head.angles_x + veh.angles_x); // tilt
1076 WriteAngle(MSG_ONE, veh.tur_head.angles_y + veh.angles_y); // yaw
1077 WriteAngle(MSG_ONE, 0); // roll
1081 WriteAngle(MSG_ONE, veh.angles_x * -1); // tilt
1082 WriteAngle(MSG_ONE, veh.angles_y); // yaw
1083 WriteAngle(MSG_ONE, 0); // roll
1087 vehicles_clearreturn(veh);
1089 CSQCVehicleSetup(pl, veh.vehicleid);
1091 MUTATOR_CALLHOOK(VehicleEnter, pl, veh);
1093 CSQCModel_UnlinkEntity(veh);
1094 info.vr_enter(info, veh);
1096 antilag_clear(pl, CS(pl));
1099 void vehicles_think(entity this)
1101 this.nextthink = time + autocvar_g_vehicles_thinkrate;
1104 STAT(VEHICLESTAT_W2MODE, this.owner) = STAT(VEHICLESTAT_W2MODE, this);
1106 Vehicle info = Vehicles_from(this.vehicleid);
1107 info.vr_think(info, this);
1109 vehicles_painframe(this);
1111 CSQCMODEL_AUTOUPDATE(this);
1114 void vehicles_reset(entity this)
1117 vehicles_exit(this, VHEF_RELEASE);
1119 vehicles_clearreturn(this);
1121 if(this.active != ACTIVE_NOT)
1122 vehicles_spawn(this);
1126 void vehicles_spawn(entity this)
1128 LOG_DEBUG("Spawning vehicle: ", this.classname);
1131 this.vehicle_hudmodel.viewmodelforclient = this;
1134 settouch(this, vehicles_touch);
1135 this.event_damage = vehicles_damage;
1136 this.event_heal = vehicles_heal;
1137 this.reset = vehicles_reset;
1138 this.iscreature = true;
1139 this.teleportable = false; // no teleporting for vehicles, too buggy
1140 this.damagedbycontents = true;
1141 set_movetype(this, MOVETYPE_WALK);
1142 this.solid = SOLID_SLIDEBOX;
1143 this.takedamage = DAMAGE_AIM;
1144 this.deadflag = DEAD_NO;
1145 if(!this.bot_attack)
1146 IL_PUSH(g_bot_targets, this);
1147 this.bot_attack = true;
1148 this.flags = FL_NOTARGET;
1149 this.avelocity = '0 0 0';
1150 this.velocity = '0 0 0';
1151 setthink(this, vehicles_think);
1152 this.nextthink = time;
1155 this.lock_strength = 0;
1156 this.lock_target = NULL;
1157 this.misc_bulletcounter = 0;
1160 this.angles = this.pos2;
1161 setorigin(this, this.pos1);
1163 Send_Effect(EFFECT_TELEPORT, this.origin + '0 0 64', '0 0 0', 1);
1165 if(this.vehicle_controller)
1166 this.team = this.vehicle_controller.team;
1168 FOREACH_CLIENT(IS_PLAYER(it),
1170 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
1172 .entity weaponentity = weaponentities[slot];
1173 if(it.(weaponentity).hook.aiment == this)
1174 RemoveHook(it.(weaponentity).hook);
1179 Vehicle info = Vehicles_from(this.vehicleid);
1180 info.vr_spawn(info, this);
1182 vehicles_reset_colors(this, NULL);
1184 CSQCMODEL_AUTOINIT(this);
1187 bool vehicle_initialize(entity this, Vehicle info, bool nodrop)
1189 if(!autocvar_g_vehicles)
1197 info.vr_precache(info);
1198 IL_PUSH(g_vehicles, this);
1201 if(this.targetname && this.targetname != "")
1203 this.vehicle_controller = find(NULL, target, this.targetname);
1204 if(!this.vehicle_controller)
1206 LOG_DEBUG("^1WARNING: ^7Vehicle with invalid .targetname");
1207 this.active = ACTIVE_ACTIVE;
1211 this.team = this.vehicle_controller.team;
1212 this.use = vehicle_use;
1216 if(this.vehicle_controller.team == 0)
1217 this.active = ACTIVE_NOT;
1219 this.active = ACTIVE_ACTIVE;
1223 else { this.active = ACTIVE_ACTIVE; }
1225 if(this.team && (!teamplay || !autocvar_g_vehicles_teams))
1228 if(this.mdl == "" || !this.mdl)
1229 _setmodel(this, info.model);
1231 _setmodel(this, this.mdl);
1233 this.vehicle_flags |= VHF_ISVEHICLE;
1235 this.vehicle_viewport = new(vehicle_viewport);
1236 this.vehicle_hudmodel = new(vehicle_hudmodel);
1237 this.tur_head = new(tur_head);
1238 this.tur_head.owner = this;
1239 this.takedamage = DAMAGE_NO;
1240 this.bot_attack = true;
1241 IL_PUSH(g_bot_targets, this);
1242 this.iscreature = true;
1243 this.teleportable = false; // no teleporting for vehicles, too buggy
1244 this.damagedbycontents = true;
1245 IL_PUSH(g_damagedbycontents, this);
1246 this.vehicleid = info.vehicleid;
1247 this.PlayerPhysplug = info.PlayerPhysplug;
1248 this.event_damage = func_null;
1249 this.event_heal = func_null;
1250 settouch(this, vehicles_touch);
1251 setthink(this, vehicles_spawn);
1252 this.nextthink = time;
1253 this.effects = EF_NODRAW;
1254 this.dphitcontentsmask = DPCONTENTS_BODY | DPCONTENTS_SOLID;
1256 if(autocvar_g_playerclip_collisions)
1257 this.dphitcontentsmask |= DPCONTENTS_PLAYERCLIP;
1259 if(autocvar_g_nodepthtestplayers)
1260 this.effects |= EF_NODEPTHTEST;
1262 if(autocvar_g_fullbrightplayers)
1263 this.effects |= EF_FULLBRIGHT;
1265 _setmodel(this.vehicle_hudmodel, info.hud_model);
1266 setmodel(this.vehicle_viewport, MDL_Null);
1268 if(info.head_model != "")
1270 _setmodel(this.tur_head, info.head_model);
1271 setattachment(this.tur_head, this, info.tag_head);
1272 setattachment(this.vehicle_hudmodel, this.tur_head, info.tag_hud);
1273 setattachment(this.vehicle_viewport, this.vehicle_hudmodel, info.tag_view);
1277 setattachment(this.tur_head, this, "");
1278 setattachment(this.vehicle_hudmodel, this, info.tag_hud);
1279 setattachment(this.vehicle_viewport, this.vehicle_hudmodel, info.tag_view);
1282 setsize(this, info.m_mins, info.m_maxs);
1284 info.vr_setup(info, this);
1288 setorigin(this, this.origin);
1289 tracebox(this.origin + '0 0 100', info.m_mins, info.m_maxs, this.origin - '0 0 10000', MOVE_WORLDONLY, this);
1290 setorigin(this, trace_endpos);
1293 this.pos1 = this.origin;
1294 this.pos2 = this.angles;
1295 this.tur_head.team = this.team;
1297 if(this.active == ACTIVE_NOT)
1298 this.nextthink = 0; // wait until activated
1299 else if(autocvar_g_vehicles_delayspawn)
1300 this.nextthink = time + this.respawntime + (random() * autocvar_g_vehicles_delayspawn_jitter);
1302 this.nextthink = time + game_starttime;
1304 if(MUTATOR_CALLHOOK(VehicleInit, this))