Browse Source

Use PickRandomlyAmong when choosing between two options

pull/4935/head
ephphatha 4 years ago committed by Anders Jenbo
parent
commit
7c54066f31
  1. 2
      Source/engine/random.hpp
  2. 10
      Source/items.cpp
  3. 6
      Source/levels/drlg_l1.cpp
  4. 36
      Source/levels/drlg_l3.cpp
  5. 8
      Source/missiles.cpp
  6. 10
      Source/monster.cpp
  7. 5
      Source/quests.cpp

2
Source/engine/random.hpp

@ -79,7 +79,7 @@ bool FlipCoin(unsigned frequency = 2);
template <typename T>
const T PickRandomlyAmong(const std::initializer_list<T> &values)
{
const auto index { std::max<int32_t>(GenerateRnd(values.size()), 0) };
const auto index { std::max<int32_t>(GenerateRnd(static_cast<int32_t>(values.size())), 0) };
return *(values.begin() + index);
}

10
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;

6
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;

36
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;

8
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)) {

10
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;

5
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));

Loading…
Cancel
Save