Advertisement
Guest User

Untitled

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