Merge pull request #260 from lioncash/array2

CPlayerState: Make g_typeNameMap a compile-time array
This commit is contained in:
Phillip Stephens 2020-03-23 23:27:19 -07:00 committed by GitHub
commit dc6954491b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 56 deletions

View File

@ -390,7 +390,8 @@ void CPlayerState::InitializeScanTimes() {
u32 CPlayerState::GetPowerUpMaxValue(EItemType type) { return PowerUpMaxValues[size_t(type)]; }
const std::unordered_map<std::string_view, CPlayerState::EItemType> CPlayerState::g_TypeNameMap = {
CPlayerState::EItemType CPlayerState::ItemNameToType(std::string_view name) {
static constexpr std::array<std::pair<std::string_view, EItemType>, 46> typeNameMap{{
{"powerbeam"sv, EItemType::PowerBeam},
{"icebeam"sv, EItemType::IceBeam},
{"wavebeam"sv, EItemType::WaveBeam},
@ -437,15 +438,18 @@ const std::unordered_map<std::string_view, CPlayerState::EItemType> CPlayerState
{"world"sv, EItemType::World},
{"spirit"sv, EItemType::Spirit},
{"newborn"sv, EItemType::Newborn},
};
}};
CPlayerState::EItemType CPlayerState::ItemNameToType(std::string_view name) {
std::string lowName = name.data();
std::string lowName{name};
athena::utility::tolower(lowName);
if (g_TypeNameMap.find(lowName) == g_TypeNameMap.end())
return EItemType::Invalid;
return g_TypeNameMap.find(lowName)->second;
const auto iter = std::find_if(typeNameMap.cbegin(), typeNameMap.cend(),
[&lowName](const auto& entry) { return entry.first == lowName; });
if (iter == typeNameMap.cend()) {
return EItemType::Invalid;
}
return iter->second;
}
} // namespace urde

View File

@ -1,7 +1,6 @@
#pragma once
#include <string_view>
#include <unordered_map>
#include <vector>
#include "Runtime/CStaticInterference.hpp"
@ -89,7 +88,6 @@ public:
enum class EBeamId : s32 { Invalid = -1, Power, Ice, Wave, Plasma, Phazon, Phazon2 = 27 };
private:
static const std::unordered_map<std::string_view, EItemType> g_TypeNameMap;
struct CPowerUp {
u32 x0_amount = 0;
u32 x4_capacity = 0;