What's new
  • Visit Rebornbuddy
  • Visit Panda Profiles
  • Visit LLamamMagic
  • Visit Resources
  • Visit Downloads
  • Visit Portal

[CB] UseItemOn_Name (untested)

axazol

New Member
Joined
Jun 27, 2010
Messages
308
Reaction score
8
UseItemOn_Name.cs
[listing]
PHP:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading;
using Styx.Helpers;
using Styx.Logic.BehaviorTree;
using Styx.Logic.Pathing;
using Styx.Logic.Questing;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using TreeSharp;
using Action = TreeSharp.Action;

namespace Styx.Bot.Quest_Behaviors
{
    /// 
    /// UseItemOn_Name by AxaZol
    /// Allows you to use items on nearby gameobjects/npc's by Name
    /// ##Syntax##
    /// QuestId: The id of the quest.
    /// TargetName: The Name of the object.
    /// ItemId: The id of the item to use.
    /// NumOfTimes: Number of times to use said item.
    /// [Optional]WaitTime: Time to wait after using an item. DefaultValue: 1500 ms
    /// [Optional]CollectionDistance: The distance it will use to collect objects. DefaultValue:100 yards
    /// TargetType: the type of object to interact with, expected value: Npc/Gameobject
    /// X,Y,Z: The general location where theese objects can be found
    /// 
    public class UseItemOn_Name : CustomForcedBehavior
    {
        public UseItemOn_Name(Dictionary args)
            : base(args)
        {
            bool error = false;

            uint questId;
            if (!uint.TryParse(Args["QuestId"], out questId))
            {
                Logging.Write("Parsing attribute 'QuestId' in UseItemOn_Name behavior failed! please check your profile!");
                error = true;
            }
            
            if (!Args.ContainsKey("TargetName"))
            {
                Logging.Write("Could not find attribute 'TargetName' in UseItemOn_Name behavior! please check your profile!");
                error = true;
            }
            
            string targetName = Args["TargetName"];

            uint itemId;
            if(!uint.TryParse(Args["ItemId"], out itemId))
            {
                Logging.Write("Parsing attribute 'ItemId' in UseItemOn_Name behavior failed! please check your profile!");
                error = true;
            }

            int numOfTimes;
            if (!int.TryParse(Args["NumOfTimes"], out numOfTimes))
            {
                Logging.Write("Parsing attribute 'NumOfTimes' in UseItemOn_Name behavior failed! please check your profile!");
                error = true;
            }

            if(Args.ContainsKey("WaitTime"))
            {
                int waitTime;
                int.TryParse(Args["WaitTime"], out waitTime);
                WaitTime = waitTime != 0 ? waitTime : 1500;
            }

            if (Args.ContainsKey("CollectionDistance"))
            {
                int distance;
                int.TryParse(Args["CollectionDistance"], out distance);
                CollectionDistance = distance != 0 ? distance : 100;
            }

            if (!Args.ContainsKey("TargetType"))
            {
                Logging.Write("Could not find attribute 'TargetType' in UseItemOn_Name behavior! please check your profile!");
                error = true;
            }

            var type = (TargetType)Enum.Parse(typeof(TargetType), Args["TargetType"], true);

            float x, y, z;
            if (!float.TryParse((string)Args["X"], out x))
            {
                Logging.Write("Parsing attribute 'X' in UseItemOn_Name behavior failed! please check your profile!");
                error = true;
            }

            if (!float.TryParse((string)Args["Y"], out y))
            {
                Logging.Write("Parsing attribute 'Y' in UseItemOn_Name behavior failed! please check your profile!");
                error = true;
            }

            if (!float.TryParse((string)Args["Z"], out z))
            {
                Logging.Write("Parsing attribute 'Z' in UseItemOn_Name behavior failed! please check your profile!");
                error = true;
            }

            if (error)
                TreeRoot.Stop();

            TargetType = type;
            QuestId = questId;
            NumOfTimes = numOfTimes;
            TargetName = targetName;
            ItemId = itemId;
            Location = new WoWPoint(x, y, z);
        }

        public WoWPoint Location { get; private set; }
        public int WaitTime { get; private set; }
        public int Counter { get; private set; }
        public string TargetName { get; private set; }
        public uint ItemId { get; private set; }
        public int NumOfTimes { get; private set; }
        public uint QuestId { get; private set; }
        public TargetType TargetType { get; private set; }
        public int CollectionDistance = 100;

        private readonly List _npcBlacklist = new List();

        ///  Current object we should interact with.
        ///  The object.
        private WoWObject CurrentObject
        {
            get
            {
                WoWObject @object = null;
                switch (TargetType)
                {
                    case TargetType.Gameobject:
                        @object = ObjectManager.GetObjectsOfType().OrderBy(ret => ret.Distance).FirstOrDefault(obj =>
                            !_npcBlacklist.Contains(obj.Guid) &&
                            obj.Distance < CollectionDistance &&
                            obj.Name == TargetName);

                        break;

                    case TargetType.Npc:
                        @object = ObjectManager.GetObjectsOfType().OrderBy(ret => ret.Distance).FirstOrDefault(obj =>
                            !_npcBlacklist.Contains(obj.Guid) &&
                            obj.Distance < CollectionDistance &&
                            obj.Name == TargetName);

                        break;

                }

                if (@object != null)
                {
                    Logging.Write(@object.Name);
                }
                return @object;
            }
        }

