OSDN Git Service

Merge pull request #3814 from Slimebreath6078/feature/Add_Laffey_II
[hengbandforosx/hengbandosx.git] / src / load / quest-loader.cpp
1 #include "load/quest-loader.h"
2 #include "artifact/fixed-art-types.h"
3 #include "dungeon/quest.h"
4 #include "floor/floor-town.h"
5 #include "load/angband-version-comparer.h"
6 #include "load/load-util.h"
7 #include "load/load-zangband.h"
8 #include "load/savedata-old-flag-types.h"
9 #include "monster-race/monster-race.h"
10 #include "object-enchant/trg-types.h"
11 #include "system/angband-exceptions.h"
12 #include "system/artifact-type-definition.h"
13 #include "system/floor-type-definition.h"
14 #include "system/monster-race-info.h"
15 #include "system/player-type-definition.h"
16 #include "util/enum-converter.h"
17
18 errr load_town(void)
19 {
20     auto max_towns_load = rd_u16b();
21     if (max_towns_load <= towns_info.size()) {
22         return 0;
23     }
24
25     load_note(format(_("町が多すぎる(%u)!", "Too many (%u) towns!"), max_towns_load));
26     return 23;
27 }
28
29 std::tuple<uint16_t, byte> load_quest_info()
30 {
31     auto max_quests_load = rd_u16b();
32     byte max_rquests_load;
33     if (h_older_than(1, 0, 7)) {
34         max_rquests_load = 10;
35     } else {
36         max_rquests_load = rd_byte();
37     }
38
39     return std::make_tuple(max_quests_load, max_rquests_load);
40 }
41
42 static void load_quest_completion(QuestType *q_ptr)
43 {
44     q_ptr->status = i2enum<QuestStatusType>(rd_s16b());
45     q_ptr->level = rd_s16b();
46
47     if (h_older_than(1, 0, 6)) {
48         q_ptr->complev = 0;
49     } else {
50         q_ptr->complev = rd_byte();
51     }
52
53     if (h_older_than(2, 1, 2, 2)) {
54         q_ptr->comptime = 0;
55     } else {
56         q_ptr->comptime = rd_u32b();
57     }
58 }
59
60 static void load_quest_details(PlayerType *player_ptr, QuestType *q_ptr, const QuestId loading_quest_index)
61 {
62     q_ptr->cur_num = rd_s16b();
63     q_ptr->max_num = rd_s16b();
64     q_ptr->type = i2enum<QuestKindType>(rd_s16b());
65
66     q_ptr->r_idx = i2enum<MonsterRaceId>(rd_s16b());
67     if ((q_ptr->type == QuestKindType::RANDOM) && !MonsterRace(q_ptr->r_idx).is_valid()) {
68         auto &quest_list = QuestList::get_instance();
69         determine_random_questor(player_ptr, &quest_list[loading_quest_index]);
70     }
71     q_ptr->reward_artifact_idx = i2enum<FixedArtifactId>(rd_s16b());
72     if (q_ptr->has_reward()) {
73         q_ptr->get_reward().gen_flags.set(ItemGenerationTraitType::QUESTITEM);
74     }
75
76     q_ptr->flags = rd_byte();
77 }
78
79 static bool is_missing_id_ver_16(const QuestId q_idx)
80 {
81     auto is_missing_id = (enum2i(q_idx) == 0);
82     is_missing_id |= (enum2i(q_idx) == 13);
83     is_missing_id |= (enum2i(q_idx) == 17);
84     is_missing_id |= (enum2i(q_idx) >= 35 && enum2i(q_idx) <= 39);
85     is_missing_id |= (enum2i(q_idx) >= 88 && enum2i(q_idx) <= 100);
86
87     auto is_deleted_random_quest = (enum2i(q_idx) >= 50 || enum2i(q_idx) <= 88);
88
89     return is_missing_id || is_deleted_random_quest;
90 }
91
92 static bool is_loadable_quest(const QuestId q_idx, const byte max_rquests_load)
93 {
94     const auto &quest_list = QuestList::get_instance();
95     if (quest_list.find(q_idx) != quest_list.end()) {
96         return true;
97     }
98
99     bool is_missing_id;
100
101     if (loading_savefile_version_is_older_than(17)) {
102         is_missing_id = is_missing_id_ver_16(q_idx);
103     } else {
104         is_missing_id = false;
105     }
106
107     if (!is_missing_id) {
108         const std::string msg(_("削除されたクエストのあるセーブデータはサポート対象外です。",
109             "The save data with deleted quests is unsupported."));
110         throw SaveDataNotSupportedException(msg);
111     }
112
113     auto status = i2enum<QuestStatusType>(rd_s16b());
114
115     strip_bytes(2);
116     if (!h_older_than(1, 0, 6)) {
117         strip_bytes(1);
118     }
119     if (!h_older_than(2, 1, 2, 2)) {
120         strip_bytes(4);
121     }
122
123     auto is_quest_running = (status == QuestStatusType::TAKEN);
124     is_quest_running |= (!h_older_than(0, 3, 14) && (status == QuestStatusType::COMPLETED));
125     is_quest_running |= (!h_older_than(1, 0, 7) && (enum2i(q_idx) >= MIN_RANDOM_QUEST) && (enum2i(q_idx) <= (MIN_RANDOM_QUEST + max_rquests_load)));
126     if (!is_quest_running) {
127         return false;
128     }
129
130     strip_bytes(2);
131     strip_bytes(2);
132     strip_bytes(2);
133     strip_bytes(2);
134     strip_bytes(2);
135     strip_bytes(1);
136     return false;
137 }
138
139 void analyze_quests(PlayerType *player_ptr, const uint16_t max_quests_load, const byte max_rquests_load)
140 {
141     QuestId old_inside_quest = player_ptr->current_floor_ptr->quest_number;
142     for (auto i = 0; i < max_quests_load; i++) {
143         QuestId q_idx;
144         if (loading_savefile_version_is_older_than(17)) {
145             q_idx = i2enum<QuestId>(i);
146         } else {
147             q_idx = i2enum<QuestId>(rd_s16b());
148         }
149         if (!is_loadable_quest(q_idx, max_rquests_load)) {
150             continue;
151         }
152
153         auto &quest_list = QuestList::get_instance();
154         auto *q_ptr = &quest_list[q_idx];
155
156         if (loading_savefile_version_is_older_than(15)) {
157             if (i == enum2i(OldQuestId15::CITY_SEA) && q_ptr->status != QuestStatusType::UNTAKEN) {
158                 const std::string msg(_("海底都市クエストを受領または解決しているセーブデータはサポート外です。",
159                     "The save data with the taken quest of The City beneath the Sea is unsupported."));
160                 throw(SaveDataNotSupportedException(msg));
161             }
162         }
163
164         load_quest_completion(q_ptr);
165         auto is_quest_running = (q_ptr->status == QuestStatusType::TAKEN);
166         is_quest_running |= (!h_older_than(0, 3, 14) && (q_ptr->status == QuestStatusType::COMPLETED));
167         is_quest_running |= (!h_older_than(1, 0, 7) && (enum2i(q_idx) >= MIN_RANDOM_QUEST) && (enum2i(q_idx) <= (MIN_RANDOM_QUEST + max_rquests_load)));
168         if (!is_quest_running) {
169             continue;
170         }
171
172         load_quest_details(player_ptr, q_ptr, q_idx);
173         if (h_older_than(0, 3, 11)) {
174             set_zangband_quest(player_ptr, q_ptr, q_idx, old_inside_quest);
175         } else {
176             q_ptr->dungeon = rd_byte();
177         }
178
179         if (q_ptr->status == QuestStatusType::TAKEN || q_ptr->status == QuestStatusType::UNTAKEN) {
180             if (monraces_info[q_ptr->r_idx].kind_flags.has(MonsterKindType::UNIQUE)) {
181                 monraces_info[q_ptr->r_idx].misc_flags.set(MonsterMiscType::QUESTOR);
182             }
183         }
184     }
185 }