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
+42 -22
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,12 +54,19 @@ 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)
{
LuaHelpers::ReportScriptErrorFmt("Error trying to read \"%s\" to load AI player skill settings.", AI_PATH);
for(int i= 0; i < NUM_SKILL_LEVELS; ++i)
{
g_Distributions[i].SetDefaultWeights();
g_Distributions[i].ChangeWeightsToPercents();
}
}
else
{
for( int i=0; i<NUM_SKILL_LEVELS; i++ ) for( int i=0; i<NUM_SKILL_LEVELS; i++ )
{ {
RString sKey = ssprintf("Skill%d", i); RString sKey = ssprintf("Skill%d", i);
@@ -44,21 +74,15 @@ void PlayerAI::InitFromDisk()
TapScoreDistribution& dist = g_Distributions[i]; TapScoreDistribution& dist = g_Distributions[i];
if( pNode == NULL ) if( pNode == NULL )
{ {
LuaHelpers::ReportScriptErrorFmt("AI.ini: \"%s\" doesn't exist.", sKey.c_str()); LuaHelpers::ReportScriptErrorFmt("AI.ini: \"%s\" section doesn't exist.", sKey.c_str());
dist.fPercent[TNS_None] = 0; dist.SetDefaultWeights();
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
{ {
#define SET_MALF_IF(condition, tns) \ #define SET_MALF_IF(condition, tns) \
if(condition) \ if(condition) \
{ \ { \
LuaHelpers::ReportScriptError("AI weight for " #tns " not set."); \ LuaHelpers::ReportScriptErrorFmt("AI weight for " #tns " in \"%s\" section not set.", sKey.c_str()); \
dist.fPercent[tns]= 0; \ dist.fPercent[tns]= 0; \
} }
dist.fPercent[TNS_None] = 0; dist.fPercent[TNS_None] = 0;
@@ -74,14 +98,10 @@ void PlayerAI::InitFromDisk()
SET_MALF_IF(!bSuccess, TNS_W2); SET_MALF_IF(!bSuccess, TNS_W2);
bSuccess = pNode->GetAttrValue( "WeightW1", dist.fPercent[TNS_W1] ); bSuccess = pNode->GetAttrValue( "WeightW1", dist.fPercent[TNS_W1] );
SET_MALF_IF(!bSuccess, TNS_W1); SET_MALF_IF(!bSuccess, TNS_W1);
#undef SET_MALF_IF #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;
} }
} }