Advertisement
Guest User

Untitled

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