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("toadstool/toadstool/DST_toadstool_phase_transition_roar_v2_" .. 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 function MosquitoNear()
	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/creatures/mosquito/mosquito_takeoff")
	sound:DoTaskInTime(2.5, function() sound:Remove() end)
end	

local function ShadowSoundDistant()
	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/nightmare_end")
	sound:DoTaskInTime(1.5, function() sound:Remove() end)
end	

local function AlienShipDistant()
	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("epicworld/epicworld/alienship_coming")
	sound:DoTaskInTime(1.5, function() sound:Remove() end)
end	

local function BellDistant()
	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_DLC001/common/glommer_bell")
	sound:DoTaskInTime(1.5, function() sound:Remove() end)
end	

local MOB_DELAY = TUNING.MAP_EVENT_DELAY
local MOB_DELAY_RANDOM = 35

local EpicWorld = 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
	self.inst:ListenForEvent("daytime", MaxwellComing)
end)

function MaxwellComing(inst, data)
    inst.components.epicworld:MaxwellComing(inst, data)
end

function MaxwellAppear(inst)
	local maxwell = SpawnPrefab("maxwell")
	maxwell.entity:AddPhysics()
	maxwell.Physics:Teleport(spawn_pt:Get())			
	maxwell.SoundEmitter:PlaySound("dontstarve/maxwell/disappear")
	SpawnPrefab("maxwell_smoke").Transform:SetPosition(maxwell.Transform:GetWorldPosition())	
	maxwell.AnimState:PlayAnimation("appear")
end

