OSDN Git Service

Merge remote-tracking branch 'remotes/hengbandosx/english-market-edits' into feature...
[hengband/hengband.git] / src / core / object-compressor.c
1 #include "core/object-compressor.h"
2 #include "core/player-redraw-types.h"
3 #include "core/window-redrawer.h"
4 #include "floor/floor-object.h"
5 #include "grid/grid.h"
6 #include "object-hook/hook-checker.h"
7 #include "object-hook/hook-enchant.h"
8 #include "object/object-generator.h"
9 #include "object/object-kind.h"
10 #include "system/floor-type-definition.h"
11 #include "system/monster-type-definition.h"
12 #include "system/object-type-definition.h"
13 #include "view/display-messages.h"
14
15 /*!
16  * @brief グローバルオブジェクト配列に対し指定範囲のオブジェクトを整理してIDの若い順に寄せる /
17  * Move an object from index i1 to index i2 in the object list
18  * @param i1 整理したい配列の始点
19  * @param i2 整理したい配列の終点
20  * @return なし
21  */
22 static void compact_objects_aux(floor_type *floor_ptr, OBJECT_IDX i1, OBJECT_IDX i2)
23 {
24     if (i1 == i2)
25         return;
26
27     object_type *o_ptr;
28     for (OBJECT_IDX i = 1; i < floor_ptr->o_max; i++) {
29         o_ptr = &floor_ptr->o_list[i];
30         if (!o_ptr->k_idx)
31             continue;
32
33         if (o_ptr->next_o_idx == i1)
34             o_ptr->next_o_idx = i2;
35     }
36
37     o_ptr = &floor_ptr->o_list[i1];
38
39     if (object_is_held_monster(o_ptr)) {
40         monster_type *m_ptr;
41         m_ptr = &floor_ptr->m_list[o_ptr->held_m_idx];
42         if (m_ptr->hold_o_idx == i1)
43             m_ptr->hold_o_idx = i2;
44     } else {
45         POSITION y = o_ptr->iy;
46         POSITION x = o_ptr->ix;
47         grid_type *g_ptr;
48         g_ptr = &floor_ptr->grid_array[y][x];
49         if (g_ptr->o_idx == i1)
50             g_ptr->o_idx = i2;
51     }
52
53     floor_ptr->o_list[i2] = floor_ptr->o_list[i1];
54     object_wipe(o_ptr);
55 }
56
57 /*!
58  * @brief グローバルオブジェクト配列から優先度の低いものを削除し、データを圧縮する。 /
59  * Compact and Reorder the object list.
60  * @param player_ptr プレーヤーへの参照ポインタ
61  * @param size 最低でも減らしたいオブジェクト数の水準
62  * @return なし
63  * @details
64  * (危険なので使用には注意すること)
65  * This function can be very dangerous, use with caution!\n
66  *\n
67  * When actually "compacting" objects, we base the saving throw on a\n
68  * combination of object level, distance from player, and current\n
69  * "desperation".\n
70  *\n
71  * After "compacting" (if needed), we "reorder" the objects into a more\n
72  * compact order, and we reset the allocation info, and the "live" array.\n
73  */
74 void compact_objects(player_type *player_ptr, int size)
75 {
76     object_type *o_ptr;
77     if (size) {
78         msg_print(_("アイテム情報を圧縮しています...", "Compacting objects..."));
79         player_ptr->redraw |= PR_MAP;
80         player_ptr->window |= PW_OVERHEAD | PW_DUNGEON;
81     }
82
83     floor_type *floor_ptr = player_ptr->current_floor_ptr;
84     for (int num = 0, cnt = 1; num < size; cnt++) {
85         int cur_lev = 5 * cnt;
86         int cur_dis = 5 * (20 - cnt);
87         for (OBJECT_IDX i = 1; i < floor_ptr->o_max; i++) {
88             o_ptr = &floor_ptr->o_list[i];
89
90             if (!object_is_valid(o_ptr) || (k_info[o_ptr->k_idx].level > cur_lev))
91                 continue;
92
93             POSITION y, x;
94             if (object_is_held_monster(o_ptr)) {
95                 monster_type *m_ptr;
96                 m_ptr = &floor_ptr->m_list[o_ptr->held_m_idx];
97                 y = m_ptr->fy;
98                 x = m_ptr->fx;
99
100                 if (randint0(100) < 90)
101                     continue;
102             } else {
103                 y = o_ptr->iy;
104                 x = o_ptr->ix;
105             }
106
107             if ((cur_dis > 0) && (distance(player_ptr->y, player_ptr->x, y, x) < cur_dis))
108                 continue;
109
110             int chance = 90;
111             if ((object_is_fixed_artifact(o_ptr) || o_ptr->art_name) && (cnt < 1000))
112                 chance = 100;
113
114             if (randint0(100) < chance)
115                 continue;
116
117             delete_object_idx(player_ptr, i);
118             num++;
119         }
120     }
121
122     for (OBJECT_IDX i = floor_ptr->o_max - 1; i >= 1; i--) {
123         o_ptr = &floor_ptr->o_list[i];
124         if (o_ptr->k_idx)
125             continue;
126
127         compact_objects_aux(floor_ptr, floor_ptr->o_max - 1, i);
128         floor_ptr->o_max--;
129     }
130 }