Clean up:

- Use std::min/max.
- remove useless test: delete null ptr is safe.
This commit is contained in:
joris 2013-05-27 14:02:36 +02:00
parent e64b56d0fb
commit 31543a7c99
18 changed files with 81 additions and 163 deletions

View file

@ -281,10 +281,7 @@ static void Finish(COrder_Built &order, CUnit &unit)
// This should happen when building unit with several peons
// Maybe also with only one.
// FIXME : Should be better to fix it in action_{build,repair}.c ?
if (unit.Variable[BUILD_INDEX].Value > unit.Variable[BUILD_INDEX].Max) {
// assume value is wrong.
unit.Variable[BUILD_INDEX].Value = unit.Variable[BUILD_INDEX].Max;
}
unit.Variable[BUILD_INDEX].Value = std::min(unit.Variable[BUILD_INDEX].Max, unit.Variable[BUILD_INDEX].Value);
}
/* virtual */ void COrder_Built::FillSeenValues(CUnit &unit) const

View file

@ -706,9 +706,7 @@ int COrder_Resource::GatherResource(CUnit &unit)
// Target is not dead, getting resources.
if (is_visible) {
// Don't load more that there is.
if (addload > source->ResourcesHeld) {
addload = source->ResourcesHeld;
}
addload = std::min(source->ResourcesHeld, addload);
unit.ResourcesHeld += addload;
source->ResourcesHeld -= addload;
}

View file

@ -504,10 +504,8 @@ void InitAiModule()
void CleanAi()
{
for (int p = 0; p < PlayerMax; ++p) {
if (Players[p].Ai) {
delete Players[p].Ai;
Players[p].Ai = NULL;
}
delete Players[p].Ai;
Players[p].Ai = NULL;
}
}

View file

