Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Drawing;
  6.  
  7. using Uber.HabboHotel.GameClients;
  8. using Uber.HabboHotel.Rooms;
  9.  
  10. namespace Uber.HabboHotel.Pathfinding
  11. {
  12.     class Pathfinder
  13.     {
  14.         Point[] Movements;
  15.         CompleteSquare[,] Squares;
  16.  
  17.         Room Room;
  18.         RoomModel Model;
  19.         RoomUser User;
  20.  
  21.         Boolean MadeProgress;
  22.  
  23.         int mapSizeX;
  24.         int mapSizeY;
  25.  
  26.         public Pathfinder(Room Room, RoomUser User)
  27.         {
  28.             this.Room = Room;
  29.             this.Model = Room.Model;
  30.             this.User = User;
  31.  
  32.             if (Room == null || Model == null || User == null)
  33.             {
  34.                 return;
  35.             }
  36.  
  37.             if (Room.DiagonalWalk)
  38.             {
  39.                 InitMovements(true);
  40.             }
  41.             else
  42.             {
  43.                 InitMovements(false);
  44.             }
  45.  
  46.             mapSizeX = Model.MapSizeX;
  47.             mapSizeY = Model.MapSizeY;
  48.  
  49.             Squares = new CompleteSquare[mapSizeX, mapSizeY];
  50.  
  51.             for (int x = 0; x < mapSizeX; x++)
  52.             {
  53.                 for (int y = 0; y < mapSizeY; y++)
  54.                 {
  55.                     Squares[x, y] = new CompleteSquare(x, y);
  56.                 }
  57.             }
  58.         }
  59.  
  60.         private IEnumerable<Point> GetSquares()
  61.         {
  62.             for (int x = 0; x < mapSizeX; x++)
  63.             {
  64.                 for (int y = 0; y < mapSizeY; y++)
  65.                 {
  66.                     yield return new Point(x, y);
  67.                 }
  68.             }
  69.         }
  70.  
  71.         private IEnumerable<Point> ValidMoves(int x, int y)
  72.         {
  73.             foreach (Point movePoint in Movements)
  74.             {
  75.                 int newX = x + movePoint.X;
  76.                 int newY = y + movePoint.Y;
  77.  
  78.                 if (ValidCoordinates(newX, newY) &&
  79.                     IsSquareOpen(newX, newY, true))
  80.                 {
  81.                     yield return new Point(newX, newY);
  82.                 }
  83.             }
  84.         }
  85.  
  86.         public List<Coord> FindPath()
  87.         {
  88.             Object PathObject = new Object();
  89.             lock (PathObject) // This may be able to be removed, it needs testing. This is temp only.
  90.             {
  91.                 // Locate the user, and set the distance to zero
  92.                 int UserX = User.X;
  93.                 int UserY = User.Y;
  94.  
  95.                 Squares[User.X, User.Y].DistanceSteps = 0;
  96.  
  97.                 // Find all possible moves
  98.                 while (User.Path.Count <= 0 && User.PathRecalcNeeded)
  99.                 {
  100.                     MadeProgress = false;
  101.  
  102.                     foreach (Point MainPoint in GetSquares())
  103.                     {
  104.                         int x = MainPoint.X;
  105.                         int y = MainPoint.Y;
  106.  
  107.                         if (IsSquareOpen(x, y, true))
  108.                         {
  109.                             int passHere = Squares[x, y].DistanceSteps;
  110.  
  111.                             foreach (Point movePoint in ValidMoves(x, y))
  112.                             {
  113.                                 int newX = movePoint.X;
  114.                                 int newY = movePoint.Y;
  115.                                 int newPass = passHere + 1;
  116.  
  117.                                 if (Squares[newX, newY].DistanceSteps > newPass)
  118.                                 {
  119.                                     Squares[newX, newY].DistanceSteps = newPass;
  120.                                     MadeProgress = true;
  121.                                 }
  122.                             }
  123.                         }
  124.                     }
  125.  
  126.                     if (!MadeProgress)
  127.                     {
  128.                         break;
  129.                     }
  130.                 }
  131.  
  132.                 // Locate the goal
  133.                 int goalX = User.GoalX;
  134.                 int goalY = User.GoalY;
  135.  
  136.                 if (goalX == -1 || goalY == -1)
  137.                 {
  138.                     return null;
  139.                 }
  140.  
  141.                 // Now trace the shortest possible route to our goal
  142.                 List<Coord> Path = new List<Coord>();
  143.  
  144.                 Path.Add(new Coord(User.GoalX, User.GoalY));
  145.  
  146.                 while (true)
  147.                 {
  148.                     Point lowestPoint = Point.Empty;
  149.                     int lowest = 100;
  150.  
  151.                     foreach (Point movePoint in ValidMoves(goalX, goalY))
  152.                     {
  153.                         int count = Squares[movePoint.X, movePoint.Y].DistanceSteps;
  154.  
  155.                         if (count < lowest)
  156.                         {
  157.                             lowest = count;
  158.  
  159.                             lowestPoint.X = movePoint.X;
  160.                             lowestPoint.Y = movePoint.Y;
  161.                         }
  162.                     }
  163.  
  164.                     if (lowest != 100)
  165.                     {
  166.                         Squares[lowestPoint.X, lowestPoint.Y].IsPath = true;
  167.                         goalX = lowestPoint.X;
  168.                         goalY = lowestPoint.Y;
  169.  
  170.                         Path.Add(new Coord(lowestPoint.X, lowestPoint.Y));
  171.                     }
  172.                     else
  173.                     {
  174.                         break;
  175.                     }
  176.  
  177.                     if (goalX == UserX && goalY == UserY)
  178.                     {
  179.                         break;
  180.                     }
  181.                 }
  182.  
  183.                 return Path;
  184.             }
  185.         }
  186.  
  187.         private Boolean IsSquareOpen(int x, int y, Boolean CheckHeight)
  188.         {
  189.             if (Room.ValidTile(x, y) && User.AllowOverride)
  190.             {
  191.                 return true;
  192.             }
  193.  
  194.             if (User.X == x && User.Y == y)
  195.             {
  196.                 return true;
  197.             }
  198.  
  199.             bool isLastStep = false;
  200.  
  201.             if (User.GoalX == x && User.GoalY == y)
  202.             {
  203.                 isLastStep = true;
  204.             }
  205.  
  206.             if (!Room.CanWalk(x, y, 0, isLastStep))
  207.             {
  208.                 return false;
  209.             }
  210.  
  211.             return true;
  212.         }
  213.  
  214.         private Boolean ValidCoordinates(int x, int y)
  215.         {
  216.             if (x < 0 || y < 0 || x > mapSizeX || y > mapSizeY)
  217.             {
  218.                 return false;
  219.             }
  220.  
  221.             return true;
  222.         }
  223.  
  224.         public void InitMovements(bool Diagonal)
  225.         {
  226.             if (Diagonal)
  227.             {
  228.                 Movements = new Point[]
  229.                 {
  230.                     // Diagonal Movements
  231.                     new Point(0, 1),
  232.                     new Point(0, -1),
  233.                     new Point(1, 0),
  234.                     new Point(1, 1),
  235.                     new Point(1, -1),
  236.                     new Point(-1, 0),
  237.                     new Point(-1, 1),
  238.                     new Point(-1, -1)
  239.                 };
  240.             }
  241.             else
  242.             {
  243.                 Movements = new Point[]
  244.                 {
  245.                     new Point(0, -1),
  246.                     new Point(1, 0),
  247.                     new Point(0, 1),
  248.                     new Point(-1, 0)
  249.                 };
  250.             }
  251.         }
  252.     }
  253.  
  254.     class CompleteSquare
  255.     {
  256.         public int x = 0;
  257.         public int y = 0;
  258.  
  259.         int _distanceSteps = 100;
  260.  
  261.         public int DistanceSteps
  262.         {
  263.             get { return _distanceSteps; }
  264.             set { _distanceSteps = value; }
  265.         }
  266.  
  267.         bool _isPath = false;
  268.  
  269.         public bool IsPath
  270.         {
  271.             get { return _isPath; }
  272.             set { _isPath = value; }
  273.         }
  274.  
  275.         public CompleteSquare(int x, int y)
  276.         {
  277.             this.x = x;
  278.             this.y = y;
  279.         }
  280.     }
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement