forked from stepmania/stepmania
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStepsUtil.cpp
421 lines (366 loc) · 11.9 KB
/
StepsUtil.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#include "global.h"
#include "StepsUtil.h"
#include "Steps.h"
#include "ProfileManager.h"
#include "Profile.h"
#include "Song.h"
#include "SongManager.h"
#include "GameManager.h"
#include "XmlFile.h"
#include "UnlockManager.h"
#include "SongUtil.h"
#include <unordered_map>
using std::vector;
bool StepsCriteria::Matches( const Song *pSong, const Steps *pSteps ) const
{
if( m_difficulty != Difficulty_Invalid && pSteps->GetDifficulty() != m_difficulty )
return false;
if( m_iLowMeter != -1 && pSteps->GetMeter() < m_iLowMeter )
return false;
if( m_iHighMeter != -1 && pSteps->GetMeter() > m_iHighMeter )
return false;
if( m_st != StepsType_Invalid && pSteps->m_StepsType != m_st )
return false;
switch( m_Locked )
{
DEFAULT_FAIL(m_Locked);
case Locked_Locked:
if( UNLOCKMAN && !UNLOCKMAN->StepsIsLocked(pSong,pSteps) )
return false;
break;
case Locked_Unlocked:
if( UNLOCKMAN && UNLOCKMAN->StepsIsLocked(pSong,pSteps) )
return false;
break;
case Locked_DontCare:
break;
}
return true;
}
void StepsUtil::GetAllMatching( const SongCriteria &soc, const StepsCriteria &stc, vector<SongAndSteps> &out )
{
const std::string &sGroupName = soc.m_sGroupName.empty()? GROUP_ALL:soc.m_sGroupName;
const vector<Song*> &songs = SONGMAN->GetSongs( sGroupName );
for (auto *so: songs)
{
if( !soc.Matches(so) )
continue;
GetAllMatching( so, stc, out );
}
}
void StepsUtil::GetAllMatching( Song *pSong, const StepsCriteria &stc, vector<SongAndSteps> &out )
{
const vector<Steps*> &vSteps = ( stc.m_st == StepsType_Invalid ? pSong->GetAllSteps() :
pSong->GetStepsByStepsType(stc.m_st) );
for (auto *st: vSteps)
{
if( stc.Matches(pSong, st) )
out.push_back( SongAndSteps(pSong, st) );
}
}
void StepsUtil::GetAllMatchingEndless( Song *pSong, const StepsCriteria &stc, vector<SongAndSteps> &out )
{
const vector<Steps*> &vSteps = ( stc.m_st == StepsType_Invalid ? pSong->GetAllSteps() :
pSong->GetStepsByStepsType( stc.m_st ) );
int previousSize = out.size();
int successful = false;
GetAllMatching( pSong, stc, out );
if( out.size() != previousSize )
{
successful = true;
}
if( !successful )
{
Difficulty difficulty = ( *( vSteps.begin() ) )->GetDifficulty();
Difficulty previousDifficulty = difficulty;
int lowestDifficultyIndex = 0;
vector<Difficulty> difficulties;
for (auto st = vSteps.begin(); st != vSteps.end(); ++st)
{
previousDifficulty = difficulty;
difficulty = ( *st )->GetDifficulty();
if( ( st - vSteps.begin() ) == 0 )
{
continue;
}
if( difficulty < previousDifficulty )
{
lowestDifficultyIndex = st - vSteps.begin();
}
}
out.push_back( SongAndSteps( pSong, vSteps.at( lowestDifficultyIndex ) ) );
}
}
bool StepsUtil::HasMatching( const SongCriteria &soc, const StepsCriteria &stc )
{
const std::string &sGroupName = soc.m_sGroupName.empty()? GROUP_ALL:soc.m_sGroupName;
const vector<Song*> &songs = SONGMAN->GetSongs( sGroupName );
auto hasMatch = [&soc, &stc](Song const *song) {
return soc.Matches(song) && HasMatching(song, stc);
};
return std::any_of(songs.begin(), songs.end(), hasMatch);
}
bool StepsUtil::HasMatching( const Song *pSong, const StepsCriteria &stc )
{
const vector<Steps*> &vSteps = stc.m_st == StepsType_Invalid? pSong->GetAllSteps():pSong->GetStepsByStepsType( stc.m_st );
auto hasMatch = [pSong, &stc](Steps const *step) {
return stc.Matches(pSong, step);
};
return std::any_of(vSteps.begin(), vSteps.end(), hasMatch);
}
// Sorting stuff
std::unordered_map<const Steps*, std::string> steps_sort_val;
static bool CompareStepsPointersBySortValueAscending(const Steps *pSteps1, const Steps *pSteps2)
{
return steps_sort_val[pSteps1] < steps_sort_val[pSteps2];
}
static bool CompareStepsPointersBySortValueDescending(const Steps *pSteps1, const Steps *pSteps2)
{
return steps_sort_val[pSteps1] > steps_sort_val[pSteps2];
}
void StepsUtil::SortStepsPointerArrayByNumPlays( vector<Steps*> &vStepsPointers, ProfileSlot slot, bool bDescending )
{
if( !PROFILEMAN->IsPersistentProfile(slot) )
return; // nothing to do since we don't have data
Profile* pProfile = PROFILEMAN->GetProfile(slot);
SortStepsPointerArrayByNumPlays( vStepsPointers, pProfile, bDescending );
}
void StepsUtil::SortStepsPointerArrayByNumPlays( vector<Steps*> &vStepsPointers, const Profile* pProfile, bool bDecending )
{
// ugly...
vector<Song*> vpSongs = SONGMAN->GetAllSongs();
vector<Steps*> vpAllSteps;
std::unordered_map<Steps*,Song*> mapStepsToSong;
{
for (auto *pSong: vpSongs)
{
vector<Steps*> vpSteps = pSong->GetAllSteps();
for (auto *pSteps: vpSteps)
{
if( pSteps->IsAutogen() )
continue; // skip
vpAllSteps.push_back( pSteps );
mapStepsToSong[pSteps] = pSong;
}
}
}
ASSERT( pProfile != nullptr );
for (auto *pSteps: vStepsPointers)
{
Song* pSong = mapStepsToSong[pSteps];
steps_sort_val[pSteps] = fmt::sprintf("%9i", pProfile->GetStepsNumTimesPlayed(pSong,pSteps));
}
stable_sort( vStepsPointers.begin(), vStepsPointers.end(), bDecending ? CompareStepsPointersBySortValueDescending : CompareStepsPointersBySortValueAscending );
steps_sort_val.clear();
}
bool StepsUtil::CompareNotesPointersByRadarValues(const Steps* pSteps1, const Steps* pSteps2)
{
float fScore1 = 0;
float fScore2 = 0;
fScore1 += pSteps1->GetRadarValues( PLAYER_1 )[RadarCategory_TapsAndHolds];
fScore2 += pSteps2->GetRadarValues( PLAYER_1 )[RadarCategory_TapsAndHolds];
return fScore1 < fScore2;
}
bool StepsUtil::CompareNotesPointersByMeter(const Steps *pSteps1, const Steps* pSteps2)
{
return pSteps1->GetMeter() < pSteps2->GetMeter();
}
bool StepsUtil::CompareNotesPointersByDifficulty(const Steps *pSteps1, const Steps *pSteps2)
{
return pSteps1->GetDifficulty() < pSteps2->GetDifficulty();
}
void StepsUtil::SortNotesArrayByDifficulty( vector<Steps*> &arraySteps )
{
/* Sort in reverse order of priority. Sort by description first, to get
* a predictable order for songs with no radar values (edits). */
stable_sort( arraySteps.begin(), arraySteps.end(), CompareStepsPointersByDescription );
stable_sort( arraySteps.begin(), arraySteps.end(), CompareNotesPointersByRadarValues );
stable_sort( arraySteps.begin(), arraySteps.end(), CompareNotesPointersByMeter );
stable_sort( arraySteps.begin(), arraySteps.end(), CompareNotesPointersByDifficulty );
}
bool StepsUtil::CompareStepsPointersByTypeAndDifficulty(const Steps *pStep1, const Steps *pStep2)
{
if( pStep1->m_StepsType < pStep2->m_StepsType )
return true;
if( pStep1->m_StepsType > pStep2->m_StepsType )
return false;
return pStep1->GetDifficulty() < pStep2->GetDifficulty();
}
void StepsUtil::SortStepsByTypeAndDifficulty( vector<Steps*> &arraySongPointers )
{
sort( arraySongPointers.begin(), arraySongPointers.end(), CompareStepsPointersByTypeAndDifficulty );
}
bool StepsUtil::CompareStepsPointersByDescription(const Steps *pStep1, const Steps *pStep2)
{
Rage::ci_ascii_string a{ pStep1->GetDescription().c_str() };
Rage::ci_ascii_string b{ pStep2->GetDescription().c_str() };
return a < b;
}
void StepsUtil::SortStepsByDescription( vector<Steps*> &arraySongPointers )
{
sort( arraySongPointers.begin(), arraySongPointers.end(), CompareStepsPointersByDescription );
}
void StepsUtil::RemoveLockedSteps( const Song *pSong, vector<Steps*> &vpSteps )
{
for( int i=vpSteps.size()-1; i>=0; i-- )
{
if( UNLOCKMAN->StepsIsLocked(pSong, vpSteps[i]) )
vpSteps.erase( vpSteps.begin()+i );
}
}
////////////////////////////////
// StepsID
////////////////////////////////
void StepsID::FromSteps( const Steps *p )
{
if( p == nullptr )
{
st = StepsType_Invalid;
dc = Difficulty_Invalid;
sDescription = "";
uHash = 0;
}
else
{
st = p->m_StepsType;
dc = p->GetDifficulty();
if( dc == Difficulty_Edit )
{
sDescription = p->GetDescription();
uHash = p->GetHash();
}
else
{
sDescription = "";
uHash = 0;
}
}
m_Cache.Unset();
}
/* XXX: Don't allow duplicate edit descriptions, and don't allow edit descriptions
* to be difficulty names (eg. "Hard"). If we do that, this will be completely unambiguous.
*
* XXX: Unless two memcards are inserted and there's overlap in the names. In that
* case, maybe both edits should be renamed to "Pn: foo"; as long as we don't write
* them back out (which we don't do except in the editor), it won't be permanent.
* We could do this during the actual Steps::GetID() call, instead, but then it'd have
* to have access to Song::m_LoadedFromProfile. */
Steps *StepsID::ToSteps( const Song *p, bool bAllowNull ) const
{
if( st == StepsType_Invalid || dc == Difficulty_Invalid )
return nullptr;
SongID songID;
songID.FromSong( p );
Steps *pRet = nullptr;
if( dc == Difficulty_Edit )
{
pRet = SongUtil::GetOneSteps( p, st, dc, -1, -1, sDescription, "", uHash, true );
}
else
{
pRet = SongUtil::GetOneSteps( p, st, dc, -1, -1, "", "", 0, true );
}
if( !bAllowNull && pRet == nullptr )
FAIL_M( fmt::sprintf("%i, %i, \"%s\"", st, dc, sDescription.c_str()) );
m_Cache.Set( pRet );
return pRet;
}
XNode* StepsID::CreateNode() const
{
XNode* pNode = new XNode( "Steps" );
pNode->AppendAttr( "StepsType", GAMEMAN->GetStepsTypeInfo(st).stepTypeName );
pNode->AppendAttr( "Difficulty", DifficultyToString(dc) );
if( dc == Difficulty_Edit )
{
pNode->AppendAttr( "Description", sDescription );
pNode->AppendAttr( "Hash", uHash );
}
return pNode;
}
void StepsID::LoadFromNode( const XNode* pNode )
{
ASSERT( pNode->GetName() == "Steps" );
std::string sTemp;
pNode->GetAttrValue( "StepsType", sTemp );
st = GAMEMAN->StringToStepsType( sTemp );
pNode->GetAttrValue( "Difficulty", sTemp );
dc = StringToDifficulty( sTemp );
if( dc == Difficulty_Edit )
{
pNode->GetAttrValue( "Description", sDescription );
pNode->GetAttrValue( "Hash", uHash );
}
else
{
sDescription = "";
uHash = 0;
}
m_Cache.Unset();
}
std::string StepsID::ToString() const
{
std::string s = GAMEMAN->GetStepsTypeInfo( st ).stepTypeName;
s += " " + DifficultyToString( dc );
if( dc == Difficulty_Edit )
{
s += " " + sDescription;
s += fmt::sprintf(" %u", uHash );
}
return s;
}
bool StepsID::IsValid() const
{
return st != StepsType_Invalid && dc != Difficulty_Invalid;
}
bool StepsID::operator<( const StepsID &rhs ) const
{
#define COMP(a) if(a<rhs.a) return true; if(a>rhs.a) return false;
COMP(st);
COMP(dc);
COMP(sDescription);
// See explanation in class declaration. -Kyz
if(uHash != 0 && rhs.uHash != 0)
{
COMP(uHash);
}
#undef COMP
return false;
}
bool StepsID::operator==(const StepsID &rhs) const
{
#define COMP(a) if(a != rhs.a) return false;
COMP(st);
COMP(dc);
COMP(sDescription);
// See explanation in class declaration. -Kyz
if(uHash != 0 && rhs.uHash != 0)
{
COMP(uHash);
}
#undef COMP
return true;
}
/*
* (c) 2001-2004 Chris Danford, Glenn Maynard
* 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.
*/