OSDN Git Service

[Refactor] #1372 フロア中の最大アイテム数/最大モンスター数をgamevalue.h にコンパイル時定数で移した
[hengbandforosx/hengbandosx.git] / src / main / game-data-initializer.cpp
1 /*!
2  * @file game-data-initializer.cpp
3  * @brief 変愚蛮怒のゲームデータ初期化定義
4  */
5
6 #include "main/game-data-initializer.h"
7 #include "cmd-io/macro-util.h"
8 #include "dungeon/quest.h"
9 #include "floor/floor-util.h"
10 #include "game-option/option-flags.h"
11 #include "game-option/option-types-table.h"
12 #include "monster-race/monster-race.h"
13 #include "system/alloc-entries.h"
14 #include "system/baseitem-info.h"
15 #include "system/dungeon-info.h"
16 #include "system/floor-type-definition.h"
17 #include "system/grid-type-definition.h"
18 #include "system/item-entity.h"
19 #include "system/monster-entity.h"
20 #include "system/monster-race-info.h"
21 #include "system/player-type-definition.h"
22 #include "system/redrawing-flags-updater.h"
23 #include "term/gameterm.h"
24 #include "util/angband-files.h"
25 #include "util/bit-flags-calculator.h"
26 #include "view/display-messages.h"
27 #include <algorithm>
28
29 /*!
30  * @brief マクロ登録の最大数 / Maximum number of macros (see "io.c")
31  * @note Default: assume at most 256 macros are used
32  */
33 constexpr int MACRO_MAX = 256;
34
35 static void init_gf_colors()
36 {
37     for (auto i = 0; i < enum2i(AttributeType::MAX); i++) {
38         gf_colors.emplace(i2enum<AttributeType>(i), "");
39     }
40 }
41
42 /*!
43  * @brief その他の初期情報更新 /
44  * Initialize some other arrays
45  * @return エラーコード
46  */
47 void init_other(PlayerType *player_ptr)
48 {
49     player_ptr->current_floor_ptr = &floor_info; // TODO:本当はこんなところで初期化したくない
50     auto *floor_ptr = player_ptr->current_floor_ptr;
51     floor_ptr->o_list.assign(MAX_FLOOR_ITEMS, {});
52     floor_ptr->m_list.assign(MAX_FLOOR_MONSTERS, {});
53     for (auto &list : floor_ptr->mproc_list) {
54         list.assign(MAX_FLOOR_MONSTERS, {});
55     }
56
57     max_dlv.assign(dungeons_info.size(), {});
58     floor_ptr->grid_array.assign(MAX_HGT, std::vector<Grid>(MAX_WID));
59     init_gf_colors();
60
61     macro_patterns.assign(MACRO_MAX, {});
62     macro_actions.assign(MACRO_MAX, {});
63     macro_buffers.assign(FILE_READ_BUFF_SIZE, {});
64     for (auto i = 0; option_info[i].o_desc; i++) {
65         int os = option_info[i].o_set;
66         int ob = option_info[i].o_bit;
67         if (option_info[i].o_var == nullptr) {
68             continue;
69         }
70
71         g_option_masks[os] |= (1UL << ob);
72         if (option_info[i].o_norm) {
73             set_bits(g_option_flags[os], 1U << ob);
74         } else {
75             reset_bits(g_option_flags[os], 1U << ob);
76         }
77     }
78
79     for (auto &window_mask : g_window_masks) {
80         for (auto i = 0; i < 32; i++) {
81             if (window_flag_desc[i]) {
82                 window_mask.set(i2enum<SubWindowRedrawingFlag>(i));
83             }
84         }
85     }
86
87     g_window_flags[1].clear();
88     g_window_flags[1].set(SubWindowRedrawingFlag::MESSAGE);
89     g_window_flags[2].clear();
90     g_window_flags[2].set(SubWindowRedrawingFlag::INVENTORY);
91 }
92
93 /*!
94  * @brief モンスター生成テーブルを初期化する
95  */
96 void init_monsters_alloc()
97 {
98     std::vector<const MonsterRaceInfo *> elements;
99     for (const auto &[r_idx, r_ref] : monraces_info) {
100         if (MonsterRace(r_ref.idx).is_valid()) {
101             elements.push_back(&r_ref);
102         }
103     }
104
105     std::sort(elements.begin(), elements.end(),
106         [](const MonsterRaceInfo *r1_ptr, const MonsterRaceInfo *r2_ptr) {
107             return r1_ptr->level < r2_ptr->level;
108         });
109
110     alloc_race_table.clear();
111     for (const auto r_ptr : elements) {
112         if (r_ptr->rarity == 0) {
113             continue;
114         }
115
116         const auto index = static_cast<short>(r_ptr->idx);
117         const auto level = r_ptr->level;
118         const auto prob = static_cast<PROB>(100 / r_ptr->rarity);
119         alloc_race_table.push_back({ index, level, prob, prob });
120     }
121 }
122
123 /*!
124  * @brief アイテム生成テーブルを初期化する
125  */
126 void init_items_alloc()
127 {
128     short num[MAX_DEPTH]{};
129     auto alloc_kind_size = 0;
130     for (const auto &baseitem : baseitems_info) {
131         for (const auto &[level, chance] : baseitem.alloc_tables) {
132             if (chance != 0) {
133                 alloc_kind_size++;
134                 num[level]++;
135             }
136         }
137     }
138
139     for (auto i = 1; i < MAX_DEPTH; i++) {
140         num[i] += num[i - 1];
141     }
142
143     if (num[0] == 0) {
144         quit(_("町のアイテムがない!", "No town objects!"));
145     }
146
147     alloc_kind_table.assign(alloc_kind_size, {});
148     short aux[MAX_DEPTH]{};
149     for (const auto &baseitem : baseitems_info) {
150         for (const auto &[level, chance] : baseitem.alloc_tables) {
151             if (chance == 0) {
152                 continue;
153             }
154
155             const auto x = level;
156             const short p = 100 / chance;
157             const auto y = (x > 0) ? num[x - 1] : 0;
158             const auto z = y + aux[x];
159             alloc_kind_table[z].index = baseitem.idx;
160             alloc_kind_table[z].level = x;
161             alloc_kind_table[z].prob1 = p;
162             alloc_kind_table[z].prob2 = p;
163             aux[x]++;
164         }
165     }
166 }