Replace void* for script parameter with EffectParameter class.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
1537a5f316
commit
e934e13950
|
@ -5,14 +5,15 @@ using namespace CreatureLib::Library;
|
||||||
export uint8_t CreatureLib_AttackData_Construct(AttackData*& out, const char* name, uint8_t type,
|
export uint8_t CreatureLib_AttackData_Construct(AttackData*& out, const char* name, uint8_t type,
|
||||||
AttackCategory category, uint8_t power, uint8_t accuracy,
|
AttackCategory category, uint8_t power, uint8_t accuracy,
|
||||||
uint8_t baseUsage, AttackTarget target, int8_t priority,
|
uint8_t baseUsage, AttackTarget target, int8_t priority,
|
||||||
float effectChance, const char* effectName, void* effectParameters[],
|
float effectChance, const char* effectName,
|
||||||
size_t effectParameterCount, const char* flags[], size_t flagsCount) {
|
EffectParameter* effectParameters[], size_t effectParameterCount,
|
||||||
|
const char* flags[], size_t flagsCount) {
|
||||||
Try({
|
Try({
|
||||||
std::unordered_set<uint32_t> conversedFlags(flagsCount);
|
std::unordered_set<uint32_t> conversedFlags(flagsCount);
|
||||||
for (size_t i = 0; i < flagsCount; i++) {
|
for (size_t i = 0; i < flagsCount; i++) {
|
||||||
conversedFlags.insert(ConstString::GetHash(flags[i]));
|
conversedFlags.insert(ConstString::GetHash(flags[i]));
|
||||||
}
|
}
|
||||||
Arbutils::Collections::List<void*> effectParameterList(effectParameterCount);
|
Arbutils::Collections::List<EffectParameter*> effectParameterList(effectParameterCount);
|
||||||
for (size_t i = 0; i < effectParameterCount; i++) {
|
for (size_t i = 0; i < effectParameterCount; i++) {
|
||||||
effectParameterList[i] = effectParameters[i];
|
effectParameterList[i] = effectParameters[i];
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,9 +3,7 @@
|
||||||
|
|
||||||
#include <Arbutils/Collections/List.hpp>
|
#include <Arbutils/Collections/List.hpp>
|
||||||
#include <Arbutils/ConstString.hpp>
|
#include <Arbutils/ConstString.hpp>
|
||||||
#include <string>
|
#include "../../Library/EffectParameter.hpp"
|
||||||
#include <utility>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
using ConstString = Arbutils::CaseInsensitiveConstString;
|
using ConstString = Arbutils::CaseInsensitiveConstString;
|
||||||
|
|
||||||
|
@ -25,7 +23,8 @@ namespace CreatureLib::Battling {
|
||||||
|
|
||||||
virtual const ConstString& GetName() const noexcept = 0;
|
virtual const ConstString& GetName() const noexcept = 0;
|
||||||
|
|
||||||
virtual void OnInitialize(const Arbutils::Collections::List<void*>& parameters){};
|
virtual void
|
||||||
|
OnInitialize(const Arbutils::Collections::List<CreatureLib::Library::EffectParameter*>& parameters){};
|
||||||
virtual void OnBeforeTurn(const BaseTurnChoice* choice){};
|
virtual void OnBeforeTurn(const BaseTurnChoice* choice){};
|
||||||
|
|
||||||
virtual void ChangeAttack(AttackTurnChoice* choice, ConstString* outAttack){};
|
virtual void ChangeAttack(AttackTurnChoice* choice, ConstString* outAttack){};
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
#include <Arbutils/Collections/List.hpp>
|
#include <Arbutils/Collections/List.hpp>
|
||||||
#include <Arbutils/ConstString.hpp>
|
#include <Arbutils/ConstString.hpp>
|
||||||
#include <any>
|
#include <any>
|
||||||
|
#include "../EffectParameter.hpp"
|
||||||
|
|
||||||
using namespace Arbutils::Collections;
|
using namespace Arbutils::Collections;
|
||||||
namespace CreatureLib::Library {
|
namespace CreatureLib::Library {
|
||||||
|
@ -11,17 +12,23 @@ namespace CreatureLib::Library {
|
||||||
private:
|
private:
|
||||||
float _chance;
|
float _chance;
|
||||||
Arbutils::CaseInsensitiveConstString _effectName;
|
Arbutils::CaseInsensitiveConstString _effectName;
|
||||||
List<void*> _parameters;
|
List<EffectParameter*> _parameters;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
SecondaryEffect() noexcept : _chance(0), _effectName(Arbutils::CaseInsensitiveConstString("")) {}
|
SecondaryEffect() noexcept : _chance(0), _effectName(Arbutils::CaseInsensitiveConstString("")) {}
|
||||||
SecondaryEffect(float chance, const Arbutils::CaseInsensitiveConstString& effectName,
|
SecondaryEffect(float chance, const Arbutils::CaseInsensitiveConstString& effectName,
|
||||||
const List<void*>& parameters) noexcept
|
const List<EffectParameter*>& parameters) noexcept
|
||||||
: _chance(chance), _effectName(effectName), _parameters(parameters) {}
|
: _chance(chance), _effectName(effectName), _parameters(parameters) {}
|
||||||
|
|
||||||
|
~SecondaryEffect() {
|
||||||
|
for (auto p : _parameters) {
|
||||||
|
delete p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
constexpr float GetChance() const noexcept { return _chance; }
|
constexpr float GetChance() const noexcept { return _chance; }
|
||||||
constexpr const Arbutils::CaseInsensitiveConstString& GetEffectName() const noexcept { return _effectName; }
|
constexpr const Arbutils::CaseInsensitiveConstString& GetEffectName() const noexcept { return _effectName; }
|
||||||
const List<void*>& GetParameters() const noexcept { return _parameters; }
|
const List<EffectParameter*>& GetParameters() const noexcept { return _parameters; }
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,53 @@
|
||||||
|
#ifndef CREATURELIB_EFFECTPARAMETER_HPP
|
||||||
|
#define CREATURELIB_EFFECTPARAMETER_HPP
|
||||||
|
#include <Arbutils/Enum.hpp>
|
||||||
|
#include <cstring>
|
||||||
|
#include "Exceptions/CreatureException.hpp"
|
||||||
|
|
||||||
|
namespace CreatureLib::Library {
|
||||||
|
ENUM(EffectParameterType, uint8_t, None, Bool, Int, String);
|
||||||
|
|
||||||
|
class EffectParameter {
|
||||||
|
private:
|
||||||
|
EffectParameterType _type = EffectParameterType::None;
|
||||||
|
void* _val;
|
||||||
|
|
||||||
|
public:
|
||||||
|
EffectParameter() : _type(EffectParameterType::None), _val(nullptr){};
|
||||||
|
EffectParameter(bool b) : _type(EffectParameterType::Bool), _val((void*)b){};
|
||||||
|
EffectParameter(int64_t i) : _type(EffectParameterType::Int), _val((void*)i){};
|
||||||
|
EffectParameter(const std::string& s) : _type(EffectParameterType::String), _val(new char[s.size() + 1]) {
|
||||||
|
strncpy((char*)_val, s.c_str(), s.size() + 1);
|
||||||
|
};
|
||||||
|
EffectParameter(const EffectParameter& other) = delete;
|
||||||
|
EffectParameter& operator=(const EffectParameter& other) = delete;
|
||||||
|
|
||||||
|
EffectParameterType GetType() const noexcept { return _type; }
|
||||||
|
bool AsBool() const {
|
||||||
|
if (_type != EffectParameterType::Bool) {
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << "Cast effect parameter to bool, but was " << EffectParameterTypeHelper::ToString(_type);
|
||||||
|
throw CreatureException(ss.str());
|
||||||
|
}
|
||||||
|
return (bool)_val;
|
||||||
|
}
|
||||||
|
int64_t AsInt() const {
|
||||||
|
if (_type != EffectParameterType::Int) {
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << "Cast effect parameter to int, but was " << EffectParameterTypeHelper::ToString(_type);
|
||||||
|
throw CreatureException(ss.str());
|
||||||
|
}
|
||||||
|
return reinterpret_cast<int64_t>(_val);
|
||||||
|
}
|
||||||
|
const char* AsString() const {
|
||||||
|
if (_type != EffectParameterType::String) {
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << "Cast effect parameter to string, but was " << EffectParameterTypeHelper::ToString(_type);
|
||||||
|
throw CreatureException(ss.str());
|
||||||
|
}
|
||||||
|
return (char*)_val;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // CREATURELIB_EFFECTPARAMETER_HPP
|
Loading…
Reference in New Issue