require "brains/ughostbrain"
require "stategraphs/SGughost"

local assets =
{
	Asset("ANIM", "anim/ghost.zip"),
	Asset("ANIM", "anim/ghost_build.zip"),
	Asset("SOUND", "sound/ghost.fsb"),
}

local function RetargetFn(inst)
	return FindEntity(inst, 10, 
		function(tar) return inst.components.combat:CanTarget(tar) end, nil, 
		{"prey", "bird", "butterfly", "aquatic", "ghost", "doydoy", "rabbit", "structure", "wall", "insect"})
end

local function KeepTarget(inst, target)
    local shouldkeep = inst.components.combat:CanTarget(target)
    local onwater = target:HasTag("aquatic")
    return shouldkeep and not onwater
end

local function OnAttacked(inst, data)
    inst.components.combat:SetTarget(data.attacker)
    inst.components.combat:ShareTarget(data.attacker, 30, function(dude)
        return dude:HasTag("ghost")
               and not dude.components.health:IsDead()
               and dude.components.follower
               and dude.components.follower.leader == inst.components.follower.leader
    end, 10)
end

local function fn(Sim)
	local inst = CreateEntity()
	local trans = inst.entity:AddTransform()
	local anim = inst.entity:AddAnimState()
	local sound = inst.entity:AddSoundEmitter()
    local light = inst.entity:AddLight()
    inst.entity:AddPhysics()
    anim:SetBloomEffectHandle("shaders/anim.ksh")
    
    MakeGhostPhysics(inst, 1, .5)
    
    light:SetIntensity(.6)
    light:SetRadius(.5)
    light:SetFalloff(.6)
    light:Enable(true)
    light:SetColour(180/255, 195/255, 225/255)
    
    local brain = require "brains/ughostbrain"
    inst:SetBrain(brain)
    
    anim:SetBank("ghost")
    anim:SetBuild("ghost_build")
    anim:PlayAnimation("idle", true)
    
    inst:AddTag("monster")
    inst:AddTag("hostile")
    inst:AddTag("ghost")
    inst:AddTag("noauradamage")

    inst:AddComponent("locomotor")
    inst.components.locomotor.walkspeed = 4
    inst.components.locomotor.runspeed = 4
    inst.components.locomotor.directdrive = true
    
    inst:SetStateGraph("SGughost")
	
	inst:AddComponent("follower")
    inst.components.follower.maxfollowtime = 99999999  
	
	inst:AddComponent("knownlocations") 
	
	inst:AddComponent("lootdropper")
    
	inst:AddComponent("sanityaura")
    inst.components.sanityaura.aura = -TUNING.SANITYAURA_MED
    
    inst:AddComponent("inspectable")
    
	inst:AddComponent("health")
	inst.components.health:SetMaxHealth(TUNING.GHOST_HEALTH)
    
    inst:AddComponent("combat")
    inst.components.combat.defaultdamage = TUNING.GHOST_DAMAGE
    inst.components.combat.playerdamagepercent = TUNING.GHOST_DMG_PLAYER_PERCENT
	inst.components.combat:SetRetargetFunction(1, RetargetFn)
	inst.components.combat:SetKeepTargetFunction(KeepTarget)
	
	inst:ListenForEvent("attacked", OnAttacked)

    inst:AddComponent("aura")
    inst.components.aura.radius = TUNING.GHOST_RADIUS
    inst.components.aura.tickperiod = TUNING.GHOST_DMG_PERIOD

    inst.SoundEmitter:PlaySound("dontstarve/ghost/ghost_howl_LP", "howl")   
    return inst
end

STRINGS.NAMES.UGHOST = "Ghost"
STRINGS.CHARACTERS.GENERIC.DESCRIBE.UGHOST = "You are supposed to rest in peace."

return Prefab( "common/monsters/ughost", fn, assets) 