Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Toggle main menu visibility
Loading...
Searching...
No Matches
random.cpp
Go to the documentation of this file.
1
/*
2
* Copyright 2020 Twilight
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
16
25
26
#include "
./random.h
"
27
#include "
utility.h
"
28
#include <QQmlEngine>
29
30
// Meyers singleton: the static local is initialised once, on first call.
31
Random*
Random::inst
()
32
{
33
static
Random* _inst =
new
Random;
34
return
_inst;
35
}
36
37
float
Random::range
(
const
float
end)
const
38
{
39
return
rnd->bounded(end);
40
}
41
42
int
Random::rangeInclusive
(
const
int
start,
const
int
end)
const
43
{
44
// Guard degenerate/inverted ranges: treat them as the fixed value `start`
45
// rather than letting bounded() receive a non-positive span.
46
if
(start == end || start > end)
47
return
start;
48
49
// bounded(lo, hi) is half-open, so +1 makes the upper bound inclusive.
50
return
rnd->bounded(start, end+1);
51
}
52
53
int
Random::rangeExclusive
(
const
int
start,
const
int
end)
const
54
{
55
// Same degenerate-range guard as rangeInclusive().
56
if
(start == end || start > end)
57
return
start;
58
59
return
rnd->bounded(start, end);
60
}
61
62
bool
Random::chanceFailure
(
const
int
percent)
const
63
{
64
// Roll 0..100; failure when the roll exceeds the success threshold.
65
return
rangeInclusive
(0, 100) >= percent;
66
}
67
68
bool
Random::chanceFailure
(
const
float
percent)
const
69
{
70
return
range
(1.0f) >= percent;
71
}
72
73
bool
Random::chanceSuccess
(
const
int
percent)
const
74
{
75
// Roll 0..100; success when the roll is within the threshold.
76
return
rangeInclusive
(0, 100) <= percent;
77
}
78
79
bool
Random::chanceSuccess
(
const
float
percent)
const
80
{
81
return
range
(1.0f) <= percent;
82
}
83
84
bool
Random::flipCoin
()
const
85
{
86
return
chanceSuccess
(50);
87
}
88
89
bool
Random::flipCoinF
()
const
90
{
91
return
chanceSuccess
(0.5f);
92
}
93
94
void
Random::qmlProtect
(
const
QQmlEngine*
const
engine)
const
95
{
96
Utility::qmlProtectUtil
(
this
, engine);
97
}
98
99
void
Random::qmlRegister()
const
100
{
101
// Idempotent: register the QML type at most once per process.
102
static
bool
registered =
false
;
103
if
(registered)
104
return
;
105
106
qmlRegisterUncreatableType<Random>(
"PSE.Common.Random"
, 1, 0,
"Random"
,
"Can't instantiate in QML"
);
107
registered =
true
;
108
}
109
110
Random::Random()
111
{
112
qmlRegister();
113
}
Random::chanceFailure
bool chanceFailure(const int percent) const
Did a percent chance fail?
Definition
random.cpp:62
Random::flipCoinF
bool flipCoinF() const
50/50 coin flip via the float path (chanceSuccess(0.5f)).
Definition
random.cpp:89
Random::chanceSuccess
bool chanceSuccess(const int percent) const
Did a percent chance succeed?
Definition
random.cpp:73
Random::qmlProtect
void qmlProtect(const QQmlEngine *const engine) const
Pin this object to C++ ownership so QML's GC never deletes it.
Definition
random.cpp:94
Random::flipCoin
bool flipCoin() const
50/50 coin flip via the integer path (chanceSuccess(50)).
Definition
random.cpp:84
Random::rangeInclusive
int rangeInclusive(const int start, const int end) const
Random integer in the closed interval [start, end].
Definition
random.cpp:42
Random::range
float range(const float end) const
Random float in the half-open interval [0.00, end).
Definition
random.cpp:37
Random::inst
static Random * inst()
< Convenience 50% coin flip (integer path), readable from QML.
Definition
random.cpp:31
Random::rangeExclusive
int rangeExclusive(const int start, const int end) const
Random integer in the half-open interval [start, end).
Definition
random.cpp:53
Utility::qmlProtectUtil
static void qmlProtectUtil(const QObject *const obj, const QQmlEngine *const engine)
Pin obj to C++ ownership so the QML engine never garbage-collects it.
Definition
utility.cpp:63
random.h
utility.h
projects
common
src
pse-common
random.cpp
Generated by
1.17.0