OSDN Git Service

Merge pull request #1173 from Hourier/feature/Replace-Boolean-Macro
[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 "core/player-update-types.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 "object/object-kind.h"
25 #include "perception/object-perception.h"
26 #include "player-status/player-energy.h"
27 #include "player-info/avatar.h"
28 #include "player/attack-defense-types.h"
29 #include "player/digestion-processor.h"
30 #include "player/mimic-info-table.h"
31 #include "player/player-class.h"
32 #include "player/player-damage.h"
33 #include "player/player-race-types.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 creature_ptr プレイヤー情報への参照ポインタ
56  * @param o_ptr 食べるオブジェクト
57  * @return 鑑定されるならTRUE、されないならFALSE
58  */
59 bool exe_eat_food_type_object(player_type *creature_ptr, object_type *o_ptr)
60 {
61     if (o_ptr->tval != TV_FOOD)
62         return false;
63
64     switch (o_ptr->sval) {
65     case SV_FOOD_POISON:
66         if (!(has_resist_pois(creature_ptr) || is_oppose_pois(creature_ptr)))
67             if (set_poisoned(creature_ptr, creature_ptr->poisoned + randint0(10) + 10))
68                 return true;
69         break;
70     case SV_FOOD_BLINDNESS:
71         if (!has_resist_blind(creature_ptr))
72             if (set_blind(creature_ptr, creature_ptr->blind + randint0(200) + 200))
73                 return true;
74         break;
75     case SV_FOOD_PARANOIA:
76         if (!has_resist_fear(creature_ptr))
77             if (set_afraid(creature_ptr, creature_ptr->afraid + randint0(10) + 10))
78                 return true;
79         break;
80     case SV_FOOD_CONFUSION:
81         if (!has_resist_conf(creature_ptr))
82             if (set_confused(creature_ptr, creature_ptr->confused + randint0(10) + 10))
83                 return true;
84         break;
85     case SV_FOOD_HALLUCINATION:
86         if (!has_resist_chaos(creature_ptr))
87             if (set_image(creature_ptr, creature_ptr->image + randint0(250) + 250))
88                 return true;
89         break;
90     case SV_FOOD_PARALYSIS:
91         if (!creature_ptr->free_act)
92             if (set_paralyzed(creature_ptr, creature_ptr->paralyzed + randint0(10) + 10))
93                 return true;
94         break;
95     case SV_FOOD_WEAKNESS:
96         take_hit(creature_ptr, DAMAGE_NOESCAPE, damroll(6, 6), _("毒入り食料", "poisonous food"));
97         (void)do_dec_stat(creature_ptr, A_STR);
98         return true;
99     case SV_FOOD_SICKNESS:
100         take_hit(creature_ptr, DAMAGE_NOESCAPE, damroll(6, 6), _("毒入り食料", "poisonous food"));
101         (void)do_dec_stat(creature_ptr, A_CON);
102         return true;
103     case SV_FOOD_STUPIDITY:
104         take_hit(creature_ptr, DAMAGE_NOESCAPE, damroll(8, 8), _("毒入り食料", "poisonous food"));
105         (void)do_dec_stat(creature_ptr, A_INT);
106         return true;
107     case SV_FOOD_NAIVETY:
108         take_hit(creature_ptr, DAMAGE_NOESCAPE, damroll(8, 8), _("毒入り食料", "poisonous food"));
109         (void)do_dec_stat(creature_ptr, A_WIS);
110         return true;
111     case SV_FOOD_UNHEALTH:
112         take_hit(creature_ptr, DAMAGE_NOESCAPE, damroll(10, 10), _("毒入り食料", "poisonous food"));
113         (void)do_dec_stat(creature_ptr, A_CON);
114         return true;
115     case SV_FOOD_DISEASE:
116         take_hit(creature_ptr, DAMAGE_NOESCAPE, damroll(10, 10), _("毒入り食料", "poisonous food"));
117         (void)do_dec_stat(creature_ptr, A_STR);
118         return true;
119     case SV_FOOD_CURE_POISON:
120         if (set_poisoned(creature_ptr, 0))
121             return true;
122         break;
123     case SV_FOOD_CURE_BLINDNESS:
124         if (set_blind(creature_ptr, 0))
125             return true;
126         break;
127     case SV_FOOD_CURE_PARANOIA:
128         if (set_afraid(creature_ptr, 0))
129             return true;
130         break;
131     case SV_FOOD_CURE_CONFUSION:
132         if (set_confused(creature_ptr, 0))
133             return true;
134         break;
135     case SV_FOOD_CURE_SERIOUS:
136         return cure_serious_wounds(creature_ptr, 4, 8);
137     case SV_FOOD_RESTORE_STR:
138         if (do_res_stat(creature_ptr, A_STR))
139             return true;
140         break;
141     case SV_FOOD_RESTORE_CON:
142         if (do_res_stat(creature_ptr, A_CON))
143             return true;
144         break;
145     case SV_FOOD_RESTORING:
146         return restore_all_status(creature_ptr);
147 #ifdef JP
148     /* それぞれの食べ物の感想をオリジナルより細かく表現 */
149     case SV_FOOD_BISCUIT:
150         msg_print("甘くてサクサクしてとてもおいしい。");
151         return true;
152     case SV_FOOD_JERKY:
153         msg_print("歯ごたえがあっておいしい。");
154         return true;
155     case SV_FOOD_SLIME_MOLD:
156         msg_print("これはなんとも形容しがたい味だ。");
157         return true;
158     case SV_FOOD_RATION:
159         msg_print("これはおいしい。");
160         return true;
161 #else
162     case SV_FOOD_RATION:
163     case SV_FOOD_BISCUIT:
164     case SV_FOOD_JERKY:
165     case SV_FOOD_SLIME_MOLD:
166         msg_print("That tastes good.");
167         return true;
168 #endif
169     case SV_FOOD_WAYBREAD:
170         msg_print(_("これはひじょうに美味だ。", "That tastes good."));
171         (void)set_poisoned(creature_ptr, 0);
172         (void)hp_player(creature_ptr, damroll(4, 8));
173         return true;
174     case SV_FOOD_PINT_OF_ALE:
175     case SV_FOOD_PINT_OF_WINE:
176         msg_print(_("のどごし爽やかだ。", "That tastes good."));
177         return true;
178     }
179
180     return false;
181 }
182
183 /*!
184  * @brief 魔法道具のチャージをの食料として食べたときの効果を発動
185  * @param creature_ptr プレイヤー情報への参照ポインタ
186  * @param o_ptr 食べるオブジェクト
187  * @param item オブジェクトのインベントリ番号
188  * @return 食べようとしたらTRUE、しなかったらFALSE
189  */
190 bool exe_eat_charge_of_magic_device(player_type *creature_ptr, object_type *o_ptr, INVENTORY_IDX item)
191 {
192     if (o_ptr->tval != TV_STAFF && o_ptr->tval != TV_WAND)
193         return false;
194
195     if (player_race_food(creature_ptr) == PlayerRaceFood::MANA) {
196         concptr staff;
197
198         if (o_ptr->tval == TV_STAFF && (item < 0) && (o_ptr->number > 1)) {
199             msg_print(_("まずは杖を拾わなければ。", "You must first pick up the staffs."));
200             return true;
201         }
202
203         staff = (o_ptr->tval == TV_STAFF) ? _("杖", "staff") : _("魔法棒", "wand");
204
205         /* "Eat" charges */
206         if (o_ptr->pval == 0) {
207             msg_format(_("この%sにはもう魔力が残っていない。", "The %s has no charges left."), staff);
208             o_ptr->ident |= (IDENT_EMPTY);
209             creature_ptr->window_flags |= (PW_INVEN);
210             return true;
211         }
212
213         msg_format(_("あなたは%sの魔力をエネルギー源として吸収した。", "You absorb mana of the %s as your energy."), staff);
214
215         /* Use a single charge */
216         o_ptr->pval--;
217
218         /* Eat a charge */
219         set_food(creature_ptr, creature_ptr->food + 5000);
220
221         /* XXX Hack -- unstack if necessary */
222         if (o_ptr->tval == TV_STAFF && (item >= 0) && (o_ptr->number > 1)) {
223             object_type forge;
224             object_type *q_ptr;
225             q_ptr = &forge;
226             q_ptr->copy_from(o_ptr);
227
228             /* Modify quantity */
229             q_ptr->number = 1;
230
231             /* Restore the charges */
232             o_ptr->pval++;
233
234             /* Unstack the used item */
235             o_ptr->number--;
236             item = store_item_to_inventory(creature_ptr, q_ptr);
237
238             msg_format(_("杖をまとめなおした。", "You unstack your staff."));
239         }
240
241         if (item >= 0) {
242             inven_item_charges(creature_ptr, item);
243         } else {
244             floor_item_charges(creature_ptr->current_floor_ptr, 0 - item);
245         }
246
247         creature_ptr->window_flags |= (PW_INVEN | PW_EQUIP);
248         return true;
249     }
250
251     return false;
252 }
253
254 /*!
255  * @brief 食料を食べるコマンドのサブルーチン
256  * @param item 食べるオブジェクトの所持品ID
257  */
258 void exe_eat_food(player_type *creature_ptr, INVENTORY_IDX item)
259 {
260     if (music_singing_any(creature_ptr))
261         stop_singing(creature_ptr);
262     if (hex_spelling_any(creature_ptr))
263         stop_hex_spell_all(creature_ptr);
264
265     object_type *o_ptr = ref_item(creature_ptr, item);
266
267     sound(SOUND_EAT);
268
269     PlayerEnergy(creature_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(creature_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 | (creature_ptr->update & PU_AUTODESTROY));
285     creature_ptr->update &= ~(PU_COMBINE | PU_REORDER | PU_AUTODESTROY);
286
287     if (!(object_is_aware(o_ptr))) {
288         chg_virtue(creature_ptr, V_KNOWLEDGE, -1);
289         chg_virtue(creature_ptr, V_PATIENCE, -1);
290         chg_virtue(creature_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 && !object_is_aware(o_ptr)) {
299         object_aware(creature_ptr, o_ptr);
300         gain_exp(creature_ptr, (lev + (creature_ptr->lev >> 1)) / creature_ptr->lev);
301     }
302
303     creature_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(creature_ptr, o_ptr, item)) {
307         creature_ptr->update |= inventory_flags;
308         return;
309     }
310
311     auto food_type = player_race_food(creature_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(creature_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(creature_ptr, PY_FOOD_MAX - 1);
319
320         creature_ptr->update |= inventory_flags;
321         vary_item(creature_ptr, item, -1);
322         return;
323     }
324
325     if (is_specific_player_race(creature_ptr, RACE_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(creature_ptr, lookup_kind(o_ptr->tval, o_ptr->sval));
332
333             /* Drop the object from heaven */
334             (void)drop_near(creature_ptr, q_ptr, -1, creature_ptr->y, creature_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(creature_ptr, creature_ptr->food + (o_ptr->pval / 10));
341         msg_print(_("あなたのような者にとって食糧など僅かな栄養にしかならない。", "Mere victuals hold scant sustenance for a being such as yourself."));
342
343         if (creature_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(creature_ptr, creature_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(creature_ptr, creature_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(creature_ptr, MAX(creature_ptr->food, PY_FOOD_MAX - 1));
355         } else {
356             /* Food can feed the player */
357             (void)set_food(creature_ptr, creature_ptr->food + o_ptr->pval);
358         }
359     }
360
361     creature_ptr->update |= inventory_flags;
362     vary_item(creature_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 *creature_ptr)
370 {
371     OBJECT_IDX item;
372     concptr q, s;
373
374     if (creature_ptr->special_defense & (KATA_MUSOU | KATA_KOUKIJIN)) {
375         set_action(creature_ptr, ACTION_NONE);
376     }
377
378     item_tester_hook = item_tester_hook_eatable;
379
380     q = _("どれを食べますか? ", "Eat which item? ");
381     s = _("食べ物がない。", "You have nothing to eat.");
382
383     if (!choose_object(creature_ptr, &item, q, s, (USE_INVEN | USE_FLOOR), TV_NONE))
384         return;
385
386     exe_eat_food(creature_ptr, item);
387 }