예제 #1
0
 public override void Run(IReGoapAction <string, object> previous, IReGoapAction <string, object> next, ReGoapState <string, object> settings, ReGoapState <string, object> goalState, Action <IReGoapAction <string, object> > done,
                          Action <IReGoapAction <string, object> > fail)
 {
     base.Run(previous, next, settings, goalState, done, fail);
     model.movementSpeed = 10f;
     if (model.Target != null)
     {
         doneCallback(this);
     }
     else
     {
         failCallback(this);
     }
 }
예제 #2
0
 public bool IsGoal(ReGoapState <T, W> goal)
 {
     return(goalMergedWithWorld.Count <= 0);
 }
예제 #3
0
    public override void Run(IReGoapAction previous, IReGoapAction next, IReGoapActionSettings settings, ReGoapState goalState, Action <IReGoapAction> done, Action <IReGoapAction> fail)
    {
        base.Run(previous, next, settings, goalState, done, fail);
        this.settings = (AddResourceToBankSettings)settings;
        var bank = agent.GetMemory().GetWorldState().Get <Bank>("nearestBank");

        if (bank.AddResource(resourcesBag, ((AddResourceToBankSettings)settings).ResourceName))
        {
            done(this);
        }
        else
        {
            fail(this);
        }
    }
예제 #4
0
        public override void Run(IReGoapAction <string, object> previous, IReGoapAction <string, object> next, IReGoapActionSettings <string, object> settings, ReGoapState <string, object> goalState, Action <IReGoapAction <string, object> > done, Action <IReGoapAction <string, object> > fail)
        {
            base.Run(previous, next, settings, goalState, done, fail);
            this.settings = (AddResourceToBankSettings)settings;
            var bank = agent.GetMemory().GetWorldState().Get("nearestBank") as Bank;

            if (bank != null && bank.AddResource(resourcesBag, ((AddResourceToBankSettings)settings).ResourceName))
            {
                done(this);
            }
            else
            {
                fail(this);
            }
        }
예제 #5
0
        public override void Run(IReGoapAction <string, object> previous, IReGoapAction <string, object> next, ReGoapState <string, object> settings, ReGoapState <string, object> goalState, Action <IReGoapAction <string, object> > done, Action <IReGoapAction <string, object> > fail)
        {
            base.Run(previous, next, settings, goalState, done, fail);
            // do your own game logic here

            if (GetComponent <BuilderMemory>().remainingBlocks <= 0)
            {
                agent.GetMemory().GetWorldState().Set("BlockAvailable", false);
                failCallback(this);
            }
            else
            {
                GetComponent <BuilderMemory>().remainingBlocks--;
                doneCallback(this); // this will tell the ReGoapAgent that the action is successfully done and go ahead in the action plan
            }
        }
예제 #6
0
 public override void Run(IReGoapAction previous, IReGoapAction next, IReGoapActionSettings settings, ReGoapState goalState, Action <IReGoapAction> done, Action <IReGoapAction> fail)
 {
     base.Run(previous, next, settings, goalState, done, fail);
     SetNeededResources(settings);
     if (resource == null || resource.GetCapacity() < ResourcePerAction)
     {
         failCallback(this);
     }
     else
     {
         gatherCooldown = Time.time + TimeToGather;
     }
 }