function EpicWorld:MaxwellComing(inst, data)
	local sound = CreateEntity()
	sound.entity:AddTransform()
	sound.entity:AddSoundEmitter()
	sound.persists = false
	local spawn_pt = self:GetFloorSpawnPoint(math.random(2,4))
	if spawn_pt then	
		if math.random() < .5 and not GetPlayer().prefab ~= "waxwell" and TUNING.ACTIVE_MAXWELL == 1 then 
			local maxwell = SpawnPrefab("maxwell")
			maxwell.entity:AddPhysics()
			maxwell.Physics:Teleport(spawn_pt:Get())			
			maxwell.SoundEmitter:PlaySound("dontstarve/maxwell/disappear")
			SpawnPrefab("maxwell_smoke").Transform:SetPosition(maxwell.Transform:GetWorldPosition())	
			local chance = math.random(1,10)
			if chance == 1 then
				maxwell.AnimState:PlayAnimation("appear2")	
				sound:DoTaskInTime(1.5, function() 
					maxwell.components.talker:Say("Yo!")  
					maxwell.AnimState:PlayAnimation("dialog2_pre")  
					maxwell.AnimState:PushAnimation("dialog2_loop", true) 
					maxwell.SoundEmitter:PlaySound("dontstarve/maxwell/talk_LP", "talk")			
				end)
				sound:DoTaskInTime(3.0, function() maxwell.components.talker:Say("Looks like you are surviving somehow.") end)
				sound:DoTaskInTime(5.0, function() maxwell.components.talker:Say("But for how much time?") end)
				sound:DoTaskInTime(7.0, function() maxwell.components.talker:Say("I'm curious to know.") end)
				sound:DoTaskInTime(9.0, function() maxwell.components.talker:Say("Anyway, see you later if you survive...") end)
				sound:DoTaskInTime(11.0, function() 
					maxwell.DynamicShadow:Enable(false)
					maxwell.SoundEmitter:PlaySound("dontstarve/maxwell/disappear") 		
					maxwell.SoundEmitter:KillSound("talk")
					SpawnPrefab("maxwell_smoke").Transform:SetPosition(maxwell.Transform:GetWorldPosition())	
					maxwell.AnimState:PlayAnimation("disappear2", false)		
					maxwell:ListenForEvent("animover", function(inst) inst:Remove() end)
				end)
			elseif chance == 2 then
				maxwell.AnimState:PlayAnimation("appear3")	
				sound:DoTaskInTime(1.5, function() 
					maxwell.components.talker:Say("Hey!")  
					maxwell.AnimState:PlayAnimation("dialog3_pre")  
					maxwell.AnimState:PushAnimation("dialog3_loop", true) 
					maxwell.SoundEmitter:PlaySound("dontstarve/maxwell/talk_LP", "talk")			
				end)
				sound:DoTaskInTime(3.0, function() maxwell.components.talker:Say("Why are you so persistent?") end)
				sound:DoTaskInTime(5.0, function() maxwell.components.talker:Say("I mean...") end)
				sound:DoTaskInTime(6.5, function() maxwell.components.talker:Say("You have no chance of coming out of this alive") end)
				sound:DoTaskInTime(9.0, function() maxwell.components.talker:Say("So please just accept your destiny!") end)
				sound:DoTaskInTime(11.0, function() 
					maxwell.DynamicShadow:Enable(false)
					maxwell.SoundEmitter:PlaySound("dontstarve/maxwell/disappear") 		
					maxwell.SoundEmitter:KillSound("talk")
					SpawnPrefab("maxwell_smoke").Transform:SetPosition(maxwell.Transform:GetWorldPosition())	
					maxwell.AnimState:PlayAnimation("disappear3", false)		
					maxwell:ListenForEvent("animover", function(inst) inst:Remove() end)
				end)			
			elseif chance == 3 then
				maxwell.AnimState:PlayAnimation("appear")	
				sound:DoTaskInTime(1.5, function() 
					maxwell.components.talker:Say("Hmm...")  
					maxwell.AnimState:PlayAnimation("dialog_pre")  
					maxwell.AnimState:PushAnimation("dial_loop", true) 
					maxwell.SoundEmitter:PlaySound("dontstarve/maxwell/talk_LP", "talk")			
				end)
				sound:DoTaskInTime(3.0, function() maxwell.components.talker:Say("If Charlie was there") end)
				sound:DoTaskInTime(4.5, function() maxwell.components.talker:Say("She would also tell you to stop this mess") end)
				sound:DoTaskInTime(6.5, function() maxwell.components.talker:Say("I'm still wondering what really happened to her") end)
				sound:DoTaskInTime(9.0, function() maxwell.components.talker:Say("Even if you don't care at all...") end)
				sound:DoTaskInTime(11.0, function() 
					maxwell.DynamicShadow:Enable(false)
					maxwell.SoundEmitter:PlaySound("dontstarve/maxwell/disappear") 		
					maxwell.SoundEmitter:KillSound("talk")
					SpawnPrefab("maxwell_smoke").Transform:SetPosition(maxwell.Transform:GetWorldPosition())	
					maxwell.AnimState:PlayAnimation("disappear", false)		
					maxwell:ListenForEvent("animover", function(inst) inst:Remove() end)
				end)		
			elseif chance == 4 then
				maxwell.AnimState:PlayAnimation("appear2")	
				sound:DoTaskInTime(1.5, function() 
					maxwell.components.talker:Say("Morning!")  
					maxwell.AnimState:PlayAnimation("dialog2_pre")  
					maxwell.AnimState:PushAnimation("dialog2_loop", true) 
					maxwell.SoundEmitter:PlaySound("dontstarve/maxwell/talk_LP", "talk")			
				end)
				sound:DoTaskInTime(3.0, function() maxwell.components.talker:Say("Seriously...") end)
				sound:DoTaskInTime(4.5, function() maxwell.components.talker:Say("I've seen greater survivors than you") end)
				sound:DoTaskInTime(6.5, function() maxwell.components.talker:Say("But at the end you know what?") end)
				sound:DoTaskInTime(8.0, function() maxwell.components.talker:Say("They all died...") end)
				sound:DoTaskInTime(9.5, function() maxwell.components.talker:Say("And you will die too") end)
				sound:DoTaskInTime(11.5, function() maxwell.components.talker:Say("It's only a question of time.") end)
				sound:DoTaskInTime(14.0, function() 
					maxwell.DynamicShadow:Enable(false)
					maxwell.SoundEmitter:PlaySound("dontstarve/maxwell/disappear") 		
					maxwell.SoundEmitter:KillSound("talk")
					SpawnPrefab("maxwell_smoke").Transform:SetPosition(maxwell.Transform:GetWorldPosition())	
					maxwell.AnimState:PlayAnimation("disappear2", false)		
					maxwell:ListenForEvent("animover", function(inst) inst:Remove() end)
				end)		
			elseif chance == 5 then
				maxwell.AnimState:PlayAnimation("appear")	
				sound:DoTaskInTime(1.5, function() 
					maxwell.components.talker:Say("Hahahaha")  
					maxwell.AnimState:PlayAnimation("dialog_pre")  
					maxwell.AnimState:PushAnimation("dial_loop", true) 
					maxwell.SoundEmitter:PlaySound("dontstarve/maxwell/talk_LP", "talk")			
				end)
				sound:DoTaskInTime(3.0, function() maxwell.components.talker:Say("So how are you doing here?") end)
				sound:DoTaskInTime(5.0, function() maxwell.components.talker:Say("I hope that you aren't too angry at me") end)
				sound:DoTaskInTime(7.0, function() maxwell.components.talker:Say("Next time you should be more careful!") end)
				sound:DoTaskInTime(9.0, function() maxwell.components.talker:Say("Hahahahahaha.") end)
				sound:DoTaskInTime(11.0, function() 
					maxwell.DynamicShadow:Enable(false)
					maxwell.SoundEmitter:PlaySound("dontstarve/maxwell/disappear") 		
					maxwell.SoundEmitter:KillSound("talk")
					SpawnPrefab("maxwell_smoke").Transform:SetPosition(maxwell.Transform:GetWorldPosition())	
					maxwell.AnimState:PlayAnimation("disappear", false)		
					maxwell:ListenForEvent("animover", function(inst) inst:Remove() end)
				end)
			elseif chance == 6 then
				maxwell.AnimState:PlayAnimation("appear4")	
				sound:DoTaskInTime(1.5, function() 
					maxwell.components.talker:Say("Huh?")  
					maxwell.AnimState:PlayAnimation("dialog4_pre")  
					maxwell.AnimState:PushAnimation("dialog4_loop", true) 
					maxwell.SoundEmitter:PlaySound("dontstarve/maxwell/talk_LP", "talk")			
				end)
				sound:DoTaskInTime(3.0, function() maxwell.components.talker:Say("You...") end)
				sound:DoTaskInTime(4.5, function() maxwell.components.talker:Say("You are still alive...") end)
				sound:DoTaskInTime(6.0, function() maxwell.components.talker:Say("I'm very tired today so I won't make fun of you") end)
				sound:DoTaskInTime(8.5, function() maxwell.components.talker:Say("That's very sad I know") end)
				sound:DoTaskInTime(10.5, function() maxwell.components.talker:Say("Hoping not to see you the next day.") end)
				sound:DoTaskInTime(13.0, function() 
					maxwell.DynamicShadow:Enable(false)
					maxwell.SoundEmitter:PlaySound("dontstarve/maxwell/disappear") 		
					maxwell.SoundEmitter:KillSound("talk")
					SpawnPrefab("maxwell_smoke").Transform:SetPosition(maxwell.Transform:GetWorldPosition())	
					maxwell.AnimState:PlayAnimation("disappear4", false)		
					maxwell:ListenForEvent("animover", function(inst) inst:Remove() end)
				end)	
			elseif chance == 7 then
				maxwell.AnimState:PlayAnimation("appear3")	
				sound:DoTaskInTime(1.5, function() 
					maxwell.components.talker:Say("Damn it!")  
					maxwell.AnimState:PlayAnimation("dialog3_pre")  
					maxwell.AnimState:PushAnimation("dialog3_loop", true) 
					maxwell.SoundEmitter:PlaySound("dontstarve/maxwell/talk_LP", "talk")			
				end)
				sound:DoTaskInTime(3.0, function() maxwell.components.talker:Say("Here you are again!") end)
				sound:DoTaskInTime(4.5, function() maxwell.components.talker:Say("Only a single mistake should be enought to take your life") end)
				sound:DoTaskInTime(6.5, function() maxwell.components.talker:Say("but no, you have decided to play the careful character") end)
				sound:DoTaskInTime(9.0, function() maxwell.components.talker:Say("It's not going to work forever I can tell.") end)
				sound:DoTaskInTime(11.0, function() maxwell.components.talker:Say("And I will be here at the right moment to watch you dying!") end)
				sound:DoTaskInTime(14.0, function() 
					maxwell.DynamicShadow:Enable(false)
					maxwell.SoundEmitter:PlaySound("dontstarve/maxwell/disappear") 		
					maxwell.SoundEmitter:KillSound("talk")
					SpawnPrefab("maxwell_smoke").Transform:SetPosition(maxwell.Transform:GetWorldPosition())	
					maxwell.AnimState:PlayAnimation("disappear3", false)		
					maxwell:ListenForEvent("animover", function(inst) inst:Remove() end)
				end)			
			elseif chance == 8 then
				maxwell.AnimState:PlayAnimation("appear2")	
				sound:DoTaskInTime(1.5, function() 
					maxwell.components.talker:Say("Time...")  
					maxwell.AnimState:PlayAnimation("dialog2_pre")  
					maxwell.AnimState:PushAnimation("dialog2_loop", true) 
					maxwell.SoundEmitter:PlaySound("dontstarve/maxwell/talk_LP", "talk")			
				end)
				sound:DoTaskInTime(3.0, function() maxwell.components.talker:Say("How much time has passed since you joined this world?") end)
				sound:DoTaskInTime(5.0, function() maxwell.components.talker:Say("One day? One week? One month?") end)
				sound:DoTaskInTime(7.0, function() maxwell.components.talker:Say("Or maybe one year, I completly forgot.") end)
				sound:DoTaskInTime(9.0, function() maxwell.components.talker:Say("And you know why?") end)
				sound:DoTaskInTime(10.5, function() maxwell.components.talker:Say("That's because so many people died here") end)
				sound:DoTaskInTime(12.5, function() maxwell.components.talker:Say("And I can't remember all their name...") end)
				sound:DoTaskInTime(14.5, function() maxwell.components.talker:Say("Well I guess it's now time for me to laught about you") end)
				sound:DoTaskInTime(17.5, function() maxwell.components.talker:Say("Haha...") end)
				sound:DoTaskInTime(19.0, function() 
					maxwell.DynamicShadow:Enable(false)
					maxwell.SoundEmitter:PlaySound("dontstarve/maxwell/disappear") 		
					maxwell.SoundEmitter:KillSound("talk")
					SpawnPrefab("maxwell_smoke").Transform:SetPosition(maxwell.Transform:GetWorldPosition())	
					maxwell.AnimState:PlayAnimation("disappear2", false)		
					maxwell:ListenForEvent("animover", function(inst) inst:Remove() end)
				end)	
			elseif chance == 9 then
				maxwell.AnimState:PlayAnimation("appear4")	
				sound:DoTaskInTime(1.5, function() 
					maxwell.components.talker:Say("There you are again!")  
					maxwell.AnimState:PlayAnimation("dialog4_pre")  
					maxwell.AnimState:PushAnimation("dialog4_loop", true) 
					maxwell.SoundEmitter:PlaySound("dontstarve/maxwell/talk_LP", "talk")			
				end)
				sound:DoTaskInTime(3.0, function() maxwell.components.talker:Say("Surviving like a little cockroach") end)
				sound:DoTaskInTime(5.0, function() maxwell.components.talker:Say("At least I'm happy that you didn't bring your lover here!") end)
				sound:DoTaskInTime(7.5, function() maxwell.components.talker:Say("Because you would have started to multiply with time like bugs") end)
				sound:DoTaskInTime(10.0, function() maxwell.components.talker:Say("And it would have been very annoying for me") end)
				sound:DoTaskInTime(12.0, function() maxwell.components.talker:Say("But I guess...") end)
				sound:DoTaskInTime(13.5, function() maxwell.components.talker:Say("You never had one!") end)
				sound:DoTaskInTime(15.0, function() maxwell.components.talker:Say("I'm not sorry at all for that.") end)
				sound:DoTaskInTime(17.0, function() 
					maxwell.DynamicShadow:Enable(false)
					maxwell.SoundEmitter:PlaySound("dontstarve/maxwell/disappear") 		
					maxwell.SoundEmitter:KillSound("talk")
					SpawnPrefab("maxwell_smoke").Transform:SetPosition(maxwell.Transform:GetWorldPosition())	
					maxwell.AnimState:PlayAnimation("disappear4", false)		
					maxwell:ListenForEvent("animover", function(inst) inst:Remove() end)
				end)	
			elseif chance == 10 then
				maxwell.AnimState:PlayAnimation("appear5")	
				sound:DoTaskInTime(1.5, function() 
					maxwell.components.talker:Say("WHAT?!")  
					maxwell.AnimState:PlayAnimation("dialog5_pre")  
					maxwell.AnimState:PushAnimation("dialog5_loop", true) 
					maxwell.SoundEmitter:PlaySound("dontstarve/maxwell/talk_LP", "talk")			
				end)
				sound:DoTaskInTime(3.0, function() maxwell.components.talker:Say("GOD DAMNIT!") end)
				sound:DoTaskInTime(4.5, function() maxwell.components.talker:Say("COULD YOU PLEASE JUST DIE!") end)
				sound:DoTaskInTime(6.0, function() maxwell.components.talker:Say("DIE!!!") end)
				sound:DoTaskInTime(8.5, function() maxwell.components.talker:Say("I DON'T WANT TO SEE YOUR FACE EVER AGAIN!") end)
				sound:DoTaskInTime(11.0, function() maxwell.components.talker:Say("UNDERSTOOD?!") end)
				sound:DoTaskInTime(13.0, function() 
					maxwell.DynamicShadow:Enable(false)
					maxwell.SoundEmitter:PlaySound("dontstarve/maxwell/disappear") 		
					maxwell.SoundEmitter:KillSound("talk")
					SpawnPrefab("maxwell_smoke").Transform:SetPosition(maxwell.Transform:GetWorldPosition())	
					maxwell.AnimState:PlayAnimation("disappear5", false)		
					maxwell:ListenForEvent("animover", function(inst) inst:Remove() end)
				end)				
			end
		end
	end
