namespace cnpc_otis
{
	
const uint g_MaxAmmoPrimary		= 7;
const string sWeaponName		= "weapon_otis";

const array<string> pPainSounds = 
{
	"barney/ba_pain1.wav",
	"barney/ba_pain2.wav",
	"barney/ba_pain3.wav"
};

const array<string> pDieSounds = 
{
	"barney/ba_die1.wav",
	"barney/ba_die2.wav",
	"barney/ba_die3.wav"
};

enum states_e
{
	STATE_IDLE = 0,
	STATE_WALK,
	STATE_RUN,
	STATE_DEATH,
	STATE_ATTACK_EAGLE,
	STATE_RELOAD
};

class weapon_otis : CBaseDriveWeapon
{
	int m_iShell;
	float m_fNextRegenTick;
	float m_fNextHealthCheck;
	private int m_iState;
	private int m_iAutoDeploy;
	private int m_iGunMuzzleFlash;

	void Spawn()
	{
		Precache();

		g_EntityFuncs.SetModel( self, "models/cnpc/otis.mdl" );
		g_EntityFuncs.DispatchKeyValue( self.edict(), "renderamt", 128 );
		g_EntityFuncs.DispatchKeyValue( self.edict(), "rendermode", 2 );
		g_EntityFuncs.DispatchKeyValue( self.edict(), "renderfx", 15 );

		self.pev.sequence = self.LookupSequence("idle1");
		self.pev.frame = 0;
		self.ResetSequenceInfo();

		self.m_iDefaultAmmo = 1006;

		m_iState = STATE_IDLE;

		self.FallInit();
	}

	void Precache()
	{
		BaseClass.Precache();
		self.PrecacheCustomModels();
		//g_Game.PrecacheGeneric( "sprites/controlnpc/weapon_otis.txt" );
		g_Game.PrecacheModel( "models/cnpc/otis.mdl" );
		m_iShell = g_Game.PrecacheModel( "models/shell.mdl" );
		m_iGunMuzzleFlash = g_Game.PrecacheModel( "sprites/muzzleflash2.spr" );

		for( uint i = 0; i < pPainSounds.length(); i++ )
			g_SoundSystem.PrecacheSound( pPainSounds[i] );

		for( uint i = 0; i < pDieSounds.length(); i++ )
			g_SoundSystem.PrecacheSound( pDieSounds[i] );
			g_SoundSystem.PrecacheSound( "common/npc_step2.wav" );
			g_SoundSystem.PrecacheSound( "common/npc_step4.wav" );
			g_SoundSystem.PrecacheSound( "weapons/desert_eagle_fire.wav" );
	}

	bool GetItemInfo( ItemInfo& out info )
	{
		info.iMaxAmmo1	= 999;
		info.iMaxClip		= 7;
		info.iSlot		= 8;
		info.iPosition		= 1;
		info.iFlags 			= ITEM_FLAG_SELECTONEMPTY; //to prevent monster from being despawned if out of ammo TODO
		info.iWeight			= 0; //-1 ??

		return true;
	}

	bool AddToPlayer( CBasePlayer@ pPlayer )
	{
		if( !BaseClass.AddToPlayer(pPlayer) )
			return false;

		@m_pPlayer = pPlayer;

		/*NetworkMessage m1( MSG_ONE, NetworkMessages::WeapPickup, pPlayer.edict() );
			m1.WriteLong( g_ItemRegistry.GetIdForName(sWeaponName) );
		m1.End();*/

		//if( (m_pPlayer.pev.flags & FL_FAKECLIENT) != 0 and m_iAutoDeploy == 1 )
		m_pPlayer.SwitchWeapon(self);

		return true;
	}

	bool Deploy()
	{
		//if( (m_pPlayer.pev.flags & FL_FAKECLIENT) != 0 )
		//{
		spawn_driveent();
		self.m_flNextPrimaryAttack = self.m_flNextSecondaryAttack = g_Engine.time + 0.1;
		//}

		return self.DefaultDeploy( "", "", 0, "" );
	}

	bool CanHolster()
	{
		return false;
	}

