Move several classes from Core to Arbutils.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2020-02-26 13:51:16 +01:00
parent 428b318baf
commit 5e6572aca5
24 changed files with 51 additions and 384 deletions

View File

@@ -6,7 +6,7 @@
using namespace CreatureLib;
using namespace Battling;
bool ___ChoiceOrderFunc(const BaseTurnChoice* a, const BaseTurnChoice* b, Core::Random& rand) {
bool ___ChoiceOrderFunc(const BaseTurnChoice* a, const BaseTurnChoice* b, Arbutils::Random& rand) {
auto aKind = a->GetKind();
auto bKind = b->GetKind();
if (aKind != bKind)
@@ -26,7 +26,7 @@ bool ___ChoiceOrderFunc(const BaseTurnChoice* a, const BaseTurnChoice* b, Core::
return randomValue == 0;
}
void TurnOrdering::OrderChoices(std::vector<BaseTurnChoice*>& vec, Core::Random& rand) {
void TurnOrdering::OrderChoices(std::vector<BaseTurnChoice*>& vec, Arbutils::Random& rand) {
auto comp = [&](const BaseTurnChoice* a, const BaseTurnChoice* b) -> bool {
return ___ChoiceOrderFunc(a, b, rand);
};

View File

@@ -1,14 +1,14 @@
#ifndef CREATURELIB_TURNORDERING_HPP
#define CREATURELIB_TURNORDERING_HPP
#include <Arbutils/Random.hpp>
#include <vector>
#include "../../Core/Random.hpp"
#include "../TurnChoices/BaseTurnChoice.hpp"
namespace CreatureLib::Battling {
class TurnOrdering {
public:
static void OrderChoices(std::vector<BaseTurnChoice*>& vec, CreatureLib::Core::Random& rand);
static void OrderChoices(std::vector<BaseTurnChoice*>& vec, Arbutils::Random& rand);
};
}

View File

@@ -1,7 +1,7 @@
#ifndef CREATURELIB_BATTLERANDOM_HPP
#define CREATURELIB_BATTLERANDOM_HPP
#include "../../Core/Random.hpp"
#include <Arbutils/Random.hpp>
namespace CreatureLib::Battling {
class ExecutingAttack;
@@ -9,7 +9,7 @@ namespace CreatureLib::Battling {
class BattleRandom {
private:
Core::Random _random;
Arbutils::Random _random;
public:
BattleRandom() : _random() {}
@@ -19,7 +19,7 @@ namespace CreatureLib::Battling {
int32_t Get() { return _random.Get(); }
int32_t Get(int32_t max) { return _random.Get(max); }
int32_t Get(int32_t min, int32_t max) { return _random.Get(min, max); }
Core::Random& GetRNG() { return _random; }
Arbutils::Random& GetRNG() { return _random; }
};
}

View File

@@ -30,7 +30,7 @@ CreateCreature* CreateCreature::WithAttack(const std::string& attackName, Attack
}
Creature* CreateCreature::Create() {
auto rand = Core::Random();
auto rand = Arbutils::Random();
auto species = this->_library->GetSpeciesLibrary()->Get(this->_species.c_str());
auto variant = species->GetVariant(this->_variant);
int8_t talent;

View File

@@ -1,8 +1,8 @@
#ifndef CREATURELIB_SCRIPTRESOLVER_HPP
#define CREATURELIB_SCRIPTRESOLVER_HPP
#include <Arbutils/Enum.hpp>
#include <string>
#include "../../Core/Enum.hpp"
#include "Script.hpp"
#include "ScriptCategory.hpp"

View File

@@ -1,73 +0,0 @@
#include <algorithm>
#include <sstream>
#include <vector>
#include "Exceptions/NotReachableException.hpp"
#include "MacroUtils.hpp"
#define ENUM_CASE(x, name) \
case name::x: \
return #x;
#
#define ENUM_PARSE_CASE(x, name) \
case ConstHash(#x): \
return name::x;
#
#define ENUM_TRY_PARSE_CASE(x, name) \
case ConstHash(#x): \
out = name::x; \
return true;
#
#define ENUM_PARSE_CASE_INSENSITIVE(x, name) \
case ConstHashCI(#x): \
return name::x;
#
#define ENUM_TRY_PARSE_CASE_INSENSITIVE(x, name) \
case ConstHashCI(#x): \
out = name::x; \
return true;
#
#define ARRAY_NAME(x, name) name::x,
#define ENUM(name, type, values...) \
enum class name : type { values }; \
class name##Helper { \
inline static uint32_t constexpr ConstHash(char const* input) { \
return *input ? static_cast<uint32_t>(*input) + 33 * ConstHash(input + 1) : 5381; \
} \
inline static constexpr char charToLower(const char c) { \
return (c >= 'A' && c <= 'Z') ? c + ('a' - 'A') : c; \
} \
inline static uint32_t constexpr ConstHashCI(char const* input) { \
return charToLower(*input) ? static_cast<uint32_t>(charToLower(*input)) + 33 * ConstHashCI(input + 1) \
: 5381; \
} \
\
public: \
constexpr static const char* ToString(name value) { \
switch (value) { FOR_EACH(ENUM_CASE, name, values) } \
throw NotReachableException(); \
} \
constexpr static name Parse(const char* input, bool caseInsensitive = false) { \
if (caseInsensitive) \
return ParseCaseInsensitive(input); \
switch (ConstHash(input)) { FOR_EACH(ENUM_PARSE_CASE, name, values) } \
throw CreatureException("Invalid AttackCategory string."); \
} \
constexpr static bool TryParse(const char* input, name& out, bool caseInsensitive = false) { \
if (caseInsensitive) \
return TryParseCaseInsensitive(input, out); \
switch (ConstHash(input)) { FOR_EACH(ENUM_TRY_PARSE_CASE, name, values) } \
return false; \
} \
static std::vector<name> GetValues() { return {FOR_EACH(ARRAY_NAME, name, values)}; } \
\
private: \
static name ParseCaseInsensitive(const char* input) { \
switch (ConstHashCI(input)) { FOR_EACH(ENUM_PARSE_CASE_INSENSITIVE, name, values) } \
throw CreatureException("Invalid AttackCategory string."); \
} \
static bool TryParseCaseInsensitive(const char* input, name& out) { \
switch (ConstHashCI(input)) { FOR_EACH(ENUM_TRY_PARSE_CASE_INSENSITIVE, name, values) } \
return false; \
} \
};

View File

@@ -1,43 +0,0 @@
#define FE_0(FUNC, arg)
#define FE_1(FUNC, arg, X) FUNC(X, arg)
#define FE_2(FUNC, arg, X, ...) FUNC(X, arg) FE_1(FUNC, arg, __VA_ARGS__)
#define FE_3(FUNC, arg, X, ...) FUNC(X, arg) FE_2(FUNC, arg, __VA_ARGS__)
#define FE_4(FUNC, arg, X, ...) FUNC(X, arg) FE_3(FUNC, arg, __VA_ARGS__)
#define FE_5(FUNC, arg, X, ...) FUNC(X, arg) FE_4(FUNC, arg, __VA_ARGS__)
#define FE_6(FUNC, arg, X, ...) FUNC(X, arg) FE_5(FUNC, arg, __VA_ARGS__)
#define FE_7(FUNC, arg, X, ...) FUNC(X, arg) FE_6(FUNC, arg, __VA_ARGS__)
#define FE_8(FUNC, arg, X, ...) FUNC(X, arg) FE_7(FUNC, arg, __VA_ARGS__)
#define FE_9(FUNC, arg, X, ...) FUNC(X, arg) FE_8(FUNC, arg, __VA_ARGS__)
#define FE_10(FUNC, arg, X, ...) FUNC(X, arg) FE_9(FUNC, arg, __VA_ARGS__)
#define FE_11(FUNC, arg, X, ...) FUNC(X, arg) FE_10(FUNC, arg, __VA_ARGS__)
#define FE_12(FUNC, arg, X, ...) FUNC(X, arg) FE_11(FUNC, arg, __VA_ARGS__)
#define FE_13(FUNC, arg, X, ...) FUNC(X, arg) FE_12(FUNC, arg, __VA_ARGS__)
#define FE_14(FUNC, arg, X, ...) FUNC(X, arg) FE_13(FUNC, arg, __VA_ARGS__)
#define FE_15(FUNC, arg, X, ...) FUNC(X, arg) FE_14(FUNC, arg, __VA_ARGS__)
#define FE_16(FUNC, arg, X, ...) FUNC(X, arg) FE_15(FUNC, arg, __VA_ARGS__)
#define FE_17(FUNC, arg, X, ...) FUNC(X, arg) FE_16(FUNC, arg, __VA_ARGS__)
#define FE_18(FUNC, arg, X, ...) FUNC(X, arg) FE_17(FUNC, arg, __VA_ARGS__)
#define FE_19(FUNC, arg, X, ...) FUNC(X, arg) FE_18(FUNC, arg, __VA_ARGS__)
#define FE_20(FUNC, arg, X, ...) FUNC(X, arg) FE_19(FUNC, arg, __VA_ARGS__)
#define FE_21(FUNC, arg, X, ...) FUNC(X, arg) FE_20(FUNC, arg, __VA_ARGS__)
#define FE_22(FUNC, arg, X, ...) FUNC(X, arg) FE_21(FUNC, arg, __VA_ARGS__)
#define FE_23(FUNC, arg, X, ...) FUNC(X, arg) FE_22(FUNC, arg, __VA_ARGS__)
#define FE_24(FUNC, arg, X, ...) FUNC(X, arg) FE_23(FUNC, arg, __VA_ARGS__)
#define FE_25(FUNC, arg, X, ...) FUNC(X, arg) FE_24(FUNC, arg, __VA_ARGS__)
#define FE_26(FUNC, arg, X, ...) FUNC(X, arg) FE_25(FUNC, arg, __VA_ARGS__)
#define FE_27(FUNC, arg, X, ...) FUNC(X, arg) FE_26(FUNC, arg, __VA_ARGS__)
#define FE_28(FUNC, arg, X, ...) FUNC(X, arg) FE_27(FUNC, arg, __VA_ARGS__)
#define FE_29(FUNC, arg, X, ...) FUNC(X, arg) FE_28(FUNC, arg, __VA_ARGS__)
#define FE_30(FUNC, arg, X, ...) FUNC(X, arg) FE_29(FUNC, arg, __VA_ARGS__)
#define FE_31(FUNC, arg, X, ...) FUNC(X, arg) FE_30(FUNC, arg, __VA_ARGS__)
#define FE_32(FUNC, arg, X, ...) FUNC(X, arg) FE_31(FUNC, arg, __VA_ARGS__)
#define FE_33(FUNC, arg, X, ...) THE_FOREACH_MACRO_CURRENTLY_ONLY_SUPPORTS_UP_TO_32_VALUES FE_32(FUNC, arg, __VA_ARGS__)
#define GET_MACRO(_0, _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, NAME, ...) \
NAME
#define FOR_EACH(action, arg, ...) \
GET_MACRO(_0, __VA_ARGS__, FE_33, FE_32, FE_31, FE_30, FE_29, FE_28, FE_27, FE_26, FE_25, FE_24, FE_23, FE_22, \
FE_21, FE_20, FE_19, FE_18, FE_17, FE_16, FE_15, FE_14, FE_13, FE_12, FE_11, FE_10, FE_9, FE_8, FE_7, \
FE_6, FE_5, FE_4, FE_3, FE_2, FE_1, FE_0) \
(action, arg, __VA_ARGS__)

View File

@@ -1,26 +0,0 @@
#include "Random.hpp"
#include <limits>
// Seed parameterless constructor with current milliseconds since epoch.
CreatureLib::Core::Random::Random()
: _rng(std::mt19937(duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count())) {}
float CreatureLib::Core::Random::GetFloat() {
return static_cast<float>(_rng()) / (static_cast<float>(std::mt19937::max() - std::mt19937::min()) - 0.5f);
}
double CreatureLib::Core::Random::GetDouble() {
return static_cast<double>(_rng()) / ((static_cast<double>(std::mt19937::max()) - std::mt19937::min()) - 0.5);
}
int32_t CreatureLib::Core::Random::Get() {
return static_cast<int32_t>(GetDouble() * static_cast<float>(std::numeric_limits<int32_t>::max()));
}
int32_t CreatureLib::Core::Random::Get(int32_t max) {
return static_cast<int32_t>(GetDouble() * static_cast<float>(max));
}
int32_t CreatureLib::Core::Random::Get(int32_t min, int32_t max) {
return static_cast<int32_t>(GetDouble() * static_cast<float>(max - min) + min);
}

View File

@@ -1,24 +0,0 @@
#ifndef CREATURELIB_RANDOM_HPP
#define CREATURELIB_RANDOM_HPP
#include <chrono>
#include <cstdint>
#include <random>
using namespace std::chrono;
namespace CreatureLib::Core {
class Random {
private:
std::mt19937 _rng;
public:
Random();
explicit Random(int32_t seed) : _rng(seed){};
[[nodiscard]] float GetFloat();
[[nodiscard]] double GetDouble();
[[nodiscard]] int32_t Get();
[[nodiscard]] int32_t Get(int32_t max);
[[nodiscard]] int32_t Get(int32_t min, int32_t max);
};
}
#endif // CREATURELIB_RANDOM_HPP

View File

@@ -1,8 +1,8 @@
#ifndef CREATURELIB_STATISTIC_HPP
#define CREATURELIB_STATISTIC_HPP
#include <Arbutils/Enum.hpp>
#include <cstdint>
#include "Enum.hpp"
namespace CreatureLib::Core {
ENUM(Statistic, uint8_t, Health, PhysicalAttack, PhysicalDefense, MagicalAttack, MagicalDefense, Speed)

View File

@@ -1,8 +1,8 @@
#ifndef CREATURELIB_ATTACKCATEGORY_HPP
#define CREATURELIB_ATTACKCATEGORY_HPP
#include <Arbutils/Enum.hpp>
#include <cstdint>
#include "../../Core/Enum.hpp"
namespace CreatureLib::Library {
ENUM(AttackCategory, uint8_t, Physical, Magical, Status)

View File

@@ -1,8 +1,8 @@
#ifndef CREATURELIB_ATTACKTARGET_HPP
#define CREATURELIB_ATTACKTARGET_HPP
#include <Arbutils/Enum.hpp>
#include <cstdint>
#include "../../Core/Enum.hpp"
namespace CreatureLib::Library {
ENUM(AttackTarget, uint8_t, Adjacent, AdjacentAlly, AdjacentAllySelf, AdjacentOpponent,

View File

@@ -40,7 +40,7 @@ void CreatureSpecies::SetVariant(const std::string& name, const SpeciesVariant*
_variants[key] = variant;
}
Gender CreatureSpecies::GetRandomGender(CreatureLib::Core::Random& rand) const {
Gender CreatureSpecies::GetRandomGender(Arbutils::Random& rand) const {
// TODO: Genderless creatures
auto val = rand.GetDouble();
if (val >= this->_genderRate)

View File

@@ -39,7 +39,7 @@ namespace CreatureLib::Library {
[[nodiscard]] bool HasVariant(const std::string& key) const;
[[nodiscard]] bool TryGetVariant(const std::string& name, const SpeciesVariant*& out) const;
[[nodiscard]] const SpeciesVariant* GetVariant(const std::string& key) const;
[[nodiscard]] Gender GetRandomGender(Core::Random& rand) const;
[[nodiscard]] Gender GetRandomGender(Arbutils::Random& rand) const;
[[nodiscard]] const std::string& GetName() const;
void SetVariant(const std::string& name, const SpeciesVariant* variant);

View File

@@ -36,7 +36,7 @@ int8_t CreatureLib::Library::SpeciesVariant::GetTalentIndex(const std::string& t
throw CreatureException("The given talent is not a valid talent for this creature.");
}
int8_t CreatureLib::Library::SpeciesVariant::GetRandomTalent(CreatureLib::Core::Random* rand) const {
int8_t CreatureLib::Library::SpeciesVariant::GetRandomTalent(Arbutils::Random* rand) const {
return rand->Get(_talents.size());
}

View File

@@ -1,9 +1,9 @@
#ifndef CREATURELIB_SPECIESVARIANT_HPP
#define CREATURELIB_SPECIESVARIANT_HPP
#include <Arbutils/Random.hpp>
#include <string>
#include <vector>
#include "../../Core/Random.hpp"
#include "../../Core/StatisticSet.hpp"
#include "CreatureMoves.hpp"
#include "LearnableAttacks.hpp"
@@ -46,7 +46,7 @@ namespace CreatureLib::Library {
[[nodiscard]] const std::string& GetTalent(int32_t index) const;
[[nodiscard]] const LearnableAttacks* GetLearnableAttacks() const;
[[nodiscard]] int8_t GetTalentIndex(const std::string& talent) const;
[[nodiscard]] int8_t GetRandomTalent(Core::Random* rand) const;
[[nodiscard]] int8_t GetRandomTalent(Arbutils::Random* rand) const;
[[nodiscard]] inline const std::vector<std::string>& GetTalents() const { return _talents; }
[[nodiscard]] inline const std::vector<std::string>& GetSecretTalents() const { return _secretTalents; }
};

View File

@@ -1,8 +1,8 @@
#ifndef CREATURELIB_GENDER_HPP
#define CREATURELIB_GENDER_HPP
#include <Arbutils/Enum.hpp>
#include <cstdint>
#include "../Core/Enum.hpp"
namespace CreatureLib::Library {
/*!

View File

@@ -1,8 +1,8 @@
#ifndef CREATURELIB_BATTLEITEMCATEGORY_HPP
#define CREATURELIB_BATTLEITEMCATEGORY_HPP
#include <Arbutils/Enum.hpp>
#include <cstdint>
#include "../../Core/Enum.hpp"
namespace CreatureLib::Library {
ENUM(BattleItemCategory, uint8_t, None, Healing, StatusHealing, CaptureDevice, MiscBattleItem)

View File

@@ -1,8 +1,8 @@
#ifndef CREATURELIB_ITEMCATEGORY_HPP
#define CREATURELIB_ITEMCATEGORY_HPP
#include <Arbutils/Enum.hpp>
#include <cstdint>
#include "../../Core/Enum.hpp"
namespace CreatureLib::Library {
ENUM(ItemCategory, uint8_t, MiscItem, CaptureDevice, Medicine, Berry, MoveLearner, VariantChanger, KeyItem, Mail)