2009-08-21 20:21:05 +00:00
/*
* This file is part of OpenTTD .
* OpenTTD is free software ; you can redistribute it and / or modify it under the terms of the GNU General Public License as published by the Free Software Foundation , version 2.
* OpenTTD is distributed in the hope that it will be useful , but WITHOUT ANY WARRANTY ; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE .
* See the GNU General Public License for more details . You should have received a copy of the GNU General Public License along with OpenTTD . If not , see < http : //www.gnu.org/licenses/>.
*/
2008-05-06 15:11:33 +00:00
/** @file newgrf_spritegroup.cpp Handling of primarily NewGRF action 2. */
2007-03-21 03:06:21 +00:00
2006-04-20 20:51:57 +00:00
# include "stdafx.h"
2012-11-10 20:37:31 +00:00
# include "debug.h"
2006-04-27 19:53:58 +00:00
# include "newgrf_spritegroup.h"
2020-01-26 12:45:51 +00:00
# include "newgrf_profiling.h"
2009-05-22 15:13:50 +00:00
# include "core/pool_func.hpp"
2006-04-20 20:51:57 +00:00
2014-04-23 20:13:33 +00:00
# include "safeguards.h"
2009-05-22 15:13:50 +00:00
SpriteGroupPool _spritegroup_pool ( " SpriteGroup " ) ;
INSTANTIATE_POOL_METHODS ( SpriteGroup )
2006-04-20 20:51:57 +00:00
2023-05-08 17:01:06 +00:00
TemporaryStorageArray < int32_t , 0x110 > _temp_store ;
2013-12-23 18:08:16 +00:00
/**
* ResolverObject ( re ) entry point .
* This cannot be made a call to a virtual function because virtual functions
2019-04-10 21:07:06 +00:00
* do not like nullptr and checking for nullptr * everywhere * is more cumbersome than
2013-12-23 18:08:16 +00:00
* this little helper function .
* @ param group the group to resolve for
* @ param object information needed to resolve the group
* @ param top_level true if this is a top - level SpriteGroup , false if used nested in another SpriteGroup .
* @ return the resolved group
*/
/* static */ const SpriteGroup * SpriteGroup : : Resolve ( const SpriteGroup * group , ResolverObject & object , bool top_level )
{
2019-04-10 21:07:06 +00:00
if ( group = = nullptr ) return nullptr ;
2020-01-26 12:45:51 +00:00
const GRFFile * grf = object . grffile ;
auto profiler = std : : find_if ( _newgrf_profilers . begin ( ) , _newgrf_profilers . end ( ) , [ & ] ( const NewGRFProfiler & pr ) { return pr . grffile = = grf ; } ) ;
if ( profiler = = _newgrf_profilers . end ( ) | | ! profiler - > active ) {
if ( top_level ) _temp_store . ClearChanges ( ) ;
return group - > Resolve ( object ) ;
} else if ( top_level ) {
profiler - > BeginResolve ( object ) ;
2013-12-23 18:08:36 +00:00
_temp_store . ClearChanges ( ) ;
2020-01-26 12:45:51 +00:00
const SpriteGroup * result = group - > Resolve ( object ) ;
profiler - > EndResolve ( result ) ;
return result ;
} else {
profiler - > RecursiveResolve ( ) ;
return group - > Resolve ( object ) ;
2013-12-23 18:08:36 +00:00
}
2013-12-23 18:08:16 +00:00
}
2023-05-08 17:01:06 +00:00
static inline uint32_t GetVariable ( const ResolverObject & object , ScopeResolver * scope , byte variable , uint32_t parameter , bool * available )
2006-04-27 19:53:58 +00:00
{
2023-05-08 17:01:06 +00:00
uint32_t value ;
2006-04-27 19:53:58 +00:00
switch ( variable ) {
2013-11-24 14:41:19 +00:00
case 0x0C : return object . callback ;
case 0x10 : return object . callback_param1 ;
case 0x18 : return object . callback_param2 ;
case 0x1C : return object . last_value ;
2006-04-27 19:53:58 +00:00
2012-11-10 20:37:31 +00:00
case 0x5F : return ( scope - > GetRandomBits ( ) < < 8 ) | scope - > GetTriggers ( ) ;
2008-06-17 07:05:17 +00:00
2011-06-12 20:40:21 +00:00
case 0x7D : return _temp_store . GetValue ( parameter ) ;
2007-04-21 07:27:16 +00:00
2008-07-30 18:23:12 +00:00
case 0x7F :
2019-04-10 21:07:06 +00:00
if ( object . grffile = = nullptr ) return 0 ;
2013-11-24 14:41:19 +00:00
return object . grffile - > GetParam ( parameter ) ;
2008-07-30 18:23:12 +00:00
2018-03-11 13:17:44 +00:00
default :
/* First handle variables common with Action7/9/D */
if ( variable < 0x40 & & GetGlobalVariable ( variable , & value , object . grffile ) ) return value ;
/* Not a common variable, so evaluate the feature specific variables */
return scope - > GetVariable ( variable , parameter , available ) ;
2006-04-27 19:53:58 +00:00
}
}
2012-11-10 20:37:31 +00:00
/**
* Get a few random bits . Default implementation has no random bits .
* @ return Random bits .
*/
2023-05-08 17:01:06 +00:00
/* virtual */ uint32_t ScopeResolver : : GetRandomBits ( ) const
2012-11-10 20:37:31 +00:00
{
return 0 ;
}
/**
* Get the triggers . Base class returns \ c 0 to prevent trouble .
* @ return The triggers .
*/
2023-05-08 17:01:06 +00:00
/* virtual */ uint32_t ScopeResolver : : GetTriggers ( ) const
2012-11-10 20:37:31 +00:00
{
return 0 ;
}
/**
* Get a variable value . Default implementation has no available variables .
* @ param variable Variable to read
* @ param parameter Parameter for 60 + x variables
* @ param [ out ] available Set to false , in case the variable does not exist .
* @ return Value
*/
2023-09-16 20:20:53 +00:00
/* virtual */ uint32_t ScopeResolver : : GetVariable ( byte variable , [[maybe_unused]] uint32_t parameter , bool * available ) const
2012-11-10 20:37:31 +00:00
{
2021-06-12 07:10:17 +00:00
Debug ( grf , 1 , " Unhandled scope variable 0x{:X} " , variable ) ;
2012-11-10 20:37:31 +00:00
* available = false ;
return UINT_MAX ;
}
/**
* Store a value into the persistent storage area ( PSA ) . Default implementation does nothing ( for newgrf classes without storage ) .
*/
2023-09-16 20:20:53 +00:00
/* virtual */ void ScopeResolver : : StorePSA ( uint , int32_t ) { }
2012-11-10 20:37:31 +00:00
2012-11-10 20:46:39 +00:00
/**
* Get the real sprites of the grf .
* @ param group Group to get .
* @ return The available sprite group .
*/
2012-11-10 20:37:31 +00:00
/* virtual */ const SpriteGroup * ResolverObject : : ResolveReal ( const RealSpriteGroup * group ) const
{
2021-06-04 11:52:00 +00:00
if ( ! group - > loaded . empty ( ) ) return group - > loaded [ 0 ] ;
if ( ! group - > loading . empty ( ) ) return group - > loading [ 0 ] ;
2019-04-10 21:07:06 +00:00
return nullptr ;
2012-11-10 20:37:31 +00:00
}
/**
2012-11-10 20:46:39 +00:00
* Get a resolver for the \ a scope .
* @ return The resolver for the requested scope .
2012-11-10 20:37:31 +00:00
*/
2023-09-16 20:20:53 +00:00
/* virtual */ ScopeResolver * ResolverObject : : GetScope ( VarSpriteGroupScope , byte )
2012-11-10 20:37:31 +00:00
{
2012-11-10 20:45:59 +00:00
return & this - > default_scope ;
2012-11-10 20:37:31 +00:00
}
2006-04-27 19:53:58 +00:00
2007-01-11 10:58:53 +00:00
/* Evaluate an adjustment for a variable of the given size.
2009-03-14 18:16:29 +00:00
* U is the unsigned type and S is the signed type to use . */
2007-01-11 10:58:53 +00:00
template < typename U , typename S >
2023-05-08 17:01:06 +00:00
static U EvalAdjustT ( const DeterministicSpriteGroupAdjust & adjust , ScopeResolver * scope , U last_value , uint32_t value )
2007-01-11 10:58:53 +00:00
{
2021-05-01 23:04:34 +00:00
value > > = adjust . shift_num ;
value & = adjust . and_mask ;
2007-01-11 10:58:53 +00:00
2021-05-01 23:04:34 +00:00
switch ( adjust . type ) {
case DSGA_TYPE_DIV : value = ( ( S ) value + ( S ) adjust . add_val ) / ( S ) adjust . divmod_val ; break ;
case DSGA_TYPE_MOD : value = ( ( S ) value + ( S ) adjust . add_val ) % ( S ) adjust . divmod_val ; break ;
2007-01-11 10:58:53 +00:00
case DSGA_TYPE_NONE : break ;
}
2006-04-27 19:53:58 +00:00
2021-05-01 23:04:34 +00:00
switch ( adjust . operation ) {
2007-01-11 10:58:53 +00:00
case DSGA_OP_ADD : return last_value + value ;
case DSGA_OP_SUB : return last_value - value ;
2021-01-08 10:16:18 +00:00
case DSGA_OP_SMIN : return std : : min < S > ( last_value , value ) ;
case DSGA_OP_SMAX : return std : : max < S > ( last_value , value ) ;
case DSGA_OP_UMIN : return std : : min < U > ( last_value , value ) ;
case DSGA_OP_UMAX : return std : : max < U > ( last_value , value ) ;
2007-05-14 17:37:34 +00:00
case DSGA_OP_SDIV : return value = = 0 ? ( S ) last_value : ( S ) last_value / ( S ) value ;
case DSGA_OP_SMOD : return value = = 0 ? ( S ) last_value : ( S ) last_value % ( S ) value ;
case DSGA_OP_UDIV : return value = = 0 ? ( U ) last_value : ( U ) last_value / ( U ) value ;
case DSGA_OP_UMOD : return value = = 0 ? ( U ) last_value : ( U ) last_value % ( U ) value ;
2007-01-11 10:58:53 +00:00
case DSGA_OP_MUL : return last_value * value ;
case DSGA_OP_AND : return last_value & value ;
case DSGA_OP_OR : return last_value | value ;
case DSGA_OP_XOR : return last_value ^ value ;
2011-06-12 20:40:21 +00:00
case DSGA_OP_STO : _temp_store . StoreValue ( ( U ) value , ( S ) last_value ) ; return last_value ;
2007-04-21 07:27:16 +00:00
case DSGA_OP_RST : return value ;
2012-11-11 12:57:27 +00:00
case DSGA_OP_STOP : scope - > StorePSA ( ( U ) value , ( S ) last_value ) ; return last_value ;
2023-05-08 17:01:06 +00:00
case DSGA_OP_ROR : return ROR < uint32_t > ( ( U ) last_value , ( U ) value & 0x1F ) ; // mask 'value' to 5 bits, which should behave the same on all architectures.
2007-09-22 20:29:17 +00:00
case DSGA_OP_SCMP : return ( ( S ) last_value = = ( S ) value ) ? 1 : ( ( S ) last_value < ( S ) value ? 0 : 2 ) ;
case DSGA_OP_UCMP : return ( ( U ) last_value = = ( U ) value ) ? 1 : ( ( U ) last_value < ( U ) value ? 0 : 2 ) ;
2023-05-08 17:01:06 +00:00
case DSGA_OP_SHL : return ( uint32_t ) ( U ) last_value < < ( ( U ) value & 0x1F ) ; // Same behaviour as in ParamSet, mask 'value' to 5 bits, which should behave the same on all architectures.
case DSGA_OP_SHR : return ( uint32_t ) ( U ) last_value > > ( ( U ) value & 0x1F ) ;
case DSGA_OP_SAR : return ( int32_t ) ( S ) last_value > > ( ( U ) value & 0x1F ) ;
2007-01-11 10:58:53 +00:00
default : return value ;
}
}
2006-04-27 19:53:58 +00:00
2024-01-03 21:33:38 +00:00
static bool RangeHighComparator ( const DeterministicSpriteGroupRange & range , uint32_t value )
2018-03-11 13:21:27 +00:00
{
return range . high < value ;
}
2013-11-24 14:41:19 +00:00
const SpriteGroup * DeterministicSpriteGroup : : Resolve ( ResolverObject & object ) const
2006-04-27 19:53:58 +00:00
{
2023-05-08 17:01:06 +00:00
uint32_t last_value = 0 ;
uint32_t value = 0 ;
2006-04-27 19:53:58 +00:00
2013-11-24 14:41:19 +00:00
ScopeResolver * scope = object . GetScope ( this - > var_scope ) ;
2006-04-27 19:53:58 +00:00
2021-05-01 23:04:34 +00:00
for ( const auto & adjust : this - > adjusts ) {
2006-05-23 19:36:50 +00:00
/* Try to get the variable. We shall assume it is available, unless told otherwise. */
bool available = true ;
2021-05-01 23:04:34 +00:00
if ( adjust . variable = = 0x7E ) {
const SpriteGroup * subgroup = SpriteGroup : : Resolve ( adjust . subroutine , object , false ) ;
2019-04-10 21:07:06 +00:00
if ( subgroup = = nullptr ) {
2007-01-12 11:20:34 +00:00
value = CALLBACK_FAILED ;
} else {
2009-05-23 12:13:42 +00:00
value = subgroup - > GetCallbackResult ( ) ;
2007-01-12 11:20:34 +00:00
}
2009-09-30 20:25:59 +00:00
2012-11-11 12:57:27 +00:00
/* Note: 'last_value' and 'reseed' are shared between the main chain and the procedure */
2021-05-01 23:04:34 +00:00
} else if ( adjust . variable = = 0x7B ) {
value = GetVariable ( object , scope , adjust . parameter , last_value , & available ) ;
2007-01-12 11:20:34 +00:00
} else {
2021-05-01 23:04:34 +00:00
value = GetVariable ( object , scope , adjust . variable , adjust . parameter , & available ) ;
2007-01-12 11:20:34 +00:00
}
2006-05-23 19:36:50 +00:00
if ( ! available ) {
2010-11-15 16:43:46 +00:00
/* Unsupported variable: skip further processing and return either
2006-05-23 19:36:50 +00:00
* the group from the first range or the default group . */
2018-03-11 13:21:27 +00:00
return SpriteGroup : : Resolve ( this - > error_group , object , false ) ;
2006-05-23 19:36:50 +00:00
}
2009-05-23 15:25:52 +00:00
switch ( this - > size ) {
2023-05-08 17:01:06 +00:00
case DSG_SIZE_BYTE : value = EvalAdjustT < uint8_t , int8_t > ( adjust , scope , last_value , value ) ; break ;
case DSG_SIZE_WORD : value = EvalAdjustT < uint16_t , int16_t > ( adjust , scope , last_value , value ) ; break ;
case DSG_SIZE_DWORD : value = EvalAdjustT < uint32_t , int32_t > ( adjust , scope , last_value , value ) ; break ;
2009-05-26 15:46:24 +00:00
default : NOT_REACHED ( ) ;
2006-04-27 19:53:58 +00:00
}
last_value = value ;
}
2013-11-24 14:41:19 +00:00
object . last_value = last_value ;
2007-01-11 14:40:12 +00:00
2018-03-11 15:08:51 +00:00
if ( this - > calculated_result ) {
2006-05-23 19:36:50 +00:00
/* nvar == 0 is a special case -- we turn our value into a callback result */
2008-05-04 22:32:25 +00:00
if ( value ! = CALLBACK_FAILED ) value = GB ( value , 0 , 15 ) ;
2011-11-08 17:22:19 +00:00
static CallbackResultSpriteGroup nvarzero ( 0 , true ) ;
2009-05-23 12:13:42 +00:00
nvarzero . result = value ;
2006-05-23 19:36:50 +00:00
return & nvarzero ;
}
2006-04-27 19:53:58 +00:00
2021-05-01 23:00:06 +00:00
if ( this - > ranges . size ( ) > 4 ) {
const auto & lower = std : : lower_bound ( this - > ranges . begin ( ) , this - > ranges . end ( ) , value , RangeHighComparator ) ;
if ( lower ! = this - > ranges . end ( ) & & lower - > low < = value ) {
2018-03-11 13:21:27 +00:00
assert ( lower - > low < = value & & value < = lower - > high ) ;
return SpriteGroup : : Resolve ( lower - > group , object , false ) ;
}
} else {
2021-05-01 23:00:06 +00:00
for ( const auto & range : this - > ranges ) {
if ( range . low < = value & & value < = range . high ) {
return SpriteGroup : : Resolve ( range . group , object , false ) ;
2018-03-11 13:21:27 +00:00
}
2006-04-27 19:53:58 +00:00
}
}
2013-12-23 18:08:16 +00:00
return SpriteGroup : : Resolve ( this - > default_group , object , false ) ;
2006-04-27 19:53:58 +00:00
}
2013-11-24 14:41:19 +00:00
const SpriteGroup * RandomizedSpriteGroup : : Resolve ( ResolverObject & object ) const
2006-04-27 19:53:58 +00:00
{
2013-11-24 14:41:19 +00:00
ScopeResolver * scope = object . GetScope ( this - > var_scope , this - > count ) ;
2017-10-25 15:38:14 +00:00
if ( object . callback = = CBID_RANDOM_TRIGGER ) {
2006-04-27 19:53:58 +00:00
/* Handle triggers */
2017-10-25 15:38:14 +00:00
byte match = this - > triggers & object . waiting_triggers ;
2009-05-23 15:25:52 +00:00
bool res = ( this - > cmp_mode = = RSG_CMP_ANY ) ? ( match ! = 0 ) : ( match = = this - > triggers ) ;
2006-04-27 19:53:58 +00:00
if ( res ) {
2017-10-25 15:38:14 +00:00
object . used_triggers | = match ;
2021-05-01 23:00:40 +00:00
object . reseed [ this - > var_scope ] | = ( this - > groups . size ( ) - 1 ) < < this - > lowest_randbit ;
2006-04-27 19:53:58 +00:00
}
}
2023-05-08 17:01:06 +00:00
uint32_t mask = ( ( uint ) this - > groups . size ( ) - 1 ) < < this - > lowest_randbit ;
2012-11-10 20:37:31 +00:00
byte index = ( scope - > GetRandomBits ( ) & mask ) > > this - > lowest_randbit ;
2006-04-27 19:53:58 +00:00
2013-12-23 18:08:16 +00:00
return SpriteGroup : : Resolve ( this - > groups [ index ] , object , false ) ;
2006-04-27 19:53:58 +00:00
}
2013-11-24 14:41:19 +00:00
const SpriteGroup * RealSpriteGroup : : Resolve ( ResolverObject & object ) const
2006-04-27 19:53:58 +00:00
{
2013-11-24 14:41:19 +00:00
return object . ResolveReal ( this ) ;
2006-04-27 19:53:58 +00:00
}
2011-05-29 16:56:22 +00:00
/**
* Process registers and the construction stage into the sprite layout .
* The passed construction stage might get reset to zero , if it gets incorporated into the layout
* during the preprocessing .
2019-04-10 21:07:06 +00:00
* @ param [ in , out ] stage Construction stage ( 0 - 3 ) , or nullptr if not applicable .
2011-05-29 16:56:22 +00:00
* @ return sprite layout to draw .
*/
2023-05-08 17:01:06 +00:00
const DrawTileSprites * TileLayoutSpriteGroup : : ProcessRegisters ( uint8_t * stage ) const
2011-05-29 16:56:22 +00:00
{
2011-09-11 15:10:09 +00:00
if ( ! this - > dts . NeedsPreprocessing ( ) ) {
2019-04-10 21:07:06 +00:00
if ( stage ! = nullptr & & this - > dts . consistent_max_offset > 0 ) * stage = GetConstructionStageOffset ( * stage , this - > dts . consistent_max_offset ) ;
2011-09-11 15:10:09 +00:00
return & this - > dts ;
}
2011-05-29 16:56:22 +00:00
static DrawTileSprites result ;
2023-05-08 17:01:06 +00:00
uint8_t actual_stage = stage ! = nullptr ? * stage : 0 ;
2011-09-11 15:10:09 +00:00
this - > dts . PrepareLayout ( 0 , 0 , 0 , actual_stage , false ) ;
2011-05-29 16:56:22 +00:00
this - > dts . ProcessRegisters ( 0 , 0 , false ) ;
result . seq = this - > dts . GetLayout ( & result . ground ) ;
/* Stage has been processed by PrepareLayout(), set it to zero. */
2019-04-10 21:07:06 +00:00
if ( stage ! = nullptr ) * stage = 0 ;
2011-05-29 16:56:22 +00:00
return & result ;
}