	void Holster( int skipLocal = 0 )
	{
		if( m_pDriveEnt !is null )
			@m_pDriveEnt.pev.owner = null;

		ResetPlayer();

		BaseClass.Holster( skipLocal );
	}

	void PrimaryAttack()
	{
		if( m_iState == STATE_RUN )
		{
			self.m_flNextPrimaryAttack = g_Engine.time + 0.1;
		}

		if( self.m_iClip <= 0 )
		{
			self.m_flNextPrimaryAttack = self.m_flNextSecondaryAttack = self.m_flTimeWeaponIdle = g_Engine.time + 0.1;
			DoIdleAnimation();
			return;
		}

		if( m_pDriveEnt !is null )
		{
			if( m_iState != STATE_DEATH and self.m_iClip != 0 )
			{
				m_iState = STATE_ATTACK_EAGLE;

				m_pPlayer.SetMaxSpeedOverride( 0 );
				m_pDriveEnt.pev.sequence = m_pDriveEnt.LookupSequence("shootgun");
				m_pDriveEnt.pev.frame = 0;
				m_pDriveEnt.ResetSequenceInfo();

				--self.m_iClip;

				TraceResult tr;
				float x, y;
				g_Utility.GetCircularGaussianSpread( x, y );
				Vector vecAiming = m_pPlayer.GetAutoaimVector( AUTOAIM_5DEGREES );
				Vector vecDir = vecAiming 
							+ x * VECTOR_CONE_2DEGREES.x * g_Engine.v_right 
							+ y * VECTOR_CONE_2DEGREES.y * g_Engine.v_up;
				Vector vecAngle, vecOrigin, vecSrc, vecMuzzle;
				vecAngle = m_pPlayer.pev.v_angle;
				vecSrc = m_pPlayer.GetGunPosition();
				m_pDriveEnt.GetAttachment( 0, vecOrigin, void );

				Vector vecEnd	= vecSrc + vecDir * 8192;

				vecMuzzle = vecOrigin + g_Engine.v_up * 2;

				NetworkMessage m1( MSG_PVS, NetworkMessages::SVC_TEMPENTITY, vecMuzzle );
					m1.WriteByte( TE_SPRITE );
					m1.WriteCoord( vecMuzzle.x );
					m1.WriteCoord( vecMuzzle.y );
					m1.WriteCoord( vecMuzzle.z );
					m1.WriteShort( m_iGunMuzzleFlash );
					m1.WriteByte( 4 ); // size * 10
					m1.WriteByte( 128 ); // brightness
				m1.End();

				g_Utility.TraceLine( vecSrc, vecEnd, dont_ignore_monsters, m_pPlayer.edict(), tr );

				m_pDriveEnt.pev.effects |= EF_MUZZLEFLASH;
				m_pPlayer.m_iWeaponVolume = LOUD_GUN_VOLUME;

				m_pPlayer.FireBullets( 1, vecSrc, vecAiming, VECTOR_CONE_2DEGREES, 8192, BULLET_PLAYER_EAGLE, 1 );
				g_SoundSystem.EmitSound( m_pDriveEnt.edict(), CHAN_WEAPON, "weapons/desert_eagle_fire.wav", VOL_NORM, 0.6 );

				Vector vecShellVelocity, vecShellOrigin;
				CS16GetDefaultShellInfo( m_pPlayer, vecShellVelocity, vecShellOrigin, 12, 6, -4, false, false );
				vecShellVelocity.y *= 1;
				g_EntityFuncs.EjectBrass( vecShellOrigin, vecShellVelocity, m_pPlayer.pev.angles.y, m_iShell, TE_BOUNCE_SHELL );

				WW2DynamicLight( m_pPlayer.pev.origin, 8, 240, 180, 0, 8, 50 );
				switch( ( self.m_iClip ) % 1 )
				{
					case 0: WW2DynamicTracer( vecSrc, tr.vecEndPos ); break;
				}

				if( tr.flFraction < 1.0 )
				{
					if( tr.pHit !is null )
					{
						CBaseEntity@ pHit = g_EntityFuncs.Instance( tr.pHit );
				
						if( pHit is null || pHit.IsBSPModel() )
							g_WeaponFuncs.DecalGunshot( tr, BULLET_PLAYER_MP5 );
					}
				}

				self.m_flNextPrimaryAttack = g_Engine.time + 0.6;
				self.m_flNextSecondaryAttack = g_Engine.time + 0.6;
				self.m_flTimeWeaponIdle = g_Engine.time + 0.6;
			}
		}
		else
		{
			spawn_driveent();
			self.m_flNextPrimaryAttack = self.m_flNextSecondaryAttack = self.m_flTimeWeaponIdle = g_Engine.time + 0.5;

			return;
		}
	}

