CreatureLib/src/Library/TypeLibrary.hpp
Deukhoofd 98dacbccde
All checks were successful
continuous-integration/drone/push Build is passing
Use Arbutils exception Macros, instead of own ones.
Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
2020-08-17 12:18:01 +02:00

45 lines
1.9 KiB
C++

#ifndef CREATURELIB_TYPELIBRARY_HPP
#define CREATURELIB_TYPELIBRARY_HPP
#include <Arbutils/Collections/Dictionary.hpp>
#include <Arbutils/Collections/List.hpp>
#include <Arbutils/StringView.hpp>
#include <numeric>
#include <unordered_set>
#include <vector>
#include "Exceptions/CreatureException.hpp"
namespace CreatureLib::Library {
class TypeLibrary {
ArbUt::Dictionary<ArbUt::StringView, uint8_t> _types;
ArbUt::List<ArbUt::List<float>> _effectiveness;
public:
TypeLibrary(size_t initialCapacity = 20)
: _types(ArbUt::Dictionary<ArbUt::StringView, uint8_t>(initialCapacity)) {}
inline uint8_t GetTypeId(const ArbUt::StringView& key) const { return _types.Get(key); }
[[nodiscard]] inline float GetSingleEffectiveness(uint8_t attacking, uint8_t defensive) const {
try {
return _effectiveness[attacking][defensive];
} catch (const std::exception& e) {
THROW("Unknown type indices were requested for effectiveness: " << (uint32_t)attacking << " and "
<< (uint32_t)defensive);
}
}
[[nodiscard]] inline float GetEffectiveness(uint8_t attacking,
const std::unordered_set<uint8_t>& 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(uint8_t type) const;
uint8_t RegisterType(const ArbUt::StringView& typeName);
void SetEffectiveness(uint8_t attacking, uint8_t defensive, float effectiveness);
};
}
#endif // CREATURELIB_TYPELIBRARY_HPP