예제 #1
0
 //Called by the planner when the action is being considered. This function is used to receive the iGoap and current state
 //variables.
 public override void OnActionSetup(IGoap iGoap, List <Condition> state)
 {
     entity = (GOAP_Agent)iGoap;
     target = entity.PatrolPoint;
     UpdatePrecondition("CloseTo", target);
     isViable = (target != null);
 }
예제 #2
0
    public override void OnActionSetup(IGoap igoap, List <Condition> state)
    {
        entity = (Entity)igoap;

        // Only set the action to viable if the entity has any food
        isViable = entity.amountOfFood > 0;
    }
예제 #3
0
        /// <summary>
        /// Formulates a plan based on the given actions and goals
        /// </summary>
        /// <param name="actions">List of available actions</param>
        /// <param name="goals">List of available goals, these will be sorted by priority</param>
        /// <param name="currentGoal">If a plan is found, this will be the current goal</param>
        /// <returns>If a plan was found, a list of actions. Otherwise null</returns>
        public static List <Action> FormulatePlan(IGoap igoap, List <Action> actions, List <Goal> goals, out Goal currentGoal, DebugPlanning debug = DebugPlanning.None)
        {
            currentGoal = null;
            if (igoap == null || actions == null || actions.Count == 0 || goals == null || goals.Count == 0)
            {
                if (igoap == null)
                {
                    DebugText(debug, DebugPlanning.Basics | DebugPlanning.Everything, "Can't formulate a plan because no suitable iGoap was found.");
                }
                if (actions == null || actions.Count == 0)
                {
                    DebugText(debug, DebugPlanning.Basics | DebugPlanning.Everything, "Can't formulate a plan because no suitable actions for iGoap were found.");
                }
                if (goals == null || goals.Count == 0)
                {
                    DebugText(debug, DebugPlanning.Basics | DebugPlanning.Everything, "Can't formulate a plan because no suitable goals for iGoap were found.");
                }

                return(null);
            }

            foreach (Goal goal in goals.OrderByDescending(g => g.priority).ToList())
            {
                List <Action> plan = FormulatePlan(igoap, actions, goal, out currentGoal, debug);

                if (plan != null)
                {
                    return(plan);
                }
            }

            return(null);
        }
예제 #4
0
 public PerformActionState(BoolDelegate _hasActionPlan, IGoap _goapData, StateMachine _controller, Queue <GoapAction> _currentActions, GameObject _agent)
 {
     currentActions = _currentActions;
     hasActionPlan  = _hasActionPlan;
     goapData       = _goapData;
     controller     = _controller;
     agent          = _agent;
 }
예제 #5
0
    public override void OnActionSetup(IGoap igoap, List <Condition> state)
    {
        entity = (GOAP_Agent)igoap;
        Transform alarm_point = entity.Alarm;

        UpdatePrecondition("CloseTo", alarm_point);
        isViable = (alarm_point != null);
    }
예제 #6
0
 private void FindDataProvider()
 {
     worldDataProvider = gameObject.GetComponent <IGoap>();
     if (worldDataProvider == null)
     {
         worldDataProvider = gameObject.AddComponent <VillagerRole>();
     }
     //
 }
예제 #7
0
 public virtual void Awake()
 {
     ourGoapAgent     = GetComponent <GoapAgent>();
     ourThreatTrigger = GetComponentInChildren <ThreatTrigger>();
     ourThreatTrigger.threatInArea += this.importantEventTriggered;
     gameStateHandler = GameObject.Find("Game State Handler").GetComponent <GameStateHandler>();
     enemySpawner     = GameObject.Find("EnemySpawner").GetComponent <EnemySpawner>();
     ourType          = GetComponent <IGoap>();
 }
예제 #8
0
    public GameObject GetClosestAlly(IGoap allyType, GameObject allySeeker)
    {
        GameObject potentialMate = null;

        //change this
        potentialMate = FindClosest.FindClosestObject(enemyDirectory[allyType.GetType()], allySeeker);

        return(potentialMate);
    }
예제 #9
0
파일: Brain.cs 프로젝트: zzq889527/goap
 public void Tick(IGoap goap)
 {
     _costTime += Time.deltaTime;
     if (_costTime >= 1)
     {
         Hunger   -= 2;
         Mind     -= 2;
         _costTime = 0;
     }
 }
