[1] | 1 | // Copyright (c) Athena Dev Teams - Licensed under GNU GPL |
---|
| 2 | // For more information, see LICENCE in the main folder |
---|
| 3 | |
---|
| 4 | #include "../common/cbasetypes.h" |
---|
| 5 | #include "../common/nullpo.h" |
---|
| 6 | #include "../common/showmsg.h" |
---|
| 7 | #include "../common/malloc.h" |
---|
| 8 | #include "map.h" |
---|
| 9 | #include "battle.h" |
---|
| 10 | #include "path.h" |
---|
| 11 | |
---|
| 12 | #include <stdio.h> |
---|
| 13 | #include <stdlib.h> |
---|
| 14 | #include <string.h> |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | #define MAX_HEAP 150 |
---|
| 18 | |
---|
| 19 | struct tmp_path { short x,y,dist,before,cost,flag;}; |
---|
| 20 | #define calc_index(x,y) (((x)+(y)*MAX_WALKPATH) & (MAX_WALKPATH*MAX_WALKPATH-1)) |
---|
| 21 | |
---|
| 22 | const char walk_choices [3][3] = |
---|
| 23 | { |
---|
| 24 | {1,0,7}, |
---|
| 25 | {2,-1,6}, |
---|
| 26 | {3,4,5}, |
---|
| 27 | }; |
---|
| 28 | |
---|
| 29 | /*========================================== |
---|
| 30 | * heap push (helper function) |
---|
| 31 | *------------------------------------------*/ |
---|
| 32 | static void push_heap_path(int *heap,struct tmp_path *tp,int index) |
---|
| 33 | { |
---|
| 34 | int i,h; |
---|
| 35 | |
---|
| 36 | h = heap[0]; |
---|
| 37 | heap[0]++; |
---|
| 38 | |
---|
| 39 | for( i = (h-1)/2; h > 0 && tp[index].cost < tp[heap[i+1]].cost; i = (h-1)/2 ) |
---|
| 40 | heap[h+1] = heap[i+1], h = i; |
---|
| 41 | |
---|
| 42 | heap[h+1] = index; |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | /*========================================== |
---|
| 46 | * heap update (helper function) |
---|
| 47 | * costªžÁœÌŪÌûÖÚ® |
---|
| 48 | *------------------------------------------*/ |
---|
| 49 | static void update_heap_path(int *heap,struct tmp_path *tp,int index) |
---|
| 50 | { |
---|
| 51 | int i,h; |
---|
| 52 | |
---|
| 53 | ARR_FIND( 0, heap[0], h, heap[h+1] == index ); |
---|
| 54 | if( h == heap[0] ) |
---|
| 55 | { |
---|
| 56 | ShowError("update_heap_path bug\n"); |
---|
| 57 | exit(EXIT_FAILURE); |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | for( i = (h-1)/2; h > 0 && tp[index].cost < tp[heap[i+1]].cost; i = (h-1)/2 ) |
---|
| 61 | heap[h+1] = heap[i+1], h = i; |
---|
| 62 | |
---|
| 63 | heap[h+1] = index; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | /*========================================== |
---|
| 67 | * heap pop (helper function) |
---|
| 68 | *------------------------------------------*/ |
---|
| 69 | static int pop_heap_path(int *heap,struct tmp_path *tp) |
---|
| 70 | { |
---|
| 71 | int i,h,k; |
---|
| 72 | int ret,last; |
---|
| 73 | |
---|
| 74 | if( heap[0] <= 0 ) |
---|
| 75 | return -1; |
---|
| 76 | ret = heap[1]; |
---|
| 77 | last = heap[heap[0]]; |
---|
| 78 | heap[0]--; |
---|
| 79 | |
---|
| 80 | for( h = 0, k = 2; k < heap[0]; k = k*2+2 ) |
---|
| 81 | { |
---|
| 82 | if( tp[heap[k+1]].cost > tp[heap[k]].cost ) |
---|
| 83 | k--; |
---|
| 84 | heap[h+1] = heap[k+1], h = k; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | if( k == heap[0] ) |
---|
| 88 | heap[h+1] = heap[k], h = k-1; |
---|
| 89 | |
---|
| 90 | for( i = (h-1)/2; h > 0 && tp[heap[i+1]].cost > tp[last].cost; i = (h-1)/2 ) |
---|
| 91 | heap[h+1] = heap[i+1], h = i; |
---|
| 92 | |
---|
| 93 | heap[h+1]=last; |
---|
| 94 | |
---|
| 95 | return ret; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | /*========================================== |
---|
| 99 | * calculate cost for the specified position |
---|
| 100 | *------------------------------------------*/ |
---|
| 101 | static int calc_cost(struct tmp_path *p,int x1,int y1) |
---|
| 102 | { |
---|
| 103 | int xd = abs(x1 - p->x); |
---|
| 104 | int yd = abs(y1 - p->y); |
---|
| 105 | return (xd + yd)*10 + p->dist; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | /*========================================== |
---|
| 109 | * attach/adjust path if neccessary |
---|
| 110 | *------------------------------------------*/ |
---|
| 111 | static int add_path(int *heap,struct tmp_path *tp,int x,int y,int dist,int before,int cost) |
---|
| 112 | { |
---|
| 113 | int i; |
---|
| 114 | |
---|
| 115 | i = calc_index(x,y); |
---|
| 116 | |
---|
| 117 | if( tp[i].x == x && tp[i].y == y ) |
---|
| 118 | { |
---|
| 119 | if( tp[i].dist > dist ) |
---|
| 120 | { |
---|
| 121 | tp[i].dist = dist; |
---|
| 122 | tp[i].before = before; |
---|
| 123 | tp[i].cost = cost; |
---|
| 124 | if( tp[i].flag ) |
---|
| 125 | push_heap_path(heap,tp,i); |
---|
| 126 | else |
---|
| 127 | update_heap_path(heap,tp,i); |
---|
| 128 | tp[i].flag = 0; |
---|
| 129 | } |
---|
| 130 | return 0; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | if( tp[i].x || tp[i].y ) |
---|
| 134 | return 1; |
---|
| 135 | |
---|
| 136 | tp[i].x = x; |
---|
| 137 | tp[i].y = y; |
---|
| 138 | tp[i].dist = dist; |
---|
| 139 | tp[i].before = before; |
---|
| 140 | tp[i].cost = cost; |
---|
| 141 | tp[i].flag = 0; |
---|
| 142 | push_heap_path(heap,tp,i); |
---|
| 143 | |
---|
| 144 | return 0; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | /*========================================== |
---|
| 148 | * Find the closest reachable cell, 'count' cells away from (x0,y0) in direction (dx,dy). |
---|
| 149 | * |
---|
| 150 | * «òεœ ÆÌÀWðŸ |
---|
| 151 | *------------------------------------------*/ |
---|
| 152 | int path_blownpos(int m,int x0,int y0,int dx,int dy,int count) |
---|
| 153 | { |
---|
| 154 | struct map_data *md; |
---|
| 155 | |
---|
| 156 | if( !map[m].cell ) |
---|
| 157 | return -1; |
---|
| 158 | md = &map[m]; |
---|
| 159 | |
---|
| 160 | if( count>25 ){ //Cap to prevent too much processing...? |
---|
| 161 | ShowWarning("path_blownpos: count too many %d !\n",count); |
---|
| 162 | count=25; |
---|
| 163 | } |
---|
| 164 | if( dx > 1 || dx < -1 || dy > 1 || dy < -1 ){ |
---|
| 165 | ShowError("path_blownpos: illegal dx=%d or dy=%d !\n",dx,dy); |
---|
| 166 | dx=(dx>0)?1:((dx<0)?-1:0); |
---|
| 167 | dy=(dy>0)?1:((dy<0)?-1:0); |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | while( count > 0 && (dx != 0 || dy != 0) ) |
---|
| 171 | { |
---|
| 172 | if( !map_getcellp(md,x0+dx,y0+dy,CELL_CHKPASS) ) |
---|
| 173 | {// attempt partial movement |
---|
| 174 | int fx = ( dx != 0 && map_getcellp(md,x0+dx,y0,CELL_CHKPASS) ); |
---|
| 175 | int fy = ( dy != 0 && map_getcellp(md,x0,y0+dy,CELL_CHKPASS) ); |
---|
| 176 | if( fx && fy ) |
---|
| 177 | { |
---|
| 178 | if(rand()&1) |
---|
| 179 | dx=0; |
---|
| 180 | else |
---|
| 181 | dy=0; |
---|
| 182 | } |
---|
| 183 | if( !fx ) |
---|
| 184 | dx=0; |
---|
| 185 | if( !fy ) |
---|
| 186 | dy=0; |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | x0 += dx; |
---|
| 190 | y0 += dy; |
---|
| 191 | count--; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | return (x0<<16)|y0; //TODO: use 'struct point' here instead? |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | /*========================================== |
---|
| 198 | * is ranged attack from (x0,y0) to (x1,y1) possible? |
---|
| 199 | *------------------------------------------*/ |
---|
| 200 | bool path_search_long(struct shootpath_data *spd,int m,int x0,int y0,int x1,int y1,cell_chk cell) |
---|
| 201 | { |
---|
| 202 | int dx, dy; |
---|
| 203 | int wx = 0, wy = 0; |
---|
| 204 | int weight; |
---|
| 205 | struct map_data *md; |
---|
| 206 | struct shootpath_data s_spd; |
---|
| 207 | |
---|
| 208 | if( spd == NULL ) |
---|
| 209 | spd = &s_spd; // use dummy output variable |
---|
| 210 | |
---|
| 211 | if (!map[m].cell) |
---|
| 212 | return false; |
---|
| 213 | md = &map[m]; |
---|
| 214 | |
---|
| 215 | dx = (x1 - x0); |
---|
| 216 | if (dx < 0) { |
---|
| 217 | swap(x0, x1); |
---|
| 218 | swap(y0, y1); |
---|
| 219 | dx = -dx; |
---|
| 220 | } |
---|
| 221 | dy = (y1 - y0); |
---|
| 222 | |
---|
| 223 | spd->rx = spd->ry = 0; |
---|
| 224 | spd->len = 1; |
---|
| 225 | spd->x[0] = x0; |
---|
| 226 | spd->y[0] = y0; |
---|
| 227 | |
---|
| 228 | if (map_getcellp(md,x1,y1,cell)) |
---|
| 229 | return false; |
---|
| 230 | |
---|
| 231 | if (dx > abs(dy)) { |
---|
| 232 | weight = dx; |
---|
| 233 | spd->ry = 1; |
---|
| 234 | } else { |
---|
| 235 | weight = abs(y1 - y0); |
---|
| 236 | spd->rx = 1; |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | while (x0 != x1 || y0 != y1) |
---|
| 240 | { |
---|
| 241 | if (map_getcellp(md,x0,y0,cell)) |
---|
| 242 | return false; |
---|
| 243 | wx += dx; |
---|
| 244 | wy += dy; |
---|
| 245 | if (wx >= weight) { |
---|
| 246 | wx -= weight; |
---|
| 247 | x0++; |
---|
| 248 | } |
---|
| 249 | if (wy >= weight) { |
---|
| 250 | wy -= weight; |
---|
| 251 | y0++; |
---|
| 252 | } else if (wy < 0) { |
---|
| 253 | wy += weight; |
---|
| 254 | y0--; |
---|
| 255 | } |
---|
| 256 | if( spd->len<MAX_WALKPATH ) |
---|
| 257 | { |
---|
| 258 | spd->x[spd->len] = x0; |
---|
| 259 | spd->y[spd->len] = y0; |
---|
| 260 | spd->len++; |
---|
| 261 | } |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | return true; |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | /*========================================== |
---|
| 268 | * path search (x0,y0)->(x1,y1) |
---|
| 269 | * wpd: path info will be written here |
---|
| 270 | * flag: &1 = easy path search only |
---|
| 271 | * cell: type of obstruction to check for |
---|
| 272 | *------------------------------------------*/ |
---|
| 273 | bool path_search(struct walkpath_data *wpd,int m,int x0,int y0,int x1,int y1,int flag,cell_chk cell) |
---|
| 274 | { |
---|
| 275 | int heap[MAX_HEAP+1]; |
---|
| 276 | struct tmp_path tp[MAX_WALKPATH*MAX_WALKPATH]; |
---|
| 277 | register int i,j,len,x,y,dx,dy; |
---|
| 278 | int rp,xs,ys; |
---|
| 279 | struct map_data *md; |
---|
| 280 | struct walkpath_data s_wpd; |
---|
| 281 | |
---|
| 282 | if( wpd == NULL ) |
---|
| 283 | wpd = &s_wpd; // use dummy output variable |
---|
| 284 | |
---|
| 285 | if( !map[m].cell ) |
---|
| 286 | return false; |
---|
| 287 | md = &map[m]; |
---|
| 288 | |
---|
| 289 | #ifdef CELL_NOSTACK |
---|
| 290 | //Do not check starting cell as that would get you stuck. |
---|
| 291 | if( x0 < 0 || x0 >= md->xs || y0 < 0 || y0 >= md->ys ) |
---|
| 292 | #else |
---|
| 293 | if( x0 < 0 || x0 >= md->xs || y0 < 0 || y0 >= md->ys /*|| map_getcellp(md,x0,y0,cell)*/ ) |
---|
| 294 | #endif |
---|
| 295 | return false; |
---|
| 296 | if( x1 < 0 || x1 >= md->xs || y1 < 0 || y1 >= md->ys || map_getcellp(md,x1,y1,cell) ) |
---|
| 297 | return false; |
---|
| 298 | |
---|
| 299 | // calculate (sgn(x1-x0), sgn(y1-y0)) |
---|
| 300 | dx = ((dx = x1-x0)) ? ((dx<0) ? -1 : 1) : 0; |
---|
| 301 | dy = ((dy = y1-y0)) ? ((dy<0) ? -1 : 1) : 0; |
---|
| 302 | |
---|
| 303 | // try finding direct path to target |
---|
| 304 | x = x0; |
---|
| 305 | y = y0; |
---|
| 306 | i = 0; |
---|
| 307 | while( i < ARRAYLENGTH(wpd->path) ) |
---|
| 308 | { |
---|
| 309 | wpd->path[i] = walk_choices[-dy + 1][dx + 1]; |
---|
| 310 | i++; |
---|
| 311 | |
---|
| 312 | x += dx; |
---|
| 313 | y += dy; |
---|
| 314 | |
---|
| 315 | if( x == x1 ) dx = 0; |
---|
| 316 | if( y == y1 ) dy = 0; |
---|
| 317 | |
---|
| 318 | if( dx == 0 && dy == 0 ) |
---|
| 319 | break; // success |
---|
| 320 | if( map_getcellp(md,x,y,cell) ) |
---|
| 321 | break; // obstacle = failure |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | if( x == x1 && y == y1 ) |
---|
| 325 | { //easy path successful. |
---|
| 326 | wpd->path_len = i; |
---|
| 327 | wpd->path_pos = 0; |
---|
| 328 | return true; |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | if( flag&1 ) |
---|
| 332 | return false; |
---|
| 333 | |
---|
| 334 | memset(tp,0,sizeof(tp)); |
---|
| 335 | |
---|
| 336 | i=calc_index(x0,y0); |
---|
| 337 | tp[i].x=x0; |
---|
| 338 | tp[i].y=y0; |
---|
| 339 | tp[i].dist=0; |
---|
| 340 | tp[i].before=0; |
---|
| 341 | tp[i].cost=calc_cost(&tp[i],x1,y1); |
---|
| 342 | tp[i].flag=0; |
---|
| 343 | heap[0]=0; |
---|
| 344 | push_heap_path(heap,tp,calc_index(x0,y0)); |
---|
| 345 | xs = md->xs-1; // ç©¶ßPžZµÄš |
---|
| 346 | ys = md->ys-1; |
---|
| 347 | |
---|
| 348 | while(1) |
---|
| 349 | { |
---|
| 350 | int e=0,f=0,dist,cost,dc[4]={0,0,0,0}; |
---|
| 351 | |
---|
| 352 | if(heap[0]==0) |
---|
| 353 | return false; |
---|
| 354 | rp = pop_heap_path(heap,tp); |
---|
| 355 | x = tp[rp].x; |
---|
| 356 | y = tp[rp].y; |
---|
| 357 | dist = tp[rp].dist + 10; |
---|
| 358 | cost = tp[rp].cost; |
---|
| 359 | |
---|
| 360 | if(x==x1 && y==y1) |
---|
| 361 | break; |
---|
| 362 | |
---|
| 363 | // dc[0] : y++ ÌÌRXgª |
---|
| 364 | // dc[1] : x-- ÌÌRXgª |
---|
| 365 | // dc[2] : y-- ÌÌRXgª |
---|
| 366 | // dc[3] : x++ ÌÌRXgª |
---|
| 367 | |
---|
| 368 | if(y < ys && !map_getcellp(md,x ,y+1,cell)) { |
---|
| 369 | f |= 1; dc[0] = (y >= y1 ? 20 : 0); |
---|
| 370 | e+=add_path(heap,tp,x ,y+1,dist,rp,cost+dc[0]); // (x, y+1) |
---|
| 371 | } |
---|
| 372 | if(x > 0 && !map_getcellp(md,x-1,y ,cell)) { |
---|
| 373 | f |= 2; dc[1] = (x <= x1 ? 20 : 0); |
---|
| 374 | e+=add_path(heap,tp,x-1,y ,dist,rp,cost+dc[1]); // (x-1, y ) |
---|
| 375 | } |
---|
| 376 | if(y > 0 && !map_getcellp(md,x ,y-1,cell)) { |
---|
| 377 | f |= 4; dc[2] = (y <= y1 ? 20 : 0); |
---|
| 378 | e+=add_path(heap,tp,x ,y-1,dist,rp,cost+dc[2]); // (x , y-1) |
---|
| 379 | } |
---|
| 380 | if(x < xs && !map_getcellp(md,x+1,y ,cell)) { |
---|
| 381 | f |= 8; dc[3] = (x >= x1 ? 20 : 0); |
---|
| 382 | e+=add_path(heap,tp,x+1,y ,dist,rp,cost+dc[3]); // (x+1, y ) |
---|
| 383 | } |
---|
| 384 | if( (f & (2+1)) == (2+1) && !map_getcellp(md,x-1,y+1,cell)) |
---|
| 385 | e+=add_path(heap,tp,x-1,y+1,dist+4,rp,cost+dc[1]+dc[0]-6); // (x-1, y+1) |
---|
| 386 | if( (f & (2+4)) == (2+4) && !map_getcellp(md,x-1,y-1,cell)) |
---|
| 387 | e+=add_path(heap,tp,x-1,y-1,dist+4,rp,cost+dc[1]+dc[2]-6); // (x-1, y-1) |
---|
| 388 | if( (f & (8+4)) == (8+4) && !map_getcellp(md,x+1,y-1,cell)) |
---|
| 389 | e+=add_path(heap,tp,x+1,y-1,dist+4,rp,cost+dc[3]+dc[2]-6); // (x+1, y-1) |
---|
| 390 | if( (f & (8+1)) == (8+1) && !map_getcellp(md,x+1,y+1,cell)) |
---|
| 391 | e+=add_path(heap,tp,x+1,y+1,dist+4,rp,cost+dc[3]+dc[0]-6); // (x+1, y+1) |
---|
| 392 | tp[rp].flag=1; |
---|
| 393 | if(e || heap[0]>=MAX_HEAP-5) |
---|
| 394 | return false; |
---|
| 395 | } |
---|
| 396 | |
---|
| 397 | if( !(x==x1 && y==y1) ) // will never happen... |
---|
| 398 | return false; |
---|
| 399 | |
---|
| 400 | for(len=0,i=rp;len<100 && i!=calc_index(x0,y0);i=tp[i].before,len++); |
---|
| 401 | if(len==100 || len>=sizeof(wpd->path)) |
---|
| 402 | return false; |
---|
| 403 | |
---|
| 404 | wpd->path_len = len; |
---|
| 405 | wpd->path_pos = 0; |
---|
| 406 | for(i=rp,j=len-1;j>=0;i=tp[i].before,j--) { |
---|
| 407 | int dx = tp[i].x - tp[tp[i].before].x; |
---|
| 408 | int dy = tp[i].y - tp[tp[i].before].y; |
---|
| 409 | int dir; |
---|
| 410 | if( dx == 0 ) { |
---|
| 411 | dir = (dy > 0 ? 0 : 4); |
---|
| 412 | } else if( dx > 0 ) { |
---|
| 413 | dir = (dy == 0 ? 6 : (dy < 0 ? 5 : 7) ); |
---|
| 414 | } else { |
---|
| 415 | dir = (dy == 0 ? 2 : (dy > 0 ? 1 : 3) ); |
---|
| 416 | } |
---|
| 417 | wpd->path[j] = dir; |
---|
| 418 | } |
---|
| 419 | |
---|
| 420 | return true; |
---|
| 421 | } |
---|
| 422 | |
---|
| 423 | |
---|
| 424 | //Distance functions, taken from http://www.flipcode.com/articles/article_fastdistance.shtml |
---|
| 425 | int check_distance(int dx, int dy, int distance) |
---|
| 426 | { |
---|
| 427 | #ifdef CIRCULAR_AREA |
---|
| 428 | //In this case, we just do a square comparison. Add 1 tile grace for diagonal range checks. |
---|
| 429 | return (dx*dx + dy*dy <= distance*distance + (dx&&dy?1:0)); |
---|
| 430 | #else |
---|
| 431 | if (dx < 0) dx = -dx; |
---|
| 432 | if (dy < 0) dy = -dy; |
---|
| 433 | return ((dx<dy?dy:dx) <= distance); |
---|
| 434 | #endif |
---|
| 435 | } |
---|
| 436 | |
---|
| 437 | unsigned int distance(int dx, int dy) |
---|
| 438 | { |
---|
| 439 | #ifdef CIRCULAR_AREA |
---|
| 440 | unsigned int min, max; |
---|
| 441 | |
---|
| 442 | if ( dx < 0 ) dx = -dx; |
---|
| 443 | if ( dy < 0 ) dy = -dy; |
---|
| 444 | //There appears to be something wrong with the aproximation below when either dx/dy is 0! [Skotlex] |
---|
| 445 | if ( dx == 0 ) return dy; |
---|
| 446 | if ( dy == 0 ) return dx; |
---|
| 447 | |
---|
| 448 | if ( dx < dy ) |
---|
| 449 | { |
---|
| 450 | min = dx; |
---|
| 451 | max = dy; |
---|
| 452 | } else { |
---|
| 453 | min = dy; |
---|
| 454 | max = dx; |
---|
| 455 | } |
---|
| 456 | // coefficients equivalent to ( 123/128 * max ) and ( 51/128 * min ) |
---|
| 457 | return ((( max << 8 ) + ( max << 3 ) - ( max << 4 ) - ( max << 1 ) + |
---|
| 458 | ( min << 7 ) - ( min << 5 ) + ( min << 3 ) - ( min << 1 )) >> 8 ); |
---|
| 459 | #else |
---|
| 460 | if (dx < 0) dx = -dx; |
---|
| 461 | if (dy < 0) dy = -dy; |
---|
| 462 | return (dx<dy?dy:dx); |
---|
| 463 | #endif |
---|
| 464 | } |
---|