diff --git a/Source/engine/random.hpp b/Source/engine/random.hpp index bee25c64d..5a629a527 100644 --- a/Source/engine/random.hpp +++ b/Source/engine/random.hpp @@ -79,7 +79,7 @@ bool FlipCoin(unsigned frequency = 2); template const T PickRandomlyAmong(const std::initializer_list &values) { - const auto index { std::max(GenerateRnd(values.size()), 0) }; + const auto index { std::max(GenerateRnd(static_cast(values.size())), 0) }; return *(values.begin() + index); } diff --git a/Source/items.cpp b/Source/items.cpp index d270ff8f5..af731b164 100644 --- a/Source/items.cpp +++ b/Source/items.cpp @@ -438,10 +438,7 @@ void AddInitItems() item._iSeed = AdvanceRndSeed(); SetRndSeed(item._iSeed); - if (FlipCoin()) - GetItemAttrs(item, IDI_MANA, curlv); - else - GetItemAttrs(item, IDI_HEAL, curlv); + GetItemAttrs(item, PickRandomlyAmong({ IDI_MANA, IDI_HEAL }), curlv); item._iCreateInfo = curlv | CF_PREGEN; SetupItem(item); @@ -1592,10 +1589,7 @@ void SetupAllUseful(Item &item, int iseed, int lvl) break; } } else { - if (FlipCoin()) - idx = IDI_MANA; - else - idx = IDI_HEAL; + idx = PickRandomlyAmong({ IDI_MANA, IDI_HEAL }); if (lvl > 1 && GenerateRnd(3) == 0) idx = IDI_PORTAL; diff --git a/Source/levels/drlg_l1.cpp b/Source/levels/drlg_l1.cpp index f21395192..c427ae8b1 100644 --- a/Source/levels/drlg_l1.cpp +++ b/Source/levels/drlg_l1.cpp @@ -1294,11 +1294,11 @@ Point SelectChamber() { int chamber; if (!HasChamber1) - chamber = FlipCoin() ? 2 : 3; + chamber = PickRandomlyAmong({ 2, 3 }); else if (!HasChamber2) - chamber = FlipCoin() ? 3 : 1; + chamber = PickRandomlyAmong({ 3, 1 }); else if (!HasChamber3) - chamber = FlipCoin() ? 2 : 1; + chamber = PickRandomlyAmong({ 2, 1 }); else chamber = GenerateRnd(3) + 1; diff --git a/Source/levels/drlg_l3.cpp b/Source/levels/drlg_l3.cpp index 386f2042b..7e00e64eb 100644 --- a/Source/levels/drlg_l3.cpp +++ b/Source/levels/drlg_l3.cpp @@ -1646,11 +1646,7 @@ void Fence() if (x - i > 0) { dungeon[i][j] = 127; for (int xx = i + 1; xx < x; xx++) { - if (FlipCoin()) { - dungeon[xx][j] = 129; - } else { - dungeon[xx][j] = 126; - } + dungeon[xx][j] = PickRandomlyAmong({ 129, 126 }); } dungeon[x][j] = 128; } @@ -1664,11 +1660,7 @@ void Fence() if (y - j > 0) { dungeon[i][j] = 123; for (int yy = j + 1; yy < y; yy++) { - if (FlipCoin()) { - dungeon[i][yy] = 124; - } else { - dungeon[i][yy] = 121; - } + dungeon[i][yy] = PickRandomlyAmong({ 124, 121 }); } dungeon[i][y] = 122; } @@ -1681,11 +1673,7 @@ void Fence() } x--; for (int xx = i + 1; xx < x; xx++) { - if (FlipCoin()) { - dungeon[xx][j] = 129; - } else { - dungeon[xx][j] = 126; - } + dungeon[xx][j] = PickRandomlyAmong({ 129, 126 }); } dungeon[x][j] = 128; int y = j + 1; @@ -1694,11 +1682,7 @@ void Fence() } y--; for (int yy = j + 1; yy < y; yy++) { - if (FlipCoin()) { - dungeon[i][yy] = 124; - } else { - dungeon[i][yy] = 121; - } + dungeon[i][yy] = PickRandomlyAmong({ 124, 121 }); } dungeon[i][y] = 122; } @@ -1736,11 +1720,7 @@ void Fence() continue; } if (dungeon[i][y] == 7) { - if (FlipCoin()) { - dungeon[i][y] = 137; - } else { - dungeon[i][y] = 135; - } + dungeon[i][y] = PickRandomlyAmong({ 137, 135 }); } if (dungeon[i][y] == 10) { dungeon[i][y] = 131; @@ -1790,11 +1770,7 @@ void Fence() continue; } if (dungeon[x][j] == 7) { - if (FlipCoin()) { - dungeon[x][j] = 136; - } else { - dungeon[x][j] = 134; - } + dungeon[x][j] = PickRandomlyAmong({ 136, 134 }); } if (dungeon[x][j] == 9) { dungeon[x][j] = 130; diff --git a/Source/missiles.cpp b/Source/missiles.cpp index 1f6b2e52f..b9f763963 100644 --- a/Source/missiles.cpp +++ b/Source/missiles.cpp @@ -361,7 +361,7 @@ bool Plr2PlrMHit(const Player &player, int p, int mindam, int maxdam, int dist, void RotateBlockedMissile(Missile &missile) { - int rotation = FlipCoin() ? -1 : 1; + int rotation = PickRandomlyAmong({ -1, 1 }); if (missile._miAnimType == MFILE_ARROWS) { int dir = missile._miAnimFrame + rotation; @@ -1245,11 +1245,7 @@ void AddStealPotions(Missile &missile, const AddMissileParameter & /*parameter*/ ii = ItemMiscIdIdx(IMISC_MANA); break; case IMISC_REJUV: - if (FlipCoin()) { - ii = ItemMiscIdIdx(IMISC_HEAL); - } else { - ii = ItemMiscIdIdx(IMISC_MANA); - } + ii = ItemMiscIdIdx(PickRandomlyAmong({ IMISC_HEAL, IMISC_MANA })); break; case IMISC_FULLREJUV: switch (GenerateRnd(3)) { diff --git a/Source/monster.cpp b/Source/monster.cpp index cd32d1cfd..70d813c75 100644 --- a/Source/monster.cpp +++ b/Source/monster.cpp @@ -2611,10 +2611,7 @@ void BatAi(int monsterId) RandomWalk(monsterId, Opposite(md)); monster.goalVar1++; } else { - if (FlipCoin()) - RandomWalk(monsterId, Right(md)); - else - RandomWalk(monsterId, Left(md)); + RandomWalk(monsterId, PickRandomlyAmong({ Right(md), Left(md) })); monster.goal = MGOAL_NORMAL; } return; @@ -2742,10 +2739,7 @@ void SneakAi(int monsterId) md = GetDirection(monster.position.tile, Players[monster.enemy].position.last); md = Opposite(md); if (monster.type().type == MT_UNSEEN) { - if (FlipCoin()) - md = Right(md); - else - md = Left(md); + md = PickRandomlyAmong({ Right(md), Left(md) }); } } monster.direction = md; diff --git a/Source/quests.cpp b/Source/quests.cpp index 0a97c4ff6..01adcecbb 100644 --- a/Source/quests.cpp +++ b/Source/quests.cpp @@ -282,10 +282,7 @@ void InitQuests() void InitialiseQuestPools(uint32_t seed, Quest quests[]) { SetRndSeed(seed); - if (FlipCoin()) - quests[Q_SKELKING]._qactive = QUEST_NOTAVAIL; - else - quests[Q_PWATER]._qactive = QUEST_NOTAVAIL; + quests[PickRandomlyAmong({ Q_SKELKING, Q_PWATER })]._qactive = QUEST_NOTAVAIL; // using int and not size_t here to detect negative values from GenerateRnd int randomIndex = GenerateRnd(sizeof(QuestGroup1) / sizeof(*QuestGroup1));