Jump to content

Bats not being agressive to a character


Recommended Posts

Hello modders!

I want a character to not be attacked by batilisks, unless attacking them first.
My current function though makes the client lose the connection to the server.
The game does not crash, with an error log. But the player gets disconnected, so I think the server is stuck in an infinite loop or something.

I tried to use a snippet from "Womp, the Abyssal" as a template, as this character won't be targeted by tentacles.

-- Override tentacle's targeting so they won't target us unless provoked
local function retargetfn_Reg(inst) 
    return GLOBAL.FindEntity(
        inst,
        TUNING.TENTACLE_ATTACK_DIST,
        function(guy) 
            return guy.prefab ~= inst.prefab
                and guy.entity:IsVisible()
                and not guy.components.health:IsDead()
                and (guy.components.combat.target == inst or
                    guy:HasTag("character") or
                    guy:HasTag("monster") or
                    guy:HasTag("animal"))
        end,
        { "_combat", "_health" },
        { "prey", "swampbro" })
end
-- Remove sanity aura when near tentacles.
local function CalcSanityAura(inst, observer)
    return observer:HasTag("swampbro") and 0 or -TUNING.SANITYAURA_MED
end
AddPrefabPostInit("tentacle", function(inst)
    if GLOBAL.TheWorld.ismastersim then
        inst.components.combat:SetRetargetFunction(GLOBAL.GetRandomWithVariance(2, 0.5), retargetfn_Reg)
		inst.components.sanityaura.aurafn = CalcSanityAura
    end
	
	inst:AddTag("tentacle")
end)

The "swambro" tag is exclusive to Womp, so I guess, the list of tags in the 2nd bracket here won't be targetted.
I don't actually understand, why the retarget function is set with an additional randomiser. Maybe that is normal or it might be tentacle-specific.
 

inst.components.combat:SetRetargetFunction(GLOBAL.GetRandomWithVariance(2, 0.5), retargetfn_Reg)


Anyways these are the functions in bat.lua regarding targetting.
 

local function MakeTeam(inst, attacker)
    local leader = SpawnPrefab("teamleader")
    leader.components.teamleader:SetUp(attacker, inst)
    leader.components.teamleader:BroadcastDistress(inst)
end

local function Retarget(inst)
    local ta = inst.components.teamattacker

    local newtarget = FindEntity(inst, TUNING.BAT_TARGET_DIST, function(guy)
            return inst.components.combat:CanTarget(guy)
        end,
        nil,
        {"bat"},
        {"character", "monster"}
    )

    if newtarget and not ta.inteam and not ta:SearchForTeam() then
        MakeTeam(inst, newtarget)
    end

    if ta.inteam and not ta.teamleader:CanAttack() then
        return newtarget
    end
end

local function KeepTarget(inst, target)
    return (inst.components.teamattacker.inteam and not inst.components.teamattacker.teamleader:CanAttack())
        or inst.components.teamattacker.orders == ORDERS.ATTACK
end

I tried to add the "batwhisperer" tag to the list of excluded tags - which here seem to be the first bracket after nil.
But apparently something else leads to a deadlock / loop or I simply used AddPrefabPostInit wrong.

 

-- Override bat's targeting so they won't target us unless provoked
local function Retarget(inst)
    local ta = inst.components.teamattacker

    local newtarget = GLOBAL.FindEntity(inst, TUNING.BAT_TARGET_DIST, function(guy)
            return inst.components.combat:CanTarget(guy)
        end,
        nil,
        {"bat", "batwhisperer"},
        {"character", "monster"}
    )

    if newtarget and not ta.inteam and not ta:SearchForTeam() then
        MakeTeam(inst, newtarget)
    end

    if ta.inteam and not ta.teamleader:CanAttack() then
        return newtarget
    end
end

-- Applying the Retarget function to the Bat prefab file.
AddPrefabPostInit("bat", function(inst)
    if GLOBAL.TheWorld.ismastersim then
        inst.components.combat:SetRetargetFunction(GLOBAL.GetRandomWithVariance(2, 0.5), Retarget)
    end
	
	inst:AddTag("bat")
end)

 


Can you spot the error and fix it please?
Also I would be very happy if you could explain what caused this behaviour and why a particular piece of code had to be different.
Many thanks.

Link to comment
Share on other sites

While this is true, I would prefer the solution that changes the behaviour of the batilisks, since giving the character the bat tag might have side-effects with other mods that effect bat-tagged things.
Also, I want to give another character the perk to not be attacked by wild hounds. (At least not the ones in the desert - not sure on the periodic scripted attacks) and I guess I will have pretty much the same problem there.

Link to comment
Share on other sites

I'd argue to not alter ingame code unless you absolutely needed to, as it could cause issues with other mods that actually need to.

As for hounds, giving yourself the hound or houndfriend tag will prevent hounds from attacking, however hounds from the hound wave will aggro you (hounded component forces you to be their target), but will lose aggro on you after they taunt or bite you.

As for the code not working, try renaming the Retarget function to a unique name? 

 

Edited by DarkKingBoo
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...