AddCSLuaFile()

SWEP.PrintName = "Cade Hammer"

SWEP.Slot = 0
SWEP.SlotPos = 4
SWEP.Instructions = "LMB - Build/Repair, RMB - Unbuild, R - Drag Prop"

SWEP.Spawnable = true

SWEP.ViewModel = Model( "models/weapons/c_arms.mdl" )
SWEP.WorldModel = "models/weapons/w_buzzhammer.mdl"
SWEP.ViewModelFOV = 54
SWEP.UseHands = true

SWEP.Primary.ClipSize = -1
SWEP.Primary.DefaultClip = -1
SWEP.Primary.Automatic = true
SWEP.Primary.Ammo = "none"

SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = "none"

SWEP.DrawAmmo = false

SWEP.Offset = {
	Pos = {Up = -5, Right = 1, Forward = 3, },
	Ang = {Up = 0, Right = 0, Forward = 90,}
}

function SWEP:DrawWorldModel()
	local hand, offset, rotate
	local pl = self:GetOwner()
	if IsValid( pl ) then
		local boneIndex = pl:LookupBone( "ValveBiped.Bip01_R_Hand" )
		if boneIndex then
			local pos, ang = pl:GetBonePosition( boneIndex )
			pos = pos + ang:Forward() * 		 	self.Offset.Pos.Forward + ang:Right() * self.Offset.Pos.Right + ang:Up() * self.Offset.Pos.Up
			ang:RotateAroundAxis( ang:Up(),    	self.Offset.Ang.Up)
			ang:RotateAroundAxis( ang:Right(), 	self.Offset.Ang.Right )
			ang:RotateAroundAxis( ang:Forward(),  self.Offset.Ang.Forward )
			self:SetRenderOrigin( pos )
			self:SetRenderAngles( ang )
			self:DrawModel()
		end
	else
		self:SetRenderOrigin( nil )
		self:SetRenderAngles( nil )
		self:DrawModel()
	end
end

function SWEP:Initialize()
	self:SetHoldType("melee")
end

function SWEP:PrimaryAttack()
	if SERVER then
		local ply = self.Owner
		local tr = ply:GetEyeTrace()
		if tr.HitPos:DistToSqr(ply:EyePos()) < 128^2 and IsValid(tr.Entity) and tr.Entity:GetClass() == "prop_physics" and ply:GetNWFloat('buildercost') >= 1 then
			local e = tr.Entity
			local tr2 = util.TraceLine({
				start = tr.HitPos,
				endpos = tr.HitPos+ply:GetAimVector()*16,
				filter = function(ent) if !ent:IsWorld() then return end end
			})
			if e:GetPhysicsObject():GetMass() < 1000 and tr.Hit and tr2.Hit then
				local ent = ents.Create("sur_barricade")
				ent.MapCade = true
				ent.id = 6
				ent:SetModel(e:GetModel())
				ent:SetPos(e:GetPos())
				ent:SetAngles(e:GetAngles())
				ent:SetSkin(e:GetSkin())
				ent:SetNWEntity('builder', ply)
				local mult = 1
				if ply:GetPlayerClass() == "engineer" and ply:GetLevelState() >= 4 then
					mult = 2
				elseif ply:GetPlayerClass() == "engineer" and ply:GetLevelState() >= 3 then
					mult = 1.5
				elseif ply:GetPlayerClass() == "engineer" and ply:GetLevelState() >= 1 then
					mult = 1.1
				end
				ent.health = e.Healthbefore or e:GetPhysicsObject():GetMass()*5*mult
				ent.healthmax = e:GetPhysicsObject():GetMass()*5*mult
				ent:Spawn()

				local bolt = ents.Create("prop_dynamic")
				bolt:SetModel("models/crossbow_bolt.mdl")
				bolt:SetAngles(ply:EyeAngles())
				bolt:SetPos(tr.HitPos-ply:GetAimVector()*4)
				bolt:Spawn()

				ent:DeleteOnRemove(bolt)

				e:EmitSound('weapons/crowbar/crowbar_impact1.wav')
				e:Remove()

				ply:SetNWFloat('buildercost', ply:GetNWFloat('buildercost')-1)
				ply:ViewPunch(Angle(25,25,25))
				BroadcastLua([[Entity(]]..ply:EntIndex()..[[):SetAnimation( PLAYER_ATTACK1 )]])
				self:SetNextPrimaryFire( CurTime() + 1 )
			end
		end
	end
end

