Skip to content

Instantly share code, notes, and snippets.

@pushedx
Created August 2, 2015 07:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pushedx/a1ec2004f16067a008e1 to your computer and use it in GitHub Desktop.
Save pushedx/a1ec2004f16067a008e1 to your computer and use it in GitHub Desktop.
using System.Diagnostics;
using System.Linq;
using log4net;
using Loki.Bot.Pathfinding;
using Loki.Common;
using Loki.Game;
namespace Loki.Bot
{
public class LockstepPlayerMover : IPlayerMover
{
private static readonly ILog Log = Logger.GetLoggerInstanceForType();
private PathfindingCommand _cmd;
private Stopwatch _sw;
/// <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, params object[] user)
{
var me = LokiPoe.Me;
var myPos = me.Position;
if (
_cmd == null || // No command yet
_cmd.EndPoint != position || // Moving to a new poisition
LokiPoe.CurrentWorldArea.IsTown || // In town, always generate new paths
(_sw != null && _sw.ElapsedMilliseconds > 5000)) // Stale path
{
_cmd = new PathfindingCommand(myPos, position, 10);
if (!ExilePather.FindPath(ref _cmd))
{
if (_cmd.Error == PathfindingError.StartNotNavigable ||
_cmd.Error == PathfindingError.StartAndEndAreSame)
{
var rnd = LokiPoe.Me.Position;
rnd += new Vector2i(LokiPoe.Random.Next(-25, 26), LokiPoe.Random.Next(-25, 26));
Log.DebugFormat("[MoveTowards] FindPath returned {0}. Now clicling the random position {1}.",
_cmd.Error, rnd);
LokiPoe.Input.SetMousePos(rnd);
LokiPoe.Input.Move();
return true;
}
// Some other error.
return false;
}
_sw = Stopwatch.StartNew();
}
// Eliminate points until we find one within a good moving range.
while (_cmd.Path.Count > 1)
{
if (_cmd.Path[0].Distance(LokiPoe.Me.Position) < 12)
{
_cmd.Path.RemoveAt(0);
}
else
{
break;
}
}
var point = _cmd.Path[0];
point += new Vector2i(LokiPoe.Random.Next(-2, 3), LokiPoe.Random.Next(-2, 3));
LokiPoe.Input.SetMousePos(point);
LokiPoe.Input.Move();
return true;
}
}
public class PredictivePlayerMover : IPlayerMover
{
private static readonly ILog Log = Logger.GetLoggerInstanceForType();
/// <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, params 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(LokiPoe.Random.Next(-25, 26), LokiPoe.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;
if (LokiPoe.CurrentWorldArea.IsTown)
{
point = path.Path.FirstOrDefault(e => e.Distance(myPos) > 8);
}
else
{
// Try to avoid path cycles from pathfinding derping.
for (var i = path.Path.Count - 1; i > 0; --i)
{
point = path.Path[i];
if (point.Distance(myPos) <= 15 && ExilePather.PathDistance(myPos, point) < 20)
break;
}
}
if (point == Vector2i.Zero)
{
point = path.Path.Last();
}
point = myPos.GetPointAtDistanceAfterThis(point, LokiPoe.Random.Next(14, 20));
point += new Vector2i(LokiPoe.Random.Next(-2, 3), LokiPoe.Random.Next(-2, 3));
LokiPoe.Input.SetMousePos(point);
LokiPoe.Input.Move();
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment