Unify counter clamp arithmetic into shared addClamped() helper

Rename MAX_COUNTERS_ON_CARD to MAX_COUNTER_VALUE

  Add addClamped() in trice_limits.h, which uses a 64-bit intermediate so
  the addition cannot overflow int. Both Server_Card and Server_Counter use it.

  Add optional [minValue, maxValue] bounds to Server_Counter; setCount()
  and incrementCount() both clamp. Defaults are unbounded, so existing
  callers are unaffected.
This commit is contained in:
DawnFire42 2026-06-19 12:20:50 -04:00
parent 687e6644bc
commit 80946b2fd4
8 changed files with 114 additions and 50 deletions

View file

@ -2,6 +2,8 @@
#define TRICE_LIMITS_H
#include <QString>
#include <QtGlobal>
#include <cstdint>
// max size for short strings, like names and things that are generally a single phrase
constexpr int MAX_NAME_LENGTH = 0xff;
@ -15,11 +17,23 @@ constexpr uint MAXIMUM_DIE_SIDES = 1000000;
constexpr uint MINIMUM_DICE_TO_ROLL = 1;
constexpr uint MAXIMUM_DICE_TO_ROLL = 100;
// Card counter value bounds [0, MAX_COUNTERS_ON_CARD].
// Counters on cards (e.g., +1/+1 counters, charge counters) are non-negative physical game objects.
// Counter value bounds [0, MAX_COUNTER_VALUE].
// Counters (on cards or players) are non-negative values.
// The max of 999 is a display constraint (3-digit rendering) and reasonable gameplay limit.
// Server enforces these bounds; client may also check for UX optimization.
constexpr int MAX_COUNTERS_ON_CARD = 999;
constexpr int MAX_COUNTER_VALUE = 999;
/**
* @brief Overflow-safe clamped addition: returns value + delta bounded to [minValue, maxValue].
*
* Uses a 64-bit intermediate so the addition itself cannot overflow int. Shared by the
* counter arithmetic in Server_Card and Server_Counter so both stay in sync.
*/
inline int addClamped(int value, int delta, int minValue, int maxValue)
{
const auto result = static_cast<int64_t>(value) + static_cast<int64_t>(delta);
return static_cast<int>(qBound(static_cast<int64_t>(minValue), result, static_cast<int64_t>(maxValue)));
}
// optimized functions to get qstrings that are at most that long
static inline QString nameFromStdString(const std::string &_string)