private void makeMoves(int toX, int toY)
 {
     destX = toX;
     destY = toY;
     ActorHandler.position currentLocation = TheActorHandler.GetMyPosition();
     if (myThread.IsAlive)
     {
         //maybe I don't need to try to abort it, but just set keep moving to false, hrm...
         //keepMoving = false;
         myThread.Abort();
         myThread.Join();
         TheTCPWrapper.Send(CommandCreator.SEND_PM(username, "Aborting movement and starting new path..."));
         Thread.Sleep(400);
     }
     myPathing.clearMap();
     myPathing.resetMap();
     currentLocation = TheActorHandler.GetMyPosition();
     myMoves = myPathing.getPath(currentLocation.x, currentLocation.y, toX, toY);
     if (myMoves.Count > 0)
     {
         //myPathing.resetMap();
         keepMoving = true;
         startX = currentLocation.x;
         startY = currentLocation.y;
         myThread = new Thread(new ThreadStart(makeMoves));
         myThread.Start();
         Thread.Sleep(0);
     }
     else
     {
         if (objectID > 0)
         {
             gettingLocationInfo = true;
             TheTCPWrapper.Send(CommandCreator.USE_MAP_OBJECT((uint)objectID, TheMySqlManager.getItemPos(useWithObject)));
             useObjectPathing = true;
             System.Threading.Thread.Sleep(3000);
             TheTCPWrapper.Send(CommandCreator.LOCATE_ME());
         }
         else if (difference(currentLocation.x, toX) + difference(currentLocation.y, toY) < 12)
         {
             useObjectPathing = false;
             string actorName = "";
             pathing.TILE targetLoc = new pathing.TILE();
             targetLoc.x = toX;
             targetLoc.y = toY;
             if (actorInTheWay(targetLoc, out actorName))
             {
                 moveLoc myLoc = new moveLoc();
                 myLoc.x = toX;
                 myLoc.y = toY;
                 myLoc = adjustMove(myLoc);
                 toX = myLoc.x;
                 toY = myLoc.y;
             }
             TheTCPWrapper.Send(CommandCreator.MOVE_TO(toX, toY));
             string moveString = "Moving from " + currentLocation.x + "," + currentLocation.y;
             moveString += " to end " + toX + "," + toY;
             TheTCPWrapper.Send(CommandCreator.SEND_PM(username, moveString));
             keepGoing = false;
             System.Threading.Thread.Sleep(4000);
             TheTCPWrapper.Send(CommandCreator.LOCATE_ME());
             //here here
             if (commandGiven == "home")
             {
                 Settings.IsTradeBot = true;
                 TradeHandler.storageOpen = false;
                 TradeHandler.openingStorage = false;
             }
             else
             {
                 Settings.IsTradeBot = false;
             }
             myPathing.clearMap();
         }
         else
         {
             TheTCPWrapper.Send(CommandCreator.SEND_PM(username, "I cannot move to that location for some reason!"));
         }
     }
 }
 private void makeMoves()
 {
     ActorHandler.position previousPosition = new ActorHandler.position();
     //pathing.TILE previousMove = (pathing.TILE)myMoves[0];
     string moveString = "";
     ActorHandler.position currentPosition = TheActorHandler.GetMyPosition();
     previousPosition = currentPosition;
     //start a loop through the moves array, it's our path
     foreach (pathing.TILE myMove in myMoves)
     {
         //we may want to save the "best" move along the way in case we don't find any moves that suit this condition...
         //if (difference(myMove.x, previousPosition.x) < 12 && difference(myMove.y, previousPosition.y) < 12)
         if (difference(myMove.x, previousPosition.x) + difference(myMove.y, previousPosition.y) < 12)
         {
             continue;
         }
         else
         {
             //this might be the place to check for actors in the way
             //if one's in the way, just keep looking? hrm...
             string actorName = "";
             bool actorInWay = actorInTheWay(myMove, out actorName); ;
             ActorHandler.position myPosition = TheActorHandler.GetMyPosition();
             if (actorInWay)
             {
                 Console.WriteLine("actor in the way: " + actorName + " " + myPosition.x + "," + myPosition.y);
                 TheTCPWrapper.Send(CommandCreator.SEND_PM(username, "trying to move around " + actorName));
                 continue;
             }
         }
         currentPosition = TheActorHandler.GetMyPosition();
         moveString = "Moving from " + currentPosition.x + "," + currentPosition.y;
         moveString += " to " + myMove.x + "," + myMove.y;
         TheTCPWrapper.Send(CommandCreator.SEND_PM(username, moveString));
         TheTCPWrapper.Send(CommandCreator.MOVE_TO(myMove.x, myMove.y));
         //wait until the move is done or the time expires
         DateTime startTime = DateTime.Now;
         bool keepStepping = true;
         while (keepStepping && keepMoving)
         {
             TimeSpan timeElapsed = DateTime.Now - startTime;
             currentPosition = TheActorHandler.GetMyPosition();
             if ((currentPosition.x == myMove.x && currentPosition.y == myMove.y) || timeElapsed.Seconds > 10)
             {
                 //got to the spot, stop moving
                 keepStepping = false;
             }
             Thread.Sleep(10);
         }
         if (currentPosition.x == previousPosition.x && currentPosition.y == previousPosition.y && keepMoving)
         {
             keepMoving = false;
             TheTCPWrapper.Send(CommandCreator.SEND_PM(username, "I seem to be stuck :P"));
         }
         else
         {
             previousPosition = currentPosition;
         }
         if (!keepMoving)
         {
             break;
         }
     }
     //we'e either at the end by now, or we're stuck :P
     if (keepMoving)
     {
         pathing.TILE endMove = new pathing.TILE();
         string actorName = "";
         ActorHandler.position myPosition = TheActorHandler.GetMyPosition();
         endMove.x = destX;
         endMove.y = destY;
         bool actorInWay = actorInTheWay(endMove, out actorName);
         if (actorInWay && actorName.ToLower()!= TheActorHandler.GetUsernameFromID((short)TheActorHandler.MyActorID).ToLower())
         {
             Console.WriteLine("actor in the way at final destination, move next to if possible: " + actorName + " " + destX + "," + destY);
             moveLoc currentLoc = new moveLoc();
             currentLoc.x = endMove.x;
             currentLoc.y = endMove.y;
             currentLoc.distance = 0;
             moveLoc adjustedMove = adjustMove(currentLoc);
             destX = adjustedMove.x;
             destY = adjustedMove.y;
         }
         TheTCPWrapper.Send(CommandCreator.MOVE_TO(destX, destY));
         moveString = "Moving from " + currentPosition.x + "," + currentPosition.y;
         moveString += " to end " + destX + "," + destY;
         TheTCPWrapper.Send(CommandCreator.SEND_PM(username, moveString));
         DateTime startTime = DateTime.Now;
         bool keepStepping = true;
         while (keepStepping && keepMoving)
         {
             TimeSpan timeElapsed = DateTime.Now - startTime;
             currentPosition = TheActorHandler.GetMyPosition();
             if ((currentPosition.x == destX && currentPosition.y == destY) || timeElapsed.Seconds > 10)
             {
                 //got to the spot, stop moving
                 keepStepping = false;
             }
             Thread.Sleep(10);
         }
     }
     else
     {
         TheTCPWrapper.Send(CommandCreator.SEND_PM(username, "Moving aborted."));
     }
     if (objectID > 0 && keepMoving)
     {
         gettingLocationInfo = true;
         TheTCPWrapper.Send(CommandCreator.USE_MAP_OBJECT((uint)objectID, TheMySqlManager.getItemPos(useWithObject)));
         useObjectPathing = true;
         System.Threading.Thread.Sleep(3000);
         TheTCPWrapper.Send(CommandCreator.LOCATE_ME());
         objectID = 0;
         //keepMoving = false;
     }
     else
     {
         useObjectPathing = false;
         keepGoing = false;
         if (keepMoving)
         {
             currentPosition = TheActorHandler.GetMyPosition();
             TheTCPWrapper.Send(CommandCreator.SEND_PM(username, "I should be at my final destination!"));
             //experimental, not sure....
             Thread.Sleep(1000);
             gettingLocationInfo = true;
             TheTCPWrapper.Send(CommandCreator.LOCATE_ME());
             if (commandGiven == "home"  && MainClass.botType == 1)
             {
                 MainClass.atHome = true;
                 Settings.IsTradeBot = true;
             }
         }
         myPathing.clearMap();
     }
 }
 private moveLoc adjustMove(moveLoc currentPos)
 {
     System.Collections.ArrayList myAdjustedMoves = new System.Collections.ArrayList();
     moveLoc adjustedMove = currentPos;
     int increment = 3; //number of squares around the spot to check
     for (int x = -increment; x <= increment; x++)
     {
         for (int y = -increment; y <= increment; y++)
         {
             if (x == 0 && y == 0)
             {
                 continue;
             }
             else
             {
                 //Console.WriteLine("{0},{1}", x, y);
                 moveLoc myMove = new moveLoc();
                 myMove.x = currentPos.x + x;
                 myMove.y = currentPos.y + y;
                 myMove.distance = Math.Max(Math.Abs(x), Math.Abs(y));
                 pathing.TILE myTile = myPathing.getTile(myMove.x, myMove.y);
                 if (myTile.z != 0)
                 {
                     myAdjustedMoves.Add(myMove);
                 }
             }
         }
     }
     mySorter mySort = new mySorter();
     myAdjustedMoves.Sort(mySort);
     if (commandGiven != "storage")
     {
         myAdjustedMoves.Reverse();
     }
     foreach (moveLoc myMove in myAdjustedMoves)
     {
         //check to see if it's open, if so, return it and break
         pathing.TILE tempMove = new pathing.TILE();
         tempMove.x = myMove.x;
         tempMove.y = myMove.y;
         string actorName = "";
         if (!actorInTheWay(tempMove, out actorName))
         {
             //need to validate the move on the map too
             adjustedMove = (moveLoc)myMove;
             break;
         }
         else
         {
             Console.WriteLine("actor in the way: " + actorName + " " + tempMove.x + "," + tempMove.y);
         }
     }
     return adjustedMove;
 }