From aad6ed8297b41cdbd623dda7b1bf46f1ade1e05c Mon Sep 17 00:00:00 2001 From: Asdow <20314541+Asdow@users.noreply.github.com> Date: Wed, 13 Nov 2024 01:29:36 +0200 Subject: [PATCH] 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 --- Tactical/Overhead Types.h | 72 +++++++++++++++++++++++++++++++++++++- Tactical/Soldier Control.h | 2 +- 2 files changed, 72 insertions(+), 2 deletions(-) diff --git a/Tactical/Overhead Types.h b/Tactical/Overhead Types.h index 2ca0d5db..a8d45707 100644 --- a/Tactical/Overhead Types.h +++ b/Tactical/Overhead Types.h @@ -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(lhs.i - rhs.i) }; } +inline SoldierID operator-(const SoldierID lhs, const int rhs) { return SoldierID{ static_cast(lhs.i - rhs) }; } +inline SoldierID operator-(const SoldierID lhs, const unsigned int rhs) { return SoldierID{ static_cast(lhs.i - rhs) }; } +inline SoldierID operator-(const SoldierID lhs, const UINT16 rhs) { return SoldierID{ static_cast(lhs.i - rhs) }; } +inline SoldierID operator-(const int lhs, const SoldierID rhs) { return SoldierID{ static_cast(lhs - rhs.i) }; } +inline SoldierID operator-(const unsigned int lhs, const SoldierID rhs) { return SoldierID{ static_cast(lhs - rhs.i) }; } + +inline SoldierID operator+(const SoldierID lhs, const SoldierID rhs) { return SoldierID{ static_cast(lhs.i + rhs.i) }; } +inline SoldierID operator+(const SoldierID lhs, const int rhs) { return SoldierID{ static_cast(lhs.i + rhs) }; } +inline SoldierID operator+(const SoldierID lhs, const unsigned int rhs) { return SoldierID{ static_cast(lhs.i + rhs) }; } +inline SoldierID operator+(const SoldierID lhs, const UINT16 rhs) { return SoldierID{ static_cast(lhs.i + rhs) }; } +inline SoldierID operator+(const unsigned int lhs, const SoldierID rhs) { return SoldierID{ static_cast(lhs + rhs.i) }; } +inline SoldierID operator+(const INT16 lhs, const SoldierID rhs) { return SoldierID{ static_cast(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 diff --git a/Tactical/Soldier Control.h b/Tactical/Soldier Control.h index bfc9e6a6..81dc88bc 100644 --- a/Tactical/Soldier Control.h +++ b/Tactical/Soldier Control.h @@ -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);