예제 #7
0
        public IReGoapGoal <T, W> Plan(IReGoapAgent <T, W> agent, IReGoapGoal <T, W> blacklistGoal = null, Queue <ReGoapActionState <T, W> > currentPlan = null, Action <IReGoapGoal <T, W> > callback = null)
        {
            if (ReGoapLogger.Level == ReGoapLogger.DebugLevel.Full)
            {
                ReGoapLogger.Log("[ReGoalPlanner] Starting planning calculation for agent: " + agent);
            }
            goapAgent   = agent;
            Calculated  = false;
            currentGoal = null;
            var possibleGoals = new List <IReGoapGoal <T, W> >();

            foreach (var goal in goapAgent.GetGoalsSet())
            {
                if (goal == blacklistGoal)
                {
                    continue;
                }
                goal.Precalculations(this);
                if (goal.IsGoalPossible())
                {
                    possibleGoals.Add(goal);
                }
            }
            possibleGoals.Sort((x, y) => x.GetPriority().CompareTo(y.GetPriority()));

            var currentState = agent.GetMemory().GetWorldState();

            while (possibleGoals.Count > 0)
            {
                currentGoal = possibleGoals[possibleGoals.Count - 1];
                possibleGoals.RemoveAt(possibleGoals.Count - 1);
                var goalState = currentGoal.GetGoalState();

                // can't work with dynamic actions, of course
                if (!settings.UsingDynamicActions)
                {
                    var wantedGoalCheck = currentGoal.GetGoalState();
                    GoapActionStackData <T, W> stackData;
                    stackData.agent        = goapAgent;
                    stackData.currentState = currentState;
                    stackData.goalState    = goalState;
                    stackData.next         = null;
                    stackData.settings     = null;
                    // we check if the goal can be archived through actions first, so we don't brute force it with A* if we can't
                    foreach (var action in goapAgent.GetActionsSet())
                    {
                        action.Precalculations(stackData);
                        if (!action.CheckProceduralCondition(stackData))
                        {
                            continue;
                        }
                        // check if the effects of all actions can archieve currentGoal
                        var previous = wantedGoalCheck;
                        wantedGoalCheck = ReGoapState <T, W> .Instantiate();

                        previous.MissingDifference(action.GetEffects(stackData), ref wantedGoalCheck);
                    }
                    // finally push the current world state
                    var current = wantedGoalCheck;
                    wantedGoalCheck = ReGoapState <T, W> .Instantiate();

                    current.MissingDifference(GetCurrentAgent().GetMemory().GetWorldState(), ref wantedGoalCheck);
                    // can't validate goal
                    if (wantedGoalCheck.Count > 0)
                    {
                        currentGoal = null;
                        continue;
                    }
                }

                goalState = goalState.Clone();
                var leaf = (ReGoapNode <T, W>)astar.Run(
                    ReGoapNode <T, W> .Instantiate(this, goalState, null, null, null), goalState, settings.MaxIterations, settings.PlanningEarlyExit);
                if (leaf == null)
                {
                    currentGoal = null;
                    continue;
                }

                var result = leaf.CalculatePath();
                if (currentPlan != null && currentPlan == result)
                {
                    currentGoal = null;
                    break;
                }
                if (result.Count == 0)
                {
                    currentGoal = null;
                    continue;
                }
                currentGoal.SetPlan(result);
                break;
            }
            Calculated = true;

            if (callback != null)
            {
                callback(currentGoal);
            }
            if (currentGoal != null)
            {
                ReGoapLogger.Log(string.Format("[ReGoapPlanner] Calculated plan for goal '{0}', plan length: {1}", currentGoal, currentGoal.GetPlan().Count));
                if (ReGoapLogger.Level == ReGoapLogger.DebugLevel.Full)
                {
                    int i = 0;
                    GoapActionStackData <T, W> stackData;
                    stackData.agent        = agent;
                    stackData.currentState = currentState;
                    stackData.goalState    = currentGoal.GetGoalState();
                    stackData.next         = null;
                    foreach (var action in currentGoal.GetPlan())
                    {
                        stackData.settings = action.Settings;
                        ReGoapLogger.Log(string.Format("[ReGoapPlanner] {0}) {1}", i++, action.Action.ToString(stackData)));
                    }
                }
            }
            else
            {
                ReGoapLogger.LogWarning("[ReGoapPlanner] Error while calculating plan.");
            }
            return(currentGoal);
        }
예제 #8
0
 public override ReGoapState <string, object> GetPreconditions(ReGoapState <string, object> goalState, IReGoapAction <string, object> next = null)
 {
     preconditions.Set("isAtPosition", agent.GetMemory().GetWorldState().Get("nearestWorkstationPosition") as Vector3?);
     return(preconditions);
 }
예제 #9
0
 protected virtual void Awake()
 {
     reGoapState = ReGoapState.Instantiate();
 }
예제 #10
0
 public void SetPreconditions(ReGoapState <string, object> preconditions)
 {
     this.preconditions = preconditions;
 }
예제 #11
0
 public override void Run(IReGoapAction <string, object> previous, IReGoapAction <string, object> next, ReGoapState <string, object> settings, ReGoapState <string, object> goalState, Action <IReGoapAction <string, object> > done,
                          Action <IReGoapAction <string, object> > fail)
 {
     base.Run(previous, next, settings, goalState, done, fail);
     Debug.Log("Find a Tree");
     transform.position = treePos.position;
     StartCoroutine(WaitASec());
 }
