path.c 12 KB

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