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

function ENT:SpawnFunction( userid, tr )

	local trace = util.GetPlayerTrace( userid, userid:GetAimVector() )
	local tr = util.TraceLine( trace )
	local thisent = ents.Create( "combine_bomb" )
		thisent:SetPos( tr.HitPos + tr.HitNormal * 32 )
--		thisent:SetAngles( tr.HitNormal:Angle() )
		thisent:Spawn()
	thisent:Activate()

end

function ENT:Initialize()

self.BeepShit = {

	Sound( "buttons/combine_button1.wav" );
	Sound( "buttons/combine_button2.wav" );
	Sound( "buttons/combine_button3.wav" );
	Sound( "buttons/combine_button5.wav" );
	Sound( "buttons/combine_button7.wav" );

}

self.Entity:SetModel("models/weapons/w_slam.mdl")
self.Entity:PhysicsInit( SOLID_VPHYSICS )
self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
self.Entity:SetSolid( SOLID_VPHYSICS )
util.PrecacheSound("buttons/combine_button_locked.wav")
self.Off = true
self.Timer = CurTime() + 2.5

end

function ENT:OnTakeDamage()

    local vPoint = self.Entity:GetPos()
    local effectdata = EffectData()
    effectdata:SetStart(vPoint)
    effectdata:SetOrigin(vPoint)
    effectdata:SetScale(1)
    util.Effect("Explosion", effectdata)
	self.Entity:Remove()
		
end

function ENT:Use( activator, caller )

	 if self.Timer < CurTime() then
			self.Timer = CurTime() + 1
		
		if caller:IsCombineDefense() then
		
				self.Entity:EmitSound( self.BeepShit[math.random( 1, #self.BeepShit )] );

			if self.Off == true then
				self.Off = false
			
			elseif self.Off == false then
				self.Off = true
			
			end
			
		end
		
		if !caller:IsCombineDefense() then
			self.Entity:EmitSound("buttons/combine_button_locked.wav", 75, 100)
		end
		
	end
	
end

function ENT:StartTouch()

end

function ENT:EndTouch()

end

local function search(xent)

if !xent:IsValid() then return end 

	local nearby=ents.FindInSphere(xent:GetPos(),50)
		for i=1,table.getn(nearby) do
			if( nearby[i]:GetClass()=="combine_sensor" && nearby[i]:GetNWBool( "beeping" ) == true )then

			local cock = ents.Create( "env_physexplosion" )
				cock:SetKeyValue( "magnitude", 250 )
				cock:SetKeyValue( "radius", 125 )
				cock:SetPos( xent:GetPos() );
			cock:Spawn();
			cock:Fire( "explode", "", 0 );
			xent:Remove();
			
			else
				// Durundal it up
			end
					
		end
		
end

local function boom(xent)

if !xent:IsValid() then return end 

			local boom=ents.FindInSphere(xent:GetPos(),150)
	for i=1,table.getn(boom) do
			if boom[i]:GetClass()=="prop_physics" then
		
			local vPoint = xent:GetPos()
			local effectdata = EffectData()
			effectdata:SetStart(vPoint)
			effectdata:SetOrigin(vPoint)
			effectdata:SetScale(1)
			util.Effect("Explosion", effectdata)
			xent:Remove();		
			boom[i]:Fire("break")
			
			end
	end
end

function ENT:Think()
	
	if self.Off == true then
	end
	
	if self.Off == false then
	--search(self.Entity)
	 timer.Simple(3, search, self.Entity) 
	 timer.Simple(3, boom, self.Entity)
	end
	
end

