Jump to content

Extending And modifying Brain


XainFaith

Recommended Posts

So as the title states i am extending and modifing a brain, this is specific to the rabbit brain.

 

Now here is my issue i have extneded and modifyed the Behvioural tree of the Rabbit Brain and i know the parts of the BT i added work. I have it so that the rabbit can become a follow when feed a carrot from a safe distance and it even follows you. The problem is it persistantly runs away from you as in " fleeing " even when i had a IfNode to prevent default behaviour from occuring when it was following the player.

 

So my question is is there a component or something that is attached to the rabbit prefab itself ( iv looked nothing obveious pops out ) that cuases this behaviour.

 

Post Brain Processing code below:

---- Created by IntelliJ IDEA.-- User: XainFaith-- Date: 20/10/13-- Time: 11:39 PM-- To change this template use File | Settings | File Templates.--require "behaviours/wander"require "behaviours/runaway"require "behaviours/doaction"require "behaviours/panic"require "behaviours/follow"local STOP_RUN_DIST = 10local SEE_PLAYER_DIST = 5local AVOID_PLAYER_DIST = 3local AVOID_PLAYER_STOP = 6local SEE_BAIT_DIST = 20local MAX_WANDER_DIST = 20local MIN_FOLLOW_DIST = 2local MAX_FOLLOW_DIST = 9local TARGET_FOLLOW_DIST = 5Saved_OnStartBase = nilSaved_EatFoodAction = nilprint("Post Processing Rabbit")---Get the rabbits brain ---local RabbitBrain = require "brains.rabbitbrain"---Save off the old on start function so we can call it in case we do not need the custom functionalitySaved_OnStartBase = RabbitBrain.OnStart---Save off the old EatFoodActionSaved_EatFoodAction = RabbitBrain.EatFoodActionlocal function GetLeader(inst)    return inst.components.follower.leaderendlocal function DebugGetLeader(inst)    if inst.components.follower.leader then        print("Has Leader")    else        print("Has no Leader")    end    return inst.components.follower.leaderendlocal function IsChalieAndLeader(inst)    if GetPlayer().prefab == "charlie" then        if GetLeader(inst) then            --print("Is Chalie and has leader")            return true        else            return false        end    else       return false    endendlocal function GoHomeAction(inst)    if inst.components.homeseeker and            inst.components.homeseeker.home and            inst.components.homeseeker.home:IsValid() and            inst.sg:HasStateTag("trapped") == false then        return BufferedAction(inst, inst.components.homeseeker.home, ACTIONS.GOHOME)    endendlocal function EatFoodAction(inst)    ---print("Custom Rabbit Food Action")    --- Check to see if custom logic applys if this is charlie    if GetPlayer().prefab == "charlie" then        --Check for carrot first then proceed with normal behaviour such as baited traps etc        local target = FindEntity(inst, SEE_BAIT_DIST,            function(item)                if inst.components.eater:CanEat(item) and item.components.bait and not item:HasTag("planted") and not (item.components.inventoryitem and item.components.inventoryitem:IsHeld()) then                    return true;                end                return false;            end        )        if target then            local act = BufferedAction(inst, target, ACTIONS.EAT)            act.validfn = function() return not (target.components.inventoryitem and target.components.inventoryitem:IsHeld()) end            act:AddSuccessAction(function()                local player = GetClosestInstWithTag("player", inst,SEE_BAIT_DIST)                if player then                    player.components.leader:AddFollower(inst)                    --TODO Add something better for time of loyalty                    if inst.components.follower then                        print("Added rabbit as follower")                        inst.components.follower:AddLoyaltyTime(target.components.edible:GetHunger() * TUNING.PIG_LOYALTY_PER_HUNGER)                    end                end            end)            return act        end    else        --- Call saved version of function as it does not apply to any other characters        Saved_EatFoodAction(self)    endendlocal function DumpBT(bnode, indent)    local s = ""    for i=1,indent do        s = s.."|   "    end    s = s..bnode.name    print(s)    if bnode.children then        for i,childnode in ipairs(bnode.children) do            DumpBT(childnode, indent+1)        end    endendfunction RabbitBrain:OnStart()    ---print("Custom Brain Start")    ---Call original on start code    --Saved_OnStartBase(self)    --- Check to see if custom logic applys if this is charlie    if GetPlayer().prefab == "charlie" then        local clock = GetClock()        --local OldRoot = self.bt.root        --self.bt = nil        local NewRoot = PriorityNode(            {                IfNode(function() return IsChalieAndLeader(self.inst) end,"ChaliesTheLeader",                    Follow(self.inst,GetLeader,MIN_FOLLOW_DIST, TARGET_FOLLOW_DIST, MAX_FOLLOW_DIST)),                IfNode(function() if GetPlayer().prefab == "charlie" then return true else return false end end,"IsChalie",                    DoAction(self.inst, EatFoodAction))           }            ,.5)        self.bt = BT(self.inst,NewRoot)        print("BT Dump Start")        DumpBT(self.bt.root,1)        print("BT Dump End")    endendreturn RabbitBrain

If anyone knows i would greatly appracete it.

 

Regards XainFaith

Link to comment
Share on other sites

  • Developer

So as the title states i am extending and modifing a brain, this is specific to the rabbit brain.

 

Now here is my issue i have extneded and modifyed the Behvioural tree of the Rabbit Brain and i know the parts of the BT i added work. I have it so that the rabbit can become a follow when feed a carrot from a safe distance and it even follows you. The problem is it persistantly runs away from you as in " fleeing " even when i had a IfNode to prevent default behaviour from occuring when it was following the player.

 

