Implemented SoldierID type to replace SOLDIERTYPE->ubID

Goal is to convert all use of SOLDIERTYPE->ubID and any other ID fields that reference the same ID to a safer type than UINT16. Same with any function that's supposed to take ubID
This commit is contained in:
Asdow
2024-11-13 01:29:36 +02:00
parent d6370b7463
commit aad6ed8297
2 changed files with 72 additions and 2 deletions
+71 -1
View File
@@ -32,7 +32,7 @@
//TACTICAL OVERHEAD STUFF
//#define NO_SOLDIER TOTAL_SOLDIERS // SAME AS NOBODY
//#define NOBODY NO_SOLDIER
#define NOBODY TOTAL_SOLDIERS
//#define NOBODY TOTAL_SOLDIERS
@@ -375,4 +375,74 @@ typedef struct
// strcmp returns 0 if true!
#define COMPARE_PALETTEREP_ID( a, b ) ( strcmp( a, b ) ? FALSE : TRUE )
typedef struct SoldierID
{
UINT16 i;
// Implicit conversion and constructor to provide compatibility with unchanged code
// TODO: Remove once SoldierID is used everywhere and these are no longer needed
inline operator UINT16() const { return i; }
SoldierID(const UINT16 val = 0)
: i(val)
{}
} SoldierID;
//inline bool operator==(const SoldierID lhs, const SoldierID rhs) { return lhs.i == rhs.i; }
//inline bool operator!=(const SoldierID lhs, const SoldierID rhs) { return lhs.i != rhs.i; }
inline bool operator<(const SoldierID lhs, const SoldierID rhs) { return lhs.i < rhs.i; }
inline bool operator<(const SoldierID lhs, const int rhs) { return lhs.i < rhs; }
inline bool operator<(const int lhs, const SoldierID rhs) { return lhs < rhs.i; }
inline bool operator<(const unsigned int lhs, const SoldierID rhs) { return lhs < rhs.i; }
inline bool operator<(const UINT16 lhs, const SoldierID rhs) { return lhs < rhs.i; }
inline bool operator>(const SoldierID lhs, const SoldierID rhs) { return lhs.i > rhs.i; }
inline bool operator>(const SoldierID lhs, const int rhs) { return lhs.i > rhs; }
inline bool operator>(const int lhs, const SoldierID rhs) { return lhs > rhs.i; }
inline bool operator>(const UINT16 lhs, const SoldierID rhs) { return lhs > rhs.i; }
inline bool operator<=(const SoldierID lhs, const SoldierID rhs) { return lhs.i <= rhs.i; }
inline bool operator<=(const SoldierID lhs, const int rhs) { return lhs.i <= rhs; }
inline bool operator<=(const SoldierID lhs, const UINT16 rhs) { return lhs.i <= rhs; }
inline bool operator<=(const int lhs, const SoldierID rhs) { return lhs <= rhs.i; }
inline bool operator<=(const UINT32 lhs, const SoldierID rhs) { return lhs <= rhs.i; }
inline bool operator<=(const UINT16 lhs, const SoldierID rhs) { return lhs <= rhs.i; }
inline bool operator<=(const INT16 lhs, const SoldierID rhs) { return lhs <= rhs.i; }
inline bool operator>=(const SoldierID lhs, const SoldierID rhs) { return lhs.i >= rhs.i; }
inline bool operator>=(const SoldierID lhs, const int rhs) { return lhs.i >= rhs; }
inline bool operator>=(const SoldierID lhs, const UINT16 rhs) { return lhs.i >= rhs; }
inline bool operator>=(const int lhs, const SoldierID rhs) { return lhs >= rhs.i; }
inline bool operator>=(const unsigned int lhs, const SoldierID rhs) { return lhs >= rhs.i; }
inline bool operator>=(const UINT16 lhs, const SoldierID rhs) { return lhs >= rhs.i; }
inline SoldierID operator-(const SoldierID lhs, const SoldierID rhs) { return SoldierID{ static_cast<UINT16>(lhs.i - rhs.i) }; }
inline SoldierID operator-(const SoldierID lhs, const int rhs) { return SoldierID{ static_cast<UINT16>(lhs.i - rhs) }; }
inline SoldierID operator-(const SoldierID lhs, const unsigned int rhs) { return SoldierID{ static_cast<UINT16>(lhs.i - rhs) }; }
inline SoldierID operator-(const SoldierID lhs, const UINT16 rhs) { return SoldierID{ static_cast<UINT16>(lhs.i - rhs) }; }
inline SoldierID operator-(const int lhs, const SoldierID rhs) { return SoldierID{ static_cast<UINT16>(lhs - rhs.i) }; }
inline SoldierID operator-(const unsigned int lhs, const SoldierID rhs) { return SoldierID{ static_cast<UINT16>(lhs - rhs.i) }; }
inline SoldierID operator+(const SoldierID lhs, const SoldierID rhs) { return SoldierID{ static_cast<UINT16>(lhs.i + rhs.i) }; }
inline SoldierID operator+(const SoldierID lhs, const int rhs) { return SoldierID{ static_cast<UINT16>(lhs.i + rhs) }; }
inline SoldierID operator+(const SoldierID lhs, const unsigned int rhs) { return SoldierID{ static_cast<UINT16>(lhs.i + rhs) }; }
inline SoldierID operator+(const SoldierID lhs, const UINT16 rhs) { return SoldierID{ static_cast<UINT16>(lhs.i + rhs) }; }
inline SoldierID operator+(const unsigned int lhs, const SoldierID rhs) { return SoldierID{ static_cast<UINT16>(lhs + rhs.i) }; }
inline SoldierID operator+(const INT16 lhs, const SoldierID rhs) { return SoldierID{ static_cast<UINT16>(lhs + rhs.i) }; }
inline SoldierID operator--(SoldierID lhs)
{
lhs.i -= 1;
return lhs;
}
inline SoldierID operator++(SoldierID lhs)
{
lhs.i += 1;
return lhs;
}
//TODO: Change this to constexpr after SoldierID is ready and the user defined constructor from uint16 is not needed anymore.
inline const SoldierID NOBODY{ TOTAL_SOLDIERS };
#endif
+1 -1
View File
@@ -1103,7 +1103,7 @@ public:
// properly until it is all fixed and the files updated.
public:
// ID
UINT16 ubID;
SoldierID ubID;
CHAR16 name[ 10 ];
INT16 GetMaxDistanceVisible(INT32 sGridNo = -1, INT8 bLevel = -1, int calcAsType = -1);