예제 #12
0
 public void SetEffects(ReGoapState <string, object> effects)
 {
     this.effects = effects;
 }
예제 #13
0
 public override void Run(IReGoapAction <string, object> previous, IReGoapAction <string, object> next, ReGoapState <string, object> settings, ReGoapState <string, object> goalState, Action <IReGoapAction <string, object> > done,
                          Action <IReGoapAction <string, object> > fail)
 {
     base.Run(previous, next, settings, goalState, done, fail);
     print("grass");
     doneCallback(this);
 }
예제 #14
0
 protected virtual void Awake()
 {
     goal = new ReGoapState();
 }
예제 #15
0
 public virtual bool CheckProceduralCondition(IReGoapAgent goapAgent, ReGoapState goalState, IReGoapAction next = null)
 {
     return(true);
 }
예제 #16
0
        public override void Run(IReGoapAction <string, object> previous, IReGoapAction <string, object> next, ReGoapState <string, object> settings, ReGoapState <string, object> goalState, Action <IReGoapAction <string, object> > done, Action <IReGoapAction <string, object> > fail)
        {
            base.Run(previous, next, settings, goalState, done, fail);

            var thisSettings = settings;

            resourcePosition = (Vector3)thisSettings.Get("ResourcePosition");
            resource         = (IResource)thisSettings.Get("Resource");

            if (resource == null || resource.GetCapacity() < ResourcePerAction)
            {
                failCallback(this);
            }
            else
            {
                gatherCooldown = Time.time + TimeToGather;
            }
        }
예제 #17
0
 public override bool CheckProceduralCondition(IReGoapAgent goapAgent, ReGoapState goalState, IReGoapAction next = null)
 {
     return(base.CheckProceduralCondition(goapAgent, goalState) && bag != null);
 }
예제 #18
0
        public override void Run(IReGoapAction <string, object> previous, IReGoapAction <string, object> next, ReGoapState <string, object> settings, ReGoapState <string, object> goalState, Action <IReGoapAction <string, object> > done, Action <IReGoapAction <string, object> > fail)
        {
            base.Run(previous, next, settings, goalState, done, fail);
            var workstation = settings.Get("workstation") as Workstation;

            if (workstation != null && workstation.CraftResource(resourcesBag, recipe))
            {
                ReGoapLogger.Log("[CraftRecipeAction] crafted recipe " + recipe.GetCraftedResource());
                done(this);
            }
            else
            {
                fail(this);
            }
        }
예제 #19
0
    public override void Run(IReGoapAction <string, object> previous, IReGoapAction <string, object> next, ReGoapState <string, object> settings, ReGoapState <string, object> goalState, Action <IReGoapAction <string, object> > done, Action <IReGoapAction <string, object> > fail)
    {
        base.Run(previous, next, settings, goalState, done, fail);

        Vector3 inRangePosition = lastKnownPlayerPosition.position * .5f;

        blackboard.currentTarget = lastKnownPlayerPosition.position;
        if (blackboard.targetReachedStatus)
        {
            //Debug.Log("Chase success");
            done(this);
        }
        fail(this);
    }
예제 #20
0
 public virtual void Precalculations(IReGoapAgent goapAgent, ReGoapState goalState)
 {
     agent = goapAgent;
 }
예제 #21
0
 public override IReGoapActionSettings <string, object> GetSettings(IReGoapAgent <string, object> goapAgent, ReGoapState <string, object> goalState)
 {
     settings = null;
     foreach (var pair in goalState.GetValues())
     {
         if (pair.Key.StartsWith("collectedResource"))
         {
             var resourceName = pair.Key.Substring(17);
             settings = new AddResourceToBankSettings
             {
                 ResourceName = resourceName
             };
             break;
         }
     }
     return(settings);
 }
예제 #22
0
 public virtual IReGoapActionSettings GetSettings(IReGoapAgent goapAgent, ReGoapState goalState)
 {
     return(settings);
 }
예제 #23
0
 public virtual ReGoapState <T, W> InstantiateNewState()
 {
     return(ReGoapState <T, W> .Instantiate());
 }
예제 #24
0
 public virtual ReGoapState GetPreconditions(ReGoapState goalState, IReGoapAction next = null)
 {
     return(preconditions);
 }
