1 #include "smith/object-smith.h"
2 #include "object-enchant/special-object-flags.h"
3 #include "object-enchant/tr-flags.h"
4 #include "object-enchant/tr-types.h"
5 #include "object/item-tester-hooker.h"
6 #include "object/object-flags.h"
7 #include "perception/object-perception.h"
8 #include "player-base/player-class.h"
9 #include "player-info/smith-data-type.h"
10 #include "smith/smith-info.h"
11 #include "smith/smith-tables.h"
12 #include "smith/smith-types.h"
13 #include "system/item-entity.h"
14 #include "system/player-type-definition.h"
18 #include <unordered_map>
24 * @brief 所持しているエッセンスで付与可能な回数を取得する
26 * @param essence_list 使用するエッセンスのリスト
27 * @param consumption 1種あたりに使用する消費量
28 * @return 所持しているエッセンスで付与可能な回数を返す
30 int addable_count(smith_data_type *smith_data, std::vector<SmithEssenceType> essence_list, int consumption)
32 if (consumption <= 0) {
36 std::vector<int> addable_count;
37 for (auto essence : essence_list) {
38 int own_amount = smith_data->essences[essence];
39 addable_count.push_back(own_amount / consumption);
41 return *std::min_element(addable_count.begin(), addable_count.end());
49 Smith::Smith(PlayerType *player_ptr)
50 : player_ptr(player_ptr)
51 , smith_data(PlayerClass(player_ptr).get_specific_data<smith_data_type>())
56 * @brief 引数で指定した鍛冶効果の鍛冶情報を得る
58 * @param effect 情報を得る鍛冶効果
59 * @return 鍛冶情報構造体へのポインタを保持する std::optional オブジェクトを返す
61 std::optional<const ISmithInfo *> Smith::find_smith_info(SmithEffectType effect)
63 // 何度も呼ぶので線形探索を避けるため鍛冶効果から鍛冶情報のテーブルを引けるmapを作成しておく。
64 static std::unordered_map<SmithEffectType, const ISmithInfo *> search_map;
65 if (search_map.empty()) {
66 for (const auto &info : smith_info_table) {
67 search_map.emplace(info->effect, info.get());
71 auto it = search_map.find(effect);
72 if (it == search_map.end()) {
80 * @brief 指定したエッセンスの表記名を取得する
82 * @param essence 表記名を取得するエッセンス
83 * @return 表記名を表す文字列へのポインタ
85 concptr Smith::get_essence_name(SmithEssenceType essence)
87 auto it = essence_to_name.find(essence);
88 auto essence_name = it != essence_to_name.end() ? it->second : _("不明", "Unknown");
93 * @brief 指定した鍛冶効果の表記名を取得する
95 * @param effect 表記名を取得する鍛冶効果
96 * @return 表記名を表す文字列へのポインタ
98 concptr Smith::get_effect_name(SmithEffectType effect)
100 auto info = find_smith_info(effect);
101 if (!info.has_value()) {
102 return _("不明", "Unknown");
105 return info.value()->name;
109 * @brief 指定した鍛冶効果の付与に必要なエッセンスの表記を取得する。複数のエッセンスが必要な場合は "+" で連結されたものとなる。
112 * @return 鍛冶効果の付与に必要なエッセンスを表記する std::string オブジェクト
114 std::string Smith::get_need_essences_desc(SmithEffectType effect)
116 auto info = find_smith_info(effect);
117 if (!info.has_value() || info.value()->need_essences.empty()) {
118 return _("不明", "Unknown");
121 const auto &need_essences = info.value()->need_essences;
122 std::stringstream ss;
123 for (auto i = 0U; i < need_essences.size(); i++) {
124 ss << Smith::get_essence_name(need_essences[i]);
125 if (i < need_essences.size() - 1) {
134 * @brief 鍛冶効果を付与するのに必要なエッセンスのリストを返す
137 * @return 鍛冶効果を付与するのに必要なエッセンスのリスト
139 std::vector<SmithEssenceType> Smith::get_need_essences(SmithEffectType effect)
141 auto info = find_smith_info(effect);
142 if (!info.has_value()) {
146 return info.value()->need_essences;
150 * @brief 鍛冶効果を付与する時のエッセンス消費量を取得する
151 * 複数のエッセンスを消費する鍛冶効果の場合は全てのエッセンスからこの量が消費される
152 * アイテムが複数ある場合はアイテムの個数倍の消費量となる
155 * @param o_ptr 鍛冶効果を付与するアイテムへのポインタ。nullptrの場合はデフォルトの消費量が返される。
156 * @return 鍛冶効果を付与する時のエッセンス消費量
158 int Smith::get_essence_consumption(SmithEffectType effect, const ItemEntity *o_ptr)
160 auto info = find_smith_info(effect);
161 if (!info.has_value()) {
165 auto consumption = info.value()->consumption;
166 if (o_ptr == nullptr) {
170 if (o_ptr->is_ammo()) {
171 consumption = (consumption + 9) / 10;
174 consumption *= o_ptr->number;
180 * @brief 鍛冶効果の対象となるアイテムを絞り込むための ItemTester クラスを取得する
183 * @return ItemTesterクラスのオブジェクトをstd::unique_ptrに格納したものを返す
185 std::unique_ptr<ItemTester> Smith::get_item_tester(SmithEffectType effect)
187 auto info = find_smith_info(effect);
188 if (!info.has_value()) {
189 return std::make_unique<TvalItemTester>(ItemKindType::NONE);
192 auto tester_func = [i = info.value()](const ItemEntity *o_ptr) {
193 return i->can_give_smith_effect(o_ptr);
195 return std::make_unique<FuncItemTester>(tester_func);
199 * @brief 鍛冶効果により得られる特性フラグを取得する
202 * @return 鍛冶効果により得られる特性フラグがONになったTrFlagsオブジェクト
204 TrFlags Smith::get_effect_tr_flags(SmithEffectType effect)
206 auto info = find_smith_info(effect);
207 if (!info.has_value()) {
211 return info.value()->tr_flags();
215 * @brief アイテムに付与されている発動効果の発動IDを得る
217 * @param o_ptr アイテム構造体へのポインタ
218 * @return アイテムに付与されている発動効果の発動ID(random_art_activation_type型)
219 * 付与されている発動効果が無い場合は std::nullopt
221 std::optional<RandomArtActType> Smith::object_activation(const ItemEntity *o_ptr)
223 return o_ptr->smith_act_idx;
227 * @brief アイテムに付与されている鍛冶効果を取得する
229 * @param o_ptr アイテム構造体へのポインタ
230 * @return アイテムに付与されている鍛冶効果を保持する std::optional オブジェクト返す。
231 * 鍛冶効果が付与できないアイテムか、何も付与されていなければ std::nullopt を返す。
233 std::optional<SmithEffectType> Smith::object_effect(const ItemEntity *o_ptr)
235 return o_ptr->smith_effect;
239 * @brief 指定した鍛冶カテゴリの鍛冶効果のリストを取得する
241 * @param category 鍛冶カテゴリ
242 * @return 指定した鍛冶カテゴリの鍛冶効果のリスト
244 std::vector<SmithEffectType> Smith::get_effect_list(SmithCategoryType category)
246 std::vector<SmithEffectType> result;
248 for (const auto &info : smith_info_table) {
249 if (info->category == category) {
250 result.push_back(info->effect);
258 * @brief 指定した鍛冶効果のエッセンスを付与できる回数を取得する
261 * @param o_ptr 鍛冶効果を付与するアイテムへのポインタ。nullptrの場合はデフォルトの消費量での回数が返される。
262 * @return エッセンスを付与できる回数を返す
264 int Smith::get_addable_count(SmithEffectType effect, const ItemEntity *o_ptr) const
266 auto info = find_smith_info(effect);
267 if (!info.has_value()) {
271 auto consumption = Smith::get_essence_consumption(effect, o_ptr);
273 return addable_count(this->smith_data.get(), info.value()->need_essences, consumption);
277 * @brief 鍛冶エッセンスの全リストを取得する
279 * @return 鍛冶エッセンスの全リスト
281 const std::vector<SmithEssenceType> &Smith::get_essence_list()
283 return essence_list_order;
287 * @brief 指定したエッセンスの所持量を取得する
289 * @param essence 所持量を取得するエッセンス
292 int Smith::get_essence_num_of_posessions(SmithEssenceType essence) const
294 return this->smith_data->essences[essence];
300 * @param o_ptr エッセンスの抽出を行うアイテムへのポインタ
301 * @return 抽出したエッセンスと抽出した量のタプルのリストを返す
303 Smith::DrainEssenceResult Smith::drain_essence(ItemEntity *o_ptr)
305 // 抽出量を揃えるためKILLフラグのみ付いている場合はSLAYフラグも付ける
306 auto old_flags = object_flags(o_ptr);
307 if (old_flags.has(TR_KILL_DRAGON)) {
308 old_flags.set(TR_SLAY_DRAGON);
310 if (old_flags.has(TR_KILL_ANIMAL)) {
311 old_flags.set(TR_SLAY_ANIMAL);
313 if (old_flags.has(TR_KILL_EVIL)) {
314 old_flags.set(TR_SLAY_EVIL);
316 if (old_flags.has(TR_KILL_UNDEAD)) {
317 old_flags.set(TR_SLAY_UNDEAD);
319 if (old_flags.has(TR_KILL_DEMON)) {
320 old_flags.set(TR_SLAY_DEMON);
322 if (old_flags.has(TR_KILL_ORC)) {
323 old_flags.set(TR_SLAY_ORC);
325 if (old_flags.has(TR_KILL_TROLL)) {
326 old_flags.set(TR_SLAY_TROLL);
328 if (old_flags.has(TR_KILL_GIANT)) {
329 old_flags.set(TR_SLAY_GIANT);
331 if (old_flags.has(TR_KILL_HUMAN)) {
332 old_flags.set(TR_SLAY_HUMAN);
334 if (old_flags.has(TR_KILL_GOOD)) {
335 old_flags.set(TR_SLAY_GOOD);
338 // マイナス効果のあるアイテムから抽出する時のペナルティを計算
340 if (o_ptr->curse_flags.has_any_of({ CurseTraitType::CURSED, CurseTraitType::HEAVY_CURSE, CurseTraitType::PERMA_CURSE })) {
344 for (auto &&info : essence_drain_info_table) {
345 if (info.amount < 0 && old_flags.has(info.tr_flag)) {
350 const auto is_fixed_or_random_artifact = o_ptr->is_fixed_or_random_artifact();
352 // アイテムをエッセンス抽出後の状態にする
353 const ItemEntity old_o = *o_ptr;
354 o_ptr->prep(o_ptr->bi_id);
355 o_ptr->iy = old_o.iy;
356 o_ptr->ix = old_o.ix;
357 o_ptr->marked = old_o.marked;
358 o_ptr->number = old_o.number;
359 o_ptr->discount = old_o.discount;
360 if (o_ptr->bi_key.tval() == ItemKindType::DRAG_ARMOR) {
361 o_ptr->timeout = old_o.timeout;
364 o_ptr->ident |= (IDENT_FULL_KNOWN);
365 object_aware(player_ptr, o_ptr);
368 auto new_flags = object_flags(o_ptr);
370 std::unordered_map<SmithEssenceType, int> drain_values;
373 for (auto &&info : essence_drain_info_table) {
375 if (TR_PVAL_FLAG_MASK.has(info.tr_flag) && old_o.pval > 0) {
376 pval = new_flags.has(info.tr_flag) ? old_o.pval - o_ptr->pval : old_o.pval;
379 if ((new_flags.has_not(info.tr_flag) || pval) && old_flags.has(info.tr_flag)) {
380 for (auto &&essence : info.essences) {
381 auto mult = TR_PVAL_FLAG_MASK.has(info.tr_flag) ? pval : 1;
382 drain_values[essence] += info.amount * mult;
387 if (is_fixed_or_random_artifact) {
388 drain_values[SmithEssenceType::UNIQUE] += 10;
391 // ダイス/命中/ダメージ/ACからの抽出
392 auto diff = [](int o, int n) { return std::max(o - n, 0); };
394 if (o_ptr->is_weapon_ammo()) {
395 drain_values[SmithEssenceType::ATTACK] += diff(old_o.ds, o_ptr->ds) * 10;
396 drain_values[SmithEssenceType::ATTACK] += diff(old_o.dd, o_ptr->dd) * 10;
399 drain_values[SmithEssenceType::ATTACK] += diff(old_o.to_h, o_ptr->to_h) * 10;
400 drain_values[SmithEssenceType::ATTACK] += diff(old_o.to_d, o_ptr->to_d) * 10;
401 drain_values[SmithEssenceType::AC] += diff(old_o.ac, o_ptr->ac) * 10;
402 drain_values[SmithEssenceType::AC] += diff(old_o.to_a, o_ptr->to_a) * 10;
404 // 個数/矢弾/マイナス効果のペナルティによる抽出量の調整
405 for (auto &&[unuse, val] : drain_values) {
406 val *= o_ptr->number;
408 val = std::max(val, 0);
409 if (o_ptr->is_ammo()) {
415 std::vector<std::tuple<SmithEssenceType, int>> result;
417 for (auto essence : essence_list_order) {
418 auto drain_value = drain_values[essence];
419 if (drain_value <= 0) {
423 this->smith_data->essences[essence] = std::min<int16_t>(Smith::ESSENCE_AMOUNT_MAX, this->smith_data->essences[essence] + drain_value);
424 result.emplace_back(essence, drain_value);
433 * @param effect 付与する鍛冶効果
434 * @param o_ptr 鍛冶効果を付与するアイテムへのポインタ
435 * @param number エッセンス付与数
436 * @return 鍛冶効果の付与に成功したら ture、失敗したら false を返す
438 bool Smith::add_essence(SmithEffectType effect, ItemEntity *o_ptr, int number)
440 auto info = find_smith_info(effect);
441 if (!info.has_value()) {
445 const auto total_consumption = this->get_essence_consumption(effect, o_ptr) * number;
446 for (auto &&essence : info.value()->need_essences) {
447 this->smith_data->essences[essence] -= static_cast<int16_t>(total_consumption);
450 return info.value()->add_essence(this->player_ptr, o_ptr, number);
456 * @param o_ptr 鍛冶効果を消去するアイテムへのポインタ
458 void Smith::erase_essence(ItemEntity *o_ptr) const
460 o_ptr->smith_act_idx = std::nullopt;
462 auto effect = Smith::object_effect(o_ptr);
463 if (!effect.has_value()) {
466 auto info = find_smith_info(effect.value());
467 if (!info.has_value()) {
471 info.value()->erase_essence(o_ptr);