Advertisement
Guest User

Untitled

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