2019-11-04 16:58:26 +00:00
|
|
|
#ifndef CREATURELIB_TYPELIBRARY_HPP
|
|
|
|
#define CREATURELIB_TYPELIBRARY_HPP
|
|
|
|
|
2021-10-30 12:24:38 +00:00
|
|
|
#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
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
namespace CreatureLib::Library {
|
2019-11-04 16:58:26 +00:00
|
|
|
class TypeLibrary {
|
2021-10-30 12:24:38 +00:00
|
|
|
ArbUt::Dictionary<ArbUt::StringView, u8> _types;
|
2020-05-26 16:31:06 +00:00
|
|
|
ArbUt::List<ArbUt::List<float>> _effectiveness;
|
2019-11-28 11:55:22 +00:00
|
|
|
|
2019-11-04 16:58:26 +00:00
|
|
|
public:
|
2021-10-30 12:24:38 +00:00
|
|
|
TypeLibrary(size_t initialCapacity = 20) : _types(ArbUt::Dictionary<ArbUt::StringView, u8>(initialCapacity)) {}
|
2019-12-31 09:48:52 +00:00
|
|
|
|
2020-08-08 10:54:28 +00:00
|
|
|
inline uint8_t GetTypeId(const ArbUt::StringView& key) const { return _types.Get(key); }
|
2021-10-30 12:24:38 +00:00
|
|
|
[[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
|
|
|
}
|
2020-06-20 16:50:55 +00:00
|
|
|
}
|
2021-10-30 12:24:38 +00:00
|
|
|
[[nodiscard]] inline float GetEffectiveness(uint8_t attacking, const std::vector<u8>& defensive) const {
|
2020-06-20 16:50:55 +00:00
|
|
|
return std::accumulate(defensive.begin(), defensive.end(), (float)1,
|
|
|
|
[this, attacking](float init, uint8_t defense) {
|
|
|
|
return init * GetSingleEffectiveness(attacking, defense);
|
|
|
|
});
|
|
|
|
}
|
2021-10-30 12:24:38 +00:00
|
|
|
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);
|
2021-10-30 12:24:38 +00:00
|
|
|
void SetEffectiveness(uint8_t attacking, u8 defensive, float effectiveness);
|
2019-11-04 16:58:26 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
#endif // CREATURELIB_TYPELIBRARY_HPP
|