CreatureLib/src/Library/TypeLibrary.hpp

42 lines
1.7 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/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::Dictionary<ArbUt::StringView, 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::Dictionary<ArbUt::StringView, u8>(initialCapacity)) {}
inline uint8_t GetTypeId(const ArbUt::StringView& key) const { return _types.Get(key); }
[[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) {
2021-11-21 11:39:07 +00:00
THROW("Unknown type indices were requested for effectiveness: ", (uint32_t)attacking, " and ",
(uint32_t)defensive);
2020-07-31 12:17:38 +00:00
}
}
[[nodiscard]] inline float GetEffectiveness(uint8_t attacking, const std::vector<u8>& defensive) const {
return std::accumulate(defensive.begin(), defensive.end(), (float)1,
[this, attacking](float init, uint8_t defense) {
return init * GetSingleEffectiveness(attacking, defense);
});
}
const ArbUt::StringView& GetTypeName(u8 type) const;
2019-11-04 16:58:26 +00:00
2020-06-26 15:08:23 +00:00
uint8_t RegisterType(const ArbUt::StringView& typeName);
void SetEffectiveness(uint8_t attacking, u8 defensive, float effectiveness);
2019-11-04 16:58:26 +00:00
};
}
#endif // CREATURELIB_TYPELIBRARY_HPP