Skip to content

Commit 04ce8a9

Browse files
committed
clang-tidy: simple cleanup of items
1 parent a977a76 commit 04ce8a9

File tree

2 files changed

+77
-40
lines changed

2 files changed

+77
-40
lines changed

Source/items.cpp

Lines changed: 76 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,85 @@
66
#include "items.h"
77

88
#include <algorithm>
9-
#ifdef _DEBUG
10-
#include <random>
11-
#endif
9+
#include <array>
10+
#include <cassert>
1211
#include <climits>
12+
#include <cstddef>
1313
#include <cstdint>
14-
14+
#include <cstring>
15+
#include <optional>
16+
#include <string>
17+
#include <string_view>
18+
#include <type_traits>
19+
#include <utility>
20+
#include <vector>
21+
22+
#include <SDL.h>
1523
#include <fmt/core.h>
16-
#include <fmt/format.h>
1724

1825
#include "DiabloUI/ui_flags.hpp"
26+
#include "control.h"
1927
#include "controls/control_mode.hpp"
20-
#include "controls/plrctrls.h"
28+
#include "controls/controller_buttons.h"
2129
#include "cursor.h"
30+
#include "diablo.h"
2231
#include "doom.h"
32+
#include "effects.h"
33+
#include "engine/animationinfo.h"
2334
#include "engine/backbuffer_state.hpp"
2435
#include "engine/clx_sprite.hpp"
25-
#include "engine/dx.h"
2636
#include "engine/load_cel.hpp"
37+
#include "engine/path.h"
38+
#include "engine/point.hpp"
2739
#include "engine/random.hpp"
2840
#include "engine/render/clx_render.hpp"
2941
#include "engine/render/primitive_render.hpp"
3042
#include "engine/render/text_render.hpp"
43+
#include "engine/surface.hpp"
44+
#include "engine/world_tile.hpp"
45+
#include "function_ref.hpp"
3146
#include "game_mode.hpp"
3247
#include "headless_mode.hpp"
48+
#include "inv.h"
3349
#include "inv_iterators.hpp"
50+
#include "itemdat.h"
51+
#include "items/validation.h"
52+
#include "levels/gendung.h"
53+
#include "levels/gendung_defs.hpp"
3454
#include "levels/tile_properties.hpp"
3555
#include "levels/town.h"
3656
#include "lighting.h"
3757
#include "minitext.h"
38-
#include "missiles.h"
58+
#include "monstdat.h"
59+
#include "monster.h"
60+
#include "msg.h"
61+
#include "multi.h"
62+
#include "objdat.h"
63+
#include "objects.h"
3964
#include "options.h"
65+
#include "pack.h"
4066
#include "panels/info_box.hpp"
4167
#include "panels/ui_panels.hpp"
4268
#include "player.h"
4369
#include "playerdat.hpp"
4470
#include "qol/stash.h"
71+
#include "quests.h"
72+
#include "sound_effect_enums.h"
73+
#include "spelldat.h"
4574
#include "spells.h"
4675
#include "stores.h"
47-
#include "utils/algorithm/container.hpp"
76+
#include "textdat.h"
77+
#include "utils/enum_traits.h"
4878
#include "utils/format_int.hpp"
4979
#include "utils/is_of.hpp"
5080
#include "utils/language.h"
5181
#include "utils/log.hpp"
5282
#include "utils/math.h"
53-
#include "utils/str_case.hpp"
83+
#include "utils/sdl_geometry.h"
84+
#include "utils/static_vector.hpp"
5485
#include "utils/str_cat.hpp"
5586
#include "utils/str_split.hpp"
87+
#include "utils/string_or_view.hpp"
5688
#include "utils/utf8.hpp"
5789

5890
namespace devilution {
@@ -1005,7 +1037,7 @@ int PLVal(int pv, int p1, int p2, int minv, int maxv)
10051037
return minv;
10061038
if (minv == maxv)
10071039
return minv;
1008-
return minv + (maxv - minv) * (100 * (pv - p1) / (p2 - p1)) / 100;
1040+
return minv + ((maxv - minv) * (100 * (pv - p1) / (p2 - p1)) / 100);
10091041
}
10101042

