From 2a531549fbbe6b273e7eec35dce9062fbc173c41 Mon Sep 17 00:00:00 2001 From: Hourier <66951241+Hourier@users.noreply.github.com> Date: Thu, 23 Nov 2023 19:07:23 +0900 Subject: [PATCH] =?utf8?q?[Refactor]=20#3745=20mmove2()=20=E3=81=AE?= =?utf8?q?=E5=BC=95=E6=95=B0=E3=81=8B=E3=82=89=E3=83=9D=E3=82=A4=E3=83=B3?= =?utf8?q?=E3=82=BF=E3=82=92=E9=99=A4=E5=8E=BB=E3=81=97=E3=80=81=E6=88=BB?= =?utf8?q?=E3=82=8A=E5=80=A4=E3=82=92Pos2D=20=E3=81=AB=E5=A4=89=E3=81=88?= =?utf8?q?=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- src/combat/shoot.cpp | 10 +++--- src/floor/geometry.cpp | 57 +++++++++++++++-------------------- src/floor/geometry.h | 19 ++++++------ src/object-use/throw-execution.cpp | 6 ++-- src/spell-realm/spells-crusade.cpp | 62 +++++++++++++++++--------------------- 5 files changed, 70 insertions(+), 84 deletions(-) diff --git a/src/combat/shoot.cpp b/src/combat/shoot.cpp index 1d3900ae1..b58f93c5e 100644 --- a/src/combat/shoot.cpp +++ b/src/combat/shoot.cpp @@ -628,9 +628,9 @@ void exe_fire(PlayerType *player_ptr, INVENTORY_IDX i_idx, ItemEntity *j_ptr, SP } /* Calculate the new location (see "project()") */ - ny = y; - nx = x; - mmove2(&ny, &nx, player_ptr->y, player_ptr->x, ty, tx); + const auto pos = mmove2({ y, x }, player_ptr->get_position(), { ty, tx }); + ny = pos.y; + nx = pos.x; /* Shatter Arrow */ if (snipe_type == SP_KILL_WALL) { @@ -877,7 +877,9 @@ void exe_fire(PlayerType *player_ptr, INVENTORY_IDX i_idx, ItemEntity *j_ptr, SP } /* Calculate the new location (see "project()") */ - mmove2(&ny, &nx, player_ptr->y, player_ptr->x, ty, tx); + const auto pos_to = mmove2({ ny, nx }, player_ptr->get_position(), { ty, tx }); + ny = pos_to.y; + nx = pos_to.x; /* Stopped by wilderness boundary */ if (!in_bounds2(floor_ptr, ny, nx)) { diff --git a/src/floor/geometry.cpp b/src/floor/geometry.cpp index f8755675e..502a1ca8f 100644 --- a/src/floor/geometry.cpp +++ b/src/floor/geometry.cpp @@ -16,42 +16,42 @@ /*! * キーパッドの方向を南から反時計回り順に列挙 / Global array for looping through the "keypad directions" */ -const POSITION ddd[9] = { 2, 8, 6, 4, 3, 1, 9, 7, 5 }; +const int ddd[9] = { 2, 8, 6, 4, 3, 1, 9, 7, 5 }; /*! * dddで定義した順にベクトルのX軸成分を定義 / Global arrays for converting "keypad direction" into offsets */ -const POSITION ddx[10] = { 0, -1, 0, 1, -1, 0, 1, -1, 0, 1 }; +const int ddx[10] = { 0, -1, 0, 1, -1, 0, 1, -1, 0, 1 }; /*! * dddで定義した順にベクトルのY軸成分を定義 / Global arrays for converting "keypad direction" into offsets */ -const POSITION ddy[10] = { 0, 1, 1, 1, 0, 0, 0, -1, -1, -1 }; +const int ddy[10] = { 0, 1, 1, 1, 0, 0, 0, -1, -1, -1 }; /*! * ddd越しにベクトルのX軸成分を定義 / Global arrays for optimizing "ddx[ddd[i]]" and "ddy[ddd[i]]" */ -const POSITION ddx_ddd[9] = { 0, 0, 1, -1, 1, -1, 1, -1, 0 }; +const int ddx_ddd[9] = { 0, 0, 1, -1, 1, -1, 1, -1, 0 }; /*! * ddd越しにベクトルのY軸成分を定義 / Global arrays for optimizing "ddx[ddd[i]]" and "ddy[ddd[i]]" */ -const POSITION ddy_ddd[9] = { 1, -1, 0, 0, 1, 1, -1, -1, 0 }; +const int ddy_ddd[9] = { 1, -1, 0, 0, 1, 1, -1, -1, 0 }; /*! * キーパッドの円環状方向配列 / Circular keypad direction array */ -const POSITION cdd[8] = { 2, 3, 6, 9, 8, 7, 4, 1 }; +const int cdd[8] = { 2, 3, 6, 9, 8, 7, 4, 1 }; /*! * cdd越しにベクトルのX軸成分を定義 / Global arrays for optimizing "ddx[cdd[i]]" and "ddy[cdd[i]]" */ -const POSITION ddx_cdd[8] = { 0, 1, 1, 1, 0, -1, -1, -1 }; +const int ddx_cdd[8] = { 0, 1, 1, 1, 0, -1, -1, -1 }; /*! * cdd越しにベクトルのY軸成分を定義 / Global arrays for optimizing "ddx[cdd[i]]" and "ddy[cdd[i]]" */ -const POSITION ddy_cdd[8] = { 1, 1, 0, -1, -1, -1, 0, 1 }; +const int ddy_cdd[8] = { 1, 1, 0, -1, -1, -1, 0, 1 }; /*! * @brief 2点間の距離をニュートン・ラプソン法で算出する / Distance between two points via Newton-Raphson technique @@ -195,52 +195,43 @@ bool player_can_see_bold(PlayerType *player_ptr, POSITION y, POSITION x) * Calculate "incremental motion". Used by project() and shoot(). * Assumes that (*y,*x) lies on the path from (y1,x1) to (y2,x2). */ -void mmove2(POSITION *y, POSITION *x, POSITION y1, POSITION x1, POSITION y2, POSITION x2) +Pos2D mmove2(const Pos2D &pos_orig, const Pos2D &pos1, const Pos2D &pos2) { - POSITION dy, dx, dist, shift; - /* Extract the distance travelled */ - dy = (*y < y1) ? y1 - *y : *y - y1; - dx = (*x < x1) ? x1 - *x : *x - x1; + auto dy = (pos_orig.y < pos1.y) ? pos1.y - pos_orig.y : pos_orig.y - pos1.y; + auto dx = (pos_orig.x < pos1.x) ? pos1.x - pos_orig.x : pos_orig.x - pos1.x; /* Number of steps */ - dist = (dy > dx) ? dy : dx; + auto dist = (dy > dx) ? dy : dx; /* We are calculating the next location */ dist++; /* Calculate the total distance along each axis */ - dy = (y2 < y1) ? (y1 - y2) : (y2 - y1); - dx = (x2 < x1) ? (x1 - x2) : (x2 - x1); + dy = (pos2.y < pos1.y) ? (pos1.y - pos2.y) : (pos2.y - pos1.y); + dx = (pos2.x < pos1.x) ? (pos1.x - pos2.x) : (pos2.x - pos1.x); /* Paranoia -- Hack -- no motion */ if (!dy && !dx) { - return; + return pos_orig; } /* Move mostly vertically */ if (dy > dx) { /* Extract a shift factor */ - shift = (dist * dx + (dy - 1) / 2) / dy; - - /* Sometimes move along the minor axis */ - (*x) = (x2 < x1) ? (x1 - shift) : (x1 + shift); + auto shift = (dist * dx + (dy - 1) / 2) / dy; - /* Always move along major axis */ - (*y) = (y2 < y1) ? (y1 - dist) : (y1 + dist); + /* Sometimes move along the minor axis, Always move along major axis */ + const auto y = (pos2.y < pos1.y) ? (pos1.y - dist) : (pos1.y + dist); + const auto x = (pos2.x < pos1.x) ? (pos1.x - shift) : (pos1.x + shift); + return { y, x }; } /* Move mostly horizontally */ - else { - /* Extract a shift factor */ - shift = (dist * dy + (dx - 1) / 2) / dx; - - /* Sometimes move along the minor axis */ - (*y) = (y2 < y1) ? (y1 - shift) : (y1 + shift); - - /* Always move along major axis */ - (*x) = (x2 < x1) ? (x1 - dist) : (x1 + dist); - } + auto shift = (dist * dy + (dx - 1) / 2) / dx; + const auto y = (pos2.y < pos1.y) ? (pos1.y - shift) : (pos1.y + shift); + const auto x = (pos2.x < pos1.x) ? (pos1.x - dist) : (pos1.x + dist); + return { y, x }; } /*! diff --git a/src/floor/geometry.h b/src/floor/geometry.h index 903132636..6ddbd4fd5 100644 --- a/src/floor/geometry.h +++ b/src/floor/geometry.h @@ -1,6 +1,7 @@ #pragma once #include "system/angband.h" +#include "util/point-2d.h" /*! * @brief 視界及び光源の過渡処理配列サイズ / Maximum size of the "temp" array @@ -19,19 +20,19 @@ struct pos_list { POSITION x[TEMP_MAX]; }; -extern const POSITION ddd[9]; -extern const POSITION ddx[10]; -extern const POSITION ddy[10]; -extern const POSITION ddx_ddd[9]; -extern const POSITION ddy_ddd[9]; -extern const POSITION cdd[8]; -extern const POSITION ddx_cdd[8]; -extern const POSITION ddy_cdd[8]; +extern const int ddd[9]; +extern const int ddx[10]; +extern const int ddy[10]; +extern const int ddx_ddd[9]; +extern const int ddy_ddd[9]; +extern const int cdd[8]; +extern const int ddx_cdd[8]; +extern const int ddy_cdd[8]; class PlayerType; DIRECTION coords_to_dir(PlayerType *player_ptr, POSITION y, POSITION x); POSITION distance(POSITION y1, POSITION x1, POSITION y2, POSITION x2); -void mmove2(POSITION *y, POSITION *x, POSITION y1, POSITION x1, POSITION y2, POSITION x2); +Pos2D mmove2(const Pos2D &pos_orig, const Pos2D &pos1, const Pos2D &pos2); bool player_can_see_bold(PlayerType *player_ptr, POSITION y, POSITION x); class MonsterEntity; diff --git a/src/object-use/throw-execution.cpp b/src/object-use/throw-execution.cpp index da088154c..f813ef2c6 100644 --- a/src/object-use/throw-execution.cpp +++ b/src/object-use/throw-execution.cpp @@ -389,9 +389,9 @@ bool ObjectThrowEntity::check_throw_boomerang() bool ObjectThrowEntity::check_racial_target_bold() { - this->ny[this->cur_dis] = this->y; - this->nx[this->cur_dis] = this->x; - mmove2(&this->ny[this->cur_dis], &this->nx[this->cur_dis], this->player_ptr->y, this->player_ptr->x, this->ty, this->tx); + const auto pos = mmove2({ this->y, this->x }, this->player_ptr->get_position(), { this->ty, this->tx }); + this->ny[this->cur_dis] = pos.y; + this->nx[this->cur_dis] = pos.x; auto *floor_ptr = this->player_ptr->current_floor_ptr; if (cave_has_flag_bold(floor_ptr, this->ny[this->cur_dis], this->nx[this->cur_dis], TerrainCharacteristics::PROJECT)) { return false; diff --git a/src/spell-realm/spells-crusade.cpp b/src/spell-realm/spells-crusade.cpp index 1255f1382..30ec5e9ae 100644 --- a/src/spell-realm/spells-crusade.cpp +++ b/src/spell-realm/spells-crusade.cpp @@ -36,60 +36,48 @@ */ bool cast_wrath_of_the_god(PlayerType *player_ptr, int dam, POSITION rad) { - DIRECTION dir; + int dir; if (!get_aim_dir(player_ptr, &dir)) { return false; } - POSITION tx = player_ptr->x + 99 * ddx[dir]; - POSITION ty = player_ptr->y + 99 * ddy[dir]; + Pos2D pos_target(player_ptr->y + 99 * ddy[dir], player_ptr->x + 99 * ddx[dir]); if ((dir == 5) && target_okay(player_ptr)) { - tx = target_col; - ty = target_row; + pos_target.x = target_col; + pos_target.y = target_row; } - POSITION x = player_ptr->x; - POSITION y = player_ptr->y; - POSITION nx, ny; + Pos2D pos = player_ptr->get_position(); + auto &floor = *player_ptr->current_floor_ptr; while (true) { - if ((y == ty) && (x == tx)) { + if (pos == pos_target) { break; } - ny = y; - nx = x; - mmove2(&ny, &nx, player_ptr->y, player_ptr->x, ty, tx); - if (AngbandSystem::get_instance().get_max_range() <= distance(player_ptr->y, player_ptr->x, ny, nx)) { + const auto pos_to = mmove2(pos, player_ptr->get_position(), pos_target); + if (AngbandSystem::get_instance().get_max_range() <= distance(player_ptr->y, player_ptr->x, pos_to.y, pos_to.x)) { break; } - if (!cave_has_flag_bold(player_ptr->current_floor_ptr, ny, nx, TerrainCharacteristics::PROJECT)) { + if (!cave_has_flag_bold(&floor, pos_to.y, pos_to.x, TerrainCharacteristics::PROJECT)) { break; } - if ((dir != 5) && player_ptr->current_floor_ptr->grid_array[ny][nx].m_idx != 0) { + if ((dir != 5) && floor.get_grid(pos_to).m_idx != 0) { break; } - x = nx; - y = ny; + pos = pos_to; } - tx = x; - ty = y; - - int b = 10 + randint1(10); - for (int i = 0; i < b; i++) { - int count = 20, d = 0; - + pos_target = pos; + const auto b = 10 + randint1(10); + for (auto i = 0; i < b; i++) { + auto count = 20; while (count--) { - int dx, dy; - - x = tx - 5 + randint0(11); - y = ty - 5 + randint0(11); - - dx = (tx > x) ? (tx - x) : (x - tx); - dy = (ty > y) ? (ty - y) : (y - ty); - - d = (dy > dx) ? (dy + (dx >> 1)) : (dx + (dy >> 1)); + const auto x = pos_target.x - 5 + randint0(11); + const auto y = pos_target.y - 5 + randint0(11); + const auto dx = (pos_target.x > x) ? (pos_target.x - x) : (x - pos_target.x); + const auto dy = (pos_target.y > y) ? (pos_target.y - y) : (y - pos_target.y); + const auto d = (dy > dx) ? (dy + (dx >> 1)) : (dx + (dy >> 1)); if (d < 5) { break; } @@ -99,11 +87,15 @@ bool cast_wrath_of_the_god(PlayerType *player_ptr, int dam, POSITION rad) continue; } - if (!in_bounds(player_ptr->current_floor_ptr, y, x) || cave_stop_disintegration(player_ptr->current_floor_ptr, y, x) || !in_disintegration_range(player_ptr->current_floor_ptr, ty, tx, y, x)) { + auto should_cast = in_bounds(&floor, pos.y, pos.x); + should_cast &= !cave_stop_disintegration(&floor, pos.y, pos.x); + should_cast &= in_disintegration_range(&floor, pos_target.y, pos_target.x, pos.y, pos.x); + if (!should_cast) { continue; } - project(player_ptr, 0, rad, y, x, dam, AttributeType::DISINTEGRATE, PROJECT_JUMP | PROJECT_GRID | PROJECT_ITEM | PROJECT_KILL); + constexpr auto mode = PROJECT_JUMP | PROJECT_GRID | PROJECT_ITEM | PROJECT_KILL; + project(player_ptr, 0, rad, pos.y, pos.x, dam, AttributeType::DISINTEGRATE, mode); } return true; -- 2.11.0