예제 #25
0
        public static ReGoapNode <T, W> Instantiate(IGoapPlanner <T, W> planner, ReGoapState <T, W> newGoal, ReGoapNode <T, W> parent, IReGoapAction <T, W> action, ReGoapState <T, W> actionSettings)
        {
            ReGoapNode <T, W> node;

            if (cachedNodes == null)
            {
                cachedNodes = new Stack <ReGoapNode <T, W> >();
            }
            lock (cachedNodes)
            {
                node = cachedNodes.Count > 0 ? cachedNodes.Pop() : new ReGoapNode <T, W>();
            }
            node.Init(planner, newGoal, parent, action, actionSettings);
            return(node);
        }
예제 #26
0
 public virtual ReGoapState GetEffects(ReGoapState goalState, IReGoapAction next = null)
 {
     return(effects);
 }
예제 #27
0
        private void Init(IGoapPlanner <T, W> planner, ReGoapState <T, W> newGoal, ReGoapNode <T, W> parent, IReGoapAction <T, W> action, ReGoapState <T, W> settings)
        {
            expandList.Clear();

            this.planner = planner;
            this.parent  = parent;
            this.action  = action;
            if (settings != null)
            {
                this.actionSettings = settings.Clone();
            }

            if (parent != null)
            {
                state = parent.GetState().Clone();
                // g(node)
                g = parent.GetPathCost();
            }
            else
            {
                state = planner.GetCurrentAgent().GetMemory().GetWorldState().Clone();
            }

            var nextAction = parent == null ? null : parent.action;

            if (action != null)
            {
                // create a new instance of the goal based on the paren't goal
                Goal = ReGoapState <T, W> .Instantiate(newGoal);

                GoapActionStackData <T, W> stackData;
                stackData.currentState = state;
                stackData.goalState    = Goal;
                stackData.next         = action;
                stackData.agent        = planner.GetCurrentAgent();
                stackData.settings     = actionSettings;

                Preconditions = action.GetPreconditions(stackData);
                Effects       = action.GetEffects(stackData);
                // addding the action's cost to the node's total cost
                g += action.GetCost(stackData);

                // adding the action's effects to the current node's state
                state.AddFromState(Effects);

                // removes from goal all the conditions that are now fullfiled in the action's effects
                Goal.ReplaceWithMissingDifference(Effects);
                // add all preconditions of the current action to the goal
                Goal.AddFromState(Preconditions);
            }
            else
            {
                Goal = newGoal;
            }
            h = Goal.Count;
            // f(node) = g(node) + h(node)
            cost = g + h * heuristicMultiplier;

            // additionally calculate the goal without any world effect to understand if we are done
            var diff = ReGoapState <T, W> .Instantiate();

            Goal.MissingDifference(planner.GetCurrentAgent().GetMemory().GetWorldState(), ref diff);
            goalMergedWithWorld = diff;
        }
예제 #28
0
 public virtual float GetCost(ReGoapState goalState, IReGoapAction next = null)
 {
     return(Cost);
 }
예제 #29
0
 public override void Run(IReGoapAction <string, object> previous, IReGoapAction <string, object> next, ReGoapState <string, object> settings, ReGoapState <string, object> goalState, Action <IReGoapAction <string, object> > done, Action <IReGoapAction <string, object> > fail)
 {
     base.Run(previous, next, settings, goalState, done, fail);
     // do your own game logic here
     Debug.Log("BLOCK PLACED");
     // when done, in this function or outside this function, call the done or fail callback, automatically saved to doneCallback and failCallback by ReGoapAction
     doneCallback(this); // this will tell the ReGoapAgent that the action is successfully done and go ahead in the action plan
     // if the action has failed then run failCallback(this), the ReGoapAgent will automatically invalidate the whole plan and ask the ReGoapPlannerManager to create a new plan
 }
예제 #30
0
        public override void Run(IReGoapAction <string, object> previous, IReGoapAction <string, object> next, ReGoapState <string, object> settings, ReGoapState <string, object> goalState, Action <IReGoapAction <string, object> > done,
                                 Action <IReGoapAction <string, object> > fail)
        {
            base.Run(previous, next, settings, goalState, done, fail);
            spinner.movementSpeed = 0f;
            Health health = spinner.Target.GetComponent <Health>();

            while (health.Amount != 0)
            {
                health.Change(-damageOverTime * Time.deltaTime, this.gameObject);
                doneCallback(this);
            }


            health.OnDeathEvent += SpawnSoul;
        }