From f8871aab07cd020bba7501e1192b9ec3f87274de Mon Sep 17 00:00:00 2001 From: Juliano Leal Goncalves Date: Thu, 15 Apr 2021 00:43:55 -0300 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Unify=20most=20valuable=20?= =?UTF-8?q?player=20item=20logic=20on=20new=20method?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/items.cpp | 246 ++++++----------------------------------------- Source/player.h | 30 ++++++ 2 files changed, 62 insertions(+), 214 deletions(-) diff --git a/Source/items.cpp b/Source/items.cpp index d25299d3e..dbdee2328 100644 --- a/Source/items.cpp +++ b/Source/items.cpp @@ -387,180 +387,6 @@ static bool IsSuffixValidForItemType(int i, int flgs) return (flgs & PLIType) != 0; } -int get_ring_max_value(int i) -{ - int j, res; - - res = 0; - for (j = 0; j < NUM_INVLOC; j++) { - if (plr[i].InvBody[j]._iClass != ICLASS_NONE && plr[i].InvBody[j]._itype == ITYPE_RING && res < plr[i].InvBody[j]._iIvalue) - res = plr[i].InvBody[j]._iIvalue; - } - for (j = 0; j < NUM_INV_GRID_ELEM; j++) { - if (plr[i].InvList[j]._iClass != ICLASS_NONE && plr[i].InvList[j]._itype == ITYPE_RING && res < plr[i].InvList[j]._iIvalue) - res = plr[i].InvList[j]._iIvalue; - } - - return res; -} - -int get_bow_max_value(int i) -{ - int j, res; - - res = 0; - for (j = 0; j < NUM_INVLOC; j++) { - if (plr[i].InvBody[j]._iClass != ICLASS_NONE && plr[i].InvBody[j]._itype == ITYPE_BOW && res < plr[i].InvBody[j]._iIvalue) - res = plr[i].InvBody[j]._iIvalue; - } - for (j = 0; j < NUM_INV_GRID_ELEM; j++) { - if (plr[i].InvList[j]._iClass != ICLASS_NONE && plr[i].InvList[j]._itype == ITYPE_BOW && res < plr[i].InvList[j]._iIvalue) - res = plr[i].InvList[j]._iIvalue; - } - - return res; -} - -int get_staff_max_value(int i) -{ - int j, res; - - res = 0; - for (j = 0; j < NUM_INVLOC; j++) { - if (plr[i].InvBody[j]._iClass != ICLASS_NONE && plr[i].InvBody[j]._itype == ITYPE_STAFF && res < plr[i].InvBody[j]._iIvalue) - res = plr[i].InvBody[j]._iIvalue; - } - for (j = 0; j < NUM_INV_GRID_ELEM; j++) { - if (plr[i].InvList[j]._iClass != ICLASS_NONE && plr[i].InvList[j]._itype == ITYPE_STAFF && res < plr[i].InvList[j]._iIvalue) - res = plr[i].InvList[j]._iIvalue; - } - - return res; -} - -int get_sword_max_value(int i) -{ - int j, res; - - res = 0; - for (j = 0; j < NUM_INVLOC; j++) { - if (plr[i].InvBody[j]._iClass != ICLASS_NONE && plr[i].InvBody[j]._itype == ITYPE_SWORD && res < plr[i].InvBody[j]._iIvalue) - res = plr[i].InvBody[j]._iIvalue; - } - for (j = 0; j < NUM_INV_GRID_ELEM; j++) { - if (plr[i].InvList[j]._iClass != ICLASS_NONE && plr[i].InvList[j]._itype == ITYPE_SWORD && res < plr[i].InvList[j]._iIvalue) - res = plr[i].InvList[j]._iIvalue; - } - - return res; -} - -int get_helm_max_value(int i) -{ - int j, res; - - res = 0; - for (j = 0; j < NUM_INVLOC; j++) { - if (plr[i].InvBody[j]._iClass != ICLASS_NONE && plr[i].InvBody[j]._itype == ITYPE_HELM && res < plr[i].InvBody[j]._iIvalue) - res = plr[i].InvBody[j]._iIvalue; - } - for (j = 0; j < NUM_INV_GRID_ELEM; j++) { - if (plr[i].InvList[j]._iClass != ICLASS_NONE && plr[i].InvList[j]._itype == ITYPE_HELM && res < plr[i].InvList[j]._iIvalue) - res = plr[i].InvList[j]._iIvalue; - } - - return res; -} - -int get_shield_max_value(int i) -{ - int j, res; - - res = 0; - for (j = 0; j < NUM_INVLOC; j++) { - if (plr[i].InvBody[j]._iClass != ICLASS_NONE && plr[i].InvBody[j]._itype == ITYPE_SHIELD && res < plr[i].InvBody[j]._iIvalue) - res = plr[i].InvBody[j]._iIvalue; - } - for (j = 0; j < NUM_INV_GRID_ELEM; j++) { - if (plr[i].InvList[j]._iClass != ICLASS_NONE && plr[i].InvList[j]._itype == ITYPE_SHIELD && res < plr[i].InvList[j]._iIvalue) - res = plr[i].InvList[j]._iIvalue; - } - - return res; -} - -int get_armor_max_value(int i) -{ - int j, res; - - res = 0; - for (j = 0; j < NUM_INVLOC; j++) { - if (plr[i].InvBody[j]._iClass != ICLASS_NONE - && (plr[i].InvBody[j]._itype == ITYPE_LARMOR || plr[i].InvBody[j]._itype == ITYPE_MARMOR || plr[i].InvBody[j]._itype == ITYPE_HARMOR) - && res < plr[i].InvBody[j]._iIvalue) - res = plr[i].InvBody[j]._iIvalue; - } - for (j = 0; j < NUM_INV_GRID_ELEM; j++) { - if (plr[i].InvList[j]._iClass != ICLASS_NONE - && (plr[i].InvList[j]._itype == ITYPE_LARMOR || plr[i].InvList[j]._itype == ITYPE_MARMOR || plr[i].InvList[j]._itype == ITYPE_HARMOR) - && res < plr[i].InvList[j]._iIvalue) - res = plr[i].InvList[j]._iIvalue; - } - - return res; -} - -int get_mace_max_value(int i) -{ - int j, res; - - res = 0; - for (j = 0; j < NUM_INVLOC; j++) { - if (plr[i].InvBody[j]._iClass != ICLASS_NONE && plr[i].InvBody[j]._itype == ITYPE_MACE && res < plr[i].InvBody[j]._iIvalue) - res = plr[i].InvBody[j]._iIvalue; - } - for (j = 0; j < NUM_INV_GRID_ELEM; j++) { - if (plr[i].InvList[j]._iClass != ICLASS_NONE && plr[i].InvList[j]._itype == ITYPE_MACE && res < plr[i].InvList[j]._iIvalue) - res = plr[i].InvList[j]._iIvalue; - } - - return res; -} - -int get_amulet_max_value(int i) -{ - int j, res; - - res = 0; - for (j = 0; j < NUM_INVLOC; j++) { - if (plr[i].InvBody[j]._iClass != ICLASS_NONE && plr[i].InvBody[j]._itype == ITYPE_AMULET && res < plr[i].InvBody[j]._iIvalue) - res = plr[i].InvBody[j]._iIvalue; - } - for (j = 0; j < NUM_INV_GRID_ELEM; j++) { - if (plr[i].InvList[j]._iClass != ICLASS_NONE && plr[i].InvList[j]._itype == ITYPE_AMULET && res < plr[i].InvList[j]._iIvalue) - res = plr[i].InvList[j]._iIvalue; - } - - return res; -} - -int get_axe_max_value(int i) -{ - int j, res; - - res = 0; - for (j = 0; j < NUM_INVLOC; j++) { - if (plr[i].InvBody[j]._iClass != ICLASS_NONE && plr[i].InvBody[j]._itype == ITYPE_AXE && res < plr[i].InvBody[j]._iIvalue) - res = plr[i].InvBody[j]._iIvalue; - } - for (j = 0; j < NUM_INV_GRID_ELEM; j++) { - if (plr[i].InvList[j]._iClass != ICLASS_NONE && plr[i].InvList[j]._itype == ITYPE_AXE && res < plr[i].InvList[j]._iIvalue) - res = plr[i].InvList[j]._iIvalue; - } - - return res; -} - int items_get_currlevel() { int lvl; @@ -4677,36 +4503,32 @@ static void SpawnOnePremium(int i, int plvl, int myplr) switch (items[0]._itype) { case ITYPE_LARMOR: case ITYPE_MARMOR: - case ITYPE_HARMOR: - ivalue = get_armor_max_value(myplr); + case ITYPE_HARMOR: { + const auto mostValuablePlayerArmor = plr[myplr].GetMostValuableItem( + [](const ItemStruct &item) { + return item._itype == ITYPE_LARMOR + || item._itype == ITYPE_MARMOR + || item._itype == ITYPE_HARMOR; + }); + + ivalue = mostValuablePlayerArmor == nullptr ? 0 : mostValuablePlayerArmor->_iIvalue; break; + } case ITYPE_SHIELD: - ivalue = get_shield_max_value(myplr); - break; case ITYPE_AXE: - ivalue = get_axe_max_value(myplr); - break; case ITYPE_BOW: - ivalue = get_bow_max_value(myplr); - break; case ITYPE_MACE: - ivalue = get_mace_max_value(myplr); - break; case ITYPE_SWORD: - ivalue = get_sword_max_value(myplr); - break; case ITYPE_HELM: - ivalue = get_helm_max_value(myplr); - break; case ITYPE_STAFF: - ivalue = get_staff_max_value(myplr); - break; case ITYPE_RING: - ivalue = get_ring_max_value(myplr); - break; - case ITYPE_AMULET: - ivalue = get_amulet_max_value(myplr); + case ITYPE_AMULET: { + const auto mostValuablePlayerItem = plr[myplr].GetMostValuableItem( + [](const ItemStruct &item) { return item._itype == items[0]._itype; }); + + ivalue = mostValuablePlayerItem == nullptr ? 0 : mostValuablePlayerItem->_iIvalue; break; + } default: ivalue = 0; break; @@ -5003,37 +4825,33 @@ void SpawnBoy(int lvl) switch (itemType) { case ITYPE_LARMOR: case ITYPE_MARMOR: - case ITYPE_HARMOR: - ivalue = get_armor_max_value(myplr); + case ITYPE_HARMOR: { + const auto mostValuablePlayerArmor = plr[myplr].GetMostValuableItem( + [](const ItemStruct &item) { + return item._itype == ITYPE_LARMOR + || item._itype == ITYPE_MARMOR + || item._itype == ITYPE_HARMOR; + }); + + ivalue = mostValuablePlayerArmor == nullptr ? 0 : mostValuablePlayerArmor->_iIvalue; break; + } case ITYPE_SHIELD: - ivalue = get_shield_max_value(myplr); - break; case ITYPE_AXE: - ivalue = get_axe_max_value(myplr); - break; case ITYPE_BOW: - ivalue = get_bow_max_value(myplr); - break; case ITYPE_MACE: - ivalue = get_mace_max_value(myplr); - break; case ITYPE_SWORD: - ivalue = get_sword_max_value(myplr); - break; case ITYPE_HELM: - ivalue = get_helm_max_value(myplr); - break; case ITYPE_STAFF: - ivalue = get_staff_max_value(myplr); - break; case ITYPE_RING: - ivalue = get_ring_max_value(myplr); - break; - case ITYPE_AMULET: - ivalue = get_amulet_max_value(myplr); + case ITYPE_AMULET: { + const auto mostValuablePlayerItem = plr[myplr].GetMostValuableItem( + [itemType](const ItemStruct &item) { return item._itype == itemType; }); + + ivalue = mostValuablePlayerItem == nullptr ? 0 : mostValuablePlayerItem->_iIvalue; break; } + } ivalue *= 0.8; count++; diff --git a/Source/player.h b/Source/player.h index 6985e037c..0095d1757 100644 --- a/Source/player.h +++ b/Source/player.h @@ -324,6 +324,36 @@ struct PlayerStruct { Uint8 *_pDData; Uint8 *_pBData; + /** + * @brief Gets the most valuable item out of all the player's items that match the given predicate. + * @param itemPredicate The predicate used to match the items. + * @return The most valuable item out of all the player's items that match the given predicate, or 'nullptr' in case no + * matching items were found. + */ + template + const ItemStruct *GetMostValuableItem(const TPredicate &itemPredicate) const + { + const auto getMostValuableItem = [&itemPredicate](const ItemStruct *begin, const ItemStruct *end, const ItemStruct *mostValuableItem = nullptr) { + for (const auto *item = begin; item < end; item++) { + if (item->isEmpty() || !itemPredicate(*item)) { + continue; + } + + if (mostValuableItem == nullptr || item->_iIvalue > mostValuableItem->_iIvalue) { + mostValuableItem = item; + } + } + + return mostValuableItem; + }; + + const ItemStruct *mostValuableItem = getMostValuableItem(SpdList, SpdList + MAXBELTITEMS); + mostValuableItem = getMostValuableItem(InvBody, InvBody + inv_body_loc::NUM_INVLOC, mostValuableItem); + mostValuableItem = getMostValuableItem(InvList, InvList + _pNumInv, mostValuableItem); + + return mostValuableItem; + } + /** * @brief Gets the base value of the player's specified attribute. * @param attribute The attribute to retrieve the base value for