Advertisement
Guest User

Untitled

a guest
Aug 5th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.17 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.             for (int x = 0; x < mapSizeX; x++)
  60.             {
  61.                 for (int y = 0; y < mapSizeY; y++)
  62.                 {
  63.                     int passHere = Squares[x, y].DistanceSteps;
  64.  
  65.                     foreach (Coord movePoint in Movements)
  66.                     {
  67.                         int newX = x + movePoint.x;
  68.                         int newY = y + movePoint.y;
  69.  
  70.                         if (ValidCoordinates(newX, newY) &&
  71.                             IsSquareOpen(newX, newY, true))
  72.                         {
  73.                             int newPass = passHere + 1;
  74.  
  75.                             if (Squares[newX, newY].DistanceSteps > newPass)
  76.                             {
  77.                                 Squares[newX, newY].DistanceSteps = newPass;
  78.                             }
  79.                         }
  80.                     }
  81.                 }
  82.             }
  83.  
  84.             // Locate the goal
  85.             int goalX = User.GoalX;
  86.             int goalY = User.GoalY;
  87.  
  88.             if (goalX == -1 || goalY == -1)
  89.             {
  90.                 return null;
  91.             }
  92.  
  93.             // Now trace the shortest possible route to our goal
  94.             List<Coord> Path = new List<Coord>();
  95.  
  96.             Path.Add(new Coord(User.GoalX, User.GoalY));
  97.  
  98.             while (Path.Count < mapSizeX * mapSizeY)
  99.             {
  100.                 Coord lowestPoint = new Coord();
  101.                 int lowest = 100;
  102.  
  103.                 foreach (Coord movePoint in Movements)
  104.                 {
  105.                     int newX = goalX + movePoint.x;
  106.                     int newY = goalY + movePoint.y;
  107.  
  108.                     if (ValidCoordinates(newX, newY) &&
  109.                         IsSquareOpen(newX, newY, true))
  110.                     {
  111.                         int count = Squares[newX, newY].DistanceSteps;
  112.  
  113.                         if (count < lowest)
  114.                         {
  115.                             lowest = count;
  116.  
  117.                             lowestPoint.x = newX;
  118.                             lowestPoint.y = newY;
  119.                         }
  120.                     }
  121.                 }
  122.  
  123.                 if (lowest != 100)
  124.                 {
  125.                     Squares[lowestPoint.x, lowestPoint.y].IsPath = true;
  126.                     goalX = lowestPoint.x;
  127.                     goalY = lowestPoint.y;
  128.  
  129.                     Path.Add(lowestPoint);
  130.                 }
  131.                 else
  132.                 {
  133.                     break;
  134.                 }
  135.  
  136.                 if (goalX == UserX && goalY == UserY)
  137.                 {
  138.                     break;
  139.                 }
  140.             }
  141.  
  142.             return Path;
  143.         }
  144.  
  145.         private Boolean IsSquareOpen(int x, int y, Boolean CheckHeight)
  146.         {
  147.             if (Room.ValidTile(x, y) && User.AllowOverride)
  148.             {
  149.                 return true;
  150.             }
  151.  
  152.             if (User.X == x && User.Y == y)
  153.             {
  154.                 return true;
  155.             }
  156.  
  157.             bool isLastStep = false;
  158.  
  159.             if (User.GoalX == x && User.GoalY == y)
  160.             {
  161.                 isLastStep = true;
  162.             }
  163.  
  164.             if (!Room.CanWalk(x, y, 0, isLastStep))
  165.             {
  166.                 return false;
  167.             }
  168.  
  169.             return true;
  170.         }
  171.  
  172.         private Boolean ValidCoordinates(int x, int y)
  173.         {
  174.             if (x < 0 || y < 0 || x > mapSizeX || y > mapSizeY)
  175.             {
  176.                 return false;
  177.             }
  178.  
  179.             return true;
  180.         }
  181.  
  182.         public void InitMovements(bool Diag)
  183.         {
  184.             if (!Diag)
  185.             {
  186.                 Movements = new Coord[]
  187.                 {
  188.                     new Coord(0, -1),
  189.                     new Coord(1, 0),
  190.                     new Coord(0, 1),
  191.                     new Coord(-1, 0)
  192.                 };
  193.             }
  194.             else
  195.             {
  196.                 Movements = new Coord[]
  197.                 {
  198.                     new Coord(0, 1),
  199.                     new Coord(0, -1),
  200.                     new Coord(1, 0),
  201.                     new Coord(1, 1),
  202.                     new Coord(1, -1),
  203.                     new Coord(-1, 0),
  204.                     new Coord(-1, 1),
  205.                     new Coord(-1, -1)
  206.                 };
  207.             }
  208.         }
  209.     }
  210.  
  211.     class CompleteSquare
  212.     {
  213.         public int x = 0;
  214.         public int y = 0;
  215.  
  216.         int _distanceSteps = 100;
  217.  
  218.         public int DistanceSteps
  219.         {
  220.             get { return _distanceSteps; }
  221.             set { _distanceSteps = value; }
  222.         }
  223.  
  224.         bool _isPath = false;
  225.  
  226.         public bool IsPath
  227.         {
  228.             get { return _isPath; }
  229.             set { _isPath = value; }
  230.         }
  231.  
  232.         public CompleteSquare(int x, int y)
  233.         {
  234.             this.x = x;
  235.             this.y = y;
  236.         }
  237.     }
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement