public Composite GenerateBehaviorTree()
        {
            if (!SingularSettings.Debug)
            {
                return(new PrioritySelector(blist.Select(b => b.behavior).ToArray()));
            }

            PrioritySelector pri = new PrioritySelector();

            foreach (PrioritizedBehavior pb in blist)
            {
                if (!SingularSettings.TraceHeals)
                {
                    pri.AddChild(pb.behavior);
                }
                else
                {
                    CallTrace ct = new CallTrace(pb.Name, pb.behavior);
                    ct.TraceActive = true;
                    ct.TraceEnter  = false;
                    ct.TraceExit   = true;
                    pri.AddChild(ct);
                }
            }

            return(pri);
        }
Esempio n. 2
0
        public Composite GenerateBehaviorTree()
        {
            if (!SingularSettings.Debug)
                return new PrioritySelector(blist.Select(b => b.behavior).ToArray());

            PrioritySelector pri = new PrioritySelector();
            foreach (PrioritizedBehavior pb in blist)
            {
                if (!SingularSettings.TraceHeals)
                    pri.AddChild(pb.behavior);
                else
                {
                    CallTrace ct = new CallTrace(pb.Name, pb.behavior);
                    ct.TraceActive = true;
                    ct.TraceEnter = false;
                    ct.TraceExit = true;
                    pri.AddChild( ct );
                }
            }

            return pri;
        }
        public static Composite GetComposite(WoWClass wowClass, WoWSpec spec, BehaviorType behavior, WoWContext context, out int behaviourCount, bool silent = false)
        {
            if (context == WoWContext.None)
            {
                // None is an invalid context, but rather than stopping bot wait it out with donothing logic
                Logger.Write(LogColor.Hilite, "No Active Context -{0}{1} for{2} set to DoNothingBehavior temporarily", wowClass.ToString().CamelToSpaced(), behavior.ToString().CamelToSpaced(), spec.ToString().CamelToSpaced());
                behaviourCount = 1;
                return(NoContextAvailable.CreateDoNothingBehavior());
            }

            SilentBehaviorCreation = silent;
            behaviourCount         = 0;
            var matchedMethods = new Dictionary <BehaviorAttribute, Composite>();

            foreach (MethodInfo mi in _methods)
            {
                // If the behavior is set as ignore. Don't use it? Duh?
                if (mi.GetCustomAttributes(typeof(IgnoreBehaviorCountAttribute), false).Any())
                {
                    continue;
                }

                // If there's no behavior attrib, then move along.
                foreach (var a in mi.GetCustomAttributes(typeof(BehaviorAttribute), false))
                {
                    var attribute = a as BehaviorAttribute;
                    if (attribute == null)
                    {
                        continue;
                    }

                    // Check if our behavior matches with what we want. If not, don't add it!
                    if (IsMatchingMethod(attribute, wowClass, spec, behavior, context))
                    {
                        if (!silent)
                        {
                            Logger.WriteFile("{0} {1} {2}", attribute.PriorityLevel.ToString().AlignRight(4), behavior.ToString().AlignLeft(15), mi.Name);
                        }

                        CurrentBehaviorType     = behavior;
                        CurrentBehaviorPriority = attribute.PriorityLevel;
                        CurrentBehaviorName     = mi.Name;

                        // if it blows up here, you defined a method with the exact same attribute and priority as one already found

                        Composite comp;
                        if (typeof(Composite).IsAssignableFrom(mi.ReturnType))
                        {
                            comp = mi.Invoke(null, null) as Composite;
                        }
                        else
                        {
                            Beta.Assert(mi.ReturnType == typeof(Task <bool>));
                            comp = new ActionRunCoroutine(o => mi.Invoke(null, null) as Task <bool>);
                        }

                        string name = behavior + "." + mi.Name + "." + attribute.PriorityLevel;

                        if (SingularSettings.Trace)
                        {
                            comp = new CallTrace(name, comp);
                        }

                        if (matchedMethods.ContainsKey(attribute))
                        {
                            Logger.Write(LogColor.Hilite, "Internal Error:  method '{0}' has attribute that already exists", name);
                            Logger.WriteDiagnostic(" ");
                            Logger.WriteDiagnostic("Dump Methods");
                            Logger.WriteDiagnostic("===========================");
                            foreach (var v in matchedMethods)
                            {
                                Logger.WriteDiagnostic("{0} {1} {2} {3}", v.Key.SpecificClass, v.Key.SpecificSpec, v.Key.SpecificContext, v.Key.PriorityLevel);
                            }
                            Logger.WriteDiagnostic("===========================");
                            Logger.WriteDiagnostic("{0} {1} {2} {3}  == add  attempt for {4}", attribute.SpecificClass, attribute.SpecificSpec, attribute.SpecificContext, attribute.PriorityLevel, name);
                            Logger.WriteDiagnostic(" ");
                        }

                        matchedMethods.Add(attribute, comp);

                        CurrentBehaviorType     = 0;
                        CurrentBehaviorPriority = 0;
                        CurrentBehaviorName     = string.Empty;
                    }
                }
            }
            // If we found no methods, rofls!
            if (matchedMethods.Count <= 0)
            {
                return(null);
            }

            var result = new PrioritySelector();

            foreach (var kvp in matchedMethods.OrderByDescending(mm => mm.Key.PriorityLevel))
            {
                result.AddChild(kvp.Value);
                behaviourCount++;
            }

            return(result);
        }
