Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Diagnostics;
- using System.Linq;
- using System.Windows.Controls;
- using log4net;
- using System;
- using Loki.Bot;
- using Loki.Bot.Pathfinding;
- using Loki.Game;
- using Loki.Utilities;
- namespace CustomPlayerMover
- {
- internal class CustomPlayerMover : IPlugin
- {
- private static readonly ILog Log = Logger.GetLoggerInstanceForType();
- private bool _enabled;
- private LeapSlamPlayerMover _mover = new LeapSlamPlayerMover();
- /// <summary> The name of the plugin. </summary>
- public string Name
- {
- get { return "CustomPlayerMover"; }
- }
- /// <summary> The description of the plugin. </summary>
- public string Description
- {
- get { return "A plugin that provides an example of using a custom IPlayerMover implementation."; }
- }
- /// <summary>The author of the plugin.</summary>
- public string Author
- {
- get { return "Bossland GmbH"; }
- }
- /// <summary>The version of the plugin.</summary>
- public Version Version
- {
- get { return new Version(0, 0, 1, 1); }
- }
- /// <summary>Initializes this plugin.</summary>
- public void Initialize()
- {
- Log.DebugFormat("[CustomPlayerMover] Initialize");
- }
- /// <summary> The plugin start callback. Do any initialization here. </summary>
- public void Start()
- {
- Log.DebugFormat("[CustomPlayerMover] Start");
- // This is all that needs to be done. All movement logic that uses PlayerMover.MoveTowards
- // will now use this new instance's implementation.
- PlayerMover.Instance = _mover;
- _mover.LeapSlamSlot = -1; // reset the skill slot in case the user changed something
- Log.InfoFormat("[CustomPlayerMover] Now setting the LeapSlamPlayerMover.");
- }
- /// <summary> The plugin tick callback. Do any update logic here. </summary>
- public void Tick()
- {
- }
- /// <summary> The plugin stop callback. Do any pre-dispose cleanup here. </summary>
- public void Stop()
- {
- Log.DebugFormat("[CustomPlayerMover] Stop");
- }
- #region Implementation of IConfigurable
- public JsonSettings Settings
- {
- get { return null; }
- }
- /// <summary> The plugin's settings control. This will be added to the Exilebuddy Settings tab.</summary>
- public UserControl Control
- {
- get { return null; }
- }
- #endregion
- #region Implementation of IEnableable
- /// <summary>Is this plugin currently enabled?</summary>
- public bool IsEnabled
- {
- get { return _enabled; }
- }
- /// <summary> The plugin is being enabled.</summary>
- public void Enable()
- {
- Log.DebugFormat("[CustomPlayerMover] Enable");
- _enabled = true;
- }
- /// <summary> The plugin is being disabled.</summary>
- public void Disable()
- {
- Log.DebugFormat("[CustomPlayerMover] Disable");
- _enabled = false;
- }
- #endregion
- #region Implementation of IDisposable
- /// <summary> </summary>
- public void Dispose()
- {
- }
- #endregion
- #region Override of Object
- /// <summary>
- ///
- /// </summary>
- /// <returns></returns>
- public override string ToString()
- {
- return Name + ": " + Description;
- }
- #endregion
- }
- /// <summary>
- /// A player mover that uses Leap Slam to try and move around.
- /// This mover pathfinds each operation to the destination to try and avoid desync issues.
- /// </summary>
- public class LeapSlamPlayerMover : IPlayerMover
- {
- private static readonly ILog Log = Logger.GetLoggerInstanceForType();
- internal int LeapSlamSlot = -1;
- private readonly Stopwatch _leapSlamStopwatch = new Stopwatch();
- /// <summary>
- /// Attempts to move towards a position. This function will perform pathfinding logic and take into consideration move distance
- /// to try and smoothly move towards a point.
- /// </summary>
- /// <param name="position">The position to move towards.</param>
- /// <param name="user">A user object passed.</param>
- /// <returns>true if the position was moved towards, and false if there was a pathfinding error.</returns>
- public bool MoveTowards(Vector2i position, object user)
- {
- var me = LokiPoe.Me;
- var myPos = me.Position;
- var path = new PathfindingCommand(myPos, position, 10);
- if (!ExilePather.FindPath(ref path))
- {
- if (path.Error == PathfindingError.StartNotNavigable ||
- path.Error == PathfindingError.StartAndEndAreSame)
- {
- var rnd = LokiPoe.Me.Position;
- rnd += new Vector2i(Utility.Random.Next(-25, 26), Utility.Random.Next(-25, 26));
- Log.DebugFormat("[MoveTowards] FindPath returned {0}. Now clicling the random position {1}.",
- path.Error, rnd);
- LokiPoe.Input.SetMousePos(rnd);
- LokiPoe.Input.Move();
- return true;
- }
- Log.ErrorFormat("[MoveTowards] FindPath returned {0}.", path.Error);
- return false;
- }
- var point = Vector2i.Zero;
- var checkLeapSlam = true;
- var canLeapSlam = false;
- var leapSlamPos = Vector2i.Zero;
- // Only check for using leap slam once every 500ms.
- if (_leapSlamStopwatch.IsRunning && _leapSlamStopwatch.ElapsedMilliseconds < 500)
- {
- checkLeapSlam = false;
- }
- // Don't try to custom move in these areas.
- if (LokiPoe.Me.IsInTown || LokiPoe.Me.IsInMapRoom || LokiPoe.Me.IsInHideout)
- checkLeapSlam = false;
- if (LeapSlamSlot == -1)
- {
- var ls = LokiPoe.Me.AvailableSkills.FirstOrDefault(s => s.Name == "Leap Slam" && s.IsOnSkillBar);
- if (ls != null)
- LeapSlamSlot = ls.Slot;
- // No leap slam skill, don't run the logic.
- if (LeapSlamSlot == -1)
- {
- checkLeapSlam = false;
- }
- }
- if (LokiPoe.CurrentWorldArea.IsTown)
- {
- // To avoid some issues in Act 2 stairs, we try to take points closer to us rather than the other way around.
- point = path.Path.FirstOrDefault(e => e.Distance(myPos) > 8);
- }
- else
- {
- // TODO: Tweak/change this logic as needed per tileset.
- // Smaller closed areas should probably just skip trying to use Leap Slam.
- for (var i = path.Path.Count - 1; i > 0; --i)
- {
- point = path.Path[i];
- if (checkLeapSlam && !canLeapSlam && point.Distance(myPos) <= 60)
- {
- if (ExilePather.CanObjectSee(LokiPoe.Me, point) && !Utility.ClosedDoorBetween(myPos, point))
- {
- canLeapSlam = true;
- leapSlamPos = point;
- }
- }
- if (point.Distance(myPos) <= 20)
- break;
- }
- }
- if (point == Vector2i.Zero)
- {
- point = path.Path.Last();
- }
- // Find a point in the direction we want to move, but keep it random to make sure bots do not do the same thing everytime.
- point = Utility.CalculatePointAtDistanceAfterStart(myPos, point, Utility.Random.Next(14, 20));
- point += new Vector2i(Utility.Random.Next(-2, 3), Utility.Random.Next(-2, 3));
- // Try to Leap Slam
- if (canLeapSlam)
- {
- var res = LokiPoe.InGameState.SkillBarPanel.UseAt(LeapSlamSl ot, false, leapSlamPos);
- if (res == LokiPoe.InGameState.UseError.None)
- {
- if (LokiPoe.Me.HasCurrentAction)
- {
- if (LokiPoe.Me.CurrentAction.Skill.Name == "Leap Slam")
- {
- _leapSlamStopwatch.Restart();
- return true;
- }
- }
- }
- }
- // Otherwise, normal movement to keep on going.
- LokiPoe.Input.SetMousePos(point);
- LokiPoe.Input.Move();
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement