From 705000c5a3a24a1bb4d7da08bd991c1cc1690ffe Mon Sep 17 00:00:00 2001 From: Hourier <66951241+Hourier@users.noreply.github.com> Date: Sat, 15 Apr 2023 23:40:55 +0900 Subject: [PATCH] =?utf8?q?[Refactor]=20#2476=20misc.txt=20=E3=81=8B?= =?utf8?q?=E3=82=89=E7=94=BA=E3=81=AE=E6=95=B0=E3=82=92=E5=BB=83=E6=AD=A2?= =?utf8?q?=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- lib/edit/misc.txt | 3 --- src/birth/character-builder.cpp | 2 +- src/core/turn-compensator.cpp | 2 +- src/floor/fixed-map-generator.cpp | 4 +--- src/floor/floor-town.cpp | 5 +---- src/floor/floor-town.h | 1 - src/load/quest-loader.cpp | 2 +- src/main-win/main-win-music.cpp | 2 +- src/market/building-initializer.cpp | 32 ++++++++++++++++++++++++++++++-- src/player/process-death.cpp | 4 ++-- src/save/save.cpp | 6 +++--- src/spell-kind/spells-world.cpp | 4 ++-- src/store/store.cpp | 12 ++++++------ 13 files changed, 49 insertions(+), 30 deletions(-) diff --git a/lib/edit/misc.txt b/lib/edit/misc.txt index 2afbbbc7f..5ba39f663 100644 --- a/lib/edit/misc.txt +++ b/lib/edit/misc.txt @@ -1,8 +1,5 @@ # File: misc.txt -# Maximum number of towns -M:T:7 - # Maximum x size of the wilderness M:WX:99 diff --git a/src/birth/character-builder.cpp b/src/birth/character-builder.cpp index 28b6e9bc4..9d98db63e 100644 --- a/src/birth/character-builder.cpp +++ b/src/birth/character-builder.cpp @@ -108,7 +108,7 @@ void player_birth(PlayerType *player_ptr) } write_birth_diary(player_ptr); - for (int i = 1; i < max_towns; i++) { + for (int i = 1; i < town_info.size(); i++) { for (auto sst : STORE_SALE_TYPE_LIST) { store_init(i, sst); } diff --git a/src/core/turn-compensator.cpp b/src/core/turn-compensator.cpp index b508e6f4c..dfa383800 100644 --- a/src/core/turn-compensator.cpp +++ b/src/core/turn-compensator.cpp @@ -64,7 +64,7 @@ void prevent_turn_overflow(PlayerType *player_ptr) player_ptr->feeling_turn = 1; } - for (int i = 1; i < max_towns; i++) { + for (int i = 1; i < town_info.size(); i++) { for (int j = 0; j < MAX_STORES; j++) { store_type *store_ptr = &town_info[i].store[j]; diff --git a/src/floor/fixed-map-generator.cpp b/src/floor/fixed-map-generator.cpp index ab96dd128..d19f49c7e 100644 --- a/src/floor/fixed-map-generator.cpp +++ b/src/floor/fixed-map-generator.cpp @@ -399,9 +399,7 @@ static bool parse_qtw_M(qtwg_type *qtwg_ptr, char **zz) return true; } - if (zz[0][0] == 'T') { - max_towns = static_cast(atoi(zz[1])); - } else if (zz[0][0] == 'O') { + if (zz[0][0] == 'O') { w_ptr->max_o_idx = (OBJECT_IDX)atoi(zz[1]); } else if (zz[0][0] == 'M') { w_ptr->max_m_idx = (MONSTER_IDX)atoi(zz[1]); diff --git a/src/floor/floor-town.cpp b/src/floor/floor-town.cpp index 023920ec2..d4f72915e 100644 --- a/src/floor/floor-town.cpp +++ b/src/floor/floor-town.cpp @@ -1,8 +1,5 @@ #include "floor/floor-town.h" #include "system/item-entity.h" -/* Maximum number of towns */ -int16_t max_towns; - -/* The towns [max_towns] */ +/* The towns [town_info.size()] */ std::vector town_info; diff --git a/src/floor/floor-town.h b/src/floor/floor-town.h index b3ad0dca6..a148965b9 100644 --- a/src/floor/floor-town.h +++ b/src/floor/floor-town.h @@ -15,7 +15,6 @@ struct town_type { byte numstores; }; -extern int16_t max_towns; constexpr short VALID_TOWNS = 6; // @details 旧海底都市クエストのマップを除外する. 有効な町に差し替え完了したら不要になるので注意. constexpr auto SECRET_TOWN = 5; // @details ズルの町番号. diff --git a/src/load/quest-loader.cpp b/src/load/quest-loader.cpp index 2cd256cb6..fca539f37 100644 --- a/src/load/quest-loader.cpp +++ b/src/load/quest-loader.cpp @@ -20,7 +20,7 @@ errr load_town(void) { auto max_towns_load = rd_u16b(); - if (max_towns_load <= max_towns) { + if (max_towns_load <= town_info.size()) { return 0; } diff --git a/src/main-win/main-win-music.cpp b/src/main-win/main-win-music.cpp index ac1dcd9a7..b5e4dbe72 100644 --- a/src/main-win/main-win-music.cpp +++ b/src/main-win/main-win-music.cpp @@ -100,7 +100,7 @@ static concptr quest_key_at(int index, char *buf) */ static concptr town_key_at(int index, char *buf) { - if (index >= static_cast(max_towns)) { + if (index >= static_cast(town_info.size())) { return nullptr; } diff --git a/src/market/building-initializer.cpp b/src/market/building-initializer.cpp index 1e8cc161a..169dde0ea 100644 --- a/src/market/building-initializer.cpp +++ b/src/market/building-initializer.cpp @@ -1,5 +1,6 @@ #include "market/building-initializer.h" #include "floor/floor-town.h" +#include "io/files-util.h" #include "object/object-kind-hook.h" #include "player-info/class-types.h" #include "store/articles-on-sale.h" @@ -10,17 +11,44 @@ #include "system/baseitem-info.h" #include "system/building-type-definition.h" #include "system/item-entity.h" +#include "util/angband-files.h" +#include +#include #include /*! + * @brief ユニークな町の数を数える + * @return ユニークな町の数 + * @details 町定義ファイル名の先頭2文字は番号であることを利用してカウントする. + * 辺境の地を表すファイルは(01_*.txt) は3つあるのでユニークではない. また町番号は1から始まるので最後に加算する. + */ +static int count_town_numbers() +{ + char towns_path[1024]{}; + path_build(towns_path, sizeof(towns_path), ANGBAND_DIR_EDIT, "towns"); + std::set unique_towns; + for (const auto &entry : std::filesystem::directory_iterator(towns_path)) { + const auto &filename = entry.path().filename().string(); + if (!filename.ends_with(".txt")) { + continue; + } + + unique_towns.insert(filename.substr(0, 2)); + } + + return unique_towns.size() + 1; +} + +/*! * @brief 町情報読み込みのメインルーチン / * Initialize town array * @details 「我が家を拡張する」オプションのON/OFFとは無関係に、ON時の容量を確保しておく. */ void init_towns(void) { - town_info = std::vector(max_towns); - for (auto i = 1; i < max_towns; i++) { + const auto town_numbers = count_town_numbers(); + town_info = std::vector(town_numbers); + for (auto i = 1; i < town_numbers; i++) { town_info[i].store = std::vector(MAX_STORES); for (auto sst : STORE_SALE_TYPE_LIST) { auto *store_ptr = &town_info[i].store[enum2i(sst)]; diff --git a/src/player/process-death.cpp b/src/player/process-death.cpp index ca7a7e4a2..ce426428d 100644 --- a/src/player/process-death.cpp +++ b/src/player/process-death.cpp @@ -258,7 +258,7 @@ static void home_aware(PlayerType *player_ptr) { ItemEntity *o_ptr; store_type *store_ptr; - for (int i = 1; i < max_towns; i++) { + for (int i = 1; i < town_info.size(); i++) { store_ptr = &town_info[i].store[enum2i(StoreSaleType::HOME)]; for (int j = 0; j < store_ptr->stock_num; j++) { o_ptr = &store_ptr->stock[j]; @@ -307,7 +307,7 @@ static bool show_dead_player_items(PlayerType *player_ptr) */ static void show_dead_home_items(PlayerType *player_ptr) { - for (int l = 1; l < max_towns; l++) { + for (int l = 1; l < town_info.size(); l++) { store_type *store_ptr; store_ptr = &town_info[l].store[enum2i(StoreSaleType::HOME)]; if (store_ptr->stock_num == 0) { diff --git a/src/save/save.cpp b/src/save/save.cpp index 38ab523ac..fc7bfafe7 100644 --- a/src/save/save.cpp +++ b/src/save/save.cpp @@ -123,7 +123,7 @@ static bool wr_savefile_new(PlayerType *player_ptr, SaveType type) wr_perception(bi_id); } - tmp16u = max_towns; + tmp16u = town_info.size(); wr_u16b(tmp16u); const auto &quest_list = QuestList::get_instance(); @@ -214,12 +214,12 @@ static bool wr_savefile_new(PlayerType *player_ptr, SaveType type) } wr_u16b(0xFFFF); - tmp16u = max_towns; + tmp16u = town_info.size(); wr_u16b(tmp16u); tmp16u = MAX_STORES; wr_u16b(tmp16u); - for (int i = 1; i < max_towns; i++) { + for (int i = 1; i < town_info.size(); i++) { for (int j = 0; j < MAX_STORES; j++) { wr_store(&town_info[i].store[j]); } diff --git a/src/spell-kind/spells-world.cpp b/src/spell-kind/spells-world.cpp index 784874173..9cd868f3d 100644 --- a/src/spell-kind/spells-world.cpp +++ b/src/spell-kind/spells-world.cpp @@ -288,7 +288,7 @@ bool tele_town(PlayerType *player_ptr) int i; int num = 0; - for (i = 1; i < max_towns; i++) { + for (i = 1; i < town_info.size(); i++) { char buf[80]; if ((i == VALID_TOWNS) || (i == SECRET_TOWN) || (i == player_ptr->town_num) || !(player_ptr->visit & (1UL << (i - 1)))) { @@ -316,7 +316,7 @@ bool tele_town(PlayerType *player_ptr) return false; } - else if ((i < 'a') || (i > ('a' + max_towns - 2))) { + else if ((i < 'a') || (i > ('a' + town_info.size() - 2))) { continue; } else if (((i - 'a' + 1) == player_ptr->town_num) || ((i - 'a' + 1) == VALID_TOWNS) || ((i - 'a' + 1) == SECRET_TOWN) || !(player_ptr->visit & (1UL << (i - 'a')))) { continue; diff --git a/src/store/store.cpp b/src/store/store.cpp index d3f1dd4b4..8b3d2807b 100644 --- a/src/store/store.cpp +++ b/src/store/store.cpp @@ -253,7 +253,7 @@ void store_examine(PlayerType *player_ptr, StoreSaleType store_num) void store_shuffle(PlayerType *player_ptr, StoreSaleType store_num) { auto owner_num = owners.at(store_num).size(); - if ((store_num == StoreSaleType::HOME) || (store_num == StoreSaleType::MUSEUM) || (owner_num <= (uint16_t)max_towns)) { + if ((store_num == StoreSaleType::HOME) || (store_num == StoreSaleType::MUSEUM) || (owner_num <= (uint16_t)town_info.size())) { return; } @@ -267,7 +267,7 @@ void store_shuffle(PlayerType *player_ptr, StoreSaleType store_num) } int i; - for (i = 1; i < max_towns; i++) { + for (i = 1; i < town_info.size(); i++) { if (i == player_ptr->town_num) { continue; } @@ -277,7 +277,7 @@ void store_shuffle(PlayerType *player_ptr, StoreSaleType store_num) } } - if (i == max_towns) { + if (i == town_info.size()) { break; } } @@ -479,13 +479,13 @@ void store_init(int town_num, StoreSaleType store_num) while (true) { st_ptr->owner = (byte)randint0(owner_num); - if (owner_num <= (uint16_t)max_towns) { + if (owner_num <= (uint16_t)town_info.size()) { break; } int i; - for (i = 1; i < (uint16_t)max_towns; i++) { + for (i = 1; i < (uint16_t)town_info.size(); i++) { if (i == town_num) { continue; } @@ -494,7 +494,7 @@ void store_init(int town_num, StoreSaleType store_num) } } - if (i == max_towns) { + if (i == town_info.size()) { break; } } -- 2.11.0