Skip to content

Commit

Permalink
Rename min/max macros to D3_min/D3_max
Browse files Browse the repository at this point in the history
These only cause trouble in stdlib headers, such as:

```
/usr/include/c++/12/limits:1828:13: error: expected unqualified-id before ‘noexcept’
 1828 |       max() _GLIBCXX_USE_NOEXCEPT { return __LDBL_MAX__; }
      |             ^~~~~~~~~~~~~~~~~~~~~
Descent3/d3music/musicapi.cpp: In function ‘void D3MusicSetVolume(float)’:
Descent3/d3music/musicapi.cpp:167:57: error: ‘std::numeric_limits<float>::min’ cannot be used as a function
  167 |   const float kEpsilon = std::numeric_limits<float>::min();
      |                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
```

plus there is the whole NOMINMAX situation on Windows.

And don't introduce <algorithm> just for that.


Automated via:

$ sed -i 's/_\?_\?min *(\(.*\),/D3_min(\1,/' `git grep -l min`
$ sed -i 's/_\?_\?max *(\(.*\),/D3_max(\1,/' `git grep -l max`

plus remove duplicates from lib/Macros.h + lib/linux/linux_fix.h
and fix netgames/dmfc/dmfcstats.cpp (2x min on one line) manually.
  • Loading branch information
