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

include('shared.lua')

function ENT:Initialize()

	self.Entity:SetModel("models/weapons/w_eq_fraggrenade_thrown.mdl")
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	self.Entity:DrawShadow( true )

	self.Entity:SetCollisionGroup(COLLISION_GROUP_PLAYER)
	
    self.FleshHit = { 
	Sound( "physics/flesh/flesh_impact_bullet1.wav" ),
	Sound( "physics/flesh/flesh_impact_bullet2.wav" ),
	Sound( "physics/flesh/flesh_impact_bullet3.wav" ) }
	
    self.Entity:SetNetworkedString("Owner", "World")
	
	local phys = self.Entity:GetPhysicsObject()
	
	if (phys:IsValid()) then
		phys:Wake()
        phys:SetMass( 3 )
	end
	
	
end

function ENT:Think()

	if self.timer == nil then -- eat shit
		self.Entity:Remove()
	end

	if self.timer and self.timer < CurTime() then
	
		self.Entity:Remove()
		
		local explo = ents.Create("env_explosion")
		explo:SetOwner(self.GrenadeOwner)
		explo:SetPos(self.Entity:GetPos())
		explo:SetKeyValue("iMagnitude", "250")
		explo:SetKeyValue("spawnflags", "66")
		explo:Spawn()
		explo:Activate()
		explo:Fire("Explode", "", 0)

		local physExplo = ents.Create( "env_physexplosion" )
		physExplo:SetOwner( self.GrenadeOwner )
		physExplo:SetPos( self.Entity:GetPos() )
		physExplo:SetKeyValue( "Magnitude", "100" )	-- Power of the Physicsexplosion
		physExplo:SetKeyValue( "radius", "500" )	-- Radius of the explosion
		physExplo:SetKeyValue( "spawnflags", "10" )
		physExplo:Spawn()
		physExplo:Fire( "Explode", "", 0.02 )
		
		local effectdata = EffectData()
		effectdata:SetOrigin( self.Entity:GetPos() )
		util.Effect( "HelicopterMegaBomb", effectdata )	 -- Big flame

		local tracedata = {}
		tracedata.start = self.Entity:GetPos()
		tracedata.endpos = self.Entity:GetPos() + Vector(0,0,-20)
		tracedata.filter = self.Entity
		tracedata.mask = MASK_SHOT
		local tr = util.TraceLine(tracedata)
		
		if (SERVER) then
			if tr.HitWorld and self.Entity:WaterLevel() == 0 then
				local Trail = ents.Create("info_particle_system")
				Trail:SetPos(self.Entity:GetPos())
				Trail:SetKeyValue( "effect_name", "strider_impale_ground" )
				Trail:SetKeyValue( "start_active", "1" )
				Trail:Spawn()
				Trail:Activate()
				timer.Simple( 3, function()
					Trail:Remove()
				end, trail)
			end
		end
	
		local shake = ents.Create( "env_shake" )
		shake:SetOwner(self.GrenadeOwner)
		shake:SetPos( self.Entity:GetPos() )
		shake:SetKeyValue( "amplitude", "3500" )	-- Power of the shake
		shake:SetKeyValue( "radius", "900" )	-- Radius of the shake
		shake:SetKeyValue( "duration", "3" )	-- Time of shake
		shake:SetKeyValue( "frequency", "255" )	-- How har should the screenshake be
		shake:SetKeyValue( "spawnflags", "4" )	-- Spawnflags( In Air )
		shake:Spawn()
		shake:Activate()
		shake:Fire( "StartShake", "", 0 )
		
		self.Entity:EmitSound("weapons/hegrenade/explode".. math.random(3, 5) .. ".wav", 150)
		
		local en = ents.FindInSphere(self.Entity:GetPos(), 75)
		
		for k, v in pairs(en) do
			if (v:GetPhysicsObject():IsValid()) then
				// Unweld and unfreeze props
				if (math.random(1, 100) < 10) then
					v:Fire("enablemotion", "", 0)
					constraint.RemoveAll(v)
				end
			end
		end
	end
end

/*---------------------------------------------------------
   Name: OnTakeDamage
   Desc: Entity takes damage
---------------------------------------------------------*/
function ENT:OnTakeDamage( dmginfo )

	self.Entity:TakePhysicsDamage(dmginfo)

end


/*---------------------------------------------------------
   Name: Use
---------------------------------------------------------*/
function ENT:Use( activator, caller, type, value )
	
	if !activator:IsPlayer() or activator.GrenadeTaked then return end
	
	local weapon = activator:GetActiveWeapon()
	
	if weapon:GetClass() != "quick_grenade" then
		self.Entity:Remove()
		activator.GrenadeTaked = true
		activator.AssPlodeTime = self.timer
		activator:SelectWeapon("quick_grenade")
	end

end


/*---------------------------------------------------------
   Name: StartTouch
---------------------------------------------------------*/
function ENT:StartTouch( entity )
end


/*---------------------------------------------------------
   Name: EndTouch
---------------------------------------------------------*/
function ENT:EndTouch( entity )
end


/*---------------------------------------------------------
   Name: Touch
---------------------------------------------------------*/
function ENT:Touch( entity )
end
