AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include('shared.lua')

local sound_dropship=Sound("npc/combine_gunship/dropship_engine_near_loop1.wav") -- DEFAULT GUNSHIP SOUND IN MOVEMENT

function ENT:SpawnFunction( ply, tr )

	local SpawnPos = tr.HitPos + tr.HitNormal * 100
	local ent = ents.Create( "sent_dropship" ) -- NAME OF THE FOLDER
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	return ent
	
end

function ENT:Initialize()
	
	if (!self.Sound) then
 		self.Sound = CreateSound( self.Entity, sound_dropship )
 	end
	
	self.Hover = true;
	
	self.Entity:SetUseType( SIMPLE_USE )
	self.Pilot = nil -- Pilot player
	self.Boarded = { } -- Boarded players
	
	self.Entity:SetModel( "models/imperial/vehicles/air/transport/imp_dropship.mdl" ) --- THIS IS THE MODEL 
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	
	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
		phys:SetMass(10000) -- wow ace this is heavy like a nigger
	end
	
	self.Entity:StartMotionController()
	self.Accel=0

end

function ENT:OnTakeDamage( dmginfo )
	 if self.Pilot then
		self.Pilot:TakeDamage(dmginfo:GetDamage()/3, dmginfo:GetAttacker())
	end
end

function ENT:OnRemove()

	if #self.Boarded > 0 then
		for k, v in pairs(self.Boarded) do
			self:RemoveBoarded( v );
		end
	end
	
	self.Boarded = { };

	if self.Pilot then
		self:UndoPilot();
	end
	
end

