From e13612254347f597bccb3d9047a9bf21a9910776 Mon Sep 17 00:00:00 2001 From: Hourier <66951241+Hourier@users.noreply.github.com> Date: Mon, 20 Nov 2023 22:25:00 +0900 Subject: [PATCH] =?utf8?q?[Refactor]=20#3745=20Chest=20=E3=82=AF=E3=83=A9?= =?utf8?q?=E3=82=B9=E3=81=AE=E3=83=A1=E3=82=BD=E3=83=83=E3=83=89=E5=BC=95?= =?utf8?q?=E6=95=B0=E3=82=92Pos2D=20=E3=81=AB=E5=A4=89=E3=81=88=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- src/action/open-close-execution.cpp | 2 +- src/cmd-action/cmd-open-close.cpp | 12 ++-- src/specific-object/chest.cpp | 116 +++++++++++++----------------------- src/specific-object/chest.h | 6 +- 4 files changed, 53 insertions(+), 83 deletions(-) diff --git a/src/action/open-close-execution.cpp b/src/action/open-close-execution.cpp index da65b3340..c8a23b9a9 100644 --- a/src/action/open-close-execution.cpp +++ b/src/action/open-close-execution.cpp @@ -252,7 +252,7 @@ bool exe_disarm_chest(PlayerType *player_ptr, POSITION y, POSITION x, OBJECT_IDX } else { msg_print(_("ãã©ãããä½åããã¦ãã¾ã£ãï¼", "You set off a trap!")); sound(SOUND_FAIL); - Chest(player_ptr).chest_trap(pos.y, pos.x, o_idx); + Chest(player_ptr).fire_trap(pos, o_idx); } return more; diff --git a/src/cmd-action/cmd-open-close.cpp b/src/cmd-action/cmd-open-close.cpp index c53c0bb83..a250eda60 100644 --- a/src/cmd-action/cmd-open-close.cpp +++ b/src/cmd-action/cmd-open-close.cpp @@ -45,10 +45,10 @@ * @details * Assume there is no monster blocking the destination */ -static bool exe_open_chest(PlayerType *player_ptr, POSITION y, POSITION x, OBJECT_IDX o_idx) +static bool exe_open_chest(PlayerType *player_ptr, const Pos2D &pos, OBJECT_IDX o_idx) { - bool flag = true; - bool more = false; + auto flag = true; + auto more = false; auto *o_ptr = &player_ptr->current_floor_ptr->o_list[o_idx]; PlayerEnergy(player_ptr).set_player_turn_energy(100); if (o_ptr->pval > 0) { @@ -84,8 +84,8 @@ static bool exe_open_chest(PlayerType *player_ptr, POSITION y, POSITION x, OBJEC if (flag) { Chest chest(player_ptr); - chest.chest_trap(y, x, o_idx); - chest.chest_death(false, y, x, o_idx); + chest.fire_trap(pos, o_idx); + chest.open(false, pos, o_idx); } return more; @@ -137,7 +137,7 @@ void do_cmd_open(PlayerType *player_ptr) msg_print(_("ã¢ã³ã¹ã¿ã¼ãç«ã¡ãµããã£ã¦ããï¼", "There is a monster in the way!")); do_cmd_attack(player_ptr, y, x, HISSATSU_NONE); } else if (o_idx) { - more = exe_open_chest(player_ptr, y, x, o_idx); + more = exe_open_chest(player_ptr, { y, x }, o_idx); } else { more = exe_open(player_ptr, y, x); } diff --git a/src/specific-object/chest.cpp b/src/specific-object/chest.cpp index 5fbff7fc5..f026fb055 100644 --- a/src/specific-object/chest.cpp +++ b/src/specific-object/chest.cpp @@ -35,36 +35,23 @@ Chest::Chest(PlayerType *player_ptr) } /*! - * @brief ç®±ããã¢ã¤ãã ãå¼ãåºã / - * Allocates objects upon opening a chest -BEN- + * @brief ç®±ããã¢ã¤ãã ãå¼ãåºã * @param scatter TRUEãªãã°ãã©ããã«ããã¢ã¤ãã ã®æ¡æ£å¦ç - * @param y ç®±ã®åå¨ãããã¹ã®Yåº§æ¨ - * @param x ç®±ã®åå¨ãããã¹ã®Xåº§æ¨ - * @param o_idx ç®±ã®ãªãã¸ã§ã¯ãID - * @return ãªã - * @details - *
- * Disperse treasures from the given chest, centered at (x,y). - * - * Small chests often contain "gold", while Large chests always contain - * items. Wooden chests contain 2 items, Iron chests contain 4 items, - * and Steel chests contain 6 items. The "value" of the items in a - * chest is based on the "power" of the chest, which is in turn based - * on the level on which the chest is generated. - *+ * @param pos ç®±ã®åº§æ¨ + * @param item_idx ããã¢å ã¢ã¤ãã ID */ -void Chest::chest_death(bool scatter, POSITION y, POSITION x, OBJECT_IDX o_idx) +void Chest::open(bool scatter, const Pos2D &pos, short item_idx) { BIT_FLAGS mode = AM_GOOD | AM_FORBID_CHEST; - auto *floor_ptr = this->player_ptr->current_floor_ptr; - auto *o_ptr = &floor_ptr->o_list[o_idx]; - if (!o_ptr->is_valid()) { + auto &floor = *this->player_ptr->current_floor_ptr; + auto &item = floor.o_list[item_idx]; + if (!item.is_valid()) { msg_print(_("ç®±ã¯æ¢ã«å£ãã¦ãã¾ã£ã¦ããâ¦", "The chest was broken and you couldn't open it...")); return; } /* Small chests often hold "gold" */ - const auto sval = *o_ptr->bi_key.sval(); + const auto sval = *item.bi_key.sval(); auto small = sval < SV_CHEST_MIN_LARGE; /* Determine how much to drop (see above) */ @@ -74,14 +61,12 @@ void Chest::chest_death(bool scatter, POSITION y, POSITION x, OBJECT_IDX o_idx) number = 5; small = false; mode |= AM_GREAT; - floor_ptr->object_level = o_ptr->chest_level; + floor.object_level = item.chest_level; } else { - /* Determine the "value" of the items */ - floor_ptr->object_level = std::abs(o_ptr->pval) + 10; + floor.object_level = std::abs(item.pval) + 10; } - /* Zero pval means empty chest */ - if (!o_ptr->pval) { + if (item.pval == 0) { number = 0; } @@ -110,11 +95,10 @@ void Chest::chest_death(bool scatter, POSITION y, POSITION x, OBJECT_IDX o_idx) /* If chest scatters its contents, pick any floor square. */ if (scatter) { - int i; - for (i = 0; i < 200; i++) { + for (auto i = 0; i < 200; i++) { /* Pick a totally random spot. */ - y = randint0(MAX_HGT); - x = randint0(MAX_WID); + const auto y = randint0(MAX_HGT); + const auto x = randint0(MAX_WID); /* Must be an empty floor. */ if (!is_cave_empty_bold(this->player_ptr, y, x)) { @@ -130,37 +114,24 @@ void Chest::chest_death(bool scatter, POSITION y, POSITION x, OBJECT_IDX o_idx) } /* Normally, drop object near the chest. */ else { - (void)drop_near(this->player_ptr, q_ptr, -1, y, x); + (void)drop_near(this->player_ptr, q_ptr, -1, pos.y, pos.x); } } - /* Reset the object level */ - floor_ptr->object_level = floor_ptr->base_level; - - /* Empty */ - o_ptr->pval = 0; - - o_ptr->mark_as_known(); + floor.object_level = floor.base_level; + item.pval = 0; + item.mark_as_known(); } /*! - * @brief ç®±ã®ãã©ããå¦ç / - * Chests have traps too. - * @param y ç®±ã®åå¨ãããã¹ã®Yåº§æ¨ + * @brief ç®±ã®ãã©ããå¦ç + * @param pos ç®±ã®åº§æ¨ * @param x ç®±ã®åå¨ãããã¹ã®Xåº§æ¨ - * @param o_idx ç®±ã®ãªãã¸ã§ã¯ãID - * @return ãªã - * @details - *
- * Exploding chest destroys contents (and traps). - * Note that the chest itself is never destroyed. - *+ * @param item_idx ç®±ã®ãªãã¸ã§ã¯ãID */ -void Chest::chest_trap(POSITION y, POSITION x, OBJECT_IDX o_idx) +void Chest::fire_trap(const Pos2D &pos, short item_idx) { - int i; - - auto *o_ptr = &this->player_ptr->current_floor_ptr->o_list[o_idx]; + auto *o_ptr = &this->player_ptr->current_floor_ptr->o_list[item_idx]; int mon_level = o_ptr->chest_level; @@ -169,8 +140,7 @@ void Chest::chest_trap(POSITION y, POSITION x, OBJECT_IDX o_idx) return; } - /* Obtain the traps */ - auto trap = chest_traps[o_ptr->pval]; + const auto &trap = chest_traps[o_ptr->pval]; /* Lose strength */ if (trap.has(ChestTrapType::LOSE_STR)) { @@ -206,11 +176,11 @@ void Chest::chest_trap(POSITION y, POSITION x, OBJECT_IDX o_idx) if (trap.has(ChestTrapType::SUMMON)) { int num = 2 + randint1(3); msg_print(_("çªå¦å¹ãåºããç ã«å ã¿è¾¼ã¾ããï¼", "You are enveloped in a cloud of smoke!")); - for (i = 0; i < num; i++) { + for (auto i = 0; i < num; i++) { if (randint1(100) < this->player_ptr->current_floor_ptr->dun_level) { activate_hi_summon(this->player_ptr, this->player_ptr->y, this->player_ptr->x, false); } else { - (void)summon_specific(this->player_ptr, 0, y, x, mon_level, SUMMON_NONE, (PM_ALLOW_GROUP | PM_ALLOW_UNIQUE | PM_NO_PET)); + (void)summon_specific(this->player_ptr, 0, pos.y, pos.x, mon_level, SUMMON_NONE, (PM_ALLOW_GROUP | PM_ALLOW_UNIQUE | PM_NO_PET)); } } } @@ -218,8 +188,8 @@ void Chest::chest_trap(POSITION y, POSITION x, OBJECT_IDX o_idx) /* Elemental summon. */ if (trap.has(ChestTrapType::E_SUMMON)) { msg_print(_("å®ãå®ãããã«ã¨ã¬ã¡ã³ã¿ã«ãç¾ããï¼", "Elemental beings appear to protect their treasures!")); - for (i = 0; i < randint1(3) + 5; i++) { - (void)summon_specific(this->player_ptr, 0, y, x, mon_level, SUMMON_ELEMENTAL, (PM_ALLOW_GROUP | PM_ALLOW_UNIQUE | PM_NO_PET)); + for (auto i = 0; i < randint1(3) + 5; i++) { + (void)summon_specific(this->player_ptr, 0, pos.y, pos.x, mon_level, SUMMON_ELEMENTAL, (PM_ALLOW_GROUP | PM_ALLOW_UNIQUE | PM_NO_PET)); } } @@ -227,12 +197,12 @@ void Chest::chest_trap(POSITION y, POSITION x, OBJECT_IDX o_idx) if (trap.has(ChestTrapType::BIRD_STORM)) { msg_print(_("é³¥ã®ç¾¤ããããªããåãå·»ããï¼", "A storm of birds swirls around you!")); - for (i = 0; i < randint1(3) + 3; i++) { - (void)fire_meteor(this->player_ptr, -1, AttributeType::FORCE, y, x, o_ptr->pval / 5, 7); + for (auto i = 0; i < randint1(3) + 3; i++) { + (void)fire_meteor(this->player_ptr, -1, AttributeType::FORCE, pos.y, pos.x, o_ptr->pval / 5, 7); } - for (i = 0; i < randint1(5) + o_ptr->pval / 5; i++) { - (void)summon_specific(this->player_ptr, 0, y, x, mon_level, SUMMON_BIRD, (PM_ALLOW_GROUP | PM_ALLOW_UNIQUE | PM_NO_PET)); + for (auto i = 0; i < randint1(5) + o_ptr->pval / 5; i++) { + (void)summon_specific(this->player_ptr, 0, pos.y, pos.x, mon_level, SUMMON_BIRD, (PM_ALLOW_GROUP | PM_ALLOW_UNIQUE | PM_NO_PET)); } } @@ -241,33 +211,33 @@ void Chest::chest_trap(POSITION y, POSITION x, OBJECT_IDX o_idx) /* Summon demons. */ if (one_in_(4)) { msg_print(_("çã¨ç¡«é»ã®é²ã®ä¸ã«æªéã姿ãç¾ããï¼", "Demons materialize in clouds of fire and brimstone!")); - for (i = 0; i < randint1(3) + 2; i++) { - (void)fire_meteor(this->player_ptr, -1, AttributeType::FIRE, y, x, 10, 5); - (void)summon_specific(this->player_ptr, 0, y, x, mon_level, SUMMON_DEMON, (PM_ALLOW_GROUP | PM_ALLOW_UNIQUE | PM_NO_PET)); + for (auto i = 0; i < randint1(3) + 2; i++) { + (void)fire_meteor(this->player_ptr, -1, AttributeType::FIRE, pos.y, pos.x, 10, 5); + (void)summon_specific(this->player_ptr, 0, pos.y, pos.x, mon_level, SUMMON_DEMON, (PM_ALLOW_GROUP | PM_ALLOW_UNIQUE | PM_NO_PET)); } } /* Summon dragons. */ else if (one_in_(3)) { msg_print(_("æéã«ãã©ã´ã³ã®å½±ãã¼ãããã¨ç¾ããï¼", "Draconic forms loom out of the darkness!")); - for (i = 0; i < randint1(3) + 2; i++) { - (void)summon_specific(this->player_ptr, 0, y, x, mon_level, SUMMON_DRAGON, (PM_ALLOW_GROUP | PM_ALLOW_UNIQUE | PM_NO_PET)); + for (auto i = 0; i < randint1(3) + 2; i++) { + (void)summon_specific(this->player_ptr, 0, pos.y, pos.x, mon_level, SUMMON_DRAGON, (PM_ALLOW_GROUP | PM_ALLOW_UNIQUE | PM_NO_PET)); } } /* Summon hybrids. */ else if (one_in_(2)) { msg_print(_("å¥å¦ãªå§¿ã®æªç©ã襲ã£ã¦æ¥ãï¼", "Creatures strange and twisted assault you!")); - for (i = 0; i < randint1(5) + 3; i++) { - (void)summon_specific(this->player_ptr, 0, y, x, mon_level, SUMMON_HYBRID, (PM_ALLOW_GROUP | PM_ALLOW_UNIQUE | PM_NO_PET)); + for (auto i = 0; i < randint1(5) + 3; i++) { + (void)summon_specific(this->player_ptr, 0, pos.y, pos.x, mon_level, SUMMON_HYBRID, (PM_ALLOW_GROUP | PM_ALLOW_UNIQUE | PM_NO_PET)); } } /* Summon vortices (scattered) */ else { msg_print(_("渦巻ãåä½ããç ´è£ããï¼", "Vortices coalesce and wreak destruction!")); - for (i = 0; i < randint1(3) + 2; i++) { - (void)summon_specific(this->player_ptr, 0, y, x, mon_level, SUMMON_VORTEX, (PM_ALLOW_GROUP | PM_ALLOW_UNIQUE | PM_NO_PET)); + for (auto i = 0; i < randint1(3) + 2; i++) { + (void)summon_specific(this->player_ptr, 0, pos.y, pos.x, mon_level, SUMMON_VORTEX, (PM_ALLOW_GROUP | PM_ALLOW_UNIQUE | PM_NO_PET)); } } } @@ -316,7 +286,7 @@ void Chest::chest_trap(POSITION y, POSITION x, OBJECT_IDX o_idx) continue; } - (void)fire_meteor(this->player_ptr, -1, AttributeType::NETHER, y, x, 150, 1); + (void)fire_meteor(this->player_ptr, -1, AttributeType::NETHER, pos.y, pos.x, 150, 1); } } @@ -337,7 +307,7 @@ void Chest::chest_trap(POSITION y, POSITION x, OBJECT_IDX o_idx) /* Scatter contents. */ if ((trap.has(ChestTrapType::SCATTER)) && o_ptr->is_valid()) { msg_print(_("å®ç®±ã®ä¸èº«ã¯ãã³ã¸ã§ã³ãã ãã«æ£ä¹±ããï¼", "The contents of the chest scatter all over the dungeon!")); - this->chest_death(true, y, x, o_idx); + this->open(true, pos, item_idx); o_ptr->pval = 0; } } diff --git a/src/specific-object/chest.h b/src/specific-object/chest.h index 5f9b9c45f..d28e6e442 100644 --- a/src/specific-object/chest.h +++ b/src/specific-object/chest.h @@ -1,14 +1,14 @@ #pragma once -#include "system/angband.h" +#include "util/point-2d.h" class PlayerType; class Chest { public: Chest(PlayerType *player_ptr); virtual ~Chest() = default; - void chest_death(bool scatter, POSITION y, POSITION x, OBJECT_IDX o_idx); - void chest_trap(POSITION y, POSITION x, OBJECT_IDX o_idx); + void open(bool scatter, const Pos2D &pos, short item_idx); + void fire_trap(const Pos2D &pos, short item_idx); private: PlayerType *player_ptr; -- 2.11.0