Skip to content

Commit

Permalink
refactor(mechanics): Move random from ConditionSet to on-demand auto-…
Browse files Browse the repository at this point in the history
…conditions (endless-sky#10836)
  • Loading branch information
petervdmeer authored Dec 6, 2024
1 parent 0ed2038 commit a672e46
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
5 changes: 1 addition & 4 deletions source/ConditionSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ this program. If not, see <https://www.gnu.org/licenses/>.
#include "DataNode.h"
#include "DataWriter.h"
#include "Logger.h"
#include "Random.h"

#include <algorithm>
#include <cmath>
Expand Down Expand Up @@ -160,9 +159,7 @@ namespace {
for(const string &str : side)
{
int64_t value = 0;
if(str == "random")
value = Random::Int(100);
else if(DataNode::IsNumber(str))
if(DataNode::IsNumber(str))
value = static_cast<int64_t>(DataNode::Value(str));
else
{
Expand Down
8 changes: 8 additions & 0 deletions source/PlayerInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3952,6 +3952,14 @@ void PlayerInfo::RegisterDerivedConditions()
return true;
});

// A condition for returning a random integer in the range [0, 100).
auto &&randomProvider = conditions.GetProviderNamed("random");
auto randomFun = [](const string &name) -> int64_t
{
return Random::Int(100);
};
randomProvider.SetGetFunction(randomFun);

// A condition for returning a random integer in the range [0, input). Input may be a number,
// or it may be the name of a condition. For example, "roll: 100" would roll a random
// integer in the range [0, 100), but if you had a condition "max roll" with a value of 100,
Expand Down

0 comments on commit a672e46

Please sign in to comment.