	void SecondaryAttack()
	{
		if( m_iState == STATE_RUN )
		{
			self.m_flNextPrimaryAttack = g_Engine.time + 0.1;
		}

		if( self.m_iClip <= 0 )
		{
			self.m_flNextPrimaryAttack = self.m_flNextSecondaryAttack = self.m_flTimeWeaponIdle = g_Engine.time + 0.1;
			DoIdleAnimation();
			return;
		}

		if( m_pDriveEnt !is null )
		{
			if( m_iState != STATE_DEATH and self.m_iClip != 0 )
			{
				m_iState = STATE_ATTACK_EAGLE;

				m_pPlayer.SetMaxSpeedOverride( 0 );
				m_pDriveEnt.pev.sequence = m_pDriveEnt.LookupSequence("shootgun");
				m_pDriveEnt.pev.frame = 0;
				m_pDriveEnt.ResetSequenceInfo();

				--self.m_iClip;
				//m_pPlayer.m_rgAmmo( self.m_iPrimaryAmmoType, m_pPlayer.m_rgAmmo( self.m_iPrimaryAmmoType ) + 1 );

				TraceResult tr;
				float x, y;
				g_Utility.GetCircularGaussianSpread( x, y );
				Vector vecAiming = m_pPlayer.GetAutoaimVector( AUTOAIM_5DEGREES );
				Vector vecDir = vecAiming 
							+ x * VECTOR_CONE_2DEGREES.x * g_Engine.v_right 
							+ y * VECTOR_CONE_2DEGREES.y * g_Engine.v_up;
				Vector vecAngle, vecOrigin, vecSrc, vecMuzzle;
				vecAngle = m_pPlayer.pev.v_angle;
				vecSrc = m_pPlayer.GetGunPosition();
				m_pDriveEnt.GetAttachment( 0, vecOrigin, void );

				Vector vecEnd	= vecSrc + vecDir * 8192;

				//vecMuzzle = vecOrigin + g_Engine.v_forward * 2 + g_Engine.v_up * 1;
				vecMuzzle = vecOrigin + g_Engine.v_up * 2;

				NetworkMessage m1( MSG_PVS, NetworkMessages::SVC_TEMPENTITY, vecMuzzle );
					m1.WriteByte( TE_SPRITE );
					m1.WriteCoord( vecMuzzle.x );
					m1.WriteCoord( vecMuzzle.y );
					m1.WriteCoord( vecMuzzle.z );
					m1.WriteShort( m_iGunMuzzleFlash );
					m1.WriteByte( 4 ); // size * 10
					m1.WriteByte( 128 ); // brightness
				m1.End();

				g_Utility.TraceLine( vecSrc, vecEnd, dont_ignore_monsters, m_pPlayer.edict(), tr );

				m_pDriveEnt.pev.effects |= EF_MUZZLEFLASH;
				m_pPlayer.m_iWeaponVolume = NORMAL_GUN_VOLUME;

				m_pPlayer.FireBullets( 1, vecSrc, vecAiming, VECTOR_CONE_2DEGREES, 8192, BULLET_PLAYER_EAGLE, 1 );
				g_SoundSystem.EmitSound( m_pDriveEnt.edict(), CHAN_WEAPON, "weapons/desert_eagle_fire.wav", VOL_NORM, 0.6 );

				Vector vecShellVelocity, vecShellOrigin;
				CS16GetDefaultShellInfo( m_pPlayer, vecShellVelocity, vecShellOrigin, 12, 6, -4, false, false );
				vecShellVelocity.y *= 1;
				g_EntityFuncs.EjectBrass( vecShellOrigin, vecShellVelocity, m_pPlayer.pev.angles.y, m_iShell, TE_BOUNCE_SHELL );

				WW2DynamicLight( m_pPlayer.pev.origin, 8, 240, 180, 0, 8, 50 );
				switch( ( self.m_iClip ) % 1 )
				{
					case 0: WW2DynamicTracer( vecSrc, tr.vecEndPos ); break;
				}

				if( tr.flFraction < 1.0 )
				{
					if( tr.pHit !is null )
					{
						CBaseEntity@ pHit = g_EntityFuncs.Instance( tr.pHit );
				
						if( pHit is null || pHit.IsBSPModel() )
							g_WeaponFuncs.DecalGunshot( tr, BULLET_PLAYER_MP5 );
					}
				}

				self.m_flNextPrimaryAttack = g_Engine.time + 0.4;
				self.m_flNextSecondaryAttack = g_Engine.time + 0.4;
				self.m_flTimeWeaponIdle = g_Engine.time + 0.4;
			}
		}

	}

