path.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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/nullpo.h"
  5. #include "../common/showmsg.h"
  6. #include "../common/malloc.h"
  7. #include "map.h"
  8. #include "battle.h"
  9. #ifdef MEMWATCH
  10. #include "memwatch.h"
  11. #endif
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. //#define PATH_STANDALONETEST
  16. #define MAX_HEAP 150
  17. struct tmp_path { short x,y,dist,before,cost,flag;};
  18. #define calc_index(x,y) (((x)+(y)*MAX_WALKPATH) & (MAX_WALKPATH*MAX_WALKPATH-1))
  19. const char walk_choices [3][3] =
  20. {
  21. {1,0,7},
  22. {2,-1,6},
  23. {3,4,5},
  24. };
  25. /*==========================================
  26. * heap push (helper function)
  27. *------------------------------------------*/
  28. static void push_heap_path(int *heap,struct tmp_path *tp,int index)
  29. {
  30. int i,h;
  31. h = heap[0];
  32. heap[0]++;
  33. for( i = (h-1)/2; h > 0 && tp[index].cost < tp[heap[i+1]].cost; i = (h-1)/2 )
  34. heap[h+1] = heap[i+1], h = i;
  35. heap[h+1] = index;
  36. }
  37. /*==========================================
  38. * heap update (helper function)
  39. * costが減ったので根の方へ移動
  40. *------------------------------------------*/
  41. static void update_heap_path(int *heap,struct tmp_path *tp,int index)
  42. {
  43. int i,h;
  44. ARR_FIND( 0, heap[0], h, heap[h+1] == index );
  45. if( h == heap[0] )
  46. {
  47. ShowError("update_heap_path bug\n");
  48. exit(EXIT_FAILURE);
  49. }
  50. for( i = (h-1)/2; h > 0 && tp[index].cost < tp[heap[i+1]].cost; i = (h-1)/2 )
  51. heap[h+1] = heap[i+1], h = i;
  52. heap[h+1] = index;
  53. }
  54. /*==========================================
  55. * heap pop (helper function)
  56. *------------------------------------------*/
  57. static int pop_heap_path(int *heap,struct tmp_path *tp)
  58. {
  59. int i,h,k;
  60. int ret,last;
  61. if( heap[0] <= 0 )
  62. return -1;
  63. ret = heap[1];
  64. last = heap[heap[0]];
  65. heap[0]--;
  66. for( h = 0, k = 2; k < heap[0]; k = k*2+2 )
  67. {
  68. if( tp[heap[k+1]].cost > tp[heap[k]].cost )
  69. k--;
  70. heap[h+1] = heap[k+1], h = k;
  71. }
  72. if( k == heap[0] )
  73. heap[h+1] = heap[k], h = k-1;
  74. for( i = (h-1)/2; h > 0 && tp[heap[i+1]].cost > tp[last].cost; i = (h-1)/2 )
  75. heap[h+1] = heap[i+1], h = i;
  76. heap[h+1]=last;
  77. return ret;
  78. }
  79. /*==========================================
  80. * calculate cost for the specified position
  81. *------------------------------------------*/
  82. static int calc_cost(struct tmp_path *p,int x1,int y1)
  83. {
  84. int xd = abs(x1 - p->x);
  85. int yd = abs(y1 - p->y);
  86. return (xd + yd)*10 + p->dist;
  87. }
  88. /*==========================================
  89. * attach/adjust path if neccessary
  90. *------------------------------------------*/
  91. static int add_path(int *heap,struct tmp_path *tp,int x,int y,int dist,int before,int cost)
  92. {
  93. int i;
  94. i = calc_index(x,y);
  95. if( tp[i].x == x && tp[i].y == y )
  96. {
  97. if( tp[i].dist > dist )
  98. {
  99. tp[i].dist = dist;
  100. tp[i].before = before;
  101. tp[i].cost = cost;
  102. if( tp[i].flag )
  103. push_heap_path(heap,tp,i);
  104. else
  105. update_heap_path(heap,tp,i);
  106. tp[i].flag = 0;
  107. }
  108. return 0;
  109. }
  110. if( tp[i].x || tp[i].y )
  111. return 1;
  112. tp[i].x = x;
  113. tp[i].y = y;
  114. tp[i].dist = dist;
  115. tp[i].before = before;
  116. tp[i].cost = cost;
  117. tp[i].flag = 0;
  118. push_heap_path(heap,tp,i);
  119. return 0;
  120. }
  121. /*==========================================
  122. * is (x,y) passable?
  123. * flag: 0x10000 = ranged attack check
  124. * 0x30000 = stacking check
  125. *------------------------------------------*/
  126. static int can_place(struct map_data *m,int x,int y,int flag)
  127. {
  128. if( map_getcellp(m,x,y,CELL_CHKPASS) )
  129. return 1;
  130. if( (flag&0x10000)&&map_getcellp(m,x,y,CELL_CHKGROUND) )
  131. return 1;
  132. #ifdef CELL_NOSTACK
  133. //Special flag for CELL_NOSTACK systems. Returns true when the given cell is stacked. [Skotlex]
  134. if( (flag&0x30000)&&map_getcellp(m,x,y,CELL_CHKSTACK) )
  135. return 1;
  136. #endif
  137. return 0;
  138. }
  139. /*==========================================
  140. * can you move from (x0,y0) to (x1,y1) in one step?
  141. * (helper function for path_blownpos())
  142. *------------------------------------------*/
  143. static int can_move(struct map_data *m,int x0,int y0,int x1,int y1,int flag)
  144. {
  145. if( x1 < 0 || y1 < 0 || x1 >= m->xs || y1 >= m->ys)
  146. return 0; // out-of-bounds coordinates
  147. if( flag&0x20000 ) //Flag to ignore everything, for use with Taekwon's Jump skill currently. [Skotlex]
  148. return 1;
  149. #ifndef CELL_NOSTACK
  150. //In no-stack mode, do not check current cell.
  151. if( !can_place(m,x0,y0,flag) )
  152. return 0;
  153. #endif
  154. if( !can_place(m,x1,y1,flag) )
  155. return 0;
  156. if( x0 == x1 || y0 == y1 )
  157. return 1;
  158. if( !can_place(m,x0,y1,flag) || !can_place(m,x1,y0,flag) )
  159. return 0;
  160. return 1;
  161. }
  162. /*==========================================
  163. * (x0,y0)から(dx,dy)方向へcountセル分
  164. * 吹き飛ばしたあとの座標を所得
  165. *------------------------------------------*/
  166. int path_blownpos(int m,int x0,int y0,int dx,int dy,int count)
  167. {
  168. struct map_data *md;
  169. if( !map[m].gat )
  170. return -1;
  171. md = &map[m];
  172. if( count>25 ){ //Cap to prevent too much processing...?
  173. ShowWarning("path_blownpos: count too many %d !\n",count);
  174. count=25;
  175. }
  176. if( dx > 1 || dx < -1 || dy > 1 || dy < -1 ){
  177. ShowError("path_blownpos: illegal dx=%d or dy=%d !\n",dx,dy);
  178. dx=(dx>0)?1:((dx<0)?-1:0);
  179. dy=(dy>0)?1:((dy<0)?-1:0);
  180. }
  181. while( (count--)>0 && (dx || dy) )
  182. {
  183. if( !can_move(md,x0,y0,x0+dx,y0+dy,0) ){
  184. int fx=(dx!=0 && can_move(md,x0,y0,x0+dx,y0,0));
  185. int fy=(dy!=0 && can_move(md,x0,y0,x0,y0+dy,0));
  186. if( fx && fy ){
  187. if(rand()&1) dx=0;
  188. else dy=0;
  189. }
  190. if( !fx ) dx=0;
  191. if( !fy ) dy=0;
  192. }
  193. x0+=dx;
  194. y0+=dy;
  195. }
  196. return (x0<<16)|y0;
  197. }
  198. /*==========================================
  199. * is ranged attack from (x0,y0) to (x1,y1) possible?
  200. *------------------------------------------*/
  201. bool path_search_long_real(struct shootpath_data *spd,int m,int x0,int y0,int x1,int y1,cell_t flag)
  202. {
  203. int dx, dy;
  204. int wx = 0, wy = 0;
  205. int weight;
  206. struct map_data *md;
  207. if (!map[m].gat)
  208. return false;
  209. md = &map[m];
  210. dx = (x1 - x0);
  211. if (dx < 0) {
  212. swap(x0, x1);
  213. swap(y0, y1);
  214. dx = -dx;
  215. }
  216. dy = (y1 - y0);
  217. if (spd) {
  218. spd->rx = spd->ry = 0;
  219. spd->len = 1;
  220. spd->x[0] = x0;
  221. spd->y[0] = y0;
  222. }
  223. if (map_getcellp(md,x1,y1,flag))
  224. return false;
  225. if (dx > abs(dy)) {
  226. weight = dx;
  227. if (spd)
  228. spd->ry=1;
  229. } else {
  230. weight = abs(y1 - y0);
  231. if (spd)
  232. spd->rx=1;
  233. }
  234. while (x0 != x1 || y0 != y1) {
  235. if (map_getcellp(md,x0,y0,flag))
  236. return false;
  237. wx += dx;
  238. wy += dy;
  239. if (wx >= weight) {
  240. wx -= weight;
  241. x0 ++;
  242. }
  243. if (wy >= weight) {
  244. wy -= weight;
  245. y0 ++;
  246. } else if (wy < 0) {
  247. wy += weight;
  248. y0 --;
  249. }
  250. if (spd && spd->len<MAX_WALKPATH) {
  251. spd->x[spd->len] = x0;
  252. spd->y[spd->len] = y0;
  253. spd->len++;
  254. }
  255. }
  256. return true;
  257. }
  258. /*==========================================
  259. * path search (x0,y0)->(x1,y1)
  260. * flag: &1 = easy path search only
  261. *------------------------------------------*/
  262. int path_search_real(struct walkpath_data *wpd,int m,int x0,int y0,int x1,int y1,int flag,cell_t flag2)
  263. {
  264. int heap[MAX_HEAP+1];
  265. struct tmp_path tp[MAX_WALKPATH*MAX_WALKPATH];
  266. register int i,x,y,dx,dy;
  267. int rp,xs,ys;
  268. struct map_data *md;
  269. nullpo_retr(0, wpd);
  270. if( !map[m].gat )
  271. return -1;
  272. md = &map[m];
  273. #ifdef CELL_NOSTACK
  274. //Do not check starting cell as that would get you stuck.
  275. if( x0 < 0 || x0 >= md->xs || y0 < 0 || y0 >= md->ys )
  276. #else
  277. if( x0 < 0 || x0 >= md->xs || y0 < 0 || y0 >= md->ys /*|| map_getcellp(md,x0,y0,flag2)*/ )
  278. #endif
  279. return -1;
  280. if( x1 < 0 || x1 >= md->xs || y1 < 0 || y1 >= md->ys || map_getcellp(md,x1,y1,flag2) )
  281. return -1;
  282. // calculate (sgn(x1-x0), sgn(y1-y0))
  283. dx = ((dx = x1-x0)) ? ((dx<0) ? -1 : 1) : 0;
  284. dy = ((dy = y1-y0)) ? ((dy<0) ? -1 : 1) : 0;
  285. // try finding direct path to target
  286. for( x = x0, y = y0, i = 0; i < ARRAYLENGTH(wpd->path); )
  287. {
  288. wpd->path[i++] = walk_choices[-dy + 1][dx + 1];
  289. x += dx;
  290. y += dy;
  291. if( x == x1 ) dx = 0;
  292. if( y == y1 ) dy = 0;
  293. if( !dx && !dy )
  294. break; // success
  295. if( map_getcellp(md,x,y,flag2) )
  296. break; // obstacle = failure
  297. }
  298. if( x == x1 && y == y1 )
  299. { //easy path successful.
  300. wpd->path_len = i;
  301. wpd->path_pos = 0;
  302. wpd->path_half = 0;
  303. return 0;
  304. }
  305. if( flag&1 )
  306. return -1;
  307. memset(tp,0,sizeof(tp));
  308. i=calc_index(x0,y0);
  309. tp[i].x=x0;
  310. tp[i].y=y0;
  311. tp[i].dist=0;
  312. tp[i].before=0;
  313. tp[i].cost=calc_cost(&tp[i],x1,y1);
  314. tp[i].flag=0;
  315. heap[0]=0;
  316. push_heap_path(heap,tp,calc_index(x0,y0));
  317. xs = md->xs-1; // あらかじめ1減算しておく
  318. ys = md->ys-1;
  319. while(1){
  320. int e=0,f=0,dist,cost,dc[4]={0,0,0,0};
  321. if(heap[0]==0)
  322. return -1;
  323. rp = pop_heap_path(heap,tp);
  324. x = tp[rp].x;
  325. y = tp[rp].y;
  326. dist = tp[rp].dist + 10;
  327. cost = tp[rp].cost;
  328. if(x==x1 && y==y1) break;
  329. // dc[0] : y++ の時のコスト増分
  330. // dc[1] : x-- の時のコスト増分
  331. // dc[2] : y-- の時のコスト増分
  332. // dc[3] : x++ の時のコスト増分
  333. if(y < ys && !map_getcellp(md,x ,y+1,flag2)) {
  334. f |= 1; dc[0] = (y >= y1 ? 20 : 0);
  335. e+=add_path(heap,tp,x ,y+1,dist,rp,cost+dc[0]); // (x, y+1)
  336. }
  337. if(x > 0 && !map_getcellp(md,x-1,y ,flag2)) {
  338. f |= 2; dc[1] = (x <= x1 ? 20 : 0);
  339. e+=add_path(heap,tp,x-1,y ,dist,rp,cost+dc[1]); // (x-1, y )
  340. }
  341. if(y > 0 && !map_getcellp(md,x ,y-1,flag2)) {
  342. f |= 4; dc[2] = (y <= y1 ? 20 : 0);
  343. e+=add_path(heap,tp,x ,y-1,dist,rp,cost+dc[2]); // (x , y-1)
  344. }
  345. if(x < xs && !map_getcellp(md,x+1,y ,flag2)) {
  346. f |= 8; dc[3] = (x >= x1 ? 20 : 0);
  347. e+=add_path(heap,tp,x+1,y ,dist,rp,cost+dc[3]); // (x+1, y )
  348. }
  349. if( (f & (2+1)) == (2+1) && !map_getcellp(md,x-1,y+1,flag2))
  350. e+=add_path(heap,tp,x-1,y+1,dist+4,rp,cost+dc[1]+dc[0]-6); // (x-1, y+1)
  351. if( (f & (2+4)) == (2+4) && !map_getcellp(md,x-1,y-1,flag2))
  352. e+=add_path(heap,tp,x-1,y-1,dist+4,rp,cost+dc[1]+dc[2]-6); // (x-1, y-1)
  353. if( (f & (8+4)) == (8+4) && !map_getcellp(md,x+1,y-1,flag2))
  354. e+=add_path(heap,tp,x+1,y-1,dist+4,rp,cost+dc[3]+dc[2]-6); // (x+1, y-1)
  355. if( (f & (8+1)) == (8+1) && !map_getcellp(md,x+1,y+1,flag2))
  356. e+=add_path(heap,tp,x+1,y+1,dist+4,rp,cost+dc[3]+dc[0]-6); // (x+1, y+1)
  357. tp[rp].flag=1;
  358. if(e || heap[0]>=MAX_HEAP-5)
  359. return -1;
  360. }
  361. if(x==x1 && y==y1) {
  362. int len,j;
  363. for(len=0,i=rp;len<100 && i!=calc_index(x0,y0);i=tp[i].before,len++);
  364. if(len==100 || len>=sizeof(wpd->path))
  365. return -1;
  366. wpd->path_len=len;
  367. wpd->path_pos=0;
  368. wpd->path_half=0;
  369. for(i=rp,j=len-1;j>=0;i=tp[i].before,j--) {
  370. int dx = tp[i].x - tp[tp[i].before].x;
  371. int dy = tp[i].y - tp[tp[i].before].y;
  372. int dir;
  373. if( dx == 0 ) {
  374. dir = (dy > 0 ? 0 : 4);
  375. } else if( dx > 0 ) {
  376. dir = (dy == 0 ? 6 : (dy < 0 ? 5 : 7) );
  377. } else {
  378. dir = (dy == 0 ? 2 : (dy > 0 ? 1 : 3) );
  379. }
  380. wpd->path[j] = dir;
  381. }
  382. #if 0
  383. // test
  384. {
  385. int dirx[8]={0,-1,-1,-1,0,1,1,1};
  386. int diry[8]={1,1,0,-1,-1,-1,0,1};
  387. x = x0; y = y0;
  388. for(i = 0; i < wpd->path_len; i++) {
  389. x += dirx[ wpd->path[i] ];
  390. y += diry[ wpd->path[i] ];
  391. if( map_getcellp(md,x,y,flag2) ) {
  392. printf("path_search_real: cannot move(%d, %d)\n", x, y);
  393. return -1;
  394. }
  395. }
  396. if( x != x1 || y != y1 ) {
  397. printf("path_search_real: dest position is wrong. ok:(%d, %d) ng:(%d,%d)\n", x1, y1, x, y);
  398. return -1;
  399. }
  400. }
  401. #endif
  402. return 0;
  403. }
  404. return -1;
  405. }