Change RadarValues internal implementation to not use a union and use the subscript operator instead of the float* operator for subscripting.

This commit is contained in:
Kyzentun
2014-08-22 13:19:17 -06:00
parent 8b062785ea
commit 2c49ec7380
6 changed files with 42 additions and 36 deletions
+1 -1
View File
@@ -128,7 +128,7 @@ static void Deserialize( RadarValues &o, const Json::Value &root )
{ {
FOREACH_ENUM( RadarCategory, rc ) FOREACH_ENUM( RadarCategory, rc )
{ {
o.m_Values.f[rc] = (float)root[ RadarCategoryToString(rc) ].asDouble(); o[rc] = (float)root[ RadarCategoryToString(rc) ].asDouble();
} }
} }
+1 -1
View File
@@ -100,7 +100,7 @@ static void Serialize( const RadarValues &o, Json::Value &root )
{ {
FOREACH_ENUM( RadarCategory, rc ) FOREACH_ENUM( RadarCategory, rc )
{ {
root[ RadarCategoryToString(rc) ] = o.m_Values.f[rc]; root[ RadarCategoryToString(rc) ] = o[rc];
} }
} }
+3 -1
View File
@@ -316,7 +316,9 @@ int PlayerStageStats::GetLessonScoreNeeded() const
float fScore = 0; float fScore = 0;
FOREACH_CONST( Steps*, m_vpPossibleSteps, steps ) FOREACH_CONST( Steps*, m_vpPossibleSteps, steps )
fScore += (*steps)->GetRadarValues( PLAYER_1 ).m_Values.v.fNumTapsAndHolds; {
fScore += (*steps)->GetRadarValues(PLAYER_1)[RadarCategory_TapsAndHolds];
}
return lrintf( fScore * LESSON_PASS_THRESHOLD ); return lrintf( fScore * LESSON_PASS_THRESHOLD );
} }
+21 -7
View File
@@ -16,13 +16,17 @@ RadarValues::RadarValues()
void RadarValues::MakeUnknown() void RadarValues::MakeUnknown()
{ {
FOREACH_ENUM( RadarCategory, rc ) FOREACH_ENUM( RadarCategory, rc )
m_Values.f[rc] = RADAR_VAL_UNKNOWN; {
(*this)[rc] = RADAR_VAL_UNKNOWN;
}
} }
void RadarValues::Zero() void RadarValues::Zero()
{ {
FOREACH_ENUM( RadarCategory, rc ) FOREACH_ENUM( RadarCategory, rc )
m_Values.f[rc] = 0; {
(*this)[rc] = 0;
}
} }
XNode* RadarValues::CreateNode( bool bIncludeSimpleValues, bool bIncludeComplexValues ) const XNode* RadarValues::CreateNode( bool bIncludeSimpleValues, bool bIncludeComplexValues ) const
@@ -35,12 +39,16 @@ XNode* RadarValues::CreateNode( bool bIncludeSimpleValues, bool bIncludeComplexV
if( rc >= RadarCategory_TapsAndHolds ) if( rc >= RadarCategory_TapsAndHolds )
{ {
if( bIncludeSimpleValues ) if( bIncludeSimpleValues )
pNode->AppendChild( RadarCategoryToString(rc), (int)m_Values.f[rc] ); {
pNode->AppendChild(RadarCategoryToString(rc), (int)((*this)[rc]));
}
} }
else else
{ {
if( bIncludeComplexValues ) if( bIncludeComplexValues )
pNode->AppendChild( RadarCategoryToString(rc), m_Values.f[rc] ); {
pNode->AppendChild(RadarCategoryToString(rc), (*this)[rc]);
}
} }
} }
@@ -54,7 +62,9 @@ void RadarValues::LoadFromNode( const XNode* pNode )
Zero(); Zero();
FOREACH_ENUM( RadarCategory, rc ) FOREACH_ENUM( RadarCategory, rc )
pNode->GetChildValue( RadarCategoryToString(rc), m_Values.f[rc] ); {
pNode->GetChildValue( RadarCategoryToString(rc), (*this)[rc] );
}
} }
/* iMaxValues is only used for writing compatibility fields in non-cache /* iMaxValues is only used for writing compatibility fields in non-cache
@@ -67,7 +77,9 @@ RString RadarValues::ToString( int iMaxValues ) const
vector<RString> asRadarValues; vector<RString> asRadarValues;
for( int r=0; r < iMaxValues; r++ ) for( int r=0; r < iMaxValues; r++ )
asRadarValues.push_back( ssprintf("%.3f", m_Values.f[r]) ); {
asRadarValues.push_back(ssprintf("%.3f", (*this)[r]));
}
return join( ",",asRadarValues ); return join( ",",asRadarValues );
} }
@@ -84,7 +96,9 @@ void RadarValues::FromString( RString sRadarValues )
} }
FOREACH_ENUM( RadarCategory, rc ) FOREACH_ENUM( RadarCategory, rc )
m_Values.f[rc] = StringToFloat( saValues[rc] ); {
(*this)[rc] = StringToFloat(saValues[rc]);
}
} }
+13 -25
View File
@@ -12,29 +12,13 @@ struct lua_State;
/** @brief Cached song statistics. */ /** @brief Cached song statistics. */
struct RadarValues struct RadarValues
{ {
union Values private:
{ float m_Values[NUM_RadarCategory];
struct public:
{ float operator[](RadarCategory cat) const { return m_Values[cat]; }
float fStream; float& operator[](RadarCategory cat) { return m_Values[cat]; }
float fVoltage; float operator[](int cat) const { return m_Values[cat]; }
float fAir; float& operator[](int cat) { return m_Values[cat]; }
float fFreeze;
float fChaos;
float fNumTapsAndHolds;
float fNumJumps;
float fNumHolds;
float fNumMines;
float fNumHands;
float fNumRolls;
float fNumLifts;
float fNumFakes;
} v;
float f[NUM_RadarCategory];
} m_Values;
operator const float* () const { return m_Values.f; };
operator float* () { return m_Values.f; };
RadarValues(); RadarValues();
void MakeUnknown(); void MakeUnknown();
@@ -48,7 +32,9 @@ struct RadarValues
RadarValues& operator+=( const RadarValues& other ) RadarValues& operator+=( const RadarValues& other )
{ {
FOREACH_ENUM( RadarCategory, rc ) FOREACH_ENUM( RadarCategory, rc )
m_Values.f[rc] += other.m_Values.f[rc]; {
(*this)[rc] += other[rc];
}
return *this; return *this;
} }
/** /**
@@ -60,9 +46,11 @@ struct RadarValues
{ {
FOREACH_ENUM( RadarCategory, rc ) FOREACH_ENUM( RadarCategory, rc )
{ {
if( m_Values.f[rc] != other.m_Values.f[rc] ) if((*this)[rc] != other[rc])
{
return false; return false;
} }
}
return true; return true;
} }
/** /**
+3 -1
View File
@@ -865,7 +865,9 @@ static HighScore MakeRandomHighScore( float fPercentDP )
hs.SetHoldNoteScore( hns, RandomInt(100) ); hs.SetHoldNoteScore( hns, RandomInt(100) );
RadarValues rv; RadarValues rv;
FOREACH_ENUM( RadarCategory, rc ) FOREACH_ENUM( RadarCategory, rc )
rv.m_Values.f[rc] = randomf( 0, 1 ); {
rv[rc] = randomf( 0, 1 );
}
hs.SetRadarValues( rv ); hs.SetRadarValues( rv );
return hs; return hs;