Don't crash if AI.ini doesn't exist.

This commit is contained in:
Kyzentun
2014-10-20 12:26:29 -06:00
parent c16f045cb4
commit 1d061ac4dd
+64 -44
View File
@@ -11,6 +11,29 @@ struct TapScoreDistribution
{ {
float fPercent[NUM_TapNoteScore]; float fPercent[NUM_TapNoteScore];
void ChangeWeightsToPercents()
{
float sum= 0;
for(int i= 0; i < NUM_TapNoteScore; ++i)
{
sum+= fPercent[i];
}
for(int i= 0; i < NUM_TapNoteScore; ++i)
{
fPercent[i]/= sum;
}
}
void SetDefaultWeights()
{
fPercent[TNS_None] = 0;
fPercent[TNS_Miss] = 1;
fPercent[TNS_W5] = 0;
fPercent[TNS_W4] = 0;
fPercent[TNS_W3] = 0;
fPercent[TNS_W2] = 0;
fPercent[TNS_W1] = 0;
}
TapNoteScore GetTapNoteScore() TapNoteScore GetTapNoteScore()
{ {
float fRand = randomf(0,1); float fRand = randomf(0,1);
@@ -31,57 +54,54 @@ static TapScoreDistribution g_Distributions[NUM_SKILL_LEVELS];
void PlayerAI::InitFromDisk() void PlayerAI::InitFromDisk()
{ {
bool bSuccess;
IniFile ini; IniFile ini;
bSuccess = ini.ReadFile( AI_PATH ); bool bSuccess = ini.ReadFile( AI_PATH );
ASSERT( bSuccess ); if(!bSuccess)
for( int i=0; i<NUM_SKILL_LEVELS; i++ )
{ {
RString sKey = ssprintf("Skill%d", i); LuaHelpers::ReportScriptErrorFmt("Error trying to read \"%s\" to load AI player skill settings.", AI_PATH);
XNode* pNode = ini.GetChild(sKey); for(int i= 0; i < NUM_SKILL_LEVELS; ++i)
TapScoreDistribution& dist = g_Distributions[i];
if( pNode == NULL )
{ {
LuaHelpers::ReportScriptErrorFmt("AI.ini: \"%s\" doesn't exist.", sKey.c_str()); g_Distributions[i].SetDefaultWeights();
dist.fPercent[TNS_None] = 0; g_Distributions[i].ChangeWeightsToPercents();
dist.fPercent[TNS_Miss] = 1;
dist.fPercent[TNS_W5] = 0;
dist.fPercent[TNS_W4] = 0;
dist.fPercent[TNS_W3] = 0;
dist.fPercent[TNS_W2] = 0;
dist.fPercent[TNS_W1] = 0;
} }
else }
else
{
for( int i=0; i<NUM_SKILL_LEVELS; i++ )
{ {
#define SET_MALF_IF(condition, tns) \ RString sKey = ssprintf("Skill%d", i);
if(condition) \ XNode* pNode = ini.GetChild(sKey);
{ \ TapScoreDistribution& dist = g_Distributions[i];
LuaHelpers::ReportScriptError("AI weight for " #tns " not set."); \ if( pNode == NULL )
dist.fPercent[tns]= 0; \ {
LuaHelpers::ReportScriptErrorFmt("AI.ini: \"%s\" section doesn't exist.", sKey.c_str());
dist.SetDefaultWeights();
} }
dist.fPercent[TNS_None] = 0; else
bSuccess = pNode->GetAttrValue( "WeightMiss", dist.fPercent[TNS_Miss] ); {
SET_MALF_IF(!bSuccess, TNS_Miss); #define SET_MALF_IF(condition, tns) \
bSuccess = pNode->GetAttrValue( "WeightW5", dist.fPercent[TNS_W5] ); if(condition) \
SET_MALF_IF(!bSuccess, TNS_W5); { \
bSuccess = pNode->GetAttrValue( "WeightW4", dist.fPercent[TNS_W4] ); LuaHelpers::ReportScriptErrorFmt("AI weight for " #tns " in \"%s\" section not set.", sKey.c_str()); \
SET_MALF_IF(!bSuccess, TNS_W4); dist.fPercent[tns]= 0; \
bSuccess = pNode->GetAttrValue( "WeightW3", dist.fPercent[TNS_W3] ); }
SET_MALF_IF(!bSuccess, TNS_W3); dist.fPercent[TNS_None] = 0;
bSuccess = pNode->GetAttrValue( "WeightW2", dist.fPercent[TNS_W2] ); bSuccess = pNode->GetAttrValue( "WeightMiss", dist.fPercent[TNS_Miss] );
SET_MALF_IF(!bSuccess, TNS_W2); SET_MALF_IF(!bSuccess, TNS_Miss);
bSuccess = pNode->GetAttrValue( "WeightW1", dist.fPercent[TNS_W1] ); bSuccess = pNode->GetAttrValue( "WeightW5", dist.fPercent[TNS_W5] );
SET_MALF_IF(!bSuccess, TNS_W1); SET_MALF_IF(!bSuccess, TNS_W5);
#undef SET_MALF_IF bSuccess = pNode->GetAttrValue( "WeightW4", dist.fPercent[TNS_W4] );
SET_MALF_IF(!bSuccess, TNS_W4);
bSuccess = pNode->GetAttrValue( "WeightW3", dist.fPercent[TNS_W3] );
SET_MALF_IF(!bSuccess, TNS_W3);
bSuccess = pNode->GetAttrValue( "WeightW2", dist.fPercent[TNS_W2] );
SET_MALF_IF(!bSuccess, TNS_W2);
bSuccess = pNode->GetAttrValue( "WeightW1", dist.fPercent[TNS_W1] );
SET_MALF_IF(!bSuccess, TNS_W1);
#undef SET_MALF_IF
}
dist.ChangeWeightsToPercents();
} }
float fSum = 0;
for( int j=0; j<NUM_TapNoteScore; j++ )
fSum += dist.fPercent[j];
for( int j=0; j<NUM_TapNoteScore; j++ )
dist.fPercent[j] /= fSum;
} }
} }