if ( SERVER ) then

	AddCSLuaFile( "shared.lua" )
	
	SWEP.HoldType			= "shotgun"
	
end

if ( CLIENT ) then

	SWEP.PrintName			= "Ultras Rail Gun"			
	SWEP.Author				= "Feihc and Ultradr3mer"

	SWEP.Slot				= 3
	SWEP.SlotPos			= 1
	SWEP.ViewModelFOV		= 65
	SWEP.IconLetter			= "x"
	SWEP.DrawCrosshair		= false
	
end
-----------------------Main functions----------------------------
 
-- function SWEP:Reload() --To do when reloading
-- end 

function SWEP:DrawHUD()
	local sW = surface.ScreenWidth() / 2
	local sH = surface.ScreenHeight() / 2
	local inner_crosshair_dist = 1
	
	local rel_time = (LocalPlayer().NextPrimaryFire - CurTime()) / 1.5;
	
	if rel_time <= 0 then
	
	surface.SetDrawColor(Color(255,255,255,255))
	surface.DrawRect(sW , sH + 1 + inner_crosshair_dist, 1, 5)
	surface.DrawRect(sW + 1 + inner_crosshair_dist, sH, 5, 1)
	surface.SetDrawColor(Color(0,0,0,255))
	surface.DrawRect(sW , sH - 5 - inner_crosshair_dist, 1, 5)
	surface.DrawRect(sW - 5 - inner_crosshair_dist, sH, 5, 1)
	
	else 
	
	local new_dist = inner_crosshair_dist * rel_time * rel_time * 50 + 1
	surface.SetDrawColor(Color(255,255,255,255))
	surface.DrawRect(sW , sH + 1 + new_dist, 1, 5)
	surface.DrawRect(sW + 1 + new_dist, sH, 5, 1)
	surface.SetDrawColor(Color(0,0,0,255))
	surface.DrawRect(sW , sH - 5 - new_dist, 1, 5)
	surface.DrawRect(sW - 5 - new_dist, sH, 5, 1)
	
	end
	
end

function Update_NextPrimaryFire( um )
	LocalPlayer().NextPrimaryFire = um:ReadFloat()
end
usermessage.Hook("SetNextPrimaryFire", Update_NextPrimaryFire) 
 
function SWEP:Think() -- Called every frame
	if SERVER then
		if self.Owner:KeyDown(IN_ATTACK2) then
			self.Owner:SetFOV(30, .1)
		end
		if self.Owner:KeyReleased(IN_ATTACK2) then
			self.Owner:SetFOV(75, .2)
		end
	end
end

function SWEP:Equip()
	if SERVER then
		if self.Owner:IsPlayer() then 
			self.Owner:GiveAmmo(100, "GaussEnergy")
		end
		umsg.Start("SetNextPrimaryFire", self.Owner)
			umsg.Float(CurTime() + self.Primary.Delay)
		umsg.End() 
	end
end


function SWEP:Initialize()
	if SERVER then
		self:SetWeaponHoldType(self.HoldType)
	end
end

function SWEP:PrimaryAttack()
	if SERVER then
	if self.Owner:GetAmmoCount("GaussEnergy") == 0 then return end
	self:SetNextPrimaryFire(CurTime() + self.Primary.Delay)
	self:SendWeaponAnim(ACT_VM_PRIMARYATTACK);
	self:TakePrimaryAmmo( 1 )
	self.Owner:SetAnimation( PLAYER_ATTACK1 )
	self.Owner:SetVelocity(self.Owner:GetForward() * -500)
	
	self.Owner:ViewPunch(Angle( -5, 0, 0 ))
	
	umsg.Start("SetNextPrimaryFire", self.Owner)
		umsg.Float(CurTime() + self.Primary.Delay)
	umsg.End() 
	
	local trace = {}
		trace.start = self.Owner:GetShootPos()
		trace.endpos = self.Owner:GetShootPos() + self.Owner:GetAimVector() * 10^14
		trace.filter = self.Owner
	local tr = util.TraceLine(trace)
	
	local vAng = (tr.HitPos-self.Owner:GetShootPos()):GetNormal():Angle()
	
	local dmginfo = DamageInfo();
	dmginfo:SetDamage( 100 );
	dmginfo:SetAttacker( self:GetOwner() );
	dmginfo:SetInflictor( self );
	
	if( dmginfo.SetDamageType ) then
		dmginfo:SetDamagePosition( tr.HitPos );
		dmginfo:SetDamageType( DMG_ENERGYBEAM  );
	end
	
	tr.Entity:DispatchTraceAttack( dmginfo, tr.HitPos, tr.HitPos - vAng:Forward() * 20 );
	
	tr.Entity:SetKeyValue("targetname", "disTarg")
	local dis = ents.Create("env_entity_dissolver")
	dis:SetPos(tr.Entity)
	dis:SetKeyValue("magnitude", "5")
	dis:SetKeyValue("dissolvetype", "0")
	dis:SetKeyValue("target", "disTarg")
	dis:Spawn()
	dis:Fire("Dissolve", "disTarg", 0)
	dis:Fire("kill", "", 0)

	local effect = EffectData()
		effect:SetStart(self.Owner:GetShootPos()+self.Owner:EyeAngles():Forward()*22+self.Owner:EyeAngles():Up()*-6+self.Owner:EyeAngles():Right()*5)
		effect:SetOrigin(tr.HitPos)
		effect:SetAngle(self.Owner:EyeAngles())
	util.Effect("Rail_Spiral", effect)
	util.Effect("AR2Impact", effect)
	util.Effect("CommandPointer", effect)
	local effect2 = EffectData()
		effect2:SetStart(tr.HitPos)
		effect2:SetNormal(tr.HitNormal)
	util.Effect("Rail_Puff", effect2)
	local hit1, hit2 = tr.HitPos + tr.HitNormal, tr.HitPos - tr.HitNormal
	util.Decal("FadingScorch", hit1, hit2)
	self:EmitSound("railgun.wav")
	end
end

function SWEP:SecondaryAttack()

end

-------------------------------------------------------------------

------------General Swep Info---------------
SWEP.Author   = "Feihc"
SWEP.Contact        = ""
SWEP.Purpose        = ""
SWEP.Instructions   = ""
SWEP.Spawnable      = true
SWEP.AdminSpawnable  = true
-----------------------------------------------

------------Models---------------------------
SWEP.ViewModel      = "models/weapons/v_vikgun.mdl"
SWEP.WorldModel   = "models/weapons/w_irifle.mdl"
-----------------------------------------------

-------------Primary Fire Attributes----------------------------------------
SWEP.Primary.Delay			= 1.5	--In seconds
SWEP.Primary.Recoil			= 0		--Gun Kick
SWEP.Primary.Damage			= 15	--Damage per Bullet
SWEP.Primary.NumShots		= 1		--Number of shots per one fire
SWEP.Primary.Cone			= 0 	--Bullet Spread
SWEP.Primary.ClipSize		= -1	--Use "-1 if there are no clips"
SWEP.Primary.DefaultClip	= 10	--Number of shots in next clip
SWEP.Primary.Automatic   	= true	--Pistol fire (false) or SMG fire (true)
SWEP.Primary.Ammo         	= "GaussEnergy"	--Ammo Type
-------------End Primary Fire Attributes------------------------------------
 
-------------Secondary Fire Attributes-------------------------------------
SWEP.Secondary.Delay		= 0.9
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   	= true
SWEP.Secondary.Ammo         = "none"
-------------End Secondary Fire Attributes--------------------------------
