Advertisement
Guest User

Customplayermover.cs

a guest
Aug 29th, 2015
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.60 KB | None | 0 0
  1. using System.Diagnostics;
  2. using System.Linq;
  3. using System.Windows.Controls;
  4. using log4net;
  5. using System;
  6. using Loki.Bot;
  7. using Loki.Bot.Pathfinding;
  8. using Loki.Game;
  9. using Loki.Utilities;
  10.  
  11. namespace CustomPlayerMover
  12. {
  13. internal class CustomPlayerMover : IPlugin
  14. {
  15. private static readonly ILog Log = Logger.GetLoggerInstanceForType();
  16.  
  17. private bool _enabled;
  18. private LeapSlamPlayerMover _mover = new LeapSlamPlayerMover();
  19.  
  20. /// <summary> The name of the plugin. </summary>
  21. public string Name
  22. {
  23. get { return "CustomPlayerMover"; }
  24. }
  25.  
  26. /// <summary> The description of the plugin. </summary>
  27. public string Description
  28. {
  29. get { return "A plugin that provides an example of using a custom IPlayerMover implementation."; }
  30. }
  31.  
  32. /// <summary>The author of the plugin.</summary>
  33. public string Author
  34. {
  35. get { return "Bossland GmbH"; }
  36. }
  37.  
  38. /// <summary>The version of the plugin.</summary>
  39. public Version Version
  40. {
  41. get { return new Version(0, 0, 1, 1); }
  42. }
  43.  
  44. /// <summary>Initializes this plugin.</summary>
  45. public void Initialize()
  46. {
  47. Log.DebugFormat("[CustomPlayerMover] Initialize");
  48. }
  49.  
  50. /// <summary> The plugin start callback. Do any initialization here. </summary>
  51. public void Start()
  52. {
  53. Log.DebugFormat("[CustomPlayerMover] Start");
  54.  
  55. // This is all that needs to be done. All movement logic that uses PlayerMover.MoveTowards
  56. // will now use this new instance's implementation.
  57. PlayerMover.Instance = _mover;
  58. _mover.LeapSlamSlot = -1; // reset the skill slot in case the user changed something
  59.  
  60. Log.InfoFormat("[CustomPlayerMover] Now setting the LeapSlamPlayerMover.");
  61. }
  62.  
  63. /// <summary> The plugin tick callback. Do any update logic here. </summary>
  64. public void Tick()
  65. {
  66. }
  67.  
  68. /// <summary> The plugin stop callback. Do any pre-dispose cleanup here. </summary>
  69. public void Stop()
  70. {
  71. Log.DebugFormat("[CustomPlayerMover] Stop");
  72. }
  73.  
  74. #region Implementation of IConfigurable
  75.  
  76. public JsonSettings Settings
  77. {
  78. get { return null; }
  79. }
  80.  
  81. /// <summary> The plugin's settings control. This will be added to the Exilebuddy Settings tab.</summary>
  82. public UserControl Control
  83. {
  84. get { return null; }
  85. }
  86.  
  87. #endregion
  88.  
  89. #region Implementation of IEnableable
  90.  
  91. /// <summary>Is this plugin currently enabled?</summary>
  92. public bool IsEnabled
  93. {
  94. get { return _enabled; }
  95. }
  96.  
  97. /// <summary> The plugin is being enabled.</summary>
  98. public void Enable()
  99. {
  100. Log.DebugFormat("[CustomPlayerMover] Enable");
  101. _enabled = true;
  102. }
  103.  
  104. /// <summary> The plugin is being disabled.</summary>
  105. public void Disable()
  106. {
  107. Log.DebugFormat("[CustomPlayerMover] Disable");
  108. _enabled = false;
  109. }
  110.  
  111. #endregion
  112.  
  113. #region Implementation of IDisposable
  114.  
  115. /// <summary> </summary>
  116. public void Dispose()
  117. {
  118. }
  119.  
  120. #endregion
  121.  
  122. #region Override of Object
  123.  
  124. /// <summary>
  125. ///
  126. /// </summary>
  127. /// <returns></returns>
  128. public override string ToString()
  129. {
  130. return Name + ": " + Description;
  131. }
  132.  
  133. #endregion
  134. }
  135.  
  136. /// <summary>
  137. /// A player mover that uses Leap Slam to try and move around.
  138. /// This mover pathfinds each operation to the destination to try and avoid desync issues.
  139. /// </summary>
  140. public class LeapSlamPlayerMover : IPlayerMover
  141. {
  142. private static readonly ILog Log = Logger.GetLoggerInstanceForType();
  143.  
  144. internal int LeapSlamSlot = -1;
  145. private readonly Stopwatch _leapSlamStopwatch = new Stopwatch();
  146.  
  147. /// <summary>
  148. /// Attempts to move towards a position. This function will perform pathfinding logic and take into consideration move distance
  149. /// to try and smoothly move towards a point.
  150. /// </summary>
  151. /// <param name="position">The position to move towards.</param>
  152. /// <param name="user">A user object passed.</param>
  153. /// <returns>true if the position was moved towards, and false if there was a pathfinding error.</returns>
  154. public bool MoveTowards(Vector2i position, object user)
  155. {
  156. var me = LokiPoe.Me;
  157. var myPos = me.Position;
  158.  
  159. var path = new PathfindingCommand(myPos, position, 10);
  160. if (!ExilePather.FindPath(ref path))
  161. {
  162. if (path.Error == PathfindingError.StartNotNavigable ||
  163. path.Error == PathfindingError.StartAndEndAreSame)
  164. {
  165. var rnd = LokiPoe.Me.Position;
  166. rnd += new Vector2i(Utility.Random.Next(-25, 26), Utility.Random.Next(-25, 26));
  167.  
  168. Log.DebugFormat("[MoveTowards] FindPath returned {0}. Now clicling the random position {1}.",
  169. path.Error, rnd);
  170.  
  171. LokiPoe.Input.SetMousePos(rnd);
  172.  
  173. LokiPoe.Input.Move();
  174.  
  175. return true;
  176. }
  177.  
  178. Log.ErrorFormat("[MoveTowards] FindPath returned {0}.", path.Error);
  179.  
  180. return false;
  181. }
  182.  
  183. var point = Vector2i.Zero;
  184.  
  185. var checkLeapSlam = true;
  186. var canLeapSlam = false;
  187. var leapSlamPos = Vector2i.Zero;
  188.  
  189. // Only check for using leap slam once every 500ms.
  190. if (_leapSlamStopwatch.IsRunning && _leapSlamStopwatch.ElapsedMilliseconds < 500)
  191. {
  192. checkLeapSlam = false;
  193. }
  194.  
  195. // Don't try to custom move in these areas.
  196. if (LokiPoe.Me.IsInTown || LokiPoe.Me.IsInMapRoom || LokiPoe.Me.IsInHideout)
  197. checkLeapSlam = false;
  198.  
  199. if (LeapSlamSlot == -1)
  200. {
  201. var ls = LokiPoe.Me.AvailableSkills.FirstOrDefault(s => s.Name == "Leap Slam" && s.IsOnSkillBar);
  202. if (ls != null)
  203. LeapSlamSlot = ls.Slot;
  204.  
  205. // No leap slam skill, don't run the logic.
  206. if (LeapSlamSlot == -1)
  207. {
  208. checkLeapSlam = false;
  209. }
  210. }
  211.  
  212. if (LokiPoe.CurrentWorldArea.IsTown)
  213. {
  214. // To avoid some issues in Act 2 stairs, we try to take points closer to us rather than the other way around.
  215. point = path.Path.FirstOrDefault(e => e.Distance(myPos) > 8);
  216. }
  217. else
  218. {
  219. // TODO: Tweak/change this logic as needed per tileset.
  220. // Smaller closed areas should probably just skip trying to use Leap Slam.
  221.  
  222. for (var i = path.Path.Count - 1; i > 0; --i)
  223. {
  224. point = path.Path[i];
  225.  
  226. if (checkLeapSlam && !canLeapSlam && point.Distance(myPos) <= 60)
  227. {
  228. if (ExilePather.CanObjectSee(LokiPoe.Me, point) && !Utility.ClosedDoorBetween(myPos, point))
  229. {
  230. canLeapSlam = true;
  231. leapSlamPos = point;
  232. }
  233. }
  234.  
  235. if (point.Distance(myPos) <= 20)
  236. break;
  237. }
  238. }
  239.  
  240. if (point == Vector2i.Zero)
  241. {
  242. point = path.Path.Last();
  243. }
  244.  
  245. // 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.
  246. point = Utility.CalculatePointAtDistanceAfterStart(myPos, point, Utility.Random.Next(14, 20));
  247. point += new Vector2i(Utility.Random.Next(-2, 3), Utility.Random.Next(-2, 3));
  248.  
  249. // Try to Leap Slam
  250. if (canLeapSlam)
  251. {
  252. var res = LokiPoe.InGameState.SkillBarPanel.UseAt(LeapSlamSl ot, false, leapSlamPos);
  253. if (res == LokiPoe.InGameState.UseError.None)
  254. {
  255. if (LokiPoe.Me.HasCurrentAction)
  256. {
  257. if (LokiPoe.Me.CurrentAction.Skill.Name == "Leap Slam")
  258. {
  259. _leapSlamStopwatch.Restart();
  260. return true;
  261. }
  262. }
  263. }
  264. }
  265.  
  266. // Otherwise, normal movement to keep on going.
  267. LokiPoe.Input.SetMousePos(point);
  268. LokiPoe.Input.Move();
  269.  
  270. return true;
  271. }
  272. }
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement