Advertisement
Guest User

Untitled

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