th1000s committed Apr 16, 2024
1 parent 60d75ac commit 7377c2c
Show file tree
Hide file tree
Showing 60 changed files with 302 additions and 313 deletions.
4 changes: 2 additions & 2 deletions Descent3/Game2DLL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -820,14 +820,14 @@ bool GetDLLNumTeamInfo(char *name, int *mint, int *maxt) {
*maxt = 1;
return false;
}
*maxt = (dllo.max_teams == 0 || dllo.max_teams == 1 || dllo.max_teams < 0) ? 1 : min(dllo.max_teams, 4);
*maxt = (dllo.max_teams == 0 || dllo.max_teams == 1 || dllo.max_teams < 0) ? 1 : D3_min(dllo.max_teams, 4);
if ((*maxt) == 1) {
*mint = 1;
} else {
*mint = 2;
}
if (dllo.flags & DOF_MINTEAMS && dllo.min_teams >= 0) {
*mint = (dllo.min_teams == 0 || dllo.min_teams == 1) ? 1 : min(*maxt, dllo.min_teams);
*mint = (dllo.min_teams == 0 || dllo.min_teams == 1) ? 1 : D3_min(*maxt, dllo.min_teams);
}
return ((*maxt) == 1) ? false : true;
}
2 changes: 1 addition & 1 deletion Descent3/LoadLevel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3894,7 +3894,7 @@ int LoadLevel(char *filename, void (*cb_fn)(const char *, int, int)) {
ubyte buf[1000];
int n = chunk_size, r;
while (n) {
r = cf_ReadBytes(buf, min(n, sizeof(buf)), ifile);
r = cf_ReadBytes(buf, D3_min(n, sizeof(buf)), ifile);
cf_WriteBytes(buf, r, ofile);
n -= r;
}
Expand Down
2 changes: 1 addition & 1 deletion Descent3/PilotPicsAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ bool PPic_GetPilot(ushort pilot_id, char *pilot_name, int buffersize) {
cfseek(PilotPic_database_index_handle, oldoffset, SEEK_SET);
return false;
}
int toread = min(name_size, buffersize - 1);
int toread = D3_min(name_size, buffersize - 1);
cf_ReadBytes((ubyte *)pilot_name, toread, PilotPic_database_index_handle);
pilot_name[toread] = '\0';

Expand Down
14 changes: 7 additions & 7 deletions Descent3/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1449,7 +1449,7 @@ void InitPlayerNewLevel(int slot) {
ship *ship = &Ships[player->ship_index];
for (int i = 0; i < MAX_PLAYER_WEAPONS; i++) {
if ((i >= SECONDARY_INDEX) || ship->static_wb[i].ammo_usage)
player->weapon_ammo[i] = min(ship->max_ammo[i], player->weapon_ammo[i]);
player->weapon_ammo[i] = D3_min(ship->max_ammo[i], player->weapon_ammo[i]);
}

#ifdef E3_DEMO
Expand Down Expand Up @@ -2933,7 +2933,7 @@ void PlayerSpewInventory(object *obj, bool spew_energy_and_shield, bool spew_non
id = Ships[playp->ship_index].spew_powerup[playp->weapon[PW_SECONDARY].index];
if (id != -1) {
ASSERT(Object_info[id].type == OBJ_POWERUP);
int count = __min(playp->weapon_ammo[playp->weapon[PW_SECONDARY].index], MAX_SECONDARY_SPEW);
int count = D3_min(playp->weapon_ammo[playp->weapon[PW_SECONDARY].index], MAX_SECONDARY_SPEW);
for (int t = 0; t < count; t++)
PlayerSpewObject(obj, OBJ_POWERUP, id, 2, NULL);
}
Expand All @@ -2954,7 +2954,7 @@ void PlayerSpewInventory(object *obj, bool spew_energy_and_shield, bool spew_non
object *objp = &Objects[objnum];
ASSERT(objp->control_type == CT_POWERUP);
if (Game_mode & GM_MULTI)
objp->ctype.powerup_info.count = max(objp->ctype.powerup_info.count / 4, playp->weapon_ammo[w]);
objp->ctype.powerup_info.count = D3_max(objp->ctype.powerup_info.count / 4, playp->weapon_ammo[w]);
else
objp->ctype.powerup_info.count = playp->weapon_ammo[w];
}
Expand All @@ -2978,7 +2978,7 @@ void PlayerSpewInventory(object *obj, bool spew_energy_and_shield, bool spew_non
if (objnum != -1)
Objects[objnum].ctype.powerup_info.count = playp->weapon_ammo[w];
} else {
int count = __min(playp->weapon_ammo[w], MAX_SECONDARY_SPEW);
int count = D3_min(playp->weapon_ammo[w], MAX_SECONDARY_SPEW);
for (int t = 0; t < count; t++)
PlayerSpewObject(obj, OBJ_POWERUP, id, 0, NULL);
}
Expand Down Expand Up @@ -3055,7 +3055,7 @@ void PlayerSpewInventory(object *obj, bool spew_energy_and_shield, bool spew_non
for (i = 0; i < count && !done; i++) {
if (count < 2 || (ps_rand() % 2)) {
int limit = playp->counter_measures.GetPosCount();
limit = min(2, limit);
limit = D3_min(2, limit);
for (int t = 0; t < limit; t++) {
playp->counter_measures.GetAuxPosTypeID(type, id);
playp->counter_measures.GetPosInfo(inven_flags, object_flags);
Expand Down Expand Up @@ -3724,8 +3724,8 @@ void DoEnergyToShields(int pnum) {
return;
}

amount = min(Frametime * CONVERTER_RATE, Players[pnum].energy - INITIAL_ENERGY);
amount = min(amount, (MAX_SHIELDS - Objects[Players[pnum].objnum].shields) * CONVERTER_SCALE);
amount = D3_min(Frametime * CONVERTER_RATE, Players[pnum].energy - INITIAL_ENERGY);
amount = D3_min(amount, (MAX_SHIELDS - Objects[Players[pnum].objnum].shields) * CONVERTER_SCALE);

Players[pnum].energy -= amount;

Expand Down
6 changes: 3 additions & 3 deletions Descent3/TelComCargo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
#define LID_INVENTORY 5

#ifdef __LINUX__
#define max(a, b) ((a > b) ? a : b)
#define D3_max(a, b) ((a > b) ? a : b)
#endif

typedef struct {
Expand Down Expand Up @@ -142,7 +142,7 @@ int TCCargoCreateLine(int id, int y, char *title, int type) {
grtext_SetFont(SM_FONT);

float shields = Player_object->shields;
shields = max(shields, 0);
shields = D3_max(shields, 0);
int perc = (int)((shields / INITIAL_SHIELDS) * 100.0f);
grtext_Printf(TITLE_X, y, title);
grtext_Printf(VALUE_X, y, "%d%c", perc, '%');
Expand All @@ -154,7 +154,7 @@ int TCCargoCreateLine(int id, int y, char *title, int type) {
grtext_SetFont(SM_FONT);

float energy = Players[Player_num].energy;
energy = max(energy, 0);
energy = D3_max(energy, 0);
int perc = (int)((energy / INITIAL_ENERGY) * 100.0f);
grtext_Printf(TITLE_X, y, title);
grtext_Printf(VALUE_X, y, "%d%c", perc, '%');
Expand Down
12 changes: 6 additions & 6 deletions Descent3/TelComGoals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,13 @@ void TCGoalsRenderCallback(void) {
float alpha_amount;
while (alpha_change > 0) {
if (active_alpha_in) {
alpha_amount = min(ACTIVE_ALPHA_MAX - active_alpha, alpha_change);
alpha_amount = D3_min(ACTIVE_ALPHA_MAX - active_alpha, alpha_change);
active_alpha += alpha_amount;

if (active_alpha > (ACTIVE_ALPHA_MAX - 1.0f))
active_alpha_in = false;
} else {
alpha_amount = min(alpha_change, active_alpha - ACTIVE_ALPHA_MIN);
alpha_amount = D3_min(alpha_change, active_alpha - ACTIVE_ALPHA_MIN);
active_alpha -= alpha_amount;

if (active_alpha < (ACTIVE_ALPHA_MIN + 1.0f))
Expand Down Expand Up @@ -570,18 +570,18 @@ void TCGoalsRenderCallback(void) {
// adjust box
if (TG_TT_curr.w > TG_TT_dest.w) {
// shrinking
TG_TT_curr.w -= min(TG_TT_curr.w - TG_TT_dest.w, (int)(TOOLTIP_SPEED * last_frametime));
TG_TT_curr.w -= D3_min(TG_TT_curr.w - TG_TT_dest.w, (int)(TOOLTIP_SPEED * last_frametime));
} else {
// enlarging
TG_TT_curr.w += min(TG_TT_dest.w - TG_TT_curr.w, (int)(TOOLTIP_SPEED * last_frametime));
TG_TT_curr.w += D3_min(TG_TT_dest.w - TG_TT_curr.w, (int)(TOOLTIP_SPEED * last_frametime));
}

if (TG_TT_curr.h > TG_TT_dest.h) {
// shrinking
TG_TT_curr.h -= min(TG_TT_curr.h - TG_TT_dest.h, (int)(TOOLTIP_SPEED * last_frametime));
TG_TT_curr.h -= D3_min(TG_TT_curr.h - TG_TT_dest.h, (int)(TOOLTIP_SPEED * last_frametime));
} else {
// enlarging
TG_TT_curr.h += min(TG_TT_dest.h - TG_TT_curr.h, (int)(TOOLTIP_SPEED * last_frametime));
TG_TT_curr.h += D3_min(TG_TT_dest.h - TG_TT_curr.h, (int)(TOOLTIP_SPEED * last_frametime));
}

// rend_SetAlphaType (AT_CONSTANT_TEXTURE);
Expand Down
4 changes: 2 additions & 2 deletions Descent3/WeaponFire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1664,7 +1664,7 @@ void WeaponDoFrame(object *obj) {
float extras = (mag * 4) + .5;
int int_extras = extras + 1;

int_extras = min(int_extras, 2);
int_extras = D3_min(int_extras, 2);

delta_vec /= int_extras;
delta_time /= int_extras;
Expand Down Expand Up @@ -2340,7 +2340,7 @@ void DoSprayEffect(object *obj, otype_wb_info *static_wb, ubyte wb_index) {

int extras = fextras;

extras = min(extras, 8);
extras = D3_min(extras, 8);

for (int t = 0; t < extras; t++) {

Expand Down
8 changes: 4 additions & 4 deletions Descent3/cockpit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@
#include "soundload.h"
#include "sounds.h"
#ifdef __LINUX__
#define min(a, b) ((a < b) ? a : b) // make Linux happy about min()
#define D3_min(a, b) ((a < b) ? a : b) // make Linux happy about min()
#elif defined(MACINTOSH)
#include "Macros.h"
#endif
Expand Down Expand Up @@ -480,9 +480,9 @@ void RenderCockpit() {
light_scalar_b *= .8f;

if (player_obj->effect_info) {
light_scalar_r = min(1.0, light_scalar_r + (player_obj->effect_info->dynamic_red));
light_scalar_g = min(1.0, light_scalar_g + (player_obj->effect_info->dynamic_green));
light_scalar_b = min(1.0, light_scalar_b + (player_obj->effect_info->dynamic_blue));
light_scalar_r = D3_min(1.0, light_scalar_r + (player_obj->effect_info->dynamic_red));
light_scalar_g = D3_min(1.0, light_scalar_g + (player_obj->effect_info->dynamic_green));
light_scalar_b = D3_min(1.0, light_scalar_b + (player_obj->effect_info->dynamic_blue));
}
if (Players[player_obj->id].flags & PLAYER_FLAGS_HEADLIGHT) {
light_scalar_r = 1.0;
Expand Down
6 changes: 3 additions & 3 deletions Descent3/damage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ void SetNapalmDamageEffect(object *obj, object *killer, int weapon_id) {
else
obj->effect_info->damage_time += (MAX_NAPALM_DAMAGE_TIME / 1.5f);

obj->effect_info->damage_time = min(MAX_NAPALM_DAMAGE_TIME, obj->effect_info->damage_time);
obj->effect_info->damage_time = D3_min(MAX_NAPALM_DAMAGE_TIME, obj->effect_info->damage_time);

if (obj->type == OBJ_PLAYER)
obj->effect_info->damage_per_second = Weapons[weapon_id].player_damage;
Expand Down Expand Up @@ -983,7 +983,7 @@ void GenerateDefaultDeath(object *obj, int *death_flags, float *delay_time) {
float extra_time = obj->rtype.pobj_info.anim_time *
(obj->rtype.pobj_info.anim_end_frame - obj->rtype.pobj_info.anim_frame) /
(obj->rtype.pobj_info.anim_end_frame - obj->rtype.pobj_info.anim_start_frame);
extra_time = min(extra_time, 3.0); // limit extra time to 3 seconds
extra_time = D3_min(extra_time, 3.0); // limit extra time to 3 seconds
*delay_time = Object_info[obj->id].anim[obj->ai_info->movement_type].elem[AS_DEATH].spc + 0.25 + extra_time;
// Walkers last a little longer
if (obj->movement_type == MT_WALKING)
Expand Down Expand Up @@ -1134,7 +1134,7 @@ float GetDeathAnimTime(object *objp) {
float extra_time = objp->rtype.pobj_info.anim_time *
(objp->rtype.pobj_info.anim_end_frame - objp->rtype.pobj_info.anim_frame) /
(objp->rtype.pobj_info.anim_end_frame - objp->rtype.pobj_info.anim_start_frame);
extra_time = min(extra_time, 3.0); // limit extra time to 3 seconds
extra_time = D3_min(extra_time, 3.0); // limit extra time to 3 seconds
mprintf((0, "extra_time = %2f\n", extra_time));

death_time = Object_info[objp->id].anim[objp->ai_info->movement_type].elem[AS_DEATH].spc + 0.25 + extra_time;
Expand Down
12 changes: 6 additions & 6 deletions Descent3/fireball.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@
#include <string.h>
#include "psrand.h"
#ifdef __LINUX__
#define min(a, b) ((a < b) ? a : b)
#define D3_min(a, b) ((a < b) ? a : b)
#elif defined(MACINTOSH)
#include "Macros.h"
#endif
Expand Down Expand Up @@ -748,7 +748,7 @@ void DrawFireballObject(object *obj) {
if (norm_time < .5) // Ramp up quickly
{
float temp_norm = norm_time / .2;
temp_norm = min(1.0, temp_norm);
temp_norm = D3_min(1.0, temp_norm);
size *= temp_norm;
} else // ramp down slowly
{
Expand Down Expand Up @@ -958,7 +958,7 @@ void CreateExtraFireballs(object *obj, float size_scale) {
if (obj->type == OBJ_BUILDING && OBJECT_OUTSIDE(obj))
extras += 4;
// Cap at 12
extras = min(12, extras);
extras = D3_min(12, extras);
// mprintf ((0,"Creating %d extra explosions\n",extras));
CreateRandomSparks(extras, &obj->pos, obj->roomnum);
for (i = 0; i < extras; i++) {
Expand Down Expand Up @@ -1040,7 +1040,7 @@ void CreateSplintersFromBody(object *obj, float explosion_mag, float lifetime) {
num_splinters++;
}
} else {
int limit = min(sm->num_faces, MAX_SPLINTERS_PER_OBJECT);
int limit = D3_min(sm->num_faces, MAX_SPLINTERS_PER_OBJECT);
for (i = 0; i < limit; i++) {
facenums[i] = ps_rand() % sm->num_faces;
num_splinters++;
Expand All @@ -1050,7 +1050,7 @@ void CreateSplintersFromBody(object *obj, float explosion_mag, float lifetime) {
for (i = 0; i < num_splinters; i++) {
int facenum = facenums[i];
vector dest, center;
int num_verts = min(sm->faces[facenum].nverts, MAX_VERTS_PER_SPLINTER);
int num_verts = D3_min(sm->faces[facenum].nverts, MAX_VERTS_PER_SPLINTER);

vm_MakeZero(&dest);
vm_MakeZero(&center);
Expand Down Expand Up @@ -1429,7 +1429,7 @@ void DoDyingFrame(object *objp) {
// If sparking death, do sparks
if (death_flags & DF_DELAY_SPARKS) {
float dying_norm = objp->ctype.dying_info.delay_time / ELECTRICAL_DEATH_TIME;
dying_norm = min(dying_norm, 1.0);
dying_norm = D3_min(dying_norm, 1.0);
// Do deform stuff
objp->effect_info->type_flags |= EF_DEFORM;
objp->effect_info->deform_time = 1.0;
Expand Down
4 changes: 2 additions & 2 deletions Descent3/gamecinematics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ bool Cinematic_StartCine(tGameCinematic *info, char *text_string, int camera_obj
int cur_path = camera->ai_info->path.cur_path;
int cur_node = camera->ai_info->path.cur_node;
int end_node = camera->ai_info->path.path_end_node[cur_path];
int next_node = min(cur_node + 1, end_node);
int next_node = D3_min(cur_node + 1, end_node);
int path_num = camera->ai_info->path.path_id[cur_path];

if (cur_node != next_node) {
Expand Down Expand Up @@ -1109,7 +1109,7 @@ void Cinematic_Frame(void) {
int cur_path = camera->ai_info->path.cur_path;
int cur_node = camera->ai_info->path.cur_node;
int end_node = camera->ai_info->path.path_end_node[cur_path];
int next_node = min(cur_node + 1, end_node);
int next_node = D3_min(cur_node + 1, end_node);
int path_num = camera->ai_info->path.path_id[cur_path];

if (cur_node != next_node) {
Expand Down
4 changes: 2 additions & 2 deletions Descent3/huddisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@
#include <stdarg.h>

#ifdef __LINUX__
#define min(a, b) ((a < b) ? a : b) // why can't I find a min in Linux at all!?
#define D3_min(a, b) ((a < b) ? a : b) // why can't I find a min in Linux at all!?
#elif defined(MACINTOSH)
#include "Macros.h"
#endif
Expand Down Expand Up @@ -962,7 +962,7 @@ void RenderHUDScore(tHUDItem *item) {
int text_height = grfont_GetHeight(HUD_FONT);
sprintf(buf, "%d ", Score_added);
w = RenderHUDGetTextLineWidth(buf); // * win_w/Game_window_w;
ubyte alpha = min(HUD_ALPHA, HUD_ALPHA * 4 * Score_added_timer / SCORE_ADDED_TIME);
ubyte alpha = D3_min(HUD_ALPHA, HUD_ALPHA * 4 * Score_added_timer / SCORE_ADDED_TIME);
RenderHUDText(item->color, alpha, 0, item->x - w - win_w, item->y + text_height, buf);
Score_added_timer -= Frametime;
}
Expand Down
Loading

0 comments on commit 7377c2c

Please sign in to comment.