OSDN Git Service

Replace sprintf() simply. Does part of the work to resolve https://github.com/hengba...
[hengbandforosx/hengbandosx.git] / src / lore / lore-calculator.cpp
index 7f3e44e..91d04c2 100644 (file)
@@ -5,8 +5,9 @@
 #include "monster-race/race-ability-flags.h"
 #include "monster-race/race-flags1.h"
 #include "mspell/mspell-damage-calculator.h"
-#include "system/monster-race-definition.h"
+#include "system/monster-race-info.h"
 #include "system/player-type-definition.h"
+#include "util/string-processor.h"
 
 /*!
  * @brief ダイス目を文字列に変換する
@@ -27,20 +28,20 @@ void dice_to_string(int base_damage, int dice_num, int dice_side, int dice_mult,
     }
 
     if (base_damage != 0) {
-        sprintf(base, "%d+", base_damage);
+        strnfmt(base, sizeof(base), "%d+", base_damage);
     }
 
     if (dice_num == 1) {
-        sprintf(dice, "d%d", dice_side);
+        strnfmt(dice, sizeof(dice), "d%d", dice_side);
     } else {
-        sprintf(dice, "%dd%d", dice_num, dice_side);
+        strnfmt(dice, sizeof(dice), "%dd%d", dice_num, dice_side);
     }
 
     if (dice_mult != 1 || dice_div != 1) {
         if (dice_div == 1) {
-            sprintf(mult, "*%d", dice_mult);
+            strnfmt(mult, sizeof(mult), "*%d", dice_mult);
         } else {
-            sprintf(mult, "*(%d/%d)", dice_mult, dice_div);
+            strnfmt(mult, sizeof(mult), "*(%d/%d)", dice_mult, dice_div);
         }
     }
 
@@ -55,9 +56,9 @@ void dice_to_string(int base_damage, int dice_num, int dice_side, int dice_mult,
  * @details
  * The higher the level, the fewer kills needed.
  */
-bool know_armour(MONRACE_IDX r_idx, const bool know_everything)
+bool know_armour(MonsterRaceId r_idx, const bool know_everything)
 {
-    auto *r_ptr = &r_info[r_idx];
+    auto *r_ptr = &monraces_info[r_idx];
     DEPTH level = r_ptr->level;
     MONSTER_NUMBER kills = r_ptr->r_tkills;
 
@@ -90,9 +91,9 @@ bool know_armour(MONRACE_IDX r_idx, const bool know_everything)
  * the more damage an attack does, the more attacks you need
  * </pre>
  */
-bool know_damage(MONRACE_IDX r_idx, int i)
+bool know_damage(MonsterRaceId r_idx, int i)
 {
-    auto *r_ptr = &r_info[r_idx];
+    auto *r_ptr = &monraces_info[r_idx];
     DEPTH level = r_ptr->level;
     int32_t a = r_ptr->r_blows[i];
 
@@ -124,22 +125,24 @@ bool know_damage(MONRACE_IDX r_idx, int i)
  */
 void set_damage(PlayerType *player_ptr, lore_type *lore_ptr, MonsterAbilityType ms_type, concptr msg)
 {
-    MONRACE_IDX r_idx = lore_ptr->r_idx;
+    MonsterRaceId r_idx = lore_ptr->r_idx;
+    char *tmp = lore_ptr->tmp_msg[lore_ptr->vn];
+    size_t tmpsz = sizeof(lore_ptr->tmp_msg[lore_ptr->vn]);
+
+    if (!know_armour(r_idx, lore_ptr->know_everything)) {
+        strnfmt(tmp, tmpsz, msg, "");
+        return;
+    }
+
     int base_damage = monspell_race_damage(player_ptr, ms_type, r_idx, BASE_DAM);
     int dice_num = monspell_race_damage(player_ptr, ms_type, r_idx, DICE_NUM);
     int dice_side = monspell_race_damage(player_ptr, ms_type, r_idx, DICE_SIDE);
     int dice_mult = monspell_race_damage(player_ptr, ms_type, r_idx, DICE_MULT);
     int dice_div = monspell_race_damage(player_ptr, ms_type, r_idx, DICE_DIV);
-    char dmg_str[80], dice_str[sizeof(dmg_str) + 10];
-    char *tmp = lore_ptr->tmp_msg[lore_ptr->vn];
-    dice_to_string(base_damage, dice_num, dice_side, dice_mult, dice_div, dmg_str);
-    sprintf(dice_str, "(%s)", dmg_str);
-
-    if (know_armour(r_idx, lore_ptr->know_everything)) {
-        sprintf(tmp, msg, dice_str);
-    } else {
-        sprintf(tmp, msg, "");
-    }
+    char dmg_str[80] = "(";
+    dice_to_string(base_damage, dice_num, dice_side, dice_mult, dice_div, dmg_str + 1);
+    angband_strcat(dmg_str, ")", sizeof(dmg_str));
+    strnfmt(tmp, tmpsz, msg, dmg_str);
 }
 
 void set_drop_flags(lore_type *lore_ptr)
@@ -148,13 +151,13 @@ void set_drop_flags(lore_type *lore_ptr)
         return;
     }
 
-    lore_ptr->drop_gold = lore_ptr->drop_item = (((lore_ptr->r_ptr->flags1 & RF1_DROP_4D2) ? 8 : 0) + ((lore_ptr->r_ptr->flags1 & RF1_DROP_3D2) ? 6 : 0) + ((lore_ptr->r_ptr->flags1 & RF1_DROP_2D2) ? 4 : 0) + ((lore_ptr->r_ptr->flags1 & RF1_DROP_1D2) ? 2 : 0) + ((lore_ptr->r_ptr->flags1 & RF1_DROP_90) ? 1 : 0) + ((lore_ptr->r_ptr->flags1 & RF1_DROP_60) ? 1 : 0));
+    lore_ptr->drop_gold = lore_ptr->drop_item = ((lore_ptr->r_ptr->drop_flags.has(MonsterDropType::DROP_4D2) ? 8 : 0) + (lore_ptr->r_ptr->drop_flags.has(MonsterDropType::DROP_3D2) ? 6 : 0) + (lore_ptr->r_ptr->drop_flags.has(MonsterDropType::DROP_2D2) ? 4 : 0) + (lore_ptr->r_ptr->drop_flags.has(MonsterDropType::DROP_1D2) ? 2 : 0) + (lore_ptr->r_ptr->drop_flags.has(MonsterDropType::DROP_90) ? 1 : 0) + (lore_ptr->r_ptr->drop_flags.has(MonsterDropType::DROP_60) ? 1 : 0));
 
-    if (lore_ptr->r_ptr->flags1 & RF1_ONLY_GOLD) {
+    if (lore_ptr->r_ptr->drop_flags.has(MonsterDropType::ONLY_GOLD)) {
         lore_ptr->drop_item = 0;
     }
 
-    if (lore_ptr->r_ptr->flags1 & RF1_ONLY_ITEM) {
+    if (lore_ptr->r_ptr->drop_flags.has(MonsterDropType::ONLY_ITEM)) {
         lore_ptr->drop_gold = 0;
     }
 
@@ -165,4 +168,5 @@ void set_drop_flags(lore_type *lore_ptr)
     lore_ptr->aura_flags = lore_ptr->r_ptr->aura_flags;
     lore_ptr->behavior_flags = lore_ptr->r_ptr->behavior_flags;
     lore_ptr->resistance_flags = lore_ptr->r_ptr->resistance_flags;
+    lore_ptr->feature_flags = lore_ptr->r_ptr->feature_flags;
 }