From 0d0ea93ca557668319c26c23c0fa5b66ddbe25f5 Mon Sep 17 00:00:00 2001 From: Hourier Date: Thu, 2 Jul 2020 21:54:59 +0900 Subject: [PATCH] [Refactor] #40521 Separated object-scanner.c/h from player-inventory.c/h --- Hengband/Hengband/Hengband.vcxproj | 2 + Hengband/Hengband/Hengband.vcxproj.filters | 6 ++ src/Makefile.am | 1 + src/floor/object-scanner.c | 166 +++++++++++++++++++++++++++++ src/floor/object-scanner.h | 7 ++ src/inventory/floor-item-getter.c | 9 +- src/inventory/player-inventory.c | 157 +-------------------------- src/inventory/player-inventory.h | 2 - src/io/targeting.c | 6 +- 9 files changed, 192 insertions(+), 164 deletions(-) create mode 100644 src/floor/object-scanner.c create mode 100644 src/floor/object-scanner.h diff --git a/Hengband/Hengband/Hengband.vcxproj b/Hengband/Hengband/Hengband.vcxproj index e26008ea4..32b3250ac 100644 --- a/Hengband/Hengband/Hengband.vcxproj +++ b/Hengband/Hengband/Hengband.vcxproj @@ -178,6 +178,7 @@ + @@ -610,6 +611,7 @@ + diff --git a/Hengband/Hengband/Hengband.vcxproj.filters b/Hengband/Hengband/Hengband.vcxproj.filters index d6c15cc77..4a626cdd4 100644 --- a/Hengband/Hengband/Hengband.vcxproj.filters +++ b/Hengband/Hengband/Hengband.vcxproj.filters @@ -1556,6 +1556,9 @@ inventory + + floor + @@ -3403,6 +3406,9 @@ inventory + + floor + diff --git a/src/Makefile.am b/src/Makefile.am index e9e9917f0..abaf3e61f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -160,6 +160,7 @@ hengband_SOURCES = \ floor/floor-town.h floor/floor-town.c \ floor/floor-util.c floor/floor-util.h \ floor/geometry.c floor/geometry.h \ + floor/object-scanner.c floor/object-scanner.h \ floor/pattern-walk.c floor/pattern-walk.h \ floor/wild.h floor/wild.c \ \ diff --git a/src/floor/object-scanner.c b/src/floor/object-scanner.c new file mode 100644 index 000000000..161a3c83f --- /dev/null +++ b/src/floor/object-scanner.c @@ -0,0 +1,166 @@ +#include "floor/object-scanner.h" +#include "floor/floor.h" +#include "game-option/text-display-options.h" +#include "grid/grid.h" +#include "inventory/inventory-util.h" +#include "io/input-key-requester.h" +#include "object/item-tester-hooker.h" +#include "object/object-flavor.h" +#include "object/object-mark-types.h" +#include "system/object-type-definition.h" +#include "term/gameterm.h" +#include "term/screen-processor.h" + +/*! + * @brief 床に落ちているオブジェクトの数を返す / scan floor items + * @param items オブジェクトのIDリストを返すための配列参照ポインタ + * @param y 走査するフロアのY座標 + * @param x 走査するフロアのX座標 + * @param mode オプションフラグ + * @return 対象のマスに落ちているアイテム数 + * @details + * Return a list of o_list[] indexes of items at the given floor + * location. Valid flags are: + * + * mode & 0x01 -- Item tester + * mode & 0x02 -- Marked items only + * mode & 0x04 -- Stop after first + */ +ITEM_NUMBER scan_floor_items(player_type *owner_ptr, OBJECT_IDX *items, POSITION y, POSITION x, BIT_FLAGS mode, tval_type item_tester_tval) +{ + floor_type *floor_ptr = owner_ptr->current_floor_ptr; + if (!in_bounds(floor_ptr, y, x)) + return 0; + + OBJECT_IDX this_o_idx, next_o_idx; + ITEM_NUMBER num = 0; + for (this_o_idx = floor_ptr->grid_array[y][x].o_idx; this_o_idx; this_o_idx = next_o_idx) { + object_type *o_ptr; + o_ptr = &floor_ptr->o_list[this_o_idx]; + next_o_idx = o_ptr->next_o_idx; + if ((mode & 0x01) && !item_tester_okay(owner_ptr, o_ptr, item_tester_tval)) + continue; + + if ((mode & 0x02) && !(o_ptr->marked & OM_FOUND)) + continue; + + if (num < 23) + items[num] = this_o_idx; + + num++; + if (mode & 0x04) + break; + } + + return num; +} + +/*! + * @brief タグIDにあわせてタグアルファベットのリストを返す(床上アイテム用) / + * Move around label characters with correspond tags (floor version) + * @param label ラベルリストを取得する文字列参照ポインタ + * @param floor_list 床上アイテムの配列 + * @param floor_num 床上アイテムの配列ID + * @return なし + */ +/* + */ +static void prepare_label_string_floor(floor_type *floor_ptr, char *label, FLOOR_IDX floor_list[], ITEM_NUMBER floor_num) +{ + concptr alphabet_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; + strcpy(label, alphabet_chars); + for (int i = 0; i < 52; i++) { + COMMAND_CODE index; + SYMBOL_CODE c = alphabet_chars[i]; + if (!get_tag_floor(floor_ptr, &index, c, floor_list, floor_num)) + continue; + + if (label[i] == c) + label[i] = ' '; + + label[index] = c; + } +} + +/*! + * @brief 床下に落ちているアイテムの一覧を返す / Display a list of the items on the floor at the given location. + * @param target_item カーソルの初期値 + * @param y 走査するフロアのY座標 + * @param x 走査するフロアのX座標 + * @param min_width 表示の長さ + * @return 選択したアイテムの添え字 + * @details + */ +COMMAND_CODE show_floor_items(player_type *owner_ptr, int target_item, POSITION y, POSITION x, TERM_LEN *min_width, tval_type item_tester_tval) +{ + COMMAND_CODE i, m; + int j, k, l; + object_type *o_ptr; + GAME_TEXT o_name[MAX_NLEN]; + char tmp_val[80]; + COMMAND_CODE out_index[23]; + TERM_COLOR out_color[23]; + char out_desc[23][MAX_NLEN]; + COMMAND_CODE target_item_label = 0; + OBJECT_IDX floor_list[23]; + ITEM_NUMBER floor_num; + TERM_LEN wid, hgt; + char floor_label[52 + 1]; + bool dont_need_to_show_weights = TRUE; + Term_get_size(&wid, &hgt); + int len = MAX((*min_width), 20); + floor_num = scan_floor_items(owner_ptr, floor_list, y, x, 0x03, item_tester_tval); + floor_type *floor_ptr = owner_ptr->current_floor_ptr; + for (k = 0, i = 0; i < floor_num && i < 23; i++) { + o_ptr = &floor_ptr->o_list[floor_list[i]]; + object_desc(owner_ptr, o_name, o_ptr, 0); + out_index[k] = i; + out_color[k] = tval_to_attr[o_ptr->tval & 0x7F]; + strcpy(out_desc[k], o_name); + l = strlen(out_desc[k]) + 5; + if (show_weights) + l += 9; + + if (o_ptr->tval != TV_GOLD) + dont_need_to_show_weights = FALSE; + + if (l > len) + len = l; + + k++; + } + + if (show_weights && dont_need_to_show_weights) + len -= 9; + + *min_width = len; + int col = (len > wid - 4) ? 0 : (wid - len - 1); + prepare_label_string_floor(floor_ptr, floor_label, floor_list, floor_num); + for (j = 0; j < k; j++) { + m = floor_list[out_index[j]]; + o_ptr = &floor_ptr->o_list[m]; + prt("", j + 1, col ? col - 2 : col); + if (use_menu && target_item) { + if (j == (target_item - 1)) { + strcpy(tmp_val, _("》", "> ")); + target_item_label = m; + } else + strcpy(tmp_val, " "); + } else { + sprintf(tmp_val, "%c)", floor_label[j]); + } + + put_str(tmp_val, j + 1, col); + c_put_str(out_color[j], out_desc[j], j + 1, col + 3); + if (show_weights && (o_ptr->tval != TV_GOLD)) { + int wgt = o_ptr->weight * o_ptr->number; + sprintf(tmp_val, _("%3d.%1d kg", "%3d.%1d lb"), _(lbtokg1(wgt), wgt / 10), _(lbtokg2(wgt), wgt % 10)); + prt(tmp_val, j + 1, wid - 9); + } + } + + if (j && (j < 23)) + prt("", j + 1, col ? col - 2 : col); + + return target_item_label; +} diff --git a/src/floor/object-scanner.h b/src/floor/object-scanner.h new file mode 100644 index 000000000..eafc0f377 --- /dev/null +++ b/src/floor/object-scanner.h @@ -0,0 +1,7 @@ +#pragma once + +#include "object/tval-types.h" +#include "system/angband.h" + +ITEM_NUMBER scan_floor_items(player_type *owner_ptr, OBJECT_IDX *items, POSITION y, POSITION x, BIT_FLAGS mode, tval_type item_tester_tval); +COMMAND_CODE show_floor_items(player_type *owner_ptr, int target_item, POSITION y, POSITION x, TERM_LEN *min_width, tval_type item_tester_tval); diff --git a/src/inventory/floor-item-getter.c b/src/inventory/floor-item-getter.c index 608918e56..ebbceb3d8 100644 --- a/src/inventory/floor-item-getter.c +++ b/src/inventory/floor-item-getter.c @@ -8,6 +8,7 @@ #include "core/stuff-handler.h" #include "floor/floor-object.h" #include "floor/floor.h" +#include "floor/object-scanner.h" #include "game-option/input-options.h" #include "game-option/option-flags.h" #include "game-option/text-display-options.h" @@ -73,7 +74,7 @@ bool get_item_floor(player_type *owner_ptr, COMMAND_CODE *cp, concptr pmt, concp return TRUE; } else if (floor && (*cp < 0)) { if (prev_tag && command_cmd) { - floor_num = scan_floor(owner_ptr, floor_list, owner_ptr->y, owner_ptr->x, 0x03, tval); + floor_num = scan_floor_items(owner_ptr, floor_list, owner_ptr->y, owner_ptr->x, 0x03, tval); if (get_tag_floor(owner_ptr->current_floor_ptr, &k, prev_tag, floor_list, floor_num)) { (*cp) = 0 - floor_list[k]; tval = 0; @@ -160,7 +161,7 @@ bool get_item_floor(player_type *owner_ptr, COMMAND_CODE *cp, concptr pmt, concp floor_num = 0; if (floor) - floor_num = scan_floor(owner_ptr, floor_list, owner_ptr->y, owner_ptr->x, 0x03, tval); + floor_num = scan_floor_items(owner_ptr, floor_list, owner_ptr->y, owner_ptr->x, 0x03, tval); if (i1 <= i2) allow_inven = TRUE; @@ -238,7 +239,7 @@ bool get_item_floor(player_type *owner_ptr, COMMAND_CODE *cp, concptr pmt, concp n1 = I2A(j - floor_top); n2 = I2A(k - floor_top); if (command_see) - get_item_label = show_floor(owner_ptr, menu_line, owner_ptr->y, owner_ptr->x, &min_width, tval); + get_item_label = show_floor_items(owner_ptr, menu_line, owner_ptr->y, owner_ptr->x, &min_width, tval); } if (command_wrk == (USE_INVEN)) { @@ -540,7 +541,7 @@ bool get_item_floor(player_type *owner_ptr, COMMAND_CODE *cp, concptr pmt, concp i = owner_ptr->current_floor_ptr->o_list[i].next_o_idx; owner_ptr->current_floor_ptr->o_list[i].next_o_idx = o_idx; - floor_num = scan_floor(owner_ptr, floor_list, owner_ptr->y, owner_ptr->x, 0x03, tval); + floor_num = scan_floor_items(owner_ptr, floor_list, owner_ptr->y, owner_ptr->x, 0x03, tval); if (command_see) { screen_load(); screen_save(); diff --git a/src/inventory/player-inventory.c b/src/inventory/player-inventory.c index 5a740ace5..946b0b5b6 100644 --- a/src/inventory/player-inventory.c +++ b/src/inventory/player-inventory.c @@ -1,6 +1,7 @@ #include "inventory/player-inventory.h" #include "core/asking-player.h" #include "floor/floor-object.h" +#include "floor/object-scanner.h" #include "game-option/birth-options.h" #include "game-option/input-options.h" #include "game-option/special-options.h" @@ -105,38 +106,11 @@ bool can_get_item(player_type *owner_ptr, tval_type tval) return TRUE; OBJECT_IDX floor_list[23]; - ITEM_NUMBER floor_num = scan_floor(owner_ptr, floor_list, owner_ptr->y, owner_ptr->x, 0x03, tval); + ITEM_NUMBER floor_num = scan_floor_items(owner_ptr, floor_list, owner_ptr->y, owner_ptr->x, 0x03, tval); return floor_num != 0; } /*! - * @brief タグIDにあわせてタグアルファベットのリストを返す(床上アイテム用) / - * Move around label characters with correspond tags (floor version) - * @param label ラベルリストを取得する文字列参照ポインタ - * @param floor_list 床上アイテムの配列 - * @param floor_num 床上アイテムの配列ID - * @return なし - */ -/* - */ -static void prepare_label_string_floor(floor_type *floor_ptr, char *label, FLOOR_IDX floor_list[], ITEM_NUMBER floor_num) -{ - concptr alphabet_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; - strcpy(label, alphabet_chars); - for (int i = 0; i < 52; i++) { - COMMAND_CODE index; - SYMBOL_CODE c = alphabet_chars[i]; - if (!get_tag_floor(floor_ptr, &index, c, floor_list, floor_num)) - continue; - - if (label[i] == c) - label[i] = ' '; - - label[index] = c; - } -} - -/*! * @brief 所持アイテムの表示を行う / * Display the inventory. * @param target_item アイテムの選択処理を行うか否か。 @@ -244,133 +218,6 @@ COMMAND_CODE show_inventory(player_type *owner_ptr, int target_item, BIT_FLAGS m } /*! - * @brief 床下に落ちているオブジェクトの数を返す / scan_floor - * @param items オブジェクトのIDリストを返すための配列参照ポインタ - * @param y 走査するフロアのY座標 - * @param x 走査するフロアのX座標 - * @param mode オプションフラグ - * @return 対象のマスに落ちているアイテム数 - * @details - * Return a list of o_list[] indexes of items at the given floor - * location. Valid flags are: - * - * mode & 0x01 -- Item tester - * mode & 0x02 -- Marked items only - * mode & 0x04 -- Stop after first - */ -ITEM_NUMBER scan_floor(player_type *owner_ptr, OBJECT_IDX *items, POSITION y, POSITION x, BIT_FLAGS mode, tval_type item_tester_tval) -{ - floor_type *floor_ptr = owner_ptr->current_floor_ptr; - if (!in_bounds(floor_ptr, y, x)) - return 0; - - OBJECT_IDX this_o_idx, next_o_idx; - ITEM_NUMBER num = 0; - for (this_o_idx = floor_ptr->grid_array[y][x].o_idx; this_o_idx; this_o_idx = next_o_idx) { - object_type *o_ptr; - o_ptr = &floor_ptr->o_list[this_o_idx]; - next_o_idx = o_ptr->next_o_idx; - if ((mode & 0x01) && !item_tester_okay(owner_ptr, o_ptr, item_tester_tval)) - continue; - - if ((mode & 0x02) && !(o_ptr->marked & OM_FOUND)) - continue; - - if (num < 23) - items[num] = this_o_idx; - - num++; - if (mode & 0x04) - break; - } - - return num; -} - -/*! - * @brief 床下に落ちているアイテムの一覧を返す / Display a list of the items on the floor at the given location. - * @param target_item カーソルの初期値 - * @param y 走査するフロアのY座標 - * @param x 走査するフロアのX座標 - * @param min_width 表示の長さ - * @return 選択したアイテムの添え字 - * @details - */ -COMMAND_CODE show_floor(player_type *owner_ptr, int target_item, POSITION y, POSITION x, TERM_LEN *min_width, tval_type item_tester_tval) -{ - COMMAND_CODE i, m; - int j, k, l; - object_type *o_ptr; - GAME_TEXT o_name[MAX_NLEN]; - char tmp_val[80]; - COMMAND_CODE out_index[23]; - TERM_COLOR out_color[23]; - char out_desc[23][MAX_NLEN]; - COMMAND_CODE target_item_label = 0; - OBJECT_IDX floor_list[23]; - ITEM_NUMBER floor_num; - TERM_LEN wid, hgt; - char floor_label[52 + 1]; - bool dont_need_to_show_weights = TRUE; - Term_get_size(&wid, &hgt); - int len = MAX((*min_width), 20); - floor_num = scan_floor(owner_ptr, floor_list, y, x, 0x03, item_tester_tval); - floor_type *floor_ptr = owner_ptr->current_floor_ptr; - for (k = 0, i = 0; i < floor_num && i < 23; i++) { - o_ptr = &floor_ptr->o_list[floor_list[i]]; - object_desc(owner_ptr, o_name, o_ptr, 0); - out_index[k] = i; - out_color[k] = tval_to_attr[o_ptr->tval & 0x7F]; - strcpy(out_desc[k], o_name); - l = strlen(out_desc[k]) + 5; - if (show_weights) - l += 9; - - if (o_ptr->tval != TV_GOLD) - dont_need_to_show_weights = FALSE; - - if (l > len) - len = l; - - k++; - } - - if (show_weights && dont_need_to_show_weights) - len -= 9; - - *min_width = len; - int col = (len > wid - 4) ? 0 : (wid - len - 1); - prepare_label_string_floor(floor_ptr, floor_label, floor_list, floor_num); - for (j = 0; j < k; j++) { - m = floor_list[out_index[j]]; - o_ptr = &floor_ptr->o_list[m]; - prt("", j + 1, col ? col - 2 : col); - if (use_menu && target_item) { - if (j == (target_item - 1)) { - strcpy(tmp_val, _("》", "> ")); - target_item_label = m; - } else - strcpy(tmp_val, " "); - } else { - sprintf(tmp_val, "%c)", floor_label[j]); - } - - put_str(tmp_val, j + 1, col); - c_put_str(out_color[j], out_desc[j], j + 1, col + 3); - if (show_weights && (o_ptr->tval != TV_GOLD)) { - int wgt = o_ptr->weight * o_ptr->number; - sprintf(tmp_val, _("%3d.%1d kg", "%3d.%1d lb"), _(lbtokg1(wgt), wgt / 10), _(lbtokg2(wgt), wgt % 10)); - prt(tmp_val, j + 1, wid - 9); - } - } - - if (j && (j < 23)) - prt("", j + 1, col ? col - 2 : col); - - return target_item_label; -} - -/*! * @brief 床上のアイテムを拾う選択用サブルーチン * @return プレイヤーによりアイテムが選択されたならTRUEを返す。 */ diff --git a/src/inventory/player-inventory.h b/src/inventory/player-inventory.h index 122e498cb..a1fc354d8 100644 --- a/src/inventory/player-inventory.h +++ b/src/inventory/player-inventory.h @@ -10,6 +10,4 @@ void display_inventory(player_type *creature_ptr, tval_type tval); COMMAND_CODE show_inventory(player_type *owner_ptr, int target_item, BIT_FLAGS mode, tval_type tval); COMMAND_CODE show_equipment(player_type *owner_ptr, int target_item, BIT_FLAGS mode, tval_type tval); bool can_get_item(player_type *owner_ptr, tval_type tval); -ITEM_NUMBER scan_floor(player_type *owner_ptr, OBJECT_IDX *items, POSITION y, POSITION x, BIT_FLAGS mode, tval_type item_tester_tval); -COMMAND_CODE show_floor(player_type *owner_ptr, int target_item, POSITION y, POSITION x, TERM_LEN *min_width, tval_type item_tester_tval); void py_pickup_floor(player_type *creature_ptr, bool pickup); diff --git a/src/io/targeting.c b/src/io/targeting.c index 7ab84837e..caa52ec28 100644 --- a/src/io/targeting.c +++ b/src/io/targeting.c @@ -23,6 +23,7 @@ #include "floor/floor-object.h" #include "floor/floor-town.h" #include "floor/floor.h" +#include "floor/object-scanner.h" #include "game-option/cheat-options.h" #include "game-option/disturbance-options.h" #include "game-option/game-play-options.h" @@ -31,7 +32,6 @@ #include "game-option/map-screen-options.h" #include "grid/feature.h" #include "grid/grid.h" -#include "inventory/player-inventory.h" #include "io/command-repeater.h" #include "io/cursor.h" #include "io/input-key-acceptor.h" @@ -556,7 +556,7 @@ static char target_set_aux(player_type *subject_ptr, POSITION y, POSITION x, BIT /* Scan all objects in the grid */ if (easy_floor) { - floor_num = scan_floor(subject_ptr, floor_list, y, x, 0x02, 0); + floor_num = scan_floor_items(subject_ptr, floor_list, y, x, 0x02, 0); if (floor_num) { @@ -795,7 +795,7 @@ static char target_set_aux(player_type *subject_ptr, POSITION y, POSITION x, BIT /* Display */ show_gold_on_floor = TRUE; - (void)show_floor(subject_ptr, 0, y, x, &min_width, 0); + (void)show_floor_items(subject_ptr, 0, y, x, &min_width, 0); show_gold_on_floor = FALSE; /* Prompt */ -- 2.11.0