CreatureLib/src/Library/TypeLibrary.hpp

44 lines
1.8 KiB
C++

#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"
#include "Exceptions/CreatureException.hpp"
namespace CreatureLib::Library {
class TypeLibrary {
ArbUt::StringViewDictionary<u8> _types;
ArbUt::List<ArbUt::List<float>> _effectiveness;
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 {
try {
return _effectiveness[attacking][defensive];
} catch (const std::exception& e) {
THROW("Unknown type indices were requested for effectiveness: ", (u32)attacking, " and ",
(u32)defensive);
}
}
[[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;
u8 RegisterType(const ArbUt::StringView& typeName);
void SetEffectiveness(u8 attacking, u8 defensive, float effectiveness);
};
}
#endif // CREATURELIB_TYPELIBRARY_HPP