Browse Source

Reduce reliance on `bool done` for control flow (part 1 of x) (#4720)

pull/4725/head
Vladimir Olteanu 4 years ago committed by GitHub
parent
commit
4d6ff58c22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 99
      Source/inv.cpp
  2. 23
      Source/monster.cpp
  3. 87
      Source/objects.cpp
  4. 47
      Source/spells.cpp

99
Source/inv.cpp

@ -292,22 +292,18 @@ bool AutoEquip(int playerId, const Item &item, inv_body_loc bodyLocation, bool p
return true;
}
void CheckInvPaste(Player &player, Point cursorPosition)
int FindSlotUnderCursor(Point cursorPosition, Size itemSize)
{
int i = cursorPosition.x;
int j = cursorPosition.y;
Size itemSize = GetInventorySize(player.HoldItem);
if (!IsHardwareCursor()) {
// offset the cursor position to match the hot pixel we'd use for a hardware cursor
i += itemSize.width * INV_SLOT_HALF_SIZE_PX;
j += itemSize.height * INV_SLOT_HALF_SIZE_PX;
}
bool done = false;
int r = 0;
for (; r < NUM_XY_SLOTS && !done; r++) {
for (int r = 0; r < NUM_XY_SLOTS; r++) {
int xo = GetRightPanel().position.x;
int yo = GetRightPanel().position.y;
if (r >= SLOTXY_BELT_FIRST) {
@ -317,8 +313,7 @@ void CheckInvPaste(Player &player, Point cursorPosition)
if (i >= InvRect[r].x + xo && i <= InvRect[r].x + xo + InventorySlotSizeInPixels.width) {
if (j >= InvRect[r].y + yo - InventorySlotSizeInPixels.height - 1 && j < InvRect[r].y + yo) {
done = true;
r--;
return r;
}
}
if (r == SLOTXY_CHEST_LAST) {
@ -330,37 +325,38 @@ void CheckInvPaste(Player &player, Point cursorPosition)
if (r == SLOTXY_INV_LAST && itemSize.height % 2 == 0)
j += INV_SLOT_HALF_SIZE_PX;
}
if (!done)
return NUM_XY_SLOTS;
}
void CheckInvPaste(Player &player, Point cursorPosition)
{
Size itemSize = GetInventorySize(player.HoldItem);
int slot = FindSlotUnderCursor(cursorPosition, itemSize);
if (slot == NUM_XY_SLOTS)
return;
item_equip_type il = ILOC_UNEQUIPABLE;
if (r >= SLOTXY_HEAD_FIRST && r <= SLOTXY_HEAD_LAST)
if (slot >= SLOTXY_HEAD_FIRST && slot <= SLOTXY_HEAD_LAST)
il = ILOC_HELM;
if (r >= SLOTXY_RING_LEFT && r <= SLOTXY_RING_RIGHT)
if (slot >= SLOTXY_RING_LEFT && slot <= SLOTXY_RING_RIGHT)
il = ILOC_RING;
if (r == SLOTXY_AMULET)
if (slot == SLOTXY_AMULET)
il = ILOC_AMULET;
if (r >= SLOTXY_HAND_LEFT_FIRST && r <= SLOTXY_HAND_RIGHT_LAST)
if (slot >= SLOTXY_HAND_LEFT_FIRST && slot <= SLOTXY_HAND_RIGHT_LAST)
il = ILOC_ONEHAND;
if (r >= SLOTXY_CHEST_FIRST && r <= SLOTXY_CHEST_LAST)
if (slot >= SLOTXY_CHEST_FIRST && slot <= SLOTXY_CHEST_LAST)
il = ILOC_ARMOR;
if (r >= SLOTXY_BELT_FIRST && r <= SLOTXY_BELT_LAST)
if (slot >= SLOTXY_BELT_FIRST && slot <= SLOTXY_BELT_LAST)
il = ILOC_BELT;
done = player.GetItemLocation(player.HoldItem) == il;
if (il == ILOC_ONEHAND && player.GetItemLocation(player.HoldItem) == ILOC_TWOHAND) {
item_equip_type desiredIl = player.GetItemLocation(player.HoldItem);
if (il == ILOC_ONEHAND && desiredIl == ILOC_TWOHAND)
il = ILOC_TWOHAND;
done = true;
}
if (il == ILOC_BELT) {
done = CanBePlacedOnBelt(player.HoldItem);
}
int8_t it = 0;
if (il == ILOC_UNEQUIPABLE) {
done = true;
int ii = r - SLOTXY_INV_FIRST;
int ii = slot - SLOTXY_INV_FIRST;
if (player.HoldItem._itype == ItemType::Gold) {
if (player.InvGrid[ii] != 0) {
int8_t iv = player.InvGrid[ii];
@ -374,22 +370,20 @@ void CheckInvPaste(Player &player, Point cursorPosition)
}
} else {
int yy = std::max(INV_ROW_SLOT_SIZE * ((ii / INV_ROW_SLOT_SIZE) - ((itemSize.height - 1) / 2)), 0);
for (j = 0; j < itemSize.height && done; j++) {
for (int j = 0; j < itemSize.height; j++) {
if (yy >= NUM_INV_GRID_ELEM)
done = false;
return;
int xx = std::max((ii % INV_ROW_SLOT_SIZE) - ((itemSize.width - 1) / 2), 0);
for (i = 0; i < itemSize.width && done; i++) {
if (xx >= INV_ROW_SLOT_SIZE) {
done = false;
} else {
if (player.InvGrid[xx + yy] != 0) {
int8_t iv = abs(player.InvGrid[xx + yy]);
if (it != 0) {
if (it != iv)
done = false;
} else {
it = iv;
}
for (int i = 0; i < itemSize.width; i++) {
if (xx >= INV_ROW_SLOT_SIZE)
return;
if (player.InvGrid[xx + yy] != 0) {
int8_t iv = abs(player.InvGrid[xx + yy]);
if (it != 0) {
if (it != iv)
return;
} else {
it = iv;
}
}
xx++;
@ -397,18 +391,17 @@ void CheckInvPaste(Player &player, Point cursorPosition)
yy += INV_ROW_SLOT_SIZE;
}
}
}
if (!done)
} else if (il == ILOC_BELT) {
if (!CanBePlacedOnBelt(player.HoldItem))
return;
} else if (desiredIl != il) {
return;
}
if (IsNoneOf(il, ILOC_UNEQUIPABLE, ILOC_BELT) && !player.CanUseItem(player.HoldItem)) {
done = false;
player.Say(HeroSpeech::ICantUseThisYet);
}
if (!done)
return;
}
if (player._pmode > PM_WALK3 && IsNoneOf(il, ILOC_UNEQUIPABLE, ILOC_BELT))
return;
@ -421,12 +414,12 @@ void CheckInvPaste(Player &player, Point cursorPosition)
case ILOC_RING:
case ILOC_AMULET:
case ILOC_ARMOR: {
auto iLocToInvLoc = [&r](item_equip_type loc) {
auto iLocToInvLoc = [&slot](item_equip_type loc) {
switch (loc) {
case ILOC_HELM:
return INVLOC_HEAD;
case ILOC_RING:
return (r == SLOTXY_RING_LEFT ? INVLOC_RING_LEFT : INVLOC_RING_RIGHT);
return (slot == SLOTXY_RING_LEFT ? INVLOC_RING_LEFT : INVLOC_RING_RIGHT);
case ILOC_AMULET:
return INVLOC_AMULET;
case ILOC_ARMOR:
@ -444,8 +437,8 @@ void CheckInvPaste(Player &player, Point cursorPosition)
break;
}
case ILOC_ONEHAND: {
inv_body_loc selectedHand = r <= SLOTXY_HAND_LEFT_LAST ? INVLOC_HAND_LEFT : INVLOC_HAND_RIGHT;
inv_body_loc otherHand = r <= SLOTXY_HAND_LEFT_LAST ? INVLOC_HAND_RIGHT : INVLOC_HAND_LEFT;
inv_body_loc selectedHand = slot <= SLOTXY_HAND_LEFT_LAST ? INVLOC_HAND_LEFT : INVLOC_HAND_RIGHT;
inv_body_loc otherHand = slot <= SLOTXY_HAND_LEFT_LAST ? INVLOC_HAND_RIGHT : INVLOC_HAND_LEFT;
bool pasteIntoSelectedHand = (player.InvBody[otherHand].isEmpty() || player.InvBody[otherHand]._iClass != player.HoldItem._iClass)
|| (player._pClass == HeroClass::Bard && player.InvBody[otherHand]._iClass == ICLASS_WEAPON && player.HoldItem._iClass == ICLASS_WEAPON);
@ -496,7 +489,7 @@ void CheckInvPaste(Player &player, Point cursorPosition)
break;
case ILOC_UNEQUIPABLE:
if (player.HoldItem._itype == ItemType::Gold && it == 0) {
int ii = r - SLOTXY_INV_FIRST;
int ii = slot - SLOTXY_INV_FIRST;
if (player.InvGrid[ii] > 0) {
int invIndex = player.InvGrid[ii] - 1;
int gt = player.InvList[invIndex]._ivalue;
@ -540,7 +533,7 @@ void CheckInvPaste(Player &player, Point cursorPosition)
itemIndex = 0;
}
}
int ii = r - SLOTXY_INV_FIRST;
int ii = slot - SLOTXY_INV_FIRST;
// Calculate top-left position of item for InvGrid and then add item to InvGrid
@ -550,7 +543,7 @@ void CheckInvPaste(Player &player, Point cursorPosition)
}
break;
case ILOC_BELT: {
int ii = r - SLOTXY_BELT_FIRST;
int ii = slot - SLOTXY_BELT_FIRST;
if (player.SpdList[ii].isEmpty()) {
player.SpdList[ii] = player.HoldItem.pop();
} else {

23
Source/monster.cpp

@ -542,25 +542,26 @@ void PlaceUniqueMonsters()
for (int u = 0; UniqueMonstersData[u].mtype != -1; u++) {
if (UniqueMonstersData[u].mlevel != currlevel)
continue;
bool done = false;
int mt;
for (mt = 0; mt < LevelMonsterTypeCount; mt++) {
for (mt = 0; mt < LevelMonsterTypeCount && !done; mt++)
done = (LevelMonsterTypes[mt].mtype == UniqueMonstersData[u].mtype);
if (done)
break;
}
if (done)
continue;
if (u == UMT_GARBUD && Quests[Q_GARBUD]._qactive == QUEST_NOTAVAIL)
done = false;
continue;
if (u == UMT_ZHAR && Quests[Q_ZHAR]._qactive == QUEST_NOTAVAIL)
done = false;
continue;
if (u == UMT_SNOTSPIL && Quests[Q_LTBANNER]._qactive == QUEST_NOTAVAIL)
done = false;
continue;
if (u == UMT_LACHDAN && Quests[Q_VEIL]._qactive == QUEST_NOTAVAIL)
done = false;
continue;
if (u == UMT_WARLORD && Quests[Q_WARLORD]._qactive == QUEST_NOTAVAIL)
done = false;
if (done)
PlaceUniqueMonst(u, mt, 8);
continue;
PlaceUniqueMonst(u, mt, 8);
}
}

87
Source/objects.cpp

@ -892,35 +892,36 @@ void AddNakrulGate()
}
}
void AddStoryBooks()
bool CanPlaceStoryBook(Point position)
{
int cnt = 0;
int xp;
int yp;
bool done = false;
while (!done) {
done = true;
xp = GenerateRnd(80) + 16;
yp = GenerateRnd(80) + 16;
for (int yy = -2; yy <= 2; yy++) {
for (int xx = -3; xx <= 3; xx++) {
if (!RndLocOk(xx + xp, yy + yp))
done = false;
}
}
if (!done) {
cnt++;
if (cnt > 20000)
return;
for (int yy = -2; yy <= 2; yy++) {
for (int xx = -3; xx <= 3; xx++) {
Point tile = position + Displacement { xx, yy };
if (!RndLocOk(tile.x, tile.y))
return false;
}
}
AddObject(OBJ_STORYBOOK, { xp, yp });
AddObject(OBJ_STORYCANDLE, { xp - 2, yp + 1 });
AddObject(OBJ_STORYCANDLE, { xp - 2, yp });
AddObject(OBJ_STORYCANDLE, { xp - 1, yp - 1 });
AddObject(OBJ_STORYCANDLE, { xp + 1, yp - 1 });
AddObject(OBJ_STORYCANDLE, { xp + 2, yp });
AddObject(OBJ_STORYCANDLE, { xp + 2, yp + 1 });
return true;
}
void AddStoryBooks()
{
std::optional<Point> position;
for (int i = 0; i <= 20000 && !position; i++) {
position = { GenerateRnd(80) + 16, GenerateRnd(80) + 16 };
if (!CanPlaceStoryBook(*position))
position = {};
}
if (!position)
return;
AddObject(OBJ_STORYBOOK, *position);
AddObject(OBJ_STORYCANDLE, *position + Displacement { -2, 1 });
AddObject(OBJ_STORYCANDLE, *position + Displacement { -2, 0 });
AddObject(OBJ_STORYCANDLE, *position + Displacement { -1, -1 });
AddObject(OBJ_STORYCANDLE, *position + Displacement { 1, -1 });
AddObject(OBJ_STORYCANDLE, *position + Displacement { 2, 0 });
AddObject(OBJ_STORYCANDLE, *position + Displacement { 2, 1 });
}
void AddHookedBodies(int freq)
@ -3735,30 +3736,16 @@ void OperateArmorStand(int pnum, int i, bool sendmsg)
int FindValidShrine()
{
bool done = false;
int rv;
do {
rv = GenerateRnd(gbIsHellfire ? NumberOfShrineTypes : 26);
if (currlevel >= shrinemin[rv] && currlevel <= shrinemax[rv] && rv != ShrineThaumaturgic) {
done = true;
}
if (done) {
if (gbIsMultiplayer) {
if (shrineavail[rv] == ShrineTypeSingle) {
done = false;
continue;
}
}
if (!gbIsMultiplayer) {
if (shrineavail[rv] == ShrineTypeMulti) {
done = false;
continue;
}
}
done = true;
}
} while (!done);
return rv;
for (;;) {
int rv = GenerateRnd(gbIsHellfire ? NumberOfShrineTypes : 26);
if (currlevel < shrinemin[rv] || currlevel > shrinemax[rv] || rv == ShrineThaumaturgic)
continue;
if (gbIsMultiplayer && shrineavail[rv] == ShrineTypeSingle)
continue;
if (!gbIsMultiplayer && shrineavail[rv] == ShrineTypeMulti)
continue;
return rv;
}
}
void OperateGoatShrine(int pnum, int i, _sfx_id sType)

47
Source/spells.cpp

@ -67,42 +67,41 @@ void ClearReadiedSpell(Player &player)
void PlacePlayer(int pnum)
{
Player &player = Players[pnum];
Point newPosition = {};
if (player.plrlevel == currlevel) {
if (player.plrlevel != currlevel)
return;
Point newPosition = [&]() {
Point okPosition = {};
for (int i = 0; i < 8; i++) {
newPosition = player.position.tile + Displacement { plrxoff2[i], plryoff2[i] };
if (PosOkPlayer(player, newPosition)) {
break;
}
okPosition = player.position.tile + Displacement { plrxoff2[i], plryoff2[i] };
if (PosOkPlayer(player, okPosition))
return okPosition;
}
if (!PosOkPlayer(player, newPosition)) {
bool done = false;
for (int max = 1, min = -1; min > -50; max++, min--) {
for (int y = min; y <= max; y++) {
okPosition.y = player.position.tile.y + y;
int min = -1;
for (int max = 1; min > -50 && !done; max++, min--) {
for (int y = min; y <= max && !done; y++) {
newPosition.y = player.position.tile.y + y;
for (int x = min; x <= max; x++) {
okPosition.x = player.position.tile.x + x;
for (int x = min; x <= max && !done; x++) {
newPosition.x = player.position.tile.x + x;
if (PosOkPlayer(player, newPosition)) {
done = true;
}
}
if (PosOkPlayer(player, okPosition))
return okPosition;
}
}
}
player.position.tile = newPosition;
return okPosition;
}();
dPlayer[newPosition.x][newPosition.y] = pnum + 1;
player.position.tile = newPosition;
if (pnum == MyPlayerId) {
ViewPosition = newPosition;
}
dPlayer[newPosition.x][newPosition.y] = pnum + 1;
if (pnum == MyPlayerId) {
ViewPosition = newPosition;
}
}

Loading…
Cancel
Save