local assets =
{
	Asset("ANIM", "anim/warning_shadow.zip"),
}

local animals = 
{
	spider                 = 75,
	spider_warrior         = 75,
	spider_spitter         = 75,
	spider_hider           = 75,
	spider_dropper         = 75,
	frog                   = 75,
	bat                    = 75,
	bee                    = 75,
	killerbee              = 75,
	perd                   = 75,
	merm                   = 75,
	mosquito               = 75,
	penguin                = 75,
	rabbit                 = 75,
	slurper                = 75,
	smallbird              = 75,	
	hound                  = 25,
	firehound              = 25,
	icehound               = 25,
	bunnyman               = 25,
	slurtle                = 25,
	snurtle                = 25,
	pigman                 = 25,
	pigguard               = 25,
	monkey                 = 25,
	teenbird               = 25,
	beefalo                = 10,
	babybeefalo            = 10,
	little_walrus          = 10,
	tallbird               = 10,	
	walrus                 = 5,
	koalefant_summer       = 5,
	koalefant_winter       = 5,
}

local animals_DLC = 
{
	spider                 = 75,
	spider_warrior         = 75,
	spider_spitter         = 75,
	spider_hider           = 75,
	spider_dropper         = 75,
	frog                   = 75,
	bat                    = 75,
	bee                    = 75,
	killerbee              = 75,
	perd                   = 75,
	merm                   = 75,
	mosquito               = 75,
	penguin                = 75,
	rabbit                 = 75,
	mole                   = 75,
	slurper                = 75,
	smallbird              = 75,	
	hound                  = 25,
	firehound              = 25,
	icehound               = 25,
	bunnyman               = 25,
	slurtle                = 25,
	snurtle                = 25,
	pigman                 = 25,
	pigguard               = 25,
	catcoon                = 25,
	monkey                 = 25,
	teenbird               = 25,
	beefalo                = 10,
	babybeefalo            = 10,
	lightninggoat          = 10,
	little_walrus          = 10,
	tallbird               = 10,	
	walrus                 = 5,
	koalefant_summer       = 5,
	koalefant_winter       = 5,
}

function GetSpawnPoint(pt, rad)
    local theta = math.random() * 2 * PI
    local radius = math.random(3,8) 	
	local result_offset = FindValidPositionByFan(theta, radius, 12, function(offset)
		local ground = GetWorld()
        local spawn_point = pt + offset
        if not (ground.Map and ground.Map:GetTileAtPoint(spawn_point.x, spawn_point.y, spawn_point.z) == GROUND.IMPASSABLE or ground.Map:GetTileAtPoint(spawn_point.x, spawn_point.y, spawn_point.z) > GROUND.UNDERGROUND) then
			return true
        end
		return false
    end)
	if result_offset then
		return pt + result_offset
	end
end   

local function AnimalFall(inst)
	local pt = Point(inst.Transform:GetWorldPosition())
	inst.Physics:SetDamping(0)
	inst.Physics:SetMass(100)
	inst.Physics:SetMotorVel(0,-20+math.random()*10,0)	
	if pt.y < 2 then
		inst.Physics:SetMotorVel(0,0,0)
    end
	if pt.y <= .2 then
		local ents = TheSim:FindEntities(pt.x, 0, pt.z, 2, nil, {'smashable'})
	    for k,v in pairs(ents) do
	    	if v and v.components.combat and v ~= inst then
	    		v.components.combat:GetAttacked(inst, 20, nil)
	    	end
	   	end
	   	inst.Physics:SetDamping(0.9)	
		if inst.updatetask then
			inst.updatetask:Cancel()
			inst.updatetask = nil
		end	
		local pos = Vector3(inst.Transform:GetWorldPosition())
		local tile = GetWorld().Map:GetTileAtPoint(pos.x, pos.y, pos.z)
		if tile == GROUND.IMPASSABLE then
			inst.SoundEmitter:PlaySound("dontstarve/common/stone_drop")
			local pt = Vector3(inst.Transform:GetWorldPosition())
			local breaking = SpawnPrefab("ground_chunks_breaking")
			breaking.Transform:SetPosition(pt.x, 0, pt.z)
			inst:Remove()
		else	
			inst.Physics:Stop()
			inst.Physics:SetDamping(5)
			inst.Physics:Teleport(pt.x,pt.y,pt.z)
		end
	end
end	

local function AnimalInitialize(inst)
	inst.updatetask = inst:DoPeriodicTask(1*FRAMES,AnimalFall,1)	
end 