        #region Overrides of CustomForcedBehavior

        private Composite _root;
        protected override Composite CreateBehavior()
        {
            return _root ?? (_root =
            new PrioritySelector(

                new Decorator(ret => Counter >= NumOfTimes,
                    new Action(ret => _isDone = true)),

                    new PrioritySelector(

                        new Decorator(ret => CurrentObject != null && !CurrentObject.WithinInteractRange,
                            new Sequence(
                                new Action(delegate { TreeRoot.StatusText = "Moving to use item on - " + CurrentObject.Name; }),
                                new Action(ret => Navigator.MoveTo(CurrentObject.Location))
                                )
                            ),

                        new Decorator(ret => CurrentObject != null && CurrentObject.WithinInteractRange,
                            new Sequence(
                                new DecoratorContinue(ret => StyxWoW.Me.IsMoving,
                                    new Action(delegate
                                    {
                                        WoWMovement.MoveStop();
                                        StyxWoW.SleepForLagDuration();
                                    })),

                                    new Action(delegate
                                    {
                                        TreeRoot.StatusText = "Using item on - " + CurrentObject.Name;
                                        if (CurrentObject is WoWUnit)
                                        {
                                            (CurrentObject as WoWUnit).Target();
                                        }

                                        var item = StyxWoW.Me.CarriedItems.FirstOrDefault(ret => ret.Entry == ItemId);
                                        if(item == null)
                                        {
                                            Logging.Write(Color.Red, "Could not find item with id:{0} for UseItemOn behavior!", ItemId);
                                            Logging.Write(Color.Red, "Honorbuddy stopped!");
                                            TreeRoot.Stop();
                                            return;
                                        }
                                        
                                        WoWMovement.Face(CurrentObject.Guid);

                                        item.UseContainerItem();
                                        _npcBlacklist.Add(CurrentObject.Guid);

                                        StyxWoW.SleepForLagDuration();
                                        Counter++;
                                        Thread.Sleep(WaitTime);
                                    }))
                                    ),

                        new Sequence(
                            new Action(delegate { TreeRoot.StatusText = "Moving towards - " + Location; }),
                            new Action(ret => Navigator.MoveTo(Location))))
                ));
        }

        private bool _isDone;
        public override bool IsDone
        {
            get
            {
                PlayerQuest quest = StyxWoW.Me.QuestLog.GetQuestById(QuestId);

                return
                    _isDone ||
                    (quest != null && quest.IsCompleted) ||
                    quest == null;
            }
        }

        public override void OnStart()
        {
            PlayerQuest quest = StyxWoW.Me.QuestLog.GetQuestById(QuestId);
            if (quest != null)
            {
                var item = StyxWoW.Me.CarriedItems.FirstOrDefault(ret => ret.Entry == ItemId);
                if(item != null)
                    TreeRoot.GoalText = string.Format("Using item {0} for {1}",
                    item.Name,
                    quest.Name);
                
                else
                {
                    TreeRoot.GoalText = string.Format("Use item {0} times on mob: {1} for quest: {2}",
                        NumOfTimes,
                        MobId,
                        quest.Name);    
                }
            }
        }

        #endregion
    }

    public enum TargetType
    {
        Npc,
        Gameobject
    }
}
Example:
PHP:
<CustomBehavior QuestId="123"
  File="UseItemOn_Name"
  ItemId="123"
  TargetName="Gameobject or Npc Name"
  NumOfTimes="1"
  WaitTime="2000"         // [optional] default 1500ms
  CollectionDistance="50" // [optional] default 100yards
  TargetType="Gameobject"
  X="12.3" Y="45.6" Z="78.9" />
Usage: For cases when unable to find object/Npc ID's</custombehavior>
 

Attachments

Fantastic i have been trubbled by that issue for the last hours
 
Creating new CustomBehaviors is always appreciated, but...
I'm a little confused as to how this is meaningfully different than the UseItemOn CB that ships with Honorbuddy.

The only difference I can see is that this one uses TargetName whereas the HB-shipped one used the MobId. The problem with using a 'name' instead of a number is that any profile that uses UseItemOn_Name is now language-specific. Meaning that people running European WoWclients won't be able to interact with an NPC name written in English, etc.

Am I missing something here?

cheers,
chinajade
 
Last edited:
The only difference I can see is that this one uses TargetName whereas the HB-shipped one used the MobId. The problem with using a 'name' instead of a number is that any profile that uses UseItemOn_Name is now language-specific. Meaning that people running European WoWclients won't be able to interact with an NPC name written in English, etc.
Yep. In that cases only one i can suggest - make localization version of profiles.
Or u always can choise: wait till wowhead add missing ID and then HB parse it, or use 'name' based behaviors.
 
Last edited:
Uhm... you do realize that mobs usually have unique IDs and names. I can't see a place where this would be useful at all. It only causes more work in the long run.
 
I didnt force anyone to use it against ID's version. It's just for cases with missing ID in DB. It's just a tool.
 
Back
Top