Skip to content

Commit

Permalink
huge refactor around min/max and standardising as much as possible
Browse files Browse the repository at this point in the history
  • Loading branch information
atsb committed Dec 6, 2022
1 parent 9f7acf4 commit 5ecc53d
Show file tree
Hide file tree
Showing 20 changed files with 95 additions and 94 deletions.
2 changes: 1 addition & 1 deletion src/avp/bh_cable.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void PowerCableBehaveFun(STRATEGYBLOCK* sbptr)
max_health=NpcData->StartingStats.Health<<ONE_FIXED_SHIFT;
current_health=Player->ObStrategyBlock->SBDamageBlock.Health;

health_gained=min(pc_bhv->current_charge,max_health-current_health);
health_gained=min_no_const(pc_bhv->current_charge,max_health-current_health);

pc_bhv->current_charge-=health_gained;
Player->ObStrategyBlock->SBDamageBlock.Health+=health_gained;
Expand Down
2 changes: 1 addition & 1 deletion src/avp/bh_deathvol.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void DeathVolumeBehaveFun(STRATEGYBLOCK* vol_sbptr)
//now check the object's vertical extents for overlap with the death volume bounding box
miny=dptr->ObWorld.vy+dptr->ObMinY;
maxy=dptr->ObWorld.vy+dptr->ObMaxY;
if(max(miny,dv_bhv->volume_min.vy) > min(maxy,dv_bhv->volume_max.vy)) continue;
if(max_no_const(miny,dv_bhv->volume_min.vy) > min_no_const(maxy,dv_bhv->volume_max.vy)) continue;

/*
if(dynPtr->Position.vx > dv_bhv->volume_min.vx &&
Expand Down
8 changes: 4 additions & 4 deletions src/avp/bh_gener.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ void GeneratorBehaviour(STRATEGYBLOCK *sbPtr)
{
genBlock->RateIncreaseTimer=ONE_FIXED*60;
genBlock->GenerationRate+=genBlock->GenerationRateIncrease;
genBlock->GenerationRate=min(genBlock->GenerationRate,GENSPERMINUTE_MAX*100);
genBlock->GenerationRate=max(genBlock->GenerationRate,GENSPERMINUTE_MIN*100);
genBlock->GenerationRate=min_no_const(genBlock->GenerationRate,GENSPERMINUTE_MAX*100);
genBlock->GenerationRate=max_no_const(genBlock->GenerationRate,GENSPERMINUTE_MIN*100);
}
}

Expand Down Expand Up @@ -1171,11 +1171,11 @@ static int GeneratorBalance_LocalLimit(int normal_limit)
}
else
{
int shift = min(GeneratorBalance.MaxAIShift,4);
int shift = min_no_const(GeneratorBalance.MaxAIShift,4);
int limit = GeneratorBalance.MaxOwnSettingNpc + shift;
int alien_shortfall = limit - NumGeneratorNPCsInEnv();

return(normal_limit + min(alien_shortfall,shift));
return(normal_limit + min_no_const(alien_shortfall,shift));

}
}
16 changes: 8 additions & 8 deletions src/avp/bh_queen.c
Original file line number Diff line number Diff line change
Expand Up @@ -3461,7 +3461,7 @@ void QueenBehaviour(STRATEGYBLOCK *sbPtr)
else if(queenStatusPointer->current_move==QM_Climbing)
{
//need to push he player out of the way
Player->ObStrategyBlock->DynPtr->LinImpulse.vx=min(Player->ObStrategyBlock->DynPtr->LinImpulse.vx,-3000);
Player->ObStrategyBlock->DynPtr->LinImpulse.vx=min_no_const(Player->ObStrategyBlock->DynPtr->LinImpulse.vx,-3000);
}
else if(queenStatusPointer->QueenState!=QBS_Engagement &&
queenStatusPointer->QueenState!=QBS_CarryingObject)
Expand Down Expand Up @@ -4636,7 +4636,7 @@ void HandleHangarAirlock()
*/

int multiplier;
AirlockTimeOpen=max(AirlockTimeOpen,30*ONE_FIXED);
AirlockTimeOpen=max_no_const(AirlockTimeOpen,30*ONE_FIXED);
multiplier=MUL_FIXED(NormalFrameTime,(AirlockTimeOpen-10*ONE_FIXED)/20);