10111043
void SaveItemAffix(const Player &player, Item &item, const PLStruct &affix)
@@ -1121,10 +1153,12 @@ std::string GenerateMagicItemName(const std::string_view &baseNamel, const PLStr
11211153
if (pPrefix != nullptr && pSufix != nullptr) {
11221154
const std::string_view fmt = translate ? _(/* TRANSLATORS: Constructs item names. Format: {Prefix} {Item} of {Suffix}. Example: King's Long Sword of the Whale */ "{0} {1} of {2}") : "{0} {1} of {2}";
11231155
return fmt::format(fmt::runtime(fmt), translate ? _(pPrefix->PLName) : pPrefix->PLName, baseNamel, translate ? _(pSufix->PLName) : pSufix->PLName);
1124-
} else if (pPrefix != nullptr) {
1156+
}
1157+
if (pPrefix != nullptr) {
11251158
const std::string_view fmt = translate ? _(/* TRANSLATORS: Constructs item names. Format: {Prefix} {Item}. Example: King's Long Sword */ "{0} {1}") : "{0} {1}";
11261159
return fmt::format(fmt::runtime(fmt), translate ? _(pPrefix->PLName) : pPrefix->PLName, baseNamel);
1127-
} else if (pSufix != nullptr) {
1160+
}
1161+
if (pSufix != nullptr) {
11281162
const std::string_view fmt = translate ? _(/* TRANSLATORS: Constructs item names. Format: {Item} of {Suffix}. Example: Long Sword of the Whale */ "{0} of {1}") : "{0} of {1}";
11291163
return fmt::format(fmt::runtime(fmt), baseNamel, translate ? _(pSufix->PLName) : pSufix->PLName);
11301164
}
@@ -1269,8 +1303,7 @@ void GetOilType(Item &item, int maxLvl)
12691303

12701304
void GetItemBonus(const Player &player, Item &item, int minlvl, int maxlvl, bool onlygood, bool allowspells)
12711305
{
1272-
if (minlvl > 25)
1273-
minlvl = 25;
1306+
minlvl = std::min(minlvl, 25);
12741307

12751308
switch (item._itype) {
12761309
case ItemType::Sword:
@@ -1331,7 +1364,7 @@ _item_indexes GetItemIndexForDroppableItem(bool considerDropRate, tl::function_r
13311364
cumulativeWeight += considerDropRate ? item.dropRate : 1;
13321365
ril.push_back({ static_cast<_item_indexes>(i), cumulativeWeight });
13331366
}
1334-
const unsigned targetWeight = static_cast<unsigned>(RandomIntLessThan(static_cast<int>(cumulativeWeight)));
1367+
const auto targetWeight = static_cast<unsigned>(RandomIntLessThan(static_cast<int>(cumulativeWeight)));
13351368
return std::upper_bound(ril.begin(), ril.end(), targetWeight, [](unsigned target, const WeightedItemIndex &value) { return target < value.cumulativeWeight; })->index;
13361369
}
13371370

@@ -1830,7 +1863,7 @@ void PrintItemInfo(const Item &item)
18301863
}
18311864
}
18321865

1833-
bool SmithItemOk(const Player &player, const ItemData &item)
1866+
bool SmithItemOk(const Player & /*player*/, const ItemData &item)
18341867
{
18351868
if (item.itype == ItemType::Misc)
18361869
return false;
@@ -1872,7 +1905,7 @@ void SortVendor(Item *itemList, size_t count)
18721905
std::sort(itemList, itemList + count, cmp);
18731906
}
18741907

1875-
bool PremiumItemOk(const Player &player, const ItemData &item)
1908+
bool PremiumItemOk(const Player & /*player*/, const ItemData &item)
18761909
{
18771910
if (item.itype == ItemType::Misc)
18781911
return false;
@@ -1971,7 +2004,7 @@ void SpawnOnePremium(Item &premiumItem, int plvl, const Player &player)
19712004
premiumItem._iStatFlag = player.CanUseItem(premiumItem);
19722005
}
19732006

