Skip to content

Commit

Permalink
[Refactor] #3539 input_stock() の引数からcom_val をなくし、戻り値をoptional<short> …
Browse files Browse the repository at this point in the history
…に変えた
  • Loading branch information
Hourier authored and sikabane-works committed May 14, 2024
1 parent 90b13b0 commit b14f441
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 47 deletions.
13 changes: 7 additions & 6 deletions src/store/museum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,23 @@ void museum_remove_object(PlayerType *player_ptr)
i = store_bottom;
}

COMMAND_CODE item;
if (!input_stock(&item, _("どのアイテムの展示をやめさせますか?", "Which item do you want to order to remove? "), 0, i - 1, StoreSaleType::MUSEUM)) {
constexpr auto mes = _("どのアイテムの展示をやめさせますか?", "Which item do you want to order to remove? ");
auto item_num_opt = input_stock(mes, 0, i - 1, StoreSaleType::MUSEUM);
if (!item_num_opt) {
return;
}

item = item + store_top;
auto *o_ptr = &st_ptr->stock[item];
const short item_num = *item_num_opt + store_top;
auto *o_ptr = &st_ptr->stock[item_num];
const auto item_name = describe_flavor(player_ptr, o_ptr, 0);
msg_print(_("展示をやめさせたアイテムは二度と見ることはできません!", "Once removed from the Museum, an item will be gone forever!"));
if (!input_check(format(_("本当に%sの展示をやめさせますか?", "Really order to remove %s from the Museum? "), item_name.data()))) {
return;
}

msg_format(_("%sの展示をやめさせた。", "You ordered to remove %s."), item_name.data());
store_item_increase(item, -o_ptr->number);
store_item_optimize(item);
store_item_increase(item_num, -o_ptr->number);
store_item_optimize(item_num);
(void)combine_and_reorder_home(player_ptr, StoreSaleType::MUSEUM);
if (st_ptr->stock_num == 0) {
store_top = 0;
Expand Down
37 changes: 16 additions & 21 deletions src/store/purchase-order.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "view/display-store.h"
#include "world/world.h"
#include <optional>
#include <string>

/*!
* @brief プレイヤーが購入する時の値切り処理メインルーチン /
Expand All @@ -62,30 +63,25 @@ static std::optional<PRICE> prompt_to_buy(PlayerType *player_ptr, ItemEntity *o_

/*!
* @brief 店舗から購入する際のアイテム選択プロンプト
* @param item 店舗インベントリ番号(アドレス渡し)
* @param i 店舗インベントリストック数
* @return 選択したらtrue、しなかったらfalse
* @details
* 選択したインベントリ番号はitemに返る。
* ブラックマーケットの時は別のメッセージ。
*/
static bool show_store_select_item(COMMAND_CODE *item, const int i, StoreSaleType store_num)
static std::optional<short> show_store_select_item(const int i, StoreSaleType store_num)
{
char prompt[160];

std::string prompt;
switch (store_num) {
case StoreSaleType::HOME:
sprintf(prompt, _("どのアイテムを取りますか? ", "Which item do you want to take? "));
prompt = _("どのアイテムを取りますか? ", "Which item do you want to take? ");
break;
case StoreSaleType::BLACK:
sprintf(prompt, _("どれ? ", "Which item, huh? "));
prompt = _("どれ? ", "Which item, huh? ");
break;
default:
sprintf(prompt, _("どの品物が欲しいんだい? ", "Which item are you interested in? "));
prompt = _("どの品物が欲しいんだい? ", "Which item are you interested in? ");
break;
}

return input_stock(item, prompt, 0, i - 1, store_num) != 0;
return input_stock(prompt, 0, i - 1, store_num);
}

/*!
Expand Down Expand Up @@ -182,14 +178,13 @@ void store_purchase(PlayerType *player_ptr, StoreSaleType store_num)
i = store_bottom;
}

COMMAND_CODE item;
if (!show_store_select_item(&item, i, store_num)) {
auto item_num_opt = show_store_select_item(i, store_num);
if (!item_num_opt) {
return;
}

item = item + store_top;
ItemEntity *o_ptr;
o_ptr = &st_ptr->stock[item];
const short item_num = *item_num_opt + store_top;
auto *o_ptr = &st_ptr->stock[item_num];

ITEM_NUMBER amt = 1;
ItemEntity forge;
Expand Down Expand Up @@ -240,14 +235,14 @@ void store_purchase(PlayerType *player_ptr, StoreSaleType store_num)
}

if (store_num == StoreSaleType::HOME) {
take_item_from_home(player_ptr, o_ptr, j_ptr, item);
take_item_from_home(player_ptr, o_ptr, j_ptr, item_num);
return;
}

COMMAND_CODE item_new;
PRICE price;
const auto purchased_item_name = describe_flavor(player_ptr, j_ptr, 0);
msg_format(_("%s(%c)を購入する。", "Buying %s (%c)."), purchased_item_name.data(), I2A(item));
msg_format(_("%s(%c)を購入する。", "Buying %s (%c)."), purchased_item_name.data(), I2A(item_num));
msg_print(nullptr);

auto res = prompt_to_buy(player_ptr, j_ptr, store_num);
Expand Down Expand Up @@ -310,7 +305,7 @@ void store_purchase(PlayerType *player_ptr, StoreSaleType store_num)
}

i = st_ptr->stock_num;
store_item_increase(item, -amt);
store_item_optimize(item);
switch_store_stock(player_ptr, i, item, store_num);
store_item_increase(item_num, -amt);
store_item_optimize(item_num);
switch_store_stock(player_ptr, i, item_num, store_num);
}
38 changes: 19 additions & 19 deletions src/store/store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,29 +145,28 @@ int store_check_num(ItemEntity *o_ptr, StoreSaleType store_num)
}

/*!
* @brief 店舗からアイテムを選択する /
* Get the ID of a store item and return its value -RAK-
* @param com_val 選択IDを返す参照ポインタ
* @brief 店舗からアイテムを選択する
* @param pmt メッセージキャプション
* @param min 選択範囲の最小値
* @param max 選択範囲の最大値
* @return 実際に選択したらTRUE、キャンセルしたらFALSE
* @return アイテムを選択したらそのインデックス ('a'等)、キャンセルしたらnullopt
* 繰り返しコマンドの時は前回の前回のインデックス
*/
int input_stock(COMMAND_CODE *com_val, concptr pmt, int min, int max, [[maybe_unused]] StoreSaleType store_num)
std::optional<short> input_stock(std::string_view fmt, int min, int max, [[maybe_unused]] StoreSaleType store_num)
{
if (repeat_pull(com_val) && (*com_val >= min) && (*com_val <= max)) {
return true;
short repeat_command;
if (repeat_pull(&repeat_command) && (repeat_command >= min) && (repeat_command <= max)) {
return repeat_command;
}

msg_print(nullptr);
*com_val = (-1);
const auto lo = I2A(min);
const auto hi = (max > 25) ? toupper(I2A(max - 26)) : I2A(max);
#ifdef JP
const auto title = (store_num == StoreSaleType::HOME) || (store_num == StoreSaleType::MUSEUM) ? "アイテム" : "商品";
const auto prompt = format("(%s:%c-%c, ESCで中断) %s", title, lo, hi, pmt);
const auto prompt = format("(%s:%c-%c, ESCで中断) %s", title, lo, hi, fmt.data());
#else
const auto prompt = format("(Items %c-%c, ESC to exit) %s", lo, hi, pmt);
const auto prompt = format("(Items %c-%c, ESC to exit) %s", lo, hi, fmt.data());
#endif

auto command = ESCAPE;
Expand All @@ -188,7 +187,7 @@ int input_stock(COMMAND_CODE *com_val, concptr pmt, int min, int max, [[maybe_un
}

if ((k >= min) && (k <= max)) {
*com_val = k;
command = k;
break;
}

Expand All @@ -197,11 +196,11 @@ int input_stock(COMMAND_CODE *com_val, concptr pmt, int min, int max, [[maybe_un

prt("", 0, 0);
if (command == ESCAPE) {
return false;
return std::nullopt;
}

repeat_push(*com_val);
return true;
repeat_push(command);
return command;
}

/*!
Expand All @@ -226,13 +225,14 @@ void store_examine(PlayerType *player_ptr, StoreSaleType store_num)
i = store_bottom;
}

COMMAND_CODE item;
if (!input_stock(&item, _("どれを調べますか?", "Which item do you want to examine? "), 0, i - 1, store_num)) {
constexpr auto mes = _("どれを調べますか?", "Which item do you want to examine? ");
auto item_num_opt = input_stock(mes, 0, i - 1, store_num);
if (!item_num_opt) {
return;
}
item = item + store_top;
ItemEntity *o_ptr;
o_ptr = &st_ptr->stock[item];

const auto item_num = *item_num_opt + store_top;
auto *o_ptr = &st_ptr->stock[item_num];
if (!o_ptr->is_fully_known()) {
msg_print(_("このアイテムについて特に知っていることはない。", "You have no special knowledge about that item."));
return;
Expand Down
4 changes: 3 additions & 1 deletion src/store/store.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "store/store-util.h"
#include "system/angband.h"
#include <optional>
#include <string_view>

/* Store constants */
#define STORE_INVEN_MAX 24 /* Max number of discrete objs in inven */
Expand All @@ -28,4 +30,4 @@ void store_maintenance(PlayerType *player_ptr, int town_num, StoreSaleType store
void store_init(int town_num, StoreSaleType store_num);
void store_examine(PlayerType *player_ptr, StoreSaleType store_num);
int store_check_num(ItemEntity *o_ptr, StoreSaleType store_num);
int input_stock(COMMAND_CODE *com_val, concptr pmt, int min, int max, StoreSaleType store_num);
std::optional<short> input_stock(std::string_view fmt, int min, int max, StoreSaleType store_num);

0 comments on commit b14f441

Please sign in to comment.