local function ToadStoolSoundDistant(boss)
	local sound = CreateEntity()
    sound.entity:AddTransform()
    sound.entity:AddSoundEmitter()
    sound.persists = false
	local theta = math.random() * 2 * PI
	local radius = 5
	local offset = Vector3(GetPlayer().Transform:GetWorldPosition()) +  Vector3(radius * math.cos( theta ), 0, -radius * math.sin( theta ))
	sound.Transform:SetPosition(offset.x,offset.y,offset.z)
	local snd = math.random(1,2)
	sound.SoundEmitter:PlaySound("epicworld/epicworld/Fortress_coming_" .. snd)
	sound:DoTaskInTime(1.5, function() sound:Remove() end)
end	

local function SmallEarthQuakeWorm()
	local sound = CreateEntity()
    sound.entity:AddTransform()
    sound.entity:AddSoundEmitter()
    sound.persists = false
	local theta = math.random() * 2 * PI
	local radius = 5
	local offset = Vector3(GetPlayer().Transform:GetWorldPosition()) +  Vector3(radius * math.cos( theta ), 0, -radius * math.sin( theta ))
	sound.Transform:SetPosition(offset.x,offset.y,offset.z)
	sound.SoundEmitter:PlaySound("dontstarve/cave/earthquake", "earthquake")
	sound.SoundEmitter:SetParameter("earthquake", "intensity", 0.05)
	sound:DoTaskInTime(1.5, function() sound:Remove() end)
	TheCamera:Shake("FULL", 1, 0.02, 3, 75)
end	

local function BigEarthQuakeWorm()
	local sound = CreateEntity()
    sound.entity:AddTransform()
    sound.entity:AddSoundEmitter()
    sound.persists = false
	local theta = math.random() * 2 * PI
	local radius = 5
	local offset = Vector3(GetPlayer().Transform:GetWorldPosition()) +  Vector3(radius * math.cos( theta ), 0, -radius * math.sin( theta ))
	sound.Transform:SetPosition(offset.x,offset.y,offset.z)
	sound.SoundEmitter:PlaySound("dontstarve/cave/earthquake", "earthquake")
	sound.SoundEmitter:SetParameter("earthquake", "intensity", 1)
	sound:DoTaskInTime(8.5, function() sound:Remove() end)
	TheCamera:Shake("FULL", 8, 0.02, 3, 75)
end	

local MOB_DELAY = TUNING.FORTRESS_SPAWN_DELAY
local MOB_DELAY_RANDOM = 8400

local EpicWorldBoss = Class(function(self, inst)
	self.inst = inst
	self.inst:StartUpdatingComponent(self)
	self.mobspawndelay = MOB_DELAY + math.random(0,MOB_DELAY_RANDOM)
	self.randommob = nil
	self.activity = false
end)

function EpicWorldBoss:OnSave()
	if not self.noserial then
		return 
		{
			mobspawndelay = self.mobspawndelay,
		}
	end
	self.noserial = false
end

function EpicWorldBoss:OnLoad(data)
	self.mobspawndelay = data.mobspawndelay or MOB_DELAY + math.random(0,MOB_DELAY_RANDOM)
	if self.mobspawndelay < 0 then
		self.mobspawndelay = 5
	end
end

function EpicWorldBoss:OnUpdate(dt)
	self.mobspawndelay = self.mobspawndelay - 1	
	--print("timer: " .. self.mobspawndelay)	
	if self.mobspawndelay == 0 then
		local sound = CreateEntity()
		sound.entity:AddTransform()
		sound.entity:AddSoundEmitter()
		sound.persists = false
		local sm = GetSeasonManager()
		local pt = Vector3(GetPlayer().Transform:GetWorldPosition())
		local spawn_pt = self:GetFloorSpawnPoint()
		if spawn_pt then
			sound:DoTaskInTime(7, function() ToadStoolSoundDistant() end)
			sound:DoTaskInTime(7, function() GetPlayer().components.talker:Say(GetString(GetPlayer().prefab, "ANNOUNCE_DEERCLOPS")) end)
			sound:DoTaskInTime(14, function() ToadStoolSoundDistant() end)
			sound:DoTaskInTime(21, function() ToadStoolSoundDistant() end)
			sound:DoTaskInTime(28, function() ToadStoolSoundDistant() end)
			sound:DoTaskInTime(28, function() GetPlayer().components.talker:Say(GetString(GetPlayer().prefab, "ANNOUNCE_DEERCLOPS")) end)
			sound:DoTaskInTime(35, function() ToadStoolSoundDistant() end)
			sound:DoTaskInTime(42, function() ToadStoolSoundDistant() end)		
			sound:DoTaskInTime(49, function() ToadStoolSoundDistant() end)
			sound:DoTaskInTime(49, function() GetPlayer().components.talker:Say(GetString(GetPlayer().prefab, "ANNOUNCE_DEERCLOPS")) end)
			sound:DoTaskInTime(56, function()
				local new_spawn_pt = self:GetFloorSpawnPoint()
				if new_spawn_pt then
					self.randommob = SpawnPrefab("fortress")
					self.randommob.entity:AddPhysics()
					self.randommob.Physics:Teleport(new_spawn_pt:Get())
					self.randommob.components.combat:SuggestTarget(GetPlayer())
					self.mobspawndelay = MOB_DELAY + math.random(0,MOB_DELAY_RANDOM)
					print("EpicWorldBoss succes!")
				else
					print("EpicWorldBoss failed!")
					self.mobspawndelay = 5
				end 	
			end)
		else
			print("EpicWorldBoss failed!")
			self.mobspawndelay = 5	
		end
	end
end

function EpicWorldBoss:GetSpawnPoint(pt)
    local theta = math.random() * 2 * PI
    local radius = 35
	local offset = FindWalkableOffset(pt, theta, radius, 12, true)
	if offset then
		return pt+offset
	end
end

function EpicWorldBoss:GetFloorSpawnPoint()
	local pt = Vector3(GetPlayer().Transform:GetWorldPosition())	
	local spawn_pt = self:GetSpawnPoint(pt)
	if spawn_pt then	
		return spawn_pt
	end
end

return EpicWorldBoss