1974-
bool WitchItemOk(const Player &player, const ItemData &item)
2007+
bool WitchItemOk(const Player & /*player*/, const ItemData &item)
19752008
{
19762009
if (IsNoneOf(item.itype, ItemType::Misc, ItemType::Staff))
19772010
return false;
@@ -2178,12 +2211,15 @@ StringOrView GetTranslatedItemName(const Item &item)
21782211

21792212
if (item._iCreateInfo == 0) {
21802213
return _(baseItemData.iName);
2181-
} else if (item._iMiscId == IMISC_BOOK) {
2214+
}
2215+
if (item._iMiscId == IMISC_BOOK) {
21822216
const std::string_view spellName = pgettext("spell", GetSpellData(item._iSpell).sNameText);
21832217
return fmt::format(fmt::runtime(_(/* TRANSLATORS: {:s} will be a spell name */ "Book of {:s}")), spellName);
2184-
} else if (item._iMiscId == IMISC_EAR) {
2218+
}
2219+
if (item._iMiscId == IMISC_EAR) {
21852220
return fmt::format(fmt::runtime(_(/* TRANSLATORS: {:s} will be a Character Name */ "Ear of {:s}")), item._iIName);
2186-
} else if (item._iMiscId > IMISC_OILFIRST && item._iMiscId < IMISC_OILLAST) {
2221+
}
2222+
if (item._iMiscId > IMISC_OILFIRST && item._iMiscId < IMISC_OILLAST) {
21872223
for (size_t i = 0; i < 10; i++) {
21882224
if (OilMagic[i] != item._iMiscId)
21892225
continue;
@@ -2229,14 +2265,13 @@ std::string GetTranslatedItemNameMagical(const Item &item, bool hellfireItem, bo
22292265
maxlvl = iblvl;
22302266
} else {
22312267
DiscardRandomValues(1); // GetItemAttrs
2232-
const int iblvl = GetItemBLevel(lvl, item._iMiscId, onlygood, item._iCreateInfo & CF_UPER15);
2268+
const int iblvl = GetItemBLevel(lvl, item._iMiscId, onlygood, (item._iCreateInfo & CF_UPER15) != 0);
22332269
minlvl = iblvl / 2;
22342270
maxlvl = iblvl;
22352271
DiscardRandomValues(1); // CheckUnique
22362272
}
22372273

2238-
if (minlvl > 25)
2239-
minlvl = 25;
2274+
minlvl = std::min(minlvl, 25);
22402275

22412276
AffixItemType affixItemType = AffixItemType::None;
22422277

@@ -2900,7 +2935,7 @@ void InitializeItem(Item &item, _item_indexes itemData)
29002935
item._iPrePower = IPL_INVALID;
29012936
item._iSufPower = IPL_INVALID;
29022937
item._iMagical = ITEM_QUALITY_NORMAL;
2903-
item.IDidx = static_cast<_item_indexes>(itemData);
2938+
item.IDidx = itemData;
29042939
if (gbIsHellfire)
29052940
item.dwBuff |= CF_HELLFIRE;
29062941
}
@@ -3046,13 +3081,13 @@ uint8_t PlaceItemInWorld(Item &&item, WorldTilePosition position)
30463081
ActiveItemCount++;
30473082

30483083
dItem[position.x][position.y] = static_cast<int8_t>(ii + 1);
3049-
auto &item_ = Items[ii];
3050-
item_ = std::move(item);
3051-
item_.position = position;
3052-
RespawnItem(item_, true);
3084+
auto &newItem = Items[ii];
3085+
newItem = std::move(item);
3086+
newItem.position = position;
3087+
RespawnItem(newItem, true);
30533088

30543089
if (CornerStone.isAvailable() && position == CornerStone.position) {
3055-
CornerStone.item = item_;
3090+
CornerStone.item = newItem;
30563091
InitQTextMsg(TEXT_CORNSTN);
30573092
Quests[Q_CORNSTN]._qactive = QUEST_DONE;
30583093
}
@@ -3371,7 +3406,8 @@ void SpawnItem(Monster &monster, Point position, bool sendmsg, bool spawn /*= fa
33713406
if (uniqueItem != nullptr && sendmsg)
33723407
NetSendCmdPItem(false, CMD_DROPITEM, uniqueItem->position, *uniqueItem);
33733408
return;
3374-
} else if (monster.isUnique() || dropsSpecialTreasure) {
3409+
}
3410+
if (monster.isUnique() || dropsSpecialTreasure) {
33753411
// Unique monster is killed => use better item base (for example no gold)
33763412
idx = RndUItem(&monster);
33773413
} else if (dropBrain && !gbIsMultiplayer) {
@@ -3408,7 +3444,7 @@ void SpawnItem(Monster &monster, Point position, bool sendmsg, bool spawn /*= fa
34083444
const int uper = monster.isUnique() ? 15 : 1;
34093445

34103446
const int8_t mLevel = monster.data().level;
3411-
SetupAllItems(*MyPlayer, item, idx, AdvanceRndSeed(), mLevel, uper, onlygood, false, false);
3447+
SetupAllItems(*MyPlayer, item, idx, AdvanceRndSeed(), mLevel, uper, onlygood, false);
34123448
TryRandomUniqueItem(item, idx, mLevel, uper, onlygood, false);
34133449
SetupItem(item);
34143450

@@ -4041,7 +4077,7 @@ void DrawUniqueInfo(const Surface &out)
40414077
DrawString(out, _(uitem.UIName), rect, { .flags = UiFlags::AlignCenter });
40424078

40434079
const Rectangle dividerLineRect { position + Displacement { 26, 25 }, { 267, 3 } };
4044-
out.BlitFrom(out, MakeSdlRect(dividerLineRect), dividerLineRect.position + Displacement { 0, 5 * 12 + 13 });
4080+
out.BlitFrom(out, MakeSdlRect(dividerLineRect), dividerLineRect.position + Displacement { 0, (5 * 12) + 13 });
40454081

40464082
rect.position.y += (10 - uitem.UINumPL) * 12;
40474083
assert(uitem.UINumPL <= sizeof(uitem.powers) / sizeof(*uitem.powers));
@@ -4781,13 +4817,14 @@ StringOrView Item::getName() const
47814817
{
47824818
if (isEmpty()) {
47834819
return std::string_view("");
4784-
} else if (!_iIdentified || _iCreateInfo == 0 || _iMagical == ITEM_QUALITY_NORMAL) {
4820+
}
4821+
if (!_iIdentified || _iCreateInfo == 0 || _iMagical == ITEM_QUALITY_NORMAL) {
47854822
return GetTranslatedItemName(*this);
4786-
} else if (_iMagical == ITEM_QUALITY_UNIQUE) {
4823+
}
4824+
if (_iMagical == ITEM_QUALITY_UNIQUE) {
47874825
return _(UniqueItems[_iUid].UIName);
4788-
} else {
4789-
return GetTranslatedItemNameMagical(*this, dwBuff & CF_HELLFIRE, true, std::nullopt);
47904826
}
4827+
return GetTranslatedItemNameMagical(*this, dwBuff & CF_HELLFIRE, true, std::nullopt);
47914828
}
47924829

47934830
bool CornerStoneStruct::isAvailable()
@@ -4978,7 +5015,7 @@ void UpdateHellfireFlag(Item &item, const char *identifiedItemName)
49785015
// But vanilla hellfire items don't have CF_HELLFIRE set in Item::dwBuff
49795016
// This functions tries to set this flag for vanilla hellfire items based on the item name
49805017
// This ensures that Item::getName() returns the correct translated item name
4981-
if (item.dwBuff & CF_HELLFIRE)
5018+
if ((item.dwBuff & CF_HELLFIRE) != 0U)
49825019
return; // Item is already a hellfire item
49835020
if (item._iMagical != ITEM_QUALITY_MAGIC)
49845021
return; // Only magic item's name can differ between diablo and hellfire

Source/items.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ void InitItems();
500500
void CalcPlrItemVals(Player &player, bool Loadgfx);
501501
void CalcPlrInv(Player &player, bool Loadgfx);
502502
void InitializeItem(Item &item, _item_indexes itemData);
503-
void GenerateNewSeed(Item &h);
503+
void GenerateNewSeed(Item &item);
504504
int GetGoldCursor(int value);
505505

506506
/**

0 commit comments

Comments
 (0)