OSDN Git Service

[Refactor] #3733 optional 型の値取得処理 value() を撤廃した その3
[hengbandforosx/hengbandosx.git] / src / cmd-item / cmd-eat.cpp
1 /*!
2  * @brief プレイヤーの食べるコマンド実装
3  * @date 2018/09/07
4  @ @author deskull
5  */
6
7 #include "cmd-item/cmd-eat.h"
8 #include "avatar/avatar.h"
9 #include "core/window-redrawer.h"
10 #include "flavor/flavor-describer.h"
11 #include "flavor/object-flavor-types.h"
12 #include "floor/floor-object.h"
13 #include "hpmp/hp-mp-processor.h"
14 #include "inventory/inventory-object.h"
15 #include "main/sound-definitions-table.h"
16 #include "main/sound-of-music.h"
17 #include "monster-race/monster-race.h"
18 #include "object-enchant/special-object-flags.h"
19 #include "object-hook/hook-expendable.h"
20 #include "object/item-tester-hooker.h"
21 #include "object/item-use-flags.h"
22 #include "object/object-info.h"
23 #include "object/object-kind-hook.h"
24 #include "perception/object-perception.h"
25 #include "player-base/player-class.h"
26 #include "player-base/player-race.h"
27 #include "player-info/class-info.h"
28 #include "player-info/mimic-info-table.h"
29 #include "player-info/samurai-data-type.h"
30 #include "player-status/player-energy.h"
31 #include "player/attack-defense-types.h"
32 #include "player/digestion-processor.h"
33 #include "player/player-damage.h"
34 #include "player/player-status-flags.h"
35 #include "player/special-defense-types.h"
36 #include "spell-realm/spells-hex.h"
37 #include "spell-realm/spells-song.h"
38 #include "spell/spells-status.h"
39 #include "status/action-setter.h"
40 #include "status/bad-status-setter.h"
41 #include "status/base-status.h"
42 #include "status/element-resistance.h"
43 #include "status/experience.h"
44 #include "sv-definition/sv-food-types.h"
45 #include "sv-definition/sv-other-types.h"
46 #include "system/baseitem-info.h"
47 #include "system/item-entity.h"
48 #include "system/monster-race-info.h"
49 #include "system/player-type-definition.h"
50 #include "system/redrawing-flags-updater.h"
51 #include "util/string-processor.h"
52 #include "view/display-messages.h"
53 #include "view/object-describer.h"
54
55 /*!
56  * @brief 食料タイプの食料を食べたときの効果を発動
57  * @param player_ptr プレイヤー情報への参照ポインタ
58  * @param o_ptr 食べるオブジェクト
59  * @return 鑑定されるならTRUE、されないならFALSE
60  */
61 static bool exe_eat_food_type_object(PlayerType *player_ptr, const BaseitemKey &bi_key)
62 {
63     if (bi_key.tval() != ItemKindType::FOOD) {
64         return false;
65     }
66
67     BadStatusSetter bss(player_ptr);
68     switch (*bi_key.sval()) {
69     case SV_FOOD_POISON:
70         return (!(has_resist_pois(player_ptr) || is_oppose_pois(player_ptr))) && bss.mod_poison(randint0(10) + 10);
71     case SV_FOOD_BLINDNESS:
72         return !has_resist_blind(player_ptr) && bss.mod_blindness(randint0(200) + 200);
73     case SV_FOOD_PARANOIA:
74         return !has_resist_fear(player_ptr) && bss.mod_fear(randint0(10) + 10);
75     case SV_FOOD_CONFUSION:
76         return !has_resist_conf(player_ptr) && bss.mod_confusion(randint0(10) + 10);
77     case SV_FOOD_HALLUCINATION:
78         return !has_resist_chaos(player_ptr) && bss.mod_hallucination(randint0(250) + 250);
79     case SV_FOOD_PARALYSIS:
80         return !player_ptr->free_act && bss.mod_paralysis(randint0(10) + 10);
81     case SV_FOOD_WEAKNESS:
82         take_hit(player_ptr, DAMAGE_NOESCAPE, damroll(6, 6), _("毒入り食料", "poisonous food"));
83         (void)do_dec_stat(player_ptr, A_STR);
84         return true;
85     case SV_FOOD_SICKNESS:
86         take_hit(player_ptr, DAMAGE_NOESCAPE, damroll(6, 6), _("毒入り食料", "poisonous food"));
87         (void)do_dec_stat(player_ptr, A_CON);
88         return true;
89     case SV_FOOD_STUPIDITY:
90         take_hit(player_ptr, DAMAGE_NOESCAPE, damroll(8, 8), _("毒入り食料", "poisonous food"));
91         (void)do_dec_stat(player_ptr, A_INT);
92         return true;
93     case SV_FOOD_NAIVETY:
94         take_hit(player_ptr, DAMAGE_NOESCAPE, damroll(8, 8), _("毒入り食料", "poisonous food"));
95         (void)do_dec_stat(player_ptr, A_WIS);
96         return true;
97     case SV_FOOD_UNHEALTH:
98         take_hit(player_ptr, DAMAGE_NOESCAPE, damroll(10, 10), _("毒入り食料", "poisonous food"));
99         (void)do_dec_stat(player_ptr, A_CON);
100         return true;
101     case SV_FOOD_DISEASE:
102         take_hit(player_ptr, DAMAGE_NOESCAPE, damroll(10, 10), _("毒入り食料", "poisonous food"));
103         (void)do_dec_stat(player_ptr, A_STR);
104         return true;
105     case SV_FOOD_CURE_POISON:
106         return bss.set_poison(0);
107     case SV_FOOD_CURE_BLINDNESS:
108         return bss.set_blindness(0);
109     case SV_FOOD_CURE_PARANOIA:
110         return bss.set_fear(0);
111     case SV_FOOD_CURE_CONFUSION:
112         return bss.set_confusion(0);
113     case SV_FOOD_CURE_SERIOUS:
114         return cure_serious_wounds(player_ptr, 4, 8);
115     case SV_FOOD_RESTORE_STR:
116         return do_res_stat(player_ptr, A_STR);
117     case SV_FOOD_RESTORE_CON:
118         return do_res_stat(player_ptr, A_CON);
119     case SV_FOOD_RESTORING:
120         return restore_all_status(player_ptr);
121     case SV_FOOD_BISCUIT:
122         msg_print(_("甘くてサクサクしてとてもおいしい。", "That is sweet, crispy, and very delicious."));
123         return true;
124     case SV_FOOD_JERKY:
125         msg_print(_("歯ごたえがあっておいしい。", "That is chewy and delicious."));
126         return true;
127     case SV_FOOD_SLIME_MOLD:
128         msg_print(_("これはなんとも形容しがたい味だ。", "That is an indescribable taste."));
129         return true;
130     case SV_FOOD_RATION:
131         msg_print(_("これはおいしい。", "That tastes good."));
132         return true;
133     case SV_FOOD_WAYBREAD:
134         msg_print(_("これはひじょうに美味だ。", "That tastes very good."));
135         (void)bss.set_poison(0);
136         (void)hp_player(player_ptr, damroll(4, 8));
137         return true;
138     case SV_FOOD_PINT_OF_ALE:
139     case SV_FOOD_PINT_OF_WINE:
140         msg_print(_("のどごし爽やかだ。", "That is refreshing and warms the innards."));
141         return true;
142     default:
143         return false;
144     }
145 }
146
147 /*!
148  * @brief 魔法道具のチャージをの食料として食べたときの効果を発動
149  * @param player_ptr プレイヤー情報への参照ポインタ
150  * @param o_ptr 食べるオブジェクト
151  * @param i_idx オブジェクトのインベントリ番号
152  * @return 食べようとしたらTRUE、しなかったらFALSE
153  */
154 static bool exe_eat_charge_of_magic_device(PlayerType *player_ptr, ItemEntity *o_ptr, short i_idx)
155 {
156     if (!o_ptr->is_wand_staff() || (PlayerRace(player_ptr).food() != PlayerRaceFoodType::MANA)) {
157         return false;
158     }
159
160     const auto is_staff = o_ptr->bi_key.tval() == ItemKindType::STAFF;
161     if (is_staff && (i_idx < 0) && (o_ptr->number > 1)) {
162         msg_print(_("まずは杖を拾わなければ。", "You must first pick up the staffs."));
163         return true;
164     }
165
166     const auto staff = is_staff ? _("杖", "staff") : _("魔法棒", "wand");
167
168     auto &rfu = RedrawingFlagsUpdater::get_instance();
169     if (o_ptr->pval == 0) {
170         msg_format(_("この%sにはもう魔力が残っていない。", "The %s has no charges left."), staff);
171         o_ptr->ident |= IDENT_EMPTY;
172         rfu.set_flag(SubWindowRedrawingFlag::INVENTORY);
173         return true;
174     }
175
176     msg_format(_("あなたは%sの魔力をエネルギー源として吸収した。", "You absorb mana of the %s as your energy."), staff);
177
178     /* Use a single charge */
179     o_ptr->pval--;
180
181     /* Eat a charge */
182     set_food(player_ptr, player_ptr->food + 5000);
183
184     /* XXX Hack -- unstack if necessary */
185     if (is_staff && (i_idx >= 0) && (o_ptr->number > 1)) {
186         auto item = *o_ptr;
187
188         /* Modify quantity */
189         item.number = 1;
190
191         /* Restore the charges */
192         o_ptr->pval++;
193
194         /* Unstack the used item */
195         o_ptr->number--;
196         i_idx = store_item_to_inventory(player_ptr, &item);
197         msg_format(_("杖をまとめなおした。", "You unstack your staff."));
198     }
199
200     if (i_idx >= 0) {
201         inven_item_charges(player_ptr->inventory_list[i_idx]);
202     } else {
203         floor_item_charges(player_ptr->current_floor_ptr, 0 - i_idx);
204     }
205
206     static constexpr auto flags = {
207         SubWindowRedrawingFlag::INVENTORY,
208         SubWindowRedrawingFlag::EQUIPMENT,
209     };
210     rfu.set_flags(flags);
211     return true;
212 }
213
214 /*!
215  * @brief 食料を食べるコマンドのサブルーチン
216  * @param i_idx 食べるオブジェクトの所持品ID
217  */
218 void exe_eat_food(PlayerType *player_ptr, INVENTORY_IDX i_idx)
219 {
220     if (music_singing_any(player_ptr)) {
221         stop_singing(player_ptr);
222     }
223
224     SpellHex spell_hex(player_ptr);
225     if (spell_hex.is_spelling_any()) {
226         (void)spell_hex.stop_all_spells();
227     }
228
229     auto *o_ptr = ref_item(player_ptr, i_idx);
230
231     sound(SOUND_EAT);
232
233     PlayerEnergy(player_ptr).set_player_turn_energy(100);
234     const auto lev = o_ptr->get_baseitem().level;
235
236     /* Identity not known yet */
237     const auto &bi_key = o_ptr->bi_key;
238     const auto ident = exe_eat_food_type_object(player_ptr, bi_key);
239
240     /*
241      * Store what may have to be updated for the inventory (including
242      * autodestroy if set by something else).  Then turn off those flags
243      * so that updates triggered by calling gain_exp() or set_food() below
244      * do not rearrange the inventory before the food item is destroyed in
245      * the pack.
246      */
247     auto &rfu = RedrawingFlagsUpdater::get_instance();
248     using Srf = StatusRecalculatingFlag;
249     EnumClassFlagGroup<Srf> flags_srf = { Srf::COMBINATION, Srf::REORDER };
250     if (rfu.has(Srf::AUTO_DESTRUCTION)) {
251         flags_srf.set(Srf::AUTO_DESTRUCTION);
252     }
253
254     rfu.reset_flags(flags_srf);
255     if (!(o_ptr->is_aware())) {
256         chg_virtue(player_ptr, Virtue::KNOWLEDGE, -1);
257         chg_virtue(player_ptr, Virtue::PATIENCE, -1);
258         chg_virtue(player_ptr, Virtue::CHANCE, 1);
259     }
260
261     /* We have tried it */
262     const auto tval = bi_key.tval();
263     if (tval == ItemKindType::FOOD) {
264         o_ptr->mark_as_tried();
265     }
266
267     /* The player is now aware of the object */
268     if (ident && !o_ptr->is_aware()) {
269         object_aware(player_ptr, o_ptr);
270         gain_exp(player_ptr, (lev + (player_ptr->lev >> 1)) / player_ptr->lev);
271     }
272
273     static constexpr auto flags_swrf = {
274         SubWindowRedrawingFlag::INVENTORY,
275         SubWindowRedrawingFlag::EQUIPMENT,
276         SubWindowRedrawingFlag::PLAYER,
277     };
278     rfu.set_flags(flags_swrf);
279
280     /* Undeads drain recharge of magic device */
281     if (exe_eat_charge_of_magic_device(player_ptr, o_ptr, i_idx)) {
282         rfu.set_flags(flags_srf);
283         return;
284     }
285
286     auto food_type = PlayerRace(player_ptr).food();
287
288     /* Balrogs change humanoid corpses to energy */
289     const auto corpse_r_idx = i2enum<MonsterRaceId>(o_ptr->pval);
290     const auto search = angband_strchr("pht", monraces_info[corpse_r_idx].d_char);
291     if (food_type == PlayerRaceFoodType::CORPSE && (bi_key == BaseitemKey(ItemKindType::CORPSE, SV_CORPSE)) && (search != nullptr)) {
292         const auto item_name = describe_flavor(player_ptr, o_ptr, (OD_OMIT_PREFIX | OD_NAME_ONLY));
293         msg_format(_("%sは燃え上り灰になった。精力を吸収した気がする。", "%s^ is burnt to ashes.  You absorb its vitality!"), item_name.data());
294         (void)set_food(player_ptr, PY_FOOD_MAX - 1);
295
296         rfu.set_flags(flags_srf);
297         vary_item(player_ptr, i_idx, -1);
298         return;
299     }
300
301     if (PlayerRace(player_ptr).equals(PlayerRaceType::SKELETON)) {
302         const auto sval = bi_key.sval();
303         if ((sval != SV_FOOD_WAYBREAD) && (sval >= SV_FOOD_BISCUIT)) {
304             ItemEntity forge;
305             auto *q_ptr = &forge;
306
307             msg_print(_("食べ物がアゴを素通りして落ちた!", "The food falls through your jaws!"));
308             q_ptr->prep(lookup_baseitem_id(bi_key));
309
310             /* Drop the object from heaven */
311             (void)drop_near(player_ptr, q_ptr, -1, player_ptr->y, player_ptr->x);
312         } else {
313             msg_print(_("食べ物がアゴを素通りして落ち、消えた!", "The food falls through your jaws and vanishes!"));
314         }
315     } else if (food_type == PlayerRaceFoodType::BLOOD) {
316         /* Vampires are filled only by bloods, so reduced nutritional benefit */
317         (void)set_food(player_ptr, player_ptr->food + (o_ptr->pval / 10));
318         msg_print(_("あなたのような者にとって食糧など僅かな栄養にしかならない。", "Mere victuals hold scant sustenance for a being such as yourself."));
319
320         if (player_ptr->food < PY_FOOD_ALERT) { /* Hungry */
321             msg_print(_("あなたの飢えは新鮮な血によってのみ満たされる!", "Your hunger can only be satisfied with fresh blood!"));
322         }
323     } else if (food_type == PlayerRaceFoodType::WATER) {
324         msg_print(_("動物の食物はあなたにとってほとんど栄養にならない。", "The food of animals is poor sustenance for you."));
325         set_food(player_ptr, player_ptr->food + ((o_ptr->pval) / 20));
326     } else if (food_type != PlayerRaceFoodType::RATION) {
327         msg_print(_("生者の食物はあなたにとってほとんど栄養にならない。", "The food of mortals is poor sustenance for you."));
328         set_food(player_ptr, player_ptr->food + ((o_ptr->pval) / 20));
329     } else {
330         if (bi_key == BaseitemKey(ItemKindType::FOOD, SV_FOOD_WAYBREAD)) {
331             /* Waybread is always fully satisfying. */
332             set_food(player_ptr, std::max<short>(player_ptr->food, PY_FOOD_MAX - 1));
333         } else {
334             /* Food can feed the player */
335             (void)set_food(player_ptr, player_ptr->food + o_ptr->pval);
336         }
337     }
338
339     rfu.set_flags(flags_srf);
340     vary_item(player_ptr, i_idx, -1);
341 }
342
343 /*!
344  * @brief 食料を食べるコマンドのメインルーチン /
345  * Eat some food (from the pack or floor)
346  */
347 void do_cmd_eat_food(PlayerType *player_ptr)
348 {
349     PlayerClass(player_ptr).break_samurai_stance({ SamuraiStanceType::MUSOU, SamuraiStanceType::KOUKIJIN });
350     constexpr auto q = _("どれを食べますか? ", "Eat which item? ");
351     constexpr auto s = _("食べ物がない。", "You have nothing to eat.");
352     short i_idx;
353     if (!choose_object(player_ptr, &i_idx, q, s, (USE_INVEN | USE_FLOOR), FuncItemTester(item_tester_hook_eatable, player_ptr))) {
354         return;
355     }
356
357     exe_eat_food(player_ptr, i_idx);
358 }