	void TertiaryAttack()
	{
	}

	void Reload()
	{
	}

	void WeaponIdle()
	{
		if( self.m_flTimeWeaponIdle > g_Engine.time )
			return;

		DoIdleAnimation();

		self.m_flTimeWeaponIdle = g_Engine.time + 1.0;
	}

	void ItemPreFrame()
	{
		if( m_pDriveEnt !is null )
		{
			m_pPlayer.pev.friction = 2;
			
			CheckMaxHealth();

			//if( m_pPlayer.pev.button & (IN_BACK|IN_MOVELEFT|IN_MOVERIGHT) != 0 )
				//m_pPlayer.SetMaxSpeedOverride( 0 );
			//else if( m_pPlayer.pev.button & IN_FORWARD != 0 )
			if( m_pPlayer.pev.button & (IN_FORWARD|IN_BACK|IN_MOVELEFT|IN_MOVERIGHT) != 0 )
			{
				if( m_iState != STATE_RELOAD and m_iState != STATE_ATTACK_EAGLE )
				{
					m_pPlayer.SetMaxSpeedOverride( 270 );
					DoMovementAnimation();
					self.m_flTimeWeaponIdle = g_Engine.time + 0.1;
				}
			}
			else if( m_pPlayer.pev.button & IN_ATTACK != 0 or m_pPlayer.pev.button & IN_ATTACK2 != 0 )
			{
				m_pPlayer.SetMaxSpeedOverride( 0 );
			}
			else if( (m_pPlayer.pev.flags & FL_FAKECLIENT) != 0 and m_pPlayer.pev.velocity.Length2D() > 0 )
			{
				if( m_iState != STATE_ATTACK_EAGLE and m_iState != STATE_RELOAD )
				{
					m_pPlayer.SetMaxSpeedOverride( 270 );
					DoMovementAnimation();
					self.m_flTimeWeaponIdle = g_Engine.time + 0.1;
				}
			}

			if( m_pPlayer.pev.velocity.Length() <= 0 and m_iState != STATE_ATTACK_EAGLE and m_iState != STATE_RELOAD )
				DoIdleAnimation();

			if( m_iState == STATE_ATTACK_EAGLE )
			{
				Vector angDir = m_pPlayer.pev.v_angle;

				if( angDir.x > 360 )
					angDir.x = angDir.x - 360;

				m_pDriveEnt.SetBlending( 0, -angDir.x );
				m_pPlayer.SetViewMode( ViewMode_ThirdPerson );
			}

			if( m_iState == STATE_RELOAD )
			{
				m_pPlayer.SetMaxSpeedOverride( 0 );
				m_pPlayer.SetViewMode( ViewMode_ThirdPerson );
			}

			if( m_iState == STATE_IDLE )
			{
				RegenerateHealth();
				m_pPlayer.pev.view_ofs = Vector( 0, 0, 40 );
				m_pPlayer.SetViewMode( ViewMode_ThirdPerson );
			}

			if( m_iState == STATE_WALK )
			{
				m_pPlayer.pev.view_ofs = Vector( 0, 0, 40 );
				m_pPlayer.SetViewMode( ViewMode_ThirdPerson );
			}

			if( m_iState == STATE_RUN )
			{
				m_pPlayer.pev.view_ofs = Vector( 0, 0, 40 );
				m_pPlayer.SetViewMode( ViewMode_ThirdPerson );
			}
			
			DoReload();
		}
	}

