From 6ce8f1375184d60afcdf2ac47fdb02610d140509 Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Sun, 23 May 2021 05:36:48 +0200 Subject: [PATCH] Apply clang-tidy to more code and do some related cleanups --- Source/control.cpp | 8 +- Source/controls/modifier_hints.cpp | 2 +- Source/engine/render/text_render.cpp | 54 +- Source/gmenu.cpp | 120 ++--- Source/gmenu.h | 4 +- Source/help.cpp | 6 +- Source/inv.cpp | 704 ++++++++++++--------------- Source/inv.h | 10 +- Source/minitext.cpp | 8 +- Source/player.cpp | 6 +- Source/player.h | 2 +- Source/stores.cpp | 477 +++++++++--------- test/inv_test.cpp | 4 +- 13 files changed, 643 insertions(+), 762 deletions(-) diff --git a/Source/control.cpp b/Source/control.cpp index 31027f388..5708ff5b6 100644 --- a/Source/control.cpp +++ b/Source/control.cpp @@ -5,8 +5,8 @@ */ #include "control.h" -#include #include +#include #include @@ -320,7 +320,7 @@ static void DrawSpell(const CelOutputBuffer &out) DrawSpellCel(out, PANEL_X + 565, PANEL_Y + 119, *pSpellCels, 27); } -static void PrintSBookHotkey(CelOutputBuffer out, int x, int y, const std::string &text) +static void PrintSBookHotkey(const CelOutputBuffer& out, int x, int y, const std::string &text) { x -= GetLineWidth(text.c_str()) + 5; x += SPLICONLENGTH; @@ -550,7 +550,7 @@ void DrawPanelBox(const CelOutputBuffer &out, int x, int y, int w, int h, int sx * @param y0 Top of the flask cel section to draw. * @param y1 Bottom of the flask cel section to draw. */ -static void DrawFlaskTop(const CelOutputBuffer &out, int sx, int sy, CelOutputBuffer celBuf, int y0, int y1) +static void DrawFlaskTop(const CelOutputBuffer &out, int sx, int sy, const CelOutputBuffer& celBuf, int y0, int y1) { const BYTE *src = celBuf.at(0, y0); BYTE *dst = out.at(sx, sy); @@ -1757,7 +1757,7 @@ void control_remove_gold(int pnum, int goldIndex) plr[pnum].HoldItem._ivalue = dropGoldValue; plr[pnum].HoldItem._iStatFlag = true; control_set_gold_curs(pnum); - plr[pnum]._pGold = CalculateGold(pnum); + plr[pnum]._pGold = CalculateGold(plr[pnum]); dropGoldValue = 0; } diff --git a/Source/controls/modifier_hints.cpp b/Source/controls/modifier_hints.cpp index a8a11fbc5..585112d11 100644 --- a/Source/controls/modifier_hints.cpp +++ b/Source/controls/modifier_hints.cpp @@ -46,7 +46,7 @@ struct CircleMenuHint { { } - int Width() const + [[nodiscard]] int Width() const { return 2 * xMid; } diff --git a/Source/engine/render/text_render.cpp b/Source/engine/render/text_render.cpp index a8a34a58b..9af535490 100644 --- a/Source/engine/render/text_render.cpp +++ b/Source/engine/render/text_render.cpp @@ -4,11 +4,11 @@ * Text rendering. */ -#include "engine.h" #include "text_render.hpp" +#include "DiabloUI/ui_item.h" #include "cel_render.hpp" +#include "engine.h" #include "palette.h" -#include "DiabloUI/ui_item.h" namespace devilution { @@ -19,7 +19,7 @@ namespace { * small, medium and large sized fonts; which corresponds to smaltext.cel, * medtexts.cel and bigtgold.cel respectively. */ -const uint8_t gbFontTransTbl[256] = { +const uint8_t FontIndex[256] = { // clang-format off '\0', 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, @@ -41,7 +41,7 @@ const uint8_t gbFontTransTbl[256] = { }; /** Maps from font index to cel frame number. */ -const uint8_t fontframe[3][128] = { +const uint8_t FontFrame[3][128] = { { // clang-format off 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -84,7 +84,7 @@ const uint8_t fontframe[3][128] = { * Maps from cel frame number to character width. Note, the character width * may be distinct from the frame width, which is the same for every cel frame. */ -const uint8_t fontkern[3][68] = { +const uint8_t FontKern[3][68] = { { // clang-format off 8, 10, 7, 9, 8, 7, 6, 8, 8, 3, @@ -119,11 +119,11 @@ const uint8_t fontkern[3][68] = { }; enum text_color : uint8_t { - COL_WHITE, - COL_BLUE, - COL_RED, - COL_GOLD, - COL_BLACK, + ColorWhite, + ColorBlue, + ColorRed, + ColorGold, + ColorBlack, }; int LineHeights[3] = { 12, 38, 50 }; @@ -135,22 +135,22 @@ uint8_t fontColorTableGold[256]; uint8_t fontColorTableBlue[256]; uint8_t fontColorTableRed[256]; -static void DrawChar(const CelOutputBuffer &out, Point point, GameFontTables size, int nCel, text_color color) +void DrawChar(const CelOutputBuffer &out, Point point, GameFontTables size, int nCel, text_color color) { switch (color) { - case COL_WHITE: + case ColorWhite: CelDrawTo(out, point.x, point.y, *fonts[size], nCel); return; - case COL_BLUE: + case ColorBlue: CelDrawLightTo(out, point.x, point.y, *fonts[size], nCel, fontColorTableBlue); break; - case COL_RED: + case ColorRed: CelDrawLightTo(out, point.x, point.y, *fonts[size], nCel, fontColorTableRed); break; - case COL_GOLD: + case ColorGold: CelDrawLightTo(out, point.x, point.y, *fonts[size], nCel, fontColorTableGold); break; - case COL_BLACK: + case ColorBlack: light_table_index = 15; CelDrawLightTo(out, point.x, point.y, *fonts[size], nCel, nullptr); return; @@ -214,8 +214,8 @@ int GetLineWidth(const char *text, GameFontTables size, int spacing, int *charac if (text[i] == '\n') break; - uint8_t frame = fontframe[size][gbFontTransTbl[static_cast(text[i])]]; - lineWidth += fontkern[size][frame] + spacing; + uint8_t frame = FontFrame[size][FontIndex[static_cast(text[i])]]; + lineWidth += FontKern[size][frame] + spacing; } if (charactersInLine != nullptr) @@ -247,8 +247,8 @@ void WordWrapGameString(char *text, size_t width, GameFontTables size, int spaci continue; } - uint8_t frame = fontframe[size][gbFontTransTbl[static_cast(text[i])]]; - lineWidth += fontkern[size][frame] + spacing; + uint8_t frame = FontFrame[size][FontIndex[static_cast(text[i])]]; + lineWidth += FontKern[size][frame] + spacing; if (lineWidth - spacing <= width) { continue; // String is still within the limit, continue to the next line @@ -286,15 +286,15 @@ int DrawString(const CelOutputBuffer &out, const char *text, const SDL_Rect &rec else if ((flags & UIS_HUGE) != 0) size = GameFontBig; - text_color color = COL_GOLD; + text_color color = ColorGold; if ((flags & UIS_SILVER) != 0) - color = COL_WHITE; + color = ColorWhite; else if ((flags & UIS_BLUE) != 0) - color = COL_BLUE; + color = ColorBlue; else if ((flags & UIS_RED) != 0) - color = COL_RED; + color = ColorRed; else if ((flags & UIS_BLACK) != 0) - color = COL_BLACK; + color = ColorBlack; const int w = rect.w != 0 ? rect.w : out.w() - rect.x; const int h = rect.h != 0 ? rect.h : out.h() - rect.y; @@ -325,8 +325,8 @@ int DrawString(const CelOutputBuffer &out, const char *text, const SDL_Rect &rec unsigned i = 0; for (; i < textLength; i++) { - uint8_t frame = fontframe[size][gbFontTransTbl[static_cast(text[i])]]; - int symbolWidth = fontkern[size][frame]; + uint8_t frame = FontFrame[size][FontIndex[static_cast(text[i])]]; + int symbolWidth = FontKern[size][frame]; if (text[i] == '\n' || sx + symbolWidth > rightMargin) { if (sy + lineHeight >= bottomMargin) break; diff --git a/Source/gmenu.cpp b/Source/gmenu.cpp index e6306df49..f3bb4a971 100644 --- a/Source/gmenu.cpp +++ b/Source/gmenu.cpp @@ -73,21 +73,19 @@ bool gmenu_is_active() return sgpCurrentMenu != nullptr; } -static void gmenu_up_down(bool isDown) +static void GmenuUpDown(bool isDown) { - int i; - if (sgpCurrItem == nullptr) { return; } mouseNavigation = false; - i = sgCurrentMenuIdx; - if (sgCurrentMenuIdx) { - while (i) { + int i = sgCurrentMenuIdx; + if (sgCurrentMenuIdx != 0) { + while (i != 0) { i--; if (isDown) { sgpCurrItem++; - if (!sgpCurrItem->fnMenu) + if (sgpCurrItem->fnMenu == nullptr) sgpCurrItem = &sgpCurrentMenu[0]; } else { if (sgpCurrItem == sgpCurrentMenu) @@ -95,7 +93,7 @@ static void gmenu_up_down(bool isDown) sgpCurrItem--; } if ((sgpCurrItem->dwFlags & GMENU_ENABLED) != 0) { - if (i) + if (i != 0) PlaySFX(IS_TITLEMOV); return; } @@ -103,15 +101,13 @@ static void gmenu_up_down(bool isDown) } } -static void gmenu_left_right(bool isRight) +static void GmenuLeftRight(bool isRight) { - int step, steps; - if ((sgpCurrItem->dwFlags & GMENU_SLIDER) == 0) return; - step = sgpCurrItem->dwFlags & 0xFFF; - steps = (int)(sgpCurrItem->dwFlags & 0xFFF000) >> 12; + int step = sgpCurrItem->dwFlags & 0xFFF; + int steps = (int)(sgpCurrItem->dwFlags & 0xFFF000) >> 12; if (isRight) { if (step == steps) return; @@ -128,8 +124,6 @@ static void gmenu_left_right(bool isRight) void gmenu_set_items(TMenuItem *pItem, void (*gmFunc)()) { - int i; - PauseMode = 0; mouseNavigation = false; sgpCurrentMenu = pItem; @@ -140,25 +134,25 @@ void gmenu_set_items(TMenuItem *pItem, void (*gmFunc)()) } sgCurrentMenuIdx = 0; if (sgpCurrentMenu != nullptr) { - for (i = 0; sgpCurrentMenu[i].fnMenu != nullptr; i++) { + for (int i = 0; sgpCurrentMenu[i].fnMenu != nullptr; i++) { sgCurrentMenuIdx++; } } // BUGFIX: OOB access when sgCurrentMenuIdx is 0; should be set to NULL instead. (fixed) sgpCurrItem = sgCurrentMenuIdx > 0 ? &sgpCurrentMenu[sgCurrentMenuIdx - 1] : nullptr; - gmenu_up_down(true); + GmenuUpDown(true); } -static void gmenu_clear_buffer(const CelOutputBuffer &out, int x, int y, int width, int height) +static void GmenuClearBuffer(const CelOutputBuffer &out, int x, int y, int width, int height) { BYTE *i = out.at(x, y); - while (height--) { + while ((height--) != 0) { memset(i, 205, width); i -= out.pitch(); } } -static int gmenu_get_lfont(TMenuItem *pItem) +static int GmenuGetLfont(TMenuItem *pItem) { if ((pItem->dwFlags & GMENU_SLIDER) != 0) return 490; @@ -166,21 +160,21 @@ static int gmenu_get_lfont(TMenuItem *pItem) return GetLineWidth(_(pItem->pszStr), GameFontBig, 2); } -static void gmenu_draw_menu_item(const CelOutputBuffer &out, TMenuItem *pItem, int y) +static void GmenuDrawMenuItem(const CelOutputBuffer &out, TMenuItem *pItem, int y) { - int w = gmenu_get_lfont(pItem); + int w = GmenuGetLfont(pItem); if ((pItem->dwFlags & GMENU_SLIDER) != 0) { int x = 16 + w / 2; CelDrawTo(out, x + PANEL_LEFT, y - 10, *optbar_cel, 1); int step = pItem->dwFlags & 0xFFF; int nSteps = std::max((pItem->dwFlags & 0xFFF000) >> 12, 2); int pos = step * 256 / nSteps; - gmenu_clear_buffer(out, x + 2 + PANEL_LEFT, y - 12, pos + 13, 28); + GmenuClearBuffer(out, x + 2 + PANEL_LEFT, y - 12, pos + 13, 28); CelDrawTo(out, x + 2 + pos + PANEL_LEFT, y - 12, *option_cel, 1); } int x = (gnScreenWidth - w) / 2; - uint16_t style = (pItem->dwFlags & GMENU_ENABLED) ? UIS_SILVER : UIS_BLACK; + uint16_t style = (pItem->dwFlags & GMENU_ENABLED) != 0 ? UIS_SILVER : UIS_BLACK; DrawString(out, _(pItem->pszStr), { x, y, 0, 0 }, style | UIS_HUGE, 2); if (pItem == sgpCurrItem) { CelDrawTo(out, x - 54, y + 1, *PentSpin_cel, PentSpn2Spin()); @@ -191,18 +185,15 @@ static void gmenu_draw_menu_item(const CelOutputBuffer &out, TMenuItem *pItem, i static void GameMenuMove() { static AxisDirectionRepeater repeater; - const AxisDirection move_dir = repeater.Get(GetLeftStickOrDpadDirection()); - if (move_dir.x != AxisDirectionX_NONE) - gmenu_left_right(move_dir.x == AxisDirectionX_RIGHT); - if (move_dir.y != AxisDirectionY_NONE) - gmenu_up_down(move_dir.y == AxisDirectionY_DOWN); + const AxisDirection moveDir = repeater.Get(GetLeftStickOrDpadDirection()); + if (moveDir.x != AxisDirectionX_NONE) + GmenuLeftRight(moveDir.x == AxisDirectionX_RIGHT); + if (moveDir.y != AxisDirectionY_NONE) + GmenuUpDown(moveDir.y == AxisDirectionY_DOWN); } void gmenu_draw(const CelOutputBuffer &out) { - int y; - TMenuItem *i; - if (sgpCurrentMenu != nullptr) { GameMenuMove(); if (gmenu_current_option != nullptr) @@ -217,11 +208,11 @@ void gmenu_draw(const CelOutputBuffer &out) } } CelDrawTo(out, (gnScreenWidth - sgpLogo->Width()) / 2, 102 + UI_OFFSET_Y, *sgpLogo, LogoAnim_frame); - y = 160 + UI_OFFSET_Y; - i = sgpCurrentMenu; + int y = 160 + UI_OFFSET_Y; + TMenuItem *i = sgpCurrentMenu; if (sgpCurrentMenu->fnMenu != nullptr) { while (i->fnMenu != nullptr) { - gmenu_draw_menu_item(out, i, y); + GmenuDrawMenuItem(out, i, y); i++; y += 45; } @@ -247,58 +238,61 @@ bool gmenu_presskeys(int vkey) case DVL_VK_SPACE: return false; case DVL_VK_LEFT: - gmenu_left_right(false); + GmenuLeftRight(false); break; case DVL_VK_RIGHT: - gmenu_left_right(true); + GmenuLeftRight(true); break; case DVL_VK_UP: - gmenu_up_down(false); + GmenuUpDown(false); break; case DVL_VK_DOWN: - gmenu_up_down(true); + GmenuUpDown(true); break; } return true; } -static bool gmenu_get_mouse_slider(int *plOffset) +static bool GmenuMouseNavigation() { - *plOffset = 282; if (MouseX < 282 + PANEL_LEFT) { - *plOffset = 0; return false; } if (MouseX > 538 + PANEL_LEFT) { - *plOffset = 256; return false; } - *plOffset = MouseX - 282 - PANEL_LEFT; return true; } -bool gmenu_on_mouse_move() +static int GmenuGetMouseSlider() { - int step, nSteps; + if (MouseX < 282 + PANEL_LEFT) { + return 0; + } + if (MouseX > 538 + PANEL_LEFT) { + return 256; + } + return MouseX - 282 - PANEL_LEFT; +} +bool gmenu_on_mouse_move() +{ if (!mouseNavigation) return false; - gmenu_get_mouse_slider(&step); - nSteps = (int)(sgpCurrItem->dwFlags & 0xFFF000) >> 12; - step *= nSteps; + + int step = (int)(sgpCurrItem->dwFlags & 0xFFF000) >> 12; + step *= GmenuGetMouseSlider(); step /= 256; sgpCurrItem->dwFlags &= 0xFFFFF000; sgpCurrItem->dwFlags |= step; sgpCurrItem->fnMenu(false); + return true; } bool gmenu_left_mouse(bool isDown) { - TMenuItem *pItem; - int i, w, dummy; - if (!isDown) { if (mouseNavigation) { mouseNavigation = false; @@ -316,15 +310,15 @@ bool gmenu_left_mouse(bool isDown) if (MouseY - (117 + UI_OFFSET_Y) < 0) { return true; } - i = (MouseY - (117 + UI_OFFSET_Y)) / 45; + int i = (MouseY - (117 + UI_OFFSET_Y)) / 45; if (i >= sgCurrentMenuIdx) { return true; } - pItem = &sgpCurrentMenu[i]; + TMenuItem *pItem = &sgpCurrentMenu[i]; if ((sgpCurrentMenu[i].dwFlags & GMENU_ENABLED) == 0) { return true; } - w = gmenu_get_lfont(pItem); + int w = GmenuGetLfont(pItem); if (MouseX < gnScreenWidth / 2 - w / 2) { return true; } @@ -334,7 +328,7 @@ bool gmenu_left_mouse(bool isDown) sgpCurrItem = pItem; PlaySFX(IS_TITLEMOV); if ((pItem->dwFlags & GMENU_SLIDER) != 0) { - mouseNavigation = gmenu_get_mouse_slider(&dummy); + mouseNavigation = GmenuMouseNavigation(); gmenu_on_mouse_move(); } else { sgpCurrItem->fnMenu(true); @@ -355,12 +349,8 @@ void gmenu_enable(TMenuItem *pMenuItem, bool enable) */ void gmenu_slider_set(TMenuItem *pItem, int min, int max, int value) { - int nSteps; - assert(pItem); - nSteps = (int)(pItem->dwFlags & 0xFFF000) >> 12; - if (nSteps < 2) - nSteps = 2; + int nSteps = std::max((int)(pItem->dwFlags & 0xFFF000) >> 12, 2); pItem->dwFlags &= 0xFFFFF000; pItem->dwFlags |= ((max - min - 1) / 2 + (value - min) * nSteps) / (max - min); } @@ -370,12 +360,8 @@ void gmenu_slider_set(TMenuItem *pItem, int min, int max, int value) */ int gmenu_slider_get(TMenuItem *pItem, int min, int max) { - int nSteps, step; - - step = pItem->dwFlags & 0xFFF; - nSteps = (int)(pItem->dwFlags & 0xFFF000) >> 12; - if (nSteps < 2) - nSteps = 2; + int step = pItem->dwFlags & 0xFFF; + int nSteps = std::max((int)(pItem->dwFlags & 0xFFF000) >> 12, 2); return min + (step * (max - min) + (nSteps - 1) / 2) / nSteps; } diff --git a/Source/gmenu.h b/Source/gmenu.h index 10daf2684..3c0cd452c 100644 --- a/Source/gmenu.h +++ b/Source/gmenu.h @@ -32,8 +32,8 @@ bool gmenu_presskeys(int vkey); bool gmenu_on_mouse_move(); bool gmenu_left_mouse(bool isDown); void gmenu_enable(TMenuItem *pMenuItem, bool enable); -void gmenu_slider_set(TMenuItem *pItem, int min, int max, int gamma); +void gmenu_slider_set(TMenuItem *pItem, int min, int max, int value); int gmenu_slider_get(TMenuItem *pItem, int min, int max); -void gmenu_slider_steps(TMenuItem *pItem, int dwTicks); +void gmenu_slider_steps(TMenuItem *pItem, int steps); } // namespace devilution diff --git a/Source/help.cpp b/Source/help.cpp index 9f1028cff..c1632bea6 100644 --- a/Source/help.cpp +++ b/Source/help.cpp @@ -19,7 +19,7 @@ namespace devilution { unsigned int SkipLines; bool helpflag; -const char *const gszHelpText[] = { +const char *const HelpText[] = { N_("$Keyboard Shortcuts:"), N_("F1: Open Help Screen"), N_("Esc: Display Main Menu"), @@ -98,7 +98,7 @@ void InitHelp() helpflag = false; char tempstr[512]; - for (auto text : gszHelpText) { + for (const auto *text : HelpText) { strcpy(tempstr, _(text)); WordWrapGameString(tempstr, 577); @@ -106,7 +106,7 @@ void InitHelp() size_t previous = 0; while (true) { - size_t next = paragraph.find("\n", previous); + size_t next = paragraph.find('\n', previous); HelpTextLines.emplace_back(paragraph.substr(previous, next)); if (next == std::string::npos) break; diff --git a/Source/inv.cpp b/Source/inv.cpp index aba328d9b..0429417d0 100644 --- a/Source/inv.cpp +++ b/Source/inv.cpp @@ -5,6 +5,7 @@ */ #include +#include #include #include "cursor.h" @@ -156,18 +157,13 @@ void InitInv() drawsbarflag = false; } -static void InvDrawSlotBack(const CelOutputBuffer &out, int X, int Y, int W, int H) +static void InvDrawSlotBack(const CelOutputBuffer &out, int x, int y, int w, int h) { - BYTE *dst; + BYTE *dst = out.at(x, y); - dst = out.at(X, Y); - - int wdt, hgt; - BYTE pix; - - for (hgt = H; hgt; hgt--, dst -= out.pitch() + W) { - for (wdt = W; wdt; wdt--) { - pix = *dst; + for (int hgt = h; hgt != 0; hgt--, dst -= out.pitch() + w) { + for (int wdt = w; wdt != 0; wdt--) { + BYTE pix = *dst; if (pix >= PAL16_BLUE) { if (pix <= PAL16_BLUE + 15) pix -= PAL16_BLUE - PAL16_BEIGE; @@ -181,7 +177,6 @@ static void InvDrawSlotBack(const CelOutputBuffer &out, int X, int Y, int W, int void DrawInv(const CelOutputBuffer &out) { - int frame, i, j, ii; CelDrawTo(out, RIGHT_PANEL_X, 351, *pInvCels, 1); InvXY slotSize[] = { @@ -206,11 +201,11 @@ void DrawInv(const CelOutputBuffer &out) for (int slot = INVLOC_HEAD; slot < NUM_INVLOC; slot++) { if (!plr[myplr].InvBody[slot].isEmpty()) { - int screen_x = slotPos[slot].X; - int screen_y = slotPos[slot].Y; - InvDrawSlotBack(out, RIGHT_PANEL_X + screen_x, screen_y, slotSize[slot].X * INV_SLOT_SIZE_PX, slotSize[slot].Y * INV_SLOT_SIZE_PX); + int screenX = slotPos[slot].X; + int screenY = slotPos[slot].Y; + InvDrawSlotBack(out, RIGHT_PANEL_X + screenX, screenY, slotSize[slot].X * INV_SLOT_SIZE_PX, slotSize[slot].Y * INV_SLOT_SIZE_PX); - frame = plr[myplr].InvBody[slot]._iCurs + CURSOR_FIRSTITEM; + int frame = plr[myplr].InvBody[slot]._iCurs + CURSOR_FIRSTITEM; int frameW; int frameH; @@ -218,21 +213,21 @@ void DrawInv(const CelOutputBuffer &out) // calc item offsets for weapons smaller than 2x3 slots if (slot == INVLOC_HAND_LEFT) { - screen_x += frameW == INV_SLOT_SIZE_PX ? 14 : 0; - screen_y += frameH == (3 * INV_SLOT_SIZE_PX) ? 0 : -14; + screenX += frameW == INV_SLOT_SIZE_PX ? 14 : 0; + screenY += frameH == (3 * INV_SLOT_SIZE_PX) ? 0 : -14; } else if (slot == INVLOC_HAND_RIGHT) { - screen_x += frameW == INV_SLOT_SIZE_PX ? 13 : 1; - screen_y += frameH == 3 * INV_SLOT_SIZE_PX ? 0 : -14; + screenX += frameW == INV_SLOT_SIZE_PX ? 13 : 1; + screenY += frameH == 3 * INV_SLOT_SIZE_PX ? 0 : -14; } const auto &cel = GetInvItemSprite(frame); const int celFrame = GetInvItemFrame(frame); if (pcursinvitem == slot) { - CelBlitOutlineTo(out, GetOutlineColor(plr[myplr].InvBody[slot], true), RIGHT_PANEL_X + screen_x, screen_y, cel, celFrame, false); + CelBlitOutlineTo(out, GetOutlineColor(plr[myplr].InvBody[slot], true), RIGHT_PANEL_X + screenX, screenY, cel, celFrame, false); } - CelDrawItem(plr[myplr].InvBody[slot]._iStatFlag, out, RIGHT_PANEL_X + screen_x, screen_y, cel, celFrame); + CelDrawItem(plr[myplr].InvBody[slot]._iStatFlag, out, RIGHT_PANEL_X + screenX, screenY, cel, celFrame); if (slot == INVLOC_HAND_LEFT) { if (plr[myplr].InvBody[slot]._iLoc == ILOC_TWOHAND) { @@ -243,9 +238,9 @@ void DrawInv(const CelOutputBuffer &out) light_table_index = 0; cel_transparency_active = true; - const int dst_x = RIGHT_PANEL_X + slotPos[INVLOC_HAND_RIGHT].X + (frameW == INV_SLOT_SIZE_PX ? 13 : -1); - const int dst_y = slotPos[INVLOC_HAND_RIGHT].Y; - CelClippedBlitLightTransTo(out, dst_x, dst_y, cel, celFrame); + const int dstX = RIGHT_PANEL_X + slotPos[INVLOC_HAND_RIGHT].X + (frameW == INV_SLOT_SIZE_PX ? 13 : -1); + const int dstY = slotPos[INVLOC_HAND_RIGHT].Y; + CelClippedBlitLightTransTo(out, dstX, dstY, cel, celFrame); cel_transparency_active = false; } @@ -254,7 +249,7 @@ void DrawInv(const CelOutputBuffer &out) } } - for (i = 0; i < NUM_INV_GRID_ELEM; i++) { + for (int i = 0; i < NUM_INV_GRID_ELEM; i++) { if (plr[myplr].InvGrid[i] != 0) { InvDrawSlotBack( out, @@ -265,10 +260,10 @@ void DrawInv(const CelOutputBuffer &out) } } - for (j = 0; j < NUM_INV_GRID_ELEM; j++) { + for (int j = 0; j < NUM_INV_GRID_ELEM; j++) { if (plr[myplr].InvGrid[j] > 0) { // first slot of an item - ii = plr[myplr].InvGrid[j] - 1; - frame = plr[myplr].InvList[ii]._iCurs + CURSOR_FIRSTITEM; + int ii = plr[myplr].InvGrid[j] - 1; + int frame = plr[myplr].InvList[ii]._iCurs + CURSOR_FIRSTITEM; const auto &cel = GetInvItemSprite(frame); const int celFrame = GetInvItemFrame(frame); @@ -625,55 +620,61 @@ bool AutoEquipEnabled(const PlayerStruct &player, const ItemStruct &item) bool AutoPlaceItemInInventory(PlayerStruct &player, const ItemStruct &item, bool persistItem) { InvXY itemSize = GetInventorySize(item); - bool done = false; if (itemSize.Y == 1) { - for (int i = 30; i <= 39 && !done; i++) { - done = AutoPlaceItemInInventorySlot(player, i, item, persistItem); + for (int i = 30; i <= 39; i++) { + if (AutoPlaceItemInInventorySlot(player, i, item, persistItem)) + return true; } - for (int x = 9; x >= 0 && !done; x--) { - for (int y = 2; y >= 0 && !done; y--) { - done = AutoPlaceItemInInventorySlot(player, 10 * y + x, item, persistItem); + for (int x = 9; x >= 0; x--) { + for (int y = 2; y >= 0; y--) { + if (AutoPlaceItemInInventorySlot(player, 10 * y + x, item, persistItem)) + return true; } } - return done; + return false; } if (itemSize.Y == 2) { - for (int x = 10 - itemSize.X; x >= 0 && !done; x -= itemSize.X) { - for (int y = 0; y < 3 && !done; y++) { - done = AutoPlaceItemInInventorySlot(player, 10 * y + x, item, persistItem); + for (int x = 10 - itemSize.X; x >= 0; x -= itemSize.X) { + for (int y = 0; y < 3; y++) { + if (AutoPlaceItemInInventorySlot(player, 10 * y + x, item, persistItem)) + return true; } } if (itemSize.X == 2) { - for (int x = 7; x >= 0 && !done; x -= 2) { - for (int y = 0; y < 3 && !done; y++) { - done = AutoPlaceItemInInventorySlot(player, 10 * y + x, item, persistItem); + for (int x = 7; x >= 0; x -= 2) { + for (int y = 0; y < 3; y++) { + if (AutoPlaceItemInInventorySlot(player, 10 * y + x, item, persistItem)) + return true; } } } - return done; + return false; } if (itemSize.X == 1 && itemSize.Y == 3) { - for (int i = 0; i < 20 && !done; i++) { - done = AutoPlaceItemInInventorySlot(player, i, item, persistItem); + for (int i = 0; i < 20; i++) { + if (AutoPlaceItemInInventorySlot(player, i, item, persistItem)) + return true; } - return done; + return false; } if (itemSize.X == 2 && itemSize.Y == 3) { - for (int i = 0; i < 9 && !done; i++) { - done = AutoPlaceItemInInventorySlot(player, i, item, persistItem); + for (int i = 0; i < 9; i++) { + if (AutoPlaceItemInInventorySlot(player, i, item, persistItem)) + return true; } - for (int i = 10; i < 19 && !done; i++) { - done = AutoPlaceItemInInventorySlot(player, i, item, persistItem); + for (int i = 10; i < 19; i++) { + if (AutoPlaceItemInInventorySlot(player, i, item, persistItem)) + return true; } - return done; + return false; } - return false; + app_fatal("Unknown item size: %ix%i", itemSize.X, itemSize.Y); } /** @@ -686,169 +687,120 @@ bool AutoPlaceItemInInventory(PlayerStruct &player, const ItemStruct &item, bool */ bool AutoPlaceItemInInventorySlot(PlayerStruct &player, int slotIndex, const ItemStruct &item, bool persistItem) { - int i, j, xx, yy; - bool done; - - done = true; - yy = 10 * (slotIndex / 10); - if (yy < 0) { - yy = 0; - } + int yy = (slotIndex > 0) ? (10 * (slotIndex / 10)) : 0; InvXY itemSize = GetInventorySize(item); - for (j = 0; j < itemSize.Y && done; j++) { + for (int j = 0; j < itemSize.Y; j++) { if (yy >= NUM_INV_GRID_ELEM) { - done = false; - } - xx = slotIndex % 10; - if (xx < 0) { - xx = 0; + return false; } - for (i = 0; i < itemSize.X && done; i++) { - if (xx >= 10) { - done = false; - } else { - done = player.InvGrid[xx + yy] == 0; + int xx = (slotIndex > 0) ? (slotIndex % 10) : 0; + for (int i = 0; i < itemSize.X; i++) { + if (xx >= 10 || player.InvGrid[xx + yy] != 0) { + return false; } xx++; } yy += 10; } - if (done && persistItem) { + + if (persistItem) { player.InvList[player._pNumInv] = player.HoldItem; player._pNumInv++; AddItemToInvGrid(player, slotIndex, player._pNumInv, itemSize.X, itemSize.Y); player.CalcScrolls(); } - return done; + + return true; } -bool GoldAutoPlace(int pnum) +bool GoldAutoPlace(PlayerStruct &player) { bool done = false; - for (int i = 0; i < plr[pnum]._pNumInv && !done; i++) { - if (plr[pnum].InvList[i]._itype != ITYPE_GOLD) + for (int i = 0; i < player._pNumInv && !done; i++) { + if (player.InvList[i]._itype != ITYPE_GOLD) continue; - if (plr[pnum].InvList[i]._ivalue >= MaxGold) + if (player.InvList[i]._ivalue >= MaxGold) continue; - plr[pnum].InvList[i]._ivalue += plr[pnum].HoldItem._ivalue; - if (plr[pnum].InvList[i]._ivalue > MaxGold) { - plr[pnum].HoldItem._ivalue = plr[pnum].InvList[i]._ivalue - MaxGold; - SetPlrHandGoldCurs(&plr[pnum].HoldItem); - plr[pnum].InvList[i]._ivalue = MaxGold; + player.InvList[i]._ivalue += player.HoldItem._ivalue; + if (player.InvList[i]._ivalue > MaxGold) { + player.HoldItem._ivalue = player.InvList[i]._ivalue - MaxGold; + SetPlrHandGoldCurs(&player.HoldItem); + player.InvList[i]._ivalue = MaxGold; if (gbIsHellfire) - GetPlrHandSeed(&plr[pnum].HoldItem); + GetPlrHandSeed(&player.HoldItem); } else { - plr[pnum].HoldItem._ivalue = 0; + player.HoldItem._ivalue = 0; done = true; } - SetPlrHandGoldCurs(&plr[pnum].InvList[i]); - plr[pnum]._pGold = CalculateGold(pnum); + SetPlrHandGoldCurs(&player.InvList[i]); + player._pGold = CalculateGold(player); } for (int i = 39; i >= 30 && !done; i--) { - done = GoldAutoPlaceInInventorySlot(pnum, i); + done = GoldAutoPlaceInInventorySlot(player, i); } for (int x = 9; x >= 0 && !done; x--) { for (int y = 2; y >= 0 && !done; y--) { - done = GoldAutoPlaceInInventorySlot(pnum, 10 * y + x); + done = GoldAutoPlaceInInventorySlot(player, 10 * y + x); } } return done; } -bool GoldAutoPlaceInInventorySlot(int pnum, int slotIndex) +bool GoldAutoPlaceInInventorySlot(PlayerStruct &player, int slotIndex) { - bool done = false; - int yy = 10 * (slotIndex / 10); int xx = slotIndex % 10; - if (plr[pnum].InvGrid[xx + yy] == 0) { - int ii = plr[pnum]._pNumInv; - plr[pnum].InvList[ii] = plr[pnum].HoldItem; - plr[pnum]._pNumInv = plr[pnum]._pNumInv + 1; - plr[pnum].InvGrid[xx + yy] = plr[pnum]._pNumInv; - GetPlrHandSeed(&plr[pnum].InvList[ii]); - int gold = plr[pnum].HoldItem._ivalue; - if (gold > MaxGold) { - gold -= MaxGold; - plr[pnum].HoldItem._ivalue = gold; - GetPlrHandSeed(&plr[pnum].HoldItem); - plr[pnum].InvList[ii]._ivalue = MaxGold; - } else { - plr[pnum].HoldItem._ivalue = 0; - done = true; - plr[pnum]._pGold = CalculateGold(pnum); - NewCursor(CURSOR_HAND); - } + + if (player.InvGrid[xx + yy] != 0) { + return false; } - return done; -} + int ii = player._pNumInv; + player.InvList[ii] = player.HoldItem; + player._pNumInv = player._pNumInv + 1; + player.InvGrid[xx + yy] = player._pNumInv; + GetPlrHandSeed(&player.InvList[ii]); -bool WeaponAutoPlace(int pnum) -{ - if (plr[pnum]._pClass == HeroClass::Monk) + int gold = player.HoldItem._ivalue; + if (gold > MaxGold) { + gold -= MaxGold; + player.HoldItem._ivalue = gold; + GetPlrHandSeed(&player.HoldItem); + player.InvList[ii]._ivalue = MaxGold; return false; - if (plr[pnum].HoldItem._iLoc != ILOC_TWOHAND - || (plr[pnum]._pClass == HeroClass::Barbarian && (plr[pnum].HoldItem._itype == ITYPE_SWORD || plr[pnum].HoldItem._itype == ITYPE_MACE))) { - if (plr[pnum]._pClass != HeroClass::Bard) { - if (!plr[pnum].InvBody[INVLOC_HAND_LEFT].isEmpty() && plr[pnum].InvBody[INVLOC_HAND_LEFT]._iClass == ICLASS_WEAPON) - return false; - if (!plr[pnum].InvBody[INVLOC_HAND_RIGHT].isEmpty() && plr[pnum].InvBody[INVLOC_HAND_RIGHT]._iClass == ICLASS_WEAPON) - return false; - } - - if (plr[pnum].InvBody[INVLOC_HAND_LEFT].isEmpty()) { - NetSendCmdChItem(true, INVLOC_HAND_LEFT); - plr[pnum].InvBody[INVLOC_HAND_LEFT] = plr[pnum].HoldItem; - return true; - } - if (plr[pnum].InvBody[INVLOC_HAND_RIGHT].isEmpty() && plr[pnum].InvBody[INVLOC_HAND_LEFT]._iLoc != ILOC_TWOHAND) { - NetSendCmdChItem(true, INVLOC_HAND_RIGHT); - plr[pnum].InvBody[INVLOC_HAND_RIGHT] = plr[pnum].HoldItem; - return true; - } - } else if (plr[pnum].InvBody[INVLOC_HAND_LEFT].isEmpty() && plr[pnum].InvBody[INVLOC_HAND_RIGHT].isEmpty()) { - NetSendCmdChItem(true, INVLOC_HAND_LEFT); - plr[pnum].InvBody[INVLOC_HAND_LEFT] = plr[pnum].HoldItem; - return true; } - return false; + player.HoldItem._ivalue = 0; + player._pGold = CalculateGold(player); + NewCursor(CURSOR_HAND); + + return true; } int SwapItem(ItemStruct *a, ItemStruct *b) { - ItemStruct h; - - h = *a; - *a = *b; - *b = h; + std::swap(*a, *b); - return h._iCurs + CURSOR_FIRSTITEM; + return b->_iCurs + CURSOR_FIRSTITEM; } void CheckInvPaste(int pnum, int mx, int my) { - int r, sx, sy; - int i, j, xx, yy, ii; - bool done, done2h; - int il, cn, it, iv, ig, gt; - ItemStruct tempitem; - SetICursor(plr[pnum].HoldItem._iCurs + CURSOR_FIRSTITEM); - i = mx + (icursW / 2); - j = my + (icursH / 2); - sx = icursW28; - sy = icursH28; - done = false; - for (r = 0; (DWORD)r < NUM_XY_SLOTS && !done; r++) { + int i = mx + (icursW / 2); + int j = my + (icursH / 2); + int sx = icursW28; + int sy = icursH28; + bool done = false; + int r = 0; + for (; r < NUM_XY_SLOTS && !done; r++) { int xo = RIGHT_PANEL; int yo = 0; if (r >= SLOTXY_BELT_FIRST) { @@ -873,7 +825,8 @@ void CheckInvPaste(int pnum, int mx, int my) } if (!done) return; - il = ILOC_UNEQUIPABLE; + + item_equip_type il = ILOC_UNEQUIPABLE; if (r >= SLOTXY_HEAD_FIRST && r <= SLOTXY_HEAD_LAST) il = ILOC_HELM; if (r >= SLOTXY_RING_LEFT && r <= SLOTXY_RING_RIGHT) @@ -886,9 +839,9 @@ void CheckInvPaste(int pnum, int mx, int my) il = ILOC_ARMOR; if (r >= SLOTXY_BELT_FIRST && r <= SLOTXY_BELT_LAST) il = ILOC_BELT; - done = false; - if (plr[pnum].HoldItem._iLoc == il) - done = true; + + done = plr[pnum].HoldItem._iLoc == il; + if (il == ILOC_ONEHAND && plr[pnum].HoldItem._iLoc == ILOC_TWOHAND) { if (plr[pnum]._pClass == HeroClass::Barbarian && (plr[pnum].HoldItem._itype == ITYPE_SWORD || plr[pnum].HoldItem._itype == ITYPE_MACE)) @@ -909,15 +862,15 @@ void CheckInvPaste(int pnum, int mx, int my) } } + int it = 0; if (il == ILOC_UNEQUIPABLE) { done = true; - it = 0; - ii = r - SLOTXY_INV_FIRST; + int ii = r - SLOTXY_INV_FIRST; if (plr[pnum].HoldItem._itype == ITYPE_GOLD) { - yy = 10 * (ii / 10); - xx = ii % 10; + int yy = 10 * (ii / 10); + int xx = ii % 10; if (plr[pnum].InvGrid[xx + yy] != 0) { - iv = plr[pnum].InvGrid[xx + yy]; + int iv = plr[pnum].InvGrid[xx + yy]; if (iv > 0) { if (plr[pnum].InvList[iv - 1]._itype != ITYPE_GOLD) { it = iv; @@ -927,13 +880,13 @@ void CheckInvPaste(int pnum, int mx, int my) } } } else { - yy = 10 * ((ii / 10) - ((sy - 1) / 2)); + int yy = 10 * ((ii / 10) - ((sy - 1) / 2)); if (yy < 0) yy = 0; for (j = 0; j < sy && done; j++) { if (yy >= NUM_INV_GRID_ELEM) done = false; - xx = (ii % 10) - ((sx - 1) / 2); + int xx = (ii % 10) - ((sx - 1) / 2); if (xx < 0) xx = 0; for (i = 0; i < sx && done; i++) { @@ -941,14 +894,15 @@ void CheckInvPaste(int pnum, int mx, int my) done = false; } else { if (plr[pnum].InvGrid[xx + yy] != 0) { - iv = plr[pnum].InvGrid[xx + yy]; + int iv = plr[pnum].InvGrid[xx + yy]; if (iv < 0) iv = -iv; if (it != 0) { if (it != iv) done = false; - } else + } else { it = iv; + } } } xx++; @@ -972,7 +926,7 @@ void CheckInvPaste(int pnum, int mx, int my) if (pnum == myplr) PlaySFX(ItemInvSnds[ItemCAnimTbl[plr[pnum].HoldItem._iCurs]]); - cn = CURSOR_HAND; + int cn = CURSOR_HAND; switch (il) { case ILOC_HELM: NetSendCmdChItem(false, INVLOC_HEAD); @@ -1058,7 +1012,7 @@ void CheckInvPaste(int pnum, int mx, int my) break; case ILOC_TWOHAND: if (!plr[pnum].InvBody[INVLOC_HAND_LEFT].isEmpty() && !plr[pnum].InvBody[INVLOC_HAND_RIGHT].isEmpty()) { - tempitem = plr[pnum].HoldItem; + ItemStruct tempitem = plr[pnum].HoldItem; if (plr[pnum].InvBody[INVLOC_HAND_RIGHT]._itype == ITYPE_SHIELD) plr[pnum].HoldItem = plr[pnum].InvBody[INVLOC_HAND_RIGHT]; else @@ -1067,7 +1021,7 @@ void CheckInvPaste(int pnum, int mx, int my) NewCursor(plr[pnum].HoldItem._iCurs + CURSOR_FIRSTITEM); else SetICursor(plr[pnum].HoldItem._iCurs + CURSOR_FIRSTITEM); - done2h = AutoPlaceItemInInventory(plr[pnum], plr[pnum].HoldItem, true); + bool done2h = AutoPlaceItemInInventory(plr[pnum], plr[pnum].HoldItem, true); plr[pnum].HoldItem = tempitem; if (pnum == myplr) NewCursor(plr[pnum].HoldItem._iCurs + CURSOR_FIRSTITEM); @@ -1108,14 +1062,13 @@ void CheckInvPaste(int pnum, int mx, int my) break; case ILOC_UNEQUIPABLE: if (plr[pnum].HoldItem._itype == ITYPE_GOLD && it == 0) { - ii = r - SLOTXY_INV_FIRST; - yy = 10 * (ii / 10); - xx = ii % 10; + int ii = r - SLOTXY_INV_FIRST; + int yy = 10 * (ii / 10); + int xx = ii % 10; if (plr[pnum].InvGrid[yy + xx] > 0) { - il = plr[pnum].InvGrid[yy + xx]; - il--; - gt = plr[pnum].InvList[il]._ivalue; - ig = plr[pnum].HoldItem._ivalue + gt; + int il = plr[pnum].InvGrid[yy + xx] - 1; + int gt = plr[pnum].InvList[il]._ivalue; + int ig = plr[pnum].HoldItem._ivalue + gt; if (ig <= MaxGold) { plr[pnum].InvList[il]._ivalue = ig; plr[pnum]._pGold += plr[pnum].HoldItem._ivalue; @@ -1131,7 +1084,7 @@ void CheckInvPaste(int pnum, int mx, int my) cn += CURSOR_FIRSTITEM; } } else { - il = plr[pnum]._pNumInv; + int il = plr[pnum]._pNumInv; plr[pnum].InvList[il] = plr[pnum].HoldItem; plr[pnum]._pNumInv++; plr[pnum].InvGrid[yy + xx] = plr[pnum]._pNumInv; @@ -1144,12 +1097,12 @@ void CheckInvPaste(int pnum, int mx, int my) plr[pnum]._pNumInv++; it = plr[pnum]._pNumInv; } else { - il = it - 1; + int il = it - 1; if (plr[pnum].HoldItem._itype == ITYPE_GOLD) plr[pnum]._pGold += plr[pnum].HoldItem._ivalue; cn = SwapItem(&plr[pnum].InvList[il], &plr[pnum].HoldItem); if (plr[pnum].HoldItem._itype == ITYPE_GOLD) - plr[pnum]._pGold = CalculateGold(pnum); + plr[pnum]._pGold = CalculateGold(plr[pnum]); for (i = 0; i < NUM_INV_GRID_ELEM; i++) { if (plr[pnum].InvGrid[i] == it) plr[pnum].InvGrid[i] = 0; @@ -1157,21 +1110,17 @@ void CheckInvPaste(int pnum, int mx, int my) plr[pnum].InvGrid[i] = 0; } } - ii = r - SLOTXY_INV_FIRST; + int ii = r - SLOTXY_INV_FIRST; // Calculate top-left position of item for InvGrid and then add item to InvGrid - yy = 10 * (ii / 10 - ((sy - 1) / 2)); - xx = (ii % 10 - ((sx - 1) / 2)); - if (yy < 0) - yy = 0; - if (xx < 0) - xx = 0; + int xx = std::max(ii % 10 - ((sx - 1) / 2), 0); + int yy = std::max(10 * (ii / 10 - ((sy - 1) / 2)), 0); AddItemToInvGrid(plr[pnum], xx + yy, it, sx, sy); } break; - case ILOC_BELT: - ii = r - SLOTXY_BELT_FIRST; + case ILOC_BELT: { + int ii = r - SLOTXY_BELT_FIRST; if (plr[pnum].HoldItem._itype == ITYPE_GOLD) { if (!plr[pnum].SpdList[ii].isEmpty()) { if (plr[pnum].SpdList[ii]._itype == ITYPE_GOLD) { @@ -1204,9 +1153,12 @@ void CheckInvPaste(int pnum, int mx, int my) } else { cn = SwapItem(&plr[pnum].SpdList[ii], &plr[pnum].HoldItem); if (plr[pnum].HoldItem._itype == ITYPE_GOLD) - plr[pnum]._pGold = CalculateGold(pnum); + plr[pnum]._pGold = CalculateGold(plr[pnum]); } drawsbarflag = true; + } break; + case ILOC_NONE: + case ILOC_INVALID: break; } CalcPlrInv(pnum, true); @@ -1219,25 +1171,23 @@ void CheckInvPaste(int pnum, int mx, int my) void CheckInvSwap(int pnum, BYTE bLoc, int idx, uint16_t wCI, int seed, bool bId, uint32_t dwBuff) { - PlayerStruct *p; - memset(&items[MAXITEMS], 0, sizeof(*items)); RecreateItem(MAXITEMS, idx, wCI, seed, 0, (dwBuff & CF_HELLFIRE) != 0); - p = &plr[pnum]; - p->HoldItem = items[MAXITEMS]; + auto &player = plr[pnum]; + player.HoldItem = items[MAXITEMS]; if (bId) { - p->HoldItem._iIdentified = true; + player.HoldItem._iIdentified = true; } if (bLoc < NUM_INVLOC) { - p->InvBody[bLoc] = p->HoldItem; + player.InvBody[bLoc] = player.HoldItem; - if (bLoc == INVLOC_HAND_LEFT && p->HoldItem._iLoc == ILOC_TWOHAND) { - p->InvBody[INVLOC_HAND_RIGHT]._itype = ITYPE_NONE; - } else if (bLoc == INVLOC_HAND_RIGHT && p->HoldItem._iLoc == ILOC_TWOHAND) { - p->InvBody[INVLOC_HAND_LEFT]._itype = ITYPE_NONE; + if (bLoc == INVLOC_HAND_LEFT && player.HoldItem._iLoc == ILOC_TWOHAND) { + player.InvBody[INVLOC_HAND_RIGHT]._itype = ITYPE_NONE; + } else if (bLoc == INVLOC_HAND_RIGHT && player.HoldItem._iLoc == ILOC_TWOHAND) { + player.InvBody[INVLOC_HAND_LEFT]._itype = ITYPE_NONE; } } @@ -1246,10 +1196,6 @@ void CheckInvSwap(int pnum, BYTE bLoc, int idx, uint16_t wCI, int seed, bool bId void CheckInvCut(int pnum, int mx, int my, bool automaticMove) { - int r; - bool done; - char ii; - int iv, ig; auto &player = plr[pnum]; if (player._pmode > PM_WALK3) { @@ -1261,9 +1207,10 @@ void CheckInvCut(int pnum, int mx, int my, bool automaticMove) dropGoldValue = 0; } - done = false; + bool done = false; - for (r = 0; (DWORD)r < NUM_XY_SLOTS && !done; r++) { + int r = 0; + for (; (DWORD)r < NUM_XY_SLOTS && !done; r++) { int xo = RIGHT_PANEL; int yo = 0; if (r >= SLOTXY_BELT_FIRST) { @@ -1392,13 +1339,10 @@ void CheckInvCut(int pnum, int mx, int my, bool automaticMove) } if (r >= SLOTXY_INV_FIRST && r <= SLOTXY_INV_LAST) { - ig = r - SLOTXY_INV_FIRST; - ii = player.InvGrid[ig]; + int ig = r - SLOTXY_INV_FIRST; + int ii = player.InvGrid[ig]; if (ii != 0) { - iv = ii; - if (ii <= 0) { - iv = -ii; - } + int iv = (ii < 0) ? -ii : ii; holdItem = player.InvList[iv - 1]; if (automaticMove) { @@ -1432,11 +1376,11 @@ void CheckInvCut(int pnum, int mx, int my, bool automaticMove) if (!holdItem.isEmpty()) { if (holdItem._itype == ITYPE_GOLD) { - player._pGold = CalculateGold(pnum); + player._pGold = CalculateGold(player); } CalcPlrInv(pnum, true); - CheckItemStats(pnum); + CheckItemStats(player); if (pnum == myplr) { if (automaticallyEquipped) { @@ -1469,11 +1413,7 @@ void inv_update_rem_item(int pnum, BYTE iv) plr[pnum].InvBody[iv]._itype = ITYPE_NONE; } - if (plr[pnum]._pmode != PM_DEATH) { - CalcPlrInv(pnum, true); - } else { - CalcPlrInv(pnum, false); - } + CalcPlrInv(pnum, plr[pnum]._pmode != PM_DEATH); } void RemoveSpdBarItem(int pnum, int iv) @@ -1504,46 +1444,91 @@ void CheckInvScrn(bool isShiftHeld) } } -void CheckItemStats(int pnum) +void CheckItemStats(PlayerStruct &player) { - PlayerStruct *p = &plr[pnum]; + ItemStruct &item = player.HoldItem; - p->HoldItem._iStatFlag = false; + item._iStatFlag = false; - if (p->_pStrength >= p->HoldItem._iMinStr - && p->_pMagic >= p->HoldItem._iMinMag - && p->_pDexterity >= p->HoldItem._iMinDex) { - p->HoldItem._iStatFlag = true; + if (player._pStrength >= item._iMinStr + && player._pMagic >= item._iMinMag + && player._pDexterity >= item._iMinDex) { + item._iStatFlag = true; } } -void CheckBookLevel(int pnum) +static void CheckBookLevel(PlayerStruct &player) { - int slvl; - - if (plr[pnum].HoldItem._iMiscId == IMISC_BOOK) { - plr[pnum].HoldItem._iMinMag = spelldata[plr[pnum].HoldItem._iSpell].sMinInt; - slvl = plr[pnum]._pSplLvl[plr[pnum].HoldItem._iSpell]; - while (slvl != 0) { - plr[pnum].HoldItem._iMinMag += 20 * plr[pnum].HoldItem._iMinMag / 100; - slvl--; - if (plr[pnum].HoldItem._iMinMag + 20 * plr[pnum].HoldItem._iMinMag / 100 > 255) { - plr[pnum].HoldItem._iMinMag = -1; - slvl = 0; - } + if (player.HoldItem._iMiscId != IMISC_BOOK) + return; + + player.HoldItem._iMinMag = spelldata[player.HoldItem._iSpell].sMinInt; + int slvl = player._pSplLvl[player.HoldItem._iSpell]; + while (slvl != 0) { + player.HoldItem._iMinMag += 20 * player.HoldItem._iMinMag / 100; + slvl--; + if (player.HoldItem._iMinMag + 20 * player.HoldItem._iMinMag / 100 > 255) { + player.HoldItem._iMinMag = -1; + slvl = 0; } } } -void CheckQuestItem(int pnum) +static void CheckNaKrulNotes(PlayerStruct &player) { - if (plr[pnum].HoldItem.IDidx == IDI_OPTAMULET && quests[Q_BLIND]._qactive == QUEST_ACTIVE) + int idx = player.HoldItem.IDidx; + + if (idx != IDI_NOTE1 && idx != IDI_NOTE2 && idx != IDI_NOTE3) { + return; + } + + int n1; + if (idx != IDI_NOTE1 && !player.HasItem(IDI_NOTE1, &n1)) { + return; + } + + int n2; + if (idx != IDI_NOTE2 && !player.HasItem(IDI_NOTE2, &n2)) { + return; + } + + int n3; + if (idx != IDI_NOTE3 && !player.HasItem(IDI_NOTE3, &n3)) { + return; + } + + plr[myplr].Say(HeroSpeech::JustWhatIWasLookingFor, 10); + + if (idx != IDI_NOTE1) { + player.RemoveInvItem(n1); + } + if (idx != IDI_NOTE2) { + player.RemoveInvItem(n2); + } + if (idx != IDI_NOTE3) { + player.RemoveInvItem(n3); + } + + int itemNum = itemactive[0]; + ItemStruct tmp = items[itemNum]; + memset(&items[itemNum], 0, sizeof(*items)); + GetItemAttrs(itemNum, IDI_FULLNOTE, 16); + SetupItem(itemNum); + player.HoldItem = items[itemNum]; + items[itemNum] = tmp; +} + +static void CheckQuestItem(PlayerStruct &player) +{ + if (player.HoldItem.IDidx == IDI_OPTAMULET && quests[Q_BLIND]._qactive == QUEST_ACTIVE) quests[Q_BLIND]._qactive = QUEST_DONE; - if (plr[pnum].HoldItem.IDidx == IDI_MUSHROOM && quests[Q_MUSHROOM]._qactive == QUEST_ACTIVE && quests[Q_MUSHROOM]._qvar1 == QS_MUSHSPAWNED) { - plr[pnum].Say(HeroSpeech::NowThatsOneBigMushroom, 10); // BUGFIX: Voice for this quest might be wrong in MP + + if (player.HoldItem.IDidx == IDI_MUSHROOM && quests[Q_MUSHROOM]._qactive == QUEST_ACTIVE && quests[Q_MUSHROOM]._qvar1 == QS_MUSHSPAWNED) { + player.Say(HeroSpeech::NowThatsOneBigMushroom, 10); // BUGFIX: Voice for this quest might be wrong in MP quests[Q_MUSHROOM]._qvar1 = QS_MUSHPICKED; } - if (plr[pnum].HoldItem.IDidx == IDI_ANVIL && quests[Q_ANVIL]._qactive != QUEST_NOTAVAIL) { + + if (player.HoldItem.IDidx == IDI_ANVIL && quests[Q_ANVIL]._qactive != QUEST_NOTAVAIL) { if (quests[Q_ANVIL]._qactive == QUEST_INIT) { quests[Q_ANVIL]._qactive = QUEST_ACTIVE; } @@ -1551,10 +1536,12 @@ void CheckQuestItem(int pnum) plr[myplr].Say(HeroSpeech::INeedToGetThisToGriswold, 10); } } - if (plr[pnum].HoldItem.IDidx == IDI_GLDNELIX && quests[Q_VEIL]._qactive != QUEST_NOTAVAIL) { + + if (player.HoldItem.IDidx == IDI_GLDNELIX && quests[Q_VEIL]._qactive != QUEST_NOTAVAIL) { plr[myplr].Say(HeroSpeech::INeedToGetThisToLachdanan, 30); } - if (plr[pnum].HoldItem.IDidx == IDI_ROCK && quests[Q_ROCK]._qactive != QUEST_NOTAVAIL) { + + if (player.HoldItem.IDidx == IDI_ROCK && quests[Q_ROCK]._qactive != QUEST_NOTAVAIL) { if (quests[Q_ROCK]._qactive == QUEST_INIT) { quests[Q_ROCK]._qactive = QUEST_ACTIVE; } @@ -1562,59 +1549,20 @@ void CheckQuestItem(int pnum) plr[myplr].Say(HeroSpeech::ThisMustBeWhatGriswoldWanted, 10); } } - if (plr[pnum].HoldItem.IDidx == IDI_ARMOFVAL && quests[Q_BLOOD]._qactive == QUEST_ACTIVE) { + + if (player.HoldItem.IDidx == IDI_ARMOFVAL && quests[Q_BLOOD]._qactive == QUEST_ACTIVE) { quests[Q_BLOOD]._qactive = QUEST_DONE; plr[myplr].Say(HeroSpeech::MayTheSpiritOfArkaineProtectMe, 20); } - if (plr[pnum].HoldItem.IDidx == IDI_MAPOFDOOM) { + + if (player.HoldItem.IDidx == IDI_MAPOFDOOM) { quests[Q_GRAVE]._qlog = false; quests[Q_GRAVE]._qactive = QUEST_ACTIVE; quests[Q_GRAVE]._qvar1 = 1; plr[myplr].Say(HeroSpeech::UhHuh, 10); } - if (plr[pnum].HoldItem.IDidx == IDI_NOTE1 || plr[pnum].HoldItem.IDidx == IDI_NOTE2 || plr[pnum].HoldItem.IDidx == IDI_NOTE3) { - int mask, idx, item_num; - ItemStruct tmp; - mask = 0; - idx = plr[pnum].HoldItem.IDidx; - if (plr[pnum].HasItem(IDI_NOTE1) || idx == IDI_NOTE1) - mask = 1; - if (plr[pnum].HasItem(IDI_NOTE2) || idx == IDI_NOTE2) - mask |= 2; - if (plr[pnum].HasItem(IDI_NOTE3) || idx == IDI_NOTE3) - mask |= 4; - if (mask == 7) { - int n1, n2, n3; - plr[myplr].Say(HeroSpeech::JustWhatIWasLookingFor, 10); - switch (idx) { - case IDI_NOTE1: - plr[pnum].HasItem(IDI_NOTE2, &n2); - plr[pnum].RemoveInvItem(n2); - plr[pnum].HasItem(IDI_NOTE3, &n3); - plr[pnum].RemoveInvItem(n3); - break; - case IDI_NOTE2: - plr[pnum].HasItem(IDI_NOTE1, &n1); - plr[pnum].RemoveInvItem(n1); - plr[pnum].HasItem(IDI_NOTE3, &n3); - plr[pnum].RemoveInvItem(n3); - break; - case IDI_NOTE3: - plr[pnum].HasItem(IDI_NOTE1, &n1); - plr[pnum].RemoveInvItem(n1); - plr[pnum].HasItem(IDI_NOTE2, &n2); - plr[pnum].RemoveInvItem(n2); - break; - } - item_num = itemactive[0]; - tmp = items[item_num]; - memset(&items[item_num], 0, sizeof(*items)); - GetItemAttrs(item_num, IDI_FULLNOTE, 16); - SetupItem(item_num); - plr[pnum].HoldItem = items[item_num]; - items[item_num] = tmp; - } - } + + CheckNaKrulNotes(player); } void CleanupItems(ItemStruct *item, int ii) @@ -1657,15 +1605,15 @@ void InvGetItem(int pnum, ItemStruct *item, int ii) item->_iCreateInfo &= ~CF_PREGEN; plr[pnum].HoldItem = *item; - CheckQuestItem(pnum); - CheckBookLevel(pnum); - CheckItemStats(pnum); - bool cursor_updated = false; - if (plr[pnum].HoldItem._itype == ITYPE_GOLD && GoldAutoPlace(pnum)) - cursor_updated = true; + CheckQuestItem(plr[pnum]); + CheckBookLevel(plr[pnum]); + CheckItemStats(plr[pnum]); + bool cursorUpdated = false; + if (plr[pnum].HoldItem._itype == ITYPE_GOLD && GoldAutoPlace(plr[pnum])) + cursorUpdated = true; CleanupItems(item, ii); pcursitem = -1; - if (!cursor_updated) + if (!cursorUpdated) NewCursor(plr[pnum].HoldItem._iCurs + CURSOR_FIRSTITEM); } @@ -1687,12 +1635,12 @@ void AutoGetItem(int pnum, ItemStruct *item, int ii) item->_iCreateInfo &= ~CF_PREGEN; plr[pnum].HoldItem = *item; /// BUGFIX: overwrites cursor item, allowing for belt dupe bug - CheckQuestItem(pnum); - CheckBookLevel(pnum); - CheckItemStats(pnum); + CheckQuestItem(plr[pnum]); + CheckBookLevel(plr[pnum]); + CheckItemStats(plr[pnum]); SetICursor(plr[pnum].HoldItem._iCurs + CURSOR_FIRSTITEM); if (plr[pnum].HoldItem._itype == ITYPE_GOLD) { - done = GoldAutoPlace(pnum); + done = GoldAutoPlace(plr[pnum]); if (!done) { item->_ivalue = plr[pnum].HoldItem._ivalue; SetPlrHandGoldCurs(item); @@ -1746,7 +1694,7 @@ void SyncGetItem(int x, int y, int idx, uint16_t ci, int iseed) { int ii; - if (dItem[x][y]) { + if (dItem[x][y] != 0) { ii = dItem[x][y] - 1; if (items[ii].IDidx == idx && items[ii]._iSeed == iseed @@ -1768,9 +1716,7 @@ void SyncGetItem(int x, int y, int idx, uint16_t ci, int iseed) bool CanPut(int x, int y) { - char oi, oi2; - - if (dItem[x][y]) + if (dItem[x][y] != 0) return false; if (nSolidTable[dPiece[x][y]]) return false; @@ -1780,7 +1726,7 @@ bool CanPut(int x, int y) return false; } - oi = dObject[x + 1][y + 1]; + int8_t oi = dObject[x + 1][y + 1]; if (oi > 0 && object[oi - 1]._oSelFlag != 0) { return false; } @@ -1790,7 +1736,7 @@ bool CanPut(int x, int y) oi = dObject[x + 1][y]; if (oi > 0) { - oi2 = dObject[x][y + 1]; + int8_t oi2 = dObject[x][y + 1]; if (oi2 > 0 && object[oi - 1]._oSelFlag != 0 && object[oi2 - 1]._oSelFlag != 0) return false; } @@ -1829,9 +1775,7 @@ bool TryInvPut() void DrawInvMsg(const char *msg) { - DWORD dwTicks; - - dwTicks = SDL_GetTicks(); + uint32_t dwTicks = SDL_GetTicks(); if (dwTicks - sgdwLastTime >= 5000) { sgdwLastTime = dwTicks; ErrorPlrMsg(msg); @@ -1926,7 +1870,7 @@ int InvPutItem(int pnum, Point position) return ii; } -int SyncPutItem(int pnum, Point position, int idx, uint16_t icreateinfo, int iseed, int Id, int dur, int mdur, int ch, int mch, int ivalue, DWORD ibuff, int to_hit, int max_dam, int min_str, int min_mag, int min_dex, int ac) +int SyncPutItem(int pnum, Point position, int idx, uint16_t icreateinfo, int iseed, int id, int dur, int mdur, int ch, int mch, int ivalue, DWORD ibuff, int toHit, int maxDam, int minStr, int minMag, int minDex, int ac) { if (!PutItem(pnum, position)) return -1; @@ -1938,20 +1882,20 @@ int SyncPutItem(int pnum, Point position, int idx, uint16_t icreateinfo, int ise dItem[position.x][position.y] = ii + 1; if (idx == IDI_EAR) { - RecreateEar(ii, icreateinfo, iseed, Id, dur, mdur, ch, mch, ivalue, ibuff); + RecreateEar(ii, icreateinfo, iseed, id, dur, mdur, ch, mch, ivalue, ibuff); } else { RecreateItem(ii, idx, icreateinfo, iseed, ivalue, (ibuff & CF_HELLFIRE) != 0); - if (Id) + if (id != 0) items[ii]._iIdentified = true; items[ii]._iDurability = dur; items[ii]._iMaxDur = mdur; items[ii]._iCharges = ch; items[ii]._iMaxCharges = mch; - items[ii]._iPLToHit = to_hit; - items[ii]._iMaxDam = max_dam; - items[ii]._iMinStr = min_str; - items[ii]._iMinMag = min_mag; - items[ii]._iMinDex = min_dex; + items[ii]._iPLToHit = toHit; + items[ii]._iMaxDam = maxDam; + items[ii]._iMinStr = minStr; + items[ii]._iMinMag = minMag; + items[ii]._iMinDex = minDex; items[ii]._iAC = ac; items[ii].dwBuff = ibuff; } @@ -1970,11 +1914,8 @@ int SyncPutItem(int pnum, Point position, int idx, uint16_t icreateinfo, int ise char CheckInvHLight() { - int r, ii, nGold; - ItemStruct *pi; - PlayerStruct *p; - - for (r = 0; (DWORD)r < NUM_XY_SLOTS; r++) { + int r = 0; + for (; (DWORD)r < NUM_XY_SLOTS; r++) { int xo = RIGHT_PANEL; int yo = 0; if (r >= SLOTXY_BELT_FIRST) { @@ -1995,47 +1936,47 @@ char CheckInvHLight() int8_t rv = -1; infoclr = UIS_SILVER; - pi = nullptr; - p = &plr[myplr]; + ItemStruct *pi = nullptr; + auto &myPlayer = plr[myplr]; ClearPanel(); if (r >= SLOTXY_HEAD_FIRST && r <= SLOTXY_HEAD_LAST) { rv = INVLOC_HEAD; - pi = &p->InvBody[rv]; + pi = &myPlayer.InvBody[rv]; } else if (r == SLOTXY_RING_LEFT) { rv = INVLOC_RING_LEFT; - pi = &p->InvBody[rv]; + pi = &myPlayer.InvBody[rv]; } else if (r == SLOTXY_RING_RIGHT) { rv = INVLOC_RING_RIGHT; - pi = &p->InvBody[rv]; + pi = &myPlayer.InvBody[rv]; } else if (r == SLOTXY_AMULET) { rv = INVLOC_AMULET; - pi = &p->InvBody[rv]; + pi = &myPlayer.InvBody[rv]; } else if (r >= SLOTXY_HAND_LEFT_FIRST && r <= SLOTXY_HAND_LEFT_LAST) { rv = INVLOC_HAND_LEFT; - pi = &p->InvBody[rv]; + pi = &myPlayer.InvBody[rv]; } else if (r >= SLOTXY_HAND_RIGHT_FIRST && r <= SLOTXY_HAND_RIGHT_LAST) { - pi = &p->InvBody[INVLOC_HAND_LEFT]; + pi = &myPlayer.InvBody[INVLOC_HAND_LEFT]; if (pi->isEmpty() || pi->_iLoc != ILOC_TWOHAND - || (p->_pClass == HeroClass::Barbarian && (p->InvBody[INVLOC_HAND_LEFT]._itype == ITYPE_SWORD || p->InvBody[INVLOC_HAND_LEFT]._itype == ITYPE_MACE))) { + || (myPlayer._pClass == HeroClass::Barbarian && (myPlayer.InvBody[INVLOC_HAND_LEFT]._itype == ITYPE_SWORD || myPlayer.InvBody[INVLOC_HAND_LEFT]._itype == ITYPE_MACE))) { rv = INVLOC_HAND_RIGHT; - pi = &p->InvBody[rv]; + pi = &myPlayer.InvBody[rv]; } else { rv = INVLOC_HAND_LEFT; } } else if (r >= SLOTXY_CHEST_FIRST && r <= SLOTXY_CHEST_LAST) { rv = INVLOC_CHEST; - pi = &p->InvBody[rv]; + pi = &myPlayer.InvBody[rv]; } else if (r >= SLOTXY_INV_FIRST && r <= SLOTXY_INV_LAST) { - r = abs(p->InvGrid[r - SLOTXY_INV_FIRST]); + r = abs(myPlayer.InvGrid[r - SLOTXY_INV_FIRST]); if (r == 0) return -1; - ii = r - 1; + int ii = r - 1; rv = ii + INVITEM_INV_FIRST; - pi = &p->InvList[ii]; + pi = &myPlayer.InvList[ii]; } else if (r >= SLOTXY_BELT_FIRST) { r -= SLOTXY_BELT_FIRST; drawsbarflag = true; - pi = &p->SpdList[r]; + pi = &myPlayer.SpdList[r]; if (pi->isEmpty()) return -1; rv = r + INVITEM_BELT_FIRST; @@ -2045,7 +1986,7 @@ char CheckInvHLight() return -1; if (pi->_itype == ITYPE_GOLD) { - nGold = pi->_ivalue; + int nGold = pi->_ivalue; strcpy(infostr, fmt::format(ngettext("{:d} gold piece", "{:d} gold pieces", nGold), nGold).c_str()); } else { if (pi->_iMagical == ITEM_QUALITY_MAGIC) { @@ -2067,9 +2008,7 @@ char CheckInvHLight() void RemoveScroll(int pnum) { - int i; - - for (i = 0; i < plr[pnum]._pNumInv; i++) { + for (int i = 0; i < plr[pnum]._pNumInv; i++) { if (!plr[pnum].InvList[i].isEmpty() && (plr[pnum].InvList[i]._iMiscId == IMISC_SCROLL || plr[pnum].InvList[i]._iMiscId == IMISC_SCROLLT) && plr[pnum].InvList[i]._iSpell == plr[pnum]._pRSpell) { @@ -2078,7 +2017,7 @@ void RemoveScroll(int pnum) return; } } - for (i = 0; i < MAXBELTITEMS; i++) { + for (int i = 0; i < MAXBELTITEMS; i++) { if (!plr[pnum].SpdList[i].isEmpty() && (plr[pnum].SpdList[i]._iMiscId == IMISC_SCROLL || plr[pnum].SpdList[i]._iMiscId == IMISC_SCROLLT) && plr[pnum].SpdList[i]._iSpell == plr[pnum]._pSpell) { @@ -2091,24 +2030,22 @@ void RemoveScroll(int pnum) bool UseScroll() { - int i; - if (pcurs != CURSOR_HAND) return false; if (leveltype == DTYPE_TOWN && !spelldata[plr[myplr]._pRSpell].sTownSpell) return false; - for (i = 0; i < plr[myplr]._pNumInv; i++) { + for (int i = 0; i < plr[myplr]._pNumInv; i++) { if (!plr[myplr].InvList[i].isEmpty() && (plr[myplr].InvList[i]._iMiscId == IMISC_SCROLL || plr[myplr].InvList[i]._iMiscId == IMISC_SCROLLT) && plr[myplr].InvList[i]._iSpell == plr[myplr]._pRSpell) { return true; } } - for (i = 0; i < MAXBELTITEMS; i++) { - if (!plr[myplr].SpdList[i].isEmpty() - && (plr[myplr].SpdList[i]._iMiscId == IMISC_SCROLL || plr[myplr].SpdList[i]._iMiscId == IMISC_SCROLLT) - && plr[myplr].SpdList[i]._iSpell == plr[myplr]._pRSpell) { + for (auto &item : plr[myplr].SpdList) { + if (!item.isEmpty() + && (item._iMiscId == IMISC_SCROLL || item._iMiscId == IMISC_SCROLLT) + && item._iSpell == plr[myplr]._pRSpell) { return true; } } @@ -2131,16 +2068,14 @@ void UseStaffCharge(int pnum) bool UseStaff() { - if (pcurs == CURSOR_HAND) { - if (!plr[myplr].InvBody[INVLOC_HAND_LEFT].isEmpty() - && (plr[myplr].InvBody[INVLOC_HAND_LEFT]._iMiscId == IMISC_STAFF || plr[myplr].InvBody[INVLOC_HAND_LEFT]._iMiscId == IMISC_UNIQUE) - && plr[myplr].InvBody[INVLOC_HAND_LEFT]._iSpell == plr[myplr]._pRSpell - && plr[myplr].InvBody[INVLOC_HAND_LEFT]._iCharges > 0) { - return true; - } + if (pcurs != CURSOR_HAND) { + return false; } - return false; + return !plr[myplr].InvBody[INVLOC_HAND_LEFT].isEmpty() + && (plr[myplr].InvBody[INVLOC_HAND_LEFT]._iMiscId == IMISC_STAFF || plr[myplr].InvBody[INVLOC_HAND_LEFT]._iMiscId == IMISC_UNIQUE) + && plr[myplr].InvBody[INVLOC_HAND_LEFT]._iSpell == plr[myplr]._pRSpell + && plr[myplr].InvBody[INVLOC_HAND_LEFT]._iCharges > 0; } void StartGoldDrop() @@ -2158,9 +2093,8 @@ void StartGoldDrop() bool UseInvItem(int pnum, int cii) { - int c, idata; - ItemStruct *Item; - bool speedlist; + int c; + ItemStruct *item; if (plr[pnum]._pInvincible && plr[pnum]._pHitPoints == 0 && pnum == myplr) return true; @@ -2171,19 +2105,19 @@ bool UseInvItem(int pnum, int cii) if (cii < INVITEM_INV_FIRST) return false; + bool speedlist = false; if (cii <= INVITEM_INV_LAST) { c = cii - INVITEM_INV_FIRST; - Item = &plr[pnum].InvList[c]; - speedlist = false; + item = &plr[pnum].InvList[c]; } else { if (talkflag) return true; c = cii - INVITEM_BELT_FIRST; - Item = &plr[pnum].SpdList[c]; + item = &plr[pnum].SpdList[c]; speedlist = true; } - switch (Item->IDidx) { + switch (item->IDidx) { case IDI_MUSHROOM: plr[pnum].Say(HeroSpeech::NowThatsOneBigMushroom, 10); return true; @@ -2193,15 +2127,15 @@ bool UseInvItem(int pnum, int cii) return true; } - if (!AllItemsList[Item->IDidx].iUsable) + if (!AllItemsList[item->IDidx].iUsable) return false; - if (!Item->_iStatFlag) { + if (!item->_iStatFlag) { plr[pnum].Say(HeroSpeech::ICantUseThisYet); return true; } - if (Item->_iMiscId == IMISC_NONE && Item->_itype == ITYPE_GOLD) { + if (item->_iMiscId == IMISC_NONE && item->_itype == ITYPE_GOLD) { StartGoldDrop(); return true; } @@ -2211,25 +2145,25 @@ bool UseInvItem(int pnum, int cii) dropGoldValue = 0; } - if (Item->_iMiscId == IMISC_SCROLL && currlevel == 0 && !spelldata[Item->_iSpell].sTownSpell) { + if (item->_iMiscId == IMISC_SCROLL && currlevel == 0 && !spelldata[item->_iSpell].sTownSpell) { return true; } - if (Item->_iMiscId == IMISC_SCROLLT && currlevel == 0 && !spelldata[Item->_iSpell].sTownSpell) { + if (item->_iMiscId == IMISC_SCROLLT && currlevel == 0 && !spelldata[item->_iSpell].sTownSpell) { return true; } - if (Item->_iMiscId > IMISC_RUNEFIRST && Item->_iMiscId < IMISC_RUNELAST && currlevel == 0) { + if (item->_iMiscId > IMISC_RUNEFIRST && item->_iMiscId < IMISC_RUNELAST && currlevel == 0) { return true; } - idata = ItemCAnimTbl[Item->_iCurs]; - if (Item->_iMiscId == IMISC_BOOK) + int idata = ItemCAnimTbl[item->_iCurs]; + if (item->_iMiscId == IMISC_BOOK) PlaySFX(IS_RBOOK); else if (pnum == myplr) PlaySFX(ItemInvSnds[idata]); - UseItem(pnum, Item->_iMiscId, Item->_iSpell); + UseItem(pnum, item->_iMiscId, item->_iSpell); if (speedlist) { if (plr[pnum].SpdList[c]._iMiscId == IMISC_NOTE) { @@ -2263,20 +2197,18 @@ void DoTelekinesis() NewCursor(CURSOR_HAND); } -int CalculateGold(int pnum) +int CalculateGold(PlayerStruct &player) { - int i, gold; - - gold = 0; - for (i = 0; i < MAXBELTITEMS; i++) { - if (plr[pnum].SpdList[i]._itype == ITYPE_GOLD) { - gold += plr[pnum].SpdList[i]._ivalue; + int gold = 0; + for (auto &item : player.SpdList) { + if (item._itype == ITYPE_GOLD) { + gold += item._ivalue; force_redraw = 255; } } - for (i = 0; i < plr[pnum]._pNumInv; i++) { - if (plr[pnum].InvList[i]._itype == ITYPE_GOLD) - gold += plr[pnum].InvList[i]._ivalue; + for (int i = 0; i < player._pNumInv; i++) { + if (player.InvList[i]._itype == ITYPE_GOLD) + gold += player.InvList[i]._ivalue; } return gold; diff --git a/Source/inv.h b/Source/inv.h index 574868ba6..b46a3bfbd 100644 --- a/Source/inv.h +++ b/Source/inv.h @@ -101,14 +101,14 @@ bool AutoEquip(int playerNumber, const ItemStruct &item, bool persistItem = true bool AutoPlaceItemInInventory(PlayerStruct &player, const ItemStruct &item, bool persistItem = false); bool AutoPlaceItemInInventorySlot(PlayerStruct &player, int slotIndex, const ItemStruct &item, bool persistItem); bool AutoPlaceItemInBelt(int playerNumber, const ItemStruct &item, bool persistItem = false); -bool GoldAutoPlace(int pnum); -bool GoldAutoPlaceInInventorySlot(int pnum, int slotIndex); +bool GoldAutoPlace(PlayerStruct &player); +bool GoldAutoPlaceInInventorySlot(PlayerStruct &player, int slotIndex); void CheckInvSwap(int pnum, BYTE bLoc, int idx, uint16_t wCI, int seed, bool bId, uint32_t dwBuff); void inv_update_rem_item(int pnum, BYTE iv); void RemoveSpdBarItem(int pnum, int iv); void CheckInvItem(bool isShiftHeld = false); void CheckInvScrn(bool isShiftHeld); -void CheckItemStats(int pnum); +void CheckItemStats(PlayerStruct &player); void InvGetItem(int pnum, ItemStruct *item, int ii); void AutoGetItem(int pnum, ItemStruct *item, int ii); int FindGetItem(int idx, uint16_t ci, int iseed); @@ -117,7 +117,7 @@ bool CanPut(int x, int y); bool TryInvPut(); void DrawInvMsg(const char *msg); int InvPutItem(int pnum, Point position); -int SyncPutItem(int pnum, Point position, int idx, uint16_t icreateinfo, int iseed, int Id, int dur, int mdur, int ch, int mch, int ivalue, DWORD ibuff, int to_hit, int max_dam, int min_str, int min_mag, int min_dex, int ac); +int SyncPutItem(int pnum, Point position, int idx, uint16_t icreateinfo, int iseed, int Id, int dur, int mdur, int ch, int mch, int ivalue, DWORD ibuff, int toHit, int maxDam, int minStr, int minMag, int minDex, int ac); char CheckInvHLight(); void RemoveScroll(int pnum); bool UseScroll(); @@ -125,7 +125,7 @@ void UseStaffCharge(int pnum); bool UseStaff(); bool UseInvItem(int pnum, int cii); void DoTelekinesis(); -int CalculateGold(int pnum); +int CalculateGold(PlayerStruct &player); bool DropItemBeforeTrig(); /* data */ diff --git a/Source/minitext.cpp b/Source/minitext.cpp index de7a78779..158e7fab5 100644 --- a/Source/minitext.cpp +++ b/Source/minitext.cpp @@ -47,7 +47,7 @@ void LoadText(const char *text) size_t previous = 0; while (true) { - size_t next = paragraphs.find("\n", previous); + size_t next = paragraphs.find('\n', previous); TextLines.emplace_back(paragraphs.substr(previous, next)); if (next == std::string::npos) break; @@ -65,8 +65,8 @@ int CalculateTextSpeed(int nSFX) const int numLines = TextLines.size(); #ifndef NOSOUND - Uint32 SfxFrames = GetSFXLength(nSFX); - assert(SfxFrames != 0); + Uint32 sfxFrames = GetSFXLength(nSFX); + assert(sfxFrames != 0); #else // Sound is disabled -- estimate length from the number of lines. Uint32 SfxFrames = numLines * 3000; @@ -75,7 +75,7 @@ int CalculateTextSpeed(int nSFX) int textHeight = LineHeight * numLines; textHeight += LineHeight * 5; // adjust so when speaker is done two line are left - return SfxFrames / textHeight; + return sfxFrames / textHeight; } int CalculateTextPosition() diff --git a/Source/player.cpp b/Source/player.cpp index a08407113..c4c69e72b 100644 --- a/Source/player.cpp +++ b/Source/player.cpp @@ -2029,7 +2029,7 @@ void DropHalfPlayersGold(int pnum) } } } - player._pGold = CalculateGold(pnum); + player._pGold = CalculateGold(player); } void StripTopGold(int pnum) @@ -2054,12 +2054,12 @@ void StripTopGold(int pnum) GetGoldSeed(pnum, &player.HoldItem); player.HoldItem._ivalue = val; SetPlrHandGoldCurs(&player.HoldItem); - if (!GoldAutoPlace(pnum)) + if (!GoldAutoPlace(player)) PlrDeadItem(player, &player.HoldItem, 0, 0); } } } - player._pGold = CalculateGold(pnum); + player._pGold = CalculateGold(player); player.HoldItem = tmpItem; } diff --git a/Source/player.h b/Source/player.h index 665f089e2..af4b8d3a6 100644 --- a/Source/player.h +++ b/Source/player.h @@ -375,7 +375,7 @@ struct PlayerStruct { void Say(HeroSpeech speechId) const; /** * @brief Says a speech line after a given delay. - * @param delay Multiple of 20ms wait before starting the speech + * @param delay Multiple of 50ms wait before starting the speech */ void Say(HeroSpeech speechId, int delay) const; /** diff --git a/Source/stores.cpp b/Source/stores.cpp index 2211e5bbe..de3156336 100644 --- a/Source/stores.cpp +++ b/Source/stores.cpp @@ -61,7 +61,7 @@ _speech_id gossipstart; _speech_id gossipend; /** Maps from towner IDs to NPC names. */ -const char *const talkname[] = { +const char *const TownerNames[] = { "Griswold", "Pepin", "", @@ -81,10 +81,8 @@ void DrawSTextBack(const CelOutputBuffer &out) void DrawSSlider(const CelOutputBuffer &out, int y1, int y2) { - int yd1, yd2, yd3; - - yd1 = y1 * 12 + 44 + UI_OFFSET_Y; - yd2 = y2 * 12 + 44 + UI_OFFSET_Y; + int yd1 = y1 * 12 + 44 + UI_OFFSET_Y; + int yd2 = y2 * 12 + 44 + UI_OFFSET_Y; if (stextscrlubtn != -1) CelDrawTo(out, PANEL_X + 601, yd1, *pSTextSlidCels, 12); else @@ -94,7 +92,8 @@ void DrawSSlider(const CelOutputBuffer &out, int y1, int y2) else CelDrawTo(out, PANEL_X + 601, yd2, *pSTextSlidCels, 9); yd1 += 12; - for (yd3 = yd1; yd3 < yd2; yd3 += 12) { + int yd3 = yd1; + for (; yd3 < yd2; yd3 += 12) { CelDrawTo(out, PANEL_X + 601, yd3, *pSTextSlidCels, 14); } if (stextsel == 22) @@ -139,8 +138,6 @@ void AddSText(int x, int y, const char *str, uint16_t flags, bool sel) void PrintStoreItem(ItemStruct *x, int l, uint16_t flags) { char sstr[128]; - char str, dex; - BYTE mag; sstr[0] = '\0'; if (x->_iIdentified) { @@ -152,18 +149,18 @@ void PrintStoreItem(ItemStruct *x, int l, uint16_t flags) } if (x->_iSufPower != -1) { PrintItemPower(x->_iSufPower, x); - if (sstr[0]) + if (sstr[0] != '\0') strcat(sstr, _(", ")); strcat(sstr, tempstr); } } - if (x->_iMiscId == IMISC_STAFF && x->_iMaxCharges) { + if (x->_iMiscId == IMISC_STAFF && x->_iMaxCharges != 0) { strcpy(tempstr, fmt::format(_("Charges: {:d}/{:d}"), x->_iCharges, x->_iMaxCharges).c_str()); - if (sstr[0]) + if (sstr[0] != '\0') strcat(sstr, _(", ")); strcat(sstr, tempstr); } - if (sstr[0]) { + if (sstr[0] != '\0') { AddSText(40, l, sstr, flags, false); l++; } @@ -172,7 +169,7 @@ void PrintStoreItem(ItemStruct *x, int l, uint16_t flags) strcpy(sstr, fmt::format(_("Damage: {:d}-{:d} "), x->_iMinDam, x->_iMaxDam).c_str()); if (x->_iClass == ICLASS_ARMOR) strcpy(sstr, fmt::format(_("Armor: {:d} "), x->_iAC).c_str()); - if (x->_iMaxDur != DUR_INDESTRUCTIBLE && x->_iMaxDur) { + if (x->_iMaxDur != DUR_INDESTRUCTIBLE && x->_iMaxDur != 0) { strcpy(tempstr, fmt::format(_("Dur: {:d}/{:d}, "), x->_iDurability, x->_iMaxDur).c_str()); strcat(sstr, tempstr); } else { @@ -180,18 +177,18 @@ void PrintStoreItem(ItemStruct *x, int l, uint16_t flags) } if (x->_itype == ITYPE_MISC) sstr[0] = '\0'; - str = x->_iMinStr; - mag = x->_iMinMag; - dex = x->_iMinDex; + int8_t str = x->_iMinStr; + uint8_t mag = x->_iMinMag; + int8_t dex = x->_iMinDex; if (str == 0 && mag == 0 && dex == 0) { strcat(sstr, _("No required attributes")); } else { strcpy(tempstr, _("Required:")); - if (str) + if (str != 0) strcpy(tempstr + strlen(tempstr), fmt::format(_(" {:d} Str"), str).c_str()); - if (mag) + if (mag != 0) strcpy(tempstr + strlen(tempstr), fmt::format(_(" {:d} Mag"), mag).c_str()); - if (dex) + if (dex != 0) strcpy(tempstr + strlen(tempstr), fmt::format(_(" {:d} Dex"), dex).c_str()); strcat(sstr, tempstr); } @@ -229,26 +226,23 @@ void S_StartSmith() void S_ScrollSBuy(int idx) { - int l, ls; - - ls = idx; ClearSText(5, 21); stextup = 5; - for (l = 5; l < 20; l += 4) { - if (!smithitem[ls].isEmpty()) { - uint16_t iclr = smithitem[ls].getTextColor(); + for (int l = 5; l < 20; l += 4) { + if (!smithitem[idx].isEmpty()) { + uint16_t iclr = smithitem[idx].getTextColor(); - if (smithitem[ls]._iMagical) { - AddSText(20, l, smithitem[ls]._iIName, iclr, true); + if (smithitem[idx]._iMagical != 0) { + AddSText(20, l, smithitem[idx]._iIName, iclr, true); } else { - AddSText(20, l, smithitem[ls]._iName, iclr, true); + AddSText(20, l, smithitem[idx]._iName, iclr, true); } - AddSTextVal(l, smithitem[ls]._iIvalue); - PrintStoreItem(&smithitem[ls], l + 1, iclr); + AddSTextVal(l, smithitem[idx]._iIvalue); + PrintStoreItem(&smithitem[idx], l + 1, iclr); stextdown = l; - ls++; + idx++; } } @@ -280,20 +274,18 @@ void S_StartSBuy() stextsmax = 0; } -void S_ScrollSPBuy(int idx) +void S_ScrollSPBuy(int boughtitems) { - int l, boughtitems; - ClearSText(5, 21); - boughtitems = idx; - stextup = 5; - for (idx = 0; boughtitems; idx++) { + + int idx = 0; + for (; boughtitems != 0; idx++) { if (!premiumitems[idx].isEmpty()) boughtitems--; } - for (l = 5; l < 20 && idx < SMITH_PREMIUM_ITEMS; l += 4) { + for (int l = 5; l < 20 && idx < SMITH_PREMIUM_ITEMS; l += 4) { if (!premiumitems[idx].isEmpty()) { uint16_t iclr = premiumitems[idx].getTextColor(); AddSText(20, l, premiumitems[idx]._iIName, iclr, true); @@ -318,7 +310,7 @@ bool S_StartSPBuy() if (!premiumitems[i].isEmpty()) storenumh++; } - if (!storenumh) { + if (storenumh == 0) { StartStore(STORE_SMITH); stextsel = 14; return false; @@ -387,7 +379,7 @@ void S_ScrollSSell(int idx) if (!storehold[idx].isEmpty()) { uint16_t iclr = storehold[idx].getTextColor(); - if (storehold[idx]._iMagical && storehold[idx]._iIdentified) { + if (storehold[idx]._iMagical != 0 && storehold[idx]._iIdentified) { AddSText(20, l, storehold[idx]._iIName, iclr, true); AddSTextVal(l, storehold[idx]._iIvalue); } else { @@ -408,17 +400,14 @@ void S_ScrollSSell(int idx) void S_StartSSell() { - int i; - bool sellok; - stextsize = true; - sellok = false; + bool sellok = false; storenumh = 0; - for (i = 0; i < 48; i++) + for (int i = 0; i < 48; i++) storehold[i]._itype = ITYPE_NONE; - for (i = 0; i < plr[myplr]._pNumInv; i++) { + for (int i = 0; i < plr[myplr]._pNumInv; i++) { if (storenumh >= 48) break; if (SmithSellOk(i)) { @@ -428,15 +417,14 @@ void S_StartSSell() if (storehold[storenumh]._iMagical != ITEM_QUALITY_NORMAL && storehold[storenumh]._iIdentified) storehold[storenumh]._ivalue = storehold[storenumh]._iIvalue; - if ((storehold[storenumh]._ivalue >>= 2) == 0) - storehold[storenumh]._ivalue = 1; - + storehold[storenumh]._ivalue = std::max(storehold[storenumh]._ivalue / 4, 1); storehold[storenumh]._iIvalue = storehold[storenumh]._ivalue; - storehidx[storenumh++] = i; + storehidx[storenumh] = i; + storenumh++; } } - for (i = 0; i < MAXBELTITEMS; i++) { + for (int i = 0; i < MAXBELTITEMS; i++) { if (storenumh >= 48) break; if (SmithSellOk(-(i + 1))) { @@ -446,11 +434,10 @@ void S_StartSSell() if (storehold[storenumh]._iMagical != ITEM_QUALITY_NORMAL && storehold[storenumh]._iIdentified) storehold[storenumh]._ivalue = storehold[storenumh]._iIvalue; - if (!(storehold[storenumh]._ivalue >>= 2)) - storehold[storenumh]._ivalue = 1; - + storehold[storenumh]._ivalue = std::max(storehold[storenumh]._ivalue / 4, 1); storehold[storenumh]._iIvalue = storehold[storenumh]._ivalue; - storehidx[storenumh++] = -(i + 1); + storehidx[storenumh] = -(i + 1); + storenumh++; } } @@ -577,26 +564,23 @@ void S_StartWitch() void S_ScrollWBuy(int idx) { - int l, ls; - - ls = idx; ClearSText(5, 21); stextup = 5; - for (l = 5; l < 20; l += 4) { - if (!witchitem[ls].isEmpty()) { - uint16_t iclr = witchitem[ls].getTextColor(); + for (int l = 5; l < 20; l += 4) { + if (!witchitem[idx].isEmpty()) { + uint16_t iclr = witchitem[idx].getTextColor(); - if (witchitem[ls]._iMagical) { - AddSText(20, l, witchitem[ls]._iIName, iclr, true); + if (witchitem[idx]._iMagical != 0) { + AddSText(20, l, witchitem[idx]._iIName, iclr, true); } else { - AddSText(20, l, witchitem[ls]._iName, iclr, true); + AddSText(20, l, witchitem[idx]._iName, iclr, true); } - AddSTextVal(l, witchitem[ls]._iIvalue); - PrintStoreItem(&witchitem[ls], l + 1, iclr); + AddSTextVal(l, witchitem[idx]._iIvalue); + PrintStoreItem(&witchitem[idx], l + 1, iclr); stextdown = l; - ls++; + idx++; } } @@ -1114,15 +1098,15 @@ void S_StartIdShow() void S_StartTalk() { - int i, sn, sn2, la; + int la; stextsize = false; stextscrl = false; - strcpy(tempstr, fmt::format(_("Talk to {:s}"), talkname[talker]).c_str()); + strcpy(tempstr, fmt::format(_("Talk to {:s}"), TownerNames[talker]).c_str()); AddSText(0, 2, tempstr, UIS_GOLD | UIS_CENTER, false); AddSLine(5); if (gbIsSpawn) { - strcpy(tempstr, fmt::format(_("Talking to {:s}"), talkname[talker]).c_str()); + strcpy(tempstr, fmt::format(_("Talking to {:s}"), TownerNames[talker]).c_str()); AddSText(0, 10, tempstr, UIS_SILVER | UIS_CENTER, false); AddSText(0, 12, _("is not available"), UIS_SILVER | UIS_CENTER, false); AddSText(0, 14, _("in the shareware"), UIS_SILVER | UIS_CENTER, false); @@ -1131,8 +1115,8 @@ void S_StartTalk() return; } - sn = 0; - for (i = 0; i < MAXQUESTS; i++) { + int sn = 0; + for (int i = 0; i < MAXQUESTS; i++) { if (quests[i]._qactive == QUEST_ACTIVE && Qtalklist[talker][i] != TEXT_NONE && quests[i]._qlog) sn++; } @@ -1145,9 +1129,9 @@ void S_StartTalk() la = 2; } - sn2 = sn - 2; + int sn2 = sn - 2; - for (i = 0; i < MAXQUESTS; i++) { + for (int i = 0; i < MAXQUESTS; i++) { if (quests[i]._qactive == QUEST_ACTIVE && Qtalklist[talker][i] != TEXT_NONE && quests[i]._qlog) { AddSText(0, sn, _(questlist[i]._qlstr), UIS_SILVER | UIS_CENTER, true); sn += la; @@ -1283,16 +1267,14 @@ void S_SBuyEnter() */ void SmithBuyPItem() { - int i, xx, idx; - TakePlrsMoney(plr[myplr].HoldItem._iIvalue); if (plr[myplr].HoldItem._iMagical == ITEM_QUALITY_NORMAL) plr[myplr].HoldItem._iIdentified = false; StoreAutoPlace(); - idx = stextvhold + ((stextlhold - stextup) / 4); - xx = 0; - for (i = 0; idx >= 0; i++) { + int idx = stextvhold + ((stextlhold - stextup) / 4); + int xx = 0; + for (int i = 0; idx >= 0; i++) { if (!premiumitems[i].isEmpty()) { idx--; xx = i; @@ -1306,65 +1288,62 @@ void SmithBuyPItem() void S_SPBuyEnter() { - int i, idx, xx; - bool done; - if (stextsel == 22) { StartStore(STORE_SMITH); stextsel = 14; - } else { - stextshold = STORE_SPBUY; - stextlhold = stextsel; - stextvhold = stextsval; - xx = stextsval + ((stextsel - stextup) / 4); - idx = 0; - for (i = 0; xx >= 0; i++) { - if (!premiumitems[i].isEmpty()) { - xx--; - idx = i; - } - } - if (plr[myplr]._pGold < premiumitems[idx]._iIvalue) { - StartStore(STORE_NOMONEY); - } else { - plr[myplr].HoldItem = premiumitems[idx]; - NewCursor(plr[myplr].HoldItem._iCurs + CURSOR_FIRSTITEM); - done = false; - if (AutoEquipEnabled(plr[myplr], plr[myplr].HoldItem) && AutoEquip(myplr, plr[myplr].HoldItem, false)) { - done = true; - } + return; + } - if (done || AutoPlaceItemInInventory(plr[myplr], plr[myplr].HoldItem, false)) - StartStore(STORE_CONFIRM); - else - StartStore(STORE_NOROOM); - NewCursor(CURSOR_HAND); + stextshold = STORE_SPBUY; + stextlhold = stextsel; + stextvhold = stextsval; + + int xx = stextsval + ((stextsel - stextup) / 4); + int idx = 0; + for (int i = 0; xx >= 0; i++) { + if (!premiumitems[i].isEmpty()) { + xx--; + idx = i; } } + + if (plr[myplr]._pGold < premiumitems[idx]._iIvalue) { + StartStore(STORE_NOMONEY); + return; + } + + plr[myplr].HoldItem = premiumitems[idx]; + NewCursor(plr[myplr].HoldItem._iCurs + CURSOR_FIRSTITEM); + bool done = AutoEquipEnabled(plr[myplr], plr[myplr].HoldItem) && AutoEquip(myplr, plr[myplr].HoldItem, false); + + if (done || AutoPlaceItemInInventory(plr[myplr], plr[myplr].HoldItem, false)) + StartStore(STORE_CONFIRM); + else + StartStore(STORE_NOROOM); + + NewCursor(CURSOR_HAND); } bool StoreGoldFit(int idx) { - int i, sz, cost, numsqrs; - - cost = storehold[idx]._iIvalue; - sz = cost / MaxGold; + int cost = storehold[idx]._iIvalue; + int sz = cost / MaxGold; if (cost % MaxGold != 0) sz++; NewCursor(storehold[idx]._iCurs + CURSOR_FIRSTITEM); - numsqrs = cursW / 28 * (cursH / 28); + int numsqrs = cursW / 28 * (cursH / 28); NewCursor(CURSOR_HAND); if (numsqrs >= sz) return true; - for (i = 0; i < NUM_INV_GRID_ELEM; i++) { + for (int i = 0; i < NUM_INV_GRID_ELEM; i++) { if (plr[myplr].InvGrid[i] == 0) numsqrs++; } - for (i = 0; i < plr[myplr]._pNumInv; i++) { + for (int i = 0; i < plr[myplr]._pNumInv; i++) { if (plr[myplr].InvList[i]._itype == ITYPE_GOLD && plr[myplr].InvList[i]._ivalue != MaxGold) { if (cost + plr[myplr].InvList[i]._ivalue <= MaxGold) cost = 0; @@ -1374,7 +1353,7 @@ bool StoreGoldFit(int idx) } sz = cost / MaxGold; - if (cost % MaxGold) + if ((cost % MaxGold) != 0) sz++; return numsqrs >= sz; @@ -1386,23 +1365,18 @@ bool StoreGoldFit(int idx) */ void PlaceStoreGold(int v) { - bool done; - int ii, xx, yy, i; - - done = false; - - for (i = 0; i < NUM_INV_GRID_ELEM && !done; i++) { - yy = 10 * (i / 10); - xx = i % 10; + for (int i = 0; i < NUM_INV_GRID_ELEM; i++) { + int xx = i % 10; + int yy = 10 * (i / 10); if (plr[myplr].InvGrid[xx + yy] == 0) { - ii = plr[myplr]._pNumInv; + int ii = plr[myplr]._pNumInv; GetGoldSeed(myplr, &golditem); plr[myplr].InvList[ii] = golditem; plr[myplr]._pNumInv++; plr[myplr].InvGrid[xx + yy] = plr[myplr]._pNumInv; plr[myplr].InvList[ii]._ivalue = v; SetGoldCurs(myplr, ii); - done = true; + return; } } } @@ -1412,14 +1386,13 @@ void PlaceStoreGold(int v) */ void StoreSellItem() { - int i, idx, cost; - - idx = stextvhold + ((stextlhold - stextup) / 4); + int idx = stextvhold + ((stextlhold - stextup) / 4); if (storehidx[idx] >= 0) plr[myplr].RemoveInvItem(storehidx[idx]); else RemoveSpdBarItem(myplr, -(storehidx[idx] + 1)); - cost = storehold[idx]._iIvalue; + + int cost = storehold[idx]._iIvalue; storenumh--; if (idx != storenumh) { while (idx < storenumh) { @@ -1429,7 +1402,7 @@ void StoreSellItem() } } plr[myplr]._pGold += cost; - for (i = 0; i < plr[myplr]._pNumInv && cost > 0; i++) { + for (int i = 0; i < plr[myplr]._pNumInv && cost > 0; i++) { if (plr[myplr].InvList[i]._itype == ITYPE_GOLD && plr[myplr].InvList[i]._ivalue != MaxGold) { if (cost + plr[myplr].InvList[i]._ivalue <= MaxGold) { plr[myplr].InvList[i]._ivalue += cost; @@ -1453,23 +1426,22 @@ void StoreSellItem() void S_SSellEnter() { - int idx; - if (stextsel == 22) { StartStore(STORE_SMITH); stextsel = 16; - } else { - stextlhold = stextsel; - idx = stextsval + ((stextsel - stextup) / 4); - stextshold = STORE_SSELL; - stextvhold = stextsval; - plr[myplr].HoldItem = storehold[idx]; - - if (StoreGoldFit(idx)) - StartStore(STORE_CONFIRM); - else - StartStore(STORE_NOROOM); + return; } + + stextlhold = stextsel; + int idx = stextsval + ((stextsel - stextup) / 4); + stextshold = STORE_SSELL; + stextvhold = stextsval; + plr[myplr].HoldItem = storehold[idx]; + + if (StoreGoldFit(idx)) + StartStore(STORE_CONFIRM); + else + StartStore(STORE_NOROOM); } /** @@ -1477,14 +1449,13 @@ void S_SSellEnter() */ void SmithRepairItem() { - int i, idx; - TakePlrsMoney(plr[myplr].HoldItem._iIvalue); - idx = stextvhold + ((stextlhold - stextup) / 4); + int idx = stextvhold + ((stextlhold - stextup) / 4); storehold[idx]._iDurability = storehold[idx]._iMaxDur; - i = storehidx[idx]; + int i = storehidx[idx]; + if (i < 0) { if (i == -1) plr[myplr].InvBody[INVLOC_HEAD]._iDurability = plr[myplr].InvBody[INVLOC_HEAD]._iMaxDur; @@ -1494,29 +1465,30 @@ void SmithRepairItem() plr[myplr].InvBody[INVLOC_HAND_LEFT]._iDurability = plr[myplr].InvBody[INVLOC_HAND_LEFT]._iMaxDur; if (i == -4) plr[myplr].InvBody[INVLOC_HAND_RIGHT]._iDurability = plr[myplr].InvBody[INVLOC_HAND_RIGHT]._iMaxDur; - } else { - plr[myplr].InvList[i]._iDurability = plr[myplr].InvList[i]._iMaxDur; + return; } + + plr[myplr].InvList[i]._iDurability = plr[myplr].InvList[i]._iMaxDur; } void S_SRepairEnter() { - int idx; - if (stextsel == 22) { StartStore(STORE_SMITH); stextsel = 18; - } else { - stextshold = STORE_SREPAIR; - stextlhold = stextsel; - stextvhold = stextsval; - idx = stextsval + ((stextsel - stextup) / 4); - plr[myplr].HoldItem = storehold[idx]; - if (plr[myplr]._pGold < storehold[idx]._iIvalue) - StartStore(STORE_NOMONEY); - else - StartStore(STORE_CONFIRM); + return; } + + stextshold = STORE_SREPAIR; + stextlhold = stextsel; + stextvhold = stextsval; + + int idx = stextsval + ((stextsel - stextup) / 4); + plr[myplr].HoldItem = storehold[idx]; + if (plr[myplr]._pGold < storehold[idx]._iIvalue) + StartStore(STORE_NOMONEY); + else + StartStore(STORE_CONFIRM); } void S_WitchEnter() @@ -1529,16 +1501,16 @@ void S_WitchEnter() gossipstart = TEXT_ADRIA2; gossipend = TEXT_ADRIA13; StartStore(STORE_GOSSIP); - return; + break; case 14: StartStore(STORE_WBUY); - return; + break; case 16: StartStore(STORE_WSELL); - return; + break; case 18: StartStore(STORE_WRECHARGE); - return; + break; case 20: stextflag = STORE_NONE; break; @@ -1550,9 +1522,7 @@ void S_WitchEnter() */ void WitchBuyItem() { - int idx; - - idx = stextvhold + ((stextlhold - stextup) / 4); + int idx = stextvhold + ((stextlhold - stextup) / 4); if (idx < 3) plr[myplr].HoldItem._iSeed = AdvanceRndSeed(); @@ -1576,56 +1546,53 @@ void WitchBuyItem() void S_WBuyEnter() { - int idx; - bool done; - if (stextsel == 22) { StartStore(STORE_WITCH); stextsel = 14; - } else { - stextlhold = stextsel; - stextvhold = stextsval; - stextshold = STORE_WBUY; - idx = stextsval + ((stextsel - stextup) / 4); + return; + } - if (plr[myplr]._pGold < witchitem[idx]._iIvalue) { - StartStore(STORE_NOMONEY); - } else { - plr[myplr].HoldItem = witchitem[idx]; - NewCursor(plr[myplr].HoldItem._iCurs + CURSOR_FIRSTITEM); - done = false; - if (AutoEquipEnabled(plr[myplr], plr[myplr].HoldItem) && AutoEquip(myplr, plr[myplr].HoldItem, false)) { - done = true; - } + stextlhold = stextsel; + stextvhold = stextsval; + stextshold = STORE_WBUY; - if (done || AutoPlaceItemInInventory(plr[myplr], plr[myplr].HoldItem, false) || AutoPlaceItemInBelt(myplr, plr[myplr].HoldItem, false)) - StartStore(STORE_CONFIRM); - else - StartStore(STORE_NOROOM); + int idx = stextsval + ((stextsel - stextup) / 4); - NewCursor(CURSOR_HAND); - } + if (plr[myplr]._pGold < witchitem[idx]._iIvalue) { + StartStore(STORE_NOMONEY); + return; } + + plr[myplr].HoldItem = witchitem[idx]; + NewCursor(plr[myplr].HoldItem._iCurs + CURSOR_FIRSTITEM); + bool done = AutoEquipEnabled(plr[myplr], plr[myplr].HoldItem) && AutoEquip(myplr, plr[myplr].HoldItem, false); + + if (done || AutoPlaceItemInInventory(plr[myplr], plr[myplr].HoldItem, false) || AutoPlaceItemInBelt(myplr, plr[myplr].HoldItem, false)) + StartStore(STORE_CONFIRM); + else + StartStore(STORE_NOROOM); + + NewCursor(CURSOR_HAND); } void S_WSellEnter() { - int idx; - if (stextsel == 22) { StartStore(STORE_WITCH); stextsel = 16; - } else { - stextlhold = stextsel; - idx = stextsval + ((stextsel - stextup) / 4); - stextshold = STORE_WSELL; - stextvhold = stextsval; - plr[myplr].HoldItem = storehold[idx]; - if (StoreGoldFit(idx)) - StartStore(STORE_CONFIRM); - else - StartStore(STORE_NOROOM); + return; } + + stextlhold = stextsel; + stextshold = STORE_WSELL; + stextvhold = stextsval; + + int idx = stextsval + ((stextsel - stextup) / 4); + plr[myplr].HoldItem = storehold[idx]; + if (StoreGoldFit(idx)) + StartStore(STORE_CONFIRM); + else + StartStore(STORE_NOROOM); } /** @@ -1633,14 +1600,12 @@ void S_WSellEnter() */ void WitchRechargeItem() { - int i, idx; - TakePlrsMoney(plr[myplr].HoldItem._iIvalue); - idx = stextvhold + ((stextlhold - stextup) / 4); + int idx = stextvhold + ((stextlhold - stextup) / 4); storehold[idx]._iCharges = storehold[idx]._iMaxCharges; - i = storehidx[idx]; + int i = storehidx[idx]; if (i < 0) plr[myplr].InvBody[INVLOC_HAND_LEFT]._iCharges = plr[myplr].InvBody[INVLOC_HAND_LEFT]._iMaxCharges; else @@ -1651,22 +1616,22 @@ void WitchRechargeItem() void S_WRechargeEnter() { - int idx; - if (stextsel == 22) { StartStore(STORE_WITCH); stextsel = 18; - } else { - stextshold = STORE_WRECHARGE; - stextlhold = stextsel; - stextvhold = stextsval; - idx = stextsval + ((stextsel - stextup) / 4); - plr[myplr].HoldItem = storehold[idx]; - if (plr[myplr]._pGold < storehold[idx]._iIvalue) - StartStore(STORE_NOMONEY); - else - StartStore(STORE_CONFIRM); + return; } + + stextshold = STORE_WRECHARGE; + stextlhold = stextsel; + stextvhold = stextsval; + + int idx = stextsval + ((stextsel - stextup) / 4); + plr[myplr].HoldItem = storehold[idx]; + if (plr[myplr]._pGold < storehold[idx]._iIvalue) + StartStore(STORE_NOMONEY); + else + StartStore(STORE_CONFIRM); } void S_BoyEnter() @@ -1681,16 +1646,20 @@ void S_BoyEnter() TakePlrsMoney(50); StartStore(STORE_BBOY); } - } else if ((stextsel == 8 && !boyitem.isEmpty()) || (stextsel == 12 && boyitem.isEmpty())) { - talker = TOWN_PEGBOY; - stextshold = STORE_BOY; - stextlhold = stextsel; - gossipstart = TEXT_WIRT2; - gossipend = TEXT_WIRT12; - StartStore(STORE_GOSSIP); - } else { + return; + } + + if ((stextsel != 8 && !boyitem.isEmpty()) || (stextsel != 12 && boyitem.isEmpty())) { stextflag = STORE_NONE; + return; } + + talker = TOWN_PEGBOY; + stextshold = STORE_BOY; + stextlhold = stextsel; + gossipstart = TEXT_WIRT2; + gossipend = TEXT_WIRT12; + StartStore(STORE_GOSSIP); } void BoyBuyItem() @@ -2112,23 +2081,18 @@ void InitStores() void SetupTownStores() { - int i, l; - SetRndSeed(glSeedTbl[currlevel] * SDL_GetTicks()); + + int l = plr[myplr]._pLevel / 2; if (!gbIsMultiplayer) { l = 0; - for (i = 0; i < NUMLEVELS; i++) { + for (int i = 0; i < NUMLEVELS; i++) { if (plr[myplr]._pLvlVisited[i]) l = i; } - } else { - l = plr[myplr]._pLevel / 2; } - l += 2; - if (l < 6) - l = 6; - if (l > 16) - l = 16; + + l = clamp(l + 2, 6, 16); SpawnStoreGold(); SpawnSmith(l); SpawnWitch(l); @@ -2190,19 +2154,18 @@ void PrintSString(const CelOutputBuffer &out, int margin, int line, const char * void DrawSLine(const CelOutputBuffer &out, int y) { - const int sy = y * 12; - BYTE *src, *dst; - int width; - if (stextsize) { - src = out.at(PANEL_LEFT + 26, 25 + UI_OFFSET_Y); - dst = out.at(26 + PANEL_X, sy + 38 + UI_OFFSET_Y); - width = 587; // BUGFIX: should be 587, not 586 (fixed) - } else { - src = out.at(PANEL_LEFT + 346, 25 + UI_OFFSET_Y); - dst = out.at(346 + PANEL_X, sy + 38 + UI_OFFSET_Y); - width = 267; // BUGFIX: should be 267, not 266 (fixed) + int sx = 26; + int sy = y * 12; + int width = 587; + + if (!stextsize) { + sx += SPANEL_WIDTH; + width -= SPANEL_WIDTH; } + BYTE *src = out.at(PANEL_LEFT + sx, UI_OFFSET_Y + 25); + BYTE *dst = out.at(PANEL_X + sx, UI_OFFSET_Y + sy + 38); + for (int i = 0; i < 3; i++, src += out.pitch(), dst += out.pitch()) memcpy(dst, src, width); } @@ -2362,9 +2325,9 @@ void DrawSText(const CelOutputBuffer &out) } for (i = 0; i < STORE_LINES; i++) { - if (stext[i]._sline) + if (stext[i]._sline != 0) DrawSLine(out, i); - if (stext[i]._sstr[0]) + if (stext[i]._sstr[0] != '\0') PrintSString(out, stext[i]._sx, i, stext[i]._sstr, stext[i].flags, stext[i]._sval); } @@ -2456,14 +2419,14 @@ void STextUp() if (stextscrl) { if (stextsel == stextup) { - if (stextsval) + if (stextsval != 0) stextsval--; return; } stextsel--; while (!stext[stextsel]._ssel) { - if (!stextsel) + if (stextsel == 0) stextsel = STORE_LINES - 1; else stextsel--; @@ -2471,13 +2434,13 @@ void STextUp() return; } - if (!stextsel) + if (stextsel == 0) stextsel = STORE_LINES - 1; else stextsel--; while (!stext[stextsel]._ssel) { - if (!stextsel) + if (stextsel == 0) stextsel = STORE_LINES - 1; else stextsel--; @@ -2526,7 +2489,7 @@ void STextPrior() PlaySFX(IS_TITLEMOV); if (stextsel != -1 && stextscrl) { if (stextsel == stextup) { - if (stextsval) + if (stextsval != 0) stextsval -= 4; if (stextsval < 0) stextsval = 0; @@ -2565,7 +2528,7 @@ void TakePlrsMoney(int cost) { int i; - plr[myplr]._pGold = CalculateGold(myplr) - cost; + plr[myplr]._pGold = CalculateGold(plr[myplr]) - cost; for (i = 0; i < MAXBELTITEMS && cost > 0; i++) { if (plr[myplr].SpdList[i]._itype == ITYPE_GOLD && plr[myplr].SpdList[i]._ivalue != MaxGold) { if (cost < plr[myplr].SpdList[i]._ivalue) { diff --git a/test/inv_test.cpp b/test/inv_test.cpp index 92face666..9ab241aa7 100644 --- a/test/inv_test.cpp +++ b/test/inv_test.cpp @@ -113,7 +113,7 @@ TEST(Inv, CalculateGold) plr[myplr].InvList[2]._ivalue = 3; plr[myplr].InvList[3]._ivalue = 30; - EXPECT_EQ(CalculateGold(myplr), 333); + EXPECT_EQ(CalculateGold(plr[myplr]), 333); } // Test automatic gold placing @@ -131,7 +131,7 @@ TEST(Inv, GoldAutoPlace) plr[myplr].HoldItem._itype = ITYPE_GOLD; plr[myplr].HoldItem._ivalue = GOLD_MAX_LIMIT - 100; - GoldAutoPlace(myplr); + GoldAutoPlace(plr[myplr]); // We expect the inventory: // | 5000 | 900 | ... EXPECT_EQ(plr[myplr].InvList[0]._ivalue, GOLD_MAX_LIMIT);