OSDN Git Service

[fix] #41503 超能力者でゲームを開始しようとするとクラッシュ
[hengband/hengband.git] / src / load / quest-loader.c
1 #include "load/quest-loader.h"
2 #include "dungeon/quest.h"
3 #include "floor/floor-town.h"
4 #include "load/angband-version-comparer.h"
5 #include "load/load-util.h"
6 #include "load/load-zangband.h"
7 #include "monster-race/monster-race.h"
8 #include "monster-race/race-flags1.h"
9 #include "monster-race/race-flags7.h"
10 #include "object-enchant/trg-types.h"
11 #include "system/artifact-type-definition.h"
12 #include "system/floor-type-definition.h"
13 #include "system/system-variables.h"
14
15 /*!
16  * @brief ランダムクエスト情報の読み込み
17  * @param なし
18  * @return なし
19  * @details MAX_TRIES: ランダムクエストのモンスターを確定するために試行する回数 /
20  * Maximum number of tries for selection of a proper quest monster
21  */
22 void rd_unique_info(void)
23 {
24     const int MAX_TRIES = 100;
25     for (int i = 0; i < max_r_idx; i++) {
26         monster_race *r_ptr = &r_info[i];
27         r_ptr->max_num = MAX_TRIES;
28         if (r_ptr->flags1 & RF1_UNIQUE)
29             r_ptr->max_num = 1;
30         else if (r_ptr->flags7 & RF7_NAZGUL)
31             r_ptr->max_num = MAX_NAZGUL_NUM;
32     }
33 }
34
35 errr load_town(void)
36 {
37     u16b max_towns_load;
38     rd_u16b(&max_towns_load);
39     if (max_towns_load <= max_towns)
40         return 0;
41
42     load_note(format(_("町が多すぎる(%u)!", "Too many (%u) towns!"), max_towns_load));
43     return 23;
44 }
45
46 errr load_quest_info(u16b *max_quests_load, byte *max_rquests_load)
47 {
48     rd_u16b(max_quests_load);
49     if (z_older_than(11, 0, 7))
50         *max_rquests_load = 10;
51     else
52         rd_byte(max_rquests_load);
53
54     if (*max_quests_load <= max_q_idx)
55         return 0;
56
57     load_note(format(_("クエストが多すぎる(%u)!", "Too many (%u) quests!"), *max_quests_load));
58     return 23;
59 }
60
61 static bool check_quest_index(int loading_quest_index)
62 {
63     if (loading_quest_index < max_q_idx)
64         return FALSE;
65
66     strip_bytes(2);
67     strip_bytes(2);
68     return TRUE;
69 }
70
71 static void load_quest_completion(quest_type *q_ptr)
72 {
73     rd_s16b(&q_ptr->status);
74     s16b tmp16s;
75     rd_s16b(&tmp16s);
76     q_ptr->level = tmp16s;
77
78     if (z_older_than(11, 0, 6))
79         q_ptr->complev = 0;
80     else {
81         byte tmp8u;
82         rd_byte(&tmp8u);
83         q_ptr->complev = tmp8u;
84     }
85
86     if (h_older_than(2, 1, 2, 2))
87         q_ptr->comptime = 0;
88     else
89         rd_u32b(&q_ptr->comptime);
90 }
91
92 static void load_quest_details(player_type *creature_ptr, quest_type *q_ptr, int loading_quest_index)
93 {
94     s16b tmp16s;
95     rd_s16b(&tmp16s);
96     q_ptr->cur_num = (MONSTER_NUMBER)tmp16s;
97     rd_s16b(&tmp16s);
98     q_ptr->max_num = (MONSTER_NUMBER)tmp16s;
99     rd_s16b(&q_ptr->type);
100
101     rd_s16b(&q_ptr->r_idx);
102     if ((q_ptr->type == QUEST_TYPE_RANDOM) && (!q_ptr->r_idx))
103         determine_random_questor(creature_ptr, &quest[loading_quest_index]);
104
105     rd_s16b(&q_ptr->k_idx);
106     if (q_ptr->k_idx)
107         a_info[q_ptr->k_idx].gen_flags |= TRG_QUESTITEM;
108
109     byte tmp8u;
110     rd_byte(&tmp8u);
111     q_ptr->flags = tmp8u;
112 }
113
114 void analyze_quests(player_type *creature_ptr, const u16b max_quests_load, const byte max_rquests_load)
115 {
116     QUEST_IDX old_inside_quest = creature_ptr->current_floor_ptr->inside_quest;
117     for (int i = 0; i < max_quests_load; i++) {
118         if (check_quest_index(i))
119             continue;
120
121         quest_type *const q_ptr = &quest[i];
122         load_quest_completion(q_ptr);
123         bool is_quest_running = (q_ptr->status == QUEST_STATUS_TAKEN);
124         is_quest_running |= (!z_older_than(10, 3, 14) && (q_ptr->status == QUEST_STATUS_COMPLETED));
125         is_quest_running |= (!z_older_than(11, 0, 7) && (i >= MIN_RANDOM_QUEST) && (i <= (MIN_RANDOM_QUEST + max_rquests_load)));
126         if (!is_quest_running)
127             continue;
128
129         load_quest_details(creature_ptr, q_ptr, i);
130         if (z_older_than(10, 3, 11))
131             set_zangband_quest(creature_ptr, q_ptr, i, old_inside_quest);
132         else {
133             byte tmp8u;
134             rd_byte(&tmp8u);
135             q_ptr->dungeon = tmp8u;
136         }
137
138         if (q_ptr->status == QUEST_STATUS_TAKEN || q_ptr->status == QUEST_STATUS_UNTAKEN)
139             if (r_info[q_ptr->r_idx].flags1 & RF1_UNIQUE)
140                 r_info[q_ptr->r_idx].flags1 |= RF1_QUESTOR;
141     }
142 }