	void RegenerateHealth ()
	{
		if( m_pDriveEnt !is null )
		{
			if ( m_fNextRegenTick < g_Engine.time and m_pPlayer.pev.health < 100 )
			{
				m_pPlayer.pev.health += 5;
				m_fNextRegenTick = g_Engine.time + 1.0;
			}
		}
	}

	void CheckMaxHealth ()
	{
		if( m_pDriveEnt !is null )
		{
			if( (m_pPlayer.pev.flags & FL_FAKECLIENT) == 0 and m_fNextHealthCheck < g_Engine.time and m_pPlayer.pev.health > 100 )
			{
				m_pPlayer.pev.health = 100;
				m_fNextHealthCheck = g_Engine.time + 0.1;
			}
		}
	}

	void DoReload()
	{
		//else if( (m_pPlayer.pev.button & IN_RELOAD) == 0 and (m_pPlayer.pev.oldbuttons & IN_RELOAD) != 0 and m_pDriveEnt !is null and m_pPlayer.IsAlive() and self.m_iClip < 7 )
		//{
		if( m_pDriveEnt !is null and m_pPlayer.IsAlive() and self.m_iClip <= 0 )
		{
			m_iState == STATE_RELOAD;
			m_pPlayer.SetMaxSpeedOverride( 0 );

			m_pDriveEnt.pev.sequence = m_pDriveEnt.LookupSequence("reload");
			m_pDriveEnt.pev.frame = 0;
			m_pDriveEnt.ResetSequenceInfo();

			//m_pPlayer.m_rgAmmo( self.m_iPrimaryAmmoType, m_pPlayer.m_rgAmmo( self.m_iPrimaryAmmoType ) - 7 );
			self.m_iClip = 7;

			SetThink( ThinkFunction(this.ReloadThink) );
			pev.nextthink = g_Engine.time + 2.0;

			m_fNextRegenTick = g_Engine.time + 5.0;
			self.m_flNextPrimaryAttack = g_Engine.time + 2.0;
			self.m_flNextSecondaryAttack = g_Engine.time + 2.0;
			self.m_flTimeWeaponIdle = g_Engine.time + 2.0;
		}
	}

	void ReloadThink()
	{
		if( m_pDriveEnt !is null )
		{
			DoIdleAnimation();
			m_pPlayer.SetMaxSpeedOverride( 270 );
		}

		SetThink( null );
	}