예제 #10
0
 public IdleState(IGoap _goapData, StateMachine _controller, List <GoapAction> _availableActions, GoapPlanner _planner, GoapAgent _agent)
 {
     goapData         = _goapData;
     worldState       = goapData.GetWorldState();
     goal             = goapData.GetGoalState();
     controller       = _controller;
     planner          = _planner;
     agent            = _agent;
     availableActions = _availableActions;
 }
    public override void OnActionSetup(IGoap igoap, System.Collections.Generic.List <Condition> state)
    {
        entity = (Entity)igoap;

        // Get the closests carcass the entity knows off, and if it's not null the action is viable
        target = entity.closestsCarcass;
        UpdatePrecondition("inRangeOf", target);

        isViable = target != null;
    }
예제 #12
0
 public GoapAgent(List <GoapAction> _availableActions, StateMachine _stateMachine, IGoap _goapData, GameObject __agent)
 {
     availableActions = _availableActions;
     stateMachine     = _stateMachine;
     goapData         = _goapData;
     _agent           = __agent;
     planner          = new GoapPlanner();
     stateMachine.states.Add("perform_action_state", new PerformActionState(HasPlan, goapData, stateMachine, currentActions, agent));
     stateMachine.states.Add("idle", new IdleState(goapData, stateMachine, availableActions, planner, this));
     stateMachine.Change("idle");
 }
예제 #13
0
파일: AIAgent.cs 프로젝트: hsinpa/GOAP
 private void findDataProvider()
 {
     foreach (Component comp in gameObject.GetComponents(typeof(Component)))
     {
         if (typeof(IGoap).IsAssignableFrom(comp.GetType()))
         {
             dataProvider = (IGoap)comp;
             return;
         }
     }
 }
예제 #14
0
 private void FindAgent()
 {
     foreach (Component comp in gameObject.GetComponents <Component>())
     {
         if (typeof(IGoap).IsAssignableFrom(comp.GetType()))
         {
             agent = (IGoap)comp;
             return;
         }
     }
 }
예제 #15
0
    void Awake()
    {
        availableActions = new HashSet <GoapAction> ();
        currentPlan      = new Queue <GoapAction> ();
        planner          = new GoapPlanner();

        agentImplementation = GetComponent <IGoap>();
        FSM = GetComponent <Animator>();

        LoadActions();
    }
예제 #16
0
        public override void Update()
        {
            IGoap c = (IGoap)fsm.owner.controller;
            Queue <GOAP_action> plan = fsm.goaplanner.MakePlan(c.GetGoalsState(), c.GetWorldState(), c.GetActions());

            if (plan.Count > 0)
            {
                fsm.currentPlan = plan;
                fsm.PushState(new PerformActionState(fsm));
            }
        }
예제 #17
0
 public GoapAgent(IGoap provider, IAgentActions actions, IAgentControllerState state)
 {
     _dataProvider     = provider;
     ActionProvider    = actions;
     StateProvider     = state;
     _stateMachine     = new FSM();
     _availableActions = new HashSet <GoapAction>();
     _currentActions   = new Queue <GoapAction>();
     _planner          = new GoapPlanner();
     createIdleState();
     createMoveToState();
     createPerformActionState();
     _stateMachine.pushState(_idleState);
     loadActions();
 }
예제 #18
0
    //Called by the planner when the action is being considered. This function is used to receive the iGoap and current state
    //variables.
    public override void OnActionSetup(IGoap iGoap, List <Condition> state)
    {
        entity = (GOAP_Agent)iGoap;
        target = null;

        foreach (Condition c in state)
        {
            if (c.identifier == "CloseTo")
            {
                target = (Transform)c.value;
                break;
            }
        }

        UpdateEffect("CloseTo", target);
        isViable = (target != null);
    }
예제 #19
0
    public override void OnActionSetup(IGoap igoap, List <Condition> state)
    {
        entity = (Entity)igoap;

        // Loop through the conditions state, and see if we can find a condition called "inRangeOf"
        foreach (Condition c in state)
        {
            // If we find one, we use the value to set our target
            if (c.identifier == "inRangeOf")
            {
                target = (Transform)c.value;
                break;
            }
        }

        // Update the effect with the target, and make sure the action is viable based on the target
        UpdateEffect("inRangeOf", target);
        isViable = target != null;
    }
예제 #20
0
    private void FindDataProvider()
    {
        //foreach (Component comp in gameObject.GetComponents(typeof(Component)))
        //{
        //    if (typeof(IGoap).IsAssignableFrom(comp.GetType()))
        //    {
        //        dataProvider = (IGoap)comp;
        //        return;
        //    }
        //}

        foreach (Component comp in gameObject.GetComponents <Component>())
        {
            if (typeof(IGoap).IsAssignableFrom(comp.GetType()))
            {
                dataProvider = (IGoap)comp;
                return;
            }
        }
    }
