Files
itgmania212121/stepmania/src/Steps.cpp
T

460 lines
12 KiB
C++
Raw Normal View History

/*
* This stores a single note pattern for a song.
*
* We can have too much data to keep everything decompressed as NoteData, so most
* songs are kept in memory compressed as SMData until requested. NoteData is normally
* not requested casually during gameplay; we can move through screens, the music
* wheel, etc. without touching any NoteData.
*
* To save more memory, if data is cached on disk, read it from disk on demand. Not
* all Steps will have an associated file for this purpose. (Profile edits don't do
* this yet.)
*
* Data can be on disk (always compressed), compressed in memory, and uncompressed in
* memory.
*/
2003-02-16 04:01:45 +00:00
#include "global.h"
2003-08-03 00:57:20 +00:00
#include "Steps.h"
#include "StepsUtil.h"
#include "song.h"
2002-05-20 08:59:37 +00:00
#include "RageUtil.h"
#include "RageLog.h"
2002-07-03 03:13:13 +00:00
#include "NoteData.h"
2002-08-23 00:22:27 +00:00
#include "GameManager.h"
2003-08-07 06:36:34 +00:00
#include "NoteDataUtil.h"
#include "NotesLoaderSM.h"
2002-05-20 08:59:37 +00:00
#include <algorithm>
2003-08-03 00:57:20 +00:00
Steps::Steps()
2002-05-20 08:59:37 +00:00
{
m_bSavedToDisk = false;
2006-09-26 20:28:46 +00:00
m_StepsType = StepsType_Invalid;
2005-12-01 03:20:25 +00:00
m_LoadedFromProfile = ProfileSlot_INVALID;
2006-08-23 20:45:30 +00:00
m_iHash = 0;
m_Difficulty = DIFFICULTY_INVALID;
2002-05-20 08:59:37 +00:00
m_iMeter = 0;
2002-06-30 23:19:33 +00:00
m_pNoteData = new NoteData;
m_bNoteDataIsFilled = false;
m_sNoteDataCompressed = "";
2003-01-02 22:10:51 +00:00
parent = NULL;
2002-05-20 08:59:37 +00:00
}
2003-08-03 00:57:20 +00:00
Steps::~Steps()
2002-05-20 08:59:37 +00:00
{
}
unsigned Steps::GetHash() const
{
if( parent )
return parent->GetHash();
2006-08-23 20:45:30 +00:00
if( m_iHash )
return m_iHash;
if( m_sNoteDataCompressed.empty() )
{
if( !m_bNoteDataIsFilled )
return 0; // No data, no hash.
NoteDataUtil::GetSMNoteDataString( *m_pNoteData, m_sNoteDataCompressed );
}
2006-08-23 20:45:30 +00:00
m_iHash = GetHashForString( m_sNoteDataCompressed );
return m_iHash;
}
2004-10-23 17:43:49 +00:00
void Steps::SetNoteData( const NoteData& noteDataNew )
2002-07-03 21:27:26 +00:00
{
2004-10-23 17:43:49 +00:00
ASSERT( noteDataNew.GetNumTracks() == GameManager::StepsTypeToNumTracks(m_StepsType) );
2002-12-14 21:43:00 +00:00
if( parent )
{
copy( parent->m_CachedRadarValues, parent->m_CachedRadarValues + NUM_PLAYERS, m_CachedRadarValues );
m_sDescription = parent->m_sDescription;
m_Difficulty = parent->m_Difficulty;
m_iMeter = parent->m_iMeter;
parent = NULL;
}
2003-01-02 22:10:51 +00:00
*m_pNoteData = noteDataNew;
m_bNoteDataIsFilled = true;
m_sNoteDataCompressed = EMPTY_STRING;
2006-08-23 20:45:30 +00:00
m_iHash = 0;
m_sFilename = EMPTY_STRING; // We can no longer read from the file because it has changed in memory.
2002-07-03 21:27:26 +00:00
}
2002-05-20 08:59:37 +00:00
2004-10-23 17:43:49 +00:00
void Steps::GetNoteData( NoteData& noteDataOut ) const
2002-07-03 03:13:13 +00:00
{
2002-12-14 21:43:00 +00:00
Decompress();
2003-07-15 19:40:41 +00:00
if( m_bNoteDataIsFilled )
{
noteDataOut = *m_pNoteData;
}
2003-07-15 19:40:41 +00:00
else
{
2004-10-23 17:43:49 +00:00
noteDataOut.ClearAll();
noteDataOut.SetNumTracks( GameManager::StepsTypeToNumTracks(m_StepsType) );
2003-07-15 19:40:41 +00:00
}
2002-07-03 03:13:13 +00:00
}
2002-05-20 08:59:37 +00:00
2006-01-22 01:00:06 +00:00
void Steps::SetSMNoteData( const RString &notes_comp_ )
2002-12-14 21:33:29 +00:00
{
m_pNoteData->Init();
m_bNoteDataIsFilled = false;
2002-12-14 21:43:00 +00:00
m_sNoteDataCompressed = notes_comp_;
2006-08-23 20:45:30 +00:00
m_iHash = 0;
m_sFilename = EMPTY_STRING; // We can no longer read from the file because it has changed in memory.
2002-12-14 21:33:29 +00:00
}
2005-04-28 06:15:01 +00:00
/* XXX: this function should pull data from m_sFilename, like Decompress() */
2006-01-22 01:00:06 +00:00
void Steps::GetSMNoteData( RString &notes_comp_out ) const
2002-12-14 21:33:29 +00:00
{
if( m_sNoteDataCompressed.empty() )
2002-12-14 21:43:00 +00:00
{
if( !m_bNoteDataIsFilled )
2003-11-17 03:38:24 +00:00
{
/* no data is no data */
2004-10-23 23:41:49 +00:00
notes_comp_out = "";
2003-11-17 03:38:24 +00:00
return;
}
NoteDataUtil::GetSMNoteDataString( *m_pNoteData, m_sNoteDataCompressed );
2002-12-14 21:43:00 +00:00
}
notes_comp_out = m_sNoteDataCompressed;
2002-12-14 21:33:29 +00:00
}
2003-12-18 21:24:30 +00:00
float Steps::PredictMeter() const
2003-12-18 20:35:46 +00:00
{
2003-12-19 03:18:52 +00:00
float pMeter = 0.775f;
2006-01-07 04:11:29 +00:00
const float RadarCoeffs[NUM_RadarCategory] =
2003-12-19 04:11:11 +00:00
{
10.1f, 5.27f,-0.905f, -1.10f, 2.86f,
2005-04-25 09:35:22 +00:00
0,0,0,0,0,0
2003-12-19 04:11:11 +00:00
};
const RadarValues &rv = GetRadarValues( PLAYER_1 );
2006-01-07 04:11:29 +00:00
for( int r = 0; r < NUM_RadarCategory; ++r )
pMeter += rv[r] * RadarCoeffs[r];
2003-12-19 03:18:52 +00:00
2006-01-07 04:11:29 +00:00
const float DifficultyCoeffs[NUM_Difficulty] =
2003-12-19 04:11:11 +00:00
{
2004-02-08 01:05:53 +00:00
-0.877f, -0.877f, 0, 0.722f, 0.722f, 0
2003-12-19 04:11:11 +00:00
};
2003-12-19 03:35:23 +00:00
pMeter += DifficultyCoeffs[this->GetDifficulty()];
2003-12-19 03:18:52 +00:00
2003-12-18 21:23:52 +00:00
// Init non-radar values
const float SV = rv[RadarCategory_Stream] * rv[RadarCategory_Voltage];
const float ChaosSquare = rv[RadarCategory_Chaos] * rv[RadarCategory_Chaos];
2003-12-19 03:35:23 +00:00
pMeter += -6.35f * SV;
pMeter += -2.58f * ChaosSquare;
2003-12-22 18:26:17 +00:00
if (pMeter < 1) pMeter = 1;
2003-12-18 20:35:46 +00:00
return pMeter;
}
2003-08-03 00:57:20 +00:00
void Steps::TidyUpData()
2002-10-06 16:56:58 +00:00
{
if( GetDifficulty() == DIFFICULTY_INVALID )
2003-12-03 00:28:17 +00:00
SetDifficulty( StringToDifficulty(GetDescription()) );
if( GetDifficulty() == DIFFICULTY_INVALID )
{
2006-02-25 00:08:55 +00:00
if( GetMeter() == 1 ) SetDifficulty( DIFFICULTY_BEGINNER );
2003-12-03 00:28:17 +00:00
else if( GetMeter() <= 3 ) SetDifficulty( DIFFICULTY_EASY );
else if( GetMeter() <= 6 ) SetDifficulty( DIFFICULTY_MEDIUM );
2006-02-25 00:08:55 +00:00
else SetDifficulty( DIFFICULTY_HARD );
2002-10-06 16:56:58 +00:00
}
2003-12-03 00:28:17 +00:00
if( GetMeter() < 1) // meter is invalid
2003-12-19 07:28:32 +00:00
SetMeter( int(PredictMeter()) );
2002-05-20 08:59:37 +00:00
}
void Steps::CalculateRadarValues( float fMusicLengthSeconds )
{
// If we're autogen, don't calculate values. GetRadarValues will take from our parent.
if( parent != NULL )
return;
// Do write radar values, and leave it up to the reading app whether they want to trust
// the cached values without recalculating them.
/*
// If we're an edit, leave the RadarValues invalid.
if( IsAnEdit() )
return;
*/
NoteData tempNoteData;
this->GetNoteData( tempNoteData );
FOREACH_PlayerNumber( pn )
m_CachedRadarValues[pn].Zero();
if( tempNoteData.IsComposite() )
{
vector<NoteData> vParts;
NoteDataUtil::SplitCompositeNoteData( tempNoteData, vParts );
for( size_t pn = 0; pn < min(vParts.size(), size_t(NUM_PLAYERS)); ++pn )
NoteDataUtil::CalculateRadarValues( vParts[pn], fMusicLengthSeconds, m_CachedRadarValues[pn] );
}
else
{
NoteDataUtil::CalculateRadarValues( tempNoteData, fMusicLengthSeconds, m_CachedRadarValues[0] );
fill_n( m_CachedRadarValues + 1, NUM_PLAYERS-1, m_CachedRadarValues[0] );
}
}
2003-08-03 00:57:20 +00:00
void Steps::Decompress() const
2002-12-14 21:43:00 +00:00
{
if( m_bNoteDataIsFilled )
2003-03-13 01:26:50 +00:00
return; // already decompressed
if( parent )
2003-01-02 22:10:51 +00:00
{
// get autogen m_pNoteData
2004-10-23 17:43:49 +00:00
NoteData notedata;
parent->GetNoteData( notedata );
2003-01-02 22:10:51 +00:00
m_bNoteDataIsFilled = true;
2006-09-12 04:27:48 +00:00
int iNewTracks = GameManager::StepsTypeToNumTracks( m_StepsType );
2004-05-20 19:05:37 +00:00
if( this->m_StepsType == STEPS_TYPE_LIGHTS_CABINET )
{
NoteDataUtil::LoadTransformedLights( notedata, *m_pNoteData, iNewTracks );
}
else
{
NoteDataUtil::LoadTransformedSlidingWindow( notedata, *m_pNoteData, iNewTracks );
NoteDataUtil::RemoveStretch( *m_pNoteData, m_StepsType );
2004-05-20 19:05:37 +00:00
}
return;
}
if( !m_sFilename.empty() && m_sNoteDataCompressed.empty() )
{
/* We have data on disk and not in memory. Load it. */
Song s;
SMLoader ld;
if( !ld.LoadFromSMFile( m_sFilename, s, true ) )
{
LOG->Warn( "Couldn't load \"%s\"", m_sFilename.c_str() );
return;
}
/* Find the steps. */
StepsID ID;
ID.FromSteps( this );
2004-08-12 04:49:15 +00:00
Steps *pSteps = ID.ToSteps( &s, true, false ); // don't use cache
if( pSteps == NULL )
{
LOG->Warn( "Couldn't find %s in \"%s\"", ID.ToString().c_str(), m_sFilename.c_str() );
return;
}
pSteps->GetSMNoteData( m_sNoteDataCompressed );
2003-01-02 22:10:51 +00:00
}
if( m_sNoteDataCompressed.empty() )
2003-03-13 01:26:50 +00:00
{
2003-08-07 06:16:17 +00:00
/* there is no data, do nothing */
2003-03-13 01:26:50 +00:00
}
else
{
// load from compressed
bool bComposite = m_StepsType == STEPS_TYPE_DANCE_ROUTINE;
m_bNoteDataIsFilled = true;
m_pNoteData->SetNumTracks( GameManager::StepsTypeToNumTracks(m_StepsType) );
2003-01-02 22:10:51 +00:00
NoteDataUtil::LoadFromSMNoteDataString( *m_pNoteData, m_sNoteDataCompressed, bComposite );
2003-03-13 01:26:50 +00:00
}
2002-12-14 21:43:00 +00:00
}
2003-08-03 00:57:20 +00:00
void Steps::Compress() const
2002-12-14 21:43:00 +00:00
{
/* Always leave lights data uncompressed. */
if( this->m_StepsType == STEPS_TYPE_LIGHTS_CABINET && m_bNoteDataIsFilled )
{
m_sNoteDataCompressed = EMPTY_STRING;
return;
}
2006-03-11 06:06:48 +00:00
if( !m_sFilename.empty() && m_LoadedFromProfile == ProfileSlot_INVALID )
{
2006-03-11 06:06:48 +00:00
/*
* We have a file on disk; clear all data in memory.
*
* Data on profiles can't be accessed normally (need to mount and time-out the
* device), and when we start a game and load edits, we want to be sure that
* it'll be available if the user picks it and pulls the device. Also,
* Decompress() doesn't know how to load .edits.
*/
m_pNoteData->Init();
m_bNoteDataIsFilled = false;
2004-10-30 07:18:28 +00:00
/* Be careful; 'x = ""', m_sNoteDataCompressed.clear() and m_sNoteDataCompressed.reserve(0)
2006-06-20 06:34:16 +00:00
* don't always free the allocated memory. */
m_sNoteDataCompressed = EMPTY_STRING;
2005-04-28 06:15:01 +00:00
return;
}
/* We have no file on disk. Compress the data, if necessary. */
if( m_sNoteDataCompressed.empty() )
2002-12-14 21:43:00 +00:00
{
2005-04-28 06:15:01 +00:00
if( !m_bNoteDataIsFilled )
return; /* no data is no data */
NoteDataUtil::GetSMNoteDataString( *m_pNoteData, m_sNoteDataCompressed );
2002-12-14 21:43:00 +00:00
}
m_pNoteData->Init();
m_bNoteDataIsFilled = false;
2002-12-14 21:43:00 +00:00
}
2003-01-02 22:10:51 +00:00
/* Copy our parent's data. This is done when we're being changed from autogen
* to normal. (needed?) */
2003-08-03 00:57:20 +00:00
void Steps::DeAutogen()
2003-01-02 22:10:51 +00:00
{
if( !parent )
2003-01-02 22:10:51 +00:00
return; /* OK */
Decompress(); // fills in m_pNoteData with sliding window transform
2003-03-13 01:26:50 +00:00
2006-02-25 00:08:55 +00:00
m_sDescription = Real()->m_sDescription;
m_Difficulty = Real()->m_Difficulty;
2004-02-07 05:57:19 +00:00
m_iMeter = Real()->m_iMeter;
copy( Real()->m_CachedRadarValues, Real()->m_CachedRadarValues + NUM_PLAYERS, m_CachedRadarValues );
2003-01-02 22:10:51 +00:00
parent = NULL;
2003-03-13 01:26:50 +00:00
Compress();
2003-01-02 22:10:51 +00:00
}
2003-12-29 22:50:54 +00:00
void Steps::AutogenFrom( const Steps *parent_, StepsType ntTo )
2003-01-02 22:10:51 +00:00
{
parent = parent_;
2003-08-07 06:36:34 +00:00
m_StepsType = ntTo;
2003-01-02 22:10:51 +00:00
}
void Steps::CopyFrom( Steps* pSource, StepsType ntTo, float fMusicLengthSeconds ) // pSource does not have to be of the same StepsType
2003-01-30 07:18:33 +00:00
{
2003-08-07 06:36:34 +00:00
m_StepsType = ntTo;
2003-01-30 07:18:33 +00:00
NoteData noteData;
2004-10-23 17:43:49 +00:00
pSource->GetNoteData( noteData );
noteData.SetNumTracks( GameManager::StepsTypeToNumTracks(ntTo) );
parent = NULL;
2004-10-23 17:43:49 +00:00
this->SetNoteData( noteData );
this->SetDescription( pSource->GetDescription() );
2003-01-30 07:18:33 +00:00
this->SetDifficulty( pSource->GetDifficulty() );
this->SetMeter( pSource->GetMeter() );
this->CalculateRadarValues( fMusicLengthSeconds );
2003-01-30 07:18:33 +00:00
}
2003-08-07 06:16:17 +00:00
void Steps::CreateBlank( StepsType ntTo )
2003-01-30 07:18:33 +00:00
{
2003-08-07 06:36:34 +00:00
m_StepsType = ntTo;
2003-01-30 07:18:33 +00:00
NoteData noteData;
2004-07-12 02:19:24 +00:00
noteData.SetNumTracks( GameManager::StepsTypeToNumTracks(ntTo) );
2004-10-23 17:43:49 +00:00
this->SetNoteData( noteData );
2003-01-30 07:18:33 +00:00
}
2006-01-22 01:00:06 +00:00
void Steps::SetDifficultyAndDescription( Difficulty dc, RString sDescription )
2003-01-02 22:10:51 +00:00
{
DeAutogen();
m_Difficulty = dc;
m_sDescription = sDescription;
2005-04-13 08:05:48 +00:00
if( GetDifficulty() == DIFFICULTY_EDIT )
MakeValidEditDescription( m_sDescription );
2005-03-07 05:23:18 +00:00
}
2006-01-22 01:00:06 +00:00
bool Steps::MakeValidEditDescription( RString &sPreferredDescription )
2005-03-07 05:23:18 +00:00
{
2005-07-29 02:23:02 +00:00
if( int(sPreferredDescription.size()) > MAX_EDIT_STEPS_DESCRIPTION_LENGTH )
2005-03-07 05:23:18 +00:00
{
2005-07-29 02:23:02 +00:00
sPreferredDescription = sPreferredDescription.Left( MAX_EDIT_STEPS_DESCRIPTION_LENGTH );
2005-03-07 05:23:18 +00:00
return true;
}
return false;
2003-01-02 22:10:51 +00:00
}
2006-09-12 03:23:28 +00:00
void Steps::SetMeter( int meter )
2003-01-02 22:10:51 +00:00
{
DeAutogen();
m_iMeter = meter;
}
void Steps::SetCachedRadarValues( const RadarValues v[NUM_PLAYERS] )
2003-01-02 22:10:51 +00:00
{
DeAutogen();
copy( v, v + NUM_PLAYERS, m_CachedRadarValues );
2003-01-02 22:10:51 +00:00
}
2005-02-22 23:06:51 +00:00
// lua start
#include "LuaBinding.h"
2005-06-20 05:02:03 +00:00
class LunaSteps: public Luna<Steps>
2005-02-22 23:06:51 +00:00
{
public:
LunaSteps() { LUA->Register( Register ); }
2006-08-22 14:10:25 +00:00
DEFINE_METHOD( GetStepsType, m_StepsType )
DEFINE_METHOD( GetDifficulty, GetDifficulty() )
DEFINE_METHOD( GetDescription, GetDescription() )
DEFINE_METHOD( GetMeter, GetMeter() )
DEFINE_METHOD( GetFilename, GetFilename() )
DEFINE_METHOD( IsAutogen, IsAutogen() )
2006-08-16 21:35:01 +00:00
static int GetRadarValues( T* p, lua_State *L )
{
2006-09-27 06:21:34 +00:00
PlayerNumber pn = Enum::Check<PlayerNumber>(L, 1);
2006-08-16 21:35:01 +00:00
RadarValues &rv = const_cast<RadarValues &>(p->GetRadarValues(pn));
rv.PushSelf(L);
return 1;
}
2005-02-22 23:06:51 +00:00
2006-08-22 14:06:24 +00:00
static void Register( lua_State *L )
2005-02-22 23:06:51 +00:00
{
ADD_METHOD( GetStepsType );
ADD_METHOD( GetDifficulty );
ADD_METHOD( GetDescription );
ADD_METHOD( GetMeter );
2005-12-06 12:39:43 +00:00
ADD_METHOD( GetFilename );
2006-08-16 21:35:01 +00:00
ADD_METHOD( GetRadarValues );
ADD_METHOD( IsAutogen );
2005-02-22 23:06:51 +00:00
Luna<T>::Register( L );
}
};
LUA_REGISTER_CLASS( Steps )
// lua end
2004-05-31 21:35:31 +00:00
/*
* (c) 2001-2004 Chris Danford, Glenn Maynard, David Wilson
* 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.
*/