	void DoMovementAnimation()
	{
		float flMinWalkVelocity = -150.0;
		float flMaxWalkVelocity = 150.0;

		if( (m_pPlayer.pev.button & IN_USE) != 0 or IsBetween(m_pPlayer.pev.velocity.Length(), flMinWalkVelocity, flMaxWalkVelocity) )
		{
			if( m_pDriveEnt.pev.sequence != m_pDriveEnt.LookupSequence("walk") )
			{
				m_iState = STATE_WALK;
				m_pDriveEnt.pev.sequence = m_pDriveEnt.LookupSequence("walk");
				m_pDriveEnt.pev.frame = 0;
				m_pDriveEnt.ResetSequenceInfo();
			}
			else
			{
				if( GetFrame(32) == 1 )
				{
					switch( Math.RandomLong(1, 2) )
					{
						case 1: g_SoundSystem.EmitSoundDyn( m_pDriveEnt.edict(), CHAN_BODY, "common/npc_step2.wav", VOL_NORM, ATTN_NORM, 0, 100 ); break;
						case 2: g_SoundSystem.EmitSoundDyn( m_pDriveEnt.edict(), CHAN_BODY, "common/npc_step4.wav", VOL_NORM, ATTN_NORM, 0, 100 ); break;
					}
				}
				else if( GetFrame(32) == 19 )
				{
					switch( Math.RandomLong(1, 2) )
					{
						case 1: g_SoundSystem.EmitSoundDyn( m_pDriveEnt.edict(), CHAN_BODY, "common/npc_step2.wav", VOL_NORM, ATTN_NORM, 0, 100 ); break;
						case 2: g_SoundSystem.EmitSoundDyn( m_pDriveEnt.edict(), CHAN_BODY, "common/npc_step4.wav", VOL_NORM, ATTN_NORM, 0, 100 ); break;
					}
				}
			}

			m_fNextRegenTick = g_Engine.time + 3.0;
			
			if( (m_pPlayer.pev.flags & FL_FAKECLIENT) != 0 )
			{
				//nada
			}
			else
			{
				self.m_flNextPrimaryAttack = g_Engine.time + 0.1;
				self.m_flNextSecondaryAttack = g_Engine.time + 0.1;
			}
		}
		else if( (m_pPlayer.pev.flags & FL_FAKECLIENT) != 0 )
		{
			if( m_pDriveEnt.pev.sequence != m_pDriveEnt.LookupSequence("run") )
			{
				m_iState = STATE_RUN;
				m_pDriveEnt.pev.sequence = m_pDriveEnt.LookupSequence("run");
				m_pDriveEnt.pev.frame = 0;
				m_pDriveEnt.ResetSequenceInfo();
			}
			else
			{
				if( GetFrame(23) == 4 )
				{
					switch( Math.RandomLong(1, 2) )
					{
						case 1: g_SoundSystem.EmitSoundDyn( m_pDriveEnt.edict(), CHAN_BODY, "common/npc_step2.wav", VOL_NORM, ATTN_NORM, 0, PITCH_NORM ); break;
						case 2: g_SoundSystem.EmitSoundDyn( m_pDriveEnt.edict(), CHAN_BODY, "common/npc_step4.wav", VOL_NORM, ATTN_NORM, 0, PITCH_NORM ); break;
					}
				}
				else if( GetFrame(23) == 15 )
				{
					switch( Math.RandomLong(1, 2) )
					{
						case 1: g_SoundSystem.EmitSoundDyn( m_pDriveEnt.edict(), CHAN_BODY, "common/npc_step2.wav", VOL_NORM, ATTN_NORM, 0, PITCH_NORM ); break;
						case 2: g_SoundSystem.EmitSoundDyn( m_pDriveEnt.edict(), CHAN_BODY, "common/npc_step4.wav", VOL_NORM, ATTN_NORM, 0, PITCH_NORM ); break;
					}
				}
			}

			m_pPlayer.pev.flTimeStepSound = 9999; //prevents normal footsteps from playing, kinda? :hehe:
		}
		else
		{
			if( m_pDriveEnt.pev.sequence != m_pDriveEnt.LookupSequence("run") )
			{
				m_iState = STATE_RUN;
				m_pDriveEnt.pev.sequence = m_pDriveEnt.LookupSequence("run");
				m_pDriveEnt.pev.frame = 0;
				m_pDriveEnt.ResetSequenceInfo();
			}
			else
			{
				if( GetFrame(23) == 4 )
				{
					switch( Math.RandomLong(1, 2) )
					{
						case 1: g_SoundSystem.EmitSoundDyn( m_pDriveEnt.edict(), CHAN_BODY, "common/npc_step2.wav", VOL_NORM, ATTN_NORM, 0, PITCH_NORM ); break;
						case 2: g_SoundSystem.EmitSoundDyn( m_pDriveEnt.edict(), CHAN_BODY, "common/npc_step4.wav", VOL_NORM, ATTN_NORM, 0, PITCH_NORM ); break;
					}
				}
				else if( GetFrame(23) == 15 )
				{
					switch( Math.RandomLong(1, 2) )
					{
						case 1: g_SoundSystem.EmitSoundDyn( m_pDriveEnt.edict(), CHAN_BODY, "common/npc_step2.wav", VOL_NORM, ATTN_NORM, 0, PITCH_NORM ); break;
						case 2: g_SoundSystem.EmitSoundDyn( m_pDriveEnt.edict(), CHAN_BODY, "common/npc_step4.wav", VOL_NORM, ATTN_NORM, 0, PITCH_NORM ); break;
					}
				}
			}

			m_fNextRegenTick = g_Engine.time + 3.0;
			self.m_flNextPrimaryAttack = g_Engine.time + 0.1;
			self.m_flNextSecondaryAttack = g_Engine.time + 0.1;
			m_pPlayer.pev.flTimeStepSound = 9999; //prevents normal footsteps from playing, kinda? :hehe:
		}
	}