So my question is is there a component or something that is attached to the rabbit prefab itself ( iv looked nothing obveious pops out ) that cuases this behaviour.

 

Post Brain Processing code below:

---- Created by IntelliJ IDEA.-- User: XainFaith-- Date: 20/10/13-- Time: 11:39 PM-- To change this template use File | Settings | File Templates.--require "behaviours/wander"require "behaviours/runaway"require "behaviours/doaction"require "behaviours/panic"require "behaviours/follow"local STOP_RUN_DIST = 10local SEE_PLAYER_DIST = 5local AVOID_PLAYER_DIST = 3local AVOID_PLAYER_STOP = 6local SEE_BAIT_DIST = 20local MAX_WANDER_DIST = 20local MIN_FOLLOW_DIST = 2local MAX_FOLLOW_DIST = 9local TARGET_FOLLOW_DIST = 5Saved_OnStartBase = nilSaved_EatFoodAction = nilprint("Post Processing Rabbit")---Get the rabbits brain ---local RabbitBrain = require "brains.rabbitbrain"---Save off the old on start function so we can call it in case we do not need the custom functionalitySaved_OnStartBase = RabbitBrain.OnStart---Save off the old EatFoodActionSaved_EatFoodAction = RabbitBrain.EatFoodActionlocal function GetLeader(inst)    return inst.components.follower.leaderendlocal function DebugGetLeader(inst)    if inst.components.follower.leader then        print("Has Leader")    else        print("Has no Leader")    end    return inst.components.follower.leaderendlocal function IsChalieAndLeader(inst)    if GetPlayer().prefab == "charlie" then        if GetLeader(inst) then            --print("Is Chalie and has leader")            return true        else            return false        end    else       return false    endendlocal function GoHomeAction(inst)    if inst.components.homeseeker and            inst.components.homeseeker.home and            inst.components.homeseeker.home:IsValid() and            inst.sg:HasStateTag("trapped") == false then        return BufferedAction(inst, inst.components.homeseeker.home, ACTIONS.GOHOME)    endendlocal function EatFoodAction(inst)    ---print("Custom Rabbit Food Action")    --- Check to see if custom logic applys if this is charlie    if GetPlayer().prefab == "charlie" then        --Check for carrot first then proceed with normal behaviour such as baited traps etc        local target = FindEntity(inst, SEE_BAIT_DIST,            function(item)                if inst.components.eater:CanEat(item) and item.components.bait and not item:HasTag("planted") and not (item.components.inventoryitem and item.components.inventoryitem:IsHeld()) then                    return true;                end                return false;            end        )        if target then            local act = BufferedAction(inst, target, ACTIONS.EAT)            act.validfn = function() return not (target.components.inventoryitem and target.components.inventoryitem:IsHeld()) end            act:AddSuccessAction(function()                local player = GetClosestInstWithTag("player", inst,SEE_BAIT_DIST)                if player then                    player.components.leader:AddFollower(inst)                    --TODO Add something better for time of loyalty                    if inst.components.follower then                        print("Added rabbit as follower")                        inst.components.follower:AddLoyaltyTime(target.components.edible:GetHunger() * TUNING.PIG_LOYALTY_PER_HUNGER)                    end                end            end)            return act        end    else        --- Call saved version of function as it does not apply to any other characters        Saved_EatFoodAction(self)    endendlocal function DumpBT(bnode, indent)    local s = ""    for i=1,indent do        s = s.."|   "    end    s = s..bnode.name    print(s)    if bnode.children then        for i,childnode in ipairs(bnode.children) do            DumpBT(childnode, indent+1)        end    endendfunction RabbitBrain:OnStart()    ---print("Custom Brain Start")    ---Call original on start code    --Saved_OnStartBase(self)    --- Check to see if custom logic applys if this is charlie    if GetPlayer().prefab == "charlie" then        local clock = GetClock()        --local OldRoot = self.bt.root        --self.bt = nil        local NewRoot = PriorityNode(            {                IfNode(function() return IsChalieAndLeader(self.inst) end,"ChaliesTheLeader",                    Follow(self.inst,GetLeader,MIN_FOLLOW_DIST, TARGET_FOLLOW_DIST, MAX_FOLLOW_DIST)),                IfNode(function() if GetPlayer().prefab == "charlie" then return true else return false end end,"IsChalie",                    DoAction(self.inst, EatFoodAction))           }            ,.5)        self.bt = BT(self.inst,NewRoot)        print("BT Dump Start")        DumpBT(self.bt.root,1)        print("BT Dump End")    endendreturn RabbitBrain

If anyone knows i would greatly appracete it.

 

Regards XainFaith

Running away happens in the stategraph 'run' state so what I would typically do is add a 'print(debug.traceback())' call to the 'onenter' function in the 'run' state which should tell you what triggered the rabbit to start running.

Link to comment
Share on other sites

Running away happens in the stategraph 'run' state so what I would typically do is add a 'print(debug.traceback)' call to the 'onenter' function in the 'run' state which should tell you what triggered the rabbit to start running.

print(debug.traceback())
:razz:
Link to comment
Share on other sites

Thanks for that information turns out the trace back was not as usfull as i would of hoped.. just noted that it was being called from the beahviour trees update etc.  But i determined in the long run that its acctually the rabbits running that causes the behaviour if i set the last param node for the follow node to be false eg( can run ) it very slowly does follow you without running away. A bit stumped as to how to fix this while still maintaing compatability at this moment. But at the very least i know roughly what is causing the behaviour.

 

Regards XainFaith

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...