Revision | ec4ffe43cf9c5b32a03ae89ba22f14082c2354cd (tree) |
---|---|
Time | 2023-03-06 02:11:50 |
Author | Adam Kaminski <kaminskiadam9@gmai...> |
Commiter | Adam Kaminski |
- 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.
@@ -1888,7 +1888,7 @@ | ||
1888 | 1888 | // |
1889 | 1889 | // ================================================================================================ |
1890 | 1890 | |
1891 | -static CustomScoreColumnBase *GetCustomScoreColumn ( const char *pszColumnName ) | |
1891 | +static CustomScoreColumn *GetCustomScoreColumn ( const char *pszColumnName ) | |
1892 | 1892 | { |
1893 | 1893 | ScoreColumn *pColumn = SCOREBOARD_GetColumn( pszColumnName, true ); |
1894 | 1894 |
@@ -1896,7 +1896,7 @@ | ||
1896 | 1896 | if (( pColumn == NULL ) || ( pColumn->IsCustomColumn( ) == false )) |
1897 | 1897 | return NULL; |
1898 | 1898 | |
1899 | - return static_cast<CustomScoreColumnBase *>( pColumn ); | |
1899 | + return static_cast<CustomScoreColumn *>( pColumn ); | |
1900 | 1900 | } |
1901 | 1901 | |
1902 | 1902 | //---- Plane watchers ----// |
@@ -7920,7 +7920,7 @@ | ||
7920 | 7920 | |
7921 | 7921 | case ACSF_SetCustomColumnValue: |
7922 | 7922 | { |
7923 | - CustomScoreColumnBase *pCustomColumn = GetCustomScoreColumn( FBehavior::StaticLookupString( args[0] )); | |
7923 | + CustomScoreColumn *pCustomColumn = GetCustomScoreColumn( FBehavior::StaticLookupString( args[0] )); | |
7924 | 7924 | |
7925 | 7925 | // [AK] Make sure that the column and player are both valid. |
7926 | 7926 | if (( pCustomColumn != NULL ) && ( PLAYER_IsValidPlayer( args[1] ))) |
@@ -7963,7 +7963,7 @@ | ||
7963 | 7963 | |
7964 | 7964 | case ACSF_GetCustomColumnValue: |
7965 | 7965 | { |
7966 | - CustomScoreColumnBase *pCustomColumn = GetCustomScoreColumn( FBehavior::StaticLookupString( args[0] )); | |
7966 | + CustomScoreColumn *pCustomColumn = GetCustomScoreColumn( FBehavior::StaticLookupString( args[0] )); | |
7967 | 7967 | |
7968 | 7968 | // [AK] Make sure that the column and player are both valid. |
7969 | 7969 | if (( pCustomColumn != NULL ) && ( PLAYER_IsValidPlayer( args[1] ))) |
@@ -8000,7 +8000,7 @@ | ||
8000 | 8000 | |
8001 | 8001 | case ACSF_ResetCustomColumnToDefault: |
8002 | 8002 | { |
8003 | - CustomScoreColumnBase *pCustomColumn = GetCustomScoreColumn( FBehavior::StaticLookupString( args[0] )); | |
8003 | + CustomScoreColumn *pCustomColumn = GetCustomScoreColumn( FBehavior::StaticLookupString( args[0] )); | |
8004 | 8004 | const ULONG ulPlayer = args[1] < 0 ? MAXPLAYERS : args[1]; |
8005 | 8005 | |
8006 | 8006 | // [AK] Make sure that the column and player are both valid (unless we're resetting for all players). |
@@ -322,9 +322,8 @@ | ||
322 | 322 | // |
323 | 323 | // [AK] ColumnValue::ChangeDataType |
324 | 324 | // |
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. | |
328 | 327 | // |
329 | 328 | //***************************************************************************** |
330 | 329 |
@@ -335,6 +334,30 @@ | ||
335 | 334 | |
336 | 335 | DeleteString( ); |
337 | 336 | 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 | + } | |
338 | 361 | } |
339 | 362 | |
340 | 363 | //***************************************************************************** |
@@ -1687,24 +1710,16 @@ | ||
1687 | 1710 | // |
1688 | 1711 | // [AK] CustomScoreColumn::CustomScoreColumn |
1689 | 1712 | // |
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). | |
1692 | 1716 | // |
1693 | 1717 | //***************************************************************************** |
1694 | 1718 | |
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 ) | |
1697 | 1720 | { |
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 ); | |
1708 | 1723 | } |
1709 | 1724 | |
1710 | 1725 | //***************************************************************************** |
@@ -1716,17 +1731,9 @@ | ||
1716 | 1731 | // |
1717 | 1732 | //***************************************************************************** |
1718 | 1733 | |
1719 | -template <typename VariableType> | |
1720 | -ColumnValue CustomScoreColumn<VariableType>::GetValue( ULONG ulPlayer ) const | |
1734 | +ColumnValue CustomScoreColumn::GetValue( ULONG ulPlayer ) const | |
1721 | 1735 | { |
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; | |
1730 | 1737 | } |
1731 | 1738 | |
1732 | 1739 | //***************************************************************************** |
@@ -1737,27 +1744,47 @@ | ||
1737 | 1744 | // |
1738 | 1745 | //***************************************************************************** |
1739 | 1746 | |
1740 | -template <typename VariableType> | |
1741 | -ColumnValue CustomScoreColumn<VariableType>::GetDefaultValue( void ) const | |
1747 | +ColumnValue CustomScoreColumn::GetDefaultValue( void ) const | |
1742 | 1748 | { |
1743 | 1749 | return DefaultVal; |
1744 | 1750 | } |
1745 | 1751 | |
1746 | 1752 | //***************************************************************************** |
1747 | 1753 | // |
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 | +// | |
1748 | 1778 | // [AK] CustomScoreColumn::SetValue |
1749 | 1779 | // |
1750 | 1780 | // Changes the value for a player in this column. |
1751 | 1781 | // |
1752 | 1782 | //***************************************************************************** |
1753 | 1783 | |
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 ) | |
1756 | 1785 | { |
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" ); | |
1761 | 1788 | } |
1762 | 1789 | |
1763 | 1790 | //***************************************************************************** |
@@ -1768,14 +1795,9 @@ | ||
1768 | 1795 | // |
1769 | 1796 | //***************************************************************************** |
1770 | 1797 | |
1771 | -template <typename VariableType> | |
1772 | -void CustomScoreColumn<VariableType>::SetDefaultValue( const ColumnValue &Value ) | |
1798 | +void CustomScoreColumn::SetDefaultValue( const ColumnValue &Value ) | |
1773 | 1799 | { |
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" ); | |
1779 | 1801 | } |
1780 | 1802 | |
1781 | 1803 | //***************************************************************************** |
@@ -1786,8 +1808,7 @@ | ||
1786 | 1808 | // |
1787 | 1809 | //***************************************************************************** |
1788 | 1810 | |
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 ) | |
1791 | 1812 | { |
1792 | 1813 | switch ( Command ) |
1793 | 1814 | { |
@@ -1871,30 +1892,60 @@ | ||
1871 | 1892 | // |
1872 | 1893 | //***************************************************************************** |
1873 | 1894 | |
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 ) | |
1876 | 1896 | { |
1877 | 1897 | // [AK] Don't reset to default if this column forbids it during a level change. |
1878 | 1898 | if (( bChangingLevel ) && ( ulFlags & COLUMNFLAG_DONTRESETONLEVELCHANGE )) |
1879 | 1899 | return; |
1880 | 1900 | |
1881 | - const ColumnValue DefaultValue = GetDefaultValue( ); | |
1882 | - | |
1883 | 1901 | // [AK] Check if we want to restore the default value for all players. |
1884 | 1902 | if ( ulPlayer == MAXPLAYERS ) |
1885 | 1903 | { |
1886 | 1904 | for ( ULONG ulIdx = 0; ulIdx < MAXPLAYERS; ulIdx++ ) |
1887 | - Val[ulIdx] = DefaultValue.GetValue<VariableType>( ); | |
1905 | + Val[ulIdx] = DefaultVal; | |
1888 | 1906 | } |
1889 | 1907 | // [AK] Otherwise, restore it only for the one player. |
1890 | 1908 | else if ( ulPlayer < MAXPLAYERS ) |
1891 | 1909 | { |
1892 | - Val[ulPlayer] = DefaultValue.GetValue<VariableType>( ); | |
1910 | + Val[ulPlayer] = DefaultVal; | |
1893 | 1911 | } |
1894 | 1912 | } |
1895 | 1913 | |
1896 | 1914 | //***************************************************************************** |
1897 | 1915 | // |
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 | +// | |
1898 | 1949 | // [AK] CompositeScoreColumn::ParseCommand |
1899 | 1950 | // |
1900 | 1951 | // Parses commands that are only used for composite columns. |
@@ -3250,7 +3301,7 @@ | ||
3250 | 3301 | while ( it.NextPair( pair )) |
3251 | 3302 | { |
3252 | 3303 | 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 ); | |
3254 | 3305 | } |
3255 | 3306 | } |
3256 | 3307 |
@@ -132,6 +132,8 @@ | ||
132 | 132 | |
133 | 133 | ~ColumnValue( void ) { DeleteString( ); } |
134 | 134 | |
135 | + void ChangeDataType( COLUMNDATA_e NewDataType ); | |
136 | + | |
135 | 137 | inline COLUMNDATA_e GetDataType( void ) const { return DataType; } |
136 | 138 | template <typename VariableType> VariableType GetValue( void ) const; |
137 | 139 | FString ToString( void ) const; |
@@ -170,7 +172,6 @@ | ||
170 | 172 | |
171 | 173 | private: |
172 | 174 | void TransferValue( const ColumnValue &Other ); |
173 | - void ChangeDataType( COLUMNDATA_e NewDataType ); | |
174 | 175 | void DeleteString( void ); |
175 | 176 | |
176 | 177 | COLUMNDATA_e DataType; |
@@ -337,44 +338,33 @@ | ||
337 | 338 | // |
338 | 339 | // [AK] CustomScoreColumn |
339 | 340 | // |
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. | |
341 | 342 | // |
342 | 343 | //***************************************************************************** |
343 | 344 | |
344 | -class CustomScoreColumnBase : public DataScoreColumn | |
345 | +class CustomScoreColumn : public DataScoreColumn | |
345 | 346 | { |
346 | 347 | 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 ); | |
364 | 349 | |
365 | 350 | 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; } | |
367 | 352 | virtual ColumnValue GetValue( const ULONG ulPlayer ) const; |
368 | 353 | virtual ColumnValue GetDefaultValue( void ) const; |
354 | + virtual void SetDataType( COLUMNDATA_e NewDataType ); | |
369 | 355 | virtual void SetValue( const ULONG ulPlayer, const ColumnValue &Value ); |
356 | + virtual void SetDefaultValue( const ColumnValue &Value ); | |
370 | 357 | virtual void ParseCommand( const FName Name, FScanner &sc, const COLUMNCMD_e Command, const FString CommandName ); |
371 | 358 | virtual void ResetToDefault( const ULONG ulPlayer, const bool bChangingLevel ); |
372 | 359 | |
373 | 360 | protected: |
374 | - virtual void SetDefaultValue( const ColumnValue &Value ); | |
361 | + COLUMNDATA_e CurrentDataType; | |
362 | + ColumnValue Val[MAXPLAYERS]; | |
363 | + ColumnValue DefaultVal; | |
375 | 364 | |
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 ); | |
378 | 368 | }; |
379 | 369 | |
380 | 370 | //***************************************************************************** |