@ -50,54 +50,51 @@
void MissileDeathCoil::Action()
{
this->Wait = this->Type->Sleep;
if (PointToPointMissile(*this)) {
Assert(this->SourceUnit != NULL);
CUnit &source = *this->SourceUnit;
if (PointToPointMissile(*this) == false) {
return;
}
Assert(this->SourceUnit != NULL);
CUnit &source = *this->SourceUnit;
if (source.Destroyed) {
if (source.Destroyed) {
return;
}
// source unit still exists
//
// Target unit still exists and casted on a special target
//
if (this->TargetUnit && !this->TargetUnit->Destroyed
&& this->TargetUnit->CurrentAction() == UnitActionDie) {
HitUnit(&source, *this->TargetUnit, this->Damage);
if (source.CurrentAction() != UnitActionDie) {
source.Variable[HP_INDEX].Value += this->Damage;
source.Variable[HP_INDEX].Value = std::min(source.Variable[HP_INDEX].Max, source.Variable[HP_INDEX].Value);
}
} else {
//
// No target unit -- try enemies in range 5x5 // Must be parametrable
//
std::vector<CUnit *> table;
const Vec2i destPos = Map.MapPixelPosToTilePos(this->destination);
const Vec2i range(2, 2);
Select(destPos - range, destPos + range, table, IsEnemyWith(*source.Player));
if (table.empty()) {
return;
}
// source unit still exists
//
// Target unit still exists and casted on a special target
//
if (this->TargetUnit && !this->TargetUnit->Destroyed
&& this->TargetUnit->CurrentAction() == UnitActionDie) {
HitUnit(&source, *this->TargetUnit, this->Damage);
if (source.CurrentAction() != UnitActionDie) {
source.Variable[HP_INDEX].Value += this->Damage;
if (source.Variable[HP_INDEX].Value > source.Variable[HP_INDEX].Max) {
source.Variable[HP_INDEX].Value = source.Variable[HP_INDEX].Max;
}
}
} else {
//
// No target unit -- try enemies in range 5x5 // Must be parametrable
//
std::vector<CUnit *> table;
const Vec2i destPos = Map.MapPixelPosToTilePos(this->destination);
const Vec2i range(2, 2);
Select(destPos - range, destPos + range, table, IsEnemyWith(*source.Player));
const size_t n = table.size(); // enemy count
const int damage = std::min<int>(1, this->Damage / n);
if (table.empty()) {
return;
}
const size_t n = table.size(); // enemy count
const int damage = std::min<int>(1, this->Damage / n);
// disperse damage between them
for (size_t i = 0; i != n; ++i) {
HitUnit(&source, *table[i], damage);
}
if (source.CurrentAction() != UnitActionDie) {
source.Variable[HP_INDEX].Value += this->Damage;
if (source.Variable[HP_INDEX].Value > source.Variable[HP_INDEX].Max) {
source.Variable[HP_INDEX].Value = source.Variable[HP_INDEX].Max;
}
}
// disperse damage between them
for (size_t i = 0; i != n; ++i) {
HitUnit(&source, *table[i], damage);
}
if (source.CurrentAction() != UnitActionDie) {
source.Variable[HP_INDEX].Value += this->Damage;
source.Variable[HP_INDEX].Value = std::min(source.Variable[HP_INDEX].Max, source.Variable[HP_INDEX].Value);
}
this->TTL = 0;
}
this->TTL = 0;
}
//@}

View file

@ -689,9 +689,7 @@ void SocketSet::AddSocket(Socket socket)
{
Sockets.push_back(socket);
SocketReady.push_back(0);
if (socket > MaxSockFD) {
MaxSockFD = socket;
}
MaxSockFD = std::max(MaxSockFD, socket);
}
/**
@ -714,9 +712,7 @@ void SocketSet::DelSocket(Socket socket)
if (socket == MaxSockFD) {
MaxSockFD = 0;
for (i = Sockets.begin(); i != Sockets.end(); ++i) {
if (*i > MaxSockFD) {
MaxSockFD = *i;
}
MaxSockFD = std::max(this->MaxSockFD, *i);
}
}
}

View file

@ -206,9 +206,7 @@ static int MixSampleToStereo32(CSample *sample, int index, unsigned char volume,
Assert(!(index & 1));
if (size >= (sample->Len - index) * div / 2) {
size = (sample->Len - index) * div / 2;
}
size = std::min((sample->Len - index) * div / 2, size);
size = ConvertToStereo32((char *)(sample->Buffer + index), (char *)buf, sample->Frequency,
sample->SampleSize / 8, sample->Channels,
@ -410,9 +408,7 @@ int SetChannelVolume(int channel, int volume)
} else {
SDL_LockAudio();
if (volume > MaxVolume) {
volume = MaxVolume;
}
volume = std::min(MaxVolume, volume);
Channels[channel].Volume = volume;
SDL_UnlockAudio();

View file

@ -189,9 +189,7 @@ int CSampleWavStream::Read(void *buf, int len)
this->Len += comp;
}
if (this->Len < len) {
len = this->Len;
}
len = std::min(this->Len, len);
memcpy(buf, this->Buffer + this->Pos, len);
this->Pos += len;
@ -209,9 +207,7 @@ CSampleWavStream::~CSampleWavStream()
int CSampleWav::Read(void *buf, int len)
{
if (len > this->Len) {
len = this->Len;
}
len = std::min(this->Len, len);
memcpy(buf, this->Buffer + this->Pos, len);
this->Pos += len;

View file

@ -114,23 +114,15 @@
HitUnit(&caster, *target, -(castcount * hp));
} else {
target->Variable[HP_INDEX].Value += castcount * hp;
if (target->Variable[HP_INDEX].Value < 0) {
target->Variable[HP_INDEX].Value = 0;
}
target->Variable[HP_INDEX].Value = std::max(target->Variable[HP_INDEX].Value, 0);
}
} else {
target->Variable[HP_INDEX].Value += castcount * hp;
if (target->Variable[HP_INDEX].Value > target->Variable[HP_INDEX].Max) {
target->Variable[HP_INDEX].Value = target->Variable[HP_INDEX].Max;
}
target->Variable[HP_INDEX].Value = std::min(target->Variable[HP_INDEX].Max, target->Variable[HP_INDEX].Value);
}
target->Variable[MANA_INDEX].Value += castcount * mana;
if (target->Variable[MANA_INDEX].Value < 0) {
target->Variable[MANA_INDEX].Value = 0;
}
if (target->Variable[MANA_INDEX].Value > target->Variable[MANA_INDEX].Max) {
target->Variable[MANA_INDEX].Value = target->Variable[MANA_INDEX].Max;
}
clamp(&target->Variable[MANA_INDEX].Value, 0, target->Variable[MANA_INDEX].Max);
if (spell.RepeatCast) {
return 1;
}

View file

@ -84,17 +84,10 @@
HitUnit(&caster, *target, -hp);
} else {
target->Variable[HP_INDEX].Value += hp;
if (target->Variable[HP_INDEX].Value > target->Variable[HP_INDEX].Max) {
target->Variable[HP_INDEX].Value = target->Variable[HP_INDEX].Max;
}
target->Variable[HP_INDEX].Value = std::min(target->Variable[HP_INDEX].Max, target->Variable[HP_INDEX].Value);
}
target->Variable[MANA_INDEX].Value += mana;
if (target->Variable[MANA_INDEX].Value < 0) {
target->Variable[MANA_INDEX].Value = 0;
}
if (target->Variable[MANA_INDEX].Value > target->Variable[MANA_INDEX].Max) {
target->Variable[MANA_INDEX].Value = target->Variable[MANA_INDEX].Max;
}
clamp(&target->Variable[MANA_INDEX].Value, 0, target->Variable[MANA_INDEX].Max);
}
return 0;
}

View file

@ -896,9 +896,7 @@ void CPlayer::ChangeResource(const int resource, const int value, const bool sto
const int fromStore = std::min(this->StoredResources[resource], abs(value));
this->StoredResources[resource] -= fromStore;
this->Resources[resource] -= abs(value) - fromStore;
if (this->Resources[resource] < 0) {
this->Resources[resource] = 0;
}
this->Resources[resource] = std::max(this->Resources[resource], 0);
} else {
if (store && this->MaxResources[resource] != -1) {
this->StoredResources[resource] += std::min(value, this->MaxResources[resource] - this->StoredResources[resource]);

View file

@ -1066,9 +1066,7 @@ std::string EvalString(const StringDesc *s)
}
if (s->D.Line.MaxLen) {
maxlen = EvalNumber(s->D.Line.MaxLen);
if (maxlen < 0) {
maxlen = 0;
}
maxlen = std::max(maxlen, 0);
} else {
maxlen = 0;
}

View file

@ -1310,9 +1310,7 @@ void UpdateTimer()
GameTimer.Cycles += GameCycle - GameTimer.LastUpdate;
} else {
GameTimer.Cycles -= GameCycle - GameTimer.LastUpdate;
if (GameTimer.Cycles < 0) {
GameTimer.Cycles = 0;
}
GameTimer.Cycles = std::max(GameTimer.Cycles, 0l);
}
GameTimer.LastUpdate = GameCycle;
}

View file

@ -432,9 +432,7 @@ static void FinishViewportModeConfiguration(CViewport new_vps[], int num_vps)
// Update the viewport pointers
//
UI.MouseViewport = GetViewport(CursorScreenPos);
if (UI.SelectedViewport > UI.Viewports + UI.NumViewports - 1) {
UI.SelectedViewport = UI.Viewports + UI.NumViewports - 1;
}
UI.SelectedViewport = std::min(UI.Viewports + UI.NumViewports - 1, UI.SelectedViewport);
}
/**

View file

@ -142,10 +142,8 @@ void freeGuichan()
Gui = NULL;
}
if (Input) {
delete Input;
Input = NULL;
}
delete Input;
Input = NULL;
}
/**
@ -548,9 +546,7 @@ void ImageRadioButton::adjustSize()
if (uncheckedNormalImage) {
width = uncheckedNormalImage->getWidth();
width += width / 2;
if (uncheckedNormalImage->getHeight() > height) {
height = uncheckedNormalImage->getHeight();
}
height = std::max(height, uncheckedNormalImage->getHeight());
} else {
width = getFont()->getHeight();
width += width / 2;
@ -683,9 +679,7 @@ void ImageCheckBox::adjustSize()
if (uncheckedNormalImage) {
width = uncheckedNormalImage->getWidth();
width += width / 2;
if (uncheckedNormalImage->getHeight() > height) {
height = uncheckedNormalImage->getHeight();
}
height = std::max(height, uncheckedNormalImage->getHeight());
} else {
width = getFont()->getHeight();
width += width / 2;

View file

@ -833,10 +833,8 @@ public:
cost = HEALTH_FACTOR * (2 * hp_damage_evaluate -
dest->Variable[HP_INDEX].Value) /
(dtype.TileWidth * dtype.TileWidth);
if (cost < 1) {
cost = 1;
}
cost = (-cost);
cost = std::max(cost, 1);
cost = -cost;
} else {
// Priority 0-255
cost += dtype.Priority * PRIORITY_FACTOR;
@ -860,14 +858,8 @@ public:
int effective_hp = (dest->Variable[HP_INDEX].Value - 2 * hp_damage_evaluate);
// Unit we won't kill are evaluated the same
if (effective_hp > 0) {
effective_hp = 0;
}
// Unit we are sure to kill are all evaluated the same (except PRIORITY)
if (effective_hp < -hp_damage_evaluate) {
effective_hp = -hp_damage_evaluate;
}
clamp(&effective_hp, 0, -hp_damage_evaluate);
// Here, effective_hp vary from -hp_damage_evaluate (unit will be killed) to 0 (unit can't be killed)
// => we prefer killing rather than only hitting...
@ -880,9 +872,7 @@ public:
// the cost may be divided accros multiple cells
cost = cost / (dtype.TileWidth * dtype.TileWidth);
if (cost < 1) {
cost = 1;
}
cost = std::max(cost, 1);
// Removed Unit's are in bunkers
int d;

View file

@ -582,15 +582,8 @@ static void ApplyUpgradeModifier(CPlayer &player, const CUpgradeModifier *um)
stat.Variables[j].Increase += um->Modifier.Variables[j].Increase;
}
if (stat.Variables[j].Value < 0) {
stat.Variables[j].Value = 0;
}
if (stat.Variables[j].Max < 0) {
stat.Variables[j].Max = 0;
}
if (stat.Variables[j].Value > stat.Variables[j].Max) {
stat.Variables[j].Value = stat.Variables[j].Max;
}
stat.Variables[j].Max = std::max(stat.Variables[j].Max, 0);
clamp(&stat.Variables[j].Value, 0, stat.Variables[j].Max);
}
// And now modify ingame units
@ -614,16 +607,10 @@ static void ApplyUpgradeModifier(CPlayer &player, const CUpgradeModifier *um)
unit.Variable[j].Increase += um->Modifier.Variables[j].Increase;
}
if (unit.Variable[j].Value < 0) {
unit.Variable[j].Value = 0;
}
unit.Variable[j].Max += um->Modifier.Variables[j].Max;
if (unit.Variable[j].Max < 0) {
unit.Variable[j].Max = 0;
}
if (unit.Variable[j].Value > unit.Variable[j].Max) {
unit.Variable[j].Value = unit.Variable[j].Max;
}
unit.Variable[j].Max = std::max(unit.Variable[j].Max, 0);
clamp(&unit.Variable[j].Value, 0, unit.Variable[j].Max);
}
}
}

View file

@ -805,9 +805,7 @@ void CFont::MeasureWidths()
for (; sp < lp; --lp) {
if (*lp != ckey && *lp != 7) {
if (lp - sp > CharWidth[y]) { // max width
CharWidth[y] = lp - sp;
}
CharWidth[y] = std::max<char>(CharWidth[y], lp - sp);
}
}
sp += G->Surface->pitch;

View file

@ -645,7 +645,7 @@ void CGraphic::GenFramesMap()
Assert(Width != 0);
Assert(Height != 0);
if (frame_map) { delete[] frame_map; }
delete[] frame_map;
frame_map = new frame_pos_t[NumFrames];
@ -818,10 +818,8 @@ void CGraphic::Free(CGraphic *g)
#endif
{
FreeSurface(&g->SurfaceFlip);
if (g->frameFlip_map) {
delete[] g->frameFlip_map;
g->frameFlip_map = NULL;
}
delete[] g->frameFlip_map;
g->frameFlip_map = NULL;
}
if (!g->HashFile.empty()) {
@ -979,7 +977,7 @@ void CGraphic::Flip()
SDL_UnlockSurface(Surface);
SDL_UnlockSurface(s);
if (frameFlip_map) { delete[] frameFlip_map; }
delete[] frameFlip_map;
frameFlip_map = new frame_pos_t[NumFrames];
@ -1267,10 +1265,8 @@ void CGraphic::Resize(int w, int h)
FreeSurface(&Surface);
Surface = NULL;
}
if (frame_map) {
delete[] frame_map;
frame_map = NULL;
}
delete[] frame_map;
frame_map = NULL;
#if defined(USE_OPENGL) || defined(USE_GLES)
if (!UseOpenGL)
#endif
@ -1279,10 +1275,8 @@ void CGraphic::Resize(int w, int h)
FreeSurface(&SurfaceFlip);
SurfaceFlip = NULL;
}
if (frameFlip_map) {
delete[] frameFlip_map;
frameFlip_map = NULL;
}
delete[] frameFlip_map;
frameFlip_map = NULL;
}
#if defined(USE_OPENGL) || defined(USE_GLES)