Advertisement
Guest User

Untitled

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