Advertisement
Guest User

Untitled

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