function ENT:Think()
	
	if( not ValidEntity( self.Pilot ) ) then
		
		self.Pilot = nil;
		
	end
	
	if self.Pilot then
		
		self.Pilot:SetPos( self.Entity:GetPos() );
		
		if( #self.Boarded > 0 ) then
		
			for k, v in pairs( self.Boarded ) do
				
				v:SetPos( self.Entity:GetPos() );
				
				if( not v:Alive() ) then
					
					self:RemoveBoarded( v );
					self:RemoveBoardedFromTable( v );
					
				end
				
			end
			
		end
		
		if( self.Pilot:KeyDown( IN_USE ) ) then
			
			self:UndoPilot();
	    
		end
		
		-- Death fix below
		
		if( #self.Boarded > 0 ) then
		
			for k, v in pairs( self.Boarded ) do
				
				if( not v:Alive() ) then
					
					self:RemoveBoarded( v );
					self:RemoveBoardedFromTable( v );
					
				end
				
			end
			
		end
		
		if( self.Pilot ) then
		
			if( not self.Pilot:Alive() ) then
				
				self:UndoPilot();
				
			end
			
		end
		
		self.Entity:NextThink(CurTime())
		
	else
		
		for k, v in pairs( self.Boarded ) do
			
			self:RemoveBoarded( v );
			
		end
		
		self.Boarded = { };
		
		self.Entity:NextThink(CurTime()+1)
		
	end	
	
	return true
	
end

function ENT:UndoPilot()
	
	self.Pilot:UnSpectate()
	self.Pilot:Spawn();
	self.Pilot:DrawViewModel(true)
	self.Pilot:DrawWorldModel(true)
	self.Entity:SetOwner(nil)
	self.Pilot:SetPos(self.Entity:GetPos()+self.Entity:GetForward()*-300)
	self.Pilot:SetNetworkedBool("isDriveJumper",false)

	self.Accel = 0
	
	self.Entity:SetLocalVelocity(Vector(0,0,0))
	self.Pilot = nil
	
	self.Sound:Stop()
	
end

function ENT:SetPilot( ply )
	
	-- Default dropship sound start when go when into the ship
	self.Sound:Play()
	
	ply:Spectate( OBS_MODE_CHASE )
	ply:SpectateEntity(self.Entity)
	ply:SetNetworkedBool("isDriveJumper",true)
	ply:SetNetworkedEntity("Jumper",self.Entity)
	self.Entity:GetPhysicsObject():Wake()
	self.Entity:GetPhysicsObject():EnableMotion(true)
	self.Entity:SetOwner(ply)
	ply:DrawViewModel(false)
	ply:DrawWorldModel(false)
	self.Pilot = ply;
	
end

function ENT:AddBoarded( ply )
	
	ply:Spectate( OBS_MODE_CHASE );
	ply:SpectateEntity(self.Entity);
	ply:SetNetworkedBool("isDriveJumper",true)
	ply:SetNetworkedEntity("Jumper",self.Entity)
	ply:DrawViewModel(false)
	ply:DrawWorldModel(false)
	table.insert( self.Boarded, ply );
	
end

function ENT:RemoveBoarded( ply )
	
	ply:UnSpectate()
	ply:Spawn();
	ply:DrawViewModel(true)
	ply:DrawWorldModel(true)
	ply:SetPos(self.Entity:GetPos()+self.Entity:GetForward()*-300)
	ply:SetNetworkedBool("isDriveJumper",false)
	
end

function ENT:RemoveBoardedFromTable( ply ) -- For removing duplicates D:
	
	local tab = self.Boarded;
	
	for k, v in pairs( tab ) do
		
		if( v == ply ) then
		
			table.remove( self.Boarded, k );
			
		end
		
	end
	
end

function ENT:Use(ply,caller)

	if( ply:GetActiveWeapon() ) then
		ply:GetActiveWeapon():SetNoDraw( true );
	end

	if self.Pilot then -- There's a pilot
		
		for k, v in pairs( self.Boarded ) do
			
			if( v == ply ) then
				
				self:RemoveBoarded( ply );
				self:RemoveBoardedFromTable( ply );
				return;
				
			end
			
		end
		
		self:RemoveBoardedFromTable( ply );
		
		ply:SelectWeapon( "ts_hands" );
		self:AddBoarded( ply );
		
	else -- No pilot. You are the lucky one
		
		ply:SelectWeapon( "ts_hands" );
		self:SetPilot( ply );
		
	end
	
end

function ENT:PhysicsSimulate( phys, deltatime )

	if self.Pilot then
	
		local num=0
		if self.Pilot:KeyDown(IN_FORWARD) then
            num=500
        elseif self.Pilot:KeyDown(IN_BACK) then
            num=-500
        end
		
		phys:Wake()
		self.Accel=math.Approach(self.Accel,num,10)
		
		if not self.Hover then
		
            if self.Accel>-200 and self.Accel < 200 then return end --with out this you float
			
        end
		
		local move={}
		move.secondstoarrive	= 1
		move.pos = self.Entity:GetPos()+self.Entity:GetForward()*self.Accel
		if self.Pilot:KeyDown( IN_DUCK ) then
            move.pos = move.pos+self.Entity:GetUp()*-200
        elseif self.Pilot:KeyDown( IN_JUMP ) then
            move.pos = move.pos+self.Entity:GetUp()*300
		end

        if self.Pilot:KeyDown( IN_MOVERIGHT ) then
			move.pos = move.pos+self.Entity:GetRight()*200
		elseif self.Pilot:KeyDown( IN_MOVELEFT ) then
			move.pos = move.pos+self.Entity:GetRight()*-200
		end
			
			
		move.maxangular		= 5000
		move.maxangulardamp	= 10000
		move.maxspeed			= 1000000
		move.maxspeeddamp		= 10000
		move.dampfactor		= 0.8
		move.teleportdistance	= 5000
		local ang = self.Pilot:GetAimVector():Angle();
		if( self.Pilot:KeyDown( IN_SPEED ) ) then
			ang = self:GetAngles();
		end
		move.angle			= ang
		move.deltatime		= deltatime
		phys:ComputeShadowControl(move)
		
	end
	
end
