• R/O
  • SSH

zandronum-sandbox-stable: Commit


Commit MetaInfo

Revisionec4ffe43cf9c5b32a03ae89ba22f14082c2354cd (tree)
Time2023-03-06 02:11:50
AuthorAdam Kaminski <kaminskiadam9@gmai...>
CommiterAdam Kaminski

Log Message

- CustomScoreColumn is no longer a template class, its values are now stored as ColumnValue objects, and the data type can now be changed on the fly if necessary.
- Made CustomScoreColumn::SetDefaultValue and ColumnValue::ChangeDataType public member functions.

Change Summary

Incremental Difference

diff -r 8069c571328e -r ec4ffe43cf9c src/p_acs.cpp
--- a/src/p_acs.cpp Sun Feb 26 10:16:36 2023 -0500
+++ b/src/p_acs.cpp Sun Mar 05 12:11:50 2023 -0500
@@ -1888,7 +1888,7 @@
18881888 //
18891889 // ================================================================================================
18901890
1891-static CustomScoreColumnBase *GetCustomScoreColumn ( const char *pszColumnName )
1891+static CustomScoreColumn *GetCustomScoreColumn ( const char *pszColumnName )
18921892 {
18931893 ScoreColumn *pColumn = SCOREBOARD_GetColumn( pszColumnName, true );
18941894
@@ -1896,7 +1896,7 @@
18961896 if (( pColumn == NULL ) || ( pColumn->IsCustomColumn( ) == false ))
18971897 return NULL;
18981898
1899- return static_cast<CustomScoreColumnBase *>( pColumn );
1899+ return static_cast<CustomScoreColumn *>( pColumn );
19001900 }
19011901
19021902 //---- Plane watchers ----//
@@ -7920,7 +7920,7 @@
79207920
79217921 case ACSF_SetCustomColumnValue:
79227922 {
7923- CustomScoreColumnBase *pCustomColumn = GetCustomScoreColumn( FBehavior::StaticLookupString( args[0] ));
7923+ CustomScoreColumn *pCustomColumn = GetCustomScoreColumn( FBehavior::StaticLookupString( args[0] ));
79247924
79257925 // [AK] Make sure that the column and player are both valid.
79267926 if (( pCustomColumn != NULL ) && ( PLAYER_IsValidPlayer( args[1] )))
@@ -7963,7 +7963,7 @@
79637963
79647964 case ACSF_GetCustomColumnValue:
79657965 {
7966- CustomScoreColumnBase *pCustomColumn = GetCustomScoreColumn( FBehavior::StaticLookupString( args[0] ));
7966+ CustomScoreColumn *pCustomColumn = GetCustomScoreColumn( FBehavior::StaticLookupString( args[0] ));
79677967
79687968 // [AK] Make sure that the column and player are both valid.
79697969 if (( pCustomColumn != NULL ) && ( PLAYER_IsValidPlayer( args[1] )))
@@ -8000,7 +8000,7 @@
80008000
80018001 case ACSF_ResetCustomColumnToDefault:
80028002 {
8003- CustomScoreColumnBase *pCustomColumn = GetCustomScoreColumn( FBehavior::StaticLookupString( args[0] ));
8003+ CustomScoreColumn *pCustomColumn = GetCustomScoreColumn( FBehavior::StaticLookupString( args[0] ));
80048004 const ULONG ulPlayer = args[1] < 0 ? MAXPLAYERS : args[1];
80058005
80068006 // [AK] Make sure that the column and player are both valid (unless we're resetting for all players).
diff -r 8069c571328e -r ec4ffe43cf9c src/scoreboard.cpp
--- a/src/scoreboard.cpp Sun Feb 26 10:16:36 2023 -0500
+++ b/src/scoreboard.cpp Sun Mar 05 12:11:50 2023 -0500
@@ -322,9 +322,8 @@
322322 //
323323 // [AK] ColumnValue::ChangeDataType
324324 //
325-// Changes the data type of a ColumnValue object, but also deletes its string
326-// from memory if it's holding one. This should only be called when a new value
327-// is being assigned.
325+// Changes the data type of a ColumnValue object, then deletes its string from
326+// memory if it's holding one.
328327 //
329328 //*****************************************************************************
330329
@@ -335,6 +334,30 @@
335334
336335 DeleteString( );
337336 DataType = NewDataType;
337+
338+ switch ( DataType )
339+ {
340+ case COLUMNDATA_INT:
341+ case COLUMNDATA_COLOR:
342+ Int = 0;
343+ break;
344+
345+ case COLUMNDATA_BOOL:
346+ Bool = false;
347+ break;
348+
349+ case COLUMNDATA_FLOAT:
350+ Float = 0.0f;
351+ break;
352+
353+ case COLUMNDATA_STRING:
354+ String = NULL;
355+ break;
356+
357+ case COLUMNDATA_TEXTURE:
358+ Texture = NULL;
359+ break;
360+ }
338361 }
339362
340363 //*****************************************************************************
@@ -1687,24 +1710,16 @@
16871710 //
16881711 // [AK] CustomScoreColumn::CustomScoreColumn
16891712 //
1690-// Initializes the custom column's default value to zero (e.g. 0 for integers,
1691-// booleans, floats, or colors, and NULL for strings or textures).
1713+// Initializes the custom column's data type and sets its default value to zero
1714+// (e.g. 0 for integers, booleans, floats, or colors, and NULL for strings
1715+// or textures).
16921716 //
16931717 //*****************************************************************************
16941718
1695-template <typename VariableType>
1696-CustomScoreColumn<VariableType>::CustomScoreColumn( const char *pszName ) : CustomScoreColumnBase( pszName )
1719+CustomScoreColumn::CustomScoreColumn( COLUMNDATA_e DataType, const char *pszName ) : DataScoreColumn( COLUMNTYPE_CUSTOM, pszName ), CurrentDataType( COLUMNDATA_UNKNOWN )
16971720 {
1698- ColumnValue Val;
1699-
1700- // [AK] Right now, DefaultVal has no data type (i.e. COLUMNDATA_UNKNOWN).
1701- // ColumnValue::GetValue always returns the "zero" value of each data type when
1702- // this is the case, so we'll use this to help initialize the default value.
1703- VariableType temp = DefaultVal.GetValue<VariableType>( );
1704-
1705- // [AK] Note: the "temp" variable will also set the correct data type of the
1706- // "DefaultVal" ColumnValue object.
1707- DefaultVal = temp;
1721+ ValidateDataType( DataType, "CustomScoreColumn::CustomScoreColumn" );
1722+ SetDataType( DataType );
17081723 }
17091724
17101725 //*****************************************************************************
@@ -1716,17 +1731,9 @@
17161731 //
17171732 //*****************************************************************************
17181733
1719-template <typename VariableType>
1720-ColumnValue CustomScoreColumn<VariableType>::GetValue( ULONG ulPlayer ) const
1734+ColumnValue CustomScoreColumn::GetValue( ULONG ulPlayer ) const
17211735 {
1722- ColumnValue Result;
1723-
1724- if ( PLAYER_IsValidPlayer( ulPlayer ))
1725- Result = Val[ulPlayer];
1726- else
1727- Result = DefaultVal;
1728-
1729- return Result;
1736+ return PLAYER_IsValidPlayer( ulPlayer ) ? Val[ulPlayer] : DefaultVal;
17301737 }
17311738
17321739 //*****************************************************************************
@@ -1737,27 +1744,47 @@
17371744 //
17381745 //*****************************************************************************
17391746
1740-template <typename VariableType>
1741-ColumnValue CustomScoreColumn<VariableType>::GetDefaultValue( void ) const
1747+ColumnValue CustomScoreColumn::GetDefaultValue( void ) const
17421748 {
17431749 return DefaultVal;
17441750 }
17451751
17461752 //*****************************************************************************
17471753 //
1754+// [AK] CustomScoreColumn::SetDataType
1755+//
1756+// Changes the columm's data type and zeroes every player's value.
1757+//
1758+//*****************************************************************************
1759+
1760+void CustomScoreColumn::SetDataType( COLUMNDATA_e NewDataType )
1761+{
1762+ if ( CurrentDataType == NewDataType )
1763+ return;
1764+
1765+ ValidateDataType( NewDataType, "CustomScoreColumn::SetDataType" );
1766+ CurrentDataType = NewDataType;
1767+
1768+ // [AK] We changed the custom column's data type. Now we need to do the same with
1769+ // the (default) values and zero everything.
1770+ for ( ULONG ulIdx = 0; ulIdx < MAXPLAYERS; ulIdx++ )
1771+ Val[ulIdx].ChangeDataType( CurrentDataType );
1772+
1773+ DefaultVal.ChangeDataType( CurrentDataType );
1774+}
1775+
1776+//*****************************************************************************
1777+//
17481778 // [AK] CustomScoreColumn::SetValue
17491779 //
17501780 // Changes the value for a player in this column.
17511781 //
17521782 //*****************************************************************************
17531783
1754-template <typename VariableType>
1755-void CustomScoreColumn<VariableType>::SetValue( const ULONG ulPlayer, const ColumnValue &Value )
1784+void CustomScoreColumn::SetValue( const ULONG ulPlayer, const ColumnValue &Value )
17561785 {
1757- if ( PLAYER_IsValidPlayer( ulPlayer ) == false )
1758- return;
1759-
1760- Val[ulPlayer] = Value.GetValue<VariableType>( );
1786+ if ( PLAYER_IsValidPlayer( ulPlayer ))
1787+ TryChangingValue( Val[ulPlayer], Value, "CustomScoreColumn::SetValue" );
17611788 }
17621789
17631790 //*****************************************************************************
@@ -1768,14 +1795,9 @@
17681795 //
17691796 //*****************************************************************************
17701797
1771-template <typename VariableType>
1772-void CustomScoreColumn<VariableType>::SetDefaultValue( const ColumnValue &Value )
1798+void CustomScoreColumn::SetDefaultValue( const ColumnValue &Value )
17731799 {
1774- // [AK] Only set the value if the data types match. Otherwise, throw a fatal error.
1775- if ( GetDataType( ) == Value.GetDataType( ))
1776- DefaultVal = Value;
1777- else
1778- I_Error( "CustomScoreColumn::SetDefaultValue: tried assigning a different data type to the default value." );
1800+ TryChangingValue( DefaultVal, Value, "CustomScoreColumn::SetDefaultValue" );
17791801 }
17801802
17811803 //*****************************************************************************
@@ -1786,8 +1808,7 @@
17861808 //
17871809 //*****************************************************************************
17881810
1789-template <typename VariableType>
1790-void CustomScoreColumn<VariableType>::ParseCommand( const FName Name, FScanner &sc, const COLUMNCMD_e Command, const FString CommandName )
1811+void CustomScoreColumn::ParseCommand( const FName Name, FScanner &sc, const COLUMNCMD_e Command, const FString CommandName )
17911812 {
17921813 switch ( Command )
17931814 {
@@ -1871,30 +1892,60 @@
18711892 //
18721893 //*****************************************************************************
18731894
1874-template <typename VariableType>
1875-void CustomScoreColumn<VariableType>::ResetToDefault( const ULONG ulPlayer, const bool bChangingLevel )
1895+void CustomScoreColumn::ResetToDefault( const ULONG ulPlayer, const bool bChangingLevel )
18761896 {
18771897 // [AK] Don't reset to default if this column forbids it during a level change.
18781898 if (( bChangingLevel ) && ( ulFlags & COLUMNFLAG_DONTRESETONLEVELCHANGE ))
18791899 return;
18801900
1881- const ColumnValue DefaultValue = GetDefaultValue( );
1882-
18831901 // [AK] Check if we want to restore the default value for all players.
18841902 if ( ulPlayer == MAXPLAYERS )
18851903 {
18861904 for ( ULONG ulIdx = 0; ulIdx < MAXPLAYERS; ulIdx++ )
1887- Val[ulIdx] = DefaultValue.GetValue<VariableType>( );
1905+ Val[ulIdx] = DefaultVal;
18881906 }
18891907 // [AK] Otherwise, restore it only for the one player.
18901908 else if ( ulPlayer < MAXPLAYERS )
18911909 {
1892- Val[ulPlayer] = DefaultValue.GetValue<VariableType>( );
1910+ Val[ulPlayer] = DefaultVal;
18931911 }
18941912 }
18951913
18961914 //*****************************************************************************
18971915 //
1916+// [AK] CustomScoreColumn::ValidateDataType
1917+//
1918+// Checks if a data type is valid, and throws a fatal error if it isn't.
1919+//
1920+//*****************************************************************************
1921+
1922+void CustomScoreColumn::ValidateDataType( COLUMNDATA_e DataType, const char *pszFunctionName ) const
1923+{
1924+ // [AK] Throw a fatal error if an invalid data type was used.
1925+ if (( DataType <= COLUMNDATA_UNKNOWN ) || ( DataType >= NUM_COLUMNDATA_TYPES ))
1926+ I_Error( "%s: an invalid data type was used, %d.", pszFunctionName ? pszFunctionName : "CustomScoreColumn", static_cast<int>( DataType ));
1927+}
1928+
1929+//*****************************************************************************
1930+//
1931+// [AK] CustomScoreColumn::TryChangingValue
1932+//
1933+// Checks if it's safe to change a value to that of another ColumnValue object
1934+// (i.e. their data types match), and throws a fatal error if they aren't.
1935+//
1936+//*****************************************************************************
1937+
1938+void CustomScoreColumn::TryChangingValue( ColumnValue &To, const ColumnValue &From, const char *pszFunctionName )
1939+{
1940+ // [AK] Only set the value if the data types match. Otherwise, throw a fatal error.
1941+ if ( GetDataType( ) == From.GetDataType( ))
1942+ To = From;
1943+ else
1944+ I_Error( "%s: the value's data type doesn't match the column's data type.", pszFunctionName ? pszFunctionName : "CustomScoreColumn" );
1945+}
1946+
1947+//*****************************************************************************
1948+//
18981949 // [AK] CompositeScoreColumn::ParseCommand
18991950 //
19001951 // Parses commands that are only used for composite columns.
@@ -3250,7 +3301,7 @@
32503301 while ( it.NextPair( pair ))
32513302 {
32523303 if (( pair->Value->IsUsableInCurrentGame( )) && ( pair->Value->IsCustomColumn( )))
3253- static_cast<CustomScoreColumnBase *>( pair->Value )->ResetToDefault( ulPlayer, bChangingLevel );
3304+ static_cast<CustomScoreColumn *>( pair->Value )->ResetToDefault( ulPlayer, bChangingLevel );
32543305 }
32553306 }
32563307
diff -r 8069c571328e -r ec4ffe43cf9c src/scoreboard.h
--- a/src/scoreboard.h Sun Feb 26 10:16:36 2023 -0500
+++ b/src/scoreboard.h Sun Mar 05 12:11:50 2023 -0500
@@ -132,6 +132,8 @@
132132
133133 ~ColumnValue( void ) { DeleteString( ); }
134134
135+ void ChangeDataType( COLUMNDATA_e NewDataType );
136+
135137 inline COLUMNDATA_e GetDataType( void ) const { return DataType; }
136138 template <typename VariableType> VariableType GetValue( void ) const;
137139 FString ToString( void ) const;
@@ -170,7 +172,6 @@
170172
171173 private:
172174 void TransferValue( const ColumnValue &Other );
173- void ChangeDataType( COLUMNDATA_e NewDataType );
174175 void DeleteString( void );
175176
176177 COLUMNDATA_e DataType;
@@ -337,44 +338,33 @@
337338 //
338339 // [AK] CustomScoreColumn
339340 //
340-// A template class for all custom columns and their respective data types.
341+// A separate class for all custom columns and their respective data types.
341342 //
342343 //*****************************************************************************
343344
344-class CustomScoreColumnBase : public DataScoreColumn
345+class CustomScoreColumn : public DataScoreColumn
345346 {
346347 public:
347- CustomScoreColumnBase( const char *pszName ) : DataScoreColumn( COLUMNTYPE_CUSTOM, pszName ) { }
348-
349- virtual ColumnValue GetDefaultValue( void ) const = 0;
350- virtual void SetValue( const ULONG ulPlayer, const ColumnValue &Value ) = 0;
351- virtual void ResetToDefault( const ULONG ulPlayer, const bool bChangingLevel ) = 0;
352-
353-protected:
354- virtual void SetDefaultValue( const ColumnValue &Value ) = 0;
355-};
356-
357-//*****************************************************************************
358-//
359-template <typename VariableType>
360-class CustomScoreColumn : public CustomScoreColumnBase
361-{
362-public:
363- CustomScoreColumn( const char *pszName );
348+ CustomScoreColumn( COLUMNDATA_e DataType, const char *pszName );
364349
365350 virtual bool IsCustomColumn( void ) const { return true; }
366- virtual COLUMNDATA_e GetDataType( void ) const { return DefaultVal.GetDataType( ); }
351+ virtual COLUMNDATA_e GetDataType( void ) const { return CurrentDataType; }
367352 virtual ColumnValue GetValue( const ULONG ulPlayer ) const;
368353 virtual ColumnValue GetDefaultValue( void ) const;
354+ virtual void SetDataType( COLUMNDATA_e NewDataType );
369355 virtual void SetValue( const ULONG ulPlayer, const ColumnValue &Value );
356+ virtual void SetDefaultValue( const ColumnValue &Value );
370357 virtual void ParseCommand( const FName Name, FScanner &sc, const COLUMNCMD_e Command, const FString CommandName );
371358 virtual void ResetToDefault( const ULONG ulPlayer, const bool bChangingLevel );
372359
373360 protected:
374- virtual void SetDefaultValue( const ColumnValue &Value );
361+ COLUMNDATA_e CurrentDataType;
362+ ColumnValue Val[MAXPLAYERS];
363+ ColumnValue DefaultVal;
375364
376- VariableType Val[MAXPLAYERS];
377- ColumnValue DefaultVal;
365+private:
366+ void ValidateDataType( COLUMNDATA_e DataType, const char *pszFunctionName ) const;
367+ void TryChangingValue( ColumnValue &To, const ColumnValue &From, const char *pszFunctionName );
378368 };
379369
380370 //*****************************************************************************
Show on old repository browser