forked from stepmania/stepmania
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrail.cpp
328 lines (297 loc) · 8.46 KB
/
Trail.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
#include "global.h"
#include "Trail.h"
#include "GameState.h"
#include "Steps.h"
#include "Song.h"
#include "PlayerOptions.h"
#include "NoteData.h"
#include "NoteDataUtil.h"
#include "CommonMetrics.h"
#include <numeric>
using std::vector;
void TrailEntry::GetAttackArray( AttackArray &out ) const
{
if( !Modifiers.empty() )
out.push_back( Attack::FromGlobalCourseModifier( Modifiers ) );
out.insert( out.end(), Attacks.begin(), Attacks.end() );
}
bool TrailEntry::operator== ( const TrailEntry &rhs ) const
{
#define EQUAL(a) (a==rhs.a)
return
EQUAL(pSong) &&
EQUAL(pSteps) &&
EQUAL(Modifiers) &&
EQUAL(Attacks) &&
EQUAL(bSecret) &&
EQUAL(iLowMeter) &&
EQUAL(iHighMeter) &&
EQUAL(dc);
}
bool TrailEntry::ContainsTransformOrTurn() const
{
PlayerOptions po;
po.FromString( Modifiers );
if( po.ContainsTransformOrTurn() )
return true;
if( Attacks.ContainsTransformOrTurn() )
return true;
return false;
}
// TrailEntry lua start
#include "LuaBinding.h"
/** @brief Allow Lua to have access to the TrailEntry. */
class LunaTrailEntry: public Luna<TrailEntry>
{
public:
static int GetSong( T* p, lua_State *L )
{
if( p->pSong )
p->pSong->PushSelf(L);
else
lua_pushnil(L);
return 1;
}
static int GetSteps( T* p, lua_State *L )
{
if( p->pSteps )
p->pSteps->PushSelf(L);
else
lua_pushnil(L);
return 1;
}
DEFINE_METHOD( IsSecret, bSecret );
DEFINE_METHOD( GetNormalModifiers, Modifiers );
LunaTrailEntry()
{
ADD_METHOD( GetSong );
ADD_METHOD( GetSteps );
ADD_METHOD( IsSecret );
ADD_METHOD( GetNormalModifiers );
}
};
LUA_REGISTER_CLASS( TrailEntry )
// TrailEntry lua end
void Trail::SetRadarValues( const RadarValues &rv )
{
m_CachedRadarValues = rv;
m_bRadarValuesCached = true;
}
const RadarValues &Trail::GetRadarValues() const
{
if( m_bRadarValuesCached )
{
return m_CachedRadarValues;
}
if( IsSecret() )
{
// Don't calculate RadarValues for a non-fixed Course. They values are
// worthless because they'll change every time this Trail is
// regenerated.
m_CachedRadarValues = RadarValues();
return m_CachedRadarValues;
}
else
{
RadarValues rv;
rv.Zero();
for (auto &e: m_vEntries)
{
const Steps *pSteps = e.pSteps;
ASSERT( pSteps != nullptr );
// Hack: don't calculate for autogen entries
if( !pSteps->IsAutogen() && e.ContainsTransformOrTurn() )
{
NoteData nd;
pSteps->GetNoteData( nd );
RadarValues rv_orig;
GAMESTATE->SetProcessedTimingData(const_cast<TimingData *>(pSteps->GetTimingData()));
NoteDataUtil::CalculateRadarValues( nd, e.pSong->m_fMusicLengthSeconds, rv_orig );
PlayerOptions po;
po.FromString( e.Modifiers );
if( po.ContainsTransformOrTurn() )
{
NoteDataUtil::TransformNoteData(nd, *(pSteps->GetTimingData()), po, pSteps->m_StepsType);
}
NoteDataUtil::TransformNoteData(nd, *(pSteps->GetTimingData()), e.Attacks, pSteps->m_StepsType, e.pSong);
RadarValues transformed_rv;
NoteDataUtil::CalculateRadarValues( nd, e.pSong->m_fMusicLengthSeconds, transformed_rv );
GAMESTATE->SetProcessedTimingData(nullptr);
rv += transformed_rv;
}
else
{
rv += pSteps->GetRadarValues( PLAYER_1 );
}
}
/* Hack: SetRadarValues is non-const (a const setter doesn't
* make sense), but it only modifies a mutable value. Just
* cast away const. */
const_cast<Trail*>(this)->SetRadarValues( rv );
return m_CachedRadarValues;
}
}
int Trail::GetMeter() const
{
if( m_iSpecifiedMeter != -1 )
return m_iSpecifiedMeter;
if( m_vEntries.empty() )
return 0;
float fMeter = GetTotalMeter() / (float)m_vEntries.size();
return std::lrint( fMeter );
}
int Trail::GetTotalMeter() const
{
auto getMeter = [](int meter, TrailEntry const &e) {
return meter + e.pSteps->GetMeter();
};
return std::accumulate(m_vEntries.begin(), m_vEntries.end(), 0, getMeter);
}
float Trail::GetLengthSeconds() const
{
auto getSeconds = [](float curr, TrailEntry const &e) {
return curr + e.pSong->m_fMusicLengthSeconds;
};
return std::accumulate(m_vEntries.begin(), m_vEntries.end(), 0.f, getSeconds);
}
void Trail::GetDisplayBpms( DisplayBpms &AddTo ) const
{
for (auto &e: m_vEntries)
{
if( e.bSecret )
{
AddTo.Add( -1 );
continue;
}
Song *pSong = e.pSong;
ASSERT( pSong != nullptr );
switch( pSong->m_DisplayBPMType )
{
case DISPLAY_BPM_ACTUAL:
case DISPLAY_BPM_SPECIFIED:
pSong->GetDisplayBpms( AddTo );
break;
case DISPLAY_BPM_RANDOM:
AddTo.Add( -1 );
break;
DEFAULT_FAIL( pSong->m_DisplayBPMType );
}
}
}
bool Trail::IsSecret() const
{
auto isSecret = [](TrailEntry const &e) {
return e.bSecret;
};
return std::any_of(m_vEntries.begin(), m_vEntries.end(), isSecret);
}
bool Trail::ContainsSong( const Song *pSong ) const
{
auto hasSong = [pSong](TrailEntry const &e) {
return e.pSong == pSong;
};
return std::any_of(m_vEntries.begin(), m_vEntries.end(), hasSong);
}
// lua start
#include "LuaBinding.h"
/** @brief Allow Lua to have access to the Trail. */
class LunaTrail: public Luna<Trail>
{
public:
static int GetDifficulty( T* p, lua_State *L ) { LuaHelpers::Push(L, p->m_CourseDifficulty ); return 1; }
static int GetMeter( T* p, lua_State *L ) { LuaHelpers::Push(L, p->GetMeter() ); return 1; }
static int GetTotalMeter( T* p, lua_State *L ) { LuaHelpers::Push(L, p->GetTotalMeter() ); return 1; }
static int GetStepsType( T* p, lua_State *L ) { LuaHelpers::Push(L, p->m_StepsType ); return 1; }
static int GetRadarValues( T* p, lua_State *L )
{
RadarValues &rv = const_cast<RadarValues &>(p->GetRadarValues());
rv.PushSelf(L);
return 1;
}
static int GetArtists( T* p, lua_State *L )
{
vector<std::string> asArtists, asAltArtists;
for (auto &e: p->m_vEntries)
{
if( e.bSecret )
{
asArtists.push_back( "???" );
asAltArtists.push_back( "???" );
}
else
{
asArtists.push_back( e.pSong->GetDisplayArtist() );
asAltArtists.push_back( e.pSong->GetTranslitArtist() );
}
}
if( (int) asArtists.size() > CommonMetrics::MAX_COURSE_ENTRIES_BEFORE_VARIOUS )
{
asArtists.clear();
asAltArtists.clear();
asArtists.push_back( "Various Artists" );
asAltArtists.push_back( "Various Artists" );
}
LuaHelpers::CreateTableFromArray( asArtists, L );
LuaHelpers::CreateTableFromArray( asAltArtists, L );
return 2;
}
static int GetTrailEntry( T* p, lua_State *L ) { TrailEntry &te = p->m_vEntries[IArg(1)]; te.PushSelf(L); return 1; }
static int GetTrailEntries( T* p, lua_State *L )
{
vector<TrailEntry*> v;
for (auto &entry: p->m_vEntries)
{
v.push_back(&entry);
}
LuaHelpers::CreateTableFromArray<TrailEntry*>( v, L );
return 1;
}
DEFINE_METHOD( GetLengthSeconds, GetLengthSeconds() )
DEFINE_METHOD( IsSecret, IsSecret() )
static int ContainsSong( T* p, lua_State *L )
{
const Song *pS = Luna<Song>::check(L,1);
lua_pushboolean(L, p->ContainsSong(pS));
return 1;
}
LunaTrail()
{
ADD_METHOD( GetDifficulty );
ADD_METHOD( GetMeter );
ADD_METHOD( GetTotalMeter );
ADD_METHOD( GetStepsType );
ADD_METHOD( GetRadarValues );
ADD_METHOD( GetArtists );
ADD_METHOD( GetTrailEntry );
ADD_METHOD( GetTrailEntries );
ADD_METHOD( GetLengthSeconds );
ADD_METHOD( IsSecret );
ADD_METHOD( ContainsSong );
}
};
LUA_REGISTER_CLASS( Trail )
// lua end
/*
* (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.
*/