Browse Source

♻️Cleanup some min/max/clamp logic

Apply suggestions from code review

Co-authored-by: Gleb Mazovetskiy <glex.spb@gmail.com>
Co-authored-by: thebigMuh <81810641+thebigMuh@users.noreply.github.com>
pull/1670/head
Anders Jenbo 5 years ago
parent
commit
72ad648e9a
  1. 36
      Source/automap.cpp
  2. 192
      Source/items.cpp
  3. 6
      Source/qol.cpp

36
Source/automap.cpp

@ -5,6 +5,8 @@
*/
#include "automap.h"
#include <algorithm>
#include "control.h"
#include "inv.h"
#include "miniwin/miniwin.h"
@ -255,32 +257,14 @@ void SearchAutomapItem(const CelOutputBuffer &out)
y++;
}
int x1 = x - 8;
if (x1 < 0)
x1 = 0;
else if (x1 > MAXDUNX)
x1 = MAXDUNX;
int y1 = y - 8;
if (y1 < 0)
y1 = 0;
else if (y1 > MAXDUNY)
y1 = MAXDUNY;
int x2 = x + 8;
if (x2 < 0)
x2 = 0;
else if (x2 > MAXDUNX)
x2 = MAXDUNX;
int y2 = y + 8;
if (y2 < 0)
y2 = 0;
else if (y2 > MAXDUNY)
y2 = MAXDUNY;
for (int i = x1; i < x2; i++) {
for (int j = y1; j < y2; j++) {
const int startX = std::clamp(x - 8, 0, MAXDUNX);
const int startY = std::clamp(y - 8, 0, MAXDUNY);
const int endX = std::clamp(x + 8, 0, MAXDUNX);
const int endY = std::clamp(y + 8, 0, MAXDUNY);
for (int i = startX; i < endX; i++) {
for (int j = startY; j < endY; j++) {
if (dItem[i][j] != 0) {
int px = i - 2 * AutoMapXOfs - ViewX;
int py = j - 2 * AutoMapYOfs - ViewY;

192
Source/items.cpp

@ -668,12 +668,7 @@ void CalcPlrItemVals(int p, bool Loadgfx)
plr[p]._pIBonusDamMod = dmod;
plr[p]._pIGetHit = ghit;
if (lrad < 2) {
lrad = 2;
}
if (lrad > 15) {
lrad = 15;
}
lrad = std::clamp(lrad, 2, 15);
if (plr[p]._pLightRad != lrad && p == myplr) {
ChangeLightRadius(plr[p]._plid, lrad);
@ -681,25 +676,10 @@ void CalcPlrItemVals(int p, bool Loadgfx)
plr[p]._pLightRad = lrad;
}
plr[p]._pStrength = sadd + plr[p]._pBaseStr;
if (plr[p]._pStrength < 0) {
plr[p]._pStrength = 0;
}
plr[p]._pMagic = madd + plr[p]._pBaseMag;
if (plr[p]._pMagic < 0) {
plr[p]._pMagic = 0;
}
plr[p]._pDexterity = dadd + plr[p]._pBaseDex;
if (plr[p]._pDexterity < 0) {
plr[p]._pDexterity = 0;
}
plr[p]._pVitality = vadd + plr[p]._pBaseVit;
if (plr[p]._pVitality < 0) {
plr[p]._pVitality = 0;
}
plr[p]._pStrength = std::max(0, sadd + plr[p]._pBaseStr);
plr[p]._pMagic = std::max(0, madd + plr[p]._pBaseMag);
plr[p]._pDexterity = std::max(0, dadd + plr[p]._pBaseDex);
plr[p]._pVitality = std::max(0, vadd + plr[p]._pBaseVit);
if (plr[p]._pClass == HeroClass::Rogue) {
plr[p]._pDamageMod = plr[p]._pLevel * (plr[p]._pStrength + plr[p]._pDexterity) / 200;
@ -772,23 +752,9 @@ void CalcPlrItemVals(int p, bool Loadgfx)
lr = 0;
}
if (mr > MAXRESIST)
mr = MAXRESIST;
else if (mr < 0)
mr = 0;
plr[p]._pMagResist = mr;
if (fr > MAXRESIST)
fr = MAXRESIST;
else if (fr < 0)
fr = 0;
plr[p]._pFireResist = fr;
if (lr > MAXRESIST)
lr = MAXRESIST;
else if (lr < 0)
lr = 0;
plr[p]._pLghtResist = lr;
plr[p]._pMagResist = std::clamp(mr, 0, MAXRESIST);
plr[p]._pFireResist = std::clamp(fr, 0, MAXRESIST);
plr[p]._pLghtResist = std::clamp(lr, 0, MAXRESIST);
if (plr[p]._pClass == HeroClass::Warrior) {
vadd *= 2;
@ -810,19 +776,15 @@ void CalcPlrItemVals(int p, bool Loadgfx)
}
imana += (madd << 6);
plr[p]._pHitPoints = ihp + plr[p]._pHPBase;
plr[p]._pMaxHP = ihp + plr[p]._pMaxHPBase;
if (plr[p]._pHitPoints > plr[p]._pMaxHP)
plr[p]._pHitPoints = plr[p]._pMaxHP;
plr[p]._pHitPoints = std::min(ihp + plr[p]._pHPBase, plr[p]._pMaxHP);
if (p == myplr && (plr[p]._pHitPoints >> 6) <= 0) {
SetPlayerHitPoints(p, 0);
}
plr[p]._pMana = imana + plr[p]._pManaBase;
plr[p]._pMaxMana = imana + plr[p]._pMaxManaBase;
if (plr[p]._pMana > plr[p]._pMaxMana)
plr[p]._pMana = plr[p]._pMaxMana;
plr[p]._pMana = std::min(imana + plr[p]._pManaBase, plr[p]._pMaxMana);
plr[p]._pIFMinDam = fmin;
plr[p]._pIFMaxDam = fmax;
@ -1481,9 +1443,7 @@ void GetSuperItemLoc(int x, int y, int *xx, int *yy)
void CalcItemValue(int i)
{
int v;
v = items[i]._iVMult1 + items[i]._iVMult2;
int v = items[i]._iVMult1 + items[i]._iVMult2;
if (v > 0) {
v *= items[i]._ivalue;
}
@ -1491,10 +1451,7 @@ void CalcItemValue(int i)
v = items[i]._ivalue / v;
}
v = items[i]._iVAdd1 + items[i]._iVAdd2 + v;
if (v <= 0) {
v = 1;
}
items[i]._iIvalue = v;
items[i]._iIvalue = std::max(v, 1);
}
void GetBookSpell(int i, int lvl)
@ -1736,10 +1693,8 @@ void GetItemAttrs(int i, int idata, int lvl)
}
if (leveltype == DTYPE_HELL)
rndv += rndv / 8;
if (rndv > GOLD_MAX_LIMIT)
rndv = GOLD_MAX_LIMIT;
items[i]._ivalue = rndv;
items[i]._ivalue = std::min(rndv, GOLD_MAX_LIMIT);
SetPlrHandGoldCurs(&items[i]);
}
@ -1831,15 +1786,9 @@ void SaveItemPower(int i, item_effect_type power, int param1, int param2, int mi
items[i]._iPLMR += r;
break;
case IPL_ALLRES:
items[i]._iPLFR += r;
items[i]._iPLLR += r;
items[i]._iPLMR += r;
if (items[i]._iPLFR < 0)
items[i]._iPLFR = 0;
if (items[i]._iPLLR < 0)
items[i]._iPLLR = 0;
if (items[i]._iPLMR < 0)
items[i]._iPLMR = 0;
items[i]._iPLFR = std::max(items[i]._iPLFR + r, 0);
items[i]._iPLLR = std::max(items[i]._iPLLR + r, 0);
items[i]._iPLMR = std::max(items[i]._iPLMR + r, 0);
break;
case IPL_SPLLVLADD:
items[i]._iSplLvlAdd = r;
@ -1935,8 +1884,8 @@ void SaveItemPower(int i, item_effect_type power, int param1, int param2, int mi
[[fallthrough]];
case IPL_DUR_CURSE:
items[i]._iMaxDur -= r * items[i]._iMaxDur / 100;
if (items[i]._iMaxDur < 1)
items[i]._iMaxDur = 1;
items[i]._iMaxDur = std::max<uint8_t>(items[i]._iMaxDur, 1);
items[i]._iDurability = items[i]._iMaxDur;
break;
case IPL_INDESTRUCTIBLE:
@ -2092,8 +2041,8 @@ void SaveItemPower(int i, item_effect_type power, int param1, int param2, int mi
break;
case IPL_FIRERESCLVL:
items[i]._iPLFR = 30 - plr[myplr]._pLevel;
if (items[i]._iPLFR < 0)
items[i]._iPLFR = 0;
items[i]._iPLFR = std::max<int16_t>(items[i]._iPLFR, 0);
break;
case IPL_FIRERES_CURSE:
items[i]._iPLFR -= r;
@ -3154,8 +3103,6 @@ void CheckIdentify(int pnum, int cii)
static void RepairItem(ItemStruct *i, int lvl)
{
int rep, d;
if (i->_iDurability == i->_iMaxDur) {
return;
}
@ -3165,22 +3112,17 @@ static void RepairItem(ItemStruct *i, int lvl)
return;
}
rep = 0;
int rep = 0;
do {
rep += lvl + GenerateRnd(lvl);
d = i->_iMaxDur / (lvl + 9);
if (d < 1)
d = 1;
i->_iMaxDur = i->_iMaxDur - d;
i->_iMaxDur -= std::max(i->_iMaxDur / (lvl + 9), 1);
if (!i->_iMaxDur) {
i->_itype = ITYPE_NONE;
return;
}
} while (rep + i->_iDurability < i->_iMaxDur);
i->_iDurability += rep;
if (i->_iDurability > i->_iMaxDur)
i->_iDurability = i->_iMaxDur;
i->_iDurability = std::min<int>(i->_iDurability + rep, i->_iMaxDur);
}
void DoRepair(int pnum, int cii)
@ -3206,17 +3148,18 @@ void DoRepair(int pnum, int cii)
static void RechargeItem(ItemStruct *i, int r)
{
if (i->_iCharges != i->_iMaxCharges) {
do {
i->_iMaxCharges--;
if (i->_iMaxCharges == 0) {
return;
}
i->_iCharges += r;
} while (i->_iCharges < i->_iMaxCharges);
if (i->_iCharges > i->_iMaxCharges)
i->_iCharges = i->_iMaxCharges;
}
if (i->_iCharges == i->_iMaxCharges)
return;
do {
i->_iMaxCharges--;
if (i->_iMaxCharges == 0) {
return;
}
i->_iCharges += r;
} while (i->_iCharges < i->_iMaxCharges);
i->_iCharges = std::min(i->_iCharges, i->_iMaxCharges);
}
void DoRecharge(int pnum, int cii)
@ -3306,29 +3249,15 @@ static bool OilItem(ItemStruct *x, PlayerStruct *p)
break;
case IMISC_OILSKILL:
r = GenerateRnd(6) + 5;
if (x->_iMinStr > r) {
x->_iMinStr = x->_iMinStr - r;
} else {
x->_iMinStr = 0;
}
if (x->_iMinMag > r) {
x->_iMinMag = x->_iMinMag - r;
} else {
x->_iMinMag = 0;
}
if (x->_iMinDex > r) {
x->_iMinDex = x->_iMinDex - r;
} else {
x->_iMinDex = 0;
}
x->_iMinStr = std::max(0, x->_iMinStr - r);
x->_iMinMag = std::max(0, x->_iMinMag - r);
x->_iMinDex = std::max(0, x->_iMinDex - r);
break;
case IMISC_OILBSMTH:
if (x->_iMaxDur != 255) {
if (x->_iDurability < x->_iMaxDur) {
dur = (x->_iMaxDur + 4) / 5 + x->_iDurability;
if (dur > x->_iMaxDur) {
dur = x->_iMaxDur;
}
dur = std::min<int>(dur, x->_iMaxDur);
} else {
if (x->_iMaxDur >= 100) {
return true;
@ -4069,12 +3998,8 @@ void UseItem(int p, item_misc_id Mid, spell_id spl)
l *= 2;
if (plr[p]._pClass == HeroClass::Rogue || plr[p]._pClass == HeroClass::Monk || plr[p]._pClass == HeroClass::Bard)
l += l / 2;
plr[p]._pHitPoints += l;
if (plr[p]._pHitPoints > plr[p]._pMaxHP)
plr[p]._pHitPoints = plr[p]._pMaxHP;
plr[p]._pHPBase += l;
if (plr[p]._pHPBase > plr[p]._pMaxHPBase)
plr[p]._pHPBase = plr[p]._pMaxHPBase;
plr[p]._pHitPoints = std::min(l + plr[p]._pHitPoints, plr[p]._pMaxHP);
plr[p]._pHPBase = std::min(l + plr[p]._pHPBase, plr[p]._pMaxHPBase);
drawhpflag = true;
break;
case IMISC_FULLHEAL:
@ -4090,12 +4015,8 @@ void UseItem(int p, item_misc_id Mid, spell_id spl)
if (plr[p]._pClass == HeroClass::Rogue || plr[p]._pClass == HeroClass::Monk || plr[p]._pClass == HeroClass::Bard)
l += l / 2;
if ((plr[p]._pIFlags & ISPL_NOMANA) == 0) {
plr[p]._pMana += l;
if (plr[p]._pMana > plr[p]._pMaxMana)
plr[p]._pMana = plr[p]._pMaxMana;
plr[p]._pManaBase += l;
if (plr[p]._pManaBase > plr[p]._pMaxManaBase)
plr[p]._pManaBase = plr[p]._pMaxManaBase;
plr[p]._pMana = std::min(l + plr[p]._pMana, plr[p]._pMaxMana);
plr[p]._pManaBase = std::min(l + plr[p]._pManaBase, plr[p]._pMaxManaBase);
drawmanaflag = true;
}
break;
@ -4135,12 +4056,8 @@ void UseItem(int p, item_misc_id Mid, spell_id spl)
l *= 2;
if (plr[p]._pClass == HeroClass::Rogue)
l += l / 2;
plr[p]._pHitPoints += l;
if (plr[p]._pHitPoints > plr[p]._pMaxHP)
plr[p]._pHitPoints = plr[p]._pMaxHP;
plr[p]._pHPBase += l;
if (plr[p]._pHPBase > plr[p]._pMaxHPBase)
plr[p]._pHPBase = plr[p]._pMaxHPBase;
plr[p]._pHitPoints = std::min(plr[p]._pHitPoints + l, plr[p]._pMaxHP);
plr[p]._pHPBase = std::min(plr[p]._pHPBase + l, plr[p]._pMaxHPBase);
drawhpflag = true;
j = plr[p]._pMaxMana >> 8;
l = ((j / 2) + GenerateRnd(j)) << 6;
@ -4149,12 +4066,8 @@ void UseItem(int p, item_misc_id Mid, spell_id spl)
if (plr[p]._pClass == HeroClass::Rogue)
l += l / 2;
if ((plr[p]._pIFlags & ISPL_NOMANA) == 0) {
plr[p]._pMana += l;
if (plr[p]._pMana > plr[p]._pMaxMana)
plr[p]._pMana = plr[p]._pMaxMana;
plr[p]._pManaBase += l;
if (plr[p]._pManaBase > plr[p]._pMaxManaBase)
plr[p]._pManaBase = plr[p]._pMaxManaBase;
plr[p]._pMana = std::min(plr[p]._pMana + l, plr[p]._pMaxMana);
plr[p]._pManaBase = std::min(plr[p]._pManaBase + l, plr[p]._pMaxManaBase);
drawmanaflag = true;
}
break;
@ -4208,11 +4121,9 @@ void UseItem(int p, item_misc_id Mid, spell_id spl)
plr[p]._pSplLvl[spl]++;
if ((plr[p]._pIFlags & ISPL_NOMANA) == 0) {
plr[p]._pMana += spelldata[spl].sManaCost << 6;
if (plr[p]._pMana > plr[p]._pMaxMana)
plr[p]._pMana = plr[p]._pMaxMana;
plr[p]._pMana = std::min(plr[p]._pMana, plr[p]._pMaxMana);
plr[p]._pManaBase += spelldata[spl].sManaCost << 6;
if (plr[p]._pManaBase > plr[p]._pMaxManaBase)
plr[p]._pManaBase = plr[p]._pMaxManaBase;
plr[p]._pManaBase = std::min(plr[p]._pManaBase, plr[p]._pMaxManaBase);
}
if (p == myplr)
CalcPlrBookVals(p);
@ -4472,10 +4383,7 @@ static void SpawnOnePremium(int i, int plvl, int myplr)
dexterity *= 1.2;
magic *= 1.2;
if (plvl > 30)
plvl = 30;
if (plvl < 1)
plvl = 1;
plvl = std::clamp(plvl, 1, 30);
int count = 0;

6
Source/qol.cpp

@ -4,6 +4,8 @@
* Quality of life features
*/
#include <algorithm>
#include "DiabloUI/art_draw.h"
#include "control.h"
#include "cursor.h"
@ -97,9 +99,7 @@ void DrawMonsterHealthBar(const CelOutputBuffer &out)
Sint32 yPos = 18;
Sint32 border = 3;
Sint32 maxLife = mon->_mmaxhp;
if (mon->_mhitpoints > maxLife)
maxLife = mon->_mhitpoints;
const Sint32 maxLife = std::max(mon->_mmaxhp, mon->_mhitpoints);
DrawArt(out, xPos, yPos, &qolArt->healthBox);
DrawHalfTransparentRectTo(out, xPos + border, yPos + border, width - (border * 2), height - (border * 2));

Loading…
Cancel
Save