summaryrefslogtreecommitdiff
path: root/src/KingSystem/GameData/gdtFlag.cpp
blob: 58e222a6df76867254491a4d04f067d90d75223b (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "KingSystem/GameData/gdtFlag.h"

namespace ksys::gdt {

FlagProperties::FlagProperties() = default;

FlagProperties::~FlagProperties() = default;

void FlagProperties::setIsProgramReadable(bool on) {
    set(PropertyFlag::IsProgramReadable, on);
}

bool FlagProperties::isProgramReadable() const {
    return get(PropertyFlag::IsProgramReadable);
}

void FlagProperties::setIsProgramWritable(bool on) {
    set(PropertyFlag::IsProgramWritable, on);
}

bool FlagProperties::isProgramWritable() const {
    return get(PropertyFlag::IsProgramWritable);
}

void FlagProperties::setIsSave(bool on) {
    set(PropertyFlag::IsSave, on);
}

bool FlagProperties::isSave() const {
    return get(PropertyFlag::IsSave);
}

void FlagProperties::setIsOneTrigger(bool on) {
    set(PropertyFlag::IsOneTrigger, on);
}

bool FlagProperties::isOneTrigger() const {
    return get(PropertyFlag::IsOneTrigger);
}

void FlagProperties::setIsEventAssociated(bool on) {
    set(PropertyFlag::IsEventAssociated, on);
}

bool FlagProperties::isEventAssociated() const {
    return get(PropertyFlag::IsEventAssociated);
}

void FlagProperties::setResetType(ResetType type) {
    mFlags = (mFlags & 0x1F) | 0x20 * u32(type);
}

ResetType FlagProperties::getResetType() const {
    return ResetType(mFlags >> 5);
}

FlagProperties& FlagProperties::operator=(const FlagProperties& other) {
    setIsProgramReadable(other.isProgramReadable());
    setIsProgramWritable(other.isProgramWritable());
    setIsSave(other.isSave());
    setIsOneTrigger(other.isOneTrigger());
    setIsEventAssociated(other.isEventAssociated());
    setResetType(other.getResetType());
    return *this;
}

s32 FlagBase::clampValue_(const s32& min_value, s32* value, const s32& max_value) {
    if (*value < min_value) {
        *value = min_value;
        return -1;
    }
    if (*value > max_value) {
        *value = max_value;
        return 1;
    }
    return 0;
}

s32 FlagBase::clampValue_(const f32& min_value, f32* value, const f32& max_value) {
    if (*value < min_value) {
        *value = min_value;
        return -1;
    }
    if (*value > max_value) {
        *value = max_value;
        return 1;
    }
    return 0;
}

s32 FlagBase::clampValue_(const sead::SafeString&, sead::SafeString*, const sead::SafeString&) {
    return 0;
}

namespace {
template <typename T>
constexpr s32 clampVectorComponentWise(const T& min_value, T* value, const T& max_value) {
    s32 ret = 0;
    for (size_t i = 0; i < min_value.e.size(); ++i) {
        if (value->e[i] < min_value.e[i]) {
            value->e[i] = min_value.e[i];
            ret = -1;
        }
    }
    for (size_t i = 0; i < max_value.e.size(); ++i) {
        if (value->e[i] > max_value.e[i]) {
            value->e[i] = max_value.e[i];
            ret = 1;
        }
    }
    return ret;
}
}  // namespace

s32 FlagBase::clampValue_(const sead::Vector2f& min_value, sead::Vector2f* value,
                          const sead::Vector2f& max_value) {
    return clampVectorComponentWise(min_value, value, max_value);
}

s32 FlagBase::clampValue_(const sead::Vector3f& min_value, sead::Vector3f* value,
                          const sead::Vector3f& max_value) {
    return clampVectorComponentWise(min_value, value, max_value);
}

s32 FlagBase::clampValue_(const sead::Vector4f& min_value, sead::Vector4f* value,
                          const sead::Vector4f& max_value) {
    return clampVectorComponentWise(min_value, value, max_value);
}

bool FlagBase::isBoolean_() const {
    const auto type = getType();
    return s32(type) == FlagType::Bool || s32(type) == FlagType::BoolArray;
}

u32 FlagBase::getCategoryForBool_(const void* data) const {
    if (!isBoolean_())
        return 0;
    return *static_cast<const u8*>(data) >> 1;
}

void FlagBase::setCategoryForBool_(void* data, u32 category) const {
    if (!isBoolean_())
        return;

    auto* ptr = static_cast<u8*>(data);
    *ptr = (*ptr & 1) | 2 * category;
}

}  // namespace ksys::gdt