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

[CB] InteractWith_Name

axazol

New Member
Joined
Jun 27, 2010
Messages
308
Reaction score
8
InteractWith_Name.cs
PHP:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using Styx.Database;
using Styx.Helpers;
using Styx.Logic.BehaviorTree;
using Styx.Logic.Inventory.Frames.Gossip;
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
{
    /// 
    /// InteractWith_Name by AxaZol
    /// Allows you to do quests that requires you to interact with nearby objects by name.
    /// ##Syntax##
    /// QuestId: Id of the quest.
    /// TargetName: Id of the object to interact with.
    /// NumOfTimes: Number of times to interact with object.
    /// [Optional]CollectionDistance: The distance it will use to collect objects. DefaultValue:100 yards
    /// [Optional]WaitTime: Time to wait after interact with object. DefaultValue: 1500 ms
    /// 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 InteractWith_Name : CustomForcedBehavior
    {
        public InteractWith_Name(Dictionary args)
            : base(args)
        {
            bool error = false;

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

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

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

            if (!Args.ContainsKey("TargetType"))
            {
                Logging.Write("Could not find attribute 'TargetType' in InteractWith_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 InteractWith_Name behavior failed! please check your profile!");
                error = true;
            }

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

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

            if (error)
                Thread.CurrentThread.Abort();

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

        public WoWPoint Location { get; private set; }
        public int WaitTime { get; private set; }
        public int Counter { get; set; }
        public string TargetName { get; set; }
        public int NumOfTimes { get; 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 interact with - " + 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 = "Interacting with - " + CurrentObject.Name;
                                            CurrentObject.Interact();
                                            _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)
                TreeRoot.GoalText = string.Format("Interacting with: {0} {1} Times for quest:{2}", TargetName, NumOfTimes, quest.Name);
        }

        #endregion
    }

    public enum TargetType
    {
        Npc,
        Gameobject
    }
}
<summary><summary>Syntax example:
PHP:
<PickUp QuestName="I  Ain't Sayin' You a Gourd Digger..." QuestId="26956" GiverName="Selyria  Groenveld" GiverId="44457"  X="1792.81" Y="-1670.1" Z="60.15903" />
<If Condition="!IsQuestCompleted(26954)">
  <RunTo QuestId="26956" X="1851.234" Y="-1621.35" Z="58.94324" />
  <CustomBehavior QuestId="26956"
    File="InteractWith_Name"
    TargetName="Unhealthy-Looking Pumpkin Removed"
    NumOfTimes="3"
    WaitTime="2000"
    TargetType="Gameobject"
    X="1851.234" Y="-1621.35" Z="58.94324" />
  <RunTo QuestId="26956" X="1806.04" Y="-1579.781" Z="59.29829" />
  <CustomBehavior QuestId="26956"
    File="InteractWith_Name"
    TargetName="Rotten Apple Removed"
    NumOfTimes="4"
    WaitTime="2000"
    TargetType="Gameobject"
    X="1806.04" Y="-1579.781" Z="59.29829" />
  <RunTo QuestId="26956" X="1887.169" Y="-1568.092" Z="59.47567" />
  <CustomBehavior QuestId="26956"
    File="InteractWith_Name"
    TargetName="Bad Corn Removed"
    NumOfTimes="6"
    WaitTime="2000"
    TargetType="Gameobject"
    X="1887.169" Y="-1568.092" Z="59.47567" />
</If>
<TurnIn QuestName="I Ain't Sayin' You a Gourd Digger..."  QuestId="26956" TurnInName="Selyria Groenveld" TurnInId="44457"  X="1792.81" Y="-1670.1" Z="60.15903" />
Usage: For cases when unable to locate Object or NPC Id's.
</turnin></runto></custombehavior></runto></custombehavior></runto></if></pickup></summary></summary>
 

Attachments

Last edited:
Thanks, cant find the id for Loose Snow so this will be helpfull :)
 
Back
Top