function SWEP:SecondaryAttack()
	if SERVER then
		local ply = self.Owner
		local tr = ply:GetEyeTrace()
		local tr2 = util.TraceLine( {
			start = tr.HitPos,
			endpos = tr.HitPos-Vector(0,0,4)
		} )
		if tr.HitPos:DistToSqr(ply:EyePos()) < 72^2 and tr.Entity and tr.Entity.MapCade then
			local e = tr.Entity

			local ent = ents.Create("prop_physics")
			ent:SetModel(e:GetModel())
			ent:SetPos(e:GetPos())
			ent:SetAngles(e:GetAngles())
			ent:SetSkin(e:GetSkin())
			ent.Healthbefore = tr.Entity.health
			ent:Spawn()
			ent:TakeDamage(0)
			ent:GetPhysicsObject():EnableMotion(true)

			e:EmitSound('weapons/crowbar/crowbar_impact2.wav')
			e:Remove()

			ply:ViewPunch(Angle(25,25,25))
			ply:SetAnimation( PLAYER_ATTACK1 )
		end
	end
end

function SWEP:Reload()
	if SERVER then
		local ply = self.Owner
		local tr = ply:GetEyeTrace()
		local center = ply:EyePos()+ply:GetAimVector()*128
		if !IsValid(self.propt) and tr.Entity and tr.Entity:GetClass() == "prop_physics" and IsValid(tr.Entity:GetPhysicsObject()) and tr.HitPos:DistToSqr(ply:EyePos()) < 72^2  and tr.Entity:GetPhysicsObject():GetMass() < 500 then
			self.propt = tr.Entity
		end
	end
end

function SWEP:OnDrop()
	if SERVER then
		self.propt = nil
	end
	self:Remove()
end

function SWEP:Deploy()
	if SERVER then
		self.propt = nil
	end
	return true
end

function SWEP:Holster()
	if SERVER then
		self.propt = nil
	end
	return true
end

local function RotateProp(ply, prop, x, y)
	local sense = 10/1
	local ang = prop:GetAngles()
	ang:RotateAroundAxis(Vector(0, 0, 1), x / sense)
	ang:RotateAroundAxis(Vector(0, 1, 0), y / sense)
	prop:SetAngles(ang)
end

function SWEP:Think()
	if SERVER then
		local ply = self.Owner
		if not IsValid(ply) then return end
		local tr = ply:GetEyeTrace()
		self:SetNWEntity('drawthis', nil)
		if tr.HitPos:DistToSqr(ply:EyePos()) < 128^2 and IsValid(tr.Entity) and tr.Entity:GetClass() == "prop_physics" then
			local tr2 = util.TraceLine({
				start = tr.HitPos,
				endpos = tr.HitPos+ply:GetAimVector()*16,
				filter = function(ent) if !ent:IsWorld() then return end end
			})
			self:SetNWFloat('colormode', 1)
			if tr2.Hit then
				self:SetNWFloat('colormode', 2)
			end
			if tr.Entity:GetPhysicsObject():GetMass() >= 1000 then
				self:SetNWFloat('colormode', 0)
			end
			self:SetNWEntity('drawthis', tr.Entity)
		else
			self:SetNWEntity('drawthis', NULL)
		end

		if IsValid(self.propt) then
			local tr = ply:GetEyeTrace()
			local center = ply:EyePos()+ply:GetAimVector()*72
			if not ply:KeyDown(IN_SPEED) then
				self.propt:GetPhysicsObject():EnableMotion(true)
			else
				self.propt:GetPhysicsObject():EnableMotion(false)
			end
			self.propt:GetPhysicsObject():SetVelocity((center-self.propt:GetPos())*4)
			self.propt:GetPhysicsObject():SetAngleVelocity(Vector())
			local ucmd = ply:GetCurrentCommand()

			if ply:KeyDown(IN_WALK) then
				RotateProp(ply, self.propt, ucmd:GetMouseX(), ucmd:GetMouseY())
				ply:SetEyeAngles(ply.RotatePropAngWep)
			else
				ply.RotatePropAngWep = ply:EyeAngles()
			end
			if center:Distance(self.propt:GetPos()) > 72 or not ply:KeyDown(IN_RELOAD) then
				self.propt:GetPhysicsObject():EnableMotion(true)
				self.propt:GetPhysicsObject():SetVelocity((center-self.propt:GetPos()))
				self.propt = nil
			end
		end
	end
end

function SWEP:DrawHUD()
	draw.SimpleText(SuR.Language["hammer_parts"]..LocalPlayer():GetNWFloat('buildercost'), "SuR_MediumFont1", ScrW()/2, ScrH()-He(250), color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 3, color_black)
	draw.SimpleText(SuR.Language["hammer_controls"], "SuR_SmallFont1", ScrW()/2, ScrH()-He(220), color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 3, color_black)
	local tr = self:GetNWEntity('drawthis')
	local colors = Color(200,20,20)
	if self:GetNWFloat('colormode') == 1 then
		colors = Color(200,200,20)
	elseif self:GetNWFloat('colormode') == 2 then
		colors = Color(20,200,20)
	end
	if IsValid(tr) then
		halo.Add( {tr}, colors, 5, 5, 2 )
	end
end