end

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

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

function EpicWorld: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(30)
		if spawn_pt then
			local worldevent = math.random(1,5)
			if worldevent == 1 then
				sound:DoTaskInTime(7, function() SmallEarthQuakeWorm() end)
				sound:DoTaskInTime(7, function() GetPlayer().components.talker:Say("Underground activity!") end)
				sound:DoTaskInTime(14, function() SmallEarthQuakeWorm() end)
				sound:DoTaskInTime(21, function() SmallEarthQuakeWorm() end)
				sound:DoTaskInTime(28, function() SmallEarthQuakeWorm() end)
				sound:DoTaskInTime(28, function() GetPlayer().components.talker:Say("It's getting closer!") end)
				sound:DoTaskInTime(35, function() SmallEarthQuakeWorm() end)
				sound:DoTaskInTime(42, function() SmallEarthQuakeWorm() end)		
				sound:DoTaskInTime(49, function() BigEarthQuakeWorm() end)
				sound:DoTaskInTime(49, function() GetPlayer().components.talker:Say("What is that?!") end)
				sound:DoTaskInTime(56, function()
					local new_spawn_pt = self:GetFloorSpawnPoint(math.random(8,12))
					if new_spawn_pt then
						self.randommob = SpawnPrefab("giantwormhole")
						self.randommob.entity:AddPhysics()
						self.randommob.Physics:Teleport(new_spawn_pt:Get())
						local dust = SpawnPrefab("small_puff")
						dust.Transform:SetPosition(self.randommob:GetPosition():Get())
						dust.Transform:SetScale(7,7,7)
						local dust = SpawnPrefab("small_puff")
						dust.Transform:SetPosition(self.randommob:GetPosition():Get())
						dust.Transform:SetScale(5,5,5)
						local dust = SpawnPrefab("collapse_small")
						dust.Transform:SetPosition(self.randommob:GetPosition():Get())
						dust.Transform:SetScale(4,4,4)
						local pt = self.randommob:GetPosition()
						local ents = TheSim:FindEntities(pt.x, pt.y, pt.z, 6)
						for k,v in pairs(ents) do
							if v.components.combat and v ~= self.randommob and not v:HasTag("shadow") then
								v.components.combat:GetAttacked(self.randommob, 750)
							elseif v.components.workable and v.components.workable.workleft > 0 then
								SpawnPrefab("collapse_small").Transform:SetPosition(v:GetPosition():Get())
								v.components.workable:Destroy(self.randommob)		
							end
						end	
						--self.randommob:FacePoint(pt)
						--self.randommob.components.combat:SuggestTarget(GetPlayer())
						self.mobspawndelay = MOB_DELAY + math.random(0,MOB_DELAY_RANDOM)
						print("EpicWorld succes!")
					else
						print("EpicWorld failed!")
						self.mobspawndelay = 5
					end 	
				end)
			elseif worldevent == 2 and not GetSeasonManager():IsWinter() then
				sound:DoTaskInTime(3, function() MosquitoNear() end)
				sound:DoTaskInTime(4, function() MosquitoNear() end)
				sound:DoTaskInTime(5, function() MosquitoNear() end)
				sound:DoTaskInTime(6, function() MosquitoNear() end)
				sound:DoTaskInTime(6, function() GetPlayer().components.talker:Say("Oh no please no!") end)
				sound:DoTaskInTime(7, function() MosquitoNear() end)
				sound:DoTaskInTime(8, function() MosquitoNear() end)
				sound:DoTaskInTime(9, function() MosquitoNear() end)
				sound:DoTaskInTime(10, function() MosquitoNear() end)
				sound:DoTaskInTime(11, function() MosquitoNear() end)
				sound:DoTaskInTime(12, function() MosquitoNear() end)
				sound:DoTaskInTime(13, function() MosquitoNear() end)
				sound:DoTaskInTime(14, function() MosquitoNear() end)
				sound:DoTaskInTime(15, function() MosquitoNear() end)
				sound:DoTaskInTime(16, function() MosquitoNear() end)
				sound:DoTaskInTime(17, function() MosquitoNear() end)
				sound:DoTaskInTime(17, function()
					local new_spawn_pt = self:FindMainBaseLocation()
					if new_spawn_pt then
						self.randommob = SpawnPrefab("infestpond")
						self.randommob.entity:AddPhysics()
						self.randommob.Physics:Teleport(new_spawn_pt:Get())
						local dust = SpawnPrefab("small_puff")
						dust.Transform:SetPosition(self.randommob:GetPosition():Get())
						dust.Transform:SetScale(7,7,7)
						local dust = SpawnPrefab("small_puff")
						dust.Transform:SetPosition(self.randommob:GetPosition():Get())
						dust.Transform:SetScale(5,5,5)
						local dust = SpawnPrefab("collapse_small")
						dust.Transform:SetPosition(self.randommob:GetPosition():Get())
						dust.Transform:SetScale(4,4,4)
						self.mobspawndelay = MOB_DELAY + math.random(0,MOB_DELAY_RANDOM)
						print("EpicWorld succes!")
					else
						print("EpicWorld failed!")
						self.mobspawndelay = 5
					end 	
				end)
			elseif worldevent == 3 and  GetClock():IsNight() then
				sound:DoTaskInTime(5, function() ShadowSoundDistant() end)
				sound:DoTaskInTime(10, function() ShadowSoundDistant() end)
				sound:DoTaskInTime(10, function() GetPlayer().components.talker:Say("The shadow forces have been awaken.") end)
				sound:DoTaskInTime(15, function() ShadowSoundDistant() end)
				sound:DoTaskInTime(20, function() ShadowSoundDistant() end)
				sound:DoTaskInTime(25, function() ShadowSoundDistant() end)
				sound:DoTaskInTime(30, function()
					local new_spawn_pt = self:GetFloorSpawnPoint(math.random(4,12))
					if new_spawn_pt then
						local amount = math.random(8,16)
						local delay = 0
						for k = 1, amount do
							delay = delay + 1
							sound:DoTaskInTime(1 + delay * .5 ,function() ShadowSoundDistant()
								new_spawn_pt = self:GetFloorSpawnPoint(math.random(4,12))
								if new_spawn_pt then
									self.randommob = SpawnPrefab("shadowfissure")
									self.randommob.entity:AddPhysics()
									self.randommob.Physics:Teleport(new_spawn_pt:Get())							
								end
							end)
						end
						self.mobspawndelay = MOB_DELAY + math.random(0,MOB_DELAY_RANDOM)
						print("EpicWorld succes!")
					else
						print("EpicWorld failed!")
						self.mobspawndelay = 5
					end 	
				end)
			elseif worldevent == 4 and not GetClock():IsDay() then
				sound:DoTaskInTime(6, function() AlienShipDistant() end)
				sound:DoTaskInTime(12, function() AlienShipDistant() end)
				sound:DoTaskInTime(12, function() GetPlayer().components.talker:Say("What thing could make such noise?") end)
				sound:DoTaskInTime(18, function() AlienShipDistant() end)
				sound:DoTaskInTime(24, function() AlienShipDistant() end)
				sound:DoTaskInTime(30, function() AlienShipDistant() end)
				sound:DoTaskInTime(36, function()
					local new_spawn_pt = self:GetFloorSpawnPoint(55)
					if new_spawn_pt then
						self.randommob = SpawnPrefab("alienship")
						self.randommob.entity:AddPhysics()
						self.randommob.Physics:Teleport(new_spawn_pt:Get())
						self.mobspawndelay = MOB_DELAY + math.random(0,MOB_DELAY_RANDOM)
						print("EpicWorld succes!")
					else
						print("EpicWorld failed!")
						self.mobspawndelay = 5
					end 	
				end)
			elseif worldevent == 5 and not GetClock():IsNight() then
				sound:DoTaskInTime(4, function() BellDistant() end)
				sound:DoTaskInTime(8, function() BellDistant() end)
				sound:DoTaskInTime(8, function() GetPlayer().components.talker:Say("What?! Who is doing that noise?") end)
				sound:DoTaskInTime(12, function() BellDistant() end)
				sound:DoTaskInTime(16, function() BellDistant() end)
				sound:DoTaskInTime(20, function() BellDistant() end)
				sound:DoTaskInTime(36, function()
					local new_spawn_pt = self:GetFloorSpawnPoint(1)
					if new_spawn_pt then
						if GetWorld().components.bigfooter then
							GetWorld().components.bigfooter:SummonFoot(GetPlayer():GetPosition())
						end
						self.mobspawndelay = MOB_DELAY + math.random(0,MOB_DELAY_RANDOM)
						print("EpicWorld succes!")
					else
						print("EpicWorld failed!")
						self.mobspawndelay = 5
					end 	
				end)	
			else
				print("EpicWorld failed!")
				self.mobspawndelay = 5			
			end
		else
			print("EpicWorld failed!")
			self.mobspawndelay = 5	
		end
	end
