summaryrefslogtreecommitdiff
path: root/src/map/path.c
diff options
context:
space:
mode:
authorultramage <ultramage@54d463be-8e91-2dee-dedb-b68131a5f0ec>2007-12-22 13:00:48 +0000
committerultramage <ultramage@54d463be-8e91-2dee-dedb-b68131a5f0ec>2007-12-22 13:00:48 +0000
commitebf1cd085c9f4061318f99f9ab19b63148d19819 (patch)
tree876e3d2c2bc05cbce67604eaa734a741410fb3bf /src/map/path.c
parent5f857bae116433a2a0fd38b8c3539352c5453f7a (diff)
downloadhercules-ebf1cd085c9f4061318f99f9ab19b63148d19819.tar.gz
hercules-ebf1cd085c9f4061318f99f9ab19b63148d19819.tar.bz2
hercules-ebf1cd085c9f4061318f99f9ab19b63148d19819.tar.xz
hercules-ebf1cd085c9f4061318f99f9ab19b63148d19819.zip
Path code cleaning...
* Added map/path.h, moved path-related function headers to path.h. * Removed the macroed _real() path functions. * Modified some functions to use boolean return values instead of 1/0 or 0/-1. * Modified path_search_long() to allow a NULL output pointer (in which case a temporary local buffer will be used instead). * Removed an unused ->path_half member variable from struct walkpath_data. git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@11958 54d463be-8e91-2dee-dedb-b68131a5f0ec
Diffstat (limited to 'src/map/path.c')
-rw-r--r--src/map/path.c175
1 files changed, 84 insertions, 91 deletions
diff --git a/src/map/path.c b/src/map/path.c
index 3d4b99fc9..52dcd0ac8 100644
--- a/src/map/path.c
+++ b/src/map/path.c
@@ -7,14 +7,13 @@
#include "../common/malloc.h"
#include "map.h"
#include "battle.h"
+#include "path.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-//#define PATH_STANDALONETEST
-
#define MAX_HEAP 150
struct tmp_path { short x,y,dist,before,cost,flag;};
@@ -226,18 +225,22 @@ int path_blownpos(int m,int x0,int y0,int dx,int dy,int count)
x0+=dx;
y0+=dy;
}
- return (x0<<16)|y0;
+ return (x0<<16)|y0; //TODO: use 'struct point' here instead?
}
/*==========================================
* is ranged attack from (x0,y0) to (x1,y1) possible?
*------------------------------------------*/
-bool path_search_long_real(struct shootpath_data *spd,int m,int x0,int y0,int x1,int y1,cell_t flag)
+bool path_search_long(struct shootpath_data *spd,int m,int x0,int y0,int x1,int y1,cell_t cell)
{
int dx, dy;
int wx = 0, wy = 0;
int weight;
struct map_data *md;
+ struct shootpath_data s_spd;
+
+ if( spd == NULL )
+ spd = &s_spd; // use dummy output variable
if (!map[m].gat)
return false;
@@ -251,43 +254,41 @@ bool path_search_long_real(struct shootpath_data *spd,int m,int x0,int y0,int x1
}
dy = (y1 - y0);
- if (spd) {
- spd->rx = spd->ry = 0;
- spd->len = 1;
- spd->x[0] = x0;
- spd->y[0] = y0;
- }
+ spd->rx = spd->ry = 0;
+ spd->len = 1;
+ spd->x[0] = x0;
+ spd->y[0] = y0;
- if (map_getcellp(md,x1,y1,flag))
+ if (map_getcellp(md,x1,y1,cell))
return false;
if (dx > abs(dy)) {
weight = dx;
- if (spd)
- spd->ry=1;
+ spd->ry = 1;
} else {
weight = abs(y1 - y0);
- if (spd)
- spd->rx=1;
+ spd->rx = 1;
}
- while (x0 != x1 || y0 != y1) {
- if (map_getcellp(md,x0,y0,flag))
+ while (x0 != x1 || y0 != y1)
+ {
+ if (map_getcellp(md,x0,y0,cell))
return false;
wx += dx;
wy += dy;
if (wx >= weight) {
wx -= weight;
- x0 ++;
+ x0++;
}
if (wy >= weight) {
wy -= weight;
- y0 ++;
+ y0++;
} else if (wy < 0) {
wy += weight;
- y0 --;
+ y0--;
}
- if (spd && spd->len<MAX_WALKPATH) {
+ if( spd->len<MAX_WALKPATH )
+ {
spd->x[spd->len] = x0;
spd->y[spd->len] = y0;
spd->len++;
@@ -299,39 +300,48 @@ bool path_search_long_real(struct shootpath_data *spd,int m,int x0,int y0,int x1
/*==========================================
* path search (x0,y0)->(x1,y1)
+ * wpd: path info will be written here
* flag: &1 = easy path search only
+ * cell: type of obstruction to check for
*------------------------------------------*/
-int path_search_real(struct walkpath_data *wpd,int m,int x0,int y0,int x1,int y1,int flag,cell_t flag2)
+bool path_search(struct walkpath_data *wpd,int m,int x0,int y0,int x1,int y1,int flag,cell_t cell)
{
int heap[MAX_HEAP+1];
struct tmp_path tp[MAX_WALKPATH*MAX_WALKPATH];
- register int i,x,y,dx,dy;
+ register int i,j,len,x,y,dx,dy;
int rp,xs,ys;
struct map_data *md;
+ struct walkpath_data s_wpd;
- nullpo_retr(0, wpd);
+ if( wpd == NULL )
+ wpd = &s_wpd; // use dummy output variable
if( !map[m].gat )
- return -1;
+ return false;
md = &map[m];
+
#ifdef CELL_NOSTACK
//Do not check starting cell as that would get you stuck.
if( x0 < 0 || x0 >= md->xs || y0 < 0 || y0 >= md->ys )
#else
- if( x0 < 0 || x0 >= md->xs || y0 < 0 || y0 >= md->ys /*|| map_getcellp(md,x0,y0,flag2)*/ )
+ if( x0 < 0 || x0 >= md->xs || y0 < 0 || y0 >= md->ys /*|| map_getcellp(md,x0,y0,cell)*/ )
#endif
- return -1;
- if( x1 < 0 || x1 >= md->xs || y1 < 0 || y1 >= md->ys || map_getcellp(md,x1,y1,flag2) )
- return -1;
+ return false;
+ if( x1 < 0 || x1 >= md->xs || y1 < 0 || y1 >= md->ys || map_getcellp(md,x1,y1,cell) )
+ return false;
// calculate (sgn(x1-x0), sgn(y1-y0))
dx = ((dx = x1-x0)) ? ((dx<0) ? -1 : 1) : 0;
dy = ((dy = y1-y0)) ? ((dy<0) ? -1 : 1) : 0;
// try finding direct path to target
- for( x = x0, y = y0, i = 0; i < ARRAYLENGTH(wpd->path); )
+ x = x0;
+ y = y0;
+ i = 0;
+ while( i < ARRAYLENGTH(wpd->path) )
{
- wpd->path[i++] = walk_choices[-dy + 1][dx + 1];
+ wpd->path[i] = walk_choices[-dy + 1][dx + 1];
+ i++;
x += dx;
y += dy;
@@ -339,9 +349,9 @@ int path_search_real(struct walkpath_data *wpd,int m,int x0,int y0,int x1,int y1
if( x == x1 ) dx = 0;
if( y == y1 ) dy = 0;
- if( !dx && !dy )
+ if( dx == 0 && dy == 0 )
break; // success
- if( map_getcellp(md,x,y,flag2) )
+ if( map_getcellp(md,x,y,cell) )
break; // obstacle = failure
}
@@ -349,12 +359,11 @@ int path_search_real(struct walkpath_data *wpd,int m,int x0,int y0,int x1,int y1
{ //easy path successful.
wpd->path_len = i;
wpd->path_pos = 0;
- wpd->path_half = 0;
- return 0;
+ return true;
}
if( flag&1 )
- return -1;
+ return false;
memset(tp,0,sizeof(tp));
@@ -369,94 +378,78 @@ int path_search_real(struct walkpath_data *wpd,int m,int x0,int y0,int x1,int y1
push_heap_path(heap,tp,calc_index(x0,y0));
xs = md->xs-1; // あらかじめ1減算しておく
ys = md->ys-1;
- while(1){
+
+ while(1)
+ {
int e=0,f=0,dist,cost,dc[4]={0,0,0,0};
if(heap[0]==0)
- return -1;
+ return false;
rp = pop_heap_path(heap,tp);
x = tp[rp].x;
y = tp[rp].y;
dist = tp[rp].dist + 10;
cost = tp[rp].cost;
- if(x==x1 && y==y1) break;
+
+ if(x==x1 && y==y1)
+ break;
// dc[0] : y++ の時のコスト増分
// dc[1] : x-- の時のコスト増分
// dc[2] : y-- の時のコスト増分
// dc[3] : x++ の時のコスト増分
- if(y < ys && !map_getcellp(md,x ,y+1,flag2)) {
+ if(y < ys && !map_getcellp(md,x ,y+1,cell)) {
f |= 1; dc[0] = (y >= y1 ? 20 : 0);
e+=add_path(heap,tp,x ,y+1,dist,rp,cost+dc[0]); // (x, y+1)
}
- if(x > 0 && !map_getcellp(md,x-1,y ,flag2)) {
+ if(x > 0 && !map_getcellp(md,x-1,y ,cell)) {
f |= 2; dc[1] = (x <= x1 ? 20 : 0);
e+=add_path(heap,tp,x-1,y ,dist,rp,cost+dc[1]); // (x-1, y )
}
- if(y > 0 && !map_getcellp(md,x ,y-1,flag2)) {
+ if(y > 0 && !map_getcellp(md,x ,y-1,cell)) {
f |= 4; dc[2] = (y <= y1 ? 20 : 0);
e+=add_path(heap,tp,x ,y-1,dist,rp,cost+dc[2]); // (x , y-1)
}
- if(x < xs && !map_getcellp(md,x+1,y ,flag2)) {
+ if(x < xs && !map_getcellp(md,x+1,y ,cell)) {
f |= 8; dc[3] = (x >= x1 ? 20 : 0);
e+=add_path(heap,tp,x+1,y ,dist,rp,cost+dc[3]); // (x+1, y )
}
- if( (f & (2+1)) == (2+1) && !map_getcellp(md,x-1,y+1,flag2))
+ if( (f & (2+1)) == (2+1) && !map_getcellp(md,x-1,y+1,cell))
e+=add_path(heap,tp,x-1,y+1,dist+4,rp,cost+dc[1]+dc[0]-6); // (x-1, y+1)
- if( (f & (2+4)) == (2+4) && !map_getcellp(md,x-1,y-1,flag2))
+ if( (f & (2+4)) == (2+4) && !map_getcellp(md,x-1,y-1,cell))
e+=add_path(heap,tp,x-1,y-1,dist+4,rp,cost+dc[1]+dc[2]-6); // (x-1, y-1)
- if( (f & (8+4)) == (8+4) && !map_getcellp(md,x+1,y-1,flag2))
+ if( (f & (8+4)) == (8+4) && !map_getcellp(md,x+1,y-1,cell))
e+=add_path(heap,tp,x+1,y-1,dist+4,rp,cost+dc[3]+dc[2]-6); // (x+1, y-1)
- if( (f & (8+1)) == (8+1) && !map_getcellp(md,x+1,y+1,flag2))
+ if( (f & (8+1)) == (8+1) && !map_getcellp(md,x+1,y+1,cell))
e+=add_path(heap,tp,x+1,y+1,dist+4,rp,cost+dc[3]+dc[0]-6); // (x+1, y+1)
tp[rp].flag=1;
if(e || heap[0]>=MAX_HEAP-5)
- return -1;
+ return false;
}
- if(x==x1 && y==y1) {
- int len,j;
-
- for(len=0,i=rp;len<100 && i!=calc_index(x0,y0);i=tp[i].before,len++);
- if(len==100 || len>=sizeof(wpd->path))
- return -1;
- wpd->path_len=len;
- wpd->path_pos=0;
- wpd->path_half=0;
- for(i=rp,j=len-1;j>=0;i=tp[i].before,j--) {
- int dx = tp[i].x - tp[tp[i].before].x;
- int dy = tp[i].y - tp[tp[i].before].y;
- int dir;
- if( dx == 0 ) {
- dir = (dy > 0 ? 0 : 4);
- } else if( dx > 0 ) {
- dir = (dy == 0 ? 6 : (dy < 0 ? 5 : 7) );
- } else {
- dir = (dy == 0 ? 2 : (dy > 0 ? 1 : 3) );
- }
- wpd->path[j] = dir;
- }
-#if 0
- // test
- {
- int dirx[8]={0,-1,-1,-1,0,1,1,1};
- int diry[8]={1,1,0,-1,-1,-1,0,1};
- x = x0; y = y0;
- for(i = 0; i < wpd->path_len; i++) {
- x += dirx[ wpd->path[i] ];
- y += diry[ wpd->path[i] ];
- if( map_getcellp(md,x,y,flag2) ) {
- printf("path_search_real: cannot move(%d, %d)\n", x, y);
- return -1;
- }
- }
- if( x != x1 || y != y1 ) {
- printf("path_search_real: dest position is wrong. ok:(%d, %d) ng:(%d,%d)\n", x1, y1, x, y);
- return -1;
- }
+
+ if( !(x==x1 && y==y1) ) // will never happen...
+ return false;
+
+ for(len=0,i=rp;len<100 && i!=calc_index(x0,y0);i=tp[i].before,len++);
+ if(len==100 || len>=sizeof(wpd->path))
+ return false;
+
+ wpd->path_len = len;
+ wpd->path_pos = 0;
+ for(i=rp,j=len-1;j>=0;i=tp[i].before,j--) {
+ int dx = tp[i].x - tp[tp[i].before].x;
+ int dy = tp[i].y - tp[tp[i].before].y;
+ int dir;
+ if( dx == 0 ) {
+ dir = (dy > 0 ? 0 : 4);
+ } else if( dx > 0 ) {
+ dir = (dy == 0 ? 6 : (dy < 0 ? 5 : 7) );
+ } else {
+ dir = (dy == 0 ? 2 : (dy > 0 ? 1 : 3) );
}
-#endif
- return 0;
+ wpd->path[j] = dir;
}
- return -1;
+
+ return true;
}