Files
itgmania212121/stepmania/src/PlayerAI.cpp
T

125 lines
4.1 KiB
C++
Raw Normal View History

2003-04-07 22:07:44 +00:00
#include "global.h"
#include "PlayerAI.h"
#include "RageUtil.h"
#include "IniFile.h"
#include "GameState.h"
#include "PlayerState.h"
2003-04-07 22:07:44 +00:00
2003-12-10 09:02:55 +00:00
#define AI_PATH "Data/AI.ini"
2003-04-07 22:07:44 +00:00
struct TapScoreDistribution
{
float fPercent[NUM_TapNoteScore];
TapNoteScore GetTapNoteScore()
2003-04-07 22:07:44 +00:00
{
float fRand = randomf(0,1);
2003-04-22 04:54:04 +00:00
float fCumulativePercent = 0;
for( int i=0; i<=TNS_W1; i++ )
2003-04-22 04:54:04 +00:00
{
fCumulativePercent += fPercent[i];
2003-05-08 22:17:22 +00:00
if( fRand <= fCumulativePercent+1e-4 ) /* rounding error */
2003-04-07 22:07:44 +00:00
return (TapNoteScore)i;
2003-04-22 04:54:04 +00:00
}
2006-04-05 02:29:57 +00:00
ASSERT_M( 0, ssprintf("%f,%f",fRand,fCumulativePercent) ); // the fCumulativePercents must sum to 1.0, so we should never get here!
return TNS_W1;
2003-04-07 22:07:44 +00:00
}
2003-04-07 22:07:44 +00:00
};
static TapScoreDistribution g_Distributions[NUM_SKILL_LEVELS];
void PlayerAI::InitFromDisk()
2003-04-07 22:07:44 +00:00
{
2006-04-05 02:29:57 +00:00
bool bSuccess;
IniFile ini;
2006-04-05 02:29:57 +00:00
bSuccess = ini.ReadFile( AI_PATH );
ASSERT( bSuccess );
for( int i=0; i<NUM_SKILL_LEVELS; i++ )
2003-04-07 22:07:44 +00:00
{
2006-01-22 01:00:06 +00:00
RString sKey = ssprintf("Skill%d", i);
2005-01-07 14:28:00 +00:00
XNode* pNode = ini.GetChild(sKey);
if( pNode == NULL )
RageException::Throw( "AI.ini: \"%s\" doesn't exist.", sKey.c_str() );
2003-04-07 22:07:44 +00:00
TapScoreDistribution& dist = g_Distributions[i];
dist.fPercent[TNS_None] = 0;
2006-04-05 02:29:57 +00:00
bSuccess = pNode->GetAttrValue( "WeightMiss", dist.fPercent[TNS_Miss] );
ASSERT( bSuccess );
bSuccess = pNode->GetAttrValue( "WeightW5", dist.fPercent[TNS_W5] );
ASSERT( bSuccess );
bSuccess = pNode->GetAttrValue( "WeightW4", dist.fPercent[TNS_W4] );
ASSERT( bSuccess );
bSuccess = pNode->GetAttrValue( "WeightW3", dist.fPercent[TNS_W3] );
ASSERT( bSuccess );
bSuccess = pNode->GetAttrValue( "WeightW2", dist.fPercent[TNS_W2] );
ASSERT( bSuccess );
bSuccess = pNode->GetAttrValue( "WeightW1", dist.fPercent[TNS_W1] );
ASSERT( bSuccess );
2003-04-22 04:54:04 +00:00
float fSum = 0;
for( int j=0; j<NUM_TapNoteScore; j++ )
2003-04-22 04:54:04 +00:00
fSum += dist.fPercent[j];
for( int j=0; j<NUM_TapNoteScore; j++ )
2003-04-22 04:54:04 +00:00
dist.fPercent[j] /= fSum;
}
}
2003-04-07 22:07:44 +00:00
TapNoteScore PlayerAI::GetTapNoteScore( const PlayerState* pPlayerState )
2003-04-07 22:07:44 +00:00
{
2005-02-07 04:43:39 +00:00
if( pPlayerState->m_PlayerController == PC_AUTOPLAY )
return TNS_W1;
2005-02-07 04:43:39 +00:00
int iCpuSkill = pPlayerState->m_iCpuSkill;
2004-01-12 03:47:55 +00:00
/* If we're in battle mode, reduce the skill based on the number of modifier attacks
* against us. If we're in demonstration or jukebox mode with modifiers attached,
* don't make demonstration miss a lot. */
if( !GAMESTATE->m_bDemonstrationOrJukebox )
{
int iSumOfAttackLevels =
pPlayerState->m_fSecondsUntilAttacksPhasedOut > 0 ?
pPlayerState->m_iLastPositiveSumOfAttackLevels :
0;
ASSERT_M( iCpuSkill>=0 && iCpuSkill<NUM_SKILL_LEVELS, ssprintf("%i", iCpuSkill) );
ASSERT_M( pPlayerState->m_PlayerController == PC_CPU, ssprintf("%i", pPlayerState->m_PlayerController) );
iCpuSkill -= iSumOfAttackLevels*3;
CLAMP( iCpuSkill, 0, NUM_SKILL_LEVELS-1 );
}
TapScoreDistribution& distribution = g_Distributions[iCpuSkill];
return distribution.GetTapNoteScore();
}
2004-05-31 21:55:14 +00:00
/*
* (c) 2003-2004 Chris Danford
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/