OSDN Git Service

Merge pull request #3569 from sikabane-works/release/3.0.0.88-alpha
[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 "world/world.h"
28 #include <algorithm>
29
30 /*!
31  * @brief マクロ登録の最大数 / Maximum number of macros (see "io.c")
32  * @note Default: assume at most 256 macros are used
33  */
34 constexpr int MACRO_MAX = 256;
35
36 static void init_gf_colors()
37 {
38     for (auto i = 0; i < enum2i(AttributeType::MAX); i++) {
39         gf_colors.emplace(i2enum<AttributeType>(i), "");
40     }
41 }
42
43 /*!
44  * @brief その他の初期情報更新 /
45  * Initialize some other arrays
46  * @return エラーコード
47  */
48 void init_other(PlayerType *player_ptr)
49 {
50     player_ptr->current_floor_ptr = &floor_info; // TODO:本当はこんなところで初期化したくない
51     auto *floor_ptr = player_ptr->current_floor_ptr;
52     floor_ptr->o_list.assign(w_ptr->max_o_idx, {});
53     floor_ptr->m_list.assign(w_ptr->max_m_idx, {});
54     for (auto &list : floor_ptr->mproc_list) {
55         list.assign(w_ptr->max_m_idx, {});
56     }
57
58     max_dlv.assign(dungeons_info.size(), {});
59     floor_ptr->grid_array.assign(MAX_HGT, std::vector<grid_type>(MAX_WID));
60     init_gf_colors();
61
62     macro_patterns.assign(MACRO_MAX, {});
63     macro_actions.assign(MACRO_MAX, {});
64     macro_buffers.assign(FILE_READ_BUFF_SIZE, {});
65     for (auto i = 0; option_info[i].o_desc; i++) {
66         int os = option_info[i].o_set;
67         int ob = option_info[i].o_bit;
68         if (option_info[i].o_var == nullptr) {
69             continue;
70         }
71
72         g_option_masks[os] |= (1UL << ob);
73         if (option_info[i].o_norm) {
74             set_bits(g_option_flags[os], 1U << ob);
75         } else {
76             reset_bits(g_option_flags[os], 1U << ob);
77         }
78     }
79
80     for (auto &window_mask : g_window_masks) {
81         for (auto i = 0; i < 32; i++) {
82             if (window_flag_desc[i]) {
83                 window_mask.set(i2enum<SubWindowRedrawingFlag>(i));
84             }
85         }
86     }
87
88     g_window_flags[1].clear();
89     g_window_flags[1].set(SubWindowRedrawingFlag::MESSAGE);
90     g_window_flags[2].clear();
91     g_window_flags[2].set(SubWindowRedrawingFlag::INVENTORY);
92 }
93
94 /*!
95  * @brief モンスター生成テーブルを初期化する
96  */
97 void init_monsters_alloc()
98 {
99     std::vector<const MonsterRaceInfo *> elements;
100     for (const auto &[r_idx, r_ref] : monraces_info) {
101         if (MonsterRace(r_ref.idx).is_valid()) {
102             elements.push_back(&r_ref);
103         }
104     }
105
106     std::sort(elements.begin(), elements.end(),
107         [](const MonsterRaceInfo *r1_ptr, const MonsterRaceInfo *r2_ptr) {
108             return r1_ptr->level < r2_ptr->level;
109         });
110
111     alloc_race_table.clear();
112     for (const auto r_ptr : elements) {
113         if (r_ptr->rarity == 0) {
114             continue;
115         }
116
117         const auto index = static_cast<short>(r_ptr->idx);
118         const auto level = r_ptr->level;
119         const auto prob = static_cast<PROB>(100 / r_ptr->rarity);
120         alloc_race_table.push_back({ index, level, prob, prob });
121     }
122 }
123
124 /*!
125  * @brief アイテム生成テーブルを初期化する
126  */
127 void init_items_alloc()
128 {
129     short num[MAX_DEPTH]{};
130     auto alloc_kind_size = 0;
131     for (const auto &baseitem : baseitems_info) {
132         for (const auto &[level, chance] : baseitem.alloc_tables) {
133             if (chance != 0) {
134                 alloc_kind_size++;
135                 num[level]++;
136             }
137         }
138     }
139
140     for (auto i = 1; i < MAX_DEPTH; i++) {
141         num[i] += num[i - 1];
142     }
143
144     if (num[0] == 0) {
145         quit(_("町のアイテムがない!", "No town objects!"));
146     }
147
148     alloc_kind_table.assign(alloc_kind_size, {});
149     short aux[MAX_DEPTH]{};
150     for (const auto &baseitem : baseitems_info) {
151         for (const auto &[level, chance] : baseitem.alloc_tables) {
152             if (chance == 0) {
153                 continue;
154             }
155
156             const auto x = level;
157             const short p = 100 / chance;
158             const auto y = (x > 0) ? num[x - 1] : 0;
159             const auto z = y + aux[x];
160             alloc_kind_table[z].index = baseitem.idx;
161             alloc_kind_table[z].level = x;
162             alloc_kind_table[z].prob1 = p;
163             alloc_kind_table[z].prob2 = p;
164             aux[x]++;
165         }
166     }
167 }