path.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "../common/cbasetypes.h"
  4. #include "../common/db.h"
  5. #include "../common/malloc.h"
  6. #include "../common/nullpo.h"
  7. #include "../common/random.h"
  8. #include "../common/showmsg.h"
  9. #include "map.h"
  10. #include "battle.h"
  11. #include "path.h"
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <math.h>
  16. #define SET_OPEN 0
  17. #define SET_CLOSED 1
  18. #define PATH_DIR_NORTH 1
  19. #define PATH_DIR_WEST 2
  20. #define PATH_DIR_SOUTH 4
  21. #define PATH_DIR_EAST 8
  22. /// @name Structures and defines for A* pathfinding
  23. /// @{
  24. /// Path node
  25. struct path_node {
  26. struct path_node *parent; ///< pointer to parent (for path reconstruction)
  27. short x; ///< X-coordinate
  28. short y; ///< Y-coordinate
  29. short g_cost; ///< Actual cost from start to this node
  30. short f_cost; ///< g_cost + heuristic(this, goal)
  31. short flag; ///< SET_OPEN / SET_CLOSED
  32. };
  33. /// Binary heap of path nodes
  34. BHEAP_STRUCT_DECL(node_heap, struct path_node*);
  35. static BHEAP_STRUCT_VAR(node_heap, g_open_set); // use static heap for all path calculations
  36. // it get's initialized in do_init_path, freed in do_final_path.
  37. /// Comparator for binary heap of path nodes (minimum cost at top)
  38. #define NODE_MINTOPCMP(i,j) ((i)->f_cost - (j)->f_cost)
  39. #define calc_index(x,y) (((x)+(y)*MAX_WALKPATH) & (MAX_WALKPATH*MAX_WALKPATH-1))
  40. /// Estimates the cost from (x0,y0) to (x1,y1).
  41. /// This is inadmissible (overestimating) heuristic used by game client.
  42. #define heuristic(x0, y0, x1, y1) (MOVE_COST * (abs((x1) - (x0)) + abs((y1) - (y0)))) // Manhattan distance
  43. /// @}
  44. // Translates dx,dy into walking direction
  45. static const unsigned char walk_choices [3][3] =
  46. {
  47. {DIR_NORTHWEST,DIR_NORTH,DIR_NORTHEAST},
  48. {DIR_WEST,DIR_CENTER,DIR_EAST},
  49. {DIR_SOUTHWEST,DIR_SOUTH,DIR_SOUTHEAST},
  50. };
  51. void do_init_path(){
  52. BHEAP_INIT(g_open_set); // [fwi]: BHEAP_STRUCT_VAR already initialized the heap, this is rudendant & just for code-conformance/readability
  53. }//
  54. void do_final_path(){
  55. BHEAP_CLEAR(g_open_set);
  56. }//
  57. /*==========================================
  58. * Find the closest reachable cell, 'count' cells away from (x0,y0) in direction (dx,dy).
  59. * Income after the coordinates of the blow
  60. *------------------------------------------*/
  61. int path_blownpos(int16 m,int16 x0,int16 y0,int16 dx,int16 dy,int count)
  62. {
  63. struct map_data *md;
  64. if( !map[m].cell )
  65. return -1;
  66. md = &map[m];
  67. if( count>25 ){ //Cap to prevent too much processing...?
  68. ShowWarning("path_blownpos: count too many %d !\n",count);
  69. count=25;
  70. }
  71. if( dx > 1 || dx < -1 || dy > 1 || dy < -1 ){
  72. ShowError("path_blownpos: illegal dx=%d or dy=%d !\n",dx,dy);
  73. dx=(dx>0)?1:((dx<0)?-1:0);
  74. dy=(dy>0)?1:((dy<0)?-1:0);
  75. }
  76. while( count > 0 && (dx != 0 || dy != 0) )
  77. {
  78. if( !map_getcellp(md,x0+dx,y0+dy,CELL_CHKPASS) )
  79. {
  80. if (battle_config.path_blown_halt)
  81. break;
  82. else
  83. {// attempt partial movement
  84. int fx = ( dx != 0 && map_getcellp(md,x0+dx,y0,CELL_CHKPASS) );
  85. int fy = ( dy != 0 && map_getcellp(md,x0,y0+dy,CELL_CHKPASS) );
  86. if( fx && fy )
  87. {
  88. if(rnd()&1)
  89. dx=0;
  90. else
  91. dy=0;
  92. }
  93. if( !fx )
  94. dx=0;
  95. if( !fy )
  96. dy=0;
  97. }
  98. }
  99. x0 += dx;
  100. y0 += dy;
  101. count--;
  102. }
  103. return (x0<<16)|y0; //TODO: use 'struct point' here instead?
  104. }
  105. /*==========================================
  106. * is ranged attack from (x0,y0) to (x1,y1) possible?
  107. *------------------------------------------*/
  108. bool path_search_long(struct shootpath_data *spd,int16 m,int16 x0,int16 y0,int16 x1,int16 y1,cell_chk cell)
  109. {
  110. int dx, dy;
  111. int wx = 0, wy = 0;
  112. int weight;
  113. struct map_data *md;
  114. struct shootpath_data s_spd;
  115. if( spd == NULL )
  116. spd = &s_spd; // use dummy output variable
  117. if (!map[m].cell)
  118. return false;
  119. md = &map[m];
  120. dx = (x1 - x0);
  121. if (dx < 0) {
  122. swap(x0, x1);
  123. swap(y0, y1);
  124. dx = -dx;
  125. }
  126. dy = (y1 - y0);
  127. spd->rx = spd->ry = 0;
  128. spd->len = 1;
  129. spd->x[0] = x0;
  130. spd->y[0] = y0;
  131. if (dx > abs(dy)) {
  132. weight = dx;
  133. spd->ry = 1;
  134. } else {
  135. weight = abs(y1 - y0);
  136. spd->rx = 1;
  137. }
  138. while (x0 != x1 || y0 != y1)
  139. {
  140. wx += dx;
  141. wy += dy;
  142. if (wx >= weight) {
  143. wx -= weight;
  144. x0++;
  145. }
  146. if (wy >= weight) {
  147. wy -= weight;
  148. y0++;
  149. } else if (wy < 0) {
  150. wy += weight;
  151. y0--;
  152. }
  153. if( spd->len<MAX_WALKPATH )
  154. {
  155. spd->x[spd->len] = x0;
  156. spd->y[spd->len] = y0;
  157. spd->len++;
  158. }
  159. if ((x0 != x1 || y0 != y1) && map_getcellp(md,x0,y0,cell))
  160. return false;
  161. }
  162. return true;
  163. }
  164. /// @name A* pathfinding related functions
  165. /// @{
  166. /// Pushes path_node to the binary node_heap.
  167. /// Ensures there is enough space in array to store new element.
  168. static void heap_push_node(struct node_heap *heap, struct path_node *node)
  169. {
  170. #ifndef __clang_analyzer__ // TODO: Figure out why clang's static analyzer doesn't like this
  171. BHEAP_ENSURE(*heap, 1, 256);
  172. BHEAP_PUSH2(*heap, node, NODE_MINTOPCMP, swap_ptr);
  173. #endif // __clang_analyzer__
  174. }
  175. /// Updates path_node in the binary node_heap.
  176. static int heap_update_node(struct node_heap *heap, struct path_node *node)
  177. {
  178. int i;
  179. ARR_FIND(0, BHEAP_LENGTH(*heap), i, BHEAP_DATA(*heap)[i] == node);
  180. if (i == BHEAP_LENGTH(*heap)) {
  181. ShowError("heap_update_node: node not found\n");
  182. return 1;
  183. }
  184. BHEAP_UPDATE(*heap, i, NODE_MINTOPCMP, swap_ptr);
  185. return 0;
  186. }
  187. /// Path_node processing in A* pathfinding.
  188. /// Adds new node to heap and updates/re-adds old ones if necessary.
  189. static int add_path(struct node_heap *heap, struct path_node *tp, int16 x, int16 y, int g_cost, struct path_node *parent, int h_cost)
  190. {
  191. int i = calc_index(x, y);
  192. if (tp[i].x == x && tp[i].y == y) { // We processed this node before
  193. if (g_cost < tp[i].g_cost) { // New path to this node is better than old one
  194. // Update costs and parent
  195. tp[i].g_cost = g_cost;
  196. tp[i].parent = parent;
  197. tp[i].f_cost = g_cost + h_cost;
  198. if (tp[i].flag == SET_CLOSED) {
  199. heap_push_node(heap, &tp[i]); // Put it in open set again
  200. }
  201. else if (heap_update_node(heap, &tp[i])) {
  202. return 1;
  203. }
  204. tp[i].flag = SET_OPEN;
  205. }
  206. return 0;
  207. }
  208. if (tp[i].x || tp[i].y) // Index is already taken; see `tp` array FIXME for details
  209. return 1;
  210. // New node
  211. tp[i].x = x;
  212. tp[i].y = y;
  213. tp[i].g_cost = g_cost;
  214. tp[i].parent = parent;
  215. tp[i].f_cost = g_cost + h_cost;
  216. tp[i].flag = SET_OPEN;
  217. heap_push_node(heap, &tp[i]);
  218. return 0;
  219. }
  220. ///@}
  221. /*==========================================
  222. * path search (x0,y0)->(x1,y1)
  223. * wpd: path info will be written here
  224. * flag: &1 = easy path search only
  225. * flag: &2 = call path_search_long instead
  226. * cell: type of obstruction to check for
  227. *
  228. * Note: uses global g_open_set, therefore this method can't be called in parallel or recursivly.
  229. *------------------------------------------*/
  230. bool path_search(struct walkpath_data *wpd, int16 m, int16 x0, int16 y0, int16 x1, int16 y1, int flag, cell_chk cell)
  231. {
  232. register int i, x, y, dx = 0, dy = 0;
  233. struct map_data *md;
  234. struct walkpath_data s_wpd;
  235. if (flag&2)
  236. return path_search_long(NULL, m, x0, y0, x1, y1, cell);
  237. if (wpd == NULL)
  238. wpd = &s_wpd; // use dummy output variable
  239. if (!map[m].cell)
  240. return false;
  241. md = &map[m];
  242. //Do not check starting cell as that would get you stuck.
  243. if (x0 < 0 || x0 >= md->xs || y0 < 0 || y0 >= md->ys /*|| map_getcellp(md,x0,y0,cell)*/)
  244. return false;
  245. // Check destination cell
  246. if (x1 < 0 || x1 >= md->xs || y1 < 0 || y1 >= md->ys || map_getcellp(md,x1,y1,cell))
  247. return false;
  248. if (flag&1) {
  249. // Try finding direct path to target
  250. // Direct path goes diagonally first, then in straight line.
  251. // calculate (sgn(x1-x0), sgn(y1-y0))
  252. dx = ((dx = x1-x0)) ? ((dx<0) ? -1 : 1) : 0;
  253. dy = ((dy = y1-y0)) ? ((dy<0) ? -1 : 1) : 0;
  254. x = x0; // Current position = starting cell
  255. y = y0;
  256. i = 0;
  257. while( i < ARRAYLENGTH(wpd->path) ) {
  258. wpd->path[i] = walk_choices[-dy + 1][dx + 1];
  259. i++;
  260. x += dx; // Advance current position
  261. y += dy;
  262. if( x == x1 ) dx = 0; // destination x reached, no longer move along x-axis
  263. if( y == y1 ) dy = 0; // destination y reached, no longer move along y-axis
  264. if( dx == 0 && dy == 0 )
  265. break; // success
  266. if( map_getcellp(md,x,y,cell) )
  267. break; // obstacle = failure
  268. }
  269. if( x == x1 && y == y1 ) { // easy path successful.
  270. wpd->path_len = i;
  271. wpd->path_pos = 0;
  272. return true;
  273. }
  274. return false; // easy path unsuccessful
  275. } else { // !(flag&1)
  276. // A* (A-star) pathfinding
  277. // We always use A* for finding walkpaths because it is what game client uses.
  278. // Easy pathfinding cuts corners of non-walkable cells, but client always walks around it.
  279. BHEAP_RESET(g_open_set);
  280. // FIXME: This array is too small to ensure all paths shorter than MAX_WALKPATH
  281. // can be found without node collision: calc_index(node1) = calc_index(node2).
  282. // Figure out more proper size or another way to keep track of known nodes.
  283. struct path_node tp[MAX_WALKPATH * MAX_WALKPATH];
  284. struct path_node *current, *it;
  285. int xs = md->xs - 1;
  286. int ys = md->ys - 1;
  287. int len = 0;
  288. int j;
  289. memset(tp, 0, sizeof(tp));
  290. // Start node
  291. i = calc_index(x0, y0);
  292. tp[i].parent = NULL;
  293. tp[i].x = x0;
  294. tp[i].y = y0;
  295. tp[i].g_cost = 0;
  296. tp[i].f_cost = heuristic(x0, y0, x1, y1);
  297. tp[i].flag = SET_OPEN;
  298. heap_push_node(&g_open_set, &tp[i]); // Put start node to 'open' set
  299. for(;;) {
  300. int e = 0; // error flag
  301. // Saves allowed directions for the current cell. Diagonal directions
  302. // are only allowed if both directions around it are allowed. This is
  303. // to prevent cutting corner of nearby wall.
  304. // For example, you can only go NW from the current cell, if you can
  305. // go N *and* you can go W. Otherwise you need to walk around the
  306. // (corner of the) non-walkable cell.
  307. int allowed_dirs = 0;
  308. int g_cost;
  309. if (BHEAP_LENGTH(g_open_set) == 0) {
  310. return false;
  311. }
  312. current = BHEAP_PEEK(g_open_set); // Look for the lowest f_cost node in the 'open' set
  313. BHEAP_POP2(g_open_set, NODE_MINTOPCMP, swap_ptr); // Remove it from 'open' set
  314. x = current->x;
  315. y = current->y;
  316. g_cost = current->g_cost;
  317. current->flag = SET_CLOSED; // Add current node to 'closed' set
  318. if (x == x1 && y == y1) {
  319. break;
  320. }
  321. if (y < ys && !map_getcellp(md, x, y+1, cell)) allowed_dirs |= PATH_DIR_NORTH;
  322. if (y > 0 && !map_getcellp(md, x, y-1, cell)) allowed_dirs |= PATH_DIR_SOUTH;
  323. if (x < xs && !map_getcellp(md, x+1, y, cell)) allowed_dirs |= PATH_DIR_EAST;
  324. if (x > 0 && !map_getcellp(md, x-1, y, cell)) allowed_dirs |= PATH_DIR_WEST;
  325. #define chk_dir(d) ((allowed_dirs & (d)) == (d))
  326. // Process neighbors of current node
  327. if (chk_dir(PATH_DIR_SOUTH|PATH_DIR_EAST) && !map_getcellp(md, x+1, y-1, cell))
  328. e += add_path(&g_open_set, tp, x+1, y-1, g_cost + MOVE_DIAGONAL_COST, current, heuristic(x+1, y-1, x1, y1)); // (x+1, y-1) 5
  329. if (chk_dir(PATH_DIR_EAST))
  330. e += add_path(&g_open_set, tp, x+1, y, g_cost + MOVE_COST, current, heuristic(x+1, y, x1, y1)); // (x+1, y) 6
  331. if (chk_dir(PATH_DIR_NORTH|PATH_DIR_EAST) && !map_getcellp(md, x+1, y+1, cell))
  332. e += add_path(&g_open_set, tp, x+1, y+1, g_cost + MOVE_DIAGONAL_COST, current, heuristic(x+1, y+1, x1, y1)); // (x+1, y+1) 7
  333. if (chk_dir(PATH_DIR_NORTH))
  334. e += add_path(&g_open_set, tp, x, y+1, g_cost + MOVE_COST, current, heuristic(x, y+1, x1, y1)); // (x, y+1) 0
  335. if (chk_dir(PATH_DIR_NORTH|PATH_DIR_WEST) && !map_getcellp(md, x-1, y+1, cell))
  336. e += add_path(&g_open_set, tp, x-1, y+1, g_cost + MOVE_DIAGONAL_COST, current, heuristic(x-1, y+1, x1, y1)); // (x-1, y+1) 1
  337. if (chk_dir(PATH_DIR_WEST))
  338. e += add_path(&g_open_set, tp, x-1, y, g_cost + MOVE_COST, current, heuristic(x-1, y, x1, y1)); // (x-1, y) 2
  339. if (chk_dir(PATH_DIR_SOUTH|PATH_DIR_WEST) && !map_getcellp(md, x-1, y-1, cell))
  340. e += add_path(&g_open_set, tp, x-1, y-1, g_cost + MOVE_DIAGONAL_COST, current, heuristic(x-1, y-1, x1, y1)); // (x-1, y-1) 3
  341. if (chk_dir(PATH_DIR_SOUTH))
  342. e += add_path(&g_open_set, tp, x, y-1, g_cost + MOVE_COST, current, heuristic(x, y-1, x1, y1)); // (x, y-1) 4
  343. #undef chk_dir
  344. if (e) {
  345. return false;
  346. }
  347. }
  348. for (it = current; it->parent != NULL; it = it->parent, len++);
  349. if (len > sizeof(wpd->path))
  350. return false;
  351. // Recreate path
  352. wpd->path_len = len;
  353. wpd->path_pos = 0;
  354. for (it = current, j = len-1; j >= 0; it = it->parent, j--) {
  355. dx = it->x - it->parent->x;
  356. dy = it->y - it->parent->y;
  357. wpd->path[j] = walk_choices[-dy + 1][dx + 1];
  358. }
  359. return true;
  360. } // A* end
  361. return false;
  362. }
  363. //Distance functions, taken from http://www.flipcode.com/articles/article_fastdistance.shtml
  364. bool check_distance(int dx, int dy, int distance)
  365. {
  366. #ifdef CIRCULAR_AREA
  367. //In this case, we just do a square comparison. Add 1 tile grace for diagonal range checks.
  368. return (dx*dx + dy*dy <= distance*distance + (dx&&dy?1:0));
  369. #else
  370. if (dx < 0) dx = -dx;
  371. if (dy < 0) dy = -dy;
  372. return ((dx<dy?dy:dx) <= distance);
  373. #endif
  374. }
  375. unsigned int distance(int dx, int dy)
  376. {
  377. #ifdef CIRCULAR_AREA
  378. unsigned int min, max;
  379. if ( dx < 0 ) dx = -dx;
  380. if ( dy < 0 ) dy = -dy;
  381. //There appears to be something wrong with the approximation below when either dx/dy is 0! [Skotlex]
  382. if ( dx == 0 ) return dy;
  383. if ( dy == 0 ) return dx;
  384. if ( dx < dy )
  385. {
  386. min = dx;
  387. max = dy;
  388. } else {
  389. min = dy;
  390. max = dx;
  391. }
  392. // coefficients equivalent to ( 123/128 * max ) and ( 51/128 * min )
  393. return ((( max << 8 ) + ( max << 3 ) - ( max << 4 ) - ( max << 1 ) +
  394. ( min << 7 ) - ( min << 5 ) + ( min << 3 ) - ( min << 1 )) >> 8 );
  395. #else
  396. if (dx < 0) dx = -dx;
  397. if (dy < 0) dy = -dy;
  398. return (dx<dy?dy:dx);
  399. #endif
  400. }
  401. /**
  402. * The client uses a circular distance instead of the square one. The circular distance
  403. * is only used by units sending their attack commands via the client (not monsters).
  404. * @param dx: Horizontal distance
  405. * @param dy: Vertical distance
  406. * @param distance: Distance to check against
  407. * @return Within distance(1); Not within distance(0);
  408. */
  409. bool check_distance_client(int dx, int dy, int distance)
  410. {
  411. if(distance < 0) distance = 0;
  412. return (distance_client(dx,dy) <= distance);
  413. }
  414. /**
  415. * The client uses a circular distance instead of the square one. The circular distance
  416. * is only used by units sending their attack commands via the client (not monsters).
  417. * @param dx: Horizontal distance
  418. * @param dy: Vertical distance
  419. * @return Circular distance
  420. */
  421. int distance_client(int dx, int dy)
  422. {
  423. double temp_dist = sqrt((double)(dx*dx + dy*dy));
  424. //Bonus factor used by client
  425. //This affects even horizontal/vertical lines so they are one cell longer than expected
  426. temp_dist -= 0.0625;
  427. if(temp_dist < 0) temp_dist = 0;
  428. return ((int)temp_dist);
  429. }