Advertisement
Guest User

Untitled

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