From 1d061ac4ddb368310f4f556efa7c72e94ec6731d Mon Sep 17 00:00:00 2001 From: Kyzentun Date: Mon, 20 Oct 2014 12:26:29 -0600 Subject: [PATCH] Don't crash if AI.ini doesn't exist. --- src/PlayerAI.cpp | 108 ++++++++++++++++++++++++++++------------------- 1 file changed, 64 insertions(+), 44 deletions(-) diff --git a/src/PlayerAI.cpp b/src/PlayerAI.cpp index b7bf80905e..29b73a9bf7 100644 --- a/src/PlayerAI.cpp +++ b/src/PlayerAI.cpp @@ -11,6 +11,29 @@ struct TapScoreDistribution { 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() { float fRand = randomf(0,1); @@ -31,57 +54,54 @@ static TapScoreDistribution g_Distributions[NUM_SKILL_LEVELS]; void PlayerAI::InitFromDisk() { - bool bSuccess; - IniFile ini; - bSuccess = ini.ReadFile( AI_PATH ); - ASSERT( bSuccess ); - - for( int i=0; iGetAttrValue( "WeightMiss", dist.fPercent[TNS_Miss] ); - SET_MALF_IF(!bSuccess, TNS_Miss); - bSuccess = pNode->GetAttrValue( "WeightW5", dist.fPercent[TNS_W5] ); - SET_MALF_IF(!bSuccess, TNS_W5); - 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 + else + { + #define SET_MALF_IF(condition, tns) \ + if(condition) \ + { \ + LuaHelpers::ReportScriptErrorFmt("AI weight for " #tns " in \"%s\" section not set.", sKey.c_str()); \ + dist.fPercent[tns]= 0; \ + } + dist.fPercent[TNS_None] = 0; + bSuccess = pNode->GetAttrValue( "WeightMiss", dist.fPercent[TNS_Miss] ); + SET_MALF_IF(!bSuccess, TNS_Miss); + bSuccess = pNode->GetAttrValue( "WeightW5", dist.fPercent[TNS_W5] ); + SET_MALF_IF(!bSuccess, TNS_W5); + 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