CauseDamageToObject(sbPtr,&VacuumDamage,multiplier,NULL);
Expand Down Expand Up @@ -4689,15 +4689,15 @@ void HandleHangarAirlock()
if(impulse.vx>cur_impulse->vx)
{
cur_impulse->vx+=MUL_FIXED(impulse.vx,NormalFrameTime);
cur_impulse->vx=min(cur_impulse->vx,impulse.vx);
cur_impulse->vx=min_no_const(cur_impulse->vx,impulse.vx);
}
}
else
{
if(impulse.vx<cur_impulse->vx)
{
cur_impulse->vx+=MUL_FIXED(impulse.vx,NormalFrameTime);
cur_impulse->vx=max(cur_impulse->vx,impulse.vx);
cur_impulse->vx=max_no_const(cur_impulse->vx,impulse.vx);
}
}

Expand All @@ -4706,15 +4706,15 @@ void HandleHangarAirlock()
if(impulse.vy>cur_impulse->vy)
{
cur_impulse->vy+=MUL_FIXED(impulse.vy,NormalFrameTime);
cur_impulse->vy=min(cur_impulse->vy,impulse.vy);
cur_impulse->vy=min_no_const(cur_impulse->vy,impulse.vy);
}
}
else
{
if(impulse.vy<cur_impulse->vy)
{
cur_impulse->vy+=MUL_FIXED(impulse.vy,NormalFrameTime);
cur_impulse->vy=max(cur_impulse->vy,impulse.vy);
cur_impulse->vy=max_no_const(cur_impulse->vy,impulse.vy);
}
}

