From 07542ff33550c6b9aa57277e103f4a7cb4b8d726 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 14 Mar 2020 17:38:55 -0400 Subject: [PATCH 01/14] CHudThreatInterface: Make use of std::array where applicable Same behavior, but with stronger typing. --- Runtime/GuiSys/CHudThreatInterface.cpp | 39 +++++++++++++++----------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/Runtime/GuiSys/CHudThreatInterface.cpp b/Runtime/GuiSys/CHudThreatInterface.cpp index 1298fcabc..7a4235b19 100644 --- a/Runtime/GuiSys/CHudThreatInterface.cpp +++ b/Runtime/GuiSys/CHudThreatInterface.cpp @@ -1,5 +1,7 @@ #include "Runtime/GuiSys/CHudThreatInterface.hpp" +#include + #include "Runtime/GameGlobalObjects.hpp" #include "Runtime/GuiSys/CAuiEnergyBarT01.hpp" #include "Runtime/GuiSys/CGuiFrame.hpp" @@ -10,11 +12,14 @@ namespace urde { -static const CAuiEnergyBarT01::FCoordFunc CoordFuncs[] = {CHudThreatInterface::CombatThreatBarCoordFunc, nullptr, - CHudThreatInterface::XRayThreatBarCoordFunc, - CHudThreatInterface::ThermalThreatBarCoordFunc, nullptr}; +constexpr std::array CoordFuncs{ + CHudThreatInterface::CombatThreatBarCoordFunc, nullptr, CHudThreatInterface::XRayThreatBarCoordFunc, + CHudThreatInterface::ThermalThreatBarCoordFunc, nullptr, +}; -static const float IconTranslateRanges[] = {6.05f, 0.f, 0.f, 8.4f, 0.f}; +constexpr std::array IconTranslateRanges{ + 6.05f, 0.f, 0.f, 8.4f, 0.f, +}; CHudThreatInterface::CHudThreatInterface(CGuiFrame& selHud, EHudType hudType, float threatDist) : x4_hudType(hudType), x10_threatDist(threatDist) { @@ -42,7 +47,7 @@ CHudThreatInterface::CHudThreatInterface(CGuiFrame& selHud, EHudType hudType, fl x6c_energybart01_threatbar->SetFilledColor(g_tweakGuiColors->GetThreatBarFilled()); x6c_energybart01_threatbar->SetShadowColor(g_tweakGuiColors->GetThreatBarShadow()); x6c_energybart01_threatbar->SetEmptyColor(g_tweakGuiColors->GetThreatBarEmpty()); - x6c_energybart01_threatbar->SetCoordFunc(CoordFuncs[int(hudType)]); + x6c_energybart01_threatbar->SetCoordFunc(CoordFuncs[size_t(hudType)]); x6c_energybart01_threatbar->SetTesselation(hudType == EHudType::Combat ? 1.f : 0.1f); x6c_energybart01_threatbar->SetMaxEnergy(g_tweakGui->GetThreatRange()); x6c_energybart01_threatbar->SetFilledDrainSpeed(9999.f); @@ -133,7 +138,7 @@ void CHudThreatInterface::Update(float dt) { x5c_basewidget_threaticon->SetLocalTransform( x18_threatIconXf * zeus::CTransform::Translate(0.f, 0.f, std::max(0.f, maxThreatEnergy - x10_threatDist) * - IconTranslateRanges[int(x4_hudType)] / maxThreatEnergy)); + IconTranslateRanges[size_t(x4_hudType)] / maxThreatEnergy)); } if (x68_textpane_threatwarning) { @@ -203,28 +208,30 @@ void CHudThreatInterface::Update(float dt) { } std::pair CHudThreatInterface::CombatThreatBarCoordFunc(float t) { - float z = IconTranslateRanges[int(EHudType::Combat)] * t; + const float z = IconTranslateRanges[size_t(EHudType::Combat)] * t; return {zeus::CVector3f(-0.3f, 0.f, z), zeus::CVector3f(0.f, 0.f, z)}; } std::pair CHudThreatInterface::XRayThreatBarCoordFunc(float t) { - float theta = 0.8f * (t - 0.5f); - float x = -9.55f * std::cos(theta); - float z = 9.55f * std::sin(theta); + const float theta = 0.8f * (t - 0.5f); + const float x = -9.55f * std::cos(theta); + const float z = 9.55f * std::sin(theta); return {zeus::CVector3f(0.4f + x, 0.f, z), zeus::CVector3f(x, 0.f, z)}; } std::pair CHudThreatInterface::ThermalThreatBarCoordFunc(float t) { - float transRange = IconTranslateRanges[int(EHudType::Thermal)]; - float a = 0.08f * transRange; - float b = t * transRange; + const float transRange = IconTranslateRanges[size_t(EHudType::Thermal)]; + const float a = 0.08f * transRange; + const float b = t * transRange; + float c; - if (b < a) + if (b < a) { c = b / a; - else if (b < transRange - a) + } else if (b < transRange - a) { c = 1.f; - else + } else { c = 1.f - (b - (transRange - a)) / a; + } return {zeus::CVector3f(0.1f, 0.f, b), zeus::CVector3f(0.5f * c + 0.1f, 0.f, b)}; } From 9fafb24fb9be3cbb1499433a2bb5da94e2589b5f Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 14 Mar 2020 17:43:08 -0400 Subject: [PATCH 02/14] CHudVisorBeamMenu: Make use of std::array where applicable --- Runtime/GuiSys/CHudVisorBeamMenu.cpp | 88 ++++++++++++++++++---------- 1 file changed, 57 insertions(+), 31 deletions(-) diff --git a/Runtime/GuiSys/CHudVisorBeamMenu.cpp b/Runtime/GuiSys/CHudVisorBeamMenu.cpp index bc7f016d1..843dd9367 100644 --- a/Runtime/GuiSys/CHudVisorBeamMenu.cpp +++ b/Runtime/GuiSys/CHudVisorBeamMenu.cpp @@ -1,5 +1,7 @@ #include "Runtime/GuiSys/CHudVisorBeamMenu.hpp" +#include + #include "Runtime/CGameState.hpp" #include "Runtime/GameGlobalObjects.hpp" #include "Runtime/Audio/CSfxManager.hpp" @@ -10,22 +12,40 @@ namespace urde { -static const char* BaseMenuNames[] = {"BaseWidget_VisorMenu", "BaseWidget_BeamMenu"}; - -static const char* TextNames[] = {"TextPane_VisorMenu", "TextPane_BeamMenu"}; - -static const char* BaseTitleNames[] = {"basewidget_visormenutitle", "basewidget_beammenutitle"}; - -static const char* ModelNames[] = {"model_visor", "model_beam"}; - -static const char MenuItemOrders[2][4] = {{'1', '0', '3', '2'}, {'3', '2', '1', '0'}}; - -static const int MenuStringIdx[2][4] = { - {0, 2, 1, 3}, // Combat, XRay, Scan, Thermal - {4, 5, 6, 7} // Power, Ice, Wave, Plasma +constexpr std::array BaseMenuNames{ + "BaseWidget_VisorMenu", + "BaseWidget_BeamMenu", }; -static const u16 SelectionSfxs[] = {SFXui_select_visor, SFXui_select_beam}; +constexpr std::array TextNames{ + "TextPane_VisorMenu", + "TextPane_BeamMenu", +}; + +constexpr std::array BaseTitleNames{ + "basewidget_visormenutitle", + "basewidget_beammenutitle", +}; + +constexpr std::array ModelNames{ + "model_visor", + "model_beam", +}; + +constexpr std::array, 2> MenuItemOrders{{ + {'1', '0', '3', '2'}, + {'3', '2', '1', '0'}, +}}; + +constexpr std::array, 2> MenuStringIdx{{ + {0, 2, 1, 3}, // Combat, XRay, Scan, Thermal + {4, 5, 6, 7}, // Power, Ice, Wave, Plasma +}}; + +constexpr std::array SelectionSfxs{ + SFXui_select_visor, + SFXui_select_beam, +}; CHudVisorBeamMenu::CHudVisorBeamMenu(CGuiFrame& baseHud, EHudVisorBeamMenu type, const rstl::reserved_vector& enables) @@ -43,19 +63,23 @@ CHudVisorBeamMenu::CHudVisorBeamMenu(CGuiFrame& baseHud, EHudVisorBeamMenu type, else swappedType = x4_type; - x20_textpane_menu = static_cast(x0_baseHud.FindWidget(TextNames[int(swappedType)])); - x1c_basewidget_menutitle = x0_baseHud.FindWidget(BaseTitleNames[int(swappedType)]); - x18_basewidget_menu = x0_baseHud.FindWidget(BaseMenuNames[int(swappedType)]); + x20_textpane_menu = static_cast(x0_baseHud.FindWidget(TextNames[size_t(swappedType)])); + x1c_basewidget_menutitle = x0_baseHud.FindWidget(BaseTitleNames[size_t(swappedType)]); + x18_basewidget_menu = x0_baseHud.FindWidget(BaseMenuNames[size_t(swappedType)]); - x24_model_ghost = static_cast(x0_baseHud.FindWidget(fmt::format(fmt("{}ghost"), ModelNames[int(x4_type)]))); + x24_model_ghost = + static_cast(x0_baseHud.FindWidget(fmt::format(fmt("{}ghost"), ModelNames[size_t(x4_type)]))); x28_menuItems.resize(4); - for (int i = 0; i < 4; ++i) { + for (size_t i = 0; i < x28_menuItems.size(); i++) { + const auto modelName = ModelNames[size_t(x4_type)]; + const auto menuItemOrder = MenuItemOrders[size_t(x4_type)][i]; + SMenuItem& item = x28_menuItems[i]; - item.x0_model_loz = static_cast( - x0_baseHud.FindWidget(fmt::format(fmt("{}loz{}"), ModelNames[int(x4_type)], MenuItemOrders[int(x4_type)][i]))); - item.x4_model_icon = static_cast( - x0_baseHud.FindWidget(fmt::format(fmt("{}icon{}"), ModelNames[int(x4_type)], MenuItemOrders[int(x4_type)][i]))); + item.x0_model_loz = + static_cast(x0_baseHud.FindWidget(fmt::format(fmt("{}loz{}"), modelName, menuItemOrder))); + item.x4_model_icon = + static_cast(x0_baseHud.FindWidget(fmt::format(fmt("{}icon{}"), modelName, menuItemOrder))); item.xc_opacity = enables[i] ? 1.f : 0.f; } @@ -71,7 +95,8 @@ CHudVisorBeamMenu::CHudVisorBeamMenu(CGuiFrame& baseHud, EHudVisorBeamMenu type, titleColor.a() = 0.f; x1c_basewidget_menutitle->SetColor(titleColor); - x20_textpane_menu->TextSupport().SetText(g_MainStringTable->GetString(MenuStringIdx[int(x4_type)][x8_selectedItem])); + x20_textpane_menu->TextSupport().SetText( + g_MainStringTable->GetString(MenuStringIdx[size_t(x4_type)][x8_selectedItem])); for (int i = 0; i < 4; ++i) { SMenuItem& item = x28_menuItems[i]; @@ -130,9 +155,9 @@ void CHudVisorBeamMenu::Update(float dt, bool init) { else swappedType = x4_type; - x18_basewidget_menu = x0_baseHud.FindWidget(BaseMenuNames[int(swappedType)]); - x20_textpane_menu = static_cast(x0_baseHud.FindWidget(TextNames[int(swappedType)])); - x1c_basewidget_menutitle = x0_baseHud.FindWidget(BaseTitleNames[int(swappedType)]); + x18_basewidget_menu = x0_baseHud.FindWidget(BaseMenuNames[size_t(swappedType)]); + x20_textpane_menu = static_cast(x0_baseHud.FindWidget(TextNames[size_t(swappedType)])); + x1c_basewidget_menutitle = x0_baseHud.FindWidget(BaseTitleNames[size_t(swappedType)]); for (int i = 0; i < 4; ++i) { SMenuItem& item = x28_menuItems[i]; @@ -146,7 +171,7 @@ void CHudVisorBeamMenu::Update(float dt, bool init) { zeus::CColor activeColor = g_tweakGuiColors->GetVisorBeamMenuItemActive(); zeus::CColor inactiveColor = g_tweakGuiColors->GetVisorBeamMenuItemInactive(); zeus::CColor lozColor = g_tweakGuiColors->GetVisorBeamMenuLozColor(); - zeus::CColor tmpColors[4]; + std::array tmpColors; for (int i = 0; i < 4; ++i) { SMenuItem& item = x28_menuItems[i]; @@ -262,13 +287,14 @@ void CHudVisorBeamMenu::SetSelection(int selection, int pending, float interp) { return; if (pending != selection) { - if (x6c_animPhase != EAnimPhase::SelectFlash) - CSfxManager::SfxStart(SelectionSfxs[int(x4_type)], 1.f, 0.f, false, 0x7f, false, kInvalidAreaId); + if (x6c_animPhase != EAnimPhase::SelectFlash) { + CSfxManager::SfxStart(SelectionSfxs[size_t(x4_type)], 1.f, 0.f, false, 0x7f, false, kInvalidAreaId); + } x6c_animPhase = EAnimPhase::SelectFlash; } else if (interp < 1.f) { x6c_animPhase = EAnimPhase::Animate; x20_textpane_menu->TextSupport().SetText( - g_MainStringTable->GetString(MenuStringIdx[int(x4_type)][x8_selectedItem])); + g_MainStringTable->GetString(MenuStringIdx[size_t(x4_type)][x8_selectedItem])); x20_textpane_menu->TextSupport().SetTypeWriteEffectOptions(true, 0.1f, 16.f); } else { if (x6c_animPhase != EAnimPhase::Steady) From c4796ed09063ff8b1426950fe4a5bb034bb8b35d Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 14 Mar 2020 17:54:24 -0400 Subject: [PATCH 03/14] CHudMissileInterface: Make use of std::array where applicable --- Runtime/GuiSys/CHudMissileInterface.cpp | 41 +++++++++++++++---------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/Runtime/GuiSys/CHudMissileInterface.cpp b/Runtime/GuiSys/CHudMissileInterface.cpp index 920d95e28..36b900a22 100644 --- a/Runtime/GuiSys/CHudMissileInterface.cpp +++ b/Runtime/GuiSys/CHudMissileInterface.cpp @@ -1,5 +1,7 @@ #include "Runtime/GuiSys/CHudMissileInterface.hpp" +#include + #include "Runtime/CStateManager.hpp" #include "Runtime/GameGlobalObjects.hpp" #include "Runtime/GuiSys/CAuiEnergyBarT01.hpp" @@ -10,11 +12,14 @@ namespace urde { -static const CAuiEnergyBarT01::FCoordFunc CoordFuncs[] = {CHudMissileInterface::CombatMissileBarCoordFunc, nullptr, - CHudMissileInterface::XRayMissileBarCoordFunc, - CHudMissileInterface::ThermalMissileBarCoordFunc, nullptr}; +constexpr std::array CoordFuncs{ + CHudMissileInterface::CombatMissileBarCoordFunc, nullptr, CHudMissileInterface::XRayMissileBarCoordFunc, + CHudMissileInterface::ThermalMissileBarCoordFunc, nullptr, +}; -static const float IconTranslateRanges[] = {6.05f, 0.f, 0.f, 8.4f, 0.f}; +constexpr std::array IconTranslateRanges{ + 6.05f, 0.f, 0.f, 8.4f, 0.f, +}; CHudMissileInterface::CHudMissileInterface(CGuiFrame& selHud, int missileCapacity, int numMissiles, float chargeFactor, bool missilesActive, EHudType hudType, const CStateManager& mgr) @@ -45,7 +50,7 @@ CHudMissileInterface::CHudMissileInterface(CGuiFrame& selHud, int missileCapacit x64_energybart01_missilebar->SetEmptyColor(g_tweakGuiColors->GetMissileBarEmpty()); x64_energybart01_missilebar->SetFilledColor(g_tweakGuiColors->GetMissileBarFilled()); x64_energybart01_missilebar->SetShadowColor(g_tweakGuiColors->GetMissileBarShadow()); - x64_energybart01_missilebar->SetCoordFunc(CoordFuncs[int(hudType)]); + x64_energybart01_missilebar->SetCoordFunc(CoordFuncs[size_t(hudType)]); x64_energybart01_missilebar->SetTesselation(hudType == EHudType::Combat ? 1.f : 0.1f); x64_energybart01_missilebar->SetMaxEnergy(5.f); x64_energybart01_missilebar->SetFilledDrainSpeed(g_tweakGui->GetEnergyBarFilledSpeed()); @@ -121,8 +126,8 @@ void CHudMissileInterface::Update(float dt, const CStateManager& mgr) { if (x58_28_notXRay) { x74_basewidget_missileicon->SetLocalTransform( x10_missleIconXf * - zeus::CTransform::Translate(0.f, 0.f, - x8_numMissles * IconTranslateRanges[int(x0_hudType)] / float(x4_missileCapacity))); + zeus::CTransform::Translate( + 0.f, 0.f, x8_numMissles * IconTranslateRanges[size_t(x0_hudType)] / float(x4_missileCapacity))); } if (x58_27_hasArrows) { @@ -236,28 +241,30 @@ CHudMissileInterface::EInventoryStatus CHudMissileInterface::GetMissileInventory } std::pair CHudMissileInterface::CombatMissileBarCoordFunc(float t) { - float z = t * IconTranslateRanges[int(EHudType::Combat)]; + const float z = t * IconTranslateRanges[size_t(EHudType::Combat)]; return {zeus::CVector3f(0.f, 0.f, z), zeus::CVector3f(0.3f, 0.f, z)}; } std::pair CHudMissileInterface::XRayMissileBarCoordFunc(float t) { - float theta = 0.8f * (t - 0.5f); - float x = 9.55f * std::cos(theta); - float z = 9.55f * std::sin(theta); + const float theta = 0.8f * (t - 0.5f); + const float x = 9.55f * std::cos(theta); + const float z = 9.55f * std::sin(theta); return {zeus::CVector3f(x - 0.4f, 0.f, z), zeus::CVector3f(x, 0.f, z)}; } std::pair CHudMissileInterface::ThermalMissileBarCoordFunc(float t) { - float transRange = IconTranslateRanges[int(EHudType::Thermal)]; - float a = 0.08f * transRange; - float b = t * transRange; + const float transRange = IconTranslateRanges[size_t(EHudType::Thermal)]; + const float a = 0.08f * transRange; + const float b = t * transRange; + float c; - if (b < a) + if (b < a) { c = b / a; - else if (b < transRange - a) + } else if (b < transRange - a) { c = 1.f; - else + } else { c = 1.f - (b - (transRange - a)) / a; + } return {zeus::CVector3f(-0.5f * c - 0.1f, 0.f, b), zeus::CVector3f(-0.1f, 0.f, b)}; } From e0392d69c4eee473d023e6c8b5436b8db02d687c Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 14 Mar 2020 17:59:39 -0400 Subject: [PATCH 04/14] CHudEnergyInterface: Make use of std::array where applicable --- Runtime/GuiSys/CHudEnergyInterface.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/Runtime/GuiSys/CHudEnergyInterface.cpp b/Runtime/GuiSys/CHudEnergyInterface.cpp index f56fa4513..e664f2bed 100644 --- a/Runtime/GuiSys/CHudEnergyInterface.cpp +++ b/Runtime/GuiSys/CHudEnergyInterface.cpp @@ -1,5 +1,7 @@ #include "Runtime/GuiSys/CHudEnergyInterface.hpp" +#include + #include "Runtime/GameGlobalObjects.hpp" #include "Runtime/Audio/CSfxManager.hpp" #include "Runtime/GuiSys/CAuiEnergyBarT01.hpp" @@ -10,12 +12,15 @@ namespace urde { -static const CAuiEnergyBarT01::FCoordFunc CoordFuncs[] = { +constexpr std::array CoordFuncs{ CHudEnergyInterface::CombatEnergyCoordFunc, CHudEnergyInterface::CombatEnergyCoordFunc, - CHudEnergyInterface::XRayEnergyCoordFunc, CHudEnergyInterface::ThermalEnergyCoordFunc, - CHudEnergyInterface::BallEnergyCoordFunc}; + CHudEnergyInterface::XRayEnergyCoordFunc, CHudEnergyInterface::ThermalEnergyCoordFunc, + CHudEnergyInterface::BallEnergyCoordFunc, +}; -static const float Tesselations[] = {0.2f, 0.2f, 0.1f, 0.2f, 1.f}; +constexpr std::array Tesselations{ + 0.2f, 0.2f, 0.1f, 0.2f, 1.f, +}; CHudEnergyInterface::CHudEnergyInterface(CGuiFrame& selHud, float tankEnergy, int totalEnergyTanks, int numTanksFilled, bool energyLow, EHudType hudType) @@ -33,8 +38,8 @@ CHudEnergyInterface::CHudEnergyInterface(CGuiFrame& selHud, float tankEnergy, in x28_textpane_energywarning = static_cast(selHud.FindWidget("textpane_energywarning")); x2c_energybart01_energybar = static_cast(selHud.FindWidget("energybart01_energybar")); - x2c_energybart01_energybar->SetCoordFunc(CoordFuncs[int(hudType)]); - x2c_energybart01_energybar->SetTesselation(Tesselations[int(hudType)]); + x2c_energybart01_energybar->SetCoordFunc(CoordFuncs[size_t(hudType)]); + x2c_energybart01_energybar->SetTesselation(Tesselations[size_t(hudType)]); ITweakGuiColors::VisorEnergyBarColors barColors = g_tweakGuiColors->GetVisorEnergyBarColors(int(hudType)); ITweakGuiColors::VisorEnergyInitColors initColors = g_tweakGuiColors->GetVisorEnergyInitColors(int(hudType)); From e64dfe76b24e69e0698b4f75adae17faa5cf5ba2 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 14 Mar 2020 18:45:34 -0400 Subject: [PATCH 05/14] CFontImageDef: Make use of emplace_back where applicable Same behavior, but constructs the instances directly within the container. --- Runtime/GuiSys/CFontImageDef.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Runtime/GuiSys/CFontImageDef.cpp b/Runtime/GuiSys/CFontImageDef.cpp index 6cabc6d3c..e20b52403 100644 --- a/Runtime/GuiSys/CFontImageDef.cpp +++ b/Runtime/GuiSys/CFontImageDef.cpp @@ -8,13 +8,14 @@ CFontImageDef::CFontImageDef(const std::vector>& texs, float in const zeus::CVector2f& cropFactor) : x0_fps(interval), x14_cropFactor(cropFactor) { x4_texs.reserve(texs.size()); - for (const TToken& tok : texs) - x4_texs.push_back(tok); + for (const TToken& tok : texs) { + x4_texs.emplace_back(tok); + } } CFontImageDef::CFontImageDef(const TToken& tex, const zeus::CVector2f& cropFactor) : x0_fps(0.f), x14_cropFactor(cropFactor) { - x4_texs.push_back(tex); + x4_texs.emplace_back(tex); } bool CFontImageDef::IsLoaded() const { From 659b8a43d342dfe48fafb4b18648399776bddf5d Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 14 Mar 2020 19:59:58 -0400 Subject: [PATCH 06/14] CGameOptions: Make use of std::string_view where applicable Enforces the use of valid strings in the interface. Also reduces c_str() noise a little bit. --- Runtime/CGameHintInfo.cpp | 8 +++--- Runtime/CGameHintInfo.hpp | 2 +- Runtime/CGameOptions.cpp | 34 +++++++++++++++--------- Runtime/CGameOptions.hpp | 7 ++--- Runtime/World/CScriptSpecialFunction.cpp | 13 ++++----- 5 files changed, 37 insertions(+), 27 deletions(-) diff --git a/Runtime/CGameHintInfo.cpp b/Runtime/CGameHintInfo.cpp index 9544f593c..6b2fded69 100644 --- a/Runtime/CGameHintInfo.cpp +++ b/Runtime/CGameHintInfo.cpp @@ -31,12 +31,12 @@ CGameHintInfo::SHintLocation::SHintLocation(CInputStream& in, s32) , x8_areaId(in.readUint32Big()) , xc_stringId(in.readUint32Big()) {} -int CGameHintInfo::FindHintIndex(const char* str) { +int CGameHintInfo::FindHintIndex(std::string_view str) { const std::vector& gameHints = g_MemoryCardSys->GetHints(); - const auto& it = std::find_if(gameHints.begin(), gameHints.end(), - [&str](const CGameHint& gh) -> bool { return gh.GetName() == str; }); + const auto it = + std::find_if(gameHints.cbegin(), gameHints.cend(), [&str](const CGameHint& gh) { return gh.GetName() == str; }); - return (it != gameHints.end() ? it - gameHints.begin() : -1); + return it != gameHints.cend() ? it - gameHints.cbegin() : -1; } CFactoryFnReturn FHintFactory(const SObjectTag&, CInputStream& in, const CVParamTransfer, CObjectReference*) { diff --git a/Runtime/CGameHintInfo.hpp b/Runtime/CGameHintInfo.hpp index 7b0e75ee3..2c530cc99 100644 --- a/Runtime/CGameHintInfo.hpp +++ b/Runtime/CGameHintInfo.hpp @@ -42,7 +42,7 @@ private: public: CGameHintInfo(CInputStream&, s32); const std::vector& GetHints() const { return x0_hints; } - static int FindHintIndex(const char* str); + static int FindHintIndex(std::string_view str); }; CFactoryFnReturn FHintFactory(const SObjectTag&, CInputStream&, const CVParamTransfer, CObjectReference*); diff --git a/Runtime/CGameOptions.cpp b/Runtime/CGameOptions.cpp index bd6dece7c..fb9a7f6ed 100644 --- a/Runtime/CGameOptions.cpp +++ b/Runtime/CGameOptions.cpp @@ -578,43 +578,51 @@ const CHintOptions::SHintState* CHintOptions::GetCurrentDisplayedHint() const { return nullptr; } -void CHintOptions::DelayHint(const char* name) { - int idx = CGameHintInfo::FindHintIndex(name); - if (idx == -1) +void CHintOptions::DelayHint(std::string_view name) { + const int idx = CGameHintInfo::FindHintIndex(name); + if (idx == -1) { return; + } - if (x10_nextHintIdx == idx) - for (SHintState& state : x0_hintStates) + if (x10_nextHintIdx == idx) { + for (SHintState& state : x0_hintStates) { state.x4_time += 60.f; + } + } x0_hintStates[idx].x0_state = EHintState::Delayed; } -void CHintOptions::ActivateImmediateHintTimer(const char* name) { - int idx = CGameHintInfo::FindHintIndex(name); - if (idx == -1) +void CHintOptions::ActivateImmediateHintTimer(std::string_view name) { + const int idx = CGameHintInfo::FindHintIndex(name); + if (idx == -1) { return; + } SHintState& hintState = x0_hintStates[idx]; const CGameHintInfo::CGameHint& hint = g_MemoryCardSys->GetHints()[idx]; - if (hintState.x0_state != EHintState::Zero) + if (hintState.x0_state != EHintState::Zero) { return; + } hintState.x0_state = EHintState::Waiting; hintState.x4_time = hint.GetImmediateTime(); } -void CHintOptions::ActivateContinueDelayHintTimer(const char* name) { +void CHintOptions::ActivateContinueDelayHintTimer(std::string_view name) { int idx = x10_nextHintIdx; - if (idx != 0) + if (idx != 0) { idx = CGameHintInfo::FindHintIndex(name); - if (idx == -1) + } + if (idx == -1) { return; + } SHintState& hintState = x0_hintStates[idx]; const CGameHintInfo::CGameHint& hint = g_MemoryCardSys->GetHints()[idx]; - if (hintState.x0_state != EHintState::Displaying) + if (hintState.x0_state != EHintState::Displaying) { return; + } hintState.x4_time = hint.GetTextTime(); } diff --git a/Runtime/CGameOptions.hpp b/Runtime/CGameOptions.hpp index cc290db78..acc3bec0c 100644 --- a/Runtime/CGameOptions.hpp +++ b/Runtime/CGameOptions.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include "Runtime/CSaveWorld.hpp" @@ -205,9 +206,9 @@ public: void SetNextHintTime(); void InitializeMemoryState(); const SHintState* GetCurrentDisplayedHint() const; - void DelayHint(const char* name); - void ActivateImmediateHintTimer(const char* name); - void ActivateContinueDelayHintTimer(const char* name); + void DelayHint(std::string_view name); + void ActivateImmediateHintTimer(std::string_view name); + void ActivateContinueDelayHintTimer(std::string_view name); void DismissDisplayedHint(); u32 GetNextHintIdx() const; const std::vector& GetHintStates() const { return x0_hintStates; } diff --git a/Runtime/World/CScriptSpecialFunction.cpp b/Runtime/World/CScriptSpecialFunction.cpp index e15b55b52..d1fa1b38b 100644 --- a/Runtime/World/CScriptSpecialFunction.cpp +++ b/Runtime/World/CScriptSpecialFunction.cpp @@ -396,12 +396,13 @@ void CScriptSpecialFunction::AcceptScriptMsg(EScriptObjectMessage msg, TUniqueId } case ESpecialFunction::RedundantHintSystem: { CHintOptions& hintOptions = g_GameState->HintOptions(); - if (msg == EScriptObjectMessage::Action) - hintOptions.ActivateContinueDelayHintTimer(xec_locatorName.c_str()); - else if (msg == EScriptObjectMessage::Increment) - hintOptions.ActivateImmediateHintTimer(xec_locatorName.c_str()); - else if (msg == EScriptObjectMessage::Decrement) - hintOptions.DelayHint(xec_locatorName.c_str()); + if (msg == EScriptObjectMessage::Action) { + hintOptions.ActivateContinueDelayHintTimer(xec_locatorName); + } else if (msg == EScriptObjectMessage::Increment) { + hintOptions.ActivateImmediateHintTimer(xec_locatorName); + } else if (msg == EScriptObjectMessage::Decrement) { + hintOptions.DelayHint(xec_locatorName); + } break; } case ESpecialFunction::Billboard: { From f5cc056a9e5801b9ce59175c1a04a87308767fd1 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 14 Mar 2020 21:28:43 -0400 Subject: [PATCH 07/14] CGameHintInfo: Take CVParamTransfer parameter by const reference Avoids trivial unnecessary copies. --- Runtime/CGameHintInfo.cpp | 2 +- Runtime/CGameHintInfo.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Runtime/CGameHintInfo.cpp b/Runtime/CGameHintInfo.cpp index 9544f593c..84df109db 100644 --- a/Runtime/CGameHintInfo.cpp +++ b/Runtime/CGameHintInfo.cpp @@ -39,7 +39,7 @@ int CGameHintInfo::FindHintIndex(const char* str) { return (it != gameHints.end() ? it - gameHints.begin() : -1); } -CFactoryFnReturn FHintFactory(const SObjectTag&, CInputStream& in, const CVParamTransfer, CObjectReference*) { +CFactoryFnReturn FHintFactory(const SObjectTag&, CInputStream& in, const CVParamTransfer&, CObjectReference*) { in.readUint32Big(); s32 version = in.readInt32Big(); diff --git a/Runtime/CGameHintInfo.hpp b/Runtime/CGameHintInfo.hpp index 7b0e75ee3..0628008ad 100644 --- a/Runtime/CGameHintInfo.hpp +++ b/Runtime/CGameHintInfo.hpp @@ -45,5 +45,5 @@ public: static int FindHintIndex(const char* str); }; -CFactoryFnReturn FHintFactory(const SObjectTag&, CInputStream&, const CVParamTransfer, CObjectReference*); +CFactoryFnReturn FHintFactory(const SObjectTag&, CInputStream&, const CVParamTransfer&, CObjectReference*); } // namespace urde From e6812a7cd955f5ab16c0e553d142f0354ce5b95c Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 14 Mar 2020 21:42:35 -0400 Subject: [PATCH 08/14] CArchitectureQueue: Make operator bool explicit Prevents potentially error-prone implicit conversion contexts. --- Runtime/CArchitectureQueue.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Runtime/CArchitectureQueue.hpp b/Runtime/CArchitectureQueue.hpp index 8aae965a0..1c14c2a47 100644 --- a/Runtime/CArchitectureQueue.hpp +++ b/Runtime/CArchitectureQueue.hpp @@ -16,7 +16,7 @@ public: return msg; } void Clear() { m_list.clear(); } - operator bool() const { return m_list.size() != 0; } + explicit operator bool() const { return !m_list.empty(); } }; } // namespace urde From ff92b72f911a2b00804498610a2a69456a8096be Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 14 Mar 2020 22:18:06 -0400 Subject: [PATCH 09/14] CMemoryCardSys: Make CardResult's conversion operator explicit Prevents implicit error-prone conversions to bool --- Runtime/CMemoryCardSys.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Runtime/CMemoryCardSys.hpp b/Runtime/CMemoryCardSys.hpp index 82b71a236..6281ca773 100644 --- a/Runtime/CMemoryCardSys.hpp +++ b/Runtime/CMemoryCardSys.hpp @@ -76,7 +76,7 @@ public: ECardResult result; CardResult(ECardResult res) : result(res) {} operator ECardResult() const { return result; } - operator bool() const { return result != ECardResult::READY; } + explicit operator bool() const { return result != ECardResult::READY; } }; struct CardFileHandle { From 7c1528688bd6f32c16663dc6bcddb2f042b2ca96 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 15 Mar 2020 18:30:55 -0400 Subject: [PATCH 10/14] CAnimSource: Simplify ClampZeroToOne() We can make use of std::clamp instead of std::min and std::max --- Runtime/Character/CAnimSource.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Runtime/Character/CAnimSource.cpp b/Runtime/Character/CAnimSource.cpp index a826eeb86..a01ac7c1a 100644 --- a/Runtime/Character/CAnimSource.cpp +++ b/Runtime/Character/CAnimSource.cpp @@ -9,7 +9,7 @@ namespace urde { -static float ClampZeroToOne(float in) { return std::max(0.f, std::min(1.f, in)); } +static constexpr float ClampZeroToOne(float in) { return std::clamp(in, 0.0f, 1.0f); } u32 RotationAndOffsetStorage::DataSizeInBytes(u32 rotPerFrame, u32 transPerFrame, u32 frameCount) { return (transPerFrame * 12 + rotPerFrame * 16) * frameCount; From 9541ed191989946ac49527b453367cb0d0e2edda Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 15 Mar 2020 18:40:40 -0400 Subject: [PATCH 11/14] CAnimSourceReader: Make use of emplace() where applicable Same behavior, but with less code. --- Runtime/Character/CAnimSourceReader.cpp | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/Runtime/Character/CAnimSourceReader.cpp b/Runtime/Character/CAnimSourceReader.cpp index 2362dfff2..0778af201 100644 --- a/Runtime/Character/CAnimSourceReader.cpp +++ b/Runtime/Character/CAnimSourceReader.cpp @@ -27,27 +27,33 @@ CCharAnimTime CAnimSourceInfo::GetAnimationDuration() const { return x4_token->G std::set> CAnimSourceReaderBase::GetUniqueParticlePOIs() const { const std::vector& particleNodes = x4_sourceInfo->GetParticlePOIStream(); std::set> ret; - for (const CParticlePOINode& node : particleNodes) - if (node.GetUnique()) - ret.insert(std::make_pair(std::string(node.GetString()), node.GetIndex())); + for (const CParticlePOINode& node : particleNodes) { + if (node.GetUnique()) { + ret.emplace(node.GetString(), node.GetIndex()); + } + } return ret; } std::set> CAnimSourceReaderBase::GetUniqueInt32POIs() const { const std::vector& int32Nodes = x4_sourceInfo->GetInt32POIStream(); std::set> ret; - for (const CInt32POINode& node : int32Nodes) - if (node.GetUnique()) - ret.insert(std::make_pair(std::string(node.GetString()), node.GetIndex())); + for (const CInt32POINode& node : int32Nodes) { + if (node.GetUnique()) { + ret.emplace(node.GetString(), node.GetIndex()); + } + } return ret; } std::set> CAnimSourceReaderBase::GetUniqueBoolPOIs() const { const std::vector& boolNodes = x4_sourceInfo->GetBoolPOIStream(); std::set> ret; - for (const CBoolPOINode& node : boolNodes) - if (node.GetUnique()) - ret.insert(std::make_pair(std::string(node.GetString()), node.GetIndex())); + for (const CBoolPOINode& node : boolNodes) { + if (node.GetUnique()) { + ret.emplace(node.GetString(), node.GetIndex()); + } + } return ret; } From 15db1b2647caadba74aae23599313e6545d336fc Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 15 Mar 2020 18:47:52 -0400 Subject: [PATCH 12/14] CAnimSourceReader: Make use of std::find_if where applicable --- Runtime/Character/CAnimSourceReader.cpp | 38 +++++++++++++++++-------- 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/Runtime/Character/CAnimSourceReader.cpp b/Runtime/Character/CAnimSourceReader.cpp index 0778af201..e9c194da7 100644 --- a/Runtime/Character/CAnimSourceReader.cpp +++ b/Runtime/Character/CAnimSourceReader.cpp @@ -1,5 +1,7 @@ #include "Runtime/Character/CAnimSourceReader.hpp" +#include + #include "Runtime/Character/CBoolPOINode.hpp" #include "Runtime/Character/CFBStreamedAnimReader.hpp" #include "Runtime/Character/CInt32POINode.hpp" @@ -171,24 +173,36 @@ u32 CAnimSourceReaderBase::VGetSoundPOIList(const CCharAnimTime& time, CSoundPOI } bool CAnimSourceReaderBase::VGetBoolPOIState(const char* name) const { - for (const auto& node : x24_boolStates) - if (node.first == name) - return node.second; - return false; + const auto iter = std::find_if(x24_boolStates.cbegin(), x24_boolStates.cend(), + [name](const auto& entry) { return entry.first == name; }); + + if (iter == x24_boolStates.cend()) { + return false; + } + + return iter->second; } s32 CAnimSourceReaderBase::VGetInt32POIState(const char* name) const { - for (const auto& node : x34_int32States) - if (node.first == name) - return node.second; - return 0; + const auto iter = std::find_if(x34_int32States.cbegin(), x34_int32States.cend(), + [name](const auto& entry) { return entry.first == name; }); + + if (iter == x34_int32States.cend()) { + return 0; + } + + return iter->second; } CParticleData::EParentedMode CAnimSourceReaderBase::VGetParticlePOIState(const char* name) const { - for (const auto& node : x44_particleStates) - if (node.first == name) - return node.second; - return CParticleData::EParentedMode::Initial; + const auto iter = std::find_if(x44_particleStates.cbegin(), x44_particleStates.cend(), + [name](const auto& entry) { return entry.first == name; }); + + if (iter == x44_particleStates.cend()) { + return CParticleData::EParentedMode::Initial; + } + + return iter->second; } CAnimSourceReaderBase::CAnimSourceReaderBase(std::unique_ptr&& sourceInfo, const CCharAnimTime& time) From 1926966d21b09fc901f305f1e0625129d3a879e3 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 15 Mar 2020 19:34:41 -0400 Subject: [PATCH 13/14] CCharAnimTime: Make constructors constexpr where applicable These can be made constexpr to allow use at compile-time, given these only hold an enum and a floating point value. --- Runtime/Character/CCharAnimTime.cpp | 4 ---- Runtime/Character/CCharAnimTime.hpp | 11 +++++------ 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/Runtime/Character/CCharAnimTime.cpp b/Runtime/Character/CCharAnimTime.cpp index f64512ea7..6d3d0eea1 100644 --- a/Runtime/Character/CCharAnimTime.cpp +++ b/Runtime/Character/CCharAnimTime.cpp @@ -5,10 +5,6 @@ namespace urde { -CCharAnimTime CCharAnimTime::Infinity() { - return {EType::Infinity, 1.f}; -} - bool CCharAnimTime::EqualsZero() const { if (x4_type == EType::ZeroIncreasing || x4_type == EType::ZeroSteady || x4_type == EType::ZeroDecreasing) return true; diff --git a/Runtime/Character/CCharAnimTime.hpp b/Runtime/Character/CCharAnimTime.hpp index 3aed3e0e8..e28e36e7d 100644 --- a/Runtime/Character/CCharAnimTime.hpp +++ b/Runtime/Character/CCharAnimTime.hpp @@ -16,13 +16,12 @@ private: EType x4_type = EType::ZeroSteady; public: - CCharAnimTime() = default; - CCharAnimTime(CInputStream& in) : x0_time(in.readFloatBig()), x4_type(EType(in.readUint32Big())) {} - CCharAnimTime(float time) : x0_time(time), x4_type(x0_time != 0.f ? EType::NonZero : EType::ZeroSteady) {} + constexpr CCharAnimTime() = default; + constexpr CCharAnimTime(float time) : x0_time(time), x4_type(x0_time != 0.f ? EType::NonZero : EType::ZeroSteady) {} + constexpr CCharAnimTime(EType type, float t) : x0_time(t), x4_type(type) {} + explicit CCharAnimTime(CInputStream& in) : x0_time(in.readFloatBig()), x4_type(EType(in.readUint32Big())) {} - CCharAnimTime(EType type, const float& t) : x0_time(t), x4_type(type) {} - - static CCharAnimTime Infinity(); + static constexpr CCharAnimTime Infinity() { return {EType::Infinity, 1.0f}; } float GetSeconds() const { return x0_time; } bool EqualsZero() const; From a9082bc445fa12b45fdcf05c0909b6d38cfcd40c Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 15 Mar 2020 20:04:08 -0400 Subject: [PATCH 14/14] CCollisionActor: Mark gkDefaultCollisionActorMaterials as constexpr While we're at it, change the g to s, since it's internally linked. --- Runtime/Collision/CCollisionActor.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Runtime/Collision/CCollisionActor.cpp b/Runtime/Collision/CCollisionActor.cpp index 1c3c4875a..2bdfb8a9b 100644 --- a/Runtime/Collision/CCollisionActor.cpp +++ b/Runtime/Collision/CCollisionActor.cpp @@ -9,14 +9,14 @@ #include "TCastTo.hpp" // Generated file, do not modify include path namespace urde { -static const CMaterialList gkDefaultCollisionActorMaterials = +constexpr CMaterialList skDefaultCollisionActorMaterials = CMaterialList(EMaterialTypes::Solid, EMaterialTypes::CollisionActor, EMaterialTypes::ScanPassthrough, EMaterialTypes::CameraPassthrough); CCollisionActor::CCollisionActor(TUniqueId uid1, TAreaId aId, TUniqueId uid2, const zeus::CVector3f& extent, const zeus::CVector3f& center, bool active, float mass, std::string_view name) : CPhysicsActor(uid1, active, "CollisionActor", CEntityInfo(aId, CEntity::NullConnectionList), - zeus::CTransform(), CModelData::CModelDataNull(), gkDefaultCollisionActorMaterials, + zeus::CTransform(), CModelData::CModelDataNull(), skDefaultCollisionActorMaterials, zeus::skNullBox, SMoverData(mass), CActorParameters::None(), 0.3f, 0.1f) , x258_primitiveType(EPrimitiveType::OBBTreeGroup) , x25c_owner(uid2) @@ -35,7 +35,7 @@ CCollisionActor::CCollisionActor(TUniqueId uid1, TAreaId aId, TUniqueId uid2, co CCollisionActor::CCollisionActor(TUniqueId uid1, TAreaId aId, TUniqueId uid2, const zeus::CVector3f& boxSize, bool active, float mass, std::string_view name) : CPhysicsActor(uid1, active, "CollisionActor", CEntityInfo(aId, CEntity::NullConnectionList), - zeus::CTransform(), CModelData::CModelDataNull(), gkDefaultCollisionActorMaterials, + zeus::CTransform(), CModelData::CModelDataNull(), skDefaultCollisionActorMaterials, zeus::skNullBox, SMoverData(mass), CActorParameters::None(), 0.3f, 0.1f) , x258_primitiveType(EPrimitiveType::AABox) , x25c_owner(uid2) @@ -54,7 +54,7 @@ CCollisionActor::CCollisionActor(TUniqueId uid1, TAreaId aId, TUniqueId uid2, co CCollisionActor::CCollisionActor(TUniqueId uid1, TAreaId aId, TUniqueId uid2, bool active, float radius, float mass, std::string_view name) : CPhysicsActor(uid1, active, "CollisionActor", CEntityInfo(aId, CEntity::NullConnectionList), - zeus::CTransform(), CModelData::CModelDataNull(), gkDefaultCollisionActorMaterials, + zeus::CTransform(), CModelData::CModelDataNull(), skDefaultCollisionActorMaterials, zeus::skNullBox, SMoverData(mass), CActorParameters::None(), 0.3f, 0.1f) , x258_primitiveType(EPrimitiveType::Sphere) , x25c_owner(uid2)