CScriptTemplate: Make use of ranged for where applicable

This commit is contained in:
Lioncash 2020-06-19 23:40:24 -04:00
parent d93810568b
commit 731e34b90b
1 changed files with 55 additions and 45 deletions

View File

@ -93,11 +93,14 @@ EVolumeShape CScriptTemplate::VolumeShape(CScriptObject *pObj)
if (mVolumeShape == EVolumeShape::ConditionalShape)
{
int32 Index = CheckVolumeConditions(pObj, true);
if (Index == -1) return EVolumeShape::InvalidShape;
else return mVolumeConditions[Index].Shape;
const int32 Index = CheckVolumeConditions(pObj, true);
if (Index == -1)
return EVolumeShape::InvalidShape;
return mVolumeConditions[Index].Shape;
}
else return mVolumeShape;
return mVolumeShape;
}
float CScriptTemplate::VolumeScale(CScriptObject *pObj)
@ -110,11 +113,14 @@ float CScriptTemplate::VolumeScale(CScriptObject *pObj)
if (mVolumeShape == EVolumeShape::ConditionalShape)
{
int32 Index = CheckVolumeConditions(pObj, false);
if (Index == -1) return mVolumeScale;
else return mVolumeConditions[Index].Scale;
const int32 Index = CheckVolumeConditions(pObj, false);
if (Index == -1)
return mVolumeScale;
return mVolumeConditions[Index].Scale;
}
else return mVolumeScale;
return mVolumeScale;
}
int32 CScriptTemplate::CheckVolumeConditions(CScriptObject *pObj, bool LogErrors)
@ -122,12 +128,12 @@ int32 CScriptTemplate::CheckVolumeConditions(CScriptObject *pObj, bool LogErrors
// Private function
if (mVolumeShape == EVolumeShape::ConditionalShape)
{
TIDString PropID = mVolumeConditionIDString;
IProperty* pProp = pObj->Template()->Properties()->ChildByIDString( PropID );
const TIDString PropID = mVolumeConditionIDString;
IProperty* pProp = pObj->Template()->Properties()->ChildByIDString(PropID);
// Get value of the condition test property (only boolean, integral, and enum types supported)
void* pData = pObj->PropertyData();
int Val;
int Val = 0;
switch (pProp->Type())
{
@ -136,11 +142,11 @@ int32 CScriptTemplate::CheckVolumeConditions(CScriptObject *pObj, bool LogErrors
break;
case EPropertyType::Byte:
Val = (int) TPropCast<CByteProperty>(pProp)->Value(pData);
Val = static_cast<int>(TPropCast<CByteProperty>(pProp)->Value(pData));
break;
case EPropertyType::Short:
Val = (int) TPropCast<CShortProperty>(pProp)->Value(pData);
Val = static_cast<int>(TPropCast<CShortProperty>(pProp)->Value(pData));
break;
case EPropertyType::Int:
@ -174,34 +180,36 @@ int32 CScriptTemplate::CheckVolumeConditions(CScriptObject *pObj, bool LogErrors
CResource* CScriptTemplate::FindDisplayAsset(void* pPropertyData, uint32& rOutCharIndex, uint32& rOutAnimIndex, bool& rOutIsInGame)
{
rOutCharIndex = -1;
rOutAnimIndex = -1;
rOutCharIndex = UINT32_MAX;
rOutAnimIndex = UINT32_MAX;
rOutIsInGame = false;
for (auto it = mAssets.begin(); it != mAssets.end(); it++)
for (const auto& asset : mAssets)
{
if (it->AssetType == SEditorAsset::EAssetType::Collision) continue;
if (asset.AssetType == SEditorAsset::EAssetType::Collision)
continue;
CResource *pRes = nullptr;
// File
if (it->AssetSource == SEditorAsset::EAssetSource::File)
pRes = gpEditorStore->LoadResource(it->AssetLocation);
// Property
else
if (asset.AssetSource == SEditorAsset::EAssetSource::File)
{
IProperty* pProp = mpProperties->ChildByIDString(it->AssetLocation);
pRes = gpEditorStore->LoadResource(asset.AssetLocation);
}
else // Property
{
IProperty* pProp = mpProperties->ChildByIDString(asset.AssetLocation);
if (it->AssetType == SEditorAsset::EAssetType::AnimParams && pProp->Type() == EPropertyType::AnimationSet)
if (asset.AssetType == SEditorAsset::EAssetType::AnimParams && pProp->Type() == EPropertyType::AnimationSet)
{
CAnimationSetProperty* pAnimSet = TPropCast<CAnimationSetProperty>(pProp);
CAnimationParameters Params = pAnimSet->Value(pPropertyData);
auto* pAnimSet = TPropCast<CAnimationSetProperty>(pProp);
const CAnimationParameters Params = pAnimSet->Value(pPropertyData);
pRes = Params.AnimSet();
if (pRes)
if (pRes != nullptr)
{
uint32 MaxNumChars = static_cast<CAnimSet*>(pRes)->NumCharacters();
rOutCharIndex = (it->ForceNodeIndex >= 0 && it->ForceNodeIndex < (int32) MaxNumChars ? it->ForceNodeIndex : Params.CharacterIndex());
const uint32 MaxNumChars = static_cast<const CAnimSet*>(pRes)->NumCharacters();
rOutCharIndex = (asset.ForceNodeIndex >= 0 && asset.ForceNodeIndex < static_cast<int32>(MaxNumChars) ? asset.ForceNodeIndex : Params.CharacterIndex());
rOutAnimIndex = Params.AnimIndex();
}
}
@ -209,17 +217,17 @@ CResource* CScriptTemplate::FindDisplayAsset(void* pPropertyData, uint32& rOutCh
else
{
ASSERT(pProp->Type() == EPropertyType::Asset);
CAssetProperty* pAsset = TPropCast<CAssetProperty>(pProp);
CAssetID ID = pAsset->Value(pPropertyData);
CResourceEntry *pEntry = gpResourceStore->FindEntry( ID );
if (pEntry) pRes = pEntry->Load();
auto* pAsset = TPropCast<CAssetProperty>(pProp);
const CAssetID ID = pAsset->Value(pPropertyData);
if (CResourceEntry* pEntry = gpResourceStore->FindEntry(ID))
pRes = pEntry->Load();
}
}
// If we have a valid resource, return
if (pRes)
if (pRes != nullptr)
{
rOutIsInGame = (pRes->Type() != EResourceType::Texture && it->AssetSource == SEditorAsset::EAssetSource::Property);
rOutIsInGame = (pRes->Type() != EResourceType::Texture && asset.AssetSource == SEditorAsset::EAssetSource::Property);
return pRes;
}
}
@ -230,29 +238,31 @@ CResource* CScriptTemplate::FindDisplayAsset(void* pPropertyData, uint32& rOutCh
CCollisionMeshGroup* CScriptTemplate::FindCollision(void* pPropertyData)
{
for (auto it = mAssets.begin(); it != mAssets.end(); it++)
for (const auto& asset : mAssets)
{
if (it->AssetType != SEditorAsset::EAssetType::Collision) continue;
if (asset.AssetType != SEditorAsset::EAssetType::Collision)
continue;
CResource *pRes = nullptr;
// File
if (it->AssetSource == SEditorAsset::EAssetSource::File)
pRes = gpResourceStore->LoadResource(it->AssetLocation);
// Property
else
if (asset.AssetSource == SEditorAsset::EAssetSource::File)
{
IProperty* pProp = mpProperties->ChildByIDString(it->AssetLocation);
pRes = gpResourceStore->LoadResource(asset.AssetLocation);
}
else // Property
{
IProperty* pProp = mpProperties->ChildByIDString(asset.AssetLocation);
if (pProp->Type() == EPropertyType::Asset)
{
CAssetProperty* pAsset = TPropCast<CAssetProperty>(pProp);
auto* pAsset = TPropCast<CAssetProperty>(pProp);
pRes = gpResourceStore->LoadResource( pAsset->Value(pPropertyData), EResourceType::DynamicCollision );
}
}
// Verify resource exists + is correct type
if (pRes && (pRes->Type() == EResourceType::DynamicCollision))
if (pRes != nullptr && (pRes->Type() == EResourceType::DynamicCollision))
return static_cast<CCollisionMeshGroup*>(pRes);
}