Expand All @@ -4724,15 +4724,15 @@ void HandleHangarAirlock()
if(impulse.vz>cur_impulse->vz)
{
cur_impulse->vz+=MUL_FIXED(impulse.vz,NormalFrameTime);
cur_impulse->vz=min(cur_impulse->vz,impulse.vz);
cur_impulse->vz=min_no_const(cur_impulse->vz,impulse.vz);
}
}
else
{
if(impulse.vz<cur_impulse->vz)
{
cur_impulse->vz+=MUL_FIXED(impulse.vz,NormalFrameTime);
cur_impulse->vz=max(cur_impulse->vz,impulse.vz);
cur_impulse->vz=max_no_const(cur_impulse->vz,impulse.vz);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/avp/hud.c
Original file line number Diff line number Diff line change
Expand Up @@ -943,12 +943,12 @@ static void DisplayHealthAndArmour(void)
{
//make sure health isn't displayed as 100 , if it is even slightly below.
//(ie round down 99.5 , even though health is rounded up normally)
health = min(health,99);
health = min_no_const(health,99);
}
if(PlayerStatusPtr->Armour<(NpcData->StartingStats.Armour<<16))
{
//similarly for armour
armour = min(armour,99);
armour = min_no_const(armour,99);
}

Render_HealthAndArmour(health,armour);
Expand Down
8 changes: 5 additions & 3 deletions src/avp/win95/avpchunk.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "avpchunk.hpp"
#include "md5.h"
#include <algorithm>

//#include "strachnk.hpp"
//#include "obchunk.hpp"

Expand Down Expand Up @@ -280,7 +282,7 @@ AVP_Generator_Extended_Settings_Chunk::AVP_Generator_Extended_Settings_Chunk(Chu
CHUNK_EXTRACT(spare2,int)


size_t size=max(*(int*) data,(int)sizeof(AVP_Generator_Weighting));
size_t size=std::max(*(int*) data,(int)sizeof(AVP_Generator_Weighting));

weights=(AVP_Generator_Weighting*)new unsigned char[size];
memset(weights,0,sizeof(AVP_Generator_Weighting));
Expand Down Expand Up @@ -551,7 +553,7 @@ RIF_IMPLEMENT_DYNCREATE("AVPENVIR",AVP_Environment_Settings_Chunk)
AVP_Environment_Settings_Chunk::AVP_Environment_Settings_Chunk(Chunk_With_Children* parent,const char* data,size_t data_size)
:Chunk(parent,"AVPENVIR")
{
size_t size=max(data_size,sizeof(AVP_Environment_Settings));
size_t size=std::max(data_size,sizeof(AVP_Environment_Settings));

settings=(AVP_Environment_Settings*)new unsigned char[size];
memcpy(settings,data,data_size);
Expand Down Expand Up @@ -642,7 +644,7 @@ AVP_Decal_Chunk::AVP_Decal_Chunk(Chunk_With_Children* parent,const char* data,si
int loaded_decal_size=*(int*)data;
data+=4;

decal_size=max(loaded_decal_size,(int)sizeof(AVP_Decal));
decal_size=std::max(loaded_decal_size,(int)sizeof(AVP_Decal));

//allocate buffer for decals , and initialise to zero
decal_buffer=new char[num_decals*decal_size];
Expand Down
17 changes: 9 additions & 8 deletions src/avp/win95/objsetup.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#define DB_LEVEL 1

#include <stdlib.h>
#include <algorithm>

#include "list_tem.hpp"
#include "chnkload.hpp"
Expand Down Expand Up @@ -956,7 +957,7 @@ void add_placed_hierarchy(Placed_Hierarchy_Chunk* phc,const char* fname,const ch
for(; !chlif.done(); chlif.next())
{
Indexed_Sound_Chunk* isc=(Indexed_Sound_Chunk*)chlif();
phtt->num_sounds=max(phtt->num_sounds,isc->index+1);
phtt->num_sounds=std::max(phtt->num_sounds,isc->index+1);
}

if(phtt->num_sounds)
Expand Down Expand Up @@ -990,7 +991,7 @@ void add_placed_hierarchy(Placed_Hierarchy_Chunk* phc,const char* fname,const ch
for(chlif.restart();!chlif.done();chlif.next())
{
Placed_Hierarchy_Sequence_Chunk* phsc=(Placed_Hierarchy_Sequence_Chunk*)chlif();
phtt->num_sequences=max(phtt->num_sequences,phsc->index+1);
phtt->num_sequences=std::max(phtt->num_sequences,phsc->index+1);
}

GLOBALASSERT(phtt->num_sequences);
Expand Down Expand Up @@ -1777,11 +1778,11 @@ static void add_placed_light(Object_Chunk* ob,int list_pos,AVP_Strategy_Chunk* a
pltd->colour_diff_green-=pltd->colour_green;
pltd->colour_diff_blue-=pltd->colour_blue;

pltd->fade_up_time=(max(lchunk->light.fade_up_time,1)*ONE_FIXED)/1000;
pltd->fade_down_time=(max(lchunk->light.fade_down_time,1)*ONE_FIXED)/1000;
pltd->up_time=(max(lchunk->light.up_time,1)*ONE_FIXED)/1000;
pltd->down_time=(max(lchunk->light.down_time,1)*ONE_FIXED)/1000;
pltd->timer=(max(lchunk->light.start_time,1)*ONE_FIXED)/1000;
pltd->fade_up_time=(std::max(lchunk->light.fade_up_time,1)*ONE_FIXED)/1000;
pltd->fade_down_time=(std::max(lchunk->light.fade_down_time,1)*ONE_FIXED)/1000;
pltd->up_time=(std::max(lchunk->light.up_time,1)*ONE_FIXED)/1000;
pltd->down_time=(std::max(lchunk->light.down_time,1)*ONE_FIXED)/1000;
pltd->timer=(std::max(lchunk->light.start_time,1)*ONE_FIXED)/1000;

pltd->type=(LIGHT_TYPE)lchunk->light.light_type;
pltd->on_off_type=(LIGHT_ON_OFF_TYPE)lchunk->light.on_off_type;
Expand Down Expand Up @@ -3808,7 +3809,7 @@ void setup_particle_generators(Environment_Data_Chunk * envd)
if(data_chunk->type==PARGEN_TYPE_SPARK)
part_temp->frequency=(data_chunk->time*ONE_FIXED)/10;
else
part_temp->frequency=ONE_FIXED/max(data_chunk->quantity,1);
part_temp->frequency=ONE_FIXED/max_no_const(data_chunk->quantity,1);

part_temp->active=!(data_chunk->flags & ParticleGeneratorFlag_Inactive);

Expand Down
24 changes: 12 additions & 12 deletions src/avp/win95/pldnet.c
Original file line number Diff line number Diff line change
Expand Up @@ -1609,7 +1609,7 @@ void NetSendMessages(void)
return;
}
netGameData.sendTimer+=netGameData.sendFrequency;
netGameData.sendTimer=max(0,netGameData.sendTimer);
netGameData.sendTimer=max_no_const(0,netGameData.sendTimer);

}

Expand Down Expand Up @@ -2015,9 +2015,9 @@ void AddNetMsg_GameDescription(void)

messagePtr->useSharedLives=netGameData.useSharedLives;
messagePtr->maxLives=netGameData.maxLives;
messagePtr->numDeaths[0]=(unsigned char) min(netGameData.numDeaths[0],255);
messagePtr->numDeaths[1]=(unsigned char) min(netGameData.numDeaths[1],255);
messagePtr->numDeaths[2]=(unsigned char) min(netGameData.numDeaths[2],255);
messagePtr->numDeaths[0]=(unsigned char) min_no_const(netGameData.numDeaths[0],255);
messagePtr->numDeaths[1]=(unsigned char) min_no_const(netGameData.numDeaths[1],255);
messagePtr->numDeaths[2]=(unsigned char) min_no_const(netGameData.numDeaths[2],255);

messagePtr->timeForRespawn=(unsigned char)netGameData.timeForRespawn;
messagePtr->pointsForRespawn=netGameData.pointsForRespawn;
Expand Down Expand Up @@ -9822,8 +9822,8 @@ static int GetDynamicScoreMultiplier(int playerKilledIndex,int killerIndex)
scoreFor=netGameData.playerData[playerKilledIndex].playerScore;
scoreAgainst=netGameData.playerData[playerKilledIndex].playerScoreAgainst;

scoreFor=max(500,scoreFor+500);
scoreAgainst=max(500,scoreAgainst+500);
scoreFor=max_no_const(500,scoreFor+500);
scoreAgainst=max_no_const(500,scoreAgainst+500);

//count players
for(i=0;i<NET_MAXPLAYERS;i++)
Expand Down Expand Up @@ -9873,11 +9873,11 @@ static int GetDynamicScoreMultiplier(int playerKilledIndex,int killerIndex)

playerCount++;
//give everyone a minimum score of 500 for the purpose of this calculation
scoreTotal+=max(500,netGameData.playerData[i].playerScore+500);
scoreTotal+=max_no_const(500,netGameData.playerData[i].playerScore+500);
}
if(playerCount<3 || !scoreTotal) return ONE_FIXED;

playerKilledScore=max(500,netGameData.playerData[playerKilledIndex].playerScore+500);
playerKilledScore=max_no_const(500,netGameData.playerData[playerKilledIndex].playerScore+500);

//get average score of all players other than killed player
playerCount--;
Expand Down Expand Up @@ -10533,11 +10533,11 @@ int DetermineAvailableCharacterTypes(BOOL ConsiderUsedCharacters)
//make sure all the limits are at least 0
for(i=0;i<NUM_PC_TYPES;i++)
{
CharacterTypesAvailable[i]=max(0,CharacterTypesAvailable[i]);
CharacterTypesAvailable[i]=max_no_const(0,CharacterTypesAvailable[i]);
}
for(i=0;i<NUM_PC_SUBTYPES;i++)
{
CharacterSubTypesAvailable[i]=max(0,CharacterSubTypesAvailable[i]);
CharacterSubTypesAvailable[i]=max_no_const(0,CharacterSubTypesAvailable[i]);
}


Expand All @@ -10547,10 +10547,10 @@ int DetermineAvailableCharacterTypes(BOOL ConsiderUsedCharacters)
maxMarines=0;
for(i=0;i<NUM_PC_SUBTYPES;i++)
{
CharacterSubTypesAvailable[i]=min(CharacterSubTypesAvailable[i],CharacterTypesAvailable[NGCT_Marine]);
CharacterSubTypesAvailable[i]=min_no_const(CharacterSubTypesAvailable[i],CharacterTypesAvailable[NGCT_Marine]);
maxMarines+=CharacterSubTypesAvailable[i];
}
CharacterTypesAvailable[NGCT_Marine]=min(CharacterTypesAvailable[NGCT_Marine],maxMarines);
CharacterTypesAvailable[NGCT_Marine]=min_no_const(CharacterTypesAvailable[NGCT_Marine],maxMarines);

//return the total number of players available
return CharacterTypesAvailable[NGCT_Marine]+
Expand Down
19 changes: 10 additions & 9 deletions src/avp/win95/projload.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#define DB_LEVEL 2

#include <algorithm>

#include "3dc.h"
#include "inline.h"
#include "module.h"
Expand Down Expand Up @@ -167,7 +169,7 @@ void setup_paths(RIFFHANDLE h)
for(; !plif.done(); plif.next())
{
AVP_Path_Chunk* apc=(AVP_Path_Chunk*) plif();
PathArraySize=max(PathArraySize,apc->PathID+1);
PathArraySize=std::max(PathArraySize,apc->PathID+1);
}

PathArray=(PATHHEADER*)PoolAllocateMem(sizeof(PATHHEADER)*PathArraySize);
Expand All @@ -187,7 +189,7 @@ void setup_paths(RIFFHANDLE h)
int length=apc->PathLength;
if(apc->flags & PathFlag_BackAndForth)
{
length=max(length,(length-1)*2);
length=std::max(length,(length-1)*2);
}

path->modules_in_path=(AIMODULE**)PoolAllocateMem(sizeof(AIMODULE*)*length);
Expand Down Expand Up @@ -463,7 +465,7 @@ Global_Hierarchy_Store::Global_Hierarchy_Store (RIFFHANDLE h)
for(; !chlif.done(); chlif.next())
{
Indexed_Sound_Chunk* isc=(Indexed_Sound_Chunk*)chlif();
max_index=max(max_index,isc->index);
max_index=std::max(max_index,isc->index);
}
//now create a large enough array , and fill it in
num_sounds=max_index+1;
Expand Down Expand Up @@ -716,7 +718,7 @@ void Global_Hierarchy_Store::setup_alternate_shape_sets(List <Object_ShapeNum_Pa
for(chlif.restart();!chlif.done();chlif.next())
{
Hierarchy_Shape_Set_Collection_Chunk* coll=(Hierarchy_Shape_Set_Collection_Chunk*)chlif();
max_index=max(max_index,coll->Set_Collection_Num);
max_index=std::max(max_index,coll->Set_Collection_Num);
}

if(max_index==-1) return;
Expand Down Expand Up @@ -846,7 +848,6 @@ void Global_Hierarchy_Store::delete_section(SECTION * s2d)
#endif
}


SECTION * Global_Hierarchy_Store::build_hierarchy (Object_Hierarchy_Chunk * ohc,char* hierarchy_name)
{
// iterative bit
Expand Down Expand Up @@ -988,11 +989,11 @@ SECTION * Global_Hierarchy_Store::build_hierarchy (Object_Hierarchy_Chunk * ohc,
if (frame_no<seq->num_frames)
{
//calculate sequence length , making sure it doesn't overflow an unsigned short
kfd->Sequence_Length =(unsigned short) min(seq->frames[frame_no].at_frame_no - this_frame_no,65535);
kfd->Sequence_Length =(unsigned short) std::min(seq->frames[frame_no].at_frame_no - this_frame_no,65535);
}
else
{
kfd->Sequence_Length =(unsigned short) min(65536 - this_frame_no,65535);
kfd->Sequence_Length =(unsigned short) std::min(65536 - this_frame_no,65535);
}

}
Expand Down Expand Up @@ -1196,11 +1197,11 @@ SECTION * Global_Hierarchy_Store::build_hierarchy (Object_Hierarchy_Chunk * ohc,
if (frame_no<num_frames)
{
//calculate sequence length , making sure it doesn't overflow an unsigned short
kfd->Sequence_Length =(unsigned short) min(frame_array[frame_no]->at_frame_no - this_frame_no,65535);
kfd->Sequence_Length =(unsigned short) min_no_const(frame_array[frame_no]->at_frame_no - this_frame_no,65535);
}
else
{
kfd->Sequence_Length =(unsigned short) min(65536 - this_frame_no , 65535);
kfd->Sequence_Length =(unsigned short) std::min(65536 - this_frame_no , 65535);
}
}
//sort out some settings for the last frame
Expand Down
2 changes: 1 addition & 1 deletion src/files.c
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ void InitGameDirectories(char *argv0)
while (*path) {
len = strcspn(path, ":");

copylen = min(len, PATH_MAX-1);
copylen = min_no_const(len, PATH_MAX-1);

strncpy(tmppath, path, copylen);
tmppath[copylen] = 0;
Expand Down
Loading

0 comments on commit 5ecc53d

Please sign in to comment.