OSDN Git Service

Merge pull request #3532 from sikabane-works/release/3.0.0.87-alpha
[hengbandforosx/hengbandosx.git] / src / util / object-sort.cpp
1 /*!
2  * @brief オブジェクトのソート処理
3  * @date 2020/06/03
4  * @author Hourier
5  */
6
7 #include "util/object-sort.h"
8 #include "monster-race/monster-race.h"
9 #include "object/object-value.h"
10 #include "object/tval-types.h"
11 #include "perception/object-perception.h"
12 #include "player/player-realm.h"
13 #include "system/item-entity.h"
14 #include "system/monster-race-info.h"
15 #include "system/player-type-definition.h"
16
17 static int get_item_sort_rank(const ItemEntity &item)
18 {
19     if (item.is_fixed_artifact()) {
20         return 3;
21     }
22
23     if (item.is_random_artifact()) {
24         return 2;
25     }
26
27     if (item.is_ego()) {
28         return 1;
29     }
30
31     return 0;
32 }
33
34 /*!
35  * @brief オブジェクトを定義された基準に従いソートするための関数 /
36  * Check if we have space for an item in the pack without overflow
37  * @param o_ptr 比較対象オブジェクトの構造体参照ポインタ1
38  * @param o_value o_ptrのアイテム価値(手動であらかじめ代入する必要がある?)
39  * @param j_ptr 比較対象オブジェクトの構造体参照ポインタ2
40  * @return o_ptrの方が上位ならばTRUEを返す。
41  */
42 bool object_sort_comp(PlayerType *player_ptr, ItemEntity *o_ptr, int32_t o_value, ItemEntity *j_ptr)
43 {
44     if (!j_ptr->is_valid()) {
45         return true;
46     }
47
48     const auto o_tval = o_ptr->bi_key.tval();
49     const auto j_tval = j_ptr->bi_key.tval();
50     if ((o_tval == get_realm1_book(player_ptr)) && (j_tval != get_realm1_book(player_ptr))) {
51         return true;
52     }
53
54     if ((j_tval == get_realm1_book(player_ptr)) && (o_tval != get_realm1_book(player_ptr))) {
55         return false;
56     }
57
58     if ((o_tval == get_realm2_book(player_ptr)) && (j_tval != get_realm2_book(player_ptr))) {
59         return true;
60     }
61
62     if ((j_tval == get_realm2_book(player_ptr)) && (o_tval != get_realm2_book(player_ptr))) {
63         return false;
64     }
65
66     if (o_tval > j_tval) {
67         return true;
68     }
69
70     if (o_tval < j_tval) {
71         return false;
72     }
73
74     if (!o_ptr->is_aware()) {
75         return false;
76     }
77
78     if (!j_ptr->is_aware()) {
79         return true;
80     }
81
82     const auto o_sval = o_ptr->bi_key.sval();
83     const auto j_sval = j_ptr->bi_key.sval();
84     if (o_sval < j_sval) {
85         return true;
86     }
87
88     if (o_sval > j_sval) {
89         return false;
90     }
91
92     if (!o_ptr->is_known()) {
93         return false;
94     }
95
96     if (!j_ptr->is_known()) {
97         return true;
98     }
99
100     const auto o_rank = get_item_sort_rank(*o_ptr);
101     const auto j_rank = get_item_sort_rank(*j_ptr);
102     if (o_rank < j_rank) {
103         return true;
104     }
105
106     if (o_rank > j_rank) {
107         return false;
108     }
109
110     switch (o_tval) {
111     case ItemKindType::FIGURINE:
112     case ItemKindType::STATUE:
113     case ItemKindType::CORPSE:
114     case ItemKindType::CAPTURE: {
115         auto o_r_idx = i2enum<MonsterRaceId>(o_ptr->pval);
116         auto j_r_idx = i2enum<MonsterRaceId>(j_ptr->pval);
117         if (monraces_info[o_r_idx].level < monraces_info[j_r_idx].level) {
118             return true;
119         }
120
121         if ((monraces_info[o_r_idx].level == monraces_info[j_r_idx].level) && (o_ptr->pval < j_ptr->pval)) {
122             return true;
123         }
124
125         return false;
126     }
127     case ItemKindType::SHOT:
128     case ItemKindType::ARROW:
129     case ItemKindType::BOLT:
130         if (o_ptr->to_h + o_ptr->to_d < j_ptr->to_h + j_ptr->to_d) {
131             return true;
132         }
133
134         if (o_ptr->to_h + o_ptr->to_d > j_ptr->to_h + j_ptr->to_d) {
135             return false;
136         }
137
138         break;
139     case ItemKindType::ROD:
140         if (o_ptr->pval < j_ptr->pval) {
141             return true;
142         }
143
144         if (o_ptr->pval > j_ptr->pval) {
145             return false;
146         }
147
148         break;
149     default:
150         break;
151     }
152
153     return o_value > j_ptr->get_price();
154 }