CreatureLib/src/Library/TypeLibrary.hpp

44 lines
1.8 KiB
C++
Raw Normal View History

2019-11-04 16:58:26 +00:00
#ifndef CREATURELIB_TYPELIBRARY_HPP
#define CREATURELIB_TYPELIBRARY_HPP
#include <Arbutils/Collections/Dictionary.hpp>
#include <Arbutils/Collections/List.hpp>
#include <Arbutils/Collections/StringViewDictionary.hpp>
#include <Arbutils/String/StringView.hpp>
#include <numeric>
#include "../Defines.hpp"
2020-07-31 12:17:38 +00:00
#include "Exceptions/CreatureException.hpp"
2019-11-04 16:58:26 +00:00
namespace CreatureLib::Library {
2019-11-04 16:58:26 +00:00
class TypeLibrary {
ArbUt::StringViewDictionary<u8> _types;
2020-05-26 16:31:06 +00:00
ArbUt::List<ArbUt::List<float>> _effectiveness;
2019-11-04 16:58:26 +00:00
public:
TypeLibrary(size_t initialCapacity = 20) : _types(ArbUt::StringViewDictionary<u8>(initialCapacity)) {}
inline u8 GetTypeId(const ArbUt::StringView& key) const { return _types.Get(key); }
inline u8 GetTypeIdByHash(u32 keyHash) const { return _types.GetFromHash(keyHash); }
[[nodiscard]] inline float GetSingleEffectiveness(u8 attacking, u8 defensive) const {
2020-07-31 12:17:38 +00:00
try {
return _effectiveness[attacking][defensive];
} catch (const std::exception& e) {
THROW("Unknown type indices were requested for effectiveness: ", (u32)attacking, " and ",
(u32)defensive);
2020-07-31 12:17:38 +00:00
}
}
[[nodiscard]] inline float GetEffectiveness(u8 attacking, const std::vector<u8>& defensive) const {
return std::accumulate(defensive.begin(), defensive.end(), (float)1,
[this, attacking](float init, u8 defense) {
return init * GetSingleEffectiveness(attacking, defense);
});
}
const ArbUt::StringView& GetTypeName(u8 type) const;
2019-11-04 16:58:26 +00:00
u8 RegisterType(const ArbUt::StringView& typeName);
void SetEffectiveness(u8 attacking, u8 defensive, float effectiveness);
2019-11-04 16:58:26 +00:00
};
}
#endif // CREATURELIB_TYPELIBRARY_HPP