]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/common/vehicles/sv_vehicles.qc
Merge branch 'master' into Mario/vehicles
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / vehicles / sv_vehicles.qc
index b3a324695bdbea79df3820dc6b66ddbee262ea82..b06596bbc9cd4da869ba3cab1d758831e210a96b 100644 (file)
@@ -1,9 +1,45 @@
-// =========================
-//  SVQC Vehicle Properties
-// =========================
+#include "../effects.qh"
+#include "vehicles.qh"
+#include "sv_vehicles.qh"
 
+#if 0
+bool vehicle_send(entity to, int sf)
+{
+       WriteByte(MSG_ENTITY, ENT_CLIENT_VEHICLE);
+       WriteByte(MSG_ENTITY, sf);
+
+       if(sf & VSF_SPAWN)
+       {
+               WriteByte(MSG_ENTITY, self.vehicleid);
+       }
+
+       if(sf & VSF_SETUP)
+       {
+               // send stuff?
+       }
+
+       if(sf & VSF_ENTER)
+       {
+               // player handles the .vehicle stuff, we need only set ourselves up for driving
+
+               // send stuff?
+       }
 
-float SendAuxiliaryXhair(entity to, float sf)
+       if(sf & VSF_EXIT)
+       {
+               // senf stuff?
+       }
+
+       if(sf & VSF_PRECACHE)
+       {
+               // send stuff?!
+       }
+
+       return true;
+}
+#endif
+
+bool SendAuxiliaryXhair(entity to, int sf)
 {
 
        WriteByte(MSG_ENTITY, ENT_CLIENT_AUXILIARYXHAIR);
@@ -18,12 +54,12 @@ float SendAuxiliaryXhair(entity to, float sf)
        WriteByte(MSG_ENTITY, rint(self.colormod_y * 255));
        WriteByte(MSG_ENTITY, rint(self.colormod_z * 255));
 
-       return TRUE;
+       return true;
 }
 
-void UpdateAuxiliaryXhair(entity own, vector loc, vector clr, float axh_id)
+void UpdateAuxiliaryXhair(entity own, vector loc, vector clr, int axh_id)
 {
-       if not(IS_REAL_CLIENT(own))
+       if(!IS_REAL_CLIENT(own))
                return;
 
        entity axh;
@@ -37,7 +73,7 @@ void UpdateAuxiliaryXhair(entity own, vector loc, vector clr, float axh_id)
                axh.cnt                          = axh_id;
                axh.drawonlytoclient    = own;
                axh.owner                          = own;
-               Net_LinkEntity(axh, FALSE, 0, SendAuxiliaryXhair);
+               Net_LinkEntity(axh, false, 0, SendAuxiliaryXhair);
        }
 
        setorigin(axh, loc);
@@ -46,26 +82,22 @@ void UpdateAuxiliaryXhair(entity own, vector loc, vector clr, float axh_id)
        own.(AuxiliaryXhair[axh_id]) = axh;
 }
 
