if CLIENT then
    SWEP.PrintName = "RPG-7"
    SWEP.Slot = 3
    SWEP.SlotPos = 3
    SWEP.DrawAmmo = false
    SWEP.DrawCrosshair = false
end

if SERVER then
	AddCSLuaFile("shared.lua")
end

SWEP.Author            = "Spy"
SWEP.Instructions    = "Left click to fire out a missile\nSecondary attack to take aim\nUse key + primary attack key to switch missile modes"
SWEP.Contact        = ""
SWEP.Purpose        = ""

SWEP.ViewModelFOV    = 65
SWEP.ViewModelFlip    = false

SWEP.Spawnable            = true
SWEP.AdminSpawnable        = true


SWEP.ViewModel      = "models/weapons/vrpg7.mdl"
SWEP.WorldModel   = "models/weapons/w_rocket_launcher.mdl"
  
-- Primary Fire Attributes --
SWEP.Primary.Recoil            = 0
SWEP.Primary.Damage            = 0
SWEP.Primary.NumShots        = 1
SWEP.Primary.Cone            = 0
SWEP.Primary.ClipSize        = 1
SWEP.Primary.DefaultClip    = 3
SWEP.Primary.Automatic       = false    
SWEP.Primary.Ammo             = "RPG_Round"
 
-- Secondary Fire Attributes --
SWEP.Secondary.Recoil        = 0
SWEP.Secondary.Damage        = 0
SWEP.Secondary.NumShots        = 1
SWEP.Secondary.Cone            = 0
SWEP.Secondary.ClipSize        = -1
SWEP.Secondary.DefaultClip    = -1
SWEP.Secondary.Automatic       = false
SWEP.Secondary.Ammo         = "none"

SWEP.IronSightsPos = Vector(-3.718, -9.181, 0.239)
SWEP.IronSightsAng = Vector(0, -0.008, 0)

function SWEP:Initialize()
    self:SetWeaponHoldType("rpg")
	self.Weapon:SetDTInt(0, 0)
end

function SWEP:Precache()
end

function SWEP:Deploy()
	if not self.Owner.CanReload then
		self.Owner.CanReload = true
	end

    self.Weapon:SendWeaponAnim( ACT_VM_DRAW )
    return true
end

function SWEP:Holster()
	if SERVER then
		if self.Weapon.Missile and self.Weapon.Missile.MissileType == 2 then
			self.Weapon.Missile:Explode()
			self.Weapon.Missile = nil
		end
	end
	
	if CLIENT then
		timer.Destroy("AnimTimer")
	end
	
    return true
end

function SWEP:Reload()
	if SERVER then
		if not self.Owner.CanReload then
			return
		end

		self.Weapon:DefaultReload(ACT_VM_RELOAD)
	end
	
    return false
end

function SWEP:PrimaryAttack()
	if self.Weapon.Missile and self.Weapon.Missile.MissileType == 2 then
		local missile = self.Weapon.Missile
		
		if ValidEntity(missile) then
			missile:SetDTInt(0, 0)
			
			local vel = missile:GetVelocity()
			
			missile:PhysicsInit(SOLID_VPHYSICS)
			missile:SetMoveType(MOVETYPE_VPHYSICS)
			missile:SetSolid(SOLID_VPHYSICS)
			missile:SetCollisionGroup(COLLISION_GROUP_NPC)
			
			local phys = missile:GetPhysicsObject()
			
			if phys and phys:IsValid() then
				phys:Wake()
				phys:SetMass(5)
				phys:SetVelocity(vel)
			end
			
			missile.RocketSound:FadeOut(0.3)
			self.Owner.CanReload = true
			self.Weapon.Missile = nil
			
			return
		end
	end

	if self.Owner:KeyDown(IN_USE) then
	
		if SERVER then
			if self.Weapon:GetDTInt(0) == 0 then
				self.Weapon:SetDTInt(0, 1)
			elseif self.Weapon:GetDTInt(0) == 1 then
				self.Weapon:SetDTInt(0, 2)
			else
				self.Weapon:SetDTInt(0, 0)
			end
		end
		
		return
	end
	

	if self.Weapon:Clip1() == 0 then
		return
	end

	local aimVec = self.Owner:GetAimVector()
	
	if SERVER then
		local missile = ents.Create("ent_missile")
		local offset = Vector(aimVec.x * (5 * math.random(-1, 1)), aimVec.y * (5 * math.random(-1, 1)), 0)
		missile:SetPos(self.Owner:GetShootPos() - Vector(0, 0, 5) + aimVec * 40 + self.Owner:GetRight() * 4.5)
		missile:SetAngles(self.Owner:EyeAngles())
		missile:Spawn()
		missile:SetOwner(self.Owner)
		missile:SetVelocity(aimVec * 2000 + offset)
		missile.MissileType = self.Weapon:GetDTInt(0)
		self.Weapon.Missile = missile
		self.Weapon:SetDTInt(1, 0)
		self.Owner:SetFOV(0, 0.3)
		
		if self.Weapon:GetDTInt(0) == 2 then
			self.Owner.CanReload = false
		end
	end
	
	self.Weapon:EmitSound("weapons/rpg/rocketfire1.wav", 95, 100)
	self.Weapon:SendWeaponAnim(ACT_VM_PRIMARYATTACK)
	self.Owner:GetViewModel():SetPlaybackRate(3)
	self.Owner:ViewPunch(Vector(-5, 0, 0))
	
	ParticleEffect("rpg7_sparks", self.Owner:GetShootPos() - Vector(0, 0, 5) + aimVec * 40 + self.Owner:GetRight() * 4.5, self.Owner:EyeAngles(), nil)
	ParticleEffect("rpg7_smoke_full", self.Owner:GetShootPos() - Vector(0, 0, 5) + aimVec * 40 + self.Owner:GetRight() * 4.5, self.Owner:EyeAngles(), nil)
	
	self.Weapon:SetClip1(0)
