OSDN Git Service

[Refactor] #1558 Replaced set_confused() to confusion() and transferred it into the...
[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/player-update-types.h"
10 #include "core/window-redrawer.h"
11 #include "flavor/flavor-describer.h"
12 #include "flavor/object-flavor-types.h"
13 #include "floor/floor-object.h"
14 #include "hpmp/hp-mp-processor.h"
15 #include "inventory/inventory-object.h"
16 #include "main/sound-definitions-table.h"
17 #include "main/sound-of-music.h"
18 #include "monster-race/monster-race.h"
19 #include "object-enchant/special-object-flags.h"
20 #include "object-hook/hook-expendable.h"
21 #include "object/item-tester-hooker.h"
22 #include "object/item-use-flags.h"
23 #include "object/object-info.h"
24 #include "object/object-kind-hook.h"
25 #include "object/object-kind.h"
26 #include "perception/object-perception.h"
27 #include "player-base/player-race.h"
28 #include "player-info/class-info.h"
29 #include "player-info/mimic-info-table.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/monster-race-definition.h"
47 #include "system/object-type-definition.h"
48 #include "system/player-type-definition.h"
49 #include "util/string-processor.h"
50 #include "view/display-messages.h"
51 #include "view/object-describer.h"
52
53 /*!
54  * @brief 食料タイプの食料を食べたときの効果を発動
55  * @param player_ptr プレイヤー情報への参照ポインタ
56  * @param o_ptr 食べるオブジェクト
57  * @return 鑑定されるならTRUE、されないならFALSE
58  */
59 bool exe_eat_food_type_object(player_type *player_ptr, object_type *o_ptr)
60 {
61     if (o_ptr->tval != TV_FOOD)
62         return false;
63
64     BadStatusSetter bss(player_ptr);
65     switch (o_ptr->sval) {
66     case SV_FOOD_POISON:
67         if (!(has_resist_pois(player_ptr) || is_oppose_pois(player_ptr)))
68             if (set_poisoned(player_ptr, player_ptr->poisoned + randint0(10) + 10))
69                 return true;
70         break;
71     case SV_FOOD_BLINDNESS:
72         if (!has_resist_blind(player_ptr))
73             if (bss.blindness(player_ptr->blind + randint0(200) + 200))
74                 return true;
75         break;
76     case SV_FOOD_PARANOIA:
77         if (!has_resist_fear(player_ptr))
78             if (set_afraid(player_ptr, player_ptr->afraid + randint0(10) + 10))
79                 return true;
80         break;
81     case SV_FOOD_CONFUSION:
82         if (!has_resist_conf(player_ptr))
83             if (bss.confusion(player_ptr->confused + randint0(10) + 10))
84                 return true;
85         break;
86     case SV_FOOD_HALLUCINATION:
87         if (!has_resist_chaos(player_ptr))
88             if (set_image(player_ptr, player_ptr->image + randint0(250) + 250))
89                 return true;
90         break;
91     case SV_FOOD_PARALYSIS:
92         if (!player_ptr->free_act)
93             if (set_paralyzed(player_ptr, player_ptr->paralyzed + randint0(10) + 10))
94                 return true;
95         break;
96     case SV_FOOD_WEAKNESS:
97         take_hit(player_ptr, DAMAGE_NOESCAPE, damroll(6, 6), _("毒入り食料", "poisonous food"));
98         (void)do_dec_stat(player_ptr, A_STR);
99         return true;
100     case SV_FOOD_SICKNESS:
101         take_hit(player_ptr, DAMAGE_NOESCAPE, damroll(6, 6), _("毒入り食料", "poisonous food"));
102         (void)do_dec_stat(player_ptr, A_CON);
103         return true;
104     case SV_FOOD_STUPIDITY:
105         take_hit(player_ptr, DAMAGE_NOESCAPE, damroll(8, 8), _("毒入り食料", "poisonous food"));
106         (void)do_dec_stat(player_ptr, A_INT);
107         return true;
108     case SV_FOOD_NAIVETY:
109         take_hit(player_ptr, DAMAGE_NOESCAPE, damroll(8, 8), _("毒入り食料", "poisonous food"));
110         (void)do_dec_stat(player_ptr, A_WIS);
111         return true;
112     case SV_FOOD_UNHEALTH:
113         take_hit(player_ptr, DAMAGE_NOESCAPE, damroll(10, 10), _("毒入り食料", "poisonous food"));
114         (void)do_dec_stat(player_ptr, A_CON);
115         return true;
116     case SV_FOOD_DISEASE:
117         take_hit(player_ptr, DAMAGE_NOESCAPE, damroll(10, 10), _("毒入り食料", "poisonous food"));
118         (void)do_dec_stat(player_ptr, A_STR);
119         return true;
120     case SV_FOOD_CURE_POISON:
121         if (set_poisoned(player_ptr, 0))
122             return true;
123         break;
124     case SV_FOOD_CURE_BLINDNESS:
125         return bss.blindness(0);
126     case SV_FOOD_CURE_PARANOIA:
127         if (set_afraid(player_ptr, 0))
128             return true;
129         break;
130     case SV_FOOD_CURE_CONFUSION:
131         return bss.confusion(0);
132     case SV_FOOD_CURE_SERIOUS:
133         return cure_serious_wounds(player_ptr, 4, 8);
134     case SV_FOOD_RESTORE_STR:
135         if (do_res_stat(player_ptr, A_STR))
136             return true;
137         break;
138     case SV_FOOD_RESTORE_CON:
139         if (do_res_stat(player_ptr, A_CON))
140             return true;
141         break;
142     case SV_FOOD_RESTORING:
143         return restore_all_status(player_ptr);
144 #ifdef JP
145     /* それぞれの食べ物の感想をオリジナルより細かく表現 */
146     case SV_FOOD_BISCUIT:
147         msg_print("甘くてサクサクしてとてもおいしい。");
148         return true;
149     case SV_FOOD_JERKY:
150         msg_print("歯ごたえがあっておいしい。");
151         return true;
152     case SV_FOOD_SLIME_MOLD:
153         msg_print("これはなんとも形容しがたい味だ。");
154         return true;
155     case SV_FOOD_RATION:
156         msg_print("これはおいしい。");
157         return true;
158 #else
159     case SV_FOOD_RATION:
160     case SV_FOOD_BISCUIT:
161     case SV_FOOD_JERKY:
162     case SV_FOOD_SLIME_MOLD:
163         msg_print("That tastes good.");
164         return true;
165 #endif
166     case SV_FOOD_WAYBREAD:
167         msg_print(_("これはひじょうに美味だ。", "That tastes good."));
168         (void)set_poisoned(player_ptr, 0);
169         (void)hp_player(player_ptr, damroll(4, 8));
170         return true;
171     case SV_FOOD_PINT_OF_ALE:
172     case SV_FOOD_PINT_OF_WINE:
173         msg_print(_("のどごし爽やかだ。", "That tastes good."));
174         return true;
175     }
176
177     return false;
178 }
179
180 /*!
181  * @brief 魔法道具のチャージをの食料として食べたときの効果を発動
182  * @param player_ptr プレイヤー情報への参照ポインタ
183  * @param o_ptr 食べるオブジェクト
184  * @param item オブジェクトのインベントリ番号
185  * @return 食べようとしたらTRUE、しなかったらFALSE
186  */
187 bool exe_eat_charge_of_magic_device(player_type *player_ptr, object_type *o_ptr, INVENTORY_IDX item)
188 {
189     if (o_ptr->tval != TV_STAFF && o_ptr->tval != TV_WAND)
190         return false;
191
192     if (player_race_food(player_ptr) == PlayerRaceFood::MANA) {
193         concptr staff;
194
195         if (o_ptr->tval == TV_STAFF && (item < 0) && (o_ptr->number > 1)) {
196             msg_print(_("まずは杖を拾わなければ。", "You must first pick up the staffs."));
197             return true;
198         }
199
200         staff = (o_ptr->tval == TV_STAFF) ? _("杖", "staff") : _("魔法棒", "wand");
201
202         /* "Eat" charges */
203         if (o_ptr->pval == 0) {
204             msg_format(_("この%sにはもう魔力が残っていない。", "The %s has no charges left."), staff);
205             o_ptr->ident |= (IDENT_EMPTY);
206             player_ptr->window_flags |= (PW_INVEN);
207             return true;
208         }
209
210         msg_format(_("あなたは%sの魔力をエネルギー源として吸収した。", "You absorb mana of the %s as your energy."), staff);
211
212         /* Use a single charge */
213         o_ptr->pval--;
214
215         /* Eat a charge */
216         set_food(player_ptr, player_ptr->food + 5000);
217
218         /* XXX Hack -- unstack if necessary */
219         if (o_ptr->tval == TV_STAFF && (item >= 0) && (o_ptr->number > 1)) {
220             object_type forge;
221             object_type *q_ptr;
222             q_ptr = &forge;
223             q_ptr->copy_from(o_ptr);
224
225             /* Modify quantity */
226             q_ptr->number = 1;
227
228             /* Restore the charges */
229             o_ptr->pval++;
230
231             /* Unstack the used item */
232             o_ptr->number--;
233             item = store_item_to_inventory(player_ptr, q_ptr);
234
235             msg_format(_("杖をまとめなおした。", "You unstack your staff."));
236         }
237
238         if (item >= 0) {
239             inven_item_charges(player_ptr, item);
240         } else {
241             floor_item_charges(player_ptr->current_floor_ptr, 0 - item);
242         }
243
244         player_ptr->window_flags |= (PW_INVEN | PW_EQUIP);
245         return true;
246     }
247
248     return false;
249 }
250
251 /*!
252  * @brief 食料を食べるコマンドのサブルーチン
253  * @param item 食べるオブジェクトの所持品ID
254  */
255 void exe_eat_food(player_type *player_ptr, INVENTORY_IDX item)
256 {
257     if (music_singing_any(player_ptr))
258         stop_singing(player_ptr);
259
260     SpellHex spell_hex(player_ptr);
261     if (spell_hex.is_spelling_any()) {
262         (void)spell_hex.stop_all_spells();
263     }
264
265     object_type *o_ptr = ref_item(player_ptr, item);
266
267     sound(SOUND_EAT);
268
269     PlayerEnergy(player_ptr).set_player_turn_energy(100);
270
271     /* Object level */
272     int lev = k_info[o_ptr->k_idx].level;
273
274     /* Identity not known yet */
275     int ident = exe_eat_food_type_object(player_ptr, o_ptr);
276
277     /*
278      * Store what may have to be updated for the inventory (including
279      * autodestroy if set by something else).  Then turn off those flags
280      * so that updates triggered by calling gain_exp() or set_food() below
281      * do not rearrange the inventory before the food item is destroyed in
282      * the pack.
283      */
284     BIT_FLAGS inventory_flags = (PU_COMBINE | PU_REORDER | (player_ptr->update & PU_AUTODESTROY));
285     player_ptr->update &= ~(PU_COMBINE | PU_REORDER | PU_AUTODESTROY);
286
287     if (!(o_ptr->is_aware())) {
288         chg_virtue(player_ptr, V_KNOWLEDGE, -1);
289         chg_virtue(player_ptr, V_PATIENCE, -1);
290         chg_virtue(player_ptr, V_CHANCE, 1);
291     }
292
293     /* We have tried it */
294     if (o_ptr->tval == TV_FOOD)
295         object_tried(o_ptr);
296
297     /* The player is now aware of the object */
298     if (ident && !o_ptr->is_aware()) {
299         object_aware(player_ptr, o_ptr);
300         gain_exp(player_ptr, (lev + (player_ptr->lev >> 1)) / player_ptr->lev);
301     }
302
303     player_ptr->window_flags |= (PW_INVEN | PW_EQUIP | PW_PLAYER);
304
305     /* Undeads drain recharge of magic device */
306     if (exe_eat_charge_of_magic_device(player_ptr, o_ptr, item)) {
307         player_ptr->update |= inventory_flags;
308         return;
309     }
310
311     auto food_type = player_race_food(player_ptr);
312
313     /* Balrogs change humanoid corpses to energy */
314     if (food_type == PlayerRaceFood::CORPSE && (o_ptr->tval == TV_CORPSE && o_ptr->sval == SV_CORPSE && angband_strchr("pht", r_info[o_ptr->pval].d_char))) {
315         GAME_TEXT o_name[MAX_NLEN];
316         describe_flavor(player_ptr, o_name, o_ptr, (OD_OMIT_PREFIX | OD_NAME_ONLY));
317         msg_format(_("%sは燃え上り灰になった。精力を吸収した気がする。", "%^s is burnt to ashes.  You absorb its vitality!"), o_name);
318         (void)set_food(player_ptr, PY_FOOD_MAX - 1);
319
320         player_ptr->update |= inventory_flags;
321         vary_item(player_ptr, item, -1);
322         return;
323     }
324
325     if (PlayerRace(player_ptr).equals(player_race_type::SKELETON)) {
326         if (!((o_ptr->sval == SV_FOOD_WAYBREAD) || (o_ptr->sval < SV_FOOD_BISCUIT))) {
327             object_type forge;
328             object_type *q_ptr = &forge;
329
330             msg_print(_("食べ物がアゴを素通りして落ちた!", "The food falls through your jaws!"));
331             q_ptr->prep(lookup_kind(o_ptr->tval, o_ptr->sval));
332
333             /* Drop the object from heaven */
334             (void)drop_near(player_ptr, q_ptr, -1, player_ptr->y, player_ptr->x);
335         } else {
336             msg_print(_("食べ物がアゴを素通りして落ち、消えた!", "The food falls through your jaws and vanishes!"));
337         }
338     } else if (food_type == PlayerRaceFood::BLOOD) {
339         /* Vampires are filled only by bloods, so reduced nutritional benefit */
340         (void)set_food(player_ptr, player_ptr->food + (o_ptr->pval / 10));
341         msg_print(_("あなたのような者にとって食糧など僅かな栄養にしかならない。", "Mere victuals hold scant sustenance for a being such as yourself."));
342
343         if (player_ptr->food < PY_FOOD_ALERT) /* Hungry */
344             msg_print(_("あなたの飢えは新鮮な血によってのみ満たされる!", "Your hunger can only be satisfied with fresh blood!"));
345     } else if (food_type == PlayerRaceFood::WATER) {
346         msg_print(_("動物の食物はあなたにとってほとんど栄養にならない。", "The food of animals is poor sustenance for you."));
347         set_food(player_ptr, player_ptr->food + ((o_ptr->pval) / 20));
348     } else if (food_type != PlayerRaceFood::RATION) {
349         msg_print(_("生者の食物はあなたにとってほとんど栄養にならない。", "The food of mortals is poor sustenance for you."));
350         set_food(player_ptr, player_ptr->food + ((o_ptr->pval) / 20));
351     } else {
352         if (o_ptr->tval == TV_FOOD && o_ptr->sval == SV_FOOD_WAYBREAD) {
353             /* Waybread is always fully satisfying. */
354             set_food(player_ptr, MAX(player_ptr->food, PY_FOOD_MAX - 1));
355         } else {
356             /* Food can feed the player */
357             (void)set_food(player_ptr, player_ptr->food + o_ptr->pval);
358         }
359     }
360
361     player_ptr->update |= inventory_flags;
362     vary_item(player_ptr, item, -1);
363 }
364
365 /*!
366  * @brief 食料を食べるコマンドのメインルーチン /
367  * Eat some food (from the pack or floor)
368  */
369 void do_cmd_eat_food(player_type *player_ptr)
370 {
371     OBJECT_IDX item;
372     concptr q, s;
373
374     if (player_ptr->special_defense & (KATA_MUSOU | KATA_KOUKIJIN)) {
375         set_action(player_ptr, ACTION_NONE);
376     }
377
378     q = _("どれを食べますか? ", "Eat which item? ");
379     s = _("食べ物がない。", "You have nothing to eat.");
380
381     if (!choose_object(player_ptr, &item, q, s, (USE_INVEN | USE_FLOOR), FuncItemTester(item_tester_hook_eatable, player_ptr)))
382         return;
383
384     exe_eat_food(player_ptr, item);
385 }