From 9ee4cb4a263284a76a9b57cd7faaf39ca1a4b360 Mon Sep 17 00:00:00 2001 From: Alexander Batalov Date: Tue, 3 Jan 2023 23:00:38 +0300 Subject: [PATCH] Refactor min/max usage --- src/character_editor.cc | 43 +++++++++++++++++------------------------ src/credits.cc | 4 +++- src/dbox.cc | 2 +- src/dfile.cc | 7 +++---- src/game_mouse.cc | 19 ++++++++---------- src/graph_lib.cc | 5 ++--- src/interface.cc | 2 -- src/inventory.cc | 2 -- src/light.cc | 12 +++--------- src/object.cc | 40 +++++++++++++------------------------- src/text_object.cc | 7 ++----- src/tile.cc | 11 ++++------- 12 files changed, 57 insertions(+), 97 deletions(-) diff --git a/src/character_editor.cc b/src/character_editor.cc index a5c31de..70563ec 100644 --- a/src/character_editor.cc +++ b/src/character_editor.cc @@ -5,6 +5,7 @@ #include #include +#include #include #include "art.h" @@ -4937,13 +4938,6 @@ static char* _itostndn(int value, char* dest) // 0x43AAEC static int characterEditorDrawCardWithOptions(int graphicId, const char* name, const char* attributes, char* description) { - unsigned char* ptr; - int v9; - int x; - int y; - short beginnings[WORD_WRAP_MAX_COUNT]; - short beginningsCount; - FrmImage frmImage; int fid = buildFid(OBJ_TYPE_SKILLDEX, graphicId, 0, 0, 0); if (!frmImage.lock(fid)) { @@ -4957,20 +4951,20 @@ static int characterEditorDrawCardWithOptions(int graphicId, const char* name, c gCharacterEditorWindowBuffer + 640 * 309 + 484, 640); - v9 = 150; - ptr = frmImage.getData(); - for (y = 0; y < frmImage.getHeight(); y++) { - for (x = 0; x < frmImage.getWidth(); x++) { - if (HighRGB(*ptr) < 2 && v9 >= x) { - v9 = x; + int extraDescriptionWidth = 150; + unsigned char* data = frmImage.getData(); + for (int y = 0; y < frmImage.getHeight(); y++) { + for (int x = 0; x < frmImage.getWidth(); x++) { + if (HighRGB(*data) < 2) { + extraDescriptionWidth = std::min(extraDescriptionWidth, x); } - ptr++; + data++; } } - v9 -= 8; - if (v9 < 0) { - v9 = 0; + extraDescriptionWidth -= 8; + if (extraDescriptionWidth < 0) { + extraDescriptionWidth = 0; } fontSetCurrent(102); @@ -4985,20 +4979,21 @@ static int characterEditorDrawCardWithOptions(int graphicId, const char* name, c fontDrawText(gCharacterEditorWindowBuffer + 640 * (268 + nameFontLineHeight - attributesFontLineHeight) + 348 + nameWidth + 8, attributes, 640, 640, _colorTable[0]); } - y = nameFontLineHeight; - windowDrawLine(gCharacterEditorWindow, 348, y + 272, 613, y + 272, _colorTable[0]); - windowDrawLine(gCharacterEditorWindow, 348, y + 273, 613, y + 273, _colorTable[0]); + windowDrawLine(gCharacterEditorWindow, 348, nameFontLineHeight + 272, 613, nameFontLineHeight + 272, _colorTable[0]); + windowDrawLine(gCharacterEditorWindow, 348, nameFontLineHeight + 273, 613, nameFontLineHeight + 273, _colorTable[0]); fontSetCurrent(101); int descriptionFontLineHeight = fontGetLineHeight(); - if (wordWrap(description, v9 + 136, beginnings, &beginningsCount) != 0) { + short beginnings[WORD_WRAP_MAX_COUNT]; + short beginningsCount; + if (wordWrap(description, extraDescriptionWidth + 136, beginnings, &beginningsCount) != 0) { // TODO: Leaking graphic handle. return -1; } - y = 315; + int y = 315; for (short i = 0; i < beginningsCount - 1; i++) { short beginning = beginnings[i]; short ending = beginnings[i + 1]; @@ -6660,9 +6655,7 @@ static int perkDialogDrawCard(int frmId, const char* name, const char* rank, cha unsigned char* stride = data; for (int x = 0; x < frmImage.getWidth(); x++) { if (HighRGB(*stride) < 2) { - if (extraDescriptionWidth > x) { - extraDescriptionWidth = x; - } + extraDescriptionWidth = std::min(extraDescriptionWidth, x); } stride++; } diff --git a/src/credits.cc b/src/credits.cc index 2021ac9..c380bbf 100644 --- a/src/credits.cc +++ b/src/credits.cc @@ -2,6 +2,8 @@ #include +#include + #include "art.h" #include "color.h" #include "cycle.h" @@ -112,7 +114,7 @@ void creditsOpen(const char* filePath, int backgroundFid, bool useReversedStyle) fontSetCurrent(gCreditsWindowNameFont); int nameFontLineHeight = fontGetLineHeight(); - int lineHeight = nameFontLineHeight + (titleFontLineHeight >= nameFontLineHeight ? titleFontLineHeight - nameFontLineHeight : 0); + int lineHeight = std::max(titleFontLineHeight, nameFontLineHeight); int stringBufferSize = windowWidth * lineHeight; unsigned char* stringBuffer = (unsigned char*)internal_malloc(stringBufferSize); if (stringBuffer != NULL) { diff --git a/src/dbox.cc b/src/dbox.cc index c348a1f..8ca5d02 100644 --- a/src/dbox.cc +++ b/src/dbox.cc @@ -178,7 +178,7 @@ int showDialogBox(const char* title, const char** body, int bodyLength, int x, i int linesCount = 0; for (int index = 0; index < bodyLength; index++) { - // NOTE: Calls [fontGetStringWidth] twice because of [max] macro. + // NOTE: Originally there is no `max` macro. maximumLineWidth = std::max(fontGetStringWidth(body[index]), maximumLineWidth); linesCount++; } diff --git a/src/dfile.cc b/src/dfile.cc index 6e8e69f..52be55e 100644 --- a/src/dfile.cc +++ b/src/dfile.cc @@ -5,6 +5,8 @@ #include #include +#include + #include #include "platform_compat.h" @@ -818,10 +820,7 @@ static bool dfileReadCompressed(DFile* stream, void* ptr, size_t size) if (stream->decompressionStream->avail_in == 0) { // No more unprocessed data, request next chunk. - size_t bytesToRead = stream->entry->dataSize - stream->compressedBytesRead; - if (bytesToRead > DFILE_DECOMPRESSION_BUFFER_SIZE) { - bytesToRead = DFILE_DECOMPRESSION_BUFFER_SIZE; - } + size_t bytesToRead = std::min(DFILE_DECOMPRESSION_BUFFER_SIZE, stream->entry->dataSize - stream->compressedBytesRead); if (fread(stream->decompressionBuffer, bytesToRead, 1, stream->stream) != 1) { break; diff --git a/src/game_mouse.cc b/src/game_mouse.cc index ddd42bd..9f82fcd 100644 --- a/src/game_mouse.cc +++ b/src/game_mouse.cc @@ -4,6 +4,8 @@ #include #include +#include + #include "actions.h" #include "animation.h" #include "art.h" @@ -802,22 +804,17 @@ void gameMouseRefresh() char formattedActionPoints[8]; int color; - int v6 = _make_path(gDude, gDude->tile, gGameMouseHexCursor->tile, NULL, 1); - if (v6) { + int distance = _make_path(gDude, gDude->tile, gGameMouseHexCursor->tile, NULL, 1); + if (distance != 0) { if (!isInCombat()) { formattedActionPoints[0] = '\0'; color = _colorTable[31744]; } else { - int v7 = critterGetMovementPointCostAdjustedForCrippledLegs(gDude, v6); - int v8; - if (v7 - _combat_free_move >= 0) { - v8 = v7 - _combat_free_move; - } else { - v8 = 0; - } + int actionPointsMax = critterGetMovementPointCostAdjustedForCrippledLegs(gDude, distance); + int actionPointsRequired = std::max(0, actionPointsMax - _combat_free_move); - if (v8 <= gDude->data.critter.combat.ap) { - snprintf(formattedActionPoints, sizeof(formattedActionPoints), "%d", v8); + if (actionPointsRequired <= gDude->data.critter.combat.ap) { + snprintf(formattedActionPoints, sizeof(formattedActionPoints), "%d", actionPointsRequired); color = _colorTable[32767]; } else { snprintf(formattedActionPoints, sizeof(formattedActionPoints), "%c", 'X'); diff --git a/src/graph_lib.cc b/src/graph_lib.cc index a34a9b8..08a65dd 100644 --- a/src/graph_lib.cc +++ b/src/graph_lib.cc @@ -407,9 +407,8 @@ void grayscalePaletteUpdate(int a1, int a2) { if (a1 >= 0 && a2 <= 255) { for (int index = a1; index <= a2; index++) { - // NOTE: The only way to explain so much calls to `Color2RGB` with - // the same repeated pattern is by the use of min/max macros. - + // NOTE: Calls `Color2RGB` many times due to `min` and `max` macro + // uses. int v1 = std::max((Color2RGB(index) & 0x7C00) >> 10, std::max((Color2RGB(index) & 0x3E0) >> 5, Color2RGB(index) & 0x1F)); int v2 = std::min((Color2RGB(index) & 0x7C00) >> 10, std::min((Color2RGB(index) & 0x3E0) >> 5, Color2RGB(index) & 0x1F)); int v3 = v1 + v2; diff --git a/src/interface.cc b/src/interface.cc index e6bd9fa..7d492c3 100644 --- a/src/interface.cc +++ b/src/interface.cc @@ -1355,7 +1355,6 @@ int _intface_update_ammo_lights() int ratio = 0; if (p->isWeapon != 0) { - // calls sub_478674 twice, probably because if min/max kind macro int maximum = ammoGetCapacity(p->item); if (maximum > 0) { int current = ammoGetQuantity(p->item); @@ -1363,7 +1362,6 @@ int _intface_update_ammo_lights() } } else { if (itemGetType(p->item) == ITEM_TYPE_MISC) { - // calls sub_4793D0 twice, probably because if min/max kind macro int maximum = miscItemGetMaxCharges(p->item); if (maximum > 0) { int current = miscItemGetCharges(p->item); diff --git a/src/inventory.cc b/src/inventory.cc index 68fcf63..bf44d3c 100644 --- a/src/inventory.cc +++ b/src/inventory.cc @@ -1947,8 +1947,6 @@ static void _display_inventory_info(Object* item, int quantity, unsigned char* d v9 -= 1; } - // NOTE: Checking for quantity twice probably means inlined function - // or some macro expansion. if (quantity > 1) { if (v9 > 99999) { v9 = 99999; diff --git a/src/light.cc b/src/light.cc index 48f3a84..1077b89 100644 --- a/src/light.cc +++ b/src/light.cc @@ -1,6 +1,6 @@ #include "light.h" -#include +#include #include "map_defs.h" #include "object.h" @@ -55,7 +55,7 @@ void lightSetLightLevel(int lightLevel, bool shouldUpdateScreen) } } -// TODO: Looks strange - it tries to clamp intensity as light level? +// 0x47A980 int _light_get_tile(int elevation, int tile) { if (!elevationIsValid(elevation)) { @@ -66,13 +66,7 @@ int _light_get_tile(int elevation, int tile) return 0; } - int result = gLightIntensity[elevation][tile]; - - if (result >= 0x10000) { - result = 0x10000; - } - - return result; + return std::min(gLightIntensity[elevation][tile], LIGHT_LEVEL_MAX); } // 0x47A9C4 diff --git a/src/object.cc b/src/object.cc index a820746..71861ac 100644 --- a/src/object.cc +++ b/src/object.cc @@ -3,6 +3,8 @@ #include #include +#include + #include "animation.h" #include "art.h" #include "color.h" @@ -808,13 +810,8 @@ void _obj_render_pre_roof(Rect* rect, int elevation) ? gObjectListHeadByTile[topLeftTile + offsets[offsetIndex]] : NULL; if (objectListNode != NULL) { - // NOTE: calls _light_get_tile two times, probably result of min/max macro - int tileLight = _light_get_tile(elevation, objectListNode->obj->tile); - if (tileLight >= ambientLight) { - light = tileLight; - } else { - light = ambientLight; - } + // NOTE: Calls `_light_get_tile` twice. + light = std::max(ambientLight, _light_get_tile(elevation, objectListNode->obj->tile)); } while (objectListNode != NULL) { @@ -852,13 +849,8 @@ void _obj_render_pre_roof(Rect* rect, int elevation) ObjectListNode* objectListNode = _renderTable[i]; if (objectListNode != NULL) { - // NOTE: calls _light_get_tile two times, probably result of min/max macro - int tileLight = _light_get_tile(elevation, objectListNode->obj->tile); - if (tileLight >= ambientLight) { - light = tileLight; - } else { - light = ambientLight; - } + // NOTE: Calls `_light_get_tile` twice. + light = std::max(ambientLight, _light_get_tile(elevation, objectListNode->obj->tile)); } while (objectListNode != NULL) { @@ -1743,34 +1735,28 @@ void _obj_rebuild_all_light() // 0x48AC90 int objectSetLight(Object* obj, int lightDistance, int lightIntensity, Rect* rect) { - int v7; - Rect new_rect; - if (obj == NULL) { return -1; } - v7 = _obj_turn_off_light(obj, rect); + int rc = _obj_turn_off_light(obj, rect); if (lightIntensity > 0) { - if (lightDistance >= 8) { - lightDistance = 8; - } - + obj->lightDistance = std::min(lightDistance, 8); obj->lightIntensity = lightIntensity; - obj->lightDistance = lightDistance; if (rect != NULL) { - v7 = _obj_turn_on_light(obj, &new_rect); - rectUnion(rect, &new_rect, rect); + Rect tempRect; + rc = _obj_turn_on_light(obj, &tempRect); + rectUnion(rect, &tempRect, rect); } else { - v7 = _obj_turn_on_light(obj, NULL); + rc = _obj_turn_on_light(obj, NULL); } } else { obj->lightIntensity = 0; obj->lightDistance = 0; } - return v7; + return rc; } // 0x48AD04 diff --git a/src/text_object.cc b/src/text_object.cc index 4a068dc..8b5e303 100644 --- a/src/text_object.cc +++ b/src/text_object.cc @@ -206,11 +206,8 @@ int textObjectAdd(Object* object, char* string, int font, int color, int a5, Rec char c = *ending; *ending = '\0'; - // NOTE: Calls [fontGetStringWidth] twice, probably result of using min/max macro - int width = fontGetStringWidth(beginning); - if (width >= textObject->width) { - textObject->width = width; - } + // NOTE: Calls `fontGetStringWidth` twice. + textObject->width = std::max(textObject->width, fontGetStringWidth(beginning)); *ending = c; } diff --git a/src/tile.cc b/src/tile.cc index e781c7d..c3a9e9b 100644 --- a/src/tile.cc +++ b/src/tile.cc @@ -4,6 +4,8 @@ #include #include +#include + #include "art.h" #include "color.h" #include "config.h" @@ -1660,13 +1662,8 @@ static void tileRenderFloor(int fid, int x, int y, Rect* rect) int parity = tile & 1; int ambientIntensity = lightGetLightLevel(); for (int i = 0; i < 10; i++) { - // NOTE: calling _light_get_tile two times, probably a result of using __min kind macro - int tileIntensity = _light_get_tile(elev, tile + _verticies[i].offsets[parity]); - if (tileIntensity <= ambientIntensity) { - tileIntensity = ambientIntensity; - } - - _verticies[i].intensity = tileIntensity; + // NOTE: Calls `_light_get_tile` twice. + _verticies[i].intensity = std::max(_light_get_tile(elev, tile + _verticies[i].offsets[parity]), ambientIntensity); } int v23 = 0;