OSDN Git Service

[Refactor] #40571 Moved targeting.c/h from io/ to target/
[hengband/hengband.git] / src / spell-kind / spells-world.c
1 #include "spell-kind/spells-world.h"
2 #include "cmd-io/cmd-save.h"
3 #include "core/asking-player.h"
4 #include "core/player-redraw-types.h"
5 #include "dungeon/dungeon.h"
6 #include "dungeon/quest.h"
7 #include "floor/cave.h"
8 #include "floor/floor-save.h"
9 #include "floor/floor-town.h"
10 #include "floor/floor.h"
11 #include "floor/geometry.h"
12 #include "floor/wild.h"
13 #include "game-option/birth-options.h"
14 #include "game-option/play-record-options.h"
15 #include "game-option/special-options.h"
16 #include "grid/grid.h"
17 #include "io/input-key-acceptor.h"
18 #include "io/write-diary.h"
19 #include "main/sound-definitions-table.h"
20 #include "main/sound-of-music.h"
21 #include "market/building-util.h"
22 #include "monster-floor/monster-remover.h"
23 #include "monster-race/monster-race.h"
24 #include "monster-race/race-flags-resistance.h"
25 #include "monster-race/race-flags1.h"
26 #include "monster/monster-describer.h"
27 #include "monster/monster-description-types.h"
28 #include "monster/monster-info.h"
29 #include "system/floor-type-definition.h"
30 #include "system/monster-type-definition.h"
31 #include "target/targeting.h"
32 #include "term/screen-processor.h"
33 #include "util/int-char-converter.h"
34 #include "view/display-messages.h"
35 #include "world/world.h"
36
37 /*!
38  * todo 変数名が実態と合っているかどうかは要確認
39  * テレポート・レベルが効かないモンスターであるかどうかを判定する
40  * @param caster_ptr プレーヤーへの参照ポインタ
41  * @param idx テレポート・レベル対象のモンスター
42  */
43 bool is_teleport_level_ineffective(player_type *caster_ptr, MONSTER_IDX idx)
44 {
45     floor_type *floor_ptr = caster_ptr->current_floor_ptr;
46     bool is_special_floor
47         = floor_ptr->inside_arena || caster_ptr->phase_out || (floor_ptr->inside_quest && !random_quest_number(caster_ptr, floor_ptr->dun_level));
48     bool is_invalid_floor = idx <= 0;
49     is_invalid_floor &= quest_number(caster_ptr, floor_ptr->dun_level) || (floor_ptr->dun_level >= d_info[caster_ptr->dungeon_idx].maxdepth);
50     is_invalid_floor &= caster_ptr->current_floor_ptr->dun_level >= 1;
51     is_invalid_floor &= ironman_downward;
52     return is_special_floor || is_invalid_floor;
53 }
54
55 /*!
56  * todo cmd-save.h への依存あり。コールバックで何とかしたい
57  * @brief プレイヤー及びモンスターをレベルテレポートさせる /
58  * Teleport the player one level up or down (random when legal)
59  * @param creature_ptr プレーヤーへの参照ポインタ
60  * @param m_idx テレポートの対象となるモンスターID(0ならばプレイヤー) / If m_idx <= 0, target is player.
61  * @return なし
62  */
63 void teleport_level(player_type *creature_ptr, MONSTER_IDX m_idx)
64 {
65     GAME_TEXT m_name[160];
66     bool see_m = TRUE;
67     if (m_idx <= 0) {
68         strcpy(m_name, _("あなた", "you"));
69     } else {
70         monster_type *m_ptr = &creature_ptr->current_floor_ptr->m_list[m_idx];
71         monster_desc(creature_ptr, m_name, m_ptr, 0);
72         see_m = is_seen(creature_ptr, m_ptr);
73     }
74
75     if (is_teleport_level_ineffective(creature_ptr, m_idx)) {
76         if (see_m)
77             msg_print(_("効果がなかった。", "There is no effect."));
78         return;
79     }
80
81     if ((m_idx <= 0) && creature_ptr->anti_tele) {
82         msg_print(_("不思議な力がテレポートを防いだ!", "A mysterious force prevents you from teleporting!"));
83         return;
84     }
85
86     bool go_up;
87     if (randint0(100) < 50)
88         go_up = TRUE;
89     else
90         go_up = FALSE;
91
92     if ((m_idx <= 0) && current_world_ptr->wizard) {
93         if (get_check("Force to go up? "))
94             go_up = TRUE;
95         else if (get_check("Force to go down? "))
96             go_up = FALSE;
97     }
98
99     if ((ironman_downward && (m_idx <= 0)) || (creature_ptr->current_floor_ptr->dun_level <= d_info[creature_ptr->dungeon_idx].mindepth)) {
100 #ifdef JP
101         if (see_m)
102             msg_format("%^sは床を突き破って沈んでいく。", m_name);
103 #else
104         if (see_m)
105             msg_format("%^s sink%s through the floor.", m_name, (m_idx <= 0) ? "" : "s");
106 #endif
107         if (m_idx <= 0) {
108             if (!creature_ptr->current_floor_ptr->dun_level) {
109                 creature_ptr->dungeon_idx = ironman_downward ? DUNGEON_ANGBAND : creature_ptr->recall_dungeon;
110                 creature_ptr->oldpy = creature_ptr->y;
111                 creature_ptr->oldpx = creature_ptr->x;
112             }
113
114             if (record_stair)
115                 exe_write_diary(creature_ptr, DIARY_TELEPORT_LEVEL, 1, NULL);
116
117             if (autosave_l)
118                 do_cmd_save_game(creature_ptr, TRUE);
119
120             if (!creature_ptr->current_floor_ptr->dun_level) {
121                 creature_ptr->current_floor_ptr->dun_level = d_info[creature_ptr->dungeon_idx].mindepth;
122                 prepare_change_floor_mode(creature_ptr, CFM_RAND_PLACE);
123             } else {
124                 prepare_change_floor_mode(creature_ptr, CFM_SAVE_FLOORS | CFM_DOWN | CFM_RAND_PLACE | CFM_RAND_CONNECT);
125             }
126
127             creature_ptr->leaving = TRUE;
128         }
129     } else if (quest_number(creature_ptr, creature_ptr->current_floor_ptr->dun_level)
130         || (creature_ptr->current_floor_ptr->dun_level >= d_info[creature_ptr->dungeon_idx].maxdepth)) {
131 #ifdef JP
132         if (see_m)
133             msg_format("%^sは天井を突き破って宙へ浮いていく。", m_name);
134 #else
135         if (see_m)
136             msg_format("%^s rise%s up through the ceiling.", m_name, (m_idx <= 0) ? "" : "s");
137 #endif
138
139         if (m_idx <= 0) {
140             if (record_stair)
141                 exe_write_diary(creature_ptr, DIARY_TELEPORT_LEVEL, -1, NULL);
142
143             if (autosave_l)
144                 do_cmd_save_game(creature_ptr, TRUE);
145
146             prepare_change_floor_mode(creature_ptr, CFM_SAVE_FLOORS | CFM_UP | CFM_RAND_PLACE | CFM_RAND_CONNECT);
147
148             leave_quest_check(creature_ptr);
149             creature_ptr->current_floor_ptr->inside_quest = 0;
150             creature_ptr->leaving = TRUE;
151         }
152     } else if (go_up) {
153 #ifdef JP
154         if (see_m)
155             msg_format("%^sは天井を突き破って宙へ浮いていく。", m_name);
156 #else
157         if (see_m)
158             msg_format("%^s rise%s up through the ceiling.", m_name, (m_idx <= 0) ? "" : "s");
159 #endif
160
161         if (m_idx <= 0) {
162             if (record_stair)
163                 exe_write_diary(creature_ptr, DIARY_TELEPORT_LEVEL, -1, NULL);
164
165             if (autosave_l)
166                 do_cmd_save_game(creature_ptr, TRUE);
167
168             prepare_change_floor_mode(creature_ptr, CFM_SAVE_FLOORS | CFM_UP | CFM_RAND_PLACE | CFM_RAND_CONNECT);
169             creature_ptr->leaving = TRUE;
170         }
171     } else {
172 #ifdef JP
173         if (see_m)
174             msg_format("%^sは床を突き破って沈んでいく。", m_name);
175 #else
176         if (see_m)
177             msg_format("%^s sink%s through the floor.", m_name, (m_idx <= 0) ? "" : "s");
178 #endif
179
180         if (m_idx <= 0) {
181             if (record_stair)
182                 exe_write_diary(creature_ptr, DIARY_TELEPORT_LEVEL, 1, NULL);
183             if (autosave_l)
184                 do_cmd_save_game(creature_ptr, TRUE);
185
186             prepare_change_floor_mode(creature_ptr, CFM_SAVE_FLOORS | CFM_DOWN | CFM_RAND_PLACE | CFM_RAND_CONNECT);
187             creature_ptr->leaving = TRUE;
188         }
189     }
190
191     if (m_idx <= 0) {
192         sound(SOUND_TPLEVEL);
193         return;
194     }
195
196     monster_type *m_ptr = &creature_ptr->current_floor_ptr->m_list[m_idx];
197     check_quest_completion(creature_ptr, m_ptr);
198     if (record_named_pet && is_pet(m_ptr) && m_ptr->nickname) {
199         char m2_name[MAX_NLEN];
200
201         monster_desc(creature_ptr, m2_name, m_ptr, MD_INDEF_VISIBLE);
202         exe_write_diary(creature_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_TELE_LEVEL, m2_name);
203     }
204
205     delete_monster_idx(creature_ptr, m_idx);
206     sound(SOUND_TPLEVEL);
207 }
208
209 bool teleport_level_other(player_type *caster_ptr)
210 {
211     if (!target_set(caster_ptr, TARGET_KILL))
212         return FALSE;
213     MONSTER_IDX target_m_idx = caster_ptr->current_floor_ptr->grid_array[target_row][target_col].m_idx;
214     if (!target_m_idx)
215         return TRUE;
216     if (!player_has_los_bold(caster_ptr, target_row, target_col))
217         return TRUE;
218     if (!projectable(caster_ptr, caster_ptr->y, caster_ptr->x, target_row, target_col))
219         return TRUE;
220
221     monster_type *m_ptr;
222     monster_race *r_ptr;
223     m_ptr = &caster_ptr->current_floor_ptr->m_list[target_m_idx];
224     r_ptr = &r_info[m_ptr->r_idx];
225     GAME_TEXT m_name[MAX_NLEN];
226     monster_desc(caster_ptr, m_name, m_ptr, 0);
227     msg_format(_("%^sの足を指さした。", "You gesture at %^s's feet."), m_name);
228
229     if ((r_ptr->flagsr & (RFR_EFF_RES_NEXU_MASK | RFR_RES_TELE)) || (r_ptr->flags1 & RF1_QUESTOR)
230         || (r_ptr->level + randint1(50) > caster_ptr->lev + randint1(60))) {
231         msg_format(_("しかし効果がなかった!", "%^s is unaffected!"), m_name);
232     } else {
233         teleport_level(caster_ptr, target_m_idx);
234     }
235
236     return TRUE;
237 }
238
239 /*!
240  * @brief 町間のテレポートを行うメインルーチン
241  * @param caster_ptr プレーヤーへの参照ポインタ
242  * @return テレポート処理を決定したか否か
243  */
244 bool tele_town(player_type *caster_ptr)
245 {
246     if (caster_ptr->current_floor_ptr->dun_level) {
247         msg_print(_("この魔法は地上でしか使えない!", "This spell can only be used on the surface!"));
248         return FALSE;
249     }
250
251     if (caster_ptr->current_floor_ptr->inside_arena || caster_ptr->phase_out) {
252         msg_print(_("この魔法は外でしか使えない!", "This spell can only be used outside!"));
253         return FALSE;
254     }
255
256     screen_save();
257     clear_bldg(4, 10);
258
259     int i;
260     int num = 0;
261     for (i = 1; i < max_towns; i++) {
262         char buf[80];
263
264         if ((i == NO_TOWN) || (i == SECRET_TOWN) || (i == caster_ptr->town_num) || !(caster_ptr->visit & (1L << (i - 1))))
265             continue;
266
267         sprintf(buf, "%c) %-20s", I2A(i - 1), town_info[i].name);
268         prt(buf, 5 + i, 5);
269         num++;
270     }
271
272     if (num == 0) {
273         msg_print(_("まだ行けるところがない。", "You have not yet visited any town."));
274         msg_print(NULL);
275         screen_load();
276         return FALSE;
277     }
278
279     prt(_("どこに行きますか:", "Where do you want to go: "), 0, 0);
280     while (TRUE) {
281         i = inkey();
282
283         if (i == ESCAPE) {
284             screen_load();
285             return FALSE;
286         }
287
288         else if ((i < 'a') || (i > ('a' + max_towns - 2)))
289             continue;
290         else if (((i - 'a' + 1) == caster_ptr->town_num) || ((i - 'a' + 1) == NO_TOWN) || ((i - 'a' + 1) == SECRET_TOWN)
291             || !(caster_ptr->visit & (1L << (i - 'a'))))
292             continue;
293         break;
294     }
295
296     for (POSITION y = 0; y < current_world_ptr->max_wild_y; y++) {
297         for (POSITION x = 0; x < current_world_ptr->max_wild_x; x++) {
298             if (wilderness[y][x].town == (i - 'a' + 1)) {
299                 caster_ptr->wilderness_y = y;
300                 caster_ptr->wilderness_x = x;
301             }
302         }
303     }
304
305     caster_ptr->leaving = TRUE;
306     caster_ptr->leave_bldg = TRUE;
307     caster_ptr->teleport_town = TRUE;
308     screen_load();
309     return TRUE;
310 }
311
312 /*!
313  * @brief 現実変容処理
314  * @param caster_ptr プレーヤーへの参照ポインタ
315  * @return なし
316  */
317 void reserve_alter_reality(player_type *caster_ptr)
318 {
319     if (caster_ptr->current_floor_ptr->inside_arena || ironman_downward) {
320         msg_print(_("何も起こらなかった。", "Nothing happens."));
321         return;
322     }
323
324     if (caster_ptr->alter_reality) {
325         caster_ptr->alter_reality = 0;
326         msg_print(_("景色が元に戻った...", "The view around you returns to normal..."));
327         caster_ptr->redraw |= PR_STATUS;
328         return;
329     }
330
331     TIME_EFFECT turns = randint0(21) + 15;
332     caster_ptr->alter_reality = turns;
333     msg_print(_("回りの景色が変わり始めた...", "The view around you begins to change..."));
334     caster_ptr->redraw |= PR_STATUS;
335 }
336
337 /*!
338  * @brief プレイヤーの帰還発動及び中止処理 /
339  * Recall the player to town or dungeon
340  * @param creature_ptr プレーヤーへの参照ポインタ
341  * @param turns 発動までのターン数
342  * @return 常にTRUEを返す
343  */
344 bool recall_player(player_type *creature_ptr, TIME_EFFECT turns)
345 {
346     /*
347      * TODO: Recall the player to the last
348      * visited town when in the wilderness
349      */
350     if (creature_ptr->current_floor_ptr->inside_arena || ironman_downward) {
351         msg_print(_("何も起こらなかった。", "Nothing happens."));
352         return TRUE;
353     }
354
355     bool is_special_floor = creature_ptr->current_floor_ptr->dun_level > 0;
356     is_special_floor &= max_dlv[creature_ptr->dungeon_idx] > creature_ptr->current_floor_ptr->dun_level;
357     is_special_floor &= !creature_ptr->current_floor_ptr->inside_quest;
358     is_special_floor &= !creature_ptr->word_recall;
359     if (is_special_floor) {
360         if (get_check(_("ここは最深到達階より浅い階です。この階に戻って来ますか? ", "Reset recall depth? "))) {
361             max_dlv[creature_ptr->dungeon_idx] = creature_ptr->current_floor_ptr->dun_level;
362             if (record_maxdepth)
363                 exe_write_diary(creature_ptr, DIARY_TRUMP, creature_ptr->dungeon_idx, _("帰還のときに", "when recalled from dungeon"));
364         }
365     }
366
367     if (creature_ptr->word_recall) {
368         creature_ptr->word_recall = 0;
369         msg_print(_("張りつめた大気が流れ去った...", "A tension leaves the air around you..."));
370         creature_ptr->redraw |= (PR_STATUS);
371         return TRUE;
372     }
373
374     if (!creature_ptr->current_floor_ptr->dun_level) {
375         DUNGEON_IDX select_dungeon;
376         select_dungeon = choose_dungeon(_("に帰還", "recall"), 2, 14);
377         if (!select_dungeon)
378             return FALSE;
379         creature_ptr->recall_dungeon = select_dungeon;
380     }
381
382     creature_ptr->word_recall = turns;
383     msg_print(_("回りの大気が張りつめてきた...", "The air about you becomes charged..."));
384     creature_ptr->redraw |= (PR_STATUS);
385     return TRUE;
386 }
387
388 bool free_level_recall(player_type *creature_ptr)
389 {
390     DUNGEON_IDX select_dungeon = choose_dungeon(_("にテレポート", "teleport"), 4, 0);
391     if (!select_dungeon)
392         return FALSE;
393
394     DEPTH max_depth = d_info[select_dungeon].maxdepth;
395     if (select_dungeon == DUNGEON_ANGBAND) {
396         if (quest[QUEST_OBERON].status != QUEST_STATUS_FINISHED)
397             max_depth = 98;
398         else if (quest[QUEST_SERPENT].status != QUEST_STATUS_FINISHED)
399             max_depth = 99;
400     }
401
402     QUANTITY amt = get_quantity(
403         format(_("%sの何階にテレポートしますか?", "Teleport to which level of %s? "), d_name + d_info[select_dungeon].name), (QUANTITY)max_depth);
404     if (amt <= 0) {
405         return FALSE;
406     }
407
408     creature_ptr->word_recall = 1;
409     creature_ptr->recall_dungeon = select_dungeon;
410     max_dlv[creature_ptr->recall_dungeon]
411         = ((amt > d_info[select_dungeon].maxdepth) ? d_info[select_dungeon].maxdepth
412                                                    : ((amt < d_info[select_dungeon].mindepth) ? d_info[select_dungeon].mindepth : amt));
413     if (record_maxdepth)
414         exe_write_diary(creature_ptr, DIARY_TRUMP, select_dungeon, _("トランプタワーで", "at Trump Tower"));
415
416     msg_print(_("回りの大気が張りつめてきた...", "The air about you becomes charged..."));
417
418     creature_ptr->redraw |= PR_STATUS;
419     return TRUE;
420 }
421
422 /*!
423  * @brief フロア・リセット処理
424  * @param caster_ptr プレーヤーへの参照ポインタ
425  * @return リセット処理が実際に行われたらTRUEを返す
426  */
427 bool reset_recall(player_type *caster_ptr)
428 {
429     int select_dungeon, dummy = 0;
430     char ppp[80];
431     char tmp_val[160];
432
433     select_dungeon = choose_dungeon(_("をセット", "reset"), 2, 14);
434     if (ironman_downward) {
435         msg_print(_("何も起こらなかった。", "Nothing happens."));
436         return TRUE;
437     }
438
439     if (!select_dungeon)
440         return FALSE;
441     sprintf(ppp, _("何階にセットしますか (%d-%d):", "Reset to which level (%d-%d): "), (int)d_info[select_dungeon].mindepth, (int)max_dlv[select_dungeon]);
442     sprintf(tmp_val, "%d", (int)MAX(caster_ptr->current_floor_ptr->dun_level, 1));
443
444     if (!get_string(ppp, tmp_val, 10)) {
445         return FALSE;
446     }
447
448     dummy = atoi(tmp_val);
449     if (dummy < 1)
450         dummy = 1;
451     if (dummy > max_dlv[select_dungeon])
452         dummy = max_dlv[select_dungeon];
453     if (dummy < d_info[select_dungeon].mindepth)
454         dummy = d_info[select_dungeon].mindepth;
455
456     max_dlv[select_dungeon] = dummy;
457
458     if (record_maxdepth)
459         exe_write_diary(caster_ptr, DIARY_TRUMP, select_dungeon, _("フロア・リセットで", "using a scroll of reset recall"));
460 #ifdef JP
461     msg_format("%sの帰還レベルを %d 階にセット。", d_name + d_info[select_dungeon].name, dummy, dummy * 50);
462 #else
463     msg_format("Recall depth set to level %d (%d').", dummy, dummy * 50);
464 #endif
465     return TRUE;
466 }