Replace void* for script parameter with EffectParameter class.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-04-10 17:54:45 +02:00
parent 1537a5f316
commit e934e13950
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
4 changed files with 70 additions and 10 deletions

View File

@ -5,14 +5,15 @@ using namespace CreatureLib::Library;
export uint8_t CreatureLib_AttackData_Construct(AttackData*& out, const char* name, uint8_t type,
AttackCategory category, uint8_t power, uint8_t accuracy,
uint8_t baseUsage, AttackTarget target, int8_t priority,
float effectChance, const char* effectName, void* effectParameters[],
size_t effectParameterCount, const char* flags[], size_t flagsCount) {
float effectChance, const char* effectName,
EffectParameter* effectParameters[], size_t effectParameterCount,
const char* flags[], size_t flagsCount) {
Try({
std::unordered_set<uint32_t> conversedFlags(flagsCount);
for (size_t i = 0; i < flagsCount; 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++) {
effectParameterList[i] = effectParameters[i];
}

View File

@ -3,9 +3,7 @@
#include <Arbutils/Collections/List.hpp>
#include <Arbutils/ConstString.hpp>
#include <string>
#include <utility>
#include <vector>
#include "../../Library/EffectParameter.hpp"
using ConstString = Arbutils::CaseInsensitiveConstString;
@ -25,7 +23,8 @@ namespace CreatureLib::Battling {
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 ChangeAttack(AttackTurnChoice* choice, ConstString* outAttack){};

View File

@ -4,6 +4,7 @@
#include <Arbutils/Collections/List.hpp>
#include <Arbutils/ConstString.hpp>
#include <any>
#include "../EffectParameter.hpp"
using namespace Arbutils::Collections;
namespace CreatureLib::Library {
@ -11,17 +12,23 @@ namespace CreatureLib::Library {
private:
float _chance;
Arbutils::CaseInsensitiveConstString _effectName;
List<void*> _parameters;
List<EffectParameter*> _parameters;
public:
SecondaryEffect() noexcept : _chance(0), _effectName(Arbutils::CaseInsensitiveConstString("")) {}
SecondaryEffect(float chance, const Arbutils::CaseInsensitiveConstString& effectName,
const List<void*>& parameters) noexcept
const List<EffectParameter*>& parameters) noexcept
: _chance(chance), _effectName(effectName), _parameters(parameters) {}
~SecondaryEffect() {
for (auto p : _parameters) {
delete p;
}
}
constexpr float GetChance() const noexcept { return _chance; }
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; }
};
}

View File

@ -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