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

include("shared.lua")

function ENT:Initialize()
	self:SetModel(table.Random(SWM_TREE_RED_MODELS))
	self:PhysicsInit(SOLID_VPHYSICS)
	self:SetMoveType(MOVETYPE_VPHYSICS)
	self:SetSolid(SOLID_VPHYSICS)
	local phys = self:GetPhysicsObject()
	self.Replace = false
	phys:Wake()
	self:SetTreeHealth(SWM_TREE_RED_HEALTH)
end

function ENT:UpdateTransmitState()
	return TRANSMIT_ALWAYS
end

function ENT:Think()
	if (!self.Replace) and (self:GetTreeHealth() <= 0)  then
		self.Replace = true
		self.ReplaceTime = CurTime() + SWM_TREE_RED_REPLACE_TIMER
		self:SetMaterial("Models/effects/vol_light001");
		self:SetCollisionGroup(10)
		self.Pos = self:GetPos()
		
		local log = ents.Create('swm_log')
		log:SetPos(self:GetPos()+Vector(0,0,30))
		log:Spawn()
		local log2 = ents.Create('swm_log')
		log2:SetPos(self:GetPos()+Vector(0,0,87))
		log2:Spawn()
		local log3 = ents.Create('swm_log')
		log3:SetPos(self:GetPos()+Vector(0,0,144))
		log3:Spawn()
		local log4 = ents.Create('swm_log')
		log4:SetPos(self:GetPos()+Vector(0,0,201))
		log4:Spawn()
		constraint.Weld(log, log2, 0, 0, 500, true, false)
		constraint.Weld(log2, log3, 0, 0, 500, true, false)
		constraint.Weld(log3, log4, 0, 0, 500, true, false)
		
		self:SetPos(self.Pos + Vector(0,0,-300))
	end;
	
	if (self.Replace) and (self.ReplaceTime < CurTime()) then
		self:SetTreeHealth(SWM_TREE_RED_HEALTH)
		self.Replace = false
		self:SetMaterial();
		self:SetCollisionGroup(0)
		self:SetPos(self.Pos)
	end
end

function ENT:OnTakeDamage(dmg)
	if table.HasValue(SWM_CUTTING_TOOLS, dmg:GetInflictor():GetClass()) or table.HasValue(SWM_CUTTING_TOOLS, dmg:GetAttacker():GetActiveWeapon():GetClass()) then
		self:SetTreeHealth( self:GetTreeHealth() - 1) 
	end
end

function ENT:OnRemove()
	if not IsValid(self) then return end
end
