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

@ -1527,9 +1527,9 @@ void PlayerActions::offsetCardCounter(QList<CardItem *> selectedCards, int count
int oldValue = card->getCounters().value(counterId, 0);
int newValue = oldValue + offset;
// Early exit optimization: server enforces [0, MAX_COUNTERS_ON_CARD].
// Early exit optimization: server enforces [0, MAX_COUNTER_VALUE].
// Compare clamped value to allow recovery from invalid states.
int clampedValue = qBound(0, newValue, MAX_COUNTERS_ON_CARD);
int clampedValue = qBound(0, newValue, MAX_COUNTER_VALUE);
if (clampedValue != oldValue) {
auto *cmd = new Command_SetCardCounter;
cmd->set_zone(card->getZone()->getName().toStdString());
@ -1563,7 +1563,7 @@ void PlayerActions::actSetCardCounter(QList<CardItem *> selectedCards, int count
Expression exp(oldValue);
double parsed = exp.parse(counterValue);
// Clamp in double precision first to avoid UB, then cast
int number = static_cast<int>(qBound(0.0, parsed, static_cast<double>(MAX_COUNTERS_ON_CARD)));
int number = static_cast<int>(qBound(0.0, parsed, static_cast<double>(MAX_COUNTER_VALUE)));
auto *cmd = new Command_SetCardCounter;
cmd->set_zone(card->getZone()->getName().toStdString());
@ -1593,7 +1593,7 @@ void PlayerActions::actIncrementAllCardCounters(QList<CardItem *> cardsToUpdate)
counterIterator.next();
int counterId = counterIterator.key();
int currentValue = counterIterator.value();
if (currentValue >= MAX_COUNTERS_ON_CARD) {
if (currentValue >= MAX_COUNTER_VALUE) {
continue;
}