Browse Source

♻️ Unify most valuable player item logic on new method

pull/1551/head
Juliano Leal Goncalves 5 years ago committed by Anders Jenbo
parent
commit
f8871aab07
  1. 246
      Source/items.cpp
  2. 30
      Source/player.h

246
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++;

30
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 <typename TPredicate>
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

Loading…
Cancel
Save