path.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "map.h"
  7. #include "battle.h"
  8. #include "../common/nullpo.h"
  9. #include "../common/showmsg.h"
  10. #ifdef MEMWATCH
  11. #include "memwatch.h"
  12. #endif
  13. //#define PATH_STANDALONETEST
  14. #define MAX_HEAP 150
  15. struct tmp_path { short x,y,dist,before,cost,flag;};
  16. #define calc_index(x,y) (((x)+(y)*MAX_WALKPATH) & (MAX_WALKPATH*MAX_WALKPATH-1))
  17. /*==========================================
  18. * 経路探索補助heap push
  19. *------------------------------------------
  20. */
  21. static void push_heap_path(int *heap,struct tmp_path *tp,int index)
  22. {
  23. int i,h;
  24. heap[0]++;
  25. for(h=heap[0]-1,i=(h-1)/2;
  26. h>0 && tp[index].cost<tp[heap[i+1]].cost;
  27. i=(h-1)/2)
  28. heap[h+1]=heap[i+1],h=i;
  29. heap[h+1]=index;
  30. }
  31. /*==========================================
  32. * 経路探索補助heap update
  33. * costが減ったので根の方へ移動
  34. *------------------------------------------
  35. */
  36. static void update_heap_path(int *heap,struct tmp_path *tp,int index)
  37. {
  38. int i,h;
  39. for(h=0;h<heap[0];h++)
  40. if(heap[h+1]==index)
  41. break;
  42. if(h==heap[0]){
  43. ShowError("update_heap_path bug\n");
  44. exit(1);
  45. }
  46. for(i=(h-1)/2;
  47. h>0 && tp[index].cost<tp[heap[i+1]].cost;
  48. i=(h-1)/2)
  49. heap[h+1]=heap[i+1],h=i;
  50. heap[h+1]=index;
  51. }
  52. /*==========================================
  53. * 経路探索補助heap pop
  54. *------------------------------------------
  55. */
  56. static int pop_heap_path(int *heap,struct tmp_path *tp)
  57. {
  58. int i,h,k;
  59. int ret,last;
  60. if(heap[0]<=0)
  61. return -1;
  62. ret=heap[1];
  63. last=heap[heap[0]];
  64. heap[0]--;
  65. for(h=0,k=2;k<heap[0];k=k*2+2){
  66. if(tp[heap[k+1]].cost>tp[heap[k]].cost)
  67. k--;
  68. heap[h+1]=heap[k+1], h=k;
  69. }
  70. if(k==heap[0])
  71. heap[h+1]=heap[k], h=k-1;
  72. for(i=(h-1)/2;
  73. h>0 && tp[heap[i+1]].cost>tp[last].cost;
  74. i=(h-1)/2)
  75. heap[h+1]=heap[i+1],h=i;
  76. heap[h+1]=last;
  77. return ret;
  78. }
  79. /*==========================================
  80. * 現在の点のcost計算
  81. *------------------------------------------
  82. */
  83. static int calc_cost(struct tmp_path *p,int x1,int y1)
  84. {
  85. int xd,yd;
  86. xd=x1-p->x;
  87. if(xd<0) xd=-xd;
  88. yd=y1-p->y;
  89. if(yd<0) yd=-yd;
  90. return (xd+yd)*10+p->dist;
  91. }
  92. /*==========================================
  93. * 必要ならpathを追加/修正する
  94. *------------------------------------------
  95. */
  96. static int add_path(int *heap,struct tmp_path *tp,int x,int y,int dist,int before,int cost)
  97. {
  98. int i;
  99. i=calc_index(x,y);
  100. if(tp[i].x==x && tp[i].y==y){
  101. if(tp[i].dist>dist){
  102. tp[i].dist=dist;
  103. tp[i].before=before;
  104. tp[i].cost=cost;
  105. if(tp[i].flag)
  106. push_heap_path(heap,tp,i);
  107. else
  108. update_heap_path(heap,tp,i);
  109. tp[i].flag=0;
  110. }
  111. return 0;
  112. }
  113. if(tp[i].x || tp[i].y)
  114. return 1;
  115. tp[i].x=x;
  116. tp[i].y=y;
  117. tp[i].dist=dist;
  118. tp[i].before=before;
  119. tp[i].cost=cost;
  120. tp[i].flag=0;
  121. push_heap_path(heap,tp,i);
  122. return 0;
  123. }
  124. /*==========================================
  125. * (x,y)が移動不可能地帯かどうか
  126. * flag 0x10000 遠距離攻撃判定
  127. *------------------------------------------
  128. */
  129. static int can_place(struct map_data *m,int x,int y,int flag)
  130. {
  131. if(map_getcellp(m,x,y,CELL_CHKPASS))
  132. return 1;
  133. if((flag&0x10000)&&map_getcellp(m,x,y,CELL_CHKGROUND))
  134. return 1;
  135. #ifdef CELL_NOSTACK
  136. //Special flag for CELL_NOSTACK systems. Returns true when the given cell is stacked. [Skotlex]
  137. if((flag&0x30000)&&map_getcellp(m,x,y,CELL_CHKSTACK))
  138. return 1;
  139. #endif
  140. return 0;
  141. }
  142. /*==========================================
  143. * (x0,y0)から(x1,y1)へ1歩で移動可能か計算
  144. *------------------------------------------
  145. */
  146. static int can_move(struct map_data *m,int x0,int y0,int x1,int y1,int flag)
  147. {
  148. if(x1<0 || y1<0 || x1>=m->xs || y1>=m->ys)
  149. return 0;
  150. if(flag&0x20000) //Flag to ignore everything, for use with Taekwon's Jump skill currently. [Skotlex]
  151. return 1;
  152. #ifndef CELL_NOSTACK
  153. //In no-stack mode, do not check current cell.
  154. if(!can_place(m,x0,y0,flag))
  155. return 0;
  156. #endif
  157. if(!can_place(m,x1,y1,flag))
  158. return 0;
  159. if(x0==x1 || y0==y1)
  160. return 1;
  161. if(!can_place(m,x0,y1,flag) || !can_place(m,x1,y0,flag))
  162. return 0;
  163. return 1;
  164. }
  165. /*==========================================
  166. * (x0,y0)から(dx,dy)方向へcountセル分
  167. * 吹き飛ばしたあとの座標を所得
  168. *------------------------------------------
  169. */
  170. int path_blownpos(int m,int x0,int y0,int dx,int dy,int count)
  171. {
  172. struct map_data *md;
  173. if(!map[m].gat)
  174. return -1;
  175. md=&map[m];
  176. if(count>15){ // 最大10マスに制限
  177. if(battle_config.error_log)
  178. ShowWarning("path_blownpos: count too many %d !\n",count);
  179. count=15;
  180. }
  181. if(dx>1 || dx<-1 || dy>1 || dy<-1){
  182. if(battle_config.error_log)
  183. ShowError("path_blownpos: illeagal dx=%d or dy=%d !\n",dx,dy);
  184. dx=(dx>=0)?1:((dx<0)?-1:0);
  185. dy=(dy>=0)?1:((dy<0)?-1:0);
  186. }
  187. while( (count--)>0 && (dx!=0 || dy!=0) ){
  188. if( !can_move(md,x0,y0,x0+dx,y0+dy,0) ){
  189. int fx=(dx!=0 && can_move(md,x0,y0,x0+dx,y0,0));
  190. int fy=(dy!=0 && can_move(md,x0,y0,x0,y0+dy,0));
  191. if( fx && fy ){
  192. if(rand()&1) dx=0;
  193. else dy=0;
  194. }
  195. if( !fx ) dx=0;
  196. if( !fy ) dy=0;
  197. }
  198. x0+=dx;
  199. y0+=dy;
  200. }
  201. return (x0<<16)|y0;
  202. }
  203. /*==========================================
  204. * 遠距離攻撃が可能かどうかを返す
  205. *------------------------------------------
  206. */
  207. #define swap(x,y) { int t; t = x; x = y; y = t; }
  208. int path_search_long_real(struct shootpath_data *spd,int m,int x0,int y0,int x1,int y1,int flag)
  209. {
  210. int dx, dy;
  211. int wx = 0, wy = 0;
  212. int weight;
  213. struct map_data *md;
  214. if (!map[m].gat)
  215. return 0;
  216. md = &map[m];
  217. dx = (x1 - x0);
  218. if (dx < 0) {
  219. swap(x0, x1);
  220. swap(y0, y1);
  221. dx = -dx;
  222. }
  223. dy = (y1 - y0);
  224. if (spd) {
  225. spd->rx = spd->ry = 0;
  226. spd->len = 1;
  227. spd->x[0] = x0;
  228. spd->y[0] = y0;
  229. }
  230. if (map_getcellp(md,x1,y1,flag))
  231. return 0;
  232. if (dx > abs(dy)) {
  233. weight = dx;
  234. if (spd)
  235. spd->ry=1;
  236. } else {
  237. weight = abs(y1 - y0);
  238. if (spd)
  239. spd->rx=1;
  240. }
  241. while (x0 != x1 || y0 != y1) {
  242. if (map_getcellp(md,x0,y0,flag))
  243. return 0;
  244. wx += dx;
  245. wy += dy;
  246. if (wx >= weight) {
  247. wx -= weight;
  248. x0 ++;
  249. }
  250. if (wy >= weight) {
  251. wy -= weight;
  252. y0 ++;
  253. } else if (wy < 0) {
  254. wy += weight;
  255. y0 --;
  256. }
  257. if (spd && spd->len<MAX_WALKPATH) {
  258. spd->x[spd->len] = x0;
  259. spd->y[spd->len] = y0;
  260. spd->len++;
  261. }
  262. }
  263. return 1;
  264. }
  265. /*==========================================
  266. * path探索 (x0,y0)->(x1,y1)
  267. *------------------------------------------
  268. */
  269. int path_search_real(struct walkpath_data *wpd,int m,int x0,int y0,int x1,int y1,int flag,int flag2)
  270. {
  271. int heap[MAX_HEAP+1];
  272. struct tmp_path tp[MAX_WALKPATH*MAX_WALKPATH];
  273. int i,rp,x,y;
  274. int xs,ys;
  275. struct map_data *md;
  276. int dx,dy;
  277. nullpo_retr(0, wpd);
  278. if(!map[m].gat)
  279. return -1;
  280. md=&map[m];
  281. #ifdef CELL_NOSTACK
  282. //Do not check starting cell as that would get you stuck.
  283. if(x0<0 || x0>=md->xs || y0<0 || y0>=md->ys)
  284. #else
  285. if(x0<0 || x0>=md->xs || y0<0 || y0>=md->ys || map_getcellp(md,x0,y0,flag2))
  286. #endif
  287. return -1;
  288. if(x1<0 || x1>=md->xs || y1<0 || y1>=md->ys || map_getcellp(md,x1,y1,flag2))
  289. return -1;
  290. // easy
  291. // この内部では、0 <= x+dx < sx, 0 <= y+dy < sy は保証されている
  292. dx = (x1-x0<0) ? -1 : 1;
  293. dy = (y1-y0<0) ? -1 : 1;
  294. for(x=x0,y=y0,i=0;x!=x1 || y!=y1;){
  295. if(i>=sizeof(wpd->path))
  296. return -1;
  297. if(x!=x1 && y!=y1){
  298. if(map_getcellp(md,x+dx,y ,flag2))
  299. break;
  300. if(map_getcellp(md,x ,y+dy,flag2))
  301. break;
  302. if(map_getcellp(md,x+dx,y+dy,flag2))
  303. break;
  304. x+=dx;
  305. y+=dy;
  306. wpd->path[i++]=(dx<0) ? ((dy>0)? 1 : 3) : ((dy<0)? 5 : 7);
  307. } else if(x!=x1){
  308. if(map_getcellp(md,x+dx,y ,flag2))
  309. break;
  310. x+=dx;
  311. wpd->path[i++]=(dx<0) ? 2 : 6;
  312. } else if(y!=y1){
  313. if(map_getcellp(md,x ,y+dy,flag2))
  314. break;
  315. y+=dy;
  316. wpd->path[i++]=(dy>0) ? 0 : 4;
  317. }
  318. }
  319. if (x==x1 && y==y1) { //easy path successful.
  320. wpd->path_len=i;
  321. wpd->path_pos=0;
  322. wpd->path_half=0;
  323. return 0;
  324. }
  325. if(flag&1)
  326. return -1;
  327. memset(tp,0,sizeof(tp));
  328. i=calc_index(x0,y0);
  329. tp[i].x=x0;
  330. tp[i].y=y0;
  331. tp[i].dist=0;
  332. tp[i].before=0;
  333. tp[i].cost=calc_cost(&tp[i],x1,y1);
  334. tp[i].flag=0;
  335. heap[0]=0;
  336. push_heap_path(heap,tp,calc_index(x0,y0));
  337. xs = md->xs-1; // あらかじめ1減算しておく
  338. ys = md->ys-1;
  339. while(1){
  340. int e=0,f=0,dist,cost,dc[4];
  341. if(heap[0]==0)
  342. return -1;
  343. rp = pop_heap_path(heap,tp);
  344. x = tp[rp].x;
  345. y = tp[rp].y;
  346. dist = tp[rp].dist + 10;
  347. cost = tp[rp].cost;
  348. if(x==x1 && y==y1) break;
  349. // dc[0] : y++ の時のコスト増分
  350. // dc[1] : x-- の時のコスト増分
  351. // dc[2] : y-- の時のコスト増分
  352. // dc[3] : x++ の時のコスト増分
  353. if(y < ys && !map_getcellp(md,x ,y+1,flag2)) {
  354. f |= 1; dc[0] = (y >= y1 ? 20 : 0);
  355. e+=add_path(heap,tp,x ,y+1,dist,rp,cost+dc[0]); // (x, y+1)
  356. }
  357. if(x > 0 && !map_getcellp(md,x-1,y ,flag2)) {
  358. f |= 2; dc[1] = (x <= x1 ? 20 : 0);
  359. e+=add_path(heap,tp,x-1,y ,dist,rp,cost+dc[1]); // (x-1, y )
  360. }
  361. if(y > 0 && !map_getcellp(md,x ,y-1,flag2)) {
  362. f |= 4; dc[2] = (y <= y1 ? 20 : 0);
  363. e+=add_path(heap,tp,x ,y-1,dist,rp,cost+dc[2]); // (x , y-1)
  364. }
  365. if(x < xs && !map_getcellp(md,x+1,y ,flag2)) {
  366. f |= 8; dc[3] = (x >= x1 ? 20 : 0);
  367. e+=add_path(heap,tp,x+1,y ,dist,rp,cost+dc[3]); // (x+1, y )
  368. }
  369. if( (f & (2+1)) == (2+1) && !map_getcellp(md,x-1,y+1,flag2))
  370. e+=add_path(heap,tp,x-1,y+1,dist+4,rp,cost+dc[1]+dc[0]-6); // (x-1, y+1)
  371. if( (f & (2+4)) == (2+4) && !map_getcellp(md,x-1,y-1,flag2))
  372. e+=add_path(heap,tp,x-1,y-1,dist+4,rp,cost+dc[1]+dc[2]-6); // (x-1, y-1)
  373. if( (f & (8+4)) == (8+4) && !map_getcellp(md,x+1,y-1,flag2))
  374. e+=add_path(heap,tp,x+1,y-1,dist+4,rp,cost+dc[3]+dc[2]-6); // (x+1, y-1)
  375. if( (f & (8+1)) == (8+1) && !map_getcellp(md,x+1,y+1,flag2))
  376. e+=add_path(heap,tp,x+1,y+1,dist+4,rp,cost+dc[3]+dc[0]-6); // (x+1, y+1)
  377. tp[rp].flag=1;
  378. if(e || heap[0]>=MAX_HEAP-5)
  379. return -1;
  380. }
  381. if(x==x1 && y==y1) {
  382. int len,j;
  383. for(len=0,i=rp;len<100 && i!=calc_index(x0,y0);i=tp[i].before,len++);
  384. if(len==100 || len>=sizeof(wpd->path))
  385. return -1;
  386. wpd->path_len=len;
  387. wpd->path_pos=0;
  388. wpd->path_half=0;
  389. for(i=rp,j=len-1;j>=0;i=tp[i].before,j--) {
  390. int dx = tp[i].x - tp[tp[i].before].x;
  391. int dy = tp[i].y - tp[tp[i].before].y;
  392. int dir;
  393. if( dx == 0 ) {
  394. dir = (dy > 0 ? 0 : 4);
  395. } else if( dx > 0 ) {
  396. dir = (dy == 0 ? 6 : (dy < 0 ? 5 : 7) );
  397. } else {
  398. dir = (dy == 0 ? 2 : (dy > 0 ? 1 : 3) );
  399. }
  400. wpd->path[j] = dir;
  401. }
  402. #if 0
  403. // test
  404. {
  405. int dirx[8]={0,-1,-1,-1,0,1,1,1};
  406. int diry[8]={1,1,0,-1,-1,-1,0,1};
  407. x = x0; y = y0;
  408. for(i = 0; i < wpd->path_len; i++) {
  409. x += dirx[ wpd->path[i] ];
  410. y += diry[ wpd->path[i] ];
  411. if( map_getcellp(md,x,y,flag2) ) {
  412. printf("path_search_real: cannot move(%d, %d)\n", x, y);
  413. return -1;
  414. }
  415. }
  416. if( x != x1 || y != y1 ) {
  417. printf("path_search_real: dest position is wrong. ok:(%d, %d) ng:(%d,%d)\n", x1, y1, x, y);
  418. return -1;
  419. }
  420. }
  421. #endif
  422. return 0;
  423. }
  424. return -1;
  425. }
  426. /*==========================================
  427. * path探索 (x0,y0)->(x1,y1)
  428. *------------------------------------------
  429. */
  430. #ifdef PATH_STANDALONETEST
  431. char gat[64][64]={
  432. {0,0,0,0,0,0,0,0,0,0},
  433. {0,0,0,0,0,0,0,0,0,0},
  434. {0,0,0,0,0,0,0,0,0,0},
  435. {0,0,0,0,0,0,0,0,0,0},
  436. {0,0,0,0,1,0,0,0,0,0},
  437. };
  438. struct map_data map[1];
  439. /*==========================================
  440. * 経路探索ルーチン単体テスト用main関数
  441. *------------------------------------------
  442. */
  443. void main(int argc,char *argv[])
  444. {
  445. struct walkpath_data wpd;
  446. map[0].gat=gat;
  447. map[0].xs=64;
  448. map[0].ys=64;
  449. path_search(&wpd,0,3,4,5,4);
  450. path_search(&wpd,0,5,4,3,4);
  451. path_search(&wpd,0,6,4,3,4);
  452. path_search(&wpd,0,7,4,3,4);
  453. path_search(&wpd,0,4,3,4,5);
  454. path_search(&wpd,0,4,2,4,5);
  455. path_search(&wpd,0,4,1,4,5);
  456. path_search(&wpd,0,4,5,4,3);
  457. path_search(&wpd,0,4,6,4,3);
  458. path_search(&wpd,0,4,7,4,3);
  459. path_search(&wpd,0,7,4,3,4);
  460. path_search(&wpd,0,8,4,3,4);
  461. path_search(&wpd,0,9,4,3,4);
  462. path_search(&wpd,0,10,4,3,4);
  463. path_search(&wpd,0,11,4,3,4);
  464. path_search(&wpd,0,12,4,3,4);
  465. path_search(&wpd,0,13,4,3,4);
  466. path_search(&wpd,0,14,4,3,4);
  467. path_search(&wpd,0,15,4,3,4);
  468. path_search(&wpd,0,16,4,3,4);
  469. path_search(&wpd,0,17,4,3,4);
  470. path_search(&wpd,0,18,4,3,4);
  471. }
  472. #endif