path.c 11 KB

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