Esempio n. 4
0
        public static Composite GetComposite(WoWClass wowClass, WoWSpec spec, BehaviorType behavior, WoWContext context, out int behaviourCount, bool silent = false)
        {
            if (context == WoWContext.None)
            {
                // None is an invalid context, but rather than stopping bot wait it out with donothing logic
                Logger.Write( LogColor.Hilite, "No Active Context -{0}{1} for{2} set to DoNothingBehavior temporarily", wowClass.ToString().CamelToSpaced(), behavior.ToString().CamelToSpaced(), spec.ToString().CamelToSpaced());
                behaviourCount = 1;
                return NoContextAvailable.CreateDoNothingBehavior();
            }

            SilentBehaviorCreation = silent;
            behaviourCount = 0;
            var matchedMethods = new Dictionary<BehaviorAttribute, Composite>();

            foreach (MethodInfo mi in _methods)
            {
                // If the behavior is set as ignore. Don't use it? Duh?
                if (mi.GetCustomAttributes(typeof(IgnoreBehaviorCountAttribute), false).Any())
                    continue;

                // If there's no behavior attrib, then move along.
                foreach (var a in mi.GetCustomAttributes(typeof(BehaviorAttribute), false))
                {
                    var attribute = a as BehaviorAttribute;
                    if (attribute == null)
                        continue;

                    // Check if our behavior matches with what we want. If not, don't add it!
                    if (IsMatchingMethod(attribute, wowClass, spec, behavior, context))
                    {
                        if (!silent)
                            Logger.WriteFile("{0} {1} {2}", attribute.PriorityLevel.ToString().AlignRight(4), behavior.ToString().AlignLeft(15), mi.Name);

                        CurrentBehaviorType = behavior;
                        CurrentBehaviorPriority = attribute.PriorityLevel;
                        CurrentBehaviorName = mi.Name;

                        // if it blows up here, you defined a method with the exact same attribute and priority as one already found

                        // wrap in trace class
                        Composite comp = mi.Invoke(null, null) as Composite;
                        string name = behavior.ToString() + "." + mi.Name + "." + attribute.PriorityLevel.ToString();

                        if (SingularSettings.Trace)
                            comp = new CallTrace( name, comp);

                        if (matchedMethods.ContainsKey(attribute))
                        {
                            Logger.Write(LogColor.Hilite, "Internal Error:  method '{0}' has attribute that already exists", name);
                            Logger.WriteDiagnostic(" ");
                            Logger.WriteDiagnostic("Dump Methods");
                            Logger.WriteDiagnostic("===========================");
                            foreach (var v in matchedMethods)
                            {
                                Logger.WriteDiagnostic("{0} {1} {2} {3}", v.Key.SpecificClass, v.Key.SpecificSpec, v.Key.SpecificContext, v.Key.PriorityLevel);
                            }
                            Logger.WriteDiagnostic("===========================");
                            Logger.WriteDiagnostic("{0} {1} {2} {3}  == add  attempt for {4}", attribute.SpecificClass, attribute.SpecificSpec, attribute.SpecificContext, attribute.PriorityLevel, name);
                            Logger.WriteDiagnostic(" ");
                        }

                        matchedMethods.Add(attribute, comp);

                        CurrentBehaviorType = 0;
                        CurrentBehaviorPriority = 0;
                        CurrentBehaviorName = string.Empty;
                    }
                }
            }
            // If we found no methods, rofls!
            if (matchedMethods.Count <= 0)
            {
                return null;
            }

            var result = new PrioritySelector();
            foreach (var kvp in matchedMethods.OrderByDescending(mm => mm.Key.PriorityLevel))
            {
                result.AddChild(kvp.Value);
                behaviourCount++;
            }

            return result;
        }
        public static Composite GetComposite(WoWClass wowClass, WoWSpec spec, BehaviorType behavior, WoWContext context, out int behaviourCount, bool silent = false)
        {
            if (context == WoWContext.None)
            {
                // None is an invalid context, but rather than stopping bot wait it out with donothing logic
                Logger.Write(Color.White, "No Active Context -{0}{1} for{2} set to DoNothingBehavior temporarily", wowClass.ToString().CamelToSpaced(), behavior.ToString().CamelToSpaced(), spec.ToString().CamelToSpaced());
                behaviourCount = 1;
                return NoContextAvailable.CreateDoNothingBehavior();
            }

            SilentBehaviorCreation = silent;
            behaviourCount = 0;
            if (_methods.Count <= 0)
            {
                // Logger.WriteDebug("Singular Behaviors: building method list");
                foreach (var type in Assembly.GetExecutingAssembly().GetTypes())
                {
                    // All behavior methods should not be generic, and should have zero parameters, with their return types being of type Composite.
                    _methods.AddRange(
                        type.GetMethods(BindingFlags.Static | BindingFlags.Public).Where(
                            mi => !mi.IsGenericMethod && mi.GetParameters().Length == 0).Where(
                                mi => mi.ReturnType.IsAssignableFrom(typeof (Composite))));
                }
                Logger.WriteFile("Singular Behaviors: Added " + _methods.Count + " behaviors");
            }

            var matchedMethods = new Dictionary<BehaviorAttribute, Composite>();

            foreach (MethodInfo mi in _methods)
            {
                // If the behavior is set as ignore. Don't use it? Duh?
                if (mi.GetCustomAttributes(typeof(IgnoreBehaviorCountAttribute), false).Any())
                    continue;

                // If there's no behavior attrib, then move along.
                foreach (var a in mi.GetCustomAttributes(typeof(BehaviorAttribute), false))
                {
                    var attribute = a as BehaviorAttribute;
                    if (attribute == null)
                        continue;

                    // Check if our behavior matches with what we want. If not, don't add it!
                    if (IsMatchingMethod(attribute, wowClass, spec, behavior, context))
                    {
                        if (!silent)
                            Logger.WriteFile("{0} {1} {2}", attribute.PriorityLevel.ToString().AlignRight(4), behavior.ToString().AlignLeft(15), mi.Name);

                        CurrentBehaviorType = behavior;

                        // if it blows up here, you defined a method with the exact same attribute and priority as one already found

                        // wrap in trace class
                        Composite comp = mi.Invoke(null, null) as Composite;
                        string name = behavior.ToString() + "." + mi.Name + "." + attribute.PriorityLevel.ToString();

                        if (SingularSettings.Trace)
                            comp = new CallTrace( name, comp);

                        matchedMethods.Add(attribute, comp);

                        CurrentBehaviorType = 0;
                    }
                }
            }
            // If we found no methods, rofls!
            if (matchedMethods.Count <= 0)
            {
                return null;
            }

            var result = new PrioritySelector();
            foreach (var kvp in matchedMethods.OrderByDescending(mm => mm.Key.PriorityLevel))
            {
                result.AddChild(kvp.Value);
                behaviourCount++;
            }

            return result;
        }