OSDN Git Service

Merge remote-tracking branch 'remotes/hengbandosx/english-io-edits' into feature...
[hengbandforosx/hengbandosx.git] / src / floor / floor-generator.c
1 /*!
2  * @brief ダンジョンの生成 / Dungeon generation
3  * @date 2014/01/04
4  * @author
5  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke\n
6  * This software may be copied and distributed for educational, research,\n
7  * and not for profit purposes provided that this copyright and statement\n
8  * are included in all such copies.  Other copyrights may also apply.\n
9  * 2014 Deskull rearranged comment for Doxygen. \n
10  */
11
12 #include "floor/floor-generator.h"
13 #include "dungeon/dungeon-flag-types.h"
14 #include "dungeon/dungeon.h"
15 #include "dungeon/quest.h"
16 #include "floor/cave-generator.h"
17 #include "floor/floor-events.h"
18 #include "floor/floor-save.h" // todo precalc_cur_num_of_pet() が依存している、違和感.
19 #include "floor/floor-util.h"
20 #include "floor/wild.h"
21 #include "game-option/birth-options.h"
22 #include "game-option/cheat-types.h"
23 #include "game-option/game-play-options.h"
24 #include "game-option/play-record-options.h"
25 #include "grid/feature.h"
26 #include "grid/grid.h"
27 #include "info-reader/feature-reader.h"
28 #include "info-reader/fixed-map-parser.h"
29 #include "io/write-diary.h"
30 #include "market/arena-info-table.h"
31 #include "monster-floor/monster-generator.h"
32 #include "monster-floor/monster-remover.h"
33 #include "monster-floor/place-monster-types.h"
34 #include "monster-race/monster-race.h"
35 #include "monster/monster-flag-types.h"
36 #include "monster/monster-status-setter.h"
37 #include "monster/monster-status.h"
38 #include "monster/monster-update.h"
39 #include "monster/monster-util.h"
40 #include "system/building-type-definition.h"
41 #include "system/floor-type-definition.h"
42 #include "system/system-variables.h"
43 #include "view/display-messages.h"
44 #include "window/main-window-util.h"
45 #include "wizard/wizard-messages.h"
46 #include "world/world.h"
47
48 /*!
49  * @brief 闘技場用のアリーナ地形を作成する / Builds the on_defeat_arena_monster after it is entered -KMW-
50  * @param player_ptr プレーヤーへの参照ポインタ
51  * @return なし
52  */
53 static void build_arena(player_type *player_ptr, POSITION *start_y, POSITION *start_x)
54 {
55     POSITION yval = SCREEN_HGT / 2;
56     POSITION xval = SCREEN_WID / 2;
57     POSITION y_height = yval - 10;
58     POSITION y_depth = yval + 10;
59     POSITION x_left = xval - 32;
60     POSITION x_right = xval + 32;
61     floor_type *floor_ptr = player_ptr->current_floor_ptr;
62     for (POSITION i = y_height; i <= y_height + 5; i++)
63         for (POSITION j = x_left; j <= x_right; j++) {
64             place_bold(player_ptr, i, j, GB_EXTRA_PERM);
65             floor_ptr->grid_array[i][j].info |= (CAVE_GLOW | CAVE_MARK);
66         }
67
68     for (POSITION i = y_depth; i >= y_depth - 5; i--)
69         for (POSITION j = x_left; j <= x_right; j++) {
70             place_bold(player_ptr, i, j, GB_EXTRA_PERM);
71             floor_ptr->grid_array[i][j].info |= (CAVE_GLOW | CAVE_MARK);
72         }
73
74     for (POSITION j = x_left; j <= x_left + 17; j++)
75         for (POSITION i = y_height; i <= y_depth; i++) {
76             place_bold(player_ptr, i, j, GB_EXTRA_PERM);
77             floor_ptr->grid_array[i][j].info |= (CAVE_GLOW | CAVE_MARK);
78         }
79
80     for (POSITION j = x_right; j >= x_right - 17; j--)
81         for (POSITION i = y_height; i <= y_depth; i++) {
82             place_bold(player_ptr, i, j, GB_EXTRA_PERM);
83             floor_ptr->grid_array[i][j].info |= (CAVE_GLOW | CAVE_MARK);
84         }
85
86     place_bold(player_ptr, y_height + 6, x_left + 18, GB_EXTRA_PERM);
87     floor_ptr->grid_array[y_height + 6][x_left + 18].info |= CAVE_GLOW | CAVE_MARK;
88     place_bold(player_ptr, y_depth - 6, x_left + 18, GB_EXTRA_PERM);
89     floor_ptr->grid_array[y_depth - 6][x_left + 18].info |= CAVE_GLOW | CAVE_MARK;
90     place_bold(player_ptr, y_height + 6, x_right - 18, GB_EXTRA_PERM);
91     floor_ptr->grid_array[y_height + 6][x_right - 18].info |= CAVE_GLOW | CAVE_MARK;
92     place_bold(player_ptr, y_depth - 6, x_right - 18, GB_EXTRA_PERM);
93     floor_ptr->grid_array[y_depth - 6][x_right - 18].info |= CAVE_GLOW | CAVE_MARK;
94
95     *start_y = y_height + 5;
96     *start_x = xval;
97     floor_ptr->grid_array[*start_y][*start_x].feat = f_tag_to_index("ARENA_GATE");
98     floor_ptr->grid_array[*start_y][*start_x].info |= CAVE_GLOW | CAVE_MARK;
99 }
100
101 /*!
102  * @brief 挑戦時闘技場への入場処理 / Town logic flow for generation of on_defeat_arena_monster -KMW-
103  * @return なし
104  */
105 static void generate_challenge_arena(player_type *challanger_ptr)
106 {
107     POSITION qy = 0;
108     POSITION qx = 0;
109     floor_type *floor_ptr = challanger_ptr->current_floor_ptr;
110     floor_ptr->height = SCREEN_HGT;
111     floor_ptr->width = SCREEN_WID;
112
113     POSITION y, x;
114     for (y = 0; y < MAX_HGT; y++)
115         for (x = 0; x < MAX_WID; x++) {
116             place_bold(challanger_ptr, y, x, GB_SOLID_PERM);
117             floor_ptr->grid_array[y][x].info |= (CAVE_GLOW | CAVE_MARK);
118         }
119
120     for (y = qy + 1; y < qy + SCREEN_HGT - 1; y++)
121         for (x = qx + 1; x < qx + SCREEN_WID - 1; x++)
122             floor_ptr->grid_array[y][x].feat = feat_floor;
123
124     build_arena(challanger_ptr, &y, &x);
125     player_place(challanger_ptr, y, x);
126     if (place_monster_aux(challanger_ptr, 0, challanger_ptr->y + 5, challanger_ptr->x, arena_info[challanger_ptr->arena_number].r_idx, PM_NO_KAGE | PM_NO_PET))
127         return;
128
129     challanger_ptr->exit_bldg = TRUE;
130     challanger_ptr->arena_number++;
131     msg_print(_("相手は欠場した。あなたの不戦勝だ。", "The enemy is unable to appear. You won by default."));
132 }
133
134 /*!
135  * @brief モンスター闘技場のフロア生成 / Builds the on_defeat_arena_monster after it is entered -KMW-
136  * @param player_ptr プレーヤーへの参照ポインタ
137  * @return なし
138  */
139 static void build_battle(player_type *player_ptr, POSITION *y, POSITION *x)
140 {
141     POSITION yval = SCREEN_HGT / 2;
142     POSITION xval = SCREEN_WID / 2;
143     POSITION y_height = yval - 10;
144     POSITION y_depth = yval + 10;
145     POSITION x_left = xval - 32;
146     POSITION x_right = xval + 32;
147
148     floor_type *floor_ptr = player_ptr->current_floor_ptr;
149     for (int i = y_height; i <= y_height + 5; i++)
150         for (int j = x_left; j <= x_right; j++) {
151             place_bold(player_ptr, i, j, GB_EXTRA_PERM);
152             floor_ptr->grid_array[i][j].info |= (CAVE_GLOW | CAVE_MARK);
153         }
154
155     for (int i = y_depth; i >= y_depth - 3; i--)
156         for (int j = x_left; j <= x_right; j++) {
157             place_bold(player_ptr, i, j, GB_EXTRA_PERM);
158             floor_ptr->grid_array[i][j].info |= (CAVE_GLOW | CAVE_MARK);
159         }
160
161     for (int j = x_left; j <= x_left + 17; j++)
162         for (int i = y_height; i <= y_depth; i++) {
163             place_bold(player_ptr, i, j, GB_EXTRA_PERM);
164             floor_ptr->grid_array[i][j].info |= (CAVE_GLOW | CAVE_MARK);
165         }
166
167     for (int j = x_right; j >= x_right - 17; j--)
168         for (int i = y_height; i <= y_depth; i++) {
169             place_bold(player_ptr, i, j, GB_EXTRA_PERM);
170             floor_ptr->grid_array[i][j].info |= (CAVE_GLOW | CAVE_MARK);
171         }
172
173     place_bold(player_ptr, y_height + 6, x_left + 18, GB_EXTRA_PERM);
174     floor_ptr->grid_array[y_height + 6][x_left + 18].info |= CAVE_GLOW | CAVE_MARK;
175     place_bold(player_ptr, y_depth - 4, x_left + 18, GB_EXTRA_PERM);
176     floor_ptr->grid_array[y_depth - 4][x_left + 18].info |= CAVE_GLOW | CAVE_MARK;
177     place_bold(player_ptr, y_height + 6, x_right - 18, GB_EXTRA_PERM);
178     floor_ptr->grid_array[y_height + 6][x_right - 18].info |= CAVE_GLOW | CAVE_MARK;
179     place_bold(player_ptr, y_depth - 4, x_right - 18, GB_EXTRA_PERM);
180     floor_ptr->grid_array[y_depth - 4][x_right - 18].info |= CAVE_GLOW | CAVE_MARK;
181
182     for (int i = y_height + 1; i <= y_height + 5; i++)
183         for (int j = x_left + 20 + 2 * (y_height + 5 - i); j <= x_right - 20 - 2 * (y_height + 5 - i); j++) {
184             floor_ptr->grid_array[i][j].feat = feat_permanent_glass_wall;
185         }
186
187     POSITION last_y = y_height + 1;
188     POSITION last_x = xval;
189     floor_ptr->grid_array[last_y][last_x].feat = f_tag_to_index("BUILDING_3");
190     floor_ptr->grid_array[last_y][last_x].info |= CAVE_GLOW | CAVE_MARK;
191     *y = last_y;
192     *x = last_x;
193 }
194
195 /*!
196  * @brief モンスター闘技場への導入処理 / Town logic flow for generation of on_defeat_arena_monster -KMW-
197  * @return なし
198  */
199 static void generate_gambling_arena(player_type *creature_ptr)
200 {
201     POSITION y, x;
202     POSITION qy = 0;
203     POSITION qx = 0;
204     floor_type *floor_ptr = creature_ptr->current_floor_ptr;
205     for (y = 0; y < MAX_HGT; y++)
206         for (x = 0; x < MAX_WID; x++) {
207             place_bold(creature_ptr, y, x, GB_SOLID_PERM);
208             floor_ptr->grid_array[y][x].info |= (CAVE_GLOW | CAVE_MARK);
209         }
210
211     for (y = qy + 1; y < qy + SCREEN_HGT - 1; y++)
212         for (x = qx + 1; x < qx + SCREEN_WID - 1; x++)
213             floor_ptr->grid_array[y][x].feat = feat_floor;
214
215     build_battle(creature_ptr, &y, &x);
216     player_place(creature_ptr, y, x);
217     for (MONSTER_IDX i = 0; i < 4; i++) {
218         place_monster_aux(creature_ptr, 0, creature_ptr->y + 8 + (i / 2) * 4, creature_ptr->x - 2 + (i % 2) * 4, battle_mon[i], (PM_NO_KAGE | PM_NO_PET));
219         set_friendly(&floor_ptr->m_list[floor_ptr->grid_array[creature_ptr->y + 8 + (i / 2) * 4][creature_ptr->x - 2 + (i % 2) * 4].m_idx]);
220     }
221
222     for (MONSTER_IDX i = 1; i < floor_ptr->m_max; i++) {
223         monster_type *m_ptr = &floor_ptr->m_list[i];
224         if (!monster_is_valid(m_ptr))
225             continue;
226
227         m_ptr->mflag2 |= MFLAG2_MARK | MFLAG2_SHOW;
228         update_monster(creature_ptr, i, FALSE);
229     }
230 }
231
232 /*!
233  * @brief 固定マップクエストのフロア生成 / Generate a quest level
234  * @param player_ptr プレーヤーへの参照ポインタ
235  * @return なし
236  */
237 static void generate_fixed_floor(player_type *player_ptr)
238 {
239     floor_type *floor_ptr = player_ptr->current_floor_ptr;
240     for (POSITION y = 0; y < floor_ptr->height; y++)
241         for (POSITION x = 0; x < floor_ptr->width; x++)
242             place_bold(player_ptr, y, x, GB_SOLID_PERM);
243
244     floor_ptr->base_level = quest[floor_ptr->inside_quest].level;
245     floor_ptr->dun_level = floor_ptr->base_level;
246     floor_ptr->object_level = floor_ptr->base_level;
247     floor_ptr->monster_level = floor_ptr->base_level;
248     if (record_stair)
249         exe_write_diary(player_ptr, DIARY_TO_QUEST, floor_ptr->inside_quest, NULL);
250
251     get_mon_num_prep(player_ptr, get_monster_hook(player_ptr), NULL);
252     init_flags = INIT_CREATE_DUNGEON;
253     parse_fixed_map(player_ptr, "q_info.txt", 0, 0, MAX_HGT, MAX_WID);
254 }
255
256 /*!
257  * @brief ダンジョン時のランダムフロア生成 / Make a real level
258  * @param player_ptr プレーヤーへの参照ポインタ
259  * @param concptr
260  * @return フロアの生成に成功したらTRUE
261  */
262 static bool level_gen(player_type *player_ptr, concptr *why)
263 {
264     floor_type *floor_ptr = player_ptr->current_floor_ptr;
265     DUNGEON_IDX d_idx = floor_ptr->dungeon_idx;
266     if ((always_small_levels || ironman_small_levels || (one_in_(SMALL_LEVEL) && small_levels) || (d_info[d_idx].flags1 & DF1_BEGINNER)
267             || (d_info[d_idx].flags1 & DF1_SMALLEST))
268         && !(d_info[d_idx].flags1 & DF1_BIG)) {
269         int level_height;
270         int level_width;
271         if (d_info[d_idx].flags1 & DF1_SMALLEST) {
272             level_height = 1;
273             level_width = 1;
274         } else if (d_info[d_idx].flags1 & DF1_BEGINNER) {
275             level_height = 2;
276             level_width = 2;
277         } else {
278             level_height = randint1(MAX_HGT / SCREEN_HGT);
279             level_width = randint1(MAX_WID / SCREEN_WID);
280             bool is_first_level_area = TRUE;
281             bool is_max_area = (level_height == MAX_HGT / SCREEN_HGT) && (level_width == MAX_WID / SCREEN_WID);
282             while (is_first_level_area || is_max_area) {
283                 level_height = randint1(MAX_HGT / SCREEN_HGT);
284                 level_width = randint1(MAX_WID / SCREEN_WID);
285                 is_first_level_area = FALSE;
286                 is_max_area = (level_height == MAX_HGT / SCREEN_HGT) && (level_width == MAX_WID / SCREEN_WID);
287             }
288         }
289
290         floor_ptr->height = level_height * SCREEN_HGT;
291         floor_ptr->width = level_width * SCREEN_WID;
292         panel_row_min = floor_ptr->height;
293         panel_col_min = floor_ptr->width;
294
295         msg_format_wizard(
296             player_ptr, CHEAT_DUNGEON, _("小さなフロア: X:%d, Y:%d", "A 'small' dungeon level: X:%d, Y:%d."), floor_ptr->width, floor_ptr->height);
297     } else {
298         floor_ptr->height = MAX_HGT;
299         floor_ptr->width = MAX_WID;
300         panel_row_min = floor_ptr->height;
301         panel_col_min = floor_ptr->width;
302     }
303
304     return cave_gen(player_ptr, why);
305 }
306
307 /*!
308  * @brief フロアに存在する全マスの記憶状態を初期化する / Wipe all unnecessary flags after grid_array generation
309  * @return なし
310  */
311 void wipe_generate_random_floor_flags(floor_type *floor_ptr)
312 {
313     for (POSITION y = 0; y < floor_ptr->height; y++)
314         for (POSITION x = 0; x < floor_ptr->width; x++)
315             floor_ptr->grid_array[y][x].info &= ~(CAVE_MASK);
316
317     if (floor_ptr->dun_level > 0)
318         for (POSITION y = 1; y < floor_ptr->height - 1; y++)
319             for (POSITION x = 1; x < floor_ptr->width - 1; x++)
320                 floor_ptr->grid_array[y][x].info |= CAVE_UNSAFE;
321 }
322
323 /*!
324  * @brief フロアの全情報を初期化する / Clear and empty floor.
325  * @parama player_ptr プレーヤーへの参照ポインタ
326  * @return なし
327  */
328 void clear_cave(player_type *player_ptr)
329 {
330     floor_type *floor_ptr = player_ptr->current_floor_ptr;
331     (void)C_WIPE(floor_ptr->o_list, floor_ptr->o_max, object_type);
332     floor_ptr->o_max = 1;
333     floor_ptr->o_cnt = 0;
334
335     for (int i = 1; i < max_r_idx; i++)
336         r_info[i].cur_num = 0;
337
338     (void)C_WIPE(floor_ptr->m_list, floor_ptr->m_max, monster_type);
339     floor_ptr->m_max = 1;
340     floor_ptr->m_cnt = 0;
341     for (int i = 0; i < MAX_MTIMED; i++)
342         floor_ptr->mproc_max[i] = 0;
343
344     precalc_cur_num_of_pet(player_ptr);
345     for (POSITION y = 0; y < MAX_HGT; y++) {
346         for (POSITION x = 0; x < MAX_WID; x++) {
347             grid_type *g_ptr = &floor_ptr->grid_array[y][x];
348             g_ptr->info = 0;
349             g_ptr->feat = 0;
350             g_ptr->o_idx = 0;
351             g_ptr->m_idx = 0;
352             g_ptr->special = 0;
353             g_ptr->mimic = 0;
354             g_ptr->cost = 0;
355             g_ptr->dist = 0;
356             g_ptr->when = 0;
357         }
358     }
359
360     floor_ptr->base_level = floor_ptr->dun_level;
361     floor_ptr->monster_level = floor_ptr->base_level;
362     floor_ptr->object_level = floor_ptr->base_level;
363 }
364
365 /*!
366  * ダンジョンのランダムフロアを生成する / Generates a random dungeon level -RAK-
367  * @parama player_ptr プレーヤーへの参照ポインタ
368  * @return なし
369  * @note Hack -- regenerate any "overflow" levels
370  */
371 void generate_floor(player_type *player_ptr)
372 {
373     floor_type *floor_ptr = player_ptr->current_floor_ptr;
374     floor_ptr->dungeon_idx = player_ptr->dungeon_idx;
375     set_floor_and_wall(floor_ptr->dungeon_idx);
376     for (int num = 0; TRUE; num++) {
377         bool okay = TRUE;
378         concptr why = NULL;
379         clear_cave(player_ptr);
380         player_ptr->x = player_ptr->y = 0;
381         if (floor_ptr->inside_arena)
382             generate_challenge_arena(player_ptr);
383         else if (player_ptr->phase_out)
384             generate_gambling_arena(player_ptr);
385         else if (floor_ptr->inside_quest)
386             generate_fixed_floor(player_ptr);
387         else if (!floor_ptr->dun_level)
388             if (player_ptr->wild_mode)
389                 wilderness_gen_small(player_ptr);
390             else
391                 wilderness_gen(player_ptr);
392         else
393             okay = level_gen(player_ptr, &why);
394
395         if (floor_ptr->o_max >= current_world_ptr->max_o_idx) {
396             why = _("アイテムが多すぎる", "too many objects");
397             okay = FALSE;
398         } else if (floor_ptr->m_max >= current_world_ptr->max_m_idx) {
399             why = _("モンスターが多すぎる", "too many monsters");
400             okay = FALSE;
401         }
402
403         if (okay)
404             break;
405
406         if (why)
407             msg_format(_("生成やり直し(%s)", "Generation restarted (%s)"), why);
408
409         wipe_o_list(floor_ptr);
410         wipe_monsters_list(player_ptr);
411     }
412
413     glow_deep_lava_and_bldg(player_ptr);
414     player_ptr->enter_dungeon = FALSE;
415     wipe_generate_random_floor_flags(floor_ptr);
416 }