-void CSQCVehicleSetup(entity own, float vehicle_id)
+void CSQCVehicleSetup(entity own, int vehicle_id)
 {
-       if not(IS_REAL_CLIENT(own))
+       if(!IS_REAL_CLIENT(own))
                return;
 
        msg_entity = own;
 
        WriteByte(MSG_ONE, SVC_TEMPENTITY);
        WriteByte(MSG_ONE, TE_CSQC_VEHICLESETUP);
-       if(vehicle_id != 0)
-               WriteByte(MSG_ONE, vehicle_id);
-       else
-               WriteByte(MSG_ONE, 1 + own.vehicle.vehicle_weapon2mode + VEH_LAST);
+       WriteByte(MSG_ONE, vehicle_id);
 }
 
 vector targetdrone_getnewspot()
 {
-
        vector spot;
-       float i;
+       int i;
        for(i = 0; i < 100; ++i)
        {
                spot = self.origin + randomvec() * 1024;
@@ -99,16 +131,17 @@ void vehicles_locktarget(float incr, float decr, float _lock_time)
 
        if(trace_ent != world)
        {
-               if(teamplay && trace_ent.team == self.team)
+               if(SAME_TEAM(trace_ent, self))
                        trace_ent = world;
 
                if(trace_ent.deadflag != DEAD_NO)
                        trace_ent = world;
 
-               if not (trace_ent.vehicle_flags & VHF_ISVEHICLE ||
-                               trace_ent.turrcaps_flags & TFL_TURRCAPS_ISTURRET ||
-                               trace_ent.takedamage == DAMAGE_TARGETDRONE)
+               if(!(IS_VEHICLE(trace_ent) || IS_TURRET(trace_ent)))
                        trace_ent = world;
+
+               if(trace_ent.alpha <= 0.5 && trace_ent.alpha != 0)
+                       trace_ent = world; // invisible
        }
 
        if(self.lock_target == world && trace_ent != world)
@@ -126,7 +159,6 @@ void vehicles_locktarget(float incr, float decr, float _lock_time)
                        play2(self.owner, "vehicles/locking.wav");
                        self.lock_soundtime = time + 0.3;
                }
-
        }
 
        // Have a locking target
@@ -182,7 +214,7 @@ vector vehicles_force_fromtag_maglev(string tag_name, float spring_length, float
 }
 
 // projectile handling
