2 * @brief フロア生成時にアイテムを配置する
5 * @todo ちょっとギリギリ。後で分割を検討する
8 #include "floor/floor-object.h"
9 #include "artifact/fixed-art-generator.h"
10 #include "core/window-redrawer.h"
11 #include "flavor/flavor-describer.h"
12 #include "flavor/object-flavor-types.h"
13 #include "floor/cave.h"
14 #include "game-option/birth-options.h"
15 #include "game-option/cheat-options.h"
16 #include "game-option/cheat-types.h"
17 #include "grid/feature.h"
18 #include "grid/grid.h"
19 #include "inventory/inventory-slot-types.h"
20 #include "inventory/item-getter.h"
21 #include "main/sound-definitions-table.h"
22 #include "main/sound-of-music.h"
23 #include "object-enchant/apply-magic.h"
24 #include "object-enchant/item-apply-magic.h"
25 #include "object-enchant/special-object-flags.h"
26 #include "object/object-info.h"
27 #include "object/object-kind-hook.h"
28 #include "object/object-kind.h"
29 #include "object/object-stack.h"
30 #include "perception/object-perception.h"
31 #include "system/alloc-entries.h"
32 #include "system/artifact-type-definition.h"
33 #include "system/floor-type-definition.h"
34 #include "system/grid-type-definition.h"
35 #include "system/object-type-definition.h"
36 #include "system/monster-type-definition.h"
37 #include "system/player-type-definition.h"
38 #include "system/system-variables.h"
39 #include "target/projection-path-calculator.h"
40 #include "util/bit-flags-calculator.h"
41 #include "view/display-messages.h"
42 #include "window/display-sub-windows.h"
43 #include "wizard/wizard-messages.h"
44 #include "world/world-object.h"
45 #include "world/world.h"
47 #define MAX_GOLD 18 /* Number of "gold" entries */
50 * @brief オブジェクト生成テーブルに生成制約を加える /
51 * Apply a "object restriction function" to the "object allocation table"
53 * @details 生成の制約はグローバルのget_obj_num_hook関数ポインタで加える
55 static errr get_obj_num_prep(void)
57 for (auto &entry : alloc_kind_table) {
58 if (!get_obj_num_hook || (*get_obj_num_hook)(entry.index)) {
59 entry.prob2 = entry.prob1;
69 * @brief デバッグ時にアイテム生成情報をメッセージに出力する / Cheat -- describe a created object for the user
70 * @param player_ptr プレイヤーへの参照ポインタ
71 * @param o_ptr デバッグ出力するオブジェクトの構造体参照ポインタ
73 static void object_mention(PlayerType *player_ptr, ObjectType *o_ptr)
75 object_aware(player_ptr, o_ptr);
78 o_ptr->ident |= (IDENT_FULL_KNOWN);
79 GAME_TEXT o_name[MAX_NLEN];
80 describe_flavor(player_ptr, o_name, o_ptr, 0);
81 msg_format_wizard(player_ptr, CHEAT_OBJECT, _("%sを生成しました。", "%s was generated."), o_name);
84 static int get_base_floor(floor_type *floor_ptr, BIT_FLAGS mode, std::optional<int> rq_mon_level)
86 if (any_bits(mode, AM_GREAT)) {
87 if (rq_mon_level.has_value()) {
88 return rq_mon_level.value() + 10 + randint1(10);
90 return floor_ptr->object_level + 15;
94 if (any_bits(mode, AM_GOOD)) {
95 return floor_ptr->object_level + 10;
98 return floor_ptr->object_level;
101 static void set_ammo_quantity(ObjectType *j_ptr)
103 auto is_ammo = j_ptr->tval == ItemKindType::SPIKE;
104 is_ammo |= j_ptr->tval == ItemKindType::SHOT;
105 is_ammo |= j_ptr->tval == ItemKindType::ARROW;
106 is_ammo |= j_ptr->tval == ItemKindType::BOLT;
107 if (is_ammo && !j_ptr->is_fixed_artifact()) {
108 j_ptr->number = damroll(6, 7);
113 * @brief 生成階に応じたベースアイテムの生成を行う。
114 * Attempt to make an object (normal or good/great)
115 * @param player_ptr プレイヤーへの参照ポインタ
116 * @param j_ptr 生成結果を収めたいオブジェクト構造体の参照ポインタ
117 * @param mode オプションフラグ
118 * @param rq_mon_level ランダムクエスト討伐対象のレベル。ランダムクエスト以外の生成であれば無効値
119 * @return アイテムの生成成功可否
121 bool make_object(PlayerType *player_ptr, ObjectType *j_ptr, BIT_FLAGS mode, std::optional<int> rq_mon_level)
123 auto *floor_ptr = player_ptr->current_floor_ptr;
124 auto prob = any_bits(mode, AM_GOOD) ? 10 : 1000;
125 auto base = get_base_floor(floor_ptr, mode, rq_mon_level);
126 if (!one_in_(prob) || !make_artifact_special(player_ptr, j_ptr)) {
127 if (any_bits(mode, AM_GOOD) && !get_obj_num_hook) {
128 get_obj_num_hook = kind_is_good;
131 if (get_obj_num_hook) {
135 auto k_idx = get_obj_num(player_ptr, base, mode);
136 if (get_obj_num_hook) {
137 get_obj_num_hook = nullptr;
148 apply_magic_to_object(player_ptr, j_ptr, floor_ptr->object_level, mode);
149 set_ammo_quantity(j_ptr);
151 object_mention(player_ptr, j_ptr);
158 * @brief 生成階に応じた財宝オブジェクトの生成を行う。
159 * Make a treasure object
160 * @param floor_ptr 現在フロアへの参照ポインタ
161 * @param j_ptr 生成結果を収めたいオブジェクト構造体の参照ポインタ
162 * @return 生成に成功したらTRUEを返す。
164 * The location must be a legal, clean, floor grid.
166 bool make_gold(PlayerType *player_ptr, ObjectType *j_ptr)
168 auto *floor_ptr = player_ptr->current_floor_ptr;
169 int i = ((randint1(floor_ptr->object_level + 2) + 2) / 2) - 1;
170 if (one_in_(GREAT_OBJ)) {
171 i += randint1(floor_ptr->object_level + 1);
178 j_ptr->prep(OBJ_GOLD_LIST + i);
180 int32_t base = k_info[OBJ_GOLD_LIST + i].cost;
181 j_ptr->pval = (base + (8L * randint1(base)) + randint1(8));
187 * @brief フロア中のアイテムを全て削除する / Deletes all objects at given location
188 * Delete a dungeon object
189 * @param player_ptr プレイヤーへの参照ポインタ
190 * @param y 削除したフロアマスのY座標
191 * @param x 削除したフロアマスのX座標
193 void delete_all_items_from_floor(PlayerType *player_ptr, POSITION y, POSITION x)
196 auto *floor_ptr = player_ptr->current_floor_ptr;
197 if (!in_bounds(floor_ptr, y, x))
200 g_ptr = &floor_ptr->grid_array[y][x];
201 for (const auto this_o_idx : g_ptr->o_idx_list) {
203 o_ptr = &floor_ptr->o_list[this_o_idx];
208 g_ptr->o_idx_list.clear();
209 lite_spot(player_ptr, y, x);
213 * @brief 床上のアイテムの数を増やす /
214 * Increase the "number" of an item on the floor
215 * @param player_ptr プレイヤーへの参照ポインタ
216 * @param item 増やしたいアイテムの所持スロット
217 * @param num 増やしたいアイテムの数
219 void floor_item_increase(PlayerType *player_ptr, INVENTORY_IDX item, ITEM_NUMBER num)
221 auto *floor_ptr = player_ptr->current_floor_ptr;
223 auto *o_ptr = &floor_ptr->o_list[item];
224 num += o_ptr->number;
230 num -= o_ptr->number;
231 o_ptr->number += num;
233 set_bits(player_ptr->window_flags, PW_FLOOR_ITEM_LIST);
237 * @brief 床上の数の無くなったアイテムスロットを消去する /
238 * Optimize an item on the floor (destroy "empty" items)
239 * @param player_ptr プレイヤーへの参照ポインタ
240 * @param item 消去したいアイテムの所持スロット
242 void floor_item_optimize(PlayerType *player_ptr, INVENTORY_IDX item)
244 auto *o_ptr = &player_ptr->current_floor_ptr->o_list[item];
250 delete_object_idx(player_ptr, item);
252 set_bits(player_ptr->window_flags, PW_FLOOR_ITEM_LIST);
256 * @brief オブジェクトを削除する /
257 * Delete a dungeon object
258 * @param player_ptr プレイヤーへの参照ポインタ
259 * @param o_idx 削除対象のオブジェクト構造体ポインタ
261 * Handle "stacks" of objects correctly.
263 void delete_object_idx(PlayerType *player_ptr, OBJECT_IDX o_idx)
266 auto *floor_ptr = player_ptr->current_floor_ptr;
267 excise_object_idx(floor_ptr, o_idx);
268 j_ptr = &floor_ptr->o_list[o_idx];
269 if (!j_ptr->is_held_by_monster()) {
273 lite_spot(player_ptr, y, x);
279 set_bits(player_ptr->window_flags, PW_FLOOR_ITEM_LIST);
283 * @brief 床上、モンスター所持でスタックされたアイテムを削除しスタックを補完する / Excise a dungeon object from any stacks
284 * @param floo_ptr 現在フロアへの参照ポインタ
285 * @param o_idx 削除対象のオブジェクト構造体ポインタ
287 void excise_object_idx(floor_type *floor_ptr, OBJECT_IDX o_idx)
289 auto &list = get_o_idx_list_contains(floor_ptr, o_idx);
294 * @brief 指定したOBJECT_IDXを含むリスト(モンスター所持リスト or 床上スタックリスト)への参照を得る
295 * @param floo_ptr 現在フロアへの参照ポインタ
296 * @param o_idx 参照を得るリストに含まれるOBJECT_IDX
297 * @return o_idxを含む ObjectIndexList への参照
299 ObjectIndexList &get_o_idx_list_contains(floor_type *floor_ptr, OBJECT_IDX o_idx)
301 auto *o_ptr = &floor_ptr->o_list[o_idx];
303 if (o_ptr->is_held_by_monster()) {
304 return floor_ptr->m_list[o_ptr->held_m_idx].hold_o_idx_list;
306 return floor_ptr->grid_array[o_ptr->iy][o_ptr->ix].o_idx_list;
311 * @brief 生成済のオブジェクトをフロアの所定の位置に落とす。
312 * Let an object fall to the ground at or near a location.
313 * @param player_ptr プレイヤーへの参照ポインタ
314 * @param j_ptr 落としたいオブジェクト構造体の参照ポインタ
315 * @param chance ドロップの消滅率(%)
316 * @param y 配置したいフロアのY座標
317 * @param x 配置したいフロアのX座標
318 * @return 生成に成功したらオブジェクトのIDを返す。
320 * The initial location is assumed to be "in_bounds(floor_ptr, )".\n
322 * This function takes a parameter "chance". This is the percentage\n
323 * chance that the item will "disappear" instead of drop. If the object\n
324 * has been thrown, then this is the chance of disappearance on contact.\n
326 * Hack -- this function uses "chance" to determine if it should produce\n
327 * some form of "description" of the drop event (under the player).\n
329 * We check several locations to see if we can find a location at which\n
330 * the object can combine, stack, or be placed. Artifacts will try very\n
331 * hard to be placed, including "teleporting" to a useful grid if needed.\n
333 OBJECT_IDX drop_near(PlayerType *player_ptr, ObjectType *j_ptr, PERCENTAGE chance, POSITION y, POSITION x)
338 OBJECT_IDX o_idx = 0;
340 GAME_TEXT o_name[MAX_NLEN];
345 bool plural = (j_ptr->number != 1);
347 describe_flavor(player_ptr, o_name, j_ptr, (OD_OMIT_PREFIX | OD_NAME_ONLY));
348 if (!j_ptr->is_artifact() && (randint0(100) < chance)) {
350 msg_format("%sは消えた。", o_name);
352 msg_format("The %s disappear%s.", o_name, (plural ? "" : "s"));
355 msg_print(_("(破損)", "(breakage)"));
365 auto *floor_ptr = player_ptr->current_floor_ptr;
366 for (dy = -3; dy <= 3; dy++) {
367 for (dx = -3; dx <= 3; dx++) {
369 d = (dy * dy) + (dx * dx);
375 if (!in_bounds(floor_ptr, ty, tx))
377 if (!projectable(player_ptr, y, x, ty, tx))
380 g_ptr = &floor_ptr->grid_array[ty][tx];
381 if (!cave_drop_bold(floor_ptr, ty, tx))
385 for (const auto this_o_idx : g_ptr->o_idx_list) {
387 o_ptr = &floor_ptr->o_list[this_o_idx];
388 if (object_similar(o_ptr, j_ptr))
399 s = 1000 - (d + k * 5);
406 if ((++bn >= 2) && !one_in_(bn))
417 if (!flag && !j_ptr->is_artifact()) {
419 msg_format("%sは消えた。", o_name);
421 msg_format("The %s disappear%s.", o_name, (plural ? "" : "s"));
424 msg_print(_("(床スペースがない)", "(no floor space)"));
429 for (i = 0; !flag && (i < 1000); i++) {
430 ty = rand_spread(by, 1);
431 tx = rand_spread(bx, 1);
433 if (!in_bounds(floor_ptr, ty, tx))
439 if (!cave_drop_bold(floor_ptr, by, bx))
446 int candidates = 0, pick;
447 for (ty = 1; ty < floor_ptr->height - 1; ty++) {
448 for (tx = 1; tx < floor_ptr->width - 1; tx++) {
449 if (cave_drop_bold(floor_ptr, ty, tx))
456 msg_format("%sは消えた。", o_name);
458 msg_format("The %s disappear%s.", o_name, (plural ? "" : "s"));
462 msg_print(_("(床スペースがない)", "(no floor space)"));
465 if (j_ptr->is_fixed_artifact() && !j_ptr->is_known()) {
466 a_info[j_ptr->name1].cur_num = 0;
473 pick = randint1(candidates);
474 for (ty = 1; ty < floor_ptr->height - 1; ty++) {
475 for (tx = 1; tx < floor_ptr->width - 1; tx++) {
476 if (cave_drop_bold(floor_ptr, ty, tx)) {
491 g_ptr = &floor_ptr->grid_array[by][bx];
492 for (const auto this_o_idx : g_ptr->o_idx_list) {
494 o_ptr = &floor_ptr->o_list[this_o_idx];
495 if (object_similar(o_ptr, j_ptr)) {
496 object_absorb(o_ptr, j_ptr);
503 o_idx = o_pop(floor_ptr);
505 if (!done && !o_idx) {
507 msg_format("%sは消えた。", o_name);
509 msg_format("The %s disappear%s.", o_name, (plural ? "" : "s"));
512 msg_print(_("(アイテムが多過ぎる)", "(too many objects)"));
514 if (j_ptr->is_fixed_artifact()) {
515 a_info[j_ptr->name1].cur_num = 0;
522 (&floor_ptr->o_list[o_idx])->copy_from(j_ptr);
523 j_ptr = &floor_ptr->o_list[o_idx];
526 j_ptr->held_m_idx = 0;
527 g_ptr->o_idx_list.add(floor_ptr, o_idx);
531 note_spot(player_ptr, by, bx);
532 lite_spot(player_ptr, by, bx);
535 if (player_bold(player_ptr, by, bx))
536 set_bits(player_ptr->window_flags, PW_FLOOR_ITEM_LIST);
538 if (chance && player_bold(player_ptr, by, bx)) {
539 msg_print(_("何かが足下に転がってきた。", "You feel something roll beneath your feet."));
546 * @brief 床上の魔道具の残り残量メッセージを表示する /
547 * Describe the charges on an item on the floor.
548 * @param floo_ptr 現在フロアへの参照ポインタ
549 * @param item メッセージの対象にしたいアイテム所持スロット
551 void floor_item_charges(floor_type *floor_ptr, INVENTORY_IDX item)
553 auto *o_ptr = &floor_ptr->o_list[item];
554 if ((o_ptr->tval != ItemKindType::STAFF) && (o_ptr->tval != ItemKindType::WAND))
556 if (!o_ptr->is_known())
560 if (o_ptr->pval <= 0) {
561 msg_print("この床上のアイテムは、もう魔力が残っていない。");
563 msg_format("この床上のアイテムは、あと %d 回分の魔力が残っている。", o_ptr->pval);
566 if (o_ptr->pval != 1) {
567 msg_format("There are %d charges remaining.", o_ptr->pval);
569 msg_format("There is %d charge remaining.", o_ptr->pval);
575 * @brief 床上のアイテムの残り数メッセージを表示する /
576 * Describe the charges on an item on the floor.
577 * @param floo_ptr 現在フロアへの参照ポインタ
578 * @param item メッセージの対象にしたいアイテム所持スロット
580 void floor_item_describe(PlayerType *player_ptr, INVENTORY_IDX item)
582 auto *o_ptr = &player_ptr->current_floor_ptr->o_list[item];
583 GAME_TEXT o_name[MAX_NLEN];
584 describe_flavor(player_ptr, o_name, o_ptr, 0);
586 if (o_ptr->number <= 0) {
587 msg_format("床上には、もう%sはない。", o_name);
589 msg_format("床上には、まだ %sがある。", o_name);
592 msg_format("You see %s.", o_name);
597 * Choose an item and get auto-picker entry from it.
599 ObjectType *choose_object(PlayerType *player_ptr, OBJECT_IDX *idx, concptr q, concptr s, BIT_FLAGS option, const ItemTester& item_tester)
606 FixItemTesterSetter setter(item_tester);
608 if (!get_item(player_ptr, &item, q, s, option, item_tester))
614 if (item == INVEN_FORCE)
617 return ref_item(player_ptr, item);