end

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

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

function EpicWorld:GetOtherFloorSpawnPoint(pt, radius)
	local spawn_pt = self:GetSpawnPoint(pt, radius)
	if spawn_pt then	
		return spawn_pt
	end
end

function GetStructures(inst)
	local x,y,z = inst.Transform:GetWorldPosition()
	local ents = TheSim:FindEntities(x,y,z,25, {"structure"}, {"FX", "NOCLICK", "DECOR","INLIMBO"})
    return ents
end

function EpicWorld:FindMainBaseLocation()
    local allvalidlabs = {}
    for guid, ent in pairs(Ents) do
        if ent.entity:IsValid() and (ent.prefab == "researchlab" or ent.prefab == "researchlab2" or ent.prefab == "researchlab3" or ent.prefab == "researchlab4") then-- and (not self.only_spawn_offscreen or ent:IsAsleep()) then
            table.insert(allvalidlabs, ent)
        end
    end
    if next(allvalidlabs) == nil then
        return
    else
        local spawnlab = GetRandomItem(allvalidlabs)
		if spawnlab then	
			local structures = GetStructures(spawnlab)
			local around = 0
			for k,v in pairs(structures) do
				around = around + 1
			end	
			if around >= 25 then
				local pt = Vector3(spawnlab.Transform:GetWorldPosition())
				local radius = math.random(14,20)
				local spawn_pt = self:GetOtherFloorSpawnPoint(pt, radius)
				if spawn_pt then	
					return spawn_pt
				end
			elseif around >= 20 then
				local pt = Vector3(spawnlab.Transform:GetWorldPosition())
				local radius = math.random(14,20)
				local spawn_pt = self:GetOtherFloorSpawnPoint(pt, radius)
				if spawn_pt then	
					return spawn_pt
				end
			elseif around >= 15 then
				local pt = Vector3(spawnlab.Transform:GetWorldPosition())
				local radius = math.random(14,20)
				local spawn_pt = self:GetOtherFloorSpawnPoint(pt, radius)
				if spawn_pt then	
					return spawn_pt
				end
			elseif around >= 10 then
				local pt = Vector3(spawnlab.Transform:GetWorldPosition())
				local radius = math.random(14,20)
				local spawn_pt = self:GetOtherFloorSpawnPoint(pt, radius)
				if spawn_pt then	
					return spawn_pt
				end
			elseif around >= 5 then
				local pt = Vector3(spawnlab.Transform:GetWorldPosition())
				local radius = math.random(14,20)
				local spawn_pt = self:GetOtherFloorSpawnPoint(pt, radius)
				if spawn_pt then	
					return spawn_pt
				end
			elseif around < 5 then
				local pt = Vector3(spawnlab.Transform:GetWorldPosition())
				local radius = math.random(14,20)
				local spawn_pt = self:GetOtherFloorSpawnPoint(pt, radius)
				if spawn_pt then	
					return spawn_pt
				end
			end			
		end		
    end
end

return EpicWorld