-void vehicles_projectile_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
+void vehicles_projectile_damage(entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
 {
        // Ignore damage from oterh projectiles from my owner (dont mess up volly's)
        if(inflictor.owner == self.owner)
@@ -213,7 +245,7 @@ void vehicles_projectile_explode()
        PROJECTILE_TOUCH;
 
        self.event_damage = func_null;
-       RadiusDamage (self, self.realowner, self.shot_dmg, 0, self.shot_radius, self, self.shot_force, self.totalfrags, other);
+       RadiusDamage (self, self.realowner, self.shot_dmg, 0, self.shot_radius, self, world, self.shot_force, self.totalfrags, other);
 
        remove (self);
 }
@@ -221,8 +253,8 @@ void vehicles_projectile_explode()
 entity vehicles_projectile(string _mzlfx, string _mzlsound,
                                                   vector _org, vector _vel,
                                                   float _dmg, float _radi, float _force,  float _size,
-                                                  float _deahtype, float _projtype, float _health,
-                                                  float _cull, float _clianim, entity _owner)
+                                                  int _deahtype, float _projtype, float _health,
+                                                  bool _cull, bool _clianim, entity _owner)
 {
        entity proj;
 
@@ -238,7 +270,7 @@ entity vehicles_projectile(string _mzlfx, string _mzlsound,
        proj.solid                      = SOLID_BBOX;
        proj.movetype            = MOVETYPE_FLYMISSILE;
        proj.flags                      = FL_PROJECTILE;
-       proj.bot_dodge          = TRUE;
+       proj.bot_dodge          = true;
        proj.bot_dodgerating  = _dmg;
        proj.velocity            = _vel;
        proj.touch                      = vehicles_projectile_explode;
@@ -261,8 +293,7 @@ entity vehicles_projectile(string _mzlfx, string _mzlsound,
                sound (self, CH_WEAPON_A, _mzlsound, VOL_BASE, ATTEN_NORM);
 
        if(_mzlfx)
-               pointparticles(particleeffectnum(_mzlfx), proj.origin, proj.velocity, 1);
-
+               Send_Effect(_mzlfx, proj.origin, proj.velocity, 1);
 
        setsize (proj, '-1 -1 -1' * _size, '1 1 1' * _size);
 
@@ -274,7 +305,8 @@ entity vehicles_projectile(string _mzlfx, string _mzlsound,
 void vehicles_gib_explode()
 {
        sound (self, CH_SHOTS, "weapons/rocket_impact.wav", VOL_BASE, ATTEN_NORM);
-       pointparticles(particleeffectnum("explosion_small"), randomvec() * 80 + (self.origin + '0 0 100'), '0 0 0', 1);
+       Send_Effect("explosion_small", randomvec() * 80 + (self.origin + '0 0 100'), '0 0 0', 1);
+       Send_Effect("explosion_small", self.wp00.origin + '0 0 64', '0 0 0', 1);
        remove(self);
 }
 
@@ -287,7 +319,7 @@ void vehicles_gib_think()
                self.nextthink = time + 0.1;
 }
 
-entity vehicle_tossgib(entity _template, vector _vel, string _tag, float _burn, float _explode, float _maxtime, vector _rot)
+entity vehicle_tossgib(entity _template, vector _vel, string _tag, bool _burn, bool _explode, float _maxtime, vector _rot)
 {
        entity _gib = spawn();
        setmodel(_gib, _template.model);
@@ -318,18 +350,19 @@ entity vehicle_tossgib(entity _template, vector _vel, string _tag, float _burn,
        return _gib;
 }
 
-float vehicle_addplayerslot(   entity _owner,
+bool vehicle_addplayerslot(    entity _owner,
                                                                entity _slot,
-                                                               float _hud,
+                                                               int _hud,
                                                                string _hud_model,
-                                                               float() _framefunc,
-                                                               void(float) _exitfunc)
+                                                               bool() _framefunc,
+                                                               void(bool) _exitfunc, float() _enterfunc)
 {
-       if not (_owner.vehicle_flags & VHF_MULTISLOT)
+       if(!(_owner.vehicle_flags & VHF_MULTISLOT))
                _owner.vehicle_flags |= VHF_MULTISLOT;
 
        _slot.PlayerPhysplug = _framefunc;
        _slot.vehicle_exit = _exitfunc;
+       _slot.vehicle_enter = _enterfunc;
        _slot.hud = _hud;
        _slot.vehicle_flags = VHF_PLAYERSLOT;
        _slot.vehicle_viewport = spawn();
@@ -343,7 +376,7 @@ float vehicle_addplayerslot(        entity _owner,
        setattachment(_slot.vehicle_hudmodel, _slot, "");
        setattachment(_slot.vehicle_viewport, _slot.vehicle_hudmodel, "");
 
-       return TRUE;
+       return true;
 }
 
 vector vehicle_aimturret(entity _vehic, vector _target, entity _turrret, string _tagname,
@@ -355,7 +388,7 @@ vector vehicle_aimturret(entity _vehic, vector _target, entity _turrret, string
        vtag = gettaginfo(_turrret, gettagindex(_turrret, _tagname));
        vtmp = vectoangles(normalize(_target - vtag));
        vtmp = AnglesTransform_ToAngles(AnglesTransform_LeftDivide(AnglesTransform_FromAngles(_vehic.angles), AnglesTransform_FromAngles(vtmp))) - _turrret.angles;
-       vtmp = AnglesTransform_Normalize(vtmp, TRUE);
+       vtmp = AnglesTransform_Normalize(vtmp, true);
        ftmp = _aimspeed * frametime;
        vtmp_y = bound(-ftmp, vtmp_y, ftmp);
        vtmp_x = bound(-ftmp, vtmp_x, ftmp);
@@ -397,6 +430,19 @@ void vehicles_reset_colors()
                }
                e = e.chain;
        }
+       // Also check head tags
+       e = findchainentity(tag_entity, self.tur_head);
+       while(e)
+       {
+               if(e != self.vehicle_shieldent)
+               {
+                       e.effects   = _effects; //  | EF_LOWPRECISION;
+                       e.colormod  = _colormod;
+                       e.colormap  = _colormap;
+                       e.alpha  = 1;
+               }
+               e = e.chain;
+       }
 
        self.vehicle_hudmodel.effects  = self.effects  = _effects; // | EF_LOWPRECISION;
        self.vehicle_hudmodel.colormod = self.colormod = _colormod;
@@ -434,7 +480,7 @@ void vehicles_clearreturn(entity veh)
 void vehicles_spawn();
 void vehicles_return()
 {
-       pointparticles(particleeffectnum("teleport"), self.wp00.origin + '0 0 64', '0 0 0', 1);
+       Send_Effect("teleport", self.wp00.origin + '0 0 64', '0 0 0', 1);
 
        self.wp00.think  = vehicles_spawn;
        self.wp00.nextthink = time;
@@ -484,7 +530,7 @@ void vehicles_showwp()
                rgb = Team_ColorRGB(self.team);
        else
                rgb = '1 1 1';
-       WaypointSprite_Spawn("vehicle", 0, 0, self, '0 0 64', world, 0, self, waypointsprite_attached, TRUE, RADARICON_POWERUP, rgb);
+       WaypointSprite_Spawn("vehicle", 0, 0, self, '0 0 64', world, 0, self, waypointsprite_attached, true, RADARICON_POWERUP, rgb);
        if(self.waypointsprite_attached)
        {
                WaypointSprite_UpdateRule(self.waypointsprite_attached, self.wp00.team, SPRITERULE_DEFAULT);
@@ -535,11 +581,19 @@ void vehicle_use()
        else
                self.active = ACTIVE_ACTIVE;
 
-       if(self.active == ACTIVE_ACTIVE && self.deadflag == DEAD_NO)
+       if(self.active == ACTIVE_ACTIVE && self.deadflag == DEAD_NO && !gameover)
        {
                dprint("Respawning vehicle: ", self.netname, "\n");
-               vehicles_setreturn(self);
-               vehicles_reset_colors();
+               if(self.effects & EF_NODRAW)
+               {
+                       self.think = vehicles_spawn;
+                       self.nextthink = time + 3;
+               }
+               else
+               {
+                       vehicles_setreturn(self);
+                       vehicles_reset_colors();
+               }
        }
 }
 
@@ -595,27 +649,33 @@ void vehicles_painframe()
        }
 }
 
-void vehicles_damage(entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
+void vehicles_damage(entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
 {
        self.dmg_time = time;
 
-       if(DEATH_ISWEAPON(deathtype, WEP_NEX))
-               damage *= autocvar_g_vehicles_nex_damagerate;
+       // WEAPONTODO
+       if(DEATH_ISWEAPON(deathtype, WEP_VORTEX.m_id))
+               damage *= autocvar_g_vehicles_vortex_damagerate;
 
-       if(DEATH_ISWEAPON(deathtype, WEP_UZI))
-               damage *= autocvar_g_vehicles_uzi_damagerate;
+       if(DEATH_ISWEAPON(deathtype, WEP_MACHINEGUN.m_id))
+               damage *= autocvar_g_vehicles_machinegun_damagerate;
 
-       if(DEATH_ISWEAPON(deathtype, WEP_RIFLE))
+       if(DEATH_ISWEAPON(deathtype, WEP_RIFLE.m_id))
                damage *= autocvar_g_vehicles_rifle_damagerate;
 
-       if(DEATH_ISWEAPON(deathtype, WEP_MINSTANEX))
-               damage *= autocvar_g_vehicles_minstanex_damagerate;
+       if(DEATH_ISWEAPON(deathtype, WEP_VAPORIZER.m_id))
+               damage *= autocvar_g_vehicles_vaporizer_damagerate;
 
-       if(DEATH_ISWEAPON(deathtype, WEP_SEEKER))
+       if(DEATH_ISWEAPON(deathtype, WEP_SEEKER.m_id))
                damage *= autocvar_g_vehicles_tag_damagerate;
 
+       if(DEATH_WEAPONOFWEAPONDEATH(deathtype))
+               damage *= autocvar_g_vehicles_weapon_damagerate;
+
        self.enemy = attacker;
 
+       self.pain_finished = time;
+
        if((self.vehicle_flags & VHF_HASSHIELD) && (self.vehicle_shield > 0))
        {
                if (wasfreed(self.vehicle_shieldent) || self.vehicle_shieldent == world)
@@ -672,7 +732,7 @@ void vehicles_damage(entity inflictor, entity attacker, float damage, float deat
                        if(self.vehicle_flags & VHF_DEATHEJECT)
                                vehicles_exit(VHEF_EJECT);
                        else
-                               vehicles_exit(VHEF_RELESE);
+                               vehicles_exit(VHEF_RELEASE);
 
 
                antilag_clear(self);
@@ -684,13 +744,13 @@ void vehicles_damage(entity inflictor, entity attacker, float damage, float deat
 
 float vehicles_crushable(entity e)
 {
-       if(IS_PLAYER(e))
-               return TRUE;
+       if(IS_PLAYER(e) && time >= e.vehicle_enter_delay)
+               return true;
 
-       if(e.flags & FL_MONSTER)
-               return TRUE;
+       if(IS_MONSTER(e))
+               return true;
 
-       return FALSE;
+       return false;
 }
 
 void vehicles_impact(float _minspeed, float _speedfac, float _maxpain)
@@ -764,7 +824,7 @@ vector vehicles_findgoodexit(vector prefer_spot)
        return self.origin;
 }
 
-void vehicles_exit(float eject)
+void vehicles_exit(bool eject)
 {
        entity _vehicle;
        entity _player;
@@ -776,7 +836,7 @@ void vehicles_exit(float eject)
                return;
        }
 
-       vehicles_exit_running = TRUE;
+       vehicles_exit_running = true;
        if(IS_CLIENT(self))
        {
                _vehicle = self.vehicle;
@@ -785,7 +845,7 @@ void vehicles_exit(float eject)
                {
                        _vehicle.vehicle_exit(eject);
                        self = _oldself;
-                       vehicles_exit_running = FALSE;
+                       vehicles_exit_running = false;
                        return;
                }
        }
@@ -816,6 +876,7 @@ void vehicles_exit(float eject)
                _player.solid                   = SOLID_SLIDEBOX;
                _player.movetype                = MOVETYPE_WALK;
                _player.effects            &= ~EF_NODRAW;
+               _player.teleportable    = TELEPORT_NORMAL;
                _player.alpha                   = 1;
                _player.PlayerPhysplug  = func_null;
                _player.vehicle                 = world;
@@ -824,6 +885,7 @@ void vehicles_exit(float eject)
                _player.hud                             = HUD_NORMAL;
                _player.switchweapon    = _vehicle.switchweapon;
                _player.last_vehiclecheck = time + 3;
+               _player.vehicle_enter_delay = time + 2;
 
                CSQCVehicleSetup(_player, HUD_NORMAL);
        }
@@ -834,7 +896,7 @@ void vehicles_exit(float eject)
 
        _vehicle.tur_head.nodrawtoclient = world;
 
-       if not(teamplay)
+       if(!teamplay)
                _vehicle.team = 0;
 
        Kill_Notification(NOTIF_ONE, _player, MSG_CENTER_CPID, CPID_VEHICLES);
@@ -842,11 +904,7 @@ void vehicles_exit(float eject)
 
        WaypointSprite_Kill(_vehicle.wps_intruder);
 
-       vh_player = _player;
-       vh_vehicle = _vehicle;
-       MUTATOR_CALLHOOK(VehicleExit);
-       _player = vh_player;
-       _vehicle = vh_vehicle;
+       MUTATOR_CALLHOOK(VehicleExit, _player, _vehicle);
 
        _vehicle.team = _vehicle.tur_head.team;
 
@@ -859,23 +917,28 @@ void vehicles_exit(float eject)
        vehicles_setreturn(_vehicle);
        vehicles_reset_colors();
        _vehicle.owner = world;
+
+       CSQCMODEL_AUTOINIT();
+
        self = _oldself;
 
-       vehicles_exit_running = FALSE;
+       vehicles_exit_running = false;
 }
 
 void vehicles_touch()
 {
-       if(MUTATOR_CALLHOOK(VehicleTouch))
+       if(MUTATOR_CALLHOOK(VehicleTouch, self, other))
                return;
 
        // Vehicle currently in use
        if(self.owner)
        {
+               if(!forbidWeaponUse(self.owner))
                if(other != world)
+               if((self.origin_z + self.maxs_z) > (other.origin_z))
                if(vehicles_crushable(other))
                {
-                       if(vlen(self.velocity) != 0)
+                       if(vlen(self.velocity) >= 30)
                                Damage(other, self, self.owner, autocvar_g_vehicles_crush_dmg, DEATH_VH_CRUSH, '0 0 0', normalize(other.origin - self.origin) * autocvar_g_vehicles_crush_force);
 
                        return; // Dont do selfdamage when hitting "soft targets".
@@ -893,29 +956,62 @@ void vehicles_touch()
        vehicles_enter(other, self);
 }
 
-void vehicles_enter(entity pl, entity veh)
+bool vehicle_impulse(int imp)
 {
-   // Remove this when bots know how to use vehicles
-       if (IS_BOT_CLIENT(pl))
-       if (autocvar_g_vehicles_allow_bots)
-               dprint("Bot enters vehicle\n"); // This is where we need to disconnect (some, all?) normal bot AI and hand over to vehicle's _aiframe()
-       else
-               return;
+       switch(imp)
+       {
+               case 17:
+               {
+                       stuffcmd(self, "\ntoggle cl_eventchase_vehicle\nset _vehicles_shownchasemessage 1\n");
+                       return true;
+               }
+       }
 
-       if not(IS_PLAYER(pl))
-               return;
+       return false;
+}
 
-       if(veh.phase > time)
+void vehicles_enter(entity pl, entity veh)
+{
+   // Remove this when bots know how to use vehicles
+       if((IS_BOT_CLIENT(pl) && !autocvar_g_vehicles_allow_bots))
                return;
 
-       if(pl.freezetag_frozen)
-               return;
+       if((!IS_PLAYER(pl))
+       || (veh.phase >= time)
+       || (pl.vehicle_enter_delay >= time)
+       || (pl.frozen)
+       || (pl.deadflag != DEAD_NO)
+       || (pl.vehicle)
+       ) { return; }
+
+       if(autocvar_g_vehicles_enter) // vehicle's touch function should handle this if entering via use key is disabled (TODO)
+       if(veh.vehicle_flags & VHF_MULTISLOT)
+       if(veh.owner)
+       {
+               entity oldself = self;
+               self = veh;
+               other = pl; // TODO: fix
+
+               if(!veh.gunner1)
+               if(time >= veh.gun1.phase)
+               if(veh.gun1.vehicle_enter)
+               if(veh.gun1.vehicle_enter())
+               {
+                       self = oldself;
+                       return;
+               }
 
-       if(pl.deadflag != DEAD_NO)
-               return;
+               if(!veh.gunner2)
+               if(time >= veh.gun2.phase)
+               if(veh.gun2.vehicle_enter)
+               if(veh.gun2.vehicle_enter())
+               {
+                       self = oldself;
+                       return;
+               }
 
-       if(pl.vehicle)
-               return;
+               self = oldself;
+       }
 
        if(teamplay)
        if(veh.team)
@@ -928,7 +1024,8 @@ void vehicles_enter(entity pl, entity veh)
 
                Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_VEHICLE_STEAL_SELF);
 
-               WaypointSprite_Spawn("intruder", 0, 0, pl, '0 0 68', world, veh.team, veh, wps_intruder, TRUE, RADARICON_DANGER, Team_ColorRGB(pl.team));
+               if(autocvar_g_vehicles_steal_show_waypoint)
+                       WaypointSprite_Spawn("intruder", 0, 0, pl, '0 0 68', world, veh.team, veh, wps_intruder, true, RADARICON_DANGER, Team_ColorRGB(pl.team));
        }
        else return;
 
@@ -948,30 +1045,33 @@ void vehicles_enter(entity pl, entity veh)
 
        veh.vehicle_hudmodel.viewmodelforclient = pl;
 
+       tracebox(pl.origin, PL_MIN, PL_MAX, pl.origin, false, pl);
+       pl.crouch = false;
+       pl.view_ofs = PL_VIEW_OFS;
+       setsize (pl, PL_MIN, PL_MAX);
+
        veh.event_damage        = vehicles_damage;
        veh.nextthink           = 0;
        pl.angles                       = veh.angles;
        pl.takedamage           = DAMAGE_NO;
        pl.solid                        = SOLID_NOT;
        pl.movetype                     = MOVETYPE_NOCLIP;
+       pl.teleportable         = false;
        pl.alpha                        = -1;
-       pl.vehicle                      = veh;
        pl.event_damage         = func_null;
        pl.view_ofs                     = '0 0 0';
        veh.colormap            = pl.colormap;
        if(veh.tur_head)
                veh.tur_head.colormap = pl.colormap;
        veh.switchweapon = pl.switchweapon;
-       if(veh.hud)
-               pl.hud = veh.hud;
-       else
-               pl.hud = veh.vehicleid;
+       pl.hud = veh.vehicleid;
        pl.PlayerPhysplug = veh.PlayerPhysplug;
 
        pl.vehicle_ammo1 = veh.vehicle_ammo1;
        pl.vehicle_ammo2 = veh.vehicle_ammo2;
        pl.vehicle_reload1 = veh.vehicle_reload1;
        pl.vehicle_reload2 = veh.vehicle_reload2;
+       pl.vehicle_energy = veh.vehicle_energy;
 
        // Cant do this, hides attached objects too.
        //veh.exteriormodeltoclient = veh.owner;
@@ -1010,14 +1110,11 @@ void vehicles_enter(entity pl, entity veh)
 
        CSQCVehicleSetup(pl, veh.vehicleid);
 
-       vh_player = pl;
-       vh_vehicle = veh;
-       MUTATOR_CALLHOOK(VehicleEnter);
-       pl = vh_player;
-       veh = vh_vehicle;
+       MUTATOR_CALLHOOK(VehicleEnter, pl, veh);
 
        entity oldself = self;
        self = veh;
+       CSQCModel_UnlinkEntity();
        VEH_ACTION(veh.vehicleid, VR_ENTER);
        self = oldself;
 
@@ -1028,13 +1125,18 @@ void vehicles_think()
 {
        self.nextthink = time;
        
+       if(self.owner)
+               self.owner.vehicle_weapon2mode = self.vehicle_weapon2mode;
+       
        VEH_ACTION(self.vehicleid, VR_THINK);
+
+       CSQCMODEL_AUTOUPDATE();
 }
 
 // initialization
 void vehicles_spawn()
 {
-       dprint("Spawning vehicle: ", self.netname, "\n");
+       dprint("Spawning vehicle: ", self.classname, "\n");
 
        // disown & reset
        self.vehicle_hudmodel.viewmodelforclient = self;
@@ -1042,14 +1144,14 @@ void vehicles_spawn()
        self.owner                              = world;
        self.touch                              = vehicles_touch;
        self.event_damage               = vehicles_damage;
-       self.iscreature                 = TRUE;
-       self.teleportable               = FALSE; // no teleporting for vehicles, too buggy
-       self.damagedbycontents  = TRUE;
+       self.iscreature                 = true;
+       self.teleportable               = false; // no teleporting for vehicles, too buggy
+       self.damagedbycontents  = true;
        self.movetype                   = MOVETYPE_WALK;
        self.solid                              = SOLID_SLIDEBOX;
        self.takedamage                 = DAMAGE_AIM;
        self.deadflag                   = DEAD_NO;
-       self.bot_attack                 = TRUE;
+       self.bot_attack                 = true;
        self.flags                              = FL_NOTARGET;
        self.avelocity                  = '0 0 0';
        self.velocity                   = '0 0 0';
@@ -1063,31 +1165,44 @@ void vehicles_spawn()
 
        // Return to spawn
        self.angles = self.pos2;
-       setorigin(self, self.pos1 + '0 0 0');
+       setorigin(self, self.pos1);
        // Show it
-       pointparticles(particleeffectnum("teleport"), self.origin + '0 0 64', '0 0 0', 1);
+       Send_Effect("teleport", self.origin + '0 0 64', '0 0 0', 1);
 
        if(self.vehicle_controller)
                self.team = self.vehicle_controller.team;
 
+       entity head; // remove hooks (if any)
+       FOR_EACH_PLAYER(head)
+       if(head.hook.aiment == self)
+               RemoveGrapplingHook(head);
+
        vehicles_reset_colors();
 
        VEH_ACTION(self.vehicleid, VR_SPAWN);
+
+       CSQCMODEL_AUTOINIT();
 }
 
-float vehicle_initialize(float vehicle_id, float nodrop)
+bool vehicle_initialize(int vehicle_id, bool nodrop)
 {
-       if not(autocvar_g_vehicles)
-               return FALSE;
+       if(!autocvar_g_vehicles)
+               return false;
 
        entity veh = get_vehicleinfo(vehicle_id);
 
-       if(self.targetname)
+       if(!veh.vehicleid)
+               return false;
+       
+       if(!veh.tur_head) { VEH_ACTION(vehicle_id, VR_PRECACHE); }
+
+       if(self.targetname && self.targetname != "")
        {
                self.vehicle_controller = find(world, target, self.targetname);
-               if not(self.vehicle_controller)
+               if(!self.vehicle_controller)
                {
                        bprint("^1WARNING: ^7Vehicle with invalid .targetname\n");
+                       self.active = ACTIVE_ACTIVE;
                }
                else
                {
@@ -1103,23 +1218,27 @@ float vehicle_initialize(float vehicle_id, float nodrop)
                        }
                }
        }
+       else { self.active = ACTIVE_ACTIVE; }
 
-       if(self.team && !teamplay)
+       if(self.team && (!teamplay || !autocvar_g_vehicles_teams))
                self.team = 0;
 
-       self.vehicle_flags |= VHF_ISVEHICLE;
+       if(self.mdl == "" || !self.mdl)
+               setmodel(self, veh.model);
+       else
+               setmodel(self, self.mdl);
 
-       setmodel(self, veh.model);
+       self.vehicle_flags |= VHF_ISVEHICLE;
 
        self.vehicle_viewport           = spawn();
        self.vehicle_hudmodel           = spawn();
        self.tur_head                           = spawn();
        self.tur_head.owner                     = self;
-       self.takedamage                         = DAMAGE_AIM;
-       self.bot_attack                         = TRUE;
-       self.iscreature                         = TRUE;
-       self.teleportable                       = FALSE; // no teleporting for vehicles, too buggy
-       self.damagedbycontents          = TRUE;
+       self.takedamage                         = DAMAGE_NO;
+       self.bot_attack                         = true;
+       self.iscreature                         = true;
+       self.teleportable                       = false; // no teleporting for vehicles, too buggy
+       self.damagedbycontents          = true;
        self.vehicleid                          = vehicle_id;
        self.PlayerPhysplug                     = veh.PlayerPhysplug;
        self.event_damage                       = func_null;
@@ -1157,7 +1276,7 @@ float vehicle_initialize(float vehicle_id, float nodrop)
 
        setsize(self, veh.mins, veh.maxs);
 
-       if not(nodrop)
+       if(!nodrop)
        {
                setorigin(self, self.origin);
                tracebox(self.origin + '0 0 100', veh.mins, veh.maxs, self.origin - '0 0 10000', MOVE_WORLDONLY, self);
@@ -1170,13 +1289,15 @@ float vehicle_initialize(float vehicle_id, float nodrop)
 
        VEH_ACTION(vehicle_id, VR_SETUP);
 
-       if(autocvar_g_vehicles_delayspawn)
+       if(self.active == ACTIVE_NOT)
+               self.nextthink = 0; // wait until activated
+       else if(autocvar_g_vehicles_delayspawn)
                self.nextthink = time + self.respawntime + (random() * autocvar_g_vehicles_delayspawn_jitter);
        else
                self.nextthink = time + game_starttime;
 
        if(MUTATOR_CALLHOOK(VehicleSpawn))
-               return FALSE;
+               return false;
 
-       return TRUE;
+       return true;
 }