	void DoIdleAnimation()
	{
		if( m_pDriveEnt is null ) return;

		if( m_iState != STATE_IDLE )
		{
			m_pPlayer.SetMaxSpeedOverride( 270 );
			m_iState = STATE_IDLE;
			m_pDriveEnt.pev.sequence = m_pDriveEnt.LookupSequence("idle1");
			m_pDriveEnt.pev.frame = 0;
			m_pDriveEnt.ResetSequenceInfo();
		}
	}

	void spawn_driveent()
	{
		if( !m_pPlayer.pev.FlagBitSet(FL_ONGROUND) )
		{
			g_EngineFuncs.ClientPrintf( m_pPlayer, print_center, "Unable to deploy\nwhile in the air" );
			return;
		}

		@m_pDriveEnt = cast<CBaseAnimating@>( g_EntityFuncs.Create("cnpc_otis", m_pPlayer.pev.origin, m_pPlayer.pev.angles, true, m_pPlayer.edict()) );

		m_pDriveEnt.pev.set_controller( 0,  127 );

		g_EntityFuncs.DispatchSpawn( m_pDriveEnt.edict() );

		m_pPlayer.pev.effects |= EF_NODRAW;
		m_pPlayer.pev.flags &= ~FL_NOTARGET;
		m_pPlayer.pev.iuser3 = 1;
		m_pPlayer.pev.fuser4 = 1; //disable jump
		self.m_bExclusiveHold = true;

		m_pPlayer.SetViewMode( ViewMode_ThirdPerson );

		NetworkMessage m1( MSG_ONE, NetworkMessages::SVC_STUFFTEXT, m_pPlayer.edict() );
			m1.WriteString( "cam_idealdist 128\n" );
		m1.End();

		CustomKeyvalues@ pCustom = m_pPlayer.GetCustomKeyvalues();
		pCustom.SetKeyvalue( CNPC::sCNPCKV, CNPC::CNPC_OTIS );
		m_pPlayer.pev.view_ofs = Vector( 0, 0, 40 );

		/*if( (m_pPlayer.pev.flags & FL_FAKECLIENT) != 0 )
		{
			m_pPlayer.pev.netname = "Otis";
		}*/
	}

	void ResetPlayer()
	{
		m_pPlayer.pev.iuser3 = 0;
		m_pPlayer.pev.fuser4 = 0; //enable jump
		m_pPlayer.SetMaxSpeedOverride( -1 );

		CustomKeyvalues@ pCustom = m_pPlayer.GetCustomKeyvalues();
		pCustom.SetKeyvalue( CNPC::sCNPCKV, 0 );
	}
}

class cnpc_otis : ScriptBaseAnimating//ScriptBaseMonsterEntity
{
	protected CBasePlayer@ m_pOwner
	{
		get { return cast<CBasePlayer@>( g_EntityFuncs.Instance(pev.owner) ); }
	}

	private float m_flNextOriginUpdate; //hopefully fixes hacky movement on other players

	void Spawn()
	{
		g_EntityFuncs.SetModel( self, "models/cnpc/otis.mdl" );
		g_EntityFuncs.SetSize( self.pev, Vector(-16, -16, 0), Vector(16, 16, 72) );
		g_EntityFuncs.SetOrigin( self, pev.origin );
		g_EngineFuncs.DropToFloor( self.edict() );

		pev.solid = SOLID_NOT;
		pev.movetype = MOVETYPE_NOCLIP;

		pev.sequence = self.LookupSequence("idle1");
		pev.frame = 0;
		self.ResetSequenceInfo();
		self.SetBodygroup( 1, 1 );

		SetThink( ThinkFunction(this.DriveThink) );
		pev.nextthink = g_Engine.time;
	}