예제 #21
0
        /// <summary>
        /// Formulates a plan based on the given actions and goal
        /// </summary>
        /// <param name="actions">List of available actions</param>
        /// <param name="goals">Available goal</param>
        /// <param name="currentGoal">If a plan is found, this will be the current goal</param>
        /// <returns>If a plan was found, a list of actions. Otherwise null</returns>
        public static List <Action> FormulatePlan(IGoap igoap, List <Action> actions, Goal goal, out Goal currentGoal, DebugPlanning debug = DebugPlanning.None)
        {
            currentGoal = goal;
            if (igoap == null || actions == null || actions.Count == 0 || goal == null)
            {
                if (igoap == null)
                {
                    DebugText(debug, DebugPlanning.Basics | DebugPlanning.Everything, "Can't formulate a plan because no suitable iGoap was found.");
                }
                if (actions == null || actions.Count == 0)
                {
                    DebugText(debug, DebugPlanning.Basics | DebugPlanning.Everything, "Can't formulate a plan because no suitable actions for iGoap were found.");
                }
                if (goal)
                {
                    DebugText(debug, DebugPlanning.Basics | DebugPlanning.Everything, "Can't formulate a plan because no suitable goal for iGoap was found.");
                }
            }

            DebugText(debug, DebugPlanning.Basics | DebugPlanning.Everything, "Formulating a plan for goal {0}.", goal.GetType());

            goal.OnGoalSetup();
            if (!goal.IsGoalRelevant())
            {
                DebugText(debug, DebugPlanning.Basics | DebugPlanning.Everything, "Goal {0} is not relevant.", goal.GetType());
                return(null);
            }

            List <Action> plan = FindSolution(igoap, actions, goal, debug);

            if (plan == null)
            {
                DebugText(debug, DebugPlanning.Basics | DebugPlanning.Everything, "Couldn't find a plan for goal {0}, returning null.", goal.GetType());
            }
            else
            {
                DebugText(debug, DebugPlanning.Basics | DebugPlanning.Everything, "Found a plan for goal {0} and returning it.", goal.GetType());
            }

            return(plan);
        }
예제 #22
0
파일: GoapPlanner.cs 프로젝트: Over42/uGOAP
    /**
     * Plan what sequence of actions can fulfill the goal.
     * Returns null if a plan could not be found, or a list of the actions
     * that must be performed, in order, to fulfill the goal.
     */
    public Queue <GoapAction> Plan(GameObject agent,
                                   HashSet <GoapAction> availableActions,
                                   Dictionary <string, object> worldState,
                                   KeyValuePair <string, object> goal,
                                   IGoap goap)
    {
        // reset the actions so we can start fresh with them
        foreach (GoapAction a in availableActions)
        {
            a.DoReset();
        }

        // check what actions can run using their checkProceduralPrecondition
        HashSet <GoapAction> usableActions = NodeManager.GetFreeActionSet();

        foreach (GoapAction a in availableActions)
        {
            if (a.CheckProceduralPrecondition(agent, goap.GetMemory()))
            {
                usableActions.Add(a);
            }
        }

        // we now have all actions that can run, stored in usableActions

        // build up the tree and record the leaf nodes that provide a solution to the goal.
        List <GoapNode> leaves = new List <GoapNode>();

        // build graph
        GoapNode start   = NodeManager.GetFreeNode(null, 0, worldState, null);
        bool     success = BuildGraph(start, leaves, usableActions, goal);

        if (!success)
        {
            // oh no, we didn't get a plan
            Debug.Log("[" + agent.name + "] " + "NO PLAN");
            return(null);
        }

        // get the cheapest leaf
        GoapNode cheapest = null;

        foreach (GoapNode leaf in leaves)
        {
            if (cheapest == null)
            {
                cheapest = leaf;
            }
            else
            {
                if (leaf.BetterThen(cheapest))
                {
                    cheapest = leaf;
                }
            }
        }

        // get its node and work back through the parents
        List <GoapAction> result = new List <GoapAction>();
        GoapNode          n      = cheapest;

        while (n != null)
        {
            if (n.action != null)
            {
                result.Insert(0, n.action); // insert the action in the front
            }
            n = n.parent;
        }
        NodeManager.Release();
        // we now have this action list in correct order

        Queue <GoapAction> queue = new Queue <GoapAction>();

        foreach (GoapAction a in result)
        {
            queue.Enqueue(a);
        }

        // Builds a shortest way for many actions
        //for (int i = 1; i < result.Count; i++)
        //{
        //    GoapAction a = result[i];
        //    a.CheckDistance(result[i-1].target, goap.GetMemory());
        //}

        // hooray we have a plan!
        return(queue);
    }
 // Should be called from the iGoap class. This initializes the goal class.
 // The reason a goal has a initialize function, and the actions don't is because action are
 // copied by the planner, while a goal remains the same throughout.
 public override void OnGoalInitialize(IGoap igoap)
 {
     this.igoap = igoap;
 }
