From 0b94b21a91cc91c225cfc850ae583e98845c857a Mon Sep 17 00:00:00 2001 From: Nicholas Hastings Date: Sun, 6 Aug 2017 10:21:33 -0400 Subject: [PATCH] Update setting ConVar int values from string to more closely match game logic. ( https://github.com/alliedmodders/sourcemod/issues/648 ) They use a double as a go-between to gain precision, rather than casting the float value to an int, --- public/tier1/strtools.h | 1 + tier1/convar.cpp | 12 ++++++++---- tier1/strtools.cpp | 7 ++++++- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/public/tier1/strtools.h b/public/tier1/strtools.h index e0f3981e..5896b52c 100644 --- a/public/tier1/strtools.h +++ b/public/tier1/strtools.h @@ -127,6 +127,7 @@ int V_atoi (const char *str); int64 V_atoi64(const char *str); uint64 V_atoui64(const char *str); float V_atof (const char *str); +double V_atod(const char *str); char* V_stristr( char* pStr, const char* pSearch ); const char* V_stristr( const char* pStr, const char* pSearch ); const char* V_strnistr( const char* pStr, const char* pSearch, int n ); diff --git a/tier1/convar.cpp b/tier1/convar.cpp index 6ed56de5..b7214a1f 100644 --- a/tier1/convar.cpp +++ b/tier1/convar.cpp @@ -784,6 +784,7 @@ int ConVar::GetSplitScreenPlayerSlot( void ) const void ConVar::InternalSetValue( const char *value ) { float fNewValue; + double dblNewValue; char tempVal[ 32 ]; char *val; @@ -792,17 +793,19 @@ void ConVar::InternalSetValue( const char *value ) float flOldValue = m_Value.m_fValue; val = (char *)value; - fNewValue = ( float )atof( value ); + dblNewValue = V_atod(value); + fNewValue = ( float )dblNewValue; if ( ClampValue( fNewValue ) ) { + dblNewValue = fNewValue; Q_snprintf( tempVal,sizeof(tempVal), "%f", fNewValue ); val = tempVal; } // Redetermine value m_Value.m_fValue = fNewValue; - m_Value.m_nValue = ( int )( fNewValue ); + m_Value.m_nValue = ( int )( dblNewValue ); if ( !( m_nFlags & FCVAR_NEVER_AS_STRING ) ) { @@ -970,7 +973,8 @@ void ConVar::Create( const char *pName, const char *pDefaultValue, int flags /*= if (callback) m_fnChangeCallbacks.AddToTail(callback); - m_Value.m_fValue = ( float )atof( m_Value.m_pszString ); + double dblValue = V_atod( m_Value.m_pszString ); + m_Value.m_fValue = ( float )dblValue; // Bounds Check, should never happen, if it does, no big deal if ( m_bHasMin && ( m_Value.m_fValue < m_fMinVal ) ) @@ -983,7 +987,7 @@ void ConVar::Create( const char *pName, const char *pDefaultValue, int flags /*= Assert( 0 ); } - m_Value.m_nValue = ( int )m_Value.m_fValue; + m_Value.m_nValue = ( int )dblValue; BaseClass::Create( pName, pHelpString, flags ); } diff --git a/tier1/strtools.cpp b/tier1/strtools.cpp index 9f270ae3..b12206af 100644 --- a/tier1/strtools.cpp +++ b/tier1/strtools.cpp @@ -379,8 +379,13 @@ int V_atoi (const char *str) return 0; } - float V_atof (const char *str) +{ + return (float)V_atod(str); +} + + +double V_atod (const char *str) { _AssertValidStringPtr( str ); double val;