Beispiel #1
0
 public PlannerNode(PlannerNode copyOf)
 {
     Parent      = copyOf.Parent;
     RunningCost = copyOf.RunningCost;
     RunningData = copyOf.RunningData;
     Action      = copyOf.Action;
 }
Beispiel #2
0
 public BaseSubject()
 {
     Data = new SubjectData()
     {
         States    = new List <State>(),
         Resources = new Dictionary <string, float>()
     };
 }
Beispiel #3
0
 public override void Apply(ref SubjectData actorData, ref SubjectData subjectData)
 {
     if (StateChange == StateChange.Gain && actorData.States.FirstOrDefault(s => s.GetType() == State.GetType()) == null)
         actorData.States.Add(State);
     else if (StateChange == StateChange.Lose)
     {
         actorData.States.Remove(actorData.States.FirstOrDefault(s => s.GetType() == State.GetType()));
     }
 }
Beispiel #4
0
        public virtual void Exit(Actor actor)
        {
            //  TODO aherrera : DUMMY! HE'S GOT A KNIFE.
            var dummySubject = new SubjectData();

            foreach (var result in Results)
            {
                result.Apply(ref actor.Data, ref dummySubject);
            }
        }
Beispiel #5
0
 public override void Apply(ref SubjectData actorData, ref SubjectData subjectData)
 {
     if (actorData.Resources.ContainsKey(Resource.Name))
     {
         actorData.Resources[Resource.Name] += Amount;
     }
     else
     {
         actorData.Resources.Add(Resource.Name, Amount);
     }
 }
Beispiel #6
0
        /// <summary>
        /// Test if given conditions are met based on given data.
        /// </summary>
        /// <param name="toTest"></param>
        /// <param name="currentData"></param>
        /// <returns></returns>
        private static bool IsConditionsMet(List <BaseCondition> toTest, SubjectData currentData)
        {
            bool allMet = true;// toTest.Count > 0;

            foreach (var condition in toTest)
            {
                allMet &= condition.IsMet(currentData);
            }

            return(allMet);
        }
Beispiel #7
0
        /// <summary>
        /// Copy the results of the action to simulate plan flow
        /// </summary>
        /// <param name="runningData"></param>
        /// <param name="actionResults"></param>
        /// <returns></returns>
        private static SubjectData CopyActionResults(SubjectData runningData, List <BaseResult> actionResults)
        {
            var newData = new SubjectData()
            {
                Resources = new Dictionary <string, float>(runningData.Resources),
                States    = new List <State>(runningData.States)
            };

            var subjectDummyData = new SubjectData();

            foreach (var result in actionResults)
            {
                //  TODO aherrera : we should be able to apply results onto a subject -- but not necessary
                result.Apply(ref newData, ref subjectDummyData);
            }

            return(newData);
        }
Beispiel #8
0
        public override bool IsMet(SubjectData data)
        {
            var agentResource = data.Resources.FirstOrDefault(r => r.Key == Resource.Name).Value;

            switch (Comparison)
            {
            case Comparison.Less:
                return(agentResource < Value);

            case Comparison.LessOrEqual:
                return(agentResource < Value || agentResource.Equals(Value));

            case Comparison.Equal:
                return(agentResource.Equals(Value));

            case Comparison.MoreOrEqual:
                return(agentResource > Value || agentResource.Equals(Value));

            case Comparison.More:
                return(agentResource > Value);
            }
            return(false);
        }
Beispiel #9
0
        /// <summary>
        /// Based on the given parent, we construct a graph branching from it.
        /// Returns true if ANY plan was found.
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="graphRef"></param>
        /// <param name="goal"></param>
        /// <param name="performableActions"></param>
        /// <returns></returns>
        private static bool BuildGraph(PlannerNode parent, List <PlannerNode> graphRef, Goal goal, List <BaseAction> performableActions)
        {
            bool planFound = false;

            foreach (var action in performableActions)
            {
                PlannerNode actualBatman = new PlannerNode(parent);

                if (IsConditionsMet(action.Conditions, actualBatman.RunningData))
                {
                    //  Apply the results of the action as if completed successfully.
                    SubjectData resultantData = CopyActionResults(actualBatman.RunningData, action.Results);
                    PlannerNode node          = new PlannerNode()
                    {
                        Parent      = actualBatman,
                        RunningCost = actualBatman.RunningCost + (int)action.Difficulty,
                        RunningData = resultantData,
                        Action      = action
                    };

                    if (IsConditionsMet(goal.DesiredConditions, resultantData))
                    {
                        //  A solution has been constructed;
                        graphRef.Add(node);
                        planFound = true;
                    }
                    else
                    {
                        //  Solution not yet found, test remaining actions
                        List <BaseAction> subsetActions = CreateActionSubset(performableActions, action);
                        planFound = BuildGraph(node, graphRef, goal, subsetActions);
                    }
                }
            }

            return(planFound);
        }
Beispiel #10
0
 public virtual bool IsMet(SubjectData data)
 {
     return(true);
 }
Beispiel #11
0
 public abstract void Apply(ref SubjectData actorData, ref SubjectData subjectData);
Beispiel #12
0
 public override bool IsMet(SubjectData data)
 {
     return(data.States.FirstOrDefault(s => s.GetType() == State.GetType()) != null);
 }