예제 #24
0
 /// <summary>
 /// Get the <see cref="IGoap"/> component in this game object
 /// </summary>
 private void FindDataProvider()
 {
     _dataProvider = GetComponent <IGoap>();
 }
예제 #25
0
 public PerformActionAction(GoapAgent agent, IGoap dataProvider)
 {
     _agent        = agent;
     _dataProvider = dataProvider;
 }
예제 #26
0
	private void findDataProvider() {
		foreach (Component comp in gameObject.GetComponents(typeof(Component)) ) {
			if ( typeof(IGoap).IsAssignableFrom(comp.GetType()) ) {
				dataProvider = (IGoap)comp;
				return;
			}
		}
	}
예제 #27
0
 private void Awake()
 {
     igoap = GetComponent <IGoap>();
 }
예제 #28
0
 public virtual void OnActionSetup(IGoap igoap, List <Condition> state)
 {
     this.igoap = igoap;
 }
예제 #29
0
    /**
     * Plan what sequence of actions can fulfill the goal.
     * Returns null if a plan could not be found, or a list of the actions
     * that must be performed, in order, to fulfill the goal.
     * 计划可以实现目标的行动顺序。
     * 如果找不到计划,或者操作列表,则返回null
     * 必须按顺序执行才能实现目标。
     */

    ///<summary>
    ///计划
    ///</summary>
    public Queue <GoapAction> Plan(GameObject agent,
                                   HashSet <GoapAction> availableActions,
                                   Dictionary <string, bool> worldState,
                                   KeyValuePair <string, bool> goal,
                                   IGoap goap)
    {
        // reset the actions so we can start fresh with them
        //重置动作,以便我们可以重新开始
        foreach (var a in availableActions)
        {
            a.doReset();
        }

        // check what actions can run using their checkProceduralPrecondition
        //使用检查程序前置条件检查可以运行的操作
        var usableActions = NodeManager.GetFreeActionSet();

        foreach (var a in availableActions)
        {
            if (a.CheckProceduralPrecondition(agent, goap.GetBlackBoard()))
            {
                usableActions.Add(a);
            }
        }

        // we now have all actions that can run, stored in usableActions
        //我们现在拥有可以运行的所有操作,存储在usefulActions中
        // build up the tree and record the leaf nodes that provide a solution to the goal.
        //构建树并记录提供目标解决方案的节点。
        var leaves = new List <GoapNode>();

        // build graph
        //构建图表
        var start = NodeManager.GetFreeNode(null, 0, 0, worldState, null);
        //构建图
        var success = BuildGraph(start, leaves, usableActions, goal);

        if (!success)
        {
            // oh no, we didn't get a plan
            //不,我们没有得到一个计划
            //            Debug.Log("NO PLAN");
            return(null);
        }

        // get the cheapest leaf
        //得到最便宜的节点
        GoapNode cheapest = null;

        foreach (var leaf in leaves)
        {
            if (cheapest == null)
            {
                cheapest = leaf;
            }
            else
            {
                if (leaf.BetterThen(cheapest))
                {
                    cheapest = leaf;
                }
            }
        }

        // get its node and work back through the parents
        //获取其节点并通过父节点返回
        var result = new List <GoapAction>();
        var n      = cheapest;

        while (n != null)
        {
            if (n.action != null)
            {
                //在前面插入动作
                result.Insert(0, n.action); // insert the action in the front
            }
            n = n.parent;
        }

        NodeManager.Release();
        // we now have this action list in correct order
        //我们现在按正确的顺序拥有此操作列表
        var queue = new Queue <GoapAction>();

        foreach (var a in result)
        {
            queue.Enqueue(a);
        }

        // hooray we have a plan!
        //万岁,我们有一个计划!
        return(queue);
    }
예제 #30
0
 public PlanAction(GoapPlanner planner, IGoap dataProvider, GoapAgent agent)
 {
     _planner      = planner;
     _dataProvider = dataProvider;
     _agent        = agent;
 }
 void FindDataProvider() => _dataProvider = gameObject.GetComponent <IGoap>();