end

function SWEP:SecondaryAttack()
	if SERVER then
		if self.Weapon:GetDTInt(1) != 1 then
			self.Weapon:SetDTInt(1, 1)
			self.Owner:SetFOV(75, 0.3)
		else
			self.Weapon:SetDTInt(1, 0)
			self.Owner:SetFOV(0, 0.3)
		end
	end
end

if CLIENT then
	local Laser = Material("effects/redlaser1")
	local LaserDot = Material("effects/brightglow_y")
	
	function SWEP:ViewModelDrawn()
		if self.Weapon:GetDTInt(0) == 2 then
			local vm = self.Owner:GetViewModel()
			local att = vm:GetAttachment("1")
			local eyeAng = self.Owner:EyeAngles()
			local eyeAngRight = eyeAng:Right()
			local eyeAngUp = eyeAng:Up()
			local aimVec = self.Owner:GetAimVector()
			
			att.Ang:RotateAroundAxis(att.Ang:Up(), -89.998) -- 100% on-spot with the player's eye angles if the weapon isn't shaking. Aw ye.
			
			local finalAng = Angle(att.Ang.p, att.Ang.y, 0)
			
			local addOffset = finalAng:Right() * 0.5 + finalAng:Up() * 3
			
			local td = {}
			td.start = att.Pos - finalAng:Forward() * 10 + addOffset 
			td.endpos = td.start + finalAng:Forward() * 4096
			td.filter = self.Owner
			
			local trace = util.TraceLine(td)
			
			cam.Start3D(EyePos(), EyeAngles())
				render.SetMaterial(Laser)
				render.DrawBeam(trace.StartPos, trace.HitPos, 0.5, 0.5, Color(255, 255, 255, 1))
				
				render.SetMaterial(LaserDot)
				render.DrawSprite(trace.HitPos - finalAng:Forward() * 1, 2, 2, Color(255, 0, 0, 255))
			cam.End3D()
		end
	end
	
	local BlendPos = Vector(0, 0, 0)
	local BlendAng = Vector(0, 0, 0)
	
	function SWEP:GetViewModelPosition(pos, ang)
		if not self.Weapon.IronSightsPos or not self.Weapon.IronSightsAng then
			return pos, ang
		end
		
		local FT = FrameTime()
		
		if self.Weapon:GetDTInt(1) == 1 then
			BlendPos = LerpVector(FT * 20, BlendPos, self.Weapon.IronSightsPos)
			BlendAng = LerpVector(FT * 20, BlendAng, self.Weapon.IronSightsAng)
			self.Weapon.BobScale = 0.5
			self.Weapon.SwayScale = 0.1
		else
			BlendPos = LerpVector(FT * 20, BlendPos, Vector(0, 0, 0))
			BlendAng = LerpVector(FT * 20, BlendAng, Vector(0, 0, 0))
			self.Weapon.BobScale = 1
			self.Weapon.SwayScale = 1
		end
		
		ang:RotateAroundAxis( ang:Right(), 		BlendAng.x)
		ang:RotateAroundAxis( ang:Up(), 		BlendAng.y)
		ang:RotateAroundAxis( ang:Forward(), 	BlendAng.z)
		
		local Right 	= ang:Right()
		local Up 		= ang:Up()
		local Forward 	= ang:Forward()	
		
		pos = pos + BlendPos.x * Right 
		pos = pos + BlendPos.y * Forward
		pos = pos + BlendPos.z * Up
		
		return pos, ang
	end
	
	function SWEP:DrawHUD()
		local Width = ScrW()
		local Height = ScrH()
		local Text
		local DTInt0 = self.Weapon:GetDTInt(0)
		
		if DTInt0 == 0 then
			Text = "Manual"
		elseif DTInt0 == 1 then
			Text = "Heat-seeking"
		elseif DTInt0 == 2 then
			Text = "Laser-guided"
		end
		
		draw.RoundedBox(6, Width * 0.5 - 60, Height * 0.9, 120, 20, Color(0, 0, 0, 220))
		draw.SimpleText(Text, "DefaultSmallDropShadow", Width * 0.5, Height * 0.9 + 10, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
	end
end