Files
itgmania212121/stepmania/src/Steps.cpp
T

413 lines
10 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-07-27 19:29:51 +00:00
#include "RageException.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
2003-08-03 00:57:20 +00:00
Steps::Steps()
2002-05-20 08:59:37 +00:00
{
m_bSavedToDisk = false;
2004-02-07 05:57:19 +00:00
m_StepsType = STEPS_TYPE_INVALID;
2004-02-08 01:05:53 +00:00
m_LoadedFromProfile = PROFILE_SLOT_INVALID;
m_uHash = 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
{
}
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
2003-01-02 22:10:51 +00:00
DeAutogen();
*m_pNoteData = noteDataNew;
m_bNoteDataIsFilled = true;
NoteDataUtil::GetSMNoteDataString( *m_pNoteData, m_sNoteDataCompressed );
m_uHash = GetHashForString( m_sNoteDataCompressed );
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
{
2003-01-19 21:14:27 +00:00
ASSERT(this);
2003-07-15 19:40:41 +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
2004-10-23 23:41:49 +00:00
void Steps::SetSMNoteData( const CString &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_;
m_uHash = GetHashForString( m_sNoteDataCompressed );
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() */
2004-10-23 23:41:49 +00:00
void Steps::GetSMNoteData( CString &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;
const float RadarCoeffs[NUM_RADAR_CATEGORIES] =
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
};
2003-12-18 21:23:52 +00:00
for( int r = 0; r < NUM_RADAR_CATEGORIES; ++r )
2003-12-19 03:18:52 +00:00
pMeter += this->GetRadarValues()[r] * RadarCoeffs[r];
const float DifficultyCoeffs[NUM_DIFFICULTIES] =
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 = this->GetRadarValues()[RADAR_STREAM] * this->GetRadarValues()[RADAR_VOLTAGE];
const float ChaosSquare = this->GetRadarValues()[RADAR_CHAOS] * this->GetRadarValues()[RADAR_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 )
{
2003-12-03 00:28:17 +00:00
if( GetMeter() == 1 ) SetDifficulty( DIFFICULTY_BEGINNER );
else if( GetMeter() <= 3 ) SetDifficulty( DIFFICULTY_EASY );
else if( GetMeter() <= 6 ) SetDifficulty( DIFFICULTY_MEDIUM );
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 )
{
m_CachedRadarValues = RadarValues();
// If we're autogen, don't calculate values. GetRadarValues will take from our parent.
if( parent != NULL )
return;
// If we're an edit, leave the RadarValues invalid.
if( IsAnEdit() )
return;
NoteData tempNoteData;
this->GetNoteData( tempNoteData );
NoteDataUtil::CalculateRadarValues( tempNoteData, fMusicLengthSeconds, m_CachedRadarValues );
}
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;
2004-07-12 02:19:24 +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
m_bNoteDataIsFilled = true;
m_pNoteData->SetNumTracks( GameManager::StepsTypeToNumTracks(m_StepsType) );
2003-01-02 22:10:51 +00:00
NoteDataUtil::LoadFromSMNoteDataString( *m_pNoteData, m_sNoteDataCompressed );
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 = CString("");
return;
}
if( !m_sFilename.empty() )
{
/* We have a file on disk; clear all data in memory. */
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)
2004-10-30 07:18:28 +00:00
* don't always free the alocated memory. */
m_sNoteDataCompressed = CString("");
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)
return; /* OK */
Decompress(); // fills in m_pNoteData with sliding window transform
2003-03-13 01:26:50 +00:00
2003-01-02 22:10:51 +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;
m_CachedRadarValues = Real()->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 );
2004-07-12 02:19:24 +00:00
noteData.SetNumTracks( GameManager::StepsTypeToNumTracks(ntTo) );
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
}
2003-08-03 00:57:20 +00:00
const Steps *Steps::Real() const
2003-01-02 22:10:51 +00:00
{
2004-02-13 23:50:21 +00:00
ASSERT( this );
2003-01-02 22:10:51 +00:00
if(parent) return parent;
return this;
}
2003-08-03 00:57:20 +00:00
bool Steps::IsAutogen() const
2003-01-02 22:10:51 +00:00
{
return parent != NULL;
}
void Steps::SetFile( CString fn )
{
m_sFilename = fn;
}
void Steps::SetDifficultyAndDescription( Difficulty dc, CString 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
}
2005-04-13 08:05:48 +00:00
bool Steps::MakeValidEditDescription( CString &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
}
2003-08-03 00:57:20 +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 )
2003-01-02 22:10:51 +00:00
{
DeAutogen();
m_CachedRadarValues = v;
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 ); }
static int GetStepsType( T* p, lua_State *L ) { lua_pushnumber(L, p->m_StepsType ); return 1; }
2005-02-23 05:02:28 +00:00
static int GetDifficulty( T* p, lua_State *L ) { lua_pushnumber(L, p->GetDifficulty() ); return 1; }
2005-02-22 23:06:51 +00:00
static int GetDescription( T* p, lua_State *L ) { lua_pushstring(L, p->GetDescription() ); return 1; }
2005-02-26 20:44:16 +00:00
static int GetMeter( T* p, lua_State *L ) { lua_pushnumber(L, p->GetMeter() ); return 1; }
2005-02-22 23:06:51 +00:00
static void Register(lua_State *L)
{
ADD_METHOD( GetStepsType );
ADD_METHOD( GetDifficulty );
ADD_METHOD( GetDescription );
ADD_METHOD( GetMeter );
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.
*/