From 02957da421bb1d813358f800c1ab6b74036bb4aa Mon Sep 17 00:00:00 2001 From: Hourier Date: Thu, 20 Aug 2020 17:21:49 +0900 Subject: [PATCH] [Refactor] #40577 Separated calc_vertical_projection() from projection_path() --- src/target/projection-path-calculator.c | 151 +++++++++++++++++--------------- 1 file changed, 82 insertions(+), 69 deletions(-) diff --git a/src/target/projection-path-calculator.c b/src/target/projection-path-calculator.c index f6a32bc08..55e544d5e 100644 --- a/src/target/projection-path-calculator.c +++ b/src/target/projection-path-calculator.c @@ -6,6 +6,9 @@ #include "system/floor-type-definition.h" typedef struct projection_path_type { + u16b *gp; + POSITION range; + BIT_FLAGS flag; POSITION y1; POSITION x1; POSITION y2; @@ -32,8 +35,12 @@ typedef struct projection_path_type { */ static u16b location_to_grid(POSITION y, POSITION x) { return 256 * y + x; } -static projection_path_type *initialize_projection_path_type(projection_path_type *pp_ptr, POSITION y1, POSITION x1, POSITION y2, POSITION x2) +static projection_path_type *initialize_projection_path_type( + projection_path_type *pp_ptr, u16b *gp, POSITION range, BIT_FLAGS flag, POSITION y1, POSITION x1, POSITION y2, POSITION x2) { + pp_ptr->gp = gp; + pp_ptr->range = range; + pp_ptr->flag = flag; pp_ptr->y1 = y1; pp_ptr->x1 = x1; pp_ptr->y2 = y2; @@ -60,6 +67,66 @@ static void set_asxy(projection_path_type *pp_ptr) } } +static bool calc_vertical_projection(player_type *player_ptr, projection_path_type *pp_ptr) +{ + if (pp_ptr->ay > pp_ptr->ax) + return FALSE; + + pp_ptr->m = pp_ptr->ax * pp_ptr->ax * 2; + pp_ptr->y = pp_ptr->y1 + pp_ptr->sy; + pp_ptr->x = pp_ptr->x1; + pp_ptr->frac = pp_ptr->m; + if (pp_ptr->frac > pp_ptr->half) { + pp_ptr->x += pp_ptr->sx; + pp_ptr->frac -= pp_ptr->full; + pp_ptr->k++; + } + + floor_type *floor_ptr = player_ptr->current_floor_ptr; + while (TRUE) { + pp_ptr->gp[pp_ptr->n++] = location_to_grid(pp_ptr->y, pp_ptr->x); + if ((pp_ptr->n + (pp_ptr->k >> 1)) >= pp_ptr->range) + break; + + if (!(pp_ptr->flag & PROJECT_THRU)) { + if ((pp_ptr->x == pp_ptr->x2) && (pp_ptr->y == pp_ptr->y2)) + break; + } + + if (pp_ptr->flag & PROJECT_DISI) { + if ((pp_ptr->n > 0) && cave_stop_disintegration(floor_ptr, pp_ptr->y, pp_ptr->x)) + break; + } else if (pp_ptr->flag & PROJECT_LOS) { + if ((pp_ptr->n > 0) && !cave_los_bold(floor_ptr, pp_ptr->y, pp_ptr->x)) + break; + } else if (!(pp_ptr->flag & PROJECT_PATH)) { + if ((pp_ptr->n > 0) && !cave_have_flag_bold(floor_ptr, pp_ptr->y, pp_ptr->x, FF_PROJECT)) + break; + } + + if (pp_ptr->flag & PROJECT_STOP) { + if ((pp_ptr->n > 0) && (player_bold(player_ptr, pp_ptr->y, pp_ptr->x) || floor_ptr->grid_array[pp_ptr->y][pp_ptr->x].m_idx != 0)) + break; + } + + if (!in_bounds(floor_ptr, pp_ptr->y, pp_ptr->x)) + break; + + if (pp_ptr->m) { + pp_ptr->frac += pp_ptr->m; + if (pp_ptr->frac > pp_ptr->half) { + pp_ptr->x += pp_ptr->sx; + pp_ptr->frac -= pp_ptr->full; + pp_ptr->k++; + } + } + + pp_ptr->y += pp_ptr->sy; + } + + return TRUE; +} + /*! * @brief Žn“_‚©‚çI“_‚Ö‚Ì’¼üŒo˜H‚ð•Ô‚· / * Determine the path taken by a projection. @@ -70,78 +137,24 @@ static void set_asxy(projection_path_type *pp_ptr) * @param x1 Žn“_XÀ•W * @param y2 I“_YÀ•W * @param x2 I“_XÀ•W - * @param flg ƒtƒ‰ƒOID + * @param flag ƒtƒ‰ƒOID * @return ƒŠƒXƒg‚Ì’·‚³ */ -int projection_path(player_type *player_ptr, u16b *gp, POSITION range, POSITION y1, POSITION x1, POSITION y2, POSITION x2, BIT_FLAGS flg) +int projection_path(player_type *player_ptr, u16b *gp, POSITION range, POSITION y1, POSITION x1, POSITION y2, POSITION x2, BIT_FLAGS flag) { if ((x1 == x2) && (y1 == y2)) return 0; projection_path_type tmp_projection_path; - projection_path_type *pp_ptr = initialize_projection_path_type(&tmp_projection_path, y1, x1, y2, x2); + projection_path_type *pp_ptr = initialize_projection_path_type(&tmp_projection_path, gp, range, flag, y1, x1, y2, x2); set_asxy(pp_ptr); pp_ptr->half = pp_ptr->ay * pp_ptr->ax; pp_ptr->full = pp_ptr->half << 1; pp_ptr->n = 0; pp_ptr->k = 0; - /* Vertical */ - if (pp_ptr->ay > pp_ptr->ax) { - pp_ptr->m = pp_ptr->ax * pp_ptr->ax * 2; - pp_ptr->y = y1 + pp_ptr->sy; - pp_ptr->x = x1; - pp_ptr->frac = pp_ptr->m; - if (pp_ptr->frac > pp_ptr->half) { - pp_ptr->x += pp_ptr->sx; - pp_ptr->frac -= pp_ptr->full; - pp_ptr->k++; - } - - floor_type *floor_ptr = player_ptr->current_floor_ptr; - while (TRUE) { - gp[pp_ptr->n++] = location_to_grid(pp_ptr->y, pp_ptr->x); - if ((pp_ptr->n + (pp_ptr->k >> 1)) >= range) - break; - - if (!(flg & PROJECT_THRU)) { - if ((pp_ptr->x == x2) && (pp_ptr->y == y2)) - break; - } - - if (flg & PROJECT_DISI) { - if ((pp_ptr->n > 0) && cave_stop_disintegration(floor_ptr, pp_ptr->y, pp_ptr->x)) - break; - } else if (flg & PROJECT_LOS) { - if ((pp_ptr->n > 0) && !cave_los_bold(floor_ptr, pp_ptr->y, pp_ptr->x)) - break; - } else if (!(flg & PROJECT_PATH)) { - if ((pp_ptr->n > 0) && !cave_have_flag_bold(floor_ptr, pp_ptr->y, pp_ptr->x, FF_PROJECT)) - break; - } - - if (flg & PROJECT_STOP) { - if ((pp_ptr->n > 0) && (player_bold(player_ptr, pp_ptr->y, pp_ptr->x) || floor_ptr->grid_array[pp_ptr->y][pp_ptr->x].m_idx != 0)) - break; - } - - if (!in_bounds(floor_ptr, pp_ptr->y, pp_ptr->x)) - break; - - if (pp_ptr->m) { - pp_ptr->frac += pp_ptr->m; - if (pp_ptr->frac > pp_ptr->half) { - pp_ptr->x += pp_ptr->sx; - pp_ptr->frac -= pp_ptr->full; - pp_ptr->k++; - } - } - - pp_ptr->y += pp_ptr->sy; - } - + if (calc_vertical_projection(player_ptr, pp_ptr)) return pp_ptr->n; - } /* Horizontal */ if (pp_ptr->ax > pp_ptr->ay) { @@ -161,23 +174,23 @@ int projection_path(player_type *player_ptr, u16b *gp, POSITION range, POSITION if ((pp_ptr->n + (pp_ptr->k >> 1)) >= range) break; - if (!(flg & (PROJECT_THRU))) { + if (!(flag & (PROJECT_THRU))) { if ((pp_ptr->x == x2) && (pp_ptr->y == y2)) break; } - if (flg & (PROJECT_DISI)) { + if (flag & (PROJECT_DISI)) { if ((pp_ptr->n > 0) && cave_stop_disintegration(floor_ptr, pp_ptr->y, pp_ptr->x)) break; - } else if (flg & (PROJECT_LOS)) { + } else if (flag & (PROJECT_LOS)) { if ((pp_ptr->n > 0) && !cave_los_bold(floor_ptr, pp_ptr->y, pp_ptr->x)) break; - } else if (!(flg & (PROJECT_PATH))) { + } else if (!(flag & (PROJECT_PATH))) { if ((pp_ptr->n > 0) && !cave_have_flag_bold(floor_ptr, pp_ptr->y, pp_ptr->x, FF_PROJECT)) break; } - if (flg & (PROJECT_STOP)) { + if (flag & (PROJECT_STOP)) { if ((pp_ptr->n > 0) && (player_bold(player_ptr, pp_ptr->y, pp_ptr->x) || floor_ptr->grid_array[pp_ptr->y][pp_ptr->x].m_idx != 0)) break; } @@ -209,23 +222,23 @@ int projection_path(player_type *player_ptr, u16b *gp, POSITION range, POSITION if ((pp_ptr->n + (pp_ptr->n >> 1)) >= range) break; - if (!(flg & PROJECT_THRU)) { + if (!(flag & PROJECT_THRU)) { if ((pp_ptr->x == x2) && (pp_ptr->y == y2)) break; } - if (flg & PROJECT_DISI) { + if (flag & PROJECT_DISI) { if ((pp_ptr->n > 0) && cave_stop_disintegration(floor_ptr, pp_ptr->y, pp_ptr->x)) break; - } else if (flg & PROJECT_LOS) { + } else if (flag & PROJECT_LOS) { if ((pp_ptr->n > 0) && !cave_los_bold(floor_ptr, pp_ptr->y, pp_ptr->x)) break; - } else if (!(flg & PROJECT_PATH)) { + } else if (!(flag & PROJECT_PATH)) { if ((pp_ptr->n > 0) && !cave_have_flag_bold(floor_ptr, pp_ptr->y, pp_ptr->x, FF_PROJECT)) break; } - if (flg & PROJECT_STOP) { + if (flag & PROJECT_STOP) { if ((pp_ptr->n > 0) && (player_bold(player_ptr, pp_ptr->y, pp_ptr->x) || floor_ptr->grid_array[pp_ptr->y][pp_ptr->x].m_idx != 0)) break; } -- 2.11.0