summaryrefslogtreecommitdiff
path: root/src/toBeSorted/counters/counter.cpp
blob: 5894c3510f5ac5c2dd3a0848abd39320e69fba30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "toBeSorted/counters/counter.h"

#include "d/flag/itemflag_manager.h"

s32 Counter::checkedAdd(s32 num) {
    s32 uncommitted = getUncommittedValue();
    s32 max = getMax();
    s32 result = uncommitted + num;
    if (result < 0) {
        setValue(0);
    } else if (result < max) {
        setValue(result);
    } else {
        setValue(max);
    }

    if (result < 0) {
        return result;
    }

    return result <= max ? 0 : (result - max);
}

u16 Counter::getCommittedValue() const {
    return ItemflagManager::sInstance->getItemCounterOrFlag(counterId);
}

u16 Counter::getUncommittedValue() const {
    return ItemflagManager::sInstance->getUncommittedItemValue(counterId);
}

void Counter::setValue(u16 num) {
    ItemflagManager::sInstance->setItemFlagOrCounterToValue(counterId, num);
}