forked from stepmania/stepmania
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerAI.cpp
170 lines (155 loc) · 5.27 KB
/
PlayerAI.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "global.h"
#include "PlayerAI.h"
#include "RageUtil.h"
#include "IniFile.h"
#include "GameState.h"
#include "PlayerState.h"
#define AI_PATH "Data/AI.ini"
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);
float fCumulativePercent = 0;
for( int i=0; i<=TNS_W1; i++ )
{
fCumulativePercent += fPercent[i];
if( fRand <= fCumulativePercent+1e-4 ) // rounding error
return (TapNoteScore)i;
}
// the fCumulativePercents must sum to 1.0, so we should never get here!
ASSERT_M( 0, fmt::sprintf("%f,%f",fRand,fCumulativePercent) );
return TNS_W1;
}
};
static TapScoreDistribution g_Distributions[NUM_SKILL_LEVELS];
void PlayerAI::InitFromDisk()
{
IniFile ini;
bool bSuccess = ini.ReadFile( AI_PATH );
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++ )
{
std::string sKey = fmt::sprintf("Skill%d", i);
XNode* pNode = ini.GetChild(sKey);
TapScoreDistribution& dist = g_Distributions[i];
if( pNode == nullptr )
{
LuaHelpers::ReportScriptErrorFmt("AI.ini: \"%s\" section doesn't exist.", sKey.c_str());
dist.SetDefaultWeights();
}
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();
}
}
}
TapNoteScore PlayerAI::GetTapNoteScore( const PlayerState* pPlayerState )
{
if( pPlayerState->m_PlayerController == PC_AUTOPLAY )
return TNS_W1;
/*
// handle replay data playback -aj
if( pPlayerState->m_PlayerController == PC_REPLAY )
{
// ghost house
}
*/
int iCpuSkill = pPlayerState->m_iCpuSkill;
/* 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. */
// Idea: weight certain modifiers (boomerang, tornado) more? (to simulate
// readability problems) -aj
if( !GAMESTATE->m_bDemonstrationOrJukebox )
{
int iSumOfAttackLevels =
pPlayerState->m_fSecondsUntilAttacksPhasedOut > 0 ?
pPlayerState->m_iLastPositiveSumOfAttackLevels :
0;
ASSERT_M( iCpuSkill>=0 && iCpuSkill<NUM_SKILL_LEVELS, fmt::sprintf("%i", iCpuSkill) );
ASSERT_M( pPlayerState->m_PlayerController == PC_CPU, fmt::sprintf("%i", pPlayerState->m_PlayerController) );
iCpuSkill -= iSumOfAttackLevels*3;
iCpuSkill = Rage::clamp( iCpuSkill, 0, NUM_SKILL_LEVELS-1 );
}
TapScoreDistribution& distribution = g_Distributions[iCpuSkill];
return distribution.GetTapNoteScore();
}
/*
* (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.
*/