diff --git a/Source/engine/random.hpp b/Source/engine/random.hpp index 95e683448..59ae02e98 100644 --- a/Source/engine/random.hpp +++ b/Source/engine/random.hpp @@ -7,7 +7,9 @@ */ #pragma once +#include #include +#include namespace devilution { @@ -57,4 +59,18 @@ int32_t AdvanceRndSeed(); */ int32_t GenerateRnd(int32_t v); +/** + * @brief Picks one of the elements in the list randomly. + * + * @param values The values to pick from + * @return A random value from the 'values' list. + */ +template +const T PickRandomlyAmong(const std::initializer_list &values) +{ + const auto index { std::max(GenerateRnd(values.size()), 0) }; + + return *(values.begin() + index); +} + } // namespace devilution diff --git a/Source/objects.cpp b/Source/objects.cpp index 73e462ca6..6a9eea0ae 100644 --- a/Source/objects.cpp +++ b/Source/objects.cpp @@ -3966,26 +3966,11 @@ bool OperateFountains(int pnum, int i) void OperateWeaponRack(int pnum, int i, bool sendmsg) { - ItemType weaponType { ItemType::ITYPE_NONE }; - if (Objects[i]._oSelFlag == 0) return; SetRndSeed(Objects[i]._oRndSeed); - switch (GenerateRnd(4) + ITYPE_SWORD) { - case ITYPE_SWORD: - weaponType = ITYPE_SWORD; - break; - case ITYPE_AXE: - weaponType = ITYPE_AXE; - break; - case ITYPE_BOW: - weaponType = ITYPE_BOW; - break; - case ITYPE_MACE: - weaponType = ITYPE_MACE; - break; - } + ItemType weaponType { PickRandomlyAmong({ ITYPE_SWORD, ITYPE_AXE, ITYPE_BOW, ITYPE_MACE }) }; Objects[i]._oSelFlag = 0; Objects[i]._oAnimFrame++;