Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.64 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.             lock (this)
  30.             {
  31.                 this.Room = Room;
  32.                 this.Model = Room.Model;
  33.                 this.User = User;
  34.  
  35.                 if (Room == null || Model == null || User == null)
  36.                 {
  37.                     return;
  38.                 }
  39.  
  40.                 if (this.Room.DiagonalWalk)
  41.                 {
  42.                     Movements = new Point[]
  43.                 {
  44.                     // Diagonal Movements
  45.                     new Point(0, 1),
  46.                     new Point(0, -1),
  47.                     new Point(1, 0),
  48.                     new Point(1, 1),
  49.                     new Point(1, -1),
  50.                     new Point(-1, 0),
  51.                     new Point(-1, 1),
  52.                     new Point(-1, -1)
  53.                 };
  54.                 }
  55.                 else
  56.                 {
  57.                     Movements = new Point[]
  58.                 {
  59.                     new Point(0, -1),
  60.                     new Point(1, 0),
  61.                     new Point(0, 1),
  62.                     new Point(-1, 0)
  63.                 };
  64.                 }
  65.  
  66.                 userRecalcX = User.PathRecalcX;
  67.                 userRecalcY = User.PathRecalcY;
  68.  
  69.                 mapSizeX = Model.MapSizeX;
  70.                 mapSizeY = Model.MapSizeY;
  71.  
  72.                 Squares = new CompleteSquare[mapSizeX, mapSizeY];
  73.  
  74.                 for (int x = 0; x < mapSizeX; x++)
  75.                 {
  76.                     for (int y = 0; y < mapSizeY; y++)
  77.                     {
  78.                         Squares[x, y] = new CompleteSquare(x, y);
  79.                     }
  80.                 }
  81.             }
  82.         }
  83.  
  84.         private IEnumerable<Point> GetSquares()
  85.         {
  86.             for (int x = 0; x < mapSizeX; x++)
  87.             {
  88.                 for (int y = 0; y < mapSizeY; y++)
  89.                 {
  90.                     if (ValidCoordinates(x, y)) // Return only valid squares
  91.                     {
  92.                         yield return new Point(x, y);
  93.                     }
  94.                 }
  95.             }
  96.         }
  97.  
  98.         private IEnumerable<Point> ValidMoves(int x, int y)
  99.         {
  100.             foreach (Point movePoint in Movements)
  101.             {
  102.                 int newX = x + movePoint.X;
  103.                 int newY = y + movePoint.Y;
  104.  
  105.                 if (ValidCoordinates(newX, newY) &&
  106.                     IsSquareOpen(newX, newY, true))
  107.                 {
  108.                     yield return new Point(newX, newY);
  109.                 }
  110.             }
  111.         }
  112.  
  113.         public List<Coord> FindPath()
  114.         {
  115.             lock (this)
  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.                 return Path;
  242.             }
  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