OSDN Git Service

Merge pull request #2399 from sikabane-works/feature/refactor-be_type
[hengbandforosx/hengbandosx.git] / src / floor / fixed-map-generator.cpp
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"
8 #include "grid/grid.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"
39
40 // PARSE_ERROR_MAXが既にあり扱い辛いのでここでconst宣言.
41 static const int PARSE_CONTINUE = 255;
42
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)
44 {
45     qtwg_ptr->buf = buf;
46     qtwg_ptr->ymin = ymin;
47     qtwg_ptr->xmin = xmin;
48     qtwg_ptr->ymax = ymax;
49     qtwg_ptr->xmax = xmax;
50     qtwg_ptr->y = y;
51     qtwg_ptr->x = x;
52     return qtwg_ptr;
53 }
54
55 /*!
56  * @brief フロアの所定のマスにオブジェクトを配置する
57  * Place the object j_ptr to a grid
58  * @param floor_ptr 現在フロアへの参照ポインタ
59  * @param j_ptr オブジェクト構造体の参照ポインタ
60  * @param y 配置先Y座標
61  * @param x 配置先X座標
62  * @return エラーコード
63  */
64 static void drop_here(floor_type *floor_ptr, ObjectType *j_ptr, POSITION y, POSITION x)
65 {
66     OBJECT_IDX o_idx = o_pop(floor_ptr);
67     ObjectType *o_ptr;
68     o_ptr = &floor_ptr->o_list[o_idx];
69     o_ptr->copy_from(j_ptr);
70     o_ptr->iy = y;
71     o_ptr->ix = x;
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);
75 }
76
77 static void generate_artifact(PlayerType *player_ptr, qtwg_type *qtwg_ptr, const ARTIFACT_IDX artifact_index)
78 {
79     if (artifact_index == 0) {
80         return;
81     }
82
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;
85         return;
86     }
87
88     KIND_OBJECT_IDX k_idx = lookup_kind(ItemKindType::SCROLL, SV_SCROLL_ACQUIREMENT);
89     ObjectType forge;
90     auto *q_ptr = &forge;
91     q_ptr->prep(k_idx);
92     drop_here(player_ptr->current_floor_ptr, q_ptr, *qtwg_ptr->y, *qtwg_ptr->x);
93 }
94
95 static void parse_qtw_D(PlayerType *player_ptr, qtwg_type *qtwg_ptr, char *s)
96 {
97     *qtwg_ptr->x = qtwg_ptr->xmin;
98     auto *floor_ptr = player_ptr->current_floor_ptr;
99     int len = strlen(s);
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];
102         int idx = s[0];
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) {
109             continue;
110         }
111
112         g_ptr->info = letter[idx].cave_info;
113         if (random & RANDOM_MONSTER) {
114             floor_ptr->monster_level = floor_ptr->base_level + monster_index;
115
116             place_monster(player_ptr, *qtwg_ptr->y, *qtwg_ptr->x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP | PM_NO_QUEST));
117
118             floor_ptr->monster_level = floor_ptr->base_level;
119         } else if (monster_index) {
120             int old_cur_num, old_max_num;
121             bool clone = false;
122
123             if (monster_index < 0) {
124                 monster_index = -monster_index;
125                 clone = true;
126             }
127
128             const auto r_idx = i2enum<MonsterRaceId>(monster_index);
129             auto &r_ref = r_info[r_idx];
130
131             old_cur_num = r_ref.cur_num;
132             old_max_num = r_ref.max_num;
133
134             if (r_ref.kind_flags.has(MonsterKindType::UNIQUE)) {
135                 r_ref.cur_num = 0;
136                 r_ref.max_num = 1;
137             } else if (r_ref.flags7 & RF7_NAZGUL) {
138                 if (r_ref.cur_num == r_ref.max_num) {
139                     r_ref.max_num++;
140                 }
141             }
142
143             place_monster_aux(player_ptr, 0, *qtwg_ptr->y, *qtwg_ptr->x, r_idx, (PM_ALLOW_SLEEP | PM_NO_KAGE));
144             if (clone) {
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;
148             }
149         }
150
151         if ((random & RANDOM_OBJECT) && (random & RANDOM_TRAP)) {
152             floor_ptr->object_level = floor_ptr->base_level + object_index;
153
154             /*
155              * Random trap and random treasure defined
156              * 25% chance for trap and 75% chance for object
157              */
158             if (randint0(100) < 75) {
159                 place_object(player_ptr, *qtwg_ptr->y, *qtwg_ptr->x, 0L);
160             } else {
161                 place_trap(player_ptr, *qtwg_ptr->y, *qtwg_ptr->x);
162             }
163
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);
171             } else {
172                 place_object(player_ptr, *qtwg_ptr->y, *qtwg_ptr->x, AM_GOOD | AM_GREAT);
173             }
174
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);
188                 coin_type = 0;
189             }
190
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);
193         }
194
195         generate_artifact(player_ptr, qtwg_ptr, artifact_index);
196         g_ptr->special = letter[idx].special;
197     }
198 }
199
200 static bool parse_qtw_QQ(quest_type *q_ptr, char **zz, int num)
201 {
202     if (zz[1][0] != 'Q') {
203         return false;
204     }
205
206     if ((init_flags & INIT_ASSIGN) == 0) {
207         return true;
208     }
209
210     monster_race *r_ptr;
211     artifact_type *a_ptr;
212
213     if (num < 9) {
214         return true;
215     }
216
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]);
225
226     if (num > 10) {
227         q_ptr->flags = atoi(zz[10]);
228     }
229
230     r_ptr = &r_info[q_ptr->r_idx];
231     if (r_ptr->kind_flags.has(MonsterKindType::UNIQUE)) {
232         r_ptr->flags1 |= RF1_QUESTOR;
233     }
234
235     a_ptr = &a_info[q_ptr->k_idx];
236     a_ptr->gen_flags.set(ItemGenerationTraitType::QUESTITEM);
237     return true;
238 }
239
240 /*!
241  * @todo 処理がどうなっているのかいずれチェックする
242  */
243 static bool parse_qtw_QR(quest_type *q_ptr, char **zz, int num)
244 {
245     if (zz[1][0] != 'R') {
246         return false;
247     }
248
249     if ((init_flags & INIT_ASSIGN) == 0) {
250         return true;
251     }
252
253     int count = 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]);
257         if (a_idx < 1) {
258             continue;
259         }
260         if (a_info[a_idx].cur_num > 0) {
261             continue;
262         }
263         count++;
264         if (one_in_(count)) {
265             reward_idx = a_idx;
266         }
267     }
268
269     if (reward_idx) {
270         q_ptr->k_idx = (KIND_OBJECT_IDX)reward_idx;
271         a_info[reward_idx].gen_flags.set(ItemGenerationTraitType::QUESTITEM);
272     } else {
273         q_ptr->type = QuestKindType::KILL_ALL;
274     }
275
276     return true;
277 }
278
279 /*!
280  * @brief t_info、q_info、w_infoにおけるQトークンをパースする
281  * @param qtwg_ptr トークンパース構造体への参照ポインタ
282  * @param zz トークン保管文字列
283  * @return エラーコード、但しPARSE_CONTINUEの時は処理続行
284  */
285 static int parse_qtw_Q(qtwg_type *qtwg_ptr, char **zz)
286 {
287     if (qtwg_ptr->buf[0] != 'Q') {
288         return PARSE_CONTINUE;
289     }
290
291 #ifdef JP
292     if (qtwg_ptr->buf[2] == '$') {
293         return PARSE_ERROR_NONE;
294     }
295 #else
296     if (qtwg_ptr->buf[2] != '$') {
297         return PARSE_ERROR_NONE;
298     }
299 #endif
300
301     int num = tokenize(qtwg_ptr->buf + _(2, 3), 33, zz, 0);
302     if (num < 3) {
303         return PARSE_ERROR_TOO_FEW_ARGUMENTS;
304     }
305
306     quest_type *q_ptr;
307     q_ptr = &(quest_map[i2enum<QuestId>(atoi(zz[0]))]);
308     if (parse_qtw_QQ(q_ptr, zz, num)) {
309         return PARSE_ERROR_NONE;
310     }
311
312     if (parse_qtw_QR(q_ptr, zz, num)) {
313         return PARSE_ERROR_NONE;
314     }
315
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]);
319         }
320
321         return PARSE_ERROR_NONE;
322     }
323
324     if (zz[1][0] == 'T') {
325         if (init_flags & INIT_SHOW_TEXT) {
326             strcpy(quest_text[quest_text_line], zz[2]);
327             quest_text_line++;
328         }
329
330         return PARSE_ERROR_NONE;
331     }
332
333     return PARSE_ERROR_GENERIC;
334 }
335
336 static bool parse_qtw_P(PlayerType *player_ptr, qtwg_type *qtwg_ptr, char **zz)
337 {
338     if (qtwg_ptr->buf[0] != 'P') {
339         return false;
340     }
341
342     if ((init_flags & INIT_CREATE_DUNGEON) == 0) {
343         return true;
344     }
345
346     if (tokenize(qtwg_ptr->buf + 2, 2, zz, 0) != 2) {
347         return true;
348     }
349
350     int panels_y = (*qtwg_ptr->y / SCREEN_HGT);
351     if (*qtwg_ptr->y % SCREEN_HGT) {
352         panels_y++;
353     }
354
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) {
359         panels_x++;
360     }
361
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]);
368         player_ptr->y = py;
369         player_ptr->x = px;
370         delete_monster(player_ptr, player_ptr->y, player_ptr->x);
371         return true;
372     }
373
374     if (!player_ptr->oldpx && !player_ptr->oldpy) {
375         player_ptr->oldpy = atoi(zz[0]);
376         player_ptr->oldpx = atoi(zz[1]);
377     }
378
379     return true;
380 }
381
382 static bool parse_qtw_M(qtwg_type *qtwg_ptr, char **zz)
383 {
384     if (qtwg_ptr->buf[0] != 'M') {
385         return false;
386     }
387
388     if ((tokenize(qtwg_ptr->buf + 2, 2, zz, 0) == 2) == 0) {
389         return true;
390     }
391
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]);
401         }
402
403         if (zz[0][1] == 'Y') {
404             w_ptr->max_wild_y = (POSITION)atoi(zz[1]);
405         }
406     }
407
408     return true;
409 }
410
411 /*!
412  * @brief 固定マップ (クエスト&街&広域マップ)をフロアに生成する
413  * Parse a sub-file of the "extra info"
414  * @param player_ptr プレイヤーへの参照ポインタ
415  * @param buf 文字列
416  * @param ymin 詳細不明
417  * @param xmin 詳細不明
418  * @param ymax 詳細不明
419  * @param xmax 詳細不明
420  * @param y 詳細不明
421  * @param x 詳細不明
422  * @return エラーコード
423  * @todo クエスト情報のみを読み込む手段と実際にフロアデータまで読み込む処理は分離したい
424  */
425 parse_error_type generate_fixed_map_floor(PlayerType *player_ptr, qtwg_type *qtwg_ptr, process_dungeon_file_pf parse_fixed_map)
426 {
427     char *zz[33];
428     if (!qtwg_ptr->buf[0]) {
429         return PARSE_ERROR_NONE;
430     }
431
432     if (iswspace(qtwg_ptr->buf[0])) {
433         return PARSE_ERROR_NONE;
434     }
435
436     if (qtwg_ptr->buf[0] == '#') {
437         return PARSE_ERROR_NONE;
438     }
439
440     if (qtwg_ptr->buf[1] != ':') {
441         return PARSE_ERROR_GENERIC;
442     }
443
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);
446     }
447
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);
451     }
452
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;
457         }
458
459         parse_qtw_D(player_ptr, qtwg_ptr, s);
460         (*qtwg_ptr->y)++;
461         return PARSE_ERROR_NONE;
462     }
463
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;
467     }
468
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);
471     }
472
473     if (parse_qtw_P(player_ptr, qtwg_ptr, zz)) {
474         return PARSE_ERROR_NONE;
475     }
476
477     if (qtwg_ptr->buf[0] == 'B') {
478         return parse_line_building(qtwg_ptr->buf);
479     }
480
481     if (parse_qtw_M(qtwg_ptr, zz)) {
482         return PARSE_ERROR_NONE;
483     }
484
485     return PARSE_ERROR_GENERIC;
486 }