Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.16 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.         int userRecalcX;
  22.         int userRecalcY;
  23.  
  24.         int mapSizeX;
  25.         int mapSizeY;
  26.  
  27.         public Pathfinder(Room Room, RoomUser User)
  28.         {
  29.             this.Room = Room;
  30.             this.Model = Room.Model;
  31.             this.User = User;
  32.  
  33.             if (Room == null || Model == null || User == null || User.RoomId != Room.RoomId)
  34.             {
  35.                 return;
  36.             }
  37.  
  38.             if (Room.DiagonalWalk)
  39.             {
  40.                 Movements = new Point[]
  41.                 {
  42.                     // Diagonal Movements
  43.                     new Point(0, 1),
  44.                     new Point(0, -1),
  45.                     new Point(1, 0),
  46.                     new Point(1, 1),
  47.                     new Point(1, -1),
  48.                     new Point(-1, 0),
  49.                     new Point(-1, 1),
  50.                     new Point(-1, -1)
  51.                 };
  52.             }
  53.             else
  54.             {
  55.                 Movements = new Point[]
  56.                 {
  57.                     new Point(0, -1),
  58.                     new Point(1, 0),
  59.                     new Point(0, 1),
  60.                     new Point(-1, 0)
  61.                 };
  62.             }
  63.  
  64.             userRecalcX = User.PathRecalcX;
  65.             userRecalcY = User.PathRecalcY;
  66.  
  67.             mapSizeX = Model.MapSizeX;
  68.             mapSizeY = Model.MapSizeY;
  69.  
  70.             Squares = new CompleteSquare[mapSizeX, mapSizeY];
  71.  
  72.             for (int x = 0; x < mapSizeX; x++)
  73.             {
  74.                 for (int y = 0; y < mapSizeY; y++)
  75.                 {
  76.                     Squares[x, y] = new CompleteSquare(x, y);
  77.                 }
  78.             }
  79.         }
  80.  
  81.         private IEnumerable<Point> GetSquares()
  82.         {
  83.             for (int x = 0; x < mapSizeX; x++)
  84.             {
  85.                 for (int y = 0; y < mapSizeY; y++)
  86.                 {
  87.                     if (ValidCoordinates(x, y)) // Return only valid squares
  88.                     {
  89.                         yield return new Point(x, y);
  90.                     }
  91.                 }
  92.             }
  93.         }
  94.  
  95.         private IEnumerable<Point> ValidMoves(int x, int y)
  96.         {
  97.             foreach (Point movePoint in Movements)
  98.             {
  99.                 int newX = x + movePoint.X;
  100.                 int newY = y + movePoint.Y;
  101.  
  102.                 if (ValidCoordinates(newX, newY) &&
  103.                     IsSquareOpen(newX, newY, true))
  104.                 {
  105.                     yield return new Point(newX, newY);
  106.                 }
  107.             }
  108.         }
  109.  
  110.         public List<Coord> FindPath()
  111.         {
  112.             if (User.RoomId != Room.RoomId)
  113.             {
  114.                 return null;
  115.             }
  116.  
  117.             // Locate the user, and set the distance to zero
  118.             int UserX = User.X;
  119.             int UserY = User.Y;
  120.  
  121.             Squares[User.X, User.Y].DistanceSteps = 0;
  122.  
  123.             // Get the Map Size and Set a maximum cycle limit
  124.             int maxX = Squares.GetLength(1) - 1;
  125.             int maxY = Squares.GetLength(0) - 1;
  126.             int maxCount = (maxX * maxY);
  127.             int cycleCount = 0;
  128.  
  129.             // Find all possible moves
  130.             while (true)
  131.             {
  132.                 Boolean MadeProgress = false;
  133.  
  134.                 if (cycleCount == maxCount)
  135.                     return null; // Cycle limit reached, return null path
  136.                 cycleCount++;
  137.  
  138.                 if (User == null)
  139.                 {
  140.                     return null;
  141.                 }
  142.  
  143.                 foreach (Point MainPoint in GetSquares())
  144.                 {
  145.                     int x = MainPoint.X;
  146.                     int y = MainPoint.Y;
  147.  
  148.                     if (IsSquareOpen(x, y, true))
  149.                     {
  150.                         int passHere = Squares[x, y].DistanceSteps;
  151.  
  152.                         foreach (Point movePoint in ValidMoves(x, y))
  153.                         {
  154.                             int newX = movePoint.X;
  155.                             int newY = movePoint.Y;
  156.                             int newPass = passHere + 1;
  157.  
  158.                             if (Squares[newX, newY].DistanceSteps > newPass)
  159.                             {
  160.                                 Squares[newX, newY].DistanceSteps = newPass;
  161.                                 MadeProgress = true;
  162.                             }
  163.                         }
  164.                     }
  165.                     else
  166.                     {
  167.                         continue;
  168.                     }
  169.                 }
  170.  
  171.                 if (!MadeProgress)
  172.                 {
  173.                     break;
  174.                 }
  175.             }
  176.  
  177.             // Locate the goal
  178.             int goalX = User.GoalX;
  179.             int goalY = User.GoalY;
  180.  
  181.             if (goalX == -1 || goalY == -1)
  182.             {
  183.                 return null;
  184.             }
  185.  
  186.             cycleCount = 0;
  187.  
  188.             // Now trace the shortest possible route to our goal
  189.             List<Coord> Path = new List<Coord>();
  190.  
  191.             Path.Add(new Coord(User.GoalX, User.GoalY));
  192.  
  193.             while (true)
  194.             {
  195.                 Point lowestPoint = Point.Empty;
  196.                 int lowest = 100;
  197.  
  198.                 if (cycleCount == maxCount)
  199.                     return null;
  200.                 cycleCount++;
  201.  
  202.                 if (User == null)
  203.                 {
  204.                     return null;
  205.                 }
  206.  
  207.                 foreach (Point movePoint in ValidMoves(goalX, goalY))
  208.                 {
  209.                     int count = Squares[movePoint.X, movePoint.Y].DistanceSteps;
  210.  
  211.                     if (count < lowest)
  212.                     {
  213.                         lowest = count;
  214.  
  215.                         lowestPoint.X = movePoint.X;
  216.                         lowestPoint.Y = movePoint.Y;
  217.                     }
  218.                 }
  219.  
  220.                 if (lowest != 100)
  221.                 {
  222.                     Squares[lowestPoint.X, lowestPoint.Y].IsPath = true;
  223.                     goalX = lowestPoint.X;
  224.                     goalY = lowestPoint.Y;
  225.  
  226.                     Path.Add(new Coord(lowestPoint.X, lowestPoint.Y));
  227.                 }
  228.                 else
  229.                 {
  230.                     break;
  231.                 }
  232.  
  233.                 if (goalX == UserX & goalY == UserY)
  234.                 {
  235.                     break;
  236.                 }
  237.  
  238.                 if (Path.Count == 0)
  239.                     return null;
  240.             }
  241.  
  242.             return Path;
  243.         }
  244.  
  245.         private Boolean IsSquareOpen(int x, int y, Boolean CheckHeight)
  246.         {
  247.             if (Room.ValidTile(x, y) && User.AllowOverride)
  248.             {
  249.                 return true;
  250.             }
  251.  
  252.             if (User.X == x && User.Y == y)
  253.             {
  254.                 return true;
  255.             }
  256.  
  257.             bool isLastStep = false;
  258.  
  259.             if (User.GoalX == x && User.GoalY == y)
  260.             {
  261.                 isLastStep = true;
  262.             }
  263.  
  264.             if (!Room.CanWalk(x, y, 0, isLastStep))
  265.             {
  266.                 return false;
  267.             }
  268.  
  269.             return true;
  270.         }
  271.  
  272.         private Boolean ValidCoordinates(int x, int y)
  273.         {
  274.             if (x < 0 || y < 0 || x > mapSizeX || y > mapSizeY)
  275.             {
  276.                 return false;
  277.             }
  278.  
  279.             return true;
  280.         }
  281.     }
  282.  
  283.     class CompleteSquare
  284.     {
  285.         public int x = 0;
  286.         public int y = 0;
  287.  
  288.         int _distanceSteps = 100;
  289.  
  290.         public int DistanceSteps
  291.         {
  292.             get { return _distanceSteps; }
  293.             set { _distanceSteps = value; }
  294.         }
  295.  
  296.         bool _isPath = false;
  297.  
  298.         public bool IsPath
  299.         {
  300.             get { return _isPath; }
  301.             set { _isPath = value; }
  302.         }
  303.  
  304.         public CompleteSquare(int x, int y)
  305.         {
  306.             this.x = x;
  307.             this.y = y;
  308.         }
  309.     }
  310. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement