From dbb9ec367fd6f1d6ef22bd5c061f484a5e1f49b1 Mon Sep 17 00:00:00 2001 From: Hourier Date: Thu, 20 Aug 2020 17:01:43 +0900 Subject: [PATCH] [Refactor] #40577 Separated projection-path-calculator.c/h from floor.c/h --- Hengband/Hengband/Hengband.vcxproj | 2 + Hengband/Hengband/Hengband.vcxproj.filters | 6 + src/Makefile.am | 1 + src/combat/shoot.c | 4 +- src/effect/effect-processor.c | 1 + src/floor/floor.c | 198 +-------------------------- src/floor/floor.h | 7 - src/io/cursor.c | 1 + src/mind/mind-ninja.c | 1 + src/mspell/mspell-checker.c | 1 + src/mspell/mspell-judgement.c | 1 + src/spell-kind/spells-fetcher.c | 3 +- src/target/projection-path-calculator.c | 211 +++++++++++++++++++++++++++++ src/target/projection-path-calculator.h | 5 + 14 files changed, 235 insertions(+), 207 deletions(-) create mode 100644 src/target/projection-path-calculator.c create mode 100644 src/target/projection-path-calculator.h diff --git a/Hengband/Hengband/Hengband.vcxproj b/Hengband/Hengband/Hengband.vcxproj index c614f51de..a1925317f 100644 --- a/Hengband/Hengband/Hengband.vcxproj +++ b/Hengband/Hengband/Hengband.vcxproj @@ -409,6 +409,7 @@ + @@ -1050,6 +1051,7 @@ + diff --git a/Hengband/Hengband/Hengband.vcxproj.filters b/Hengband/Hengband/Hengband.vcxproj.filters index 1f1b7cad9..7f5e6d774 100644 --- a/Hengband/Hengband/Hengband.vcxproj.filters +++ b/Hengband/Hengband/Hengband.vcxproj.filters @@ -2093,6 +2093,9 @@ cmd-item + + target + @@ -4537,6 +4540,9 @@ cmd-item + + target + diff --git a/src/Makefile.am b/src/Makefile.am index 3c972349e..508b4dfbc 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -788,6 +788,7 @@ hengband_SOURCES = \ system/gamevalue.h \ \ target/grid-selector.c target/grid-selector.h \ + target/projection-path-calculator.c target/projection-path-calculator.h target/target-checker.c target/target-checker.h \ target/target-describer.c target/target-describer.h \ target/target-getter.c target/target-getter.h \ diff --git a/src/combat/shoot.c b/src/combat/shoot.c index b1eaf12d9..4d2234be4 100644 --- a/src/combat/shoot.c +++ b/src/combat/shoot.c @@ -10,7 +10,6 @@ #include "flavor/object-flavor-types.h" #include "floor/cave.h" #include "floor/floor-object.h" -#include "floor/floor.h" #include "game-option/cheat-types.h" #include "game-option/special-options.h" #include "grid/feature-flag-types.h" @@ -55,6 +54,7 @@ #include "sv-definition/sv-bow-types.h" #include "system/artifact-type-definition.h" #include "system/floor-type-definition.h" +#include "target/projection-path-calculator.h" #include "target/target-checker.h" #include "target/target-getter.h" #include "util/bit-flags-calculator.h" @@ -452,7 +452,7 @@ void exe_fire(player_type *shooter_ptr, INVENTORY_IDX item, object_type *j_ptr, } /* Get projection path length */ - tdis = project_path(shooter_ptr, path_g, project_length, shooter_ptr->y, shooter_ptr->x, ty, tx, PROJECT_PATH | PROJECT_THRU) - 1; + tdis = projection_path(shooter_ptr, path_g, project_length, shooter_ptr->y, shooter_ptr->x, ty, tx, PROJECT_PATH | PROJECT_THRU) - 1; project_length = 0; /* reset to default */ diff --git a/src/effect/effect-processor.c b/src/effect/effect-processor.c index 82e3f167d..7cae4564a 100644 --- a/src/effect/effect-processor.c +++ b/src/effect/effect-processor.c @@ -24,6 +24,7 @@ #include "spell/range-calc.h" #include "spell/spell-types.h" #include "system/floor-type-definition.h" +#include "target/projection-path-calculator.h" #include "term/gameterm.h" #include "util/bit-flags-calculator.h" #include "view/display-messages.h" diff --git a/src/floor/floor.c b/src/floor/floor.c index dddace382..bac1ef423 100644 --- a/src/floor/floor.c +++ b/src/floor/floor.c @@ -30,6 +30,7 @@ #include "room/door-definition.h" #include "system/artifact-type-definition.h" #include "system/floor-type-definition.h" +#include "target/projection-path-calculator.h" #include "util/bit-flags-calculator.h" #include "view/display-messages.h" #include "world/world-object.h" @@ -880,203 +881,6 @@ void vault_objects(player_type *player_ptr, POSITION y, POSITION x, int num) } /*! - * @brief 始点から終点への直線経路を返す / - * Determine the path taken by a projection. - * @param player_ptr プレーヤーへの参照ポインタ - * @param gp 経路座標リストを返す参照ポインタ - * @param range 距離 - * @param y1 始点Y座標 - * @param x1 始点X座標 - * @param y2 終点Y座標 - * @param x2 終点X座標 - * @param flg フラグID - * @return リストの長さ - */ -int projection_path(player_type *player_ptr, u16b *gp, POSITION range, POSITION y1, POSITION x1, POSITION y2, POSITION x2, BIT_FLAGS flg) -{ - if ((x1 == x2) && (y1 == y2)) - return 0; - - POSITION y, x; - POSITION ay, ax; - POSITION sy, sx; - int frac; - int m; - - if (y2 < y1) { - ay = (y1 - y2); - sy = -1; - } else { - ay = (y2 - y1); - sy = 1; - } - - if (x2 < x1) { - ax = (x1 - x2); - sx = -1; - } else { - ax = (x2 - x1); - sx = 1; - } - - int half = (ay * ax); - int full = half << 1; - floor_type *floor_ptr = player_ptr->current_floor_ptr; - int n = 0; - int k = 0; - - /* Vertical */ - if (ay > ax) { - m = ax * ax * 2; - y = y1 + sy; - x = x1; - frac = m; - if (frac > half) { - x += sx; - frac -= full; - k++; - } - - while (TRUE) { - gp[n++] = GRID(y, x); - if ((n + (k >> 1)) >= range) - break; - - if (!(flg & PROJECT_THRU)) { - if ((x == x2) && (y == y2)) - break; - } - - if (flg & PROJECT_DISI) { - if ((n > 0) && cave_stop_disintegration(floor_ptr, y, x)) - break; - } else if (flg & PROJECT_LOS) { - if ((n > 0) && !cave_los_bold(floor_ptr, y, x)) - break; - } else if (!(flg & PROJECT_PATH)) { - if ((n > 0) && !cave_have_flag_bold(floor_ptr, y, x, FF_PROJECT)) - break; - } - - if (flg & PROJECT_STOP) { - if ((n > 0) && (player_bold(player_ptr, y, x) || floor_ptr->grid_array[y][x].m_idx != 0)) - break; - } - - if (!in_bounds(floor_ptr, y, x)) - break; - - if (m) { - frac += m; - if (frac > half) { - x += sx; - frac -= full; - k++; - } - } - - y += sy; - } - - return n; - } - - /* Horizontal */ - if (ax > ay) { - m = ay * ay * 2; - y = y1; - x = x1 + sx; - frac = m; - if (frac > half) { - y += sy; - frac -= full; - k++; - } - - while (TRUE) { - gp[n++] = GRID(y, x); - if ((n + (k >> 1)) >= range) - break; - - if (!(flg & (PROJECT_THRU))) { - if ((x == x2) && (y == y2)) - break; - } - - if (flg & (PROJECT_DISI)) { - if ((n > 0) && cave_stop_disintegration(floor_ptr, y, x)) - break; - } else if (flg & (PROJECT_LOS)) { - if ((n > 0) && !cave_los_bold(floor_ptr, y, x)) - break; - } else if (!(flg & (PROJECT_PATH))) { - if ((n > 0) && !cave_have_flag_bold(floor_ptr, y, x, FF_PROJECT)) - break; - } - - if (flg & (PROJECT_STOP)) { - if ((n > 0) && (player_bold(player_ptr, y, x) || floor_ptr->grid_array[y][x].m_idx != 0)) - break; - } - - if (!in_bounds(floor_ptr, y, x)) - break; - - if (m) { - frac += m; - if (frac > half) { - y += sy; - frac -= full; - k++; - } - } - - x += sx; - } - - return n; - } - - y = y1 + sy; - x = x1 + sx; - - while (TRUE) { - gp[n++] = GRID(y, x); - if ((n + (n >> 1)) >= range) - break; - - if (!(flg & (PROJECT_THRU))) { - if ((x == x2) && (y == y2)) - break; - } - - if (flg & (PROJECT_DISI)) { - if ((n > 0) && cave_stop_disintegration(floor_ptr, y, x)) - break; - } else if (flg & (PROJECT_LOS)) { - if ((n > 0) && !cave_los_bold(floor_ptr, y, x)) - break; - } else if (!(flg & (PROJECT_PATH))) { - if ((n > 0) && !cave_have_flag_bold(floor_ptr, y, x, FF_PROJECT)) - break; - } - - if (flg & (PROJECT_STOP)) { - if ((n > 0) && (player_bold(player_ptr, y, x) || floor_ptr->grid_array[y][x].m_idx != 0)) - break; - } - - if (!in_bounds(floor_ptr, y, x)) - break; - - y += sy; - x += sx; - } - - return n; -} - -/*! * @brief 指定のマスを床地形に変える / Set a square to be floor. (Includes range checking.) * @param player_ptr プレーヤーへの参照ポインタ * @param x 地形を変えたいマスのX座標 diff --git a/src/floor/floor.h b/src/floor/floor.h index 507538399..144ecabb6 100644 --- a/src/floor/floor.h +++ b/src/floor/floor.h @@ -5,12 +5,6 @@ extern floor_type floor_info; /* - * Convert a "location" (Y,X) into a "grid" (G) - */ -#define GRID(Y,X) \ - (256 * (Y) + (X)) - -/* * Convert a "grid" (G) into a "location" (Y) */ #define GRID_Y(G) \ @@ -40,7 +34,6 @@ void wipe_o_list(floor_type *floor_ptr); bool get_is_floor(floor_type *floor_ptr, POSITION x, POSITION y); FEAT_IDX conv_dungeon_feat(floor_type *floor_ptr, FEAT_IDX newfeat); void vault_objects(player_type *player_ptr, POSITION y, POSITION x, int num); -int projection_path(player_type *player_ptr, u16b *gp, POSITION range, POSITION y1, POSITION x1, POSITION y2, POSITION x2, BIT_FLAGS flg); void set_floor(player_type *player_ptr, POSITION x, POSITION y); void place_object(player_type *owner_ptr, POSITION y, POSITION x, BIT_FLAGS mode); void place_gold(player_type *player_ptr, POSITION y, POSITION x); diff --git a/src/io/cursor.c b/src/io/cursor.c index 7e3bbe6f6..a57ae2f95 100644 --- a/src/io/cursor.c +++ b/src/io/cursor.c @@ -12,6 +12,7 @@ #include "grid/grid.h" #include "io/screen-util.h" #include "system/floor-type-definition.h" +#include "target/projection-path-calculator.h" #include "term/term-color-types.h" #include "view/display-map.h" #include "window/main-window-util.h" diff --git a/src/mind/mind-ninja.c b/src/mind/mind-ninja.c index d9d9b470c..e95dc2ec1 100644 --- a/src/mind/mind-ninja.c +++ b/src/mind/mind-ninja.c @@ -42,6 +42,7 @@ #include "status/element-resistance.h" #include "status/temporary-resistance.h" #include "system/floor-type-definition.h" +#include "target/projection-path-calculator.h" #include "target/target-checker.h" #include "target/target-getter.h" #include "util/bit-flags-calculator.h" diff --git a/src/mspell/mspell-checker.c b/src/mspell/mspell-checker.c index 045e5cbda..39cea85f2 100644 --- a/src/mspell/mspell-checker.c +++ b/src/mspell/mspell-checker.c @@ -51,6 +51,7 @@ #include "spell/range-calc.h" #include "spell/spell-types.h" #include "system/floor-type-definition.h" +#include "target/projection-path-calculator.h" #include "util/bit-flags-calculator.h" #include "view/display-messages.h" #include "world/world.h" diff --git a/src/mspell/mspell-judgement.c b/src/mspell/mspell-judgement.c index 419bf266a..86951977d 100644 --- a/src/mspell/mspell-judgement.c +++ b/src/mspell/mspell-judgement.c @@ -28,6 +28,7 @@ #include "spell/range-calc.h" #include "spell/spell-types.h" #include "system/floor-type-definition.h" +#include "target/projection-path-calculator.h" /*! * @brief モンスターが敵対モンスターにビームを当てること可能かを判定する / diff --git a/src/spell-kind/spells-fetcher.c b/src/spell-kind/spells-fetcher.c index faef529d5..b17a84436 100644 --- a/src/spell-kind/spells-fetcher.c +++ b/src/spell-kind/spells-fetcher.c @@ -15,6 +15,7 @@ #include "monster/monster-update.h" #include "system/floor-type-definition.h" #include "system/object-type-definition.h" +#include "target/projection-path-calculator.h" #include "target/target-checker.h" #include "target/target-setter.h" #include "target/target-types.h" @@ -135,7 +136,7 @@ bool fetch_monster(player_type *caster_ptr) m_ptr = &caster_ptr->current_floor_ptr->m_list[m_idx]; monster_desc(caster_ptr, m_name, m_ptr, 0); msg_format(_("%sを引き戻した。", "You pull back %s."), m_name); - path_n = project_path(caster_ptr, path_g, get_max_range(caster_ptr), target_row, target_col, caster_ptr->y, caster_ptr->x, 0); + path_n = projection_path(caster_ptr, path_g, get_max_range(caster_ptr), target_row, target_col, caster_ptr->y, caster_ptr->x, 0); ty = target_row, tx = target_col; for (i = 1; i < path_n; i++) { POSITION ny = GRID_Y(path_g[i]); diff --git a/src/target/projection-path-calculator.c b/src/target/projection-path-calculator.c new file mode 100644 index 000000000..21bb41b07 --- /dev/null +++ b/src/target/projection-path-calculator.c @@ -0,0 +1,211 @@ +#include "target/projection-path-calculator.h" +#include "effect/effect-characteristics.h" +#include "floor/cave.h" +#include "grid/feature-flag-types.h" +#include "grid/grid.h" +#include "system/floor-type-definition.h" + +/* + * @brief Convert a "location" (Y, X) into a "grid" (G) + * @param y YÀ•W + * @param x XÀ•W + * return Œo˜HÀ•W + */ +static u16b location_to_grid(POSITION y, POSITION x) { return 256 * y + x; } + +/*! + * @brief Žn“_‚©‚çI“_‚Ö‚Ì’¼üŒo˜H‚ð•Ô‚· / + * Determine the path taken by a projection. + * @param player_ptr ƒvƒŒ[ƒ„[‚Ö‚ÌŽQÆƒ|ƒCƒ“ƒ^ + * @param gp Œo˜HÀ•WƒŠƒXƒg‚ð•Ô‚·ŽQÆƒ|ƒCƒ“ƒ^ + * @param range ‹——£ + * @param y1 Žn“_YÀ•W + * @param x1 Žn“_XÀ•W + * @param y2 I“_YÀ•W + * @param x2 I“_XÀ•W + * @param flg ƒ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) +{ + if ((x1 == x2) && (y1 == y2)) + return 0; + + POSITION y, x; + POSITION ay, ax; + POSITION sy, sx; + int frac; + int m; + + if (y2 < y1) { + ay = (y1 - y2); + sy = -1; + } else { + ay = (y2 - y1); + sy = 1; + } + + if (x2 < x1) { + ax = (x1 - x2); + sx = -1; + } else { + ax = (x2 - x1); + sx = 1; + } + + int half = (ay * ax); + int full = half << 1; + floor_type *floor_ptr = player_ptr->current_floor_ptr; + int n = 0; + int k = 0; + + /* Vertical */ + if (ay > ax) { + m = ax * ax * 2; + y = y1 + sy; + x = x1; + frac = m; + if (frac > half) { + x += sx; + frac -= full; + k++; + } + + while (TRUE) { + gp[n++] = location_to_grid(y, x); + if ((n + (k >> 1)) >= range) + break; + + if (!(flg & PROJECT_THRU)) { + if ((x == x2) && (y == y2)) + break; + } + + if (flg & PROJECT_DISI) { + if ((n > 0) && cave_stop_disintegration(floor_ptr, y, x)) + break; + } else if (flg & PROJECT_LOS) { + if ((n > 0) && !cave_los_bold(floor_ptr, y, x)) + break; + } else if (!(flg & PROJECT_PATH)) { + if ((n > 0) && !cave_have_flag_bold(floor_ptr, y, x, FF_PROJECT)) + break; + } + + if (flg & PROJECT_STOP) { + if ((n > 0) && (player_bold(player_ptr, y, x) || floor_ptr->grid_array[y][x].m_idx != 0)) + break; + } + + if (!in_bounds(floor_ptr, y, x)) + break; + + if (m) { + frac += m; + if (frac > half) { + x += sx; + frac -= full; + k++; + } + } + + y += sy; + } + + return n; + } + + /* Horizontal */ + if (ax > ay) { + m = ay * ay * 2; + y = y1; + x = x1 + sx; + frac = m; + if (frac > half) { + y += sy; + frac -= full; + k++; + } + + while (TRUE) { + gp[n++] = location_to_grid(y, x); + if ((n + (k >> 1)) >= range) + break; + + if (!(flg & (PROJECT_THRU))) { + if ((x == x2) && (y == y2)) + break; + } + + if (flg & (PROJECT_DISI)) { + if ((n > 0) && cave_stop_disintegration(floor_ptr, y, x)) + break; + } else if (flg & (PROJECT_LOS)) { + if ((n > 0) && !cave_los_bold(floor_ptr, y, x)) + break; + } else if (!(flg & (PROJECT_PATH))) { + if ((n > 0) && !cave_have_flag_bold(floor_ptr, y, x, FF_PROJECT)) + break; + } + + if (flg & (PROJECT_STOP)) { + if ((n > 0) && (player_bold(player_ptr, y, x) || floor_ptr->grid_array[y][x].m_idx != 0)) + break; + } + + if (!in_bounds(floor_ptr, y, x)) + break; + + if (m) { + frac += m; + if (frac > half) { + y += sy; + frac -= full; + k++; + } + } + + x += sx; + } + + return n; + } + + y = y1 + sy; + x = x1 + sx; + + while (TRUE) { + gp[n++] = location_to_grid(y, x); + if ((n + (n >> 1)) >= range) + break; + + if (!(flg & PROJECT_THRU)) { + if ((x == x2) && (y == y2)) + break; + } + + if (flg & PROJECT_DISI) { + if ((n > 0) && cave_stop_disintegration(floor_ptr, y, x)) + break; + } else if (flg & PROJECT_LOS) { + if ((n > 0) && !cave_los_bold(floor_ptr, y, x)) + break; + } else if (!(flg & PROJECT_PATH)) { + if ((n > 0) && !cave_have_flag_bold(floor_ptr, y, x, FF_PROJECT)) + break; + } + + if (flg & PROJECT_STOP) { + if ((n > 0) && (player_bold(player_ptr, y, x) || floor_ptr->grid_array[y][x].m_idx != 0)) + break; + } + + if (!in_bounds(floor_ptr, y, x)) + break; + + y += sy; + x += sx; + } + + return n; +} diff --git a/src/target/projection-path-calculator.h b/src/target/projection-path-calculator.h new file mode 100644 index 000000000..97709d39d --- /dev/null +++ b/src/target/projection-path-calculator.h @@ -0,0 +1,5 @@ +#pragma once + +#include "system/angband.h" + +int projection_path(player_type *player_ptr, u16b *gp, POSITION range, POSITION y1, POSITION x1, POSITION y2, POSITION x2, BIT_FLAGS flg); -- 2.11.0