1 #include "floor/fixed-map-generator.h"
2 #include "artifact/fixed-art-generator.h"
3 #include "dungeon/quest.h"
4 #include "floor/floor-object.h"
5 #include "floor/floor-town.h"
6 #include "floor/wild.h"
7 #include "grid/feature.h"
9 #include "grid/object-placer.h"
10 #include "grid/trap.h"
11 #include "info-reader/general-parser.h"
12 #include "info-reader/random-grid-effect-types.h"
13 #include "io/tokenizer.h"
14 #include "monster-floor/monster-generator.h"
15 #include "monster-floor/place-monster-types.h"
16 #include "monster-race/monster-race.h"
17 #include "monster-race/race-flags1.h"
18 #include "monster-race/race-flags7.h"
19 #include "monster/monster-util.h"
20 #include "monster/smart-learn-types.h"
21 #include "object-enchant/apply-magic.h"
22 #include "object-enchant/item-apply-magic.h"
23 #include "object-enchant/object-ego.h"
24 #include "object-enchant/trg-types.h"
25 #include "object/object-info.h"
26 #include "object/object-kind-hook.h"
27 #include "object/object-kind.h"
28 #include "room/rooms-vault.h"
29 #include "sv-definition/sv-scroll-types.h"
30 #include "system/artifact-type-definition.h"
31 #include "system/floor-type-definition.h"
32 #include "system/grid-type-definition.h"
33 #include "system/monster-race-definition.h"
34 #include "system/monster-type-definition.h"
35 #include "system/player-type-definition.h"
36 #include "window/main-window-util.h"
37 #include "world/world-object.h"
38 #include "world/world.h"
40 // PARSE_ERROR_MAXが既にあり扱い辛いのでここでconst宣言.
41 static const int PARSE_CONTINUE = 255;
43 qtwg_type *initialize_quest_generator_type(qtwg_type *qtwg_ptr, char *buf, int ymin, int xmin, int ymax, int xmax, int *y, int *x)
46 qtwg_ptr->ymin = ymin;
47 qtwg_ptr->xmin = xmin;
48 qtwg_ptr->ymax = ymax;
49 qtwg_ptr->xmax = xmax;
56 * @brief フロアの所定のマスにオブジェクトを配置する
57 * Place the object j_ptr to a grid
58 * @param floor_ptr 現在フロアへの参照ポインタ
59 * @param j_ptr オブジェクト構造体の参照ポインタ
64 static void drop_here(floor_type *floor_ptr, ObjectType *j_ptr, POSITION y, POSITION x)
66 OBJECT_IDX o_idx = o_pop(floor_ptr);
68 o_ptr = &floor_ptr->o_list[o_idx];
69 o_ptr->copy_from(j_ptr);
72 o_ptr->held_m_idx = 0;
73 auto *g_ptr = &floor_ptr->grid_array[y][x];
74 g_ptr->o_idx_list.add(floor_ptr, o_idx);
77 static void generate_artifact(PlayerType *player_ptr, qtwg_type *qtwg_ptr, const ARTIFACT_IDX artifact_index)
79 if (artifact_index == 0) {
83 if ((a_info[artifact_index].cur_num == 0) && create_named_art(player_ptr, artifact_index, *qtwg_ptr->y, *qtwg_ptr->x)) {
84 a_info[artifact_index].cur_num = 1;
88 KIND_OBJECT_IDX k_idx = lookup_kind(ItemKindType::SCROLL, SV_SCROLL_ACQUIREMENT);
92 drop_here(player_ptr->current_floor_ptr, q_ptr, *qtwg_ptr->y, *qtwg_ptr->x);
95 static void parse_qtw_D(PlayerType *player_ptr, qtwg_type *qtwg_ptr, char *s)
97 *qtwg_ptr->x = qtwg_ptr->xmin;
98 auto *floor_ptr = player_ptr->current_floor_ptr;
100 for (int i = 0; ((*qtwg_ptr->x < qtwg_ptr->xmax) && (i < len)); (*qtwg_ptr->x)++, s++, i++) {
101 auto *g_ptr = &floor_ptr->grid_array[*qtwg_ptr->y][*qtwg_ptr->x];
103 OBJECT_IDX object_index = letter[idx].object;
104 MONSTER_IDX monster_index = letter[idx].monster;
105 int random = letter[idx].random;
106 ARTIFACT_IDX artifact_index = letter[idx].artifact;
107 g_ptr->feat = conv_dungeon_feat(floor_ptr, letter[idx].feature);
108 if (init_flags & INIT_ONLY_FEATURES) {
112 g_ptr->info = letter[idx].cave_info;
113 if (random & RANDOM_MONSTER) {
114 floor_ptr->monster_level = floor_ptr->base_level + monster_index;
116 place_monster(player_ptr, *qtwg_ptr->y, *qtwg_ptr->x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP | PM_NO_QUEST));
118 floor_ptr->monster_level = floor_ptr->base_level;
119 } else if (monster_index) {
120 int old_cur_num, old_max_num;
123 if (monster_index < 0) {
124 monster_index = -monster_index;
128 const auto r_idx = i2enum<MonsterRaceId>(monster_index);
129 auto &r_ref = r_info[r_idx];
131 old_cur_num = r_ref.cur_num;
132 old_max_num = r_ref.max_num;
134 if (r_ref.kind_flags.has(MonsterKindType::UNIQUE)) {
137 } else if (r_ref.flags7 & RF7_NAZGUL) {
138 if (r_ref.cur_num == r_ref.max_num) {
143 place_monster_aux(player_ptr, 0, *qtwg_ptr->y, *qtwg_ptr->x, r_idx, (PM_ALLOW_SLEEP | PM_NO_KAGE));
145 floor_ptr->m_list[hack_m_idx_ii].mflag2.set(MonsterConstantFlagType::CLONED);
146 r_ref.cur_num = old_cur_num;
147 r_ref.max_num = old_max_num;
151 if ((random & RANDOM_OBJECT) && (random & RANDOM_TRAP)) {
152 floor_ptr->object_level = floor_ptr->base_level + object_index;
155 * Random trap and random treasure defined
156 * 25% chance for trap and 75% chance for object
158 if (randint0(100) < 75) {
159 place_object(player_ptr, *qtwg_ptr->y, *qtwg_ptr->x, 0L);
161 place_trap(player_ptr, *qtwg_ptr->y, *qtwg_ptr->x);
164 floor_ptr->object_level = floor_ptr->base_level;
165 } else if (random & RANDOM_OBJECT) {
166 floor_ptr->object_level = floor_ptr->base_level + object_index;
167 if (randint0(100) < 75) {
168 place_object(player_ptr, *qtwg_ptr->y, *qtwg_ptr->x, 0L);
169 } else if (randint0(100) < 80) {
170 place_object(player_ptr, *qtwg_ptr->y, *qtwg_ptr->x, AM_GOOD);
172 place_object(player_ptr, *qtwg_ptr->y, *qtwg_ptr->x, AM_GOOD | AM_GREAT);
175 floor_ptr->object_level = floor_ptr->base_level;
176 } else if (random & RANDOM_TRAP) {
177 place_trap(player_ptr, *qtwg_ptr->y, *qtwg_ptr->x);
178 } else if (letter[idx].trap) {
179 g_ptr->mimic = g_ptr->feat;
180 g_ptr->feat = conv_dungeon_feat(floor_ptr, letter[idx].trap);
181 } else if (object_index) {
182 ObjectType tmp_object;
183 auto *o_ptr = &tmp_object;
184 o_ptr->prep(object_index);
185 if (o_ptr->tval == ItemKindType::GOLD) {
186 coin_type = object_index - OBJ_GOLD_LIST;
187 make_gold(player_ptr, o_ptr);
191 apply_magic_to_object(player_ptr, o_ptr, floor_ptr->base_level, AM_NO_FIXED_ART | AM_GOOD);
192 drop_here(floor_ptr, o_ptr, *qtwg_ptr->y, *qtwg_ptr->x);
195 generate_artifact(player_ptr, qtwg_ptr, artifact_index);
196 g_ptr->special = letter[idx].special;
200 static bool parse_qtw_QQ(quest_type *q_ptr, char **zz, int num)
202 if (zz[1][0] != 'Q') {
206 if ((init_flags & INIT_ASSIGN) == 0) {
211 artifact_type *a_ptr;
217 q_ptr->type = i2enum<QuestKindType>(atoi(zz[2]));
218 q_ptr->num_mon = (MONSTER_NUMBER)atoi(zz[3]);
219 q_ptr->cur_num = (MONSTER_NUMBER)atoi(zz[4]);
220 q_ptr->max_num = (MONSTER_NUMBER)atoi(zz[5]);
221 q_ptr->level = (DEPTH)atoi(zz[6]);
222 q_ptr->r_idx = i2enum<MonsterRaceId>(atoi(zz[7]));
223 q_ptr->k_idx = (KIND_OBJECT_IDX)atoi(zz[8]);
224 q_ptr->dungeon = (DUNGEON_IDX)atoi(zz[9]);
227 q_ptr->flags = atoi(zz[10]);
230 r_ptr = &r_info[q_ptr->r_idx];
231 if (r_ptr->kind_flags.has(MonsterKindType::UNIQUE)) {
232 r_ptr->flags1 |= RF1_QUESTOR;
235 a_ptr = &a_info[q_ptr->k_idx];
236 a_ptr->gen_flags.set(ItemGenerationTraitType::QUESTITEM);
241 * @todo 処理がどうなっているのかいずれチェックする
243 static bool parse_qtw_QR(quest_type *q_ptr, char **zz, int num)
245 if (zz[1][0] != 'R') {
249 if ((init_flags & INIT_ASSIGN) == 0) {
254 ARTIFACT_IDX idx, reward_idx = 0;
255 for (idx = 2; idx < num; idx++) {
256 ARTIFACT_IDX a_idx = (ARTIFACT_IDX)atoi(zz[idx]);
260 if (a_info[a_idx].cur_num > 0) {
264 if (one_in_(count)) {
270 q_ptr->k_idx = (KIND_OBJECT_IDX)reward_idx;
271 a_info[reward_idx].gen_flags.set(ItemGenerationTraitType::QUESTITEM);
273 q_ptr->type = QuestKindType::KILL_ALL;
280 * @brief t_info、q_info、w_infoにおけるQトークンをパースする
281 * @param qtwg_ptr トークンパース構造体への参照ポインタ
282 * @param zz トークン保管文字列
283 * @return エラーコード、但しPARSE_CONTINUEの時は処理続行
285 static int parse_qtw_Q(qtwg_type *qtwg_ptr, char **zz)
287 if (qtwg_ptr->buf[0] != 'Q') {
288 return PARSE_CONTINUE;
292 if (qtwg_ptr->buf[2] == '$') {
293 return PARSE_ERROR_NONE;
296 if (qtwg_ptr->buf[2] != '$') {
297 return PARSE_ERROR_NONE;
301 int num = tokenize(qtwg_ptr->buf + _(2, 3), 33, zz, 0);
303 return PARSE_ERROR_TOO_FEW_ARGUMENTS;
307 q_ptr = &(quest_map[i2enum<QuestId>(atoi(zz[0]))]);
308 if (parse_qtw_QQ(q_ptr, zz, num)) {
309 return PARSE_ERROR_NONE;
312 if (parse_qtw_QR(q_ptr, zz, num)) {
313 return PARSE_ERROR_NONE;
316 if (zz[1][0] == 'N') {
317 if (init_flags & (INIT_ASSIGN | INIT_SHOW_TEXT | INIT_NAME_ONLY)) {
318 strcpy(q_ptr->name, zz[2]);
321 return PARSE_ERROR_NONE;
324 if (zz[1][0] == 'T') {
325 if (init_flags & INIT_SHOW_TEXT) {
326 strcpy(quest_text[quest_text_line], zz[2]);
330 return PARSE_ERROR_NONE;
333 return PARSE_ERROR_GENERIC;
336 static bool parse_qtw_P(PlayerType *player_ptr, qtwg_type *qtwg_ptr, char **zz)
338 if (qtwg_ptr->buf[0] != 'P') {
342 if ((init_flags & INIT_CREATE_DUNGEON) == 0) {
346 if (tokenize(qtwg_ptr->buf + 2, 2, zz, 0) != 2) {
350 int panels_y = (*qtwg_ptr->y / SCREEN_HGT);
351 if (*qtwg_ptr->y % SCREEN_HGT) {
355 auto *floor_ptr = player_ptr->current_floor_ptr;
356 floor_ptr->height = panels_y * SCREEN_HGT;
357 int panels_x = (*qtwg_ptr->x / SCREEN_WID);
358 if (*qtwg_ptr->x % SCREEN_WID) {
362 floor_ptr->width = panels_x * SCREEN_WID;
363 panel_row_min = floor_ptr->height;
364 panel_col_min = floor_ptr->width;
365 if (inside_quest(floor_ptr->quest_number)) {
366 POSITION py = atoi(zz[0]);
367 POSITION px = atoi(zz[1]);
370 delete_monster(player_ptr, player_ptr->y, player_ptr->x);
374 if (!player_ptr->oldpx && !player_ptr->oldpy) {
375 player_ptr->oldpy = atoi(zz[0]);
376 player_ptr->oldpx = atoi(zz[1]);
382 static bool parse_qtw_M(qtwg_type *qtwg_ptr, char **zz)
384 if (qtwg_ptr->buf[0] != 'M') {
388 if ((tokenize(qtwg_ptr->buf + 2, 2, zz, 0) == 2) == 0) {
392 if (zz[0][0] == 'T') {
393 max_towns = static_cast<int16_t>(atoi(zz[1]));
394 } else if (zz[0][0] == 'O') {
395 w_ptr->max_o_idx = (OBJECT_IDX)atoi(zz[1]);
396 } else if (zz[0][0] == 'M') {
397 w_ptr->max_m_idx = (MONSTER_IDX)atoi(zz[1]);
398 } else if (zz[0][0] == 'W') {
399 if (zz[0][1] == 'X') {
400 w_ptr->max_wild_x = (POSITION)atoi(zz[1]);
403 if (zz[0][1] == 'Y') {
404 w_ptr->max_wild_y = (POSITION)atoi(zz[1]);
412 * @brief 固定マップ (クエスト&街&広域マップ)をフロアに生成する
413 * Parse a sub-file of the "extra info"
414 * @param player_ptr プレイヤーへの参照ポインタ
423 * @todo クエスト情報のみを読み込む手段と実際にフロアデータまで読み込む処理は分離したい
425 parse_error_type generate_fixed_map_floor(PlayerType *player_ptr, qtwg_type *qtwg_ptr, process_dungeon_file_pf parse_fixed_map)
428 if (!qtwg_ptr->buf[0]) {
429 return PARSE_ERROR_NONE;
432 if (iswspace(qtwg_ptr->buf[0])) {
433 return PARSE_ERROR_NONE;
436 if (qtwg_ptr->buf[0] == '#') {
437 return PARSE_ERROR_NONE;
440 if (qtwg_ptr->buf[1] != ':') {
441 return PARSE_ERROR_GENERIC;
444 if (qtwg_ptr->buf[0] == '%') {
445 return (*parse_fixed_map)(player_ptr, qtwg_ptr->buf + 2, qtwg_ptr->ymin, qtwg_ptr->xmin, qtwg_ptr->ymax, qtwg_ptr->xmax);
448 /* Process "F:<letter>:<terrain>:<cave_info>:<monster>:<object>:<ego>:<artifact>:<trap>:<special>" -- info for dungeon grid */
449 if (qtwg_ptr->buf[0] == 'F') {
450 return parse_line_feature(player_ptr->current_floor_ptr, qtwg_ptr->buf);
453 if (qtwg_ptr->buf[0] == 'D') {
454 char *s = qtwg_ptr->buf + 2;
455 if (init_flags & INIT_ONLY_BUILDINGS) {
456 return PARSE_ERROR_NONE;
459 parse_qtw_D(player_ptr, qtwg_ptr, s);
461 return PARSE_ERROR_NONE;
464 parse_error_type parse_result_Q = i2enum<parse_error_type>(parse_qtw_Q(qtwg_ptr, zz));
465 if (parse_result_Q != PARSE_CONTINUE) {
466 return parse_result_Q;
469 if (qtwg_ptr->buf[0] == 'W') {
470 return parse_line_wilderness(player_ptr, qtwg_ptr->buf, qtwg_ptr->xmin, qtwg_ptr->xmax, qtwg_ptr->y, qtwg_ptr->x);
473 if (parse_qtw_P(player_ptr, qtwg_ptr, zz)) {
474 return PARSE_ERROR_NONE;
477 if (qtwg_ptr->buf[0] == 'B') {
478 return parse_line_building(qtwg_ptr->buf);
481 if (parse_qtw_M(qtwg_ptr, zz)) {
482 return PARSE_ERROR_NONE;
485 return PARSE_ERROR_GENERIC;