Fixed some more hardcoded directions (#7317)

Replaced hardcoded numbers with their respective constants to increase maintainability and readability.
This commit is contained in:
Lemongrass3110 2022-10-12 22:38:22 +02:00 committed by GitHub
parent e4e4ba1af0
commit 5b396bf7f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1379,7 +1379,7 @@ int map_foreachindir(int(*func)(struct block_list*, va_list), int16 m, int16 x0,
struct block_list *bl; struct block_list *bl;
int bx, by; int bx, by;
int mx0, mx1, my0, my1, rx, ry; int mx0, mx1, my0, my1, rx, ry;
uint8 dir = map_calc_dir_xy(x0, y0, x1, y1, 6); uint8 dir = map_calc_dir_xy( x0, y0, x1, y1, DIR_EAST );
short dx = dirx[dir]; short dx = dirx[dir];
short dy = diry[dir]; short dy = diry[dir];
va_list ap; va_list ap;
@ -1395,7 +1395,7 @@ int map_foreachindir(int(*func)(struct block_list*, va_list), int16 m, int16 x0,
return 0; return 0;
//Special offset handling for diagonal paths //Special offset handling for diagonal paths
if (offset && (dir % 2)) { if( offset && direction_diagonal( (directions)dir ) ){
//So that diagonal paths can attach to each other, we have to work with half-tile offsets //So that diagonal paths can attach to each other, we have to work with half-tile offsets
offset = (2 * offset) - 1; offset = (2 * offset) - 1;
//To get the half-tiles we need to increase length by one //To get the half-tiles we need to increase length by one
@ -1421,10 +1421,10 @@ int map_foreachindir(int(*func)(struct block_list*, va_list), int16 m, int16 x0,
SWAP(my0, my1); SWAP(my0, my1);
//Apply width to the path by turning 90 degrees //Apply width to the path by turning 90 degrees
mx0 -= abs(range*dirx[(dir + 2) % 8]); mx0 -= abs( range * dirx[( dir + 2 ) % DIR_MAX] );
my0 -= abs(range*diry[(dir + 2) % 8]); my0 -= abs( range * diry[( dir + 2 ) % DIR_MAX] );
mx1 += abs(range*dirx[(dir + 2) % 8]); mx1 += abs( range * dirx[( dir + 2 ) % DIR_MAX] );
my1 += abs(range*diry[(dir + 2) % 8]); my1 += abs( range * diry[( dir + 2 ) % DIR_MAX] );
mx0 = max(mx0, 0); mx0 = max(mx0, 0);
my0 = max(my0, 0); my0 = max(my0, 0);
@ -1449,7 +1449,7 @@ int map_foreachindir(int(*func)(struct block_list*, va_list), int16 m, int16 x0,
rx *= dx; rx *= dx;
ry *= dy; ry *= dy;
//These checks only need to be done for diagonal paths //These checks only need to be done for diagonal paths
if (dir % 2) { if( direction_diagonal( (directions)dir ) ){
//Check for length //Check for length
if ((rx + ry < offset) || (rx + ry > 2 * (length + (offset/2) - 1))) if ((rx + ry < offset) || (rx + ry > 2 * (length + (offset/2) - 1)))
continue; continue;
@ -1485,7 +1485,7 @@ int map_foreachindir(int(*func)(struct block_list*, va_list), int16 m, int16 x0,
rx *= dx; rx *= dx;
ry *= dy; ry *= dy;
//These checks only need to be done for diagonal paths //These checks only need to be done for diagonal paths
if (dir % 2) { if( direction_diagonal( (directions)dir ) ){
//Check for length //Check for length
if ((rx + ry < offset) || (rx + ry > 2 * (length + (offset / 2) - 1))) if ((rx + ry < offset) || (rx + ry > 2 * (length + (offset / 2) - 1)))
continue; continue;
@ -1773,7 +1773,7 @@ int map_search_freecell(struct block_list *src, int16 m, int16 *x,int16 *y, int1
*------------------------------------------*/ *------------------------------------------*/
bool map_closest_freecell(int16 m, int16 *x, int16 *y, int type, int flag) bool map_closest_freecell(int16 m, int16 *x, int16 *y, int type, int flag)
{ {
uint8 dir = 6; uint8 dir = DIR_EAST;
int16 tx = *x; int16 tx = *x;
int16 ty = *y; int16 ty = *y;
int costrange = 10; int costrange = 10;
@ -1787,7 +1787,7 @@ bool map_closest_freecell(int16 m, int16 *x, int16 *y, int type, int flag)
short dy = diry[dir]; short dy = diry[dir];
//Linear search //Linear search
if(dir%2 == 0 && costrange%MOVE_COST == 0) { if( !direction_diagonal( (directions)dir ) && costrange % MOVE_COST == 0 ){
tx = *x+dx*(costrange/MOVE_COST); tx = *x+dx*(costrange/MOVE_COST);
ty = *y+dy*(costrange/MOVE_COST); ty = *y+dy*(costrange/MOVE_COST);
if(!map_count_oncell(m, tx, ty, type, flag) && map_getcell(m,tx,ty,CELL_CHKPASS)) { if(!map_count_oncell(m, tx, ty, type, flag) && map_getcell(m,tx,ty,CELL_CHKPASS)) {
@ -1797,7 +1797,7 @@ bool map_closest_freecell(int16 m, int16 *x, int16 *y, int type, int flag)
} }
} }
//Full diagonal search //Full diagonal search
else if(dir%2 == 1 && costrange%MOVE_DIAGONAL_COST == 0) { else if( direction_diagonal( (directions)dir ) && costrange % MOVE_DIAGONAL_COST == 0 ){
tx = *x+dx*(costrange/MOVE_DIAGONAL_COST); tx = *x+dx*(costrange/MOVE_DIAGONAL_COST);
ty = *y+dy*(costrange/MOVE_DIAGONAL_COST); ty = *y+dy*(costrange/MOVE_DIAGONAL_COST);
if(!map_count_oncell(m, tx, ty, type, flag) && map_getcell(m,tx,ty,CELL_CHKPASS)) { if(!map_count_oncell(m, tx, ty, type, flag) && map_getcell(m,tx,ty,CELL_CHKPASS)) {
@ -1807,7 +1807,7 @@ bool map_closest_freecell(int16 m, int16 *x, int16 *y, int type, int flag)
} }
} }
//One cell diagonal, rest linear (TODO: Find a better algorithm for this) //One cell diagonal, rest linear (TODO: Find a better algorithm for this)
else if(dir%2 == 1 && costrange%MOVE_COST == 4) { else if( direction_diagonal( (directions)dir ) && costrange % MOVE_COST == 4 ){
tx = *x+dx*((dir%4==3)?(costrange/MOVE_COST):1); tx = *x+dx*((dir%4==3)?(costrange/MOVE_COST):1);
ty = *y+dy*((dir%4==1)?(costrange/MOVE_COST):1); ty = *y+dy*((dir%4==1)?(costrange/MOVE_COST):1);
if(!map_count_oncell(m, tx, ty, type, flag) && map_getcell(m,tx,ty,CELL_CHKPASS)) { if(!map_count_oncell(m, tx, ty, type, flag) && map_getcell(m,tx,ty,CELL_CHKPASS)) {
@ -1825,17 +1825,17 @@ bool map_closest_freecell(int16 m, int16 *x, int16 *y, int type, int flag)
} }
//Get next direction //Get next direction
if (dir == 5) { if( dir == DIR_SOUTHEAST ){
//Diagonal search complete, repeat with higher cost range //Diagonal search complete, repeat with higher cost range
if(costrange == 14) costrange += 6; if(costrange == 14) costrange += 6;
else if(costrange == 28 || costrange >= 38) costrange += 2; else if(costrange == 28 || costrange >= 38) costrange += 2;
else costrange += 4; else costrange += 4;
dir = 6; dir = DIR_EAST;
} else if (dir == 4) { }else if( dir == DIR_SOUTH ){
//Linear search complete, switch to diagonal directions //Linear search complete, switch to diagonal directions
dir = 7; dir = DIR_NORTHEAST;
} else { } else {
dir = (dir+2)%8; dir = ( dir + 2 ) % DIR_MAX;
} }
} }
@ -3021,14 +3021,14 @@ int map_check_dir(int s_dir,int t_dir)
if(s_dir == t_dir) if(s_dir == t_dir)
return 0; return 0;
switch(s_dir) { switch(s_dir) {
case 0: if(t_dir == 7 || t_dir == 1 || t_dir == 0) return 0; break; case DIR_NORTH: if( t_dir == DIR_NORTHEAST || t_dir == DIR_NORTHWEST || t_dir == DIR_NORTH ) return 0; break;
case 1: if(t_dir == 0 || t_dir == 2 || t_dir == 1) return 0; break; case DIR_NORTHWEST: if( t_dir == DIR_NORTH || t_dir == DIR_WEST || t_dir == DIR_NORTHWEST ) return 0; break;
case 2: if(t_dir == 1 || t_dir == 3 || t_dir == 2) return 0; break; case DIR_WEST: if( t_dir == DIR_NORTHWEST || t_dir == DIR_SOUTHWEST || t_dir == DIR_WEST ) return 0; break;
case 3: if(t_dir == 2 || t_dir == 4 || t_dir == 3) return 0; break; case DIR_SOUTHWEST: if( t_dir == DIR_WEST || t_dir == DIR_SOUTH || t_dir == DIR_SOUTHWEST ) return 0; break;
case 4: if(t_dir == 3 || t_dir == 5 || t_dir == 4) return 0; break; case DIR_SOUTH: if( t_dir == DIR_SOUTHWEST || t_dir == DIR_SOUTHEAST || t_dir == DIR_SOUTH ) return 0; break;
case 5: if(t_dir == 4 || t_dir == 6 || t_dir == 5) return 0; break; case DIR_SOUTHEAST: if( t_dir == DIR_SOUTH || t_dir == DIR_EAST || t_dir == DIR_SOUTHEAST ) return 0; break;
case 6: if(t_dir == 5 || t_dir == 7 || t_dir == 6) return 0; break; case DIR_EAST: if( t_dir == DIR_SOUTHEAST || t_dir == DIR_NORTHEAST || t_dir == DIR_EAST ) return 0; break;
case 7: if(t_dir == 6 || t_dir == 0 || t_dir == 7) return 0; break; case DIR_NORTHEAST: if( t_dir == DIR_EAST || t_dir == DIR_NORTH || t_dir == DIR_NORTHEAST ) return 0; break;
} }
return 1; return 1;
} }
@ -3038,9 +3038,9 @@ int map_check_dir(int s_dir,int t_dir)
*------------------------------------------*/ *------------------------------------------*/
uint8 map_calc_dir(struct block_list* src, int16 x, int16 y) uint8 map_calc_dir(struct block_list* src, int16 x, int16 y)
{ {
uint8 dir = 0; uint8 dir = DIR_NORTH;
nullpo_ret(src); nullpo_retr( dir, src );
dir = map_calc_dir_xy(src->x, src->y, x, y, unit_getdir(src)); dir = map_calc_dir_xy(src->x, src->y, x, y, unit_getdir(src));
@ -3052,7 +3052,7 @@ uint8 map_calc_dir(struct block_list* src, int16 x, int16 y)
* Use this if you don't have a block list available to check against * Use this if you don't have a block list available to check against
*------------------------------------------*/ *------------------------------------------*/
uint8 map_calc_dir_xy(int16 srcx, int16 srcy, int16 x, int16 y, uint8 srcdir) { uint8 map_calc_dir_xy(int16 srcx, int16 srcy, int16 x, int16 y, uint8 srcdir) {
uint8 dir = 0; uint8 dir = DIR_NORTH;
int dx, dy; int dx, dy;
dx = x-srcx; dx = x-srcx;
@ -3061,31 +3061,31 @@ uint8 map_calc_dir_xy(int16 srcx, int16 srcy, int16 x, int16 y, uint8 srcdir) {
{ // both are standing on the same spot { // both are standing on the same spot
// aegis-style, makes knockback default to the left // aegis-style, makes knockback default to the left
// athena-style, makes knockback default to behind 'src' // athena-style, makes knockback default to behind 'src'
dir = (battle_config.knockback_left ? 6 : srcdir); dir = ( battle_config.knockback_left ? DIR_EAST : srcdir );
} }
else if( dx >= 0 && dy >=0 ) else if( dx >= 0 && dy >=0 )
{ // upper-right { // upper-right
if( dx >= dy*3 ) dir = 6; // right if( dx >= dy*3 ) dir = DIR_EAST; // right
else if( dx*3 < dy ) dir = 0; // up else if( dx*3 < dy ) dir = DIR_NORTH; // up
else dir = 7; // up-right else dir = DIR_NORTHEAST; // up-right
} }
else if( dx >= 0 && dy <= 0 ) else if( dx >= 0 && dy <= 0 )
{ // lower-right { // lower-right
if( dx >= -dy*3 ) dir = 6; // right if( dx >= -dy*3 ) dir = DIR_EAST; // right
else if( dx*3 < -dy ) dir = 4; // down else if( dx*3 < -dy ) dir = DIR_SOUTH; // down
else dir = 5; // down-right else dir = DIR_SOUTHEAST; // down-right
} }
else if( dx <= 0 && dy <= 0 ) else if( dx <= 0 && dy <= 0 )
{ // lower-left { // lower-left
if( dx*3 >= dy ) dir = 4; // down if( dx*3 >= dy ) dir = DIR_SOUTH; // down
else if( dx < dy*3 ) dir = 2; // left else if( dx < dy*3 ) dir = DIR_WEST; // left
else dir = 3; // down-left else dir = DIR_SOUTHWEST; // down-left
} }
else else
{ // upper-left { // upper-left
if( -dx*3 <= dy ) dir = 0; // up if( -dx*3 <= dy ) dir = DIR_NORTH; // up
else if( -dx > dy*3 ) dir = 2; // left else if( -dx > dy*3 ) dir = DIR_WEST; // left
else dir = 1; // up-left else dir = DIR_NORTHWEST; // up-left
} }
return dir; return dir;
} }
@ -3303,16 +3303,16 @@ bool map_iwall_exist(const char* wall_name)
void map_iwall_nextxy(int16 x, int16 y, int8 dir, int pos, int16 *x1, int16 *y1) void map_iwall_nextxy(int16 x, int16 y, int8 dir, int pos, int16 *x1, int16 *y1)
{ {
if( dir == 0 || dir == 4 ) if( dir == DIR_NORTH || dir == DIR_SOUTH )
*x1 = x; // Keep X *x1 = x; // Keep X
else if( dir > 0 && dir < 4 ) else if( dir > DIR_NORTH && dir < DIR_SOUTH )
*x1 = x - pos; // Going left *x1 = x - pos; // Going left
else else
*x1 = x + pos; // Going right *x1 = x + pos; // Going right
if( dir == 2 || dir == 6 ) if( dir == DIR_WEST || dir == DIR_EAST )
*y1 = y; *y1 = y;
else if( dir > 2 && dir < 6 ) else if( dir > DIR_WEST && dir < DIR_EAST )
*y1 = y - pos; *y1 = y - pos;
else else
*y1 = y + pos; *y1 = y + pos;