Refactor min/max usage

This commit is contained in:
Alexander Batalov 2023-01-03 23:00:38 +03:00
parent a5cefd6c8b
commit 9ee4cb4a26
12 changed files with 57 additions and 97 deletions

View File

@ -5,6 +5,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <algorithm>
#include <vector> #include <vector>
#include "art.h" #include "art.h"
@ -4937,13 +4938,6 @@ static char* _itostndn(int value, char* dest)
// 0x43AAEC // 0x43AAEC
static int characterEditorDrawCardWithOptions(int graphicId, const char* name, const char* attributes, char* description) 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; FrmImage frmImage;
int fid = buildFid(OBJ_TYPE_SKILLDEX, graphicId, 0, 0, 0); int fid = buildFid(OBJ_TYPE_SKILLDEX, graphicId, 0, 0, 0);
if (!frmImage.lock(fid)) { if (!frmImage.lock(fid)) {
@ -4957,20 +4951,20 @@ static int characterEditorDrawCardWithOptions(int graphicId, const char* name, c
gCharacterEditorWindowBuffer + 640 * 309 + 484, gCharacterEditorWindowBuffer + 640 * 309 + 484,
640); 640);
v9 = 150; int extraDescriptionWidth = 150;
ptr = frmImage.getData(); unsigned char* data = frmImage.getData();
for (y = 0; y < frmImage.getHeight(); y++) { for (int y = 0; y < frmImage.getHeight(); y++) {
for (x = 0; x < frmImage.getWidth(); x++) { for (int x = 0; x < frmImage.getWidth(); x++) {
if (HighRGB(*ptr) < 2 && v9 >= x) { if (HighRGB(*data) < 2) {
v9 = x; extraDescriptionWidth = std::min(extraDescriptionWidth, x);
} }
ptr++; data++;
} }
} }
v9 -= 8; extraDescriptionWidth -= 8;
if (v9 < 0) { if (extraDescriptionWidth < 0) {
v9 = 0; extraDescriptionWidth = 0;
} }
fontSetCurrent(102); 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]); fontDrawText(gCharacterEditorWindowBuffer + 640 * (268 + nameFontLineHeight - attributesFontLineHeight) + 348 + nameWidth + 8, attributes, 640, 640, _colorTable[0]);
} }
y = nameFontLineHeight; windowDrawLine(gCharacterEditorWindow, 348, nameFontLineHeight + 272, 613, nameFontLineHeight + 272, _colorTable[0]);
windowDrawLine(gCharacterEditorWindow, 348, y + 272, 613, y + 272, _colorTable[0]); windowDrawLine(gCharacterEditorWindow, 348, nameFontLineHeight + 273, 613, nameFontLineHeight + 273, _colorTable[0]);
windowDrawLine(gCharacterEditorWindow, 348, y + 273, 613, y + 273, _colorTable[0]);
fontSetCurrent(101); fontSetCurrent(101);
int descriptionFontLineHeight = fontGetLineHeight(); 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. // TODO: Leaking graphic handle.
return -1; return -1;
} }
y = 315; int y = 315;
for (short i = 0; i < beginningsCount - 1; i++) { for (short i = 0; i < beginningsCount - 1; i++) {
short beginning = beginnings[i]; short beginning = beginnings[i];
short ending = beginnings[i + 1]; 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; unsigned char* stride = data;
for (int x = 0; x < frmImage.getWidth(); x++) { for (int x = 0; x < frmImage.getWidth(); x++) {
if (HighRGB(*stride) < 2) { if (HighRGB(*stride) < 2) {
if (extraDescriptionWidth > x) { extraDescriptionWidth = std::min(extraDescriptionWidth, x);
extraDescriptionWidth = x;
}
} }
stride++; stride++;
} }

View File

@ -2,6 +2,8 @@
#include <string.h> #include <string.h>
#include <algorithm>
#include "art.h" #include "art.h"
#include "color.h" #include "color.h"
#include "cycle.h" #include "cycle.h"
@ -112,7 +114,7 @@ void creditsOpen(const char* filePath, int backgroundFid, bool useReversedStyle)
fontSetCurrent(gCreditsWindowNameFont); fontSetCurrent(gCreditsWindowNameFont);
int nameFontLineHeight = fontGetLineHeight(); int nameFontLineHeight = fontGetLineHeight();
int lineHeight = nameFontLineHeight + (titleFontLineHeight >= nameFontLineHeight ? titleFontLineHeight - nameFontLineHeight : 0); int lineHeight = std::max(titleFontLineHeight, nameFontLineHeight);
int stringBufferSize = windowWidth * lineHeight; int stringBufferSize = windowWidth * lineHeight;
unsigned char* stringBuffer = (unsigned char*)internal_malloc(stringBufferSize); unsigned char* stringBuffer = (unsigned char*)internal_malloc(stringBufferSize);
if (stringBuffer != NULL) { if (stringBuffer != NULL) {

View File

@ -178,7 +178,7 @@ int showDialogBox(const char* title, const char** body, int bodyLength, int x, i
int linesCount = 0; int linesCount = 0;
for (int index = 0; index < bodyLength; index++) { 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); maximumLineWidth = std::max(fontGetStringWidth(body[index]), maximumLineWidth);
linesCount++; linesCount++;
} }

View File

@ -5,6 +5,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <algorithm>
#include <fpattern.h> #include <fpattern.h>
#include "platform_compat.h" #include "platform_compat.h"
@ -818,10 +820,7 @@ static bool dfileReadCompressed(DFile* stream, void* ptr, size_t size)
if (stream->decompressionStream->avail_in == 0) { if (stream->decompressionStream->avail_in == 0) {
// No more unprocessed data, request next chunk. // No more unprocessed data, request next chunk.
size_t bytesToRead = stream->entry->dataSize - stream->compressedBytesRead; size_t bytesToRead = std::min(DFILE_DECOMPRESSION_BUFFER_SIZE, stream->entry->dataSize - stream->compressedBytesRead);
if (bytesToRead > DFILE_DECOMPRESSION_BUFFER_SIZE) {
bytesToRead = DFILE_DECOMPRESSION_BUFFER_SIZE;
}
if (fread(stream->decompressionBuffer, bytesToRead, 1, stream->stream) != 1) { if (fread(stream->decompressionBuffer, bytesToRead, 1, stream->stream) != 1) {
break; break;

View File

@ -4,6 +4,8 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <algorithm>
#include "actions.h" #include "actions.h"
#include "animation.h" #include "animation.h"
#include "art.h" #include "art.h"
@ -802,22 +804,17 @@ void gameMouseRefresh()
char formattedActionPoints[8]; char formattedActionPoints[8];
int color; int color;
int v6 = _make_path(gDude, gDude->tile, gGameMouseHexCursor->tile, NULL, 1); int distance = _make_path(gDude, gDude->tile, gGameMouseHexCursor->tile, NULL, 1);
if (v6) { if (distance != 0) {
if (!isInCombat()) { if (!isInCombat()) {
formattedActionPoints[0] = '\0'; formattedActionPoints[0] = '\0';
color = _colorTable[31744]; color = _colorTable[31744];
} else { } else {
int v7 = critterGetMovementPointCostAdjustedForCrippledLegs(gDude, v6); int actionPointsMax = critterGetMovementPointCostAdjustedForCrippledLegs(gDude, distance);
int v8; int actionPointsRequired = std::max(0, actionPointsMax - _combat_free_move);
if (v7 - _combat_free_move >= 0) {
v8 = v7 - _combat_free_move;
} else {
v8 = 0;
}
if (v8 <= gDude->data.critter.combat.ap) { if (actionPointsRequired <= gDude->data.critter.combat.ap) {
snprintf(formattedActionPoints, sizeof(formattedActionPoints), "%d", v8); snprintf(formattedActionPoints, sizeof(formattedActionPoints), "%d", actionPointsRequired);
color = _colorTable[32767]; color = _colorTable[32767];
} else { } else {
snprintf(formattedActionPoints, sizeof(formattedActionPoints), "%c", 'X'); snprintf(formattedActionPoints, sizeof(formattedActionPoints), "%c", 'X');

View File

@ -407,9 +407,8 @@ void grayscalePaletteUpdate(int a1, int a2)
{ {
if (a1 >= 0 && a2 <= 255) { if (a1 >= 0 && a2 <= 255) {
for (int index = a1; index <= a2; index++) { for (int index = a1; index <= a2; index++) {
// NOTE: The only way to explain so much calls to `Color2RGB` with // NOTE: Calls `Color2RGB` many times due to `min` and `max` macro
// the same repeated pattern is by the use of min/max macros. // uses.
int v1 = std::max((Color2RGB(index) & 0x7C00) >> 10, std::max((Color2RGB(index) & 0x3E0) >> 5, Color2RGB(index) & 0x1F)); 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 v2 = std::min((Color2RGB(index) & 0x7C00) >> 10, std::min((Color2RGB(index) & 0x3E0) >> 5, Color2RGB(index) & 0x1F));
int v3 = v1 + v2; int v3 = v1 + v2;

View File

@ -1355,7 +1355,6 @@ int _intface_update_ammo_lights()
int ratio = 0; int ratio = 0;
if (p->isWeapon != 0) { if (p->isWeapon != 0) {
// calls sub_478674 twice, probably because if min/max kind macro
int maximum = ammoGetCapacity(p->item); int maximum = ammoGetCapacity(p->item);
if (maximum > 0) { if (maximum > 0) {
int current = ammoGetQuantity(p->item); int current = ammoGetQuantity(p->item);
@ -1363,7 +1362,6 @@ int _intface_update_ammo_lights()
} }
} else { } else {
if (itemGetType(p->item) == ITEM_TYPE_MISC) { if (itemGetType(p->item) == ITEM_TYPE_MISC) {
// calls sub_4793D0 twice, probably because if min/max kind macro
int maximum = miscItemGetMaxCharges(p->item); int maximum = miscItemGetMaxCharges(p->item);
if (maximum > 0) { if (maximum > 0) {
int current = miscItemGetCharges(p->item); int current = miscItemGetCharges(p->item);

View File

@ -1947,8 +1947,6 @@ static void _display_inventory_info(Object* item, int quantity, unsigned char* d
v9 -= 1; v9 -= 1;
} }
// NOTE: Checking for quantity twice probably means inlined function
// or some macro expansion.
if (quantity > 1) { if (quantity > 1) {
if (v9 > 99999) { if (v9 > 99999) {
v9 = 99999; v9 = 99999;

View File

@ -1,6 +1,6 @@
#include "light.h" #include "light.h"
#include <math.h> #include <algorithm>
#include "map_defs.h" #include "map_defs.h"
#include "object.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) int _light_get_tile(int elevation, int tile)
{ {
if (!elevationIsValid(elevation)) { if (!elevationIsValid(elevation)) {
@ -66,13 +66,7 @@ int _light_get_tile(int elevation, int tile)
return 0; return 0;
} }
int result = gLightIntensity[elevation][tile]; return std::min(gLightIntensity[elevation][tile], LIGHT_LEVEL_MAX);
if (result >= 0x10000) {
result = 0x10000;
}
return result;
} }
// 0x47A9C4 // 0x47A9C4

View File

@ -3,6 +3,8 @@
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
#include <algorithm>
#include "animation.h" #include "animation.h"
#include "art.h" #include "art.h"
#include "color.h" #include "color.h"
@ -808,13 +810,8 @@ void _obj_render_pre_roof(Rect* rect, int elevation)
? gObjectListHeadByTile[topLeftTile + offsets[offsetIndex]] ? gObjectListHeadByTile[topLeftTile + offsets[offsetIndex]]
: NULL; : NULL;
if (objectListNode != NULL) { if (objectListNode != NULL) {
// NOTE: calls _light_get_tile two times, probably result of min/max macro // NOTE: Calls `_light_get_tile` twice.
int tileLight = _light_get_tile(elevation, objectListNode->obj->tile); light = std::max(ambientLight, _light_get_tile(elevation, objectListNode->obj->tile));
if (tileLight >= ambientLight) {
light = tileLight;
} else {
light = ambientLight;
}
} }
while (objectListNode != NULL) { while (objectListNode != NULL) {
@ -852,13 +849,8 @@ void _obj_render_pre_roof(Rect* rect, int elevation)
ObjectListNode* objectListNode = _renderTable[i]; ObjectListNode* objectListNode = _renderTable[i];
if (objectListNode != NULL) { if (objectListNode != NULL) {
// NOTE: calls _light_get_tile two times, probably result of min/max macro // NOTE: Calls `_light_get_tile` twice.
int tileLight = _light_get_tile(elevation, objectListNode->obj->tile); light = std::max(ambientLight, _light_get_tile(elevation, objectListNode->obj->tile));
if (tileLight >= ambientLight) {
light = tileLight;
} else {
light = ambientLight;
}
} }
while (objectListNode != NULL) { while (objectListNode != NULL) {
@ -1743,34 +1735,28 @@ void _obj_rebuild_all_light()
// 0x48AC90 // 0x48AC90
int objectSetLight(Object* obj, int lightDistance, int lightIntensity, Rect* rect) int objectSetLight(Object* obj, int lightDistance, int lightIntensity, Rect* rect)
{ {
int v7;
Rect new_rect;
if (obj == NULL) { if (obj == NULL) {
return -1; return -1;
} }
v7 = _obj_turn_off_light(obj, rect); int rc = _obj_turn_off_light(obj, rect);
if (lightIntensity > 0) { if (lightIntensity > 0) {
if (lightDistance >= 8) { obj->lightDistance = std::min(lightDistance, 8);
lightDistance = 8;
}
obj->lightIntensity = lightIntensity; obj->lightIntensity = lightIntensity;
obj->lightDistance = lightDistance;
if (rect != NULL) { if (rect != NULL) {
v7 = _obj_turn_on_light(obj, &new_rect); Rect tempRect;
rectUnion(rect, &new_rect, rect); rc = _obj_turn_on_light(obj, &tempRect);
rectUnion(rect, &tempRect, rect);
} else { } else {
v7 = _obj_turn_on_light(obj, NULL); rc = _obj_turn_on_light(obj, NULL);
} }
} else { } else {
obj->lightIntensity = 0; obj->lightIntensity = 0;
obj->lightDistance = 0; obj->lightDistance = 0;
} }
return v7; return rc;
} }
// 0x48AD04 // 0x48AD04

View File

@ -206,11 +206,8 @@ int textObjectAdd(Object* object, char* string, int font, int color, int a5, Rec
char c = *ending; char c = *ending;
*ending = '\0'; *ending = '\0';
// NOTE: Calls [fontGetStringWidth] twice, probably result of using min/max macro // NOTE: Calls `fontGetStringWidth` twice.
int width = fontGetStringWidth(beginning); textObject->width = std::max(textObject->width, fontGetStringWidth(beginning));
if (width >= textObject->width) {
textObject->width = width;
}
*ending = c; *ending = c;
} }

View File

@ -4,6 +4,8 @@
#include <math.h> #include <math.h>
#include <string.h> #include <string.h>
#include <algorithm>
#include "art.h" #include "art.h"
#include "color.h" #include "color.h"
#include "config.h" #include "config.h"
@ -1660,13 +1662,8 @@ static void tileRenderFloor(int fid, int x, int y, Rect* rect)
int parity = tile & 1; int parity = tile & 1;
int ambientIntensity = lightGetLightLevel(); int ambientIntensity = lightGetLightLevel();
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
// NOTE: calling _light_get_tile two times, probably a result of using __min kind macro // NOTE: Calls `_light_get_tile` twice.
int tileIntensity = _light_get_tile(elev, tile + _verticies[i].offsets[parity]); _verticies[i].intensity = std::max(_light_get_tile(elev, tile + _verticies[i].offsets[parity]), ambientIntensity);
if (tileIntensity <= ambientIntensity) {
tileIntensity = ambientIntensity;
}
_verticies[i].intensity = tileIntensity;
} }
int v23 = 0; int v23 = 0;