	void DriveThink()
	{
		if( m_pOwner is null or !m_pOwner.IsConnected() or m_pOwner.pev.deadflag != DEAD_NO )
		{
			pev.velocity = g_vecZero;
			SetThink( ThinkFunction(this.DieThink) );
			pev.nextthink = g_Engine.time;

			return;
		}

		if( m_flNextOriginUpdate < g_Engine.time )
		{
			Vector vecOrigin = m_pOwner.pev.origin;
			vecOrigin.z -= 32.0;
			g_EntityFuncs.SetOrigin( self, vecOrigin );
			m_flNextOriginUpdate = g_Engine.time + 0.1;
		}

		pev.velocity = m_pOwner.pev.velocity;

		pev.angles.x = 0;

		if( pev.velocity.Length2D() > 0.0 and (pev.sequence == self.LookupSequence("walk") or pev.sequence == self.LookupSequence("run")) )
			pev.angles.y = Math.VecToAngles( pev.velocity ).y;
		else
			pev.angles.y = m_pOwner.pev.angles.y;

		pev.angles.z = 0;

		self.StudioFrameAdvance();

		pev.nextthink = g_Engine.time + 0.01;
	}

	void DieThink()
	{
		array<string> arrsDeathAnims = 
		{
			"diecrump",
			"diebackward",
			"dieviolent",
			"diegutshot",
			"diesimple",
			"dieforward"
		};

		pev.sequence = self.LookupSequence( arrsDeathAnims[Math.RandomLong(0, arrsDeathAnims.length()-1)] );
		pev.frame = 0;
		self.ResetSequenceInfo();
		g_EngineFuncs.DropToFloor( self.edict() );

		g_SoundSystem.EmitSound( self.edict(), CHAN_VOICE, pDieSounds[Math.RandomLong(0, pDieSounds.length()-1)], VOL_NORM, ATTN_NORM );

		SetThink( ThinkFunction(this.SUB_StartFadeOut) );
		pev.nextthink = g_Engine.time + 15.0;
	}

	void SUB_StartFadeOut()
	{
		if( pev.rendermode == kRenderNormal )
		{
			pev.renderamt = 255;
			pev.rendermode = kRenderTransTexture;
		}

		pev.solid = SOLID_NOT;
		pev.avelocity = g_vecZero;

		pev.nextthink = g_Engine.time + 0.1;
		SetThink( ThinkFunction(this.SUB_FadeOut) );
	}

	void SUB_FadeOut()
	{
		if( pev.renderamt > 7 )
		{
			pev.renderamt -= 7;
			pev.nextthink = g_Engine.time + 0.1;
		}
		else 
		{
			pev.renderamt = 0;
			pev.nextthink = g_Engine.time + 0.2;
			SetThink( ThinkFunction(this.SUB_Remove) );
		}
	}

	void SUB_Remove()
	{
		self.UpdateOnRemove();

		if( pev.health > 0 )
			pev.health = 0;

		NetworkMessage killbeam( MSG_BROADCAST, NetworkMessages::SVC_TEMPENTITY, null );
			killbeam.WriteByte(TE_KILLBEAM);
			killbeam.WriteShort(self.entindex());
		killbeam.End();

		g_EntityFuncs.Remove(self);
	}
}

void Register()
{
	g_CustomEntityFuncs.RegisterCustomEntity( "cnpc_otis::cnpc_otis", "cnpc_otis" );
	g_Game.PrecacheOther( "cnpc_otis" );

	g_CustomEntityFuncs.RegisterCustomEntity( "cnpc_otis::weapon_otis", sWeaponName );
	g_ItemRegistry.RegisterWeapon( sWeaponName, "controlnpc", "otis" );
	g_Game.PrecacheOther( sWeaponName );
}

} //namespace cnpc_otis END

/* TODO
Use turning animations 
*/