local function SpawnAnimal(inst)
	local animal_prefab
	if (IsDLCEnabled(CAPY_DLC) or IsDLCEnabled(REIGN_OF_GIANTS)) then
		animal_prefab = weighted_random_choice(animals_DLC)
	else
		animal_prefab = weighted_random_choice(animals)
	end
	local animal = SpawnPrefab(animal_prefab) 
	if animal.prefab == "mole" then
		animal.sg:GoToState("fall")
	end
	local pos = Vector3(inst.Transform:GetWorldPosition())
	local spawn_point = GetSpawnPoint(pos)	
	animal.Physics:Teleport(spawn_point.x,35,spawn_point.z)	
	inst:DoTaskInTime(0.5, function(inst) AnimalInitialize(animal) end)
end

local function fn(Sim)
	local inst = CreateEntity()
	local trans = inst.entity:AddTransform()
	local anim = inst.entity:AddAnimState()
	local sound = inst.entity:AddSoundEmitter()
	local phys = inst.entity:AddPhysics()
	
	anim:SetBank("warning_shadow")
	anim:SetBuild("warning_shadow")
	anim:PlayAnimation("idle", true)
	
	inst.entity:AddLight()
	inst.Light:Enable(true)
	inst.Light:SetRadius(25)
	inst.Light:SetFalloff(0.8)
	inst.Light:SetIntensity(.75)
	inst.Light:SetColour(223/255, 208/255, 69/255)
	
	trans:SetScale(23.0, 23.0, 23.0)
	
	MakeCharacterPhysics(inst, 10000, .01)
	
	inst:AddTag("alienship")
		
	inst:AddComponent("inspectable")
			
	inst:AddComponent("locomotor")
    inst.components.locomotor.walkspeed = 9/23.0
	inst.components.locomotor.runspeed = 9/23.0
	
	inst:AddComponent("knownlocations")
	
	inst:SetStateGraph("SGalienship")
	
	local brain = require "brains/alienshipbrain"
	inst:SetBrain(brain)
	
	inst.SoundEmitter:PlaySound("epicworld/epicworld/alienship_idle", "channel")
	inst:DoPeriodicTask(50, function(inst) inst.SoundEmitter:PlaySound("epicworld/epicworld/alienship_idle", "channel") end)
	
	inst:DoPeriodicTask(5, function(inst)
		local target = GetPlayer()
		if target then
			if inst.components.knownlocations:GetLocation("playerpos") then
				inst.components.knownlocations:ForgetLocation("playerpos")
			end
			local targetPos = Vector3(target.Transform:GetWorldPosition())
			inst.components.knownlocations:RememberLocation("playerpos", targetPos)
		end
	end)

	inst:DoPeriodicTask(180, function(inst)
		local pt = Vector3(GetPlayer().Transform:GetWorldPosition())
		local theta = math.random() * 2 * PI
		local radius = math.random(150,450)
		local offset = FindWalkableOffset(pt, theta, radius, 12, true)
		if offset then
			local leaving = pt + offset
			if leaving then
				if inst.components.knownlocations:GetLocation("playerpos") then
					inst.shouldleave = true
					inst.components.knownlocations:ForgetLocation("playerpos")
				end
				if inst.components.knownlocations:GetLocation("leavepos") then
					inst.components.knownlocations:ForgetLocation("leavepos")
				end
				inst.components.knownlocations:RememberLocation("leavepos", leaving)
			end
		end	
	end)	
	
	inst:DoPeriodicTask(3, function()
		if not inst.shouldleave then 
			SpawnAnimal(inst)
		end
	end)
	
	inst:DoPeriodicTask(1, function()
		trans:SetScale(23.0, 23.0, 23.0)
		inst.Physics:ClearCollisionMask()
		if inst:GetTimeAlive() > 240 then
			inst:Remove()
		end
	end)
	
	inst.OnSave = function(inst, data)
		if inst.shouldleave then 
			data.shouldleave = inst.shouldleave
		end
    end     
	
	inst.OnLoad = function(inst, data)    
		inst.SoundEmitter:PlaySound("epicworld/epicworld/alienship_idle", "channel")
		if data and data.shouldleave then
			inst.shouldleave = data.shouldleave
			inst:Remove()
		end
	end
    return inst
end

STRINGS.NAMES.ALIENSHIP = "Alien Ship"
STRINGS.CHARACTERS.GENERIC.DESCRIBE.ALIENSHIP = "I knew it they really exist!"

return Prefab("common/alienship", fn, assets) 