diff --git a/lib/BattleHex.cpp b/lib/BattleHex.cpp index 654197d44..659de94cb 100644 --- a/lib/BattleHex.cpp +++ b/lib/BattleHex.cpp @@ -44,18 +44,6 @@ BattleHex& BattleHex::moveInDir(EDir dir, bool hasToBeValid) return *this; } -void BattleHex::operator+=(EDir dir) -{ - moveInDir(dir); -} - -BattleHex BattleHex::operator+(EDir dir) const -{ - BattleHex ret(*this); - ret += dir; - return ret; -} - std::vector BattleHex::neighbouringTiles() const { std::vector ret; @@ -93,17 +81,18 @@ char BattleHex::getDistance(BattleHex hex1, BattleHex hex2) { int y1 = hex1.getY(), y2 = hex2.getY(); - - int x1 = hex1.getX() + y1 / 2.0, - x2 = hex2.getX() + y2 / 2.0; + + // FIXME: Omit floating point arithmetics + int x1 = (int)(hex1.getX() + y1 * 0.5), + x2 = (int)(hex2.getX() + y2 * 0.5); int xDst = x2 - x1, yDst = y2 - y1; if ((xDst >= 0 && yDst >= 0) || (xDst < 0 && yDst < 0)) return std::max(std::abs(xDst), std::abs(yDst)); - else - return std::abs(xDst) + std::abs(yDst); + + return std::abs(xDst) + std::abs(yDst); } void BattleHex::checkAndPush(BattleHex tile, std::vector & ret) diff --git a/lib/BattleHex.h b/lib/BattleHex.h index 48c53c007..26042e7b4 100644 --- a/lib/BattleHex.h +++ b/lib/BattleHex.h @@ -17,22 +17,16 @@ struct DLL_LINKAGE BattleHex { static const si16 INVALID = -1; - enum EDir{RIGHT, BOTTOM_RIGHT, BOTTOM_LEFT, LEFT, TOP_LEFT, TOP_RIGHT}; + enum EDir { RIGHT, BOTTOM_RIGHT, BOTTOM_LEFT, LEFT, TOP_LEFT, TOP_RIGHT }; si16 hex; BattleHex() : hex(INVALID) {} BattleHex(si16 _hex) : hex(_hex) {} - operator si16() const - { - return hex; - } + operator si16() const { return hex; } - bool isValid() const - { - return hex >= 0 && hex < GameConstants::BFIELD_SIZE; - } + bool isValid() const { return hex >= 0 && hex < GameConstants::BFIELD_SIZE; } template BattleHex(inttype x, inttype y) @@ -61,9 +55,7 @@ struct DLL_LINKAGE BattleHex void setXY(si16 x, si16 y, bool hasToBeValid = true) { if(hasToBeValid) - { - assert(x >= 0 && x < GameConstants::BFIELD_WIDTH && y >= 0 && y < GameConstants::BFIELD_HEIGHT); - } + assert(x >= 0 && x < GameConstants::BFIELD_WIDTH && y >= 0 && y < GameConstants::BFIELD_HEIGHT); hex = x + y * GameConstants::BFIELD_WIDTH; } @@ -73,28 +65,23 @@ struct DLL_LINKAGE BattleHex setXY(xy.first, xy.second); } - si16 getY() const - { - return hex / GameConstants::BFIELD_WIDTH; - } + si16 getY() const { return hex / GameConstants::BFIELD_WIDTH; } + si16 getX() const { return hex % GameConstants::BFIELD_WIDTH; } - si16 getX() const - { - int pos = hex - getY() * GameConstants::BFIELD_WIDTH; - return pos; - } - - std::pair getXY() const - { - return std::make_pair(getX(), getY()); - } + std::pair getXY() const { return std::make_pair(getX(), getY()); } //moving to direction BattleHex& moveInDir(EDir dir, bool hasToBeValid = true); - void operator+=(EDir dir); //sugar for above + BattleHex& operator+=(EDir dir) { return moveInDir(dir); } //sugar for above //generates new BattleHex moved by given dir - BattleHex operator+(EDir dir) const; + BattleHex movedInDir(EDir dir, bool hasToBeValid = true) const + { + BattleHex result(*this); + result.moveInDir(dir, hasToBeValid); + return result; + } + BattleHex operator+(EDir dir) const { return movedInDir(dir); } std::vector neighbouringTiles() const; diff --git a/lib/CondSh.h b/lib/CondSh.h index fe9cac8a2..1ec163571 100644 --- a/lib/CondSh.h +++ b/lib/CondSh.h @@ -17,50 +17,48 @@ template struct CondSh boost::condition_variable cond; boost::mutex mx; - CondSh() - {} - - CondSh(T t) - { - data = t; - } + CondSh() {} + CondSh(T t) : data(t) {} + // set data void set(T t) { boost::unique_lock lock(mx); - data=t; + data = t; } - void setn(T t) //set data and notify + // set data and notify + void setn(T t) { - { - boost::unique_lock lock(mx); - data=t; - } + set(t); cond.notify_all(); }; - T get() //get stored value + // get stored value + T get() { boost::unique_lock lock(mx); return data; } - void waitWhileTrue() //waits until data is set to false + // waits until data is set to false + void waitWhileTrue() { boost::unique_lock un(mx); while(data) cond.wait(un); } - void waitWhile(const T &t) //waits while data is set to arg + // waits while data is set to arg + void waitWhile(const T & t) { boost::unique_lock un(mx); while(data == t) cond.wait(un); } - void waitUntil(const T &t) //waits until data is set to arg + // waits until data is set to arg + void waitUntil(const T & t) { boost::unique_lock un(mx); while(data != t) diff --git a/lib/int3.h b/lib/int3.h index 41a8dd908..8827949cf 100644 --- a/lib/int3.h +++ b/lib/int3.h @@ -94,11 +94,11 @@ public: } //returns squared distance on Oxy plane (z coord is not used) - si32 dist2dSQ(const int3 & o) const + ui32 dist2dSQ(const int3 & o) const { const si32 dx = (x - o.x); const si32 dy = (y - o.y); - return dx*dx + dy*dy; + return (ui32)(dx*dx) + (ui32)(dy*dy); } //returns distance on Oxy plane (z coord is not used) double dist2d(const int3 & o) const @@ -157,15 +157,17 @@ struct ShashInt3 static const int3 dirs[] = { int3(0,1,0),int3(0,-1,0),int3(-1,0,0),int3(+1,0,0), int3(1,1,0),int3(-1,1,0),int3(1,-1,0),int3(-1,-1,0) }; -//FIXME: make sure it's container and not just any template int3 findClosestTile (Container & container, int3 dest) { - int3 result(-1,-1,-1); + static_assert(std::is_same::value, + "findClosestTile requires container."); + + int3 result(-1, -1, -1); ui32 distance = std::numeric_limits::max(); - for (int3 tile : container) + for (const int3& tile : container) { - ui32 currentDistance = dest.dist2dSQ(tile); + const ui32 currentDistance = dest.dist2dSQ(tile); if (currentDistance < distance) { result = tile; diff --git a/lib/rmg/float3.h b/lib/rmg/float3.h index 36f6feba0..402ba66b0 100644 --- a/lib/rmg/float3.h +++ b/lib/rmg/float3.h @@ -10,75 +10,103 @@ * */ +// FIXME: Class doesn't contain three float values. Update name and description. /// Class which consists of three float values. Represents position virtual RMG (0;1) area. class float3 { public: float x, y; si32 z; - inline float3():x(0),y(0),z(0){}; //c-tor, x/y/z initialized to 0 - inline float3(const float X, const float Y, const si32 Z):x(X),y(Y),z(Z){}; //c-tor - inline float3(const float3 & val) : x(val.x), y(val.y), z(val.z){} //copy c-tor - inline float3 & operator=(const float3 & val) {x = val.x; y = val.y; z = val.z; return *this;} //assignemt operator - ~float3() {} // d-tor - does nothing - inline float3 operator+(const float3 & i) const //returns float3 with coordinates increased by corresponding coordinate of given float3 - {return float3(x+i.x,y+i.y,z+i.z);} - inline float3 operator+(const float i) const //returns float3 with coordinates increased by given numer - {return float3(x+i,y+i,z+i);} - inline float3 operator-(const float3 & i) const //returns float3 with coordinates decreased by corresponding coordinate of given float3 - {return float3(x-i.x,y-i.y,z-i.z);} - inline float3 operator-(const float i) const //returns float3 with coordinates decreased by given numer - {return float3(x-i,y-i,z-i);} - inline float3 operator*(const float i) const //returns float3 with plane coordinates decreased by given numer - {return float3(x*i, y*i, z);} - inline float3 operator/(const float i) const //returns float3 with plane coordinates decreased by given numer - {return float3(x/i, y/i, z);} - inline float3 operator-() const //returns opposite position - {return float3(-x,-y,-z);} - inline double dist2d(const float3 &other) const //distance (z coord is not used) - {return std::sqrt((double)(x-other.x)*(x-other.x) + (y-other.y)*(y-other.y));} - inline bool areNeighbours(const float3 &other) const - {return dist2d(other) < 2. && z == other.z;} - inline void operator+=(const float3 & i) + + float3() : x(0), y(0), z(0) {} + float3(const float X, const float Y, const si32 Z): x(X), y(Y), z(Z) {} + float3(const float3 & copy) : x(copy.x), y(copy.y), z(copy.z) {} + float3 & operator=(const float3 & copy) { x = copy.x; y = copy.y; z = copy.z; return *this; } + + // returns float3 with coordinates increased by corresponding coordinate of given float3 + float3 operator+(const float3 & i) const { return float3(x + i.x, y + i.y, z + i.z); } + // returns float3 with coordinates increased by given numer + float3 operator+(const float i) const { return float3(x + i, y + i, z + (si32)i); } + // returns float3 with coordinates decreased by corresponding coordinate of given float3 + float3 operator-(const float3 & i) const { return float3(x - i.x, y - i.y, z - i.z); } + // returns float3 with coordinates decreased by given numer + float3 operator-(const float i) const { return float3(x - i, y - i, z - (si32)i); } + + // returns float3 with plane coordinates decreased by given numer + float3 operator*(const float i) const {return float3(x * i, y * i, z);} + // returns float3 with plane coordinates decreased by given numer + float3 operator/(const float i) const {return float3(x / i, y / i, z);} + + // returns opposite position + float3 operator-() const { return float3(-x, -y, -z); } + + // returns squared distance on Oxy plane (z coord is not used) + double dist2dSQ(const float3 & o) const { - x+=i.x; - y+=i.y; - z+=i.z; + const double dx = (x - o.x); + const double dy = (y - o.y); + return dx*dx + dy*dy; } - inline void operator+=(const float & i) + // returns distance on Oxy plane (z coord is not used) + double dist2d(const float3 &other) const { return std::sqrt(dist2dSQ(other)); } + + bool areNeighbours(const float3 &other) const { return (dist2dSQ(other) < 4.0) && z == other.z; } + + float3& operator+=(const float3 & i) { - x+=i; - y+=i; - z+=i; + x += i.x; + y += i.y; + z += i.z; + + return *this; } - inline void operator-=(const float3 & i) + float3& operator+=(const float & i) { - x-=i.x; - y-=i.y; - z-=i.z; + x += i; + y += i; + z += (si32)i; + + return *this; } - inline void operator-=(const float & i) + + float3& operator-=(const float3 & i) { - x+=i; - y+=i; - z+=i; + x -= i.x; + y -= i.y; + z -= i.z; + + return *this; } - inline void operator*=(const float & i) //scale on plane + float3& operator-=(const float & i) { - x*=i; - y*=i; - } - inline void operator/=(const float & i) //scale on plane - { - x/=i; - y/=i; + x += i; + y += i; + z += (si32)i; + + return *this; } - inline bool operator==(const float3 & i) const - {return (x==i.x) && (y==i.y) && (z==i.z);} - inline bool operator!=(const float3 & i) const - {return !(*this==i);} - inline bool operator<(const float3 & i) const + // scale on plane + float3& operator*=(const float & i) + { + x *= i; + y *= i; + + return *this; + } + // scale on plane + float3& operator/=(const float & i) + { + x /= i; + y /= i; + + return *this; + } + + bool operator==(const float3 & i) const { return (x == i.x) && (y == i.y) && (z == i.z); } + bool operator!=(const float3 & i) const { return (x != i.x) || (y != i.y) || (z != i.z); } + + bool operator<(const float3 & i) const { if (zi.x) return false; + return false; } - inline std::string operator ()() const + + std::string operator ()() const { return "(" + boost::lexical_cast(x) + " " + boost::lexical_cast(y) + " " + boost::lexical_cast(z) + ")"; } - inline bool valid() const + + bool valid() const { return z >= 0; //minimal condition that needs to be fulfilled for tiles in the map } + template void serialize(Handler &h, const float version) { h & x & y & z; } - }; + inline std::istream & operator>>(std::istream & str, float3 & dest) { - str>>dest.x>>dest.y>>dest.z; - return str; + return str >> dest.x >> dest.y >> dest.z; } inline std::ostream & operator<<(std::ostream & str, const float3 & sth) { - return str<