Factorize code with new function clamp.
This commit is contained in:
parent
2f97c091fb
commit
3bbde68a25
14 changed files with 55 additions and 120 deletions
src
|
@ -184,16 +184,7 @@ static bool MoveRandomly(CUnit &unit)
|
|||
}
|
||||
|
||||
// restrict to map
|
||||
if (pos.x < 0) {
|
||||
pos.x = 0;
|
||||
} else if (pos.x >= Map.Info.MapWidth) {
|
||||
pos.x = Map.Info.MapWidth - 1;
|
||||
}
|
||||
if (pos.y < 0) {
|
||||
pos.y = 0;
|
||||
} else if (pos.y >= Map.Info.MapHeight) {
|
||||
pos.y = Map.Info.MapHeight - 1;
|
||||
}
|
||||
Map.Clamp(pos);
|
||||
|
||||
// move if possible
|
||||
if (pos != unit.tilePos) {
|
||||
|
|
|
@ -293,24 +293,17 @@ static void HandleBuffs(CUnit &unit, int amount)
|
|||
|
||||
unit.Variable[SHIELD_INDEX].Increase = 1;
|
||||
|
||||
const bool lastStatusIsHidden = unit.Variable[INVISIBLE_INDEX].Value > 0;
|
||||
// User defined variables
|
||||
for (unsigned int i = 0; i < UnitTypeVar.GetNumberVariable(); i++) {
|
||||
if (unit.Variable[i].Enable && unit.Variable[i].Increase) {
|
||||
if (i == INVISIBLE_INDEX &&
|
||||
unit.Variable[INVISIBLE_INDEX].Value > 0 &&
|
||||
unit.Variable[INVISIBLE_INDEX].Value +
|
||||
unit.Variable[INVISIBLE_INDEX].Increase <= 0) {
|
||||
UnHideUnit(unit);
|
||||
} else {
|
||||
unit.Variable[i].Value += unit.Variable[i].Increase;
|
||||
if (unit.Variable[i].Value <= 0) {
|
||||
unit.Variable[i].Value = 0;
|
||||
} else if (unit.Variable[i].Value > unit.Variable[i].Max) {
|
||||
unit.Variable[i].Value = unit.Variable[i].Max;
|
||||
}
|
||||
}
|
||||
unit.Variable[i].Value += unit.Variable[i].Increase;
|
||||
clamp(&unit.Variable[i].Value, 0, unit.Variable[i].Max);
|
||||
}
|
||||
}
|
||||
if (lastStatusIsHidden && unit.Variable[INVISIBLE_INDEX].Value == 0) {
|
||||
UnHideUnit(unit);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -503,6 +503,8 @@ public:
|
|||
/// Remove unit from cache
|
||||
void Remove(CUnit &unit);
|
||||
|
||||
void Clamp(Vec2i &pos) const;
|
||||
|
||||
//Warning: we expect typical usage as xmin = x - range
|
||||
void FixSelectionArea(Vec2i &minpos, Vec2i &maxpos) {
|
||||
minpos.x = std::max<short>(0, minpos.x);
|
||||
|
|
|
@ -87,6 +87,7 @@
|
|||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
/*============================================================================
|
||||
== Macro
|
||||
|
|
|
@ -176,6 +176,20 @@ extern inline int MyAbs(int x) { return (x ^ (x >> 31)) - (x >> 31); }
|
|||
/// Compute a square root using ints
|
||||
extern long isqrt(long num);
|
||||
|
||||
|
||||
template <typename T>
|
||||
void clamp(T* value, T minValue, T maxValue)
|
||||
{
|
||||
Assert(minValue < maxValue);
|
||||
|
||||
if (*value < minValue) {
|
||||
*value = minValue;
|
||||
} else if (maxValue < *value) {
|
||||
*value = maxValue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*----------------------------------------------------------------------------
|
||||
-- Strings
|
||||
----------------------------------------------------------------------------*/
|
||||
|
|
|
@ -65,16 +65,8 @@ bool CViewport::Contains(const PixelPos &screenPos) const
|
|||
|
||||
void CViewport::Restrict(int &screenPosX, int &screenPosY) const
|
||||
{
|
||||
if (screenPosX < this->GetTopLeftPos().x) {
|
||||
screenPosX = this->GetTopLeftPos().x;
|
||||
} else if (screenPosX > this->GetBottomRightPos().x - 1) {
|
||||
screenPosX = this->GetBottomRightPos().x - 1;
|
||||
}
|
||||
if (screenPosY < this->GetTopLeftPos().y) {
|
||||
screenPosY = this->GetTopLeftPos().y;
|
||||
} else if (screenPosY > this->GetBottomRightPos().y - 1) {
|
||||
screenPosY = this->GetBottomRightPos().y - 1;
|
||||
}
|
||||
clamp(&screenPosX, this->GetTopLeftPos().x, this->GetBottomRightPos().x - 1);
|
||||
clamp(&screenPosY, this->GetTopLeftPos().y, this->GetBottomRightPos().y - 1);
|
||||
}
|
||||
|
||||
PixelSize CViewport::GetPixelSize() const
|
||||
|
|
|
@ -472,22 +472,15 @@ static int CclSetGlobalSoundRange(lua_State *l)
|
|||
*/
|
||||
static int CclSetSoundRange(lua_State *l)
|
||||
{
|
||||
unsigned char theRange;
|
||||
int tmp;
|
||||
CSound *id;
|
||||
|
||||
LuaCheckArgs(l, 2);
|
||||
|
||||
tmp = LuaToNumber(l, 2);
|
||||
if (tmp < 0) {
|
||||
theRange = 0;
|
||||
} else if (tmp > 255) {
|
||||
theRange = 255;
|
||||
} else {
|
||||
theRange = (unsigned char)tmp;
|
||||
}
|
||||
int tmp = LuaToNumber(l, 2);
|
||||
clamp(&tmp, 0, 255);
|
||||
const unsigned char theRange = static_cast<unsigned char>(tmp);
|
||||
|
||||
lua_pushvalue(l, 1);
|
||||
id = CclGetSound(l);
|
||||
CSound *id = CclGetSound(l);
|
||||
SetSoundRange(id, theRange);
|
||||
return 1;
|
||||
}
|
||||
|
|
|
@ -251,17 +251,10 @@ static unsigned char CalculateVolume(bool isVolume, int power, unsigned char ran
|
|||
*/
|
||||
static char CalculateStereo(const CUnit &unit)
|
||||
{
|
||||
int stereo;
|
||||
|
||||
stereo = ((unit.tilePos.x * PixelTileSize.x + unit.Type->TileWidth * PixelTileSize.x / 2 +
|
||||
unit.IX - UI.SelectedViewport->MapX * PixelTileSize.x) * 256 /
|
||||
((UI.SelectedViewport->MapWidth - 1) * PixelTileSize.x)) - 128;
|
||||
if (stereo < -128) {
|
||||
stereo = -128;
|
||||
} else if (stereo > 127) {
|
||||
stereo = 127;
|
||||
}
|
||||
|
||||
int stereo = ((unit.tilePos.x * PixelTileSize.x + unit.Type->TileWidth * PixelTileSize.x / 2 +
|
||||
unit.IX - UI.SelectedViewport->MapX * PixelTileSize.x) * 256 /
|
||||
((UI.SelectedViewport->MapWidth - 1) * PixelTileSize.x)) - 128;
|
||||
clamp(&stereo, -128, 127);
|
||||
return stereo;
|
||||
}
|
||||
|
||||
|
@ -321,12 +314,7 @@ void PlayMissileSound(const Missile *missile, CSound *sound)
|
|||
int stereo = ((missile->position.x + missile->Type->G->Width / 2 -
|
||||
UI.SelectedViewport->MapX * PixelTileSize.x) * 256 /
|
||||
((UI.SelectedViewport->MapWidth - 1) * PixelTileSize.x)) - 128;
|
||||
|
||||
if (stereo < -128) {
|
||||
stereo = -128;
|
||||
} else if (stereo > 127) {
|
||||
stereo = 127;
|
||||
}
|
||||
clamp(&stereo, -128, 127);
|
||||
|
||||
Origin source = {NULL, 0};
|
||||
|
||||
|
|
|
@ -585,13 +585,8 @@ int PlaySoundFile(const std::string &name)
|
|||
*/
|
||||
void SetEffectsVolume(int volume)
|
||||
{
|
||||
if (volume < 0) {
|
||||
EffectsVolume = 0;
|
||||
} else if (volume > MaxVolume) {
|
||||
EffectsVolume = MaxVolume;
|
||||
} else {
|
||||
EffectsVolume = volume;
|
||||
}
|
||||
clamp(&volume, 0, MaxVolume);
|
||||
EffectsVolume = volume;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -717,13 +712,8 @@ void StopMusic()
|
|||
*/
|
||||
void SetMusicVolume(int volume)
|
||||
{
|
||||
if (volume < 0) {
|
||||
MusicVolume = 0;
|
||||
} else if (volume > MaxVolume) {
|
||||
MusicVolume = MaxVolume;
|
||||
} else {
|
||||
MusicVolume = volume;
|
||||
}
|
||||
clamp(&volume, 0, MaxVolume);
|
||||
MusicVolume = volume;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -364,14 +364,9 @@ int AdjustVariable::Cast(CUnit &caster, const SpellType *, CUnit *target, const
|
|||
unit->Variable[i].Value = this->Var[i].Value;
|
||||
}
|
||||
unit->Variable[i].Value += this->Var[i].AddValue;
|
||||
unit->Variable[i].Value += this->Var[i].IncreaseTime
|
||||
* unit->Variable[i].Increase;
|
||||
unit->Variable[i].Value += this->Var[i].IncreaseTime * unit->Variable[i].Increase;
|
||||
|
||||
if (unit->Variable[i].Value <= 0) {
|
||||
unit->Variable[i].Value = 0;
|
||||
} else if (unit->Variable[i].Value > unit->Variable[i].Max) {
|
||||
unit->Variable[i].Value = unit->Variable[i].Max;
|
||||
}
|
||||
clamp(&unit->Variable[i].Value, 0, unit->Variable[i].Max);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
|
|
@ -1204,16 +1204,8 @@ int HandleMouseScrollArea(int x, int y)
|
|||
void HandleCursorMove(int *x, int *y)
|
||||
{
|
||||
// Reduce coordinates to window-size.
|
||||
if (*x < 0) {
|
||||
*x = 0;
|
||||
} else if (*x >= Video.Width) {
|
||||
*x = Video.Width - 1;
|
||||
}
|
||||
if (*y < 0) {
|
||||
*y = 0;
|
||||
} else if (*y >= Video.Height) {
|
||||
*y = Video.Height - 1;
|
||||
}
|
||||
clamp(x, 0, Video.Width - 1);
|
||||
clamp(y, 0, Video.Height - 1);
|
||||
CursorX = *x;
|
||||
CursorY = *y;
|
||||
}
|
||||
|
|
|
@ -766,24 +766,11 @@ void RestrictCursorToViewport()
|
|||
*/
|
||||
void RestrictCursorToMinimap()
|
||||
{
|
||||
if (CursorX < UI.Minimap.X) {
|
||||
CursorStartX = UI.Minimap.X;
|
||||
} else if (CursorX >= UI.Minimap.X + UI.Minimap.W) {
|
||||
CursorStartX = UI.Minimap.X + UI.Minimap.W - 1;
|
||||
} else {
|
||||
CursorStartX = CursorX;
|
||||
}
|
||||
clamp(&CursorX, UI.Minimap.X, UI.Minimap.X + UI.Minimap.W - 1);
|
||||
clamp(&CursorY, UI.Minimap.Y, UI.Minimap.Y + UI.Minimap.H - 1);
|
||||
|
||||
if (CursorY < UI.Minimap.Y) {
|
||||
CursorStartY = UI.Minimap.Y;
|
||||
} else if (CursorY >= UI.Minimap.Y + UI.Minimap.H) {
|
||||
CursorStartY = UI.Minimap.Y + UI.Minimap.H - 1;
|
||||
} else {
|
||||
CursorStartY = CursorY;
|
||||
}
|
||||
|
||||
CursorX = UI.MouseWarpX = CursorStartX;
|
||||
CursorY = UI.MouseWarpY = CursorStartY;
|
||||
UI.MouseWarpX = CursorStartX = CursorX;
|
||||
UI.MouseWarpY = CursorStartY = CursorY;
|
||||
CursorOn = CursorOnMinimap;
|
||||
}
|
||||
|
||||
|
|
|
@ -2955,17 +2955,8 @@ void HitUnit(CUnit *attacker, CUnit &target, int damage)
|
|||
d = 1;
|
||||
}
|
||||
pos.x = target.tilePos.x + (pos.x * 5) / d + (SyncRand() & 3);
|
||||
if (pos.x < 0) {
|
||||
pos.x = 0;
|
||||
} else if (pos.x >= Map.Info.MapWidth) {
|
||||
pos.x = Map.Info.MapWidth - 1;
|
||||
}
|
||||
pos.y = target.tilePos.y + (pos.y * 5) / d + (SyncRand() & 3);
|
||||
if (pos.y < 0) {
|
||||
pos.y = 0;
|
||||
} else if (pos.y >= Map.Info.MapHeight) {
|
||||
pos.y = Map.Info.MapHeight - 1;
|
||||
}
|
||||
Map.Clamp(pos);
|
||||
CommandStopUnit(target);
|
||||
CommandMove(target, pos, 0);
|
||||
}
|
||||
|
|
|
@ -93,6 +93,12 @@ void CMap::Remove(CUnit &unit)
|
|||
}
|
||||
|
||||
|
||||
void CMap::Clamp(Vec2i &pos) const
|
||||
{
|
||||
clamp<short int>(&pos.x, 0, this->Info.MapWidth - 1);
|
||||
clamp<short int>(&pos.y, 0, this->Info.MapHeight - 1);
|
||||
}
|
||||
|
||||
class NoFilter
|
||||
{
|
||||
public:
|
||||
|
|
Loading…
Add table
Reference in a new issue