require "brains/yiyu_catbrain"
require "stategraphs/SGyiyu_cat"

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

local prefabs ={}

local loot = 
{
    "bell_blueprint",
}

local MAX_TARGET_SHARES = 5
local SHARE_TARGET_DIST = 50

local function ondeath(inst)
	local pt = Vector3(inst.Transform:GetWorldPosition())
	if not SaveGameIndex:IsModeShipwrecked() then
		local drop1 = SpawnPrefab("bell_blueprint")
		if drop1 then drop1.Transform:SetPosition(pt.x, pt.y, pt.z) end
	end
	local drop2 = SpawnPrefab("strawberry")
	drop2.Transform:SetPosition(pt.x, pt.y, pt.z)
	local house = TheSim:FindFirstEntityWithTag("yiyu_cathouse")
	house.cat_out = 2
end

local function RetargetFn(inst)
    local defenseTarget = inst
    local home = inst.components.homeseeker and inst.components.homeseeker.home
    if home and inst:GetDistanceSqToInst(home) < TUNING.MERM_DEFEND_DIST*TUNING.MERM_DEFEND_DIST then
        defenseTarget = home
    end
    local invader = FindEntity(defenseTarget or inst, TUNING.MERM_TARGET_DIST, function(guy)
        return guy:HasTag("player")
    end)
    return invader
end

local function KeepTargetFn(inst, target)
    local home = inst.components.homeseeker and inst.components.homeseeker.home
    if home then
        return home:GetDistanceSqToInst(target) < TUNING.MERM_DEFEND_DIST*TUNING.MERM_DEFEND_DIST
               and home:GetDistanceSqToInst(inst) < TUNING.MERM_DEFEND_DIST*TUNING.MERM_DEFEND_DIST
    end
    return inst.components.combat:CanTarget(target)     
end

local function OnAttacked(inst, data)
    local attacker = data and data.attacker
    if attacker and inst.components.combat:CanTarget(attacker) then
        inst.components.combat:SetTarget(attacker)
        local targetshares = MAX_TARGET_SHARES
        if inst.components.homeseeker and inst.components.homeseeker.home then
            local home = inst.components.homeseeker.home
            if home and home.components.childspawner and inst:GetDistanceSqToInst(home) <= SHARE_TARGET_DIST*SHARE_TARGET_DIST then
                targetshares = targetshares - home.components.childspawner.childreninside
                home.components.childspawner:ReleaseAllChildren(attacker)
            end
            inst.components.combat:ShareTarget(attacker, SHARE_TARGET_DIST, function(dude)
                return dude.components.homeseeker
                       and dude.components.homeseeker.home
                       and dude.components.homeseeker.home == home
            end, targetshares)
        end
    end
end

local function ontimerdone(inst, data)
    if data.name == "POOP" then
        inst.CanPoop = true
    end
    if data.name == "LONG" then
        inst.CanLong = true
    end
    if data.name == "BIG" then
        inst.CanBig = true
    end
    if data.name == "BALL" then
        inst.CanBall = true
    end
    if data.name == "FIRE" then
        inst.CanFire = true
    end
    if data.name == "THUNDER" then
        inst.CanThunder = true
    end
end

local function fn()
	local inst = CreateEntity()
	local trans = inst.entity:AddTransform()
	local anim = inst.entity:AddAnimState()
	local sound = inst.entity:AddSoundEmitter()
	local shadow = inst.entity:AddDynamicShadow()
	shadow:SetSize( 1.5, .75 )
	trans:SetScale(3.0, 3.0, 3.0)
    inst.Transform:SetFourFaced()
    MakeCharacterPhysics(inst, 50, .5)

    anim:SetBank("yiyu_cat")
    anim:SetBuild("yiyu_cat")
    
    inst:AddComponent("locomotor")
	inst.components.locomotor.walkspeed = 1.5
	inst.components.locomotor.runspeed = 3

	inst:AddComponent("named")
	inst.components.named:SetName(STRINGS.NAMES.YIYU_CAT)

    inst:SetStateGraph("SGyiyu_cat")
    local brain = require "brains/yiyu_catbrain"
    inst:SetBrain(brain)

	inst:AddComponent("combat")
--	inst.components.combat.hiteffectsymbol = "pig_torso"
	inst.components.combat:SetAttackPeriod(2)
	inst.components.combat:SetRetargetFunction(1, RetargetFn)
	inst.components.combat:SetKeepTargetFunction(KeepTargetFn)

	inst:AddComponent("health")
    inst.components.health:SetMaxHealth(999)
    inst.components.combat:SetDefaultDamage(1)
    inst.components.combat:SetAttackPeriod(2)
	local difficulty = (TUNING.DIFFICULTY-0.5)*10--0.05-0.2
	inst.components.health.absorb = 0.75 + difficulty/100

    inst:AddComponent("talker")
    inst.components.talker.fontsize = 32
    inst.components.talker.colour = Vector3(1, 0.5, 1, 1)

--	inst:AddComponent("lootdropper")
--	inst.components.lootdropper:SetLoot(loot)

    inst:AddComponent("inventory")    
    inst:AddComponent("inspectable")
    inst:AddComponent("knownlocations")
    inst:ListenForEvent("attacked", OnAttacked)
	inst:ListenForEvent("death", ondeath)
    
    inst:AddComponent("timer")
    inst:ListenForEvent("timerdone", ontimerdone)
    inst.CanPoop = false
    inst.CanLong = false
    inst.CanBig = false
    inst.CanBall = true
    inst.CanFire = false
    inst.CanThunder = false

    return inst
end

return Prefab("forest/animals/yiyu_cat", fn, assets, prefabs) 
