OSDN Git Service

[Refactor] #933 Renamed accessory-enchanter-base.h to enchanter-base.h
[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-generator.h"
26 #include "object/object-info.h"
27 #include "object/object-kind-hook.h"
28 #include "object/object-kind.h"
29 #include "room/rooms-vault.h"
30 #include "sv-definition/sv-scroll-types.h"
31 #include "system/artifact-type-definition.h"
32 #include "system/floor-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, object_type *j_ptr, POSITION y, POSITION x)
65 {
66     OBJECT_IDX o_idx = o_pop(floor_ptr);
67     object_type *o_ptr;
68     o_ptr = &floor_ptr->o_list[o_idx];
69     object_copy(o_ptr, j_ptr);
70     o_ptr->iy = y;
71     o_ptr->ix = x;
72     o_ptr->held_m_idx = 0;
73     grid_type *g_ptr = &floor_ptr->grid_array[y][x];
74     g_ptr->o_idx_list.push_front(o_idx);
75 }
76
77 static void generate_artifact(player_type *player_ptr, qtwg_type *qtwg_ptr, const ARTIFACT_IDX artifact_index)
78 {
79     if (artifact_index == 0)
80         return;
81
82     if ((a_info[artifact_index].cur_num == 0) && create_named_art(player_ptr, artifact_index, *qtwg_ptr->y, *qtwg_ptr->x)) {
83         a_info[artifact_index].cur_num = 1;
84         return;
85     }
86
87     KIND_OBJECT_IDX k_idx = lookup_kind(TV_SCROLL, SV_SCROLL_ACQUIREMENT);
88     object_type forge;
89     object_type *q_ptr = &forge;
90     object_prep(player_ptr, q_ptr, k_idx);
91     drop_here(player_ptr->current_floor_ptr, q_ptr, *qtwg_ptr->y, *qtwg_ptr->x);
92 }
93
94 static void parse_qtw_D(player_type *player_ptr, qtwg_type *qtwg_ptr, char *s)
95 {
96     *qtwg_ptr->x = qtwg_ptr->xmin;
97     floor_type *floor_ptr = player_ptr->current_floor_ptr;
98     int len = strlen(s);
99     for (int i = 0; ((*qtwg_ptr->x < qtwg_ptr->xmax) && (i < len)); (*qtwg_ptr->x)++, s++, i++) {
100         grid_type *g_ptr = &floor_ptr->grid_array[*qtwg_ptr->y][*qtwg_ptr->x];
101         int idx = s[0];
102         OBJECT_IDX object_index = letter[idx].object;
103         MONSTER_IDX monster_index = letter[idx].monster;
104         int random = letter[idx].random;
105         ARTIFACT_IDX artifact_index = letter[idx].artifact;
106         g_ptr->feat = conv_dungeon_feat(floor_ptr, letter[idx].feature);
107         if (init_flags & INIT_ONLY_FEATURES)
108             continue;
109
110         g_ptr->info = letter[idx].cave_info;
111         if (random & RANDOM_MONSTER) {
112             floor_ptr->monster_level = floor_ptr->base_level + monster_index;
113
114             place_monster(player_ptr, *qtwg_ptr->y, *qtwg_ptr->x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
115
116             floor_ptr->monster_level = floor_ptr->base_level;
117         } else if (monster_index) {
118             int old_cur_num, old_max_num;
119             bool clone = FALSE;
120
121             if (monster_index < 0) {
122                 monster_index = -monster_index;
123                 clone = TRUE;
124             }
125
126             old_cur_num = r_info[monster_index].cur_num;
127             old_max_num = r_info[monster_index].max_num;
128
129             if (r_info[monster_index].flags1 & RF1_UNIQUE) {
130                 r_info[monster_index].cur_num = 0;
131                 r_info[monster_index].max_num = 1;
132             } else if (r_info[monster_index].flags7 & RF7_NAZGUL) {
133                 if (r_info[monster_index].cur_num == r_info[monster_index].max_num) {
134                     r_info[monster_index].max_num++;
135                 }
136             }
137
138             place_monster_aux(player_ptr, 0, *qtwg_ptr->y, *qtwg_ptr->x, monster_index, (PM_ALLOW_SLEEP | PM_NO_KAGE));
139             if (clone) {
140                 floor_ptr->m_list[hack_m_idx_ii].mflag2.set(MFLAG2::CLONED);
141                 r_info[monster_index].cur_num = old_cur_num;
142                 r_info[monster_index].max_num = old_max_num;
143             }
144         }
145
146         if ((random & RANDOM_OBJECT) && (random & RANDOM_TRAP)) {
147             floor_ptr->object_level = floor_ptr->base_level + object_index;
148
149             /*
150              * Random trap and random treasure defined
151              * 25% chance for trap and 75% chance for object
152              */
153             if (randint0(100) < 75) {
154                 place_object(player_ptr, *qtwg_ptr->y, *qtwg_ptr->x, 0L);
155             } else {
156                 place_trap(player_ptr, *qtwg_ptr->y, *qtwg_ptr->x);
157             }
158
159             floor_ptr->object_level = floor_ptr->base_level;
160         } else if (random & RANDOM_OBJECT) {
161             floor_ptr->object_level = floor_ptr->base_level + object_index;
162             if (randint0(100) < 75)
163                 place_object(player_ptr, *qtwg_ptr->y, *qtwg_ptr->x, 0L);
164             else if (randint0(100) < 80)
165                 place_object(player_ptr, *qtwg_ptr->y, *qtwg_ptr->x, AM_GOOD);
166             else
167                 place_object(player_ptr, *qtwg_ptr->y, *qtwg_ptr->x, AM_GOOD | AM_GREAT);
168
169             floor_ptr->object_level = floor_ptr->base_level;
170         } else if (random & RANDOM_TRAP) {
171             place_trap(player_ptr, *qtwg_ptr->y, *qtwg_ptr->x);
172         } else if (letter[idx].trap) {
173             g_ptr->mimic = g_ptr->feat;
174             g_ptr->feat = conv_dungeon_feat(floor_ptr, letter[idx].trap);
175         } else if (object_index) {
176             object_type tmp_object;
177             object_type *o_ptr = &tmp_object;
178             object_prep(player_ptr, o_ptr, object_index);
179             if (o_ptr->tval == TV_GOLD) {
180                 coin_type = object_index - OBJ_GOLD_LIST;
181                 make_gold(player_ptr, o_ptr);
182                 coin_type = 0;
183             }
184
185             apply_magic_to_object(player_ptr, o_ptr, floor_ptr->base_level, AM_NO_FIXED_ART | AM_GOOD);
186             drop_here(floor_ptr, o_ptr, *qtwg_ptr->y, *qtwg_ptr->x);
187         }
188
189         generate_artifact(player_ptr, qtwg_ptr, artifact_index);
190         g_ptr->special = letter[idx].special;
191     }
192 }
193
194 static bool parse_qtw_QQ(quest_type *q_ptr, char **zz, int num)
195 {
196     if (zz[1][0] != 'Q')
197         return FALSE;
198
199     if ((init_flags & INIT_ASSIGN) == 0)
200         return TRUE;
201
202     monster_race *r_ptr;
203     artifact_type *a_ptr;
204
205     if (num < 9)
206         return TRUE;
207
208     q_ptr->type = (QUEST_TYPE)atoi(zz[2]);
209     q_ptr->num_mon = (MONSTER_NUMBER)atoi(zz[3]);
210     q_ptr->cur_num = (MONSTER_NUMBER)atoi(zz[4]);
211     q_ptr->max_num = (MONSTER_NUMBER)atoi(zz[5]);
212     q_ptr->level = (DEPTH)atoi(zz[6]);
213     q_ptr->r_idx = (MONRACE_IDX)atoi(zz[7]);
214     q_ptr->k_idx = (KIND_OBJECT_IDX)atoi(zz[8]);
215     q_ptr->dungeon = (DUNGEON_IDX)atoi(zz[9]);
216
217     if (num > 10)
218         q_ptr->flags = atoi(zz[10]);
219
220     r_ptr = &r_info[q_ptr->r_idx];
221     if (r_ptr->flags1 & RF1_UNIQUE)
222         r_ptr->flags1 |= RF1_QUESTOR;
223
224     a_ptr = &a_info[q_ptr->k_idx];
225     a_ptr->gen_flags.set(TRG::QUESTITEM);
226     return TRUE;
227 }
228
229 /*!
230  * @todo 処理がどうなっているのかいずれチェックする
231  */
232 static bool parse_qtw_QR(quest_type *q_ptr, char **zz, int num)
233 {
234     if (zz[1][0] != 'R')
235         return FALSE;
236
237     if ((init_flags & INIT_ASSIGN) == 0)
238         return TRUE;
239
240     int count = 0;
241     ARTIFACT_IDX idx, reward_idx = 0;
242     for (idx = 2; idx < num; idx++) {
243         ARTIFACT_IDX a_idx = (ARTIFACT_IDX)atoi(zz[idx]);
244         if (a_idx < 1)
245             continue;
246         if (a_info[a_idx].cur_num > 0)
247             continue;
248         count++;
249         if (one_in_(count))
250             reward_idx = a_idx;
251     }
252
253     if (reward_idx) {
254         q_ptr->k_idx = (KIND_OBJECT_IDX)reward_idx;
255         a_info[reward_idx].gen_flags.set(TRG::QUESTITEM);
256     } else {
257         q_ptr->type = QUEST_TYPE_KILL_ALL;
258     }
259
260     return TRUE;
261 }
262
263 /*!
264  * @brief t_info、q_info、w_infoにおけるQトークンをパースする
265  * @param qtwg_ptr トークンパース構造体への参照ポインタ
266  * @param zz トークン保管文字列
267  * @return エラーコード、但しPARSE_CONTINUEの時は処理続行
268  */
269 static int parse_qtw_Q(qtwg_type *qtwg_ptr, char **zz)
270 {
271     if (qtwg_ptr->buf[0] != 'Q')
272         return PARSE_CONTINUE;
273
274 #ifdef JP
275     if (qtwg_ptr->buf[2] == '$')
276         return PARSE_ERROR_NONE;
277 #else
278     if (qtwg_ptr->buf[2] != '$')
279         return PARSE_ERROR_NONE;
280 #endif
281
282     int num = tokenize(qtwg_ptr->buf + _(2, 3), 33, zz, 0);
283     if (num < 3)
284         return PARSE_ERROR_TOO_FEW_ARGUMENTS;
285
286     quest_type *q_ptr;
287     q_ptr = &(quest[atoi(zz[0])]);
288     if (parse_qtw_QQ(q_ptr, zz, num))
289         return PARSE_ERROR_NONE;
290
291     if (parse_qtw_QR(q_ptr, zz, num))
292         return PARSE_ERROR_NONE;
293
294     if (zz[1][0] == 'N') {
295         if (init_flags & (INIT_ASSIGN | INIT_SHOW_TEXT | INIT_NAME_ONLY)) {
296             strcpy(q_ptr->name, zz[2]);
297         }
298
299         return PARSE_ERROR_NONE;
300     }
301
302     if (zz[1][0] == 'T') {
303         if (init_flags & INIT_SHOW_TEXT) {
304             strcpy(quest_text[quest_text_line], zz[2]);
305             quest_text_line++;
306         }
307
308         return PARSE_ERROR_NONE;
309     }
310
311     return PARSE_ERROR_GENERIC;
312 }
313
314 static bool parse_qtw_P(player_type *player_ptr, qtwg_type *qtwg_ptr, char **zz)
315 {
316     if (qtwg_ptr->buf[0] != 'P')
317         return FALSE;
318
319     if ((init_flags & INIT_CREATE_DUNGEON) == 0)
320         return TRUE;
321
322     if (tokenize(qtwg_ptr->buf + 2, 2, zz, 0) != 2)
323         return TRUE;
324
325     int panels_y = (*qtwg_ptr->y / SCREEN_HGT);
326     if (*qtwg_ptr->y % SCREEN_HGT)
327         panels_y++;
328
329     floor_type *floor_ptr = player_ptr->current_floor_ptr;
330     floor_ptr->height = panels_y * SCREEN_HGT;
331     int panels_x = (*qtwg_ptr->x / SCREEN_WID);
332     if (*qtwg_ptr->x % SCREEN_WID)
333         panels_x++;
334
335     floor_ptr->width = panels_x * SCREEN_WID;
336     panel_row_min = floor_ptr->height;
337     panel_col_min = floor_ptr->width;
338     if (floor_ptr->inside_quest) {
339         POSITION py = atoi(zz[0]);
340         POSITION px = atoi(zz[1]);
341         player_ptr->y = py;
342         player_ptr->x = px;
343         delete_monster(player_ptr, player_ptr->y, player_ptr->x);
344         return TRUE;
345     }
346
347     if (!player_ptr->oldpx && !player_ptr->oldpy) {
348         player_ptr->oldpy = atoi(zz[0]);
349         player_ptr->oldpx = atoi(zz[1]);
350     }
351
352     return TRUE;
353 }
354
355 static bool parse_qtw_M(qtwg_type *qtwg_ptr, char **zz)
356 {
357     if (qtwg_ptr->buf[0] != 'M')
358         return FALSE;
359
360     if ((tokenize(qtwg_ptr->buf + 2, 2, zz, 0) == 2) == 0)
361         return TRUE;
362
363     if (zz[0][0] == 'T') {
364         max_towns = (TOWN_IDX)atoi(zz[1]);
365     } else if (zz[0][0] == 'Q') {
366         max_q_idx = (QUEST_IDX)atoi(zz[1]);
367     } else if (zz[0][0] == 'R') {
368         max_r_idx = (player_race_type)atoi(zz[1]);
369     } else if (zz[0][0] == 'K') {
370         max_k_idx = (KIND_OBJECT_IDX)atoi(zz[1]);
371     } else if (zz[0][0] == 'V') {
372         max_v_idx = (VAULT_IDX)atoi(zz[1]);
373     } else if (zz[0][0] == 'F') {
374         max_f_idx = (FEAT_IDX)atoi(zz[1]);
375     } else if (zz[0][0] == 'A') {
376         max_a_idx = (ARTIFACT_IDX)atoi(zz[1]);
377     } else if (zz[0][0] == 'E') {
378         max_e_idx = (EGO_IDX)atoi(zz[1]);
379     } else if (zz[0][0] == 'D') {
380         current_world_ptr->max_d_idx = (DUNGEON_IDX)atoi(zz[1]);
381     } else if (zz[0][0] == 'O') {
382         current_world_ptr->max_o_idx = (OBJECT_IDX)atoi(zz[1]);
383     } else if (zz[0][0] == 'M') {
384         current_world_ptr->max_m_idx = (MONSTER_IDX)atoi(zz[1]);
385     } else if (zz[0][0] == 'W') {
386         if (zz[0][1] == 'X')
387             current_world_ptr->max_wild_x = (POSITION)atoi(zz[1]);
388
389         if (zz[0][1] == 'Y')
390             current_world_ptr->max_wild_y = (POSITION)atoi(zz[1]);
391     }
392
393     return TRUE;
394 }
395
396 /*!
397  * @brief 固定マップ (クエスト&街&広域マップ)をフロアに生成する
398  * Parse a sub-file of the "extra info"
399  * @param player_ptr プレーヤーへの参照ポインタ
400  * @param buf 文字列
401  * @param ymin 詳細不明
402  * @param xmin 詳細不明
403  * @param ymax 詳細不明
404  * @param xmax 詳細不明
405  * @param y 詳細不明
406  * @param x 詳細不明
407  * @return エラーコード
408  */
409 parse_error_type generate_fixed_map_floor(player_type *player_ptr, qtwg_type *qtwg_ptr, process_dungeon_file_pf parse_fixed_map)
410 {
411     char *zz[33];
412     if (!qtwg_ptr->buf[0])
413         return PARSE_ERROR_NONE;
414
415     if (iswspace(qtwg_ptr->buf[0]))
416         return PARSE_ERROR_NONE;
417
418     if (qtwg_ptr->buf[0] == '#')
419         return PARSE_ERROR_NONE;
420
421     if (qtwg_ptr->buf[1] != ':')
422         return PARSE_ERROR_GENERIC;
423
424     if (qtwg_ptr->buf[0] == '%')
425         return (*parse_fixed_map)(player_ptr, qtwg_ptr->buf + 2, qtwg_ptr->ymin, qtwg_ptr->xmin, qtwg_ptr->ymax, qtwg_ptr->xmax);
426
427     /* Process "F:<letter>:<terrain>:<cave_info>:<monster>:<object>:<ego>:<artifact>:<trap>:<special>" -- info for dungeon grid */
428     if (qtwg_ptr->buf[0] == 'F')
429         return parse_line_feature(player_ptr->current_floor_ptr, qtwg_ptr->buf);
430
431     if (qtwg_ptr->buf[0] == 'D') {
432         char *s = qtwg_ptr->buf + 2;
433         if (init_flags & INIT_ONLY_BUILDINGS)
434             return PARSE_ERROR_NONE;
435
436         parse_qtw_D(player_ptr, qtwg_ptr, s);
437         (*qtwg_ptr->y)++;
438         return PARSE_ERROR_NONE;
439     }
440
441     parse_error_type parse_result_Q = static_cast<parse_error_type>(parse_qtw_Q(qtwg_ptr, zz));
442     if (parse_result_Q != PARSE_CONTINUE)
443         return parse_result_Q;
444
445     if (qtwg_ptr->buf[0] == 'W')
446         return parse_line_wilderness(player_ptr, qtwg_ptr->buf, qtwg_ptr->xmin, qtwg_ptr->xmax, qtwg_ptr->y, qtwg_ptr->x);
447
448     if (parse_qtw_P(player_ptr, qtwg_ptr, zz))
449         return PARSE_ERROR_NONE;
450
451     if (qtwg_ptr->buf[0] == 'B')
452         return parse_line_building(qtwg_ptr->buf);
453
454     if (parse_qtw_M(qtwg_ptr, zz))
455         return PARSE_ERROR_NONE;
456
457     return PARSE_ERROR_GENERIC;
458 }