OSDN Git Service

Merge branch 'master' of git.osdn.net:/gitroot/hengband/hengband
[hengband/hengband.git] / src / monster / monster-compaction.c
1 #include "monster/monster-compaction.h"
2 #include "core/stuff-handler.h"
3 #include "game-option/play-record-options.h"
4 #include "grid/grid.h"
5 #include "io/write-diary.h"
6 #include "monster-floor/monster-remover.h"
7 #include "monster-race/monster-race.h"
8 #include "monster-race/race-flags1.h"
9 #include "monster/monster-describer.h"
10 #include "monster/monster-description-types.h"
11 #include "monster/monster-info.h"
12 #include "monster/monster-status.h"
13 #include "system/floor-type-definition.h"
14 #include "system/monster-type-definition.h"
15 #include "system/object-type-definition.h"
16 #include "target/target-checker.h"
17 #include "view/display-messages.h"
18
19 /*!
20  * @brief モンスター情報を配列内移動する / Move an object from index i1 to index i2 in the object list
21  * @param player_ptr プレーヤーへの参照ポインタ
22  * @param i1 配列移動元添字
23  * @param i2 配列移動先添字
24  * @return なし
25  */
26 static void compact_monsters_aux(player_type *player_ptr, MONSTER_IDX i1, MONSTER_IDX i2)
27 {
28     if (i1 == i2)
29         return;
30
31     floor_type *floor_ptr = player_ptr->current_floor_ptr;
32     monster_type *m_ptr;
33     m_ptr = &floor_ptr->m_list[i1];
34
35     POSITION y = m_ptr->fy;
36     POSITION x = m_ptr->fx;
37     grid_type *g_ptr;
38     g_ptr = &floor_ptr->grid_array[y][x];
39     g_ptr->m_idx = i2;
40
41     OBJECT_IDX next_o_idx = 0;
42     for (OBJECT_IDX this_o_idx = m_ptr->hold_o_idx; this_o_idx; this_o_idx = next_o_idx) {
43         object_type *o_ptr;
44         o_ptr = &floor_ptr->o_list[this_o_idx];
45         next_o_idx = o_ptr->next_o_idx;
46         o_ptr->held_m_idx = i2;
47     }
48
49     if (target_who == i1)
50         target_who = i2;
51
52     if (player_ptr->pet_t_m_idx == i1)
53         player_ptr->pet_t_m_idx = i2;
54     if (player_ptr->riding_t_m_idx == i1)
55         player_ptr->riding_t_m_idx = i2;
56
57     if (player_ptr->riding == i1)
58         player_ptr->riding = i2;
59
60     if (player_ptr->health_who == i1)
61         health_track(player_ptr, i2);
62
63     if (is_pet(m_ptr)) {
64         for (int i = 1; i < floor_ptr->m_max; i++) {
65             monster_type *m2_ptr = &floor_ptr->m_list[i];
66
67             if (m2_ptr->parent_m_idx == i1)
68                 m2_ptr->parent_m_idx = i2;
69         }
70     }
71
72     (void)COPY(&floor_ptr->m_list[i2], &floor_ptr->m_list[i1], monster_type);
73     (void)WIPE(&floor_ptr->m_list[i1], monster_type);
74
75     for (int i = 0; i < MAX_MTIMED; i++) {
76         int mproc_idx = get_mproc_idx(floor_ptr, i1, i);
77         if (mproc_idx >= 0)
78             floor_ptr->mproc_list[i][mproc_idx] = i2;
79     }
80 }
81
82 /*!
83  * @brief モンスター情報配列を圧縮する / Compact and Reorder the monster list
84  * @param player_ptr プレーヤーへの参照ポインタ
85  * @param size 圧縮後のモンスター件数目標
86  * @return なし
87  * @details
88  * This function can be very dangerous, use with caution!
89  *
90  * When actually "compacting" monsters, we base the saving throw
91  * on a combination of monster level, distance from player, and
92  * current "desperation".
93  *
94  * After "compacting" (if needed), we "reorder" the monsters into a more
95  * compact order, and we reset the allocation info, and the "live" array.
96  */
97 void compact_monsters(player_type *player_ptr, int size)
98 {
99     if (size)
100         msg_print(_("モンスター情報を圧縮しています...", "Compacting monsters..."));
101
102     /* Compact at least 'size' objects */
103     floor_type *floor_ptr = player_ptr->current_floor_ptr;
104     for (int num = 0, cnt = 1; num < size; cnt++) {
105         int cur_lev = 5 * cnt;
106         int cur_dis = 5 * (20 - cnt);
107         for (MONSTER_IDX i = 1; i < floor_ptr->m_max; i++) {
108             monster_type *m_ptr = &floor_ptr->m_list[i];
109             monster_race *r_ptr = &r_info[m_ptr->r_idx];
110             if (!monster_is_valid(m_ptr))
111                 continue;
112             if (r_ptr->level > cur_lev)
113                 continue;
114             if (i == player_ptr->riding)
115                 continue;
116             if ((cur_dis > 0) && (m_ptr->cdis < cur_dis))
117                 continue;
118
119             int chance = 90;
120             if ((r_ptr->flags1 & (RF1_QUESTOR)) && (cnt < 1000))
121                 chance = 100;
122
123             if (r_ptr->flags1 & (RF1_UNIQUE))
124                 chance = 100;
125
126             if (randint0(100) < chance)
127                 continue;
128
129             if (record_named_pet && is_pet(m_ptr) && m_ptr->nickname) {
130                 GAME_TEXT m_name[MAX_NLEN];
131                 monster_desc(player_ptr, m_name, m_ptr, MD_INDEF_VISIBLE);
132                 exe_write_diary(player_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_COMPACT, m_name);
133             }
134
135             delete_monster_idx(player_ptr, i);
136             num++;
137         }
138     }
139
140     /* Excise dead monsters (backwards!) */
141     for (MONSTER_IDX i = floor_ptr->m_max - 1; i >= 1; i--) {
142         monster_type *m_ptr = &floor_ptr->m_list[i];
143         if (m_ptr->r_idx)
144             continue;
145         compact_monsters_aux(player_ptr, floor_ptr->m_max - 1, i);
146         floor_ptr->m_max--;
147     }
148 }