2009-08-21 22:21:05 +02: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 17:11:33 +02:00
/** @file newgrf.cpp Base of all NewGRF support. */
2007-03-21 04:06:21 +01:00
2004-08-09 19:04:08 +02:00
# include "stdafx.h"
2023-05-18 11:20:35 +02:00
# include "core/container_func.hpp"
2005-02-05 16:58:59 +01:00
# include "debug.h"
2008-08-31 12:50:05 +02:00
# include "fileio_func.h"
2008-03-31 02:17:39 +02:00
# include "engine_func.h"
2008-04-29 23:31:29 +02:00
# include "engine_base.h"
2005-10-01 19:38:48 +02:00
# include "bridge.h"
2007-03-19 12:27:30 +01:00
# include "town.h"
2006-02-03 13:55:21 +01:00
# include "newgrf_engine.h"
2006-04-21 05:00:20 +02:00
# include "newgrf_text.h"
2006-11-16 23:05:33 +01:00
# include "fontcache.h"
2006-08-25 02:41:10 +02:00
# include "currency.h"
2007-03-20 14:47:00 +01:00
# include "landscape.h"
2009-11-05 20:46:17 +01:00
# include "newgrf_cargo.h"
2007-03-19 12:27:30 +01:00
# include "newgrf_house.h"
2006-09-27 20:17:01 +02:00
# include "newgrf_sound.h"
2008-03-31 08:42:26 +02:00
# include "newgrf_station.h"
2010-08-27 00:01:16 +02:00
# include "industrytype.h"
2022-05-29 15:56:30 +02:00
# include "industry_map.h"
2007-05-06 20:14:33 +02:00
# include "newgrf_canal.h"
2007-06-19 01:00:55 +02:00
# include "newgrf_townname.h"
2007-07-12 00:57:47 +02:00
# include "newgrf_industries.h"
2010-02-22 15:15:48 +01:00
# include "newgrf_airporttiles.h"
2010-03-06 00:21:23 +01:00
# include "newgrf_airport.h"
2010-08-28 19:29:12 +02:00
# include "newgrf_object.h"
2008-05-04 23:53:36 +02:00
# include "rev.h"
2007-10-30 00:02:31 +01:00
# include "fios.h"
2007-12-21 20:49:27 +01:00
# include "strings_func.h"
2023-04-24 18:55:40 +02:00
# include "timer/timer_game_tick.h"
2023-04-24 20:33:18 +02:00
# include "timer/timer_game_calendar.h"
2008-01-07 15:23:25 +01:00
# include "string_func.h"
2021-07-11 21:22:29 +02:00
# include "network/core/config.h"
2010-02-08 22:19:41 +01:00
# include "smallmap_gui.h"
2010-07-19 19:17:36 +02:00
# include "genworld.h"
2011-12-10 14:54:10 +01:00
# include "error.h"
2023-04-19 22:47:36 +02:00
# include "error_func.h"
2010-09-25 23:59:22 +02:00
# include "vehicle_func.h"
2010-11-16 13:47:22 +01:00
# include "language.h"
2010-11-18 15:20:03 +01:00
# include "vehicle_base.h"
2019-04-06 08:46:15 +02:00
# include "road.h"
2022-11-06 16:01:27 +01:00
# include "newgrf_roadstop.h"
2006-04-20 22:51:57 +02:00
2008-01-13 02:21:35 +01:00
# include "table/strings.h"
# include "table/build_industry.h"
2014-04-23 22:13:33 +02:00
# include "safeguards.h"
2004-08-09 19:04:08 +02:00
/* TTDPatch extended GRF format codec
* ( c ) Petr Baudis 2004 ( GPL ' d )
2004-11-12 16:15:56 +01:00
* Changes by Florian octo Forster are ( c ) by the OpenTTD development team .
*
2004-08-09 19:04:08 +02:00
* Contains portions of documentation by TTDPatch team .
* Thanks especially to Josef Drexler for the documentation as well as a lot
2012-09-21 11:47:21 +02:00
* of help at # tycoon . Also thanks to Michael Blunck for his GRF files which
2004-08-09 19:04:08 +02:00
* served as subject to the initial testing of this codec . */
2011-08-21 14:05:47 +02:00
/** List of all loaded GRF files */
2019-03-03 18:30:09 +01:00
static std : : vector < GRFFile * > _grf_files ;
2009-11-23 14:14:45 +01:00
2020-01-26 13:45:51 +01:00
const std : : vector < GRFFile * > & GetAllGRFFiles ( )
{
return _grf_files ;
}
2011-08-21 14:05:47 +02:00
/** Miscellaneous GRF features, set by Action 0x0D, parameter 0x9E */
2012-01-01 17:38:38 +01:00
byte _misc_grf_features = 0 ;
2006-05-11 14:41:02 +02:00
2011-08-21 14:05:47 +02:00
/** 32 * 8 = 256 flags. Apparently TTDPatch uses this many.. */
2004-11-12 20:03:37 +01:00
static uint32 _ttdpatch_flags [ 8 ] ;
2011-08-21 14:05:47 +02:00
/** Indicates which are the newgrf features currently loaded ingame */
2007-06-16 01:55:52 +02:00
GRFLoadedFeatures _loaded_newgrf_features ;
2004-11-12 20:03:37 +01:00
2011-08-21 14:07:15 +02:00
static const uint MAX_SPRITEGROUP = UINT8_MAX ; ///< Maximum GRF-local ID for a spritegroup.
2011-08-21 14:05:47 +02:00
/** Temporary data during loading of GRFs */
struct GrfProcessingState {
2011-09-11 17:08:17 +02:00
private :
2011-09-11 17:10:56 +02:00
/** Definition of a single Action1 spriteset */
struct SpriteSet {
SpriteID sprite ; ///< SpriteID of the first sprite of the set.
uint num_sprites ; ///< Number of sprites in the set.
} ;
/** Currently referenceable spritesets */
std : : map < uint , SpriteSet > spritesets [ GSF_END ] ;
2011-09-11 17:08:17 +02:00
public :
2011-08-21 14:05:47 +02:00
/* Global state */
GrfLoadingStage stage ; ///< Current loading stage
SpriteID spriteid ; ///< First available SpriteID for loading realsprites.
/* Local state in the file */
2021-04-14 17:20:39 +02:00
SpriteFile * file ; ///< File of currently processed GRF file.
2011-08-21 14:05:47 +02:00
GRFFile * grffile ; ///< Currently processed GRF file.
GRFConfig * grfconfig ; ///< Config of the currently processed GRF file.
uint32 nfo_line ; ///< Currently processed pseudo sprite number in the GRF.
/* Kind of return values when processing certain actions */
2019-09-29 22:27:32 +02:00
int skip_sprites ; ///< Number of pseudo sprites to skip before processing the next one. (-1 to skip to end of file)
2011-08-21 14:06:24 +02:00
2011-08-21 14:07:15 +02:00
/* Currently referenceable spritegroups */
2021-06-04 13:31:00 +02:00
const SpriteGroup * spritegroups [ MAX_SPRITEGROUP + 1 ] ;
2011-08-21 14:07:15 +02:00
2011-08-21 14:06:24 +02:00
/** Clear temporary data before processing the next file in the current loading stage */
void ClearDataForNextFile ( )
{
2011-08-21 16:59:05 +02:00
this - > nfo_line = 0 ;
this - > skip_sprites = 0 ;
2011-08-21 14:06:54 +02:00
2011-09-11 17:10:56 +02:00
for ( uint i = 0 ; i < GSF_END ; i + + ) {
this - > spritesets [ i ] . clear ( ) ;
}
2011-08-21 14:07:15 +02:00
2011-08-21 16:59:05 +02:00
memset ( this - > spritegroups , 0 , sizeof ( this - > spritegroups ) ) ;
2011-08-21 14:06:24 +02:00
}
2011-09-11 17:08:17 +02:00
/**
* Records new spritesets .
* @ param feature GrfSpecFeature the set is defined for .
* @ param first_sprite SpriteID of the first sprite in the set .
2011-09-11 17:11:23 +02:00
* @ param first_set First spriteset to define .
2011-09-11 17:08:17 +02:00
* @ param numsets Number of sets to define .
* @ param numents Number of sprites per set to define .
*/
2011-09-11 17:11:23 +02:00
void AddSpriteSets ( byte feature , SpriteID first_sprite , uint first_set , uint numsets , uint numents )
2011-09-11 17:08:17 +02:00
{
2011-09-11 17:10:56 +02:00
assert ( feature < GSF_END ) ;
for ( uint i = 0 ; i < numsets ; i + + ) {
2011-09-11 17:11:23 +02:00
SpriteSet & set = this - > spritesets [ feature ] [ first_set + i ] ;
2011-09-11 17:10:56 +02:00
set . sprite = first_sprite + i * numents ;
set . num_sprites = numents ;
}
2011-09-11 17:08:17 +02:00
}
/**
* Check whether there are any valid spritesets for a feature .
* @ param feature GrfSpecFeature to check .
* @ return true if there are any valid sets .
* @ note Spritesets with zero sprites are valid to allow callback - failures .
*/
bool HasValidSpriteSets ( byte feature ) const
{
2011-09-11 17:10:56 +02:00
assert ( feature < GSF_END ) ;
return ! this - > spritesets [ feature ] . empty ( ) ;
2011-09-11 17:08:17 +02:00
}
/**
* Check whether a specific set is defined .
* @ param feature GrfSpecFeature to check .
* @ param set Set to check .
* @ return true if the set is valid .
* @ note Spritesets with zero sprites are valid to allow callback - failures .
*/
bool IsValidSpriteSet ( byte feature , uint set ) const
{
2011-09-11 17:10:56 +02:00
assert ( feature < GSF_END ) ;
return this - > spritesets [ feature ] . find ( set ) ! = this - > spritesets [ feature ] . end ( ) ;
2011-09-11 17:08:17 +02:00
}
/**
* Returns the first sprite of a spriteset .
* @ param feature GrfSpecFeature to query .
* @ param set Set to query .
* @ return First sprite of the set .
*/
SpriteID GetSprite ( byte feature , uint set ) const
{
assert ( IsValidSpriteSet ( feature , set ) ) ;
2011-09-11 17:10:56 +02:00
return this - > spritesets [ feature ] . find ( set ) - > second . sprite ;
2011-09-11 17:08:17 +02:00
}
/**
* Returns the number of sprites in a spriteset
* @ param feature GrfSpecFeature to query .
* @ param set Set to query .
* @ return Number of sprites in the set .
*/
uint GetNumEnts ( byte feature , uint set ) const
{
assert ( IsValidSpriteSet ( feature , set ) ) ;
2011-09-11 17:10:56 +02:00
return this - > spritesets [ feature ] . find ( set ) - > second . num_sprites ;
2011-09-11 17:08:17 +02:00
}
2011-08-21 14:05:47 +02:00
} ;
static GrfProcessingState _cur ;
2006-09-27 20:17:01 +02:00
2013-11-26 17:08:58 +01:00
/**
* Helper to check whether an image index is valid for a particular NewGRF vehicle .
2018-10-28 03:17:36 +01:00
* @ tparam T The type of vehicle .
2013-11-26 17:08:58 +01:00
* @ param image_index The image index to check .
* @ return True iff the image index is valid , or 0xFD ( use new graphics ) .
*/
template < VehicleType T >
static inline bool IsValidNewGRFImageIndex ( uint8 image_index )
{
return image_index = = 0xFD | | IsValidImageIndex < T > ( image_index ) ;
}
2010-01-22 14:02:15 +01:00
class OTTDByteReaderSignal { } ;
2006-09-27 20:17:01 +02:00
2011-09-11 13:14:22 +02:00
/** Class to read from a NewGRF file */
2010-01-22 14:02:15 +01:00
class ByteReader {
protected :
byte * data ;
byte * end ;
public :
ByteReader ( byte * data , byte * end ) : data ( data ) , end ( end ) { }
2021-05-01 19:07:47 +02:00
inline byte * ReadBytes ( size_t size )
{
if ( data + size > = end ) {
/* Put data at the end, as would happen if every byte had been individually read. */
data = end ;
throw OTTDByteReaderSignal ( ) ;
}
byte * ret = data ;
data + = size ;
return ret ;
}
2011-12-20 18:57:56 +01:00
inline byte ReadByte ( )
2010-01-22 14:02:15 +01:00
{
if ( data < end ) return * ( data ) + + ;
throw OTTDByteReaderSignal ( ) ;
}
2010-01-23 14:17:30 +01:00
uint16 ReadWord ( )
{
uint16 val = ReadByte ( ) ;
return val | ( ReadByte ( ) < < 8 ) ;
}
2010-03-10 08:31:08 +01:00
uint16 ReadExtendedByte ( )
2010-01-23 14:17:30 +01:00
{
uint16 val = ReadByte ( ) ;
return val = = 0xFF ? ReadWord ( ) : val ;
}
uint32 ReadDWord ( )
{
uint32 val = ReadWord ( ) ;
return val | ( ReadWord ( ) < < 16 ) ;
}
uint32 ReadVarSize ( byte size )
{
switch ( size ) {
case 1 : return ReadByte ( ) ;
case 2 : return ReadWord ( ) ;
case 4 : return ReadDWord ( ) ;
default :
NOT_REACHED ( ) ;
return 0 ;
}
}
const char * ReadString ( )
{
char * string = reinterpret_cast < char * > ( data ) ;
size_t string_length = ttd_strnlen ( string , Remaining ( ) ) ;
if ( string_length = = Remaining ( ) ) {
/* String was not NUL terminated, so make sure it is now. */
string [ string_length - 1 ] = ' \0 ' ;
2023-04-18 21:24:42 +02:00
GrfMsg ( 7 , " String was not terminated with a zero byte. " ) ;
2010-01-23 14:17:30 +01:00
} else {
/* Increase the string length to include the NUL byte. */
string_length + + ;
}
Skip ( string_length ) ;
return string ;
}
2011-12-20 18:57:56 +01:00
inline size_t Remaining ( ) const
2010-01-22 14:02:15 +01:00
{
2010-01-22 16:34:22 +01:00
return end - data ;
2010-01-22 14:02:15 +01:00
}
2011-12-20 18:57:56 +01:00
inline bool HasData ( size_t count = 1 ) const
2010-01-22 14:02:15 +01:00
{
2011-05-18 20:31:23 +02:00
return data + count < = end ;
2010-01-22 14:02:15 +01:00
}
2011-12-20 18:57:56 +01:00
inline byte * Data ( )
2010-01-22 14:02:15 +01:00
{
return data ;
}
2011-12-20 18:57:56 +01:00
inline void Skip ( size_t len )
2010-01-22 14:02:15 +01:00
{
data + = len ;
/* It is valid to move the buffer to exactly the end of the data,
* as there may not be any more data read . */
if ( data > end ) throw OTTDByteReaderSignal ( ) ;
}
} ;
typedef void ( * SpecialSpriteHandler ) ( ByteReader * buf ) ;
2004-08-09 19:04:08 +02:00
2023-04-18 01:12:34 +02:00
/** The maximum amount of stations a single GRF is allowed to add */
static const uint NUM_STATIONS_PER_GRF = UINT16_MAX - 1 ;
2006-05-07 00:20:16 +02:00
2011-09-11 13:14:22 +02:00
/** Temporary engine data used when loading only */
2008-04-22 20:20:41 +02:00
struct GRFTempEngineData {
2012-09-21 11:47:21 +02:00
/** Summary state of refittability properties */
2012-02-07 23:03:03 +01:00
enum Refittability {
UNSET = 0 , ///< No properties assigned. Default refit masks shall be activated.
EMPTY , ///< GRF defined vehicle as not-refittable. The vehicle shall only carry the default cargo.
NONEMPTY , ///< GRF defined the vehicle as refittable. If the refitmask is empty after translation (cargotypes not available), disable the vehicle.
} ;
2008-04-22 20:20:41 +02:00
uint16 cargo_allowed ;
uint16 cargo_disallowed ;
2010-03-23 12:05:28 +01:00
RailTypeLabel railtypelabel ;
2019-04-06 08:46:15 +02:00
uint8 roadtramtype ;
2012-02-07 23:59:30 +01:00
const GRFFile * defaultcargo_grf ; ///< GRF defining the cargo translation table to use if the default cargo is the 'first refittable'.
2012-02-07 23:05:44 +01:00
Refittability refittability ; ///< Did the newgrf set any refittability property? If not, default refittability will be applied.
2009-01-06 15:45:38 +01:00
uint8 rv_max_speed ; ///< Temporary storage of RV prop 15, maximum speed in mph/0.8
2018-05-21 23:08:39 +02:00
CargoTypes ctt_include_mask ; ///< Cargo types always included in the refit mask.
CargoTypes ctt_exclude_mask ; ///< Cargo types always excluded from the refit mask.
2012-02-07 23:03:03 +01:00
/**
* Update the summary refittability on setting a refittability property .
* @ param non_empty true if the GRF sets the vehicle to be refittable .
*/
void UpdateRefittability ( bool non_empty )
{
if ( non_empty ) {
this - > refittability = NONEMPTY ;
} else if ( this - > refittability = = UNSET ) {
this - > refittability = EMPTY ;
}
}
2008-04-22 20:20:41 +02:00
} ;
2023-04-20 21:38:48 +02:00
static std : : vector < GRFTempEngineData > _gted ; ///< Temporary engine data used during NewGRF loading
2004-08-09 19:04:08 +02:00
2011-09-11 13:14:22 +02:00
/**
* Contains the GRF ID of the owner of a vehicle if it has been reserved .
2008-04-29 23:31:29 +02:00
* GRM for vehicles is only used if dynamic engine allocation is disabled ,
* so 256 is the number of original engines . */
static uint32 _grm_engines [ 256 ] ;
2006-10-27 15:15:21 +02:00
2011-09-11 13:14:22 +02:00
/** Contains the GRF ID of the owner of a cargo if it has been reserved */
2011-12-15 22:56:00 +01:00
static uint32 _grm_cargoes [ NUM_CARGO * 2 ] ;
2007-06-02 10:56:21 +02:00
2008-05-06 19:24:27 +02:00
struct GRFLocation {
2008-05-05 21:02:10 +02:00
uint32 grfid ;
uint32 nfoline ;
2008-05-06 19:24:27 +02:00
GRFLocation ( uint32 grfid , uint32 nfoline ) : grfid ( grfid ) , nfoline ( nfoline ) { }
2008-05-05 21:02:10 +02:00
2008-05-06 19:24:27 +02:00
bool operator < ( const GRFLocation & other ) const
2008-05-05 21:02:10 +02:00
{
return this - > grfid < other . grfid | | ( this - > grfid = = other . grfid & & this - > nfoline < other . nfoline ) ;
}
2008-08-19 11:08:45 +02:00
2009-07-16 12:13:33 +02:00
bool operator = = ( const GRFLocation & other ) const
2008-08-19 11:08:45 +02:00
{
return this - > grfid = = other . grfid & & this - > nfoline = = other . nfoline ;
}
2008-05-05 21:02:10 +02:00
} ;
2008-05-06 19:24:27 +02:00
static std : : map < GRFLocation , SpriteID > _grm_sprites ;
2023-04-26 07:45:11 +02:00
typedef std : : map < GRFLocation , std : : vector < byte > > GRFLineToSpriteOverride ;
2009-11-09 10:59:35 +01:00
static GRFLineToSpriteOverride _grf_line_to_action6_sprite_override ;
2008-05-05 21:02:10 +02:00
2010-08-01 21:22:34 +02:00
/**
2021-06-12 09:10:17 +02:00
* Debug ( ) function dedicated to newGRF debugging messages
* Function is essentially the same as Debug ( grf , severity , . . . ) with the
2006-12-26 18:36:18 +01:00
* addition of file : line information when parsing grf files .
2023-04-18 21:24:42 +02:00
* NOTE : for the above reason ( s ) GrfMsg ( ) should ONLY be used for
2006-12-26 18:36:18 +01:00
* loading / parsing grf files , not for runtime debug messages as there
* is no file information available during that time .
* @ param severity debugging severity level , see debug . h
2023-04-18 21:24:42 +02:00
* @ param msg the message
2010-08-01 21:44:49 +02:00
*/
2023-04-18 21:24:42 +02:00
void GrfMsgI ( int severity , const std : : string & msg )
2004-08-09 19:04:08 +02:00
{
2023-04-18 21:24:42 +02:00
Debug ( grf , severity , " [{}:{}] {} " , _cur . grfconfig - > filename , _cur . nfo_line , msg ) ;
2004-08-09 19:04:08 +02:00
}
2011-09-11 13:14:22 +02:00
/**
* Obtain a NewGRF file by its grfID
* @ param grfid The grfID to obtain the file for
* @ return The file .
*/
2005-03-10 08:01:43 +01:00
static GRFFile * GetFileByGRFID ( uint32 grfid )
2004-11-12 23:28:19 +01:00
{
2019-02-17 12:20:52 +01:00
for ( GRFFile * const file : _grf_files ) {
if ( file - > grfid = = grfid ) return file ;
2005-03-10 08:01:43 +01:00
}
2019-04-10 23:07:06 +02:00
return nullptr ;
2004-11-12 23:28:19 +01:00
}
2011-09-11 13:14:22 +02:00
/**
* Obtain a NewGRF file by its filename
* @ param filename The filename to obtain the file for .
* @ return The file .
*/
2023-04-27 20:42:25 +02:00
static GRFFile * GetFileByFilename ( const std : : string & filename )
2004-11-12 23:28:19 +01:00
{
2019-02-17 12:20:52 +01:00
for ( GRFFile * const file : _grf_files ) {
2023-04-27 20:42:25 +02:00
if ( file - > filename = = filename ) return file ;
2005-03-10 08:01:43 +01:00
}
2019-04-10 23:07:06 +02:00
return nullptr ;
2004-11-12 23:28:19 +01:00
}
2008-07-04 20:42:04 +02:00
/** Reset all NewGRFData that was used only while processing data */
2008-12-25 00:01:16 +01:00
static void ClearTemporaryNewGRFData ( GRFFile * gf )
2008-07-04 20:42:04 +02:00
{
2022-12-25 23:32:22 +01:00
gf - > labels . clear ( ) ;
2008-07-04 20:42:04 +02:00
}
2011-05-11 22:22:04 +02:00
/**
* Disable a GRF
2011-09-11 13:14:22 +02:00
* @ param message Error message or STR_NULL .
2019-04-10 23:07:06 +02:00
* @ param config GRFConfig to disable , nullptr for current .
2011-09-11 13:14:22 +02:00
* @ return Error message of the GRF for further customisation .
2011-05-11 22:22:04 +02:00
*/
2019-04-10 23:07:06 +02:00
static GRFError * DisableGrf ( StringID message = STR_NULL , GRFConfig * config = nullptr )
2011-05-11 22:22:04 +02:00
{
GRFFile * file ;
2019-04-10 23:07:06 +02:00
if ( config ! = nullptr ) {
2011-05-11 22:22:04 +02:00
file = GetFileByGRFID ( config - > ident . grfid ) ;
} else {
2011-08-21 14:05:47 +02:00
config = _cur . grfconfig ;
file = _cur . grffile ;
2011-05-11 22:22:04 +02:00
}
config - > status = GCS_DISABLED ;
2019-04-10 23:07:06 +02:00
if ( file ! = nullptr ) ClearTemporaryNewGRFData ( file ) ;
2011-08-21 14:05:47 +02:00
if ( config = = _cur . grfconfig ) _cur . skip_sprites = - 1 ;
2011-05-11 22:22:04 +02:00
2023-06-25 12:57:58 +02:00
if ( message = = STR_NULL ) return nullptr ;
2011-05-11 22:22:04 +02:00
2023-06-25 12:57:58 +02:00
config - > error = { STR_NEWGRF_ERROR_MSG_FATAL , message } ;
if ( config = = _cur . grfconfig ) config - > error - > param_value [ 0 ] = _cur . nfo_line ;
return & config - > error . value ( ) ;
2011-05-11 22:22:04 +02:00
}
2014-01-12 19:01:50 +01:00
/**
* Information for mapping static StringIDs .
*/
struct StringIDMapping {
uint32 grfid ; ///< Source NewGRF.
StringID source ; ///< Source StringID (GRF local).
StringID * target ; ///< Destination for mapping result.
} ;
2019-03-03 18:30:09 +01:00
typedef std : : vector < StringIDMapping > StringIDMappingVector ;
2014-01-12 19:01:50 +01:00
static StringIDMappingVector _string_to_grf_mapping ;
2004-11-12 23:28:19 +01:00
2014-01-12 19:01:50 +01:00
/**
* Record a static StringID for getting translated later .
* @ param source Source StringID ( GRF local ) .
* @ param target Destination for the mapping result .
*/
static void AddStringForMapping ( StringID source , StringID * target )
{
* target = STR_UNDEFINED ;
2019-02-18 23:39:06 +01:00
_string_to_grf_mapping . push_back ( { _cur . grffile - > grfid , source , target } ) ;
2014-01-12 19:01:50 +01:00
}
2008-04-17 13:47:22 +02:00
2014-01-12 19:01:16 +01:00
/**
* Perform a mapping from TTDPatch ' s string IDs to OpenTTD ' s
* string IDs , but only for the ones we are aware off ; the rest
* like likely unused and will show a warning .
* @ param str the string ID to convert
* @ return the converted string ID
*/
static StringID TTDPStringIDToOTTDStringIDMapping ( StringID str )
{
/* StringID table for TextIDs 0x4E->0x6D */
static const StringID units_volume [ ] = {
STR_ITEMS , STR_PASSENGERS , STR_TONS , STR_BAGS ,
STR_LITERS , STR_ITEMS , STR_CRATES , STR_TONS ,
STR_TONS , STR_TONS , STR_TONS , STR_BAGS ,
STR_TONS , STR_TONS , STR_TONS , STR_BAGS ,
STR_TONS , STR_TONS , STR_BAGS , STR_LITERS ,
STR_TONS , STR_LITERS , STR_TONS , STR_ITEMS ,
STR_BAGS , STR_LITERS , STR_TONS , STR_ITEMS ,
STR_TONS , STR_ITEMS , STR_LITERS , STR_ITEMS
} ;
/* A string straight from a NewGRF; this was already translated by MapGRFStringID(). */
assert ( ! IsInsideMM ( str , 0xD000 , 0xD7FF ) ) ;
# define TEXTID_TO_STRINGID(begin, end, stringid, stringend) \
2020-12-27 11:44:22 +01:00
static_assert ( stringend - stringid = = end - begin ) ; \
2014-01-12 19:01:16 +01:00
if ( str > = begin & & str < = end ) return str + ( stringid - begin )
/* We have some changes in our cargo strings, resulting in some missing. */
TEXTID_TO_STRINGID ( 0x000E , 0x002D , STR_CARGO_PLURAL_NOTHING , STR_CARGO_PLURAL_FIZZY_DRINKS ) ;
TEXTID_TO_STRINGID ( 0x002E , 0x004D , STR_CARGO_SINGULAR_NOTHING , STR_CARGO_SINGULAR_FIZZY_DRINK ) ;
if ( str > = 0x004E & & str < = 0x006D ) return units_volume [ str - 0x004E ] ;
TEXTID_TO_STRINGID ( 0x006E , 0x008D , STR_QUANTITY_NOTHING , STR_QUANTITY_FIZZY_DRINKS ) ;
TEXTID_TO_STRINGID ( 0x008E , 0x00AD , STR_ABBREV_NOTHING , STR_ABBREV_FIZZY_DRINKS ) ;
TEXTID_TO_STRINGID ( 0x00D1 , 0x00E0 , STR_COLOUR_DARK_BLUE , STR_COLOUR_WHITE ) ;
/* Map building names according to our lang file changes. There are several
* ranges of house ids , all of which need to be remapped to allow newgrfs
* to use original house names . */
TEXTID_TO_STRINGID ( 0x200F , 0x201F , STR_TOWN_BUILDING_NAME_TALL_OFFICE_BLOCK_1 , STR_TOWN_BUILDING_NAME_OLD_HOUSES_1 ) ;
TEXTID_TO_STRINGID ( 0x2036 , 0x2041 , STR_TOWN_BUILDING_NAME_COTTAGES_1 , STR_TOWN_BUILDING_NAME_SHOPPING_MALL_1 ) ;
TEXTID_TO_STRINGID ( 0x2059 , 0x205C , STR_TOWN_BUILDING_NAME_IGLOO_1 , STR_TOWN_BUILDING_NAME_PIGGY_BANK_1 ) ;
/* Same thing for industries */
TEXTID_TO_STRINGID ( 0x4802 , 0x4826 , STR_INDUSTRY_NAME_COAL_MINE , STR_INDUSTRY_NAME_SUGAR_MINE ) ;
TEXTID_TO_STRINGID ( 0x482D , 0x482E , STR_NEWS_INDUSTRY_CONSTRUCTION , STR_NEWS_INDUSTRY_PLANTED ) ;
TEXTID_TO_STRINGID ( 0x4832 , 0x4834 , STR_NEWS_INDUSTRY_CLOSURE_GENERAL , STR_NEWS_INDUSTRY_CLOSURE_LACK_OF_TREES ) ;
TEXTID_TO_STRINGID ( 0x4835 , 0x4838 , STR_NEWS_INDUSTRY_PRODUCTION_INCREASE_GENERAL , STR_NEWS_INDUSTRY_PRODUCTION_INCREASE_FARM ) ;
TEXTID_TO_STRINGID ( 0x4839 , 0x483A , STR_NEWS_INDUSTRY_PRODUCTION_DECREASE_GENERAL , STR_NEWS_INDUSTRY_PRODUCTION_DECREASE_FARM ) ;
switch ( str ) {
case 0x4830 : return STR_ERROR_CAN_T_CONSTRUCT_THIS_INDUSTRY ;
case 0x4831 : return STR_ERROR_FOREST_CAN_ONLY_BE_PLANTED ;
case 0x483B : return STR_ERROR_CAN_ONLY_BE_POSITIONED ;
}
# undef TEXTID_TO_STRINGID
if ( str = = STR_NULL ) return STR_EMPTY ;
2021-06-12 09:10:17 +02:00
Debug ( grf , 0 , " Unknown StringID 0x{:04X} remapped to STR_EMPTY. Please open a Feature Request if you need it " , str ) ;
2014-01-12 19:01:16 +01:00
return STR_EMPTY ;
}
2010-08-01 21:22:34 +02:00
/**
* Used when setting an object ' s property to map to the GRF ' s strings
2007-03-19 12:27:30 +01:00
* while taking in consideration the " drift " between TTDPatch string system and OpenTTD ' s one
2011-09-11 13:14:22 +02:00
* @ param grfid Id of the grf file .
* @ param str StringID that we want to have the equivalent in OoenTTD .
* @ return The properly adjusted StringID .
2007-03-19 12:27:30 +01:00
*/
2007-09-23 01:40:35 +02:00
StringID MapGRFStringID ( uint32 grfid , StringID str )
2007-03-19 12:27:30 +01:00
{
2023-01-22 18:34:35 +01:00
if ( IsInsideMM ( str , 0xD800 , 0x10000 ) ) {
2017-03-05 15:44:15 +01:00
/* General text provided by NewGRF.
2019-09-29 22:27:32 +02:00
* In the specs this is called the 0xDC xx range ( misc persistent texts ) ,
2023-01-22 18:34:35 +01:00
* but we meanwhile extended the range to 0xD800 - 0xFFFF .
2017-03-05 15:44:15 +01:00
* Note : We are not involved in the " persistent " business , since we do not store
* any NewGRF strings in savegames . */
return GetGRFStringID ( grfid , str ) ;
} else if ( IsInsideMM ( str , 0xD000 , 0xD800 ) ) {
/* Callback text provided by NewGRF.
* In the specs this is called the 0xD0 xx range ( misc graphics texts ) .
* These texts can be returned by various callbacks .
*
* Due to how TTDP implements the GRF - local - to global - textid translation
* texts included via 0x80 or 0x81 control codes have to add 0x400 to the textid .
* We do not care about that difference and just mask out the 0x400 bit .
*/
str & = ~ 0x400 ;
return GetGRFStringID ( grfid , str ) ;
} else {
/* The NewGRF wants to include/reference an original TTD string.
* Try our best to find an equivalent one . */
return TTDPStringIDToOTTDStringIDMapping ( str ) ;
2007-03-19 12:27:30 +01:00
}
}
2008-04-29 23:31:29 +02:00
static std : : map < uint32 , uint32 > _grf_id_overrides ;
2011-09-11 13:14:22 +02:00
/**
* Set the override for a NewGRF
* @ param source_grfid The grfID which wants to override another NewGRF .
* @ param target_grfid The grfID which is being overridden .
*/
2008-04-29 23:31:29 +02:00
static void SetNewGRFOverride ( uint32 source_grfid , uint32 target_grfid )
{
_grf_id_overrides [ source_grfid ] = target_grfid ;
2023-04-20 17:00:46 +02:00
GrfMsg ( 5 , " SetNewGRFOverride: Added override of 0x{:X} to 0x{:X} " , BSWAP32 ( source_grfid ) , BSWAP32 ( target_grfid ) ) ;
2008-04-29 23:31:29 +02:00
}
2009-03-08 19:08:30 +01:00
/**
* Returns the engine associated to a certain internal_id , resp . allocates it .
2011-09-11 13:14:22 +02:00
* @ param file NewGRF that wants to change the engine .
* @ param type Vehicle type .
* @ param internal_id Engine ID inside the NewGRF .
2019-04-10 23:07:06 +02:00
* @ param static_access If the engine is not present , return nullptr instead of allocating a new engine . ( Used for static Action 0x04 ) .
2011-09-11 13:14:22 +02:00
* @ return The requested engine .
2009-03-08 19:08:30 +01:00
*/
static Engine * GetNewEngine ( const GRFFile * file , VehicleType type , uint16 internal_id , bool static_access = false )
2008-04-29 23:31:29 +02:00
{
/* Hack for add-on GRFs that need to modify another GRF's engines. This lets
* them use the same engine slots . */
2009-03-08 17:51:08 +01:00
uint32 scope_grfid = INVALID_GRFID ; // If not using dynamic_engines, all newgrfs share their ID range
2008-05-29 17:13:28 +02:00
if ( _settings_game . vehicle . dynamic_engines ) {
2009-03-08 17:51:08 +01:00
/* If dynamic_engies is enabled, there can be multiple independent ID ranges. */
scope_grfid = file - > grfid ;
2008-04-29 23:31:29 +02:00
uint32 override = _grf_id_overrides [ file - > grfid ] ;
if ( override ! = 0 ) {
2009-03-08 17:51:08 +01:00
scope_grfid = override ;
const GRFFile * grf_match = GetFileByGRFID ( override ) ;
2019-04-10 23:07:06 +02:00
if ( grf_match = = nullptr ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 5 , " Tried mapping from GRFID {:x} to {:x} but target is not loaded " , BSWAP32 ( file - > grfid ) , BSWAP32 ( override ) ) ;
2008-04-29 23:31:29 +02:00
} else {
2023-04-18 21:24:42 +02:00
GrfMsg ( 5 , " Mapping from GRFID {:x} to {:x} " , BSWAP32 ( file - > grfid ) , BSWAP32 ( override ) ) ;
2008-04-29 23:31:29 +02:00
}
}
2009-03-08 17:51:08 +01:00
/* Check if the engine is registered in the override manager */
EngineID engine = _engine_mngr . GetID ( type , internal_id , scope_grfid ) ;
2009-07-04 14:20:42 +02:00
if ( engine ! = INVALID_ENGINE ) {
Engine * e = Engine : : Get ( engine ) ;
2019-04-10 23:07:06 +02:00
if ( e - > grf_prop . grffile = = nullptr ) e - > grf_prop . grffile = file ;
2009-07-04 14:20:42 +02:00
return e ;
}
2008-04-29 23:31:29 +02:00
}
2009-03-08 17:51:08 +01:00
/* Check if there is an unreserved slot */
EngineID engine = _engine_mngr . GetID ( type , internal_id , INVALID_GRFID ) ;
if ( engine ! = INVALID_ENGINE ) {
2009-05-17 01:34:14 +02:00
Engine * e = Engine : : Get ( engine ) ;
2008-04-29 23:31:29 +02:00
2019-04-10 23:07:06 +02:00
if ( e - > grf_prop . grffile = = nullptr ) {
2010-08-10 17:54:53 +02:00
e - > grf_prop . grffile = file ;
2023-04-18 21:24:42 +02:00
GrfMsg ( 5 , " Replaced engine at index {} for GRFID {:x}, type {}, index {} " , e - > index , BSWAP32 ( file - > grfid ) , type , internal_id ) ;
2008-04-29 23:31:29 +02:00
}
2009-03-08 17:51:08 +01:00
/* Reserve the engine slot */
2009-03-08 19:08:30 +01:00
if ( ! static_access ) {
2018-09-25 22:01:56 +02:00
EngineIDMapping * eid = _engine_mngr . data ( ) + engine ;
2009-03-08 19:08:30 +01:00
eid - > grfid = scope_grfid ; // Note: this is INVALID_GRFID if dynamic_engines is disabled, so no reservation
}
2009-03-08 17:51:08 +01:00
2008-04-29 23:31:29 +02:00
return e ;
}
2019-04-10 23:07:06 +02:00
if ( static_access ) return nullptr ;
2009-03-08 19:08:30 +01:00
2011-02-09 19:46:55 +01:00
if ( ! Engine : : CanAllocateItem ( ) ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 0 , " Can't allocate any more engines " ) ;
2019-04-10 23:07:06 +02:00
return nullptr ;
2011-02-09 19:46:55 +01:00
}
2009-05-22 17:13:50 +02:00
size_t engine_pool_size = Engine : : GetPoolSize ( ) ;
2008-04-29 23:31:29 +02:00
/* ... it's not, so create a new one based off an existing engine */
2009-03-08 17:51:08 +01:00
Engine * e = new Engine ( type , internal_id ) ;
2010-08-10 17:54:53 +02:00
e - > grf_prop . grffile = file ;
2008-04-29 23:31:29 +02:00
2009-03-08 17:51:08 +01:00
/* Reserve the engine slot */
2018-09-23 13:23:54 +02:00
assert ( _engine_mngr . size ( ) = = e - > index ) ;
2019-02-18 23:39:06 +01:00
_engine_mngr . push_back ( {
scope_grfid , // Note: this is INVALID_GRFID if dynamic_engines is disabled, so no reservation
internal_id ,
2019-04-21 23:25:54 +02:00
type ,
2021-01-08 11:16:18 +01:00
std : : min < uint8 > ( internal_id , _engine_counts [ type ] ) // substitute_id == _engine_counts[subtype] means "no substitute"
2019-02-18 23:39:06 +01:00
} ) ;
2009-03-08 17:51:08 +01:00
2009-05-17 01:44:36 +02:00
if ( engine_pool_size ! = Engine : : GetPoolSize ( ) ) {
2008-04-29 23:31:29 +02:00
/* Resize temporary engine data ... */
2023-04-20 21:38:48 +02:00
_gted . resize ( Engine : : GetPoolSize ( ) ) ;
2008-04-29 23:31:29 +02:00
}
2010-04-17 11:12:26 +02:00
if ( type = = VEH_TRAIN ) {
_gted [ e - > index ] . railtypelabel = GetRailTypeInfo ( e - > u . rail . railtype ) - > label ;
}
2008-04-29 23:31:29 +02:00
2023-04-18 21:24:42 +02:00
GrfMsg ( 5 , " Created new engine at index {} for GRFID {:x}, type {}, index {} " , e - > index , BSWAP32 ( file - > grfid ) , type , internal_id ) ;
2008-04-29 23:31:29 +02:00
return e ;
}
2011-09-11 13:14:22 +02:00
/**
* Return the ID of a new engine
* @ param file The NewGRF file providing the engine .
* @ param type The Vehicle type .
* @ param internal_id NewGRF - internal ID of the engine .
* @ return The new EngineID .
* @ note depending on the dynamic_engine setting and a possible override
* property the grfID may be unique or overwriting or partially re - defining
* properties of an existing engine .
*/
2008-04-30 19:42:08 +02:00
EngineID GetNewEngineID ( const GRFFile * file , VehicleType type , uint16 internal_id )
{
2009-03-08 17:51:08 +01:00
uint32 scope_grfid = INVALID_GRFID ; // If not using dynamic_engines, all newgrfs share their ID range
2008-05-29 17:13:28 +02:00
if ( _settings_game . vehicle . dynamic_engines ) {
2009-03-08 17:51:08 +01:00
scope_grfid = file - > grfid ;
2008-04-30 19:42:08 +02:00
uint32 override = _grf_id_overrides [ file - > grfid ] ;
2009-03-08 17:51:08 +01:00
if ( override ! = 0 ) scope_grfid = override ;
2008-04-30 19:42:08 +02:00
}
2009-03-08 17:51:08 +01:00
return _engine_mngr . GetID ( type , internal_id , scope_grfid ) ;
2008-04-30 19:42:08 +02:00
}
2010-08-01 21:22:34 +02:00
/**
* Map the colour modifiers of TTDPatch to those that Open is using .
2011-09-11 13:14:22 +02:00
* @ param grf_sprite Pointer to the structure been modified .
2008-02-16 04:01:05 +01:00
*/
static void MapSpriteMappingRecolour ( PalSpriteID * grf_sprite )
{
if ( HasBit ( grf_sprite - > pal , 14 ) ) {
ClrBit ( grf_sprite - > pal , 14 ) ;
SetBit ( grf_sprite - > sprite , SPRITE_MODIFIER_OPAQUE ) ;
}
if ( HasBit ( grf_sprite - > sprite , 14 ) ) {
ClrBit ( grf_sprite - > sprite , 14 ) ;
SetBit ( grf_sprite - > sprite , PALETTE_MODIFIER_TRANSPARENT ) ;
}
if ( HasBit ( grf_sprite - > sprite , 15 ) ) {
ClrBit ( grf_sprite - > sprite , 15 ) ;
2009-02-09 03:57:15 +01:00
SetBit ( grf_sprite - > sprite , PALETTE_MODIFIER_COLOUR ) ;
2008-02-16 04:01:05 +01:00
}
}
2007-03-19 12:27:30 +01:00
2011-05-18 20:31:23 +02:00
/**
* Read a sprite and a palette from the GRF and convert them into a format
* suitable to OpenTTD .
* @ param buf Input stream .
2011-05-29 18:56:22 +02:00
* @ param read_flags Whether to read TileLayoutFlags .
2011-05-18 20:31:23 +02:00
* @ param invert_action1_flag Set to true , if palette bit 15 means ' not from action 1 ' .
2011-09-11 17:08:17 +02:00
* @ param use_cur_spritesets Whether to use currently referenceable action 1 sets .
* @ param feature GrfSpecFeature to use spritesets from .
2018-10-28 03:17:36 +01:00
* @ param [ out ] grf_sprite Read sprite and palette .
* @ param [ out ] max_sprite_offset Optionally returns the number of sprites in the spriteset of the sprite . ( 0 if no spritset )
* @ param [ out ] max_palette_offset Optionally returns the number of sprites in the spriteset of the palette . ( 0 if no spritset )
2011-09-11 13:14:22 +02:00
* @ return Read TileLayoutFlags .
2011-05-18 20:31:23 +02:00
*/
2019-04-10 23:07:06 +02:00
static TileLayoutFlags ReadSpriteLayoutSprite ( ByteReader * buf , bool read_flags , bool invert_action1_flag , bool use_cur_spritesets , int feature , PalSpriteID * grf_sprite , uint16 * max_sprite_offset = nullptr , uint16 * max_palette_offset = nullptr )
2011-05-18 20:31:23 +02:00
{
grf_sprite - > sprite = buf - > ReadWord ( ) ;
grf_sprite - > pal = buf - > ReadWord ( ) ;
2011-05-29 18:56:22 +02:00
TileLayoutFlags flags = read_flags ? ( TileLayoutFlags ) buf - > ReadWord ( ) : TLF_NOTHING ;
2011-05-18 20:31:23 +02:00
MapSpriteMappingRecolour ( grf_sprite ) ;
bool custom_sprite = HasBit ( grf_sprite - > pal , 15 ) ! = invert_action1_flag ;
ClrBit ( grf_sprite - > pal , 15 ) ;
if ( custom_sprite ) {
/* Use sprite from Action 1 */
uint index = GB ( grf_sprite - > sprite , 0 , 14 ) ;
2011-09-11 17:08:17 +02:00
if ( use_cur_spritesets & & ( ! _cur . IsValidSpriteSet ( feature , index ) | | _cur . GetNumEnts ( feature , index ) = = 0 ) ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " ReadSpriteLayoutSprite: Spritelayout uses undefined custom spriteset {} " , index ) ;
2011-05-18 20:31:23 +02:00
grf_sprite - > sprite = SPR_IMG_QUERY ;
grf_sprite - > pal = PAL_NONE ;
} else {
2011-09-11 17:08:17 +02:00
SpriteID sprite = use_cur_spritesets ? _cur . GetSprite ( feature , index ) : index ;
2019-04-10 23:07:06 +02:00
if ( max_sprite_offset ! = nullptr ) * max_sprite_offset = use_cur_spritesets ? _cur . GetNumEnts ( feature , index ) : UINT16_MAX ;
2011-05-18 20:31:23 +02:00
SB ( grf_sprite - > sprite , 0 , SPRITE_WIDTH , sprite ) ;
SetBit ( grf_sprite - > sprite , SPRITE_MODIFIER_CUSTOM_SPRITE ) ;
}
2011-05-29 18:56:22 +02:00
} else if ( ( flags & TLF_SPRITE_VAR10 ) & & ! ( flags & TLF_SPRITE_REG_FLAGS ) ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " ReadSpriteLayoutSprite: Spritelayout specifies var10 value for non-action-1 sprite " ) ;
2011-05-29 18:56:22 +02:00
DisableGrf ( STR_NEWGRF_ERROR_INVALID_SPRITE_LAYOUT ) ;
return flags ;
2011-05-18 20:31:23 +02:00
}
2011-05-29 18:56:22 +02:00
if ( flags & TLF_CUSTOM_PALETTE ) {
/* Use palette from Action 1 */
uint index = GB ( grf_sprite - > pal , 0 , 14 ) ;
2011-09-11 17:08:17 +02:00
if ( use_cur_spritesets & & ( ! _cur . IsValidSpriteSet ( feature , index ) | | _cur . GetNumEnts ( feature , index ) = = 0 ) ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " ReadSpriteLayoutSprite: Spritelayout uses undefined custom spriteset {} for 'palette' " , index ) ;
2011-05-29 18:56:22 +02:00
grf_sprite - > pal = PAL_NONE ;
} else {
2011-09-11 17:08:17 +02:00
SpriteID sprite = use_cur_spritesets ? _cur . GetSprite ( feature , index ) : index ;
2019-04-10 23:07:06 +02:00
if ( max_palette_offset ! = nullptr ) * max_palette_offset = use_cur_spritesets ? _cur . GetNumEnts ( feature , index ) : UINT16_MAX ;
2011-05-29 18:56:22 +02:00
SB ( grf_sprite - > pal , 0 , SPRITE_WIDTH , sprite ) ;
SetBit ( grf_sprite - > pal , SPRITE_MODIFIER_CUSTOM_SPRITE ) ;
}
} else if ( ( flags & TLF_PALETTE_VAR10 ) & & ! ( flags & TLF_PALETTE_REG_FLAGS ) ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " ReadSpriteLayoutRegisters: Spritelayout specifies var10 value for non-action-1 palette " ) ;
2011-05-29 18:56:22 +02:00
DisableGrf ( STR_NEWGRF_ERROR_INVALID_SPRITE_LAYOUT ) ;
return flags ;
}
return flags ;
}
/**
* Preprocess the TileLayoutFlags and read register modifiers from the GRF .
* @ param buf Input stream .
* @ param flags TileLayoutFlags to process .
* @ param is_parent Whether the sprite is a parentsprite with a bounding box .
* @ param dts Sprite layout to insert data into .
* @ param index Sprite index to process ; 0 for ground sprite .
*/
static void ReadSpriteLayoutRegisters ( ByteReader * buf , TileLayoutFlags flags , bool is_parent , NewGRFSpriteLayout * dts , uint index )
{
if ( ! ( flags & TLF_DRAWING_FLAGS ) ) return ;
2019-04-10 23:07:06 +02:00
if ( dts - > registers = = nullptr ) dts - > AllocateRegisters ( ) ;
2011-05-29 18:56:22 +02:00
TileLayoutRegisters & regs = const_cast < TileLayoutRegisters & > ( dts - > registers [ index ] ) ;
regs . flags = flags & TLF_DRAWING_FLAGS ;
if ( flags & TLF_DODRAW ) regs . dodraw = buf - > ReadByte ( ) ;
if ( flags & TLF_SPRITE ) regs . sprite = buf - > ReadByte ( ) ;
if ( flags & TLF_PALETTE ) regs . palette = buf - > ReadByte ( ) ;
if ( is_parent ) {
if ( flags & TLF_BB_XY_OFFSET ) {
regs . delta . parent [ 0 ] = buf - > ReadByte ( ) ;
regs . delta . parent [ 1 ] = buf - > ReadByte ( ) ;
}
if ( flags & TLF_BB_Z_OFFSET ) regs . delta . parent [ 2 ] = buf - > ReadByte ( ) ;
} else {
if ( flags & TLF_CHILD_X_OFFSET ) regs . delta . child [ 0 ] = buf - > ReadByte ( ) ;
if ( flags & TLF_CHILD_Y_OFFSET ) regs . delta . child [ 1 ] = buf - > ReadByte ( ) ;
}
if ( flags & TLF_SPRITE_VAR10 ) {
regs . sprite_var10 = buf - > ReadByte ( ) ;
if ( regs . sprite_var10 > TLR_MAX_VAR10 ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " ReadSpriteLayoutRegisters: Spritelayout specifies var10 ({}) exceeding the maximal allowed value {} " , regs . sprite_var10 , TLR_MAX_VAR10 ) ;
2011-05-29 18:56:22 +02:00
DisableGrf ( STR_NEWGRF_ERROR_INVALID_SPRITE_LAYOUT ) ;
return ;
}
}
if ( flags & TLF_PALETTE_VAR10 ) {
regs . palette_var10 = buf - > ReadByte ( ) ;
if ( regs . palette_var10 > TLR_MAX_VAR10 ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " ReadSpriteLayoutRegisters: Spritelayout specifies var10 ({}) exceeding the maximal allowed value {} " , regs . palette_var10 , TLR_MAX_VAR10 ) ;
2011-05-29 18:56:22 +02:00
DisableGrf ( STR_NEWGRF_ERROR_INVALID_SPRITE_LAYOUT ) ;
return ;
}
}
}
/**
* Read a spritelayout from the GRF .
* @ param buf Input
* @ param num_building_sprites Number of building sprites to read
2011-09-11 17:08:17 +02:00
* @ param use_cur_spritesets Whether to use currently referenceable action 1 sets .
* @ param feature GrfSpecFeature to use spritesets from .
2019-09-29 22:27:32 +02:00
* @ param allow_var10 Whether the spritelayout may specify var10 values for resolving multiple action - 1 - 2 - 3 chains
2011-05-29 18:56:22 +02:00
* @ param no_z_position Whether bounding boxes have no Z offset
* @ param dts Layout container to output into
2011-09-11 13:14:22 +02:00
* @ return True on error ( GRF was disabled ) .
2011-05-29 18:56:22 +02:00
*/
2011-09-11 17:08:17 +02:00
static bool ReadSpriteLayout ( ByteReader * buf , uint num_building_sprites , bool use_cur_spritesets , byte feature , bool allow_var10 , bool no_z_position , NewGRFSpriteLayout * dts )
2011-05-29 18:56:22 +02:00
{
bool has_flags = HasBit ( num_building_sprites , 6 ) ;
ClrBit ( num_building_sprites , 6 ) ;
TileLayoutFlags valid_flags = TLF_KNOWN_FLAGS ;
if ( ! allow_var10 ) valid_flags & = ~ TLF_VAR10_FLAGS ;
dts - > Allocate ( num_building_sprites ) ; // allocate before reading groundsprite flags
2021-05-01 22:06:17 +02:00
std : : vector < uint16 > max_sprite_offset ( num_building_sprites + 1 , 0 ) ;
std : : vector < uint16 > max_palette_offset ( num_building_sprites + 1 , 0 ) ;
2011-09-11 17:10:09 +02:00
2011-05-29 18:56:22 +02:00
/* Groundsprite */
2021-05-01 22:06:17 +02:00
TileLayoutFlags flags = ReadSpriteLayoutSprite ( buf , has_flags , false , use_cur_spritesets , feature , & dts - > ground , max_sprite_offset . data ( ) , max_palette_offset . data ( ) ) ;
2011-08-21 14:05:47 +02:00
if ( _cur . skip_sprites < 0 ) return true ;
2011-05-29 18:56:22 +02:00
if ( flags & ~ ( valid_flags & ~ TLF_NON_GROUND_FLAGS ) ) {
2023-04-20 17:00:46 +02:00
GrfMsg ( 1 , " ReadSpriteLayout: Spritelayout uses invalid flag 0x{:X} for ground sprite " , flags & ~ ( valid_flags & ~ TLF_NON_GROUND_FLAGS ) ) ;
2011-05-29 18:56:22 +02:00
DisableGrf ( STR_NEWGRF_ERROR_INVALID_SPRITE_LAYOUT ) ;
return true ;
}
ReadSpriteLayoutRegisters ( buf , flags , false , dts , 0 ) ;
2011-08-21 14:05:47 +02:00
if ( _cur . skip_sprites < 0 ) return true ;
2011-05-29 18:56:22 +02:00
for ( uint i = 0 ; i < num_building_sprites ; i + + ) {
DrawTileSeqStruct * seq = const_cast < DrawTileSeqStruct * > ( & dts - > seq [ i ] ) ;
2021-05-01 22:06:17 +02:00
flags = ReadSpriteLayoutSprite ( buf , has_flags , false , use_cur_spritesets , feature , & seq - > image , max_sprite_offset . data ( ) + i + 1 , max_palette_offset . data ( ) + i + 1 ) ;
2011-08-21 14:05:47 +02:00
if ( _cur . skip_sprites < 0 ) return true ;
2011-05-29 18:56:22 +02:00
if ( flags & ~ valid_flags ) {
2023-04-20 17:00:46 +02:00
GrfMsg ( 1 , " ReadSpriteLayout: Spritelayout uses unknown flag 0x{:X} " , flags & ~ valid_flags ) ;
2011-05-29 18:56:22 +02:00
DisableGrf ( STR_NEWGRF_ERROR_INVALID_SPRITE_LAYOUT ) ;
return true ;
}
seq - > delta_x = buf - > ReadByte ( ) ;
seq - > delta_y = buf - > ReadByte ( ) ;
if ( ! no_z_position ) seq - > delta_z = buf - > ReadByte ( ) ;
if ( seq - > IsParentSprite ( ) ) {
seq - > size_x = buf - > ReadByte ( ) ;
seq - > size_y = buf - > ReadByte ( ) ;
seq - > size_z = buf - > ReadByte ( ) ;
}
ReadSpriteLayoutRegisters ( buf , flags , seq - > IsParentSprite ( ) , dts , i + 1 ) ;
2011-08-21 14:05:47 +02:00
if ( _cur . skip_sprites < 0 ) return true ;
2011-05-29 18:56:22 +02:00
}
2011-09-11 17:10:09 +02:00
/* Check if the number of sprites per spriteset is consistent */
bool is_consistent = true ;
dts - > consistent_max_offset = 0 ;
for ( uint i = 0 ; i < num_building_sprites + 1 ; i + + ) {
if ( max_sprite_offset [ i ] > 0 ) {
if ( dts - > consistent_max_offset = = 0 ) {
dts - > consistent_max_offset = max_sprite_offset [ i ] ;
} else if ( dts - > consistent_max_offset ! = max_sprite_offset [ i ] ) {
is_consistent = false ;
break ;
}
}
if ( max_palette_offset [ i ] > 0 ) {
if ( dts - > consistent_max_offset = = 0 ) {
dts - > consistent_max_offset = max_palette_offset [ i ] ;
} else if ( dts - > consistent_max_offset ! = max_palette_offset [ i ] ) {
is_consistent = false ;
break ;
}
}
}
/* When the Action1 sets are unknown, everything should be 0 (no spriteset usage) or UINT16_MAX (some spriteset usage) */
assert ( use_cur_spritesets | | ( is_consistent & & ( dts - > consistent_max_offset = = 0 | | dts - > consistent_max_offset = = UINT16_MAX ) ) ) ;
2019-04-10 23:07:06 +02:00
if ( ! is_consistent | | dts - > registers ! = nullptr ) {
2011-09-11 17:10:09 +02:00
dts - > consistent_max_offset = 0 ;
2019-04-10 23:07:06 +02:00
if ( dts - > registers = = nullptr ) dts - > AllocateRegisters ( ) ;
2011-09-11 17:10:09 +02:00
for ( uint i = 0 ; i < num_building_sprites + 1 ; i + + ) {
TileLayoutRegisters & regs = const_cast < TileLayoutRegisters & > ( dts - > registers [ i ] ) ;
regs . max_sprite_offset = max_sprite_offset [ i ] ;
regs . max_palette_offset = max_palette_offset [ i ] ;
}
}
2011-05-29 18:56:22 +02:00
return false ;
2011-05-18 20:31:23 +02:00
}
2012-02-07 23:48:09 +01:00
/**
2018-05-21 23:08:39 +02:00
* Translate the refit mask . refit_mask is uint32 as it has not been mapped to CargoTypes .
2012-02-07 23:48:09 +01:00
*/
2018-05-21 23:08:39 +02:00
static CargoTypes TranslateRefitMask ( uint32 refit_mask )
2012-02-07 23:48:09 +01:00
{
2018-05-21 23:08:39 +02:00
CargoTypes result = 0 ;
2021-06-13 04:18:21 +02:00
for ( uint8 bit : SetBitIterator ( refit_mask ) ) {
2012-02-07 23:48:09 +01:00
CargoID cargo = GetCargoTranslation ( bit , _cur . grffile , true ) ;
2023-05-04 12:29:21 +02:00
if ( IsValidCargoID ( cargo ) ) SetBit ( result , cargo ) ;
2012-02-07 23:48:09 +01:00
}
return result ;
}
2009-11-07 23:37:22 +01:00
/**
2009-11-07 23:47:54 +01:00
* Converts TTD ( P ) Base Price pointers into the enum used by OTTD
2009-11-07 23:37:22 +01:00
* See http : //wiki.ttdpatch.net/tiki-index.php?page=BaseCosts
* @ param base_pointer TTD ( P ) Base Price Pointer
* @ param error_location Function name for grf error messages
2012-07-10 20:37:54 +02:00
* @ param [ out ] index If \ a base_pointer is valid , \ a index is assigned to the matching price ; else it is left unchanged
2009-11-07 23:37:22 +01:00
*/
2009-11-07 23:47:54 +01:00
static void ConvertTTDBasePrice ( uint32 base_pointer , const char * error_location , Price * index )
2009-11-07 23:37:22 +01:00
{
/* Special value for 'none' */
if ( base_pointer = = 0 ) {
2009-11-07 23:47:54 +01:00
* index = INVALID_PRICE ;
2009-11-07 23:37:22 +01:00
return ;
}
static const uint32 start = 0x4B34 ; ///< Position of first base price
static const uint32 size = 6 ; ///< Size of each base price record
2009-11-08 13:23:02 +01:00
if ( base_pointer < start | | ( base_pointer - start ) % size ! = 0 | | ( base_pointer - start ) / size > = PR_END ) {
2023-04-20 17:00:46 +02:00
GrfMsg ( 1 , " {}: Unsupported running cost base 0x{:04X}, ignoring " , error_location , base_pointer ) ;
2009-11-07 23:37:22 +01:00
return ;
}
2009-11-07 23:47:54 +01:00
* index = ( Price ) ( ( base_pointer - start ) / size ) ;
2009-11-07 23:37:22 +01:00
}
2011-09-11 13:14:22 +02:00
/** Possible return values for the FeatureChangeInfo functions */
2008-08-31 18:41:27 +02:00
enum ChangeInfoResult {
CIR_SUCCESS , ///< Variable was parsed and read
2011-05-29 18:56:22 +02:00
CIR_DISABLED , ///< GRF was disabled due to error
2008-08-31 18:41:27 +02:00
CIR_UNHANDLED , ///< Variable was parsed but unread
CIR_UNKNOWN , ///< Variable is unknown
CIR_INVALID_ID , ///< Attempt to modify an invalid ID
} ;
2010-01-22 14:02:15 +01:00
typedef ChangeInfoResult ( * VCI_Handler ) ( uint engine , int numinfo , int prop , ByteReader * buf ) ;
2004-08-09 19:04:08 +02:00
2011-09-11 13:14:22 +02:00
/**
* Define properties common to all vehicles
* @ param ei Engine info .
* @ param prop The property to change .
* @ param buf The property value .
* @ return ChangeInfoResult .
*/
2010-01-23 14:17:30 +01:00
static ChangeInfoResult CommonVehicleChangeInfo ( EngineInfo * ei , int prop , ByteReader * buf )
2008-08-31 11:52:47 +02:00
{
switch ( prop ) {
case 0x00 : // Introduction date
2010-01-23 14:17:30 +01:00
ei - > base_intro = buf - > ReadWord ( ) + DAYS_TILL_ORIGINAL_BASE_YEAR ;
2008-08-31 11:52:47 +02:00
break ;
case 0x02 : // Decay speed
2010-01-23 14:17:30 +01:00
ei - > decay_speed = buf - > ReadByte ( ) ;
2008-08-31 11:52:47 +02:00
break ;
case 0x03 : // Vehicle life
2010-01-23 14:17:30 +01:00
ei - > lifelength = buf - > ReadByte ( ) ;
2008-08-31 11:52:47 +02:00
break ;
case 0x04 : // Model life
2010-01-23 14:17:30 +01:00
ei - > base_life = buf - > ReadByte ( ) ;
2008-08-31 11:52:47 +02:00
break ;
case 0x06 : // Climates available
2010-01-23 14:17:30 +01:00
ei - > climates = buf - > ReadByte ( ) ;
2008-08-31 11:52:47 +02:00
break ;
2011-11-08 18:28:05 +01:00
case PROP_VEHICLE_LOAD_AMOUNT : // 0x07 Loading speed
2008-08-31 11:52:47 +02:00
/* Amount of cargo loaded during a vehicle's "loading tick" */
2010-01-23 14:17:30 +01:00
ei - > load_amount = buf - > ReadByte ( ) ;
2008-08-31 11:52:47 +02:00
break ;
default :
2008-08-31 18:41:27 +02:00
return CIR_UNKNOWN ;
2008-08-31 11:52:47 +02:00
}
2008-08-31 18:41:27 +02:00
return CIR_SUCCESS ;
2008-08-31 11:52:47 +02:00
}
2011-09-11 13:14:22 +02:00
/**
* Define properties for rail vehicles
* @ param engine : ocal ID of the first vehicle .
* @ param numinfo Number of subsequent IDs to change the property for .
* @ param prop The property to change .
* @ param buf The property value .
* @ return ChangeInfoResult .
*/
2010-01-22 14:02:15 +01:00
static ChangeInfoResult RailVehicleChangeInfo ( uint engine , int numinfo , int prop , ByteReader * buf )
2004-08-09 19:04:08 +02:00
{
2008-08-31 18:41:27 +02:00
ChangeInfoResult ret = CIR_SUCCESS ;
2004-08-09 19:04:08 +02:00
2007-07-04 14:52:43 +02:00
for ( int i = 0 ; i < numinfo ; i + + ) {
2011-08-21 14:05:47 +02:00
Engine * e = GetNewEngine ( _cur . grffile , VEH_TRAIN , engine + i ) ;
2019-04-10 23:07:06 +02:00
if ( e = = nullptr ) return CIR_INVALID_ID ; // No engine could be allocated, so neither can any next vehicles
2011-02-09 19:46:55 +01:00
2008-04-29 23:31:29 +02:00
EngineInfo * ei = & e - > info ;
RailVehicleInfo * rvi = & e - > u . rail ;
2007-07-04 14:52:43 +02:00
switch ( prop ) {
case 0x05 : { // Track type
2010-01-23 14:17:30 +01:00
uint8 tracktype = buf - > ReadByte ( ) ;
2004-08-09 19:04:08 +02:00
2018-09-23 13:23:54 +02:00
if ( tracktype < _cur . grffile - > railtype_list . size ( ) ) {
2011-08-21 14:05:47 +02:00
_gted [ e - > index ] . railtypelabel = _cur . grffile - > railtype_list [ tracktype ] ;
2009-02-08 19:11:06 +01:00
break ;
}
2006-02-15 13:53:36 +01:00
switch ( tracktype ) {
2010-03-23 12:05:28 +01:00
case 0 : _gted [ e - > index ] . railtypelabel = rvi - > engclass > = 2 ? RAILTYPE_ELECTRIC_LABEL : RAILTYPE_RAIL_LABEL ; break ;
case 1 : _gted [ e - > index ] . railtypelabel = RAILTYPE_MONO_LABEL ; break ;
case 2 : _gted [ e - > index ] . railtypelabel = RAILTYPE_MAGLEV_LABEL ; break ;
2006-02-15 13:53:36 +01:00
default :
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " RailVehicleChangeInfo: Invalid track type {} specified, ignoring " , tracktype ) ;
2006-02-15 13:53:36 +01:00
break ;
}
2010-08-01 20:53:30 +02:00
break ;
}
2006-02-15 13:53:36 +01:00
2007-07-04 14:52:43 +02:00
case 0x08 : // AI passenger service
2008-01-30 11:27:45 +01:00
/* Tells the AI that this engine is designed for
2007-07-04 15:30:33 +02:00
* passenger services and shouldn ' t be used for freight . */
2010-01-23 14:17:30 +01:00
rvi - > ai_passenger_only = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2006-02-27 16:35:22 +01:00
2009-09-22 21:28:57 +02:00
case PROP_TRAIN_SPEED : { // 0x09 Speed (1 unit is 1 km-ish/h)
2010-01-23 14:17:30 +01:00
uint16 speed = buf - > ReadWord ( ) ;
2006-02-18 15:41:24 +01:00
if ( speed = = 0xFFFF ) speed = 0 ;
2004-08-09 19:04:08 +02:00
2007-07-04 14:52:43 +02:00
rvi - > max_speed = speed ;
2010-08-01 20:53:30 +02:00
break ;
}
2006-02-27 16:35:22 +01:00
2009-09-22 21:28:57 +02:00
case PROP_TRAIN_POWER : // 0x0B Power
2010-01-23 14:17:30 +01:00
rvi - > power = buf - > ReadWord ( ) ;
2008-06-05 14:08:20 +02:00
/* Set engine / wagon state based on power */
if ( rvi - > power ! = 0 ) {
if ( rvi - > railveh_type = = RAILVEH_WAGON ) {
rvi - > railveh_type = RAILVEH_SINGLEHEAD ;
}
} else {
rvi - > railveh_type = RAILVEH_WAGON ;
}
2008-05-13 22:39:57 +02:00
break ;
2006-01-07 14:20:02 +01:00
2009-09-22 21:28:57 +02:00
case PROP_TRAIN_RUNNING_COST_FACTOR : // 0x0D Running cost factor
2010-01-23 14:17:30 +01:00
rvi - > running_cost = buf - > ReadByte ( ) ;
2008-05-13 22:39:57 +02:00
break ;
2006-02-27 16:35:22 +01:00
2009-11-07 23:37:22 +01:00
case 0x0E : // Running cost base
2010-01-23 14:17:30 +01:00
ConvertTTDBasePrice ( buf - > ReadDWord ( ) , " RailVehicleChangeInfo " , & rvi - > running_cost_class ) ;
2009-11-07 23:37:22 +01:00
break ;
2006-02-27 16:35:22 +01:00
2007-07-04 14:52:43 +02:00
case 0x12 : { // Sprite ID
2010-01-23 14:17:30 +01:00
uint8 spriteid = buf - > ReadByte ( ) ;
2013-11-26 17:08:58 +01:00
uint8 orig_spriteid = spriteid ;
2004-08-09 19:04:08 +02:00
2006-01-27 08:13:58 +01:00
/* TTD sprite IDs point to a location in a 16bit array, but we use it
* as an array index , so we need it to be half the original value . */
2006-02-18 15:41:24 +01:00
if ( spriteid < 0xFD ) spriteid > > = 1 ;
2006-01-27 08:13:58 +01:00
2013-11-26 17:08:58 +01:00
if ( IsValidNewGRFImageIndex < VEH_TRAIN > ( spriteid ) ) {
rvi - > image_index = spriteid ;
} else {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " RailVehicleChangeInfo: Invalid Sprite {} specified, ignoring " , orig_spriteid ) ;
2013-11-26 17:08:58 +01:00
rvi - > image_index = 0 ;
}
2010-08-01 20:53:30 +02:00
break ;
}
2006-02-27 16:35:22 +01:00
2007-07-04 14:52:43 +02:00
case 0x13 : { // Dual-headed
2010-01-23 14:17:30 +01:00
uint8 dual = buf - > ReadByte ( ) ;
2004-08-09 19:04:08 +02:00
2004-11-13 13:21:32 +01:00
if ( dual ! = 0 ) {
2007-07-04 14:52:43 +02:00
rvi - > railveh_type = RAILVEH_MULTIHEAD ;
2004-08-09 19:04:08 +02:00
} else {
2007-07-04 14:52:43 +02:00
rvi - > railveh_type = rvi - > power = = 0 ?
2007-01-30 23:00:25 +01:00
RAILVEH_WAGON : RAILVEH_SINGLEHEAD ;
2004-08-09 19:04:08 +02:00
}
2010-08-01 20:53:30 +02:00
break ;
}
2004-08-09 19:04:08 +02:00
2009-09-22 21:28:57 +02:00
case PROP_TRAIN_CARGO_CAPACITY : // 0x14 Cargo capacity
2010-01-23 14:17:30 +01:00
rvi - > capacity = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2006-02-27 16:35:22 +01:00
2007-07-04 14:52:43 +02:00
case 0x15 : { // Cargo type
2012-02-07 23:59:30 +01:00
_gted [ e - > index ] . defaultcargo_grf = _cur . grffile ;
2010-01-23 14:17:30 +01:00
uint8 ctype = buf - > ReadByte ( ) ;
2004-08-09 19:04:08 +02:00
2011-11-08 18:25:42 +01:00
if ( ctype = = 0xFF ) {
2008-08-30 10:59:55 +02:00
/* 0xFF is specified as 'use first refittable' */
2009-08-17 20:33:48 +02:00
ei - > cargo_type = CT_INVALID ;
2011-11-08 18:25:42 +01:00
} else if ( _cur . grffile - > grf_version > = 8 ) {
/* Use translated cargo. Might result in CT_INVALID (first refittable), if cargo is not defined. */
ei - > cargo_type = GetCargoTranslation ( ctype , _cur . grffile ) ;
2012-07-27 21:16:01 +02:00
} else if ( ctype < NUM_CARGO ) {
2011-11-08 18:25:42 +01:00
/* Use untranslated cargo. */
ei - > cargo_type = ctype ;
2006-01-12 08:57:40 +01:00
} else {
2009-08-17 20:33:48 +02:00
ei - > cargo_type = CT_INVALID ;
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " RailVehicleChangeInfo: Invalid cargo type {}, using first refittable " , ctype ) ;
2006-01-12 08:57:40 +01:00
}
2010-08-01 20:53:30 +02:00
break ;
}
2007-07-04 14:52:43 +02:00
2009-09-22 21:28:57 +02:00
case PROP_TRAIN_WEIGHT : // 0x16 Weight
2010-01-23 14:17:30 +01:00
SB ( rvi - > weight , 0 , 8 , buf - > ReadByte ( ) ) ;
2007-07-04 14:52:43 +02:00
break ;
2009-09-22 21:28:57 +02:00
case PROP_TRAIN_COST_FACTOR : // 0x17 Cost factor
2010-01-23 14:17:30 +01:00
rvi - > cost_factor = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
case 0x18 : // AI rank
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " RailVehicleChangeInfo: Property 0x18 'AI rank' not used by NoAI, ignored. " ) ;
2010-01-23 14:17:30 +01:00
buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
case 0x19 : { // Engine traction type
/* What do the individual numbers mean?
* 0x00 . . 0x07 : Steam
* 0x08 . . 0x27 : Diesel
* 0x28 . . 0x31 : Electric
* 0x32 . . 0x37 : Monorail
* 0x38 . . 0x41 : Maglev
*/
2010-01-23 14:17:30 +01:00
uint8 traction = buf - > ReadByte ( ) ;
2007-05-06 17:01:44 +02:00
EngineClass engclass ;
2004-08-09 19:04:08 +02:00
2006-02-18 15:41:24 +01:00
if ( traction < = 0x07 ) {
2007-05-06 17:01:44 +02:00
engclass = EC_STEAM ;
2006-02-18 15:41:24 +01:00
} else if ( traction < = 0x27 ) {
2007-05-06 17:01:44 +02:00
engclass = EC_DIESEL ;
2006-03-29 18:30:26 +02:00
} else if ( traction < = 0x31 ) {
2007-05-06 17:01:44 +02:00
engclass = EC_ELECTRIC ;
2007-05-06 21:44:22 +02:00
} else if ( traction < = 0x37 ) {
engclass = EC_MONORAIL ;
2006-02-18 15:41:24 +01:00
} else if ( traction < = 0x41 ) {
2007-05-06 21:44:22 +02:00
engclass = EC_MAGLEV ;
2006-02-18 15:41:24 +01:00
} else {
2004-08-09 19:04:08 +02:00
break ;
2006-02-18 15:41:24 +01:00
}
2009-02-08 19:11:06 +01:00
2018-09-23 13:23:54 +02:00
if ( _cur . grffile - > railtype_list . size ( ) = = 0 ) {
2009-02-08 19:11:06 +01:00
/* Use traction type to select between normal and electrified
* rail only when no translation list is in place . */
2010-04-04 08:03:52 +02:00
if ( _gted [ e - > index ] . railtypelabel = = RAILTYPE_RAIL_LABEL & & engclass > = EC_ELECTRIC ) _gted [ e - > index ] . railtypelabel = RAILTYPE_ELECTRIC_LABEL ;
if ( _gted [ e - > index ] . railtypelabel = = RAILTYPE_ELECTRIC_LABEL & & engclass < EC_ELECTRIC ) _gted [ e - > index ] . railtypelabel = RAILTYPE_RAIL_LABEL ;
2009-02-08 19:11:06 +01:00
}
2004-08-09 19:04:08 +02:00
2007-07-04 14:52:43 +02:00
rvi - > engclass = engclass ;
2010-08-01 20:53:30 +02:00
break ;
}
2006-02-27 16:35:22 +01:00
2008-04-29 23:31:29 +02:00
case 0x1A : // Alter purchase list sort order
2010-03-10 08:31:08 +01:00
AlterVehicleListOrder ( e - > index , buf - > ReadExtendedByte ( ) ) ;
2008-04-29 23:31:29 +02:00
break ;
2006-02-04 22:48:46 +01:00
2007-07-04 14:52:43 +02:00
case 0x1B : // Powered wagons power bonus
2010-01-23 14:17:30 +01:00
rvi - > pow_wag_power = buf - > ReadWord ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2005-06-06 02:19:24 +02:00
2007-07-04 14:52:43 +02:00
case 0x1C : // Refit cost
2010-01-23 14:17:30 +01:00
ei - > refit_cost = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2006-06-09 18:35:07 +02:00
2012-02-07 23:48:09 +01:00
case 0x1D : { // Refit cargo
uint32 mask = buf - > ReadDWord ( ) ;
_gted [ e - > index ] . UpdateRefittability ( mask ! = 0 ) ;
ei - > refit_mask = TranslateRefitMask ( mask ) ;
2012-02-07 23:59:30 +01:00
_gted [ e - > index ] . defaultcargo_grf = _cur . grffile ;
2007-07-04 14:52:43 +02:00
break ;
2012-02-07 23:48:09 +01:00
}
2004-11-17 02:09:15 +01:00
2007-07-04 14:52:43 +02:00
case 0x1E : // Callback
2023-01-22 18:56:36 +01:00
SB ( ei - > callback_mask , 0 , 8 , buf - > ReadByte ( ) ) ;
2007-07-04 14:52:43 +02:00
break ;
2005-06-07 00:44:11 +02:00
2009-09-22 21:28:57 +02:00
case PROP_TRAIN_TRACTIVE_EFFORT : // 0x1F Tractive effort coefficient
2010-01-23 14:17:30 +01:00
rvi - > tractive_effort = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2006-12-28 14:18:07 +01:00
2007-10-26 01:24:28 +02:00
case 0x20 : // Air drag
2010-08-02 16:51:35 +02:00
rvi - > air_drag = buf - > ReadByte ( ) ;
2007-10-26 01:24:28 +02:00
break ;
2011-11-08 18:27:55 +01:00
case PROP_TRAIN_SHORTEN_FACTOR : // 0x21 Shorter vehicle
2010-01-23 14:17:30 +01:00
rvi - > shorten_factor = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2005-06-07 00:44:11 +02:00
2007-07-04 14:52:43 +02:00
case 0x22 : // Visual effect
2010-01-23 14:17:30 +01:00
rvi - > visual_effect = buf - > ReadByte ( ) ;
2010-11-18 15:20:03 +01:00
/* Avoid accidentally setting visual_effect to the default value
* Since bit 6 ( disable effects ) is set anyways , we can safely erase some bits . */
if ( rvi - > visual_effect = = VE_DEFAULT ) {
assert ( HasBit ( rvi - > visual_effect , VE_DISABLE_EFFECT ) ) ;
SB ( rvi - > visual_effect , VE_TYPE_START , VE_TYPE_COUNT , 0 ) ;
}
2007-07-04 14:52:43 +02:00
break ;
2005-06-06 02:19:24 +02:00
2007-07-04 14:52:43 +02:00
case 0x23 : // Powered wagons weight bonus
2010-01-23 14:17:30 +01:00
rvi - > pow_wag_weight = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2005-06-06 02:19:24 +02:00
2007-07-04 14:52:43 +02:00
case 0x24 : { // High byte of vehicle weight
2010-01-23 14:17:30 +01:00
byte weight = buf - > ReadByte ( ) ;
2005-10-20 17:15:58 +02:00
2005-10-20 21:44:41 +02:00
if ( weight > 4 ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " RailVehicleChangeInfo: Nonsensical weight of {} tons, ignoring " , weight < < 8 ) ;
2005-10-20 17:15:58 +02:00
} else {
2007-07-04 14:52:43 +02:00
SB ( rvi - > weight , 8 , 8 , weight ) ;
2005-10-20 17:15:58 +02:00
}
2010-08-01 20:53:30 +02:00
break ;
}
2006-02-27 16:35:22 +01:00
2009-09-22 21:28:57 +02:00
case PROP_TRAIN_USER_DATA : // 0x25 User-defined bit mask to set when checking veh. var. 42
2010-01-23 14:17:30 +01:00
rvi - > user_def_data = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
case 0x26 : // Retire vehicle early
2010-01-23 14:17:30 +01:00
ei - > retire_early = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
case 0x27 : // Miscellaneous flags
2010-01-23 14:17:30 +01:00
ei - > misc_flags = buf - > ReadByte ( ) ;
2007-11-19 22:02:30 +01:00
_loaded_newgrf_features . has_2CC | = HasBit ( ei - > misc_flags , EF_USES_2CC ) ;
2007-07-04 14:52:43 +02:00
break ;
case 0x28 : // Cargo classes allowed
2010-01-23 14:17:30 +01:00
_gted [ e - > index ] . cargo_allowed = buf - > ReadWord ( ) ;
2012-02-07 23:03:03 +01:00
_gted [ e - > index ] . UpdateRefittability ( _gted [ e - > index ] . cargo_allowed ! = 0 ) ;
2012-02-07 23:59:30 +01:00
_gted [ e - > index ] . defaultcargo_grf = _cur . grffile ;
2007-07-04 14:52:43 +02:00
break ;
case 0x29 : // Cargo classes disallowed
2010-01-23 14:17:30 +01:00
_gted [ e - > index ] . cargo_disallowed = buf - > ReadWord ( ) ;
2012-02-07 23:03:03 +01:00
_gted [ e - > index ] . UpdateRefittability ( false ) ;
2007-07-04 14:52:43 +02:00
break ;
case 0x2A : // Long format introduction date (days since year 0)
2010-01-23 14:17:30 +01:00
ei - > base_intro = buf - > ReadDWord ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2011-08-03 22:55:08 +02:00
case PROP_TRAIN_CARGO_AGE_PERIOD : // 0x2B Cargo aging period
ei - > cargo_age_period = buf - > ReadWord ( ) ;
break ;
2011-11-21 23:39:59 +01:00
case 0x2C : // CTT refit include list
case 0x2D : { // CTT refit exclude list
uint8 count = buf - > ReadByte ( ) ;
2012-02-07 23:03:03 +01:00
_gted [ e - > index ] . UpdateRefittability ( prop = = 0x2C & & count ! = 0 ) ;
2012-02-07 23:59:30 +01:00
if ( prop = = 0x2C ) _gted [ e - > index ] . defaultcargo_grf = _cur . grffile ;
2018-05-21 23:08:39 +02:00
CargoTypes & ctt = prop = = 0x2C ? _gted [ e - > index ] . ctt_include_mask : _gted [ e - > index ] . ctt_exclude_mask ;
2012-02-16 23:22:45 +01:00
ctt = 0 ;
2011-11-21 23:39:59 +01:00
while ( count - - ) {
CargoID ctype = GetCargoTranslation ( buf - > ReadByte ( ) , _cur . grffile ) ;
2023-05-04 12:29:21 +02:00
if ( IsValidCargoID ( ctype ) ) SetBit ( ctt , ctype ) ;
2011-11-21 23:39:59 +01:00
}
break ;
}
2021-08-15 11:17:05 +02:00
case PROP_TRAIN_CURVE_SPEED_MOD : // 0x2E Curve speed modifier
rvi - > curve_speed_mod = buf - > ReadWord ( ) ;
break ;
2019-02-27 08:00:33 +01:00
case 0x2F : // Engine variant
2023-01-13 20:22:31 +01:00
ei - > variant_id = buf - > ReadWord ( ) ;
2019-02-27 08:00:33 +01:00
break ;
2022-12-09 02:40:55 +01:00
case 0x30 : // Extra miscellaneous flags
ei - > extra_flags = static_cast < ExtraEngineFlags > ( buf - > ReadDWord ( ) ) ;
break ;
2023-01-22 18:56:36 +01:00
case 0x31 : // Callback additional mask
SB ( ei - > callback_mask , 8 , 8 , buf - > ReadByte ( ) ) ;
break ;
2007-07-04 14:52:43 +02:00
default :
2010-01-23 14:17:30 +01:00
ret = CommonVehicleChangeInfo ( ei , prop , buf ) ;
2007-07-04 14:52:43 +02:00
break ;
}
2004-08-09 19:04:08 +02:00
}
2007-07-04 14:52:43 +02:00
2004-08-09 19:04:08 +02:00
return ret ;
}
2011-09-11 13:14:22 +02:00
/**
* Define properties for road vehicles
* @ param engine Local ID of the first vehicle .
* @ param numinfo Number of subsequent IDs to change the property for .
* @ param prop The property to change .
* @ param buf The property value .
* @ return ChangeInfoResult .
*/
2010-01-22 14:02:15 +01:00
static ChangeInfoResult RoadVehicleChangeInfo ( uint engine , int numinfo , int prop , ByteReader * buf )
2004-11-13 19:01:33 +01:00
{
2008-08-31 18:41:27 +02:00
ChangeInfoResult ret = CIR_SUCCESS ;
2004-11-13 19:01:33 +01:00
2007-07-04 14:52:43 +02:00
for ( int i = 0 ; i < numinfo ; i + + ) {
2011-08-21 14:05:47 +02:00
Engine * e = GetNewEngine ( _cur . grffile , VEH_ROAD , engine + i ) ;
2019-04-10 23:07:06 +02:00
if ( e = = nullptr ) return CIR_INVALID_ID ; // No engine could be allocated, so neither can any next vehicles
2011-02-09 19:46:55 +01:00
2008-04-29 23:31:29 +02:00
EngineInfo * ei = & e - > info ;
RoadVehicleInfo * rvi = & e - > u . road ;
2004-11-13 19:01:33 +01:00
2007-07-04 14:52:43 +02:00
switch ( prop ) {
2019-04-06 08:46:15 +02:00
case 0x05 : // Road/tram type
/* RoadTypeLabel is looked up later after the engine's road/tram
* flag is set , however 0 means the value has not been set . */
_gted [ e - > index ] . roadtramtype = buf - > ReadByte ( ) + 1 ;
break ;
2007-07-04 14:52:43 +02:00
case 0x08 : // Speed (1 unit is 0.5 kmh)
2010-01-23 14:17:30 +01:00
rvi - > max_speed = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2004-11-13 19:01:33 +01:00
2009-11-08 14:02:05 +01:00
case PROP_ROADVEH_RUNNING_COST_FACTOR : // 0x09 Running cost factor
2010-01-23 14:17:30 +01:00
rvi - > running_cost = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2006-02-27 16:35:22 +01:00
2009-11-07 23:37:22 +01:00
case 0x0A : // Running cost base
2010-01-23 14:17:30 +01:00
ConvertTTDBasePrice ( buf - > ReadDWord ( ) , " RoadVehicleChangeInfo " , & rvi - > running_cost_class ) ;
2007-07-04 14:52:43 +02:00
break ;
case 0x0E : { // Sprite ID
2010-01-23 14:17:30 +01:00
uint8 spriteid = buf - > ReadByte ( ) ;
2013-11-26 17:08:58 +01:00
uint8 orig_spriteid = spriteid ;
2004-11-13 19:01:33 +01:00
2007-03-21 04:06:21 +01:00
/* cars have different custom id in the GRF file */
2006-02-18 15:41:24 +01:00
if ( spriteid = = 0xFF ) spriteid = 0xFD ;
2004-11-13 19:01:33 +01:00
2006-02-18 15:41:24 +01:00
if ( spriteid < 0xFD ) spriteid > > = 1 ;
2006-01-27 08:13:58 +01:00
2013-11-26 17:08:58 +01:00
if ( IsValidNewGRFImageIndex < VEH_ROAD > ( spriteid ) ) {
rvi - > image_index = spriteid ;
} else {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " RoadVehicleChangeInfo: Invalid Sprite {} specified, ignoring " , orig_spriteid ) ;
2013-11-26 17:08:58 +01:00
rvi - > image_index = 0 ;
}
2010-08-01 20:53:30 +02:00
break ;
}
2004-11-13 19:01:33 +01:00
2009-09-22 21:28:57 +02:00
case PROP_ROADVEH_CARGO_CAPACITY : // 0x0F Cargo capacity
2010-01-23 14:17:30 +01:00
rvi - > capacity = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2006-02-27 16:35:22 +01:00
2007-07-04 14:52:43 +02:00
case 0x10 : { // Cargo type
2012-02-07 23:59:30 +01:00
_gted [ e - > index ] . defaultcargo_grf = _cur . grffile ;
2011-11-08 18:25:42 +01:00
uint8 ctype = buf - > ReadByte ( ) ;
2004-11-13 19:01:33 +01:00
2011-11-08 18:25:42 +01:00
if ( ctype = = 0xFF ) {
/* 0xFF is specified as 'use first refittable' */
2009-08-17 20:33:48 +02:00
ei - > cargo_type = CT_INVALID ;
2011-11-08 18:25:42 +01:00
} else if ( _cur . grffile - > grf_version > = 8 ) {
/* Use translated cargo. Might result in CT_INVALID (first refittable), if cargo is not defined. */
ei - > cargo_type = GetCargoTranslation ( ctype , _cur . grffile ) ;
2012-07-27 21:16:01 +02:00
} else if ( ctype < NUM_CARGO ) {
2011-11-08 18:25:42 +01:00
/* Use untranslated cargo. */
ei - > cargo_type = ctype ;
2008-08-30 10:59:55 +02:00
} else {
2009-08-17 20:33:48 +02:00
ei - > cargo_type = CT_INVALID ;
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " RailVehicleChangeInfo: Invalid cargo type {}, using first refittable " , ctype ) ;
2006-01-12 08:57:40 +01:00
}
2010-08-01 20:53:30 +02:00
break ;
}
2006-02-27 16:35:22 +01:00
2009-09-22 21:28:57 +02:00
case PROP_ROADVEH_COST_FACTOR : // 0x11 Cost factor
2010-01-23 14:17:30 +01:00
rvi - > cost_factor = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
case 0x12 : // SFX
2016-02-08 22:05:57 +01:00
rvi - > sfx = GetNewGRFSoundID ( _cur . grffile , buf - > ReadByte ( ) ) ;
2007-07-04 14:52:43 +02:00
break ;
2010-10-30 16:31:49 +02:00
case PROP_ROADVEH_POWER : // Power in units of 10 HP.
2010-01-23 14:17:30 +01:00
rvi - > power = buf - > ReadByte ( ) ;
2009-02-02 22:52:34 +01:00
break ;
2010-10-30 16:31:49 +02:00
case PROP_ROADVEH_WEIGHT : // Weight in units of 1/4 tons.
2010-01-23 14:17:30 +01:00
rvi - > weight = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2010-11-06 14:11:24 +01:00
case PROP_ROADVEH_SPEED : // Speed in mph/0.8
2010-01-23 14:17:30 +01:00
_gted [ e - > index ] . rv_max_speed = buf - > ReadByte ( ) ;
2009-01-06 15:45:38 +01:00
break ;
2012-02-07 23:48:09 +01:00
case 0x16 : { // Cargoes available for refitting
uint32 mask = buf - > ReadDWord ( ) ;
_gted [ e - > index ] . UpdateRefittability ( mask ! = 0 ) ;
ei - > refit_mask = TranslateRefitMask ( mask ) ;
2012-02-07 23:59:30 +01:00
_gted [ e - > index ] . defaultcargo_grf = _cur . grffile ;
2007-07-04 14:52:43 +02:00
break ;
2012-02-07 23:48:09 +01:00
}
2007-07-04 14:52:43 +02:00
case 0x17 : // Callback mask
2023-01-22 18:56:36 +01:00
SB ( ei - > callback_mask , 0 , 8 , buf - > ReadByte ( ) ) ;
2007-07-04 14:52:43 +02:00
break ;
2010-10-30 16:31:49 +02:00
case PROP_ROADVEH_TRACTIVE_EFFORT : // Tractive effort coefficient in 1/256.
2010-01-23 14:17:30 +01:00
rvi - > tractive_effort = buf - > ReadByte ( ) ;
2009-02-02 22:52:34 +01:00
break ;
2007-10-26 01:24:28 +02:00
case 0x19 : // Air drag
2010-01-23 14:17:30 +01:00
rvi - > air_drag = buf - > ReadByte ( ) ;
2007-10-26 01:24:28 +02:00
break ;
2007-07-04 14:52:43 +02:00
case 0x1A : // Refit cost
2010-01-23 14:17:30 +01:00
ei - > refit_cost = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
case 0x1B : // Retire vehicle early
2010-01-23 14:17:30 +01:00
ei - > retire_early = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
case 0x1C : // Miscellaneous flags
2010-01-23 14:17:30 +01:00
ei - > misc_flags = buf - > ReadByte ( ) ;
2007-11-19 22:02:30 +01:00
_loaded_newgrf_features . has_2CC | = HasBit ( ei - > misc_flags , EF_USES_2CC ) ;
2007-07-04 14:52:43 +02:00
break ;
case 0x1D : // Cargo classes allowed
2010-01-23 14:17:30 +01:00
_gted [ e - > index ] . cargo_allowed = buf - > ReadWord ( ) ;
2012-02-07 23:03:03 +01:00
_gted [ e - > index ] . UpdateRefittability ( _gted [ e - > index ] . cargo_allowed ! = 0 ) ;
2012-02-07 23:59:30 +01:00
_gted [ e - > index ] . defaultcargo_grf = _cur . grffile ;
2007-07-04 14:52:43 +02:00
break ;
case 0x1E : // Cargo classes disallowed
2010-01-23 14:17:30 +01:00
_gted [ e - > index ] . cargo_disallowed = buf - > ReadWord ( ) ;
2012-02-07 23:03:03 +01:00
_gted [ e - > index ] . UpdateRefittability ( false ) ;
2007-07-04 14:52:43 +02:00
break ;
case 0x1F : // Long format introduction date (days since year 0)
2010-01-23 14:17:30 +01:00
ei - > base_intro = buf - > ReadDWord ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2008-07-21 16:42:25 +02:00
case 0x20 : // Alter purchase list sort order
2010-03-10 08:31:08 +01:00
AlterVehicleListOrder ( e - > index , buf - > ReadExtendedByte ( ) ) ;
2008-07-21 16:42:25 +02:00
break ;
2010-11-18 15:32:09 +01:00
case 0x21 : // Visual effect
rvi - > visual_effect = buf - > ReadByte ( ) ;
/* Avoid accidentally setting visual_effect to the default value
* Since bit 6 ( disable effects ) is set anyways , we can safely erase some bits . */
if ( rvi - > visual_effect = = VE_DEFAULT ) {
assert ( HasBit ( rvi - > visual_effect , VE_DISABLE_EFFECT ) ) ;
SB ( rvi - > visual_effect , VE_TYPE_START , VE_TYPE_COUNT , 0 ) ;
}
break ;
2011-08-03 22:55:08 +02:00
case PROP_ROADVEH_CARGO_AGE_PERIOD : // 0x22 Cargo aging period
ei - > cargo_age_period = buf - > ReadWord ( ) ;
break ;
2011-11-08 18:27:55 +01:00
case PROP_ROADVEH_SHORTEN_FACTOR : // 0x23 Shorter vehicle
2011-11-08 18:27:46 +01:00
rvi - > shorten_factor = buf - > ReadByte ( ) ;
break ;
2011-11-21 23:39:59 +01:00
case 0x24 : // CTT refit include list
case 0x25 : { // CTT refit exclude list
uint8 count = buf - > ReadByte ( ) ;
2012-02-07 23:03:03 +01:00
_gted [ e - > index ] . UpdateRefittability ( prop = = 0x24 & & count ! = 0 ) ;
2012-02-07 23:59:30 +01:00
if ( prop = = 0x24 ) _gted [ e - > index ] . defaultcargo_grf = _cur . grffile ;
2018-05-21 23:08:39 +02:00
CargoTypes & ctt = prop = = 0x24 ? _gted [ e - > index ] . ctt_include_mask : _gted [ e - > index ] . ctt_exclude_mask ;
2012-02-16 23:22:45 +01:00
ctt = 0 ;
2011-11-21 23:39:59 +01:00
while ( count - - ) {
CargoID ctype = GetCargoTranslation ( buf - > ReadByte ( ) , _cur . grffile ) ;
2023-05-04 12:29:21 +02:00
if ( IsValidCargoID ( ctype ) ) SetBit ( ctt , ctype ) ;
2011-11-21 23:39:59 +01:00
}
break ;
}
2019-02-27 08:00:33 +01:00
case 0x26 : // Engine variant
2023-01-13 20:22:31 +01:00
ei - > variant_id = buf - > ReadWord ( ) ;
2019-02-27 08:00:33 +01:00
break ;
2022-12-09 02:40:55 +01:00
case 0x27 : // Extra miscellaneous flags
ei - > extra_flags = static_cast < ExtraEngineFlags > ( buf - > ReadDWord ( ) ) ;
break ;
2023-01-22 18:56:36 +01:00
case 0x28 : // Callback additional mask
SB ( ei - > callback_mask , 8 , 8 , buf - > ReadByte ( ) ) ;
break ;
2007-07-04 14:52:43 +02:00
default :
2010-01-23 14:17:30 +01:00
ret = CommonVehicleChangeInfo ( ei , prop , buf ) ;
2007-07-04 14:52:43 +02:00
break ;
}
2004-11-13 19:01:33 +01:00
}
return ret ;
}
2011-09-11 13:14:22 +02:00
/**
* Define properties for ships
* @ param engine Local ID of the first vehicle .
* @ param numinfo Number of subsequent IDs to change the property for .
* @ param prop The property to change .
* @ param buf The property value .
* @ return ChangeInfoResult .
*/
2010-01-22 14:02:15 +01:00
static ChangeInfoResult ShipVehicleChangeInfo ( uint engine , int numinfo , int prop , ByteReader * buf )
2004-08-09 19:04:08 +02:00
{
2008-08-31 18:41:27 +02:00
ChangeInfoResult ret = CIR_SUCCESS ;
2004-08-09 19:04:08 +02:00
2007-07-04 14:52:43 +02:00
for ( int i = 0 ; i < numinfo ; i + + ) {
2011-08-21 14:05:47 +02:00
Engine * e = GetNewEngine ( _cur . grffile , VEH_SHIP , engine + i ) ;
2019-04-10 23:07:06 +02:00
if ( e = = nullptr ) return CIR_INVALID_ID ; // No engine could be allocated, so neither can any next vehicles
2011-02-09 19:46:55 +01:00
2008-04-29 23:31:29 +02:00
EngineInfo * ei = & e - > info ;
ShipVehicleInfo * svi = & e - > u . ship ;
2007-07-04 14:52:43 +02:00
switch ( prop ) {
case 0x08 : { // Sprite ID
2010-01-23 14:17:30 +01:00
uint8 spriteid = buf - > ReadByte ( ) ;
2013-11-26 17:08:58 +01:00
uint8 orig_spriteid = spriteid ;
2004-08-09 19:04:08 +02:00
2007-03-21 04:06:21 +01:00
/* ships have different custom id in the GRF file */
2006-02-18 15:41:24 +01:00
if ( spriteid = = 0xFF ) spriteid = 0xFD ;
2004-08-09 19:04:08 +02:00
2006-02-18 15:41:24 +01:00
if ( spriteid < 0xFD ) spriteid > > = 1 ;
2006-01-27 08:13:58 +01:00
2013-11-26 17:08:58 +01:00
if ( IsValidNewGRFImageIndex < VEH_SHIP > ( spriteid ) ) {
svi - > image_index = spriteid ;
} else {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " ShipVehicleChangeInfo: Invalid Sprite {} specified, ignoring " , orig_spriteid ) ;
2013-11-26 17:08:58 +01:00
svi - > image_index = 0 ;
}
2010-08-01 20:53:30 +02:00
break ;
}
2004-08-09 19:04:08 +02:00
2007-07-04 14:52:43 +02:00
case 0x09 : // Refittable
2010-01-23 14:17:30 +01:00
svi - > old_refittable = ( buf - > ReadByte ( ) ! = 0 ) ;
2007-07-04 14:52:43 +02:00
break ;
2004-08-09 19:04:08 +02:00
2009-09-22 21:28:57 +02:00
case PROP_SHIP_COST_FACTOR : // 0x0A Cost factor
2010-01-23 14:17:30 +01:00
svi - > cost_factor = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2004-08-09 19:04:08 +02:00
2009-09-22 21:28:57 +02:00
case PROP_SHIP_SPEED : // 0x0B Speed (1 unit is 0.5 km-ish/h)
2010-01-23 14:17:30 +01:00
svi - > max_speed = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2006-02-27 16:35:22 +01:00
2007-07-04 14:52:43 +02:00
case 0x0C : { // Cargo type
2012-02-07 23:59:30 +01:00
_gted [ e - > index ] . defaultcargo_grf = _cur . grffile ;
2011-11-08 18:25:42 +01:00
uint8 ctype = buf - > ReadByte ( ) ;
2004-08-09 19:04:08 +02:00
2011-11-08 18:25:42 +01:00
if ( ctype = = 0xFF ) {
/* 0xFF is specified as 'use first refittable' */
2009-08-17 20:33:48 +02:00
ei - > cargo_type = CT_INVALID ;
2011-11-08 18:25:42 +01:00
} else if ( _cur . grffile - > grf_version > = 8 ) {
/* Use translated cargo. Might result in CT_INVALID (first refittable), if cargo is not defined. */
ei - > cargo_type = GetCargoTranslation ( ctype , _cur . grffile ) ;
2012-07-27 21:16:01 +02:00
} else if ( ctype < NUM_CARGO ) {
2011-11-08 18:25:42 +01:00
/* Use untranslated cargo. */
ei - > cargo_type = ctype ;
2006-01-12 08:57:40 +01:00
} else {
2009-08-17 20:33:48 +02:00
ei - > cargo_type = CT_INVALID ;
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " ShipVehicleChangeInfo: Invalid cargo type {}, using first refittable " , ctype ) ;
2006-01-12 08:57:40 +01:00
}
2010-08-01 20:53:30 +02:00
break ;
}
2006-02-27 16:35:22 +01:00
2009-09-22 21:28:57 +02:00
case PROP_SHIP_CARGO_CAPACITY : // 0x0D Cargo capacity
2010-01-23 14:17:30 +01:00
svi - > capacity = buf - > ReadWord ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2009-09-22 21:28:57 +02:00
case PROP_SHIP_RUNNING_COST_FACTOR : // 0x0F Running cost factor
2010-01-23 14:17:30 +01:00
svi - > running_cost = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
case 0x10 : // SFX
2016-02-08 22:05:57 +01:00
svi - > sfx = GetNewGRFSoundID ( _cur . grffile , buf - > ReadByte ( ) ) ;
2007-07-04 14:52:43 +02:00
break ;
2012-02-07 23:48:09 +01:00
case 0x11 : { // Cargoes available for refitting
uint32 mask = buf - > ReadDWord ( ) ;
_gted [ e - > index ] . UpdateRefittability ( mask ! = 0 ) ;
ei - > refit_mask = TranslateRefitMask ( mask ) ;
2012-02-07 23:59:30 +01:00
_gted [ e - > index ] . defaultcargo_grf = _cur . grffile ;
2007-07-04 14:52:43 +02:00
break ;
2012-02-07 23:48:09 +01:00
}
2007-07-04 14:52:43 +02:00
case 0x12 : // Callback mask
2023-01-22 18:56:36 +01:00
SB ( ei - > callback_mask , 0 , 8 , buf - > ReadByte ( ) ) ;
2007-07-04 14:52:43 +02:00
break ;
case 0x13 : // Refit cost
2010-01-23 14:17:30 +01:00
ei - > refit_cost = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2007-10-26 01:24:28 +02:00
case 0x14 : // Ocean speed fraction
2011-07-07 16:16:22 +02:00
svi - > ocean_speed_frac = buf - > ReadByte ( ) ;
break ;
2007-10-26 01:24:28 +02:00
case 0x15 : // Canal speed fraction
2011-07-07 16:16:22 +02:00
svi - > canal_speed_frac = buf - > ReadByte ( ) ;
2007-10-26 01:24:28 +02:00
break ;
2007-07-04 14:52:43 +02:00
case 0x16 : // Retire vehicle early
2010-01-23 14:17:30 +01:00
ei - > retire_early = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
case 0x17 : // Miscellaneous flags
2010-01-23 14:17:30 +01:00
ei - > misc_flags = buf - > ReadByte ( ) ;
2007-11-19 22:02:30 +01:00
_loaded_newgrf_features . has_2CC | = HasBit ( ei - > misc_flags , EF_USES_2CC ) ;
2007-07-04 14:52:43 +02:00
break ;
case 0x18 : // Cargo classes allowed
2010-01-23 14:17:30 +01:00
_gted [ e - > index ] . cargo_allowed = buf - > ReadWord ( ) ;
2012-02-07 23:03:03 +01:00
_gted [ e - > index ] . UpdateRefittability ( _gted [ e - > index ] . cargo_allowed ! = 0 ) ;
2012-02-07 23:59:30 +01:00
_gted [ e - > index ] . defaultcargo_grf = _cur . grffile ;
2007-07-04 14:52:43 +02:00
break ;
case 0x19 : // Cargo classes disallowed
2010-01-23 14:17:30 +01:00
_gted [ e - > index ] . cargo_disallowed = buf - > ReadWord ( ) ;
2012-02-07 23:03:03 +01:00
_gted [ e - > index ] . UpdateRefittability ( false ) ;
2007-07-04 14:52:43 +02:00
break ;
case 0x1A : // Long format introduction date (days since year 0)
2010-01-23 14:17:30 +01:00
ei - > base_intro = buf - > ReadDWord ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2008-07-21 16:42:25 +02:00
case 0x1B : // Alter purchase list sort order
2010-03-10 08:31:08 +01:00
AlterVehicleListOrder ( e - > index , buf - > ReadExtendedByte ( ) ) ;
2008-07-21 16:42:25 +02:00
break ;
2010-11-18 15:32:09 +01:00
case 0x1C : // Visual effect
svi - > visual_effect = buf - > ReadByte ( ) ;
/* Avoid accidentally setting visual_effect to the default value
* Since bit 6 ( disable effects ) is set anyways , we can safely erase some bits . */
if ( svi - > visual_effect = = VE_DEFAULT ) {
assert ( HasBit ( svi - > visual_effect , VE_DISABLE_EFFECT ) ) ;
SB ( svi - > visual_effect , VE_TYPE_START , VE_TYPE_COUNT , 0 ) ;
}
break ;
2011-08-03 22:55:08 +02:00
case PROP_SHIP_CARGO_AGE_PERIOD : // 0x1D Cargo aging period
ei - > cargo_age_period = buf - > ReadWord ( ) ;
break ;
2011-11-21 23:39:59 +01:00
case 0x1E : // CTT refit include list
case 0x1F : { // CTT refit exclude list
uint8 count = buf - > ReadByte ( ) ;
2012-02-07 23:03:03 +01:00
_gted [ e - > index ] . UpdateRefittability ( prop = = 0x1E & & count ! = 0 ) ;
2012-02-07 23:59:30 +01:00
if ( prop = = 0x1E ) _gted [ e - > index ] . defaultcargo_grf = _cur . grffile ;
2018-05-21 23:08:39 +02:00
CargoTypes & ctt = prop = = 0x1E ? _gted [ e - > index ] . ctt_include_mask : _gted [ e - > index ] . ctt_exclude_mask ;
2012-02-16 23:22:45 +01:00
ctt = 0 ;
2011-11-21 23:39:59 +01:00
while ( count - - ) {
CargoID ctype = GetCargoTranslation ( buf - > ReadByte ( ) , _cur . grffile ) ;
2023-05-04 12:29:21 +02:00
if ( IsValidCargoID ( ctype ) ) SetBit ( ctt , ctype ) ;
2011-11-21 23:39:59 +01:00
}
break ;
}
2019-02-27 08:00:33 +01:00
case 0x20 : // Engine variant
2023-01-13 20:22:31 +01:00
ei - > variant_id = buf - > ReadWord ( ) ;
2019-02-27 08:00:33 +01:00
break ;
2022-12-09 02:40:55 +01:00
case 0x21 : // Extra miscellaneous flags
ei - > extra_flags = static_cast < ExtraEngineFlags > ( buf - > ReadDWord ( ) ) ;
break ;
2023-01-22 18:56:36 +01:00
case 0x22 : // Callback additional mask
SB ( ei - > callback_mask , 8 , 8 , buf - > ReadByte ( ) ) ;
break ;
2007-07-04 14:52:43 +02:00
default :
2010-01-23 14:17:30 +01:00
ret = CommonVehicleChangeInfo ( ei , prop , buf ) ;
2007-07-04 14:52:43 +02:00
break ;
}
2004-08-09 19:04:08 +02:00
}
return ret ;
}
2011-09-11 13:14:22 +02:00
/**
* Define properties for aircraft
* @ param engine Local ID of the aircraft .
* @ param numinfo Number of subsequent IDs to change the property for .
* @ param prop The property to change .
* @ param buf The property value .
* @ return ChangeInfoResult .
*/
2010-01-22 14:02:15 +01:00
static ChangeInfoResult AircraftVehicleChangeInfo ( uint engine , int numinfo , int prop , ByteReader * buf )
2004-11-13 21:37:57 +01:00
{
2008-08-31 18:41:27 +02:00
ChangeInfoResult ret = CIR_SUCCESS ;
2004-11-13 21:37:57 +01:00
2007-07-04 14:52:43 +02:00
for ( int i = 0 ; i < numinfo ; i + + ) {
2011-08-21 14:05:47 +02:00
Engine * e = GetNewEngine ( _cur . grffile , VEH_AIRCRAFT , engine + i ) ;
2019-04-10 23:07:06 +02:00
if ( e = = nullptr ) return CIR_INVALID_ID ; // No engine could be allocated, so neither can any next vehicles
2011-02-09 19:46:55 +01:00
2008-04-29 23:31:29 +02:00
EngineInfo * ei = & e - > info ;
AircraftVehicleInfo * avi = & e - > u . air ;
2007-07-04 14:52:43 +02:00
switch ( prop ) {
case 0x08 : { // Sprite ID
2010-01-23 14:17:30 +01:00
uint8 spriteid = buf - > ReadByte ( ) ;
2013-11-26 17:08:58 +01:00
uint8 orig_spriteid = spriteid ;
2004-11-13 21:37:57 +01:00
2007-03-21 04:06:21 +01:00
/* aircraft have different custom id in the GRF file */
2006-02-18 15:41:24 +01:00
if ( spriteid = = 0xFF ) spriteid = 0xFD ;
2004-11-13 21:37:57 +01:00
2006-02-18 15:41:24 +01:00
if ( spriteid < 0xFD ) spriteid > > = 1 ;
2006-01-27 08:13:58 +01:00
2013-11-26 17:08:58 +01:00
if ( IsValidNewGRFImageIndex < VEH_AIRCRAFT > ( spriteid ) ) {
avi - > image_index = spriteid ;
} else {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " AircraftVehicleChangeInfo: Invalid Sprite {} specified, ignoring " , orig_spriteid ) ;
2013-11-26 17:08:58 +01:00
avi - > image_index = 0 ;
}
2010-08-01 20:53:30 +02:00
break ;
}
2004-11-13 21:37:57 +01:00
2007-07-04 14:52:43 +02:00
case 0x09 : // Helicopter
2010-01-23 14:17:30 +01:00
if ( buf - > ReadByte ( ) = = 0 ) {
2007-07-04 14:52:43 +02:00
avi - > subtype = AIR_HELI ;
2006-10-23 19:01:02 +02:00
} else {
2007-07-04 14:52:43 +02:00
SB ( avi - > subtype , 0 , 1 , 1 ) ; // AIR_CTOL
2006-10-23 19:01:02 +02:00
}
2007-07-04 14:52:43 +02:00
break ;
2004-11-13 21:37:57 +01:00
2007-07-04 14:52:43 +02:00
case 0x0A : // Large
2010-01-23 14:17:30 +01:00
SB ( avi - > subtype , 1 , 1 , ( buf - > ReadByte ( ) ! = 0 ? 1 : 0 ) ) ; // AIR_FAST
2007-07-04 14:52:43 +02:00
break ;
2004-11-13 21:37:57 +01:00
2009-09-22 21:28:57 +02:00
case PROP_AIRCRAFT_COST_FACTOR : // 0x0B Cost factor
2010-01-23 14:17:30 +01:00
avi - > cost_factor = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2004-11-13 21:37:57 +01:00
2010-07-16 19:45:34 +02:00
case PROP_AIRCRAFT_SPEED : // 0x0C Speed (1 unit is 8 mph, we translate to 1 unit is 1 km-ish/h)
avi - > max_speed = ( buf - > ReadByte ( ) * 128 ) / 10 ;
2007-07-04 14:52:43 +02:00
break ;
2004-11-13 21:37:57 +01:00
2013-03-22 22:27:13 +01:00
case 0x0D : // Acceleration
avi - > acceleration = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2004-11-13 21:37:57 +01:00
2009-09-22 21:28:57 +02:00
case PROP_AIRCRAFT_RUNNING_COST_FACTOR : // 0x0E Running cost factor
2010-01-23 14:17:30 +01:00
avi - > running_cost = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2004-11-13 21:37:57 +01:00
2010-02-22 22:46:20 +01:00
case PROP_AIRCRAFT_PASSENGER_CAPACITY : // 0x0F Passenger capacity
2010-01-23 14:17:30 +01:00
avi - > passenger_capacity = buf - > ReadWord ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2004-11-13 21:37:57 +01:00
2010-02-22 22:46:20 +01:00
case PROP_AIRCRAFT_MAIL_CAPACITY : // 0x11 Mail capacity
2010-01-23 14:17:30 +01:00
avi - > mail_capacity = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2006-02-11 08:34:05 +01:00
2007-07-04 14:52:43 +02:00
case 0x12 : // SFX
2016-02-08 22:05:57 +01:00
avi - > sfx = GetNewGRFSoundID ( _cur . grffile , buf - > ReadByte ( ) ) ;
2007-07-04 14:52:43 +02:00
break ;
2006-02-27 16:35:22 +01:00
2012-02-07 23:48:09 +01:00
case 0x13 : { // Cargoes available for refitting
uint32 mask = buf - > ReadDWord ( ) ;
_gted [ e - > index ] . UpdateRefittability ( mask ! = 0 ) ;
ei - > refit_mask = TranslateRefitMask ( mask ) ;
2012-02-07 23:59:30 +01:00
_gted [ e - > index ] . defaultcargo_grf = _cur . grffile ;
2007-07-04 14:52:43 +02:00
break ;
2012-02-07 23:48:09 +01:00
}
2006-02-27 16:35:22 +01:00
2007-07-04 14:52:43 +02:00
case 0x14 : // Callback mask
2023-01-22 18:56:36 +01:00
SB ( ei - > callback_mask , 0 , 8 , buf - > ReadByte ( ) ) ;
2007-07-04 14:52:43 +02:00
break ;
2006-02-27 16:35:22 +01:00
2007-07-04 14:52:43 +02:00
case 0x15 : // Refit cost
2010-01-23 14:17:30 +01:00
ei - > refit_cost = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2006-06-09 18:35:07 +02:00
2007-07-04 14:52:43 +02:00
case 0x16 : // Retire vehicle early
2010-01-23 14:17:30 +01:00
ei - > retire_early = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2007-05-11 01:10:23 +02:00
2007-07-04 14:52:43 +02:00
case 0x17 : // Miscellaneous flags
2010-01-23 14:17:30 +01:00
ei - > misc_flags = buf - > ReadByte ( ) ;
2007-11-19 22:02:30 +01:00
_loaded_newgrf_features . has_2CC | = HasBit ( ei - > misc_flags , EF_USES_2CC ) ;
2007-07-04 14:52:43 +02:00
break ;
2006-02-28 09:13:15 +01:00
2007-07-04 14:52:43 +02:00
case 0x18 : // Cargo classes allowed
2010-01-23 14:17:30 +01:00
_gted [ e - > index ] . cargo_allowed = buf - > ReadWord ( ) ;
2012-02-07 23:03:03 +01:00
_gted [ e - > index ] . UpdateRefittability ( _gted [ e - > index ] . cargo_allowed ! = 0 ) ;
2012-02-07 23:59:30 +01:00
_gted [ e - > index ] . defaultcargo_grf = _cur . grffile ;
2007-07-04 14:52:43 +02:00
break ;
2006-02-27 16:35:22 +01:00
2007-07-04 14:52:43 +02:00
case 0x19 : // Cargo classes disallowed
2010-01-23 14:17:30 +01:00
_gted [ e - > index ] . cargo_disallowed = buf - > ReadWord ( ) ;
2012-02-07 23:03:03 +01:00
_gted [ e - > index ] . UpdateRefittability ( false ) ;
2007-07-04 14:52:43 +02:00
break ;
2006-02-11 08:34:05 +01:00
2007-07-04 14:52:43 +02:00
case 0x1A : // Long format introduction date (days since year 0)
2010-01-23 14:17:30 +01:00
ei - > base_intro = buf - > ReadDWord ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2006-11-17 12:03:38 +01:00
2008-07-21 16:42:25 +02:00
case 0x1B : // Alter purchase list sort order
2010-03-10 08:31:08 +01:00
AlterVehicleListOrder ( e - > index , buf - > ReadExtendedByte ( ) ) ;
2008-07-21 16:42:25 +02:00
break ;
2011-08-03 22:55:08 +02:00
case PROP_AIRCRAFT_CARGO_AGE_PERIOD : // 0x1C Cargo aging period
ei - > cargo_age_period = buf - > ReadWord ( ) ;
break ;
2011-11-21 23:39:59 +01:00
case 0x1D : // CTT refit include list
case 0x1E : { // CTT refit exclude list
uint8 count = buf - > ReadByte ( ) ;
2012-02-07 23:03:03 +01:00
_gted [ e - > index ] . UpdateRefittability ( prop = = 0x1D & & count ! = 0 ) ;
2012-02-07 23:59:30 +01:00
if ( prop = = 0x1D ) _gted [ e - > index ] . defaultcargo_grf = _cur . grffile ;
2018-05-21 23:08:39 +02:00
CargoTypes & ctt = prop = = 0x1D ? _gted [ e - > index ] . ctt_include_mask : _gted [ e - > index ] . ctt_exclude_mask ;
2012-02-16 23:22:45 +01:00
ctt = 0 ;
2011-11-21 23:39:59 +01:00
while ( count - - ) {
CargoID ctype = GetCargoTranslation ( buf - > ReadByte ( ) , _cur . grffile ) ;
2023-05-04 12:29:21 +02:00
if ( IsValidCargoID ( ctype ) ) SetBit ( ctt , ctype ) ;
2011-11-21 23:39:59 +01:00
}
break ;
}
2011-12-13 01:43:35 +01:00
case PROP_AIRCRAFT_RANGE : // 0x1F Max aircraft range
avi - > max_range = buf - > ReadWord ( ) ;
break ;
2019-02-27 08:00:33 +01:00
case 0x20 : // Engine variant
2023-01-13 20:22:31 +01:00
ei - > variant_id = buf - > ReadWord ( ) ;
2019-02-27 08:00:33 +01:00
break ;
2022-12-09 02:40:55 +01:00
case 0x21 : // Extra miscellaneous flags
ei - > extra_flags = static_cast < ExtraEngineFlags > ( buf - > ReadDWord ( ) ) ;
break ;
2023-01-22 18:56:36 +01:00
case 0x22 : // Callback additional mask
SB ( ei - > callback_mask , 8 , 8 , buf - > ReadByte ( ) ) ;
break ;
2007-07-04 14:52:43 +02:00
default :
2010-01-23 14:17:30 +01:00
ret = CommonVehicleChangeInfo ( ei , prop , buf ) ;
2007-07-04 14:52:43 +02:00
break ;
}
2004-11-13 21:37:57 +01:00
}
return ret ;
}
2011-09-11 13:14:22 +02:00
/**
* Define properties for stations
2018-10-28 03:17:36 +01:00
* @ param stid StationID of the first station tile .
2011-09-11 13:14:22 +02:00
* @ param numinfo Number of subsequent station tiles to change the property for .
* @ param prop The property to change .
* @ param buf The property value .
* @ return ChangeInfoResult .
*/
2010-01-22 14:02:15 +01:00
static ChangeInfoResult StationChangeInfo ( uint stid , int numinfo , int prop , ByteReader * buf )
2004-11-14 10:07:15 +01:00
{
2008-08-31 18:41:27 +02:00
ChangeInfoResult ret = CIR_SUCCESS ;
2004-11-14 10:07:15 +01:00
2013-10-12 18:35:18 +02:00
if ( stid + numinfo > NUM_STATIONS_PER_GRF ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " StationChangeInfo: Station {} is invalid, max {}, ignoring " , stid + numinfo , NUM_STATIONS_PER_GRF ) ;
2008-08-31 18:41:27 +02:00
return CIR_INVALID_ID ;
2006-05-07 00:20:16 +02:00
}
2006-04-11 12:05:52 +02:00
/* Allocate station specs if necessary */
2023-01-08 03:12:33 +01:00
if ( _cur . grffile - > stations . size ( ) < stid + numinfo ) _cur . grffile - > stations . resize ( stid + numinfo ) ;
2006-05-07 00:20:16 +02:00
2007-07-04 14:52:43 +02:00
for ( int i = 0 ; i < numinfo ; i + + ) {
2023-04-15 22:30:37 +02:00
StationSpec * statspec = _cur . grffile - > stations [ stid + i ] . get ( ) ;
2006-04-11 12:05:52 +02:00
2007-07-04 14:52:43 +02:00
/* Check that the station we are modifying is defined. */
2019-04-10 23:07:06 +02:00
if ( statspec = = nullptr & & prop ! = 0x08 ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " StationChangeInfo: Attempt to modify undefined station {}, ignoring " , stid + i ) ;
2008-08-31 18:41:27 +02:00
return CIR_INVALID_ID ;
2006-04-11 12:05:52 +02:00
}
2004-11-14 10:07:15 +01:00
2007-07-04 14:52:43 +02:00
switch ( prop ) {
case 0x08 : { // Class ID
2006-05-07 00:20:16 +02:00
/* Property 0x08 is special; it is where the station is allocated */
2023-04-15 22:30:37 +02:00
if ( statspec = = nullptr ) {
_cur . grffile - > stations [ stid + i ] = std : : make_unique < StationSpec > ( ) ;
statspec = _cur . grffile - > stations [ stid + i ] . get ( ) ;
}
2006-05-07 00:20:16 +02:00
2006-05-01 23:45:35 +02:00
/* Swap classid because we read it in BE meaning WAYP or DFLT */
2010-01-23 14:17:30 +01:00
uint32 classid = buf - > ReadDWord ( ) ;
2023-04-15 22:30:37 +02:00
statspec - > cls_id = StationClass : : Allocate ( BSWAP32 ( classid ) ) ;
2010-08-01 20:53:30 +02:00
break ;
}
2004-11-14 14:59:11 +01:00
2021-05-01 21:28:23 +02:00
case 0x09 : { // Define sprite layout
uint16 tiles = buf - > ReadExtendedByte ( ) ;
statspec - > renderdata . clear ( ) ; // delete earlier loaded stuff
statspec - > renderdata . reserve ( tiles ) ;
2006-05-03 23:07:44 +02:00
2021-05-01 21:28:23 +02:00
for ( uint t = 0 ; t < tiles ; t + + ) {
NewGRFSpriteLayout * dts = & statspec - > renderdata . emplace_back ( ) ;
2011-09-11 17:10:09 +02:00
dts - > consistent_max_offset = UINT16_MAX ; // Spritesets are unknown, so no limit.
2004-11-14 10:07:15 +01:00
2011-05-18 20:31:23 +02:00
if ( buf - > HasData ( 4 ) & & * ( uint32 * ) buf - > Data ( ) = = 0 ) {
buf - > Skip ( 4 ) ;
2011-03-03 22:24:03 +01:00
extern const DrawTileSprites _station_display_datas_rail [ 8 ] ;
2011-05-14 19:25:45 +02:00
dts - > Clone ( & _station_display_datas_rail [ t % 8 ] ) ;
2011-03-03 22:24:03 +01:00
continue ;
}
2008-02-16 04:01:05 +01:00
2011-09-11 17:08:17 +02:00
ReadSpriteLayoutSprite ( buf , false , false , false , GSF_STATIONS , & dts - > ground ) ;
2011-05-29 18:56:22 +02:00
/* On error, bail out immediately. Temporary GRF data was already freed */
2011-08-21 14:05:47 +02:00
if ( _cur . skip_sprites < 0 ) return CIR_DISABLED ;
2005-11-12 01:19:34 +01:00
2019-03-03 18:30:09 +01:00
static std : : vector < DrawTileSeqStruct > tmp_layout ;
2018-09-21 00:44:14 +02:00
tmp_layout . clear ( ) ;
2011-03-03 20:52:42 +01:00
for ( ; ; ) {
2007-03-21 04:06:21 +01:00
/* no relative bounding box support */
2020-12-15 00:22:04 +01:00
DrawTileSeqStruct & dtss = tmp_layout . emplace_back ( ) ;
2019-02-18 23:39:06 +01:00
MemSetT ( & dtss , 0 ) ;
dtss . delta_x = buf - > ReadByte ( ) ;
if ( dtss . IsTerminator ( ) ) break ;
dtss . delta_y = buf - > ReadByte ( ) ;
dtss . delta_z = buf - > ReadByte ( ) ;
dtss . size_x = buf - > ReadByte ( ) ;
dtss . size_y = buf - > ReadByte ( ) ;
dtss . size_z = buf - > ReadByte ( ) ;
ReadSpriteLayoutSprite ( buf , false , true , false , GSF_STATIONS , & dtss . image ) ;
2011-05-29 18:56:22 +02:00
/* On error, bail out immediately. Temporary GRF data was already freed */
2011-08-21 14:05:47 +02:00
if ( _cur . skip_sprites < 0 ) return CIR_DISABLED ;
2004-11-14 10:07:15 +01:00
}
2019-02-17 12:20:52 +01:00
dts - > Clone ( tmp_layout . data ( ) ) ;
2004-11-14 10:07:15 +01:00
}
2022-09-10 23:08:30 +02:00
/* Number of layouts must be even, alternating X and Y */
if ( statspec - > renderdata . size ( ) & 1 ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " StationChangeInfo: Station {} defines an odd number of sprite layouts, dropping the last item " , stid + i ) ;
2022-09-10 23:08:30 +02:00
statspec - > renderdata . pop_back ( ) ;
}
2007-07-04 14:52:43 +02:00
break ;
2021-05-01 21:28:23 +02:00
}
2006-04-11 15:00:06 +02:00
2007-07-04 14:52:43 +02:00
case 0x0A : { // Copy sprite layout
2023-04-18 01:12:34 +02:00
uint16_t srcid = buf - > ReadExtendedByte ( ) ;
2023-04-15 22:30:37 +02:00
const StationSpec * srcstatspec = srcid > = _cur . grffile - > stations . size ( ) ? nullptr : _cur . grffile - > stations [ srcid ] . get ( ) ;
2004-11-14 14:59:11 +01:00
2019-04-10 23:07:06 +02:00
if ( srcstatspec = = nullptr ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " StationChangeInfo: Station {} is not defined, cannot copy sprite layout to {}. " , srcid , stid + i ) ;
2010-01-27 11:17:09 +01:00
continue ;
}
2021-05-01 21:28:23 +02:00
statspec - > renderdata . clear ( ) ; // delete earlier loaded stuff
statspec - > renderdata . reserve ( srcstatspec - > renderdata . size ( ) ) ;
2011-05-17 22:11:19 +02:00
2021-05-01 21:28:23 +02:00
for ( const auto & it : srcstatspec - > renderdata ) {
NewGRFSpriteLayout * dts = & statspec - > renderdata . emplace_back ( ) ;
dts - > Clone ( & it ) ;
2011-03-03 22:24:03 +01:00
}
2010-08-01 20:53:30 +02:00
break ;
}
2006-04-11 15:00:06 +02:00
2007-07-04 14:52:43 +02:00
case 0x0B : // Callback mask
2010-01-23 14:17:30 +01:00
statspec - > callback_mask = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2004-11-17 19:03:33 +01:00
2007-07-04 14:52:43 +02:00
case 0x0C : // Disallowed number of platforms
2010-01-23 14:17:30 +01:00
statspec - > disallowed_platforms = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2006-04-11 15:00:06 +02:00
2007-07-04 14:52:43 +02:00
case 0x0D : // Disallowed platform lengths
2010-01-23 14:17:30 +01:00
statspec - > disallowed_lengths = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2004-11-17 19:03:33 +01:00
2007-07-04 14:52:43 +02:00
case 0x0E : // Define custom layout
2010-01-22 14:02:15 +01:00
while ( buf - > HasData ( ) ) {
2010-01-23 14:17:30 +01:00
byte length = buf - > ReadByte ( ) ;
byte number = buf - > ReadByte ( ) ;
2004-11-17 19:03:33 +01:00
if ( length = = 0 | | number = = 0 ) break ;
2021-05-01 19:07:47 +02:00
if ( statspec - > layouts . size ( ) < length ) statspec - > layouts . resize ( length ) ;
if ( statspec - > layouts [ length - 1 ] . size ( ) < number ) statspec - > layouts [ length - 1 ] . resize ( number ) ;
2004-11-17 19:03:33 +01:00
2021-05-01 19:07:47 +02:00
const byte * layout = buf - > ReadBytes ( length * number ) ;
statspec - > layouts [ length - 1 ] [ number - 1 ] . assign ( layout , layout + length * number ) ;
2021-05-02 11:47:05 +02:00
/* Validate tile values are only the permitted 00, 02, 04 and 06. */
for ( auto & tile : statspec - > layouts [ length - 1 ] [ number - 1 ] ) {
if ( ( tile & 6 ) ! = tile ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " StationChangeInfo: Invalid tile {} in layout {}x{} " , tile , length , number ) ;
2021-05-02 11:47:05 +02:00
tile & = 6 ;
}
}
2004-11-14 10:07:15 +01:00
}
2007-07-04 14:52:43 +02:00
break ;
2006-04-11 15:00:06 +02:00
2007-07-04 14:52:43 +02:00
case 0x0F : { // Copy custom layout
2023-04-18 01:12:34 +02:00
uint16_t srcid = buf - > ReadExtendedByte ( ) ;
2023-04-15 22:30:37 +02:00
const StationSpec * srcstatspec = srcid > = _cur . grffile - > stations . size ( ) ? nullptr : _cur . grffile - > stations [ srcid ] . get ( ) ;
2006-11-08 13:17:14 +01:00
2019-04-10 23:07:06 +02:00
if ( srcstatspec = = nullptr ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " StationChangeInfo: Station {} is not defined, cannot copy tile layout to {}. " , srcid , stid + i ) ;
2010-01-27 11:17:09 +01:00
continue ;
}
2021-05-01 19:07:47 +02:00
statspec - > layouts = srcstatspec - > layouts ;
2010-08-01 20:53:30 +02:00
break ;
}
2006-04-11 15:00:06 +02:00
2007-07-04 14:52:43 +02:00
case 0x10 : // Little/lots cargo threshold
2010-01-23 14:17:30 +01:00
statspec - > cargo_threshold = buf - > ReadWord ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2006-04-11 15:00:06 +02:00
2007-07-04 14:52:43 +02:00
case 0x11 : // Pylon placement
2010-01-23 14:17:30 +01:00
statspec - > pylons = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2006-04-11 15:00:06 +02:00
2007-07-04 14:52:43 +02:00
case 0x12 : // Cargo types for random triggers
2013-01-11 08:39:25 +01:00
if ( _cur . grffile - > grf_version > = 7 ) {
2018-05-21 23:08:39 +02:00
statspec - > cargo_triggers = TranslateRefitMask ( buf - > ReadDWord ( ) ) ;
} else {
statspec - > cargo_triggers = ( CargoTypes ) buf - > ReadDWord ( ) ;
2013-01-11 08:39:25 +01:00
}
2007-07-04 14:52:43 +02:00
break ;
2006-04-11 15:00:06 +02:00
2007-07-04 14:52:43 +02:00
case 0x13 : // General flags
2010-01-23 14:17:30 +01:00
statspec - > flags = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2006-04-11 15:00:06 +02:00
2007-07-04 14:52:43 +02:00
case 0x14 : // Overhead wire placement
2010-01-23 14:17:30 +01:00
statspec - > wires = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2006-04-11 15:00:06 +02:00
2007-07-04 14:52:43 +02:00
case 0x15 : // Blocked tiles
2010-01-23 14:17:30 +01:00
statspec - > blocked = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2006-04-11 15:00:06 +02:00
2008-04-20 01:19:12 +02:00
case 0x16 : // Animation info
2010-08-26 17:31:40 +02:00
statspec - > animation . frames = buf - > ReadByte ( ) ;
statspec - > animation . status = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2007-02-02 03:56:29 +01:00
2008-04-20 01:19:12 +02:00
case 0x17 : // Animation speed
2010-08-26 17:31:40 +02:00
statspec - > animation . speed = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2007-02-02 03:56:29 +01:00
2008-04-20 01:19:12 +02:00
case 0x18 : // Animation triggers
2010-08-26 17:31:40 +02:00
statspec - > animation . triggers = buf - > ReadWord ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2007-02-02 03:56:29 +01:00
2023-05-01 13:02:11 +02:00
/* 0x19 road routing (not implemented) */
2021-05-01 21:28:23 +02:00
case 0x1A : { // Advanced sprite layout
uint16 tiles = buf - > ReadExtendedByte ( ) ;
statspec - > renderdata . clear ( ) ; // delete earlier loaded stuff
statspec - > renderdata . reserve ( tiles ) ;
2011-05-29 18:56:22 +02:00
2021-05-01 21:28:23 +02:00
for ( uint t = 0 ; t < tiles ; t + + ) {
NewGRFSpriteLayout * dts = & statspec - > renderdata . emplace_back ( ) ;
2011-05-29 18:56:22 +02:00
uint num_building_sprites = buf - > ReadByte ( ) ;
/* On error, bail out immediately. Temporary GRF data was already freed */
2011-09-11 17:08:17 +02:00
if ( ReadSpriteLayout ( buf , num_building_sprites , false , GSF_STATIONS , true , false , dts ) ) return CIR_DISABLED ;
2011-05-29 18:56:22 +02:00
}
2022-09-10 23:08:30 +02:00
/* Number of layouts must be even, alternating X and Y */
if ( statspec - > renderdata . size ( ) & 1 ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " StationChangeInfo: Station {} defines an odd number of sprite layouts, dropping the last item " , stid + i ) ;
2022-09-10 23:08:30 +02:00
statspec - > renderdata . pop_back ( ) ;
}
2011-05-29 18:56:22 +02:00
break ;
2021-05-01 21:28:23 +02:00
}
2011-05-29 18:56:22 +02:00
2023-05-01 13:02:11 +02:00
case 0x1B : // Minimum bridge height (not implemented)
buf - > ReadWord ( ) ;
buf - > ReadWord ( ) ;
buf - > ReadWord ( ) ;
buf - > ReadWord ( ) ;
break ;
2023-04-18 01:07:57 +02:00
case 0x1C : // Station Name
AddStringForMapping ( buf - > ReadWord ( ) , & statspec - > name ) ;
break ;
case 0x1D : // Station Class name
AddStringForMapping ( buf - > ReadWord ( ) , & StationClass : : Get ( statspec - > cls_id ) - > name ) ;
break ;
2007-07-04 14:52:43 +02:00
default :
2008-08-31 18:41:27 +02:00
ret = CIR_UNKNOWN ;
2007-07-04 14:52:43 +02:00
break ;
}
2004-11-14 10:07:15 +01:00
}
return ret ;
}
2011-09-11 13:14:22 +02:00
/**
* Define properties for water features
* @ param id Type of the first water feature .
* @ param numinfo Number of subsequent water feature ids to change the property for .
* @ param prop The property to change .
* @ param buf The property value .
* @ return ChangeInfoResult .
*/
2010-01-22 14:02:15 +01:00
static ChangeInfoResult CanalChangeInfo ( uint id , int numinfo , int prop , ByteReader * buf )
2008-01-21 21:41:04 +01:00
{
2008-08-31 18:41:27 +02:00
ChangeInfoResult ret = CIR_SUCCESS ;
2008-01-21 21:41:04 +01:00
if ( id + numinfo > CF_END ) {
2023-04-20 17:00:46 +02:00
GrfMsg ( 1 , " CanalChangeInfo: Canal feature 0x{:02X} is invalid, max {}, ignoring " , id + numinfo , CF_END ) ;
2008-08-31 18:41:27 +02:00
return CIR_INVALID_ID ;
2008-01-21 21:41:04 +01:00
}
for ( int i = 0 ; i < numinfo ; i + + ) {
2011-09-11 11:31:57 +02:00
CanalProperties * cp = & _cur . grffile - > canal_local_properties [ id + i ] ;
2008-01-21 21:41:04 +01:00
switch ( prop ) {
case 0x08 :
2011-09-11 11:31:57 +02:00
cp - > callback_mask = buf - > ReadByte ( ) ;
2008-01-21 21:41:04 +01:00
break ;
case 0x09 :
2011-09-11 11:31:57 +02:00
cp - > flags = buf - > ReadByte ( ) ;
2008-01-21 21:41:04 +01:00
break ;
default :
2008-08-31 18:41:27 +02:00
ret = CIR_UNKNOWN ;
2008-01-21 21:41:04 +01:00
break ;
}
}
return ret ;
}
2011-09-11 13:14:22 +02:00
/**
* Define properties for bridges
* @ param brid BridgeID of the bridge .
* @ param numinfo Number of subsequent bridgeIDs to change the property for .
* @ param prop The property to change .
* @ param buf The property value .
* @ return ChangeInfoResult .
*/
2010-01-22 14:02:15 +01:00
static ChangeInfoResult BridgeChangeInfo ( uint brid , int numinfo , int prop , ByteReader * buf )
2005-10-01 19:38:48 +02:00
{
2008-08-31 18:41:27 +02:00
ChangeInfoResult ret = CIR_SUCCESS ;
2005-10-01 19:38:48 +02:00
2007-07-04 14:52:43 +02:00
if ( brid + numinfo > MAX_BRIDGES ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " BridgeChangeInfo: Bridge {} is invalid, max {}, ignoring " , brid + numinfo , MAX_BRIDGES ) ;
2008-08-31 18:41:27 +02:00
return CIR_INVALID_ID ;
2007-07-04 14:52:43 +02:00
}
for ( int i = 0 ; i < numinfo ; i + + ) {
2008-02-11 18:35:15 +01:00
BridgeSpec * bridge = & _bridge [ brid + i ] ;
2005-10-01 19:38:48 +02:00
2007-07-04 14:52:43 +02:00
switch ( prop ) {
2009-05-09 21:48:14 +02:00
case 0x08 : { // Year of availability
/* We treat '0' as always available */
2010-01-23 14:17:30 +01:00
byte year = buf - > ReadByte ( ) ;
2009-05-09 21:48:14 +02:00
bridge - > avail_year = ( year > 0 ? ORIGINAL_BASE_YEAR + year : 0 ) ;
2007-07-04 14:52:43 +02:00
break ;
2009-05-09 21:48:14 +02:00
}
2005-10-01 19:38:48 +02:00
2007-07-04 14:52:43 +02:00
case 0x09 : // Minimum length
2010-01-23 14:17:30 +01:00
bridge - > min_length = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2005-10-01 19:38:48 +02:00
2007-07-04 14:52:43 +02:00
case 0x0A : // Maximum length
2010-01-23 14:17:30 +01:00
bridge - > max_length = buf - > ReadByte ( ) ;
2023-01-16 18:51:34 +01:00
if ( bridge - > max_length > 16 ) bridge - > max_length = UINT16_MAX ;
2007-07-04 14:52:43 +02:00
break ;
2005-10-01 19:38:48 +02:00
2007-07-04 14:52:43 +02:00
case 0x0B : // Cost factor
2010-01-23 14:17:30 +01:00
bridge - > price = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2005-10-01 19:38:48 +02:00
2007-07-04 14:52:43 +02:00
case 0x0C : // Maximum speed
2010-01-23 14:17:30 +01:00
bridge - > speed = buf - > ReadWord ( ) ;
2023-01-16 18:51:34 +01:00
if ( bridge - > speed = = 0 ) bridge - > speed = UINT16_MAX ;
2007-07-04 14:52:43 +02:00
break ;
case 0x0D : { // Bridge sprite tables
2010-01-23 14:17:30 +01:00
byte tableid = buf - > ReadByte ( ) ;
byte numtables = buf - > ReadByte ( ) ;
2005-10-01 19:38:48 +02:00
2019-04-10 23:07:06 +02:00
if ( bridge - > sprite_table = = nullptr ) {
2006-04-15 18:03:45 +02:00
/* Allocate memory for sprite table pointers and zero out */
2007-01-11 18:29:39 +01:00
bridge - > sprite_table = CallocT < PalSpriteID * > ( 7 ) ;
2005-10-01 19:38:48 +02:00
}
2006-04-15 18:03:45 +02:00
for ( ; numtables - - ! = 0 ; tableid + + ) {
if ( tableid > = 7 ) { // skip invalid data
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " BridgeChangeInfo: Table {} >= 7, skipping " , tableid ) ;
2010-01-23 14:17:30 +01:00
for ( byte sprite = 0 ; sprite < 32 ; sprite + + ) buf - > ReadDWord ( ) ;
2006-04-15 18:03:45 +02:00
continue ;
2005-10-02 14:00:30 +02:00
}
2006-04-15 18:03:45 +02:00
2019-04-10 23:07:06 +02:00
if ( bridge - > sprite_table [ tableid ] = = nullptr ) {
2007-01-11 18:29:39 +01:00
bridge - > sprite_table [ tableid ] = MallocT < PalSpriteID > ( 32 ) ;
2006-04-15 18:03:45 +02:00
}
2007-03-10 10:55:11 +01:00
for ( byte sprite = 0 ; sprite < 32 ; sprite + + ) {
2010-01-23 14:17:30 +01:00
SpriteID image = buf - > ReadWord ( ) ;
PaletteID pal = buf - > ReadWord ( ) ;
2007-01-14 20:57:49 +01:00
bridge - > sprite_table [ tableid ] [ sprite ] . sprite = image ;
bridge - > sprite_table [ tableid ] [ sprite ] . pal = pal ;
2008-02-16 04:01:05 +01:00
MapSpriteMappingRecolour ( & bridge - > sprite_table [ tableid ] [ sprite ] ) ;
2007-01-14 20:57:49 +01:00
}
2005-10-01 19:38:48 +02:00
}
2010-08-01 20:53:30 +02:00
break ;
}
2005-10-01 19:38:48 +02:00
2007-07-04 14:52:43 +02:00
case 0x0E : // Flags; bit 0 - disable far pillars
2010-01-23 14:17:30 +01:00
bridge - > flags = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2005-10-01 19:38:48 +02:00
2007-07-04 14:52:43 +02:00
case 0x0F : // Long format year of availability (year since year 0)
2010-01-23 14:17:30 +01:00
bridge - > avail_year = Clamp ( buf - > ReadDWord ( ) , MIN_YEAR , MAX_YEAR ) ;
2007-07-04 14:52:43 +02:00
break ;
2006-11-17 12:03:38 +01:00
2008-02-06 04:01:29 +01:00
case 0x10 : { // purchase string
2011-08-21 14:05:47 +02:00
StringID newone = GetGRFStringID ( _cur . grffile - > grfid , buf - > ReadWord ( ) ) ;
2008-02-06 04:01:29 +01:00
if ( newone ! = STR_UNDEFINED ) bridge - > material = newone ;
2010-08-01 20:53:30 +02:00
break ;
}
2008-02-06 04:01:29 +01:00
2008-02-12 04:05:27 +01:00
case 0x11 : // description of bridge with rails or roads
case 0x12 : {
2011-08-21 14:05:47 +02:00
StringID newone = GetGRFStringID ( _cur . grffile - > grfid , buf - > ReadWord ( ) ) ;
2008-02-12 04:05:27 +01:00
if ( newone ! = STR_UNDEFINED ) bridge - > transport_name [ prop - 0x11 ] = newone ;
2010-08-01 20:53:30 +02:00
break ;
}
2008-02-06 04:01:29 +01:00
2008-08-25 19:16:27 +02:00
case 0x13 : // 16 bits cost multiplier
2010-01-23 14:17:30 +01:00
bridge - > price = buf - > ReadWord ( ) ;
2008-08-25 19:16:27 +02:00
break ;
2007-07-04 14:52:43 +02:00
default :
2008-08-31 18:41:27 +02:00
ret = CIR_UNKNOWN ;
2007-07-04 14:52:43 +02:00
break ;
}
2005-10-01 19:38:48 +02:00
}
return ret ;
}
2004-11-14 10:07:15 +01:00
2011-09-11 13:14:22 +02:00
/**
* Ignore a house property
* @ param prop Property to read .
* @ param buf Property value .
* @ return ChangeInfoResult .
*/
2010-04-02 23:30:37 +02:00
static ChangeInfoResult IgnoreTownHouseProperty ( int prop , ByteReader * buf )
{
ChangeInfoResult ret = CIR_SUCCESS ;
switch ( prop ) {
case 0x09 :
case 0x0B :
case 0x0C :
case 0x0D :
case 0x0E :
case 0x0F :
case 0x11 :
case 0x14 :
case 0x15 :
case 0x16 :
case 0x18 :
case 0x19 :
case 0x1A :
case 0x1B :
case 0x1C :
case 0x1D :
case 0x1F :
buf - > ReadByte ( ) ;
break ;
case 0x0A :
case 0x10 :
case 0x12 :
case 0x13 :
case 0x21 :
case 0x22 :
buf - > ReadWord ( ) ;
break ;
case 0x1E :
buf - > ReadDWord ( ) ;
break ;
case 0x17 :
for ( uint j = 0 ; j < 4 ; j + + ) buf - > ReadByte ( ) ;
break ;
case 0x20 : {
byte count = buf - > ReadByte ( ) ;
for ( byte j = 0 ; j < count ; j + + ) buf - > ReadByte ( ) ;
2010-08-01 20:53:30 +02:00
break ;
}
2010-04-02 23:30:37 +02:00
2018-07-28 23:28:24 +02:00
case 0x23 :
buf - > Skip ( buf - > ReadByte ( ) * 2 ) ;
break ;
2010-04-02 23:30:37 +02:00
default :
ret = CIR_UNKNOWN ;
break ;
}
return ret ;
}
2011-09-11 13:14:22 +02:00
/**
* Define properties for houses
* @ param hid HouseID of the house .
* @ param numinfo Number of subsequent houseIDs to change the property for .
* @ param prop The property to change .
* @ param buf The property value .
* @ return ChangeInfoResult .
*/
2010-01-22 14:02:15 +01:00
static ChangeInfoResult TownHouseChangeInfo ( uint hid , int numinfo , int prop , ByteReader * buf )
2007-03-19 12:27:30 +01:00
{
2008-08-31 18:41:27 +02:00
ChangeInfoResult ret = CIR_SUCCESS ;
2007-03-19 12:27:30 +01:00
2013-10-12 18:34:04 +02:00
if ( hid + numinfo > NUM_HOUSES_PER_GRF ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " TownHouseChangeInfo: Too many houses loaded ({}), max ({}). Ignoring. " , hid + numinfo , NUM_HOUSES_PER_GRF ) ;
2008-08-31 18:41:27 +02:00
return CIR_INVALID_ID ;
2007-03-19 12:27:30 +01:00
}
/* Allocate house specs if they haven't been allocated already. */
2023-01-08 03:12:33 +01:00
if ( _cur . grffile - > housespec . size ( ) < hid + numinfo ) _cur . grffile - > housespec . resize ( hid + numinfo ) ;
2007-03-19 12:27:30 +01:00
2007-07-04 14:52:43 +02:00
for ( int i = 0 ; i < numinfo ; i + + ) {
2023-04-15 22:30:37 +02:00
HouseSpec * housespec = _cur . grffile - > housespec [ hid + i ] . get ( ) ;
2007-03-19 12:27:30 +01:00
2019-04-10 23:07:06 +02:00
if ( prop ! = 0x08 & & housespec = = nullptr ) {
2010-04-02 23:30:37 +02:00
/* If the house property 08 is not yet set, ignore this property */
ChangeInfoResult cir = IgnoreTownHouseProperty ( prop , buf ) ;
if ( cir > ret ) ret = cir ;
continue ;
2007-03-19 12:27:30 +01:00
}
2007-07-04 14:52:43 +02:00
switch ( prop ) {
case 0x08 : { // Substitute building type, and definition of a new house
2010-01-23 14:17:30 +01:00
byte subs_id = buf - > ReadByte ( ) ;
2007-03-19 12:27:30 +01:00
if ( subs_id = = 0xFF ) {
/* Instead of defining a new house, a substitute house id
* of 0xFF disables the old house with the current id . */
2023-04-15 22:30:37 +02:00
if ( hid + i < NEW_HOUSE_OFFSET ) HouseSpec : : Get ( hid + i ) - > enabled = false ;
2007-03-19 12:27:30 +01:00
continue ;
} else if ( subs_id > = NEW_HOUSE_OFFSET ) {
/* The substitute id must be one of the original houses. */
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " TownHouseChangeInfo: Attempt to use new house {} as substitute house for {}. Ignoring. " , subs_id , hid + i ) ;
2008-08-31 17:26:46 +02:00
continue ;
2007-03-19 12:27:30 +01:00
}
/* Allocate space for this house. */
2023-04-15 22:30:37 +02:00
if ( housespec = = nullptr ) {
2023-04-17 01:03:39 +02:00
/* Only the first property 08 setting copies properties; if you later change it, properties will stay. */
2023-04-15 22:30:37 +02:00
_cur . grffile - > housespec [ hid + i ] = std : : make_unique < HouseSpec > ( * HouseSpec : : Get ( subs_id ) ) ;
housespec = _cur . grffile - > housespec [ hid + i ] . get ( ) ;
2007-07-04 14:52:43 +02:00
2023-04-17 01:03:39 +02:00
housespec - > enabled = true ;
housespec - > grf_prop . local_id = hid + i ;
housespec - > grf_prop . subst_id = subs_id ;
housespec - > grf_prop . grffile = _cur . grffile ;
housespec - > random_colour [ 0 ] = 0x04 ; // those 4 random colours are the base colour
housespec - > random_colour [ 1 ] = 0x08 ; // for all new houses
housespec - > random_colour [ 2 ] = 0x0C ; // they stand for red, blue, orange and green
housespec - > random_colour [ 3 ] = 0x06 ;
2023-04-17 09:03:37 +02:00
/* House flags 40 and 80 are exceptions; these flags are never set automatically. */
housespec - > building_flags & = ~ ( BUILDING_IS_CHURCH | BUILDING_IS_STADIUM ) ;
2023-04-17 01:03:39 +02:00
/* Make sure that the third cargo type is valid in this
* climate . This can cause problems when copying the properties
* of a house that accepts food , where the new house is valid
* in the temperate climate . */
if ( ! CargoSpec : : Get ( housespec - > accepts_cargo [ 2 ] ) - > IsValid ( ) ) {
housespec - > cargo_acceptance [ 2 ] = 0 ;
}
2007-08-10 10:57:04 +02:00
}
2010-08-01 20:53:30 +02:00
break ;
}
2007-07-04 14:52:43 +02:00
case 0x09 : // Building flags
2010-01-23 14:17:30 +01:00
housespec - > building_flags = ( BuildingFlags ) buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
case 0x0A : { // Availability years
2010-01-23 14:17:30 +01:00
uint16 years = buf - > ReadWord ( ) ;
2008-06-03 05:06:16 +02:00
housespec - > min_year = GB ( years , 0 , 8 ) > 150 ? MAX_YEAR : ORIGINAL_BASE_YEAR + GB ( years , 0 , 8 ) ;
housespec - > max_year = GB ( years , 8 , 8 ) > 150 ? MAX_YEAR : ORIGINAL_BASE_YEAR + GB ( years , 8 , 8 ) ;
2010-08-01 20:53:30 +02:00
break ;
}
2007-07-04 14:52:43 +02:00
case 0x0B : // Population
2010-01-23 14:17:30 +01:00
housespec - > population = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
case 0x0C : // Mail generation multiplier
2010-01-23 14:17:30 +01:00
housespec - > mail_generation = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
case 0x0D : // Passenger acceptance
case 0x0E : // Mail acceptance
2010-01-23 14:17:30 +01:00
housespec - > cargo_acceptance [ prop - 0x0D ] = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
case 0x0F : { // Goods/candy, food/fizzy drinks acceptance
2010-01-23 14:17:30 +01:00
int8 goods = buf - > ReadByte ( ) ;
2007-03-20 03:24:14 +01:00
/* If value of goods is negative, it means in fact food or, if in toyland, fizzy_drink acceptance.
* Else , we have " standard " 3 rd cargo type , goods or candy , for toyland once more */
2008-05-29 17:13:28 +02:00
CargoID cid = ( goods > = 0 ) ? ( ( _settings_game . game_creation . landscape = = LT_TOYLAND ) ? CT_CANDY : CT_GOODS ) :
( ( _settings_game . game_creation . landscape = = LT_TOYLAND ) ? CT_FIZZY_DRINKS : CT_FOOD ) ;
2007-03-20 03:24:14 +01:00
2007-08-10 10:57:04 +02:00
/* Make sure the cargo type is valid in this climate. */
2009-07-16 21:00:13 +02:00
if ( ! CargoSpec : : Get ( cid ) - > IsValid ( ) ) goods = 0 ;
2007-08-10 10:57:04 +02:00
housespec - > accepts_cargo [ 2 ] = cid ;
2007-07-04 14:52:43 +02:00
housespec - > cargo_acceptance [ 2 ] = abs ( goods ) ; // but we do need positive value here
2010-08-01 20:53:30 +02:00
break ;
}
2007-03-19 12:27:30 +01:00
2007-07-04 14:52:43 +02:00
case 0x10 : // Local authority rating decrease on removal
2010-01-23 14:17:30 +01:00
housespec - > remove_rating_decrease = buf - > ReadWord ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2007-03-19 12:27:30 +01:00
2007-07-04 14:52:43 +02:00
case 0x11 : // Removal cost multiplier
2010-01-23 14:17:30 +01:00
housespec - > removal_cost = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2007-03-19 12:27:30 +01:00
2007-07-04 14:52:43 +02:00
case 0x12 : // Building name ID
2014-01-12 19:01:50 +01:00
AddStringForMapping ( buf - > ReadWord ( ) , & housespec - > building_name ) ;
2007-07-04 14:52:43 +02:00
break ;
2007-03-19 12:27:30 +01:00
2007-07-04 14:52:43 +02:00
case 0x13 : // Building availability mask
2010-01-23 14:17:30 +01:00
housespec - > building_availability = ( HouseZones ) buf - > ReadWord ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2007-03-19 12:27:30 +01:00
2009-09-14 14:22:57 +02:00
case 0x14 : // House callback mask
2010-01-23 14:17:30 +01:00
housespec - > callback_mask | = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2007-03-19 12:27:30 +01:00
2007-07-04 14:52:43 +02:00
case 0x15 : { // House override byte
2010-01-23 14:17:30 +01:00
byte override = buf - > ReadByte ( ) ;
2007-03-19 12:27:30 +01:00
/* The house being overridden must be an original house. */
if ( override > = NEW_HOUSE_OFFSET ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " TownHouseChangeInfo: Attempt to override new house {} with house id {}. Ignoring. " , override , hid + i ) ;
2007-07-04 14:52:43 +02:00
continue ;
2007-03-19 12:27:30 +01:00
}
2011-08-21 14:05:47 +02:00
_house_mngr . Add ( hid + i , _cur . grffile - > grfid , override ) ;
2010-08-01 20:53:30 +02:00
break ;
}
2007-07-04 14:52:43 +02:00
case 0x16 : // Periodic refresh multiplier
2021-01-08 11:16:18 +01:00
housespec - > processing_time = std : : min < byte > ( buf - > ReadByte ( ) , 63u ) ;
2007-07-04 14:52:43 +02:00
break ;
case 0x17 : // Four random colours to use
2010-01-23 14:17:30 +01:00
for ( uint j = 0 ; j < 4 ; j + + ) housespec - > random_colour [ j ] = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
case 0x18 : // Relative probability of appearing
2010-01-23 14:17:30 +01:00
housespec - > probability = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
case 0x19 : // Extra flags
2010-01-23 14:17:30 +01:00
housespec - > extra_flags = ( HouseExtraFlags ) buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
case 0x1A : // Animation frames
2010-08-26 17:31:40 +02:00
housespec - > animation . frames = buf - > ReadByte ( ) ;
housespec - > animation . status = GB ( housespec - > animation . frames , 7 , 1 ) ;
SB ( housespec - > animation . frames , 7 , 1 , 0 ) ;
2007-07-04 14:52:43 +02:00
break ;
case 0x1B : // Animation speed
2010-08-26 17:31:40 +02:00
housespec - > animation . speed = Clamp ( buf - > ReadByte ( ) , 2 , 16 ) ;
2007-07-04 14:52:43 +02:00
break ;
case 0x1C : // Class of the building type
2011-08-21 14:05:47 +02:00
housespec - > class_id = AllocateHouseClassID ( buf - > ReadByte ( ) , _cur . grffile - > grfid ) ;
2007-07-04 14:52:43 +02:00
break ;
2009-09-14 14:22:57 +02:00
case 0x1D : // Callback mask part 2
2010-01-23 14:17:30 +01:00
housespec - > callback_mask | = ( buf - > ReadByte ( ) < < 8 ) ;
2007-07-04 14:52:43 +02:00
break ;
case 0x1E : { // Accepted cargo types
2010-01-23 14:17:30 +01:00
uint32 cargotypes = buf - > ReadDWord ( ) ;
2007-04-13 23:18:48 +02:00
/* Check if the cargo types should not be changed */
if ( cargotypes = = 0xFFFFFFFF ) break ;
for ( uint j = 0 ; j < 3 ; j + + ) {
/* Get the cargo number from the 'list' */
uint8 cargo_part = GB ( cargotypes , 8 * j , 8 ) ;
2011-08-21 14:05:47 +02:00
CargoID cargo = GetCargoTranslation ( cargo_part , _cur . grffile ) ;
2007-04-13 23:18:48 +02:00
2023-05-04 12:29:21 +02:00
if ( ! IsValidCargoID ( cargo ) ) {
2007-04-13 23:18:48 +02:00
/* Disable acceptance of invalid cargo type */
2007-07-04 14:52:43 +02:00
housespec - > cargo_acceptance [ j ] = 0 ;
2007-04-13 23:18:48 +02:00
} else {
2007-07-04 14:52:43 +02:00
housespec - > accepts_cargo [ j ] = cargo ;
2007-04-13 23:18:48 +02:00
}
}
2010-08-01 20:53:30 +02:00
break ;
}
2007-03-19 12:27:30 +01:00
2007-07-04 14:52:43 +02:00
case 0x1F : // Minimum life span
2010-01-23 14:17:30 +01:00
housespec - > minimum_life = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2007-05-10 18:03:06 +02:00
2011-10-31 23:31:40 +01:00
case 0x20 : { // Cargo acceptance watch list
2010-01-23 14:17:30 +01:00
byte count = buf - > ReadByte ( ) ;
2011-10-31 23:31:40 +01:00
for ( byte j = 0 ; j < count ; j + + ) {
CargoID cargo = GetCargoTranslation ( buf - > ReadByte ( ) , _cur . grffile ) ;
2023-05-04 12:29:21 +02:00
if ( IsValidCargoID ( cargo ) ) SetBit ( housespec - > watched_cargoes , cargo ) ;
2011-10-31 23:31:40 +01:00
}
2010-08-01 20:53:30 +02:00
break ;
}
2007-10-26 01:24:28 +02:00
2008-06-10 02:42:19 +02:00
case 0x21 : // long introduction year
2010-01-23 14:17:30 +01:00
housespec - > min_year = buf - > ReadWord ( ) ;
2008-06-10 02:42:19 +02:00
break ;
case 0x22 : // long maximum year
2010-01-23 14:17:30 +01:00
housespec - > max_year = buf - > ReadWord ( ) ;
2008-06-10 02:42:19 +02:00
break ;
2018-07-28 23:28:24 +02:00
case 0x23 : { // variable length cargo types accepted
uint count = buf - > ReadByte ( ) ;
if ( count > lengthof ( housespec - > accepts_cargo ) ) {
GRFError * error = DisableGrf ( STR_NEWGRF_ERROR_LIST_PROPERTY_TOO_LONG ) ;
error - > param_value [ 1 ] = prop ;
return CIR_DISABLED ;
}
/* Always write the full accepts_cargo array, and check each index for being inside the
* provided data . This ensures all values are properly initialized , and also avoids
* any risks of array overrun . */
for ( uint i = 0 ; i < lengthof ( housespec - > accepts_cargo ) ; i + + ) {
if ( i < count ) {
housespec - > accepts_cargo [ i ] = GetCargoTranslation ( buf - > ReadByte ( ) , _cur . grffile ) ;
housespec - > cargo_acceptance [ i ] = buf - > ReadByte ( ) ;
} else {
housespec - > accepts_cargo [ i ] = CT_INVALID ;
housespec - > cargo_acceptance [ i ] = 0 ;
}
}
break ;
}
2007-07-04 14:52:43 +02:00
default :
2008-08-31 18:41:27 +02:00
ret = CIR_UNKNOWN ;
2007-07-04 14:52:43 +02:00
break ;
}
2007-03-19 12:27:30 +01:00
}
return ret ;
}
2010-11-16 15:00:02 +01:00
/**
* Get the language map associated with a given NewGRF and language .
* @ param grfid The NewGRF to get the map for .
* @ param language_id The ( NewGRF ) language ID to get the map for .
2019-04-10 23:07:06 +02:00
* @ return The LanguageMap , or nullptr if it couldn ' t be found .
2010-11-16 15:00:02 +01:00
*/
/* static */ const LanguageMap * LanguageMap : : GetLanguageMap ( uint32 grfid , uint8 language_id )
{
/* LanguageID "MAX_LANG", i.e. 7F is any. This language can't have a gender/case mapping, but has to be handled gracefully. */
const GRFFile * grffile = GetFileByGRFID ( grfid ) ;
2019-04-10 23:07:06 +02:00
return ( grffile ! = nullptr & & grffile - > language_map ! = nullptr & & language_id < MAX_LANG ) ? & grffile - > language_map [ language_id ] : nullptr ;
2010-11-16 15:00:02 +01:00
}
2012-05-25 23:00:00 +02:00
/**
* Load a cargo - or railtype - translation table .
* @ param gvid ID of the global variable . This is basically only checked for zerones .
* @ param numinfo Number of subsequent IDs to change the property for .
* @ param buf The property value .
2018-10-28 03:17:36 +01:00
* @ param [ in , out ] translation_table Storage location for the translation table .
2012-05-25 23:00:00 +02:00
* @ param name Name of the table for debug output .
* @ return ChangeInfoResult .
*/
template < typename T >
static ChangeInfoResult LoadTranslationTable ( uint gvid , int numinfo , ByteReader * buf , T & translation_table , const char * name )
{
if ( gvid ! = 0 ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " LoadTranslationTable: {} translation table must start at zero " , name ) ;
2012-05-25 23:00:00 +02:00
return CIR_INVALID_ID ;
}
2018-09-21 00:44:14 +02:00
translation_table . clear ( ) ;
2012-05-25 23:00:00 +02:00
for ( int i = 0 ; i < numinfo ; i + + ) {
uint32 item = buf - > ReadDWord ( ) ;
2019-02-18 23:39:06 +01:00
translation_table . push_back ( BSWAP32 ( item ) ) ;
2012-05-25 23:00:00 +02:00
}
return CIR_SUCCESS ;
}
2021-04-28 16:46:24 +02:00
/**
* Helper to read a DWord worth of bytes from the reader
* and to return it as a valid string .
* @ param reader The source of the DWord .
* @ return The read DWord as string .
*/
static std : : string ReadDWordAsString ( ByteReader * reader )
{
char output [ 5 ] ;
for ( int i = 0 ; i < 4 ; i + + ) output [ i ] = reader - > ReadByte ( ) ;
output [ 4 ] = ' \0 ' ;
2021-05-29 11:21:38 +02:00
StrMakeValidInPlace ( output , lastof ( output ) ) ;
2021-04-28 16:46:24 +02:00
return std : : string ( output ) ;
}
2011-09-11 13:14:22 +02:00
/**
* Define properties for global variables
* @ param gvid ID of the global variable .
* @ param numinfo Number of subsequent IDs to change the property for .
* @ param prop The property to change .
* @ param buf The property value .
* @ return ChangeInfoResult .
*/
2010-01-22 14:02:15 +01:00
static ChangeInfoResult GlobalVarChangeInfo ( uint gvid , int numinfo , int prop , ByteReader * buf )
2005-10-12 09:27:56 +02:00
{
2012-05-25 23:00:00 +02:00
/* Properties which are handled as a whole */
switch ( prop ) {
case 0x09 : // Cargo Translation Table; loading during both reservation and activation stage (in case it is selected depending on defined cargos)
return LoadTranslationTable ( gvid , numinfo , buf , _cur . grffile - > cargo_list , " Cargo " ) ;
case 0x12 : // Rail type translation table; loading during both reservation and activation stage (in case it is selected depending on defined railtypes)
return LoadTranslationTable ( gvid , numinfo , buf , _cur . grffile - > railtype_list , " Rail type " ) ;
2019-04-06 08:46:15 +02:00
case 0x16 : // Road type translation table; loading during both reservation and activation stage (in case it is selected depending on defined railtypes)
return LoadTranslationTable ( gvid , numinfo , buf , _cur . grffile - > roadtype_list , " Road type " ) ;
case 0x17 : // Tram type translation table; loading during both reservation and activation stage (in case it is selected depending on defined railtypes)
return LoadTranslationTable ( gvid , numinfo , buf , _cur . grffile - > tramtype_list , " Tram type " ) ;
2012-05-25 23:00:00 +02:00
default :
break ;
}
2005-10-12 09:27:56 +02:00
2012-05-25 23:00:00 +02:00
/* Properties which are handled per item */
ChangeInfoResult ret = CIR_SUCCESS ;
2007-07-04 14:52:43 +02:00
for ( int i = 0 ; i < numinfo ; i + + ) {
switch ( prop ) {
2007-07-04 15:30:33 +02:00
case 0x08 : { // Cost base factor
2010-01-23 14:17:30 +01:00
int factor = buf - > ReadByte ( ) ;
2005-10-12 11:54:29 +02:00
uint price = gvid + i ;
2005-10-12 09:27:56 +02:00
2009-11-08 13:23:02 +01:00
if ( price < PR_END ) {
2021-01-08 11:16:18 +01:00
_cur . grffile - > price_base_multipliers [ price ] = std : : min < int > ( factor - 8 , MAX_PRICE_MODIFIER ) ;
2005-10-12 11:54:29 +02:00
} else {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " GlobalVarChangeInfo: Price {} out of range, ignoring " , price ) ;
2005-10-12 11:54:29 +02:00
}
2010-08-01 20:53:30 +02:00
break ;
}
2006-04-11 12:53:07 +02:00
2007-07-04 14:52:43 +02:00
case 0x0A : { // Currency display names
2006-09-20 02:34:06 +02:00
uint curidx = GetNewgrfCurrencyIdConverted ( gvid + i ) ;
2011-08-21 14:05:47 +02:00
StringID newone = GetGRFStringID ( _cur . grffile - > grfid , buf - > ReadWord ( ) ) ;
2006-08-25 02:41:10 +02:00
2013-03-09 17:16:17 +01:00
if ( ( newone ! = STR_UNDEFINED ) & & ( curidx < CURRENCY_END ) ) {
2006-09-01 04:56:02 +02:00
_currency_specs [ curidx ] . name = newone ;
2023-06-23 01:11:11 +02:00
_currency_specs [ curidx ] . code . clear ( ) ;
2006-08-25 02:41:10 +02:00
}
2010-08-01 20:53:30 +02:00
break ;
}
2006-08-25 02:41:10 +02:00
2007-07-04 14:52:43 +02:00
case 0x0B : { // Currency multipliers
2006-09-20 02:34:06 +02:00
uint curidx = GetNewgrfCurrencyIdConverted ( gvid + i ) ;
2010-01-23 14:17:30 +01:00
uint32 rate = buf - > ReadDWord ( ) ;
2006-08-25 02:41:10 +02:00
2013-03-09 17:16:17 +01:00
if ( curidx < CURRENCY_END ) {
2006-09-15 04:52:17 +02:00
/* TTDPatch uses a multiple of 1000 for its conversion calculations,
* which OTTD does not . For this reason , divide grf value by 1000 ,
* to be compatible */
_currency_specs [ curidx ] . rate = rate / 1000 ;
2006-08-25 02:41:10 +02:00
} else {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " GlobalVarChangeInfo: Currency multipliers {} out of range, ignoring " , curidx ) ;
2006-08-25 02:41:10 +02:00
}
2010-08-01 20:53:30 +02:00
break ;
}
2006-08-25 02:41:10 +02:00
2007-07-04 14:52:43 +02:00
case 0x0C : { // Currency options
2006-09-20 02:34:06 +02:00
uint curidx = GetNewgrfCurrencyIdConverted ( gvid + i ) ;
2010-01-23 14:17:30 +01:00
uint16 options = buf - > ReadWord ( ) ;
2006-08-25 02:41:10 +02:00
2013-03-09 17:16:17 +01:00
if ( curidx < CURRENCY_END ) {
2021-11-06 18:50:28 +01:00
_currency_specs [ curidx ] . separator . clear ( ) ;
_currency_specs [ curidx ] . separator . push_back ( GB ( options , 0 , 8 ) ) ;
2006-09-15 04:52:17 +02:00
/* By specifying only one bit, we prevent errors,
* since newgrf specs said that only 0 and 1 can be set for symbol_pos */
_currency_specs [ curidx ] . symbol_pos = GB ( options , 8 , 1 ) ;
2006-08-25 02:41:10 +02:00
} else {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " GlobalVarChangeInfo: Currency option {} out of range, ignoring " , curidx ) ;
2006-08-25 02:41:10 +02:00
}
2010-08-01 20:53:30 +02:00
break ;
}
2006-08-25 02:41:10 +02:00
2007-07-04 14:52:43 +02:00
case 0x0D : { // Currency prefix symbol
2006-09-20 02:34:06 +02:00
uint curidx = GetNewgrfCurrencyIdConverted ( gvid + i ) ;
2021-04-28 16:46:24 +02:00
std : : string prefix = ReadDWordAsString ( buf ) ;
2006-08-25 02:41:10 +02:00
2013-03-09 17:16:17 +01:00
if ( curidx < CURRENCY_END ) {
2021-04-28 16:46:24 +02:00
_currency_specs [ curidx ] . prefix = prefix ;
2006-08-25 02:41:10 +02:00
} else {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " GlobalVarChangeInfo: Currency symbol {} out of range, ignoring " , curidx ) ;
2006-08-25 02:41:10 +02:00
}
2010-08-01 20:53:30 +02:00
break ;
}
2006-08-25 02:41:10 +02:00
2007-07-04 14:52:43 +02:00
case 0x0E : { // Currency suffix symbol
2006-09-20 02:34:06 +02:00
uint curidx = GetNewgrfCurrencyIdConverted ( gvid + i ) ;
2021-04-28 16:46:24 +02:00
std : : string suffix = ReadDWordAsString ( buf ) ;
2006-08-25 02:41:10 +02:00
2013-03-09 17:16:17 +01:00
if ( curidx < CURRENCY_END ) {
2021-04-28 16:46:24 +02:00
_currency_specs [ curidx ] . suffix = suffix ;
2006-08-25 02:41:10 +02:00
} else {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " GlobalVarChangeInfo: Currency symbol {} out of range, ignoring " , curidx ) ;
2006-08-25 02:41:10 +02:00
}
2010-08-01 20:53:30 +02:00
break ;
}
2006-08-25 02:41:10 +02:00
2007-07-04 14:52:43 +02:00
case 0x0F : { // Euro introduction dates
2006-09-20 02:34:06 +02:00
uint curidx = GetNewgrfCurrencyIdConverted ( gvid + i ) ;
2023-04-24 20:33:18 +02:00
TimerGameCalendar : : Year year_euro = buf - > ReadWord ( ) ;
2006-08-25 02:41:10 +02:00
2013-03-09 17:16:17 +01:00
if ( curidx < CURRENCY_END ) {
2006-08-25 02:41:10 +02:00
_currency_specs [ curidx ] . to_euro = year_euro ;
} else {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " GlobalVarChangeInfo: Euro intro date {} out of range, ignoring " , curidx ) ;
2006-08-25 02:41:10 +02:00
}
2010-08-01 20:53:30 +02:00
break ;
}
2007-07-04 14:52:43 +02:00
2007-07-04 15:30:33 +02:00
case 0x10 : // Snow line height table
2007-07-04 14:52:43 +02:00
if ( numinfo > 1 | | IsSnowLineSet ( ) ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " GlobalVarChangeInfo: The snowline can only be set once ({}) " , numinfo ) ;
2010-01-22 14:02:15 +01:00
} else if ( buf - > Remaining ( ) < SNOW_LINE_MONTHS * SNOW_LINE_DAYS ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " GlobalVarChangeInfo: Not enough entries set in the snowline table ({}) " , buf - > Remaining ( ) ) ;
2007-07-04 14:52:43 +02:00
} else {
byte table [ SNOW_LINE_MONTHS ] [ SNOW_LINE_DAYS ] ;
for ( uint i = 0 ; i < SNOW_LINE_MONTHS ; i + + ) {
for ( uint j = 0 ; j < SNOW_LINE_DAYS ; j + + ) {
2010-01-23 14:17:30 +01:00
table [ i ] [ j ] = buf - > ReadByte ( ) ;
2011-11-08 18:28:42 +01:00
if ( _cur . grffile - > grf_version > = 8 ) {
2021-03-24 09:42:54 +01:00
if ( table [ i ] [ j ] ! = 0xFF ) table [ i ] [ j ] = table [ i ] [ j ] * ( 1 + _settings_game . construction . map_height_limit ) / 256 ;
2011-11-08 18:28:42 +01:00
} else {
if ( table [ i ] [ j ] > = 128 ) {
/* no snow */
table [ i ] [ j ] = 0xFF ;
} else {
2021-03-24 09:42:54 +01:00
table [ i ] [ j ] = table [ i ] [ j ] * ( 1 + _settings_game . construction . map_height_limit ) / 128 ;
2011-11-08 18:28:42 +01:00
}
}
2007-07-04 14:52:43 +02:00
}
2007-03-20 14:47:00 +01:00
}
2007-07-04 14:52:43 +02:00
SetSnowLine ( table ) ;
2007-03-20 14:47:00 +01:00
}
2007-07-04 14:52:43 +02:00
break ;
2007-03-20 14:47:00 +01:00
2008-04-29 23:31:29 +02:00
case 0x11 : // GRF match for engine allocation
/* This is loaded during the reservation stage, so just skip it here. */
/* Each entry is 8 bytes. */
2010-01-22 14:02:15 +01:00
buf - > Skip ( 8 ) ;
2008-04-29 23:31:29 +02:00
break ;
2010-11-16 13:47:22 +01:00
case 0x13 : // Gender translation table
2010-11-16 22:03:39 +01:00
case 0x14 : // Case translation table
case 0x15 : { // Plural form translation
2010-11-16 13:47:22 +01:00
uint curidx = gvid + i ; // The current index, i.e. language.
2019-04-10 23:07:06 +02:00
const LanguageMetadata * lang = curidx < MAX_LANG ? GetLanguage ( curidx ) : nullptr ;
if ( lang = = nullptr ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " GlobalVarChangeInfo: Language {} is not known, ignoring " , curidx ) ;
2010-11-16 13:47:22 +01:00
/* Skip over the data. */
2012-01-12 18:44:20 +01:00
if ( prop = = 0x15 ) {
buf - > ReadByte ( ) ;
} else {
while ( buf - > ReadByte ( ) ! = 0 ) {
buf - > ReadString ( ) ;
}
2010-11-16 13:47:22 +01:00
}
break ;
}
2019-04-10 23:07:06 +02:00
if ( _cur . grffile - > language_map = = nullptr ) _cur . grffile - > language_map = new LanguageMap [ MAX_LANG ] ;
2010-11-16 13:47:22 +01:00
2010-11-16 22:03:39 +01:00
if ( prop = = 0x15 ) {
uint plural_form = buf - > ReadByte ( ) ;
if ( plural_form > = LANGUAGE_MAX_PLURAL ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " GlobalVarChanceInfo: Plural form {} is out of range, ignoring " , plural_form ) ;
2010-11-16 22:03:39 +01:00
} else {
2011-08-21 14:05:47 +02:00
_cur . grffile - > language_map [ curidx ] . plural_form = plural_form ;
2010-11-16 22:03:39 +01:00
}
break ;
}
2010-11-16 13:47:22 +01:00
byte newgrf_id = buf - > ReadByte ( ) ; // The NewGRF (custom) identifier.
while ( newgrf_id ! = 0 ) {
const char * name = buf - > ReadString ( ) ; // The name for the OpenTTD identifier.
/* We'll just ignore the UTF8 identifier character. This is (fairly)
* safe as OpenTTD ' s strings gender / cases are usually in ASCII which
* is just a subset of UTF8 , or they need the bigger UTF8 characters
* such as Cyrillic . Thus we will simply assume they ' re all UTF8 . */
WChar c ;
size_t len = Utf8Decode ( & c , name ) ;
if ( c = = NFO_UTF8_IDENTIFIER ) name + = len ;
LanguageMap : : Mapping map ;
map . newgrf_id = newgrf_id ;
if ( prop = = 0x13 ) {
map . openttd_id = lang - > GetGenderIndex ( name ) ;
if ( map . openttd_id > = MAX_NUM_GENDERS ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " GlobalVarChangeInfo: Gender name {} is not known, ignoring " , name ) ;
2010-11-16 13:47:22 +01:00
} else {
2019-02-18 23:39:06 +01:00
_cur . grffile - > language_map [ curidx ] . gender_map . push_back ( map ) ;
2010-11-16 13:47:22 +01:00
}
} else {
map . openttd_id = lang - > GetCaseIndex ( name ) ;
if ( map . openttd_id > = MAX_NUM_CASES ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " GlobalVarChangeInfo: Case name {} is not known, ignoring " , name ) ;
2010-11-16 13:47:22 +01:00
} else {
2019-02-18 23:39:06 +01:00
_cur . grffile - > language_map [ curidx ] . case_map . push_back ( map ) ;
2010-11-16 13:47:22 +01:00
}
}
newgrf_id = buf - > ReadByte ( ) ;
}
break ;
}
2007-07-04 14:52:43 +02:00
default :
2008-08-31 18:41:27 +02:00
ret = CIR_UNKNOWN ;
2007-07-04 14:52:43 +02:00
break ;
}
2005-10-12 09:27:56 +02:00
}
2006-04-11 12:53:07 +02:00
2005-10-12 09:27:56 +02:00
return ret ;
}
2010-01-22 14:02:15 +01:00
static ChangeInfoResult GlobalVarReserveInfo ( uint gvid , int numinfo , int prop , ByteReader * buf )
2009-01-09 21:14:07 +01:00
{
2012-05-25 23:00:00 +02:00
/* Properties which are handled as a whole */
switch ( prop ) {
case 0x09 : // Cargo Translation Table; loading during both reservation and activation stage (in case it is selected depending on defined cargos)
return LoadTranslationTable ( gvid , numinfo , buf , _cur . grffile - > cargo_list , " Cargo " ) ;
case 0x12 : // Rail type translation table; loading during both reservation and activation stage (in case it is selected depending on defined railtypes)
return LoadTranslationTable ( gvid , numinfo , buf , _cur . grffile - > railtype_list , " Rail type " ) ;
2009-01-09 21:14:07 +01:00
2019-04-06 08:46:15 +02:00
case 0x16 : // Road type translation table; loading during both reservation and activation stage (in case it is selected depending on defined roadtypes)
return LoadTranslationTable ( gvid , numinfo , buf , _cur . grffile - > roadtype_list , " Road type " ) ;
case 0x17 : // Tram type translation table; loading during both reservation and activation stage (in case it is selected depending on defined tramtypes)
return LoadTranslationTable ( gvid , numinfo , buf , _cur . grffile - > tramtype_list , " Tram type " ) ;
2012-05-25 23:00:00 +02:00
default :
break ;
}
/* Properties which are handled per item */
ChangeInfoResult ret = CIR_SUCCESS ;
2009-01-09 21:14:07 +01:00
for ( int i = 0 ; i < numinfo ; i + + ) {
switch ( prop ) {
case 0x08 : // Cost base factor
2010-11-16 22:03:39 +01:00
case 0x15 : // Plural form translation
2010-01-23 14:17:30 +01:00
buf - > ReadByte ( ) ;
2009-01-09 21:14:07 +01:00
break ;
case 0x0A : // Currency display names
case 0x0C : // Currency options
case 0x0F : // Euro introduction dates
2010-01-23 14:17:30 +01:00
buf - > ReadWord ( ) ;
2009-01-09 21:14:07 +01:00
break ;
case 0x0B : // Currency multipliers
case 0x0D : // Currency prefix symbol
case 0x0E : // Currency suffix symbol
2010-01-23 14:17:30 +01:00
buf - > ReadDWord ( ) ;
2009-01-09 21:14:07 +01:00
break ;
case 0x10 : // Snow line height table
2010-01-22 14:02:15 +01:00
buf - > Skip ( SNOW_LINE_MONTHS * SNOW_LINE_DAYS ) ;
2009-01-09 21:14:07 +01:00
break ;
case 0x11 : { // GRF match for engine allocation
2010-01-23 14:17:30 +01:00
uint32 s = buf - > ReadDWord ( ) ;
uint32 t = buf - > ReadDWord ( ) ;
2009-01-09 21:14:07 +01:00
SetNewGRFOverride ( s , t ) ;
break ;
}
2010-11-16 13:47:22 +01:00
case 0x13 : // Gender translation table
case 0x14 : // Case translation table
while ( buf - > ReadByte ( ) ! = 0 ) {
buf - > ReadString ( ) ;
}
break ;
2009-01-09 21:14:07 +01:00
default :
ret = CIR_UNKNOWN ;
break ;
}
}
return ret ;
}
2011-09-11 13:14:22 +02:00
/**
2011-12-15 22:56:00 +01:00
* Define properties for cargoes
2011-09-11 13:14:22 +02:00
* @ param cid Local ID of the cargo .
* @ param numinfo Number of subsequent IDs to change the property for .
* @ param prop The property to change .
* @ param buf The property value .
* @ return ChangeInfoResult .
*/
2010-01-22 14:02:15 +01:00
static ChangeInfoResult CargoChangeInfo ( uint cid , int numinfo , int prop , ByteReader * buf )
2007-03-23 00:19:40 +01:00
{
2008-08-31 18:41:27 +02:00
ChangeInfoResult ret = CIR_SUCCESS ;
2007-07-04 14:52:43 +02:00
2007-03-23 00:19:40 +01:00
if ( cid + numinfo > NUM_CARGO ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " CargoChangeInfo: Cargo type {} out of range (max {}) " , cid + numinfo , NUM_CARGO - 1 ) ;
2008-08-31 18:41:27 +02:00
return CIR_INVALID_ID ;
2007-03-23 00:19:40 +01:00
}
2007-07-04 14:52:43 +02:00
for ( int i = 0 ; i < numinfo ; i + + ) {
2009-07-16 21:00:13 +02:00
CargoSpec * cs = CargoSpec : : Get ( cid + i ) ;
2007-03-23 00:19:40 +01:00
2007-07-04 14:52:43 +02:00
switch ( prop ) {
2009-03-15 01:32:18 +01:00
case 0x08 : // Bit number of cargo
2010-01-23 14:17:30 +01:00
cs - > bitnum = buf - > ReadByte ( ) ;
2007-03-23 00:19:40 +01:00
if ( cs - > IsValid ( ) ) {
2011-08-21 14:05:47 +02:00
cs - > grffile = _cur . grffile ;
2007-11-20 14:35:54 +01:00
SetBit ( _cargo_mask , cid + i ) ;
2007-03-23 00:19:40 +01:00
} else {
2007-11-19 22:32:20 +01:00
ClrBit ( _cargo_mask , cid + i ) ;
2007-03-23 00:19:40 +01:00
}
2007-07-04 14:52:43 +02:00
break ;
2009-03-15 01:32:18 +01:00
case 0x09 : // String ID for cargo type name
2014-01-12 19:01:50 +01:00
AddStringForMapping ( buf - > ReadWord ( ) , & cs - > name ) ;
2007-07-04 14:52:43 +02:00
break ;
2009-03-15 01:32:18 +01:00
case 0x0A : // String for 1 unit of cargo
2014-01-12 19:01:50 +01:00
AddStringForMapping ( buf - > ReadWord ( ) , & cs - > name_single ) ;
2007-07-04 14:52:43 +02:00
break ;
2010-11-17 19:35:59 +01:00
case 0x0B : // String for singular quantity of cargo (e.g. 1 tonne of coal)
case 0x1B : // String for cargo units
/* String for units of cargo. This is different in OpenTTD
* ( e . g . tonnes ) to TTDPatch ( e . g . { COMMA } tonne of coal ) .
* Property 1 B is used to set OpenTTD ' s behaviour . */
2014-01-12 19:01:50 +01:00
AddStringForMapping ( buf - > ReadWord ( ) , & cs - > units_volume ) ;
2007-07-04 14:52:43 +02:00
break ;
2010-11-17 19:35:59 +01:00
case 0x0C : // String for plural quantity of cargo (e.g. 10 tonnes of coal)
case 0x1C : // String for any amount of cargo
/* Strings for an amount of cargo. This is different in OpenTTD
* ( e . g . { WEIGHT } of coal ) to TTDPatch ( e . g . { COMMA } tonnes of coal ) .
* Property 1 C is used to set OpenTTD ' s behaviour . */
2014-01-12 19:01:50 +01:00
AddStringForMapping ( buf - > ReadWord ( ) , & cs - > quantifier ) ;
2007-07-04 14:52:43 +02:00
break ;
2009-03-15 01:32:18 +01:00
case 0x0D : // String for two letter cargo abbreviation
2014-01-12 19:01:50 +01:00
AddStringForMapping ( buf - > ReadWord ( ) , & cs - > abbrev ) ;
2007-07-04 14:52:43 +02:00
break ;
2009-03-15 01:32:18 +01:00
case 0x0E : // Sprite ID for cargo icon
2010-01-23 14:17:30 +01:00
cs - > sprite = buf - > ReadWord ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2009-03-15 01:32:18 +01:00
case 0x0F : // Weight of one unit of cargo
2010-01-23 14:17:30 +01:00
cs - > weight = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2009-03-15 01:32:18 +01:00
case 0x10 : // Used for payment calculation
2010-01-23 14:17:30 +01:00
cs - > transit_days [ 0 ] = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2009-03-15 01:32:18 +01:00
case 0x11 : // Used for payment calculation
2010-01-23 14:17:30 +01:00
cs - > transit_days [ 1 ] = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2009-03-15 01:32:18 +01:00
case 0x12 : // Base cargo price
2010-01-23 14:17:30 +01:00
cs - > initial_payment = buf - > ReadDWord ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2009-03-15 01:32:18 +01:00
case 0x13 : // Colour for station rating bars
2011-05-04 19:12:37 +02:00
cs - > rating_colour = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2009-03-15 01:32:18 +01:00
case 0x14 : // Colour for cargo graph
2011-05-04 19:12:37 +02:00
cs - > legend_colour = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2009-03-15 01:32:18 +01:00
case 0x15 : // Freight status
2010-01-23 14:17:30 +01:00
cs - > is_freight = ( buf - > ReadByte ( ) ! = 0 ) ;
2007-07-04 14:52:43 +02:00
break ;
2009-03-15 01:32:18 +01:00
case 0x16 : // Cargo classes
2010-01-23 14:17:30 +01:00
cs - > classes = buf - > ReadWord ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2009-03-15 01:32:18 +01:00
case 0x17 : // Cargo label
2010-01-23 14:17:30 +01:00
cs - > label = buf - > ReadDWord ( ) ;
2007-07-04 14:52:43 +02:00
cs - > label = BSWAP32 ( cs - > label ) ;
break ;
2009-03-15 01:32:18 +01:00
case 0x18 : { // Town growth substitute type
2010-01-23 14:17:30 +01:00
uint8 substitute_type = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
2007-03-23 00:19:40 +01:00
switch ( substitute_type ) {
2007-07-04 14:52:43 +02:00
case 0x00 : cs - > town_effect = TE_PASSENGERS ; break ;
case 0x02 : cs - > town_effect = TE_MAIL ; break ;
case 0x05 : cs - > town_effect = TE_GOODS ; break ;
case 0x09 : cs - > town_effect = TE_WATER ; break ;
case 0x0B : cs - > town_effect = TE_FOOD ; break ;
2007-03-23 00:19:40 +01:00
default :
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " CargoChangeInfo: Unknown town growth substitute value {}, setting to none. " , substitute_type ) ;
2017-08-13 20:38:42 +02:00
FALLTHROUGH ;
2007-07-04 14:52:43 +02:00
case 0xFF : cs - > town_effect = TE_NONE ; break ;
2007-03-23 00:19:40 +01:00
}
2010-08-01 20:53:30 +02:00
break ;
}
2007-03-23 00:19:40 +01:00
2009-03-15 01:32:18 +01:00
case 0x19 : // Town growth coefficient
2021-11-16 23:48:21 +01:00
buf - > ReadWord ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2007-03-23 00:19:40 +01:00
2009-03-15 01:32:18 +01:00
case 0x1A : // Bitmask of callbacks to use
2010-01-23 14:17:30 +01:00
cs - > callback_mask = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2007-03-23 00:19:40 +01:00
2012-01-28 13:08:03 +01:00
case 0x1D : // Vehicle capacity muliplier
2021-01-08 11:16:18 +01:00
cs - > multiplier = std : : max < uint16 > ( 1u , buf - > ReadWord ( ) ) ;
2012-01-28 13:08:03 +01:00
break ;
2007-07-04 14:52:43 +02:00
default :
2008-08-31 18:41:27 +02:00
ret = CIR_UNKNOWN ;
2007-07-04 14:52:43 +02:00
break ;
}
2007-03-23 00:19:40 +01:00
}
return ret ;
}
2011-09-11 13:14:22 +02:00
/**
* Define properties for sound effects
* @ param sid Local ID of the sound .
* @ param numinfo Number of subsequent IDs to change the property for .
* @ param prop The property to change .
* @ param buf The property value .
* @ return ChangeInfoResult .
*/
2010-01-22 14:02:15 +01:00
static ChangeInfoResult SoundEffectChangeInfo ( uint sid , int numinfo , int prop , ByteReader * buf )
2006-09-27 20:17:01 +02:00
{
2008-08-31 18:41:27 +02:00
ChangeInfoResult ret = CIR_SUCCESS ;
2006-09-27 20:17:01 +02:00
2011-08-21 14:05:47 +02:00
if ( _cur . grffile - > sound_offset = = 0 ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " SoundEffectChangeInfo: No effects defined, skipping " ) ;
2008-08-31 18:41:27 +02:00
return CIR_INVALID_ID ;
2006-09-27 20:17:01 +02:00
}
2011-08-21 14:05:47 +02:00
if ( sid + numinfo - ORIGINAL_SAMPLE_COUNT > _cur . grffile - > num_sounds ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " SoundEffectChangeInfo: Attempting to change undefined sound effect ({}), max ({}). Ignoring. " , sid + numinfo , ORIGINAL_SAMPLE_COUNT + _cur . grffile - > num_sounds ) ;
2010-04-20 07:52:51 +02:00
return CIR_INVALID_ID ;
}
2006-09-27 20:17:01 +02:00
2010-04-20 07:52:51 +02:00
for ( int i = 0 ; i < numinfo ; i + + ) {
2011-08-21 14:05:47 +02:00
SoundEntry * sound = GetSound ( sid + i + _cur . grffile - > sound_offset - ORIGINAL_SAMPLE_COUNT ) ;
2006-09-27 20:17:01 +02:00
2007-07-04 14:52:43 +02:00
switch ( prop ) {
case 0x08 : // Relative volume
2010-04-20 07:52:51 +02:00
sound - > volume = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2006-09-27 20:17:01 +02:00
2007-07-04 14:52:43 +02:00
case 0x09 : // Priority
2010-04-20 07:52:51 +02:00
sound - > priority = buf - > ReadByte ( ) ;
2007-07-04 14:52:43 +02:00
break ;
2006-09-27 20:17:01 +02:00
2007-07-04 14:52:43 +02:00
case 0x0A : { // Override old sound
2010-01-23 14:17:30 +01:00
SoundID orig_sound = buf - > ReadByte ( ) ;
2006-09-27 20:17:01 +02:00
2009-05-17 19:00:30 +02:00
if ( orig_sound > = ORIGINAL_SAMPLE_COUNT ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " SoundEffectChangeInfo: Original sound {} not defined (max {}) " , orig_sound , ORIGINAL_SAMPLE_COUNT ) ;
2006-09-27 20:17:01 +02:00
} else {
2009-05-17 21:27:50 +02:00
SoundEntry * old_sound = GetSound ( orig_sound ) ;
2006-09-27 20:17:01 +02:00
/* Literally copy the data of the new sound over the original */
2010-04-20 07:52:51 +02:00
* old_sound = * sound ;
2006-09-27 20:17:01 +02:00
}
2010-08-01 20:53:30 +02:00
break ;
}
2006-09-27 20:17:01 +02:00
2007-07-04 14:52:43 +02:00
default :
2008-08-31 18:41:27 +02:00
ret = CIR_UNKNOWN ;
2007-07-04 14:52:43 +02:00
break ;
}
2006-09-27 20:17:01 +02:00
}
return ret ;
}
2011-09-11 13:14:22 +02:00
/**
* Ignore an industry tile property
* @ param prop The property to ignore .
* @ param buf The property value .
* @ return ChangeInfoResult .
*/
2010-04-02 23:30:37 +02:00
static ChangeInfoResult IgnoreIndustryTileProperty ( int prop , ByteReader * buf )
{
ChangeInfoResult ret = CIR_SUCCESS ;
switch ( prop ) {
case 0x09 :
case 0x0D :
case 0x0E :
case 0x10 :
case 0x11 :
case 0x12 :
buf - > ReadByte ( ) ;
break ;
case 0x0A :
case 0x0B :
case 0x0C :
case 0x0F :
buf - > ReadWord ( ) ;
break ;
2018-07-26 19:29:54 +02:00
case 0x13 :
buf - > Skip ( buf - > ReadByte ( ) * 2 ) ;
break ;
2010-04-02 23:30:37 +02:00
default :
ret = CIR_UNKNOWN ;
break ;
}
return ret ;
}
2011-09-11 13:14:22 +02:00
/**
* Define properties for industry tiles
* @ param indtid Local ID of the industry tile .
* @ param numinfo Number of subsequent industry tile IDs to change the property for .
* @ param prop The property to change .
* @ param buf The property value .
* @ return ChangeInfoResult .
*/
2010-01-22 14:02:15 +01:00
static ChangeInfoResult IndustrytilesChangeInfo ( uint indtid , int numinfo , int prop , ByteReader * buf )
2007-07-10 02:10:19 +02:00
{
2008-08-31 18:41:27 +02:00
ChangeInfoResult ret = CIR_SUCCESS ;
2007-07-10 02:10:19 +02:00
2013-10-12 18:32:59 +02:00
if ( indtid + numinfo > NUM_INDUSTRYTILES_PER_GRF ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " IndustryTilesChangeInfo: Too many industry tiles loaded ({}), max ({}). Ignoring. " , indtid + numinfo , NUM_INDUSTRYTILES_PER_GRF ) ;
2008-08-31 18:41:27 +02:00
return CIR_INVALID_ID ;
2007-07-10 02:10:19 +02:00
}
/* Allocate industry tile specs if they haven't been allocated already. */
2023-01-08 03:12:33 +01:00
if ( _cur . grffile - > indtspec . size ( ) < indtid + numinfo ) _cur . grffile - > indtspec . resize ( indtid + numinfo ) ;
2007-07-10 02:10:19 +02:00
for ( int i = 0 ; i < numinfo ; i + + ) {
2023-04-15 22:30:37 +02:00
IndustryTileSpec * tsp = _cur . grffile - > indtspec [ indtid + i ] . get ( ) ;
2007-07-10 02:10:19 +02:00
2019-04-10 23:07:06 +02:00
if ( prop ! = 0x08 & & tsp = = nullptr ) {
2010-04-02 23:30:37 +02:00
ChangeInfoResult cir = IgnoreIndustryTileProperty ( prop , buf ) ;
if ( cir > ret ) ret = cir ;
continue ;
2007-07-10 02:10:19 +02:00
}
switch ( prop ) {
case 0x08 : { // Substitute industry tile type
2010-01-23 14:17:30 +01:00
byte subs_id = buf - > ReadByte ( ) ;
2007-08-21 02:29:10 +02:00
if ( subs_id > = NEW_INDUSTRYTILEOFFSET ) {
2007-07-10 02:10:19 +02:00
/* The substitute id must be one of the original industry tile. */
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " IndustryTilesChangeInfo: Attempt to use new industry tile {} as substitute industry tile for {}. Ignoring. " , subs_id , indtid + i ) ;
2008-08-31 17:26:46 +02:00
continue ;
2007-07-10 02:10:19 +02:00
}
/* Allocate space for this industry. */
2023-04-15 22:30:37 +02:00
if ( tsp = = nullptr ) {
_cur . grffile - > indtspec [ indtid + i ] = std : : make_unique < IndustryTileSpec > ( _industry_tile_specs [ subs_id ] ) ;
tsp = _cur . grffile - > indtspec [ indtid + i ] . get ( ) ;
2007-07-10 02:10:19 +02:00
tsp - > enabled = true ;
2007-12-18 22:25:44 +01:00
/* A copied tile should not have the animation infos copied too.
* The anim_state should be left untouched , though
2021-05-09 19:02:17 +02:00
* It is up to the author to animate them */
2007-12-18 22:25:44 +01:00
tsp - > anim_production = INDUSTRYTILE_NOANIM ;
tsp - > anim_next = INDUSTRYTILE_NOANIM ;
2007-07-10 02:10:19 +02:00
tsp - > grf_prop . local_id = indtid + i ;
tsp - > grf_prop . subst_id = subs_id ;
2011-08-21 14:05:47 +02:00
tsp - > grf_prop . grffile = _cur . grffile ;
_industile_mngr . AddEntityID ( indtid + i , _cur . grffile - > grfid , subs_id ) ; // pre-reserve the tile slot
2007-07-10 02:10:19 +02:00
}
2010-08-01 20:53:30 +02:00
break ;
}
2007-07-10 02:10:19 +02:00
case 0x09 : { // Industry tile override
2010-01-23 14:17:30 +01:00
byte ovrid = buf - > ReadByte ( ) ;
2007-07-10 02:10:19 +02:00
/* The industry being overridden must be an original industry. */
if ( ovrid > = NEW_INDUSTRYTILEOFFSET ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " IndustryTilesChangeInfo: Attempt to override new industry tile {} with industry tile id {}. Ignoring. " , ovrid , indtid + i ) ;
2008-08-31 17:26:46 +02:00
continue ;
2007-07-10 02:10:19 +02:00
}
2011-08-21 14:05:47 +02:00
_industile_mngr . Add ( indtid + i , _cur . grffile - > grfid , ovrid ) ;
2010-08-01 20:53:30 +02:00
break ;
}
2007-07-10 02:10:19 +02:00
case 0x0A : // Tile acceptance
case 0x0B :
case 0x0C : {
2010-01-23 14:17:30 +01:00
uint16 acctp = buf - > ReadWord ( ) ;
2011-08-21 14:05:47 +02:00
tsp - > accepts_cargo [ prop - 0x0A ] = GetCargoTranslation ( GB ( acctp , 0 , 8 ) , _cur . grffile ) ;
2018-07-26 19:29:54 +02:00
tsp - > acceptance [ prop - 0x0A ] = Clamp ( GB ( acctp , 8 , 8 ) , 0 , 16 ) ;
2010-08-01 20:53:30 +02:00
break ;
}
2007-07-10 02:10:19 +02:00
case 0x0D : // Land shape flags
2010-01-23 14:17:30 +01:00
tsp - > slopes_refused = ( Slope ) buf - > ReadByte ( ) ;
2007-07-10 02:10:19 +02:00
break ;
2009-09-14 14:22:57 +02:00
case 0x0E : // Callback mask
2010-01-23 14:17:30 +01:00
tsp - > callback_mask = buf - > ReadByte ( ) ;
2007-07-10 02:10:19 +02:00
break ;
case 0x0F : // Animation information
2010-08-26 17:31:40 +02:00
tsp - > animation . frames = buf - > ReadByte ( ) ;
tsp - > animation . status = buf - > ReadByte ( ) ;
2007-07-10 02:10:19 +02:00
break ;
case 0x10 : // Animation speed
2010-08-26 17:31:40 +02:00
tsp - > animation . speed = buf - > ReadByte ( ) ;
2007-07-10 02:10:19 +02:00
break ;
case 0x11 : // Triggers for callback 25
2010-08-26 17:31:40 +02:00
tsp - > animation . triggers = buf - > ReadByte ( ) ;
2007-07-10 02:10:19 +02:00
break ;
case 0x12 : // Special flags
2010-02-07 13:04:44 +01:00
tsp - > special_flags = ( IndustryTileSpecialFlags ) buf - > ReadByte ( ) ;
2007-07-10 02:10:19 +02:00
break ;
2018-07-26 19:29:54 +02:00
case 0x13 : { // variable length cargo acceptance
byte num_cargoes = buf - > ReadByte ( ) ;
if ( num_cargoes > lengthof ( tsp - > acceptance ) ) {
GRFError * error = DisableGrf ( STR_NEWGRF_ERROR_LIST_PROPERTY_TOO_LONG ) ;
error - > param_value [ 1 ] = prop ;
return CIR_DISABLED ;
}
for ( uint i = 0 ; i < lengthof ( tsp - > acceptance ) ; i + + ) {
if ( i < num_cargoes ) {
tsp - > accepts_cargo [ i ] = GetCargoTranslation ( buf - > ReadByte ( ) , _cur . grffile ) ;
/* Tile acceptance can be negative to counteract the INDTILE_SPECIAL_ACCEPTS_ALL_CARGO flag */
tsp - > acceptance [ i ] = ( int8 ) buf - > ReadByte ( ) ;
} else {
tsp - > accepts_cargo [ i ] = CT_INVALID ;
tsp - > acceptance [ i ] = 0 ;
}
}
break ;
}
2007-07-10 02:10:19 +02:00
default :
2008-08-31 18:41:27 +02:00
ret = CIR_UNKNOWN ;
2007-07-10 02:10:19 +02:00
break ;
}
}
return ret ;
}
2011-09-11 13:14:22 +02:00
/**
* Ignore an industry property
* @ param prop The property to ignore .
* @ param buf The property value .
* @ return ChangeInfoResult .
*/
2010-04-02 23:30:37 +02:00
static ChangeInfoResult IgnoreIndustryProperty ( int prop , ByteReader * buf )
{
ChangeInfoResult ret = CIR_SUCCESS ;
switch ( prop ) {
case 0x09 :
case 0x0B :
case 0x0F :
case 0x12 :
case 0x13 :
case 0x14 :
case 0x17 :
case 0x18 :
case 0x19 :
case 0x21 :
case 0x22 :
buf - > ReadByte ( ) ;
break ;
case 0x0C :
case 0x0D :
case 0x0E :
case 0x10 :
case 0x1B :
case 0x1F :
case 0x24 :
buf - > ReadWord ( ) ;
break ;
2011-02-28 16:24:35 +01:00
case 0x11 :
2010-04-02 23:30:37 +02:00
case 0x1A :
case 0x1C :
case 0x1D :
case 0x1E :
case 0x20 :
case 0x23 :
buf - > ReadDWord ( ) ;
break ;
case 0x0A : {
byte num_table = buf - > ReadByte ( ) ;
for ( byte j = 0 ; j < num_table ; j + + ) {
for ( uint k = 0 ; ; k + + ) {
byte x = buf - > ReadByte ( ) ;
if ( x = = 0xFE & & k = = 0 ) {
buf - > ReadByte ( ) ;
buf - > ReadByte ( ) ;
break ;
}
byte y = buf - > ReadByte ( ) ;
if ( x = = 0 & & y = = 0x80 ) break ;
byte gfx = buf - > ReadByte ( ) ;
if ( gfx = = 0xFE ) buf - > ReadWord ( ) ;
}
}
2010-08-01 20:53:30 +02:00
break ;
}
2010-04-02 23:30:37 +02:00
case 0x16 :
for ( byte j = 0 ; j < 3 ; j + + ) buf - > ReadByte ( ) ;
break ;
2018-07-26 19:29:54 +02:00
case 0x15 :
case 0x25 :
case 0x26 :
case 0x27 :
buf - > Skip ( buf - > ReadByte ( ) ) ;
break ;
case 0x28 : {
int num_inputs = buf - > ReadByte ( ) ;
int num_outputs = buf - > ReadByte ( ) ;
buf - > Skip ( num_inputs * num_outputs * 2 ) ;
2010-08-01 20:53:30 +02:00
break ;
}
2010-04-02 23:30:37 +02:00
default :
ret = CIR_UNKNOWN ;
break ;
}
return ret ;
}
2009-09-21 20:31:47 +02:00
/**
* Validate the industry layout ; e . g . to prevent duplicate tiles .
2011-09-11 13:14:22 +02:00
* @ param layout The layout to check .
* @ return True if the layout is deemed valid .
2009-09-21 20:31:47 +02:00
*/
2019-10-04 21:26:44 +02:00
static bool ValidateIndustryLayout ( const IndustryTileLayout & layout )
2009-09-21 20:31:47 +02:00
{
2019-10-04 21:26:44 +02:00
const size_t size = layout . size ( ) ;
2022-05-29 15:56:30 +02:00
if ( size = = 0 ) return false ;
2019-10-04 21:26:44 +02:00
for ( size_t i = 0 ; i < size - 1 ; i + + ) {
for ( size_t j = i + 1 ; j < size ; j + + ) {
2009-09-21 20:31:47 +02:00
if ( layout [ i ] . ti . x = = layout [ j ] . ti . x & &
layout [ i ] . ti . y = = layout [ j ] . ti . y ) {
return false ;
}
}
}
2022-06-18 12:08:13 +02:00
bool have_regular_tile = false ;
for ( size_t i = 0 ; i < size ; i + + ) {
if ( layout [ i ] . gfx ! = GFX_WATERTILE_SPECIALCHECK ) {
have_regular_tile = true ;
break ;
}
}
return have_regular_tile ;
2009-09-21 20:31:47 +02:00
}
2011-09-11 13:14:22 +02:00
/**
* Define properties for industries
* @ param indid Local ID of the industry .
* @ param numinfo Number of subsequent industry IDs to change the property for .
* @ param prop The property to change .
* @ param buf The property value .
* @ return ChangeInfoResult .
*/
2010-01-22 14:02:15 +01:00
static ChangeInfoResult IndustriesChangeInfo ( uint indid , int numinfo , int prop , ByteReader * buf )
2007-07-10 02:10:19 +02:00
{
2008-08-31 18:41:27 +02:00
ChangeInfoResult ret = CIR_SUCCESS ;
2007-07-10 02:10:19 +02:00
2013-10-12 18:32:16 +02:00
if ( indid + numinfo > NUM_INDUSTRYTYPES_PER_GRF ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " IndustriesChangeInfo: Too many industries loaded ({}), max ({}). Ignoring. " , indid + numinfo , NUM_INDUSTRYTYPES_PER_GRF ) ;
2008-08-31 18:41:27 +02:00
return CIR_INVALID_ID ;
2007-07-10 02:10:19 +02:00
}
/* Allocate industry specs if they haven't been allocated already. */
2023-01-08 03:12:33 +01:00
if ( _cur . grffile - > industryspec . size ( ) < indid + numinfo ) _cur . grffile - > industryspec . resize ( indid + numinfo ) ;
2007-07-10 02:10:19 +02:00
for ( int i = 0 ; i < numinfo ; i + + ) {
2023-04-15 22:30:37 +02:00
IndustrySpec * indsp = _cur . grffile - > industryspec [ indid + i ] . get ( ) ;
2007-07-10 02:10:19 +02:00
2019-04-10 23:07:06 +02:00
if ( prop ! = 0x08 & & indsp = = nullptr ) {
2010-04-02 23:30:37 +02:00
ChangeInfoResult cir = IgnoreIndustryProperty ( prop , buf ) ;
if ( cir > ret ) ret = cir ;
continue ;
2007-07-10 02:10:19 +02:00
}
switch ( prop ) {
case 0x08 : { // Substitute industry type
2010-01-23 14:17:30 +01:00
byte subs_id = buf - > ReadByte ( ) ;
2007-07-10 02:10:19 +02:00
if ( subs_id = = 0xFF ) {
/* Instead of defining a new industry, a substitute industry id
* of 0xFF disables the old industry with the current id . */
_industry_specs [ indid + i ] . enabled = false ;
continue ;
} else if ( subs_id > = NEW_INDUSTRYOFFSET ) {
/* The substitute id must be one of the original industry. */
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " _industry_specs: Attempt to use new industry {} as substitute industry for {}. Ignoring. " , subs_id , indid + i ) ;
2008-08-31 17:26:46 +02:00
continue ;
2007-07-10 02:10:19 +02:00
}
/* Allocate space for this industry.
* Only need to do it once . If ever it is called again , it should not
* do anything */
2023-04-15 22:30:37 +02:00
if ( indsp = = nullptr ) {
_cur . grffile - > industryspec [ indid + i ] = std : : make_unique < IndustrySpec > ( _origin_industry_specs [ subs_id ] ) ;
indsp = _cur . grffile - > industryspec [ indid + i ] . get ( ) ;
2007-07-10 02:10:19 +02:00
indsp - > enabled = true ;
indsp - > grf_prop . local_id = indid + i ;
indsp - > grf_prop . subst_id = subs_id ;
2011-08-21 14:05:47 +02:00
indsp - > grf_prop . grffile = _cur . grffile ;
2019-09-29 22:27:32 +02:00
/* If the grf industry needs to check its surrounding upon creation, it should
2007-10-13 02:49:13 +02:00
* rely on callbacks , not on the original placement functions */
indsp - > check_proc = CHECK_NOTHING ;
2007-07-10 02:10:19 +02:00
}
2010-08-01 20:53:30 +02:00
break ;
}
2007-07-10 02:10:19 +02:00
case 0x09 : { // Industry type override
2010-01-23 14:17:30 +01:00
byte ovrid = buf - > ReadByte ( ) ;
2007-07-10 02:10:19 +02:00
/* The industry being overridden must be an original industry. */
if ( ovrid > = NEW_INDUSTRYOFFSET ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " IndustriesChangeInfo: Attempt to override new industry {} with industry id {}. Ignoring. " , ovrid , indid + i ) ;
2008-08-31 17:26:46 +02:00
continue ;
2007-07-10 02:10:19 +02:00
}
indsp - > grf_prop . override = ovrid ;
2011-08-21 14:05:47 +02:00
_industry_mngr . Add ( indid + i , _cur . grffile - > grfid , ovrid ) ;
2010-08-01 20:53:30 +02:00
break ;
}
2007-07-10 02:10:19 +02:00
case 0x0A : { // Set industry layout(s)
2019-10-04 21:26:44 +02:00
byte new_num_layouts = buf - > ReadByte ( ) ;
uint32 definition_size = buf - > ReadDWord ( ) ;
uint32 bytes_read = 0 ;
std : : vector < IndustryTileLayout > new_layouts ;
IndustryTileLayout layout ;
for ( byte j = 0 ; j < new_num_layouts ; j + + ) {
layout . clear ( ) ;
for ( uint k = 0 ; ; k + + ) {
if ( bytes_read > = definition_size ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 3 , " IndustriesChangeInfo: Incorrect size for industry tile layout definition for industry {}. " , indid ) ;
2019-10-04 21:26:44 +02:00
/* Avoid warning twice */
definition_size = UINT32_MAX ;
}
2007-07-10 02:10:19 +02:00
2019-10-04 21:26:44 +02:00
layout . push_back ( IndustryTileLayoutTile { } ) ;
IndustryTileLayoutTile & it = layout . back ( ) ;
2009-12-18 00:26:25 +01:00
2019-10-04 21:26:44 +02:00
it . ti . x = buf - > ReadByte ( ) ; // Offsets from northermost tile
+ + bytes_read ;
2007-07-10 02:10:19 +02:00
2019-10-04 21:26:44 +02:00
if ( it . ti . x = = 0xFE & & k = = 0 ) {
/* This means we have to borrow the layout from an old industry */
IndustryType type = buf - > ReadByte ( ) ;
byte laynbr = buf - > ReadByte ( ) ;
bytes_read + = 2 ;
2007-07-10 02:10:19 +02:00
2019-10-04 21:26:44 +02:00
if ( type > = lengthof ( _origin_industry_specs ) ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " IndustriesChangeInfo: Invalid original industry number for layout import, industry {} " , indid ) ;
2019-10-04 21:26:44 +02:00
DisableGrf ( STR_NEWGRF_ERROR_INVALID_ID ) ;
return CIR_DISABLED ;
2007-07-10 02:10:19 +02:00
}
2019-10-04 21:26:44 +02:00
if ( laynbr > = _origin_industry_specs [ type ] . layouts . size ( ) ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " IndustriesChangeInfo: Invalid original industry layout index for layout import, industry {} " , indid ) ;
2019-10-04 21:26:44 +02:00
DisableGrf ( STR_NEWGRF_ERROR_INVALID_ID ) ;
return CIR_DISABLED ;
}
layout = _origin_industry_specs [ type ] . layouts [ laynbr ] ;
break ;
}
2007-07-10 02:10:19 +02:00
2019-10-04 21:26:44 +02:00
it . ti . y = buf - > ReadByte ( ) ; // Or table definition finalisation
+ + bytes_read ;
2007-07-10 02:10:19 +02:00
2019-10-04 21:26:44 +02:00
if ( it . ti . x = = 0 & & it . ti . y = = 0x80 ) {
/* Terminator, remove and finish up */
layout . pop_back ( ) ;
break ;
}
2007-07-10 02:10:19 +02:00
2019-10-04 21:26:44 +02:00
it . gfx = buf - > ReadByte ( ) ;
+ + bytes_read ;
2007-07-10 02:10:19 +02:00
2019-10-04 21:26:44 +02:00
if ( it . gfx = = 0xFE ) {
/* Use a new tile from this GRF */
int local_tile_id = buf - > ReadWord ( ) ;
bytes_read + = 2 ;
2007-07-10 02:10:19 +02:00
2019-10-04 21:26:44 +02:00
/* Read the ID from the _industile_mngr. */
int tempid = _industile_mngr . GetID ( local_tile_id , _cur . grffile - > grfid ) ;
2007-07-10 02:10:19 +02:00
2019-10-04 21:26:44 +02:00
if ( tempid = = INVALID_INDUSTRYTILE ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " IndustriesChangeInfo: Attempt to use industry tile {} with industry id {}, not yet defined. Ignoring. " , local_tile_id , indid ) ;
2019-10-04 21:26:44 +02:00
} else {
/* Declared as been valid, can be used */
it . gfx = tempid ;
2007-07-10 02:10:19 +02:00
}
2022-05-29 15:56:30 +02:00
} else if ( it . gfx = = GFX_WATERTILE_SPECIALCHECK ) {
2019-10-04 21:26:44 +02:00
it . ti . x = ( int8 ) GB ( it . ti . x , 0 , 8 ) ;
it . ti . y = ( int8 ) GB ( it . ti . y , 0 , 8 ) ;
/* When there were only 256x256 maps, TileIndex was a uint16 and
* it . ti was just a TileIndexDiff that was added to it .
* As such negative " x " values were shifted into the " y " position .
* x = - 1 , y = 1 - > x = 255 , y = 0
* Since GRF version 8 the position is interpreted as pair of independent int8 .
* For GRF version < 8 we need to emulate the old shifting behaviour .
*/
if ( _cur . grffile - > grf_version < 8 & & it . ti . x < 0 ) it . ti . y + = 1 ;
2010-01-22 14:02:15 +01:00
}
2009-09-21 20:31:47 +02:00
}
2019-10-04 21:26:44 +02:00
if ( ! ValidateIndustryLayout ( layout ) ) {
/* The industry layout was not valid, so skip this one. */
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " IndustriesChangeInfo: Invalid industry layout for industry id {}. Ignoring " , indid ) ;
2019-10-04 21:26:44 +02:00
new_num_layouts - - ;
j - - ;
} else {
new_layouts . push_back ( layout ) ;
2010-01-22 15:03:36 +01:00
}
2007-07-10 02:10:19 +02:00
}
2010-01-22 14:02:15 +01:00
2007-07-10 02:10:19 +02:00
/* Install final layout construction in the industry spec */
2019-10-04 21:26:44 +02:00
indsp - > layouts = new_layouts ;
2010-08-01 20:53:30 +02:00
break ;
}
2007-07-10 02:10:19 +02:00
case 0x0B : // Industry production flags
2010-01-23 14:17:30 +01:00
indsp - > life_type = ( IndustryLifeType ) buf - > ReadByte ( ) ;
2007-07-10 02:10:19 +02:00
break ;
case 0x0C : // Industry closure message
2014-01-12 19:01:50 +01:00
AddStringForMapping ( buf - > ReadWord ( ) , & indsp - > closure_text ) ;
2007-07-10 02:10:19 +02:00
break ;
case 0x0D : // Production increase message
2014-01-12 19:01:50 +01:00
AddStringForMapping ( buf - > ReadWord ( ) , & indsp - > production_up_text ) ;
2007-07-10 02:10:19 +02:00
break ;
case 0x0E : // Production decrease message
2014-01-12 19:01:50 +01:00
AddStringForMapping ( buf - > ReadWord ( ) , & indsp - > production_down_text ) ;
2007-07-10 02:10:19 +02:00
break ;
case 0x0F : // Fund cost multiplier
2010-01-23 14:17:30 +01:00
indsp - > cost_multiplier = buf - > ReadByte ( ) ;
2007-07-10 02:10:19 +02:00
break ;
case 0x10 : // Production cargo types
for ( byte j = 0 ; j < 2 ; j + + ) {
2011-08-21 14:05:47 +02:00
indsp - > produced_cargo [ j ] = GetCargoTranslation ( buf - > ReadByte ( ) , _cur . grffile ) ;
2007-07-10 02:10:19 +02:00
}
break ;
case 0x11 : // Acceptance cargo types
for ( byte j = 0 ; j < 3 ; j + + ) {
2011-08-21 14:05:47 +02:00
indsp - > accepts_cargo [ j ] = GetCargoTranslation ( buf - > ReadByte ( ) , _cur . grffile ) ;
2007-07-10 02:10:19 +02:00
}
2010-01-23 14:17:30 +01:00
buf - > ReadByte ( ) ; // Unnused, eat it up
2007-07-10 02:10:19 +02:00
break ;
case 0x12 : // Production multipliers
case 0x13 :
2010-01-23 14:17:30 +01:00
indsp - > production_rate [ prop - 0x12 ] = buf - > ReadByte ( ) ;
2007-07-10 02:10:19 +02:00
break ;
case 0x14 : // Minimal amount of cargo distributed
2010-01-23 14:17:30 +01:00
indsp - > minimal_cargo = buf - > ReadByte ( ) ;
2007-07-10 02:10:19 +02:00
break ;
case 0x15 : { // Random sound effects
2010-01-23 14:17:30 +01:00
indsp - > number_of_sounds = buf - > ReadByte ( ) ;
2007-07-10 02:10:19 +02:00
uint8 * sounds = MallocT < uint8 > ( indsp - > number_of_sounds ) ;
2010-01-22 14:02:15 +01:00
try {
for ( uint8 j = 0 ; j < indsp - > number_of_sounds ; j + + ) {
2010-01-23 14:17:30 +01:00
sounds [ j ] = buf - > ReadByte ( ) ;
2010-01-22 14:02:15 +01:00
}
} catch ( . . . ) {
free ( sounds ) ;
throw ;
}
2011-03-03 21:26:19 +01:00
if ( HasBit ( indsp - > cleanup_flag , CLEAN_RANDOMSOUNDS ) ) {
2011-11-12 14:00:29 +01:00
free ( indsp - > random_sounds ) ;
2011-03-03 21:26:19 +01:00
}
2007-07-10 02:10:19 +02:00
indsp - > random_sounds = sounds ;
2011-03-03 21:26:19 +01:00
SetBit ( indsp - > cleanup_flag , CLEAN_RANDOMSOUNDS ) ;
2010-08-01 20:53:30 +02:00
break ;
}
2007-07-10 02:10:19 +02:00
case 0x16 : // Conflicting industry types
2010-01-23 14:17:30 +01:00
for ( byte j = 0 ; j < 3 ; j + + ) indsp - > conflicting [ j ] = buf - > ReadByte ( ) ;
2007-07-10 02:10:19 +02:00
break ;
case 0x17 : // Probability in random game
2010-01-23 14:17:30 +01:00
indsp - > appear_creation [ _settings_game . game_creation . landscape ] = buf - > ReadByte ( ) ;
2007-07-10 02:10:19 +02:00
break ;
case 0x18 : // Probability during gameplay
2010-01-23 14:17:30 +01:00
indsp - > appear_ingame [ _settings_game . game_creation . landscape ] = buf - > ReadByte ( ) ;
2007-07-10 02:10:19 +02:00
break ;
2009-02-09 03:57:15 +01:00
case 0x19 : // Map colour
2011-05-04 19:12:37 +02:00
indsp - > map_colour = buf - > ReadByte ( ) ;
2007-07-10 02:10:19 +02:00
break ;
case 0x1A : // Special industry flags to define special behavior
2010-01-23 14:17:30 +01:00
indsp - > behaviour = ( IndustryBehaviour ) buf - > ReadDWord ( ) ;
2007-07-10 02:10:19 +02:00
break ;
case 0x1B : // New industry text ID
2014-01-12 19:01:50 +01:00
AddStringForMapping ( buf - > ReadWord ( ) , & indsp - > new_industry_text ) ;
2007-07-10 02:10:19 +02:00
break ;
case 0x1C : // Input cargo multipliers for the three input cargo types
case 0x1D :
case 0x1E : {
2010-01-23 14:17:30 +01:00
uint32 multiples = buf - > ReadDWord ( ) ;
2008-12-20 00:13:51 +01:00
indsp - > input_cargo_multiplier [ prop - 0x1C ] [ 0 ] = GB ( multiples , 0 , 16 ) ;
indsp - > input_cargo_multiplier [ prop - 0x1C ] [ 1 ] = GB ( multiples , 16 , 16 ) ;
2010-08-01 20:53:30 +02:00
break ;
}
2007-07-10 02:10:19 +02:00
case 0x1F : // Industry name
2014-01-12 19:01:50 +01:00
AddStringForMapping ( buf - > ReadWord ( ) , & indsp - > name ) ;
2007-07-10 02:10:19 +02:00
break ;
case 0x20 : // Prospecting success chance
2010-01-23 14:17:30 +01:00
indsp - > prospecting_chance = buf - > ReadDWord ( ) ;
2007-07-10 02:10:19 +02:00
break ;
2009-09-14 14:22:57 +02:00
case 0x21 : // Callback mask
case 0x22 : { // Callback additional mask
2010-01-23 14:17:30 +01:00
byte aflag = buf - > ReadByte ( ) ;
2009-09-14 14:22:57 +02:00
SB ( indsp - > callback_mask , ( prop - 0x21 ) * 8 , 8 , aflag ) ;
2010-08-01 20:53:30 +02:00
break ;
}
2007-07-10 02:10:19 +02:00
2007-11-27 18:13:49 +01:00
case 0x23 : // removal cost multiplier
2010-01-23 14:17:30 +01:00
indsp - > removal_cost_multiplier = buf - > ReadDWord ( ) ;
2007-11-27 18:13:49 +01:00
break ;
2014-01-12 19:01:50 +01:00
case 0x24 : { // name for nearby station
uint16 str = buf - > ReadWord ( ) ;
if ( str = = 0 ) {
indsp - > station_name = STR_NULL ;
} else {
AddStringForMapping ( str , & indsp - > station_name ) ;
}
2008-02-02 04:23:26 +01:00
break ;
2014-01-12 19:01:50 +01:00
}
2008-02-02 04:23:26 +01:00
2018-07-26 19:29:54 +02:00
case 0x25 : { // variable length produced cargoes
byte num_cargoes = buf - > ReadByte ( ) ;
if ( num_cargoes > lengthof ( indsp - > produced_cargo ) ) {
GRFError * error = DisableGrf ( STR_NEWGRF_ERROR_LIST_PROPERTY_TOO_LONG ) ;
error - > param_value [ 1 ] = prop ;
return CIR_DISABLED ;
}
for ( uint i = 0 ; i < lengthof ( indsp - > produced_cargo ) ; i + + ) {
if ( i < num_cargoes ) {
CargoID cargo = GetCargoTranslation ( buf - > ReadByte ( ) , _cur . grffile ) ;
indsp - > produced_cargo [ i ] = cargo ;
} else {
indsp - > produced_cargo [ i ] = CT_INVALID ;
}
}
break ;
}
case 0x26 : { // variable length accepted cargoes
byte num_cargoes = buf - > ReadByte ( ) ;
if ( num_cargoes > lengthof ( indsp - > accepts_cargo ) ) {
GRFError * error = DisableGrf ( STR_NEWGRF_ERROR_LIST_PROPERTY_TOO_LONG ) ;
error - > param_value [ 1 ] = prop ;
return CIR_DISABLED ;
}
for ( uint i = 0 ; i < lengthof ( indsp - > accepts_cargo ) ; i + + ) {
if ( i < num_cargoes ) {
CargoID cargo = GetCargoTranslation ( buf - > ReadByte ( ) , _cur . grffile ) ;
indsp - > accepts_cargo [ i ] = cargo ;
} else {
indsp - > accepts_cargo [ i ] = CT_INVALID ;
}
}
break ;
}
case 0x27 : { // variable length production rates
byte num_cargoes = buf - > ReadByte ( ) ;
if ( num_cargoes > lengthof ( indsp - > production_rate ) ) {
GRFError * error = DisableGrf ( STR_NEWGRF_ERROR_LIST_PROPERTY_TOO_LONG ) ;
error - > param_value [ 1 ] = prop ;
return CIR_DISABLED ;
}
for ( uint i = 0 ; i < lengthof ( indsp - > production_rate ) ; i + + ) {
if ( i < num_cargoes ) {
indsp - > production_rate [ i ] = buf - > ReadByte ( ) ;
} else {
indsp - > production_rate [ i ] = 0 ;
}
}
break ;
}
case 0x28 : { // variable size input/output production multiplier table
byte num_inputs = buf - > ReadByte ( ) ;
byte num_outputs = buf - > ReadByte ( ) ;
if ( num_inputs > lengthof ( indsp - > accepts_cargo ) | | num_outputs > lengthof ( indsp - > produced_cargo ) ) {
GRFError * error = DisableGrf ( STR_NEWGRF_ERROR_LIST_PROPERTY_TOO_LONG ) ;
error - > param_value [ 1 ] = prop ;
return CIR_DISABLED ;
}
for ( uint i = 0 ; i < lengthof ( indsp - > accepts_cargo ) ; i + + ) {
for ( uint j = 0 ; j < lengthof ( indsp - > produced_cargo ) ; j + + ) {
uint16 mult = 0 ;
if ( i < num_inputs & & j < num_outputs ) mult = buf - > ReadWord ( ) ;
indsp - > input_cargo_multiplier [ i ] [ j ] = mult ;
}
}
break ;
}
2007-07-10 02:10:19 +02:00
default :
2008-08-31 18:41:27 +02:00
ret = CIR_UNKNOWN ;
2007-07-10 02:10:19 +02:00
break ;
}
}
return ret ;
}
2010-03-19 00:15:38 +01:00
/**
* Create a copy of the tile table so it can be freed later
* without problems .
* @ param as The AirportSpec to copy the arrays of .
*/
static void DuplicateTileTable ( AirportSpec * as )
{
AirportTileTable * * table_list = MallocT < AirportTileTable * > ( as - > num_table ) ;
for ( int i = 0 ; i < as - > num_table ; i + + ) {
uint num_tiles = 1 ;
const AirportTileTable * it = as - > table [ 0 ] ;
do {
num_tiles + + ;
} while ( ( + + it ) - > ti . x ! = - 0x80 ) ;
table_list [ i ] = MallocT < AirportTileTable > ( num_tiles ) ;
MemCpyT ( table_list [ i ] , as - > table [ i ] , num_tiles ) ;
}
as - > table = table_list ;
2010-03-19 12:17:52 +01:00
HangarTileTable * depot_table = MallocT < HangarTileTable > ( as - > nof_depots ) ;
2010-03-19 00:15:38 +01:00
MemCpyT ( depot_table , as - > depot_table , as - > nof_depots ) ;
as - > depot_table = depot_table ;
2018-03-06 08:48:29 +01:00
Direction * rotation = MallocT < Direction > ( as - > num_table ) ;
MemCpyT ( rotation , as - > rotation , as - > num_table ) ;
as - > rotation = rotation ;
2010-03-19 00:15:38 +01:00
}
2011-09-11 13:14:22 +02:00
/**
* Define properties for airports
* @ param airport Local ID of the airport .
* @ param numinfo Number of subsequent airport IDs to change the property for .
* @ param prop The property to change .
* @ param buf The property value .
* @ return ChangeInfoResult .
*/
2010-03-19 00:15:38 +01:00
static ChangeInfoResult AirportChangeInfo ( uint airport , int numinfo , int prop , ByteReader * buf )
{
ChangeInfoResult ret = CIR_SUCCESS ;
2013-10-12 18:34:23 +02:00
if ( airport + numinfo > NUM_AIRPORTS_PER_GRF ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " AirportChangeInfo: Too many airports, trying id ({}), max ({}). Ignoring. " , airport + numinfo , NUM_AIRPORTS_PER_GRF ) ;
2010-03-19 00:15:38 +01:00
return CIR_INVALID_ID ;
}
/* Allocate industry specs if they haven't been allocated already. */
2023-01-08 03:12:33 +01:00
if ( _cur . grffile - > airportspec . size ( ) < airport + numinfo ) _cur . grffile - > airportspec . resize ( airport + numinfo ) ;
2010-03-19 00:15:38 +01:00
for ( int i = 0 ; i < numinfo ; i + + ) {
2023-04-15 22:30:37 +02:00
AirportSpec * as = _cur . grffile - > airportspec [ airport + i ] . get ( ) ;
2010-03-19 00:15:38 +01:00
2019-04-10 23:07:06 +02:00
if ( as = = nullptr & & prop ! = 0x08 & & prop ! = 0x09 ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " AirportChangeInfo: Attempt to modify undefined airport {}, ignoring " , airport + i ) ;
2010-03-19 00:15:38 +01:00
return CIR_INVALID_ID ;
}
switch ( prop ) {
case 0x08 : { // Modify original airport
byte subs_id = buf - > ReadByte ( ) ;
if ( subs_id = = 0xFF ) {
/* Instead of defining a new airport, an airport id
* of 0xFF disables the old airport with the current id . */
AirportSpec : : GetWithoutOverride ( airport + i ) - > enabled = false ;
continue ;
} else if ( subs_id > = NEW_AIRPORT_OFFSET ) {
/* The substitute id must be one of the original airports. */
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " AirportChangeInfo: Attempt to use new airport {} as substitute airport for {}. Ignoring. " , subs_id , airport + i ) ;
2010-03-19 00:15:38 +01:00
continue ;
}
/* Allocate space for this airport.
* Only need to do it once . If ever it is called again , it should not
* do anything */
2023-04-15 22:30:37 +02:00
if ( as = = nullptr ) {
_cur . grffile - > airportspec [ airport + i ] = std : : make_unique < AirportSpec > ( * AirportSpec : : GetWithoutOverride ( subs_id ) ) ;
as = _cur . grffile - > airportspec [ airport + i ] . get ( ) ;
2010-03-19 00:15:38 +01:00
as - > enabled = true ;
as - > grf_prop . local_id = airport + i ;
as - > grf_prop . subst_id = subs_id ;
2011-08-21 14:05:47 +02:00
as - > grf_prop . grffile = _cur . grffile ;
2010-03-19 00:15:38 +01:00
/* override the default airport */
2011-08-21 14:05:47 +02:00
_airport_mngr . Add ( airport + i , _cur . grffile - > grfid , subs_id ) ;
2010-03-19 00:15:38 +01:00
/* Create a copy of the original tiletable so it can be freed later. */
DuplicateTileTable ( as ) ;
}
2010-08-01 20:53:30 +02:00
break ;
}
2010-03-19 00:15:38 +01:00
2010-08-05 14:10:22 +02:00
case 0x0A : { // Set airport layout
2021-04-02 10:13:53 +02:00
byte old_num_table = as - > num_table ;
2018-03-06 08:48:29 +01:00
free ( as - > rotation ) ;
2010-08-05 14:10:22 +02:00
as - > num_table = buf - > ReadByte ( ) ; // Number of layaouts
as - > rotation = MallocT < Direction > ( as - > num_table ) ;
uint32 defsize = buf - > ReadDWord ( ) ; // Total size of the definition
AirportTileTable * * tile_table = CallocT < AirportTileTable * > ( as - > num_table ) ; // Table with tiles to compose the airport
AirportTileTable * att = CallocT < AirportTileTable > ( defsize ) ; // Temporary array to read the tile layouts from the GRF
int size ;
const AirportTileTable * copy_from ;
try {
for ( byte j = 0 ; j < as - > num_table ; j + + ) {
2017-08-31 08:55:38 +02:00
const_cast < Direction & > ( as - > rotation [ j ] ) = ( Direction ) buf - > ReadByte ( ) ;
2010-08-05 14:10:22 +02:00
for ( int k = 0 ; ; k + + ) {
att [ k ] . ti . x = buf - > ReadByte ( ) ; // Offsets from northermost tile
att [ k ] . ti . y = buf - > ReadByte ( ) ;
if ( att [ k ] . ti . x = = 0 & & att [ k ] . ti . y = = 0x80 ) {
/* Not the same terminator. The one we are using is rather
2013-06-01 09:44:53 +02:00
* x = - 80 , y = 0 . So , adjust it . */
2010-08-05 14:10:22 +02:00
att [ k ] . ti . x = - 0x80 ;
att [ k ] . ti . y = 0 ;
att [ k ] . gfx = 0 ;
size = k + 1 ;
copy_from = att ;
break ;
}
att [ k ] . gfx = buf - > ReadByte ( ) ;
if ( att [ k ] . gfx = = 0xFE ) {
/* Use a new tile from this GRF */
int local_tile_id = buf - > ReadWord ( ) ;
/* Read the ID from the _airporttile_mngr. */
2011-08-21 14:05:47 +02:00
uint16 tempid = _airporttile_mngr . GetID ( local_tile_id , _cur . grffile - > grfid ) ;
2010-08-05 14:10:22 +02:00
if ( tempid = = INVALID_AIRPORTTILE ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " AirportChangeInfo: Attempt to use airport tile {} with airport id {}, not yet defined. Ignoring. " , local_tile_id , airport + i ) ;
2010-08-05 14:10:22 +02:00
} else {
/* Declared as been valid, can be used */
att [ k ] . gfx = tempid ;
}
} else if ( att [ k ] . gfx = = 0xFF ) {
att [ k ] . ti . x = ( int8 ) GB ( att [ k ] . ti . x , 0 , 8 ) ;
att [ k ] . ti . y = ( int8 ) GB ( att [ k ] . ti . y , 0 , 8 ) ;
}
if ( as - > rotation [ j ] = = DIR_E | | as - > rotation [ j ] = = DIR_W ) {
2021-01-08 11:16:18 +01:00
as - > size_x = std : : max < byte > ( as - > size_x , att [ k ] . ti . y + 1 ) ;
as - > size_y = std : : max < byte > ( as - > size_y , att [ k ] . ti . x + 1 ) ;
2010-08-05 14:10:22 +02:00
} else {
2021-01-08 11:16:18 +01:00
as - > size_x = std : : max < byte > ( as - > size_x , att [ k ] . ti . x + 1 ) ;
as - > size_y = std : : max < byte > ( as - > size_y , att [ k ] . ti . y + 1 ) ;
2010-08-05 14:10:22 +02:00
}
}
tile_table [ j ] = CallocT < AirportTileTable > ( size ) ;
memcpy ( tile_table [ j ] , copy_from , sizeof ( * copy_from ) * size ) ;
}
2021-04-02 10:13:53 +02:00
/* Free old layouts in the airport spec */
for ( int j = 0 ; j < old_num_table ; j + + ) {
/* remove the individual layouts */
free ( as - > table [ j ] ) ;
}
free ( as - > table ) ;
2010-08-05 14:10:22 +02:00
/* Install final layout construction in the airport spec */
as - > table = tile_table ;
free ( att ) ;
} catch ( . . . ) {
for ( int i = 0 ; i < as - > num_table ; i + + ) {
free ( tile_table [ i ] ) ;
}
free ( tile_table ) ;
free ( att ) ;
throw ;
}
break ;
}
2010-03-19 00:15:38 +01:00
case 0x0C :
as - > min_year = buf - > ReadWord ( ) ;
as - > max_year = buf - > ReadWord ( ) ;
if ( as - > max_year = = 0xFFFF ) as - > max_year = MAX_YEAR ;
break ;
case 0x0D :
as - > ttd_airport_type = ( TTDPAirportType ) buf - > ReadByte ( ) ;
break ;
case 0x0E :
as - > catchment = Clamp ( buf - > ReadByte ( ) , 1 , MAX_CATCHMENT ) ;
break ;
case 0x0F :
as - > noise_level = buf - > ReadByte ( ) ;
break ;
case 0x10 :
2014-01-12 19:01:50 +01:00
AddStringForMapping ( buf - > ReadWord ( ) , & as - > name ) ;
2010-03-19 00:15:38 +01:00
break ;
2011-12-04 00:40:46 +01:00
case 0x11 : // Maintenance cost factor
as - > maintenance_cost = buf - > ReadWord ( ) ;
break ;
2010-03-19 00:15:38 +01:00
default :
ret = CIR_UNKNOWN ;
break ;
}
}
return ret ;
}
2011-09-11 13:14:22 +02:00
/**
* Ignore properties for objects
* @ param prop The property to ignore .
* @ param buf The property value .
* @ return ChangeInfoResult .
*/
2010-08-28 20:21:09 +02:00
static ChangeInfoResult IgnoreObjectProperty ( uint prop , ByteReader * buf )
{
ChangeInfoResult ret = CIR_SUCCESS ;
switch ( prop ) {
case 0x0B :
case 0x0C :
case 0x0D :
case 0x12 :
case 0x14 :
case 0x16 :
2010-12-10 22:30:26 +01:00
case 0x17 :
2023-03-01 01:19:39 +01:00
case 0x18 :
2010-08-28 20:21:09 +02:00
buf - > ReadByte ( ) ;
2013-11-23 19:10:06 +01:00
break ;
2010-08-28 20:21:09 +02:00
case 0x09 :
case 0x0A :
case 0x10 :
case 0x11 :
2010-09-01 21:45:50 +02:00
case 0x13 :
2010-08-28 20:21:09 +02:00
case 0x15 :
buf - > ReadWord ( ) ;
break ;
case 0x08 :
case 0x0E :
case 0x0F :
buf - > ReadDWord ( ) ;
break ;
default :
ret = CIR_UNKNOWN ;
break ;
}
return ret ;
}
2011-09-11 13:14:22 +02:00
/**
* Define properties for objects
* @ param id Local ID of the object .
* @ param numinfo Number of subsequent objectIDs to change the property for .
* @ param prop The property to change .
* @ param buf The property value .
* @ return ChangeInfoResult .
*/
2010-08-28 20:21:09 +02:00
static ChangeInfoResult ObjectChangeInfo ( uint id , int numinfo , int prop , ByteReader * buf )
{
ChangeInfoResult ret = CIR_SUCCESS ;
2013-10-12 18:31:31 +02:00
if ( id + numinfo > NUM_OBJECTS_PER_GRF ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " ObjectChangeInfo: Too many objects loaded ({}), max ({}). Ignoring. " , id + numinfo , NUM_OBJECTS_PER_GRF ) ;
2010-08-28 20:21:09 +02:00
return CIR_INVALID_ID ;
}
/* Allocate object specs if they haven't been allocated already. */
2023-01-08 03:12:33 +01:00
if ( _cur . grffile - > objectspec . size ( ) < id + numinfo ) _cur . grffile - > objectspec . resize ( id + numinfo ) ;
2010-08-28 20:21:09 +02:00
for ( int i = 0 ; i < numinfo ; i + + ) {
2023-04-15 22:30:37 +02:00
ObjectSpec * spec = _cur . grffile - > objectspec [ id + i ] . get ( ) ;
2010-08-28 20:21:09 +02:00
2019-04-10 23:07:06 +02:00
if ( prop ! = 0x08 & & spec = = nullptr ) {
2010-08-28 20:21:09 +02:00
/* If the object property 08 is not yet set, ignore this property */
ChangeInfoResult cir = IgnoreObjectProperty ( prop , buf ) ;
if ( cir > ret ) ret = cir ;
continue ;
}
switch ( prop ) {
case 0x08 : { // Class ID
/* Allocate space for this object. */
2023-04-15 22:30:37 +02:00
if ( spec = = nullptr ) {
_cur . grffile - > objectspec [ id + i ] = std : : make_unique < ObjectSpec > ( ) ;
spec = _cur . grffile - > objectspec [ id + i ] . get ( ) ;
spec - > views = 1 ; // Default for NewGRFs that don't set it.
spec - > size = OBJECT_SIZE_1X1 ; // Default for NewGRFs that manage to not set it (1x1)
2010-12-10 22:30:26 +01:00
}
2010-08-28 20:21:09 +02:00
/* Swap classid because we read it in BE. */
uint32 classid = buf - > ReadDWord ( ) ;
2023-04-15 22:30:37 +02:00
spec - > cls_id = ObjectClass : : Allocate ( BSWAP32 ( classid ) ) ;
2010-08-28 20:21:09 +02:00
break ;
}
case 0x09 : { // Class name
2012-04-22 18:27:55 +02:00
ObjectClass * objclass = ObjectClass : : Get ( spec - > cls_id ) ;
2014-01-12 19:01:50 +01:00
AddStringForMapping ( buf - > ReadWord ( ) , & objclass - > name ) ;
2010-08-28 20:21:09 +02:00
break ;
}
case 0x0A : // Object name
2014-01-12 19:01:50 +01:00
AddStringForMapping ( buf - > ReadWord ( ) , & spec - > name ) ;
2010-08-28 20:21:09 +02:00
break ;
case 0x0B : // Climate mask
spec - > climate = buf - > ReadByte ( ) ;
break ;
case 0x0C : // Size
spec - > size = buf - > ReadByte ( ) ;
2021-08-31 22:40:05 +02:00
if ( GB ( spec - > size , 0 , 4 ) = = 0 | | GB ( spec - > size , 4 , 4 ) = = 0 ) {
2023-04-20 17:00:46 +02:00
GrfMsg ( 0 , " ObjectChangeInfo: Invalid object size requested (0x{:X}) for object id {}. Ignoring. " , spec - > size , id + i ) ;
2022-10-16 17:05:50 +02:00
spec - > size = OBJECT_SIZE_1X1 ;
2021-02-21 18:33:26 +01:00
}
2010-08-28 20:21:09 +02:00
break ;
case 0x0D : // Build cost multipler
spec - > build_cost_multiplier = buf - > ReadByte ( ) ;
spec - > clear_cost_multiplier = spec - > build_cost_multiplier ;
break ;
case 0x0E : // Introduction date
spec - > introduction_date = buf - > ReadDWord ( ) ;
break ;
case 0x0F : // End of life
spec - > end_of_life_date = buf - > ReadDWord ( ) ;
break ;
case 0x10 : // Flags
spec - > flags = ( ObjectFlags ) buf - > ReadWord ( ) ;
_loaded_newgrf_features . has_2CC | = ( spec - > flags & OBJECT_FLAG_2CC_COLOUR ) ! = 0 ;
break ;
case 0x11 : // Animation info
spec - > animation . frames = buf - > ReadByte ( ) ;
spec - > animation . status = buf - > ReadByte ( ) ;
break ;
case 0x12 : // Animation speed
spec - > animation . speed = buf - > ReadByte ( ) ;
break ;
case 0x13 : // Animation triggers
spec - > animation . triggers = buf - > ReadWord ( ) ;
break ;
case 0x14 : // Removal cost multiplier
spec - > clear_cost_multiplier = buf - > ReadByte ( ) ;
break ;
case 0x15 : // Callback mask
spec - > callback_mask = buf - > ReadWord ( ) ;
break ;
case 0x16 : // Building height
spec - > height = buf - > ReadByte ( ) ;
break ;
2010-12-10 22:30:26 +01:00
case 0x17 : // Views
spec - > views = buf - > ReadByte ( ) ;
if ( spec - > views ! = 1 & & spec - > views ! = 2 & & spec - > views ! = 4 ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " ObjectChangeInfo: Invalid number of views ({}) for object id {}. Ignoring. " , spec - > views , id + i ) ;
2010-12-10 22:30:26 +01:00
spec - > views = 1 ;
}
break ;
2013-10-17 23:45:40 +02:00
case 0x18 : // Amount placed on 256^2 map on map creation
spec - > generate_amount = buf - > ReadByte ( ) ;
break ;
2010-08-28 20:21:09 +02:00
default :
ret = CIR_UNKNOWN ;
break ;
}
}
return ret ;
}
2011-09-11 13:14:22 +02:00
/**
* Define properties for railtypes
* @ param id ID of the railtype .
* @ param numinfo Number of subsequent IDs to change the property for .
* @ param prop The property to change .
* @ param buf The property value .
* @ return ChangeInfoResult .
*/
2010-01-30 22:49:22 +01:00
static ChangeInfoResult RailTypeChangeInfo ( uint id , int numinfo , int prop , ByteReader * buf )
{
ChangeInfoResult ret = CIR_SUCCESS ;
extern RailtypeInfo _railtypes [ RAILTYPE_END ] ;
2010-02-03 09:02:07 +01:00
if ( id + numinfo > RAILTYPE_END ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " RailTypeChangeInfo: Rail type {} is invalid, max {}, ignoring " , id + numinfo , RAILTYPE_END ) ;
2010-02-03 09:02:07 +01:00
return CIR_INVALID_ID ;
}
2010-01-30 22:49:22 +01:00
for ( int i = 0 ; i < numinfo ; i + + ) {
2011-08-21 14:05:47 +02:00
RailType rt = _cur . grffile - > railtype_map [ id + i ] ;
2010-01-30 22:49:22 +01:00
if ( rt = = INVALID_RAILTYPE ) return CIR_INVALID_ID ;
RailtypeInfo * rti = & _railtypes [ rt ] ;
switch ( prop ) {
case 0x08 : // Label of rail type
/* Skipped here as this is loaded during reservation stage. */
buf - > ReadDWord ( ) ;
break ;
2014-01-12 19:01:50 +01:00
case 0x09 : { // Toolbar caption of railtype (sets name as well for backwards compatibility for grf ver < 8)
uint16 str = buf - > ReadWord ( ) ;
AddStringForMapping ( str , & rti - > strings . toolbar_caption ) ;
2011-11-08 20:23:56 +01:00
if ( _cur . grffile - > grf_version < 8 ) {
2014-01-12 19:01:50 +01:00
AddStringForMapping ( str , & rti - > strings . name ) ;
2011-11-08 20:23:56 +01:00
}
2010-01-30 22:49:22 +01:00
break ;
2014-01-12 19:01:50 +01:00
}
2010-01-30 22:49:22 +01:00
case 0x0A : // Menu text of railtype
2014-01-12 19:01:50 +01:00
AddStringForMapping ( buf - > ReadWord ( ) , & rti - > strings . menu_text ) ;
2010-01-30 22:49:22 +01:00
break ;
case 0x0B : // Build window caption
2014-01-12 19:01:50 +01:00
AddStringForMapping ( buf - > ReadWord ( ) , & rti - > strings . build_caption ) ;
2010-01-30 22:49:22 +01:00
break ;
case 0x0C : // Autoreplace text
2014-01-12 19:01:50 +01:00
AddStringForMapping ( buf - > ReadWord ( ) , & rti - > strings . replace_text ) ;
2010-01-30 22:49:22 +01:00
break ;
case 0x0D : // New locomotive text
2014-01-12 19:01:50 +01:00
AddStringForMapping ( buf - > ReadWord ( ) , & rti - > strings . new_loco ) ;
2010-01-30 22:49:22 +01:00
break ;
case 0x0E : // Compatible railtype list
case 0x0F : // Powered railtype list
2011-01-18 22:30:59 +01:00
case 0x18 : // Railtype list required for date introduction
2011-01-18 22:28:07 +01:00
case 0x19 : // Introduced railtype list
2010-01-30 22:49:22 +01:00
{
/* Rail type compatibility bits are added to the existing bits
* to allow multiple GRFs to modify compatibility with the
* default rail types . */
int n = buf - > ReadByte ( ) ;
for ( int j = 0 ; j ! = n ; j + + ) {
RailTypeLabel label = buf - > ReadDWord ( ) ;
2023-01-28 20:06:51 +01:00
RailType resolved_rt = GetRailTypeByLabel ( BSWAP32 ( label ) , false ) ;
if ( resolved_rt ! = INVALID_RAILTYPE ) {
2011-01-18 22:28:07 +01:00
switch ( prop ) {
2023-01-28 20:06:51 +01:00
case 0x0F : SetBit ( rti - > powered_railtypes , resolved_rt ) ; FALLTHROUGH ; // Powered implies compatible.
case 0x0E : SetBit ( rti - > compatible_railtypes , resolved_rt ) ; break ;
case 0x18 : SetBit ( rti - > introduction_required_railtypes , resolved_rt ) ; break ;
case 0x19 : SetBit ( rti - > introduces_railtypes , resolved_rt ) ; break ;
2010-01-30 22:49:22 +01:00
}
}
}
break ;
}
case 0x10 : // Rail Type flags
rti - > flags = ( RailTypeFlags ) buf - > ReadByte ( ) ;
break ;
case 0x11 : // Curve speed advantage
rti - > curve_speed = buf - > ReadByte ( ) ;
break ;
case 0x12 : // Station graphic
2011-05-08 17:58:59 +02:00
rti - > fallback_railtype = Clamp ( buf - > ReadByte ( ) , 0 , 2 ) ;
2010-01-30 22:49:22 +01:00
break ;
case 0x13 : // Construction cost factor
2010-03-03 19:58:32 +01:00
rti - > cost_multiplier = buf - > ReadWord ( ) ;
2010-01-30 22:49:22 +01:00
break ;
case 0x14 : // Speed limit
2010-01-31 14:17:29 +01:00
rti - > max_speed = buf - > ReadWord ( ) ;
2010-01-30 22:49:22 +01:00
break ;
case 0x15 : // Acceleration model
rti - > acceleration_type = Clamp ( buf - > ReadByte ( ) , 0 , 2 ) ;
break ;
2010-03-03 20:07:34 +01:00
case 0x16 : // Map colour
2011-05-04 19:12:37 +02:00
rti - > map_colour = buf - > ReadByte ( ) ;
2010-03-03 20:07:34 +01:00
break ;
2011-01-18 22:30:59 +01:00
case 0x17 : // Introduction date
rti - > introduction_date = buf - > ReadDWord ( ) ;
break ;
2011-01-20 13:22:38 +01:00
case 0x1A : // Sort order
rti - > sorting_order = buf - > ReadByte ( ) ;
break ;
2011-11-08 20:23:56 +01:00
case 0x1B : // Name of railtype (overridden by prop 09 for grf ver < 8)
2014-01-12 19:01:50 +01:00
AddStringForMapping ( buf - > ReadWord ( ) , & rti - > strings . name ) ;
2011-11-07 20:35:02 +01:00
break ;
2011-12-04 00:40:46 +01:00
case 0x1C : // Maintenance cost factor
rti - > maintenance_multiplier = buf - > ReadWord ( ) ;
break ;
2012-01-05 20:40:34 +01:00
case 0x1D : // Alternate rail type label list
/* Skipped here as this is loaded during reservation stage. */
for ( int j = buf - > ReadByte ( ) ; j ! = 0 ; j - - ) buf - > ReadDWord ( ) ;
break ;
2010-01-30 22:49:22 +01:00
default :
ret = CIR_UNKNOWN ;
break ;
}
}
return ret ;
}
static ChangeInfoResult RailTypeReserveInfo ( uint id , int numinfo , int prop , ByteReader * buf )
{
ChangeInfoResult ret = CIR_SUCCESS ;
2012-01-05 20:40:34 +01:00
extern RailtypeInfo _railtypes [ RAILTYPE_END ] ;
2010-02-03 09:02:07 +01:00
if ( id + numinfo > RAILTYPE_END ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " RailTypeReserveInfo: Rail type {} is invalid, max {}, ignoring " , id + numinfo , RAILTYPE_END ) ;
2010-02-03 09:02:07 +01:00
return CIR_INVALID_ID ;
}
2010-01-30 22:49:22 +01:00
for ( int i = 0 ; i < numinfo ; i + + ) {
switch ( prop ) {
case 0x08 : // Label of rail type
{
RailTypeLabel rtl = buf - > ReadDWord ( ) ;
rtl = BSWAP32 ( rtl ) ;
2012-01-05 20:40:34 +01:00
RailType rt = GetRailTypeByLabel ( rtl , false ) ;
2010-01-30 22:49:22 +01:00
if ( rt = = INVALID_RAILTYPE ) {
/* Set up new rail type */
rt = AllocateRailType ( rtl ) ;
}
2011-08-21 14:05:47 +02:00
_cur . grffile - > railtype_map [ id + i ] = rt ;
2010-01-30 22:49:22 +01:00
break ;
}
2011-11-07 20:35:02 +01:00
case 0x09 : // Toolbar caption of railtype
2010-01-30 22:49:22 +01:00
case 0x0A : // Menu text
case 0x0B : // Build window caption
case 0x0C : // Autoreplace text
case 0x0D : // New loco
2010-03-03 19:58:32 +01:00
case 0x13 : // Construction cost
2010-01-30 22:49:22 +01:00
case 0x14 : // Speed limit
2011-11-07 20:35:02 +01:00
case 0x1B : // Name of railtype
2011-12-04 00:40:46 +01:00
case 0x1C : // Maintenance cost factor
2010-01-30 22:49:22 +01:00
buf - > ReadWord ( ) ;
break ;
2012-01-05 20:40:34 +01:00
case 0x1D : // Alternate rail type label list
if ( _cur . grffile - > railtype_map [ id + i ] ! = INVALID_RAILTYPE ) {
int n = buf - > ReadByte ( ) ;
for ( int j = 0 ; j ! = n ; j + + ) {
2019-02-18 23:39:06 +01:00
_railtypes [ _cur . grffile - > railtype_map [ id + i ] ] . alternate_labels . push_back ( BSWAP32 ( buf - > ReadDWord ( ) ) ) ;
2012-01-05 20:40:34 +01:00
}
break ;
}
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " RailTypeReserveInfo: Ignoring property 1D for rail type {} because no label was set " , id + i ) ;
2017-08-13 20:38:42 +02:00
FALLTHROUGH ;
2012-01-05 20:40:34 +01:00
2010-01-30 22:49:22 +01:00
case 0x0E : // Compatible railtype list
case 0x0F : // Powered railtype list
2011-01-18 22:30:59 +01:00
case 0x18 : // Railtype list required for date introduction
2011-01-18 22:28:07 +01:00
case 0x19 : // Introduced railtype list
2010-01-30 22:49:22 +01:00
for ( int j = buf - > ReadByte ( ) ; j ! = 0 ; j - - ) buf - > ReadDWord ( ) ;
break ;
case 0x10 : // Rail Type flags
case 0x11 : // Curve speed advantage
case 0x12 : // Station graphic
case 0x15 : // Acceleration model
2010-03-03 20:07:34 +01:00
case 0x16 : // Map colour
2011-01-20 13:22:38 +01:00
case 0x1A : // Sort order
2010-01-30 22:49:22 +01:00
buf - > ReadByte ( ) ;
break ;
2011-01-18 22:30:59 +01:00
case 0x17 : // Introduction date
buf - > ReadDWord ( ) ;
break ;
2010-01-30 22:49:22 +01:00
default :
ret = CIR_UNKNOWN ;
break ;
}
}
return ret ;
}
2019-04-06 08:46:15 +02:00
/**
* Define properties for roadtypes
* @ param id ID of the roadtype .
* @ param numinfo Number of subsequent IDs to change the property for .
* @ param prop The property to change .
* @ param buf The property value .
* @ return ChangeInfoResult .
*/
static ChangeInfoResult RoadTypeChangeInfo ( uint id , int numinfo , int prop , ByteReader * buf , RoadTramType rtt )
{
ChangeInfoResult ret = CIR_SUCCESS ;
extern RoadTypeInfo _roadtypes [ ROADTYPE_END ] ;
RoadType * type_map = ( rtt = = RTT_TRAM ) ? _cur . grffile - > tramtype_map : _cur . grffile - > roadtype_map ;
if ( id + numinfo > ROADTYPE_END ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " RoadTypeChangeInfo: Road type {} is invalid, max {}, ignoring " , id + numinfo , ROADTYPE_END ) ;
2019-04-06 08:46:15 +02:00
return CIR_INVALID_ID ;
}
for ( int i = 0 ; i < numinfo ; i + + ) {
RoadType rt = type_map [ id + i ] ;
if ( rt = = INVALID_ROADTYPE ) return CIR_INVALID_ID ;
RoadTypeInfo * rti = & _roadtypes [ rt ] ;
switch ( prop ) {
case 0x08 : // Label of road type
/* Skipped here as this is loaded during reservation stage. */
buf - > ReadDWord ( ) ;
break ;
case 0x09 : { // Toolbar caption of roadtype (sets name as well for backwards compatibility for grf ver < 8)
uint16 str = buf - > ReadWord ( ) ;
AddStringForMapping ( str , & rti - > strings . toolbar_caption ) ;
break ;
}
case 0x0A : // Menu text of roadtype
AddStringForMapping ( buf - > ReadWord ( ) , & rti - > strings . menu_text ) ;
break ;
case 0x0B : // Build window caption
AddStringForMapping ( buf - > ReadWord ( ) , & rti - > strings . build_caption ) ;
break ;
case 0x0C : // Autoreplace text
AddStringForMapping ( buf - > ReadWord ( ) , & rti - > strings . replace_text ) ;
break ;
case 0x0D : // New engine text
AddStringForMapping ( buf - > ReadWord ( ) , & rti - > strings . new_engine ) ;
break ;
case 0x0F : // Powered roadtype list
case 0x18 : // Roadtype list required for date introduction
case 0x19 : { // Introduced roadtype list
/* Road type compatibility bits are added to the existing bits
* to allow multiple GRFs to modify compatibility with the
* default road types . */
int n = buf - > ReadByte ( ) ;
for ( int j = 0 ; j ! = n ; j + + ) {
RoadTypeLabel label = buf - > ReadDWord ( ) ;
2023-01-28 20:06:51 +01:00
RoadType resolved_rt = GetRoadTypeByLabel ( BSWAP32 ( label ) , false ) ;
if ( resolved_rt ! = INVALID_ROADTYPE ) {
2019-04-06 08:46:15 +02:00
switch ( prop ) {
2023-01-28 20:06:51 +01:00
case 0x0F : SetBit ( rti - > powered_roadtypes , resolved_rt ) ; break ;
case 0x18 : SetBit ( rti - > introduction_required_roadtypes , resolved_rt ) ; break ;
case 0x19 : SetBit ( rti - > introduces_roadtypes , resolved_rt ) ; break ;
2019-04-06 08:46:15 +02:00
}
}
}
break ;
}
case 0x10 : // Road Type flags
rti - > flags = ( RoadTypeFlags ) buf - > ReadByte ( ) ;
break ;
case 0x13 : // Construction cost factor
rti - > cost_multiplier = buf - > ReadWord ( ) ;
break ;
case 0x14 : // Speed limit
rti - > max_speed = buf - > ReadWord ( ) ;
break ;
case 0x16 : // Map colour
rti - > map_colour = buf - > ReadByte ( ) ;
break ;
case 0x17 : // Introduction date
rti - > introduction_date = buf - > ReadDWord ( ) ;
break ;
case 0x1A : // Sort order
rti - > sorting_order = buf - > ReadByte ( ) ;
break ;
case 0x1B : // Name of roadtype
AddStringForMapping ( buf - > ReadWord ( ) , & rti - > strings . name ) ;
break ;
case 0x1C : // Maintenance cost factor
rti - > maintenance_multiplier = buf - > ReadWord ( ) ;
break ;
case 0x1D : // Alternate road type label list
/* Skipped here as this is loaded during reservation stage. */
for ( int j = buf - > ReadByte ( ) ; j ! = 0 ; j - - ) buf - > ReadDWord ( ) ;
break ;
default :
ret = CIR_UNKNOWN ;
break ;
}
}
return ret ;
}
static ChangeInfoResult RoadTypeChangeInfo ( uint id , int numinfo , int prop , ByteReader * buf )
{
return RoadTypeChangeInfo ( id , numinfo , prop , buf , RTT_ROAD ) ;
}
static ChangeInfoResult TramTypeChangeInfo ( uint id , int numinfo , int prop , ByteReader * buf )
{
return RoadTypeChangeInfo ( id , numinfo , prop , buf , RTT_TRAM ) ;
}
static ChangeInfoResult RoadTypeReserveInfo ( uint id , int numinfo , int prop , ByteReader * buf , RoadTramType rtt )
{
ChangeInfoResult ret = CIR_SUCCESS ;
extern RoadTypeInfo _roadtypes [ ROADTYPE_END ] ;
RoadType * type_map = ( rtt = = RTT_TRAM ) ? _cur . grffile - > tramtype_map : _cur . grffile - > roadtype_map ;
if ( id + numinfo > ROADTYPE_END ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " RoadTypeReserveInfo: Road type {} is invalid, max {}, ignoring " , id + numinfo , ROADTYPE_END ) ;
2019-04-06 08:46:15 +02:00
return CIR_INVALID_ID ;
}
for ( int i = 0 ; i < numinfo ; i + + ) {
switch ( prop ) {
case 0x08 : { // Label of road type
RoadTypeLabel rtl = buf - > ReadDWord ( ) ;
rtl = BSWAP32 ( rtl ) ;
RoadType rt = GetRoadTypeByLabel ( rtl , false ) ;
if ( rt = = INVALID_ROADTYPE ) {
/* Set up new road type */
rt = AllocateRoadType ( rtl , rtt ) ;
} else if ( GetRoadTramType ( rt ) ! = rtt ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " RoadTypeReserveInfo: Road type {} is invalid type (road/tram), ignoring " , id + numinfo ) ;
2019-04-06 08:46:15 +02:00
return CIR_INVALID_ID ;
}
type_map [ id + i ] = rt ;
break ;
}
case 0x09 : // Toolbar caption of roadtype
case 0x0A : // Menu text
case 0x0B : // Build window caption
case 0x0C : // Autoreplace text
case 0x0D : // New loco
case 0x13 : // Construction cost
case 0x14 : // Speed limit
case 0x1B : // Name of roadtype
case 0x1C : // Maintenance cost factor
buf - > ReadWord ( ) ;
break ;
case 0x1D : // Alternate road type label list
if ( type_map [ id + i ] ! = INVALID_ROADTYPE ) {
int n = buf - > ReadByte ( ) ;
for ( int j = 0 ; j ! = n ; j + + ) {
_roadtypes [ type_map [ id + i ] ] . alternate_labels . push_back ( BSWAP32 ( buf - > ReadDWord ( ) ) ) ;
}
break ;
}
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " RoadTypeReserveInfo: Ignoring property 1D for road type {} because no label was set " , id + i ) ;
2019-04-06 08:46:15 +02:00
/* FALL THROUGH */
case 0x0F : // Powered roadtype list
case 0x18 : // Roadtype list required for date introduction
case 0x19 : // Introduced roadtype list
for ( int j = buf - > ReadByte ( ) ; j ! = 0 ; j - - ) buf - > ReadDWord ( ) ;
break ;
case 0x10 : // Road Type flags
case 0x16 : // Map colour
case 0x1A : // Sort order
buf - > ReadByte ( ) ;
break ;
case 0x17 : // Introduction date
buf - > ReadDWord ( ) ;
break ;
default :
ret = CIR_UNKNOWN ;
break ;
}
}
return ret ;
}
static ChangeInfoResult RoadTypeReserveInfo ( uint id , int numinfo , int prop , ByteReader * buf )
{
return RoadTypeReserveInfo ( id , numinfo , prop , buf , RTT_ROAD ) ;
}
static ChangeInfoResult TramTypeReserveInfo ( uint id , int numinfo , int prop , ByteReader * buf )
{
return RoadTypeReserveInfo ( id , numinfo , prop , buf , RTT_TRAM ) ;
}
2010-02-22 15:16:19 +01:00
static ChangeInfoResult AirportTilesChangeInfo ( uint airtid , int numinfo , int prop , ByteReader * buf )
{
ChangeInfoResult ret = CIR_SUCCESS ;
2013-10-12 18:34:59 +02:00
if ( airtid + numinfo > NUM_AIRPORTTILES_PER_GRF ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " AirportTileChangeInfo: Too many airport tiles loaded ({}), max ({}). Ignoring. " , airtid + numinfo , NUM_AIRPORTTILES_PER_GRF ) ;
2010-02-22 15:16:19 +01:00
return CIR_INVALID_ID ;
}
/* Allocate airport tile specs if they haven't been allocated already. */
2023-01-08 03:12:33 +01:00
if ( _cur . grffile - > airtspec . size ( ) < airtid + numinfo ) _cur . grffile - > airtspec . resize ( airtid + numinfo ) ;
2010-02-22 15:16:19 +01:00
for ( int i = 0 ; i < numinfo ; i + + ) {
2023-04-15 22:30:37 +02:00
AirportTileSpec * tsp = _cur . grffile - > airtspec [ airtid + i ] . get ( ) ;
2010-02-22 15:16:19 +01:00
2019-04-10 23:07:06 +02:00
if ( prop ! = 0x08 & & tsp = = nullptr ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " AirportTileChangeInfo: Attempt to modify undefined airport tile {}. Ignoring. " , airtid + i ) ;
2010-02-22 15:16:19 +01:00
return CIR_INVALID_ID ;
}
switch ( prop ) {
case 0x08 : { // Substitute airport tile type
byte subs_id = buf - > ReadByte ( ) ;
if ( subs_id > = NEW_AIRPORTTILE_OFFSET ) {
/* The substitute id must be one of the original airport tiles. */
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " AirportTileChangeInfo: Attempt to use new airport tile {} as substitute airport tile for {}. Ignoring. " , subs_id , airtid + i ) ;
2010-02-22 15:16:19 +01:00
continue ;
}
/* Allocate space for this airport tile. */
2023-04-15 22:30:37 +02:00
if ( tsp = = nullptr ) {
_cur . grffile - > airtspec [ airtid + i ] = std : : make_unique < AirportTileSpec > ( * AirportTileSpec : : Get ( subs_id ) ) ;
tsp = _cur . grffile - > airtspec [ airtid + i ] . get ( ) ;
2010-02-22 15:16:19 +01:00
tsp - > enabled = true ;
2010-08-26 17:31:40 +02:00
tsp - > animation . status = ANIM_STATUS_NO_ANIMATION ;
2010-02-22 15:16:19 +01:00
tsp - > grf_prop . local_id = airtid + i ;
tsp - > grf_prop . subst_id = subs_id ;
2011-08-21 14:05:47 +02:00
tsp - > grf_prop . grffile = _cur . grffile ;
_airporttile_mngr . AddEntityID ( airtid + i , _cur . grffile - > grfid , subs_id ) ; // pre-reserve the tile slot
2010-02-22 15:16:19 +01:00
}
2010-08-01 20:53:30 +02:00
break ;
}
2010-02-22 15:16:19 +01:00
case 0x09 : { // Airport tile override
byte override = buf - > ReadByte ( ) ;
/* The airport tile being overridden must be an original airport tile. */
if ( override > = NEW_AIRPORTTILE_OFFSET ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " AirportTileChangeInfo: Attempt to override new airport tile {} with airport tile id {}. Ignoring. " , override , airtid + i ) ;
2010-02-22 15:16:19 +01:00
continue ;
}
2011-08-21 14:05:47 +02:00
_airporttile_mngr . Add ( airtid + i , _cur . grffile - > grfid , override ) ;
2010-08-01 20:53:30 +02:00
break ;
}
2010-02-22 15:16:19 +01:00
2010-08-26 13:18:32 +02:00
case 0x0E : // Callback mask
tsp - > callback_mask = buf - > ReadByte ( ) ;
2010-02-22 15:16:19 +01:00
break ;
case 0x0F : // Animation information
2010-08-26 17:31:40 +02:00
tsp - > animation . frames = buf - > ReadByte ( ) ;
tsp - > animation . status = buf - > ReadByte ( ) ;
2010-02-22 15:16:19 +01:00
break ;
case 0x10 : // Animation speed
2010-08-26 17:31:40 +02:00
tsp - > animation . speed = buf - > ReadByte ( ) ;
2010-02-22 15:16:19 +01:00
break ;
case 0x11 : // Animation triggers
2010-08-26 17:31:40 +02:00
tsp - > animation . triggers = buf - > ReadByte ( ) ;
2010-02-22 15:16:19 +01:00
break ;
default :
ret = CIR_UNKNOWN ;
break ;
}
}
return ret ;
}
2022-11-06 16:01:27 +01:00
/**
* Ignore properties for roadstops
* @ param prop The property to ignore .
* @ param buf The property value .
* @ return ChangeInfoResult .
*/
static ChangeInfoResult IgnoreRoadStopProperty ( uint prop , ByteReader * buf )
{
ChangeInfoResult ret = CIR_SUCCESS ;
switch ( prop ) {
case 0x09 :
case 0x0C :
2023-03-01 01:19:39 +01:00
case 0x0F :
case 0x11 :
2022-11-06 16:01:27 +01:00
buf - > ReadByte ( ) ;
break ;
case 0x0A :
case 0x0B :
2023-03-01 01:19:39 +01:00
case 0x0E :
case 0x10 :
case 0x15 :
2022-11-06 16:01:27 +01:00
buf - > ReadWord ( ) ;
break ;
case 0x08 :
case 0x0D :
2023-03-01 01:19:39 +01:00
case 0x12 :
2022-11-06 16:01:27 +01:00
buf - > ReadDWord ( ) ;
break ;
default :
ret = CIR_UNKNOWN ;
break ;
}
return ret ;
}
static ChangeInfoResult RoadStopChangeInfo ( uint id , int numinfo , int prop , ByteReader * buf )
{
ChangeInfoResult ret = CIR_SUCCESS ;
2023-04-06 21:29:52 +02:00
if ( id + numinfo > NUM_ROADSTOPS_PER_GRF ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " RoadStopChangeInfo: RoadStop {} is invalid, max {}, ignoring " , id + numinfo , NUM_ROADSTOPS_PER_GRF ) ;
2022-11-06 16:01:27 +01:00
return CIR_INVALID_ID ;
}
2023-01-08 03:12:33 +01:00
if ( _cur . grffile - > roadstops . size ( ) < id + numinfo ) _cur . grffile - > roadstops . resize ( id + numinfo ) ;
2022-11-06 16:01:27 +01:00
for ( int i = 0 ; i < numinfo ; i + + ) {
2023-04-15 22:30:37 +02:00
RoadStopSpec * rs = _cur . grffile - > roadstops [ id + i ] . get ( ) ;
2022-11-06 16:01:27 +01:00
if ( rs = = nullptr & & prop ! = 0x08 ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " RoadStopChangeInfo: Attempt to modify undefined road stop {}, ignoring " , id + i ) ;
2022-11-06 16:01:27 +01:00
ChangeInfoResult cir = IgnoreRoadStopProperty ( prop , buf ) ;
if ( cir > ret ) ret = cir ;
continue ;
}
switch ( prop ) {
case 0x08 : { // Road Stop Class ID
2023-04-15 22:30:37 +02:00
if ( rs = = nullptr ) {
_cur . grffile - > roadstops [ id + i ] = std : : make_unique < RoadStopSpec > ( ) ;
rs = _cur . grffile - > roadstops [ id + i ] . get ( ) ;
2022-11-06 16:01:27 +01:00
}
uint32 classid = buf - > ReadDWord ( ) ;
2023-04-15 22:30:37 +02:00
rs - > cls_id = RoadStopClass : : Allocate ( BSWAP32 ( classid ) ) ;
rs - > spec_id = id + i ;
2022-11-06 16:01:27 +01:00
break ;
}
case 0x09 : // Road stop type
rs - > stop_type = ( RoadStopAvailabilityType ) buf - > ReadByte ( ) ;
break ;
case 0x0A : // Road Stop Name
AddStringForMapping ( buf - > ReadWord ( ) , & rs - > name ) ;
break ;
case 0x0B : // Road Stop Class name
AddStringForMapping ( buf - > ReadWord ( ) , & RoadStopClass : : Get ( rs - > cls_id ) - > name ) ;
break ;
case 0x0C : // The draw mode
rs - > draw_mode = ( RoadStopDrawMode ) buf - > ReadByte ( ) ;
break ;
case 0x0D : // Cargo types for random triggers
rs - > cargo_triggers = TranslateRefitMask ( buf - > ReadDWord ( ) ) ;
break ;
case 0x0E : // Animation info
rs - > animation . frames = buf - > ReadByte ( ) ;
rs - > animation . status = buf - > ReadByte ( ) ;
break ;
case 0x0F : // Animation speed
rs - > animation . speed = buf - > ReadByte ( ) ;
break ;
case 0x10 : // Animation triggers
rs - > animation . triggers = buf - > ReadWord ( ) ;
break ;
case 0x11 : // Callback mask
rs - > callback_mask = buf - > ReadByte ( ) ;
break ;
case 0x12 : // General flags
rs - > flags = ( uint8 ) buf - > ReadDWord ( ) ; // Future-proofing, size this as 4 bytes, but we only need one byte's worth of flags at present
break ;
case 0x15 : // Cost multipliers
rs - > build_cost_multiplier = buf - > ReadByte ( ) ;
rs - > clear_cost_multiplier = buf - > ReadByte ( ) ;
break ;
default :
ret = CIR_UNKNOWN ;
break ;
}
}
return ret ;
}
2009-01-09 21:14:07 +01:00
static bool HandleChangeInfoResult ( const char * caller , ChangeInfoResult cir , uint8 feature , uint8 property )
{
switch ( cir ) {
default : NOT_REACHED ( ) ;
2011-05-29 18:56:22 +02:00
case CIR_DISABLED :
/* Error has already been printed; just stop parsing */
return true ;
2009-01-09 21:14:07 +01:00
case CIR_SUCCESS :
return false ;
case CIR_UNHANDLED :
2023-04-20 17:00:46 +02:00
GrfMsg ( 1 , " {}: Ignoring property 0x{:02X} of feature 0x{:02X} (not implemented) " , caller , property , feature ) ;
2009-01-09 21:14:07 +01:00
return false ;
case CIR_UNKNOWN :
2023-04-20 17:00:46 +02:00
GrfMsg ( 0 , " {}: Unknown property 0x{:02X} of feature 0x{:02X}, disabling " , caller , property , feature ) ;
2017-08-13 20:38:42 +02:00
FALLTHROUGH ;
2009-01-09 21:14:07 +01:00
2012-01-15 19:18:44 +01:00
case CIR_INVALID_ID : {
2009-01-09 21:14:07 +01:00
/* No debug message for an invalid ID, as it has already been output */
2012-01-15 19:18:44 +01:00
GRFError * error = DisableGrf ( cir = = CIR_INVALID_ID ? STR_NEWGRF_ERROR_INVALID_ID : STR_NEWGRF_ERROR_UNKNOWN_PROPERTY ) ;
if ( cir ! = CIR_INVALID_ID ) error - > param_value [ 1 ] = property ;
2009-01-09 21:14:07 +01:00
return true ;
2012-01-15 19:18:44 +01:00
}
2009-01-09 21:14:07 +01:00
}
}
2004-11-12 16:15:56 +01:00
/* Action 0x00 */
2010-01-22 14:02:15 +01:00
static void FeatureChangeInfo ( ByteReader * buf )
2004-08-09 19:04:08 +02:00
{
/* <00> <feature> <num-props> <num-info> <id> (<property <new-info>)...
*
2009-11-24 14:15:58 +01:00
* B feature
2004-08-09 19:04:08 +02:00
* B num - props how many properties to change per vehicle / station
* B num - info how many vehicles / stations to change
2007-11-16 03:25:56 +01:00
* E id ID of first vehicle / station to change , if num - info is
2004-08-09 19:04:08 +02:00
* greater than one , this one and the following
* vehicles / stations will be changed
* B property what property to change , depends on the feature
* V new - info new bytes of info ( variable size ; depends on properties ) */
2005-09-20 11:38:09 +02:00
static const VCI_Handler handler [ ] = {
2010-04-24 13:52:58 +02:00
/* GSF_TRAINS */ RailVehicleChangeInfo ,
/* GSF_ROADVEHICLES */ RoadVehicleChangeInfo ,
/* GSF_SHIPS */ ShipVehicleChangeInfo ,
/* GSF_AIRCRAFT */ AircraftVehicleChangeInfo ,
/* GSF_STATIONS */ StationChangeInfo ,
/* GSF_CANALS */ CanalChangeInfo ,
/* GSF_BRIDGES */ BridgeChangeInfo ,
/* GSF_HOUSES */ TownHouseChangeInfo ,
/* GSF_GLOBALVAR */ GlobalVarChangeInfo ,
/* GSF_INDUSTRYTILES */ IndustrytilesChangeInfo ,
/* GSF_INDUSTRIES */ IndustriesChangeInfo ,
2019-04-10 23:07:06 +02:00
/* GSF_CARGOES */ nullptr , // Cargo is handled during reservation
2010-04-24 13:52:58 +02:00
/* GSF_SOUNDFX */ SoundEffectChangeInfo ,
/* GSF_AIRPORTS */ AirportChangeInfo ,
2019-04-10 23:07:06 +02:00
/* GSF_SIGNALS */ nullptr ,
2010-08-28 20:21:09 +02:00
/* GSF_OBJECTS */ ObjectChangeInfo ,
2010-04-24 13:52:58 +02:00
/* GSF_RAILTYPES */ RailTypeChangeInfo ,
/* GSF_AIRPORTTILES */ AirportTilesChangeInfo ,
2019-04-06 08:46:15 +02:00
/* GSF_ROADTYPES */ RoadTypeChangeInfo ,
/* GSF_TRAMTYPES */ TramTypeChangeInfo ,
2022-11-06 16:01:27 +01:00
/* GSF_ROADSTOPS */ RoadStopChangeInfo ,
2004-08-09 19:04:08 +02:00
} ;
2021-05-10 23:02:44 +02:00
static_assert ( GSF_END = = lengthof ( handler ) ) ;
2004-11-12 19:43:12 +01:00
2010-01-23 14:17:30 +01:00
uint8 feature = buf - > ReadByte ( ) ;
uint8 numprops = buf - > ReadByte ( ) ;
uint numinfo = buf - > ReadByte ( ) ;
2010-03-10 08:31:08 +01:00
uint engine = buf - > ReadExtendedByte ( ) ;
2004-11-12 16:36:49 +01:00
2019-03-18 03:05:06 +01:00
if ( feature > = GSF_END ) {
2023-04-20 17:00:46 +02:00
GrfMsg ( 1 , " FeatureChangeInfo: Unsupported feature 0x{:02X}, skipping " , feature ) ;
2019-03-18 03:05:06 +01:00
return ;
}
2023-04-20 17:00:46 +02:00
GrfMsg ( 6 , " FeatureChangeInfo: Feature 0x{:02X}, {} properties, to apply to {}+{} " ,
2004-11-13 19:01:33 +01:00
feature , numprops , engine , numinfo ) ;
2021-05-10 23:02:44 +02:00
if ( handler [ feature ] = = nullptr ) {
2023-04-20 17:00:46 +02:00
if ( feature ! = GSF_CARGOES ) GrfMsg ( 1 , " FeatureChangeInfo: Unsupported feature 0x{:02X}, skipping " , feature ) ;
2004-11-14 11:03:21 +01:00
return ;
}
2009-11-24 14:15:58 +01:00
/* Mark the feature as used by the grf */
2011-08-21 14:05:47 +02:00
SetBit ( _cur . grffile - > grf_features , feature ) ;
2009-11-24 14:15:58 +01:00
2010-01-22 14:02:15 +01:00
while ( numprops - - & & buf - > HasData ( ) ) {
2010-01-23 14:17:30 +01:00
uint8 prop = buf - > ReadByte ( ) ;
2004-08-09 19:04:08 +02:00
2010-01-22 14:02:15 +01:00
ChangeInfoResult cir = handler [ feature ] ( engine , numinfo , prop , buf ) ;
2009-01-09 21:14:07 +01:00
if ( HandleChangeInfoResult ( " FeatureChangeInfo " , cir , feature , prop ) ) return ;
2004-08-09 19:04:08 +02:00
}
}
2007-01-04 20:24:42 +01:00
/* Action 0x00 (GLS_SAFETYSCAN) */
2010-01-22 14:02:15 +01:00
static void SafeChangeInfo ( ByteReader * buf )
2007-01-04 20:24:42 +01:00
{
2010-01-23 14:17:30 +01:00
uint8 feature = buf - > ReadByte ( ) ;
uint8 numprops = buf - > ReadByte ( ) ;
uint numinfo = buf - > ReadByte ( ) ;
2010-03-10 08:31:08 +01:00
buf - > ReadExtendedByte ( ) ; // id
2007-01-04 20:24:42 +01:00
2010-04-24 13:52:58 +02:00
if ( feature = = GSF_BRIDGES & & numprops = = 1 ) {
2010-01-23 14:17:30 +01:00
uint8 prop = buf - > ReadByte ( ) ;
2007-01-04 20:24:42 +01:00
/* Bridge property 0x0D is redefinition of sprite layout tables, which
* is considered safe . */
if ( prop = = 0x0D ) return ;
2009-03-14 20:38:19 +01:00
} else if ( feature = = GSF_GLOBALVAR & & numprops = = 1 ) {
2010-01-23 14:17:30 +01:00
uint8 prop = buf - > ReadByte ( ) ;
2009-03-14 20:38:19 +01:00
/* Engine ID Mappings are safe, if the source is static */
if ( prop = = 0x11 ) {
bool is_safe = true ;
for ( uint i = 0 ; i < numinfo ; i + + ) {
2010-01-23 14:17:30 +01:00
uint32 s = buf - > ReadDWord ( ) ;
buf - > ReadDWord ( ) ; // dest
2009-03-14 20:38:19 +01:00
const GRFConfig * grfconfig = GetGRFConfig ( s ) ;
2019-04-10 23:07:06 +02:00
if ( grfconfig ! = nullptr & & ! HasBit ( grfconfig - > flags , GCF_STATIC ) ) {
2009-03-14 20:38:19 +01:00
is_safe = false ;
break ;
}
}
if ( is_safe ) return ;
}
2007-01-04 20:24:42 +01:00
}
2011-08-21 14:05:47 +02:00
SetBit ( _cur . grfconfig - > flags , GCF_UNSAFE ) ;
2007-01-04 20:24:42 +01:00
/* Skip remainder of GRF */
2011-08-21 14:05:47 +02:00
_cur . skip_sprites = - 1 ;
2007-01-04 20:24:42 +01:00
}
2007-03-23 00:19:40 +01:00
/* Action 0x00 (GLS_RESERVE) */
2010-01-22 14:02:15 +01:00
static void ReserveChangeInfo ( ByteReader * buf )
2007-03-23 00:19:40 +01:00
{
2010-01-23 14:17:30 +01:00
uint8 feature = buf - > ReadByte ( ) ;
2007-03-23 00:19:40 +01:00
2019-04-06 08:46:15 +02:00
if ( feature ! = GSF_CARGOES & & feature ! = GSF_GLOBALVAR & & feature ! = GSF_RAILTYPES & & feature ! = GSF_ROADTYPES & & feature ! = GSF_TRAMTYPES ) return ;
2007-03-23 00:19:40 +01:00
2010-01-23 14:17:30 +01:00
uint8 numprops = buf - > ReadByte ( ) ;
uint8 numinfo = buf - > ReadByte ( ) ;
2010-03-10 08:31:08 +01:00
uint8 index = buf - > ReadExtendedByte ( ) ;
2007-03-23 00:19:40 +01:00
2010-01-22 14:02:15 +01:00
while ( numprops - - & & buf - > HasData ( ) ) {
2010-01-23 14:17:30 +01:00
uint8 prop = buf - > ReadByte ( ) ;
2009-01-09 21:14:07 +01:00
ChangeInfoResult cir = CIR_SUCCESS ;
2007-03-23 00:19:40 +01:00
2007-11-22 23:37:06 +01:00
switch ( feature ) {
default : NOT_REACHED ( ) ;
2011-12-15 22:56:00 +01:00
case GSF_CARGOES :
2010-01-22 14:02:15 +01:00
cir = CargoChangeInfo ( index , numinfo , prop , buf ) ;
2007-11-22 23:37:06 +01:00
break ;
2008-02-03 21:23:59 +01:00
2009-01-09 21:14:07 +01:00
case GSF_GLOBALVAR :
2010-01-22 14:02:15 +01:00
cir = GlobalVarReserveInfo ( index , numinfo , prop , buf ) ;
2007-11-22 23:37:06 +01:00
break ;
2010-01-30 22:49:22 +01:00
case GSF_RAILTYPES :
cir = RailTypeReserveInfo ( index , numinfo , prop , buf ) ;
break ;
2019-04-06 08:46:15 +02:00
case GSF_ROADTYPES :
cir = RoadTypeReserveInfo ( index , numinfo , prop , buf ) ;
break ;
case GSF_TRAMTYPES :
cir = TramTypeReserveInfo ( index , numinfo , prop , buf ) ;
break ;
2007-03-23 00:19:40 +01:00
}
2007-11-22 23:37:06 +01:00
2009-01-09 21:14:07 +01:00
if ( HandleChangeInfoResult ( " ReserveChangeInfo " , cir , feature , prop ) ) return ;
2007-03-23 00:19:40 +01:00
}
}
2004-11-12 16:15:56 +01:00
/* Action 0x01 */
2010-01-22 14:02:15 +01:00
static void NewSpriteSet ( ByteReader * buf )
2004-08-09 19:04:08 +02:00
{
2012-06-29 17:38:41 +02:00
/* Basic format: <01> <feature> <num-sets> <num-ent>
* Extended format : < 01 > < feature > 00 < first - set > < num - sets > < num - ent >
2004-08-09 19:04:08 +02:00
*
* B feature feature to define sprites for
* 0 , 1 , 2 , 3 : veh - type , 4 : train stations
2012-06-29 17:38:41 +02:00
* E first - set first sprite set to define
* B num - sets number of sprite sets ( extended byte in extended format )
2005-08-16 00:48:33 +02:00
* E num - ent how many entries per sprite set
2004-08-09 19:04:08 +02:00
* For vehicles , this is the number of different
* vehicle directions in each sprite set
* Set num - dirs = 8 , unless your sprites are symmetric .
* In that case , use num - dirs = 4.
2007-03-10 10:55:11 +01:00
*/
2004-08-09 19:04:08 +02:00
2012-06-29 17:38:41 +02:00
uint8 feature = buf - > ReadByte ( ) ;
uint16 num_sets = buf - > ReadByte ( ) ;
2011-09-11 17:11:23 +02:00
uint16 first_set = 0 ;
2012-06-29 17:38:41 +02:00
if ( num_sets = = 0 & & buf - > HasData ( 3 ) ) {
2011-09-11 17:11:23 +02:00
/* Extended Action1 format.
* Some GRFs define zero sets of zero sprites , though there is actually no use in that . Ignore them . */
first_set = buf - > ReadExtendedByte ( ) ;
num_sets = buf - > ReadExtendedByte ( ) ;
}
2010-03-10 08:31:08 +01:00
uint16 num_ents = buf - > ReadExtendedByte ( ) ;
2004-08-09 19:04:08 +02:00
2019-03-18 03:05:06 +01:00
if ( feature > = GSF_END ) {
_cur . skip_sprites = num_sets * num_ents ;
2023-04-20 17:00:46 +02:00
GrfMsg ( 1 , " NewSpriteSet: Unsupported feature 0x{:02X}, skipping {} sprites " , feature , _cur . skip_sprites ) ;
2019-03-18 03:05:06 +01:00
return ;
}
2011-09-11 17:11:23 +02:00
_cur . AddSpriteSets ( feature , _cur . spriteid , first_set , num_sets , num_ents ) ;
2005-08-15 13:39:13 +02:00
2023-04-20 17:00:46 +02:00
GrfMsg ( 7 , " New sprite set at {} of feature 0x{:02X}, consisting of {} sets with {} views each (total {}) " ,
2011-08-21 14:05:47 +02:00
_cur . spriteid , feature , num_sets , num_ents , num_sets * num_ents
2005-08-15 13:39:13 +02:00
) ;
2008-01-14 12:18:43 +01:00
for ( int i = 0 ; i < num_sets * num_ents ; i + + ) {
2011-08-21 14:05:47 +02:00
_cur . nfo_line + + ;
2021-04-14 17:20:39 +02:00
LoadNextSprite ( _cur . spriteid + + , * _cur . file , _cur . nfo_line ) ;
2005-08-15 13:39:13 +02:00
}
2004-08-09 19:04:08 +02:00
}
2007-05-05 12:47:35 +02:00
/* Action 0x01 (SKIP) */
2010-01-22 14:02:15 +01:00
static void SkipAct1 ( ByteReader * buf )
2007-05-05 12:47:35 +02:00
{
2010-01-23 14:17:30 +01:00
buf - > ReadByte ( ) ;
2012-06-29 17:38:41 +02:00
uint16 num_sets = buf - > ReadByte ( ) ;
if ( num_sets = = 0 & & buf - > HasData ( 3 ) ) {
/* Extended Action1 format.
* Some GRFs define zero sets of zero sprites , though there is actually no use in that . Ignore them . */
2012-07-02 18:23:24 +02:00
buf - > ReadExtendedByte ( ) ; // first_set
2012-06-29 17:38:41 +02:00
num_sets = buf - > ReadExtendedByte ( ) ;
}
2010-03-10 08:31:08 +01:00
uint16 num_ents = buf - > ReadExtendedByte ( ) ;
2007-05-05 12:47:35 +02:00
2011-08-21 14:05:47 +02:00
_cur . skip_sprites = num_sets * num_ents ;
2007-05-05 12:47:35 +02:00
2023-04-18 21:24:42 +02:00
GrfMsg ( 3 , " SkipAct1: Skipping {} sprites " , _cur . skip_sprites ) ;
2007-05-05 12:47:35 +02:00
}
2006-04-26 21:42:05 +02:00
/* Helper function to either create a callback or link to a previously
* defined spritegroup . */
2009-01-10 01:31:47 +01:00
static const SpriteGroup * GetGroupFromGroupID ( byte setid , byte type , uint16 groupid )
2006-04-26 21:42:05 +02:00
{
2011-02-09 19:46:55 +01:00
if ( HasBit ( groupid , 15 ) ) {
assert ( CallbackResultSpriteGroup : : CanAllocateItem ( ) ) ;
2011-11-08 18:22:19 +01:00
return new CallbackResultSpriteGroup ( groupid , _cur . grffile - > grf_version > = 8 ) ;
2011-02-09 19:46:55 +01:00
}
2006-04-26 21:42:05 +02:00
2019-04-10 23:07:06 +02:00
if ( groupid > MAX_SPRITEGROUP | | _cur . spritegroups [ groupid ] = = nullptr ) {
2023-04-20 17:00:46 +02:00
GrfMsg ( 1 , " GetGroupFromGroupID(0x{:02X}:0x{:02X}): Groupid 0x{:04X} does not exist, leaving empty " , setid , type , groupid ) ;
2019-04-10 23:07:06 +02:00
return nullptr ;
2006-04-26 21:42:05 +02:00
}
2011-08-21 14:07:15 +02:00
return _cur . spritegroups [ groupid ] ;
2006-04-26 21:42:05 +02:00
}
2011-09-11 17:08:17 +02:00
/**
* Helper function to either create a callback or a result sprite group .
* @ param feature GrfSpecFeature to define spritegroup for .
* @ param setid SetID of the currently being parsed Action2 . ( only for debug output )
* @ param type Type of the currently being parsed Action2 . ( only for debug output )
* @ param spriteid Raw value from the GRF for the new spritegroup ; describes either the return value or the referenced spritegroup .
* @ return Created spritegroup .
*/
static const SpriteGroup * CreateGroupFromGroupID ( byte feature , byte setid , byte type , uint16 spriteid )
2006-04-26 21:42:05 +02:00
{
2011-02-09 19:46:55 +01:00
if ( HasBit ( spriteid , 15 ) ) {
assert ( CallbackResultSpriteGroup : : CanAllocateItem ( ) ) ;
2011-11-08 18:22:19 +01:00
return new CallbackResultSpriteGroup ( spriteid , _cur . grffile - > grf_version > = 8 ) ;
2011-02-09 19:46:55 +01:00
}
2006-04-26 21:42:05 +02:00
2011-09-11 17:08:17 +02:00
if ( ! _cur . IsValidSpriteSet ( feature , spriteid ) ) {
2023-04-20 17:00:46 +02:00
GrfMsg ( 1 , " CreateGroupFromGroupID(0x{:02X}:0x{:02X}): Sprite set {} invalid " , setid , type , spriteid ) ;
2019-04-10 23:07:06 +02:00
return nullptr ;
2006-04-26 22:18:29 +02:00
}
2011-09-11 17:08:17 +02:00
SpriteID spriteset_start = _cur . GetSprite ( feature , spriteid ) ;
uint num_sprites = _cur . GetNumEnts ( feature , spriteid ) ;
2006-04-26 21:42:05 +02:00
2011-09-11 17:08:17 +02:00
/* Ensure that the sprites are loeded */
assert ( spriteset_start + num_sprites < = _cur . spriteid ) ;
2006-05-10 08:23:43 +02:00
2011-02-09 19:46:55 +01:00
assert ( ResultSpriteGroup : : CanAllocateItem ( ) ) ;
2011-09-11 17:08:17 +02:00
return new ResultSpriteGroup ( spriteset_start , num_sprites ) ;
2006-04-26 21:42:05 +02:00
}
2004-11-12 16:15:56 +01:00
/* Action 0x02 */
2010-01-22 14:02:15 +01:00
static void NewSpriteGroup ( ByteReader * buf )
2004-08-09 19:04:08 +02:00
{
/* <02> <feature> <set-id> <type/num-entries> <feature-specific-data...>
*
* B feature see action 1
* B set - id ID of this particular definition
* B type / num - entries
* if 80 or greater , this is a randomized or variational
* list definition , see below
* otherwise it specifies a number of entries , the exact
* meaning depends on the feature
* V feature - specific - data ( huge mess , don ' t even look it up - - pasky ) */
2021-06-04 13:31:00 +02:00
const SpriteGroup * act_group = nullptr ;
2006-04-23 11:46:23 +02:00
2010-01-23 14:17:30 +01:00
uint8 feature = buf - > ReadByte ( ) ;
2019-03-18 03:05:06 +01:00
if ( feature > = GSF_END ) {
2023-04-20 17:00:46 +02:00
GrfMsg ( 1 , " NewSpriteGroup: Unsupported feature 0x{:02X}, skipping " , feature ) ;
2019-03-18 03:05:06 +01:00
return ;
}
2010-01-23 14:17:30 +01:00
uint8 setid = buf - > ReadByte ( ) ;
uint8 type = buf - > ReadByte ( ) ;
2004-08-09 19:04:08 +02:00
2010-01-22 14:02:15 +01:00
/* Sprite Groups are created here but they are allocated from a pool, so
* we do not need to delete anything if there is an exception from the
* ByteReader . */
2006-04-23 11:46:23 +02:00
switch ( type ) {
/* Deterministic Sprite Group */
case 0x81 : // Self scope, byte
case 0x82 : // Parent scope, byte
2006-04-26 22:44:28 +02:00
case 0x85 : // Self scope, word
case 0x86 : // Parent scope, word
case 0x89 : // Self scope, dword
case 0x8A : // Parent scope, dword
2006-04-23 11:46:23 +02:00
{
2006-04-26 22:44:28 +02:00
byte varadjust ;
byte varsize ;
2006-04-23 11:46:23 +02:00
2011-02-09 19:46:55 +01:00
assert ( DeterministicSpriteGroup : : CanAllocateItem ( ) ) ;
2009-05-23 14:13:42 +02:00
DeterministicSpriteGroup * group = new DeterministicSpriteGroup ( ) ;
2020-01-26 13:45:51 +01:00
group - > nfo_line = _cur . nfo_line ;
2009-05-23 14:13:42 +02:00
act_group = group ;
group - > var_scope = HasBit ( type , 1 ) ? VSG_SCOPE_PARENT : VSG_SCOPE_SELF ;
2006-04-26 22:44:28 +02:00
switch ( GB ( type , 2 , 2 ) ) {
2006-04-27 08:33:21 +02:00
default : NOT_REACHED ( ) ;
2009-05-23 14:13:42 +02:00
case 0 : group - > size = DSG_SIZE_BYTE ; varsize = 1 ; break ;
case 1 : group - > size = DSG_SIZE_WORD ; varsize = 2 ; break ;
case 2 : group - > size = DSG_SIZE_DWORD ; varsize = 4 ; break ;
2006-04-23 11:46:23 +02:00
}
2004-11-14 19:18:28 +01:00
2006-04-26 22:44:28 +02:00
/* Loop through the var adjusts. Unfortunately we don't know how many we have
* from the outset , so we shall have to keep reallocing . */
do {
2021-05-02 01:00:06 +02:00
DeterministicSpriteGroupAdjust & adjust = group - > adjusts . emplace_back ( ) ;
2006-04-26 22:44:28 +02:00
/* The first var adjust doesn't have an operation specified, so we set it to add. */
2021-05-02 01:00:06 +02:00
adjust . operation = group - > adjusts . size ( ) = = 1 ? DSGA_OP_ADD : ( DeterministicSpriteGroupAdjustOperation ) buf - > ReadByte ( ) ;
2019-02-18 23:39:06 +01:00
adjust . variable = buf - > ReadByte ( ) ;
if ( adjust . variable = = 0x7E ) {
2007-01-12 12:20:34 +01:00
/* Link subroutine group */
2019-02-18 23:39:06 +01:00
adjust . subroutine = GetGroupFromGroupID ( setid , type , buf - > ReadByte ( ) ) ;
2007-01-12 12:20:34 +01:00
} else {
2019-02-18 23:39:06 +01:00
adjust . parameter = IsInsideMM ( adjust . variable , 0x60 , 0x80 ) ? buf - > ReadByte ( ) : 0 ;
2007-01-12 12:20:34 +01:00
}
2006-04-26 22:44:28 +02:00
2010-01-23 14:17:30 +01:00
varadjust = buf - > ReadByte ( ) ;
2019-02-18 23:39:06 +01:00
adjust . shift_num = GB ( varadjust , 0 , 5 ) ;
adjust . type = ( DeterministicSpriteGroupAdjustType ) GB ( varadjust , 6 , 2 ) ;
adjust . and_mask = buf - > ReadVarSize ( varsize ) ;
2006-04-26 22:44:28 +02:00
2019-02-18 23:39:06 +01:00
if ( adjust . type ! = DSGA_TYPE_NONE ) {
adjust . add_val = buf - > ReadVarSize ( varsize ) ;
adjust . divmod_val = buf - > ReadVarSize ( varsize ) ;
2006-04-26 22:44:28 +02:00
} else {
2019-02-18 23:39:06 +01:00
adjust . add_val = 0 ;
adjust . divmod_val = 0 ;
2006-04-26 22:44:28 +02:00
}
/* Continue reading var adjusts while bit 5 is set. */
2007-11-19 22:02:30 +01:00
} while ( HasBit ( varadjust , 5 ) ) ;
2006-04-26 22:44:28 +02:00
2018-03-11 14:21:27 +01:00
std : : vector < DeterministicSpriteGroupRange > ranges ;
ranges . resize ( buf - > ReadByte ( ) ) ;
for ( uint i = 0 ; i < ranges . size ( ) ; i + + ) {
ranges [ i ] . group = GetGroupFromGroupID ( setid , type , buf - > ReadWord ( ) ) ;
ranges [ i ] . low = buf - > ReadVarSize ( varsize ) ;
ranges [ i ] . high = buf - > ReadVarSize ( varsize ) ;
2006-04-23 11:46:23 +02:00
}
2004-11-15 00:14:44 +01:00
2010-01-23 14:17:30 +01:00
group - > default_group = GetGroupFromGroupID ( setid , type , buf - > ReadWord ( ) ) ;
2018-03-11 14:21:27 +01:00
group - > error_group = ranges . size ( ) > 0 ? ranges [ 0 ] . group : group - > default_group ;
2018-03-11 16:08:51 +01:00
/* nvar == 0 is a special case -- we turn our value into a callback result */
group - > calculated_result = ranges . size ( ) = = 0 ;
2018-03-11 14:21:27 +01:00
/* Sort ranges ascending. When ranges overlap, this may required clamping or splitting them */
std : : vector < uint32 > bounds ;
for ( uint i = 0 ; i < ranges . size ( ) ; i + + ) {
bounds . push_back ( ranges [ i ] . low ) ;
if ( ranges [ i ] . high ! = UINT32_MAX ) bounds . push_back ( ranges [ i ] . high + 1 ) ;
}
std : : sort ( bounds . begin ( ) , bounds . end ( ) ) ;
bounds . erase ( std : : unique ( bounds . begin ( ) , bounds . end ( ) ) , bounds . end ( ) ) ;
std : : vector < const SpriteGroup * > target ;
for ( uint j = 0 ; j < bounds . size ( ) ; + + j ) {
uint32 v = bounds [ j ] ;
2018-03-11 16:08:51 +01:00
const SpriteGroup * t = group - > default_group ;
2018-03-11 14:21:27 +01:00
for ( uint i = 0 ; i < ranges . size ( ) ; i + + ) {
if ( ranges [ i ] . low < = v & & v < = ranges [ i ] . high ) {
t = ranges [ i ] . group ;
2018-03-11 16:08:51 +01:00
break ;
2018-03-11 14:21:27 +01:00
}
}
target . push_back ( t ) ;
}
assert ( target . size ( ) = = bounds . size ( ) ) ;
for ( uint j = 0 ; j < bounds . size ( ) ; ) {
2018-03-11 16:08:51 +01:00
if ( target [ j ] ! = group - > default_group ) {
2021-05-02 01:00:06 +02:00
DeterministicSpriteGroupRange & r = group - > ranges . emplace_back ( ) ;
2018-03-11 14:21:27 +01:00
r . group = target [ j ] ;
r . low = bounds [ j ] ;
while ( j < bounds . size ( ) & & target [ j ] = = r . group ) {
j + + ;
}
r . high = j < bounds . size ( ) ? bounds [ j ] - 1 : UINT32_MAX ;
} else {
j + + ;
}
}
2006-04-23 11:46:23 +02:00
break ;
2004-11-14 19:18:28 +01:00
}
2006-04-23 11:46:23 +02:00
/* Randomized Sprite Group */
case 0x80 : // Self scope
case 0x83 : // Parent scope
2008-03-27 22:36:16 +01:00
case 0x84 : // Relative scope
2006-04-23 11:46:23 +02:00
{
2011-02-09 19:46:55 +01:00
assert ( RandomizedSpriteGroup : : CanAllocateItem ( ) ) ;
2009-05-23 14:13:42 +02:00
RandomizedSpriteGroup * group = new RandomizedSpriteGroup ( ) ;
2020-01-26 13:45:51 +01:00
group - > nfo_line = _cur . nfo_line ;
2009-05-23 14:13:42 +02:00
act_group = group ;
group - > var_scope = HasBit ( type , 1 ) ? VSG_SCOPE_PARENT : VSG_SCOPE_SELF ;
2004-11-17 09:52:47 +01:00
2008-03-28 03:10:25 +01:00
if ( HasBit ( type , 2 ) ) {
2009-05-23 14:13:42 +02:00
if ( feature < = GSF_AIRCRAFT ) group - > var_scope = VSG_SCOPE_RELATIVE ;
2010-01-23 14:17:30 +01:00
group - > count = buf - > ReadByte ( ) ;
2008-03-27 22:36:16 +01:00
}
2010-01-23 14:17:30 +01:00
uint8 triggers = buf - > ReadByte ( ) ;
2009-05-23 14:13:42 +02:00
group - > triggers = GB ( triggers , 0 , 7 ) ;
group - > cmp_mode = HasBit ( triggers , 7 ) ? RSG_CMP_ALL : RSG_CMP_ANY ;
2010-01-23 14:17:30 +01:00
group - > lowest_randbit = buf - > ReadByte ( ) ;
2004-11-17 09:52:47 +01:00
2021-05-02 01:00:40 +02:00
byte num_groups = buf - > ReadByte ( ) ;
2021-05-02 01:34:17 +02:00
if ( ! HasExactlyOneBit ( num_groups ) ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " NewSpriteGroup: Random Action 2 nrand should be power of 2 " ) ;
2021-05-02 01:34:17 +02:00
}
2021-05-02 01:00:40 +02:00
for ( uint i = 0 ; i < num_groups ; i + + ) {
group - > groups . push_back ( GetGroupFromGroupID ( setid , type , buf - > ReadWord ( ) ) ) ;
2006-04-23 11:46:23 +02:00
}
2004-11-17 09:52:47 +01:00
2006-04-23 11:46:23 +02:00
break ;
}
2004-11-17 09:52:47 +01:00
2006-04-26 22:44:28 +02:00
/* Neither a variable or randomized sprite group... must be a real group */
2006-04-23 11:46:23 +02:00
default :
{
2006-04-26 22:44:28 +02:00
switch ( feature ) {
2010-04-24 13:52:58 +02:00
case GSF_TRAINS :
case GSF_ROADVEHICLES :
case GSF_SHIPS :
2006-04-26 22:44:28 +02:00
case GSF_AIRCRAFT :
2010-04-24 13:52:58 +02:00
case GSF_STATIONS :
case GSF_CANALS :
2011-12-15 22:56:00 +01:00
case GSF_CARGOES :
2010-03-19 00:15:38 +01:00
case GSF_AIRPORTS :
2010-02-07 23:22:54 +01:00
case GSF_RAILTYPES :
2019-04-06 08:46:15 +02:00
case GSF_ROADTYPES :
case GSF_TRAMTYPES :
2006-04-26 22:44:28 +02:00
{
byte num_loaded = type ;
2010-01-23 14:17:30 +01:00
byte num_loading = buf - > ReadByte ( ) ;
2006-04-26 22:44:28 +02:00
2011-09-11 17:08:17 +02:00
if ( ! _cur . HasValidSpriteSets ( feature ) ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 0 , " NewSpriteGroup: No sprite set to work on! Skipping " ) ;
2006-04-26 22:44:28 +02:00
return ;
}
2006-04-23 11:46:23 +02:00
2023-04-20 17:00:46 +02:00
GrfMsg ( 6 , " NewSpriteGroup: New SpriteGroup 0x{:02X}, {} loaded, {} loading " ,
2011-09-11 17:08:17 +02:00
setid , num_loaded , num_loading ) ;
2006-04-23 11:46:23 +02:00
2021-09-19 22:52:08 +02:00
if ( num_loaded + num_loading = = 0 ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " NewSpriteGroup: no result, skipping invalid RealSpriteGroup " ) ;
2021-09-19 22:52:08 +02:00
break ;
}
2021-06-04 13:31:00 +02:00
if ( num_loaded + num_loading = = 1 ) {
/* Avoid creating 'Real' sprite group if only one option. */
2010-01-23 14:17:30 +01:00
uint16 spriteid = buf - > ReadWord ( ) ;
2021-06-04 13:31:00 +02:00
act_group = CreateGroupFromGroupID ( feature , setid , type , spriteid ) ;
2023-04-18 21:24:42 +02:00
GrfMsg ( 8 , " NewSpriteGroup: one result, skipping RealSpriteGroup = subset {} " , spriteid ) ;
2021-06-04 13:31:00 +02:00
break ;
}
std : : vector < uint16 > loaded ;
std : : vector < uint16 > loading ;
for ( uint i = 0 ; i < num_loaded ; i + + ) {
loaded . push_back ( buf - > ReadWord ( ) ) ;
2023-04-18 21:24:42 +02:00
GrfMsg ( 8 , " NewSpriteGroup: + rg->loaded[{}] = subset {} " , i , loaded [ i ] ) ;
2006-04-26 22:44:28 +02:00
}
2006-04-23 11:46:23 +02:00
2007-03-10 10:55:11 +01:00
for ( uint i = 0 ; i < num_loading ; i + + ) {
2021-06-04 13:31:00 +02:00
loading . push_back ( buf - > ReadWord ( ) ) ;
2023-04-18 21:24:42 +02:00
GrfMsg ( 8 , " NewSpriteGroup: + rg->loading[{}] = subset {} " , i , loading [ i ] ) ;
2021-06-04 13:31:00 +02:00
}
if ( std : : adjacent_find ( loaded . begin ( ) , loaded . end ( ) , std : : not_equal_to < > ( ) ) = = loaded . end ( ) & &
std : : adjacent_find ( loading . begin ( ) , loading . end ( ) , std : : not_equal_to < > ( ) ) = = loading . end ( ) & &
loaded [ 0 ] = = loading [ 0 ] )
{
/* Both lists only contain the same value, so don't create 'Real' sprite group */
act_group = CreateGroupFromGroupID ( feature , setid , type , loaded [ 0 ] ) ;
2023-04-18 21:24:42 +02:00
GrfMsg ( 8 , " NewSpriteGroup: same result, skipping RealSpriteGroup = subset {} " , loaded [ 0 ] ) ;
2021-06-04 13:31:00 +02:00
break ;
}
assert ( RealSpriteGroup : : CanAllocateItem ( ) ) ;
RealSpriteGroup * group = new RealSpriteGroup ( ) ;
group - > nfo_line = _cur . nfo_line ;
act_group = group ;
for ( uint16 spriteid : loaded ) {
const SpriteGroup * t = CreateGroupFromGroupID ( feature , setid , type , spriteid ) ;
group - > loaded . push_back ( t ) ;
}
for ( uint16 spriteid : loading ) {
const SpriteGroup * t = CreateGroupFromGroupID ( feature , setid , type , spriteid ) ;
group - > loading . push_back ( t ) ;
2006-04-26 22:44:28 +02:00
}
2006-04-23 11:46:23 +02:00
2006-04-26 22:44:28 +02:00
break ;
}
2004-08-09 19:04:08 +02:00
2010-04-24 13:52:58 +02:00
case GSF_HOUSES :
2010-02-22 15:16:19 +01:00
case GSF_AIRPORTTILES :
2010-08-28 20:22:24 +02:00
case GSF_OBJECTS :
2022-11-06 16:01:27 +01:00
case GSF_INDUSTRYTILES :
case GSF_ROADSTOPS : {
2021-01-08 11:16:18 +01:00
byte num_building_sprites = std : : max ( ( uint8 ) 1 , type ) ;
2007-03-19 12:27:30 +01:00
2011-02-09 19:46:55 +01:00
assert ( TileLayoutSpriteGroup : : CanAllocateItem ( ) ) ;
2009-05-23 14:13:42 +02:00
TileLayoutSpriteGroup * group = new TileLayoutSpriteGroup ( ) ;
2020-01-26 13:45:51 +01:00
group - > nfo_line = _cur . nfo_line ;
2009-05-23 14:13:42 +02:00
act_group = group ;
2007-03-19 12:27:30 +01:00
2011-05-29 18:56:22 +02:00
/* On error, bail out immediately. Temporary GRF data was already freed */
2011-09-11 17:08:17 +02:00
if ( ReadSpriteLayout ( buf , num_building_sprites , true , feature , false , type = = 0 , & group - > dts ) ) return ;
2007-03-19 12:27:30 +01:00
break ;
}
2007-07-05 07:41:56 +02:00
case GSF_INDUSTRIES : {
2018-07-26 19:29:54 +02:00
if ( type > 2 ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " NewSpriteGroup: Unsupported industry production version {}, skipping " , type ) ;
2007-07-05 07:41:56 +02:00
break ;
}
2011-02-09 19:46:55 +01:00
assert ( IndustryProductionSpriteGroup : : CanAllocateItem ( ) ) ;
2009-05-23 14:13:42 +02:00
IndustryProductionSpriteGroup * group = new IndustryProductionSpriteGroup ( ) ;
2020-01-26 13:45:51 +01:00
group - > nfo_line = _cur . nfo_line ;
2009-05-23 14:13:42 +02:00
act_group = group ;
group - > version = type ;
2007-07-05 07:41:56 +02:00
if ( type = = 0 ) {
2018-07-26 19:29:54 +02:00
group - > num_input = 3 ;
2007-07-05 07:41:56 +02:00
for ( uint i = 0 ; i < 3 ; i + + ) {
2010-01-23 14:17:30 +01:00
group - > subtract_input [ i ] = ( int16 ) buf - > ReadWord ( ) ; // signed
2007-07-05 07:41:56 +02:00
}
2018-07-26 19:29:54 +02:00
group - > num_output = 2 ;
2007-07-05 07:41:56 +02:00
for ( uint i = 0 ; i < 2 ; i + + ) {
2010-01-23 14:17:30 +01:00
group - > add_output [ i ] = buf - > ReadWord ( ) ; // unsigned
2007-07-05 07:41:56 +02:00
}
2010-01-23 14:17:30 +01:00
group - > again = buf - > ReadByte ( ) ;
2018-07-26 19:29:54 +02:00
} else if ( type = = 1 ) {
group - > num_input = 3 ;
2007-07-05 07:41:56 +02:00
for ( uint i = 0 ; i < 3 ; i + + ) {
2010-01-23 14:17:30 +01:00
group - > subtract_input [ i ] = buf - > ReadByte ( ) ;
2007-07-05 07:41:56 +02:00
}
2018-07-26 19:29:54 +02:00
group - > num_output = 2 ;
2007-07-05 07:41:56 +02:00
for ( uint i = 0 ; i < 2 ; i + + ) {
2010-01-23 14:17:30 +01:00
group - > add_output [ i ] = buf - > ReadByte ( ) ;
2007-07-05 07:41:56 +02:00
}
2010-01-23 14:17:30 +01:00
group - > again = buf - > ReadByte ( ) ;
2018-07-26 19:29:54 +02:00
} else if ( type = = 2 ) {
group - > num_input = buf - > ReadByte ( ) ;
if ( group - > num_input > lengthof ( group - > subtract_input ) ) {
GRFError * error = DisableGrf ( STR_NEWGRF_ERROR_INDPROD_CALLBACK ) ;
2020-05-17 23:31:54 +02:00
error - > data = " too many inputs (max 16) " ;
2018-07-26 19:29:54 +02:00
return ;
}
for ( uint i = 0 ; i < group - > num_input ; i + + ) {
byte rawcargo = buf - > ReadByte ( ) ;
CargoID cargo = GetCargoTranslation ( rawcargo , _cur . grffile ) ;
2023-05-04 12:29:21 +02:00
if ( ! IsValidCargoID ( cargo ) ) {
2019-03-10 18:45:15 +01:00
/* The mapped cargo is invalid. This is permitted at this point,
* as long as the result is not used . Mark it invalid so this
* can be tested later . */
group - > version = 0xFF ;
} else if ( std : : find ( group - > cargo_input , group - > cargo_input + i , cargo ) ! = group - > cargo_input + i ) {
2018-07-26 19:29:54 +02:00
GRFError * error = DisableGrf ( STR_NEWGRF_ERROR_INDPROD_CALLBACK ) ;
2020-05-17 23:31:54 +02:00
error - > data = " duplicate input cargo " ;
2018-07-26 19:29:54 +02:00
return ;
}
group - > cargo_input [ i ] = cargo ;
group - > subtract_input [ i ] = buf - > ReadByte ( ) ;
}
group - > num_output = buf - > ReadByte ( ) ;
if ( group - > num_output > lengthof ( group - > add_output ) ) {
GRFError * error = DisableGrf ( STR_NEWGRF_ERROR_INDPROD_CALLBACK ) ;
2020-05-17 23:31:54 +02:00
error - > data = " too many outputs (max 16) " ;
2018-07-26 19:29:54 +02:00
return ;
}
for ( uint i = 0 ; i < group - > num_output ; i + + ) {
byte rawcargo = buf - > ReadByte ( ) ;
CargoID cargo = GetCargoTranslation ( rawcargo , _cur . grffile ) ;
2023-05-04 12:29:21 +02:00
if ( ! IsValidCargoID ( cargo ) ) {
2019-03-10 18:45:15 +01:00
/* Mark this result as invalid to use */
group - > version = 0xFF ;
} else if ( std : : find ( group - > cargo_output , group - > cargo_output + i , cargo ) ! = group - > cargo_output + i ) {
2018-07-26 19:29:54 +02:00
GRFError * error = DisableGrf ( STR_NEWGRF_ERROR_INDPROD_CALLBACK ) ;
2020-05-17 23:31:54 +02:00
error - > data = " duplicate output cargo " ;
2018-07-26 19:29:54 +02:00
return ;
}
group - > cargo_output [ i ] = cargo ;
group - > add_output [ i ] = buf - > ReadByte ( ) ;
}
group - > again = buf - > ReadByte ( ) ;
} else {
NOT_REACHED ( ) ;
2007-07-05 07:41:56 +02:00
}
break ;
}
2006-04-26 22:44:28 +02:00
/* Loading of Tile Layout and Production Callback groups would happen here */
2023-04-20 17:00:46 +02:00
default : GrfMsg ( 1 , " NewSpriteGroup: Unsupported feature 0x{:02X}, skipping " , feature ) ;
2006-04-26 22:44:28 +02:00
}
2005-10-04 22:38:26 +02:00
}
2004-08-09 19:04:08 +02:00
}
2005-09-22 22:35:20 +02:00
2011-08-21 14:07:15 +02:00
_cur . spritegroups [ setid ] = act_group ;
2004-08-09 19:04:08 +02:00
}
2007-02-24 20:36:47 +01:00
static CargoID TranslateCargo ( uint8 feature , uint8 ctype )
{
/* Special cargo types for purchase list and stations */
2022-11-06 16:01:27 +01:00
if ( ( feature = = GSF_STATIONS | | feature = = GSF_ROADSTOPS ) & & ctype = = 0xFE ) return CT_DEFAULT_NA ;
2007-02-25 00:36:40 +01:00
if ( ctype = = 0xFF ) return CT_PURCHASE ;
2007-02-24 20:36:47 +01:00
2018-09-23 13:23:54 +02:00
if ( _cur . grffile - > cargo_list . size ( ) = = 0 ) {
2007-03-23 01:03:08 +01:00
/* No cargo table, so use bitnum values */
if ( ctype > = 32 ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " TranslateCargo: Cargo bitnum {} out of range (max 31), skipping. " , ctype ) ;
2007-03-23 01:03:08 +01:00
return CT_INVALID ;
}
2021-04-29 17:51:05 +02:00
for ( const CargoSpec * cs : CargoSpec : : Iterate ( ) ) {
2007-03-23 01:03:08 +01:00
if ( cs - > bitnum = = ctype ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 6 , " TranslateCargo: Cargo bitnum {} mapped to cargo type {}. " , ctype , cs - > Index ( ) ) ;
2009-07-16 22:40:06 +02:00
return cs - > Index ( ) ;
2007-03-23 01:03:08 +01:00
}
}
2023-04-18 21:24:42 +02:00
GrfMsg ( 5 , " TranslateCargo: Cargo bitnum {} not available in this climate, skipping. " , ctype ) ;
2007-03-23 01:03:08 +01:00
return CT_INVALID ;
}
2007-02-24 20:36:47 +01:00
/* Check if the cargo type is out of bounds of the cargo translation table */
2018-09-23 13:23:54 +02:00
if ( ctype > = _cur . grffile - > cargo_list . size ( ) ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " TranslateCargo: Cargo type {} out of range (max {}), skipping. " , ctype , ( unsigned int ) _cur . grffile - > cargo_list . size ( ) - 1 ) ;
2007-02-24 20:36:47 +01:00
return CT_INVALID ;
}
/* Look up the cargo label from the translation table */
2011-08-21 14:05:47 +02:00
CargoLabel cl = _cur . grffile - > cargo_list [ ctype ] ;
2007-02-24 20:36:47 +01:00
if ( cl = = 0 ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 5 , " TranslateCargo: Cargo type {} not available in this climate, skipping. " , ctype ) ;
2007-02-24 20:36:47 +01:00
return CT_INVALID ;
}
ctype = GetCargoIDByLabel ( cl ) ;
2023-05-04 12:29:21 +02:00
if ( ! IsValidCargoID ( ctype ) ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 5 , " TranslateCargo: Cargo '{:c}{:c}{:c}{:c}' unsupported, skipping. " , GB ( cl , 24 , 8 ) , GB ( cl , 16 , 8 ) , GB ( cl , 8 , 8 ) , GB ( cl , 0 , 8 ) ) ;
2007-02-24 20:36:47 +01:00
return CT_INVALID ;
}
2023-04-18 21:24:42 +02:00
GrfMsg ( 6 , " TranslateCargo: Cargo '{:c}{:c}{:c}{:c}' mapped to cargo type {}. " , GB ( cl , 24 , 8 ) , GB ( cl , 16 , 8 ) , GB ( cl , 8 , 8 ) , GB ( cl , 0 , 8 ) , ctype ) ;
2007-02-24 20:36:47 +01:00
return ctype ;
}
2004-08-09 19:04:08 +02:00
2008-06-02 21:35:30 +02:00
static bool IsValidGroupID ( uint16 groupid , const char * function )
{
2019-04-10 23:07:06 +02:00
if ( groupid > MAX_SPRITEGROUP | | _cur . spritegroups [ groupid ] = = nullptr ) {
2023-04-20 17:00:46 +02:00
GrfMsg ( 1 , " {}: Spritegroup 0x{:04X} out of range or empty, skipping. " , function , groupid ) ;
2008-06-02 21:35:30 +02:00
return false ;
}
return true ;
}
2010-01-22 14:02:15 +01:00
static void VehicleMapSpriteGroup ( ByteReader * buf , byte feature , uint8 idcount )
2007-03-23 21:01:25 +01:00
{
2008-01-05 16:39:22 +01:00
static EngineID * last_engines ;
static uint last_engines_count ;
2008-06-02 21:52:41 +02:00
bool wagover = false ;
/* Test for 'wagon override' flag */
if ( HasBit ( idcount , 7 ) ) {
wagover = true ;
/* Strip off the flag */
idcount = GB ( idcount , 0 , 7 ) ;
2004-11-12 16:36:49 +01:00
2004-11-13 13:21:32 +01:00
if ( last_engines_count = = 0 ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 0 , " VehicleMapSpriteGroup: WagonOverride: No engine to do override with " ) ;
2004-11-13 13:21:32 +01:00
return ;
}
2007-03-23 21:01:25 +01:00
2023-04-18 21:24:42 +02:00
GrfMsg ( 6 , " VehicleMapSpriteGroup: WagonOverride: {} engines, {} wagons " ,
2004-11-13 19:08:45 +01:00
last_engines_count , idcount ) ;
2008-06-02 21:52:41 +02:00
} else {
if ( last_engines_count ! = idcount ) {
last_engines = ReallocT ( last_engines , idcount ) ;
last_engines_count = idcount ;
}
2004-11-13 13:21:32 +01:00
}
2004-11-14 14:59:11 +01:00
2021-05-01 22:06:17 +02:00
std : : vector < EngineID > engines ;
2007-03-10 10:55:11 +01:00
for ( uint i = 0 ; i < idcount ; i + + ) {
2011-08-21 14:05:47 +02:00
Engine * e = GetNewEngine ( _cur . grffile , ( VehicleType ) feature , buf - > ReadExtendedByte ( ) ) ;
2019-04-10 23:07:06 +02:00
if ( e = = nullptr ) {
2011-02-09 19:46:55 +01:00
/* No engine could be allocated?!? Deal with it. Okay,
* this might look bad . Also make sure this NewGRF
* gets disabled , as a half loaded one is bad . */
HandleChangeInfoResult ( " VehicleMapSpriteGroup " , CIR_INVALID_ID , 0 , 0 ) ;
return ;
}
2021-05-01 22:06:17 +02:00
engines . push_back ( e - > index ) ;
2008-06-02 21:52:41 +02:00
if ( ! wagover ) last_engines [ i ] = engines [ i ] ;
}
2004-08-09 19:04:08 +02:00
2010-01-23 14:17:30 +01:00
uint8 cidcount = buf - > ReadByte ( ) ;
2008-06-02 21:52:41 +02:00
for ( uint c = 0 ; c < cidcount ; c + + ) {
2010-01-23 14:17:30 +01:00
uint8 ctype = buf - > ReadByte ( ) ;
uint16 groupid = buf - > ReadWord ( ) ;
2008-06-02 21:52:41 +02:00
if ( ! IsValidGroupID ( groupid , " VehicleMapSpriteGroup " ) ) continue ;
2004-11-13 19:08:45 +01:00
2023-04-20 17:00:46 +02:00
GrfMsg ( 8 , " VehicleMapSpriteGroup: * [{}] Cargo type 0x{:X}, group id 0x{:02X} " , c , ctype , groupid ) ;
2004-08-09 19:04:08 +02:00
2008-06-02 21:52:41 +02:00
ctype = TranslateCargo ( feature , ctype ) ;
2023-05-04 12:29:21 +02:00
if ( ! IsValidCargoID ( ctype ) ) continue ;
2004-11-13 19:08:45 +01:00
2008-06-02 21:52:41 +02:00
for ( uint i = 0 ; i < idcount ; i + + ) {
EngineID engine = engines [ i ] ;
2023-04-18 21:24:42 +02:00
GrfMsg ( 7 , " VehicleMapSpriteGroup: [{}] Engine {}... " , i , engine ) ;
2006-12-18 11:46:06 +01:00
2004-11-13 19:08:45 +01:00
if ( wagover ) {
2011-08-21 14:07:15 +02:00
SetWagonOverrideSprites ( engine , ctype , _cur . spritegroups [ groupid ] , last_engines , last_engines_count ) ;
2004-11-12 16:36:49 +01:00
} else {
2011-08-21 14:07:15 +02:00
SetCustomEngineSprites ( engine , ctype , _cur . spritegroups [ groupid ] ) ;
2004-08-09 19:04:08 +02:00
}
}
2004-11-12 16:36:49 +01:00
}
2004-08-09 19:04:08 +02:00
2010-01-23 14:17:30 +01:00
uint16 groupid = buf - > ReadWord ( ) ;
2008-06-02 21:52:41 +02:00
if ( ! IsValidGroupID ( groupid , " VehicleMapSpriteGroup " ) ) return ;
2004-08-09 19:04:08 +02:00
2023-04-20 17:00:46 +02:00
GrfMsg ( 8 , " -- Default group id 0x{:04X} " , groupid ) ;
2004-11-13 19:08:45 +01:00
2008-06-02 21:52:41 +02:00
for ( uint i = 0 ; i < idcount ; i + + ) {
EngineID engine = engines [ i ] ;
2004-08-09 19:04:08 +02:00
2008-06-02 21:52:41 +02:00
if ( wagover ) {
2011-08-21 14:07:15 +02:00
SetWagonOverrideSprites ( engine , CT_DEFAULT , _cur . spritegroups [ groupid ] , last_engines , last_engines_count ) ;
2008-06-02 21:52:41 +02:00
} else {
2011-08-21 14:07:15 +02:00
SetCustomEngineSprites ( engine , CT_DEFAULT , _cur . spritegroups [ groupid ] ) ;
2011-08-21 14:05:47 +02:00
SetEngineGRF ( engine , _cur . grffile ) ;
2004-08-09 19:04:08 +02:00
}
}
}
2007-03-23 21:01:25 +01:00
2010-01-22 14:02:15 +01:00
static void CanalMapSpriteGroup ( ByteReader * buf , uint8 idcount )
2007-05-06 20:14:33 +02:00
{
2023-04-30 11:00:43 +02:00
std : : vector < uint16_t > cfs ;
2021-05-01 22:06:17 +02:00
cfs . reserve ( idcount ) ;
2008-06-02 21:52:41 +02:00
for ( uint i = 0 ; i < idcount ; i + + ) {
2023-04-30 11:00:43 +02:00
cfs . push_back ( buf - > ReadExtendedByte ( ) ) ;
2008-06-02 21:52:41 +02:00
}
2010-01-23 14:17:30 +01:00
uint8 cidcount = buf - > ReadByte ( ) ;
2010-01-22 14:02:15 +01:00
buf - > Skip ( cidcount * 3 ) ;
2008-06-02 21:52:41 +02:00
2010-01-23 14:17:30 +01:00
uint16 groupid = buf - > ReadWord ( ) ;
2008-06-02 21:35:30 +02:00
if ( ! IsValidGroupID ( groupid , " CanalMapSpriteGroup " ) ) return ;
2007-05-06 20:14:33 +02:00
2023-04-09 23:32:01 +02:00
for ( auto & cf : cfs ) {
2007-05-06 20:14:33 +02:00
if ( cf > = CF_END ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " CanalMapSpriteGroup: Canal subset {} out of range, skipping " , cf ) ;
2007-05-06 20:14:33 +02:00
continue ;
}
2011-08-21 14:05:47 +02:00
_water_feature [ cf ] . grffile = _cur . grffile ;
2011-08-21 14:07:15 +02:00
_water_feature [ cf ] . group = _cur . spritegroups [ groupid ] ;
2007-05-06 20:14:33 +02:00
}
}
2010-01-22 14:02:15 +01:00
static void StationMapSpriteGroup ( ByteReader * buf , uint8 idcount )
2007-03-23 21:01:25 +01:00
{
2023-01-08 03:12:33 +01:00
if ( _cur . grffile - > stations . empty ( ) ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " StationMapSpriteGroup: No stations defined, skipping " ) ;
2023-04-06 21:29:57 +02:00
return ;
}
2023-04-30 11:00:43 +02:00
std : : vector < uint16_t > stations ;
2021-05-01 22:06:17 +02:00
stations . reserve ( idcount ) ;
2007-03-23 21:01:25 +01:00
for ( uint i = 0 ; i < idcount ; i + + ) {
2023-04-30 11:00:43 +02:00
stations . push_back ( buf - > ReadExtendedByte ( ) ) ;
2008-06-02 21:52:41 +02:00
}
2007-10-09 17:28:40 +02:00
2010-01-23 14:17:30 +01:00
uint8 cidcount = buf - > ReadByte ( ) ;
2008-06-02 21:52:41 +02:00
for ( uint c = 0 ; c < cidcount ; c + + ) {
2010-01-23 14:17:30 +01:00
uint8 ctype = buf - > ReadByte ( ) ;
uint16 groupid = buf - > ReadWord ( ) ;
2008-06-02 21:52:41 +02:00
if ( ! IsValidGroupID ( groupid , " StationMapSpriteGroup " ) ) continue ;
2007-10-09 17:28:40 +02:00
2010-04-24 13:52:58 +02:00
ctype = TranslateCargo ( GSF_STATIONS , ctype ) ;
2023-05-04 12:29:21 +02:00
if ( ! IsValidCargoID ( ctype ) ) continue ;
2007-03-23 21:01:25 +01:00
2023-04-09 23:32:01 +02:00
for ( auto & station : stations ) {
2023-04-15 22:30:37 +02:00
StationSpec * statspec = station > = _cur . grffile - > stations . size ( ) ? nullptr : _cur . grffile - > stations [ station ] . get ( ) ;
2007-03-23 21:01:25 +01:00
2019-04-10 23:07:06 +02:00
if ( statspec = = nullptr ) {
2023-04-30 10:59:24 +02:00
GrfMsg ( 1 , " StationMapSpriteGroup: Station {} undefined, skipping " , station ) ;
2008-08-31 17:46:14 +02:00
continue ;
2008-06-02 21:52:41 +02:00
}
2007-03-23 21:01:25 +01:00
2011-08-21 14:07:15 +02:00
statspec - > grf_prop . spritegroup [ ctype ] = _cur . spritegroups [ groupid ] ;
2007-03-23 21:01:25 +01:00
}
}
2010-01-23 14:17:30 +01:00
uint16 groupid = buf - > ReadWord ( ) ;
2008-06-02 21:52:41 +02:00
if ( ! IsValidGroupID ( groupid , " StationMapSpriteGroup " ) ) return ;
2007-03-23 21:01:25 +01:00
2023-04-09 23:32:01 +02:00
for ( auto & station : stations ) {
2023-04-15 22:30:37 +02:00
StationSpec * statspec = station > = _cur . grffile - > stations . size ( ) ? nullptr : _cur . grffile - > stations [ station ] . get ( ) ;
2007-03-23 21:01:25 +01:00
2019-04-10 23:07:06 +02:00
if ( statspec = = nullptr ) {
2023-04-30 10:59:24 +02:00
GrfMsg ( 1 , " StationMapSpriteGroup: Station {} undefined, skipping " , station ) ;
2008-06-02 21:52:41 +02:00
continue ;
2007-03-23 21:01:25 +01:00
}
2008-06-02 21:52:41 +02:00
2019-04-10 23:07:06 +02:00
if ( statspec - > grf_prop . grffile ! = nullptr ) {
2023-04-30 10:59:24 +02:00
GrfMsg ( 1 , " StationMapSpriteGroup: Station {} mapped multiple times, skipping " , station ) ;
2010-08-07 23:39:30 +02:00
continue ;
}
2011-08-21 14:07:15 +02:00
statspec - > grf_prop . spritegroup [ CT_DEFAULT ] = _cur . spritegroups [ groupid ] ;
2011-08-21 14:05:47 +02:00
statspec - > grf_prop . grffile = _cur . grffile ;
2023-04-09 23:32:01 +02:00
statspec - > grf_prop . local_id = station ;
2010-08-08 00:07:07 +02:00
StationClass : : Assign ( statspec ) ;
2007-03-23 21:01:25 +01:00
}
}
2010-01-22 14:02:15 +01:00
static void TownHouseMapSpriteGroup ( ByteReader * buf , uint8 idcount )
2007-03-23 21:01:25 +01:00
{
2023-01-08 03:12:33 +01:00
if ( _cur . grffile - > housespec . empty ( ) ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " TownHouseMapSpriteGroup: No houses defined, skipping " ) ;
2023-04-06 21:29:57 +02:00
return ;
}
2023-04-30 11:00:43 +02:00
std : : vector < uint16_t > houses ;
2021-05-01 22:06:17 +02:00
houses . reserve ( idcount ) ;
2008-06-02 21:52:41 +02:00
for ( uint i = 0 ; i < idcount ; i + + ) {
2023-04-30 11:00:43 +02:00
houses . push_back ( buf - > ReadExtendedByte ( ) ) ;
2008-06-02 21:52:41 +02:00
}
/* Skip the cargo type section, we only care about the default group */
2010-01-23 14:17:30 +01:00
uint8 cidcount = buf - > ReadByte ( ) ;
2010-01-22 14:02:15 +01:00
buf - > Skip ( cidcount * 3 ) ;
2008-06-02 21:52:41 +02:00
2010-01-23 14:17:30 +01:00
uint16 groupid = buf - > ReadWord ( ) ;
2008-06-02 21:35:30 +02:00
if ( ! IsValidGroupID ( groupid , " TownHouseMapSpriteGroup " ) ) return ;
2007-03-23 21:01:25 +01:00
2023-04-09 23:32:01 +02:00
for ( auto & house : houses ) {
2023-04-15 22:30:37 +02:00
HouseSpec * hs = house > = _cur . grffile - > housespec . size ( ) ? nullptr : _cur . grffile - > housespec [ house ] . get ( ) ;
2007-03-23 21:01:25 +01:00
2019-04-10 23:07:06 +02:00
if ( hs = = nullptr ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " TownHouseMapSpriteGroup: House {} undefined, skipping. " , house ) ;
2008-08-31 17:46:14 +02:00
continue ;
2007-03-23 21:01:25 +01:00
}
2011-08-21 14:07:15 +02:00
hs - > grf_prop . spritegroup [ 0 ] = _cur . spritegroups [ groupid ] ;
2007-03-23 21:01:25 +01:00
}
}
2010-01-22 14:02:15 +01:00
static void IndustryMapSpriteGroup ( ByteReader * buf , uint8 idcount )
2007-07-10 02:10:19 +02:00
{
2023-01-08 03:12:33 +01:00
if ( _cur . grffile - > industryspec . empty ( ) ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " IndustryMapSpriteGroup: No industries defined, skipping " ) ;
2023-04-06 21:29:57 +02:00
return ;
}
2023-04-30 11:00:43 +02:00
std : : vector < uint16_t > industries ;
2021-05-01 22:06:17 +02:00
industries . reserve ( idcount ) ;
2008-06-02 21:52:41 +02:00
for ( uint i = 0 ; i < idcount ; i + + ) {
2023-04-30 11:00:43 +02:00
industries . push_back ( buf - > ReadExtendedByte ( ) ) ;
2008-06-02 21:52:41 +02:00
}
/* Skip the cargo type section, we only care about the default group */
2010-01-23 14:17:30 +01:00
uint8 cidcount = buf - > ReadByte ( ) ;
2010-01-22 14:02:15 +01:00
buf - > Skip ( cidcount * 3 ) ;
2008-06-02 21:52:41 +02:00
2010-01-23 14:17:30 +01:00
uint16 groupid = buf - > ReadWord ( ) ;
2008-06-02 21:35:30 +02:00
if ( ! IsValidGroupID ( groupid , " IndustryMapSpriteGroup " ) ) return ;
2007-07-10 02:10:19 +02:00
2023-04-09 23:32:01 +02:00
for ( auto & industry : industries ) {
2023-04-15 22:30:37 +02:00
IndustrySpec * indsp = industry > = _cur . grffile - > industryspec . size ( ) ? nullptr : _cur . grffile - > industryspec [ industry ] . get ( ) ;
2007-07-10 02:10:19 +02:00
2019-04-10 23:07:06 +02:00
if ( indsp = = nullptr ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " IndustryMapSpriteGroup: Industry {} undefined, skipping " , industry ) ;
2008-08-31 17:46:14 +02:00
continue ;
2007-07-10 02:10:19 +02:00
}
2011-08-21 14:07:15 +02:00
indsp - > grf_prop . spritegroup [ 0 ] = _cur . spritegroups [ groupid ] ;
2007-07-10 02:10:19 +02:00
}
}
2010-01-22 14:02:15 +01:00
static void IndustrytileMapSpriteGroup ( ByteReader * buf , uint8 idcount )
2007-07-10 02:10:19 +02:00
{
2023-01-08 03:12:33 +01:00
if ( _cur . grffile - > indtspec . empty ( ) ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " IndustrytileMapSpriteGroup: No industry tiles defined, skipping " ) ;
2023-04-06 21:29:57 +02:00
return ;
}
2023-04-30 11:00:43 +02:00
std : : vector < uint16_t > indtiles ;
2021-05-01 22:06:17 +02:00
indtiles . reserve ( idcount ) ;
2008-06-02 21:52:41 +02:00
for ( uint i = 0 ; i < idcount ; i + + ) {
2023-04-30 11:00:43 +02:00
indtiles . push_back ( buf - > ReadExtendedByte ( ) ) ;
2008-06-02 21:52:41 +02:00
}
/* Skip the cargo type section, we only care about the default group */
2010-01-23 14:17:30 +01:00
uint8 cidcount = buf - > ReadByte ( ) ;
2010-01-22 14:02:15 +01:00
buf - > Skip ( cidcount * 3 ) ;
2008-06-02 21:52:41 +02:00
2010-01-23 14:17:30 +01:00
uint16 groupid = buf - > ReadWord ( ) ;
2008-06-02 21:35:30 +02:00
if ( ! IsValidGroupID ( groupid , " IndustrytileMapSpriteGroup " ) ) return ;
2007-07-10 02:10:19 +02:00
2023-04-09 23:32:01 +02:00
for ( auto & indtile : indtiles ) {
2023-04-15 22:30:37 +02:00
IndustryTileSpec * indtsp = indtile > = _cur . grffile - > indtspec . size ( ) ? nullptr : _cur . grffile - > indtspec [ indtile ] . get ( ) ;
2007-07-10 02:10:19 +02:00
2019-04-10 23:07:06 +02:00
if ( indtsp = = nullptr ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " IndustrytileMapSpriteGroup: Industry tile {} undefined, skipping " , indtile ) ;
2008-08-31 17:46:14 +02:00
continue ;
2007-07-10 02:10:19 +02:00
}
2011-08-21 14:07:15 +02:00
indtsp - > grf_prop . spritegroup [ 0 ] = _cur . spritegroups [ groupid ] ;
2007-07-10 02:10:19 +02:00
}
}
2007-03-23 21:55:45 +01:00
2010-01-22 14:02:15 +01:00
static void CargoMapSpriteGroup ( ByteReader * buf , uint8 idcount )
2007-03-23 21:55:45 +01:00
{
2023-04-30 11:00:43 +02:00
std : : vector < uint16_t > cargoes ;
2021-05-01 22:06:17 +02:00
cargoes . reserve ( idcount ) ;
2008-06-02 21:52:41 +02:00
for ( uint i = 0 ; i < idcount ; i + + ) {
2023-04-30 11:00:43 +02:00
cargoes . push_back ( buf - > ReadExtendedByte ( ) ) ;
2008-06-02 21:52:41 +02:00
}
/* Skip the cargo type section, we only care about the default group */
2010-01-23 14:17:30 +01:00
uint8 cidcount = buf - > ReadByte ( ) ;
2010-01-22 14:02:15 +01:00
buf - > Skip ( cidcount * 3 ) ;
2008-06-02 21:52:41 +02:00
2010-01-23 14:17:30 +01:00
uint16 groupid = buf - > ReadWord ( ) ;
2008-06-02 21:35:30 +02:00
if ( ! IsValidGroupID ( groupid , " CargoMapSpriteGroup " ) ) return ;
2007-03-23 21:55:45 +01:00
2023-04-09 23:32:01 +02:00
for ( auto & cid : cargoes ) {
2007-03-23 21:55:45 +01:00
if ( cid > = NUM_CARGO ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " CargoMapSpriteGroup: Cargo ID {} out of range, skipping " , cid ) ;
2007-03-23 21:55:45 +01:00
continue ;
}
2009-07-16 21:00:13 +02:00
CargoSpec * cs = CargoSpec : : Get ( cid ) ;
2011-08-21 14:05:47 +02:00
cs - > grffile = _cur . grffile ;
2011-08-21 14:07:15 +02:00
cs - > group = _cur . spritegroups [ groupid ] ;
2007-03-23 21:55:45 +01:00
}
}
2010-08-28 20:22:24 +02:00
static void ObjectMapSpriteGroup ( ByteReader * buf , uint8 idcount )
{
2023-01-08 03:12:33 +01:00
if ( _cur . grffile - > objectspec . empty ( ) ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " ObjectMapSpriteGroup: No object tiles defined, skipping " ) ;
2010-08-28 20:22:24 +02:00
return ;
}
2023-04-30 11:00:43 +02:00
std : : vector < uint16_t > objects ;
2021-05-01 22:06:17 +02:00
objects . reserve ( idcount ) ;
2010-08-28 20:22:24 +02:00
for ( uint i = 0 ; i < idcount ; i + + ) {
2023-04-30 11:00:43 +02:00
objects . push_back ( buf - > ReadExtendedByte ( ) ) ;
2010-08-28 20:22:24 +02:00
}
uint8 cidcount = buf - > ReadByte ( ) ;
for ( uint c = 0 ; c < cidcount ; c + + ) {
uint8 ctype = buf - > ReadByte ( ) ;
uint16 groupid = buf - > ReadWord ( ) ;
if ( ! IsValidGroupID ( groupid , " ObjectMapSpriteGroup " ) ) continue ;
2023-06-02 10:25:13 +02:00
/* The only valid option here is purchase list sprite groups. */
if ( ctype ! = 0xFF ) {
GrfMsg ( 1 , " ObjectMapSpriteGroup: Invalid cargo bitnum {} for objects, skipping. " , ctype ) ;
continue ;
}
2010-08-28 20:22:24 +02:00
2023-04-09 23:32:01 +02:00
for ( auto & object : objects ) {
2023-04-15 22:30:37 +02:00
ObjectSpec * spec = object > = _cur . grffile - > objectspec . size ( ) ? nullptr : _cur . grffile - > objectspec [ object ] . get ( ) ;
2010-08-28 20:22:24 +02:00
2019-04-10 23:07:06 +02:00
if ( spec = = nullptr ) {
2023-04-30 10:59:24 +02:00
GrfMsg ( 1 , " ObjectMapSpriteGroup: Object {} undefined, skipping " , object ) ;
2010-08-28 20:22:24 +02:00
continue ;
}
2023-06-02 10:25:13 +02:00
spec - > grf_prop . spritegroup [ OBJECT_SPRITE_GROUP_PURCHASE ] = _cur . spritegroups [ groupid ] ;
2010-08-28 20:22:24 +02:00
}
}
uint16 groupid = buf - > ReadWord ( ) ;
if ( ! IsValidGroupID ( groupid , " ObjectMapSpriteGroup " ) ) return ;
2023-04-09 23:32:01 +02:00
for ( auto & object : objects ) {
2023-04-15 22:30:37 +02:00
ObjectSpec * spec = object > = _cur . grffile - > objectspec . size ( ) ? nullptr : _cur . grffile - > objectspec [ object ] . get ( ) ;
2010-08-28 20:22:24 +02:00
2019-04-10 23:07:06 +02:00
if ( spec = = nullptr ) {
2023-04-30 10:59:24 +02:00
GrfMsg ( 1 , " ObjectMapSpriteGroup: Object {} undefined, skipping " , object ) ;
2010-08-28 20:22:24 +02:00
continue ;
}
2019-04-10 23:07:06 +02:00
if ( spec - > grf_prop . grffile ! = nullptr ) {
2023-04-30 10:59:24 +02:00
GrfMsg ( 1 , " ObjectMapSpriteGroup: Object {} mapped multiple times, skipping " , object ) ;
2010-08-28 20:22:24 +02:00
continue ;
}
2023-06-02 10:25:13 +02:00
spec - > grf_prop . spritegroup [ OBJECT_SPRITE_GROUP_DEFAULT ] = _cur . spritegroups [ groupid ] ;
spec - > grf_prop . grffile = _cur . grffile ;
spec - > grf_prop . local_id = object ;
2010-08-28 20:22:24 +02:00
}
}
2010-02-07 23:22:54 +01:00
static void RailTypeMapSpriteGroup ( ByteReader * buf , uint8 idcount )
{
2021-05-01 22:06:17 +02:00
std : : vector < uint8 > railtypes ;
railtypes . reserve ( idcount ) ;
2010-02-07 23:22:54 +01:00
for ( uint i = 0 ; i < idcount ; i + + ) {
2023-04-30 11:00:43 +02:00
uint16_t id = buf - > ReadExtendedByte ( ) ;
2021-05-01 22:06:17 +02:00
railtypes . push_back ( id < RAILTYPE_END ? _cur . grffile - > railtype_map [ id ] : INVALID_RAILTYPE ) ;
2010-02-07 23:22:54 +01:00
}
uint8 cidcount = buf - > ReadByte ( ) ;
for ( uint c = 0 ; c < cidcount ; c + + ) {
uint8 ctype = buf - > ReadByte ( ) ;
uint16 groupid = buf - > ReadWord ( ) ;
if ( ! IsValidGroupID ( groupid , " RailTypeMapSpriteGroup " ) ) continue ;
if ( ctype > = RTSG_END ) continue ;
extern RailtypeInfo _railtypes [ RAILTYPE_END ] ;
2023-04-09 23:32:01 +02:00
for ( auto & railtype : railtypes ) {
if ( railtype ! = INVALID_RAILTYPE ) {
RailtypeInfo * rti = & _railtypes [ railtype ] ;
2010-02-07 23:22:54 +01:00
2011-08-21 14:05:47 +02:00
rti - > grffile [ ctype ] = _cur . grffile ;
2011-08-21 14:07:15 +02:00
rti - > group [ ctype ] = _cur . spritegroups [ groupid ] ;
2010-02-07 23:22:54 +01:00
}
}
}
/* Railtypes do not use the default group. */
buf - > ReadWord ( ) ;
}
2019-04-06 08:46:15 +02:00
static void RoadTypeMapSpriteGroup ( ByteReader * buf , uint8 idcount , RoadTramType rtt )
{
RoadType * type_map = ( rtt = = RTT_TRAM ) ? _cur . grffile - > tramtype_map : _cur . grffile - > roadtype_map ;
2021-05-01 22:06:17 +02:00
std : : vector < uint8 > roadtypes ;
roadtypes . reserve ( idcount ) ;
2019-04-06 08:46:15 +02:00
for ( uint i = 0 ; i < idcount ; i + + ) {
2023-04-30 11:00:43 +02:00
uint16_t id = buf - > ReadExtendedByte ( ) ;
2021-05-01 22:06:17 +02:00
roadtypes . push_back ( id < ROADTYPE_END ? type_map [ id ] : INVALID_ROADTYPE ) ;
2019-04-06 08:46:15 +02:00
}
uint8 cidcount = buf - > ReadByte ( ) ;
for ( uint c = 0 ; c < cidcount ; c + + ) {
uint8 ctype = buf - > ReadByte ( ) ;
uint16 groupid = buf - > ReadWord ( ) ;
if ( ! IsValidGroupID ( groupid , " RoadTypeMapSpriteGroup " ) ) continue ;
if ( ctype > = ROTSG_END ) continue ;
extern RoadTypeInfo _roadtypes [ ROADTYPE_END ] ;
2023-04-09 23:32:01 +02:00
for ( auto & roadtype : roadtypes ) {
if ( roadtype ! = INVALID_ROADTYPE ) {
RoadTypeInfo * rti = & _roadtypes [ roadtype ] ;
2019-04-06 08:46:15 +02:00
rti - > grffile [ ctype ] = _cur . grffile ;
rti - > group [ ctype ] = _cur . spritegroups [ groupid ] ;
}
}
}
/* Roadtypes do not use the default group. */
buf - > ReadWord ( ) ;
}
2010-03-19 00:15:38 +01:00
static void AirportMapSpriteGroup ( ByteReader * buf , uint8 idcount )
{
2023-01-08 03:12:33 +01:00
if ( _cur . grffile - > airportspec . empty ( ) ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " AirportMapSpriteGroup: No airports defined, skipping " ) ;
2023-04-06 21:29:57 +02:00
return ;
}
2023-04-30 11:00:43 +02:00
std : : vector < uint16_t > airports ;
2021-05-01 22:06:17 +02:00
airports . reserve ( idcount ) ;
2010-03-19 00:15:38 +01:00
for ( uint i = 0 ; i < idcount ; i + + ) {
2023-04-30 11:00:43 +02:00
airports . push_back ( buf - > ReadExtendedByte ( ) ) ;
2010-03-19 00:15:38 +01:00
}
/* Skip the cargo type section, we only care about the default group */
uint8 cidcount = buf - > ReadByte ( ) ;
buf - > Skip ( cidcount * 3 ) ;
uint16 groupid = buf - > ReadWord ( ) ;
if ( ! IsValidGroupID ( groupid , " AirportMapSpriteGroup " ) ) return ;
2023-04-09 23:32:01 +02:00
for ( auto & airport : airports ) {
2023-04-15 22:30:37 +02:00
AirportSpec * as = airport > = _cur . grffile - > airportspec . size ( ) ? nullptr : _cur . grffile - > airportspec [ airport ] . get ( ) ;
2010-03-19 00:15:38 +01:00
2019-04-10 23:07:06 +02:00
if ( as = = nullptr ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " AirportMapSpriteGroup: Airport {} undefined, skipping " , airport ) ;
2010-03-19 00:15:38 +01:00
continue ;
}
2011-08-21 14:07:15 +02:00
as - > grf_prop . spritegroup [ 0 ] = _cur . spritegroups [ groupid ] ;
2010-03-19 00:15:38 +01:00
}
}
2010-02-22 15:16:19 +01:00
static void AirportTileMapSpriteGroup ( ByteReader * buf , uint8 idcount )
{
2023-01-08 03:12:33 +01:00
if ( _cur . grffile - > airtspec . empty ( ) ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " AirportTileMapSpriteGroup: No airport tiles defined, skipping " ) ;
2023-04-06 21:29:57 +02:00
return ;
}
2023-04-30 11:00:43 +02:00
std : : vector < uint16_t > airptiles ;
2021-05-01 22:06:17 +02:00
airptiles . reserve ( idcount ) ;
2010-02-22 15:16:19 +01:00
for ( uint i = 0 ; i < idcount ; i + + ) {
2023-04-30 11:00:43 +02:00
airptiles . push_back ( buf - > ReadExtendedByte ( ) ) ;
2010-02-22 15:16:19 +01:00
}
/* Skip the cargo type section, we only care about the default group */
uint8 cidcount = buf - > ReadByte ( ) ;
buf - > Skip ( cidcount * 3 ) ;
uint16 groupid = buf - > ReadWord ( ) ;
if ( ! IsValidGroupID ( groupid , " AirportTileMapSpriteGroup " ) ) return ;
2023-04-09 23:32:01 +02:00
for ( auto & airptile : airptiles ) {
2023-04-15 22:30:37 +02:00
AirportTileSpec * airtsp = airptile > = _cur . grffile - > airtspec . size ( ) ? nullptr : _cur . grffile - > airtspec [ airptile ] . get ( ) ;
2010-02-22 15:16:19 +01:00
2019-04-10 23:07:06 +02:00
if ( airtsp = = nullptr ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " AirportTileMapSpriteGroup: Airport tile {} undefined, skipping " , airptile ) ;
2010-02-22 15:16:19 +01:00
continue ;
}
2011-08-21 14:07:15 +02:00
airtsp - > grf_prop . spritegroup [ 0 ] = _cur . spritegroups [ groupid ] ;
2010-02-22 15:16:19 +01:00
}
}
2022-11-06 16:01:27 +01:00
static void RoadStopMapSpriteGroup ( ByteReader * buf , uint8 idcount )
{
2023-01-08 03:12:33 +01:00
if ( _cur . grffile - > roadstops . empty ( ) ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " RoadStopMapSpriteGroup: No roadstops defined, skipping " ) ;
2023-04-06 21:29:57 +02:00
return ;
}
2023-04-30 11:00:43 +02:00
std : : vector < uint16_t > roadstops ;
2021-05-01 22:06:17 +02:00
roadstops . reserve ( idcount ) ;
2022-11-06 16:01:27 +01:00
for ( uint i = 0 ; i < idcount ; i + + ) {
2023-04-30 11:00:43 +02:00
roadstops . push_back ( buf - > ReadExtendedByte ( ) ) ;
2022-11-06 16:01:27 +01:00
}
uint8 cidcount = buf - > ReadByte ( ) ;
for ( uint c = 0 ; c < cidcount ; c + + ) {
uint8 ctype = buf - > ReadByte ( ) ;
uint16 groupid = buf - > ReadWord ( ) ;
if ( ! IsValidGroupID ( groupid , " RoadStopMapSpriteGroup " ) ) continue ;
ctype = TranslateCargo ( GSF_ROADSTOPS , ctype ) ;
2023-05-04 12:29:21 +02:00
if ( ! IsValidCargoID ( ctype ) ) continue ;
2022-11-06 16:01:27 +01:00
2023-04-09 23:32:01 +02:00
for ( auto & roadstop : roadstops ) {
2023-04-15 22:30:37 +02:00
RoadStopSpec * roadstopspec = roadstop > = _cur . grffile - > roadstops . size ( ) ? nullptr : _cur . grffile - > roadstops [ roadstop ] . get ( ) ;
2022-11-06 16:01:27 +01:00
if ( roadstopspec = = nullptr ) {
2023-04-30 10:59:24 +02:00
GrfMsg ( 1 , " RoadStopMapSpriteGroup: Road stop {} undefined, skipping " , roadstop ) ;
2022-11-06 16:01:27 +01:00
continue ;
}
roadstopspec - > grf_prop . spritegroup [ ctype ] = _cur . spritegroups [ groupid ] ;
}
}
uint16 groupid = buf - > ReadWord ( ) ;
if ( ! IsValidGroupID ( groupid , " RoadStopMapSpriteGroup " ) ) return ;
2023-04-09 23:32:01 +02:00
for ( auto & roadstop : roadstops ) {
2023-04-15 22:30:37 +02:00
RoadStopSpec * roadstopspec = roadstop > = _cur . grffile - > roadstops . size ( ) ? nullptr : _cur . grffile - > roadstops [ roadstop ] . get ( ) ;
2022-11-06 16:01:27 +01:00
if ( roadstopspec = = nullptr ) {
2023-04-30 10:59:24 +02:00
GrfMsg ( 1 , " RoadStopMapSpriteGroup: Road stop {} undefined, skipping. " , roadstop ) ;
2022-11-06 16:01:27 +01:00
continue ;
}
if ( roadstopspec - > grf_prop . grffile ! = nullptr ) {
2023-04-30 10:59:24 +02:00
GrfMsg ( 1 , " RoadStopMapSpriteGroup: Road stop {} mapped multiple times, skipping " , roadstop ) ;
2022-11-06 16:01:27 +01:00
continue ;
}
roadstopspec - > grf_prop . spritegroup [ CT_DEFAULT ] = _cur . spritegroups [ groupid ] ;
roadstopspec - > grf_prop . grffile = _cur . grffile ;
2023-04-09 23:32:01 +02:00
roadstopspec - > grf_prop . local_id = roadstop ;
2022-11-06 16:01:27 +01:00
RoadStopClass : : Assign ( roadstopspec ) ;
}
}
2007-03-23 21:55:45 +01:00
2007-03-23 21:01:25 +01:00
/* Action 0x03 */
2010-01-22 14:02:15 +01:00
static void FeatureMapSpriteGroup ( ByteReader * buf )
2007-03-23 21:01:25 +01:00
{
/* <03> <feature> <n-id> <ids>... <num-cid> [<cargo-type> <cid>]... <def-cid>
* id - list : = [ < id > ] [ id - list ]
* cargo - list : = < cargo - type > < cid > [ cargo - list ]
*
* B feature see action 0
* B n - id bits 0 - 6 : how many IDs this definition applies to
* bit 7 : if set , this is a wagon override definition ( see below )
2023-04-30 11:00:43 +02:00
* E ids the IDs for which this definition applies
2007-03-23 21:01:25 +01:00
* B num - cid number of cargo IDs ( sprite group IDs ) in this definition
* can be zero , in that case the def - cid is used always
* B cargo - type type of this cargo type ( e . g . mail = 2 , wood = 7 , see below )
* W cid cargo ID ( sprite group ID ) for this type of cargo
* W def - cid default cargo ID ( sprite group ID ) */
2010-01-23 14:17:30 +01:00
uint8 feature = buf - > ReadByte ( ) ;
uint8 idcount = buf - > ReadByte ( ) ;
2007-03-23 21:01:25 +01:00
2019-03-18 03:05:06 +01:00
if ( feature > = GSF_END ) {
2023-04-20 17:00:46 +02:00
GrfMsg ( 1 , " FeatureMapSpriteGroup: Unsupported feature 0x{:02X}, skipping " , feature ) ;
2019-03-18 03:05:06 +01:00
return ;
}
2007-03-23 21:01:25 +01:00
/* If idcount is zero, this is a feature callback */
if ( idcount = = 0 ) {
2008-06-02 21:52:41 +02:00
/* Skip number of cargo ids? */
2010-01-23 14:17:30 +01:00
buf - > ReadByte ( ) ;
uint16 groupid = buf - > ReadWord ( ) ;
2011-05-01 23:02:18 +02:00
if ( ! IsValidGroupID ( groupid , " FeatureMapSpriteGroup " ) ) return ;
2008-02-12 14:23:57 +01:00
2023-04-20 17:00:46 +02:00
GrfMsg ( 6 , " FeatureMapSpriteGroup: Adding generic feature callback for feature 0x{:02X} " , feature ) ;
2008-02-12 14:23:57 +01:00
2011-08-21 14:07:15 +02:00
AddGenericCallback ( feature , _cur . grffile , _cur . spritegroups [ groupid ] ) ;
2007-03-23 21:01:25 +01:00
return ;
}
2009-11-24 14:15:58 +01:00
/* Mark the feature as used by the grf (generic callbacks do not count) */
2011-08-21 14:05:47 +02:00
SetBit ( _cur . grffile - > grf_features , feature ) ;
2009-11-24 14:15:58 +01:00
2023-04-20 17:00:46 +02:00
GrfMsg ( 6 , " FeatureMapSpriteGroup: Feature 0x{:02X}, {} ids " , feature , idcount ) ;
2007-03-23 21:01:25 +01:00
switch ( feature ) {
2010-04-24 13:52:58 +02:00
case GSF_TRAINS :
case GSF_ROADVEHICLES :
case GSF_SHIPS :
2007-03-23 21:01:25 +01:00
case GSF_AIRCRAFT :
2008-06-02 21:52:41 +02:00
VehicleMapSpriteGroup ( buf , feature , idcount ) ;
2007-03-23 21:01:25 +01:00
return ;
2010-04-24 13:52:58 +02:00
case GSF_CANALS :
2008-06-02 21:52:41 +02:00
CanalMapSpriteGroup ( buf , idcount ) ;
2007-05-06 20:14:33 +02:00
return ;
2010-04-24 13:52:58 +02:00
case GSF_STATIONS :
2008-06-02 21:52:41 +02:00
StationMapSpriteGroup ( buf , idcount ) ;
2007-03-23 21:01:25 +01:00
return ;
2010-04-24 13:52:58 +02:00
case GSF_HOUSES :
2008-06-02 21:52:41 +02:00
TownHouseMapSpriteGroup ( buf , idcount ) ;
2007-03-23 21:01:25 +01:00
return ;
2007-07-10 02:10:19 +02:00
case GSF_INDUSTRIES :
2008-06-02 21:52:41 +02:00
IndustryMapSpriteGroup ( buf , idcount ) ;
2007-07-10 02:10:19 +02:00
return ;
case GSF_INDUSTRYTILES :
2008-06-02 21:52:41 +02:00
IndustrytileMapSpriteGroup ( buf , idcount ) ;
2007-07-10 02:10:19 +02:00
return ;
2011-12-15 22:56:00 +01:00
case GSF_CARGOES :
2008-06-02 21:52:41 +02:00
CargoMapSpriteGroup ( buf , idcount ) ;
2007-03-23 21:55:45 +01:00
return ;
2010-03-19 00:15:38 +01:00
case GSF_AIRPORTS :
AirportMapSpriteGroup ( buf , idcount ) ;
return ;
2010-08-28 20:22:24 +02:00
case GSF_OBJECTS :
ObjectMapSpriteGroup ( buf , idcount ) ;
break ;
2010-02-07 23:22:54 +01:00
case GSF_RAILTYPES :
RailTypeMapSpriteGroup ( buf , idcount ) ;
break ;
2019-04-06 08:46:15 +02:00
case GSF_ROADTYPES :
RoadTypeMapSpriteGroup ( buf , idcount , RTT_ROAD ) ;
break ;
case GSF_TRAMTYPES :
RoadTypeMapSpriteGroup ( buf , idcount , RTT_TRAM ) ;
break ;
2010-02-22 15:16:19 +01:00
case GSF_AIRPORTTILES :
AirportTileMapSpriteGroup ( buf , idcount ) ;
return ;
2022-11-06 16:01:27 +01:00
case GSF_ROADSTOPS :
RoadStopMapSpriteGroup ( buf , idcount ) ;
return ;
2007-03-23 21:01:25 +01:00
default :
2023-04-20 17:00:46 +02:00
GrfMsg ( 1 , " FeatureMapSpriteGroup: Unsupported feature 0x{:02X}, skipping " , feature ) ;
2007-03-23 21:01:25 +01:00
return ;
}
}
2004-11-12 16:15:56 +01:00
/* Action 0x04 */
2010-01-22 14:02:15 +01:00
static void FeatureNewName ( ByteReader * buf )
2004-08-09 19:04:08 +02:00
{
/* <04> <veh-type> <language-id> <num-veh> <offset> <data...>
*
2006-04-21 05:00:20 +02:00
* B veh - type see action 0 ( as 00. .07 , + 0 A
* But IF veh - type = 48 , then generic text
* B language - id If bit 6 is set , This is the extended language scheme ,
2009-08-20 12:23:39 +02:00
* with up to 64 language .
* Otherwise , it is a mapping where set bits have meaning
* 0 = american , 1 = english , 2 = german , 3 = french , 4 = spanish
* Bit 7 set means this is a generic text , not a vehicle one ( or else )
2004-08-09 19:04:08 +02:00
* B num - veh number of vehicles which are getting a new name
2005-08-16 00:48:33 +02:00
* B / W offset number of the first vehicle that gets a new name
2006-04-21 05:00:20 +02:00
* Byte : ID of vehicle to change
* Word : ID of string to change / add
2004-08-09 19:04:08 +02:00
* S data new texts , each of them zero - terminated , after
* which the next name begins . */
2007-03-10 10:55:11 +01:00
2011-08-21 14:05:47 +02:00
bool new_scheme = _cur . grffile - > grf_version > = 7 ;
2004-08-09 19:04:08 +02:00
2010-01-23 14:17:30 +01:00
uint8 feature = buf - > ReadByte ( ) ;
2019-03-30 20:56:43 +01:00
if ( feature > = GSF_END & & feature ! = 0x48 ) {
2023-04-20 17:00:46 +02:00
GrfMsg ( 1 , " FeatureNewName: Unsupported feature 0x{:02X}, skipping " , feature ) ;
2019-03-18 03:05:06 +01:00
return ;
}
2010-01-23 14:17:30 +01:00
uint8 lang = buf - > ReadByte ( ) ;
uint8 num = buf - > ReadByte ( ) ;
2007-11-19 22:02:30 +01:00
bool generic = HasBit ( lang , 7 ) ;
2008-06-12 11:25:04 +02:00
uint16 id ;
if ( generic ) {
2010-01-23 14:17:30 +01:00
id = buf - > ReadWord ( ) ;
2008-06-12 11:25:04 +02:00
} else if ( feature < = GSF_AIRCRAFT ) {
2010-03-10 08:31:08 +01:00
id = buf - > ReadExtendedByte ( ) ;
2008-06-12 11:25:04 +02:00
} else {
2010-01-23 14:17:30 +01:00
id = buf - > ReadByte ( ) ;
2008-06-12 11:25:04 +02:00
}
2006-10-28 20:42:57 +02:00
2007-11-19 22:32:20 +01:00
ClrBit ( lang , 7 ) ;
2005-08-16 00:48:33 +02:00
2007-03-10 10:55:11 +01:00
uint16 endid = id + num ;
2004-08-09 19:04:08 +02:00
2023-04-20 17:00:46 +02:00
GrfMsg ( 6 , " FeatureNewName: About to rename engines {}..{} (feature 0x{:02X}) in language 0x{:02X} " ,
2004-11-13 19:08:45 +01:00
id , endid , feature , lang ) ;
2010-01-22 14:02:15 +01:00
for ( ; id < endid & & buf - > HasData ( ) ; id + + ) {
2010-01-23 14:17:30 +01:00
const char * name = buf - > ReadString ( ) ;
2023-04-20 17:00:46 +02:00
GrfMsg ( 8 , " FeatureNewName: 0x{:04X} <- {} " , id , name ) ;
2006-04-21 05:00:20 +02:00
2007-05-11 21:22:43 +02:00
switch ( feature ) {
2010-04-24 13:52:58 +02:00
case GSF_TRAINS :
case GSF_ROADVEHICLES :
case GSF_SHIPS :
2007-05-11 21:22:43 +02:00
case GSF_AIRCRAFT :
2008-06-03 11:58:13 +02:00
if ( ! generic ) {
2011-08-21 14:05:47 +02:00
Engine * e = GetNewEngine ( _cur . grffile , ( VehicleType ) feature , id , HasBit ( _cur . grfconfig - > flags , GCF_STATIC ) ) ;
2019-04-10 23:07:06 +02:00
if ( e = = nullptr ) break ;
2011-10-17 22:28:56 +02:00
StringID string = AddGRFString ( _cur . grffile - > grfid , e - > index , lang , new_scheme , false , name , e - > info . string_id ) ;
2008-04-29 23:31:29 +02:00
e - > info . string_id = string ;
2007-05-11 21:22:43 +02:00
} else {
2011-10-01 22:31:21 +02:00
AddGRFString ( _cur . grffile - > grfid , id , lang , new_scheme , true , name , STR_UNDEFINED ) ;
2006-04-21 22:59:04 +02:00
}
2007-05-11 21:22:43 +02:00
break ;
2006-04-21 05:00:20 +02:00
2007-05-11 21:22:43 +02:00
default :
2023-01-22 18:34:35 +01:00
if ( IsInsideMM ( id , 0xD000 , 0xD400 ) | | IsInsideMM ( id , 0xD800 , 0x10000 ) ) {
2017-03-05 15:44:15 +01:00
AddGRFString ( _cur . grffile - > grfid , id , lang , new_scheme , true , name , STR_UNDEFINED ) ;
break ;
}
2007-05-11 21:22:43 +02:00
switch ( GB ( id , 8 , 8 ) ) {
case 0xC4 : // Station class name
2023-01-08 03:12:33 +01:00
if ( GB ( id , 0 , 8 ) > = _cur . grffile - > stations . size ( ) | | _cur . grffile - > stations [ GB ( id , 0 , 8 ) ] = = nullptr ) {
2023-04-20 17:00:46 +02:00
GrfMsg ( 1 , " FeatureNewName: Attempt to name undefined station 0x{:X}, ignoring " , GB ( id , 0 , 8 ) ) ;
2007-05-11 21:22:43 +02:00
} else {
2011-08-21 14:05:47 +02:00
StationClassID cls_id = _cur . grffile - > stations [ GB ( id , 0 , 8 ) ] - > cls_id ;
2012-04-22 18:27:55 +02:00
StationClass : : Get ( cls_id ) - > name = AddGRFString ( _cur . grffile - > grfid , id , lang , new_scheme , false , name , STR_UNDEFINED ) ;
2007-03-19 12:27:30 +01:00
}
2007-05-11 21:22:43 +02:00
break ;
2007-03-19 12:27:30 +01:00
2007-05-11 21:22:43 +02:00
case 0xC5 : // Station name
2023-01-08 03:12:33 +01:00
if ( GB ( id , 0 , 8 ) > = _cur . grffile - > stations . size ( ) | | _cur . grffile - > stations [ GB ( id , 0 , 8 ) ] = = nullptr ) {
2023-04-20 17:00:46 +02:00
GrfMsg ( 1 , " FeatureNewName: Attempt to name undefined station 0x{:X}, ignoring " , GB ( id , 0 , 8 ) ) ;
2007-05-11 21:22:43 +02:00
} else {
2011-10-01 22:31:21 +02:00
_cur . grffile - > stations [ GB ( id , 0 , 8 ) ] - > name = AddGRFString ( _cur . grffile - > grfid , id , lang , new_scheme , false , name , STR_UNDEFINED ) ;
2007-05-11 21:22:43 +02:00
}
break ;
2006-09-01 04:56:02 +02:00
2010-02-22 15:17:17 +01:00
case 0xC7 : // Airporttile name
2023-01-08 03:12:33 +01:00
if ( GB ( id , 0 , 8 ) > = _cur . grffile - > airtspec . size ( ) | | _cur . grffile - > airtspec [ GB ( id , 0 , 8 ) ] = = nullptr ) {
2023-04-20 17:00:46 +02:00
GrfMsg ( 1 , " FeatureNewName: Attempt to name undefined airport tile 0x{:X}, ignoring " , GB ( id , 0 , 8 ) ) ;
2010-02-22 15:17:17 +01:00
} else {
2011-10-01 22:31:21 +02:00
_cur . grffile - > airtspec [ GB ( id , 0 , 8 ) ] - > name = AddGRFString ( _cur . grffile - > grfid , id , lang , new_scheme , false , name , STR_UNDEFINED ) ;
2010-02-22 15:17:17 +01:00
}
break ;
2007-05-11 21:22:43 +02:00
case 0xC9 : // House name
2023-01-08 03:12:33 +01:00
if ( GB ( id , 0 , 8 ) > = _cur . grffile - > housespec . size ( ) | | _cur . grffile - > housespec [ GB ( id , 0 , 8 ) ] = = nullptr ) {
2023-04-20 17:00:46 +02:00
GrfMsg ( 1 , " FeatureNewName: Attempt to name undefined house 0x{:X}, ignoring. " , GB ( id , 0 , 8 ) ) ;
2007-05-11 21:22:43 +02:00
} else {
2011-10-01 22:31:21 +02:00
_cur . grffile - > housespec [ GB ( id , 0 , 8 ) ] - > building_name = AddGRFString ( _cur . grffile - > grfid , id , lang , new_scheme , false , name , STR_UNDEFINED ) ;
2007-05-11 21:22:43 +02:00
}
break ;
default :
2023-04-20 17:00:46 +02:00
GrfMsg ( 7 , " FeatureNewName: Unsupported ID (0x{:04X}) " , id ) ;
2007-05-11 21:22:43 +02:00
break ;
}
break ;
2004-11-13 19:08:45 +01:00
}
2004-08-09 19:04:08 +02:00
}
}
2007-11-15 08:42:25 +01:00
/**
* Sanitize incoming sprite offsets for Action 5 graphics replacements .
2011-09-11 13:14:22 +02:00
* @ param num The number of sprites to load .
* @ param offset Offset from the base .
* @ param max_sprites The maximum number of sprites that can be loaded in this action 5.
* @ param name Used for error warnings .
* @ return The number of sprites that is going to be skipped .
2007-11-15 08:42:25 +01:00
*/
static uint16 SanitizeSpriteOffset ( uint16 & num , uint16 offset , int max_sprites , const char * name )
{
if ( offset > = max_sprites ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " GraphicsNew: {} sprite offset must be less than {}, skipping " , name , max_sprites ) ;
2007-11-15 08:42:25 +01:00
uint orig_num = num ;
num = 0 ;
return orig_num ;
}
if ( offset + num > max_sprites ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 4 , " GraphicsNew: {} sprite overflow, truncating... " , name ) ;
2007-11-15 08:42:25 +01:00
uint orig_num = num ;
2021-01-08 11:16:18 +01:00
num = std : : max ( max_sprites - offset , 0 ) ;
2007-11-15 08:42:25 +01:00
return orig_num - num ;
}
return 0 ;
}
2010-08-19 21:23:38 +02:00
/** The type of action 5 type. */
enum Action5BlockType {
A5BLOCK_FIXED , ///< Only allow replacing a whole block of sprites. (TTDP compatible)
A5BLOCK_ALLOW_OFFSET , ///< Allow replacing any subset by specifiing an offset.
A5BLOCK_INVALID , ///< unknown/not-implemented type
} ;
/** Information about a single action 5 type. */
struct Action5Type {
Action5BlockType block_type ; ///< How is this Action5 type processed?
SpriteID sprite_base ; ///< Load the sprites starting from this sprite.
uint16 min_sprites ; ///< If the Action5 contains less sprites, the whole block will be ignored.
uint16 max_sprites ; ///< If the Action5 contains more sprites, only the first max_sprites sprites will be used.
const char * name ; ///< Name for error messages.
} ;
/** The information about action 5 types. */
static const Action5Type _action5_types [ ] = {
/* Note: min_sprites should not be changed. Therefore these constants are directly here and not in sprites.h */
2011-10-05 19:07:43 +02:00
/* 0x00 */ { A5BLOCK_INVALID , 0 , 0 , 0 , " Type 0x00 " } ,
/* 0x01 */ { A5BLOCK_INVALID , 0 , 0 , 0 , " Type 0x01 " } ,
/* 0x02 */ { A5BLOCK_INVALID , 0 , 0 , 0 , " Type 0x02 " } ,
/* 0x03 */ { A5BLOCK_INVALID , 0 , 0 , 0 , " Type 0x03 " } ,
/* 0x04 */ { A5BLOCK_ALLOW_OFFSET , SPR_SIGNALS_BASE , 1 , PRESIGNAL_SEMAPHORE_AND_PBS_SPRITE_COUNT , " Signal graphics " } ,
2016-11-05 12:53:03 +01:00
/* 0x05 */ { A5BLOCK_ALLOW_OFFSET , SPR_ELRAIL_BASE , 1 , ELRAIL_SPRITE_COUNT , " Rail catenary graphics " } ,
2011-10-05 19:07:43 +02:00
/* 0x06 */ { A5BLOCK_ALLOW_OFFSET , SPR_SLOPES_BASE , 1 , NORMAL_AND_HALFTILE_FOUNDATION_SPRITE_COUNT , " Foundation graphics " } ,
/* 0x07 */ { A5BLOCK_INVALID , 0 , 75 , 0 , " TTDP GUI graphics " } , // Not used by OTTD.
/* 0x08 */ { A5BLOCK_ALLOW_OFFSET , SPR_CANALS_BASE , 1 , CANALS_SPRITE_COUNT , " Canal graphics " } ,
/* 0x09 */ { A5BLOCK_ALLOW_OFFSET , SPR_ONEWAY_BASE , 1 , ONEWAY_SPRITE_COUNT , " One way road graphics " } ,
/* 0x0A */ { A5BLOCK_ALLOW_OFFSET , SPR_2CCMAP_BASE , 1 , TWOCCMAP_SPRITE_COUNT , " 2CC colour maps " } ,
/* 0x0B */ { A5BLOCK_ALLOW_OFFSET , SPR_TRAMWAY_BASE , 1 , TRAMWAY_SPRITE_COUNT , " Tramway graphics " } ,
/* 0x0C */ { A5BLOCK_INVALID , 0 , 133 , 0 , " Snowy temperate tree " } , // Not yet used by OTTD.
/* 0x0D */ { A5BLOCK_FIXED , SPR_SHORE_BASE , 16 , SPR_SHORE_SPRITE_COUNT , " Shore graphics " } ,
/* 0x0E */ { A5BLOCK_INVALID , 0 , 0 , 0 , " New Signals graphics " } , // Not yet used by OTTD.
/* 0x0F */ { A5BLOCK_ALLOW_OFFSET , SPR_TRACKS_FOR_SLOPES_BASE , 1 , TRACKS_FOR_SLOPES_SPRITE_COUNT , " Sloped rail track " } ,
/* 0x10 */ { A5BLOCK_ALLOW_OFFSET , SPR_AIRPORTX_BASE , 1 , AIRPORTX_SPRITE_COUNT , " Airport graphics " } ,
/* 0x11 */ { A5BLOCK_ALLOW_OFFSET , SPR_ROADSTOP_BASE , 1 , ROADSTOP_SPRITE_COUNT , " Road stop graphics " } ,
/* 0x12 */ { A5BLOCK_ALLOW_OFFSET , SPR_AQUEDUCT_BASE , 1 , AQUEDUCT_SPRITE_COUNT , " Aqueduct graphics " } ,
/* 0x13 */ { A5BLOCK_ALLOW_OFFSET , SPR_AUTORAIL_BASE , 1 , AUTORAIL_SPRITE_COUNT , " Autorail graphics " } ,
2022-09-26 17:09:30 +02:00
/* 0x14 */ { A5BLOCK_INVALID , 0 , 1 , 0 , " Flag graphics " } , // deprecated, no longer used.
2011-10-05 19:07:43 +02:00
/* 0x15 */ { A5BLOCK_ALLOW_OFFSET , SPR_OPENTTD_BASE , 1 , OPENTTD_SPRITE_COUNT , " OpenTTD GUI graphics " } ,
2010-08-19 21:23:38 +02:00
/* 0x16 */ { A5BLOCK_ALLOW_OFFSET , SPR_AIRPORT_PREVIEW_BASE , 1 , SPR_AIRPORT_PREVIEW_COUNT , " Airport preview graphics " } ,
2012-02-15 22:23:48 +01:00
/* 0x17 */ { A5BLOCK_ALLOW_OFFSET , SPR_RAILTYPE_TUNNEL_BASE , 1 , RAILTYPE_TUNNEL_BASE_COUNT , " Railtype tunnel base " } ,
2014-09-21 09:57:45 +02:00
/* 0x18 */ { A5BLOCK_ALLOW_OFFSET , SPR_PALETTE_BASE , 1 , PALETTE_SPRITE_COUNT , " Palette " } ,
2010-08-19 21:23:38 +02:00
} ;
2004-11-12 16:15:56 +01:00
/* Action 0x05 */
2010-01-22 14:02:15 +01:00
static void GraphicsNew ( ByteReader * buf )
2004-08-09 19:04:08 +02:00
{
/* <05> <graphics-type> <num-sprites> <other data...>
*
* B graphics - type What set of graphics the sprites define .
2005-08-16 00:48:33 +02:00
* E num - sprites How many sprites are in this set ?
2004-08-09 19:04:08 +02:00
* V other data Graphics type specific data . Currently unused . */
2004-11-12 16:15:56 +01:00
2010-01-23 14:17:30 +01:00
uint8 type = buf - > ReadByte ( ) ;
2010-03-10 08:31:08 +01:00
uint16 num = buf - > ReadExtendedByte ( ) ;
uint16 offset = HasBit ( type , 7 ) ? buf - > ReadExtendedByte ( ) : 0 ;
2007-11-19 22:32:20 +01:00
ClrBit ( type , 7 ) ; // Clear the high bit as that only indicates whether there is an offset.
2004-11-12 16:15:56 +01:00
2017-01-07 22:36:25 +01:00
if ( ( type = = 0x0D ) & & ( num = = 10 ) & & HasBit ( _cur . grfconfig - > flags , GCF_SYSTEM ) ) {
2010-08-19 21:23:38 +02:00
/* Special not-TTDP-compatible case used in openttd.grf
2008-01-10 19:02:15 +01:00
* Missing shore sprites and initialisation of SPR_SHORE_BASE */
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " GraphicsNew: Loading 10 missing shore sprites from extra grf. " ) ;
2021-04-14 17:20:39 +02:00
LoadNextSprite ( SPR_SHORE_BASE + 0 , * _cur . file , _cur . nfo_line + + ) ; // SLOPE_STEEP_S
LoadNextSprite ( SPR_SHORE_BASE + 5 , * _cur . file , _cur . nfo_line + + ) ; // SLOPE_STEEP_W
LoadNextSprite ( SPR_SHORE_BASE + 7 , * _cur . file , _cur . nfo_line + + ) ; // SLOPE_WSE
LoadNextSprite ( SPR_SHORE_BASE + 10 , * _cur . file , _cur . nfo_line + + ) ; // SLOPE_STEEP_N
LoadNextSprite ( SPR_SHORE_BASE + 11 , * _cur . file , _cur . nfo_line + + ) ; // SLOPE_NWS
LoadNextSprite ( SPR_SHORE_BASE + 13 , * _cur . file , _cur . nfo_line + + ) ; // SLOPE_ENW
LoadNextSprite ( SPR_SHORE_BASE + 14 , * _cur . file , _cur . nfo_line + + ) ; // SLOPE_SEN
LoadNextSprite ( SPR_SHORE_BASE + 15 , * _cur . file , _cur . nfo_line + + ) ; // SLOPE_STEEP_E
LoadNextSprite ( SPR_SHORE_BASE + 16 , * _cur . file , _cur . nfo_line + + ) ; // SLOPE_EW
LoadNextSprite ( SPR_SHORE_BASE + 17 , * _cur . file , _cur . nfo_line + + ) ; // SLOPE_NS
2008-01-24 15:49:40 +01:00
if ( _loaded_newgrf_features . shore = = SHORE_REPLACE_NONE ) _loaded_newgrf_features . shore = SHORE_REPLACE_ONLY_NEW ;
2008-01-10 19:02:15 +01:00
return ;
}
2007-11-15 08:42:25 +01:00
2008-01-10 19:02:15 +01:00
/* Supported type? */
2010-08-19 21:23:38 +02:00
if ( ( type > = lengthof ( _action5_types ) ) | | ( _action5_types [ type ] . block_type = = A5BLOCK_INVALID ) ) {
2023-04-20 17:00:46 +02:00
GrfMsg ( 2 , " GraphicsNew: Custom graphics (type 0x{:02X}) sprite block of length {} (unimplemented, ignoring) " , type , num ) ;
2011-08-21 14:05:47 +02:00
_cur . skip_sprites = num ;
2008-01-10 19:02:15 +01:00
return ;
}
2007-11-15 08:42:25 +01:00
2010-08-19 21:23:38 +02:00
const Action5Type * action5_type = & _action5_types [ type ] ;
2007-11-15 08:42:25 +01:00
2011-10-05 19:39:12 +02:00
/* Contrary to TTDP we allow always to specify too few sprites as we allow always an offset,
* except for the long version of the shore type :
* Ignore offset if not allowed */
if ( ( action5_type - > block_type ! = A5BLOCK_ALLOW_OFFSET ) & & ( offset ! = 0 ) ) {
2023-04-20 17:00:46 +02:00
GrfMsg ( 1 , " GraphicsNew: {} (type 0x{:02X}) do not allow an <offset> field. Ignoring offset. " , action5_type - > name , type ) ;
2011-10-05 19:39:12 +02:00
offset = 0 ;
}
/* Ignore action5 if too few sprites are specified. (for TTDP compatibility)
* This does not make sense , if < offset > is allowed */
if ( ( action5_type - > block_type = = A5BLOCK_FIXED ) & & ( num < action5_type - > min_sprites ) ) {
2023-04-20 17:00:46 +02:00
GrfMsg ( 1 , " GraphicsNew: {} (type 0x{:02X}) count must be at least {}. Only {} were specified. Skipping. " , action5_type - > name , type , action5_type - > min_sprites , num ) ;
2011-10-05 19:39:12 +02:00
_cur . skip_sprites = num ;
return ;
}
2006-04-23 17:57:34 +02:00
2019-09-29 22:27:32 +02:00
/* Load at most max_sprites sprites. Skip remaining sprites. (for compatibility with TTDP and future extensions) */
2008-01-10 19:02:15 +01:00
uint16 skip_num = SanitizeSpriteOffset ( num , offset , action5_type - > max_sprites , action5_type - > name ) ;
SpriteID replace = action5_type - > sprite_base + offset ;
/* Load <num> sprites starting from <replace>, then skip <skip_num> sprites. */
2023-04-20 17:00:46 +02:00
GrfMsg ( 2 , " GraphicsNew: Replacing sprites {} to {} of {} (type 0x{:02X}) at SpriteID 0x{:04X} " , offset , offset + num - 1 , action5_type - > name , type , replace ) ;
2008-01-10 19:02:15 +01:00
2019-04-06 08:46:15 +02:00
if ( type = = 0x0D ) _loaded_newgrf_features . shore = SHORE_REPLACE_ACTION_5 ;
if ( type = = 0x0B ) {
static const SpriteID depot_with_track_offset = SPR_TRAMWAY_DEPOT_WITH_TRACK - SPR_TRAMWAY_BASE ;
static const SpriteID depot_no_track_offset = SPR_TRAMWAY_DEPOT_NO_TRACK - SPR_TRAMWAY_BASE ;
if ( offset < = depot_with_track_offset & & offset + num > depot_with_track_offset ) _loaded_newgrf_features . tram = TRAMWAY_REPLACE_DEPOT_WITH_TRACK ;
if ( offset < = depot_no_track_offset & & offset + num > depot_no_track_offset ) _loaded_newgrf_features . tram = TRAMWAY_REPLACE_DEPOT_NO_TRACK ;
}
2022-12-26 21:06:21 +01:00
/* If the baseset or grf only provides sprites for flat tiles (pre #10282), duplicate those for use on slopes. */
bool dup_oneway_sprites = ( ( type = = 0x09 ) & & ( offset + num < = SPR_ONEWAY_SLOPE_N_OFFSET ) ) ;
2006-04-23 17:57:34 +02:00
for ( ; num > 0 ; num - - ) {
2011-08-21 14:05:47 +02:00
_cur . nfo_line + + ;
2022-12-26 21:06:21 +01:00
int load_index = ( replace = = 0 ? _cur . spriteid + + : replace + + ) ;
LoadNextSprite ( load_index , * _cur . file , _cur . nfo_line ) ;
if ( dup_oneway_sprites ) {
DupSprite ( load_index , load_index + SPR_ONEWAY_SLOPE_N_OFFSET ) ;
DupSprite ( load_index , load_index + SPR_ONEWAY_SLOPE_S_OFFSET ) ;
}
2006-04-23 17:57:34 +02:00
}
2007-11-15 08:42:25 +01:00
2011-08-21 14:05:47 +02:00
_cur . skip_sprites = skip_num ;
2004-08-09 19:04:08 +02:00
}
2007-04-26 08:49:32 +02:00
/* Action 0x05 (SKIP) */
2010-01-22 14:02:15 +01:00
static void SkipAct5 ( ByteReader * buf )
2007-04-26 08:49:32 +02:00
{
/* Ignore type byte */
2010-01-23 14:17:30 +01:00
buf - > ReadByte ( ) ;
2007-04-26 08:49:32 +02:00
/* Skip the sprites of this action */
2011-08-21 14:05:47 +02:00
_cur . skip_sprites = buf - > ReadExtendedByte ( ) ;
2007-04-26 08:49:32 +02:00
2023-04-18 21:24:42 +02:00
GrfMsg ( 3 , " SkipAct5: Skipping {} sprites " , _cur . skip_sprites ) ;
2007-04-26 08:49:32 +02:00
}
2008-02-12 16:43:10 +01:00
/**
* Reads a variable common to VarAction2 and Action7 / 9 / D .
*
* Returns VarAction2 variable ' param ' resp . Action7 / 9 / D variable ' 0x80 + param ' .
* If a variable is not accessible from all four actions , it is handled in the action specific functions .
*
* @ param param variable number ( as for VarAction2 , for Action7 / 9 / D you have to subtract 0x80 first ) .
* @ param value returns the value of the variable .
2011-11-08 18:28:52 +01:00
* @ param grffile NewGRF querying the variable
2008-02-12 16:43:10 +01:00
* @ return true iff the variable is known and the value is returned in ' value ' .
*/
2011-11-08 18:28:52 +01:00
bool GetGlobalVariable ( byte param , uint32 * value , const GRFFile * grffile )
2006-05-10 23:34:57 +02:00
{
switch ( param ) {
2008-02-12 16:43:10 +01:00
case 0x00 : // current date
2023-04-24 17:56:01 +02:00
* value = std : : max ( TimerGameCalendar : : date - DAYS_TILL_ORIGINAL_BASE_YEAR , 0 ) ;
2008-02-12 16:43:10 +01:00
return true ;
case 0x01 : // current year
2023-04-24 17:56:01 +02:00
* value = Clamp ( TimerGameCalendar : : year , ORIGINAL_BASE_YEAR , ORIGINAL_MAX_YEAR ) - ORIGINAL_BASE_YEAR ;
2008-02-12 16:43:10 +01:00
return true ;
2008-06-20 23:14:10 +02:00
case 0x02 : { // detailed date information: month of year (bit 0-7), day of month (bit 8-12), leap year (bit 15), day of year (bit 16-24)
2023-05-04 15:14:12 +02:00
TimerGameCalendar : : YearMonthDay ymd ;
TimerGameCalendar : : ConvertDateToYMD ( TimerGameCalendar : : date , & ymd ) ;
TimerGameCalendar : : Date start_of_year = TimerGameCalendar : : ConvertYMDToDate ( ymd . year , 0 , 1 ) ;
* value = ymd . month | ( ymd . day - 1 ) < < 8 | ( TimerGameCalendar : : IsLeapYear ( ymd . year ) ? 1 < < 15 : 0 ) | ( TimerGameCalendar : : date - start_of_year ) < < 16 ;
2008-02-12 16:43:10 +01:00
return true ;
2008-06-20 23:14:10 +02:00
}
2008-02-12 16:43:10 +01:00
case 0x03 : // current climate, 0=temp, 1=arctic, 2=trop, 3=toyland
2008-05-29 17:13:28 +02:00
* value = _settings_game . game_creation . landscape ;
2008-02-12 16:43:10 +01:00
return true ;
case 0x06 : // road traffic side, bit 4 clear=left, set=right
2008-05-29 17:13:28 +02:00
* value = _settings_game . vehicle . road_side < < 4 ;
2008-02-12 16:43:10 +01:00
return true ;
case 0x09 : // date fraction
2023-04-24 17:56:01 +02:00
* value = TimerGameCalendar : : date_fract * 885 ;
2008-02-12 16:43:10 +01:00
return true ;
case 0x0A : // animation counter
2023-04-24 18:55:40 +02:00
* value = GB ( TimerGameTick : : counter , 0 , 16 ) ;
2008-02-12 16:43:10 +01:00
return true ;
case 0x0B : { // TTDPatch version
uint major = 2 ;
uint minor = 6 ;
uint revision = 1 ; // special case: 2.0.1 is 2.0.10
uint build = 1382 ;
* value = ( major < < 24 ) | ( minor < < 20 ) | ( revision < < 16 ) | build ;
return true ;
}
case 0x0D : // TTD Version, 00=DOS, 01=Windows
2011-08-21 14:05:47 +02:00
* value = _cur . grfconfig - > palette & GRFP_USE_MASK ;
2008-02-12 16:43:10 +01:00
return true ;
case 0x0E : // Y-offset for train sprites
2011-08-21 14:05:47 +02:00
* value = _cur . grffile - > traininfo_vehicle_pitch ;
2008-02-12 16:43:10 +01:00
return true ;
case 0x0F : // Rail track type cost factors
* value = 0 ;
2008-08-25 01:10:23 +02:00
SB ( * value , 0 , 8 , GetRailTypeInfo ( RAILTYPE_RAIL ) - > cost_multiplier ) ; // normal rail
2008-05-29 17:13:28 +02:00
if ( _settings_game . vehicle . disable_elrails ) {
2008-02-12 16:43:10 +01:00
/* skip elrail multiplier - disabled */
2008-08-25 01:10:23 +02:00
SB ( * value , 8 , 8 , GetRailTypeInfo ( RAILTYPE_MONO ) - > cost_multiplier ) ; // monorail
2008-02-12 16:43:10 +01:00
} else {
2008-08-25 01:10:23 +02:00
SB ( * value , 8 , 8 , GetRailTypeInfo ( RAILTYPE_ELECTRIC ) - > cost_multiplier ) ; // electified railway
2008-02-12 16:43:10 +01:00
/* Skip monorail multiplier - no space in result */
}
2008-08-25 01:10:23 +02:00
SB ( * value , 16 , 8 , GetRailTypeInfo ( RAILTYPE_MAGLEV ) - > cost_multiplier ) ; // maglev
2008-02-12 16:43:10 +01:00
return true ;
case 0x11 : // current rail tool type
2011-07-22 20:59:10 +02:00
* value = 0 ; // constant fake value to avoid desync
2008-02-12 16:43:10 +01:00
return true ;
case 0x12 : // Game mode
* value = _game_mode ;
return true ;
/* case 0x13: // Tile refresh offset to left not implemented */
/* case 0x14: // Tile refresh offset to right not implemented */
/* case 0x15: // Tile refresh offset upwards not implemented */
/* case 0x16: // Tile refresh offset downwards not implemented */
/* case 0x17: // temperate snow line not implemented */
case 0x1A : // Always -1
* value = UINT_MAX ;
return true ;
case 0x1B : // Display options
2011-07-22 20:59:10 +02:00
* value = 0x3F ; // constant fake value to avoid desync
2008-02-12 16:43:10 +01:00
return true ;
case 0x1D : // TTD Platform, 00=TTDPatch, 01=OpenTTD
* value = 1 ;
return true ;
case 0x1E : // Miscellaneous GRF features
* value = _misc_grf_features ;
2009-07-18 00:00:13 +02:00
/* Add the local flags */
assert ( ! HasBit ( * value , GMB_TRAIN_WIDTH_32_PIXELS ) ) ;
2011-08-21 14:05:47 +02:00
if ( _cur . grffile - > traininfo_vehicle_width = = VEHICLEINFO_FULL_VEHICLE_WIDTH ) SetBit ( * value , GMB_TRAIN_WIDTH_32_PIXELS ) ;
2008-02-12 16:43:10 +01:00
return true ;
2011-07-22 20:59:10 +02:00
/* case 0x1F: // locale dependent settings not implemented to avoid desync */
2006-12-18 16:34:12 +01:00
2011-11-08 18:28:52 +01:00
case 0x20 : { // snow line height
byte snowline = GetSnowLine ( ) ;
2021-03-24 09:42:54 +01:00
if ( _settings_game . game_creation . landscape = = LT_ARCTIC & & snowline < = _settings_game . construction . map_height_limit ) {
2011-11-08 18:28:52 +01:00
* value = Clamp ( snowline * ( grffile - > grf_version > = 8 ? 1 : TILE_HEIGHT ) , 0 , 0xFE ) ;
} else {
/* No snow */
* value = 0xFF ;
}
2008-02-12 16:43:10 +01:00
return true ;
2011-11-08 18:28:52 +01:00
}
2006-05-10 23:34:57 +02:00
2008-05-04 23:53:36 +02:00
case 0x21 : // OpenTTD version
2008-02-12 16:43:10 +01:00
* value = _openttd_newgrf_version ;
return true ;
2008-03-27 18:06:35 +01:00
case 0x22 : // difficulty level
2012-12-05 20:36:41 +01:00
* value = SP_CUSTOM ;
2008-03-27 18:06:35 +01:00
return true ;
2008-06-04 02:17:48 +02:00
case 0x23 : // long format date
2023-04-24 17:56:01 +02:00
* value = TimerGameCalendar : : date ;
2008-06-04 02:17:48 +02:00
return true ;
case 0x24 : // long format year
2023-04-24 17:56:01 +02:00
* value = TimerGameCalendar : : year ;
2008-06-04 02:17:48 +02:00
return true ;
2008-02-12 16:43:10 +01:00
default : return false ;
}
}
static uint32 GetParamVal ( byte param , uint32 * cond_val )
{
/* First handle variable common with VarAction2 */
uint32 value ;
2011-11-08 18:28:52 +01:00
if ( GetGlobalVariable ( param - 0x80 , & value , _cur . grffile ) ) return value ;
2008-02-12 16:43:10 +01:00
/* Non-common variable */
switch ( param ) {
2007-06-07 09:20:44 +02:00
case 0x84 : { // GRF loading stage
uint32 res = 0 ;
2011-08-21 14:05:47 +02:00
if ( _cur . stage > GLS_INIT ) SetBit ( res , 0 ) ;
if ( _cur . stage = = GLS_RESERVE ) SetBit ( res , 8 ) ;
if ( _cur . stage = = GLS_ACTIVATION ) SetBit ( res , 9 ) ;
2007-06-07 09:20:44 +02:00
return res ;
}
2006-05-10 23:34:57 +02:00
2007-03-21 04:06:21 +01:00
case 0x85 : // TTDPatch flags, only for bit tests
2019-04-10 23:07:06 +02:00
if ( cond_val = = nullptr ) {
2006-05-11 11:54:11 +02:00
/* Supported in Action 0x07 and 0x09, not 0x0D */
return 0 ;
} else {
2018-08-14 16:41:50 +02:00
uint32 index = * cond_val / 0x20 ;
uint32 param_val = index < lengthof ( _ttdpatch_flags ) ? _ttdpatch_flags [ index ] : 0 ;
2006-05-11 11:54:11 +02:00
* cond_val % = 0x20 ;
return param_val ;
}
2006-05-10 23:34:57 +02:00
2007-03-21 04:06:21 +01:00
case 0x88 : // GRF ID check
2006-05-10 23:34:57 +02:00
return 0 ;
2013-08-09 20:43:44 +02:00
/* case 0x99: Global ID offset not implemented */
2007-10-21 16:59:05 +02:00
2006-05-10 23:34:57 +02:00
default :
/* GRF Parameter */
2011-08-21 14:05:47 +02:00
if ( param < 0x80 ) return _cur . grffile - > GetParam ( param ) ;
2006-05-10 23:34:57 +02:00
/* In-game variable. */
2023-04-20 17:00:46 +02:00
GrfMsg ( 1 , " Unsupported in-game variable 0x{:02X} " , param ) ;
2007-01-10 19:56:51 +01:00
return UINT_MAX ;
2006-05-10 23:34:57 +02:00
}
}
2006-05-23 22:59:01 +02:00
/* Action 0x06 */
2010-01-22 14:02:15 +01:00
static void CfgApply ( ByteReader * buf )
2006-05-23 22:59:01 +02:00
{
/* <06> <param-num> <param-size> <offset> ... <FF>
*
* B param - num Number of parameter to substitute ( First = " zero " )
* Ignored if that parameter was not specified in newgrf . cfg
* B param - size How many bytes to replace . If larger than 4 , the
* bytes of the following parameter are used . In that
* case , nothing is applied unless * all * parameters
* were specified .
* B offset Offset into data from beginning of next sprite
* to place where parameter is to be stored . */
/* Preload the next sprite */
2021-04-14 17:41:45 +02:00
SpriteFile & file = * _cur . file ;
size_t pos = file . GetPos ( ) ;
uint32 num = file . GetContainerVersion ( ) > = 2 ? file . ReadDword ( ) : file . ReadWord ( ) ;
uint8 type = file . ReadByte ( ) ;
2006-05-23 22:59:01 +02:00
/* Check if the sprite is a pseudo sprite. We can't operate on real sprites. */
if ( type ! = 0xFF ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " CfgApply: Ignoring (next sprite is real, unsupported) " ) ;
2023-04-26 07:45:11 +02:00
/* Reset the file position to the start of the next sprite */
file . SeekTo ( pos , SEEK_SET ) ;
2006-05-23 22:59:01 +02:00
return ;
}
2023-04-26 07:45:11 +02:00
/* Get (or create) the override for the next sprite. */
2011-08-21 14:05:47 +02:00
GRFLocation location ( _cur . grfconfig - > ident . grfid , _cur . nfo_line + 1 ) ;
2023-04-26 07:45:11 +02:00
std : : vector < byte > & preload_sprite = _grf_line_to_action6_sprite_override [ location ] ;
/* Load new sprite data if it hasn't already been loaded. */
if ( preload_sprite . empty ( ) ) {
preload_sprite . resize ( num ) ;
file . ReadBlock ( preload_sprite . data ( ) , num ) ;
2008-08-19 11:08:45 +02:00
}
2023-04-26 07:45:11 +02:00
/* Reset the file position to the start of the next sprite */
file . SeekTo ( pos , SEEK_SET ) ;
2006-05-23 22:59:01 +02:00
2023-04-26 07:45:11 +02:00
/* Now perform the Action 0x06 on our data. */
2006-05-23 22:59:01 +02:00
for ( ; ; ) {
uint i ;
uint param_num ;
uint param_size ;
uint offset ;
bool add_value ;
/* Read the parameter to apply. 0xFF indicates no more data to change. */
2010-01-23 14:17:30 +01:00
param_num = buf - > ReadByte ( ) ;
2006-05-23 22:59:01 +02:00
if ( param_num = = 0xFF ) break ;
/* Get the size of the parameter to use. If the size covers multiple
* double words , sequential parameter values are used . */
2010-01-23 14:17:30 +01:00
param_size = buf - > ReadByte ( ) ;
2006-05-23 22:59:01 +02:00
/* Bit 7 of param_size indicates we should add to the original value
* instead of replacing it . */
2007-11-19 22:02:30 +01:00
add_value = HasBit ( param_size , 7 ) ;
2006-05-23 22:59:01 +02:00
param_size = GB ( param_size , 0 , 7 ) ;
/* Where to apply the data to within the pseudo sprite data. */
2010-03-10 08:31:08 +01:00
offset = buf - > ReadExtendedByte ( ) ;
2006-05-23 22:59:01 +02:00
/* If the parameter is a GRF parameter (not an internal variable) check
* if it ( and all further sequential parameters ) has been defined . */
2011-08-21 14:05:47 +02:00
if ( param_num < 0x80 & & ( param_num + ( param_size - 1 ) / 4 ) > = _cur . grffile - > param_end ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " CfgApply: Ignoring (param {} not set) " , ( param_num + ( param_size - 1 ) / 4 ) ) ;
2006-05-23 22:59:01 +02:00
break ;
}
2023-04-20 17:00:46 +02:00
GrfMsg ( 8 , " CfgApply: Applying {} bytes from parameter 0x{:02X} at offset 0x{:04X} " , param_size , param_num , offset ) ;
2006-05-23 22:59:01 +02:00
2007-05-20 00:37:24 +02:00
bool carry = false ;
2008-12-13 20:43:57 +01:00
for ( i = 0 ; i < param_size & & offset + i < num ; i + + ) {
2019-04-10 23:07:06 +02:00
uint32 value = GetParamVal ( param_num + i / 4 , nullptr ) ;
2007-05-20 00:37:24 +02:00
/* Reset carry flag for each iteration of the variable (only really
* matters if param_size is greater than 4 ) */
2011-02-04 09:29:14 +01:00
if ( i % 4 = = 0 ) carry = false ;
2006-05-23 22:59:01 +02:00
if ( add_value ) {
2008-08-19 11:08:45 +02:00
uint new_value = preload_sprite [ offset + i ] + GB ( value , ( i % 4 ) * 8 , 8 ) + ( carry ? 1 : 0 ) ;
preload_sprite [ offset + i ] = GB ( new_value , 0 , 8 ) ;
2007-05-20 00:37:24 +02:00
/* Check if the addition overflowed */
carry = new_value > = 256 ;
2006-05-23 22:59:01 +02:00
} else {
2008-08-19 11:08:45 +02:00
preload_sprite [ offset + i ] = GB ( value , ( i % 4 ) * 8 , 8 ) ;
2006-05-23 22:59:01 +02:00
}
}
}
}
2008-06-19 12:20:45 +02:00
/**
* Disable a static NewGRF when it is influencing another ( non - static )
* NewGRF as this could cause desyncs .
*
* We could just tell the NewGRF querying that the file doesn ' t exist ,
* but that might give unwanted results . Disabling the NewGRF gives the
* best result as no NewGRF author can complain about that .
2011-09-11 13:14:22 +02:00
* @ param c The NewGRF to disable .
2008-06-19 12:20:45 +02:00
*/
static void DisableStaticNewGRFInfluencingNonStaticNewGRFs ( GRFConfig * c )
{
2011-05-11 22:22:04 +02:00
GRFError * error = DisableGrf ( STR_NEWGRF_ERROR_STATIC_GRF_CAUSES_DESYNC , c ) ;
2020-05-17 23:31:54 +02:00
error - > data = _cur . grfconfig - > GetName ( ) ;
2008-06-19 12:20:45 +02:00
}
2009-03-15 01:32:18 +01:00
/* Action 0x07
* Action 0x09 */
2010-01-22 14:02:15 +01:00
static void SkipIf ( ByteReader * buf )
2004-08-09 19:04:08 +02:00
{
/* <07/09> <param-num> <param-size> <condition-type> <value> <num-sprites>
*
* B param - num
* B param - size
* B condition - type
* V value
* B num - sprites */
2005-08-13 18:23:07 +02:00
uint32 cond_val = 0 ;
2006-05-10 23:34:57 +02:00
uint32 mask = 0 ;
2004-11-12 19:43:12 +01:00
bool result ;
2004-11-12 16:36:49 +01:00
2010-01-23 14:17:30 +01:00
uint8 param = buf - > ReadByte ( ) ;
uint8 paramsize = buf - > ReadByte ( ) ;
uint8 condtype = buf - > ReadByte ( ) ;
2004-11-12 16:36:49 +01:00
2004-11-12 18:27:30 +01:00
if ( condtype < 2 ) {
2005-04-01 12:43:20 +02:00
/* Always 1 for bit tests, the given value should be ignored. */
2004-11-12 18:27:30 +01:00
paramsize = 1 ;
}
2005-04-01 12:43:20 +02:00
switch ( paramsize ) {
2010-01-23 14:17:30 +01:00
case 8 : cond_val = buf - > ReadDWord ( ) ; mask = buf - > ReadDWord ( ) ; break ;
case 4 : cond_val = buf - > ReadDWord ( ) ; mask = 0xFFFFFFFF ; break ;
case 2 : cond_val = buf - > ReadWord ( ) ; mask = 0x0000FFFF ; break ;
case 1 : cond_val = buf - > ReadByte ( ) ; mask = 0x000000FF ; break ;
2005-04-01 12:43:20 +02:00
default : break ;
}
2004-11-12 18:27:30 +01:00
2011-08-21 14:05:47 +02:00
if ( param < 0x80 & & _cur . grffile - > param_end < = param ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 7 , " SkipIf: Param {} undefined, skipping test " , param ) ;
2006-05-10 23:34:57 +02:00
return ;
}
2005-08-13 18:23:07 +02:00
2023-04-20 17:00:46 +02:00
GrfMsg ( 7 , " SkipIf: Test condtype {}, param 0x{:02X}, condval 0x{:08X} " , condtype , param , cond_val ) ;
2006-05-10 23:34:57 +02:00
2020-12-15 22:42:03 +01:00
/* condtypes that do not use 'param' are always valid.
* condtypes that use ' param ' are either not valid for param 0x88 , or they are only valid for param 0x88 .
2007-10-30 18:24:04 +01:00
*/
2020-12-15 22:42:03 +01:00
if ( condtype > = 0x0B ) {
/* Tests that ignore 'param' */
switch ( condtype ) {
2023-05-04 12:29:21 +02:00
case 0x0B : result = ! IsValidCargoID ( GetCargoIDByLabel ( BSWAP32 ( cond_val ) ) ) ;
2020-12-15 22:42:03 +01:00
break ;
2023-05-04 12:29:21 +02:00
case 0x0C : result = IsValidCargoID ( GetCargoIDByLabel ( BSWAP32 ( cond_val ) ) ) ;
2020-12-15 22:42:03 +01:00
break ;
case 0x0D : result = GetRailTypeByLabel ( BSWAP32 ( cond_val ) ) = = INVALID_RAILTYPE ;
break ;
case 0x0E : result = GetRailTypeByLabel ( BSWAP32 ( cond_val ) ) ! = INVALID_RAILTYPE ;
break ;
case 0x0F : {
RoadType rt = GetRoadTypeByLabel ( BSWAP32 ( cond_val ) ) ;
result = rt = = INVALID_ROADTYPE | | ! RoadTypeIsRoad ( rt ) ;
break ;
}
case 0x10 : {
RoadType rt = GetRoadTypeByLabel ( BSWAP32 ( cond_val ) ) ;
result = rt ! = INVALID_ROADTYPE & & RoadTypeIsRoad ( rt ) ;
break ;
}
case 0x11 : {
RoadType rt = GetRoadTypeByLabel ( BSWAP32 ( cond_val ) ) ;
result = rt = = INVALID_ROADTYPE | | ! RoadTypeIsTram ( rt ) ;
break ;
}
case 0x12 : {
RoadType rt = GetRoadTypeByLabel ( BSWAP32 ( cond_val ) ) ;
result = rt ! = INVALID_ROADTYPE & & RoadTypeIsTram ( rt ) ;
break ;
}
2023-04-18 21:24:42 +02:00
default : GrfMsg ( 1 , " SkipIf: Unsupported condition type {:02X}. Ignoring " , condtype ) ; return ;
2020-12-15 22:42:03 +01:00
}
} else if ( param = = 0x88 ) {
2007-01-01 02:40:56 +01:00
/* GRF ID checks */
2006-12-04 14:45:20 +01:00
2009-01-16 20:53:52 +01:00
GRFConfig * c = GetGRFConfig ( cond_val , mask ) ;
2008-06-19 12:20:45 +02:00
2019-04-10 23:07:06 +02:00
if ( c ! = nullptr & & HasBit ( c - > flags , GCF_STATIC ) & & ! HasBit ( _cur . grfconfig - > flags , GCF_STATIC ) & & _networking ) {
2008-06-19 12:20:45 +02:00
DisableStaticNewGRFInfluencingNonStaticNewGRFs ( c ) ;
2019-04-10 23:07:06 +02:00
c = nullptr ;
2008-06-19 12:20:45 +02:00
}
2006-12-04 14:45:20 +01:00
2019-04-10 23:07:06 +02:00
if ( condtype ! = 10 & & c = = nullptr ) {
2023-04-20 17:00:46 +02:00
GrfMsg ( 7 , " SkipIf: GRFID 0x{:08X} unknown, skipping test " , BSWAP32 ( cond_val ) ) ;
2007-01-01 02:40:56 +01:00
return ;
2006-12-04 14:45:20 +01:00
}
2007-01-01 02:40:56 +01:00
switch ( condtype ) {
2007-10-30 18:24:04 +01:00
/* Tests 0x06 to 0x0A are only for param 0x88, GRFID checks */
case 0x06 : // Is GRFID active?
2007-03-06 20:33:28 +01:00
result = c - > status = = GCS_ACTIVATED ;
2007-01-01 02:40:56 +01:00
break ;
2006-12-04 14:45:20 +01:00
2007-10-30 18:24:04 +01:00
case 0x07 : // Is GRFID non-active?
2007-03-06 20:33:28 +01:00
result = c - > status ! = GCS_ACTIVATED ;
2007-01-01 02:40:56 +01:00
break ;
2007-10-30 18:24:04 +01:00
case 0x08 : // GRFID is not but will be active?
2007-03-06 20:33:28 +01:00
result = c - > status = = GCS_INITIALISED ;
2007-01-01 02:40:56 +01:00
break ;
2007-10-30 18:24:04 +01:00
case 0x09 : // GRFID is or will be active?
2007-03-06 20:33:28 +01:00
result = c - > status = = GCS_ACTIVATED | | c - > status = = GCS_INITIALISED ;
2007-01-01 02:40:56 +01:00
break ;
2007-10-30 18:24:04 +01:00
case 0x0A : // GRFID is not nor will be active
2007-01-01 02:40:56 +01:00
/* This is the only condtype that doesn't get ignored if the GRFID is not found */
2019-04-10 23:07:06 +02:00
result = c = = nullptr | | c - > status = = GCS_DISABLED | | c - > status = = GCS_NOT_FOUND ;
2007-01-01 02:40:56 +01:00
break ;
2023-04-18 21:24:42 +02:00
default : GrfMsg ( 1 , " SkipIf: Unsupported GRF condition type {:02X}. Ignoring " , condtype ) ; return ;
2006-12-04 14:45:20 +01:00
}
2007-01-01 02:40:56 +01:00
} else {
2020-12-15 22:42:03 +01:00
/* Tests that use 'param' and are not GRF ID checks. */
uint32 param_val = GetParamVal ( param , & cond_val ) ; // cond_val is modified for param == 0x85
2007-01-01 02:40:56 +01:00
switch ( condtype ) {
2007-10-30 18:24:04 +01:00
case 0x00 : result = ! ! ( param_val & ( 1 < < cond_val ) ) ;
2007-01-01 02:40:56 +01:00
break ;
2007-10-30 18:24:04 +01:00
case 0x01 : result = ! ( param_val & ( 1 < < cond_val ) ) ;
2007-01-01 02:40:56 +01:00
break ;
2007-10-30 18:24:04 +01:00
case 0x02 : result = ( param_val & mask ) = = cond_val ;
2007-01-01 02:40:56 +01:00
break ;
2007-10-30 18:24:04 +01:00
case 0x03 : result = ( param_val & mask ) ! = cond_val ;
2007-01-01 02:40:56 +01:00
break ;
2007-10-30 18:24:04 +01:00
case 0x04 : result = ( param_val & mask ) < cond_val ;
2007-01-01 02:40:56 +01:00
break ;
2007-10-30 18:24:04 +01:00
case 0x05 : result = ( param_val & mask ) > cond_val ;
2007-01-01 02:40:56 +01:00
break ;
2023-04-18 21:24:42 +02:00
default : GrfMsg ( 1 , " SkipIf: Unsupported condition type {:02X}. Ignoring " , condtype ) ; return ;
2007-01-01 02:40:56 +01:00
}
2004-11-12 16:36:49 +01:00
}
2004-08-09 19:04:08 +02:00
2005-03-30 14:30:24 +02:00
if ( ! result ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " SkipIf: Not skipping sprites, test was false " ) ;
2004-11-12 16:36:49 +01:00
return ;
2004-11-12 18:27:30 +01:00
}
2010-01-23 14:17:30 +01:00
uint8 numsprites = buf - > ReadByte ( ) ;
2006-04-15 23:27:59 +02:00
/* numsprites can be a GOTO label if it has been defined in the GRF
* file . The jump will always be the first matching label that follows
* the current nfo_line . If no matching label is found , the first matching
* label in the file is used . */
2022-12-25 23:32:22 +01:00
const GRFLabel * choice = nullptr ;
for ( const auto & label : _cur . grffile - > labels ) {
if ( label . label ! = numsprites ) continue ;
2006-04-15 23:27:59 +02:00
/* Remember a goto before the current line */
2022-12-25 23:32:22 +01:00
if ( choice = = nullptr ) choice = & label ;
2006-04-15 23:27:59 +02:00
/* If we find a label here, this is definitely good */
2022-12-25 23:32:22 +01:00
if ( label . nfo_line > _cur . nfo_line ) {
choice = & label ;
2006-04-15 23:27:59 +02:00
break ;
}
}
2019-04-10 23:07:06 +02:00
if ( choice ! = nullptr ) {
2023-04-20 17:00:46 +02:00
GrfMsg ( 2 , " SkipIf: Jumping to label 0x{:X} at line {}, test was true " , choice - > label , choice - > nfo_line ) ;
2021-04-14 17:41:45 +02:00
_cur . file - > SeekTo ( choice - > pos , SEEK_SET ) ;
2011-08-21 14:05:47 +02:00
_cur . nfo_line = choice - > nfo_line ;
2006-04-15 23:27:59 +02:00
return ;
}
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " SkipIf: Skipping {} sprites, test was true " , numsprites ) ;
2011-08-21 14:05:47 +02:00
_cur . skip_sprites = numsprites ;
if ( _cur . skip_sprites = = 0 ) {
2004-11-12 16:36:49 +01:00
/* Zero means there are no sprites to skip, so
* we use - 1 to indicate that all further
* sprites should be skipped . */
2011-08-21 14:05:47 +02:00
_cur . skip_sprites = - 1 ;
2007-02-21 18:54:30 +01:00
/* If an action 8 hasn't been encountered yet, disable the grf. */
2011-08-21 14:05:47 +02:00
if ( _cur . grfconfig - > status ! = ( _cur . stage < GLS_RESERVE ? GCS_INITIALISED : GCS_ACTIVATED ) ) {
2011-05-11 22:22:04 +02:00
DisableGrf ( ) ;
2007-06-17 13:01:58 +02:00
}
2004-08-09 19:04:08 +02:00
}
}
2006-12-10 22:39:38 +01:00
2007-01-03 12:33:54 +01:00
/* Action 0x08 (GLS_FILESCAN) */
2010-01-22 14:02:15 +01:00
static void ScanInfo ( ByteReader * buf )
2006-12-04 09:30:04 +01:00
{
2011-01-15 22:13:47 +01:00
uint8 grf_version = buf - > ReadByte ( ) ;
uint32 grfid = buf - > ReadDWord ( ) ;
const char * name = buf - > ReadString ( ) ;
2006-12-04 09:30:04 +01:00
2011-08-21 14:05:47 +02:00
_cur . grfconfig - > ident . grfid = grfid ;
2006-12-10 22:39:38 +01:00
2011-11-08 18:30:19 +01:00
if ( grf_version < 2 | | grf_version > 8 ) {
2011-08-21 14:05:47 +02:00
SetBit ( _cur . grfconfig - > flags , GCF_INVALID ) ;
2021-06-12 09:10:17 +02:00
Debug ( grf , 0 , " {}: NewGRF \" {} \" (GRFID {:08X}) uses GRF version {}, which is incompatible with this version of OpenTTD. " , _cur . grfconfig - > filename , name , BSWAP32 ( grfid ) , grf_version ) ;
2011-01-15 22:13:47 +01:00
}
2006-12-12 20:38:41 +01:00
/* GRF IDs starting with 0xFF are reserved for internal TTDPatch use */
2017-01-07 22:20:02 +01:00
if ( GB ( grfid , 0 , 8 ) = = 0xFF ) SetBit ( _cur . grfconfig - > flags , GCF_SYSTEM ) ;
2006-12-12 20:38:41 +01:00
2020-05-17 23:31:47 +02:00
AddGRFTextToList ( _cur . grfconfig - > name , 0x7F , grfid , false , name ) ;
2006-12-10 22:39:38 +01:00
2010-01-22 14:02:15 +01:00
if ( buf - > HasData ( ) ) {
2010-01-23 14:17:30 +01:00
const char * info = buf - > ReadString ( ) ;
2020-05-17 23:31:47 +02:00
AddGRFTextToList ( _cur . grfconfig - > info , 0x7F , grfid , true , info ) ;
2006-12-10 22:39:38 +01:00
}
2006-12-04 09:30:04 +01:00
2006-12-12 20:38:41 +01:00
/* GLS_INFOSCAN only looks for the action 8, so we can skip the rest of the file */
2011-08-21 14:05:47 +02:00
_cur . skip_sprites = - 1 ;
2006-12-04 09:30:04 +01:00
}
2006-04-23 20:03:55 +02:00
/* Action 0x08 */
2010-01-22 14:02:15 +01:00
static void GRFInfo ( ByteReader * buf )
2004-08-09 19:04:08 +02:00
{
/* <08> <version> <grf-id> <name> <info>
*
* B version newgrf version , currently 06
* 4 * B grf - id globally unique ID of this . grf file
* S name name of this . grf set
* S info string describing the set , and e . g . author and copyright */
2004-11-12 16:36:49 +01:00
2010-01-23 14:17:30 +01:00
uint8 version = buf - > ReadByte ( ) ;
uint32 grfid = buf - > ReadDWord ( ) ;
const char * name = buf - > ReadString ( ) ;
2004-11-12 23:28:19 +01:00
2011-08-21 14:05:47 +02:00
if ( _cur . stage < GLS_RESERVE & & _cur . grfconfig - > status ! = GCS_UNKNOWN ) {
2011-05-11 22:22:04 +02:00
DisableGrf ( STR_NEWGRF_ERROR_MULTIPLE_ACTION_8 ) ;
2009-01-10 20:30:40 +01:00
return ;
}
2011-08-21 14:05:47 +02:00
if ( _cur . grffile - > grfid ! = grfid ) {
2021-06-12 09:10:17 +02:00
Debug ( grf , 0 , " GRFInfo: GRFID {:08X} in FILESCAN stage does not match GRFID {:08X} in INIT/RESERVE/ACTIVATION stage " , BSWAP32 ( _cur . grffile - > grfid ) , BSWAP32 ( grfid ) ) ;
2011-08-21 14:05:47 +02:00
_cur . grffile - > grfid = grfid ;
2010-11-10 18:04:32 +01:00
}
2011-08-21 14:05:47 +02:00
_cur . grffile - > grf_version = version ;
_cur . grfconfig - > status = _cur . stage < GLS_RESERVE ? GCS_INITIALISED : GCS_ACTIVATED ;
2004-11-12 23:28:19 +01:00
2006-05-01 23:45:35 +02:00
/* Do swap the GRFID for displaying purposes since people expect that */
2021-06-12 09:10:17 +02:00
Debug ( grf , 1 , " GRFInfo: Loaded GRFv{} set {:08X} - {} (palette: {}, version: {}) " , version , BSWAP32 ( grfid ) , name , ( _cur . grfconfig - > palette & GRFP_USE_MASK ) ? " Windows " : " DOS " , _cur . grfconfig - > version ) ;
2004-08-09 19:04:08 +02:00
}
2006-04-23 20:03:55 +02:00
/* Action 0x0A */
2010-01-22 14:02:15 +01:00
static void SpriteReplace ( ByteReader * buf )
2004-08-09 19:04:08 +02:00
{
/* <0A> <num-sets> <set1> [<set2> ...]
* < set > : < num - sprites > < first - sprite >
*
* B num - sets How many sets of sprites to replace .
* Each set :
* B num - sprites How many sprites are in this set
* W first - sprite First sprite number to replace */
2004-11-12 19:47:19 +01:00
2010-01-23 14:17:30 +01:00
uint8 num_sets = buf - > ReadByte ( ) ;
2004-11-12 19:47:19 +01:00
2007-03-10 10:55:11 +01:00
for ( uint i = 0 ; i < num_sets ; i + + ) {
2010-01-23 14:17:30 +01:00
uint8 num_sprites = buf - > ReadByte ( ) ;
uint16 first_sprite = buf - > ReadWord ( ) ;
2004-11-14 14:59:11 +01:00
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " SpriteReplace: [Set {}] Changing {} sprites, beginning with {} " ,
2005-08-15 13:39:13 +02:00
i , num_sprites , first_sprite
) ;
2004-11-12 19:47:19 +01:00
2007-03-10 10:55:11 +01:00
for ( uint j = 0 ; j < num_sprites ; j + + ) {
2008-01-03 03:39:06 +01:00
int load_index = first_sprite + j ;
2011-08-21 14:05:47 +02:00
_cur . nfo_line + + ;
2021-04-14 17:20:39 +02:00
LoadNextSprite ( load_index , * _cur . file , _cur . nfo_line ) ; // XXX
2008-01-03 03:39:06 +01:00
2008-01-24 15:49:40 +01:00
/* Shore sprites now located at different addresses.
* So detect when the old ones get replaced . */
if ( IsInsideMM ( load_index , SPR_ORIGINALSHORE_START , SPR_ORIGINALSHORE_END + 1 ) ) {
if ( _loaded_newgrf_features . shore ! = SHORE_REPLACE_ACTION_5 ) _loaded_newgrf_features . shore = SHORE_REPLACE_ACTION_A ;
}
2004-11-12 19:47:19 +01:00
}
}
2004-08-09 19:04:08 +02:00
}
2007-04-26 08:49:32 +02:00
/* Action 0x0A (SKIP) */
2010-01-22 14:02:15 +01:00
static void SkipActA ( ByteReader * buf )
2007-04-26 08:49:32 +02:00
{
2010-01-23 14:17:30 +01:00
uint8 num_sets = buf - > ReadByte ( ) ;
2007-04-26 08:49:32 +02:00
for ( uint i = 0 ; i < num_sets ; i + + ) {
/* Skip the sprites this replaces */
2011-08-21 14:05:47 +02:00
_cur . skip_sprites + = buf - > ReadByte ( ) ;
2007-04-26 08:49:32 +02:00
/* But ignore where they go */
2010-01-23 14:17:30 +01:00
buf - > ReadWord ( ) ;
2007-04-26 08:49:32 +02:00
}
2023-04-18 21:24:42 +02:00
GrfMsg ( 3 , " SkipActA: Skipping {} sprites " , _cur . skip_sprites ) ;
2007-04-26 08:49:32 +02:00
}
2006-05-02 11:31:59 +02:00
/* Action 0x0B */
2010-01-22 14:02:15 +01:00
static void GRFLoadError ( ByteReader * buf )
2004-08-09 19:04:08 +02:00
{
/* <0B> <severity> <language-id> <message-id> [<message...> 00] [<data...>] 00 [<parnum>]
*
2019-09-29 22:27:32 +02:00
* B severity 00 : notice , continue loading grf file
2004-08-09 19:04:08 +02:00
* 01 : warning , continue loading grf file
* 02 : error , but continue loading grf file , and attempt
* loading grf again when loading or starting next game
* 03 : error , abort loading and prevent loading again in
* the future ( only when restarting the patch )
* B language - id see action 4 , use 1F for built - in error messages
* B message - id message to show , see below
* S message for custom messages ( message - id FF ) , text of the message
* not present for built - in messages .
* V data additional data for built - in ( or custom ) messages
2007-02-24 17:34:37 +01:00
* B parnum parameter numbers to be shown in the message ( maximum of 2 ) */
static const StringID msgstr [ ] = {
STR_NEWGRF_ERROR_VERSION_NUMBER ,
STR_NEWGRF_ERROR_DOS_OR_WINDOWS ,
STR_NEWGRF_ERROR_UNSET_SWITCH ,
STR_NEWGRF_ERROR_INVALID_PARAMETER ,
STR_NEWGRF_ERROR_LOAD_BEFORE ,
2007-10-21 16:59:05 +02:00
STR_NEWGRF_ERROR_LOAD_AFTER ,
STR_NEWGRF_ERROR_OTTD_VERSION_NUMBER ,
2004-08-09 19:04:08 +02:00
} ;
2006-12-26 18:22:47 +01:00
2007-02-24 17:34:37 +01:00
static const StringID sevstr [ ] = {
STR_NEWGRF_ERROR_MSG_INFO ,
STR_NEWGRF_ERROR_MSG_WARNING ,
STR_NEWGRF_ERROR_MSG_ERROR ,
STR_NEWGRF_ERROR_MSG_FATAL
2006-12-26 18:22:47 +01:00
} ;
2004-08-09 19:04:08 +02:00
2010-01-23 14:17:30 +01:00
byte severity = buf - > ReadByte ( ) ;
byte lang = buf - > ReadByte ( ) ;
byte message_id = buf - > ReadByte ( ) ;
2007-02-20 18:52:43 +01:00
2007-06-12 15:22:14 +02:00
/* Skip the error if it isn't valid for the current language. */
2011-08-21 14:05:47 +02:00
if ( ! CheckGrfLangID ( lang , _cur . grffile - > grf_version ) ) return ;
2007-06-12 15:22:14 +02:00
2007-02-21 18:20:44 +01:00
/* Skip the error until the activation stage unless bit 7 of the severity
* is set . */
2011-08-21 14:05:47 +02:00
if ( ! HasBit ( severity , 7 ) & & _cur . stage = = GLS_INIT ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 7 , " GRFLoadError: Skipping non-fatal GRFLoadError in stage {} " , _cur . stage ) ;
2004-11-17 03:01:12 +01:00
return ;
2006-04-19 09:50:16 +02:00
}
2007-11-19 22:32:20 +01:00
ClrBit ( severity , 7 ) ;
2007-02-21 18:20:44 +01:00
if ( severity > = lengthof ( sevstr ) ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 7 , " GRFLoadError: Invalid severity id {}. Setting to 2 (non-fatal error). " , severity ) ;
2007-02-21 18:20:44 +01:00
severity = 2 ;
} else if ( severity = = 3 ) {
/* This is a fatal error, so make sure the GRF is deactivated and no
* more of it gets loaded . */
2011-05-11 22:22:04 +02:00
DisableGrf ( ) ;
2012-01-12 18:46:42 +01:00
/* Make sure we show fatal errors, instead of silly infos from before */
2023-04-27 20:28:09 +02:00
_cur . grfconfig - > error . reset ( ) ;
2007-02-21 18:20:44 +01:00
}
if ( message_id > = lengthof ( msgstr ) & & message_id ! = 0xFF ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 7 , " GRFLoadError: Invalid message id. " ) ;
2007-02-21 18:20:44 +01:00
return ;
}
2010-01-22 14:02:15 +01:00
if ( buf - > Remaining ( ) < = 1 ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 7 , " GRFLoadError: No message data supplied. " ) ;
2007-02-21 18:20:44 +01:00
return ;
}
2004-11-17 03:01:12 +01:00
2012-01-12 18:46:42 +01:00
/* For now we can only show one message per newgrf file. */
2023-06-25 12:57:58 +02:00
if ( _cur . grfconfig - > error . has_value ( ) ) return ;
2012-01-12 18:46:42 +01:00
2023-06-25 12:57:58 +02:00
_cur . grfconfig - > error = { sevstr [ severity ] } ;
GRFError * error = & _cur . grfconfig - > error . value ( ) ;
2007-02-24 17:34:37 +01:00
if ( message_id = = 0xFF ) {
/* This is a custom error message. */
2010-01-22 14:02:15 +01:00
if ( buf - > HasData ( ) ) {
2010-01-23 14:17:30 +01:00
const char * message = buf - > ReadString ( ) ;
2007-02-24 17:34:37 +01:00
2020-05-17 23:31:54 +02:00
error - > custom_message = TranslateTTDPatchCodes ( _cur . grffile - > grfid , lang , true , message , SCC_RAW_STRING_POINTER ) ;
2010-01-22 14:02:15 +01:00
} else {
2023-04-18 21:24:42 +02:00
GrfMsg ( 7 , " GRFLoadError: No custom message supplied. " ) ;
2020-05-17 23:31:54 +02:00
error - > custom_message . clear ( ) ;
2010-01-22 14:02:15 +01:00
}
2007-02-24 17:34:37 +01:00
} else {
error - > message = msgstr [ message_id ] ;
}
2010-01-22 14:02:15 +01:00
if ( buf - > HasData ( ) ) {
2010-01-23 14:17:30 +01:00
const char * data = buf - > ReadString ( ) ;
2007-02-24 17:34:37 +01:00
2020-05-17 23:31:54 +02:00
error - > data = TranslateTTDPatchCodes ( _cur . grffile - > grfid , lang , true , data ) ;
2009-12-30 21:31:43 +01:00
} else {
2023-04-18 21:24:42 +02:00
GrfMsg ( 7 , " GRFLoadError: No message data supplied. " ) ;
2020-05-17 23:31:54 +02:00
error - > data . clear ( ) ;
2007-02-24 17:34:37 +01:00
}
2007-02-22 00:18:08 +01:00
2007-02-24 17:34:37 +01:00
/* Only two parameter numbers can be used in the string. */
2023-05-18 20:56:09 +02:00
for ( uint i = 0 ; i < error - > param_value . size ( ) & & buf - > HasData ( ) ; i + + ) {
2010-01-23 14:17:30 +01:00
uint param_number = buf - > ReadByte ( ) ;
2011-08-21 14:05:47 +02:00
error - > param_value [ i ] = _cur . grffile - > GetParam ( param_number ) ;
2007-02-22 00:18:08 +01:00
}
2004-08-09 19:04:08 +02:00
}
2006-05-02 11:31:59 +02:00
/* Action 0x0C */
2010-01-22 14:02:15 +01:00
static void GRFComment ( ByteReader * buf )
2004-08-09 19:04:08 +02:00
{
/* <0C> [<ignored...>]
*
* V ignored Anything following the 0 C is ignored */
2006-04-15 23:27:59 +02:00
2010-01-22 14:02:15 +01:00
if ( ! buf - > HasData ( ) ) return ;
2006-04-15 23:27:59 +02:00
2010-01-23 14:17:30 +01:00
const char * text = buf - > ReadString ( ) ;
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " GRFComment: {} " , text ) ;
2004-08-09 19:04:08 +02:00
}
2007-01-04 20:24:42 +01:00
/* Action 0x0D (GLS_SAFETYSCAN) */
2010-01-22 14:02:15 +01:00
static void SafeParamSet ( ByteReader * buf )
2007-01-04 20:24:42 +01:00
{
2010-01-23 14:17:30 +01:00
uint8 target = buf - > ReadByte ( ) ;
2007-01-04 20:24:42 +01:00
2016-01-13 22:55:50 +01:00
/* Writing GRF parameters and some bits of 'misc GRF features' are safe. */
if ( target < 0x80 | | target = = 0x9E ) return ;
2007-01-04 20:24:42 +01:00
/* GRM could be unsafe, but as here it can only happen after other GRFs
* are loaded , it should be okay . If the GRF tried to use the slots it
* reserved , it would be marked unsafe anyway . GRM for ( e . g . bridge )
* sprites is considered safe . */
2011-08-21 14:05:47 +02:00
SetBit ( _cur . grfconfig - > flags , GCF_UNSAFE ) ;
2007-01-04 20:24:42 +01:00
/* Skip remainder of GRF */
2011-08-21 14:05:47 +02:00
_cur . skip_sprites = - 1 ;
2007-01-04 20:24:42 +01:00
}
2007-04-20 23:19:28 +02:00
static uint32 GetPatchVariable ( uint8 param )
{
switch ( param ) {
/* start year - 1920 */
2021-01-08 11:16:18 +01:00
case 0x0B : return std : : max ( _settings_game . game_creation . starting_year , ORIGINAL_BASE_YEAR ) - ORIGINAL_BASE_YEAR ;
2008-01-23 18:08:35 +01:00
2007-04-20 23:19:28 +02:00
/* freight trains weight factor */
2008-05-29 17:13:28 +02:00
case 0x0E : return _settings_game . vehicle . freight_trains ;
2008-01-23 18:08:35 +01:00
2007-04-20 23:19:28 +02:00
/* empty wagon speed increase */
case 0x0F : return 0 ;
2008-01-23 18:08:35 +01:00
2008-02-27 22:33:52 +01:00
/* plane speed factor; our patch option is reversed from TTDPatch's,
* the following is good for 1 x , 2 x and 4 x ( most common ? ) and . . .
* well not really for 3 x . */
case 0x10 :
2008-05-29 17:13:28 +02:00
switch ( _settings_game . vehicle . plane_speed ) {
2008-02-27 22:33:52 +01:00
default :
case 4 : return 1 ;
case 3 : return 2 ;
case 2 : return 2 ;
case 1 : return 4 ;
}
2008-01-23 18:08:35 +01:00
2009-02-09 03:57:15 +01:00
/* 2CC colourmap base sprite */
2007-04-20 23:19:28 +02:00
case 0x11 : return SPR_2CCMAP_BASE ;
2008-01-23 18:08:35 +01:00
/* map size: format = -MABXYSS
* M : the type of map
* bit 0 : set : squared map . Bit 1 is now not relevant
* clear : rectangle map . Bit 1 will indicate the bigger edge of the map
* bit 1 : set : Y is the bigger edge . Bit 0 is clear
* clear : X is the bigger edge .
* A : minimum edge ( log2 ) of the map
* B : maximum edge ( log2 ) of the map
* XY : edges ( log2 ) of each side of the map .
* SS : combination of both X and Y , thus giving the size ( log2 ) of the map
*/
case 0x13 : {
byte map_bits = 0 ;
2023-01-21 10:43:03 +01:00
byte log_X = Map : : LogX ( ) - 6 ; // subtraction is required to make the minimal size (64) zero based
byte log_Y = Map : : LogY ( ) - 6 ;
2021-01-08 11:16:18 +01:00
byte max_edge = std : : max ( log_X , log_Y ) ;
2008-01-23 18:08:35 +01:00
if ( log_X = = log_Y ) { // we have a squared map, since both edges are identical
2009-07-16 12:13:33 +02:00
SetBit ( map_bits , 0 ) ;
2008-01-23 18:08:35 +01:00
} else {
if ( max_edge = = log_Y ) SetBit ( map_bits , 1 ) ; // edge Y been the biggest, mark it
}
2021-01-08 11:16:18 +01:00
return ( map_bits < < 24 ) | ( std : : min ( log_X , log_Y ) < < 20 ) | ( max_edge < < 16 ) |
2008-01-23 18:08:35 +01:00
( log_X < < 12 ) | ( log_Y < < 8 ) | ( log_X + log_Y ) ;
}
2011-11-08 18:29:41 +01:00
/* The maximum height of the map. */
case 0x14 :
2021-03-24 09:42:54 +01:00
return _settings_game . construction . map_height_limit ;
2011-11-08 18:29:41 +01:00
2013-05-07 14:38:43 +02:00
/* Extra foundations base sprite */
case 0x15 :
return SPR_SLOPES_BASE ;
/* Shore base sprite */
case 0x16 :
return SPR_SHORE_BASE ;
2022-03-13 11:21:40 +01:00
/* Game map seed */
case 0x17 :
return _settings_game . game_creation . generation_seed ;
2007-04-20 23:19:28 +02:00
default :
2023-04-20 17:00:46 +02:00
GrfMsg ( 2 , " ParamSet: Unknown Patch variable 0x{:02X}. " , param ) ;
2007-04-20 23:19:28 +02:00
return 0 ;
}
}
2007-06-02 10:56:21 +02:00
static uint32 PerformGRM ( uint32 * grm , uint16 num_ids , uint16 count , uint8 op , uint8 target , const char * type )
{
uint start = 0 ;
uint size = 0 ;
if ( op = = 6 ) {
/* Return GRFID of set that reserved ID */
2011-08-21 14:05:47 +02:00
return grm [ _cur . grffile - > GetParam ( target ) ] ;
2007-06-02 10:56:21 +02:00
}
/* With an operation of 2 or 3, we want to reserve a specific block of IDs */
2011-08-21 14:05:47 +02:00
if ( op = = 2 | | op = = 3 ) start = _cur . grffile - > GetParam ( target ) ;
2007-06-02 10:56:21 +02:00
for ( uint i = start ; i < num_ids ; i + + ) {
if ( grm [ i ] = = 0 ) {
size + + ;
} else {
if ( op = = 2 | | op = = 3 ) break ;
start = i + 1 ;
size = 0 ;
}
if ( size = = count ) break ;
}
if ( size = = count ) {
/* Got the slot... */
if ( op = = 0 | | op = = 3 ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " ParamSet: GRM: Reserving {} {} at {} " , count , type , start ) ;
2011-08-21 14:05:47 +02:00
for ( uint i = 0 ; i < count ; i + + ) grm [ start + i ] = _cur . grffile - > grfid ;
2007-06-02 10:56:21 +02:00
}
return start ;
}
/* Unable to allocate */
if ( op ! = 4 & & op ! = 5 ) {
/* Deactivate GRF */
2023-04-18 21:24:42 +02:00
GrfMsg ( 0 , " ParamSet: GRM: Unable to allocate {} {}, deactivating " , count , type ) ;
2011-05-11 22:23:37 +02:00
DisableGrf ( STR_NEWGRF_ERROR_GRM_FAILED ) ;
2007-06-02 10:56:21 +02:00
return UINT_MAX ;
}
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " ParamSet: GRM: Unable to allocate {} {} " , count , type ) ;
2007-06-02 10:56:21 +02:00
return UINT_MAX ;
}
2011-09-11 13:14:22 +02:00
/** Action 0x0D: Set parameter */
2010-01-22 14:02:15 +01:00
static void ParamSet ( ByteReader * buf )
2004-08-09 19:04:08 +02:00
{
/* <0D> <target> <operation> <source1> <source2> [<data>]
*
* B target parameter number where result is stored
* B operation operation to perform , see below
* B source1 first source operand
* B source2 second source operand
* D data data to use in the calculation , not necessary
* if both source1 and source2 refer to actual parameters
*
* Operations
* 00 Set parameter equal to source1
* 01 Addition , source1 + source2
* 02 Subtraction , source1 - source2
* 03 Unsigned multiplication , source1 * source2 ( both unsigned )
* 04 Signed multiplication , source1 * source2 ( both signed )
* 05 Unsigned bit shift , source1 by source2 ( source2 taken to be a
* signed quantity ; left shift if positive and right shift if
* negative , source1 is unsigned )
* 06 Signed bit shift , source1 by source2
* ( source2 like in 05 , and source1 as well )
*/
2004-11-12 19:57:46 +01:00
2010-01-23 14:17:30 +01:00
uint8 target = buf - > ReadByte ( ) ;
uint8 oper = buf - > ReadByte ( ) ;
uint32 src1 = buf - > ReadByte ( ) ;
uint32 src2 = buf - > ReadByte ( ) ;
2004-11-12 19:57:46 +01:00
2007-03-10 10:55:11 +01:00
uint32 data = 0 ;
2010-01-23 14:17:30 +01:00
if ( buf - > Remaining ( ) > = 4 ) data = buf - > ReadDWord ( ) ;
2004-11-14 14:59:11 +01:00
2004-11-12 19:57:46 +01:00
/* You can add 80 to the operation to make it apply only if the target
* is not defined yet . In this respect , a parameter is taken to be
* defined if any of the following applies :
* - it has been set to any value in the newgrf ( w ) . cfg parameter list
* - it OR A PARAMETER WITH HIGHER NUMBER has been set to any value by
* an earlier action D */
2007-11-19 22:02:30 +01:00
if ( HasBit ( oper , 7 ) ) {
2011-08-21 14:05:47 +02:00
if ( target < 0x80 & & target < _cur . grffile - > param_end ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 7 , " ParamSet: Param {} already defined, skipping " , target ) ;
2004-11-12 19:57:46 +01:00
return ;
2006-05-10 23:41:16 +02:00
}
2007-03-10 10:55:11 +01:00
oper = GB ( oper , 0 , 7 ) ;
2004-11-12 19:57:46 +01:00
}
2006-05-10 23:55:44 +02:00
if ( src2 = = 0xFE ) {
if ( GB ( data , 0 , 8 ) = = 0xFF ) {
if ( data = = 0x0000FFFF ) {
/* Patch variables */
2007-04-20 23:19:28 +02:00
src1 = GetPatchVariable ( src1 ) ;
2006-05-10 23:55:44 +02:00
} else {
/* GRF Resource Management */
2008-05-05 21:02:10 +02:00
uint8 op = src1 ;
uint8 feature = GB ( data , 8 , 8 ) ;
uint16 count = GB ( data , 16 , 16 ) ;
2011-08-21 14:05:47 +02:00
if ( _cur . stage = = GLS_RESERVE ) {
2008-05-05 21:02:10 +02:00
if ( feature = = 0x08 ) {
/* General sprites */
if ( op = = 0 ) {
/* Check if the allocated sprites will fit below the original sprite limit */
2011-08-21 14:05:47 +02:00
if ( _cur . spriteid + count > = 16384 ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 0 , " ParamSet: GRM: Unable to allocate {} sprites; try changing NewGRF order " , count ) ;
2011-05-11 22:23:37 +02:00
DisableGrf ( STR_NEWGRF_ERROR_GRM_FAILED ) ;
2008-05-05 21:02:10 +02:00
return ;
}
2006-10-27 15:15:21 +02:00
2008-05-05 21:02:10 +02:00
/* Reserve space at the current sprite ID */
2023-04-18 21:24:42 +02:00
GrfMsg ( 4 , " ParamSet: GRM: Allocated {} sprites at {} " , count , _cur . spriteid ) ;
2011-08-21 14:05:47 +02:00
_grm_sprites [ GRFLocation ( _cur . grffile - > grfid , _cur . nfo_line ) ] = _cur . spriteid ;
_cur . spriteid + = count ;
2008-05-05 21:02:10 +02:00
}
}
/* Ignore GRM result during reservation */
src1 = 0 ;
2011-08-21 14:05:47 +02:00
} else if ( _cur . stage = = GLS_ACTIVATION ) {
2006-10-27 15:15:21 +02:00
switch ( feature ) {
2007-03-21 04:06:21 +01:00
case 0x00 : // Trains
case 0x01 : // Road Vehicles
case 0x02 : // Ships
case 0x03 : // Aircraft
2008-05-29 17:13:28 +02:00
if ( ! _settings_game . vehicle . dynamic_engines ) {
2008-04-29 23:31:29 +02:00
src1 = PerformGRM ( & _grm_engines [ _engine_offsets [ feature ] ] , _engine_counts [ feature ] , count , op , target , " vehicles " ) ;
2011-08-21 14:05:47 +02:00
if ( _cur . skip_sprites = = - 1 ) return ;
2008-04-29 23:31:29 +02:00
} else {
2009-03-15 01:32:18 +01:00
/* GRM does not apply for dynamic engine allocation. */
2008-04-29 23:31:29 +02:00
switch ( op ) {
case 2 :
case 3 :
2011-08-21 14:05:47 +02:00
src1 = _cur . grffile - > GetParam ( target ) ;
2008-04-29 23:31:29 +02:00
break ;
default :
src1 = 0 ;
break ;
}
}
2006-10-27 15:15:21 +02:00
break ;
2007-03-21 04:06:21 +01:00
case 0x08 : // General sprites
2006-10-27 15:15:21 +02:00
switch ( op ) {
case 0 :
2008-05-05 21:02:10 +02:00
/* Return space reserved during reservation stage */
2011-08-21 14:05:47 +02:00
src1 = _grm_sprites [ GRFLocation ( _cur . grffile - > grfid , _cur . nfo_line ) ] ;
2023-04-18 21:24:42 +02:00
GrfMsg ( 4 , " ParamSet: GRM: Using pre-allocated sprites at {} " , src1 ) ;
2006-10-27 15:15:21 +02:00
break ;
case 1 :
2011-08-21 14:05:47 +02:00
src1 = _cur . spriteid ;
2006-10-27 15:15:21 +02:00
break ;
default :
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " ParamSet: GRM: Unsupported operation {} for general sprites " , op ) ;
2006-10-27 15:15:21 +02:00
return ;
}
break ;
2007-06-02 10:56:21 +02:00
case 0x0B : // Cargo
2007-10-07 10:48:31 +02:00
/* There are two ranges: one for cargo IDs and one for cargo bitmasks */
2011-12-15 22:56:00 +01:00
src1 = PerformGRM ( _grm_cargoes , NUM_CARGO * 2 , count , op , target , " cargoes " ) ;
2011-08-21 14:05:47 +02:00
if ( _cur . skip_sprites = = - 1 ) return ;
2007-06-02 10:56:21 +02:00
break ;
2023-04-20 17:00:46 +02:00
default : GrfMsg ( 1 , " ParamSet: GRM: Unsupported feature 0x{:X} " , feature ) ; return ;
2006-10-27 15:15:21 +02:00
}
2008-05-05 21:02:10 +02:00
} else {
/* Ignore GRM during initialization */
src1 = 0 ;
2006-10-27 15:15:21 +02:00
}
2006-05-10 23:55:44 +02:00
}
} else {
/* Read another GRF File's parameter */
const GRFFile * file = GetFileByGRFID ( data ) ;
2008-06-19 12:20:45 +02:00
GRFConfig * c = GetGRFConfig ( data ) ;
2019-04-10 23:07:06 +02:00
if ( c ! = nullptr & & HasBit ( c - > flags , GCF_STATIC ) & & ! HasBit ( _cur . grfconfig - > flags , GCF_STATIC ) & & _networking ) {
2008-06-19 12:20:45 +02:00
/* Disable the read GRF if it is a static NewGRF. */
DisableStaticNewGRFInfluencingNonStaticNewGRFs ( c ) ;
src1 = 0 ;
2019-04-10 23:07:06 +02:00
} else if ( file = = nullptr | | c = = nullptr | | c - > status = = GCS_DISABLED ) {
2006-05-10 23:55:44 +02:00
src1 = 0 ;
2010-08-28 23:45:44 +02:00
} else if ( src1 = = 0xFE ) {
src1 = c - > version ;
2006-05-10 23:55:44 +02:00
} else {
2010-01-03 20:29:56 +01:00
src1 = file - > GetParam ( src1 ) ;
2006-05-10 23:55:44 +02:00
}
}
2004-11-12 19:57:46 +01:00
} else {
2006-05-10 23:55:44 +02:00
/* The source1 and source2 operands refer to the grf parameter number
* like in action 6 and 7. In addition , they can refer to the special
* variables available in action 7 , or they can be FF to use the value
* of < data > . If referring to parameters that are undefined , a value
* of 0 is used instead . */
2019-04-10 23:07:06 +02:00
src1 = ( src1 = = 0xFF ) ? data : GetParamVal ( src1 , nullptr ) ;
src2 = ( src2 = = 0xFF ) ? data : GetParamVal ( src2 , nullptr ) ;
2004-11-12 19:57:46 +01:00
}
2007-03-10 10:55:11 +01:00
uint32 res ;
2004-11-12 19:57:46 +01:00
switch ( oper ) {
case 0x00 :
2005-08-15 23:19:28 +02:00
res = src1 ;
2004-11-12 19:57:46 +01:00
break ;
2005-08-15 23:19:28 +02:00
2004-11-12 19:57:46 +01:00
case 0x01 :
2005-08-15 23:19:28 +02:00
res = src1 + src2 ;
2004-11-12 19:57:46 +01:00
break ;
2005-08-15 23:19:28 +02:00
2004-11-12 19:57:46 +01:00
case 0x02 :
2005-08-15 23:19:28 +02:00
res = src1 - src2 ;
2004-11-12 19:57:46 +01:00
break ;
2005-08-15 23:19:28 +02:00
2004-11-12 19:57:46 +01:00
case 0x03 :
2005-08-15 23:19:28 +02:00
res = src1 * src2 ;
2004-11-12 19:57:46 +01:00
break ;
2005-08-15 23:19:28 +02:00
2004-11-12 19:57:46 +01:00
case 0x04 :
2005-08-15 23:19:28 +02:00
res = ( int32 ) src1 * ( int32 ) src2 ;
2004-11-12 19:57:46 +01:00
break ;
2005-08-15 23:19:28 +02:00
2004-11-12 19:57:46 +01:00
case 0x05 :
2006-06-27 23:25:53 +02:00
if ( ( int32 ) src2 < 0 ) {
2005-08-15 23:19:28 +02:00
res = src1 > > - ( int32 ) src2 ;
2006-06-27 23:25:53 +02:00
} else {
2014-04-20 17:47:50 +02:00
res = src1 < < ( src2 & 0x1F ) ; // Same behaviour as in EvalAdjustT, mask 'value' to 5 bits, which should behave the same on all architectures.
2006-06-27 23:25:53 +02:00
}
2004-11-12 19:57:46 +01:00
break ;
2005-08-15 23:19:28 +02:00
2004-11-12 19:57:46 +01:00
case 0x06 :
2006-06-27 23:25:53 +02:00
if ( ( int32 ) src2 < 0 ) {
2005-08-15 23:19:28 +02:00
res = ( int32 ) src1 > > - ( int32 ) src2 ;
2006-06-27 23:25:53 +02:00
} else {
2014-04-20 17:47:50 +02:00
res = ( int32 ) src1 < < ( src2 & 0x1F ) ; // Same behaviour as in EvalAdjustT, mask 'value' to 5 bits, which should behave the same on all architectures.
2006-06-27 23:25:53 +02:00
}
2004-11-12 19:57:46 +01:00
break ;
2005-08-15 23:19:28 +02:00
2007-03-21 04:06:21 +01:00
case 0x07 : // Bitwise AND
2005-09-21 21:07:58 +02:00
res = src1 & src2 ;
break ;
2007-03-21 04:06:21 +01:00
case 0x08 : // Bitwise OR
2005-09-21 21:07:58 +02:00
res = src1 | src2 ;
break ;
2007-03-21 04:06:21 +01:00
case 0x09 : // Unsigned division
2005-09-21 21:07:58 +02:00
if ( src2 = = 0 ) {
res = src1 ;
} else {
res = src1 / src2 ;
}
break ;
2019-09-29 22:27:32 +02:00
case 0x0A : // Signed division
2005-09-21 21:07:58 +02:00
if ( src2 = = 0 ) {
res = src1 ;
} else {
res = ( int32 ) src1 / ( int32 ) src2 ;
}
break ;
2007-03-21 04:06:21 +01:00
case 0x0B : // Unsigned modulo
2005-09-21 21:07:58 +02:00
if ( src2 = = 0 ) {
res = src1 ;
} else {
res = src1 % src2 ;
}
break ;
2007-03-21 04:06:21 +01:00
case 0x0C : // Signed modulo
2005-09-21 21:07:58 +02:00
if ( src2 = = 0 ) {
res = src1 ;
} else {
res = ( int32 ) src1 % ( int32 ) src2 ;
}
break ;
2023-04-18 21:24:42 +02:00
default : GrfMsg ( 0 , " ParamSet: Unknown operation {}, skipping " , oper ) ; return ;
2005-08-15 23:19:28 +02:00
}
switch ( target ) {
case 0x8E : // Y-Offset for train sprites
2011-08-21 14:05:47 +02:00
_cur . grffile - > traininfo_vehicle_pitch = res ;
2005-08-15 23:19:28 +02:00
break ;
2008-08-25 01:10:23 +02:00
case 0x8F : { // Rail track type cost factors
extern RailtypeInfo _railtypes [ RAILTYPE_END ] ;
_railtypes [ RAILTYPE_RAIL ] . cost_multiplier = GB ( res , 0 , 8 ) ;
2008-05-29 17:13:28 +02:00
if ( _settings_game . vehicle . disable_elrails ) {
2008-08-25 01:10:23 +02:00
_railtypes [ RAILTYPE_ELECTRIC ] . cost_multiplier = GB ( res , 0 , 8 ) ;
_railtypes [ RAILTYPE_MONO ] . cost_multiplier = GB ( res , 8 , 8 ) ;
2007-10-14 23:20:12 +02:00
} else {
2008-08-25 01:10:23 +02:00
_railtypes [ RAILTYPE_ELECTRIC ] . cost_multiplier = GB ( res , 8 , 8 ) ;
_railtypes [ RAILTYPE_MONO ] . cost_multiplier = GB ( res , 16 , 8 ) ;
2007-10-14 23:20:12 +02:00
}
2008-08-25 01:10:23 +02:00
_railtypes [ RAILTYPE_MAGLEV ] . cost_multiplier = GB ( res , 16 , 8 ) ;
2007-10-14 23:20:12 +02:00
break ;
2008-08-25 01:10:23 +02:00
}
2007-10-14 23:20:12 +02:00
2019-03-01 00:00:50 +01:00
/* not implemented */
case 0x93 : // Tile refresh offset to left -- Intended to allow support for larger sprites, not necessary for OTTD
2005-08-15 23:19:28 +02:00
case 0x94 : // Tile refresh offset to right
case 0x95 : // Tile refresh offset upwards
case 0x96 : // Tile refresh offset downwards
2019-03-01 00:00:50 +01:00
case 0x97 : // Snow line height -- Better supported by feature 8 property 10h (snow line table) TODO: implement by filling the entire snow line table with the given value
case 0x99 : // Global ID offset -- Not necessary since IDs are remapped automatically
2023-04-20 17:00:46 +02:00
GrfMsg ( 7 , " ParamSet: Skipping unimplemented target 0x{:02X} " , target ) ;
2005-08-15 23:19:28 +02:00
break ;
2007-03-21 04:06:21 +01:00
case 0x9E : // Miscellaneous GRF features
2006-05-14 22:58:12 +02:00
/* Set train list engine width */
2012-03-25 20:18:35 +02:00
_cur . grffile - > traininfo_vehicle_width = HasBit ( res , GMB_TRAIN_WIDTH_32_PIXELS ) ? VEHICLEINFO_FULL_VEHICLE_WIDTH : TRAININFO_DEFAULT_VEHICLE_WIDTH ;
2009-07-18 00:00:13 +02:00
/* Remove the local flags from the global flags */
2012-03-25 20:18:35 +02:00
ClrBit ( res , GMB_TRAIN_WIDTH_32_PIXELS ) ;
2016-01-13 22:55:50 +01:00
/* Only copy safe bits for static grfs */
if ( HasBit ( _cur . grfconfig - > flags , GCF_STATIC ) ) {
uint32 safe_bits = 0 ;
SetBit ( safe_bits , GMB_SECOND_ROCKY_TILE_SET ) ;
_misc_grf_features = ( _misc_grf_features & ~ safe_bits ) | ( res & safe_bits ) ;
} else {
_misc_grf_features = res ;
}
2006-05-11 14:41:02 +02:00
break ;
2008-02-12 16:43:10 +01:00
case 0x9F : // locale-dependent settings
2023-04-20 17:00:46 +02:00
GrfMsg ( 7 , " ParamSet: Skipping unimplemented target 0x{:02X} " , target ) ;
2008-02-12 16:43:10 +01:00
break ;
2005-08-15 23:19:28 +02:00
default :
if ( target < 0x80 ) {
2011-08-21 14:05:47 +02:00
_cur . grffile - > param [ target ] = res ;
2010-01-03 20:29:56 +01:00
/* param is zeroed by default */
2011-08-21 14:05:47 +02:00
if ( target + 1U > _cur . grffile - > param_end ) _cur . grffile - > param_end = target + 1 ;
2005-08-15 23:19:28 +02:00
} else {
2023-04-20 17:00:46 +02:00
GrfMsg ( 7 , " ParamSet: Skipping unknown target 0x{:02X} " , target ) ;
2005-08-15 23:19:28 +02:00
}
break ;
2004-11-12 19:57:46 +01:00
}
2004-08-09 19:04:08 +02:00
}
2007-01-03 12:33:54 +01:00
/* Action 0x0E (GLS_SAFETYSCAN) */
2010-01-22 14:02:15 +01:00
static void SafeGRFInhibit ( ByteReader * buf )
2007-01-03 12:33:54 +01:00
{
/* <0E> <num> <grfids...>
*
* B num Number of GRFIDs that follow
* D grfids GRFIDs of the files to deactivate */
2010-01-23 14:17:30 +01:00
uint8 num = buf - > ReadByte ( ) ;
2007-01-03 12:33:54 +01:00
2007-03-10 10:55:11 +01:00
for ( uint i = 0 ; i < num ; i + + ) {
2010-01-23 14:17:30 +01:00
uint32 grfid = buf - > ReadDWord ( ) ;
2007-01-03 12:33:54 +01:00
/* GRF is unsafe it if tries to deactivate other GRFs */
2011-08-21 14:05:47 +02:00
if ( grfid ! = _cur . grfconfig - > ident . grfid ) {
SetBit ( _cur . grfconfig - > flags , GCF_UNSAFE ) ;
2007-01-03 12:33:54 +01:00
/* Skip remainder of GRF */
2011-08-21 14:05:47 +02:00
_cur . skip_sprites = - 1 ;
2007-01-03 12:33:54 +01:00
return ;
}
}
}
2006-05-02 11:31:59 +02:00
/* Action 0x0E */
2010-01-22 14:02:15 +01:00
static void GRFInhibit ( ByteReader * buf )
2004-08-09 19:04:08 +02:00
{
/* <0E> <num> <grfids...>
*
* B num Number of GRFIDs that follow
* D grfids GRFIDs of the files to deactivate */
2004-11-12 23:39:00 +01:00
2010-01-23 14:17:30 +01:00
uint8 num = buf - > ReadByte ( ) ;
2004-11-12 23:39:00 +01:00
2007-03-10 10:55:11 +01:00
for ( uint i = 0 ; i < num ; i + + ) {
2010-01-23 14:17:30 +01:00
uint32 grfid = buf - > ReadDWord ( ) ;
2006-12-14 16:14:29 +01:00
GRFConfig * file = GetGRFConfig ( grfid ) ;
2004-11-12 23:39:00 +01:00
/* Unset activation flag */
2019-04-10 23:07:06 +02:00
if ( file ! = nullptr & & file ! = _cur . grfconfig ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " GRFInhibit: Deactivating file '{}' " , file - > filename ) ;
2011-05-11 22:23:37 +02:00
GRFError * error = DisableGrf ( STR_NEWGRF_ERROR_FORCEFULLY_DISABLED , file ) ;
2021-05-09 22:51:26 +02:00
error - > data = _cur . grfconfig - > GetName ( ) ;
2004-11-12 23:39:00 +01:00
}
}
2004-08-09 19:04:08 +02:00
}
2011-09-11 13:14:22 +02:00
/** Action 0x0F - Define Town names */
2010-01-22 14:02:15 +01:00
static void FeatureTownName ( ByteReader * buf )
2007-06-19 01:00:55 +02:00
{
/* <0F> <id> <style-name> <num-parts> <parts>
*
* B id ID of this definition in bottom 7 bits ( final definition if bit 7 set )
* V style - name Name of the style ( only for final definition )
* B num - parts Number of parts in this definition
* V parts The parts */
2011-08-21 14:05:47 +02:00
uint32 grfid = _cur . grffile - > grfid ;
2007-06-19 01:00:55 +02:00
GRFTownName * townname = AddGRFTownName ( grfid ) ;
2010-01-23 14:17:30 +01:00
byte id = buf - > ReadByte ( ) ;
2023-04-20 17:00:46 +02:00
GrfMsg ( 6 , " FeatureTownName: definition 0x{:02X} " , id & 0x7F ) ;
2007-06-19 01:00:55 +02:00
2007-11-19 22:02:30 +01:00
if ( HasBit ( id , 7 ) ) {
2007-06-19 01:00:55 +02:00
/* Final definition */
2007-11-19 22:32:20 +01:00
ClrBit ( id , 7 ) ;
2011-08-21 14:05:47 +02:00
bool new_scheme = _cur . grffile - > grf_version > = 7 ;
2007-06-19 01:00:55 +02:00
2010-01-23 14:17:30 +01:00
byte lang = buf - > ReadByte ( ) ;
2023-04-16 22:24:54 +02:00
StringID style = STR_UNDEFINED ;
2007-06-19 01:00:55 +02:00
do {
2007-11-19 22:32:20 +01:00
ClrBit ( lang , 7 ) ;
2007-06-19 01:00:55 +02:00
2010-01-23 14:17:30 +01:00
const char * name = buf - > ReadString ( ) ;
2008-12-24 22:25:09 +01:00
2020-05-17 23:31:51 +02:00
std : : string lang_name = TranslateTTDPatchCodes ( grfid , lang , false , name ) ;
2023-06-15 19:15:08 +02:00
GrfMsg ( 6 , " FeatureTownName: lang 0x{:X} -> '{}' " , lang , lang_name ) ;
2007-06-19 01:00:55 +02:00
2023-04-16 22:24:54 +02:00
style = AddGRFString ( grfid , id , lang , new_scheme , false , name , STR_UNDEFINED ) ;
2007-06-19 01:00:55 +02:00
2010-01-23 14:17:30 +01:00
lang = buf - > ReadByte ( ) ;
2007-06-19 01:00:55 +02:00
} while ( lang ! = 0 ) ;
2023-04-16 22:24:54 +02:00
townname - > styles . emplace_back ( style , id ) ;
2007-06-19 01:00:55 +02:00
}
2023-04-16 22:24:54 +02:00
uint8 parts = buf - > ReadByte ( ) ;
2023-04-18 21:24:42 +02:00
GrfMsg ( 6 , " FeatureTownName: {} parts " , parts ) ;
2007-06-19 01:00:55 +02:00
2023-04-16 22:24:54 +02:00
townname - > partlists [ id ] . reserve ( parts ) ;
for ( uint partnum = 0 ; partnum < parts ; partnum + + ) {
NamePartList & partlist = townname - > partlists [ id ] . emplace_back ( ) ;
uint8 texts = buf - > ReadByte ( ) ;
partlist . bitstart = buf - > ReadByte ( ) ;
partlist . bitcount = buf - > ReadByte ( ) ;
partlist . maxprob = 0 ;
2023-04-18 21:24:42 +02:00
GrfMsg ( 6 , " FeatureTownName: part {} contains {} texts and will use GB(seed, {}, {}) " , partnum , texts , partlist . bitstart , partlist . bitcount ) ;
2007-06-19 01:00:55 +02:00
2023-04-16 22:24:54 +02:00
partlist . parts . reserve ( texts ) ;
for ( uint textnum = 0 ; textnum < texts ; textnum + + ) {
NamePart & part = partlist . parts . emplace_back ( ) ;
part . prob = buf - > ReadByte ( ) ;
2007-06-19 01:00:55 +02:00
2023-04-16 22:24:54 +02:00
if ( HasBit ( part . prob , 7 ) ) {
2010-01-23 14:17:30 +01:00
byte ref_id = buf - > ReadByte ( ) ;
2023-04-16 22:24:54 +02:00
if ( ref_id > = GRFTownName : : MAX_LISTS | | townname - > partlists [ ref_id ] . empty ( ) ) {
2023-04-20 17:00:46 +02:00
GrfMsg ( 0 , " FeatureTownName: definition 0x{:02X} doesn't exist, deactivating " , ref_id ) ;
2007-06-19 01:00:55 +02:00
DelGRFTownName ( grfid ) ;
2011-05-11 22:22:04 +02:00
DisableGrf ( STR_NEWGRF_ERROR_INVALID_ID ) ;
2007-06-19 01:00:55 +02:00
return ;
}
2023-04-16 22:24:54 +02:00
part . id = ref_id ;
2023-04-20 17:00:46 +02:00
GrfMsg ( 6 , " FeatureTownName: part {}, text {}, uses intermediate definition 0x{:02X} (with probability {}) " , partnum , textnum , ref_id , part . prob & 0x7F ) ;
2007-06-19 01:00:55 +02:00
} else {
2010-01-23 14:17:30 +01:00
const char * text = buf - > ReadString ( ) ;
2023-04-16 22:24:54 +02:00
part . text = TranslateTTDPatchCodes ( grfid , 0 , false , text ) ;
2023-06-15 19:15:08 +02:00
GrfMsg ( 6 , " FeatureTownName: part {}, text {}, '{}' (with probability {}) " , partnum , textnum , part . text , part . prob ) ;
2007-06-19 01:00:55 +02:00
}
2023-04-16 22:24:54 +02:00
partlist . maxprob + = GB ( part . prob , 0 , 7 ) ;
2007-06-19 01:00:55 +02:00
}
2023-04-18 21:24:42 +02:00
GrfMsg ( 6 , " FeatureTownName: part {}, total probability {} " , partnum , partlist . maxprob ) ;
2007-06-19 01:00:55 +02:00
}
}
2011-09-11 13:14:22 +02:00
/** Action 0x10 - Define goto label */
2010-01-22 14:02:15 +01:00
static void DefineGotoLabel ( ByteReader * buf )
2006-04-15 23:27:59 +02:00
{
/* <10> <label> [<comment>]
*
* B label The label to define
* V comment Optional comment - ignored */
2010-01-23 14:17:30 +01:00
byte nfo_label = buf - > ReadByte ( ) ;
2006-04-15 23:27:59 +02:00
2022-12-25 23:32:22 +01:00
_cur . grffile - > labels . emplace_back ( nfo_label , _cur . nfo_line , _cur . file - > GetPos ( ) ) ;
2006-04-15 23:27:59 +02:00
2023-04-20 17:00:46 +02:00
GrfMsg ( 2 , " DefineGotoLabel: GOTO target with label 0x{:02X} " , nfo_label ) ;
2006-04-15 23:27:59 +02:00
}
2004-11-12 20:03:37 +01:00
2012-03-19 23:55:29 +01:00
/**
* Process a sound import from another GRF file .
* @ param sound Destination for sound .
*/
static void ImportGRFSound ( SoundEntry * sound )
2006-10-12 09:42:37 +02:00
{
const GRFFile * file ;
2021-04-14 17:41:45 +02:00
uint32 grfid = _cur . file - > ReadDword ( ) ;
SoundID sound_id = _cur . file - > ReadWord ( ) ;
2006-10-12 09:42:37 +02:00
file = GetFileByGRFID ( grfid ) ;
2019-04-10 23:07:06 +02:00
if ( file = = nullptr | | file - > sound_offset = = 0 ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " ImportGRFSound: Source file not available " ) ;
2006-10-12 09:42:37 +02:00
return ;
}
2010-04-20 07:52:51 +02:00
if ( sound_id > = file - > num_sounds ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " ImportGRFSound: Sound effect {} is invalid " , sound_id ) ;
2006-10-12 09:42:37 +02:00
return ;
}
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " ImportGRFSound: Copying sound {} ({}) from file {:x} " , sound_id , file - > sound_offset + sound_id , grfid ) ;
2006-10-12 09:42:37 +02:00
2009-05-17 21:27:50 +02:00
* sound = * GetSound ( file - > sound_offset + sound_id ) ;
2006-10-12 09:42:37 +02:00
/* Reset volume and priority, which TTDPatch doesn't copy */
2009-05-17 21:27:50 +02:00
sound - > volume = 128 ;
sound - > priority = 0 ;
2006-10-12 09:42:37 +02:00
}
2012-03-19 23:55:29 +01:00
/**
* Load a sound from a file .
* @ param offs File offset to read sound from .
* @ param sound Destination for sound .
*/
static void LoadGRFSound ( size_t offs , SoundEntry * sound )
2006-10-12 09:42:37 +02:00
{
2012-02-04 14:28:44 +01:00
/* Set default volume and priority */
sound - > volume = 0x80 ;
sound - > priority = 0 ;
2006-10-12 09:42:37 +02:00
2012-02-04 14:29:04 +01:00
if ( offs ! = SIZE_MAX ) {
/* Sound is present in the NewGRF. */
2021-04-14 17:20:39 +02:00
sound - > file = _cur . file ;
2012-02-04 14:29:04 +01:00
sound - > file_offset = offs ;
2021-04-14 17:20:39 +02:00
sound - > grf_container_ver = _cur . file - > GetContainerVersion ( ) ;
2012-02-04 14:29:04 +01:00
}
2006-10-12 09:42:37 +02:00
}
2012-02-04 14:28:44 +01:00
/* Action 0x11 */
static void GRFSound ( ByteReader * buf )
2006-09-27 20:17:01 +02:00
{
2012-02-04 14:28:44 +01:00
/* <11> <num>
*
* W num Number of sound files that follow */
2006-09-27 20:17:01 +02:00
2012-02-04 14:28:44 +01:00
uint16 num = buf - > ReadWord ( ) ;
2012-03-19 23:55:29 +01:00
if ( num = = 0 ) return ;
2006-09-27 20:17:01 +02:00
2012-03-19 23:55:29 +01:00
SoundEntry * sound ;
2012-02-04 14:28:44 +01:00
if ( _cur . grffile - > sound_offset = = 0 ) {
_cur . grffile - > sound_offset = GetNumSounds ( ) ;
_cur . grffile - > num_sounds = num ;
2012-03-19 23:55:29 +01:00
sound = AllocateSound ( num ) ;
} else {
sound = GetSound ( _cur . grffile - > sound_offset ) ;
2006-09-27 20:17:01 +02:00
}
2021-04-14 17:41:45 +02:00
SpriteFile & file = * _cur . file ;
byte grf_container_version = file . GetContainerVersion ( ) ;
2012-02-04 14:28:44 +01:00
for ( int i = 0 ; i < num ; i + + ) {
_cur . nfo_line + + ;
2006-09-27 20:17:01 +02:00
2012-03-19 23:55:29 +01:00
/* Check whether the index is in range. This might happen if multiple action 11 are present.
* While this is invalid , we do not check for this . But we should prevent it from causing bigger trouble */
bool invalid = i > = _cur . grffile - > num_sounds ;
2021-04-14 17:41:45 +02:00
size_t offs = file . GetPos ( ) ;
2006-09-27 20:17:01 +02:00
2021-04-14 17:41:45 +02:00
uint32 len = grf_container_version > = 2 ? file . ReadDword ( ) : file . ReadWord ( ) ;
byte type = file . ReadByte ( ) ;
2006-09-27 20:17:01 +02:00
2021-04-14 17:20:39 +02:00
if ( grf_container_version > = 2 & & type = = 0xFD ) {
2012-02-04 14:29:04 +01:00
/* Reference to sprite section. */
2012-03-19 23:55:29 +01:00
if ( invalid ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " GRFSound: Sound index out of range (multiple Action 11?) " ) ;
2021-04-14 17:41:45 +02:00
file . SkipBytes ( len ) ;
2012-03-19 23:55:29 +01:00
} else if ( len ! = 4 ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " GRFSound: Invalid sprite section import " ) ;
2021-04-14 17:41:45 +02:00
file . SkipBytes ( len ) ;
2012-02-04 14:29:04 +01:00
} else {
2021-04-14 17:41:45 +02:00
uint32 id = file . ReadDword ( ) ;
2012-03-19 23:55:29 +01:00
if ( _cur . stage = = GLS_INIT ) LoadGRFSound ( GetGRFSpriteOffset ( id ) , sound + i ) ;
2012-02-04 14:29:04 +01:00
}
continue ;
}
2012-02-04 14:28:44 +01:00
if ( type ! = 0xFF ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " GRFSound: Unexpected RealSprite found, skipping " ) ;
2021-04-14 17:41:45 +02:00
file . SkipBytes ( 7 ) ;
2021-04-14 17:20:39 +02:00
SkipSpriteData ( * _cur . file , type , len - 8 ) ;
2012-02-04 14:28:44 +01:00
continue ;
}
2006-09-27 20:17:01 +02:00
2012-03-19 23:55:29 +01:00
if ( invalid ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " GRFSound: Sound index out of range (multiple Action 11?) " ) ;
2021-04-14 17:41:45 +02:00
file . SkipBytes ( len ) ;
2012-03-19 23:55:29 +01:00
}
2021-04-14 17:41:45 +02:00
byte action = file . ReadByte ( ) ;
2012-02-04 14:28:44 +01:00
switch ( action ) {
case 0xFF :
2012-02-04 14:28:48 +01:00
/* Allocate sound only in init stage. */
2012-02-04 14:29:04 +01:00
if ( _cur . stage = = GLS_INIT ) {
2021-04-14 17:20:39 +02:00
if ( grf_container_version > = 2 ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " GRFSound: Inline sounds are not supported for container version >= 2 " ) ;
2012-02-04 14:29:04 +01:00
} else {
2012-03-19 23:55:29 +01:00
LoadGRFSound ( offs , sound + i ) ;
2012-02-04 14:29:04 +01:00
}
}
2021-04-14 17:41:45 +02:00
file . SkipBytes ( len - 1 ) ; // already read <action>
2012-02-04 14:28:44 +01:00
break ;
2006-09-27 20:17:01 +02:00
2012-02-04 14:28:44 +01:00
case 0xFE :
2012-02-04 14:28:48 +01:00
if ( _cur . stage = = GLS_ACTIVATION ) {
/* XXX 'Action 0xFE' isn't really specified. It is only mentioned for
* importing sounds , so this is probably all wrong . . . */
2023-04-18 21:24:42 +02:00
if ( file . ReadByte ( ) ! = 0 ) GrfMsg ( 1 , " GRFSound: Import type mismatch " ) ;
2012-03-19 23:55:29 +01:00
ImportGRFSound ( sound + i ) ;
2012-02-04 14:28:48 +01:00
} else {
2021-04-14 17:41:45 +02:00
file . SkipBytes ( len - 1 ) ; // already read <action>
2012-02-04 14:28:48 +01:00
}
2012-02-04 14:28:44 +01:00
break ;
2006-09-27 20:17:01 +02:00
default :
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " GRFSound: Unexpected Action {:x} found, skipping " , action ) ;
2021-04-14 17:41:45 +02:00
file . SkipBytes ( len - 1 ) ; // already read <action>
2009-11-07 19:22:00 +01:00
break ;
2006-09-27 20:17:01 +02:00
}
}
2012-02-04 14:28:44 +01:00
}
/* Action 0x11 (SKIP) */
static void SkipAct11 ( ByteReader * buf )
{
/* <11> <num>
*
* W num Number of sound files that follow */
2009-11-07 19:22:00 +01:00
2012-02-04 14:28:44 +01:00
_cur . skip_sprites = buf - > ReadWord ( ) ;
2009-11-07 19:22:00 +01:00
2023-04-18 21:24:42 +02:00
GrfMsg ( 3 , " SkipAct11: Skipping {} sprites " , _cur . skip_sprites ) ;
2006-09-27 20:17:01 +02:00
}
2011-09-11 13:14:22 +02:00
/** Action 0x12 */
2010-01-22 14:02:15 +01:00
static void LoadFontGlyph ( ByteReader * buf )
2006-11-16 23:05:33 +01:00
{
/* <12> <num_def> <font_size> <num_char> <base_char>
*
* B num_def Number of definitions
2011-11-20 16:17:34 +01:00
* B font_size Size of font ( 0 = normal , 1 = small , 2 = large , 3 = mono )
2006-11-16 23:05:33 +01:00
* B num_char Number of consecutive glyphs
* W base_char First character index */
2010-01-23 14:17:30 +01:00
uint8 num_def = buf - > ReadByte ( ) ;
2006-11-16 23:05:33 +01:00
2007-03-10 10:55:11 +01:00
for ( uint i = 0 ; i < num_def ; i + + ) {
2010-01-23 14:17:30 +01:00
FontSize size = ( FontSize ) buf - > ReadByte ( ) ;
uint8 num_char = buf - > ReadByte ( ) ;
uint16 base_char = buf - > ReadWord ( ) ;
2006-11-16 23:05:33 +01:00
2011-11-20 16:17:34 +01:00
if ( size > = FS_END ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " LoadFontGlyph: Size {} is not supported, ignoring " , size ) ;
2011-11-20 16:17:34 +01:00
}
2023-04-20 17:00:46 +02:00
GrfMsg ( 7 , " LoadFontGlyph: Loading {} glyph(s) at 0x{:04X} for size {} " , num_char , base_char , size ) ;
2006-11-16 23:05:33 +01:00
2007-03-10 10:55:11 +01:00
for ( uint c = 0 ; c < num_char ; c + + ) {
2011-11-20 16:17:34 +01:00
if ( size < FS_END ) SetUnicodeGlyph ( size , base_char + c , _cur . spriteid ) ;
2011-08-21 14:05:47 +02:00
_cur . nfo_line + + ;
2021-04-14 17:20:39 +02:00
LoadNextSprite ( _cur . spriteid + + , * _cur . file , _cur . nfo_line ) ;
2006-11-16 23:05:33 +01:00
}
}
}
2011-09-11 13:14:22 +02:00
/** Action 0x12 (SKIP) */
2010-01-22 14:02:15 +01:00
static void SkipAct12 ( ByteReader * buf )
2008-01-28 16:55:59 +01:00
{
/* <12> <num_def> <font_size> <num_char> <base_char>
*
* B num_def Number of definitions
* B font_size Size of font ( 0 = normal , 1 = small , 2 = large )
* B num_char Number of consecutive glyphs
* W base_char First character index */
2010-01-23 14:17:30 +01:00
uint8 num_def = buf - > ReadByte ( ) ;
2008-01-28 16:55:59 +01:00
for ( uint i = 0 ; i < num_def ; i + + ) {
/* Ignore 'size' byte */
2010-01-23 14:17:30 +01:00
buf - > ReadByte ( ) ;
2008-01-28 16:55:59 +01:00
/* Sum up number of characters */
2011-08-21 14:05:47 +02:00
_cur . skip_sprites + = buf - > ReadByte ( ) ;
2008-01-28 16:55:59 +01:00
/* Ignore 'base_char' word */
2010-01-23 14:17:30 +01:00
buf - > ReadWord ( ) ;
2008-01-28 16:55:59 +01:00
}
2023-04-18 21:24:42 +02:00
GrfMsg ( 3 , " SkipAct12: Skipping {} sprites " , _cur . skip_sprites ) ;
2008-01-28 16:55:59 +01:00
}
2011-09-11 13:14:22 +02:00
/** Action 0x13 */
2010-01-22 14:02:15 +01:00
static void TranslateGRFStrings ( ByteReader * buf )
2007-03-06 23:00:13 +01:00
{
/* <13> <grfid> <num-ent> <offset> <text...>
*
* 4 * B grfid The GRFID of the file whose texts are to be translated
* B num - ent Number of strings
* W offset First text ID
* S text . . . Zero - terminated strings */
2010-01-23 14:17:30 +01:00
uint32 grfid = buf - > ReadDWord ( ) ;
2007-03-06 23:00:13 +01:00
const GRFConfig * c = GetGRFConfig ( grfid ) ;
2019-04-10 23:07:06 +02:00
if ( c = = nullptr | | ( c - > status ! = GCS_INITIALISED & & c - > status ! = GCS_ACTIVATED ) ) {
2023-04-20 17:00:46 +02:00
GrfMsg ( 7 , " TranslateGRFStrings: GRFID 0x{:08X} unknown, skipping action 13 " , BSWAP32 ( grfid ) ) ;
2007-03-06 23:00:13 +01:00
return ;
}
if ( c - > status = = GCS_INITIALISED ) {
/* If the file is not active but will be activated later, give an error
* and disable this file . */
2011-05-11 22:22:04 +02:00
GRFError * error = DisableGrf ( STR_NEWGRF_ERROR_LOAD_AFTER ) ;
2007-06-12 15:22:14 +02:00
2021-05-14 18:45:54 +02:00
error - > data = GetString ( STR_NEWGRF_ERROR_AFTER_TRANSLATED_FILE ) ;
2007-03-06 23:00:13 +01:00
return ;
}
2011-12-01 20:42:51 +01:00
/* Since no language id is supplied for with version 7 and lower NewGRFs, this string has
* to be added as a generic string , thus the language id of 0x7F . For this to work
* new_scheme has to be true as well , which will also be implicitly the case for version 8
* and higher . A language id of 0x7F will be overridden by a non - generic id , so this will
* not change anything if a string has been provided specifically for this language . */
byte language = _cur . grffile - > grf_version > = 8 ? buf - > ReadByte ( ) : 0x7F ;
2010-01-23 14:17:30 +01:00
byte num_strings = buf - > ReadByte ( ) ;
uint16 first_id = buf - > ReadWord ( ) ;
2007-03-06 23:00:13 +01:00
2017-03-05 15:45:13 +01:00
if ( ! ( ( first_id > = 0xD000 & & first_id + num_strings < = 0xD400 ) | | ( first_id > = 0xD800 & & first_id + num_strings < = 0xE000 ) ) ) {
2023-04-20 17:00:46 +02:00
GrfMsg ( 7 , " TranslateGRFStrings: Attempting to set out-of-range string IDs in action 13 (first: 0x{:04X}, number: 0x{:02X}) " , first_id , num_strings ) ;
2007-03-06 23:00:13 +01:00
return ;
}
2010-01-22 14:02:15 +01:00
for ( uint i = 0 ; i < num_strings & & buf - > HasData ( ) ; i + + ) {
2010-01-23 14:17:30 +01:00
const char * string = buf - > ReadString ( ) ;
2007-03-06 23:00:13 +01:00
2010-01-22 14:02:15 +01:00
if ( StrEmpty ( string ) ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 7 , " TranslateGRFString: Ignoring empty string. " ) ;
2007-03-06 23:00:13 +01:00
continue ;
}
2011-12-01 20:42:51 +01:00
AddGRFString ( grfid , first_id + i , language , true , true , string , STR_UNDEFINED ) ;
2007-03-06 23:00:13 +01:00
}
}
2010-07-31 11:35:42 +02:00
/** Callback function for 'INFO'->'NAME' to add a translation to the newgrf name. */
static bool ChangeGRFName ( byte langid , const char * str )
{
2020-05-17 23:31:47 +02:00
AddGRFTextToList ( _cur . grfconfig - > name , langid , _cur . grfconfig - > ident . grfid , false , str ) ;
2010-07-31 11:35:42 +02:00
return true ;
}
/** Callback function for 'INFO'->'DESC' to add a translation to the newgrf description. */
static bool ChangeGRFDescription ( byte langid , const char * str )
{
2020-05-17 23:31:47 +02:00
AddGRFTextToList ( _cur . grfconfig - > info , langid , _cur . grfconfig - > ident . grfid , true , str ) ;
2010-07-31 11:35:42 +02:00
return true ;
}
2011-12-11 13:55:04 +01:00
/** Callback function for 'INFO'->'URL_' to set the newgrf url. */
static bool ChangeGRFURL ( byte langid , const char * str )
{
2020-05-17 23:31:47 +02:00
AddGRFTextToList ( _cur . grfconfig - > url , langid , _cur . grfconfig - > ident . grfid , false , str ) ;
2011-12-11 13:55:04 +01:00
return true ;
}
2010-07-31 11:35:49 +02:00
/** Callback function for 'INFO'->'NPAR' to set the number of valid parameters. */
static bool ChangeGRFNumUsedParams ( size_t len , ByteReader * buf )
{
if ( len ! = 1 ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " StaticGRFInfo: expected only 1 byte for 'INFO'->'NPAR' but got {}, ignoring this field " , len ) ;
2010-07-31 11:35:49 +02:00
buf - > Skip ( len ) ;
} else {
2023-05-18 20:56:24 +02:00
_cur . grfconfig - > num_valid_params = std : : min ( buf - > ReadByte ( ) , ClampTo < uint8_t > ( _cur . grfconfig - > param . size ( ) ) ) ;
2010-07-31 11:35:49 +02:00
}
return true ;
}
2010-07-31 11:44:38 +02:00
/** Callback function for 'INFO'->'PALS' to set the number of valid parameters. */
static bool ChangeGRFPalette ( size_t len , ByteReader * buf )
{
if ( len ! = 1 ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " StaticGRFInfo: expected only 1 byte for 'INFO'->'PALS' but got {}, ignoring this field " , len ) ;
2010-07-31 11:44:38 +02:00
buf - > Skip ( len ) ;
} else {
char data = buf - > ReadByte ( ) ;
2011-05-03 22:19:57 +02:00
GRFPalette pal = GRFP_GRF_UNSET ;
2010-07-31 11:44:38 +02:00
switch ( data ) {
case ' * ' :
2011-05-03 22:19:57 +02:00
case ' A ' : pal = GRFP_GRF_ANY ; break ;
case ' W ' : pal = GRFP_GRF_WINDOWS ; break ;
case ' D ' : pal = GRFP_GRF_DOS ; break ;
2010-07-31 11:44:38 +02:00
default :
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " StaticGRFInfo: unexpected value '{:02X}' for 'INFO'->'PALS', ignoring this field " , data ) ;
2010-07-31 11:44:38 +02:00
break ;
}
2011-05-03 22:19:57 +02:00
if ( pal ! = GRFP_GRF_UNSET ) {
2011-08-21 14:05:47 +02:00
_cur . grfconfig - > palette & = ~ GRFP_GRF_MASK ;
_cur . grfconfig - > palette | = pal ;
2011-05-03 22:19:57 +02:00
}
2010-07-31 11:44:38 +02:00
}
return true ;
}
2011-10-04 23:35:52 +02:00
/** Callback function for 'INFO'->'BLTR' to set the blitter info. */
static bool ChangeGRFBlitter ( size_t len , ByteReader * buf )
{
if ( len ! = 1 ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " StaticGRFInfo: expected only 1 byte for 'INFO'->'BLTR' but got {}, ignoring this field " , len ) ;
2011-10-04 23:35:52 +02:00
buf - > Skip ( len ) ;
} else {
char data = buf - > ReadByte ( ) ;
GRFPalette pal = GRFP_BLT_UNSET ;
switch ( data ) {
case ' 8 ' : pal = GRFP_BLT_UNSET ; break ;
case ' 3 ' : pal = GRFP_BLT_32BPP ; break ;
default :
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " StaticGRFInfo: unexpected value '{:02X}' for 'INFO'->'BLTR', ignoring this field " , data ) ;
2011-10-04 23:35:52 +02:00
return true ;
}
_cur . grfconfig - > palette & = ~ GRFP_BLT_MASK ;
_cur . grfconfig - > palette | = pal ;
}
return true ;
}
2010-07-31 16:40:50 +02:00
/** Callback function for 'INFO'->'VRSN' to the version of the NewGRF. */
static bool ChangeGRFVersion ( size_t len , ByteReader * buf )
{
if ( len ! = 4 ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " StaticGRFInfo: expected 4 bytes for 'INFO'->'VRSN' but got {}, ignoring this field " , len ) ;
2010-07-31 16:40:50 +02:00
buf - > Skip ( len ) ;
} else {
2010-10-17 14:14:49 +02:00
/* Set min_loadable_version as well (default to minimal compatibility) */
2011-08-21 14:05:47 +02:00
_cur . grfconfig - > version = _cur . grfconfig - > min_loadable_version = buf - > ReadDWord ( ) ;
2010-07-31 16:40:50 +02:00
}
return true ;
}
2010-10-17 14:14:49 +02:00
/** Callback function for 'INFO'->'MINV' to the minimum compatible version of the NewGRF. */
static bool ChangeGRFMinVersion ( size_t len , ByteReader * buf )
{
if ( len ! = 4 ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " StaticGRFInfo: expected 4 bytes for 'INFO'->'MINV' but got {}, ignoring this field " , len ) ;
2010-10-17 14:14:49 +02:00
buf - > Skip ( len ) ;
} else {
2011-08-21 14:05:47 +02:00
_cur . grfconfig - > min_loadable_version = buf - > ReadDWord ( ) ;
if ( _cur . grfconfig - > version = = 0 ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " StaticGRFInfo: 'MINV' defined before 'VRSN' or 'VRSN' set to 0, ignoring this field " ) ;
2011-08-21 14:05:47 +02:00
_cur . grfconfig - > min_loadable_version = 0 ;
2010-10-17 14:14:49 +02:00
}
2011-08-21 14:05:47 +02:00
if ( _cur . grfconfig - > version < _cur . grfconfig - > min_loadable_version ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " StaticGRFInfo: 'MINV' defined as {}, limiting it to 'VRSN' " , _cur . grfconfig - > min_loadable_version ) ;
2011-08-21 14:05:47 +02:00
_cur . grfconfig - > min_loadable_version = _cur . grfconfig - > version ;
2010-10-17 14:14:49 +02:00
}
}
return true ;
}
2010-07-31 11:51:04 +02:00
static GRFParameterInfo * _cur_parameter ; ///< The parameter which info is currently changed by the newgrf.
/** Callback function for 'INFO'->'PARAM'->param_num->'NAME' to set the name of a parameter. */
static bool ChangeGRFParamName ( byte langid , const char * str )
{
2020-05-17 23:31:47 +02:00
AddGRFTextToList ( _cur_parameter - > name , langid , _cur . grfconfig - > ident . grfid , false , str ) ;
2010-07-31 11:51:04 +02:00
return true ;
}
/** Callback function for 'INFO'->'PARAM'->param_num->'DESC' to set the description of a parameter. */
static bool ChangeGRFParamDescription ( byte langid , const char * str )
{
2020-05-17 23:31:47 +02:00
AddGRFTextToList ( _cur_parameter - > desc , langid , _cur . grfconfig - > ident . grfid , true , str ) ;
2010-07-31 11:51:04 +02:00
return true ;
}
/** Callback function for 'INFO'->'PARAM'->param_num->'TYPE' to set the typeof a parameter. */
static bool ChangeGRFParamType ( size_t len , ByteReader * buf )
{
if ( len ! = 1 ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " StaticGRFInfo: expected 1 byte for 'INFO'->'PARA'->'TYPE' but got {}, ignoring this field " , len ) ;
2010-07-31 11:51:04 +02:00
buf - > Skip ( len ) ;
} else {
GRFParameterType type = ( GRFParameterType ) buf - > ReadByte ( ) ;
if ( type < PTYPE_END ) {
_cur_parameter - > type = type ;
} else {
2023-04-18 21:24:42 +02:00
GrfMsg ( 3 , " StaticGRFInfo: unknown parameter type {}, ignoring this field " , type ) ;
2010-07-31 11:51:04 +02:00
}
}
return true ;
}
/** Callback function for 'INFO'->'PARAM'->param_num->'LIMI' to set the min/max value of a parameter. */
static bool ChangeGRFParamLimits ( size_t len , ByteReader * buf )
{
if ( _cur_parameter - > type ! = PTYPE_UINT_ENUM ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " StaticGRFInfo: 'INFO'->'PARA'->'LIMI' is only valid for parameters with type uint/enum, ignoring this field " ) ;
2010-07-31 11:51:04 +02:00
buf - > Skip ( len ) ;
} else if ( len ! = 8 ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " StaticGRFInfo: expected 8 bytes for 'INFO'->'PARA'->'LIMI' but got {}, ignoring this field " , len ) ;
2010-07-31 11:51:04 +02:00
buf - > Skip ( len ) ;
} else {
2019-11-23 14:21:01 +01:00
uint32 min_value = buf - > ReadDWord ( ) ;
uint32 max_value = buf - > ReadDWord ( ) ;
if ( min_value < = max_value ) {
_cur_parameter - > min_value = min_value ;
_cur_parameter - > max_value = max_value ;
} else {
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " StaticGRFInfo: 'INFO'->'PARA'->'LIMI' values are incoherent, ignoring this field " ) ;
2019-11-23 14:21:01 +01:00
}
2010-07-31 11:51:04 +02:00
}
return true ;
}
/** Callback function for 'INFO'->'PARAM'->param_num->'MASK' to set the parameter and bits to use. */
static bool ChangeGRFParamMask ( size_t len , ByteReader * buf )
{
if ( len < 1 | | len > 3 ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " StaticGRFInfo: expected 1 to 3 bytes for 'INFO'->'PARA'->'MASK' but got {}, ignoring this field " , len ) ;
2010-07-31 11:51:04 +02:00
buf - > Skip ( len ) ;
} else {
byte param_nr = buf - > ReadByte ( ) ;
2023-05-18 20:56:24 +02:00
if ( param_nr > = _cur . grfconfig - > param . size ( ) ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " StaticGRFInfo: invalid parameter number in 'INFO'->'PARA'->'MASK', param {}, ignoring this field " , param_nr ) ;
2010-07-31 11:51:04 +02:00
buf - > Skip ( len - 1 ) ;
} else {
_cur_parameter - > param_nr = param_nr ;
2021-01-08 11:16:18 +01:00
if ( len > = 2 ) _cur_parameter - > first_bit = std : : min < byte > ( buf - > ReadByte ( ) , 31 ) ;
if ( len > = 3 ) _cur_parameter - > num_bit = std : : min < byte > ( buf - > ReadByte ( ) , 32 - _cur_parameter - > first_bit ) ;
2010-07-31 11:51:04 +02:00
}
}
return true ;
}
2010-08-23 23:53:55 +02:00
/** Callback function for 'INFO'->'PARAM'->param_num->'DFLT' to set the default value. */
2010-08-23 23:47:07 +02:00
static bool ChangeGRFParamDefault ( size_t len , ByteReader * buf )
{
if ( len ! = 4 ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " StaticGRFInfo: expected 4 bytes for 'INFO'->'PARA'->'DEFA' but got {}, ignoring this field " , len ) ;
2010-08-23 23:47:07 +02:00
buf - > Skip ( len ) ;
} else {
_cur_parameter - > def_value = buf - > ReadDWord ( ) ;
}
2011-08-21 14:05:47 +02:00
_cur . grfconfig - > has_param_defaults = true ;
2010-08-23 23:47:07 +02:00
return true ;
}
2010-07-31 11:51:04 +02:00
2010-07-31 11:35:42 +02:00
typedef bool ( * DataHandler ) ( size_t , ByteReader * ) ; ///< Type of callback function for binary nodes
typedef bool ( * TextHandler ) ( byte , const char * str ) ; ///< Type of callback function for text nodes
typedef bool ( * BranchHandler ) ( ByteReader * ) ; ///< Type of callback function for branch nodes
/**
* Data structure to store the allowed id / type combinations for action 14. The
* data can be represented as a tree with 3 types of nodes :
* 1. Branch nodes ( identified by ' C ' for choice ) .
* 2. Binary leaf nodes ( identified by ' B ' ) .
* 3. Text leaf nodes ( identified by ' T ' ) .
*/
struct AllowedSubtags {
/** Create empty subtags object used to identify the end of a list. */
AllowedSubtags ( ) :
id ( 0 ) ,
type ( 0 )
{ }
/**
* Create a binary leaf node .
* @ param id The id for this node .
* @ param handler The callback function to call .
*/
AllowedSubtags ( uint32 id , DataHandler handler ) :
id ( id ) ,
type ( ' B ' )
{
this - > handler . data = handler ;
}
/**
* Create a text leaf node .
* @ param id The id for this node .
* @ param handler The callback function to call .
*/
AllowedSubtags ( uint32 id , TextHandler handler ) :
id ( id ) ,
type ( ' T ' )
{
this - > handler . text = handler ;
}
/**
* Create a branch node with a callback handler
* @ param id The id for this node .
* @ param handler The callback function to call .
*/
AllowedSubtags ( uint32 id , BranchHandler handler ) :
id ( id ) ,
type ( ' C ' )
{
this - > handler . call_handler = true ;
this - > handler . u . branch = handler ;
}
/**
* Create a branch node with a list of sub - nodes .
* @ param id The id for this node .
* @ param subtags Array with all valid subtags .
*/
AllowedSubtags ( uint32 id , AllowedSubtags * subtags ) :
id ( id ) ,
type ( ' C ' )
{
this - > handler . call_handler = false ;
this - > handler . u . subtags = subtags ;
}
uint32 id ; ///< The identifier for this node
byte type ; ///< The type of the node, must be one of 'C', 'B' or 'T'.
union {
DataHandler data ; ///< Callback function for a binary node, only valid if type == 'B'.
TextHandler text ; ///< Callback function for a text node, only valid if type == 'T'.
struct {
union {
BranchHandler branch ; ///< Callback function for a branch node, only valid if type == 'C' && call_handler.
AllowedSubtags * subtags ; ///< Pointer to a list of subtags, only valid if type == 'C' && !call_handler.
} u ;
bool call_handler ; ///< True if there is a callback function for this node, false if there is a list of subnodes.
} ;
} handler ;
} ;
2010-07-31 11:51:04 +02:00
static bool SkipUnknownInfo ( ByteReader * buf , byte type ) ;
2010-07-31 14:05:41 +02:00
static bool HandleNodes ( ByteReader * buf , AllowedSubtags * tags ) ;
2010-07-31 11:51:04 +02:00
/**
* Callback function for ' INFO ' - > ' PARA ' - > param_num - > ' VALU ' to set the names
* of some parameter values ( type uint / enum ) or the names of some bits
* ( type bitmask ) . In both cases the format is the same :
* Each subnode should be a text node with the value / bit number as id .
*/
static bool ChangeGRFParamValueNames ( ByteReader * buf )
{
byte type = buf - > ReadByte ( ) ;
while ( type ! = 0 ) {
uint32 id = buf - > ReadDWord ( ) ;
if ( type ! = ' T ' | | id > _cur_parameter - > max_value ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " StaticGRFInfo: all child nodes of 'INFO'->'PARA'->param_num->'VALU' should have type 't' and the value/bit number as id " ) ;
2010-07-31 11:51:04 +02:00
if ( ! SkipUnknownInfo ( buf , type ) ) return false ;
2011-02-24 19:13:59 +01:00
type = buf - > ReadByte ( ) ;
continue ;
2010-07-31 11:51:04 +02:00
}
byte langid = buf - > ReadByte ( ) ;
const char * name_string = buf - > ReadString ( ) ;
2023-05-16 21:50:41 +02:00
auto val_name = _cur_parameter - > value_names . find ( id ) ;
if ( val_name ! = _cur_parameter - > value_names . end ( ) ) {
2020-05-17 23:31:47 +02:00
AddGRFTextToList ( val_name - > second , langid , _cur . grfconfig - > ident . grfid , false , name_string ) ;
2010-07-31 11:51:04 +02:00
} else {
2020-05-17 23:31:47 +02:00
GRFTextList list ;
AddGRFTextToList ( list , langid , _cur . grfconfig - > ident . grfid , false , name_string ) ;
2023-05-16 21:50:41 +02:00
_cur_parameter - > value_names [ id ] = list ;
2010-07-31 11:51:04 +02:00
}
type = buf - > ReadByte ( ) ;
}
return true ;
}
2011-09-11 13:14:22 +02:00
/** Action14 parameter tags */
2010-07-31 11:51:04 +02:00
AllowedSubtags _tags_parameters [ ] = {
AllowedSubtags ( ' NAME ' , ChangeGRFParamName ) ,
AllowedSubtags ( ' DESC ' , ChangeGRFParamDescription ) ,
AllowedSubtags ( ' TYPE ' , ChangeGRFParamType ) ,
AllowedSubtags ( ' LIMI ' , ChangeGRFParamLimits ) ,
AllowedSubtags ( ' MASK ' , ChangeGRFParamMask ) ,
AllowedSubtags ( ' VALU ' , ChangeGRFParamValueNames ) ,
2010-08-23 23:53:55 +02:00
AllowedSubtags ( ' DFLT ' , ChangeGRFParamDefault ) ,
2010-07-31 11:51:04 +02:00
AllowedSubtags ( )
} ;
/**
* Callback function for ' INFO ' - > ' PARA ' to set extra information about the
* parameters . Each subnode of ' INFO ' - > ' PARA ' should be a branch node with
* the parameter number as id . The first parameter has id 0. The maximum
* parameter that can be changed is set by ' INFO ' - > ' NPAR ' which defaults to 80.
*/
static bool HandleParameterInfo ( ByteReader * buf )
{
byte type = buf - > ReadByte ( ) ;
while ( type ! = 0 ) {
uint32 id = buf - > ReadDWord ( ) ;
2011-08-21 14:05:47 +02:00
if ( type ! = ' C ' | | id > = _cur . grfconfig - > num_valid_params ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " StaticGRFInfo: all child nodes of 'INFO'->'PARA' should have type 'C' and their parameter number as id " ) ;
2011-02-24 19:13:59 +01:00
if ( ! SkipUnknownInfo ( buf , type ) ) return false ;
type = buf - > ReadByte ( ) ;
continue ;
2010-07-31 11:51:04 +02:00
}
2018-09-23 13:23:54 +02:00
if ( id > = _cur . grfconfig - > param_info . size ( ) ) {
2019-02-18 23:39:06 +01:00
_cur . grfconfig - > param_info . resize ( id + 1 ) ;
2010-07-31 11:51:04 +02:00
}
2023-05-18 13:19:51 +02:00
if ( ! _cur . grfconfig - > param_info [ id ] . has_value ( ) ) {
_cur . grfconfig - > param_info [ id ] = GRFParameterInfo ( id ) ;
2010-07-31 11:51:04 +02:00
}
2023-05-18 13:19:51 +02:00
_cur_parameter = & _cur . grfconfig - > param_info [ id ] . value ( ) ;
2010-07-31 11:51:04 +02:00
/* Read all parameter-data and process each node. */
2010-07-31 14:05:41 +02:00
if ( ! HandleNodes ( buf , _tags_parameters ) ) return false ;
2010-07-31 11:51:04 +02:00
type = buf - > ReadByte ( ) ;
}
return true ;
}
2011-09-11 13:14:22 +02:00
/** Action14 tags for the INFO node */
2010-07-31 11:35:42 +02:00
AllowedSubtags _tags_info [ ] = {
AllowedSubtags ( ' NAME ' , ChangeGRFName ) ,
AllowedSubtags ( ' DESC ' , ChangeGRFDescription ) ,
2011-12-11 13:55:04 +01:00
AllowedSubtags ( ' URL_ ' , ChangeGRFURL ) ,
2010-07-31 11:35:49 +02:00
AllowedSubtags ( ' NPAR ' , ChangeGRFNumUsedParams ) ,
2010-07-31 11:44:38 +02:00
AllowedSubtags ( ' PALS ' , ChangeGRFPalette ) ,
2011-10-04 23:35:52 +02:00
AllowedSubtags ( ' BLTR ' , ChangeGRFBlitter ) ,
2010-07-31 16:40:50 +02:00
AllowedSubtags ( ' VRSN ' , ChangeGRFVersion ) ,
2010-10-17 14:14:49 +02:00
AllowedSubtags ( ' MINV ' , ChangeGRFMinVersion ) ,
2010-07-31 11:51:04 +02:00
AllowedSubtags ( ' PARA ' , HandleParameterInfo ) ,
2010-07-31 11:35:42 +02:00
AllowedSubtags ( )
} ;
2011-09-11 13:14:22 +02:00
/** Action14 root tags */
2010-07-31 11:35:42 +02:00
AllowedSubtags _tags_root [ ] = {
AllowedSubtags ( ' INFO ' , _tags_info ) ,
AllowedSubtags ( )
} ;
/**
* Try to skip the current node and all subnodes ( if it ' s a branch node ) .
2011-09-11 13:14:22 +02:00
* @ param buf Buffer .
* @ param type The node type to skip .
2014-04-12 17:59:56 +02:00
* @ return True if we could skip the node , false if an error occurred .
2010-07-31 11:35:42 +02:00
*/
static bool SkipUnknownInfo ( ByteReader * buf , byte type )
{
/* type and id are already read */
switch ( type ) {
case ' C ' : {
byte new_type = buf - > ReadByte ( ) ;
while ( new_type ! = 0 ) {
buf - > ReadDWord ( ) ; // skip the id
if ( ! SkipUnknownInfo ( buf , new_type ) ) return false ;
new_type = buf - > ReadByte ( ) ;
}
break ;
}
case ' T ' :
buf - > ReadByte ( ) ; // lang
buf - > ReadString ( ) ; // actual text
break ;
case ' B ' : {
uint16 size = buf - > ReadWord ( ) ;
buf - > Skip ( size ) ;
break ;
}
default :
return false ;
}
return true ;
}
2011-09-11 13:14:22 +02:00
/**
* Handle the nodes of an Action14
* @ param type Type of node .
* @ param id ID .
* @ param buf Buffer .
* @ param subtags Allowed subtags .
* @ return Whether all tags could be handled .
*/
2010-07-31 11:35:42 +02:00
static bool HandleNode ( byte type , uint32 id , ByteReader * buf , AllowedSubtags subtags [ ] )
{
uint i = 0 ;
AllowedSubtags * tag ;
while ( ( tag = & subtags [ i + + ] ) - > type ! = 0 ) {
if ( tag - > id ! = BSWAP32 ( id ) | | tag - > type ! = type ) continue ;
switch ( type ) {
default : NOT_REACHED ( ) ;
case ' T ' : {
byte langid = buf - > ReadByte ( ) ;
return tag - > handler . text ( langid , buf - > ReadString ( ) ) ;
}
case ' B ' : {
size_t len = buf - > ReadWord ( ) ;
if ( buf - > Remaining ( ) < len ) return false ;
return tag - > handler . data ( len , buf ) ;
}
case ' C ' : {
if ( tag - > handler . call_handler ) {
return tag - > handler . u . branch ( buf ) ;
}
2010-07-31 14:05:41 +02:00
return HandleNodes ( buf , tag - > handler . u . subtags ) ;
2010-07-31 11:35:42 +02:00
}
}
}
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " StaticGRFInfo: unknown type/id combination found, type={:c}, id={:x} " , type , id ) ;
2010-07-31 11:35:42 +02:00
return SkipUnknownInfo ( buf , type ) ;
}
2011-09-11 13:14:22 +02:00
/**
* Handle the contents of a ' C ' choice of an Action14
* @ param buf Buffer .
* @ param subtags List of subtags .
* @ return Whether the nodes could all be handled .
*/
2010-07-31 14:05:41 +02:00
static bool HandleNodes ( ByteReader * buf , AllowedSubtags subtags [ ] )
{
byte type = buf - > ReadByte ( ) ;
while ( type ! = 0 ) {
uint32 id = buf - > ReadDWord ( ) ;
if ( ! HandleNode ( type , id , buf , subtags ) ) return false ;
type = buf - > ReadByte ( ) ;
}
return true ;
}
2011-09-11 13:14:22 +02:00
/**
* Handle Action 0x14
* @ param buf Buffer .
*/
2010-07-31 11:35:42 +02:00
static void StaticGRFInfo ( ByteReader * buf )
{
/* <14> <type> <id> <text/data...> */
2010-07-31 14:05:41 +02:00
HandleNodes ( buf , _tags_root ) ;
2010-07-31 11:35:42 +02:00
}
2011-09-11 13:14:22 +02:00
/**
* Set the current NewGRF as unsafe for static use
* @ param buf Unused .
* @ note Used during safety scan on unsafe actions .
*/
2010-01-22 14:02:15 +01:00
static void GRFUnsafe ( ByteReader * buf )
2006-12-12 20:38:41 +01:00
{
2011-08-21 14:05:47 +02:00
SetBit ( _cur . grfconfig - > flags , GCF_UNSAFE ) ;
2006-12-12 20:38:41 +01:00
2007-01-03 12:33:54 +01:00
/* Skip remainder of GRF */
2011-08-21 14:05:47 +02:00
_cur . skip_sprites = - 1 ;
2006-12-12 20:38:41 +01:00
}
2011-09-11 13:14:22 +02:00
/** Initialize the TTDPatch flags */
2007-03-07 12:47:46 +01:00
static void InitializeGRFSpecial ( )
2004-11-12 20:03:37 +01:00
{
2022-11-04 08:15:59 +01:00
_ttdpatch_flags [ 0 ] = ( ( _settings_game . station . never_expire_airports ? 1U : 0U ) < < 0x0C ) // keepsmallairport
| ( 1U < < 0x0D ) // newairports
| ( 1U < < 0x0E ) // largestations
| ( ( _settings_game . construction . max_bridge_length > 16 ? 1U : 0U ) < < 0x0F ) // longbridges
| ( 0U < < 0x10 ) // loadtime
| ( 1U < < 0x12 ) // presignals
| ( 1U < < 0x13 ) // extpresignals
| ( ( _settings_game . vehicle . never_expire_vehicles ? 1U : 0U ) < < 0x16 ) // enginespersist
| ( 1U < < 0x1B ) // multihead
| ( 1U < < 0x1D ) // lowmemory
| ( 1U < < 0x1E ) ; // generalfixes
_ttdpatch_flags [ 1 ] = ( ( _settings_game . economy . station_noise_level ? 1U : 0U ) < < 0x07 ) // moreairports - based on units of noise
| ( 1U < < 0x08 ) // mammothtrains
| ( 1U < < 0x09 ) // trainrefit
| ( 0U < < 0x0B ) // subsidiaries
| ( ( _settings_game . order . gradual_loading ? 1U : 0U ) < < 0x0C ) // gradualloading
| ( 1U < < 0x12 ) // unifiedmaglevmode - set bit 0 mode. Not revelant to OTTD
| ( 1U < < 0x13 ) // unifiedmaglevmode - set bit 1 mode
| ( 1U < < 0x14 ) // bridgespeedlimits
| ( 1U < < 0x16 ) // eternalgame
| ( 1U < < 0x17 ) // newtrains
| ( 1U < < 0x18 ) // newrvs
| ( 1U < < 0x19 ) // newships
| ( 1U < < 0x1A ) // newplanes
| ( ( _settings_game . construction . train_signal_side = = 1 ? 1U : 0U ) < < 0x1B ) // signalsontrafficside
| ( ( _settings_game . vehicle . disable_elrails ? 0U : 1U ) < < 0x1C ) ; // electrifiedrailway
_ttdpatch_flags [ 2 ] = ( 1U < < 0x01 ) // loadallgraphics - obsolote
| ( 1U < < 0x03 ) // semaphores
| ( 1U < < 0x0A ) // newobjects
| ( 0U < < 0x0B ) // enhancedgui
| ( 0U < < 0x0C ) // newagerating
| ( ( _settings_game . construction . build_on_slopes ? 1U : 0U ) < < 0x0D ) // buildonslopes
| ( 1U < < 0x0E ) // fullloadany
| ( 1U < < 0x0F ) // planespeed
| ( 0U < < 0x10 ) // moreindustriesperclimate - obsolete
| ( 0U < < 0x11 ) // moretoylandfeatures
| ( 1U < < 0x12 ) // newstations
| ( 1U < < 0x13 ) // tracktypecostdiff
| ( 1U < < 0x14 ) // manualconvert
| ( ( _settings_game . construction . build_on_slopes ? 1U : 0U ) < < 0x15 ) // buildoncoasts
| ( 1U < < 0x16 ) // canals
| ( 1U < < 0x17 ) // newstartyear
| ( ( _settings_game . vehicle . freight_trains > 1 ? 1U : 0U ) < < 0x18 ) // freighttrains
| ( 1U < < 0x19 ) // newhouses
| ( 1U < < 0x1A ) // newbridges
| ( 1U < < 0x1B ) // newtownnames
| ( 1U < < 0x1C ) // moreanimation
| ( ( _settings_game . vehicle . wagon_speed_limits ? 1U : 0U ) < < 0x1D ) // wagonspeedlimits
| ( 1U < < 0x1E ) // newshistory
| ( 0U < < 0x1F ) ; // custombridgeheads
_ttdpatch_flags [ 3 ] = ( 0U < < 0x00 ) // newcargodistribution
| ( 1U < < 0x01 ) // windowsnap
| ( ( _settings_game . economy . allow_town_roads | | _generating_world ? 0U : 1U ) < < 0x02 ) // townbuildnoroad
| ( 1U < < 0x03 ) // pathbasedsignalling
| ( 0U < < 0x04 ) // aichoosechance
| ( 1U < < 0x05 ) // resolutionwidth
| ( 1U < < 0x06 ) // resolutionheight
| ( 1U < < 0x07 ) // newindustries
| ( ( _settings_game . order . improved_load ? 1U : 0U ) < < 0x08 ) // fifoloading
| ( 0U < < 0x09 ) // townroadbranchprob
| ( 0U < < 0x0A ) // tempsnowline
| ( 1U < < 0x0B ) // newcargo
| ( 1U < < 0x0C ) // enhancemultiplayer
| ( 1U < < 0x0D ) // onewayroads
| ( 1U < < 0x0E ) // irregularstations
| ( 1U < < 0x0F ) // statistics
| ( 1U < < 0x10 ) // newsounds
| ( 1U < < 0x11 ) // autoreplace
| ( 1U < < 0x12 ) // autoslope
| ( 0U < < 0x13 ) // followvehicle
| ( 1U < < 0x14 ) // trams
| ( 0U < < 0x15 ) // enhancetunnels
| ( 1U < < 0x16 ) // shortrvs
| ( 1U < < 0x17 ) // articulatedrvs
| ( ( _settings_game . vehicle . dynamic_engines ? 1U : 0U ) < < 0x18 ) // dynamic engines
| ( 1U < < 0x1E ) // variablerunningcosts
| ( 1U < < 0x1F ) ; // any switch is on
_ttdpatch_flags [ 4 ] = ( 1U < < 0x00 ) // larger persistent storage
2023-01-22 18:34:35 +01:00
| ( ( _settings_game . economy . inflation ? 1U : 0U ) < < 0x01 ) // inflation is on
| ( 1U < < 0x02 ) ; // extended string range
2004-11-12 20:03:37 +01:00
}
2011-09-11 13:14:22 +02:00
/** Reset and clear all NewGRF stations */
2007-03-07 12:47:46 +01:00
static void ResetCustomStations ( )
2005-11-12 01:19:34 +01:00
{
2019-02-17 12:20:52 +01:00
for ( GRFFile * const file : _grf_files ) {
2023-01-08 03:12:33 +01:00
file - > stations . clear ( ) ;
2005-11-12 01:19:34 +01:00
}
}
2011-09-11 13:14:22 +02:00
/** Reset and clear all NewGRF houses */
2007-03-19 12:27:30 +01:00
static void ResetCustomHouses ( )
{
2019-02-17 12:20:52 +01:00
for ( GRFFile * const file : _grf_files ) {
2023-01-08 03:12:33 +01:00
file - > housespec . clear ( ) ;
2007-03-19 12:27:30 +01:00
}
}
2011-09-11 13:14:22 +02:00
/** Reset and clear all NewGRF airports */
2010-02-22 15:16:19 +01:00
static void ResetCustomAirports ( )
{
2019-02-17 12:20:52 +01:00
for ( GRFFile * const file : _grf_files ) {
2023-04-15 22:30:37 +02:00
for ( auto & as : file - > airportspec ) {
2023-01-08 03:12:33 +01:00
if ( as ! = nullptr ) {
/* We need to remove the tiles layouts */
for ( int j = 0 ; j < as - > num_table ; j + + ) {
/* remove the individual layouts */
free ( as - > table [ j ] ) ;
2010-03-19 00:15:38 +01:00
}
2023-01-08 03:12:33 +01:00
free ( as - > table ) ;
free ( as - > depot_table ) ;
free ( as - > rotation ) ;
2010-03-19 00:15:38 +01:00
}
}
2023-01-08 03:12:33 +01:00
file - > airportspec . clear ( ) ;
file - > airtspec . clear ( ) ;
2010-02-22 15:16:19 +01:00
}
}
2011-09-11 13:14:22 +02:00
/** Reset and clear all NewGRF industries */
2007-05-31 05:35:11 +02:00
static void ResetCustomIndustries ( )
{
2019-02-17 12:20:52 +01:00
for ( GRFFile * const file : _grf_files ) {
2023-01-08 03:12:33 +01:00
file - > industryspec . clear ( ) ;
file - > indtspec . clear ( ) ;
2007-05-31 05:35:11 +02:00
}
}
2011-09-11 13:14:22 +02:00
/** Reset and clear all NewObjects */
2010-08-28 20:21:09 +02:00
static void ResetCustomObjects ( )
{
2019-02-17 12:20:52 +01:00
for ( GRFFile * const file : _grf_files ) {
2023-01-08 03:12:33 +01:00
file - > objectspec . clear ( ) ;
2010-08-28 20:21:09 +02:00
}
}
2022-11-06 16:01:27 +01:00
static void ResetCustomRoadStops ( )
{
for ( auto file : _grf_files ) {
2023-01-08 03:12:33 +01:00
file - > roadstops . clear ( ) ;
2022-11-06 16:01:27 +01:00
}
}
2011-09-11 13:14:22 +02:00
/** Reset and clear all NewGRFs */
2007-03-07 12:47:46 +01:00
static void ResetNewGRF ( )
2006-10-26 21:16:10 +02:00
{
2019-02-17 12:20:52 +01:00
for ( GRFFile * const file : _grf_files ) {
delete file ;
2006-10-26 21:16:10 +02:00
}
2018-09-21 00:44:14 +02:00
_grf_files . clear ( ) ;
2019-04-10 23:07:06 +02:00
_cur . grffile = nullptr ;
2006-10-26 21:16:10 +02:00
}
2011-09-11 13:14:22 +02:00
/** Clear all NewGRF errors */
2007-06-12 11:41:12 +02:00
static void ResetNewGRFErrors ( )
{
2019-04-10 23:07:06 +02:00
for ( GRFConfig * c = _grfconfig ; c ! = nullptr ; c = c - > next ) {
2023-04-27 20:28:09 +02:00
c - > error . reset ( ) ;
2007-06-12 11:41:12 +02:00
}
}
2005-09-26 20:43:58 +02:00
/**
* Reset all NewGRF loaded data
*/
2010-09-06 20:20:07 +02:00
void ResetNewGRFData ( )
2005-09-26 20:43:58 +02:00
{
2006-04-21 05:00:20 +02:00
CleanUpStrings ( ) ;
2007-06-19 01:00:55 +02:00
CleanUpGRFTownNames ( ) ;
2006-04-21 05:00:20 +02:00
2007-03-21 04:06:21 +01:00
/* Copy/reset original engine info data */
2008-01-08 19:25:51 +01:00
SetupEngines ( ) ;
2005-10-01 19:38:48 +02:00
2008-02-05 05:48:56 +01:00
/* Copy/reset original bridge info data */
ResetBridges ( ) ;
2005-10-04 23:42:00 +02:00
2008-08-25 01:00:11 +02:00
/* Reset rail type information */
ResetRailTypes ( ) ;
2019-04-06 08:46:15 +02:00
/* Copy/reset original road type info data */
ResetRoadTypes ( ) ;
2008-04-22 20:20:41 +02:00
/* Allocate temporary refit/cargo class data */
2023-04-20 21:38:48 +02:00
_gted . resize ( Engine : : GetPoolSize ( ) ) ;
2005-11-06 14:42:26 +01:00
2010-03-23 12:05:28 +01:00
/* Fill rail type label temporary data for default trains */
2019-12-15 22:45:18 +01:00
for ( const Engine * e : Engine : : IterateType ( VEH_TRAIN ) ) {
2010-03-23 12:05:28 +01:00
_gted [ e - > index ] . railtypelabel = GetRailTypeInfo ( e - > u . rail . railtype ) - > label ;
}
2007-03-21 04:06:21 +01:00
/* Reset GRM reservations */
2006-10-27 15:15:21 +02:00
memset ( & _grm_engines , 0 , sizeof ( _grm_engines ) ) ;
2011-12-15 22:56:00 +01:00
memset ( & _grm_cargoes , 0 , sizeof ( _grm_cargoes ) ) ;
2006-10-27 15:15:21 +02:00
2008-02-12 14:23:57 +01:00
/* Reset generic feature callback lists */
ResetGenericCallbacks ( ) ;
2007-03-21 04:06:21 +01:00
/* Reset price base data */
2005-10-12 09:27:56 +02:00
ResetPriceBaseMultipliers ( ) ;
2005-11-12 01:19:34 +01:00
2006-08-25 02:41:10 +02:00
/* Reset the curencies array */
ResetCurrencies ( ) ;
2007-03-19 12:27:30 +01:00
/* Reset the house array */
ResetCustomHouses ( ) ;
ResetHouses ( ) ;
2007-05-29 19:41:59 +02:00
/* Reset the industries structures*/
2007-05-31 05:35:11 +02:00
ResetCustomIndustries ( ) ;
2007-05-29 19:41:59 +02:00
ResetIndustries ( ) ;
2010-08-28 19:29:12 +02:00
/* Reset the objects. */
2010-08-28 19:32:30 +02:00
ObjectClass : : Reset ( ) ;
2010-08-28 20:21:09 +02:00
ResetCustomObjects ( ) ;
2010-08-28 19:29:12 +02:00
ResetObjects ( ) ;
2007-03-21 04:06:21 +01:00
/* Reset station classes */
2010-08-08 00:07:07 +02:00
StationClass : : Reset ( ) ;
2005-11-12 01:19:34 +01:00
ResetCustomStations ( ) ;
2006-01-02 23:40:39 +01:00
2010-02-22 15:15:48 +01:00
/* Reset airport-related structures */
2010-08-08 00:08:20 +02:00
AirportClass : : Reset ( ) ;
2010-02-22 15:16:19 +01:00
ResetCustomAirports ( ) ;
2010-03-06 00:21:23 +01:00
AirportSpec : : ResetAirports ( ) ;
2010-02-22 15:15:48 +01:00
AirportTileSpec : : ResetAirportTiles ( ) ;
2022-11-06 16:01:27 +01:00
/* Reset road stop classes */
RoadStopClass : : Reset ( ) ;
ResetCustomRoadStops ( ) ;
2008-01-21 21:41:04 +01:00
/* Reset canal sprite groups and flags */
memset ( _water_feature , 0 , sizeof ( _water_feature ) ) ;
2007-05-06 20:14:33 +02:00
2007-03-20 14:47:00 +01:00
/* Reset the snowline table. */
ClearSnowLine ( ) ;
2006-10-26 21:16:10 +02:00
/* Reset NewGRF files */
ResetNewGRF ( ) ;
2007-06-12 11:41:12 +02:00
/* Reset NewGRF errors. */
ResetNewGRFErrors ( ) ;
2007-02-20 23:09:21 +01:00
/* Set up the default cargo types */
2008-05-29 17:13:28 +02:00
SetupCargoForClimate ( _settings_game . game_creation . landscape ) ;
2007-02-20 23:09:21 +01:00
2006-05-14 22:58:12 +02:00
/* Reset misc GRF features and train list display variables */
_misc_grf_features = 0 ;
2007-05-31 21:13:41 +02:00
2007-06-16 01:55:52 +02:00
_loaded_newgrf_features . has_2CC = false ;
2010-09-25 23:59:22 +02:00
_loaded_newgrf_features . used_liveries = 1 < < LS_DEFAULT ;
2008-01-24 15:49:40 +01:00
_loaded_newgrf_features . shore = SHORE_REPLACE_NONE ;
2019-04-06 08:46:15 +02:00
_loaded_newgrf_features . tram = TRAMWAY_REPLACE_DEPOT_NONE ;
2006-05-14 22:58:12 +02:00
2008-04-29 23:31:29 +02:00
/* Clear all GRF overrides */
_grf_id_overrides . clear ( ) ;
2006-09-27 20:17:01 +02:00
InitializeSoundPool ( ) ;
2009-05-22 17:13:50 +02:00
_spritegroup_pool . CleanPool ( ) ;
2005-09-26 20:43:58 +02:00
}
2011-07-30 00:23:17 +02:00
/**
* Reset NewGRF data which is stored persistently in savegames .
*/
void ResetPersistentNewGRFData ( )
{
/* Reset override managers */
_engine_mngr . ResetToDefaultMapping ( ) ;
_house_mngr . ResetMapping ( ) ;
_industry_mngr . ResetMapping ( ) ;
_industile_mngr . ResetMapping ( ) ;
_airport_mngr . ResetMapping ( ) ;
_airporttile_mngr . ResetMapping ( ) ;
}
2011-09-11 13:14:22 +02:00
/**
* Construct the Cargo Mapping
* @ note This is the reverse of a cargo translation table
*/
2007-02-25 00:20:21 +01:00
static void BuildCargoTranslationMap ( )
{
2011-08-21 14:05:47 +02:00
memset ( _cur . grffile - > cargo_map , 0xFF , sizeof ( _cur . grffile - > cargo_map ) ) ;
2007-02-25 00:20:21 +01:00
for ( CargoID c = 0 ; c < NUM_CARGO ; c + + ) {
2009-07-16 21:00:13 +02:00
const CargoSpec * cs = CargoSpec : : Get ( c ) ;
2007-02-25 00:20:21 +01:00
if ( ! cs - > IsValid ( ) ) continue ;
2018-09-23 13:23:54 +02:00
if ( _cur . grffile - > cargo_list . size ( ) = = 0 ) {
2007-02-25 00:20:21 +01:00
/* Default translation table, so just a straight mapping to bitnum */
2011-08-21 14:05:47 +02:00
_cur . grffile - > cargo_map [ c ] = cs - > bitnum ;
2007-02-25 00:20:21 +01:00
} else {
/* Check the translation table for this cargo's label */
2019-02-20 20:27:10 +01:00
int idx = find_index ( _cur . grffile - > cargo_list , { cs - > label } ) ;
if ( idx > = 0 ) _cur . grffile - > cargo_map [ c ] = idx ;
2007-02-25 00:20:21 +01:00
}
}
}
2011-09-11 13:14:22 +02:00
/**
* Prepare loading a NewGRF file with its config
* @ param config The NewGRF configuration struct with name , id , parameters and alike .
*/
2011-05-01 22:46:36 +02:00
static void InitNewGRFFile ( const GRFConfig * config )
2004-11-13 00:26:02 +01:00
{
2007-03-10 10:55:11 +01:00
GRFFile * newfile = GetFileByFilename ( config - > filename ) ;
2019-04-10 23:07:06 +02:00
if ( newfile ! = nullptr ) {
2004-11-22 23:51:19 +01:00
/* We already loaded it once. */
2011-08-21 14:05:47 +02:00
_cur . grffile = newfile ;
2004-11-22 23:51:19 +01:00
return ;
}
2012-05-25 22:54:54 +02:00
newfile = new GRFFile ( config ) ;
2019-02-18 23:39:06 +01:00
_grf_files . push_back ( _cur . grffile = newfile ) ;
2012-05-25 22:54:54 +02:00
}
2004-11-12 23:28:19 +01:00
2012-05-25 22:54:54 +02:00
/**
* Constructor for GRFFile
* @ param config GRFConfig to copy name , grfid and parameters from .
*/
GRFFile : : GRFFile ( const GRFConfig * config )
{
2023-04-27 20:42:25 +02:00
this - > filename = config - > filename ;
2012-05-25 22:54:54 +02:00
this - > grfid = config - > ident . grfid ;
2004-11-12 23:28:19 +01:00
2009-07-18 00:00:13 +02:00
/* Initialise local settings to defaults */
2012-05-25 22:54:54 +02:00
this - > traininfo_vehicle_pitch = 0 ;
this - > traininfo_vehicle_width = TRAININFO_DEFAULT_VEHICLE_WIDTH ;
2009-07-18 00:00:13 +02:00
2009-11-24 14:15:58 +01:00
/* Mark price_base_multipliers as 'not set' */
for ( Price i = PR_BEGIN ; i < PR_END ; i + + ) {
2012-05-25 22:54:54 +02:00
this - > price_base_multipliers [ i ] = INVALID_PRICE_MODIFIER ;
2009-11-24 14:15:58 +01:00
}
2010-02-03 08:58:43 +01:00
/* Initialise rail type map with default rail types */
2020-06-01 11:25:58 +02:00
std : : fill ( std : : begin ( this - > railtype_map ) , std : : end ( this - > railtype_map ) , INVALID_RAILTYPE ) ;
2012-05-25 22:54:54 +02:00
this - > railtype_map [ 0 ] = RAILTYPE_RAIL ;
this - > railtype_map [ 1 ] = RAILTYPE_ELECTRIC ;
this - > railtype_map [ 2 ] = RAILTYPE_MONO ;
this - > railtype_map [ 3 ] = RAILTYPE_MAGLEV ;
2010-02-03 08:58:43 +01:00
2019-04-06 08:46:15 +02:00
/* Initialise road type map with default road types */
2020-06-01 11:25:58 +02:00
std : : fill ( std : : begin ( this - > roadtype_map ) , std : : end ( this - > roadtype_map ) , INVALID_ROADTYPE ) ;
2019-04-06 08:46:15 +02:00
this - > roadtype_map [ 0 ] = ROADTYPE_ROAD ;
/* Initialise tram type map with default tram types */
2020-06-01 11:25:58 +02:00
std : : fill ( std : : begin ( this - > tramtype_map ) , std : : end ( this - > tramtype_map ) , INVALID_ROADTYPE ) ;
2019-04-06 08:46:15 +02:00
this - > tramtype_map [ 0 ] = ROADTYPE_TRAM ;
2010-01-03 20:29:56 +01:00
/* Copy the initial parameter list
* ' Uninitialised ' parameters are zeroed as that is their default value when dynamically creating them . */
2023-05-18 20:56:24 +02:00
this - > param = config - > param ;
2012-05-25 22:54:54 +02:00
this - > param_end = config - > num_params ;
}
2006-04-22 15:17:13 +02:00
2012-05-25 22:54:54 +02:00
GRFFile : : ~ GRFFile ( )
{
delete [ ] this - > language_map ;
2004-11-12 23:28:19 +01:00
}
2006-11-06 11:11:03 +01:00
2005-11-06 14:42:26 +01:00
/**
* Precalculate refit masks from cargo classes for all vehicles .
*/
2007-03-07 12:47:46 +01:00
static void CalculateRefitMasks ( )
2005-11-06 14:42:26 +01:00
{
2021-04-30 21:15:27 +02:00
CargoTypes original_known_cargoes = 0 ;
for ( int ct = 0 ; ct ! = NUM_ORIGINAL_CARGO ; + + ct ) {
CargoID cid = GetDefaultCargoID ( _settings_game . game_creation . landscape , static_cast < CargoType > ( ct ) ) ;
2023-05-04 12:29:21 +02:00
if ( IsValidCargoID ( cid ) ) SetBit ( original_known_cargoes , cid ) ;
2021-04-30 21:15:27 +02:00
}
2019-12-15 22:45:18 +01:00
for ( Engine * e : Engine : : Iterate ( ) ) {
2008-04-29 23:31:29 +02:00
EngineID engine = e - > index ;
EngineInfo * ei = & e - > info ;
2011-11-01 01:29:05 +01:00
bool only_defaultcargo ; ///< Set if the vehicle shall carry only the default cargo
2007-03-10 17:21:29 +01:00
2021-04-30 21:15:27 +02:00
/* If the NewGRF did not set any cargo properties, we apply default values. */
2021-04-30 17:35:32 +02:00
if ( _gted [ engine ] . defaultcargo_grf = = nullptr ) {
2021-04-30 21:15:27 +02:00
/* If the vehicle has any capacity, apply the default refit masks */
if ( e - > type ! = VEH_TRAIN | | e - > u . rail . capacity ! = 0 ) {
static constexpr byte T = 1 < < LT_TEMPERATE ;
static constexpr byte A = 1 < < LT_ARCTIC ;
static constexpr byte S = 1 < < LT_TROPIC ;
static constexpr byte Y = 1 < < LT_TOYLAND ;
static const struct DefaultRefitMasks {
byte climate ;
CargoType cargo_type ;
CargoTypes cargo_allowed ;
CargoTypes cargo_disallowed ;
} _default_refit_masks [ ] = {
{ T | A | S | Y , CT_PASSENGERS , CC_PASSENGERS , 0 } ,
{ T | A | S , CT_MAIL , CC_MAIL , 0 } ,
{ T | A | S , CT_VALUABLES , CC_ARMOURED , CC_LIQUID } ,
{ Y , CT_MAIL , CC_MAIL | CC_ARMOURED , CC_LIQUID } ,
{ T | A , CT_COAL , CC_BULK , 0 } ,
{ S , CT_COPPER_ORE , CC_BULK , 0 } ,
{ Y , CT_SUGAR , CC_BULK , 0 } ,
{ T | A | S , CT_OIL , CC_LIQUID , 0 } ,
{ Y , CT_COLA , CC_LIQUID , 0 } ,
{ T , CT_GOODS , CC_PIECE_GOODS | CC_EXPRESS , CC_LIQUID | CC_PASSENGERS } ,
{ A | S , CT_GOODS , CC_PIECE_GOODS | CC_EXPRESS , CC_LIQUID | CC_PASSENGERS | CC_REFRIGERATED } ,
{ A | S , CT_FOOD , CC_REFRIGERATED , 0 } ,
{ Y , CT_CANDY , CC_PIECE_GOODS | CC_EXPRESS , CC_LIQUID | CC_PASSENGERS } ,
} ;
if ( e - > type = = VEH_AIRCRAFT ) {
/* Aircraft default to "light" cargoes */
_gted [ engine ] . cargo_allowed = CC_PASSENGERS | CC_MAIL | CC_ARMOURED | CC_EXPRESS ;
_gted [ engine ] . cargo_disallowed = CC_LIQUID ;
} else if ( e - > type = = VEH_SHIP ) {
switch ( ei - > cargo_type ) {
case CT_PASSENGERS :
/* Ferries */
_gted [ engine ] . cargo_allowed = CC_PASSENGERS ;
_gted [ engine ] . cargo_disallowed = 0 ;
break ;
case CT_OIL :
/* Tankers */
_gted [ engine ] . cargo_allowed = CC_LIQUID ;
_gted [ engine ] . cargo_disallowed = 0 ;
break ;
default :
/* Cargo ships */
if ( _settings_game . game_creation . landscape = = LT_TOYLAND ) {
/* No tanker in toyland :( */
_gted [ engine ] . cargo_allowed = CC_MAIL | CC_ARMOURED | CC_EXPRESS | CC_BULK | CC_PIECE_GOODS | CC_LIQUID ;
_gted [ engine ] . cargo_disallowed = CC_PASSENGERS ;
} else {
_gted [ engine ] . cargo_allowed = CC_MAIL | CC_ARMOURED | CC_EXPRESS | CC_BULK | CC_PIECE_GOODS ;
_gted [ engine ] . cargo_disallowed = CC_LIQUID | CC_PASSENGERS ;
}
break ;
}
e - > u . ship . old_refittable = true ;
} else if ( e - > type = = VEH_TRAIN & & e - > u . rail . railveh_type ! = RAILVEH_WAGON ) {
/* Train engines default to all cargoes, so you can build single-cargo consists with fast engines.
* Trains loading multiple cargoes may start stations accepting unwanted cargoes . */
_gted [ engine ] . cargo_allowed = CC_PASSENGERS | CC_MAIL | CC_ARMOURED | CC_EXPRESS | CC_BULK | CC_PIECE_GOODS | CC_LIQUID ;
_gted [ engine ] . cargo_disallowed = 0 ;
} else {
/* Train wagons and road vehicles are classified by their default cargo type */
for ( const auto & drm : _default_refit_masks ) {
if ( ! HasBit ( drm . climate , _settings_game . game_creation . landscape ) ) continue ;
if ( drm . cargo_type ! = ei - > cargo_type ) continue ;
_gted [ engine ] . cargo_allowed = drm . cargo_allowed ;
_gted [ engine ] . cargo_disallowed = drm . cargo_disallowed ;
break ;
}
/* All original cargoes have specialised vehicles, so exclude them */
_gted [ engine ] . ctt_exclude_mask = original_known_cargoes ;
}
}
_gted [ engine ] . UpdateRefittability ( _gted [ engine ] . cargo_allowed ! = 0 ) ;
2021-04-30 17:35:32 +02:00
/* Translate cargo_type using the original climate-specific cargo table. */
ei - > cargo_type = GetDefaultCargoID ( _settings_game . game_creation . landscape , static_cast < CargoType > ( ei - > cargo_type ) ) ;
2023-05-04 12:29:21 +02:00
if ( IsValidCargoID ( ei - > cargo_type ) ) ClrBit ( _gted [ engine ] . ctt_exclude_mask , ei - > cargo_type ) ;
2021-04-30 17:35:32 +02:00
}
2021-04-30 21:15:27 +02:00
/* Compute refittability */
{
2018-05-21 23:08:39 +02:00
CargoTypes mask = 0 ;
CargoTypes not_mask = 0 ;
CargoTypes xor_mask = ei - > refit_mask ;
2011-11-01 01:26:22 +01:00
2011-11-01 01:29:05 +01:00
/* If the original masks set by the grf are zero, the vehicle shall only carry the default cargo.
* Note : After applying the translations , the vehicle may end up carrying no defined cargo . It becomes unavailable in that case . */
2021-04-30 21:15:27 +02:00
only_defaultcargo = _gted [ engine ] . refittability ! = GRFTempEngineData : : NONEMPTY ;
2011-11-01 01:29:05 +01:00
2009-09-28 21:24:20 +02:00
if ( _gted [ engine ] . cargo_allowed ! = 0 ) {
/* Build up the list of cargo types from the set cargo classes. */
2021-04-29 17:51:05 +02:00
for ( const CargoSpec * cs : CargoSpec : : Iterate ( ) ) {
2009-09-28 21:24:20 +02:00
if ( _gted [ engine ] . cargo_allowed & cs - > classes ) SetBit ( mask , cs - > Index ( ) ) ;
if ( _gted [ engine ] . cargo_disallowed & cs - > classes ) SetBit ( not_mask , cs - > Index ( ) ) ;
2007-03-10 17:21:29 +01:00
}
}
2011-11-01 01:26:22 +01:00
ei - > refit_mask = ( ( mask & ~ not_mask ) ^ xor_mask ) & _cargo_mask ;
2011-11-21 23:39:59 +01:00
/* Apply explicit refit includes/excludes. */
ei - > refit_mask | = _gted [ engine ] . ctt_include_mask ;
ei - > refit_mask & = ~ _gted [ engine ] . ctt_exclude_mask ;
2011-11-01 01:29:05 +01:00
}
2012-07-27 21:16:01 +02:00
/* Clear invalid cargoslots (from default vehicles or pre-NewCargo GRFs) */
2023-05-04 12:29:21 +02:00
if ( IsValidCargoID ( ei - > cargo_type ) & & ! HasBit ( _cargo_mask , ei - > cargo_type ) ) ei - > cargo_type = CT_INVALID ;
2012-07-27 21:16:01 +02:00
2011-12-15 22:56:00 +01:00
/* Ensure that the vehicle is either not refittable, or that the default cargo is one of the refittable cargoes.
2011-11-01 01:29:05 +01:00
* Note : Vehicles refittable to no cargo are handle differently to vehicle refittable to a single cargo . The latter might have subtypes . */
2023-05-04 12:29:21 +02:00
if ( ! only_defaultcargo & & ( e - > type ! = VEH_SHIP | | e - > u . ship . old_refittable ) & & IsValidCargoID ( ei - > cargo_type ) & & ! HasBit ( ei - > refit_mask , ei - > cargo_type ) ) {
2011-11-01 01:29:05 +01:00
ei - > cargo_type = CT_INVALID ;
2011-11-01 01:26:22 +01:00
}
2007-02-24 20:26:45 +01:00
/* Check if this engine's cargo type is valid. If not, set to the first refittable
2009-08-17 20:33:48 +02:00
* cargo type . Finally disable the vehicle , if there is still no cargo . */
2023-05-04 12:29:21 +02:00
if ( ! IsValidCargoID ( ei - > cargo_type ) & & ei - > refit_mask ! = 0 ) {
2012-02-07 23:59:30 +01:00
/* Figure out which CTT to use for the default cargo, if it is 'first refittable'. */
2019-04-10 23:07:06 +02:00
const uint8 * cargo_map_for_first_refittable = nullptr ;
2012-02-07 23:59:30 +01:00
{
const GRFFile * file = _gted [ engine ] . defaultcargo_grf ;
2019-04-10 23:07:06 +02:00
if ( file = = nullptr ) file = e - > GetGRF ( ) ;
if ( file ! = nullptr & & file - > grf_version > = 8 & & file - > cargo_list . size ( ) ! = 0 ) {
2012-02-07 23:59:30 +01:00
cargo_map_for_first_refittable = file - > cargo_map ;
}
}
2019-04-10 23:07:06 +02:00
if ( cargo_map_for_first_refittable ! = nullptr ) {
2011-11-08 18:25:51 +01:00
/* Use first refittable cargo from cargo translation table */
byte best_local_slot = 0xFF ;
2021-06-13 04:29:24 +02:00
for ( CargoID cargo_type : SetCargoBitIterator ( ei - > refit_mask ) ) {
2011-11-08 18:25:51 +01:00
byte local_slot = cargo_map_for_first_refittable [ cargo_type ] ;
if ( local_slot < best_local_slot ) {
best_local_slot = local_slot ;
ei - > cargo_type = cargo_type ;
}
}
}
2012-01-22 16:53:17 +01:00
2023-05-04 12:29:21 +02:00
if ( ! IsValidCargoID ( ei - > cargo_type ) ) {
2012-01-22 16:53:17 +01:00
/* Use first refittable cargo slot */
ei - > cargo_type = ( CargoID ) FindFirstBit ( ei - > refit_mask ) ;
}
2011-11-08 18:25:51 +01:00
}
2023-05-04 12:29:21 +02:00
if ( ! IsValidCargoID ( ei - > cargo_type ) ) ei - > climates = 0 ;
2011-11-01 17:18:28 +01:00
/* Clear refit_mask for not refittable ships */
if ( e - > type = = VEH_SHIP & & ! e - > u . ship . old_refittable ) {
ei - > refit_mask = 0 ;
}
2005-11-06 14:42:26 +01:00
}
}
2004-11-12 20:03:37 +01:00
2011-09-11 11:31:57 +02:00
/** Set to use the correct action0 properties for each canal feature */
static void FinaliseCanals ( )
{
for ( uint i = 0 ; i < CF_END ; i + + ) {
2019-04-10 23:07:06 +02:00
if ( _water_feature [ i ] . grffile ! = nullptr ) {
2011-09-11 11:31:57 +02:00
_water_feature [ i ] . callback_mask = _water_feature [ i ] . grffile - > canal_local_properties [ i ] . callback_mask ;
_water_feature [ i ] . flags = _water_feature [ i ] . grffile - > canal_local_properties [ i ] . flags ;
}
}
}
2010-05-21 22:51:00 +02:00
/** Check for invalid engines */
static void FinaliseEngineArray ( )
{
2019-12-15 22:45:18 +01:00
for ( Engine * e : Engine : : Iterate ( ) ) {
2019-04-10 23:07:06 +02:00
if ( e - > GetGRF ( ) = = nullptr ) {
2010-05-21 22:51:00 +02:00
const EngineIDMapping & eid = _engine_mngr [ e - > index ] ;
if ( eid . grfid ! = INVALID_GRFID | | eid . internal_id ! = eid . substitute_id ) {
e - > info . string_id = STR_NEWGRF_INVALID_ENGINE ;
}
}
2010-09-25 23:59:22 +02:00
2023-01-13 20:22:31 +01:00
/* Do final mapping on variant engine ID and set appropriate flags on variant engine */
2019-02-27 08:00:33 +01:00
if ( e - > info . variant_id ! = INVALID_ENGINE ) {
2023-01-13 20:22:31 +01:00
e - > info . variant_id = GetNewEngineID ( e - > grf_prop . grffile , e - > type , e - > info . variant_id ) ;
if ( e - > info . variant_id ! = INVALID_ENGINE ) {
Engine : : Get ( e - > info . variant_id ) - > display_flags | = EngineDisplayFlags : : HasVariants | EngineDisplayFlags : : IsFolded ;
}
2019-02-27 08:00:33 +01:00
}
2023-01-13 20:22:31 +01:00
if ( ! HasBit ( e - > info . climates , _settings_game . game_creation . landscape ) ) continue ;
2010-09-25 23:59:22 +02:00
/* Skip wagons, there livery is defined via the engine */
if ( e - > type ! = VEH_TRAIN | | e - > u . rail . railveh_type ! = RAILVEH_WAGON ) {
2019-04-10 23:07:06 +02:00
LiveryScheme ls = GetEngineLiveryScheme ( e - > index , INVALID_ENGINE , nullptr ) ;
2010-09-25 23:59:22 +02:00
SetBit ( _loaded_newgrf_features . used_liveries , ls ) ;
/* Note: For ships and roadvehicles we assume that they cannot be refitted between passenger and freight */
if ( e - > type = = VEH_TRAIN ) {
SetBit ( _loaded_newgrf_features . used_liveries , LS_FREIGHT_WAGON ) ;
switch ( ls ) {
case LS_STEAM :
case LS_DIESEL :
case LS_ELECTRIC :
case LS_MONORAIL :
case LS_MAGLEV :
SetBit ( _loaded_newgrf_features . used_liveries , LS_PASSENGER_WAGON_STEAM + ls - LS_STEAM ) ;
break ;
case LS_DMU :
case LS_EMU :
SetBit ( _loaded_newgrf_features . used_liveries , LS_PASSENGER_WAGON_DIESEL + ls - LS_DMU ) ;
break ;
default : NOT_REACHED ( ) ;
}
}
}
2010-05-21 22:51:00 +02:00
}
}
2011-12-15 22:56:00 +01:00
/** Check for invalid cargoes */
2010-05-21 22:51:00 +02:00
static void FinaliseCargoArray ( )
{
for ( CargoID c = 0 ; c < NUM_CARGO ; c + + ) {
CargoSpec * cs = CargoSpec : : Get ( c ) ;
if ( ! cs - > IsValid ( ) ) {
cs - > name = cs - > name_single = cs - > units_volume = STR_NEWGRF_INVALID_CARGO ;
cs - > quantifier = STR_NEWGRF_INVALID_CARGO_QUANTITY ;
cs - > abbrev = STR_NEWGRF_INVALID_CARGO_ABBREV ;
}
}
}
2010-09-13 15:08:53 +02:00
/**
* Check if a given housespec is valid and disable it if it ' s not .
* The housespecs that follow it are used to check the validity of
* multitile houses .
* @ param hs The housespec to check .
* @ param next1 The housespec that follows \ c hs .
* @ param next2 The housespec that follows \ c next1 .
* @ param next3 The housespec that follows \ c next2 .
* @ param filename The filename of the newgrf this house was defined in .
* @ return Whether the given housespec is valid .
*/
2023-04-27 20:42:25 +02:00
static bool IsHouseSpecValid ( HouseSpec * hs , const HouseSpec * next1 , const HouseSpec * next2 , const HouseSpec * next3 , const std : : string & filename )
2010-09-13 15:08:53 +02:00
{
if ( ( ( hs - > building_flags & BUILDING_HAS_2_TILES ) ! = 0 & &
2019-04-10 23:07:06 +02:00
( next1 = = nullptr | | ! next1 - > enabled | | ( next1 - > building_flags & BUILDING_HAS_1_TILE ) ! = 0 ) ) | |
2010-09-13 15:08:53 +02:00
( ( hs - > building_flags & BUILDING_HAS_4_TILES ) ! = 0 & &
2019-04-10 23:07:06 +02:00
( next2 = = nullptr | | ! next2 - > enabled | | ( next2 - > building_flags & BUILDING_HAS_1_TILE ) ! = 0 | |
next3 = = nullptr | | ! next3 - > enabled | | ( next3 - > building_flags & BUILDING_HAS_1_TILE ) ! = 0 ) ) ) {
2010-09-13 15:08:53 +02:00
hs - > enabled = false ;
2023-04-27 20:42:25 +02:00
if ( ! filename . empty ( ) ) Debug ( grf , 1 , " FinaliseHouseArray: {} defines house {} as multitile, but no suitable tiles follow. Disabling house. " , filename , hs - > grf_prop . local_id ) ;
2010-09-13 15:08:53 +02:00
return false ;
}
/* Some places sum population by only counting north tiles. Other places use all tiles causing desyncs.
* As the newgrf specs define population to be zero for non - north tiles , we just disable the offending house .
* If you want to allow non - zero populations somewhen , make sure to sum the population of all tiles in all places . */
if ( ( ( hs - > building_flags & BUILDING_HAS_2_TILES ) ! = 0 & & next1 - > population ! = 0 ) | |
( ( hs - > building_flags & BUILDING_HAS_4_TILES ) ! = 0 & & ( next2 - > population ! = 0 | | next3 - > population ! = 0 ) ) ) {
hs - > enabled = false ;
2023-04-27 20:42:25 +02:00
if ( ! filename . empty ( ) ) Debug ( grf , 1 , " FinaliseHouseArray: {} defines multitile house {} with non-zero population on additional tiles. Disabling house. " , filename , hs - > grf_prop . local_id ) ;
2010-09-13 15:08:53 +02:00
return false ;
}
/* Substitute type is also used for override, and having an override with a different size causes crashes.
* This check should only be done for NewGRF houses because grf_prop . subst_id is not set for original houses . */
2023-04-27 20:42:25 +02:00
if ( ! filename . empty ( ) & & ( hs - > building_flags & BUILDING_HAS_1_TILE ) ! = ( HouseSpec : : Get ( hs - > grf_prop . subst_id ) - > building_flags & BUILDING_HAS_1_TILE ) ) {
2010-09-13 15:08:53 +02:00
hs - > enabled = false ;
2021-06-12 09:10:17 +02:00
Debug ( grf , 1 , " FinaliseHouseArray: {} defines house {} with different house size then it's substitute type. Disabling house. " , filename , hs - > grf_prop . local_id ) ;
2010-09-13 15:08:53 +02:00
return false ;
}
2010-09-13 15:27:29 +02:00
/* Make sure that additional parts of multitile houses are not available. */
if ( ( hs - > building_flags & BUILDING_HAS_1_TILE ) = = 0 & & ( hs - > building_availability & HZ_ZONALL ) ! = 0 & & ( hs - > building_availability & HZ_CLIMALL ) ! = 0 ) {
hs - > enabled = false ;
2023-04-27 20:42:25 +02:00
if ( ! filename . empty ( ) ) Debug ( grf , 1 , " FinaliseHouseArray: {} defines house {} without a size but marked it as available. Disabling house. " , filename , hs - > grf_prop . local_id ) ;
2010-09-13 15:27:29 +02:00
return false ;
}
2010-09-13 15:08:53 +02:00
return true ;
}
2011-04-30 21:44:20 +02:00
/**
* Make sure there is at least one house available in the year 0 for the given
* climate / housezone combination .
* @ param bitmask The climate and housezone to check for . Exactly one climate
* bit and one housezone bit should be set .
*/
static void EnsureEarlyHouse ( HouseZones bitmask )
{
2023-04-24 20:33:18 +02:00
TimerGameCalendar : : Year min_year = MAX_YEAR ;
2011-04-30 21:44:20 +02:00
2013-10-12 18:33:19 +02:00
for ( int i = 0 ; i < NUM_HOUSES ; i + + ) {
2011-04-30 21:44:20 +02:00
HouseSpec * hs = HouseSpec : : Get ( i ) ;
2019-04-10 23:07:06 +02:00
if ( hs = = nullptr | | ! hs - > enabled ) continue ;
2011-04-30 21:44:20 +02:00
if ( ( hs - > building_availability & bitmask ) ! = bitmask ) continue ;
if ( hs - > min_year < min_year ) min_year = hs - > min_year ;
}
if ( min_year = = 0 ) return ;
2013-10-12 18:33:19 +02:00
for ( int i = 0 ; i < NUM_HOUSES ; i + + ) {
2011-04-30 21:44:20 +02:00
HouseSpec * hs = HouseSpec : : Get ( i ) ;
2019-04-10 23:07:06 +02:00
if ( hs = = nullptr | | ! hs - > enabled ) continue ;
2011-04-30 21:44:20 +02:00
if ( ( hs - > building_availability & bitmask ) ! = bitmask ) continue ;
if ( hs - > min_year = = min_year ) hs - > min_year = 0 ;
}
}
2010-08-01 21:22:34 +02:00
/**
* Add all new houses to the house array . House properties can be set at any
2007-03-19 12:27:30 +01:00
* time in the GRF file , so we can only add a house spec to the house array
* after the file has finished loading . We also need to check the dates , due to
2010-08-01 21:44:49 +02:00
* the TTDPatch behaviour described below that we need to emulate .
*/
2007-03-19 12:27:30 +01:00
static void FinaliseHouseArray ( )
{
/* If there are no houses with start dates before 1930, then all houses
* with start dates of 1930 have them reset to 0. This is in order to be
* compatible with TTDPatch , where if no houses have start dates before
* 1930 and the date is before 1930 , the game pretends that this is 1930.
* If there have been any houses defined with start dates before 1930 then
2007-12-31 08:14:25 +01:00
* the dates are left alone .
* On the other hand , why 1930 ? Just ' fix ' the houses with the lowest
* minimum introduction date to 0.
*/
2019-02-17 12:20:52 +01:00
for ( GRFFile * const file : _grf_files ) {
2023-01-08 03:12:33 +01:00
if ( file - > housespec . empty ( ) ) continue ;
2007-03-19 12:27:30 +01:00
2023-01-08 03:12:33 +01:00
size_t num_houses = file - > housespec . size ( ) ;
for ( size_t i = 0 ; i < num_houses ; i + + ) {
2023-04-15 22:30:37 +02:00
HouseSpec * hs = file - > housespec [ i ] . get ( ) ;
2009-05-11 00:16:51 +02:00
2019-04-10 23:07:06 +02:00
if ( hs = = nullptr ) continue ;
2009-05-11 00:16:51 +02:00
2023-04-15 22:30:37 +02:00
const HouseSpec * next1 = ( i + 1 < num_houses ? file - > housespec [ i + 1 ] . get ( ) : nullptr ) ;
const HouseSpec * next2 = ( i + 2 < num_houses ? file - > housespec [ i + 2 ] . get ( ) : nullptr ) ;
const HouseSpec * next3 = ( i + 3 < num_houses ? file - > housespec [ i + 3 ] . get ( ) : nullptr ) ;
2009-05-11 00:16:51 +02:00
2019-02-17 12:20:52 +01:00
if ( ! IsHouseSpecValid ( hs , next1 , next2 , next3 , file - > filename ) ) continue ;
2009-05-22 20:44:22 +02:00
2009-05-11 00:16:51 +02:00
_house_mngr . SetEntitySpec ( hs ) ;
2007-03-19 12:27:30 +01:00
}
}
2023-01-08 03:12:33 +01:00
for ( size_t i = 0 ; i < NUM_HOUSES ; i + + ) {
2010-09-13 15:08:53 +02:00
HouseSpec * hs = HouseSpec : : Get ( i ) ;
2019-04-10 23:07:06 +02:00
const HouseSpec * next1 = ( i + 1 < NUM_HOUSES ? HouseSpec : : Get ( i + 1 ) : nullptr ) ;
const HouseSpec * next2 = ( i + 2 < NUM_HOUSES ? HouseSpec : : Get ( i + 2 ) : nullptr ) ;
const HouseSpec * next3 = ( i + 3 < NUM_HOUSES ? HouseSpec : : Get ( i + 3 ) : nullptr ) ;
2010-09-13 15:08:53 +02:00
/* We need to check all houses again to we are sure that multitile houses
* did get consecutive IDs and none of the parts are missing . */
2023-04-27 20:42:25 +02:00
if ( ! IsHouseSpecValid ( hs , next1 , next2 , next3 , std : : string { } ) ) {
2011-02-13 22:44:00 +01:00
/* GetHouseNorthPart checks 3 houses that are directly before
* it in the house pool . If any of those houses have multi - tile
* flags set it assumes it ' s part of a multitile house . Since
* we can have invalid houses in the pool marked as disabled , we
* don ' t want to have them influencing valid tiles . As such set
* building_flags to zero here to make sure any house following
* this one in the pool is properly handled as 1 x1 house . */
2011-02-14 13:39:45 +01:00
hs - > building_flags = TILE_NO_FLAG ;
2011-02-13 22:44:00 +01:00
}
2010-09-13 15:08:53 +02:00
}
2011-04-30 21:44:20 +02:00
HouseZones climate_mask = ( HouseZones ) ( 1 < < ( _settings_game . game_creation . landscape + 12 ) ) ;
EnsureEarlyHouse ( HZ_ZON1 | climate_mask ) ;
EnsureEarlyHouse ( HZ_ZON2 | climate_mask ) ;
EnsureEarlyHouse ( HZ_ZON3 | climate_mask ) ;
EnsureEarlyHouse ( HZ_ZON4 | climate_mask ) ;
EnsureEarlyHouse ( HZ_ZON5 | climate_mask ) ;
2007-03-19 12:27:30 +01:00
2011-04-30 21:44:20 +02:00
if ( _settings_game . game_creation . landscape = = LT_ARCTIC ) {
EnsureEarlyHouse ( HZ_ZON1 | HZ_SUBARTC_ABOVE ) ;
EnsureEarlyHouse ( HZ_ZON2 | HZ_SUBARTC_ABOVE ) ;
EnsureEarlyHouse ( HZ_ZON3 | HZ_SUBARTC_ABOVE ) ;
EnsureEarlyHouse ( HZ_ZON4 | HZ_SUBARTC_ABOVE ) ;
EnsureEarlyHouse ( HZ_ZON5 | HZ_SUBARTC_ABOVE ) ;
2007-03-19 12:27:30 +01:00
}
}
2010-08-01 21:22:34 +02:00
/**
* Add all new industries to the industry array . Industry properties can be set at any
2007-07-06 22:40:58 +02:00
* time in the GRF file , so we can only add a industry spec to the industry array
2010-08-01 21:44:49 +02:00
* after the file has finished loading .
*/
2007-07-06 22:40:58 +02:00
static void FinaliseIndustriesArray ( )
{
2019-02-17 12:20:52 +01:00
for ( GRFFile * const file : _grf_files ) {
2023-04-15 22:30:37 +02:00
for ( const auto & indsp : file - > industryspec ) {
2023-01-08 03:12:33 +01:00
if ( indsp = = nullptr | | ! indsp - > enabled ) continue ;
2008-02-02 04:23:26 +01:00
2023-01-08 03:12:33 +01:00
StringID strid ;
/* process the conversion of text at the end, so to be sure everything will be fine
* and available . Check if it does not return undefind marker , which is a very good sign of a
* substitute industry who has not changed the string been examined , thus using it as such */
strid = GetGRFStringID ( indsp - > grf_prop . grffile - > grfid , indsp - > name ) ;
if ( strid ! = STR_UNDEFINED ) indsp - > name = strid ;
strid = GetGRFStringID ( indsp - > grf_prop . grffile - > grfid , indsp - > closure_text ) ;
if ( strid ! = STR_UNDEFINED ) indsp - > closure_text = strid ;
strid = GetGRFStringID ( indsp - > grf_prop . grffile - > grfid , indsp - > production_up_text ) ;
if ( strid ! = STR_UNDEFINED ) indsp - > production_up_text = strid ;
strid = GetGRFStringID ( indsp - > grf_prop . grffile - > grfid , indsp - > production_down_text ) ;
if ( strid ! = STR_UNDEFINED ) indsp - > production_down_text = strid ;
strid = GetGRFStringID ( indsp - > grf_prop . grffile - > grfid , indsp - > new_industry_text ) ;
if ( strid ! = STR_UNDEFINED ) indsp - > new_industry_text = strid ;
if ( indsp - > station_name ! = STR_NULL ) {
/* STR_NULL (0) can be set by grf. It has a meaning regarding assignation of the
* station ' s name . Don ' t want to lose the value , therefore , do not process . */
strid = GetGRFStringID ( indsp - > grf_prop . grffile - > grfid , indsp - > station_name ) ;
if ( strid ! = STR_UNDEFINED ) indsp - > station_name = strid ;
2007-07-06 22:40:58 +02:00
}
2023-01-08 03:12:33 +01:00
2023-04-15 22:30:37 +02:00
_industry_mngr . SetEntitySpec ( indsp . get ( ) ) ;
2007-07-06 22:40:58 +02:00
}
2023-04-15 22:30:37 +02:00
for ( const auto & indtsp : file - > indtspec ) {
2023-01-08 03:12:33 +01:00
if ( indtsp ! = nullptr ) {
2023-04-15 22:30:37 +02:00
_industile_mngr . SetEntitySpec ( indtsp . get ( ) ) ;
2007-07-06 22:40:58 +02:00
}
}
}
2007-07-12 00:57:47 +02:00
for ( uint j = 0 ; j < NUM_INDUSTRYTYPES ; j + + ) {
IndustrySpec * indsp = & _industry_specs [ j ] ;
2019-04-10 23:07:06 +02:00
if ( indsp - > enabled & & indsp - > grf_prop . grffile ! = nullptr ) {
2007-07-12 00:57:47 +02:00
for ( uint i = 0 ; i < 3 ; i + + ) {
indsp - > conflicting [ i ] = MapNewGRFIndustryType ( indsp - > conflicting [ i ] , indsp - > grf_prop . grffile - > grfid ) ;
}
}
2010-05-21 22:51:00 +02:00
if ( ! indsp - > enabled ) {
indsp - > name = STR_NEWGRF_INVALID_INDUSTRYTYPE ;
}
2007-07-12 00:57:47 +02:00
}
2007-07-06 22:40:58 +02:00
}
2007-03-23 00:19:40 +01:00
2010-08-28 20:21:09 +02:00
/**
* Add all new objects to the object array . Object properties can be set at any
* time in the GRF file , so we can only add an object spec to the object array
* after the file has finished loading .
*/
static void FinaliseObjectsArray ( )
{
2019-02-17 12:20:52 +01:00
for ( GRFFile * const file : _grf_files ) {
2023-04-15 22:30:37 +02:00
for ( auto & objectspec : file - > objectspec ) {
2023-01-08 03:12:33 +01:00
if ( objectspec ! = nullptr & & objectspec - > grf_prop . grffile ! = nullptr & & objectspec - > IsEnabled ( ) ) {
2023-04-15 22:30:37 +02:00
_object_mngr . SetEntitySpec ( objectspec . get ( ) ) ;
2010-08-28 20:21:09 +02:00
}
}
}
2023-01-17 00:31:31 +01:00
ObjectSpec : : BindToClasses ( ) ;
2010-08-28 20:21:09 +02:00
}
2010-02-22 15:16:19 +01:00
/**
* Add all new airports to the airport array . Airport properties can be set at any
* time in the GRF file , so we can only add a airport spec to the airport array
* after the file has finished loading .
*/
static void FinaliseAirportsArray ( )
{
2019-02-17 12:20:52 +01:00
for ( GRFFile * const file : _grf_files ) {
2023-04-15 22:30:37 +02:00
for ( auto & as : file - > airportspec ) {
2023-01-08 03:12:33 +01:00
if ( as ! = nullptr & & as - > enabled ) {
2023-04-15 22:30:37 +02:00
_airport_mngr . SetEntitySpec ( as . get ( ) ) ;
2010-03-19 00:12:38 +01:00
}
}
2023-04-15 22:30:37 +02:00
for ( auto & ats : file - > airtspec ) {
2023-01-08 03:12:33 +01:00
if ( ats ! = nullptr & & ats - > enabled ) {
2023-04-15 22:30:37 +02:00
_airporttile_mngr . SetEntitySpec ( ats . get ( ) ) ;
2010-02-22 15:16:19 +01:00
}
}
}
}
2004-08-09 19:04:08 +02:00
/* Here we perform initial decoding of some special sprites (as are they
* described at http : //www.ttdpatch.net/src/newgrf.txt, but this is only a very
2009-03-15 01:32:18 +01:00
* partial implementation yet ) .
* XXX : We consider GRF files trusted . It would be trivial to exploit OTTD by
2004-08-09 19:04:08 +02:00
* a crafted invalid GRF file . We should tell that to the user somehow , or
* better make this more robust in the future . */
2009-03-09 14:06:49 +01:00
static void DecodeSpecialSprite ( byte * buf , uint num , GrfLoadingStage stage )
2004-08-09 19:04:08 +02:00
{
2005-08-15 13:39:13 +02:00
/* XXX: There is a difference between staged loading in TTDPatch and
* here . In TTDPatch , for some reason actions 1 and 2 are carried out
2006-04-15 23:27:59 +02:00
* during stage 1 , whilst action 3 is carried out during stage 2 ( to
2005-08-15 13:39:13 +02:00
* " resolve " cargo IDs . . . wtf ) . This is a little problem , because cargo
* IDs are valid only within a given set ( action 1 ) block , and may be
* overwritten after action 3 associates them . But overwriting happens
* in an earlier stage than associating , so . . . We just process actions
2006-04-15 23:27:59 +02:00
* 1 and 2 in stage 2 now , let ' s hope that won ' t get us into problems .
2009-03-15 01:32:18 +01:00
* - - pasky
* We need a pre - stage to set up GOTO labels of Action 0x10 because the grf
2006-04-15 23:27:59 +02:00
* is not in memory and scanning the file every time would be too expensive .
* In other stages we skip action 0x10 since it ' s already dealt with . */
2006-10-31 23:54:57 +01:00
static const SpecialSpriteHandler handlers [ ] [ GLS_END ] = {
2019-04-13 21:18:31 +02:00
/* 0x00 */ { nullptr , SafeChangeInfo , nullptr , nullptr , ReserveChangeInfo , FeatureChangeInfo , } ,
/* 0x01 */ { SkipAct1 , SkipAct1 , SkipAct1 , SkipAct1 , SkipAct1 , NewSpriteSet , } ,
/* 0x02 */ { nullptr , nullptr , nullptr , nullptr , nullptr , NewSpriteGroup , } ,
/* 0x03 */ { nullptr , GRFUnsafe , nullptr , nullptr , nullptr , FeatureMapSpriteGroup , } ,
/* 0x04 */ { nullptr , nullptr , nullptr , nullptr , nullptr , FeatureNewName , } ,
/* 0x05 */ { SkipAct5 , SkipAct5 , SkipAct5 , SkipAct5 , SkipAct5 , GraphicsNew , } ,
/* 0x06 */ { nullptr , nullptr , nullptr , CfgApply , CfgApply , CfgApply , } ,
/* 0x07 */ { nullptr , nullptr , nullptr , nullptr , SkipIf , SkipIf , } ,
/* 0x08 */ { ScanInfo , nullptr , nullptr , GRFInfo , GRFInfo , GRFInfo , } ,
/* 0x09 */ { nullptr , nullptr , nullptr , SkipIf , SkipIf , SkipIf , } ,
/* 0x0A */ { SkipActA , SkipActA , SkipActA , SkipActA , SkipActA , SpriteReplace , } ,
/* 0x0B */ { nullptr , nullptr , nullptr , GRFLoadError , GRFLoadError , GRFLoadError , } ,
/* 0x0C */ { nullptr , nullptr , nullptr , GRFComment , nullptr , GRFComment , } ,
/* 0x0D */ { nullptr , SafeParamSet , nullptr , ParamSet , ParamSet , ParamSet , } ,
/* 0x0E */ { nullptr , SafeGRFInhibit , nullptr , GRFInhibit , GRFInhibit , GRFInhibit , } ,
/* 0x0F */ { nullptr , GRFUnsafe , nullptr , FeatureTownName , nullptr , nullptr , } ,
/* 0x10 */ { nullptr , nullptr , DefineGotoLabel , nullptr , nullptr , nullptr , } ,
/* 0x11 */ { SkipAct11 , GRFUnsafe , SkipAct11 , GRFSound , SkipAct11 , GRFSound , } ,
/* 0x12 */ { SkipAct12 , SkipAct12 , SkipAct12 , SkipAct12 , SkipAct12 , LoadFontGlyph , } ,
/* 0x13 */ { nullptr , nullptr , nullptr , nullptr , nullptr , TranslateGRFStrings , } ,
/* 0x14 */ { StaticGRFInfo , nullptr , nullptr , nullptr , nullptr , nullptr , } ,
2004-08-09 19:04:08 +02:00
} ;
2005-08-15 13:39:13 +02:00
2011-08-21 14:05:47 +02:00
GRFLocation location ( _cur . grfconfig - > ident . grfid , _cur . nfo_line ) ;
2004-08-09 19:04:08 +02:00
2008-08-19 11:08:45 +02:00
GRFLineToSpriteOverride : : iterator it = _grf_line_to_action6_sprite_override . find ( location ) ;
if ( it = = _grf_line_to_action6_sprite_override . end ( ) ) {
2009-03-09 14:06:49 +01:00
/* No preloaded sprite to work with; read the
2006-05-23 22:59:01 +02:00
* pseudo sprite content . */
2021-04-14 17:41:45 +02:00
_cur . file - > ReadBlock ( buf , num ) ;
2006-05-23 22:59:01 +02:00
} else {
/* Use the preloaded sprite data. */
2023-04-26 07:45:11 +02:00
buf = _grf_line_to_action6_sprite_override [ location ] . data ( ) ;
2023-04-18 21:24:42 +02:00
GrfMsg ( 7 , " DecodeSpecialSprite: Using preloaded pseudo sprite data " ) ;
2006-05-23 22:59:01 +02:00
/* Skip the real (original) content of this action. */
2021-04-14 17:41:45 +02:00
_cur . file - > SeekTo ( num , SEEK_CUR ) ;
2006-05-23 22:59:01 +02:00
}
2004-11-12 16:15:56 +01:00
2010-01-22 14:02:15 +01:00
ByteReader br ( buf , buf + num ) ;
ByteReader * bufp = & br ;
2005-08-15 13:39:13 +02:00
2010-01-22 14:02:15 +01:00
try {
2010-01-23 14:17:30 +01:00
byte action = bufp - > ReadByte ( ) ;
2010-01-22 14:02:15 +01:00
if ( action = = 0xFF ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " DecodeSpecialSprite: Unexpected data block, skipping " ) ;
2010-01-22 14:02:15 +01:00
} else if ( action = = 0xFE ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 2 , " DecodeSpecialSprite: Unexpected import block, skipping " ) ;
2010-01-22 14:02:15 +01:00
} else if ( action > = lengthof ( handlers ) ) {
2023-04-20 17:00:46 +02:00
GrfMsg ( 7 , " DecodeSpecialSprite: Skipping unknown action 0x{:02X} " , action ) ;
2019-04-10 23:07:06 +02:00
} else if ( handlers [ action ] [ stage ] = = nullptr ) {
2023-04-20 17:00:46 +02:00
GrfMsg ( 7 , " DecodeSpecialSprite: Skipping action 0x{:02X} in stage {} " , action , stage ) ;
2010-01-22 14:02:15 +01:00
} else {
2023-04-20 17:00:46 +02:00
GrfMsg ( 7 , " DecodeSpecialSprite: Handling action 0x{:02X} in stage {} " , action , stage ) ;
2010-01-22 14:02:15 +01:00
handlers [ action ] [ stage ] ( bufp ) ;
}
} catch ( . . . ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 1 , " DecodeSpecialSprite: Tried to read past end of pseudo-sprite data " ) ;
2011-05-11 22:22:04 +02:00
DisableGrf ( STR_NEWGRF_ERROR_READ_BOUNDS ) ;
2005-08-15 13:39:13 +02:00
}
}
2004-11-13 00:26:02 +01:00
2011-08-24 15:48:29 +02:00
/**
2021-04-14 17:41:45 +02:00
* Load a particular NewGRF from a SpriteFile .
* @ param config The configuration of the to be loaded NewGRF .
* @ param stage The loading stage of the NewGRF .
* @ param file The file to load the GRF data from .
2011-08-24 15:48:29 +02:00
*/
2021-04-14 17:41:45 +02:00
static void LoadNewGRFFileFromFile ( GRFConfig * config , GrfLoadingStage stage , SpriteFile & file )
2005-08-15 13:39:13 +02:00
{
2021-04-14 17:41:45 +02:00
_cur . file = & file ;
2011-08-21 14:05:47 +02:00
_cur . grfconfig = config ;
2006-12-04 09:30:04 +01:00
2021-06-12 09:10:17 +02:00
Debug ( grf , 2 , " LoadNewGRFFile: Reading NewGRF-file '{}' " , config - > filename ) ;
2005-08-15 13:39:13 +02:00
2021-04-14 17:41:45 +02:00
byte grf_container_version = file . GetContainerVersion ( ) ;
2021-04-14 17:20:39 +02:00
if ( grf_container_version = = 0 ) {
2021-06-12 09:10:17 +02:00
Debug ( grf , 7 , " LoadNewGRFFile: Custom .grf has invalid format " ) ;
2012-02-04 14:29:04 +01:00
return ;
}
if ( stage = = GLS_INIT | | stage = = GLS_ACTIVATION ) {
/* We need the sprite offsets in the init stage for NewGRF sounds
* and in the activation stage for real sprites . */
2021-04-14 17:41:45 +02:00
ReadGRFSpriteOffsets ( file ) ;
2012-02-04 14:29:04 +01:00
} else {
/* Skip sprite section offset if present. */
2021-04-14 17:41:45 +02:00
if ( grf_container_version > = 2 ) file . ReadDword ( ) ;
2012-02-04 14:29:04 +01:00
}
2021-04-14 17:20:39 +02:00
if ( grf_container_version > = 2 ) {
2012-02-04 14:29:04 +01:00
/* Read compression value. */
2021-04-14 17:41:45 +02:00
byte compression = file . ReadByte ( ) ;
2012-02-04 14:29:04 +01:00
if ( compression ! = 0 ) {
2021-06-12 09:10:17 +02:00
Debug ( grf , 7 , " LoadNewGRFFile: Unsupported compression format " ) ;
2012-02-04 14:29:04 +01:00
return ;
}
}
2005-08-15 13:39:13 +02:00
/* Skip the first sprite; we don't care about how many sprites this
* does contain ; newest TTDPatches and George ' s longvehicles don ' t
* neither , apparently . */
2021-04-14 17:41:45 +02:00
uint32 num = grf_container_version > = 2 ? file . ReadDword ( ) : file . ReadWord ( ) ;
if ( num = = 4 & & file . ReadByte ( ) = = 0xFF ) {
file . ReadDword ( ) ;
2005-08-15 13:39:13 +02:00
} else {
2021-06-12 09:10:17 +02:00
Debug ( grf , 7 , " LoadNewGRFFile: Custom .grf has invalid format " ) ;
2006-12-04 09:30:04 +01:00
return ;
2005-08-15 13:39:13 +02:00
}
2011-08-21 14:06:24 +02:00
_cur . ClearDataForNextFile ( ) ;
2005-08-15 13:39:13 +02:00
2009-03-09 14:06:49 +01:00
ReusableBuffer < byte > buf ;
2021-04-14 17:41:45 +02:00
while ( ( num = ( grf_container_version > = 2 ? file . ReadDword ( ) : file . ReadWord ( ) ) ) ! = 0 ) {
byte type = file . ReadByte ( ) ;
2011-08-21 14:05:47 +02:00
_cur . nfo_line + + ;
2005-08-15 13:39:13 +02:00
if ( type = = 0xFF ) {
2011-08-21 14:05:47 +02:00
if ( _cur . skip_sprites = = 0 ) {
2009-03-09 14:06:49 +01:00
DecodeSpecialSprite ( buf . Allocate ( num ) , num , stage ) ;
2006-11-01 18:42:34 +01:00
/* Stop all processing if we are to skip the remaining sprites */
2011-08-21 14:05:47 +02:00
if ( _cur . skip_sprites = = - 1 ) break ;
2006-11-01 18:42:34 +01:00
2005-08-15 13:39:13 +02:00
continue ;
} else {
2021-04-14 17:41:45 +02:00
file . SkipBytes ( num ) ;
2005-08-15 13:39:13 +02:00
}
} else {
2011-08-21 14:05:47 +02:00
if ( _cur . skip_sprites = = 0 ) {
2023-04-18 21:24:42 +02:00
GrfMsg ( 0 , " LoadNewGRFFile: Unexpected sprite, disabling " ) ;
2011-05-11 22:22:04 +02:00
DisableGrf ( STR_NEWGRF_ERROR_UNEXPECTED_SPRITE ) ;
2008-08-29 22:03:51 +02:00
break ;
}
2005-08-15 13:39:13 +02:00
2021-04-14 17:20:39 +02:00
if ( grf_container_version > = 2 & & type = = 0xFD ) {
2012-02-04 14:29:04 +01:00
/* Reference to data section. Container version >= 2 only. */
2021-04-14 17:41:45 +02:00
file . SkipBytes ( num ) ;
2012-02-04 14:29:04 +01:00
} else {
2021-04-14 17:41:45 +02:00
file . SkipBytes ( 7 ) ;
SkipSpriteData ( file , type , num - 8 ) ;
2012-02-04 14:29:04 +01:00
}
2004-11-13 00:26:02 +01:00
}
2005-08-15 13:39:13 +02:00
2011-08-21 14:05:47 +02:00
if ( _cur . skip_sprites > 0 ) _cur . skip_sprites - - ;
2004-08-09 19:04:08 +02:00
}
2005-08-15 13:39:13 +02:00
}
2004-08-09 19:04:08 +02:00
2021-04-14 17:41:45 +02:00
/**
* Load a particular NewGRF .
* @ param config The configuration of the to be loaded NewGRF .
* @ param stage The loading stage of the NewGRF .
* @ param subdir The sub directory to find the NewGRF in .
* @ param temporary The NewGRF / sprite file is to be loaded temporarily and should be closed immediately ,
* contrary to loading the SpriteFile and having it cached by the SpriteCache .
*/
void LoadNewGRFFile ( GRFConfig * config , GrfLoadingStage stage , Subdirectory subdir , bool temporary )
{
2023-04-27 20:42:25 +02:00
const std : : string & filename = config - > filename ;
2021-04-14 17:41:45 +02:00
/* A .grf file is activated only if it was active when the game was
* started . If a game is loaded , only its active . grfs will be
* reactivated , unless " loadallgraphics on " is used . A . grf file is
* considered active if its action 8 has been processed , i . e . its
* action 8 hasn ' t been skipped using an action 7.
*
* During activation , only actions 0 , 1 , 2 , 3 , 4 , 5 , 7 , 8 , 9 , 0 A and 0 B are
* carried out . All others are ignored , because they only need to be
* processed once at initialization . */
if ( stage ! = GLS_FILESCAN & & stage ! = GLS_SAFETYSCAN & & stage ! = GLS_LABELSCAN ) {
_cur . grffile = GetFileByFilename ( filename ) ;
2023-04-19 22:47:36 +02:00
if ( _cur . grffile = = nullptr ) UserError ( " File '{}' lost in cache. \n " , filename ) ;
2021-04-14 17:41:45 +02:00
if ( stage = = GLS_RESERVE & & config - > status ! = GCS_INITIALISED ) return ;
if ( stage = = GLS_ACTIVATION & & ! HasBit ( config - > flags , GCF_RESERVED ) ) return ;
}
bool needs_palette_remap = config - > palette & GRFP_USE_MASK ;
if ( temporary ) {
SpriteFile temporarySpriteFile ( filename , subdir , needs_palette_remap ) ;
LoadNewGRFFileFromFile ( config , stage , temporarySpriteFile ) ;
} else {
LoadNewGRFFileFromFile ( config , stage , OpenCachedSpriteFile ( filename , subdir , needs_palette_remap ) ) ;
}
}
2008-01-24 15:49:40 +01:00
/**
* Relocates the old shore sprites at new positions .
*
* 1. If shore sprites are neither loaded by Action5 nor ActionA , the extra sprites from openttd ( w / d ) . grf are used . ( SHORE_REPLACE_ONLY_NEW )
* 2. If a newgrf replaces some shore sprites by ActionA . The ( maybe also replaced ) grass tiles are used for corner shores . ( SHORE_REPLACE_ACTION_A )
* 3. If a newgrf replaces shore sprites by Action5 any shore replacement by ActionA has no effect . ( SHORE_REPLACE_ACTION_5 )
*/
static void ActivateOldShore ( )
{
/* Use default graphics, if no shore sprites were loaded.
2013-01-08 23:46:42 +01:00
* Should not happen , as the base set ' s extra grf should include some . */
2008-01-24 15:49:40 +01:00
if ( _loaded_newgrf_features . shore = = SHORE_REPLACE_NONE ) _loaded_newgrf_features . shore = SHORE_REPLACE_ACTION_A ;
if ( _loaded_newgrf_features . shore ! = SHORE_REPLACE_ACTION_5 ) {
DupSprite ( SPR_ORIGINALSHORE_START + 1 , SPR_SHORE_BASE + 1 ) ; // SLOPE_W
DupSprite ( SPR_ORIGINALSHORE_START + 2 , SPR_SHORE_BASE + 2 ) ; // SLOPE_S
DupSprite ( SPR_ORIGINALSHORE_START + 6 , SPR_SHORE_BASE + 3 ) ; // SLOPE_SW
2009-10-02 17:13:15 +02:00
DupSprite ( SPR_ORIGINALSHORE_START + 0 , SPR_SHORE_BASE + 4 ) ; // SLOPE_E
2008-01-24 15:49:40 +01:00
DupSprite ( SPR_ORIGINALSHORE_START + 4 , SPR_SHORE_BASE + 6 ) ; // SLOPE_SE
DupSprite ( SPR_ORIGINALSHORE_START + 3 , SPR_SHORE_BASE + 8 ) ; // SLOPE_N
DupSprite ( SPR_ORIGINALSHORE_START + 7 , SPR_SHORE_BASE + 9 ) ; // SLOPE_NW
DupSprite ( SPR_ORIGINALSHORE_START + 5 , SPR_SHORE_BASE + 12 ) ; // SLOPE_NE
}
if ( _loaded_newgrf_features . shore = = SHORE_REPLACE_ACTION_A ) {
DupSprite ( SPR_FLAT_GRASS_TILE + 16 , SPR_SHORE_BASE + 0 ) ; // SLOPE_STEEP_S
DupSprite ( SPR_FLAT_GRASS_TILE + 17 , SPR_SHORE_BASE + 5 ) ; // SLOPE_STEEP_W
DupSprite ( SPR_FLAT_GRASS_TILE + 7 , SPR_SHORE_BASE + 7 ) ; // SLOPE_WSE
DupSprite ( SPR_FLAT_GRASS_TILE + 15 , SPR_SHORE_BASE + 10 ) ; // SLOPE_STEEP_N
DupSprite ( SPR_FLAT_GRASS_TILE + 11 , SPR_SHORE_BASE + 11 ) ; // SLOPE_NWS
DupSprite ( SPR_FLAT_GRASS_TILE + 13 , SPR_SHORE_BASE + 13 ) ; // SLOPE_ENW
DupSprite ( SPR_FLAT_GRASS_TILE + 14 , SPR_SHORE_BASE + 14 ) ; // SLOPE_SEN
DupSprite ( SPR_FLAT_GRASS_TILE + 18 , SPR_SHORE_BASE + 15 ) ; // SLOPE_STEEP_E
/* XXX - SLOPE_EW, SLOPE_NS are currently not used.
* If they would be used somewhen , then these grass tiles will most like not look as needed */
DupSprite ( SPR_FLAT_GRASS_TILE + 5 , SPR_SHORE_BASE + 16 ) ; // SLOPE_EW
DupSprite ( SPR_FLAT_GRASS_TILE + 10 , SPR_SHORE_BASE + 17 ) ; // SLOPE_NS
}
}
2019-04-06 08:46:15 +02:00
/**
* Replocate the old tram depot sprites to the new position , if no new ones were loaded .
*/
static void ActivateOldTramDepot ( )
{
if ( _loaded_newgrf_features . tram = = TRAMWAY_REPLACE_DEPOT_WITH_TRACK ) {
DupSprite ( SPR_ROAD_DEPOT + 0 , SPR_TRAMWAY_DEPOT_NO_TRACK + 0 ) ; // use road depot graphics for "no tracks"
DupSprite ( SPR_TRAMWAY_DEPOT_WITH_TRACK + 1 , SPR_TRAMWAY_DEPOT_NO_TRACK + 1 ) ;
DupSprite ( SPR_ROAD_DEPOT + 2 , SPR_TRAMWAY_DEPOT_NO_TRACK + 2 ) ; // use road depot graphics for "no tracks"
DupSprite ( SPR_TRAMWAY_DEPOT_WITH_TRACK + 3 , SPR_TRAMWAY_DEPOT_NO_TRACK + 3 ) ;
DupSprite ( SPR_TRAMWAY_DEPOT_WITH_TRACK + 4 , SPR_TRAMWAY_DEPOT_NO_TRACK + 4 ) ;
DupSprite ( SPR_TRAMWAY_DEPOT_WITH_TRACK + 5 , SPR_TRAMWAY_DEPOT_NO_TRACK + 5 ) ;
}
}
2009-11-24 14:15:58 +01:00
/**
* Decide whether price base multipliers of grfs shall apply globally or only to the grf specifying them
*/
static void FinalisePriceBaseMultipliers ( )
{
extern const PriceBaseSpec _price_base_specs [ ] ;
2011-03-05 19:27:43 +01:00
/** Features, to which '_grf_id_overrides' applies. Currently vehicle features only. */
2010-04-24 13:52:58 +02:00
static const uint32 override_features = ( 1 < < GSF_TRAINS ) | ( 1 < < GSF_ROADVEHICLES ) | ( 1 < < GSF_SHIPS ) | ( 1 < < GSF_AIRCRAFT ) ;
2009-11-24 14:15:58 +01:00
/* Evaluate grf overrides */
2019-03-28 00:09:33 +01:00
int num_grfs = ( uint ) _grf_files . size ( ) ;
2021-05-01 22:06:17 +02:00
std : : vector < int > grf_overrides ( num_grfs , - 1 ) ;
2009-11-24 14:15:58 +01:00
for ( int i = 0 ; i < num_grfs ; i + + ) {
GRFFile * source = _grf_files [ i ] ;
uint32 override = _grf_id_overrides [ source - > grfid ] ;
if ( override = = 0 ) continue ;
GRFFile * dest = GetFileByGRFID ( override ) ;
2019-04-10 23:07:06 +02:00
if ( dest = = nullptr ) continue ;
2009-11-24 14:15:58 +01:00
2019-02-20 20:27:10 +01:00
grf_overrides [ i ] = find_index ( _grf_files , dest ) ;
2009-11-24 14:15:58 +01:00
assert ( grf_overrides [ i ] > = 0 ) ;
}
/* Override features and price base multipliers of earlier loaded grfs */
for ( int i = 0 ; i < num_grfs ; i + + ) {
if ( grf_overrides [ i ] < 0 | | grf_overrides [ i ] > = i ) continue ;
GRFFile * source = _grf_files [ i ] ;
GRFFile * dest = _grf_files [ grf_overrides [ i ] ] ;
uint32 features = ( source - > grf_features | dest - > grf_features ) & override_features ;
source - > grf_features | = features ;
dest - > grf_features | = features ;
for ( Price p = PR_BEGIN ; p < PR_END ; p + + ) {
/* No price defined -> nothing to do */
2009-11-25 00:34:46 +01:00
if ( ! HasBit ( features , _price_base_specs [ p ] . grf_feature ) | | source - > price_base_multipliers [ p ] = = INVALID_PRICE_MODIFIER ) continue ;
2021-06-12 09:10:17 +02:00
Debug ( grf , 3 , " '{}' overrides price base multiplier {} of '{}' " , source - > filename , p , dest - > filename ) ;
2009-11-24 14:15:58 +01:00
dest - > price_base_multipliers [ p ] = source - > price_base_multipliers [ p ] ;
}
}
/* Propagate features and price base multipliers of afterwards loaded grfs, if none is present yet */
for ( int i = num_grfs - 1 ; i > = 0 ; i - - ) {
if ( grf_overrides [ i ] < 0 | | grf_overrides [ i ] < = i ) continue ;
GRFFile * source = _grf_files [ i ] ;
GRFFile * dest = _grf_files [ grf_overrides [ i ] ] ;
uint32 features = ( source - > grf_features | dest - > grf_features ) & override_features ;
source - > grf_features | = features ;
dest - > grf_features | = features ;
for ( Price p = PR_BEGIN ; p < PR_END ; p + + ) {
/* Already a price defined -> nothing to do */
2009-11-25 00:34:46 +01:00
if ( ! HasBit ( features , _price_base_specs [ p ] . grf_feature ) | | dest - > price_base_multipliers [ p ] ! = INVALID_PRICE_MODIFIER ) continue ;
2021-06-12 09:10:17 +02:00
Debug ( grf , 3 , " Price base multiplier {} from '{}' propagated to '{}' " , p , source - > filename , dest - > filename ) ;
2009-11-24 14:15:58 +01:00
dest - > price_base_multipliers [ p ] = source - > price_base_multipliers [ p ] ;
}
}
/* The 'master grf' now have the correct multipliers. Assign them to the 'addon grfs' to make everything consistent. */
for ( int i = 0 ; i < num_grfs ; i + + ) {
if ( grf_overrides [ i ] < 0 ) continue ;
GRFFile * source = _grf_files [ i ] ;
GRFFile * dest = _grf_files [ grf_overrides [ i ] ] ;
uint32 features = ( source - > grf_features | dest - > grf_features ) & override_features ;
source - > grf_features | = features ;
dest - > grf_features | = features ;
for ( Price p = PR_BEGIN ; p < PR_END ; p + + ) {
if ( ! HasBit ( features , _price_base_specs [ p ] . grf_feature ) ) continue ;
if ( source - > price_base_multipliers [ p ] ! = dest - > price_base_multipliers [ p ] ) {
2021-06-12 09:10:17 +02:00
Debug ( grf , 3 , " Price base multiplier {} from '{}' propagated to '{}' " , p , dest - > filename , source - > filename ) ;
2009-11-24 14:15:58 +01:00
}
source - > price_base_multipliers [ p ] = dest - > price_base_multipliers [ p ] ;
}
}
2011-11-08 18:24:01 +01:00
/* Apply fallback prices for grf version < 8 */
2019-02-17 12:20:52 +01:00
for ( GRFFile * const file : _grf_files ) {
if ( file - > grf_version > = 8 ) continue ;
PriceMultipliers & price_base_multipliers = file - > price_base_multipliers ;
2009-11-24 23:15:42 +01:00
for ( Price p = PR_BEGIN ; p < PR_END ; p + + ) {
Price fallback_price = _price_base_specs [ p ] . fallback_price ;
2009-11-25 00:34:46 +01:00
if ( fallback_price ! = INVALID_PRICE & & price_base_multipliers [ p ] = = INVALID_PRICE_MODIFIER ) {
2009-11-24 23:15:42 +01:00
/* No price multiplier has been set.
* So copy the multiplier from the fallback price , maybe a multiplier was set there . */
price_base_multipliers [ p ] = price_base_multipliers [ fallback_price ] ;
}
}
}
/* Decide local/global scope of price base multipliers */
2019-02-17 12:20:52 +01:00
for ( GRFFile * const file : _grf_files ) {
PriceMultipliers & price_base_multipliers = file - > price_base_multipliers ;
2009-11-24 14:15:58 +01:00
for ( Price p = PR_BEGIN ; p < PR_END ; p + + ) {
2009-11-25 00:34:46 +01:00
if ( price_base_multipliers [ p ] = = INVALID_PRICE_MODIFIER ) {
2009-11-24 14:15:58 +01:00
/* No multiplier was set; set it to a neutral value */
price_base_multipliers [ p ] = 0 ;
} else {
2019-02-17 12:20:52 +01:00
if ( ! HasBit ( file - > grf_features , _price_base_specs [ p ] . grf_feature ) ) {
2009-11-24 14:15:58 +01:00
/* The grf does not define any objects of the feature,
* so it must be a difficulty setting . Apply it globally */
2021-06-12 09:10:17 +02:00
Debug ( grf , 3 , " '{}' sets global price base multiplier {} " , file - > filename , p ) ;
2009-11-24 14:15:58 +01:00
SetPriceBaseMultiplier ( p , price_base_multipliers [ p ] ) ;
price_base_multipliers [ p ] = 0 ;
} else {
2021-06-12 09:10:17 +02:00
Debug ( grf , 3 , " '{}' sets local price base multiplier {} " , file - > filename , p ) ;
2009-11-24 14:15:58 +01:00
}
}
}
}
}
2008-01-17 03:10:26 +01:00
extern void InitGRFTownGeneratorNames ( ) ;
2007-06-19 01:00:55 +02:00
2011-09-11 13:14:22 +02:00
/** Finish loading NewGRFs and execute needed post-processing */
2007-03-19 12:27:30 +01:00
static void AfterLoadGRFs ( )
{
2019-02-17 12:20:52 +01:00
for ( StringIDMapping & it : _string_to_grf_mapping ) {
* it . target = MapGRFStringID ( it . grfid , it . source ) ;
2008-04-17 13:47:22 +02:00
}
2018-09-21 00:44:14 +02:00
_string_to_grf_mapping . clear ( ) ;
2008-04-17 13:47:22 +02:00
2023-04-26 07:45:11 +02:00
/* Clear the action 6 override sprites. */
2008-08-19 11:08:45 +02:00
_grf_line_to_action6_sprite_override . clear ( ) ;
2011-12-15 22:56:00 +01:00
/* Polish cargoes */
2010-05-21 22:51:00 +02:00
FinaliseCargoArray ( ) ;
2007-03-19 12:27:30 +01:00
/* Pre-calculate all refit masks after loading GRF files. */
CalculateRefitMasks ( ) ;
2010-05-21 22:51:00 +02:00
/* Polish engines */
FinaliseEngineArray ( ) ;
2011-09-11 11:31:57 +02:00
/* Set the actually used Canal properties */
FinaliseCanals ( ) ;
2007-03-19 12:27:30 +01:00
/* Add all new houses to the house array. */
FinaliseHouseArray ( ) ;
2007-03-23 00:19:40 +01:00
2007-07-06 22:40:58 +02:00
/* Add all new industries to the industry array. */
FinaliseIndustriesArray ( ) ;
2010-08-28 20:21:09 +02:00
/* Add all new objects to the object array. */
FinaliseObjectsArray ( ) ;
2010-04-01 21:48:28 +02:00
InitializeSortedCargoSpecs ( ) ;
2010-03-28 14:25:09 +02:00
/* Sort the list of industry types. */
SortIndustryTypes ( ) ;
2007-04-26 22:20:12 +02:00
/* Create dynamic list of industry legends for smallmap_gui.cpp */
BuildIndustriesLegend ( ) ;
2013-05-19 16:36:35 +02:00
/* Build the routemap legend, based on the available cargos */
BuildLinkStatsLegend ( ) ;
2010-02-22 15:16:19 +01:00
/* Add all new airports to the airports array. */
FinaliseAirportsArray ( ) ;
2010-03-06 00:21:35 +01:00
BindAirportSpecs ( ) ;
2010-02-22 15:16:19 +01:00
2007-06-19 01:00:55 +02:00
/* Update the townname generators list */
2008-01-17 03:10:26 +01:00
InitGRFTownGeneratorNames ( ) ;
2008-01-24 15:49:40 +01:00
2008-04-29 23:31:29 +02:00
/* Run all queued vehicle list order changes */
2008-07-20 23:23:28 +02:00
CommitVehicleListOrderChanges ( ) ;
2008-04-29 23:31:29 +02:00
2008-01-24 15:49:40 +01:00
/* Load old shore sprites in new position, if they were replaced by ActionA */
ActivateOldShore ( ) ;
2008-04-22 20:20:41 +02:00
2019-04-06 08:46:15 +02:00
/* Load old tram depot sprites in new position, if no new ones are present */
ActivateOldTramDepot ( ) ;
2010-02-07 23:22:54 +01:00
/* Set up custom rail types */
InitRailTypes ( ) ;
2019-04-06 08:46:15 +02:00
InitRoadTypes ( ) ;
2010-02-07 23:22:54 +01:00
2019-12-15 22:45:18 +01:00
for ( Engine * e : Engine : : IterateType ( VEH_ROAD ) ) {
2009-01-06 15:45:38 +01:00
if ( _gted [ e - > index ] . rv_max_speed ! = 0 ) {
/* Set RV maximum speed from the mph/0.8 unit value */
e - > u . road . max_speed = _gted [ e - > index ] . rv_max_speed * 4 ;
}
2019-04-06 08:46:15 +02:00
RoadTramType rtt = HasBit ( e - > info . misc_flags , EF_ROAD_TRAM ) ? RTT_TRAM : RTT_ROAD ;
const GRFFile * file = e - > GetGRF ( ) ;
if ( file = = nullptr | | _gted [ e - > index ] . roadtramtype = = 0 ) {
e - > u . road . roadtype = ( rtt = = RTT_TRAM ) ? ROADTYPE_TRAM : ROADTYPE_ROAD ;
continue ;
}
/* Remove +1 offset. */
_gted [ e - > index ] . roadtramtype - - ;
const std : : vector < RoadTypeLabel > * list = ( rtt = = RTT_TRAM ) ? & file - > tramtype_list : & file - > roadtype_list ;
if ( _gted [ e - > index ] . roadtramtype < list - > size ( ) )
{
RoadTypeLabel rtl = ( * list ) [ _gted [ e - > index ] . roadtramtype ] ;
RoadType rt = GetRoadTypeByLabel ( rtl ) ;
if ( rt ! = INVALID_ROADTYPE & & GetRoadTramType ( rt ) = = rtt ) {
e - > u . road . roadtype = rt ;
continue ;
}
}
/* Road type is not available, so disable this engine */
e - > info . climates = 0 ;
2009-01-06 15:45:38 +01:00
}
2019-12-15 22:45:18 +01:00
for ( Engine * e : Engine : : IterateType ( VEH_TRAIN ) ) {
2010-03-23 12:05:28 +01:00
RailType railtype = GetRailTypeByLabel ( _gted [ e - > index ] . railtypelabel ) ;
if ( railtype = = INVALID_RAILTYPE ) {
/* Rail type is not available, so disable this engine */
2011-03-13 23:05:29 +01:00
e - > info . climates = 0 ;
2010-03-23 12:05:28 +01:00
} else {
e - > u . rail . railtype = railtype ;
2023-01-06 01:44:57 +01:00
e - > u . rail . intended_railtype = railtype ;
2010-03-23 12:05:28 +01:00
}
}
2009-01-08 22:48:59 +01:00
SetYearEngineAgingStops ( ) ;
2009-11-24 14:15:58 +01:00
FinalisePriceBaseMultipliers ( ) ;
2008-04-22 20:20:41 +02:00
/* Deallocate temporary loading data */
2023-04-20 21:38:48 +02:00
_gted . clear ( ) ;
2008-05-05 21:02:10 +02:00
_grm_sprites . clear ( ) ;
2007-03-19 12:27:30 +01:00
}
2011-08-24 15:48:29 +02:00
/**
* Load all the NewGRFs .
* @ param load_index The offset for the first sprite to add .
2017-01-14 16:48:19 +01:00
* @ param num_baseset Number of NewGRFs at the front of the list to look up in the baseset dir instead of the newgrf dir .
2011-08-24 15:48:29 +02:00
*/
2021-04-14 17:41:45 +02:00
void LoadNewGRF ( uint load_index , uint num_baseset )
2005-08-15 13:39:13 +02:00
{
2008-12-29 21:36:12 +01:00
/* In case of networking we need to "sync" the start values
* so all NewGRFs are loaded equally . For this we use the
* start date of the game and we set the counters , etc . to
* 0 so they ' re the same too . */
2023-04-24 20:33:18 +02:00
TimerGameCalendar : : Date date = TimerGameCalendar : : date ;
TimerGameCalendar : : Year year = TimerGameCalendar : : year ;
TimerGameCalendar : : DateFract date_fract = TimerGameCalendar : : date_fract ;
2023-04-24 18:55:40 +02:00
uint64 tick_counter = TimerGameTick : : counter ;
2008-12-29 21:36:12 +01:00
byte display_opt = _display_opt ;
if ( _networking ) {
2023-04-24 17:56:01 +02:00
TimerGameCalendar : : year = _settings_game . game_creation . starting_year ;
2023-05-04 15:14:12 +02:00
TimerGameCalendar : : date = TimerGameCalendar : : ConvertYMDToDate ( TimerGameCalendar : : year , 0 , 1 ) ;
2023-04-24 17:56:01 +02:00
TimerGameCalendar : : date_fract = 0 ;
2023-04-24 18:55:40 +02:00
TimerGameTick : : counter = 0 ;
2008-12-29 21:36:12 +01:00
_display_opt = 0 ;
}
2006-01-30 15:50:55 +01:00
InitializeGRFSpecial ( ) ;
2005-08-15 13:39:13 +02:00
2005-09-26 20:43:58 +02:00
ResetNewGRFData ( ) ;
2007-12-30 00:49:48 +01:00
/*
* Reset the status of all files , so we can ' retry ' to load them .
* This is needed when one for example rearranges the NewGRFs in - game
2019-09-29 22:27:32 +02:00
* and a previously disabled NewGRF becomes usable . If it would not
2007-12-30 00:49:48 +01:00
* be reset , the NewGRF would remain disabled even though it should
* have been enabled .
*/
2019-04-10 23:07:06 +02:00
for ( GRFConfig * c = _grfconfig ; c ! = nullptr ; c = c - > next ) {
2007-12-30 00:49:48 +01:00
if ( c - > status ! = GCS_NOT_FOUND ) c - > status = GCS_UNKNOWN ;
}
2011-08-21 14:05:47 +02:00
_cur . spriteid = load_index ;
2008-05-05 21:02:10 +02:00
2005-08-15 13:39:13 +02:00
/* Load newgrf sprites
* in each loading stage , ( try to ) open each file specified in the config
* and load information from it . */
2007-11-04 23:21:46 +01:00
for ( GrfLoadingStage stage = GLS_LABELSCAN ; stage < = GLS_ACTIVATION ; stage + + ) {
2008-03-04 11:41:16 +01:00
/* Set activated grfs back to will-be-activated between reservation- and activation-stage.
* This ensures that action7 / 9 conditions 0x06 - 0x0A work correctly . */
2019-04-10 23:07:06 +02:00
for ( GRFConfig * c = _grfconfig ; c ! = nullptr ; c = c - > next ) {
2008-03-04 11:41:16 +01:00
if ( c - > status = = GCS_ACTIVATED ) c - > status = GCS_INITIALISED ;
}
2011-11-15 21:30:58 +01:00
if ( stage = = GLS_RESERVE ) {
static const uint32 overrides [ ] [ 2 ] = {
{ 0x44442202 , 0x44440111 } , // UKRS addons modifies UKRS
{ 0x6D620402 , 0x6D620401 } , // DBSetXL ECS extension modifies DBSetXL
{ 0x4D656f20 , 0x4D656F17 } , // LV4cut modifies LV4
} ;
for ( size_t i = 0 ; i < lengthof ( overrides ) ; i + + ) {
SetNewGRFOverride ( BSWAP32 ( overrides [ i ] [ 0 ] ) , BSWAP32 ( overrides [ i ] [ 1 ] ) ) ;
}
}
2021-04-14 17:41:45 +02:00
uint num_grfs = 0 ;
2017-01-14 14:12:49 +01:00
uint num_non_static = 0 ;
2005-08-15 13:39:13 +02:00
2011-08-21 14:05:47 +02:00
_cur . stage = stage ;
2019-04-10 23:07:06 +02:00
for ( GRFConfig * c = _grfconfig ; c ! = nullptr ; c = c - > next ) {
2007-03-06 20:33:28 +01:00
if ( c - > status = = GCS_DISABLED | | c - > status = = GCS_NOT_FOUND ) continue ;
2007-11-19 22:02:30 +01:00
if ( stage > GLS_INIT & & HasBit ( c - > flags , GCF_INIT_ONLY ) ) continue ;
2006-12-04 09:30:04 +01:00
2021-04-14 17:41:45 +02:00
Subdirectory subdir = num_grfs < num_baseset ? BASESET_DIR : NEWGRF_DIR ;
2011-08-24 15:48:29 +02:00
if ( ! FioCheckFileExists ( c - > filename , subdir ) ) {
2021-06-12 09:10:17 +02:00
Debug ( grf , 0 , " NewGRF file is missing '{}'; disabling " , c - > filename ) ;
2009-06-18 11:47:06 +02:00
c - > status = GCS_NOT_FOUND ;
continue ;
}
2006-04-22 15:17:13 +02:00
2011-05-01 22:46:36 +02:00
if ( stage = = GLS_LABELSCAN ) InitNewGRFFile ( c ) ;
2017-01-14 14:12:49 +01:00
if ( ! HasBit ( c - > flags , GCF_STATIC ) & & ! HasBit ( c - > flags , GCF_SYSTEM ) ) {
if ( num_non_static = = NETWORK_MAX_GRF_COUNT ) {
2021-06-12 09:10:17 +02:00
Debug ( grf , 0 , " '{}' is not loaded as the maximum number of non-static GRFs has been reached " , c - > filename ) ;
2017-01-14 14:12:49 +01:00
c - > status = GCS_DISABLED ;
2023-06-25 12:57:58 +02:00
c - > error = { STR_NEWGRF_ERROR_MSG_FATAL , STR_NEWGRF_ERROR_TOO_MANY_NEWGRFS_LOADED } ;
2017-01-14 14:12:49 +01:00
continue ;
}
num_non_static + + ;
}
2021-04-14 17:41:45 +02:00
2021-07-11 11:39:24 +02:00
num_grfs + + ;
2021-04-14 17:41:45 +02:00
LoadNewGRFFile ( c , stage , subdir , false ) ;
2007-06-07 20:13:31 +02:00
if ( stage = = GLS_RESERVE ) {
2007-11-20 14:35:54 +01:00
SetBit ( c - > flags , GCF_RESERVED ) ;
2007-11-04 23:21:46 +01:00
} else if ( stage = = GLS_ACTIVATION ) {
2007-11-19 22:32:20 +01:00
ClrBit ( c - > flags , GCF_RESERVED ) ;
2011-08-21 14:05:47 +02:00
assert ( GetFileByGRFID ( c - > ident . grfid ) = = _cur . grffile ) ;
ClearTemporaryNewGRFData ( _cur . grffile ) ;
2007-02-25 00:20:21 +01:00
BuildCargoTranslationMap ( ) ;
2021-06-12 09:10:17 +02:00
Debug ( sprite , 2 , " LoadNewGRF: Currently {} sprites are loaded " , _cur . spriteid ) ;
2009-01-24 18:02:59 +01:00
} else if ( stage = = GLS_INIT & & HasBit ( c - > flags , GCF_INIT_ONLY ) ) {
/* We're not going to activate this, so free whatever data we allocated */
2011-08-21 14:05:47 +02:00
ClearTemporaryNewGRFData ( _cur . grffile ) ;
2007-01-02 21:00:10 +01:00
}
2005-08-15 13:39:13 +02:00
}
}
2005-11-06 14:42:26 +01:00
2011-09-11 17:08:17 +02:00
/* Pseudo sprite processing is finished; free temporary stuff */
_cur . ClearDataForNextFile ( ) ;
2007-03-19 12:27:30 +01:00
/* Call any functions that should be run after GRFs have been loaded. */
AfterLoadGRFs ( ) ;
2008-12-29 21:36:12 +01:00
/* Now revert back to the original situation */
2023-04-24 17:56:01 +02:00
TimerGameCalendar : : year = year ;
TimerGameCalendar : : date = date ;
TimerGameCalendar : : date_fract = date_fract ;
2023-04-24 18:55:40 +02:00
TimerGameTick : : counter = tick_counter ;
2008-12-29 21:36:12 +01:00
_display_opt = display_opt ;
2004-08-09 19:04:08 +02:00
}