axazol
New Member
- Joined
- Jun 27, 2010
- Messages
- 308
- Reaction score
- 8
UseItemOn_Name.cs
[listing]
Example:
Usage: For cases when unable to find object/Npc ID's</custombehavior>
[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
}
}
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" />