CCollisionResponseData: Make use of std::array where applicable

Allows us to replace some heap allocating file-scope std::vector
instances with constexpr std::arrays, eliminating quite a few static
constructors.
This commit is contained in:
Lioncash 2019-10-29 19:17:53 -04:00
parent c1beec5bb0
commit 3d67088697
2 changed files with 54 additions and 32 deletions

View File

@ -1,5 +1,7 @@
#include "Runtime/Collision/CCollisionResponseData.hpp" #include "Runtime/Collision/CCollisionResponseData.hpp"
#include <array>
#include "Runtime/CRandom16.hpp" #include "Runtime/CRandom16.hpp"
#include "Runtime/CSimplePool.hpp" #include "Runtime/CSimplePool.hpp"
#include "Runtime/Graphics/CModel.hpp" #include "Runtime/Graphics/CModel.hpp"
@ -10,7 +12,8 @@
#include "Runtime/Particle/CSwooshDescription.hpp" #include "Runtime/Particle/CSwooshDescription.hpp"
namespace urde { namespace urde {
const EWeaponCollisionResponseTypes CCollisionResponseData::skWorldMaterialTable[32] = { namespace {
constexpr std::array skWorldMaterialTable{
EWeaponCollisionResponseTypes::Default, EWeaponCollisionResponseTypes::Unknown2, EWeaponCollisionResponseTypes::Default, EWeaponCollisionResponseTypes::Unknown2,
EWeaponCollisionResponseTypes::Metal, EWeaponCollisionResponseTypes::Grass, EWeaponCollisionResponseTypes::Metal, EWeaponCollisionResponseTypes::Grass,
EWeaponCollisionResponseTypes::Ice, EWeaponCollisionResponseTypes::Goo, EWeaponCollisionResponseTypes::Ice, EWeaponCollisionResponseTypes::Goo,
@ -29,9 +32,9 @@ const EWeaponCollisionResponseTypes CCollisionResponseData::skWorldMaterialTable
EWeaponCollisionResponseTypes::Default, EWeaponCollisionResponseTypes::Default, EWeaponCollisionResponseTypes::Default, EWeaponCollisionResponseTypes::Default,
}; };
const s32 CCollisionResponseData::kInvalidSFX = -1; constexpr s32 kInvalidSFX = -1;
static const std::vector<FourCC> kWCRTSFXIDs = { constexpr std::array<FourCC, 94> kWCRTSFXIDs{{
SBIG('NSFX'), SBIG('DSFX'), SBIG('CSFX'), SBIG('MSFX'), SBIG('GRFX'), SBIG('ICFX'), SBIG('GOFX'), SBIG('WSFX'), SBIG('NSFX'), SBIG('DSFX'), SBIG('CSFX'), SBIG('MSFX'), SBIG('GRFX'), SBIG('ICFX'), SBIG('GOFX'), SBIG('WSFX'),
SBIG('WTFX'), SBIG('2MUD'), SBIG('2LAV'), SBIG('2SAN'), SBIG('2PRJ'), SBIG('DCFX'), SBIG('DSFX'), SBIG('DSHX'), SBIG('WTFX'), SBIG('2MUD'), SBIG('2LAV'), SBIG('2SAN'), SBIG('2PRJ'), SBIG('DCFX'), SBIG('DSFX'), SBIG('DSHX'),
SBIG('DEFX'), SBIG('ESFX'), SBIG('SHFX'), SBIG('BEFX'), SBIG('WWFX'), SBIG('TAFX'), SBIG('GTFX'), SBIG('SPFX'), SBIG('DEFX'), SBIG('ESFX'), SBIG('SHFX'), SBIG('BEFX'), SBIG('WWFX'), SBIG('TAFX'), SBIG('GTFX'), SBIG('SPFX'),
@ -44,9 +47,9 @@ static const std::vector<FourCC> kWCRTSFXIDs = {
SBIG('GHFX'), SBIG('SHFX'), SBIG('FHFX'), SBIG('HFFX'), SBIG('PHFX'), SBIG('MHFX'), SBIG('HBFX'), SBIG('PBHX'), SBIG('GHFX'), SBIG('SHFX'), SBIG('FHFX'), SBIG('HFFX'), SBIG('PHFX'), SBIG('MHFX'), SBIG('HBFX'), SBIG('PBHX'),
SBIG('IBHX'), SBIG('6SVA'), SBIG('6RPR'), SBIG('6MTR'), SBIG('6PDS'), SBIG('6FLB'), SBIG('6DRN'), SBIG('6MRE'), SBIG('IBHX'), SBIG('6SVA'), SBIG('6RPR'), SBIG('6MTR'), SBIG('6PDS'), SBIG('6FLB'), SBIG('6DRN'), SBIG('6MRE'),
SBIG('CHFX'), SBIG('JZHS'), SBIG('6ISE'), SBIG('6BSE'), SBIG('6ATB'), SBIG('6ATA'), SBIG('CHFX'), SBIG('JZHS'), SBIG('6ISE'), SBIG('6BSE'), SBIG('6ATB'), SBIG('6ATA'),
}; }};
static const std::vector<FourCC> kWCRTIDs = { constexpr std::array<FourCC, 94> kWCRTIDs{{
SBIG('NODP'), SBIG('DEFS'), SBIG('CRTS'), SBIG('MTLS'), SBIG('GRAS'), SBIG('ICEE'), SBIG('GOOO'), SBIG('WODS'), SBIG('NODP'), SBIG('DEFS'), SBIG('CRTS'), SBIG('MTLS'), SBIG('GRAS'), SBIG('ICEE'), SBIG('GOOO'), SBIG('WODS'),
SBIG('WATR'), SBIG('1MUD'), SBIG('1LAV'), SBIG('1SAN'), SBIG('1PRJ'), SBIG('DCHR'), SBIG('DCHS'), SBIG('DCSH'), SBIG('WATR'), SBIG('1MUD'), SBIG('1LAV'), SBIG('1SAN'), SBIG('1PRJ'), SBIG('DCHR'), SBIG('DCHS'), SBIG('DCSH'),
SBIG('DENM'), SBIG('DESP'), SBIG('DESH'), SBIG('BTLE'), SBIG('WASP'), SBIG('TALP'), SBIG('PTGM'), SBIG('SPIR'), SBIG('DENM'), SBIG('DESP'), SBIG('DESH'), SBIG('BTLE'), SBIG('WASP'), SBIG('TALP'), SBIG('PTGM'), SBIG('SPIR'),
@ -58,33 +61,49 @@ static const std::vector<FourCC> kWCRTIDs = {
SBIG('JZSP'), SBIG('3ISE'), SBIG('3BSE'), SBIG('3ATB'), SBIG('3ATA'), SBIG('BTSH'), SBIG('WWSH'), SBIG('TASH'), SBIG('JZSP'), SBIG('3ISE'), SBIG('3BSE'), SBIG('3ATB'), SBIG('3ATA'), SBIG('BTSH'), SBIG('WWSH'), SBIG('TASH'),
SBIG('TGSH'), SBIG('SPSH'), SBIG('FPSH'), SBIG('FFSH'), SBIG('PSSH'), SBIG('BMSH'), SBIG('BFSH'), SBIG('PBSH'), SBIG('TGSH'), SBIG('SPSH'), SBIG('FPSH'), SBIG('FFSH'), SBIG('PSSH'), SBIG('BMSH'), SBIG('BFSH'), SBIG('PBSH'),
SBIG('IBSH'), SBIG('3SVA'), SBIG('3RPR'), SBIG('3MTR'), SBIG('3PDS'), SBIG('3FLB'), SBIG('3DRN'), SBIG('3MRE'), SBIG('IBSH'), SBIG('3SVA'), SBIG('3RPR'), SBIG('3MTR'), SBIG('3PDS'), SBIG('3FLB'), SBIG('3DRN'), SBIG('3MRE'),
SBIG('CHSH'), SBIG('JZSH'), SBIG('5ISE'), SBIG('5BSE'), SBIG('5ATB'), SBIG('5ATA')}; SBIG('CHSH'), SBIG('JZSH'), SBIG('5ISE'), SBIG('5BSE'), SBIG('5ATB'), SBIG('5ATA'),
}};
static const std::vector<FourCC> kWCRTDecalIDs = {SBIG('NCDL'), SBIG('DDCL'), SBIG('CODL'), SBIG('MEDL'), SBIG('GRDL'), constexpr std::array<FourCC, 14> kWCRTDecalIDs{{
SBIG('ICDL'), SBIG('GODL'), SBIG('WODL'), SBIG('WTDL'), SBIG('3MUD'), SBIG('NCDL'),
SBIG('3LAV'), SBIG('3SAN'), SBIG('CHDL'), SBIG('ENDL')}; SBIG('DDCL'),
SBIG('CODL'),
SBIG('MEDL'),
SBIG('GRDL'),
SBIG('ICDL'),
SBIG('GODL'),
SBIG('WODL'),
SBIG('WTDL'),
SBIG('3MUD'),
SBIG('3LAV'),
SBIG('3SAN'),
SBIG('CHDL'),
SBIG('ENDL'),
}};
using CPF = CParticleDataFactory; using CPF = CParticleDataFactory;
} // Anonymous namespace
void CCollisionResponseData::AddParticleSystemToResponse(EWeaponCollisionResponseTypes type, CInputStream& in, void CCollisionResponseData::AddParticleSystemToResponse(EWeaponCollisionResponseTypes type, CInputStream& in,
CSimplePool* resPool) { CSimplePool* resPool) {
int i = int(type); const auto i = size_t(type);
std::vector<CAssetId> tracker; const std::vector<CAssetId> tracker(8);
tracker.resize(8);
x0_generators[i].emplace(CPF::GetChildGeneratorDesc(in, resPool, tracker).m_token); x0_generators[i].emplace(CPF::GetChildGeneratorDesc(in, resPool, tracker).m_token);
} }
bool CCollisionResponseData::CheckAndAddDecalToResponse(FourCC clsId, CInputStream& in, CSimplePool* resPool) { bool CCollisionResponseData::CheckAndAddDecalToResponse(FourCC clsId, CInputStream& in, CSimplePool* resPool) {
int i = 0; size_t i = 0;
for (const FourCC& type : kWCRTDecalIDs) { for (const FourCC& type : kWCRTDecalIDs) {
if (type == clsId) { if (type == clsId) {
FourCC cls = CPF::GetClassID(in); const FourCC cls = CPF::GetClassID(in);
if (cls == SBIG('NONE')) if (cls == SBIG('NONE')) {
return true; return true;
}
CAssetId id = u64(in.readUint32Big()); const CAssetId id{u64(in.readUint32Big())};
if (!id.IsValid()) if (!id.IsValid()) {
return true; return true;
}
x20_decals[i].emplace(resPool->GetObj({FOURCC('DPSC'), id})); x20_decals[i].emplace(resPool->GetObj({FOURCC('DPSC'), id}));
return true; return true;
@ -95,12 +114,13 @@ bool CCollisionResponseData::CheckAndAddDecalToResponse(FourCC clsId, CInputStre
} }
bool CCollisionResponseData::CheckAndAddSoundFXToResponse(FourCC clsId, CInputStream& in) { bool CCollisionResponseData::CheckAndAddSoundFXToResponse(FourCC clsId, CInputStream& in) {
int i = 0; size_t i = 0;
for (const FourCC& type : kWCRTSFXIDs) { for (const FourCC& type : kWCRTSFXIDs) {
if (type == clsId) { if (type == clsId) {
FourCC cls = CPF::GetClassID(in); const FourCC cls = CPF::GetClassID(in);
if (cls == SBIG('NONE')) if (cls == SBIG('NONE')) {
return true; return true;
}
x10_sfx[i] = CPF::GetInt(in); x10_sfx[i] = CPF::GetInt(in);
return true; return true;
@ -112,7 +132,7 @@ bool CCollisionResponseData::CheckAndAddSoundFXToResponse(FourCC clsId, CInputSt
} }
bool CCollisionResponseData::CheckAndAddParticleSystemToResponse(FourCC clsId, CInputStream& in, CSimplePool* resPool) { bool CCollisionResponseData::CheckAndAddParticleSystemToResponse(FourCC clsId, CInputStream& in, CSimplePool* resPool) {
int i = 0; size_t i = 0;
for (const FourCC& type : kWCRTIDs) { for (const FourCC& type : kWCRTIDs) {
if (type == clsId) { if (type == clsId) {
AddParticleSystemToResponse(EWeaponCollisionResponseTypes(i), in, resPool); AddParticleSystemToResponse(EWeaponCollisionResponseTypes(i), in, resPool);
@ -165,8 +185,9 @@ CCollisionResponseData::CCollisionResponseData(CInputStream& in, CSimplePool* re
const std::optional<TLockedToken<CGenDescription>>& const std::optional<TLockedToken<CGenDescription>>&
CCollisionResponseData::GetParticleDescription(EWeaponCollisionResponseTypes type) const { CCollisionResponseData::GetParticleDescription(EWeaponCollisionResponseTypes type) const {
if (x0_generators[u32(type)]) if (x0_generators[size_t(type)]) {
return x0_generators[u32(type)]; return x0_generators[size_t(type)];
}
bool foundType = false; bool foundType = false;
if (ResponseTypeIsEnemyNormal(type)) { if (ResponseTypeIsEnemyNormal(type)) {
@ -180,22 +201,24 @@ CCollisionResponseData::GetParticleDescription(EWeaponCollisionResponseTypes typ
foundType = true; foundType = true;
} }
if (foundType && !x0_generators[u32(type)]) if (foundType && !x0_generators[size_t(type)]) {
type = EWeaponCollisionResponseTypes::EnemyNormal; type = EWeaponCollisionResponseTypes::EnemyNormal;
}
if (!x0_generators[u32(type)] && type != EWeaponCollisionResponseTypes::None) if (!x0_generators[size_t(type)] && type != EWeaponCollisionResponseTypes::None) {
type = EWeaponCollisionResponseTypes::Default; type = EWeaponCollisionResponseTypes::Default;
}
return x0_generators[u32(type)]; return x0_generators[size_t(type)];
} }
const std::optional<TLockedToken<CDecalDescription>>& const std::optional<TLockedToken<CDecalDescription>>&
CCollisionResponseData::GetDecalDescription(EWeaponCollisionResponseTypes type) const { CCollisionResponseData::GetDecalDescription(EWeaponCollisionResponseTypes type) const {
return x20_decals[u32(type)]; return x20_decals[size_t(type)];
} }
s32 CCollisionResponseData::GetSoundEffectId(EWeaponCollisionResponseTypes type) const { s32 CCollisionResponseData::GetSoundEffectId(EWeaponCollisionResponseTypes type) const {
if (x10_sfx[u32(type)] == kInvalidSFX) { if (x10_sfx[size_t(type)] == kInvalidSFX) {
if (ResponseTypeIsEnemyNormal(type)) if (ResponseTypeIsEnemyNormal(type))
type = EWeaponCollisionResponseTypes::EnemyNormal; type = EWeaponCollisionResponseTypes::EnemyNormal;
else if (ResponseTypeIsEnemySpecial(type)) else if (ResponseTypeIsEnemySpecial(type))
@ -206,12 +229,13 @@ s32 CCollisionResponseData::GetSoundEffectId(EWeaponCollisionResponseTypes type)
type = EWeaponCollisionResponseTypes::Default; type = EWeaponCollisionResponseTypes::Default;
} }
return x10_sfx[u32(type)]; return x10_sfx[size_t(type)];
} }
EWeaponCollisionResponseTypes CCollisionResponseData::GetWorldCollisionResponseType(s32 id) { EWeaponCollisionResponseTypes CCollisionResponseData::GetWorldCollisionResponseType(s32 id) {
if (id < 0 || id >= 32) if (id < 0 || size_t(id) >= skWorldMaterialTable.size()) {
return EWeaponCollisionResponseTypes::Default; return EWeaponCollisionResponseTypes::Default;
}
return skWorldMaterialTable[id]; return skWorldMaterialTable[id];
} }

View File

@ -113,8 +113,6 @@ enum class EWeaponCollisionResponseTypes {
}; };
class CCollisionResponseData { class CCollisionResponseData {
static const EWeaponCollisionResponseTypes skWorldMaterialTable[32];
static const s32 kInvalidSFX;
std::vector<std::optional<TLockedToken<CGenDescription>>> x0_generators; std::vector<std::optional<TLockedToken<CGenDescription>>> x0_generators;
std::vector<s32> x10_sfx; std::vector<s32> x10_sfx;
std::vector<std::optional<TLockedToken<CDecalDescription>>> x20_decals; std::vector<std::optional<TLockedToken<CDecalDescription>>> x20_decals;