2005-07-24 14:12:37 +00:00
/* $Id$ */
2009-08-21 20:21:05 +00:00
/*
* This file is part of OpenTTD .
* OpenTTD is free software ; you can redistribute it and / or modify it under the terms of the GNU General Public License as published by the Free Software Foundation , version 2.
* OpenTTD is distributed in the hope that it will be useful , but WITHOUT ANY WARRANTY ; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE .
* See the GNU General Public License for more details . You should have received a copy of the GNU General Public License along with OpenTTD . If not , see < http : //www.gnu.org/licenses/>.
*/
2008-05-06 15:11:33 +00:00
/** @file newgrf.cpp Base of all NewGRF support. */
2007-03-21 03:06:21 +00:00
2004-08-09 17:04:08 +00:00
# include "stdafx.h"
# include <stdarg.h>
2005-02-05 15:58:59 +00:00
# include "debug.h"
2008-08-31 10:50:05 +00:00
# include "fileio_func.h"
2008-03-31 00:17:39 +00:00
# include "engine_func.h"
2008-04-29 21:31:29 +00:00
# include "engine_base.h"
2005-10-01 17:38:48 +00:00
# include "bridge.h"
2007-03-19 11:27:30 +00:00
# include "town.h"
2006-02-03 12:55:21 +00:00
# include "newgrf_engine.h"
2006-04-21 03:00:20 +00:00
# include "newgrf_text.h"
2006-11-16 22:05:33 +00:00
# include "fontcache.h"
2006-08-25 00:41:10 +00:00
# include "currency.h"
2007-03-20 13:47:00 +00:00
# include "landscape.h"
2010-01-15 16:41:15 +00:00
# include "newgrf.h"
2009-11-05 19:46:17 +00:00
# include "newgrf_cargo.h"
2007-03-19 11:27:30 +00:00
# include "newgrf_house.h"
2006-09-27 18:17:01 +00:00
# include "newgrf_sound.h"
2008-03-31 06:42:26 +00:00
# include "newgrf_station.h"
2010-08-26 22:01:16 +00:00
# include "industrytype.h"
2007-05-06 18:14:33 +00:00
# include "newgrf_canal.h"
2007-06-18 23:00:55 +00:00
# include "newgrf_townname.h"
2007-07-11 22:57:47 +00:00
# include "newgrf_industries.h"
2010-02-22 14:15:48 +00:00
# include "newgrf_airporttiles.h"
2010-03-05 23:21:23 +00:00
# include "newgrf_airport.h"
2010-08-28 17:29:12 +00:00
# include "newgrf_object.h"
2008-05-04 21:53:36 +00:00
# include "rev.h"
2007-10-29 23:02:31 +00:00
# include "fios.h"
2007-12-21 19:49:27 +00:00
# include "strings_func.h"
2007-12-26 13:50:40 +00:00
# include "date_func.h"
2008-01-07 14:23:25 +00:00
# include "string_func.h"
2008-06-19 10:20:45 +00:00
# include "network/network.h"
2008-04-29 21:31:29 +00:00
# include <map>
2010-02-08 21:19:41 +00:00
# include "smallmap_gui.h"
2010-07-19 17:17:36 +00:00
# include "genworld.h"
2010-08-19 19:23:38 +00:00
# include "gui.h"
2010-09-25 21:59:22 +00:00
# include "vehicle_func.h"
2010-11-16 12:47:22 +00:00
# include "language.h"
2010-11-18 14:20:03 +00:00
# include "vehicle_base.h"
2006-04-20 20:51:57 +00:00
2008-01-13 01:21:35 +00:00
# include "table/strings.h"
# include "table/build_industry.h"
2004-08-09 17:04:08 +00:00
/* TTDPatch extended GRF format codec
* ( c ) Petr Baudis 2004 ( GPL ' d )
2004-11-12 15:15:56 +00:00
* Changes by Florian octo Forster are ( c ) by the OpenTTD development team .
*
2004-08-09 17:04:08 +00:00
* Contains portions of documentation by TTDPatch team .
* Thanks especially to Josef Drexler for the documentation as well as a lot
* of help at # tycoon . Also thanks to Michael Blunck for is GRF files which
* served as subject to the initial testing of this codec . */
2011-08-21 12:05:47 +00:00
/** List of all loaded GRF files */
2009-11-23 13:14:45 +00:00
static SmallVector < GRFFile * , 16 > _grf_files ;
2011-08-21 12:05:47 +00:00
/** Miscellaneous GRF features, set by Action 0x0D, parameter 0x9E */
2006-05-11 12:41:02 +00:00
static byte _misc_grf_features = 0 ;
2011-08-21 12:05:47 +00:00
/** 32 * 8 = 256 flags. Apparently TTDPatch uses this many.. */
2004-11-12 19:03:37 +00:00
static uint32 _ttdpatch_flags [ 8 ] ;
2011-08-21 12:05:47 +00:00
/** Indicates which are the newgrf features currently loaded ingame */
2007-06-15 23:55:52 +00:00
GRFLoadedFeatures _loaded_newgrf_features ;
2004-11-12 19:03:37 +00:00
2007-03-07 12:11:48 +00:00
enum GrfDataType {
2006-09-27 18:17:01 +00:00
GDT_SOUND ,
2007-03-07 12:11:48 +00:00
} ;
2006-09-27 18:17:01 +00:00
2011-08-21 12:05:47 +00:00
/** Temporary data during loading of GRFs */
struct GrfProcessingState {
/* Global state */
GrfLoadingStage stage ; ///< Current loading stage
SpriteID spriteid ; ///< First available SpriteID for loading realsprites.
/* Local state in the file */
uint file_index ; ///< File index of currently processed GRF file.
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 */
int skip_sprites ; ///< Number of psuedo sprites to skip before processing the next one. (-1 to skip to end of file)
byte data_blocks ; ///< Number of binary include sprites to read before processing the next pseudo sprite.
GrfDataType data_type ; ///< Type of the binary include sprites to read.
} ;
static GrfProcessingState _cur ;
2006-09-27 18:17:01 +00:00
2010-01-22 13:02:15 +00:00
class OTTDByteReaderSignal { } ;
2006-09-27 18:17:01 +00:00
2010-01-22 13:02:15 +00:00
class ByteReader {
protected :
byte * data ;
byte * end ;
public :
ByteReader ( byte * data , byte * end ) : data ( data ) , end ( end ) { }
FORCEINLINE byte ReadByte ( )
{
if ( data < end ) return * ( data ) + + ;
throw OTTDByteReaderSignal ( ) ;
}
2010-01-23 13:17:30 +00:00
uint16 ReadWord ( )
{
uint16 val = ReadByte ( ) ;
return val | ( ReadByte ( ) < < 8 ) ;
}
2010-03-10 07:31:08 +00:00
uint16 ReadExtendedByte ( )
2010-01-23 13:17:30 +00: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 ' ;
grfmsg ( 7 , " String was not terminated with a zero byte. " ) ;
} else {
/* Increase the string length to include the NUL byte. */
string_length + + ;
}
Skip ( string_length ) ;
return string ;
}
2010-01-22 13:02:15 +00:00
FORCEINLINE size_t Remaining ( ) const
{
2010-01-22 15:34:22 +00:00
return end - data ;
2010-01-22 13:02:15 +00:00
}
2011-05-18 18:31:23 +00:00
FORCEINLINE bool HasData ( size_t count = 1 ) const
2010-01-22 13:02:15 +00:00
{
2011-05-18 18:31:23 +00:00
return data + count < = end ;
2010-01-22 13:02:15 +00:00
}
FORCEINLINE byte * Data ( )
{
return data ;
}
FORCEINLINE void Skip ( size_t len )
{
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 17:04:08 +00:00
2010-05-13 10:14:29 +00:00
static const uint MAX_STATIONS = 256 ;
2006-05-06 22:20:16 +00:00
2008-04-22 18:20:41 +00:00
/* Temporary data used when loading only */
struct GRFTempEngineData {
uint16 cargo_allowed ;
uint16 cargo_disallowed ;
2010-03-23 11:05:28 +00:00
RailTypeLabel railtypelabel ;
2011-04-10 17:21:43 +00:00
const GRFFile * refitmask_grf ; ///< GRF providing the cargo translation table for the refitmask.
2009-09-28 19:24:20 +00:00
bool refitmask_valid ; ///< Did the newgrf set any refittability property? If not, default refittability will be applied.
2011-02-13 19:49:53 +00:00
bool prop27_set ; ///< Did the NewGRF set property 27 (misc flags)?
2009-01-06 14:45:38 +00:00
uint8 rv_max_speed ; ///< Temporary storage of RV prop 15, maximum speed in mph/0.8
2008-04-22 18:20:41 +00:00
} ;
static GRFTempEngineData * _gted ;
2004-08-09 17:04:08 +00:00
2008-04-29 21:31:29 +00:00
/* Contains the GRF ID of the owner of a vehicle if it has been reserved.
* 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 13:15:21 +00:00
2007-06-02 08:56:21 +00:00
/* Contains the GRF ID of the owner of a cargo if it has been reserved */
2008-03-05 09:37:43 +00:00
static uint32 _grm_cargos [ NUM_CARGO * 2 ] ;
2007-06-02 08:56:21 +00:00
2008-05-06 17:24:27 +00:00
struct GRFLocation {
2008-05-05 19:02:10 +00:00
uint32 grfid ;
uint32 nfoline ;
2008-05-06 17:24:27 +00:00
GRFLocation ( uint32 grfid , uint32 nfoline ) : grfid ( grfid ) , nfoline ( nfoline ) { }
2008-05-05 19:02:10 +00:00
2008-05-06 17:24:27 +00:00
bool operator < ( const GRFLocation & other ) const
2008-05-05 19:02:10 +00:00
{
return this - > grfid < other . grfid | | ( this - > grfid = = other . grfid & & this - > nfoline < other . nfoline ) ;
}
2008-08-19 09:08:45 +00:00
2009-07-16 10:13:33 +00:00
bool operator = = ( const GRFLocation & other ) const
2008-08-19 09:08:45 +00:00
{
return this - > grfid = = other . grfid & & this - > nfoline = = other . nfoline ;
}
2008-05-05 19:02:10 +00:00
} ;
2008-05-06 17:24:27 +00:00
static std : : map < GRFLocation , SpriteID > _grm_sprites ;
2008-08-19 09:08:45 +00:00
typedef std : : map < GRFLocation , byte * > GRFLineToSpriteOverride ;
2009-11-09 09:59:35 +00:00
static GRFLineToSpriteOverride _grf_line_to_action6_sprite_override ;
2008-05-05 19:02:10 +00:00
2010-08-01 19:22:34 +00:00
/**
* DEBUG ( ) function dedicated to newGRF debugging messages
2010-07-31 21:02:56 +00:00
* Function is essentially the same as DEBUG ( grf , severity , . . . ) with the
2006-12-26 17:36:18 +00:00
* addition of file : line information when parsing grf files .
* NOTE : for the above reason ( s ) grfmsg ( ) should ONLY be used for
* 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
2010-08-01 19:44:49 +00:00
* @ param str message in printf ( ) format
*/
2006-12-26 17:36:18 +00:00
void CDECL grfmsg ( int severity , const char * str , . . . )
2004-08-09 17:04:08 +00:00
{
2004-08-16 14:48:35 +00:00
char buf [ 1024 ] ;
2004-08-09 17:04:08 +00:00
va_list va ;
va_start ( va , str ) ;
2005-03-10 07:01:43 +00:00
vsnprintf ( buf , sizeof ( buf ) , str , va ) ;
2004-08-09 17:04:08 +00:00
va_end ( va ) ;
2004-11-13 12:21:32 +00:00
2011-08-21 12:05:47 +00:00
DEBUG ( grf , severity , " [%s:%d] %s " , _cur . grfconfig - > filename , _cur . nfo_line , buf ) ;
2004-08-09 17:04:08 +00:00
}
2005-03-10 07:01:43 +00:00
static GRFFile * GetFileByGRFID ( uint32 grfid )
2004-11-12 22:28:19 +00:00
{
2009-11-23 13:14:45 +00:00
const GRFFile * const * end = _grf_files . End ( ) ;
for ( GRFFile * const * file = _grf_files . Begin ( ) ; file ! = end ; file + + ) {
if ( ( * file ) - > grfid = = grfid ) return * file ;
2005-03-10 07:01:43 +00:00
}
2009-11-23 13:14:45 +00:00
return NULL ;
2004-11-12 22:28:19 +00:00
}
2005-03-10 07:01:43 +00:00
static GRFFile * GetFileByFilename ( const char * filename )
2004-11-12 22:28:19 +00:00
{
2009-11-23 13:14:45 +00:00
const GRFFile * const * end = _grf_files . End ( ) ;
for ( GRFFile * const * file = _grf_files . Begin ( ) ; file ! = end ; file + + ) {
if ( strcmp ( ( * file ) - > filename , filename ) = = 0 ) return * file ;
2005-03-10 07:01:43 +00:00
}
2009-11-23 13:14:45 +00:00
return NULL ;
2004-11-12 22:28:19 +00:00
}
2008-07-04 18:42:04 +00:00
/** Reset all NewGRFData that was used only while processing data */
2008-12-24 23:01:16 +00:00
static void ClearTemporaryNewGRFData ( GRFFile * gf )
2008-07-04 18:42:04 +00:00
{
/* Clear the GOTO labels used for GRF processing */
2008-12-24 23:01:16 +00:00
for ( GRFLabel * l = gf - > label ; l ! = NULL ; ) {
2008-07-04 18:42:04 +00:00
GRFLabel * l2 = l - > next ;
free ( l ) ;
l = l2 ;
}
2008-12-24 23:01:16 +00:00
gf - > label = NULL ;
2008-07-04 18:42:04 +00:00
/* Clear the list of spritegroups */
2008-12-24 23:01:16 +00:00
free ( gf - > spritegroups ) ;
gf - > spritegroups = NULL ;
gf - > spritegroups_count = 0 ;
2008-07-04 18:42:04 +00:00
}
2011-05-11 20:22:04 +00:00
/**
* Disable a GRF
* @ param message Error message or STR_NULL
* @ param config GRFConfig to disable , NULL for current
* @ return Error message of the GRF for further customisation
*/
static GRFError * DisableGrf ( StringID message = STR_NULL , GRFConfig * config = NULL )
{
GRFFile * file ;
if ( config ! = NULL ) {
file = GetFileByGRFID ( config - > ident . grfid ) ;
} else {
2011-08-21 12:05:47 +00:00
config = _cur . grfconfig ;
file = _cur . grffile ;
2011-05-11 20:22:04 +00:00
}
config - > status = GCS_DISABLED ;
if ( file ! = NULL ) ClearTemporaryNewGRFData ( file ) ;
2011-08-21 12:05:47 +00:00
if ( config = = _cur . grfconfig ) _cur . skip_sprites = - 1 ;
2011-05-11 20:22:04 +00:00
if ( message ! = STR_NULL ) {
delete config - > error ;
config - > error = new GRFError ( STR_NEWGRF_ERROR_MSG_FATAL , message ) ;
}
return config - > error ;
}
2004-11-12 22:28:19 +00:00
2008-04-17 11:47:22 +00:00
typedef std : : map < StringID * , uint32 > StringIDToGRFIDMapping ;
2009-11-09 09:59:35 +00:00
static StringIDToGRFIDMapping _string_to_grf_mapping ;
2008-04-17 11:47:22 +00:00
2010-08-01 19:22:34 +00:00
/**
* Used when setting an object ' s property to map to the GRF ' s strings
2007-03-19 11:27:30 +00:00
* while taking in consideration the " drift " between TTDPatch string system and OpenTTD ' s one
2007-04-17 20:23:13 +00:00
* @ param grfid Id of the grf file
2007-03-19 11:27:30 +00:00
* @ param str StringID that we want to have the equivalent in OoenTTD
* @ return the properly adjusted StringID
*/
2007-09-22 23:40:35 +00:00
StringID MapGRFStringID ( uint32 grfid , StringID str )
2007-03-19 11:27:30 +00:00
{
/* 0xD0 and 0xDC stand for all the TextIDs in the range
2007-03-21 03:06:21 +00:00
* of 0xD000 ( misc graphics texts ) and 0xDC00 ( misc persistent texts ) .
2007-03-19 11:27:30 +00:00
* These strings are unique to each grf file , and thus require to be used with the
* grfid in which they are declared */
2008-02-29 08:40:49 +00:00
switch ( GB ( str , 8 , 8 ) ) {
case 0xD0 : case 0xD1 : case 0xD2 : case 0xD3 :
case 0xDC :
return GetGRFStringID ( grfid , str ) ;
case 0xD4 : case 0xD5 : case 0xD6 : case 0xD7 :
/* Strings embedded via 0x81 have 0x400 added to them (no real
* explanation why . . . ) */
return GetGRFStringID ( grfid , str - 0x400 ) ;
default : break ;
2007-03-19 11:27:30 +00:00
}
2008-02-29 08:40:49 +00:00
2009-09-20 10:21:00 +00:00
return TTDPStringIDToOTTDStringIDMapping ( str ) ;
2007-03-19 11:27:30 +00:00
}
2008-04-29 21:31:29 +00:00
static std : : map < uint32 , uint32 > _grf_id_overrides ;
static void SetNewGRFOverride ( uint32 source_grfid , uint32 target_grfid )
{
_grf_id_overrides [ source_grfid ] = target_grfid ;
grfmsg ( 5 , " SetNewGRFOverride: Added override of 0x%X to 0x%X " , BSWAP32 ( source_grfid ) , BSWAP32 ( target_grfid ) ) ;
}
2009-03-08 18:08:30 +00:00
/**
* Returns the engine associated to a certain internal_id , resp . allocates it .
* @ param file NewGRF that wants to change the engine
* @ param type Vehicle type
* @ param internal_id Engine ID inside the NewGRF
* @ param static_access If the engine is not present , return NULL instead of allocating a new engine . ( Used for static Action 0x04 )
* @ return The requested engine
*/
static Engine * GetNewEngine ( const GRFFile * file , VehicleType type , uint16 internal_id , bool static_access = false )
2008-04-29 21:31:29 +00: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 16:51:08 +00:00
uint32 scope_grfid = INVALID_GRFID ; // If not using dynamic_engines, all newgrfs share their ID range
2008-05-29 15:13:28 +00:00
if ( _settings_game . vehicle . dynamic_engines ) {
2009-03-08 16:51:08 +00:00
/* If dynamic_engies is enabled, there can be multiple independent ID ranges. */
scope_grfid = file - > grfid ;
2008-04-29 21:31:29 +00:00
uint32 override = _grf_id_overrides [ file - > grfid ] ;
if ( override ! = 0 ) {
2009-03-08 16:51:08 +00:00
scope_grfid = override ;
const GRFFile * grf_match = GetFileByGRFID ( override ) ;
2008-04-29 21:31:29 +00:00
if ( grf_match = = NULL ) {
grfmsg ( 5 , " Tried mapping from GRFID %x to %x but target is not loaded " , BSWAP32 ( file - > grfid ) , BSWAP32 ( override ) ) ;
} else {
grfmsg ( 5 , " Mapping from GRFID %x to %x " , BSWAP32 ( file - > grfid ) , BSWAP32 ( override ) ) ;
}
}
2009-03-08 16:51:08 +00:00
/* Check if the engine is registered in the override manager */
EngineID engine = _engine_mngr . GetID ( type , internal_id , scope_grfid ) ;
2009-07-04 12:20:42 +00:00
if ( engine ! = INVALID_ENGINE ) {
Engine * e = Engine : : Get ( engine ) ;
2010-08-10 15:54:53 +00:00
if ( e - > grf_prop . grffile = = NULL ) e - > grf_prop . grffile = file ;
2009-07-04 12:20:42 +00:00
return e ;
}
2008-04-29 21:31:29 +00:00
}
2009-03-08 16:51:08 +00:00
/* Check if there is an unreserved slot */
EngineID engine = _engine_mngr . GetID ( type , internal_id , INVALID_GRFID ) ;
if ( engine ! = INVALID_ENGINE ) {
2009-05-16 23:34:14 +00:00
Engine * e = Engine : : Get ( engine ) ;
2008-04-29 21:31:29 +00:00
2010-08-10 15:54:53 +00:00
if ( e - > grf_prop . grffile = = NULL ) {
e - > grf_prop . grffile = file ;
2008-04-29 21:31:29 +00:00
grfmsg ( 5 , " Replaced engine at index %d for GRFID %x, type %d, index %d " , e - > index , BSWAP32 ( file - > grfid ) , type , internal_id ) ;
}
2009-03-08 16:51:08 +00:00
/* Reserve the engine slot */
2009-03-08 18:08:30 +00:00
if ( ! static_access ) {
EngineIDMapping * eid = _engine_mngr . Get ( engine ) ;
eid - > grfid = scope_grfid ; // Note: this is INVALID_GRFID if dynamic_engines is disabled, so no reservation
}
2009-03-08 16:51:08 +00:00
2008-04-29 21:31:29 +00:00
return e ;
}
2009-03-08 18:08:30 +00:00
if ( static_access ) return NULL ;
2011-02-09 18:46:55 +00:00
if ( ! Engine : : CanAllocateItem ( ) ) {
grfmsg ( 0 , " Can't allocate any more engines " ) ;
return NULL ;
}
2009-05-22 15:13:50 +00:00
size_t engine_pool_size = Engine : : GetPoolSize ( ) ;
2008-04-29 21:31:29 +00:00
/* ... it's not, so create a new one based off an existing engine */
2009-03-08 16:51:08 +00:00
Engine * e = new Engine ( type , internal_id ) ;
2010-08-10 15:54:53 +00:00
e - > grf_prop . grffile = file ;
2008-04-29 21:31:29 +00:00
2009-03-08 16:51:08 +00:00
/* Reserve the engine slot */
assert ( _engine_mngr . Length ( ) = = e - > index ) ;
EngineIDMapping * eid = _engine_mngr . Append ( ) ;
eid - > type = type ;
eid - > grfid = scope_grfid ; // Note: this is INVALID_GRFID if dynamic_engines is disabled, so no reservation
eid - > internal_id = internal_id ;
eid - > substitute_id = min ( internal_id , _engine_counts [ type ] ) ; // substitute_id == _engine_counts[subtype] means "no substitute"
2009-05-16 23:44:36 +00:00
if ( engine_pool_size ! = Engine : : GetPoolSize ( ) ) {
2008-04-29 21:31:29 +00:00
/* Resize temporary engine data ... */
2009-05-16 23:44:36 +00:00
_gted = ReallocT ( _gted , Engine : : GetPoolSize ( ) ) ;
2008-04-29 21:31:29 +00:00
/* and blank the new block. */
2009-05-16 23:44:36 +00:00
size_t len = ( Engine : : GetPoolSize ( ) - engine_pool_size ) * sizeof ( * _gted ) ;
2008-04-29 21:31:29 +00:00
memset ( _gted + engine_pool_size , 0 , len ) ;
}
2010-04-17 09:12:26 +00:00
if ( type = = VEH_TRAIN ) {
_gted [ e - > index ] . railtypelabel = GetRailTypeInfo ( e - > u . rail . railtype ) - > label ;
}
2008-04-29 21:31:29 +00:00
grfmsg ( 5 , " Created new engine at index %d for GRFID %x, type %d, index %d " , e - > index , BSWAP32 ( file - > grfid ) , type , internal_id ) ;
return e ;
}
2008-04-30 17:42:08 +00:00
EngineID GetNewEngineID ( const GRFFile * file , VehicleType type , uint16 internal_id )
{
2009-03-08 16:51:08 +00:00
uint32 scope_grfid = INVALID_GRFID ; // If not using dynamic_engines, all newgrfs share their ID range
2008-05-29 15:13:28 +00:00
if ( _settings_game . vehicle . dynamic_engines ) {
2009-03-08 16:51:08 +00:00
scope_grfid = file - > grfid ;
2008-04-30 17:42:08 +00:00
uint32 override = _grf_id_overrides [ file - > grfid ] ;
2009-03-08 16:51:08 +00:00
if ( override ! = 0 ) scope_grfid = override ;
2008-04-30 17:42:08 +00:00
}
2009-03-08 16:51:08 +00:00
return _engine_mngr . GetID ( type , internal_id , scope_grfid ) ;
2008-04-30 17:42:08 +00:00
}
2010-08-01 19:22:34 +00:00
/**
* Map the colour modifiers of TTDPatch to those that Open is using .
2008-02-16 03:01:05 +00:00
* @ param grf_sprite pointer to the structure been modified
*/
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 02:57:15 +00:00
SetBit ( grf_sprite - > sprite , PALETTE_MODIFIER_COLOUR ) ;
2008-02-16 03:01:05 +00:00
}
}
2007-03-19 11:27:30 +00:00
2011-05-18 18:31:23 +00: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 16:56:22 +00:00
* @ param read_flags Whether to read TileLayoutFlags .
2011-05-18 18:31:23 +00:00
* @ param invert_action1_flag Set to true , if palette bit 15 means ' not from action 1 ' .
* @ param action1_offset Offset to add to action 1 sprites .
* @ param action1_pitch Factor to multiply action 1 sprite indices with .
* @ param action1_max Maximal valid action 1 index .
* @ param [ out ] grf_sprite Read sprite and palette .
2011-05-29 16:56:22 +00:00
* @ return read TileLayoutFlags .
2011-05-18 18:31:23 +00:00
*/
2011-05-29 16:56:22 +00:00
static TileLayoutFlags ReadSpriteLayoutSprite ( ByteReader * buf , bool read_flags , bool invert_action1_flag , uint action1_offset , uint action1_pitch , uint action1_max , PalSpriteID * grf_sprite )
2011-05-18 18:31:23 +00:00
{
grf_sprite - > sprite = buf - > ReadWord ( ) ;
grf_sprite - > pal = buf - > ReadWord ( ) ;
2011-05-29 16:56:22 +00:00
TileLayoutFlags flags = read_flags ? ( TileLayoutFlags ) buf - > ReadWord ( ) : TLF_NOTHING ;
2011-05-18 18:31:23 +00: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 ) ;
if ( action1_pitch = = 0 | | index > = action1_max ) {
grfmsg ( 1 , " ReadSpriteLayoutSprite: Spritelayout uses undefined custom spriteset %d " , index ) ;
grf_sprite - > sprite = SPR_IMG_QUERY ;
grf_sprite - > pal = PAL_NONE ;
} else {
SpriteID sprite = action1_offset + index * action1_pitch ;
SB ( grf_sprite - > sprite , 0 , SPRITE_WIDTH , sprite ) ;
SetBit ( grf_sprite - > sprite , SPRITE_MODIFIER_CUSTOM_SPRITE ) ;
}
2011-05-29 16:56:22 +00:00
} else if ( ( flags & TLF_SPRITE_VAR10 ) & & ! ( flags & TLF_SPRITE_REG_FLAGS ) ) {
grfmsg ( 1 , " ReadSpriteLayoutSprite: Spritelayout specifies var10 value for non-action-1 sprite " ) ;
DisableGrf ( STR_NEWGRF_ERROR_INVALID_SPRITE_LAYOUT ) ;
return flags ;
2011-05-18 18:31:23 +00:00
}
2011-05-29 16:56:22 +00:00
if ( flags & TLF_CUSTOM_PALETTE ) {
/* Use palette from Action 1 */
uint index = GB ( grf_sprite - > pal , 0 , 14 ) ;
if ( action1_pitch = = 0 | | index > = action1_max ) {
grfmsg ( 1 , " ReadSpriteLayoutSprite: Spritelayout uses undefined custom spriteset %d for 'palette' " , index ) ;
grf_sprite - > pal = PAL_NONE ;
} else {
SpriteID sprite = action1_offset + index * action1_pitch ;
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 ) ) {
grfmsg ( 1 , " ReadSpriteLayoutRegisters: Spritelayout specifies var10 value for non-action-1 palette " ) ;
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 ;
if ( dts - > registers = = NULL ) dts - > AllocateRegisters ( ) ;
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 ) {
grfmsg ( 1 , " ReadSpriteLayoutRegisters: Spritelayout specifies var10 (%d) exceeding the maximal allowed value %d " , regs . sprite_var10 , TLR_MAX_VAR10 ) ;
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 ) {
grfmsg ( 1 , " ReadSpriteLayoutRegisters: Spritelayout specifies var10 (%d) exceeding the maximal allowed value %d " , regs . palette_var10 , TLR_MAX_VAR10 ) ;
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
* @ param action1_offset Offset to add to action 1 sprites
* @ param action1_pitch Factor to multiply action 1 sprite indices with
* @ param action1_max Maximal valid action 1 index
* @ param allow_var10 Whether the spritelayout may specifiy var10 values for resolving multiple action - 1 - 2 - 3 chains
* @ param no_z_position Whether bounding boxes have no Z offset
* @ param dts Layout container to output into
* @ return true on error ( GRF was disabled )
*/
static bool ReadSpriteLayout ( ByteReader * buf , uint num_building_sprites , uint action1_offset , uint action1_pitch , uint action1_max , bool allow_var10 , bool no_z_position , NewGRFSpriteLayout * dts )
{
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
/* Groundsprite */
TileLayoutFlags flags = ReadSpriteLayoutSprite ( buf , has_flags , false , action1_offset , action1_pitch , action1_max , & dts - > ground ) ;
2011-08-21 12:05:47 +00:00
if ( _cur . skip_sprites < 0 ) return true ;
2011-05-29 16:56:22 +00:00
if ( flags & ~ ( valid_flags & ~ TLF_NON_GROUND_FLAGS ) ) {
grfmsg ( 1 , " ReadSpriteLayout: Spritelayout uses invalid flag 0x%x for ground sprite " , flags & ~ ( valid_flags & ~ TLF_NON_GROUND_FLAGS ) ) ;
DisableGrf ( STR_NEWGRF_ERROR_INVALID_SPRITE_LAYOUT ) ;
return true ;
}
ReadSpriteLayoutRegisters ( buf , flags , false , dts , 0 ) ;
2011-08-21 12:05:47 +00:00
if ( _cur . skip_sprites < 0 ) return true ;
2011-05-29 16:56:22 +00:00
for ( uint i = 0 ; i < num_building_sprites ; i + + ) {
DrawTileSeqStruct * seq = const_cast < DrawTileSeqStruct * > ( & dts - > seq [ i ] ) ;
flags = ReadSpriteLayoutSprite ( buf , has_flags , false , action1_offset , action1_pitch , action1_max , & seq - > image ) ;
2011-08-21 12:05:47 +00:00
if ( _cur . skip_sprites < 0 ) return true ;
2011-05-29 16:56:22 +00:00
if ( flags & ~ valid_flags ) {
grfmsg ( 1 , " ReadSpriteLayout: Spritelayout uses unknown flag 0x%x " , flags & ~ valid_flags ) ;
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 12:05:47 +00:00
if ( _cur . skip_sprites < 0 ) return true ;
2011-05-29 16:56:22 +00:00
}
return false ;
2011-05-18 18:31:23 +00:00
}
2009-11-07 22:37:22 +00:00
/**
2009-11-07 22:47:54 +00:00
* Converts TTD ( P ) Base Price pointers into the enum used by OTTD
2009-11-07 22:37:22 +00: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
* @ param index If # base_pointer is valid , # index is assigned to the matching price ; else it is left unchanged
*/
2009-11-07 22:47:54 +00:00
static void ConvertTTDBasePrice ( uint32 base_pointer , const char * error_location , Price * index )
2009-11-07 22:37:22 +00:00
{
/* Special value for 'none' */
if ( base_pointer = = 0 ) {
2009-11-07 22:47:54 +00:00
* index = INVALID_PRICE ;
2009-11-07 22:37:22 +00: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 12:23:02 +00:00
if ( base_pointer < start | | ( base_pointer - start ) % size ! = 0 | | ( base_pointer - start ) / size > = PR_END ) {
2009-11-07 22:37:22 +00:00
grfmsg ( 1 , " %s: Unsupported running cost base 0x%04X, ignoring " , error_location , base_pointer ) ;
return ;
}
2009-11-07 22:47:54 +00:00
* index = ( Price ) ( ( base_pointer - start ) / size ) ;
2009-11-07 22:37:22 +00:00
}
2008-08-31 16:41:27 +00:00
enum ChangeInfoResult {
CIR_SUCCESS , ///< Variable was parsed and read
2011-05-29 16:56:22 +00:00
CIR_DISABLED , ///< GRF was disabled due to error
2008-08-31 16:41:27 +00: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 13:02:15 +00:00
typedef ChangeInfoResult ( * VCI_Handler ) ( uint engine , int numinfo , int prop , ByteReader * buf ) ;
2004-08-09 17:04:08 +00:00
2010-01-23 13:17:30 +00:00
static ChangeInfoResult CommonVehicleChangeInfo ( EngineInfo * ei , int prop , ByteReader * buf )
2008-08-31 09:52:47 +00:00
{
switch ( prop ) {
case 0x00 : // Introduction date
2010-01-23 13:17:30 +00:00
ei - > base_intro = buf - > ReadWord ( ) + DAYS_TILL_ORIGINAL_BASE_YEAR ;
2008-08-31 09:52:47 +00:00
break ;
case 0x02 : // Decay speed
2010-01-23 13:17:30 +00:00
ei - > decay_speed = buf - > ReadByte ( ) ;
2008-08-31 09:52:47 +00:00
break ;
case 0x03 : // Vehicle life
2010-01-23 13:17:30 +00:00
ei - > lifelength = buf - > ReadByte ( ) ;
2008-08-31 09:52:47 +00:00
break ;
case 0x04 : // Model life
2010-01-23 13:17:30 +00:00
ei - > base_life = buf - > ReadByte ( ) ;
2008-08-31 09:52:47 +00:00
break ;
case 0x06 : // Climates available
2010-01-23 13:17:30 +00:00
ei - > climates = buf - > ReadByte ( ) ;
2008-08-31 09:52:47 +00:00
break ;
case 0x07 : // Loading speed
/* Amount of cargo loaded during a vehicle's "loading tick" */
2010-01-23 13:17:30 +00:00
ei - > load_amount = buf - > ReadByte ( ) ;
2008-08-31 09:52:47 +00:00
break ;
default :
2008-08-31 16:41:27 +00:00
return CIR_UNKNOWN ;
2008-08-31 09:52:47 +00:00
}
2008-08-31 16:41:27 +00:00
return CIR_SUCCESS ;
2008-08-31 09:52:47 +00:00
}
2010-01-22 13:02:15 +00:00
static ChangeInfoResult RailVehicleChangeInfo ( uint engine , int numinfo , int prop , ByteReader * buf )
2004-08-09 17:04:08 +00:00
{
2008-08-31 16:41:27 +00:00
ChangeInfoResult ret = CIR_SUCCESS ;
2004-08-09 17:04:08 +00:00
2007-07-04 12:52:43 +00:00
for ( int i = 0 ; i < numinfo ; i + + ) {
2011-08-21 12:05:47 +00:00
Engine * e = GetNewEngine ( _cur . grffile , VEH_TRAIN , engine + i ) ;
2011-02-09 18:46:55 +00:00
if ( e = = NULL ) return CIR_INVALID_ID ; // No engine could be allocated, so neither can any next vehicles
2008-04-29 21:31:29 +00:00
EngineInfo * ei = & e - > info ;
RailVehicleInfo * rvi = & e - > u . rail ;
2007-07-04 12:52:43 +00:00
switch ( prop ) {
case 0x05 : { // Track type
2010-01-23 13:17:30 +00:00
uint8 tracktype = buf - > ReadByte ( ) ;
2004-08-09 17:04:08 +00:00
2011-08-21 12:05:47 +00:00
if ( tracktype < _cur . grffile - > railtype_max ) {
_gted [ e - > index ] . railtypelabel = _cur . grffile - > railtype_list [ tracktype ] ;
2009-02-08 18:11:06 +00:00
break ;
}
2006-02-15 12:53:36 +00:00
switch ( tracktype ) {
2010-03-23 11:05:28 +00: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 12:53:36 +00:00
default :
2006-12-26 17:36:18 +00:00
grfmsg ( 1 , " RailVehicleChangeInfo: Invalid track type %d specified, ignoring " , tracktype ) ;
2006-02-15 12:53:36 +00:00
break ;
}
2010-08-01 18:53:30 +00:00
break ;
}
2006-02-15 12:53:36 +00:00
2007-07-04 12:52:43 +00:00
case 0x08 : // AI passenger service
2008-01-30 10:27:45 +00:00
/* Tells the AI that this engine is designed for
2007-07-04 13:30:33 +00:00
* passenger services and shouldn ' t be used for freight . */
2010-01-23 13:17:30 +00:00
rvi - > ai_passenger_only = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2006-02-27 15:35:22 +00:00
2009-09-22 19:28:57 +00:00
case PROP_TRAIN_SPEED : { // 0x09 Speed (1 unit is 1 km-ish/h)
2010-01-23 13:17:30 +00:00
uint16 speed = buf - > ReadWord ( ) ;
2006-02-18 14:41:24 +00:00
if ( speed = = 0xFFFF ) speed = 0 ;
2004-08-09 17:04:08 +00:00
2007-07-04 12:52:43 +00:00
rvi - > max_speed = speed ;
2010-08-01 18:53:30 +00:00
break ;
}
2006-02-27 15:35:22 +00:00
2009-09-22 19:28:57 +00:00
case PROP_TRAIN_POWER : // 0x0B Power
2010-01-23 13:17:30 +00:00
rvi - > power = buf - > ReadWord ( ) ;
2008-06-05 12:08:20 +00: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 20:39:57 +00:00
break ;
2006-01-07 13:20:02 +00:00
2009-09-22 19:28:57 +00:00
case PROP_TRAIN_RUNNING_COST_FACTOR : // 0x0D Running cost factor
2010-01-23 13:17:30 +00:00
rvi - > running_cost = buf - > ReadByte ( ) ;
2008-05-13 20:39:57 +00:00
break ;
2006-02-27 15:35:22 +00:00
2009-11-07 22:37:22 +00:00
case 0x0E : // Running cost base
2010-01-23 13:17:30 +00:00
ConvertTTDBasePrice ( buf - > ReadDWord ( ) , " RailVehicleChangeInfo " , & rvi - > running_cost_class ) ;
2009-11-07 22:37:22 +00:00
break ;
2006-02-27 15:35:22 +00:00
2007-07-04 12:52:43 +00:00
case 0x12 : { // Sprite ID
2010-01-23 13:17:30 +00:00
uint8 spriteid = buf - > ReadByte ( ) ;
2004-08-09 17:04:08 +00:00
2006-01-27 07:13:58 +00: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 14:41:24 +00:00
if ( spriteid < 0xFD ) spriteid > > = 1 ;
2006-01-27 07:13:58 +00:00
2007-07-04 12:52:43 +00:00
rvi - > image_index = spriteid ;
2010-08-01 18:53:30 +00:00
break ;
}
2006-02-27 15:35:22 +00:00
2007-07-04 12:52:43 +00:00
case 0x13 : { // Dual-headed
2010-01-23 13:17:30 +00:00
uint8 dual = buf - > ReadByte ( ) ;
2004-08-09 17:04:08 +00:00
2004-11-13 12:21:32 +00:00
if ( dual ! = 0 ) {
2007-07-04 12:52:43 +00:00
rvi - > railveh_type = RAILVEH_MULTIHEAD ;
2004-08-09 17:04:08 +00:00
} else {
2007-07-04 12:52:43 +00:00
rvi - > railveh_type = rvi - > power = = 0 ?
2007-01-30 22:00:25 +00:00
RAILVEH_WAGON : RAILVEH_SINGLEHEAD ;
2004-08-09 17:04:08 +00:00
}
2010-08-01 18:53:30 +00:00
break ;
}
2004-08-09 17:04:08 +00:00
2009-09-22 19:28:57 +00:00
case PROP_TRAIN_CARGO_CAPACITY : // 0x14 Cargo capacity
2010-01-23 13:17:30 +00:00
rvi - > capacity = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2006-02-27 15:35:22 +00:00
2007-07-04 12:52:43 +00:00
case 0x15 : { // Cargo type
2010-01-23 13:17:30 +00:00
uint8 ctype = buf - > ReadByte ( ) ;
2004-08-09 17:04:08 +00:00
2007-11-19 21:02:30 +00:00
if ( ctype < NUM_CARGO & & HasBit ( _cargo_mask , ctype ) ) {
2009-08-17 18:33:48 +00:00
ei - > cargo_type = ctype ;
2008-08-30 08:59:55 +00:00
} else if ( ctype = = 0xFF ) {
/* 0xFF is specified as 'use first refittable' */
2009-08-17 18:33:48 +00:00
ei - > cargo_type = CT_INVALID ;
2006-01-12 07:57:40 +00:00
} else {
2009-08-17 18:33:48 +00:00
ei - > cargo_type = CT_INVALID ;
2007-02-24 19:26:45 +00:00
grfmsg ( 2 , " RailVehicleChangeInfo: Invalid cargo type %d, using first refittable " , ctype ) ;
2006-01-12 07:57:40 +00:00
}
2010-08-01 18:53:30 +00:00
break ;
}
2007-07-04 12:52:43 +00:00
2009-09-22 19:28:57 +00:00
case PROP_TRAIN_WEIGHT : // 0x16 Weight
2010-01-23 13:17:30 +00:00
SB ( rvi - > weight , 0 , 8 , buf - > ReadByte ( ) ) ;
2007-07-04 12:52:43 +00:00
break ;
2009-09-22 19:28:57 +00:00
case PROP_TRAIN_COST_FACTOR : // 0x17 Cost factor
2010-01-23 13:17:30 +00:00
rvi - > cost_factor = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
case 0x18 : // AI rank
2009-08-18 16:21:22 +00:00
grfmsg ( 2 , " RailVehicleChangeInfo: Property 0x18 'AI rank' not used by NoAI, ignored. " ) ;
2010-01-23 13:17:30 +00:00
buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00: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 13:17:30 +00:00
uint8 traction = buf - > ReadByte ( ) ;
2007-05-06 15:01:44 +00:00
EngineClass engclass ;
2004-08-09 17:04:08 +00:00
2006-02-18 14:41:24 +00:00
if ( traction < = 0x07 ) {
2007-05-06 15:01:44 +00:00
engclass = EC_STEAM ;
2006-02-18 14:41:24 +00:00
} else if ( traction < = 0x27 ) {
2007-05-06 15:01:44 +00:00
engclass = EC_DIESEL ;
2006-03-29 16:30:26 +00:00
} else if ( traction < = 0x31 ) {
2007-05-06 15:01:44 +00:00
engclass = EC_ELECTRIC ;
2007-05-06 19:44:22 +00:00
} else if ( traction < = 0x37 ) {
engclass = EC_MONORAIL ;
2006-02-18 14:41:24 +00:00
} else if ( traction < = 0x41 ) {
2007-05-06 19:44:22 +00:00
engclass = EC_MAGLEV ;
2006-02-18 14:41:24 +00:00
} else {
2004-08-09 17:04:08 +00:00
break ;
2006-02-18 14:41:24 +00:00
}
2009-02-08 18:11:06 +00:00
2011-08-21 12:05:47 +00:00
if ( _cur . grffile - > railtype_max = = 0 ) {
2009-02-08 18:11:06 +00:00
/* Use traction type to select between normal and electrified
* rail only when no translation list is in place . */
2010-04-04 06:03:52 +00: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 18:11:06 +00:00
}
2004-08-09 17:04:08 +00:00
2007-07-04 12:52:43 +00:00
rvi - > engclass = engclass ;
2010-08-01 18:53:30 +00:00
break ;
}
2006-02-27 15:35:22 +00:00
2008-04-29 21:31:29 +00:00
case 0x1A : // Alter purchase list sort order
2010-03-10 07:31:08 +00:00
AlterVehicleListOrder ( e - > index , buf - > ReadExtendedByte ( ) ) ;
2008-04-29 21:31:29 +00:00
break ;
2006-02-04 21:48:46 +00:00
2007-07-04 12:52:43 +00:00
case 0x1B : // Powered wagons power bonus
2010-01-23 13:17:30 +00:00
rvi - > pow_wag_power = buf - > ReadWord ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2005-06-06 00:19:24 +00:00
2007-07-04 12:52:43 +00:00
case 0x1C : // Refit cost
2010-01-23 13:17:30 +00:00
ei - > refit_cost = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2006-06-09 16:35:07 +00:00
2007-07-04 12:52:43 +00:00
case 0x1D : // Refit cargo
2010-01-23 13:17:30 +00:00
ei - > refit_mask = buf - > ReadDWord ( ) ;
2009-09-28 19:24:20 +00:00
_gted [ e - > index ] . refitmask_valid = true ;
2011-08-21 12:05:47 +00:00
_gted [ e - > index ] . refitmask_grf = _cur . grffile ;
2007-07-04 12:52:43 +00:00
break ;
2004-11-17 01:09:15 +00:00
2007-07-04 12:52:43 +00:00
case 0x1E : // Callback
2010-01-23 13:17:30 +00:00
ei - > callback_mask = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2005-06-06 22:44:11 +00:00
2009-09-22 19:28:57 +00:00
case PROP_TRAIN_TRACTIVE_EFFORT : // 0x1F Tractive effort coefficient
2010-01-23 13:17:30 +00:00
rvi - > tractive_effort = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2006-12-28 13:18:07 +00:00
2007-10-25 23:24:28 +00:00
case 0x20 : // Air drag
2010-08-02 14:51:35 +00:00
rvi - > air_drag = buf - > ReadByte ( ) ;
2007-10-25 23:24:28 +00:00
break ;
2007-07-04 12:52:43 +00:00
case 0x21 : // Shorter vehicle
2010-01-23 13:17:30 +00:00
rvi - > shorten_factor = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2005-06-06 22:44:11 +00:00
2007-07-04 12:52:43 +00:00
case 0x22 : // Visual effect
2010-01-23 13:17:30 +00:00
rvi - > visual_effect = buf - > ReadByte ( ) ;
2010-11-18 14:20:03 +00: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 12:52:43 +00:00
break ;
2005-06-06 00:19:24 +00:00
2007-07-04 12:52:43 +00:00
case 0x23 : // Powered wagons weight bonus
2010-01-23 13:17:30 +00:00
rvi - > pow_wag_weight = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2005-06-06 00:19:24 +00:00
2007-07-04 12:52:43 +00:00
case 0x24 : { // High byte of vehicle weight
2010-01-23 13:17:30 +00:00
byte weight = buf - > ReadByte ( ) ;
2005-10-20 15:15:58 +00:00
2005-10-20 19:44:41 +00:00
if ( weight > 4 ) {
2006-12-26 17:36:18 +00:00
grfmsg ( 2 , " RailVehicleChangeInfo: Nonsensical weight of %d tons, ignoring " , weight < < 8 ) ;
2005-10-20 15:15:58 +00:00
} else {
2007-07-04 12:52:43 +00:00
SB ( rvi - > weight , 8 , 8 , weight ) ;
2005-10-20 15:15:58 +00:00
}
2010-08-01 18:53:30 +00:00
break ;
}
2006-02-27 15:35:22 +00:00
2009-09-22 19:28:57 +00:00
case PROP_TRAIN_USER_DATA : // 0x25 User-defined bit mask to set when checking veh. var. 42
2010-01-23 13:17:30 +00:00
rvi - > user_def_data = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
case 0x26 : // Retire vehicle early
2010-01-23 13:17:30 +00:00
ei - > retire_early = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
case 0x27 : // Miscellaneous flags
2010-01-23 13:17:30 +00:00
ei - > misc_flags = buf - > ReadByte ( ) ;
2007-11-19 21:02:30 +00:00
_loaded_newgrf_features . has_2CC | = HasBit ( ei - > misc_flags , EF_USES_2CC ) ;
2011-02-13 19:49:53 +00:00
_gted [ e - > index ] . prop27_set = true ;
2007-07-04 12:52:43 +00:00
break ;
case 0x28 : // Cargo classes allowed
2010-01-23 13:17:30 +00:00
_gted [ e - > index ] . cargo_allowed = buf - > ReadWord ( ) ;
2009-09-28 19:24:20 +00:00
_gted [ e - > index ] . refitmask_valid = true ;
2007-07-04 12:52:43 +00:00
break ;
case 0x29 : // Cargo classes disallowed
2010-01-23 13:17:30 +00:00
_gted [ e - > index ] . cargo_disallowed = buf - > ReadWord ( ) ;
2009-09-28 19:24:20 +00:00
_gted [ e - > index ] . refitmask_valid = true ;
2007-07-04 12:52:43 +00:00
break ;
case 0x2A : // Long format introduction date (days since year 0)
2010-01-23 13:17:30 +00:00
ei - > base_intro = buf - > ReadDWord ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2011-08-03 20:55:08 +00:00
case PROP_TRAIN_CARGO_AGE_PERIOD : // 0x2B Cargo aging period
ei - > cargo_age_period = buf - > ReadWord ( ) ;
break ;
2007-07-04 12:52:43 +00:00
default :
2010-01-23 13:17:30 +00:00
ret = CommonVehicleChangeInfo ( ei , prop , buf ) ;
2007-07-04 12:52:43 +00:00
break ;
}
2004-08-09 17:04:08 +00:00
}
2007-07-04 12:52:43 +00:00
2004-08-09 17:04:08 +00:00
return ret ;
}
2010-01-22 13:02:15 +00:00
static ChangeInfoResult RoadVehicleChangeInfo ( uint engine , int numinfo , int prop , ByteReader * buf )
2004-11-13 18:01:33 +00:00
{
2008-08-31 16:41:27 +00:00
ChangeInfoResult ret = CIR_SUCCESS ;
2004-11-13 18:01:33 +00:00
2007-07-04 12:52:43 +00:00
for ( int i = 0 ; i < numinfo ; i + + ) {
2011-08-21 12:05:47 +00:00
Engine * e = GetNewEngine ( _cur . grffile , VEH_ROAD , engine + i ) ;
2011-02-09 18:46:55 +00:00
if ( e = = NULL ) return CIR_INVALID_ID ; // No engine could be allocated, so neither can any next vehicles
2008-04-29 21:31:29 +00:00
EngineInfo * ei = & e - > info ;
RoadVehicleInfo * rvi = & e - > u . road ;
2004-11-13 18:01:33 +00:00
2007-07-04 12:52:43 +00:00
switch ( prop ) {
case 0x08 : // Speed (1 unit is 0.5 kmh)
2010-01-23 13:17:30 +00:00
rvi - > max_speed = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2004-11-13 18:01:33 +00:00
2009-11-08 13:02:05 +00:00
case PROP_ROADVEH_RUNNING_COST_FACTOR : // 0x09 Running cost factor
2010-01-23 13:17:30 +00:00
rvi - > running_cost = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2006-02-27 15:35:22 +00:00
2009-11-07 22:37:22 +00:00
case 0x0A : // Running cost base
2010-01-23 13:17:30 +00:00
ConvertTTDBasePrice ( buf - > ReadDWord ( ) , " RoadVehicleChangeInfo " , & rvi - > running_cost_class ) ;
2007-07-04 12:52:43 +00:00
break ;
case 0x0E : { // Sprite ID
2010-01-23 13:17:30 +00:00
uint8 spriteid = buf - > ReadByte ( ) ;
2004-11-13 18:01:33 +00:00
2007-03-21 03:06:21 +00:00
/* cars have different custom id in the GRF file */
2006-02-18 14:41:24 +00:00
if ( spriteid = = 0xFF ) spriteid = 0xFD ;
2004-11-13 18:01:33 +00:00
2006-02-18 14:41:24 +00:00
if ( spriteid < 0xFD ) spriteid > > = 1 ;
2006-01-27 07:13:58 +00:00
2007-07-04 12:52:43 +00:00
rvi - > image_index = spriteid ;
2010-08-01 18:53:30 +00:00
break ;
}
2004-11-13 18:01:33 +00:00
2009-09-22 19:28:57 +00:00
case PROP_ROADVEH_CARGO_CAPACITY : // 0x0F Cargo capacity
2010-01-23 13:17:30 +00:00
rvi - > capacity = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2006-02-27 15:35:22 +00:00
2007-07-04 12:52:43 +00:00
case 0x10 : { // Cargo type
2010-01-23 13:17:30 +00:00
uint8 cargo = buf - > ReadByte ( ) ;
2004-11-13 18:01:33 +00:00
2007-11-19 21:02:30 +00:00
if ( cargo < NUM_CARGO & & HasBit ( _cargo_mask , cargo ) ) {
2009-08-17 18:33:48 +00:00
ei - > cargo_type = cargo ;
2008-08-30 08:59:55 +00:00
} else if ( cargo = = 0xFF ) {
2009-08-17 18:33:48 +00:00
ei - > cargo_type = CT_INVALID ;
2008-08-30 08:59:55 +00:00
} else {
2009-08-17 18:33:48 +00:00
ei - > cargo_type = CT_INVALID ;
2007-02-24 19:26:45 +00:00
grfmsg ( 2 , " RoadVehicleChangeInfo: Invalid cargo type %d, using first refittable " , cargo ) ;
2006-01-12 07:57:40 +00:00
}
2010-08-01 18:53:30 +00:00
break ;
}
2006-02-27 15:35:22 +00:00
2009-09-22 19:28:57 +00:00
case PROP_ROADVEH_COST_FACTOR : // 0x11 Cost factor
2010-01-23 13:17:30 +00:00
rvi - > cost_factor = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
case 0x12 : // SFX
2010-01-23 13:17:30 +00:00
rvi - > sfx = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2010-10-30 14:31:49 +00:00
case PROP_ROADVEH_POWER : // Power in units of 10 HP.
2010-01-23 13:17:30 +00:00
rvi - > power = buf - > ReadByte ( ) ;
2009-02-02 21:52:34 +00:00
break ;
2010-10-30 14:31:49 +00:00
case PROP_ROADVEH_WEIGHT : // Weight in units of 1/4 tons.
2010-01-23 13:17:30 +00:00
rvi - > weight = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2010-11-06 13:11:24 +00:00
case PROP_ROADVEH_SPEED : // Speed in mph/0.8
2010-01-23 13:17:30 +00:00
_gted [ e - > index ] . rv_max_speed = buf - > ReadByte ( ) ;
2009-01-06 14:45:38 +00:00
break ;
2007-07-04 12:52:43 +00:00
case 0x16 : // Cargos available for refitting
2010-01-23 13:17:30 +00:00
ei - > refit_mask = buf - > ReadDWord ( ) ;
2009-09-28 19:24:20 +00:00
_gted [ e - > index ] . refitmask_valid = true ;
2011-08-21 12:05:47 +00:00
_gted [ e - > index ] . refitmask_grf = _cur . grffile ;
2007-07-04 12:52:43 +00:00
break ;
case 0x17 : // Callback mask
2010-01-23 13:17:30 +00:00
ei - > callback_mask = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2010-10-30 14:31:49 +00:00
case PROP_ROADVEH_TRACTIVE_EFFORT : // Tractive effort coefficient in 1/256.
2010-01-23 13:17:30 +00:00
rvi - > tractive_effort = buf - > ReadByte ( ) ;
2009-02-02 21:52:34 +00:00
break ;
2007-10-25 23:24:28 +00:00
case 0x19 : // Air drag
2010-01-23 13:17:30 +00:00
rvi - > air_drag = buf - > ReadByte ( ) ;
2007-10-25 23:24:28 +00:00
break ;
2007-07-04 12:52:43 +00:00
case 0x1A : // Refit cost
2010-01-23 13:17:30 +00:00
ei - > refit_cost = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
case 0x1B : // Retire vehicle early
2010-01-23 13:17:30 +00:00
ei - > retire_early = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
case 0x1C : // Miscellaneous flags
2010-01-23 13:17:30 +00:00
ei - > misc_flags = buf - > ReadByte ( ) ;
2007-11-19 21:02:30 +00:00
_loaded_newgrf_features . has_2CC | = HasBit ( ei - > misc_flags , EF_USES_2CC ) ;
2007-07-04 12:52:43 +00:00
break ;
case 0x1D : // Cargo classes allowed
2010-01-23 13:17:30 +00:00
_gted [ e - > index ] . cargo_allowed = buf - > ReadWord ( ) ;
2009-09-28 19:24:20 +00:00
_gted [ e - > index ] . refitmask_valid = true ;
2007-07-04 12:52:43 +00:00
break ;
case 0x1E : // Cargo classes disallowed
2010-01-23 13:17:30 +00:00
_gted [ e - > index ] . cargo_disallowed = buf - > ReadWord ( ) ;
2009-09-28 19:24:20 +00:00
_gted [ e - > index ] . refitmask_valid = true ;
2007-07-04 12:52:43 +00:00
break ;
case 0x1F : // Long format introduction date (days since year 0)
2010-01-23 13:17:30 +00:00
ei - > base_intro = buf - > ReadDWord ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2008-07-21 14:42:25 +00:00
case 0x20 : // Alter purchase list sort order
2010-03-10 07:31:08 +00:00
AlterVehicleListOrder ( e - > index , buf - > ReadExtendedByte ( ) ) ;
2008-07-21 14:42:25 +00:00
break ;
2010-11-18 14:32:09 +00: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 20:55:08 +00:00
case PROP_ROADVEH_CARGO_AGE_PERIOD : // 0x22 Cargo aging period
ei - > cargo_age_period = buf - > ReadWord ( ) ;
break ;
2007-07-04 12:52:43 +00:00
default :
2010-01-23 13:17:30 +00:00
ret = CommonVehicleChangeInfo ( ei , prop , buf ) ;
2007-07-04 12:52:43 +00:00
break ;
}
2004-11-13 18:01:33 +00:00
}
return ret ;
}
2010-01-22 13:02:15 +00:00
static ChangeInfoResult ShipVehicleChangeInfo ( uint engine , int numinfo , int prop , ByteReader * buf )
2004-08-09 17:04:08 +00:00
{
2008-08-31 16:41:27 +00:00
ChangeInfoResult ret = CIR_SUCCESS ;
2004-08-09 17:04:08 +00:00
2007-07-04 12:52:43 +00:00
for ( int i = 0 ; i < numinfo ; i + + ) {
2011-08-21 12:05:47 +00:00
Engine * e = GetNewEngine ( _cur . grffile , VEH_SHIP , engine + i ) ;
2011-02-09 18:46:55 +00:00
if ( e = = NULL ) return CIR_INVALID_ID ; // No engine could be allocated, so neither can any next vehicles
2008-04-29 21:31:29 +00:00
EngineInfo * ei = & e - > info ;
ShipVehicleInfo * svi = & e - > u . ship ;
2007-07-04 12:52:43 +00:00
switch ( prop ) {
case 0x08 : { // Sprite ID
2010-01-23 13:17:30 +00:00
uint8 spriteid = buf - > ReadByte ( ) ;
2004-08-09 17:04:08 +00:00
2007-03-21 03:06:21 +00:00
/* ships have different custom id in the GRF file */
2006-02-18 14:41:24 +00:00
if ( spriteid = = 0xFF ) spriteid = 0xFD ;
2004-08-09 17:04:08 +00:00
2006-02-18 14:41:24 +00:00
if ( spriteid < 0xFD ) spriteid > > = 1 ;
2006-01-27 07:13:58 +00:00
2007-07-04 12:52:43 +00:00
svi - > image_index = spriteid ;
2010-08-01 18:53:30 +00:00
break ;
}
2004-08-09 17:04:08 +00:00
2007-07-04 12:52:43 +00:00
case 0x09 : // Refittable
2010-01-23 13:17:30 +00:00
svi - > old_refittable = ( buf - > ReadByte ( ) ! = 0 ) ;
2007-07-04 12:52:43 +00:00
break ;
2004-08-09 17:04:08 +00:00
2009-09-22 19:28:57 +00:00
case PROP_SHIP_COST_FACTOR : // 0x0A Cost factor
2010-01-23 13:17:30 +00:00
svi - > cost_factor = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2004-08-09 17:04:08 +00:00
2009-09-22 19:28:57 +00:00
case PROP_SHIP_SPEED : // 0x0B Speed (1 unit is 0.5 km-ish/h)
2010-01-23 13:17:30 +00:00
svi - > max_speed = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2006-02-27 15:35:22 +00:00
2007-07-04 12:52:43 +00:00
case 0x0C : { // Cargo type
2010-01-23 13:17:30 +00:00
uint8 cargo = buf - > ReadByte ( ) ;
2004-08-09 17:04:08 +00:00
2007-11-19 21:02:30 +00:00
if ( cargo < NUM_CARGO & & HasBit ( _cargo_mask , cargo ) ) {
2009-08-17 18:33:48 +00:00
ei - > cargo_type = cargo ;
2008-08-30 08:59:55 +00:00
} else if ( cargo = = 0xFF ) {
2009-08-17 18:33:48 +00:00
ei - > cargo_type = CT_INVALID ;
2006-01-12 07:57:40 +00:00
} else {
2009-08-17 18:33:48 +00:00
ei - > cargo_type = CT_INVALID ;
2007-02-24 19:26:45 +00:00
grfmsg ( 2 , " ShipVehicleChangeInfo: Invalid cargo type %d, using first refittable " , cargo ) ;
2006-01-12 07:57:40 +00:00
}
2010-08-01 18:53:30 +00:00
break ;
}
2006-02-27 15:35:22 +00:00
2009-09-22 19:28:57 +00:00
case PROP_SHIP_CARGO_CAPACITY : // 0x0D Cargo capacity
2010-01-23 13:17:30 +00:00
svi - > capacity = buf - > ReadWord ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2009-09-22 19:28:57 +00:00
case PROP_SHIP_RUNNING_COST_FACTOR : // 0x0F Running cost factor
2010-01-23 13:17:30 +00:00
svi - > running_cost = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
case 0x10 : // SFX
2010-01-23 13:17:30 +00:00
svi - > sfx = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
case 0x11 : // Cargos available for refitting
2010-01-23 13:17:30 +00:00
ei - > refit_mask = buf - > ReadDWord ( ) ;
2009-09-28 19:24:20 +00:00
_gted [ e - > index ] . refitmask_valid = true ;
2011-08-21 12:05:47 +00:00
_gted [ e - > index ] . refitmask_grf = _cur . grffile ;
2007-07-04 12:52:43 +00:00
break ;
case 0x12 : // Callback mask
2010-01-23 13:17:30 +00:00
ei - > callback_mask = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
case 0x13 : // Refit cost
2010-01-23 13:17:30 +00:00
ei - > refit_cost = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2007-10-25 23:24:28 +00:00
case 0x14 : // Ocean speed fraction
2011-07-07 14:16:22 +00:00
svi - > ocean_speed_frac = buf - > ReadByte ( ) ;
break ;
2007-10-25 23:24:28 +00:00
case 0x15 : // Canal speed fraction
2011-07-07 14:16:22 +00:00
svi - > canal_speed_frac = buf - > ReadByte ( ) ;
2007-10-25 23:24:28 +00:00
break ;
2007-07-04 12:52:43 +00:00
case 0x16 : // Retire vehicle early
2010-01-23 13:17:30 +00:00
ei - > retire_early = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
case 0x17 : // Miscellaneous flags
2010-01-23 13:17:30 +00:00
ei - > misc_flags = buf - > ReadByte ( ) ;
2007-11-19 21:02:30 +00:00
_loaded_newgrf_features . has_2CC | = HasBit ( ei - > misc_flags , EF_USES_2CC ) ;
2007-07-04 12:52:43 +00:00
break ;
case 0x18 : // Cargo classes allowed
2010-01-23 13:17:30 +00:00
_gted [ e - > index ] . cargo_allowed = buf - > ReadWord ( ) ;
2009-09-28 19:24:20 +00:00
_gted [ e - > index ] . refitmask_valid = true ;
2007-07-04 12:52:43 +00:00
break ;
case 0x19 : // Cargo classes disallowed
2010-01-23 13:17:30 +00:00
_gted [ e - > index ] . cargo_disallowed = buf - > ReadWord ( ) ;
2009-09-28 19:24:20 +00:00
_gted [ e - > index ] . refitmask_valid = true ;
2007-07-04 12:52:43 +00:00
break ;
case 0x1A : // Long format introduction date (days since year 0)
2010-01-23 13:17:30 +00:00
ei - > base_intro = buf - > ReadDWord ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2008-07-21 14:42:25 +00:00
case 0x1B : // Alter purchase list sort order
2010-03-10 07:31:08 +00:00
AlterVehicleListOrder ( e - > index , buf - > ReadExtendedByte ( ) ) ;
2008-07-21 14:42:25 +00:00
break ;
2010-11-18 14:32:09 +00: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 20:55:08 +00:00
case PROP_SHIP_CARGO_AGE_PERIOD : // 0x1D Cargo aging period
ei - > cargo_age_period = buf - > ReadWord ( ) ;
break ;
2007-07-04 12:52:43 +00:00
default :
2010-01-23 13:17:30 +00:00
ret = CommonVehicleChangeInfo ( ei , prop , buf ) ;
2007-07-04 12:52:43 +00:00
break ;
}
2004-08-09 17:04:08 +00:00
}
return ret ;
}
2010-01-22 13:02:15 +00:00
static ChangeInfoResult AircraftVehicleChangeInfo ( uint engine , int numinfo , int prop , ByteReader * buf )
2004-11-13 20:37:57 +00:00
{
2008-08-31 16:41:27 +00:00
ChangeInfoResult ret = CIR_SUCCESS ;
2004-11-13 20:37:57 +00:00
2007-07-04 12:52:43 +00:00
for ( int i = 0 ; i < numinfo ; i + + ) {
2011-08-21 12:05:47 +00:00
Engine * e = GetNewEngine ( _cur . grffile , VEH_AIRCRAFT , engine + i ) ;
2011-02-09 18:46:55 +00:00
if ( e = = NULL ) return CIR_INVALID_ID ; // No engine could be allocated, so neither can any next vehicles
2008-04-29 21:31:29 +00:00
EngineInfo * ei = & e - > info ;
AircraftVehicleInfo * avi = & e - > u . air ;
2007-07-04 12:52:43 +00:00
switch ( prop ) {
case 0x08 : { // Sprite ID
2010-01-23 13:17:30 +00:00
uint8 spriteid = buf - > ReadByte ( ) ;
2004-11-13 20:37:57 +00:00
2007-03-21 03:06:21 +00:00
/* aircraft have different custom id in the GRF file */
2006-02-18 14:41:24 +00:00
if ( spriteid = = 0xFF ) spriteid = 0xFD ;
2004-11-13 20:37:57 +00:00
2006-02-18 14:41:24 +00:00
if ( spriteid < 0xFD ) spriteid > > = 1 ;
2006-01-27 07:13:58 +00:00
2007-07-04 12:52:43 +00:00
avi - > image_index = spriteid ;
2010-08-01 18:53:30 +00:00
break ;
}
2004-11-13 20:37:57 +00:00
2007-07-04 12:52:43 +00:00
case 0x09 : // Helicopter
2010-01-23 13:17:30 +00:00
if ( buf - > ReadByte ( ) = = 0 ) {
2007-07-04 12:52:43 +00:00
avi - > subtype = AIR_HELI ;
2006-10-23 17:01:02 +00:00
} else {
2007-07-04 12:52:43 +00:00
SB ( avi - > subtype , 0 , 1 , 1 ) ; // AIR_CTOL
2006-10-23 17:01:02 +00:00
}
2007-07-04 12:52:43 +00:00
break ;
2004-11-13 20:37:57 +00:00
2007-07-04 12:52:43 +00:00
case 0x0A : // Large
2010-01-23 13:17:30 +00:00
SB ( avi - > subtype , 1 , 1 , ( buf - > ReadByte ( ) ! = 0 ? 1 : 0 ) ) ; // AIR_FAST
2007-07-04 12:52:43 +00:00
break ;
2004-11-13 20:37:57 +00:00
2009-09-22 19:28:57 +00:00
case PROP_AIRCRAFT_COST_FACTOR : // 0x0B Cost factor
2010-01-23 13:17:30 +00:00
avi - > cost_factor = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2004-11-13 20:37:57 +00:00
2010-07-16 17:45:34 +00: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 12:52:43 +00:00
break ;
2004-11-13 20:37:57 +00:00
2007-07-04 12:52:43 +00:00
case 0x0D : // Acceleration
2010-07-16 17:45:34 +00:00
avi - > acceleration = ( buf - > ReadByte ( ) * 128 ) / 10 ;
2007-07-04 12:52:43 +00:00
break ;
2004-11-13 20:37:57 +00:00
2009-09-22 19:28:57 +00:00
case PROP_AIRCRAFT_RUNNING_COST_FACTOR : // 0x0E Running cost factor
2010-01-23 13:17:30 +00:00
avi - > running_cost = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2004-11-13 20:37:57 +00:00
2010-02-22 21:46:20 +00:00
case PROP_AIRCRAFT_PASSENGER_CAPACITY : // 0x0F Passenger capacity
2010-01-23 13:17:30 +00:00
avi - > passenger_capacity = buf - > ReadWord ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2004-11-13 20:37:57 +00:00
2010-02-22 21:46:20 +00:00
case PROP_AIRCRAFT_MAIL_CAPACITY : // 0x11 Mail capacity
2010-01-23 13:17:30 +00:00
avi - > mail_capacity = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2006-02-11 07:34:05 +00:00
2007-07-04 12:52:43 +00:00
case 0x12 : // SFX
2010-01-23 13:17:30 +00:00
avi - > sfx = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2006-02-27 15:35:22 +00:00
2007-07-04 12:52:43 +00:00
case 0x13 : // Cargos available for refitting
2010-01-23 13:17:30 +00:00
ei - > refit_mask = buf - > ReadDWord ( ) ;
2009-09-28 19:24:20 +00:00
_gted [ e - > index ] . refitmask_valid = true ;
2011-08-21 12:05:47 +00:00
_gted [ e - > index ] . refitmask_grf = _cur . grffile ;
2007-07-04 12:52:43 +00:00
break ;
2006-02-27 15:35:22 +00:00
2007-07-04 12:52:43 +00:00
case 0x14 : // Callback mask
2010-01-23 13:17:30 +00:00
ei - > callback_mask = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2006-02-27 15:35:22 +00:00
2007-07-04 12:52:43 +00:00
case 0x15 : // Refit cost
2010-01-23 13:17:30 +00:00
ei - > refit_cost = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2006-06-09 16:35:07 +00:00
2007-07-04 12:52:43 +00:00
case 0x16 : // Retire vehicle early
2010-01-23 13:17:30 +00:00
ei - > retire_early = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2007-05-10 23:10:23 +00:00
2007-07-04 12:52:43 +00:00
case 0x17 : // Miscellaneous flags
2010-01-23 13:17:30 +00:00
ei - > misc_flags = buf - > ReadByte ( ) ;
2007-11-19 21:02:30 +00:00
_loaded_newgrf_features . has_2CC | = HasBit ( ei - > misc_flags , EF_USES_2CC ) ;
2007-07-04 12:52:43 +00:00
break ;
2006-02-28 08:13:15 +00:00
2007-07-04 12:52:43 +00:00
case 0x18 : // Cargo classes allowed
2010-01-23 13:17:30 +00:00
_gted [ e - > index ] . cargo_allowed = buf - > ReadWord ( ) ;
2009-09-28 19:24:20 +00:00
_gted [ e - > index ] . refitmask_valid = true ;
2007-07-04 12:52:43 +00:00
break ;
2006-02-27 15:35:22 +00:00
2007-07-04 12:52:43 +00:00
case 0x19 : // Cargo classes disallowed
2010-01-23 13:17:30 +00:00
_gted [ e - > index ] . cargo_disallowed = buf - > ReadWord ( ) ;
2009-09-28 19:24:20 +00:00
_gted [ e - > index ] . refitmask_valid = true ;
2007-07-04 12:52:43 +00:00
break ;
2006-02-11 07:34:05 +00:00
2007-07-04 12:52:43 +00:00
case 0x1A : // Long format introduction date (days since year 0)
2010-01-23 13:17:30 +00:00
ei - > base_intro = buf - > ReadDWord ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2006-11-17 11:03:38 +00:00
2008-07-21 14:42:25 +00:00
case 0x1B : // Alter purchase list sort order
2010-03-10 07:31:08 +00:00
AlterVehicleListOrder ( e - > index , buf - > ReadExtendedByte ( ) ) ;
2008-07-21 14:42:25 +00:00
break ;
2011-08-03 20:55:08 +00:00
case PROP_AIRCRAFT_CARGO_AGE_PERIOD : // 0x1C Cargo aging period
ei - > cargo_age_period = buf - > ReadWord ( ) ;
break ;
2007-07-04 12:52:43 +00:00
default :
2010-01-23 13:17:30 +00:00
ret = CommonVehicleChangeInfo ( ei , prop , buf ) ;
2007-07-04 12:52:43 +00:00
break ;
}
2004-11-13 20:37:57 +00:00
}
return ret ;
}
2010-01-22 13:02:15 +00:00
static ChangeInfoResult StationChangeInfo ( uint stid , int numinfo , int prop , ByteReader * buf )
2004-11-14 09:07:15 +00:00
{
2008-08-31 16:41:27 +00:00
ChangeInfoResult ret = CIR_SUCCESS ;
2004-11-14 09:07:15 +00:00
2006-05-06 22:20:16 +00:00
if ( stid + numinfo > MAX_STATIONS ) {
2006-12-26 17:36:18 +00:00
grfmsg ( 1 , " StationChangeInfo: Station %u is invalid, max %u, ignoring " , stid + numinfo , MAX_STATIONS ) ;
2008-08-31 16:41:27 +00:00
return CIR_INVALID_ID ;
2006-05-06 22:20:16 +00:00
}
2006-04-11 10:05:52 +00:00
/* Allocate station specs if necessary */
2011-08-21 12:05:47 +00:00
if ( _cur . grffile - > stations = = NULL ) _cur . grffile - > stations = CallocT < StationSpec * > ( MAX_STATIONS ) ;
2006-05-06 22:20:16 +00:00
2007-07-04 12:52:43 +00:00
for ( int i = 0 ; i < numinfo ; i + + ) {
2011-08-21 12:05:47 +00:00
StationSpec * statspec = _cur . grffile - > stations [ stid + i ] ;
2006-04-11 10:05:52 +00:00
2007-07-04 12:52:43 +00:00
/* Check that the station we are modifying is defined. */
if ( statspec = = NULL & & prop ! = 0x08 ) {
grfmsg ( 2 , " StationChangeInfo: Attempt to modify undefined station %u, ignoring " , stid + i ) ;
2008-08-31 16:41:27 +00:00
return CIR_INVALID_ID ;
2006-04-11 10:05:52 +00:00
}
2004-11-14 09:07:15 +00:00
2007-07-04 12:52:43 +00:00
switch ( prop ) {
case 0x08 : { // Class ID
2011-08-21 12:05:47 +00:00
StationSpec * * spec = & _cur . grffile - > stations [ stid + i ] ;
2007-07-04 12:52:43 +00:00
2006-05-06 22:20:16 +00:00
/* Property 0x08 is special; it is where the station is allocated */
2007-07-04 12:52:43 +00:00
if ( * spec = = NULL ) * spec = CallocT < StationSpec > ( 1 ) ;
2006-05-06 22:20:16 +00:00
2006-05-01 21:45:35 +00:00
/* Swap classid because we read it in BE meaning WAYP or DFLT */
2010-01-23 13:17:30 +00:00
uint32 classid = buf - > ReadDWord ( ) ;
2010-08-07 22:07:07 +00:00
( * spec ) - > cls_id = StationClass : : Allocate ( BSWAP32 ( classid ) ) ;
2010-08-01 18:53:30 +00:00
break ;
}
2004-11-14 13:59:11 +00:00
2007-07-04 12:52:43 +00:00
case 0x09 : // Define sprite layout
2010-03-10 07:31:08 +00:00
statspec - > tiles = buf - > ReadExtendedByte ( ) ;
2011-05-29 16:54:40 +00:00
delete [ ] statspec - > renderdata ; // delete earlier loaded stuff
2011-05-14 17:25:45 +00:00
statspec - > renderdata = new NewGRFSpriteLayout [ statspec - > tiles ] ;
2006-05-03 21:07:44 +00:00
2007-03-10 09:55:11 +00:00
for ( uint t = 0 ; t < statspec - > tiles ; t + + ) {
2011-05-14 17:25:45 +00:00
NewGRFSpriteLayout * dts = & statspec - > renderdata [ t ] ;
2004-11-14 09:07:15 +00:00
2011-05-18 18:31:23 +00:00
if ( buf - > HasData ( 4 ) & & * ( uint32 * ) buf - > Data ( ) = = 0 ) {
buf - > Skip ( 4 ) ;
2011-03-03 21:24:03 +00:00
extern const DrawTileSprites _station_display_datas_rail [ 8 ] ;
2011-05-14 17:25:45 +00:00
dts - > Clone ( & _station_display_datas_rail [ t % 8 ] ) ;
2011-03-03 21:24:03 +00:00
continue ;
}
2008-02-16 03:01:05 +00:00
2011-05-29 16:56:22 +00:00
ReadSpriteLayoutSprite ( buf , false , false , 0 , 1 , UINT_MAX , & dts - > ground ) ;
/* On error, bail out immediately. Temporary GRF data was already freed */
2011-08-21 12:05:47 +00:00
if ( _cur . skip_sprites < 0 ) return CIR_DISABLED ;
2005-11-12 00:19:34 +00:00
2011-05-14 17:25:45 +00:00
static SmallVector < DrawTileSeqStruct , 8 > tmp_layout ;
tmp_layout . Clear ( ) ;
2011-03-03 19:52:42 +00:00
for ( ; ; ) {
2007-03-21 03:06:21 +00:00
/* no relative bounding box support */
2011-05-14 17:25:45 +00:00
DrawTileSeqStruct * dtss = tmp_layout . Append ( ) ;
MemSetT ( dtss , 0 ) ;
2004-11-14 09:07:15 +00:00
2010-01-23 13:17:30 +00:00
dtss - > delta_x = buf - > ReadByte ( ) ;
2011-02-19 13:16:34 +00:00
if ( dtss - > IsTerminator ( ) ) break ;
2010-01-23 13:17:30 +00:00
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 ( ) ;
2007-01-14 19:57:49 +00:00
2011-05-29 16:56:22 +00:00
ReadSpriteLayoutSprite ( buf , false , true , 0 , 1 , UINT_MAX , & dtss - > image ) ;
/* On error, bail out immediately. Temporary GRF data was already freed */
2011-08-21 12:05:47 +00:00
if ( _cur . skip_sprites < 0 ) return CIR_DISABLED ;
2004-11-14 09:07:15 +00:00
}
2011-05-14 17:25:45 +00:00
dts - > Clone ( tmp_layout . Begin ( ) ) ;
2004-11-14 09:07:15 +00:00
}
2007-07-04 12:52:43 +00:00
break ;
2006-04-11 13:00:06 +00:00
2007-07-04 12:52:43 +00:00
case 0x0A : { // Copy sprite layout
2010-01-23 13:17:30 +00:00
byte srcid = buf - > ReadByte ( ) ;
2011-08-21 12:05:47 +00:00
const StationSpec * srcstatspec = _cur . grffile - > stations [ srcid ] ;
2004-11-14 13:59:11 +00:00
2010-01-27 10:17:09 +00:00
if ( srcstatspec = = NULL ) {
grfmsg ( 1 , " StationChangeInfo: Station %u is not defined, cannot copy sprite layout to %u. " , srcid , stid + i ) ;
continue ;
}
2011-05-17 20:11:19 +00:00
delete [ ] statspec - > renderdata ; // delete earlier loaded stuff
2006-04-27 18:28:56 +00:00
statspec - > tiles = srcstatspec - > tiles ;
2011-05-14 17:25:45 +00:00
statspec - > renderdata = new NewGRFSpriteLayout [ statspec - > tiles ] ;
2011-03-03 21:24:03 +00:00
for ( uint t = 0 ; t < statspec - > tiles ; t + + ) {
2011-05-14 17:25:45 +00:00
statspec - > renderdata [ t ] . Clone ( & srcstatspec - > renderdata [ t ] ) ;
2011-03-03 21:24:03 +00:00
}
2010-08-01 18:53:30 +00:00
break ;
}
2006-04-11 13:00:06 +00:00
2007-07-04 12:52:43 +00:00
case 0x0B : // Callback mask
2010-01-23 13:17:30 +00:00
statspec - > callback_mask = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2004-11-17 18:03:33 +00:00
2007-07-04 12:52:43 +00:00
case 0x0C : // Disallowed number of platforms
2010-01-23 13:17:30 +00:00
statspec - > disallowed_platforms = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2006-04-11 13:00:06 +00:00
2007-07-04 12:52:43 +00:00
case 0x0D : // Disallowed platform lengths
2010-01-23 13:17:30 +00:00
statspec - > disallowed_lengths = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2004-11-17 18:03:33 +00:00
2007-07-04 12:52:43 +00:00
case 0x0E : // Define custom layout
2006-11-08 12:17:14 +00:00
statspec - > copied_layouts = false ;
2010-01-22 13:02:15 +00:00
while ( buf - > HasData ( ) ) {
2010-01-23 13:17:30 +00:00
byte length = buf - > ReadByte ( ) ;
byte number = buf - > ReadByte ( ) ;
2004-11-17 18:03:33 +00:00
StationLayout layout ;
2006-04-16 18:57:07 +00:00
uint l , p ;
2004-11-17 18:03:33 +00:00
if ( length = = 0 | | number = = 0 ) break ;
2006-04-27 18:28:56 +00:00
if ( length > statspec - > lengths ) {
2007-01-11 17:29:39 +00:00
statspec - > platforms = ReallocT ( statspec - > platforms , length ) ;
2006-04-27 18:28:56 +00:00
memset ( statspec - > platforms + statspec - > lengths , 0 , length - statspec - > lengths ) ;
2004-11-17 18:03:33 +00:00
2007-01-11 17:29:39 +00:00
statspec - > layouts = ReallocT ( statspec - > layouts , length ) ;
2006-04-27 18:28:56 +00:00
memset ( statspec - > layouts + statspec - > lengths , 0 ,
( length - statspec - > lengths ) * sizeof ( * statspec - > layouts ) ) ;
2004-11-17 18:03:33 +00:00
2006-04-27 18:28:56 +00:00
statspec - > lengths = length ;
2004-11-17 18:03:33 +00:00
}
l = length - 1 ; // index is zero-based
2006-04-27 18:28:56 +00:00
if ( number > statspec - > platforms [ l ] ) {
2007-01-11 17:29:39 +00:00
statspec - > layouts [ l ] = ReallocT ( statspec - > layouts [ l ] , number ) ;
2007-03-21 03:06:21 +00:00
/* We expect NULL being 0 here, but C99 guarantees that. */
2006-04-27 18:28:56 +00:00
memset ( statspec - > layouts [ l ] + statspec - > platforms [ l ] , 0 ,
( number - statspec - > platforms [ l ] ) * sizeof ( * * statspec - > layouts ) ) ;
2004-11-17 18:03:33 +00:00
2006-04-27 18:28:56 +00:00
statspec - > platforms [ l ] = number ;
2004-11-17 18:03:33 +00:00
}
2004-12-04 15:02:45 +00:00
p = 0 ;
2007-01-11 17:29:39 +00:00
layout = MallocT < byte > ( length * number ) ;
2010-01-22 13:02:15 +00:00
try {
for ( l = 0 ; l < length ; l + + ) {
for ( p = 0 ; p < number ; p + + ) {
2010-01-23 13:17:30 +00:00
layout [ l * number + p ] = buf - > ReadByte ( ) ;
2010-01-22 13:02:15 +00:00
}
2006-02-18 14:41:24 +00:00
}
2010-01-22 13:02:15 +00:00
} catch ( . . . ) {
free ( layout ) ;
throw ;
2006-02-18 14:41:24 +00:00
}
2004-11-14 09:07:15 +00:00
2004-11-17 18:03:33 +00:00
l - - ;
p - - ;
2006-04-27 18:28:56 +00:00
free ( statspec - > layouts [ l ] [ p ] ) ;
statspec - > layouts [ l ] [ p ] = layout ;
2004-11-14 09:07:15 +00:00
}
2007-07-04 12:52:43 +00:00
break ;
2006-04-11 13:00:06 +00:00
2007-07-04 12:52:43 +00:00
case 0x0F : { // Copy custom layout
2010-01-23 13:17:30 +00:00
byte srcid = buf - > ReadByte ( ) ;
2011-08-21 12:05:47 +00:00
const StationSpec * srcstatspec = _cur . grffile - > stations [ srcid ] ;
2006-11-08 12:17:14 +00:00
2010-01-27 10:17:09 +00:00
if ( srcstatspec = = NULL ) {
grfmsg ( 1 , " StationChangeInfo: Station %u is not defined, cannot copy tile layout to %u. " , srcid , stid + i ) ;
continue ;
}
2006-11-08 12:17:14 +00:00
statspec - > lengths = srcstatspec - > lengths ;
statspec - > platforms = srcstatspec - > platforms ;
statspec - > layouts = srcstatspec - > layouts ;
statspec - > copied_layouts = true ;
2010-08-01 18:53:30 +00:00
break ;
}
2006-04-11 13:00:06 +00:00
2007-07-04 12:52:43 +00:00
case 0x10 : // Little/lots cargo threshold
2010-01-23 13:17:30 +00:00
statspec - > cargo_threshold = buf - > ReadWord ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2006-04-11 13:00:06 +00:00
2007-07-04 12:52:43 +00:00
case 0x11 : // Pylon placement
2010-01-23 13:17:30 +00:00
statspec - > pylons = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2006-04-11 13:00:06 +00:00
2007-07-04 12:52:43 +00:00
case 0x12 : // Cargo types for random triggers
2010-01-23 13:17:30 +00:00
statspec - > cargo_triggers = buf - > ReadDWord ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2006-04-11 13:00:06 +00:00
2007-07-04 12:52:43 +00:00
case 0x13 : // General flags
2010-01-23 13:17:30 +00:00
statspec - > flags = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2006-04-11 13:00:06 +00:00
2007-07-04 12:52:43 +00:00
case 0x14 : // Overhead wire placement
2010-01-23 13:17:30 +00:00
statspec - > wires = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2006-04-11 13:00:06 +00:00
2007-07-04 12:52:43 +00:00
case 0x15 : // Blocked tiles
2010-01-23 13:17:30 +00:00
statspec - > blocked = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2006-04-11 13:00:06 +00:00
2008-04-19 23:19:12 +00:00
case 0x16 : // Animation info
2010-08-26 15:31:40 +00:00
statspec - > animation . frames = buf - > ReadByte ( ) ;
statspec - > animation . status = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2007-02-02 02:56:29 +00:00
2008-04-19 23:19:12 +00:00
case 0x17 : // Animation speed
2010-08-26 15:31:40 +00:00
statspec - > animation . speed = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2007-02-02 02:56:29 +00:00
2008-04-19 23:19:12 +00:00
case 0x18 : // Animation triggers
2010-08-26 15:31:40 +00:00
statspec - > animation . triggers = buf - > ReadWord ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2007-02-02 02:56:29 +00:00
2011-06-23 08:25:15 +00:00
case 0x1A : // Advanced sprite layout
2011-05-29 16:56:22 +00:00
statspec - > tiles = buf - > ReadExtendedByte ( ) ;
delete [ ] statspec - > renderdata ; // delete earlier loaded stuff
statspec - > renderdata = new NewGRFSpriteLayout [ statspec - > tiles ] ;
for ( uint t = 0 ; t < statspec - > tiles ; t + + ) {
NewGRFSpriteLayout * dts = & statspec - > renderdata [ t ] ;
uint num_building_sprites = buf - > ReadByte ( ) ;
/* On error, bail out immediately. Temporary GRF data was already freed */
if ( ReadSpriteLayout ( buf , num_building_sprites , 0 , 1 , UINT_MAX , true , false , dts ) ) return CIR_DISABLED ;
}
break ;
2007-07-04 12:52:43 +00:00
default :
2008-08-31 16:41:27 +00:00
ret = CIR_UNKNOWN ;
2007-07-04 12:52:43 +00:00
break ;
}
2004-11-14 09:07:15 +00:00
}
return ret ;
}
2010-01-22 13:02:15 +00:00
static ChangeInfoResult CanalChangeInfo ( uint id , int numinfo , int prop , ByteReader * buf )
2008-01-21 20:41:04 +00:00
{
2008-08-31 16:41:27 +00:00
ChangeInfoResult ret = CIR_SUCCESS ;
2008-01-21 20:41:04 +00:00
if ( id + numinfo > CF_END ) {
grfmsg ( 1 , " CanalChangeInfo: Canal feature %u is invalid, max %u, ignoreing " , id + numinfo , CF_END ) ;
2008-08-31 16:41:27 +00:00
return CIR_INVALID_ID ;
2008-01-21 20:41:04 +00:00
}
for ( int i = 0 ; i < numinfo ; i + + ) {
WaterFeature * wf = & _water_feature [ id + i ] ;
switch ( prop ) {
case 0x08 :
2010-01-23 13:17:30 +00:00
wf - > callback_mask = buf - > ReadByte ( ) ;
2008-01-21 20:41:04 +00:00
break ;
case 0x09 :
2010-01-23 13:17:30 +00:00
wf - > flags = buf - > ReadByte ( ) ;
2008-01-21 20:41:04 +00:00
break ;
default :
2008-08-31 16:41:27 +00:00
ret = CIR_UNKNOWN ;
2008-01-21 20:41:04 +00:00
break ;
}
}
return ret ;
}
2010-01-22 13:02:15 +00:00
static ChangeInfoResult BridgeChangeInfo ( uint brid , int numinfo , int prop , ByteReader * buf )
2005-10-01 17:38:48 +00:00
{
2008-08-31 16:41:27 +00:00
ChangeInfoResult ret = CIR_SUCCESS ;
2005-10-01 17:38:48 +00:00
2007-07-04 12:52:43 +00:00
if ( brid + numinfo > MAX_BRIDGES ) {
grfmsg ( 1 , " BridgeChangeInfo: Bridge %u is invalid, max %u, ignoring " , brid + numinfo , MAX_BRIDGES ) ;
2008-08-31 16:41:27 +00:00
return CIR_INVALID_ID ;
2007-07-04 12:52:43 +00:00
}
for ( int i = 0 ; i < numinfo ; i + + ) {
2008-02-11 17:35:15 +00:00
BridgeSpec * bridge = & _bridge [ brid + i ] ;
2005-10-01 17:38:48 +00:00
2007-07-04 12:52:43 +00:00
switch ( prop ) {
2009-05-09 19:48:14 +00:00
case 0x08 : { // Year of availability
/* We treat '0' as always available */
2010-01-23 13:17:30 +00:00
byte year = buf - > ReadByte ( ) ;
2009-05-09 19:48:14 +00:00
bridge - > avail_year = ( year > 0 ? ORIGINAL_BASE_YEAR + year : 0 ) ;
2007-07-04 12:52:43 +00:00
break ;
2009-05-09 19:48:14 +00:00
}
2005-10-01 17:38:48 +00:00
2007-07-04 12:52:43 +00:00
case 0x09 : // Minimum length
2010-01-23 13:17:30 +00:00
bridge - > min_length = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2005-10-01 17:38:48 +00:00
2007-07-04 12:52:43 +00:00
case 0x0A : // Maximum length
2010-01-23 13:17:30 +00:00
bridge - > max_length = buf - > ReadByte ( ) ;
2011-02-04 15:37:23 +00:00
if ( bridge - > max_length > 16 ) bridge - > max_length = 0xFFFF ;
2007-07-04 12:52:43 +00:00
break ;
2005-10-01 17:38:48 +00:00
2007-07-04 12:52:43 +00:00
case 0x0B : // Cost factor
2010-01-23 13:17:30 +00:00
bridge - > price = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2005-10-01 17:38:48 +00:00
2007-07-04 12:52:43 +00:00
case 0x0C : // Maximum speed
2010-01-23 13:17:30 +00:00
bridge - > speed = buf - > ReadWord ( ) ;
2007-07-04 12:52:43 +00:00
break ;
case 0x0D : { // Bridge sprite tables
2010-01-23 13:17:30 +00:00
byte tableid = buf - > ReadByte ( ) ;
byte numtables = buf - > ReadByte ( ) ;
2005-10-01 17:38:48 +00:00
if ( bridge - > sprite_table = = NULL ) {
2006-04-15 16:03:45 +00:00
/* Allocate memory for sprite table pointers and zero out */
2007-01-11 17:29:39 +00:00
bridge - > sprite_table = CallocT < PalSpriteID * > ( 7 ) ;
2005-10-01 17:38:48 +00:00
}
2006-04-15 16:03:45 +00:00
for ( ; numtables - - ! = 0 ; tableid + + ) {
if ( tableid > = 7 ) { // skip invalid data
2006-12-26 17:36:18 +00:00
grfmsg ( 1 , " BridgeChangeInfo: Table %d >= 7, skipping " , tableid ) ;
2010-01-23 13:17:30 +00:00
for ( byte sprite = 0 ; sprite < 32 ; sprite + + ) buf - > ReadDWord ( ) ;
2006-04-15 16:03:45 +00:00
continue ;
2005-10-02 12:00:30 +00:00
}
2006-04-15 16:03:45 +00:00
if ( bridge - > sprite_table [ tableid ] = = NULL ) {
2007-01-11 17:29:39 +00:00
bridge - > sprite_table [ tableid ] = MallocT < PalSpriteID > ( 32 ) ;
2006-04-15 16:03:45 +00:00
}
2007-03-10 09:55:11 +00:00
for ( byte sprite = 0 ; sprite < 32 ; sprite + + ) {
2010-01-23 13:17:30 +00:00
SpriteID image = buf - > ReadWord ( ) ;
PaletteID pal = buf - > ReadWord ( ) ;
2007-01-14 19:57:49 +00:00
bridge - > sprite_table [ tableid ] [ sprite ] . sprite = image ;
bridge - > sprite_table [ tableid ] [ sprite ] . pal = pal ;
2008-02-16 03:01:05 +00:00
MapSpriteMappingRecolour ( & bridge - > sprite_table [ tableid ] [ sprite ] ) ;
2007-01-14 19:57:49 +00:00
}
2005-10-01 17:38:48 +00:00
}
2010-08-01 18:53:30 +00:00
break ;
}
2005-10-01 17:38:48 +00:00
2007-07-04 12:52:43 +00:00
case 0x0E : // Flags; bit 0 - disable far pillars
2010-01-23 13:17:30 +00:00
bridge - > flags = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2005-10-01 17:38:48 +00:00
2007-07-04 12:52:43 +00:00
case 0x0F : // Long format year of availability (year since year 0)
2010-01-23 13:17:30 +00:00
bridge - > avail_year = Clamp ( buf - > ReadDWord ( ) , MIN_YEAR , MAX_YEAR ) ;
2007-07-04 12:52:43 +00:00
break ;
2006-11-17 11:03:38 +00:00
2008-02-06 03:01:29 +00:00
case 0x10 : { // purchase string
2011-08-21 12:05:47 +00:00
StringID newone = GetGRFStringID ( _cur . grffile - > grfid , buf - > ReadWord ( ) ) ;
2008-02-06 03:01:29 +00:00
if ( newone ! = STR_UNDEFINED ) bridge - > material = newone ;
2010-08-01 18:53:30 +00:00
break ;
}
2008-02-06 03:01:29 +00:00
2008-02-12 03:05:27 +00:00
case 0x11 : // description of bridge with rails or roads
case 0x12 : {
2011-08-21 12:05:47 +00:00
StringID newone = GetGRFStringID ( _cur . grffile - > grfid , buf - > ReadWord ( ) ) ;
2008-02-12 03:05:27 +00:00
if ( newone ! = STR_UNDEFINED ) bridge - > transport_name [ prop - 0x11 ] = newone ;
2010-08-01 18:53:30 +00:00
break ;
}
2008-02-06 03:01:29 +00:00
2008-08-25 17:16:27 +00:00
case 0x13 : // 16 bits cost multiplier
2010-01-23 13:17:30 +00:00
bridge - > price = buf - > ReadWord ( ) ;
2008-08-25 17:16:27 +00:00
break ;
2007-07-04 12:52:43 +00:00
default :
2008-08-31 16:41:27 +00:00
ret = CIR_UNKNOWN ;
2007-07-04 12:52:43 +00:00
break ;
}
2005-10-01 17:38:48 +00:00
}
return ret ;
}
2004-11-14 09:07:15 +00:00
2010-04-02 21:30:37 +00: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 ( ) ;
ret = CIR_UNHANDLED ;
2010-08-01 18:53:30 +00:00
break ;
}
2010-04-02 21:30:37 +00:00
default :
ret = CIR_UNKNOWN ;
break ;
}
return ret ;
}
2010-01-22 13:02:15 +00:00
static ChangeInfoResult TownHouseChangeInfo ( uint hid , int numinfo , int prop , ByteReader * buf )
2007-03-19 11:27:30 +00:00
{
2008-08-31 16:41:27 +00:00
ChangeInfoResult ret = CIR_SUCCESS ;
2007-03-19 11:27:30 +00:00
2007-07-04 13:01:55 +00:00
if ( hid + numinfo > HOUSE_MAX ) {
grfmsg ( 1 , " TownHouseChangeInfo: Too many houses loaded (%u), max (%u). Ignoring. " , hid + numinfo , HOUSE_MAX ) ;
2008-08-31 16:41:27 +00:00
return CIR_INVALID_ID ;
2007-03-19 11:27:30 +00:00
}
/* Allocate house specs if they haven't been allocated already. */
2011-08-21 12:05:47 +00:00
if ( _cur . grffile - > housespec = = NULL ) {
_cur . grffile - > housespec = CallocT < HouseSpec * > ( HOUSE_MAX ) ;
2007-03-19 11:27:30 +00:00
}
2007-07-04 12:52:43 +00:00
for ( int i = 0 ; i < numinfo ; i + + ) {
2011-08-21 12:05:47 +00:00
HouseSpec * housespec = _cur . grffile - > housespec [ hid + i ] ;
2007-03-19 11:27:30 +00:00
2007-07-04 12:52:43 +00:00
if ( prop ! = 0x08 & & housespec = = NULL ) {
2010-04-02 21:30:37 +00: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 11:27:30 +00:00
}
2007-07-04 12:52:43 +00:00
switch ( prop ) {
case 0x08 : { // Substitute building type, and definition of a new house
2011-08-21 12:05:47 +00:00
HouseSpec * * house = & _cur . grffile - > housespec [ hid + i ] ;
2010-01-23 13:17:30 +00:00
byte subs_id = buf - > ReadByte ( ) ;
2007-03-19 11:27:30 +00: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 . */
2009-06-26 14:02:27 +00:00
HouseSpec : : Get ( hid + i ) - > enabled = false ;
2007-03-19 11:27:30 +00:00
continue ;
} else if ( subs_id > = NEW_HOUSE_OFFSET ) {
/* The substitute id must be one of the original houses. */
grfmsg ( 2 , " TownHouseChangeInfo: Attempt to use new house %u as substitute house for %u. Ignoring. " , subs_id , hid + i ) ;
2008-08-31 15:26:46 +00:00
continue ;
2007-03-19 11:27:30 +00:00
}
/* Allocate space for this house. */
2007-07-04 12:52:43 +00:00
if ( * house = = NULL ) * house = CallocT < HouseSpec > ( 1 ) ;
2007-03-19 11:27:30 +00:00
2007-07-04 12:52:43 +00:00
housespec = * house ;
2007-03-19 11:27:30 +00:00
2009-06-26 14:02:27 +00:00
MemCpyT ( housespec , HouseSpec : : Get ( subs_id ) ) ;
2007-07-04 12:52:43 +00:00
housespec - > enabled = true ;
2010-08-07 21:03:38 +00:00
housespec - > grf_prop . local_id = hid + i ;
housespec - > grf_prop . subst_id = subs_id ;
2011-08-21 12:05:47 +00:00
housespec - > grf_prop . grffile = _cur . grffile ;
2007-07-04 12:52:43 +00:00
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 ;
2007-03-19 11:27:30 +00:00
2007-08-10 08:57:04 +00: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 . */
2009-07-16 19:00:13 +00:00
if ( ! CargoSpec : : Get ( housespec - > accepts_cargo [ 2 ] ) - > IsValid ( ) ) {
2007-08-10 08:57:04 +00:00
housespec - > cargo_acceptance [ 2 ] = 0 ;
}
2007-07-04 13:30:33 +00:00
/**
* New houses do not ( currently ) expect to have a default start
* date before 1930 , as this breaks the build date stuff .
* @ see FinaliseHouseArray ( ) for more details .
*/
2008-06-03 03:06:16 +00:00
if ( housespec - > min_year < 1930 ) housespec - > min_year = 1930 ;
2007-07-04 12:52:43 +00:00
_loaded_newgrf_features . has_newhouses = true ;
2010-08-01 18:53:30 +00:00
break ;
}
2007-07-04 12:52:43 +00:00
case 0x09 : // Building flags
2010-01-23 13:17:30 +00:00
housespec - > building_flags = ( BuildingFlags ) buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
case 0x0A : { // Availability years
2010-01-23 13:17:30 +00:00
uint16 years = buf - > ReadWord ( ) ;
2008-06-03 03:06:16 +00: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 18:53:30 +00:00
break ;
}
2007-07-04 12:52:43 +00:00
case 0x0B : // Population
2010-01-23 13:17:30 +00:00
housespec - > population = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
case 0x0C : // Mail generation multiplier
2010-01-23 13:17:30 +00:00
housespec - > mail_generation = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
case 0x0D : // Passenger acceptance
case 0x0E : // Mail acceptance
2010-01-23 13:17:30 +00:00
housespec - > cargo_acceptance [ prop - 0x0D ] = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
case 0x0F : { // Goods/candy, food/fizzy drinks acceptance
2010-01-23 13:17:30 +00:00
int8 goods = buf - > ReadByte ( ) ;
2007-03-20 02:24:14 +00: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 15:13:28 +00: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 02:24:14 +00:00
2007-08-10 08:57:04 +00:00
/* Make sure the cargo type is valid in this climate. */
2009-07-16 19:00:13 +00:00
if ( ! CargoSpec : : Get ( cid ) - > IsValid ( ) ) goods = 0 ;
2007-08-10 08:57:04 +00:00
housespec - > accepts_cargo [ 2 ] = cid ;
2007-07-04 12:52:43 +00:00
housespec - > cargo_acceptance [ 2 ] = abs ( goods ) ; // but we do need positive value here
2010-08-01 18:53:30 +00:00
break ;
}
2007-03-19 11:27:30 +00:00
2007-07-04 12:52:43 +00:00
case 0x10 : // Local authority rating decrease on removal
2010-01-23 13:17:30 +00:00
housespec - > remove_rating_decrease = buf - > ReadWord ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2007-03-19 11:27:30 +00:00
2007-07-04 12:52:43 +00:00
case 0x11 : // Removal cost multiplier
2010-01-23 13:17:30 +00:00
housespec - > removal_cost = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2007-03-19 11:27:30 +00:00
2007-07-04 12:52:43 +00:00
case 0x12 : // Building name ID
2010-01-23 13:17:30 +00:00
housespec - > building_name = buf - > ReadWord ( ) ;
2011-08-21 12:05:47 +00:00
_string_to_grf_mapping [ & housespec - > building_name ] = _cur . grffile - > grfid ;
2007-07-04 12:52:43 +00:00
break ;
2007-03-19 11:27:30 +00:00
2007-07-04 12:52:43 +00:00
case 0x13 : // Building availability mask
2010-01-23 13:17:30 +00:00
housespec - > building_availability = ( HouseZones ) buf - > ReadWord ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2007-03-19 11:27:30 +00:00
2009-09-14 12:22:57 +00:00
case 0x14 : // House callback mask
2010-01-23 13:17:30 +00:00
housespec - > callback_mask | = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2007-03-19 11:27:30 +00:00
2007-07-04 12:52:43 +00:00
case 0x15 : { // House override byte
2010-01-23 13:17:30 +00:00
byte override = buf - > ReadByte ( ) ;
2007-03-19 11:27:30 +00:00
/* The house being overridden must be an original house. */
if ( override > = NEW_HOUSE_OFFSET ) {
2007-07-04 12:57:07 +00:00
grfmsg ( 2 , " TownHouseChangeInfo: Attempt to override new house %u with house id %u. Ignoring. " , override , hid + i ) ;
2007-07-04 12:52:43 +00:00
continue ;
2007-03-19 11:27:30 +00:00
}
2011-08-21 12:05:47 +00:00
_house_mngr . Add ( hid + i , _cur . grffile - > grfid , override ) ;
2010-08-01 18:53:30 +00:00
break ;
}
2007-07-04 12:52:43 +00:00
case 0x16 : // Periodic refresh multiplier
2010-08-26 14:36:00 +00:00
housespec - > processing_time = min ( buf - > ReadByte ( ) , 63 ) ;
2007-07-04 12:52:43 +00:00
break ;
case 0x17 : // Four random colours to use
2010-01-23 13:17:30 +00:00
for ( uint j = 0 ; j < 4 ; j + + ) housespec - > random_colour [ j ] = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
case 0x18 : // Relative probability of appearing
2010-01-23 13:17:30 +00:00
housespec - > probability = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
case 0x19 : // Extra flags
2010-01-23 13:17:30 +00:00
housespec - > extra_flags = ( HouseExtraFlags ) buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
case 0x1A : // Animation frames
2010-08-26 15:31:40 +00: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 12:52:43 +00:00
break ;
case 0x1B : // Animation speed
2010-08-26 15:31:40 +00:00
housespec - > animation . speed = Clamp ( buf - > ReadByte ( ) , 2 , 16 ) ;
2007-07-04 12:52:43 +00:00
break ;
case 0x1C : // Class of the building type
2011-08-21 12:05:47 +00:00
housespec - > class_id = AllocateHouseClassID ( buf - > ReadByte ( ) , _cur . grffile - > grfid ) ;
2007-07-04 12:52:43 +00:00
break ;
2009-09-14 12:22:57 +00:00
case 0x1D : // Callback mask part 2
2010-01-23 13:17:30 +00:00
housespec - > callback_mask | = ( buf - > ReadByte ( ) < < 8 ) ;
2007-07-04 12:52:43 +00:00
break ;
case 0x1E : { // Accepted cargo types
2010-01-23 13:17:30 +00:00
uint32 cargotypes = buf - > ReadDWord ( ) ;
2007-04-13 21:18:48 +00: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 12:05:47 +00:00
CargoID cargo = GetCargoTranslation ( cargo_part , _cur . grffile ) ;
2007-04-13 21:18:48 +00:00
if ( cargo = = CT_INVALID ) {
/* Disable acceptance of invalid cargo type */
2007-07-04 12:52:43 +00:00
housespec - > cargo_acceptance [ j ] = 0 ;
2007-04-13 21:18:48 +00:00
} else {
2007-07-04 12:52:43 +00:00
housespec - > accepts_cargo [ j ] = cargo ;
2007-04-13 21:18:48 +00:00
}
}
2010-08-01 18:53:30 +00:00
break ;
}
2007-03-19 11:27:30 +00:00
2007-07-04 12:52:43 +00:00
case 0x1F : // Minimum life span
2010-01-23 13:17:30 +00:00
housespec - > minimum_life = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2007-05-10 16:03:06 +00:00
2007-10-25 23:24:28 +00:00
case 0x20 : { // @todo Cargo acceptance watch list
2010-01-23 13:17:30 +00:00
byte count = buf - > ReadByte ( ) ;
for ( byte j = 0 ; j < count ; j + + ) buf - > ReadByte ( ) ;
2008-08-31 16:41:27 +00:00
ret = CIR_UNHANDLED ;
2010-08-01 18:53:30 +00:00
break ;
}
2007-10-25 23:24:28 +00:00
2008-06-10 00:42:19 +00:00
case 0x21 : // long introduction year
2010-01-23 13:17:30 +00:00
housespec - > min_year = buf - > ReadWord ( ) ;
2008-06-10 00:42:19 +00:00
break ;
case 0x22 : // long maximum year
2010-01-23 13:17:30 +00:00
housespec - > max_year = buf - > ReadWord ( ) ;
2008-06-10 00:42:19 +00:00
break ;
2007-07-04 12:52:43 +00:00
default :
2008-08-31 16:41:27 +00:00
ret = CIR_UNKNOWN ;
2007-07-04 12:52:43 +00:00
break ;
}
2007-03-19 11:27:30 +00:00
}
return ret ;
}
2010-11-16 14:00:02 +00: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 .
* @ return the LanguageMap , or NULL if it couldn ' t be found .
*/
/* 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 ) ;
return ( grffile ! = NULL & & grffile - > language_map ! = NULL & & language_id < MAX_LANG ) ? & grffile - > language_map [ language_id ] : NULL ;
}
2010-01-22 13:02:15 +00:00
static ChangeInfoResult GlobalVarChangeInfo ( uint gvid , int numinfo , int prop , ByteReader * buf )
2005-10-12 07:27:56 +00:00
{
2008-08-31 16:41:27 +00:00
ChangeInfoResult ret = CIR_SUCCESS ;
2005-10-12 07:27:56 +00:00
2007-07-04 12:52:43 +00:00
for ( int i = 0 ; i < numinfo ; i + + ) {
switch ( prop ) {
2007-07-04 13:30:33 +00:00
case 0x08 : { // Cost base factor
2010-01-23 13:17:30 +00:00
int factor = buf - > ReadByte ( ) ;
2005-10-12 09:54:29 +00:00
uint price = gvid + i ;
2005-10-12 07:27:56 +00:00
2009-11-08 12:23:02 +00:00
if ( price < PR_END ) {
2011-08-21 12:05:47 +00:00
_cur . grffile - > price_base_multipliers [ price ] = min < int > ( factor - 8 , MAX_PRICE_MODIFIER ) ;
2005-10-12 09:54:29 +00:00
} else {
2006-12-26 17:36:18 +00:00
grfmsg ( 1 , " GlobalVarChangeInfo: Price %d out of range, ignoring " , price ) ;
2005-10-12 09:54:29 +00:00
}
2010-08-01 18:53:30 +00:00
break ;
}
2006-04-11 10:53:07 +00:00
2008-02-03 20:23:59 +00:00
case 0x09 : // Cargo translation table
/* This is loaded during the reservation stage, so just skip it here. */
/* Each entry is 4 bytes. */
2010-01-22 13:02:15 +00:00
buf - > Skip ( 4 ) ;
2008-02-03 20:23:59 +00:00
break ;
2007-02-24 19:36:47 +00:00
2007-07-04 12:52:43 +00:00
case 0x0A : { // Currency display names
2006-09-20 00:34:06 +00:00
uint curidx = GetNewgrfCurrencyIdConverted ( gvid + i ) ;
2011-08-21 12:05:47 +00:00
StringID newone = GetGRFStringID ( _cur . grffile - > grfid , buf - > ReadWord ( ) ) ;
2006-08-25 00:41:10 +00:00
2006-09-01 02:56:02 +00:00
if ( ( newone ! = STR_UNDEFINED ) & & ( curidx < NUM_CURRENCY ) ) {
_currency_specs [ curidx ] . name = newone ;
2006-08-25 00:41:10 +00:00
}
2010-08-01 18:53:30 +00:00
break ;
}
2006-08-25 00:41:10 +00:00
2007-07-04 12:52:43 +00:00
case 0x0B : { // Currency multipliers
2006-09-20 00:34:06 +00:00
uint curidx = GetNewgrfCurrencyIdConverted ( gvid + i ) ;
2010-01-23 13:17:30 +00:00
uint32 rate = buf - > ReadDWord ( ) ;
2006-08-25 00:41:10 +00:00
if ( curidx < NUM_CURRENCY ) {
2006-09-15 02:52:17 +00: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 00:41:10 +00:00
} else {
2006-12-26 17:36:18 +00:00
grfmsg ( 1 , " GlobalVarChangeInfo: Currency multipliers %d out of range, ignoring " , curidx ) ;
2006-08-25 00:41:10 +00:00
}
2010-08-01 18:53:30 +00:00
break ;
}
2006-08-25 00:41:10 +00:00
2007-07-04 12:52:43 +00:00
case 0x0C : { // Currency options
2006-09-20 00:34:06 +00:00
uint curidx = GetNewgrfCurrencyIdConverted ( gvid + i ) ;
2010-01-23 13:17:30 +00:00
uint16 options = buf - > ReadWord ( ) ;
2006-08-25 00:41:10 +00:00
if ( curidx < NUM_CURRENCY ) {
2009-04-25 20:16:56 +00:00
_currency_specs [ curidx ] . separator [ 0 ] = GB ( options , 0 , 8 ) ;
_currency_specs [ curidx ] . separator [ 1 ] = ' \0 ' ;
2006-09-15 02:52:17 +00: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 00:41:10 +00:00
} else {
2006-12-26 17:36:18 +00:00
grfmsg ( 1 , " GlobalVarChangeInfo: Currency option %d out of range, ignoring " , curidx ) ;
2006-08-25 00:41:10 +00:00
}
2010-08-01 18:53:30 +00:00
break ;
}
2006-08-25 00:41:10 +00:00
2007-07-04 12:52:43 +00:00
case 0x0D : { // Currency prefix symbol
2006-09-20 00:34:06 +00:00
uint curidx = GetNewgrfCurrencyIdConverted ( gvid + i ) ;
2010-01-23 13:17:30 +00:00
uint32 tempfix = buf - > ReadDWord ( ) ;
2006-08-25 00:41:10 +00:00
if ( curidx < NUM_CURRENCY ) {
2007-07-04 13:30:33 +00:00
memcpy ( _currency_specs [ curidx ] . prefix , & tempfix , 4 ) ;
2006-08-25 00:41:10 +00:00
_currency_specs [ curidx ] . prefix [ 4 ] = 0 ;
} else {
2006-12-26 17:36:18 +00:00
grfmsg ( 1 , " GlobalVarChangeInfo: Currency symbol %d out of range, ignoring " , curidx ) ;
2006-08-25 00:41:10 +00:00
}
2010-08-01 18:53:30 +00:00
break ;
}
2006-08-25 00:41:10 +00:00
2007-07-04 12:52:43 +00:00
case 0x0E : { // Currency suffix symbol
2006-09-20 00:34:06 +00:00
uint curidx = GetNewgrfCurrencyIdConverted ( gvid + i ) ;
2010-01-23 13:17:30 +00:00
uint32 tempfix = buf - > ReadDWord ( ) ;
2006-08-25 00:41:10 +00:00
if ( curidx < NUM_CURRENCY ) {
2007-04-18 22:10:36 +00:00
memcpy ( & _currency_specs [ curidx ] . suffix , & tempfix , 4 ) ;
2006-08-25 00:41:10 +00:00
_currency_specs [ curidx ] . suffix [ 4 ] = 0 ;
} else {
2006-12-26 17:36:18 +00:00
grfmsg ( 1 , " GlobalVarChangeInfo: Currency symbol %d out of range, ignoring " , curidx ) ;
2006-08-25 00:41:10 +00:00
}
2010-08-01 18:53:30 +00:00
break ;
}
2006-08-25 00:41:10 +00:00
2007-07-04 12:52:43 +00:00
case 0x0F : { // Euro introduction dates
2006-09-20 00:34:06 +00:00
uint curidx = GetNewgrfCurrencyIdConverted ( gvid + i ) ;
2010-01-23 13:17:30 +00:00
Year year_euro = buf - > ReadWord ( ) ;
2006-08-25 00:41:10 +00:00
if ( curidx < NUM_CURRENCY ) {
_currency_specs [ curidx ] . to_euro = year_euro ;
} else {
2006-12-26 17:36:18 +00:00
grfmsg ( 1 , " GlobalVarChangeInfo: Euro intro date %d out of range, ignoring " , curidx ) ;
2006-08-25 00:41:10 +00:00
}
2010-08-01 18:53:30 +00:00
break ;
}
2007-07-04 12:52:43 +00:00
2007-07-04 13:30:33 +00:00
case 0x10 : // Snow line height table
2007-07-04 12:52:43 +00:00
if ( numinfo > 1 | | IsSnowLineSet ( ) ) {
grfmsg ( 1 , " GlobalVarChangeInfo: The snowline can only be set once (%d) " , numinfo ) ;
2010-01-22 13:02:15 +00:00
} else if ( buf - > Remaining ( ) < SNOW_LINE_MONTHS * SNOW_LINE_DAYS ) {
grfmsg ( 1 , " GlobalVarChangeInfo: Not enough entries set in the snowline table ( " PRINTF_SIZE " ) " , buf - > Remaining ( ) ) ;
2007-07-04 12:52:43 +00: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 13:17:30 +00:00
table [ i ] [ j ] = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
}
2007-03-20 13:47:00 +00:00
}
2007-07-04 12:52:43 +00:00
SetSnowLine ( table ) ;
2007-03-20 13:47:00 +00:00
}
2007-07-04 12:52:43 +00:00
break ;
2007-03-20 13:47:00 +00:00
2008-04-29 21:31:29 +00: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 13:02:15 +00:00
buf - > Skip ( 8 ) ;
2008-04-29 21:31:29 +00:00
break ;
2009-02-08 18:11:06 +00:00
case 0x12 : // Rail type translation table
/* This is loaded during the reservation stage, so just skip it here. */
/* Each entry is 4 bytes. */
2010-01-22 13:02:15 +00:00
buf - > Skip ( 4 ) ;
2009-02-08 18:11:06 +00:00
break ;
2010-11-16 12:47:22 +00:00
case 0x13 : // Gender translation table
2010-11-16 21:03:39 +00:00
case 0x14 : // Case translation table
case 0x15 : { // Plural form translation
2010-11-16 12:47:22 +00:00
uint curidx = gvid + i ; // The current index, i.e. language.
const LanguageMetadata * lang = curidx < MAX_LANG ? GetLanguage ( curidx ) : NULL ;
if ( lang = = NULL ) {
grfmsg ( 1 , " GlobalVarChangeInfo: Language %d is not known, ignoring " , curidx ) ;
/* Skip over the data. */
while ( buf - > ReadByte ( ) ! = 0 ) {
buf - > ReadString ( ) ;
}
break ;
}
2011-08-21 12:05:47 +00:00
if ( _cur . grffile - > language_map = = NULL ) _cur . grffile - > language_map = new LanguageMap [ MAX_LANG ] ;
2010-11-16 12:47:22 +00:00
2010-11-16 21:03:39 +00:00
if ( prop = = 0x15 ) {
uint plural_form = buf - > ReadByte ( ) ;
if ( plural_form > = LANGUAGE_MAX_PLURAL ) {
grfmsg ( 1 , " GlobalVarChanceInfo: Plural form %d is out of range, ignoring " , plural_form ) ;
} else {
2011-08-21 12:05:47 +00:00
_cur . grffile - > language_map [ curidx ] . plural_form = plural_form ;
2010-11-16 21:03:39 +00:00
}
break ;
}
2010-11-16 12:47:22 +00: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 ) {
grfmsg ( 1 , " GlobalVarChangeInfo: Gender name %s is not known, ignoring " , name ) ;
} else {
2011-08-21 12:05:47 +00:00
* _cur . grffile - > language_map [ curidx ] . gender_map . Append ( ) = map ;
2010-11-16 12:47:22 +00:00
}
} else {
map . openttd_id = lang - > GetCaseIndex ( name ) ;
if ( map . openttd_id > = MAX_NUM_CASES ) {
grfmsg ( 1 , " GlobalVarChangeInfo: Case name %s is not known, ignoring " , name ) ;
} else {
2011-08-21 12:05:47 +00:00
* _cur . grffile - > language_map [ curidx ] . case_map . Append ( ) = map ;
2010-11-16 12:47:22 +00:00
}
}
newgrf_id = buf - > ReadByte ( ) ;
}
break ;
}
2007-07-04 12:52:43 +00:00
default :
2008-08-31 16:41:27 +00:00
ret = CIR_UNKNOWN ;
2007-07-04 12:52:43 +00:00
break ;
}
2005-10-12 07:27:56 +00:00
}
2006-04-11 10:53:07 +00:00
2005-10-12 07:27:56 +00:00
return ret ;
}
2010-01-22 13:02:15 +00:00
static ChangeInfoResult GlobalVarReserveInfo ( uint gvid , int numinfo , int prop , ByteReader * buf )
2009-01-09 20:14:07 +00:00
{
ChangeInfoResult ret = CIR_SUCCESS ;
for ( int i = 0 ; i < numinfo ; i + + ) {
switch ( prop ) {
case 0x08 : // Cost base factor
2010-11-16 21:03:39 +00:00
case 0x15 : // Plural form translation
2010-01-23 13:17:30 +00:00
buf - > ReadByte ( ) ;
2009-01-09 20:14:07 +00:00
break ;
case 0x09 : { // Cargo Translation Table
if ( i = = 0 ) {
if ( gvid ! = 0 ) {
grfmsg ( 1 , " ReserveChangeInfo: Cargo translation table must start at zero " ) ;
return CIR_INVALID_ID ;
}
2011-08-21 12:05:47 +00:00
free ( _cur . grffile - > cargo_list ) ;
_cur . grffile - > cargo_max = numinfo ;
_cur . grffile - > cargo_list = MallocT < CargoLabel > ( numinfo ) ;
2009-01-09 20:14:07 +00:00
}
2010-01-23 13:17:30 +00:00
CargoLabel cl = buf - > ReadDWord ( ) ;
2011-08-21 12:05:47 +00:00
_cur . grffile - > cargo_list [ i ] = BSWAP32 ( cl ) ;
2009-01-09 20:14:07 +00:00
break ;
}
case 0x0A : // Currency display names
case 0x0C : // Currency options
case 0x0F : // Euro introduction dates
2010-01-23 13:17:30 +00:00
buf - > ReadWord ( ) ;
2009-01-09 20:14:07 +00:00
break ;
case 0x0B : // Currency multipliers
case 0x0D : // Currency prefix symbol
case 0x0E : // Currency suffix symbol
2010-01-23 13:17:30 +00:00
buf - > ReadDWord ( ) ;
2009-01-09 20:14:07 +00:00
break ;
case 0x10 : // Snow line height table
2010-01-22 13:02:15 +00:00
buf - > Skip ( SNOW_LINE_MONTHS * SNOW_LINE_DAYS ) ;
2009-01-09 20:14:07 +00:00
break ;
case 0x11 : { // GRF match for engine allocation
2010-01-23 13:17:30 +00:00
uint32 s = buf - > ReadDWord ( ) ;
uint32 t = buf - > ReadDWord ( ) ;
2009-01-09 20:14:07 +00:00
SetNewGRFOverride ( s , t ) ;
break ;
}
2009-02-08 18:11:06 +00:00
case 0x12 : { // Rail type translation table
if ( i = = 0 ) {
if ( gvid ! = 0 ) {
grfmsg ( 1 , " ReserveChangeInfo: Rail type translation table must start at zero " ) ;
return CIR_INVALID_ID ;
}
2011-08-21 12:05:47 +00:00
free ( _cur . grffile - > railtype_list ) ;
_cur . grffile - > railtype_max = numinfo ;
_cur . grffile - > railtype_list = MallocT < RailTypeLabel > ( numinfo ) ;
2009-02-08 18:11:06 +00:00
}
2010-01-23 13:17:30 +00:00
RailTypeLabel rtl = buf - > ReadDWord ( ) ;
2011-08-21 12:05:47 +00:00
_cur . grffile - > railtype_list [ i ] = BSWAP32 ( rtl ) ;
2009-02-08 18:11:06 +00:00
break ;
}
2010-11-16 12:47:22 +00:00
case 0x13 : // Gender translation table
case 0x14 : // Case translation table
while ( buf - > ReadByte ( ) ! = 0 ) {
buf - > ReadString ( ) ;
}
break ;
2009-01-09 20:14:07 +00:00
default :
ret = CIR_UNKNOWN ;
break ;
}
}
return ret ;
}
2010-01-22 13:02:15 +00:00
static ChangeInfoResult CargoChangeInfo ( uint cid , int numinfo , int prop , ByteReader * buf )
2007-03-22 23:19:40 +00:00
{
2008-08-31 16:41:27 +00:00
ChangeInfoResult ret = CIR_SUCCESS ;
2007-07-04 12:52:43 +00:00
2007-03-22 23:19:40 +00:00
if ( cid + numinfo > NUM_CARGO ) {
grfmsg ( 2 , " CargoChangeInfo: Cargo type %d out of range (max %d) " , cid + numinfo , NUM_CARGO - 1 ) ;
2008-08-31 16:41:27 +00:00
return CIR_INVALID_ID ;
2007-03-22 23:19:40 +00:00
}
2007-07-04 12:52:43 +00:00
for ( int i = 0 ; i < numinfo ; i + + ) {
2009-07-16 19:00:13 +00:00
CargoSpec * cs = CargoSpec : : Get ( cid + i ) ;
2007-03-22 23:19:40 +00:00
2007-07-04 12:52:43 +00:00
switch ( prop ) {
2009-03-15 00:32:18 +00:00
case 0x08 : // Bit number of cargo
2010-01-23 13:17:30 +00:00
cs - > bitnum = buf - > ReadByte ( ) ;
2007-03-22 23:19:40 +00:00
if ( cs - > IsValid ( ) ) {
2011-08-21 12:05:47 +00:00
cs - > grffile = _cur . grffile ;
2007-11-20 13:35:54 +00:00
SetBit ( _cargo_mask , cid + i ) ;
2007-03-22 23:19:40 +00:00
} else {
2007-11-19 21:32:20 +00:00
ClrBit ( _cargo_mask , cid + i ) ;
2007-03-22 23:19:40 +00:00
}
2007-07-04 12:52:43 +00:00
break ;
2009-03-15 00:32:18 +00:00
case 0x09 : // String ID for cargo type name
2010-01-23 13:17:30 +00:00
cs - > name = buf - > ReadWord ( ) ;
2011-08-21 12:05:47 +00:00
_string_to_grf_mapping [ & cs - > name ] = _cur . grffile - > grfid ;
2007-07-04 12:52:43 +00:00
break ;
2009-03-15 00:32:18 +00:00
case 0x0A : // String for 1 unit of cargo
2010-01-23 13:17:30 +00:00
cs - > name_single = buf - > ReadWord ( ) ;
2011-08-21 12:05:47 +00:00
_string_to_grf_mapping [ & cs - > name_single ] = _cur . grffile - > grfid ;
2007-07-04 12:52:43 +00:00
break ;
2010-11-17 18:35:59 +00: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 . */
2010-01-23 13:17:30 +00:00
cs - > units_volume = buf - > ReadWord ( ) ;
2011-08-21 12:05:47 +00:00
_string_to_grf_mapping [ & cs - > units_volume ] = _cur . grffile - > grfid ;
2007-07-04 12:52:43 +00:00
break ;
2010-11-17 18:35:59 +00: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 . */
2010-01-23 13:17:30 +00:00
cs - > quantifier = buf - > ReadWord ( ) ;
2011-08-21 12:05:47 +00:00
_string_to_grf_mapping [ & cs - > quantifier ] = _cur . grffile - > grfid ;
2007-07-04 12:52:43 +00:00
break ;
2009-03-15 00:32:18 +00:00
case 0x0D : // String for two letter cargo abbreviation
2010-01-23 13:17:30 +00:00
cs - > abbrev = buf - > ReadWord ( ) ;
2011-08-21 12:05:47 +00:00
_string_to_grf_mapping [ & cs - > abbrev ] = _cur . grffile - > grfid ;
2007-07-04 12:52:43 +00:00
break ;
2009-03-15 00:32:18 +00:00
case 0x0E : // Sprite ID for cargo icon
2010-01-23 13:17:30 +00:00
cs - > sprite = buf - > ReadWord ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2009-03-15 00:32:18 +00:00
case 0x0F : // Weight of one unit of cargo
2010-01-23 13:17:30 +00:00
cs - > weight = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2009-03-15 00:32:18 +00:00
case 0x10 : // Used for payment calculation
2010-01-23 13:17:30 +00:00
cs - > transit_days [ 0 ] = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2009-03-15 00:32:18 +00:00
case 0x11 : // Used for payment calculation
2010-01-23 13:17:30 +00:00
cs - > transit_days [ 1 ] = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2009-03-15 00:32:18 +00:00
case 0x12 : // Base cargo price
2010-01-23 13:17:30 +00:00
cs - > initial_payment = buf - > ReadDWord ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2009-03-15 00:32:18 +00:00
case 0x13 : // Colour for station rating bars
2011-05-04 17:12:37 +00:00
cs - > rating_colour = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2009-03-15 00:32:18 +00:00
case 0x14 : // Colour for cargo graph
2011-05-04 17:12:37 +00:00
cs - > legend_colour = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2009-03-15 00:32:18 +00:00
case 0x15 : // Freight status
2010-01-23 13:17:30 +00:00
cs - > is_freight = ( buf - > ReadByte ( ) ! = 0 ) ;
2007-07-04 12:52:43 +00:00
break ;
2009-03-15 00:32:18 +00:00
case 0x16 : // Cargo classes
2010-01-23 13:17:30 +00:00
cs - > classes = buf - > ReadWord ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2009-03-15 00:32:18 +00:00
case 0x17 : // Cargo label
2010-01-23 13:17:30 +00:00
cs - > label = buf - > ReadDWord ( ) ;
2007-07-04 12:52:43 +00:00
cs - > label = BSWAP32 ( cs - > label ) ;
break ;
2009-03-15 00:32:18 +00:00
case 0x18 : { // Town growth substitute type
2010-01-23 13:17:30 +00:00
uint8 substitute_type = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
2007-03-22 23:19:40 +00:00
switch ( substitute_type ) {
2007-07-04 12:52:43 +00: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-22 23:19:40 +00:00
default :
grfmsg ( 1 , " CargoChangeInfo: Unknown town growth substitute value %d, setting to none. " , substitute_type ) ;
2007-07-04 12:52:43 +00:00
case 0xFF : cs - > town_effect = TE_NONE ; break ;
2007-03-22 23:19:40 +00:00
}
2010-08-01 18:53:30 +00:00
break ;
}
2007-03-22 23:19:40 +00:00
2009-03-15 00:32:18 +00:00
case 0x19 : // Town growth coefficient
2010-01-23 13:17:30 +00:00
cs - > multipliertowngrowth = buf - > ReadWord ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2007-03-22 23:19:40 +00:00
2009-03-15 00:32:18 +00:00
case 0x1A : // Bitmask of callbacks to use
2010-01-23 13:17:30 +00:00
cs - > callback_mask = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2007-03-22 23:19:40 +00:00
2007-07-04 12:52:43 +00:00
default :
2008-08-31 16:41:27 +00:00
ret = CIR_UNKNOWN ;
2007-07-04 12:52:43 +00:00
break ;
}
2007-03-22 23:19:40 +00:00
}
return ret ;
}
2010-01-22 13:02:15 +00:00
static ChangeInfoResult SoundEffectChangeInfo ( uint sid , int numinfo , int prop , ByteReader * buf )
2006-09-27 18:17:01 +00:00
{
2008-08-31 16:41:27 +00:00
ChangeInfoResult ret = CIR_SUCCESS ;
2006-09-27 18:17:01 +00:00
2011-08-21 12:05:47 +00:00
if ( _cur . grffile - > sound_offset = = 0 ) {
2006-12-26 17:36:18 +00:00
grfmsg ( 1 , " SoundEffectChangeInfo: No effects defined, skipping " ) ;
2008-08-31 16:41:27 +00:00
return CIR_INVALID_ID ;
2006-09-27 18:17:01 +00:00
}
2011-08-21 12:05:47 +00:00
if ( sid + numinfo - ORIGINAL_SAMPLE_COUNT > _cur . grffile - > num_sounds ) {
grfmsg ( 1 , " SoundEffectChangeInfo: Attemting to change undefined sound effect (%u), max (%u). Ignoring. " , sid + numinfo , ORIGINAL_SAMPLE_COUNT + _cur . grffile - > num_sounds ) ;
2010-04-20 05:52:51 +00:00
return CIR_INVALID_ID ;
}
2006-09-27 18:17:01 +00:00
2010-04-20 05:52:51 +00:00
for ( int i = 0 ; i < numinfo ; i + + ) {
2011-08-21 12:05:47 +00:00
SoundEntry * sound = GetSound ( sid + i + _cur . grffile - > sound_offset - ORIGINAL_SAMPLE_COUNT ) ;
2006-09-27 18:17:01 +00:00
2007-07-04 12:52:43 +00:00
switch ( prop ) {
case 0x08 : // Relative volume
2010-04-20 05:52:51 +00:00
sound - > volume = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2006-09-27 18:17:01 +00:00
2007-07-04 12:52:43 +00:00
case 0x09 : // Priority
2010-04-20 05:52:51 +00:00
sound - > priority = buf - > ReadByte ( ) ;
2007-07-04 12:52:43 +00:00
break ;
2006-09-27 18:17:01 +00:00
2007-07-04 12:52:43 +00:00
case 0x0A : { // Override old sound
2010-01-23 13:17:30 +00:00
SoundID orig_sound = buf - > ReadByte ( ) ;
2006-09-27 18:17:01 +00:00
2009-05-17 17:00:30 +00:00
if ( orig_sound > = ORIGINAL_SAMPLE_COUNT ) {
grfmsg ( 1 , " SoundEffectChangeInfo: Original sound %d not defined (max %d) " , orig_sound , ORIGINAL_SAMPLE_COUNT ) ;
2006-09-27 18:17:01 +00:00
} else {
2009-05-17 19:27:50 +00:00
SoundEntry * old_sound = GetSound ( orig_sound ) ;
2006-09-27 18:17:01 +00:00
/* Literally copy the data of the new sound over the original */
2010-04-20 05:52:51 +00:00
* old_sound = * sound ;
2006-09-27 18:17:01 +00:00
}
2010-08-01 18:53:30 +00:00
break ;
}
2006-09-27 18:17:01 +00:00
2007-07-04 12:52:43 +00:00
default :
2008-08-31 16:41:27 +00:00
ret = CIR_UNKNOWN ;
2007-07-04 12:52:43 +00:00
break ;
}
2006-09-27 18:17:01 +00:00
}
return ret ;
}
2010-04-02 21:30:37 +00: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 ;
default :
ret = CIR_UNKNOWN ;
break ;
}
return ret ;
}
2010-01-22 13:02:15 +00:00
static ChangeInfoResult IndustrytilesChangeInfo ( uint indtid , int numinfo , int prop , ByteReader * buf )
2007-07-10 00:10:19 +00:00
{
2008-08-31 16:41:27 +00:00
ChangeInfoResult ret = CIR_SUCCESS ;
2007-07-10 00:10:19 +00:00
if ( indtid + numinfo > NUM_INDUSTRYTILES ) {
grfmsg ( 1 , " IndustryTilesChangeInfo: Too many industry tiles loaded (%u), max (%u). Ignoring. " , indtid + numinfo , NUM_INDUSTRYTILES ) ;
2008-08-31 16:41:27 +00:00
return CIR_INVALID_ID ;
2007-07-10 00:10:19 +00:00
}
/* Allocate industry tile specs if they haven't been allocated already. */
2011-08-21 12:05:47 +00:00
if ( _cur . grffile - > indtspec = = NULL ) {
_cur . grffile - > indtspec = CallocT < IndustryTileSpec * > ( NUM_INDUSTRYTILES ) ;
2007-07-10 00:10:19 +00:00
}
for ( int i = 0 ; i < numinfo ; i + + ) {
2011-08-21 12:05:47 +00:00
IndustryTileSpec * tsp = _cur . grffile - > indtspec [ indtid + i ] ;
2007-07-10 00:10:19 +00:00
if ( prop ! = 0x08 & & tsp = = NULL ) {
2010-04-02 21:30:37 +00:00
ChangeInfoResult cir = IgnoreIndustryTileProperty ( prop , buf ) ;
if ( cir > ret ) ret = cir ;
continue ;
2007-07-10 00:10:19 +00:00
}
switch ( prop ) {
case 0x08 : { // Substitute industry tile type
2011-08-21 12:05:47 +00:00
IndustryTileSpec * * tilespec = & _cur . grffile - > indtspec [ indtid + i ] ;
2010-01-23 13:17:30 +00:00
byte subs_id = buf - > ReadByte ( ) ;
2007-07-10 00:10:19 +00:00
2007-08-21 00:29:10 +00:00
if ( subs_id > = NEW_INDUSTRYTILEOFFSET ) {
2007-07-10 00:10:19 +00:00
/* The substitute id must be one of the original industry tile. */
grfmsg ( 2 , " IndustryTilesChangeInfo: Attempt to use new industry tile %u as substitute industry tile for %u. Ignoring. " , subs_id , indtid + i ) ;
2008-08-31 15:26:46 +00:00
continue ;
2007-07-10 00:10:19 +00:00
}
/* Allocate space for this industry. */
if ( * tilespec = = NULL ) {
* tilespec = CallocT < IndustryTileSpec > ( 1 ) ;
tsp = * tilespec ;
memcpy ( tsp , & _industry_tile_specs [ subs_id ] , sizeof ( _industry_tile_specs [ subs_id ] ) ) ;
tsp - > enabled = true ;
2007-12-18 21:25:44 +00:00
/* A copied tile should not have the animation infos copied too.
* The anim_state should be left untouched , though
* It is up to the author to animate them himself */
tsp - > anim_production = INDUSTRYTILE_NOANIM ;
tsp - > anim_next = INDUSTRYTILE_NOANIM ;
2007-07-10 00:10:19 +00:00
tsp - > grf_prop . local_id = indtid + i ;
tsp - > grf_prop . subst_id = subs_id ;
2011-08-21 12:05:47 +00: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 00:10:19 +00:00
}
2010-08-01 18:53:30 +00:00
break ;
}
2007-07-10 00:10:19 +00:00
case 0x09 : { // Industry tile override
2010-01-23 13:17:30 +00:00
byte ovrid = buf - > ReadByte ( ) ;
2007-07-10 00:10:19 +00:00
/* The industry being overridden must be an original industry. */
if ( ovrid > = NEW_INDUSTRYTILEOFFSET ) {
grfmsg ( 2 , " IndustryTilesChangeInfo: Attempt to override new industry tile %u with industry tile id %u. Ignoring. " , ovrid , indtid + i ) ;
2008-08-31 15:26:46 +00:00
continue ;
2007-07-10 00:10:19 +00:00
}
2011-08-21 12:05:47 +00:00
_industile_mngr . Add ( indtid + i , _cur . grffile - > grfid , ovrid ) ;
2010-08-01 18:53:30 +00:00
break ;
}
2007-07-10 00:10:19 +00:00
case 0x0A : // Tile acceptance
case 0x0B :
case 0x0C : {
2010-01-23 13:17:30 +00:00
uint16 acctp = buf - > ReadWord ( ) ;
2011-08-21 12:05:47 +00:00
tsp - > accepts_cargo [ prop - 0x0A ] = GetCargoTranslation ( GB ( acctp , 0 , 8 ) , _cur . grffile ) ;
2007-09-26 02:11:30 +00:00
tsp - > acceptance [ prop - 0x0A ] = GB ( acctp , 8 , 8 ) ;
2010-08-01 18:53:30 +00:00
break ;
}
2007-07-10 00:10:19 +00:00
case 0x0D : // Land shape flags
2010-01-23 13:17:30 +00:00
tsp - > slopes_refused = ( Slope ) buf - > ReadByte ( ) ;
2007-07-10 00:10:19 +00:00
break ;
2009-09-14 12:22:57 +00:00
case 0x0E : // Callback mask
2010-01-23 13:17:30 +00:00
tsp - > callback_mask = buf - > ReadByte ( ) ;
2007-07-10 00:10:19 +00:00
break ;
case 0x0F : // Animation information
2010-08-26 15:31:40 +00:00
tsp - > animation . frames = buf - > ReadByte ( ) ;
tsp - > animation . status = buf - > ReadByte ( ) ;
2007-07-10 00:10:19 +00:00
break ;
case 0x10 : // Animation speed
2010-08-26 15:31:40 +00:00
tsp - > animation . speed = buf - > ReadByte ( ) ;
2007-07-10 00:10:19 +00:00
break ;
case 0x11 : // Triggers for callback 25
2010-08-26 15:31:40 +00:00
tsp - > animation . triggers = buf - > ReadByte ( ) ;
2007-07-10 00:10:19 +00:00
break ;
case 0x12 : // Special flags
2010-02-07 12:04:44 +00:00
tsp - > special_flags = ( IndustryTileSpecialFlags ) buf - > ReadByte ( ) ;
2007-07-10 00:10:19 +00:00
break ;
default :
2008-08-31 16:41:27 +00:00
ret = CIR_UNKNOWN ;
2007-07-10 00:10:19 +00:00
break ;
}
}
return ret ;
}
2010-04-02 21:30:37 +00: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 15:24:35 +00:00
case 0x11 :
2010-04-02 21:30:37 +00: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 18:53:30 +00:00
break ;
}
2010-04-02 21:30:37 +00:00
case 0x16 :
for ( byte j = 0 ; j < 3 ; j + + ) buf - > ReadByte ( ) ;
break ;
case 0x15 : {
byte number_of_sounds = buf - > ReadByte ( ) ;
for ( uint8 j = 0 ; j < number_of_sounds ; j + + ) {
buf - > ReadByte ( ) ;
}
2010-08-01 18:53:30 +00:00
break ;
}
2010-04-02 21:30:37 +00:00
default :
ret = CIR_UNKNOWN ;
break ;
}
return ret ;
}
2009-09-21 18:31:47 +00:00
/**
* Validate the industry layout ; e . g . to prevent duplicate tiles .
* @ param layout the layout to check
* @ param size the size of the layout
* @ return true if the layout is deemed valid
*/
static bool ValidateIndustryLayout ( const IndustryTileTable * layout , int size )
{
for ( int i = 0 ; i < size - 1 ; i + + ) {
for ( int j = i + 1 ; j < size ; j + + ) {
if ( layout [ i ] . ti . x = = layout [ j ] . ti . x & &
layout [ i ] . ti . y = = layout [ j ] . ti . y ) {
return false ;
}
}
}
return true ;
}
2011-03-03 20:26:19 +00:00
/** Clean the tile table of the IndustrySpec if it's needed. */
static void CleanIndustryTileTable ( IndustrySpec * ind )
{
if ( HasBit ( ind - > cleanup_flag , CLEAN_TILELAYOUT ) & & ind - > table ! = NULL ) {
for ( int j = 0 ; j < ind - > num_table ; j + + ) {
/* remove the individual layouts */
free ( ( void * ) ind - > table [ j ] ) ;
}
/* remove the layouts pointers */
free ( ( void * ) ind - > table ) ;
ind - > table = NULL ;
}
}
2010-01-22 13:02:15 +00:00
static ChangeInfoResult IndustriesChangeInfo ( uint indid , int numinfo , int prop , ByteReader * buf )
2007-07-10 00:10:19 +00:00
{
2008-08-31 16:41:27 +00:00
ChangeInfoResult ret = CIR_SUCCESS ;
2007-07-10 00:10:19 +00:00
if ( indid + numinfo > NUM_INDUSTRYTYPES ) {
grfmsg ( 1 , " IndustriesChangeInfo: Too many industries loaded (%u), max (%u). Ignoring. " , indid + numinfo , NUM_INDUSTRYTYPES ) ;
2008-08-31 16:41:27 +00:00
return CIR_INVALID_ID ;
2007-07-10 00:10:19 +00:00
}
grfmsg ( 1 , " IndustriesChangeInfo: newid %u " , indid ) ;
/* Allocate industry specs if they haven't been allocated already. */
2011-08-21 12:05:47 +00:00
if ( _cur . grffile - > industryspec = = NULL ) {
_cur . grffile - > industryspec = CallocT < IndustrySpec * > ( NUM_INDUSTRYTYPES ) ;
2007-07-10 00:10:19 +00:00
}
for ( int i = 0 ; i < numinfo ; i + + ) {
2011-08-21 12:05:47 +00:00
IndustrySpec * indsp = _cur . grffile - > industryspec [ indid + i ] ;
2007-07-10 00:10:19 +00:00
if ( prop ! = 0x08 & & indsp = = NULL ) {
2010-04-02 21:30:37 +00:00
ChangeInfoResult cir = IgnoreIndustryProperty ( prop , buf ) ;
if ( cir > ret ) ret = cir ;
continue ;
2007-07-10 00:10:19 +00:00
}
switch ( prop ) {
case 0x08 : { // Substitute industry type
2011-08-21 12:05:47 +00:00
IndustrySpec * * indspec = & _cur . grffile - > industryspec [ indid + i ] ;
2010-01-23 13:17:30 +00:00
byte subs_id = buf - > ReadByte ( ) ;
2007-07-10 00:10:19 +00: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. */
grfmsg ( 2 , " _industry_specs: Attempt to use new industry %u as substitute industry for %u. Ignoring. " , subs_id , indid + i ) ;
2008-08-31 15:26:46 +00:00
continue ;
2007-07-10 00:10:19 +00:00
}
/* Allocate space for this industry.
* Only need to do it once . If ever it is called again , it should not
* do anything */
if ( * indspec = = NULL ) {
* indspec = CallocT < IndustrySpec > ( 1 ) ;
indsp = * indspec ;
memcpy ( indsp , & _origin_industry_specs [ subs_id ] , sizeof ( _industry_specs [ subs_id ] ) ) ;
indsp - > enabled = true ;
indsp - > grf_prop . local_id = indid + i ;
indsp - > grf_prop . subst_id = subs_id ;
2011-08-21 12:05:47 +00:00
indsp - > grf_prop . grffile = _cur . grffile ;
2007-10-13 00:49:13 +00:00
/* If the grf industry needs to check its surounding upon creation, it should
* rely on callbacks , not on the original placement functions */
indsp - > check_proc = CHECK_NOTHING ;
2007-07-10 00:10:19 +00:00
}
2010-08-01 18:53:30 +00:00
break ;
}
2007-07-10 00:10:19 +00:00
case 0x09 : { // Industry type override
2010-01-23 13:17:30 +00:00
byte ovrid = buf - > ReadByte ( ) ;
2007-07-10 00:10:19 +00:00
/* The industry being overridden must be an original industry. */
if ( ovrid > = NEW_INDUSTRYOFFSET ) {
grfmsg ( 2 , " IndustriesChangeInfo: Attempt to override new industry %u with industry id %u. Ignoring. " , ovrid , indid + i ) ;
2008-08-31 15:26:46 +00:00
continue ;
2007-07-10 00:10:19 +00:00
}
indsp - > grf_prop . override = ovrid ;
2011-08-21 12:05:47 +00:00
_industry_mngr . Add ( indid + i , _cur . grffile - > grfid , ovrid ) ;
2010-08-01 18:53:30 +00:00
break ;
}
2007-07-10 00:10:19 +00:00
case 0x0A : { // Set industry layout(s)
2011-03-03 20:26:19 +00:00
byte new_num_layouts = buf - > ReadByte ( ) ; // Number of layaouts
2009-12-17 23:26:25 +00:00
/* We read the total size in bytes, but we can't rely on the
* newgrf to provide a sane value . First assume the value is
* sane but later on we make sure we enlarge the array if the
* newgrf contains more data . Each tile uses either 3 or 5
* bytes , so to play it safe we assume 3. */
2010-01-23 13:17:30 +00:00
uint32 def_num_tiles = buf - > ReadDWord ( ) / 3 + 1 ;
2011-03-03 20:26:19 +00:00
IndustryTileTable * * tile_table = CallocT < IndustryTileTable * > ( new_num_layouts ) ; // Table with tiles to compose an industry
2009-12-17 23:26:25 +00:00
IndustryTileTable * itt = CallocT < IndustryTileTable > ( def_num_tiles ) ; // Temporary array to read the tile layouts from the GRF
uint size ;
2009-05-24 20:29:04 +00:00
const IndustryTileTable * copy_from ;
2007-07-10 00:10:19 +00:00
2010-01-22 13:02:15 +00:00
try {
2011-03-03 20:26:19 +00:00
for ( byte j = 0 ; j < new_num_layouts ; j + + ) {
2010-01-22 13:02:15 +00:00
for ( uint k = 0 ; ; k + + ) {
if ( k > = def_num_tiles ) {
grfmsg ( 3 , " IndustriesChangeInfo: Incorrect size for industry tile layout definition for industry %u. " , indid ) ;
/* Size reported by newgrf was not big enough so enlarge the array. */
def_num_tiles * = 2 ;
itt = ReallocT < IndustryTileTable > ( itt , def_num_tiles ) ;
}
2009-12-17 23:26:25 +00:00
2010-01-23 13:17:30 +00:00
itt [ k ] . ti . x = buf - > ReadByte ( ) ; // Offsets from northermost tile
2007-07-10 00:10:19 +00:00
2010-01-22 13:02:15 +00:00
if ( itt [ k ] . ti . x = = 0xFE & & k = = 0 ) {
/* This means we have to borrow the layout from an old industry */
2010-01-23 13:17:30 +00:00
IndustryType type = buf - > ReadByte ( ) ; // industry holding required layout
byte laynbr = buf - > ReadByte ( ) ; // layout number to borrow
2007-07-10 00:10:19 +00:00
2010-01-22 13:02:15 +00:00
copy_from = _origin_industry_specs [ type ] . table [ laynbr ] ;
for ( size = 1 ; ; size + + ) {
if ( copy_from [ size - 1 ] . ti . x = = - 0x80 & & copy_from [ size - 1 ] . ti . y = = 0 ) break ;
}
break ;
2007-07-10 00:10:19 +00:00
}
2010-01-23 13:17:30 +00:00
itt [ k ] . ti . y = buf - > ReadByte ( ) ; // Or table definition finalisation
2007-07-10 00:10:19 +00:00
2010-01-22 13:02:15 +00:00
if ( itt [ k ] . ti . x = = 0 & & itt [ k ] . ti . y = = 0x80 ) {
/* Not the same terminator. The one we are using is rather
x = - 80 , y = x . So , adjust it . */
itt [ k ] . ti . x = - 0x80 ;
itt [ k ] . ti . y = 0 ;
itt [ k ] . gfx = 0 ;
2007-07-10 00:10:19 +00:00
2010-01-22 13:02:15 +00:00
size = k + 1 ;
copy_from = itt ;
break ;
}
2007-07-10 00:10:19 +00:00
2010-01-23 13:17:30 +00:00
itt [ k ] . gfx = buf - > ReadByte ( ) ;
2007-07-10 00:10:19 +00:00
2010-01-22 13:02:15 +00:00
if ( itt [ k ] . gfx = = 0xFE ) {
/* Use a new tile from this GRF */
2010-01-23 13:17:30 +00:00
int local_tile_id = buf - > ReadWord ( ) ;
2007-07-10 00:10:19 +00:00
2010-01-22 13:02:15 +00:00
/* Read the ID from the _industile_mngr. */
2011-08-21 12:05:47 +00:00
int tempid = _industile_mngr . GetID ( local_tile_id , _cur . grffile - > grfid ) ;
2007-07-10 00:10:19 +00:00
2010-01-22 13:02:15 +00:00
if ( tempid = = INVALID_INDUSTRYTILE ) {
grfmsg ( 2 , " IndustriesChangeInfo: Attempt to use industry tile %u with industry id %u, not yet defined. Ignoring. " , local_tile_id , indid ) ;
} else {
/* Declared as been valid, can be used */
itt [ k ] . gfx = tempid ;
size = k + 1 ;
copy_from = itt ;
}
} else if ( itt [ k ] . gfx = = 0xFF ) {
itt [ k ] . ti . x = ( int8 ) GB ( itt [ k ] . ti . x , 0 , 8 ) ;
itt [ k ] . ti . y = ( int8 ) GB ( itt [ k ] . ti . y , 0 , 8 ) ;
2007-07-10 00:10:19 +00:00
}
2007-09-26 02:20:58 +00:00
}
2009-09-21 18:31:47 +00:00
2010-01-22 13:02:15 +00:00
if ( ! ValidateIndustryLayout ( copy_from , size ) ) {
/* The industry layout was not valid, so skip this one. */
grfmsg ( 1 , " IndustriesChangeInfo: Invalid industry layout for industry id %u. Ignoring " , indid ) ;
2011-03-03 20:26:19 +00:00
new_num_layouts - - ;
2010-01-22 13:02:15 +00:00
j - - ;
} else {
tile_table [ j ] = CallocT < IndustryTileTable > ( size ) ;
memcpy ( tile_table [ j ] , copy_from , sizeof ( * copy_from ) * size ) ;
}
2009-09-21 18:31:47 +00:00
}
2010-01-22 13:02:15 +00:00
} catch ( . . . ) {
2011-03-03 20:26:19 +00:00
for ( int i = 0 ; i < new_num_layouts ; i + + ) {
2010-01-22 14:03:36 +00:00
free ( tile_table [ i ] ) ;
}
2010-01-22 13:02:15 +00:00
free ( tile_table ) ;
free ( itt ) ;
throw ;
2007-07-10 00:10:19 +00:00
}
2010-01-22 13:02:15 +00:00
2011-03-03 20:26:19 +00:00
/* Clean the tile table if it was already set by a previous prop A. */
CleanIndustryTileTable ( indsp ) ;
2007-07-10 00:10:19 +00:00
/* Install final layout construction in the industry spec */
2011-03-03 20:26:19 +00:00
indsp - > num_table = new_num_layouts ;
2007-07-10 00:10:19 +00:00
indsp - > table = tile_table ;
2011-03-03 20:26:19 +00:00
SetBit ( indsp - > cleanup_flag , CLEAN_TILELAYOUT ) ;
2007-07-10 00:10:19 +00:00
free ( itt ) ;
2010-08-01 18:53:30 +00:00
break ;
}
2007-07-10 00:10:19 +00:00
case 0x0B : // Industry production flags
2010-01-23 13:17:30 +00:00
indsp - > life_type = ( IndustryLifeType ) buf - > ReadByte ( ) ;
2007-07-10 00:10:19 +00:00
break ;
case 0x0C : // Industry closure message
2010-01-23 13:17:30 +00:00
indsp - > closure_text = buf - > ReadWord ( ) ;
2011-08-21 12:05:47 +00:00
_string_to_grf_mapping [ & indsp - > closure_text ] = _cur . grffile - > grfid ;
2007-07-10 00:10:19 +00:00
break ;
case 0x0D : // Production increase message
2010-01-23 13:17:30 +00:00
indsp - > production_up_text = buf - > ReadWord ( ) ;
2011-08-21 12:05:47 +00:00
_string_to_grf_mapping [ & indsp - > production_up_text ] = _cur . grffile - > grfid ;
2007-07-10 00:10:19 +00:00
break ;
case 0x0E : // Production decrease message
2010-01-23 13:17:30 +00:00
indsp - > production_down_text = buf - > ReadWord ( ) ;
2011-08-21 12:05:47 +00:00
_string_to_grf_mapping [ & indsp - > production_down_text ] = _cur . grffile - > grfid ;
2007-07-10 00:10:19 +00:00
break ;
case 0x0F : // Fund cost multiplier
2010-01-23 13:17:30 +00:00
indsp - > cost_multiplier = buf - > ReadByte ( ) ;
2007-07-10 00:10:19 +00:00
break ;
case 0x10 : // Production cargo types
for ( byte j = 0 ; j < 2 ; j + + ) {
2011-08-21 12:05:47 +00:00
indsp - > produced_cargo [ j ] = GetCargoTranslation ( buf - > ReadByte ( ) , _cur . grffile ) ;
2007-07-10 00:10:19 +00:00
}
break ;
case 0x11 : // Acceptance cargo types
for ( byte j = 0 ; j < 3 ; j + + ) {
2011-08-21 12:05:47 +00:00
indsp - > accepts_cargo [ j ] = GetCargoTranslation ( buf - > ReadByte ( ) , _cur . grffile ) ;
2007-07-10 00:10:19 +00:00
}
2010-01-23 13:17:30 +00:00
buf - > ReadByte ( ) ; // Unnused, eat it up
2007-07-10 00:10:19 +00:00
break ;
case 0x12 : // Production multipliers
case 0x13 :
2010-01-23 13:17:30 +00:00
indsp - > production_rate [ prop - 0x12 ] = buf - > ReadByte ( ) ;
2007-07-10 00:10:19 +00:00
break ;
case 0x14 : // Minimal amount of cargo distributed
2010-01-23 13:17:30 +00:00
indsp - > minimal_cargo = buf - > ReadByte ( ) ;
2007-07-10 00:10:19 +00:00
break ;
case 0x15 : { // Random sound effects
2010-01-23 13:17:30 +00:00
indsp - > number_of_sounds = buf - > ReadByte ( ) ;
2007-07-10 00:10:19 +00:00
uint8 * sounds = MallocT < uint8 > ( indsp - > number_of_sounds ) ;
2010-01-22 13:02:15 +00:00
try {
for ( uint8 j = 0 ; j < indsp - > number_of_sounds ; j + + ) {
2010-01-23 13:17:30 +00:00
sounds [ j ] = buf - > ReadByte ( ) ;
2010-01-22 13:02:15 +00:00
}
} catch ( . . . ) {
free ( sounds ) ;
throw ;
}
2011-03-03 20:26:19 +00:00
if ( HasBit ( indsp - > cleanup_flag , CLEAN_RANDOMSOUNDS ) ) {
free ( ( void * ) indsp - > random_sounds ) ;
}
2007-07-10 00:10:19 +00:00
indsp - > random_sounds = sounds ;
2011-03-03 20:26:19 +00:00
SetBit ( indsp - > cleanup_flag , CLEAN_RANDOMSOUNDS ) ;
2010-08-01 18:53:30 +00:00
break ;
}
2007-07-10 00:10:19 +00:00
case 0x16 : // Conflicting industry types
2010-01-23 13:17:30 +00:00
for ( byte j = 0 ; j < 3 ; j + + ) indsp - > conflicting [ j ] = buf - > ReadByte ( ) ;
2007-07-10 00:10:19 +00:00
break ;
case 0x17 : // Probability in random game
2010-01-23 13:17:30 +00:00
indsp - > appear_creation [ _settings_game . game_creation . landscape ] = buf - > ReadByte ( ) ;
2007-07-10 00:10:19 +00:00
break ;
case 0x18 : // Probability during gameplay
2010-01-23 13:17:30 +00:00
indsp - > appear_ingame [ _settings_game . game_creation . landscape ] = buf - > ReadByte ( ) ;
2007-07-10 00:10:19 +00:00
break ;
2009-02-09 02:57:15 +00:00
case 0x19 : // Map colour
2011-05-04 17:12:37 +00:00
indsp - > map_colour = buf - > ReadByte ( ) ;
2007-07-10 00:10:19 +00:00
break ;
case 0x1A : // Special industry flags to define special behavior
2010-01-23 13:17:30 +00:00
indsp - > behaviour = ( IndustryBehaviour ) buf - > ReadDWord ( ) ;
2007-07-10 00:10:19 +00:00
break ;
case 0x1B : // New industry text ID
2010-01-23 13:17:30 +00:00
indsp - > new_industry_text = buf - > ReadWord ( ) ;
2011-08-21 12:05:47 +00:00
_string_to_grf_mapping [ & indsp - > new_industry_text ] = _cur . grffile - > grfid ;
2007-07-10 00:10:19 +00:00
break ;
case 0x1C : // Input cargo multipliers for the three input cargo types
case 0x1D :
case 0x1E : {
2010-01-23 13:17:30 +00:00
uint32 multiples = buf - > ReadDWord ( ) ;
2008-12-19 23:13:51 +00: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 18:53:30 +00:00
break ;
}
2007-07-10 00:10:19 +00:00
case 0x1F : // Industry name
2010-01-23 13:17:30 +00:00
indsp - > name = buf - > ReadWord ( ) ;
2011-08-21 12:05:47 +00:00
_string_to_grf_mapping [ & indsp - > name ] = _cur . grffile - > grfid ;
2007-07-10 00:10:19 +00:00
break ;
case 0x20 : // Prospecting success chance
2010-01-23 13:17:30 +00:00
indsp - > prospecting_chance = buf - > ReadDWord ( ) ;
2007-07-10 00:10:19 +00:00
break ;
2009-09-14 12:22:57 +00:00
case 0x21 : // Callback mask
case 0x22 : { // Callback additional mask
2010-01-23 13:17:30 +00:00
byte aflag = buf - > ReadByte ( ) ;
2009-09-14 12:22:57 +00:00
SB ( indsp - > callback_mask , ( prop - 0x21 ) * 8 , 8 , aflag ) ;
2010-08-01 18:53:30 +00:00
break ;
}
2007-07-10 00:10:19 +00:00
2007-11-27 17:13:49 +00:00
case 0x23 : // removal cost multiplier
2010-01-23 13:17:30 +00:00
indsp - > removal_cost_multiplier = buf - > ReadDWord ( ) ;
2007-11-27 17:13:49 +00:00
break ;
2008-02-02 03:23:26 +00:00
case 0x24 : // name for nearby station
2010-01-23 13:17:30 +00:00
indsp - > station_name = buf - > ReadWord ( ) ;
2011-08-21 12:05:47 +00:00
if ( indsp - > station_name ! = STR_NULL ) _string_to_grf_mapping [ & indsp - > station_name ] = _cur . grffile - > grfid ;
2008-02-02 03:23:26 +00:00
break ;
2007-07-10 00:10:19 +00:00
default :
2008-08-31 16:41:27 +00:00
ret = CIR_UNKNOWN ;
2007-07-10 00:10:19 +00:00
break ;
}
}
return ret ;
}
2010-03-18 23:15:38 +00: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 11:17:52 +00:00
HangarTileTable * depot_table = MallocT < HangarTileTable > ( as - > nof_depots ) ;
2010-03-18 23:15:38 +00:00
MemCpyT ( depot_table , as - > depot_table , as - > nof_depots ) ;
as - > depot_table = depot_table ;
}
static ChangeInfoResult AirportChangeInfo ( uint airport , int numinfo , int prop , ByteReader * buf )
{
ChangeInfoResult ret = CIR_SUCCESS ;
if ( airport + numinfo > NUM_AIRPORTS ) {
grfmsg ( 1 , " AirportChangeInfo: Too many airports, trying id (%u), max (%u). Ignoring. " , airport + numinfo , NUM_AIRPORTS ) ;
return CIR_INVALID_ID ;
}
grfmsg ( 1 , " AirportChangeInfo: newid %u " , airport ) ;
/* Allocate industry specs if they haven't been allocated already. */
2011-08-21 12:05:47 +00:00
if ( _cur . grffile - > airportspec = = NULL ) {
_cur . grffile - > airportspec = CallocT < AirportSpec * > ( NUM_AIRPORTS ) ;
2010-03-18 23:15:38 +00:00
}
for ( int i = 0 ; i < numinfo ; i + + ) {
2011-08-21 12:05:47 +00:00
AirportSpec * as = _cur . grffile - > airportspec [ airport + i ] ;
2010-03-18 23:15:38 +00:00
if ( as = = NULL & & prop ! = 0x08 & & prop ! = 0x09 ) {
grfmsg ( 2 , " AirportChangeInfo: Attempt to modify undefined airport %u, ignoring " , airport + i ) ;
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. */
grfmsg ( 2 , " AirportChangeInfo: Attempt to use new airport %u as substitute airport for %u. Ignoring. " , subs_id , airport + i ) ;
continue ;
}
2011-08-21 12:05:47 +00:00
AirportSpec * * spec = & _cur . grffile - > airportspec [ airport + i ] ;
2010-03-18 23:15:38 +00:00
/* Allocate space for this airport.
* Only need to do it once . If ever it is called again , it should not
* do anything */
if ( * spec = = NULL ) {
* spec = MallocT < AirportSpec > ( 1 ) ;
as = * spec ;
memcpy ( as , AirportSpec : : GetWithoutOverride ( subs_id ) , sizeof ( * as ) ) ;
as - > enabled = true ;
as - > grf_prop . local_id = airport + i ;
as - > grf_prop . subst_id = subs_id ;
2011-08-21 12:05:47 +00:00
as - > grf_prop . grffile = _cur . grffile ;
2010-03-18 23:15:38 +00:00
/* override the default airport */
2011-08-21 12:05:47 +00:00
_airport_mngr . Add ( airport + i , _cur . grffile - > grfid , subs_id ) ;
2010-03-18 23:15:38 +00:00
/* Create a copy of the original tiletable so it can be freed later. */
DuplicateTileTable ( as ) ;
}
2010-08-01 18:53:30 +00:00
break ;
}
2010-03-18 23:15:38 +00:00
2010-08-05 12:10:22 +00:00
case 0x0A : { // Set airport layout
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 + + ) {
as - > rotation [ j ] = ( Direction ) buf - > ReadByte ( ) ;
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
x = - 80 , y = 0 . So , adjust it . */
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 12:05:47 +00:00
uint16 tempid = _airporttile_mngr . GetID ( local_tile_id , _cur . grffile - > grfid ) ;
2010-08-05 12:10:22 +00:00
if ( tempid = = INVALID_AIRPORTTILE ) {
grfmsg ( 2 , " AirportChangeInfo: Attempt to use airport tile %u with airport id %u, not yet defined. Ignoring. " , local_tile_id , airport + i ) ;
} else {
/* Declared as been valid, can be used */
att [ k ] . gfx = tempid ;
size = k + 1 ;
copy_from = att ;
}
} 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 ) {
as - > size_x = max < byte > ( as - > size_x , att [ k ] . ti . y + 1 ) ;
as - > size_y = max < byte > ( as - > size_y , att [ k ] . ti . x + 1 ) ;
} else {
as - > size_x = max < byte > ( as - > size_x , att [ k ] . ti . x + 1 ) ;
as - > size_y = max < byte > ( as - > size_y , att [ k ] . ti . y + 1 ) ;
}
}
tile_table [ j ] = CallocT < AirportTileTable > ( size ) ;
memcpy ( tile_table [ j ] , copy_from , sizeof ( * copy_from ) * size ) ;
}
/* 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-18 23:15:38 +00: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 :
as - > name = buf - > ReadWord ( ) ;
2011-08-21 12:05:47 +00:00
_string_to_grf_mapping [ & as - > name ] = _cur . grffile - > grfid ;
2010-03-18 23:15:38 +00:00
break ;
default :
ret = CIR_UNKNOWN ;
break ;
}
}
return ret ;
}
2010-08-28 18:21:09 +00: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 21:30:26 +00:00
case 0x17 :
2010-08-28 18:21:09 +00:00
buf - > ReadByte ( ) ;
case 0x09 :
case 0x0A :
case 0x10 :
case 0x11 :
2010-09-01 19:45:50 +00:00
case 0x13 :
2010-08-28 18:21:09 +00:00
case 0x15 :
buf - > ReadWord ( ) ;
break ;
case 0x08 :
case 0x0E :
case 0x0F :
buf - > ReadDWord ( ) ;
break ;
default :
ret = CIR_UNKNOWN ;
break ;
}
return ret ;
}
static ChangeInfoResult ObjectChangeInfo ( uint id , int numinfo , int prop , ByteReader * buf )
{
ChangeInfoResult ret = CIR_SUCCESS ;
if ( id + numinfo > NUM_OBJECTS ) {
grfmsg ( 1 , " ObjectChangeInfo: Too many objects loaded (%u), max (%u). Ignoring. " , id + numinfo , NUM_OBJECTS ) ;
return CIR_INVALID_ID ;
}
/* Allocate object specs if they haven't been allocated already. */
2011-08-21 12:05:47 +00:00
if ( _cur . grffile - > objectspec = = NULL ) {
_cur . grffile - > objectspec = CallocT < ObjectSpec * > ( NUM_OBJECTS ) ;
2010-08-28 18:21:09 +00:00
}
for ( int i = 0 ; i < numinfo ; i + + ) {
2011-08-21 12:05:47 +00:00
ObjectSpec * spec = _cur . grffile - > objectspec [ id + i ] ;
2010-08-28 18:21:09 +00:00
if ( prop ! = 0x08 & & spec = = NULL ) {
/* 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
2011-08-21 12:05:47 +00:00
ObjectSpec * * ospec = & _cur . grffile - > objectspec [ id + i ] ;
2010-08-28 18:21:09 +00:00
/* Allocate space for this object. */
2010-12-10 21:30:26 +00:00
if ( * ospec = = NULL ) {
* ospec = CallocT < ObjectSpec > ( 1 ) ;
( * ospec ) - > views = 1 ; // Default for NewGRFs that don't set it.
}
2010-08-28 18:21:09 +00:00
/* Swap classid because we read it in BE. */
uint32 classid = buf - > ReadDWord ( ) ;
( * ospec ) - > cls_id = ObjectClass : : Allocate ( BSWAP32 ( classid ) ) ;
( * ospec ) - > enabled = true ;
break ;
}
case 0x09 : { // Class name
StringID class_name = buf - > ReadWord ( ) ;
ObjectClass : : SetName ( spec - > cls_id , class_name ) ;
2011-08-21 12:05:47 +00:00
_string_to_grf_mapping [ & ObjectClass : : classes [ spec - > cls_id ] . name ] = _cur . grffile - > grfid ;
2010-08-28 18:21:09 +00:00
break ;
}
case 0x0A : // Object name
spec - > name = buf - > ReadWord ( ) ;
2011-08-21 12:05:47 +00:00
_string_to_grf_mapping [ & spec - > name ] = _cur . grffile - > grfid ;
2010-08-28 18:21:09 +00:00
break ;
case 0x0B : // Climate mask
spec - > climate = buf - > ReadByte ( ) ;
break ;
case 0x0C : // Size
spec - > size = buf - > ReadByte ( ) ;
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 21:30:26 +00:00
case 0x17 : // Views
spec - > views = buf - > ReadByte ( ) ;
if ( spec - > views ! = 1 & & spec - > views ! = 2 & & spec - > views ! = 4 ) {
grfmsg ( 2 , " ObjectChangeInfo: Invalid number of views (%u) for object id %u. Ignoring. " , spec - > views , id + i ) ;
spec - > views = 1 ;
}
break ;
2010-08-28 18:21:09 +00:00
default :
ret = CIR_UNKNOWN ;
break ;
}
}
return ret ;
}
2010-01-30 21:49:22 +00:00
static ChangeInfoResult RailTypeChangeInfo ( uint id , int numinfo , int prop , ByteReader * buf )
{
ChangeInfoResult ret = CIR_SUCCESS ;
extern RailtypeInfo _railtypes [ RAILTYPE_END ] ;
2010-02-03 08:02:07 +00:00
if ( id + numinfo > RAILTYPE_END ) {
grfmsg ( 1 , " RailTypeChangeInfo: Rail type %u is invalid, max %u, ignoring " , id + numinfo , RAILTYPE_END ) ;
return CIR_INVALID_ID ;
}
2010-01-30 21:49:22 +00:00
for ( int i = 0 ; i < numinfo ; i + + ) {
2011-08-21 12:05:47 +00:00
RailType rt = _cur . grffile - > railtype_map [ id + i ] ;
2010-01-30 21:49:22 +00: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 ;
case 0x09 : // Name of railtype
rti - > strings . toolbar_caption = buf - > ReadWord ( ) ;
2011-08-21 12:05:47 +00:00
_string_to_grf_mapping [ & rti - > strings . toolbar_caption ] = _cur . grffile - > grfid ;
2010-01-30 21:49:22 +00:00
break ;
case 0x0A : // Menu text of railtype
rti - > strings . menu_text = buf - > ReadWord ( ) ;
2011-08-21 12:05:47 +00:00
_string_to_grf_mapping [ & rti - > strings . menu_text ] = _cur . grffile - > grfid ;
2010-01-30 21:49:22 +00:00
break ;
case 0x0B : // Build window caption
rti - > strings . build_caption = buf - > ReadWord ( ) ;
2011-08-21 12:05:47 +00:00
_string_to_grf_mapping [ & rti - > strings . build_caption ] = _cur . grffile - > grfid ;
2010-01-30 21:49:22 +00:00
break ;
case 0x0C : // Autoreplace text
rti - > strings . replace_text = buf - > ReadWord ( ) ;
2011-08-21 12:05:47 +00:00
_string_to_grf_mapping [ & rti - > strings . replace_text ] = _cur . grffile - > grfid ;
2010-01-30 21:49:22 +00:00
break ;
case 0x0D : // New locomotive text
rti - > strings . new_loco = buf - > ReadWord ( ) ;
2011-08-21 12:05:47 +00:00
_string_to_grf_mapping [ & rti - > strings . new_loco ] = _cur . grffile - > grfid ;
2010-01-30 21:49:22 +00:00
break ;
case 0x0E : // Compatible railtype list
case 0x0F : // Powered railtype list
2011-01-18 21:30:59 +00:00
case 0x18 : // Railtype list required for date introduction
2011-01-18 21:28:07 +00:00
case 0x19 : // Introduced railtype list
2010-01-30 21:49:22 +00: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 ( ) ;
RailType rt = GetRailTypeByLabel ( BSWAP32 ( label ) ) ;
if ( rt ! = INVALID_RAILTYPE ) {
2011-01-18 21:28:07 +00:00
switch ( prop ) {
case 0x0E : SetBit ( rti - > compatible_railtypes , rt ) ; break ;
case 0x0F : SetBit ( rti - > powered_railtypes , rt ) ; break ;
2011-01-18 21:30:59 +00:00
case 0x18 : SetBit ( rti - > introduction_required_railtypes , rt ) ; break ;
2011-01-18 21:28:07 +00:00
case 0x19 : SetBit ( rti - > introduces_railtypes , rt ) ; break ;
2010-01-30 21:49:22 +00: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 15:58:59 +00:00
rti - > fallback_railtype = Clamp ( buf - > ReadByte ( ) , 0 , 2 ) ;
2010-01-30 21:49:22 +00:00
break ;
case 0x13 : // Construction cost factor
2010-03-03 18:58:32 +00:00
rti - > cost_multiplier = buf - > ReadWord ( ) ;
2010-01-30 21:49:22 +00:00
break ;
case 0x14 : // Speed limit
2010-01-31 13:17:29 +00:00
rti - > max_speed = buf - > ReadWord ( ) ;
2010-01-30 21:49:22 +00:00
break ;
case 0x15 : // Acceleration model
rti - > acceleration_type = Clamp ( buf - > ReadByte ( ) , 0 , 2 ) ;
break ;
2010-03-03 19:07:34 +00:00
case 0x16 : // Map colour
2011-05-04 17:12:37 +00:00
rti - > map_colour = buf - > ReadByte ( ) ;
2010-03-03 19:07:34 +00:00
break ;
2011-01-18 21:30:59 +00:00
case 0x17 : // Introduction date
rti - > introduction_date = buf - > ReadDWord ( ) ;
break ;
2011-01-20 12:22:38 +00:00
case 0x1A : // Sort order
rti - > sorting_order = buf - > ReadByte ( ) ;
break ;
2010-01-30 21:49:22 +00:00
default :
ret = CIR_UNKNOWN ;
break ;
}
}
return ret ;
}
static ChangeInfoResult RailTypeReserveInfo ( uint id , int numinfo , int prop , ByteReader * buf )
{
ChangeInfoResult ret = CIR_SUCCESS ;
2010-02-03 08:02:07 +00:00
if ( id + numinfo > RAILTYPE_END ) {
grfmsg ( 1 , " RailTypeReserveInfo: Rail type %u is invalid, max %u, ignoring " , id + numinfo , RAILTYPE_END ) ;
return CIR_INVALID_ID ;
}
2010-01-30 21:49:22 +00:00
for ( int i = 0 ; i < numinfo ; i + + ) {
switch ( prop ) {
case 0x08 : // Label of rail type
{
RailTypeLabel rtl = buf - > ReadDWord ( ) ;
rtl = BSWAP32 ( rtl ) ;
RailType rt = GetRailTypeByLabel ( rtl ) ;
if ( rt = = INVALID_RAILTYPE ) {
/* Set up new rail type */
rt = AllocateRailType ( rtl ) ;
}
2011-08-21 12:05:47 +00:00
_cur . grffile - > railtype_map [ id + i ] = rt ;
2010-01-30 21:49:22 +00:00
break ;
}
case 0x09 : // Name of railtype
case 0x0A : // Menu text
case 0x0B : // Build window caption
case 0x0C : // Autoreplace text
case 0x0D : // New loco
2010-03-03 18:58:32 +00:00
case 0x13 : // Construction cost
2010-01-30 21:49:22 +00:00
case 0x14 : // Speed limit
buf - > ReadWord ( ) ;
break ;
case 0x0E : // Compatible railtype list
case 0x0F : // Powered railtype list
2011-01-18 21:30:59 +00:00
case 0x18 : // Railtype list required for date introduction
2011-01-18 21:28:07 +00:00
case 0x19 : // Introduced railtype list
2010-01-30 21:49:22 +00: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 19:07:34 +00:00
case 0x16 : // Map colour
2011-01-20 12:22:38 +00:00
case 0x1A : // Sort order
2010-01-30 21:49:22 +00:00
buf - > ReadByte ( ) ;
break ;
2011-01-18 21:30:59 +00:00
case 0x17 : // Introduction date
buf - > ReadDWord ( ) ;
break ;
2010-01-30 21:49:22 +00:00
default :
ret = CIR_UNKNOWN ;
break ;
}
}
return ret ;
}
2010-02-22 14:16:19 +00:00
static ChangeInfoResult AirportTilesChangeInfo ( uint airtid , int numinfo , int prop , ByteReader * buf )
{
ChangeInfoResult ret = CIR_SUCCESS ;
if ( airtid + numinfo > NUM_AIRPORTTILES ) {
grfmsg ( 1 , " AirportTileChangeInfo: Too many airport tiles loaded (%u), max (%u). Ignoring. " , airtid + numinfo , NUM_AIRPORTTILES ) ;
return CIR_INVALID_ID ;
}
/* Allocate airport tile specs if they haven't been allocated already. */
2011-08-21 12:05:47 +00:00
if ( _cur . grffile - > airtspec = = NULL ) {
_cur . grffile - > airtspec = CallocT < AirportTileSpec * > ( NUM_AIRPORTTILES ) ;
2010-02-22 14:16:19 +00:00
}
for ( int i = 0 ; i < numinfo ; i + + ) {
2011-08-21 12:05:47 +00:00
AirportTileSpec * tsp = _cur . grffile - > airtspec [ airtid + i ] ;
2010-02-22 14:16:19 +00:00
if ( prop ! = 0x08 & & tsp = = NULL ) {
grfmsg ( 2 , " AirportTileChangeInfo: Attempt to modify undefined airport tile %u. Ignoring. " , airtid + i ) ;
return CIR_INVALID_ID ;
}
switch ( prop ) {
case 0x08 : { // Substitute airport tile type
2011-08-21 12:05:47 +00:00
AirportTileSpec * * tilespec = & _cur . grffile - > airtspec [ airtid + i ] ;
2010-02-22 14:16:19 +00:00
byte subs_id = buf - > ReadByte ( ) ;
if ( subs_id > = NEW_AIRPORTTILE_OFFSET ) {
/* The substitute id must be one of the original airport tiles. */
grfmsg ( 2 , " AirportTileChangeInfo: Attempt to use new airport tile %u as substitute airport tile for %u. Ignoring. " , subs_id , airtid + i ) ;
continue ;
}
/* Allocate space for this airport tile. */
if ( * tilespec = = NULL ) {
* tilespec = CallocT < AirportTileSpec > ( 1 ) ;
tsp = * tilespec ;
memcpy ( tsp , AirportTileSpec : : Get ( subs_id ) , sizeof ( AirportTileSpec ) ) ;
tsp - > enabled = true ;
2010-08-26 15:31:40 +00:00
tsp - > animation . status = ANIM_STATUS_NO_ANIMATION ;
2010-02-22 14:16:19 +00:00
tsp - > grf_prop . local_id = airtid + i ;
tsp - > grf_prop . subst_id = subs_id ;
2011-08-21 12:05:47 +00: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 14:16:19 +00:00
}
2010-08-01 18:53:30 +00:00
break ;
}
2010-02-22 14:16:19 +00: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 ) {
grfmsg ( 2 , " AirportTileChangeInfo: Attempt to override new airport tile %u with airport tile id %u. Ignoring. " , override , airtid + i ) ;
continue ;
}
2011-08-21 12:05:47 +00:00
_airporttile_mngr . Add ( airtid + i , _cur . grffile - > grfid , override ) ;
2010-08-01 18:53:30 +00:00
break ;
}
2010-02-22 14:16:19 +00:00
2010-08-26 11:18:32 +00:00
case 0x0E : // Callback mask
tsp - > callback_mask = buf - > ReadByte ( ) ;
2010-02-22 14:16:19 +00:00
break ;
case 0x0F : // Animation information
2010-08-26 15:31:40 +00:00
tsp - > animation . frames = buf - > ReadByte ( ) ;
tsp - > animation . status = buf - > ReadByte ( ) ;
2010-02-22 14:16:19 +00:00
break ;
case 0x10 : // Animation speed
2010-08-26 15:31:40 +00:00
tsp - > animation . speed = buf - > ReadByte ( ) ;
2010-02-22 14:16:19 +00:00
break ;
case 0x11 : // Animation triggers
2010-08-26 15:31:40 +00:00
tsp - > animation . triggers = buf - > ReadByte ( ) ;
2010-02-22 14:16:19 +00:00
break ;
default :
ret = CIR_UNKNOWN ;
break ;
}
}
return ret ;
}
2009-01-09 20:14:07 +00:00
static bool HandleChangeInfoResult ( const char * caller , ChangeInfoResult cir , uint8 feature , uint8 property )
{
switch ( cir ) {
default : NOT_REACHED ( ) ;
2011-05-29 16:56:22 +00:00
case CIR_DISABLED :
/* Error has already been printed; just stop parsing */
return true ;
2009-01-09 20:14:07 +00:00
case CIR_SUCCESS :
return false ;
case CIR_UNHANDLED :
grfmsg ( 1 , " %s: Ignoring property 0x%02X of feature 0x%02X (not implemented) " , caller , property , feature ) ;
return false ;
case CIR_UNKNOWN :
grfmsg ( 0 , " %s: Unknown property 0x%02X of feature 0x%02X, disabling " , caller , property , feature ) ;
2010-07-29 14:26:28 +00:00
/* FALL THROUGH */
2009-01-09 20:14:07 +00:00
case CIR_INVALID_ID :
/* No debug message for an invalid ID, as it has already been output */
2011-05-11 20:22:04 +00:00
DisableGrf ( cir = = CIR_INVALID_ID ? STR_NEWGRF_ERROR_INVALID_ID : STR_NEWGRF_ERROR_UNKNOWN_PROPERTY ) ;
2009-01-09 20:14:07 +00:00
return true ;
}
}
2004-11-12 15:15:56 +00:00
/* Action 0x00 */
2010-01-22 13:02:15 +00:00
static void FeatureChangeInfo ( ByteReader * buf )
2004-08-09 17:04:08 +00:00
{
/* <00> <feature> <num-props> <num-info> <id> (<property <new-info>)...
*
2009-11-24 13:15:58 +00:00
* B feature
2004-08-09 17:04:08 +00:00
* B num - props how many properties to change per vehicle / station
* B num - info how many vehicles / stations to change
2007-11-16 02:25:56 +00:00
* E id ID of first vehicle / station to change , if num - info is
2004-08-09 17:04:08 +00: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 09:38:09 +00:00
static const VCI_Handler handler [ ] = {
2010-04-24 11:52:58 +00: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 ,
/* GSF_CARGOS */ NULL , // Cargo is handled during reservation
/* GSF_SOUNDFX */ SoundEffectChangeInfo ,
/* GSF_AIRPORTS */ AirportChangeInfo ,
/* GSF_SIGNALS */ NULL ,
2010-08-28 18:21:09 +00:00
/* GSF_OBJECTS */ ObjectChangeInfo ,
2010-04-24 11:52:58 +00:00
/* GSF_RAILTYPES */ RailTypeChangeInfo ,
/* GSF_AIRPORTTILES */ AirportTilesChangeInfo ,
2004-08-09 17:04:08 +00:00
} ;
2004-11-12 18:43:12 +00:00
2010-01-23 13:17:30 +00:00
uint8 feature = buf - > ReadByte ( ) ;
uint8 numprops = buf - > ReadByte ( ) ;
uint numinfo = buf - > ReadByte ( ) ;
2010-03-10 07:31:08 +00:00
uint engine = buf - > ReadExtendedByte ( ) ;
2004-11-12 15:36:49 +00:00
2006-12-26 17:36:18 +00:00
grfmsg ( 6 , " FeatureChangeInfo: feature %d, %d properties, to apply to %d+%d " ,
2004-11-13 18:01:33 +00:00
feature , numprops , engine , numinfo ) ;
2005-09-20 17:33:57 +00:00
if ( feature > = lengthof ( handler ) | | handler [ feature ] = = NULL ) {
2009-05-23 22:24:09 +00:00
if ( feature ! = GSF_CARGOS ) grfmsg ( 1 , " FeatureChangeInfo: Unsupported feature %d, skipping " , feature ) ;
2004-11-14 10:03:21 +00:00
return ;
}
2009-11-24 13:15:58 +00:00
/* Mark the feature as used by the grf */
2011-08-21 12:05:47 +00:00
SetBit ( _cur . grffile - > grf_features , feature ) ;
2009-11-24 13:15:58 +00:00
2010-01-22 13:02:15 +00:00
while ( numprops - - & & buf - > HasData ( ) ) {
2010-01-23 13:17:30 +00:00
uint8 prop = buf - > ReadByte ( ) ;
2004-08-09 17:04:08 +00:00
2010-01-22 13:02:15 +00:00
ChangeInfoResult cir = handler [ feature ] ( engine , numinfo , prop , buf ) ;
2009-01-09 20:14:07 +00:00
if ( HandleChangeInfoResult ( " FeatureChangeInfo " , cir , feature , prop ) ) return ;
2004-08-09 17:04:08 +00:00
}
}
2007-01-04 19:24:42 +00:00
/* Action 0x00 (GLS_SAFETYSCAN) */
2010-01-22 13:02:15 +00:00
static void SafeChangeInfo ( ByteReader * buf )
2007-01-04 19:24:42 +00:00
{
2010-01-23 13:17:30 +00:00
uint8 feature = buf - > ReadByte ( ) ;
uint8 numprops = buf - > ReadByte ( ) ;
uint numinfo = buf - > ReadByte ( ) ;
2010-03-10 07:31:08 +00:00
buf - > ReadExtendedByte ( ) ; // id
2007-01-04 19:24:42 +00:00
2010-04-24 11:52:58 +00:00
if ( feature = = GSF_BRIDGES & & numprops = = 1 ) {
2010-01-23 13:17:30 +00:00
uint8 prop = buf - > ReadByte ( ) ;
2007-01-04 19:24:42 +00:00
/* Bridge property 0x0D is redefinition of sprite layout tables, which
* is considered safe . */
if ( prop = = 0x0D ) return ;
2009-03-14 19:38:19 +00:00
} else if ( feature = = GSF_GLOBALVAR & & numprops = = 1 ) {
2010-01-23 13:17:30 +00:00
uint8 prop = buf - > ReadByte ( ) ;
2009-03-14 19:38:19 +00: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 13:17:30 +00:00
uint32 s = buf - > ReadDWord ( ) ;
buf - > ReadDWord ( ) ; // dest
2009-03-14 19:38:19 +00:00
const GRFConfig * grfconfig = GetGRFConfig ( s ) ;
if ( grfconfig ! = NULL & & ! HasBit ( grfconfig - > flags , GCF_STATIC ) ) {
is_safe = false ;
break ;
}
}
if ( is_safe ) return ;
}
2007-01-04 19:24:42 +00:00
}
2011-08-21 12:05:47 +00:00
SetBit ( _cur . grfconfig - > flags , GCF_UNSAFE ) ;
2007-01-04 19:24:42 +00:00
/* Skip remainder of GRF */
2011-08-21 12:05:47 +00:00
_cur . skip_sprites = - 1 ;
2007-01-04 19:24:42 +00:00
}
2007-03-22 23:19:40 +00:00
/* Action 0x00 (GLS_RESERVE) */
2010-01-22 13:02:15 +00:00
static void ReserveChangeInfo ( ByteReader * buf )
2007-03-22 23:19:40 +00:00
{
2010-01-23 13:17:30 +00:00
uint8 feature = buf - > ReadByte ( ) ;
2007-03-22 23:19:40 +00:00
2010-01-30 21:49:22 +00:00
if ( feature ! = GSF_CARGOS & & feature ! = GSF_GLOBALVAR & & feature ! = GSF_RAILTYPES ) return ;
2007-03-22 23:19:40 +00:00
2010-01-23 13:17:30 +00:00
uint8 numprops = buf - > ReadByte ( ) ;
uint8 numinfo = buf - > ReadByte ( ) ;
2010-03-10 07:31:08 +00:00
uint8 index = buf - > ReadExtendedByte ( ) ;
2007-03-22 23:19:40 +00:00
2010-01-22 13:02:15 +00:00
while ( numprops - - & & buf - > HasData ( ) ) {
2010-01-23 13:17:30 +00:00
uint8 prop = buf - > ReadByte ( ) ;
2009-01-09 20:14:07 +00:00
ChangeInfoResult cir = CIR_SUCCESS ;
2007-03-22 23:19:40 +00:00
2007-11-22 22:37:06 +00:00
switch ( feature ) {
default : NOT_REACHED ( ) ;
case GSF_CARGOS :
2010-01-22 13:02:15 +00:00
cir = CargoChangeInfo ( index , numinfo , prop , buf ) ;
2007-11-22 22:37:06 +00:00
break ;
2008-02-03 20:23:59 +00:00
2009-01-09 20:14:07 +00:00
case GSF_GLOBALVAR :
2010-01-22 13:02:15 +00:00
cir = GlobalVarReserveInfo ( index , numinfo , prop , buf ) ;
2007-11-22 22:37:06 +00:00
break ;
2010-01-30 21:49:22 +00:00
case GSF_RAILTYPES :
cir = RailTypeReserveInfo ( index , numinfo , prop , buf ) ;
break ;
2007-03-22 23:19:40 +00:00
}
2007-11-22 22:37:06 +00:00
2009-01-09 20:14:07 +00:00
if ( HandleChangeInfoResult ( " ReserveChangeInfo " , cir , feature , prop ) ) return ;
2007-03-22 23:19:40 +00:00
}
}
2004-11-12 15:15:56 +00:00
/* Action 0x01 */
2010-01-22 13:02:15 +00:00
static void NewSpriteSet ( ByteReader * buf )
2004-08-09 17:04:08 +00:00
{
/* <01> <feature> <num-sets> <num-ent>
*
* B feature feature to define sprites for
* 0 , 1 , 2 , 3 : veh - type , 4 : train stations
* B num - sets number of sprite sets
2005-08-15 22:48:33 +00:00
* E num - ent how many entries per sprite set
2004-08-09 17:04:08 +00: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 09:55:11 +00:00
*/
2004-08-09 17:04:08 +00:00
2010-01-23 13:17:30 +00:00
uint8 feature = buf - > ReadByte ( ) ;
uint8 num_sets = buf - > ReadByte ( ) ;
2010-03-10 07:31:08 +00:00
uint16 num_ents = buf - > ReadExtendedByte ( ) ;
2004-08-09 17:04:08 +00:00
2011-08-21 12:05:47 +00:00
_cur . grffile - > spriteset_start = _cur . spriteid ;
_cur . grffile - > spriteset_feature = feature ;
_cur . grffile - > spriteset_numsets = num_sets ;
_cur . grffile - > spriteset_numents = num_ents ;
2005-08-15 11:39:13 +00:00
2006-12-26 17:36:18 +00:00
grfmsg ( 7 , " New sprite set at %d of type %d, consisting of %d sets with %d views each (total %d) " ,
2011-08-21 12:05:47 +00:00
_cur . spriteid , feature , num_sets , num_ents , num_sets * num_ents
2005-08-15 11:39:13 +00:00
) ;
2008-01-14 11:18:43 +00:00
for ( int i = 0 ; i < num_sets * num_ents ; i + + ) {
2011-08-21 12:05:47 +00:00
_cur . nfo_line + + ;
LoadNextSprite ( _cur . spriteid + + , _cur . file_index , _cur . nfo_line ) ;
2005-08-15 11:39:13 +00:00
}
2004-08-09 17:04:08 +00:00
}
2007-05-05 10:47:35 +00:00
/* Action 0x01 (SKIP) */
2010-01-22 13:02:15 +00:00
static void SkipAct1 ( ByteReader * buf )
2007-05-05 10:47:35 +00:00
{
2010-01-23 13:17:30 +00:00
buf - > ReadByte ( ) ;
uint8 num_sets = buf - > ReadByte ( ) ;
2010-03-10 07:31:08 +00:00
uint16 num_ents = buf - > ReadExtendedByte ( ) ;
2007-05-05 10:47:35 +00:00
2011-08-21 12:05:47 +00:00
_cur . skip_sprites = num_sets * num_ents ;
2007-05-05 10:47:35 +00:00
2011-08-21 12:05:47 +00:00
grfmsg ( 3 , " SkipAct1: Skipping %d sprites " , _cur . skip_sprites ) ;
2007-05-05 10:47:35 +00:00
}
2006-04-26 19:42:05 +00:00
/* Helper function to either create a callback or link to a previously
* defined spritegroup . */
2009-01-10 00:31:47 +00:00
static const SpriteGroup * GetGroupFromGroupID ( byte setid , byte type , uint16 groupid )
2006-04-26 19:42:05 +00:00
{
2011-02-09 18:46:55 +00:00
if ( HasBit ( groupid , 15 ) ) {
assert ( CallbackResultSpriteGroup : : CanAllocateItem ( ) ) ;
return new CallbackResultSpriteGroup ( groupid ) ;
}
2006-04-26 19:42:05 +00:00
2011-08-21 12:05:47 +00:00
if ( groupid > = _cur . grffile - > spritegroups_count | | _cur . grffile - > spritegroups [ groupid ] = = NULL ) {
2007-04-16 20:56:19 +00:00
grfmsg ( 1 , " GetGroupFromGroupID(0x%02X:0x%02X): Groupid 0x%04X does not exist, leaving empty " , setid , type , groupid ) ;
2006-04-26 19:42:05 +00:00
return NULL ;
}
2011-08-21 12:05:47 +00:00
return _cur . grffile - > spritegroups [ groupid ] ;
2006-04-26 19:42:05 +00:00
}
/* Helper function to either create a callback or a result sprite group. */
2009-01-10 00:31:47 +00:00
static const SpriteGroup * CreateGroupFromGroupID ( byte feature , byte setid , byte type , uint16 spriteid , uint16 num_sprites )
2006-04-26 19:42:05 +00:00
{
2011-02-09 18:46:55 +00:00
if ( HasBit ( spriteid , 15 ) ) {
assert ( CallbackResultSpriteGroup : : CanAllocateItem ( ) ) ;
return new CallbackResultSpriteGroup ( spriteid ) ;
}
2006-04-26 19:42:05 +00:00
2011-08-21 12:05:47 +00:00
if ( spriteid > = _cur . grffile - > spriteset_numsets ) {
grfmsg ( 1 , " CreateGroupFromGroupID(0x%02X:0x%02X): Sprite set %u invalid, max %u " , setid , type , spriteid , _cur . grffile - > spriteset_numsets ) ;
2006-04-26 20:18:29 +00:00
return NULL ;
}
2006-04-26 19:42:05 +00:00
/* Check if the sprite is within range. This can fail if the Action 0x01
* is skipped , as TTDPatch mandates that Action 0x02 s must be processed .
* We don ' t have that rule , but must live by the Patch . . . */
2011-08-21 12:05:47 +00:00
if ( _cur . grffile - > spriteset_start + spriteid * num_sprites + num_sprites > _cur . spriteid ) {
2007-04-16 20:56:19 +00:00
grfmsg ( 1 , " CreateGroupFromGroupID(0x%02X:0x%02X): Real Sprite IDs 0x%04X - 0x%04X do not (all) exist (max 0x%04X), leaving empty " ,
2006-04-26 19:42:05 +00:00
setid , type ,
2011-08-21 12:05:47 +00:00
_cur . grffile - > spriteset_start + spriteid * num_sprites ,
_cur . grffile - > spriteset_start + spriteid * num_sprites + num_sprites - 1 , _cur . spriteid - 1 ) ;
2006-04-26 19:42:05 +00:00
return NULL ;
}
2011-08-21 12:05:47 +00:00
if ( feature ! = _cur . grffile - > spriteset_feature ) {
2007-04-16 20:56:19 +00:00
grfmsg ( 1 , " CreateGroupFromGroupID(0x%02X:0x%02X): Sprite set feature 0x%02X does not match action feature 0x%02X, skipping " ,
2007-06-13 06:42:43 +00:00
setid , type ,
2011-08-21 12:05:47 +00:00
_cur . grffile - > spriteset_feature , feature ) ;
2006-05-10 06:23:43 +00:00
return NULL ;
}
2011-02-09 18:46:55 +00:00
assert ( ResultSpriteGroup : : CanAllocateItem ( ) ) ;
2011-08-21 12:05:47 +00:00
return new ResultSpriteGroup ( _cur . grffile - > spriteset_start + spriteid * num_sprites , num_sprites ) ;
2006-04-26 19:42:05 +00:00
}
2004-11-12 15:15:56 +00:00
/* Action 0x02 */
2010-01-22 13:02:15 +00:00
static void NewSpriteGroup ( ByteReader * buf )
2004-08-09 17:04:08 +00: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 ) */
2009-05-23 12:13:42 +00:00
SpriteGroup * act_group = NULL ;
2006-04-23 09:46:23 +00:00
2010-01-23 13:17:30 +00:00
uint8 feature = buf - > ReadByte ( ) ;
uint8 setid = buf - > ReadByte ( ) ;
uint8 type = buf - > ReadByte ( ) ;
2004-08-09 17:04:08 +00:00
2011-08-21 12:05:47 +00:00
if ( setid > = _cur . grffile - > spritegroups_count ) {
2007-03-21 03:06:21 +00:00
/* Allocate memory for new sprite group references. */
2011-08-21 12:05:47 +00:00
_cur . grffile - > spritegroups = ReallocT ( _cur . grffile - > spritegroups , setid + 1 ) ;
2007-03-21 03:06:21 +00:00
/* Initialise new space to NULL */
2011-08-21 12:05:47 +00:00
for ( ; _cur . grffile - > spritegroups_count < ( setid + 1 ) ; _cur . grffile - > spritegroups_count + + ) {
_cur . grffile - > spritegroups [ _cur . grffile - > spritegroups_count ] = NULL ;
2009-11-23 13:14:45 +00:00
}
2005-09-22 20:35:20 +00:00
}
2010-01-22 13:02:15 +00: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 09:46:23 +00:00
switch ( type ) {
/* Deterministic Sprite Group */
case 0x81 : // Self scope, byte
case 0x82 : // Parent scope, byte
2006-04-26 20:44:28 +00:00
case 0x85 : // Self scope, word
case 0x86 : // Parent scope, word
case 0x89 : // Self scope, dword
case 0x8A : // Parent scope, dword
2006-04-23 09:46:23 +00:00
{
2006-04-26 20:44:28 +00:00
byte varadjust ;
byte varsize ;
2006-04-23 09:46:23 +00:00
2011-02-09 18:46:55 +00:00
assert ( DeterministicSpriteGroup : : CanAllocateItem ( ) ) ;
2009-05-23 12:13:42 +00:00
DeterministicSpriteGroup * group = new DeterministicSpriteGroup ( ) ;
act_group = group ;
group - > var_scope = HasBit ( type , 1 ) ? VSG_SCOPE_PARENT : VSG_SCOPE_SELF ;
2006-04-26 20:44:28 +00:00
switch ( GB ( type , 2 , 2 ) ) {
2006-04-27 06:33:21 +00:00
default : NOT_REACHED ( ) ;
2009-05-23 12:13:42 +00: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 09:46:23 +00:00
}
2004-11-14 18:18:28 +00:00
2011-08-06 13:55:52 +00:00
static SmallVector < DeterministicSpriteGroupAdjust , 16 > adjusts ;
adjusts . Clear ( ) ;
2006-04-26 20:44:28 +00: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 {
2011-08-06 13:55:52 +00:00
DeterministicSpriteGroupAdjust * adjust = adjusts . Append ( ) ;
2006-04-26 20:44:28 +00:00
/* The first var adjust doesn't have an operation specified, so we set it to add. */
2011-08-06 13:55:52 +00:00
adjust - > operation = adjusts . Length ( ) = = 1 ? DSGA_OP_ADD : ( DeterministicSpriteGroupAdjustOperation ) buf - > ReadByte ( ) ;
2010-01-23 13:17:30 +00:00
adjust - > variable = buf - > ReadByte ( ) ;
2007-01-12 11:20:34 +00:00
if ( adjust - > variable = = 0x7E ) {
/* Link subroutine group */
2010-01-23 13:17:30 +00:00
adjust - > subroutine = GetGroupFromGroupID ( setid , type , buf - > ReadByte ( ) ) ;
2007-01-12 11:20:34 +00:00
} else {
2010-01-23 13:17:30 +00:00
adjust - > parameter = IsInsideMM ( adjust - > variable , 0x60 , 0x80 ) ? buf - > ReadByte ( ) : 0 ;
2007-01-12 11:20:34 +00:00
}
2006-04-26 20:44:28 +00:00
2010-01-23 13:17:30 +00:00
varadjust = buf - > ReadByte ( ) ;
2006-04-26 20:44:28 +00:00
adjust - > shift_num = GB ( varadjust , 0 , 5 ) ;
2007-01-10 18:56:51 +00:00
adjust - > type = ( DeterministicSpriteGroupAdjustType ) GB ( varadjust , 6 , 2 ) ;
2010-01-23 13:17:30 +00:00
adjust - > and_mask = buf - > ReadVarSize ( varsize ) ;
2006-04-26 20:44:28 +00:00
if ( adjust - > type ! = DSGA_TYPE_NONE ) {
2010-01-23 13:17:30 +00:00
adjust - > add_val = buf - > ReadVarSize ( varsize ) ;
adjust - > divmod_val = buf - > ReadVarSize ( varsize ) ;
2006-04-26 20:44:28 +00:00
} else {
adjust - > add_val = 0 ;
adjust - > divmod_val = 0 ;
}
/* Continue reading var adjusts while bit 5 is set. */
2007-11-19 21:02:30 +00:00
} while ( HasBit ( varadjust , 5 ) ) ;
2006-04-26 20:44:28 +00:00
2011-08-06 13:55:52 +00:00
group - > num_adjusts = adjusts . Length ( ) ;
group - > adjusts = MallocT < DeterministicSpriteGroupAdjust > ( group - > num_adjusts ) ;
MemCpyT ( group - > adjusts , adjusts . Begin ( ) , group - > num_adjusts ) ;
2010-01-23 13:17:30 +00:00
group - > num_ranges = buf - > ReadByte ( ) ;
2009-05-23 12:13:42 +00:00
if ( group - > num_ranges > 0 ) group - > ranges = CallocT < DeterministicSpriteGroupRange > ( group - > num_ranges ) ;
2006-04-23 09:46:23 +00:00
2009-05-23 12:13:42 +00:00
for ( uint i = 0 ; i < group - > num_ranges ; i + + ) {
2010-01-23 13:17:30 +00:00
group - > ranges [ i ] . group = GetGroupFromGroupID ( setid , type , buf - > ReadWord ( ) ) ;
group - > ranges [ i ] . low = buf - > ReadVarSize ( varsize ) ;
group - > ranges [ i ] . high = buf - > ReadVarSize ( varsize ) ;
2006-04-23 09:46:23 +00:00
}
2004-11-14 23:14:44 +00:00
2010-01-23 13:17:30 +00:00
group - > default_group = GetGroupFromGroupID ( setid , type , buf - > ReadWord ( ) ) ;
2006-04-23 09:46:23 +00:00
break ;
2004-11-14 18:18:28 +00:00
}
2006-04-23 09:46:23 +00:00
/* Randomized Sprite Group */
case 0x80 : // Self scope
case 0x83 : // Parent scope
2008-03-27 21:36:16 +00:00
case 0x84 : // Relative scope
2006-04-23 09:46:23 +00:00
{
2011-02-09 18:46:55 +00:00
assert ( RandomizedSpriteGroup : : CanAllocateItem ( ) ) ;
2009-05-23 12:13:42 +00:00
RandomizedSpriteGroup * group = new RandomizedSpriteGroup ( ) ;
act_group = group ;
group - > var_scope = HasBit ( type , 1 ) ? VSG_SCOPE_PARENT : VSG_SCOPE_SELF ;
2004-11-17 08:52:47 +00:00
2008-03-28 02:10:25 +00:00
if ( HasBit ( type , 2 ) ) {
2009-05-23 12:13:42 +00:00
if ( feature < = GSF_AIRCRAFT ) group - > var_scope = VSG_SCOPE_RELATIVE ;
2010-01-23 13:17:30 +00:00
group - > count = buf - > ReadByte ( ) ;
2008-03-27 21:36:16 +00:00
}
2010-01-23 13:17:30 +00:00
uint8 triggers = buf - > ReadByte ( ) ;
2009-05-23 12:13:42 +00:00
group - > triggers = GB ( triggers , 0 , 7 ) ;
group - > cmp_mode = HasBit ( triggers , 7 ) ? RSG_CMP_ALL : RSG_CMP_ANY ;
2010-01-23 13:17:30 +00:00
group - > lowest_randbit = buf - > ReadByte ( ) ;
group - > num_groups = buf - > ReadByte ( ) ;
2009-05-23 12:13:42 +00:00
group - > groups = CallocT < const SpriteGroup * > ( group - > num_groups ) ;
2004-11-17 08:52:47 +00:00
2009-05-23 12:13:42 +00:00
for ( uint i = 0 ; i < group - > num_groups ; i + + ) {
2010-01-23 13:17:30 +00:00
group - > groups [ i ] = GetGroupFromGroupID ( setid , type , buf - > ReadWord ( ) ) ;
2006-04-23 09:46:23 +00:00
}
2004-11-17 08:52:47 +00:00
2006-04-23 09:46:23 +00:00
break ;
}
2004-11-17 08:52:47 +00:00
2006-04-26 20:44:28 +00:00
/* Neither a variable or randomized sprite group... must be a real group */
2006-04-23 09:46:23 +00:00
default :
{
2006-04-26 20:44:28 +00:00
switch ( feature ) {
2010-04-24 11:52:58 +00:00
case GSF_TRAINS :
case GSF_ROADVEHICLES :
case GSF_SHIPS :
2006-04-26 20:44:28 +00:00
case GSF_AIRCRAFT :
2010-04-24 11:52:58 +00:00
case GSF_STATIONS :
case GSF_CANALS :
2007-03-23 20:55:45 +00:00
case GSF_CARGOS :
2010-03-18 23:15:38 +00:00
case GSF_AIRPORTS :
2010-02-07 22:22:54 +00:00
case GSF_RAILTYPES :
2006-04-26 20:44:28 +00:00
{
2011-08-21 12:05:47 +00:00
byte sprites = _cur . grffile - > spriteset_numents ;
2006-04-26 20:44:28 +00:00
byte num_loaded = type ;
2010-01-23 13:17:30 +00:00
byte num_loading = buf - > ReadByte ( ) ;
2006-04-26 20:44:28 +00:00
2011-08-21 12:05:47 +00:00
if ( _cur . grffile - > spriteset_start = = 0 ) {
2006-12-26 17:36:18 +00:00
grfmsg ( 0 , " NewSpriteGroup: No sprite set to work on! Skipping " ) ;
2006-04-26 20:44:28 +00:00
return ;
}
2006-04-23 09:46:23 +00:00
2011-02-09 18:46:55 +00:00
assert ( RealSpriteGroup : : CanAllocateItem ( ) ) ;
2009-05-23 12:13:42 +00:00
RealSpriteGroup * group = new RealSpriteGroup ( ) ;
act_group = group ;
2004-08-09 17:04:08 +00:00
2009-05-23 12:13:42 +00:00
group - > num_loaded = num_loaded ;
group - > num_loading = num_loading ;
if ( num_loaded > 0 ) group - > loaded = CallocT < const SpriteGroup * > ( num_loaded ) ;
if ( num_loading > 0 ) group - > loading = CallocT < const SpriteGroup * > ( num_loading ) ;
2004-11-12 15:36:49 +00:00
2006-12-26 17:36:18 +00:00
grfmsg ( 6 , " NewSpriteGroup: New SpriteGroup 0x%02X, %u views, %u loaded, %u loading " ,
2006-04-26 20:44:28 +00:00
setid , sprites , num_loaded , num_loading ) ;
2006-04-23 09:46:23 +00:00
2007-03-10 09:55:11 +00:00
for ( uint i = 0 ; i < num_loaded ; i + + ) {
2010-01-23 13:17:30 +00:00
uint16 spriteid = buf - > ReadWord ( ) ;
2009-05-23 12:13:42 +00:00
group - > loaded [ i ] = CreateGroupFromGroupID ( feature , setid , type , spriteid , sprites ) ;
2006-12-26 17:36:18 +00:00
grfmsg ( 8 , " NewSpriteGroup: + rg->loaded[%i] = subset %u " , i , spriteid ) ;
2006-04-26 20:44:28 +00:00
}
2006-04-23 09:46:23 +00:00
2007-03-10 09:55:11 +00:00
for ( uint i = 0 ; i < num_loading ; i + + ) {
2010-01-23 13:17:30 +00:00
uint16 spriteid = buf - > ReadWord ( ) ;
2009-05-23 12:13:42 +00:00
group - > loading [ i ] = CreateGroupFromGroupID ( feature , setid , type , spriteid , sprites ) ;
2006-12-26 17:36:18 +00:00
grfmsg ( 8 , " NewSpriteGroup: + rg->loading[%i] = subset %u " , i , spriteid ) ;
2006-04-26 20:44:28 +00:00
}
2006-04-23 09:46:23 +00:00
2006-04-26 20:44:28 +00:00
break ;
}
2004-08-09 17:04:08 +00:00
2010-04-24 11:52:58 +00:00
case GSF_HOUSES :
2010-02-22 14:16:19 +00:00
case GSF_AIRPORTTILES :
2010-08-28 18:22:24 +00:00
case GSF_OBJECTS :
2007-07-05 16:39:56 +00:00
case GSF_INDUSTRYTILES : {
2011-08-21 12:05:47 +00:00
byte num_spriteset_ents = _cur . grffile - > spriteset_numents ;
byte num_spritesets = _cur . grffile - > spriteset_numsets ;
2009-12-16 11:35:05 +00:00
byte num_building_sprites = max ( ( uint8 ) 1 , type ) ;
2007-03-19 11:27:30 +00:00
2011-02-09 18:46:55 +00:00
assert ( TileLayoutSpriteGroup : : CanAllocateItem ( ) ) ;
2009-05-23 12:13:42 +00:00
TileLayoutSpriteGroup * group = new TileLayoutSpriteGroup ( ) ;
act_group = group ;
2010-01-08 20:42:12 +00:00
/* num_building_stages should be 1, if we are only using non-custom sprites */
group - > num_building_stages = max ( ( uint8 ) 1 , num_spriteset_ents ) ;
2007-03-19 11:27:30 +00:00
2011-05-29 16:56:22 +00:00
/* On error, bail out immediately. Temporary GRF data was already freed */
2011-08-21 12:05:47 +00:00
if ( ReadSpriteLayout ( buf , num_building_sprites , _cur . grffile - > spriteset_start , num_spriteset_ents , num_spritesets , false , type = = 0 , & group - > dts ) ) return ;
2007-03-19 11:27:30 +00:00
break ;
}
2007-07-05 05:41:56 +00:00
case GSF_INDUSTRIES : {
if ( type > 1 ) {
grfmsg ( 1 , " NewSpriteGroup: Unsupported industry production version %d, skipping " , type ) ;
break ;
}
2011-02-09 18:46:55 +00:00
assert ( IndustryProductionSpriteGroup : : CanAllocateItem ( ) ) ;
2009-05-23 12:13:42 +00:00
IndustryProductionSpriteGroup * group = new IndustryProductionSpriteGroup ( ) ;
act_group = group ;
group - > version = type ;
2007-07-05 05:41:56 +00:00
if ( type = = 0 ) {
for ( uint i = 0 ; i < 3 ; i + + ) {
2010-01-23 13:17:30 +00:00
group - > subtract_input [ i ] = ( int16 ) buf - > ReadWord ( ) ; // signed
2007-07-05 05:41:56 +00:00
}
for ( uint i = 0 ; i < 2 ; i + + ) {
2010-01-23 13:17:30 +00:00
group - > add_output [ i ] = buf - > ReadWord ( ) ; // unsigned
2007-07-05 05:41:56 +00:00
}
2010-01-23 13:17:30 +00:00
group - > again = buf - > ReadByte ( ) ;
2007-07-05 05:41:56 +00:00
} else {
for ( uint i = 0 ; i < 3 ; i + + ) {
2010-01-23 13:17:30 +00:00
group - > subtract_input [ i ] = buf - > ReadByte ( ) ;
2007-07-05 05:41:56 +00:00
}
for ( uint i = 0 ; i < 2 ; i + + ) {
2010-01-23 13:17:30 +00:00
group - > add_output [ i ] = buf - > ReadByte ( ) ;
2007-07-05 05:41:56 +00:00
}
2010-01-23 13:17:30 +00:00
group - > again = buf - > ReadByte ( ) ;
2007-07-05 05:41:56 +00:00
}
break ;
}
2006-04-26 20:44:28 +00:00
/* Loading of Tile Layout and Production Callback groups would happen here */
2006-12-26 17:36:18 +00:00
default : grfmsg ( 1 , " NewSpriteGroup: Unsupported feature %d, skipping " , feature ) ;
2006-04-26 20:44:28 +00:00
}
2005-10-04 20:38:26 +00:00
}
2004-08-09 17:04:08 +00:00
}
2005-09-22 20:35:20 +00:00
2011-08-21 12:05:47 +00:00
_cur . grffile - > spritegroups [ setid ] = act_group ;
2004-08-09 17:04:08 +00:00
}
2007-02-24 19:36:47 +00:00
static CargoID TranslateCargo ( uint8 feature , uint8 ctype )
{
2010-08-28 18:22:24 +00:00
if ( feature = = GSF_OBJECTS ) {
switch ( ctype ) {
case 0 : return 0 ;
case 0xFF : return CT_PURCHASE_OBJECT ;
default :
grfmsg ( 1 , " TranslateCargo: Invalid cargo bitnum %d for objects, skipping. " , ctype ) ;
return CT_INVALID ;
}
}
2007-02-24 19:36:47 +00:00
/* Special cargo types for purchase list and stations */
2010-04-24 11:52:58 +00:00
if ( feature = = GSF_STATIONS & & ctype = = 0xFE ) return CT_DEFAULT_NA ;
2007-02-24 23:36:40 +00:00
if ( ctype = = 0xFF ) return CT_PURCHASE ;
2007-02-24 19:36:47 +00:00
2011-08-21 12:05:47 +00:00
if ( _cur . grffile - > cargo_max = = 0 ) {
2007-03-23 00:03:08 +00:00
/* No cargo table, so use bitnum values */
if ( ctype > = 32 ) {
2007-04-16 20:56:19 +00:00
grfmsg ( 1 , " TranslateCargo: Cargo bitnum %d out of range (max 31), skipping. " , ctype ) ;
2007-03-23 00:03:08 +00:00
return CT_INVALID ;
}
2009-07-16 20:40:06 +00:00
const CargoSpec * cs ;
FOR_ALL_CARGOSPECS ( cs ) {
2007-03-23 00:03:08 +00:00
if ( cs - > bitnum = = ctype ) {
2009-07-16 20:40:06 +00:00
grfmsg ( 6 , " TranslateCargo: Cargo bitnum %d mapped to cargo type %d. " , ctype , cs - > Index ( ) ) ;
return cs - > Index ( ) ;
2007-03-23 00:03:08 +00:00
}
}
2007-04-16 20:56:19 +00:00
grfmsg ( 5 , " TranslateCargo: Cargo bitnum %d not available in this climate, skipping. " , ctype ) ;
2007-03-23 00:03:08 +00:00
return CT_INVALID ;
}
2007-02-24 19:36:47 +00:00
/* Check if the cargo type is out of bounds of the cargo translation table */
2011-08-21 12:05:47 +00:00
if ( ctype > = _cur . grffile - > cargo_max ) {
grfmsg ( 1 , " TranslateCargo: Cargo type %d out of range (max %d), skipping. " , ctype , _cur . grffile - > cargo_max - 1 ) ;
2007-02-24 19:36:47 +00:00
return CT_INVALID ;
}
/* Look up the cargo label from the translation table */
2011-08-21 12:05:47 +00:00
CargoLabel cl = _cur . grffile - > cargo_list [ ctype ] ;
2007-02-24 19:36:47 +00:00
if ( cl = = 0 ) {
2007-04-16 20:56:19 +00:00
grfmsg ( 5 , " TranslateCargo: Cargo type %d not available in this climate, skipping. " , ctype ) ;
2007-02-24 19:36:47 +00:00
return CT_INVALID ;
}
ctype = GetCargoIDByLabel ( cl ) ;
if ( ctype = = CT_INVALID ) {
2007-04-16 20:56:19 +00: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 19:36:47 +00:00
return CT_INVALID ;
}
2007-04-16 20:56:19 +00:00
grfmsg ( 6 , " TranslateCargo: Cargo '%c%c%c%c' mapped to cargo type %d. " , GB ( cl , 24 , 8 ) , GB ( cl , 16 , 8 ) , GB ( cl , 8 , 8 ) , GB ( cl , 0 , 8 ) , ctype ) ;
2007-02-24 19:36:47 +00:00
return ctype ;
}
2004-08-09 17:04:08 +00:00
2008-06-02 19:35:30 +00:00
static bool IsValidGroupID ( uint16 groupid , const char * function )
{
2011-08-21 12:05:47 +00:00
if ( groupid > = _cur . grffile - > spritegroups_count | | _cur . grffile - > spritegroups [ groupid ] = = NULL ) {
grfmsg ( 1 , " %s: Spriteset 0x%04X out of range (maximum 0x%02X) or empty, skipping. " , function , groupid , _cur . grffile - > spritegroups_count - 1 ) ;
2008-06-02 19:35:30 +00:00
return false ;
}
return true ;
}
2010-01-22 13:02:15 +00:00
static void VehicleMapSpriteGroup ( ByteReader * buf , byte feature , uint8 idcount )
2007-03-23 20:01:25 +00:00
{
2008-01-05 15:39:22 +00:00
static EngineID * last_engines ;
static uint last_engines_count ;
2008-06-02 19:52:41 +00: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 15:36:49 +00:00
2004-11-13 12:21:32 +00:00
if ( last_engines_count = = 0 ) {
2007-04-16 20:56:19 +00:00
grfmsg ( 0 , " VehicleMapSpriteGroup: WagonOverride: No engine to do override with " ) ;
2004-11-13 12:21:32 +00:00
return ;
}
2007-03-23 20:01:25 +00:00
2007-04-16 20:56:19 +00:00
grfmsg ( 6 , " VehicleMapSpriteGroup: WagonOverride: %u engines, %u wagons " ,
2004-11-13 18:08:45 +00:00
last_engines_count , idcount ) ;
2008-06-02 19:52:41 +00:00
} else {
if ( last_engines_count ! = idcount ) {
last_engines = ReallocT ( last_engines , idcount ) ;
last_engines_count = idcount ;
}
2004-11-13 12:21:32 +00:00
}
2004-11-14 13:59:11 +00:00
2008-06-10 21:59:22 +00:00
EngineID * engines = AllocaM ( EngineID , idcount ) ;
2007-03-10 09:55:11 +00:00
for ( uint i = 0 ; i < idcount ; i + + ) {
2011-08-21 12:05:47 +00:00
Engine * e = GetNewEngine ( _cur . grffile , ( VehicleType ) feature , buf - > ReadExtendedByte ( ) ) ;
2011-02-09 18:46:55 +00:00
if ( e = = NULL ) {
/* 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 ;
}
engines [ i ] = e - > index ;
2008-06-02 19:52:41 +00:00
if ( ! wagover ) last_engines [ i ] = engines [ i ] ;
}
2004-08-09 17:04:08 +00:00
2010-01-23 13:17:30 +00:00
uint8 cidcount = buf - > ReadByte ( ) ;
2008-06-02 19:52:41 +00:00
for ( uint c = 0 ; c < cidcount ; c + + ) {
2010-01-23 13:17:30 +00:00
uint8 ctype = buf - > ReadByte ( ) ;
uint16 groupid = buf - > ReadWord ( ) ;
2008-06-02 19:52:41 +00:00
if ( ! IsValidGroupID ( groupid , " VehicleMapSpriteGroup " ) ) continue ;
2004-11-13 18:08:45 +00:00
2008-06-02 19:52:41 +00:00
grfmsg ( 8 , " VehicleMapSpriteGroup: * [%d] Cargo type 0x%X, group id 0x%02X " , c , ctype , groupid ) ;
2004-08-09 17:04:08 +00:00
2008-06-02 19:52:41 +00:00
ctype = TranslateCargo ( feature , ctype ) ;
if ( ctype = = CT_INVALID ) continue ;
2004-11-13 18:08:45 +00:00
2008-06-02 19:52:41 +00:00
for ( uint i = 0 ; i < idcount ; i + + ) {
EngineID engine = engines [ i ] ;
grfmsg ( 7 , " VehicleMapSpriteGroup: [%d] Engine %d... " , i , engine ) ;
2006-12-18 10:46:06 +00:00
2004-11-13 18:08:45 +00:00
if ( wagover ) {
2011-08-21 12:05:47 +00:00
SetWagonOverrideSprites ( engine , ctype , _cur . grffile - > spritegroups [ groupid ] , last_engines , last_engines_count ) ;
2004-11-12 15:36:49 +00:00
} else {
2011-08-21 12:05:47 +00:00
SetCustomEngineSprites ( engine , ctype , _cur . grffile - > spritegroups [ groupid ] ) ;
2004-08-09 17:04:08 +00:00
}
}
2004-11-12 15:36:49 +00:00
}
2004-08-09 17:04:08 +00:00
2010-01-23 13:17:30 +00:00
uint16 groupid = buf - > ReadWord ( ) ;
2008-06-02 19:52:41 +00:00
if ( ! IsValidGroupID ( groupid , " VehicleMapSpriteGroup " ) ) return ;
2004-08-09 17:04:08 +00:00
2008-06-02 19:52:41 +00:00
grfmsg ( 8 , " -- Default group id 0x%04X " , groupid ) ;
2004-11-13 18:08:45 +00:00
2008-06-02 19:52:41 +00:00
for ( uint i = 0 ; i < idcount ; i + + ) {
EngineID engine = engines [ i ] ;
2004-08-09 17:04:08 +00:00
2008-06-02 19:52:41 +00:00
if ( wagover ) {
2011-08-21 12:05:47 +00:00
SetWagonOverrideSprites ( engine , CT_DEFAULT , _cur . grffile - > spritegroups [ groupid ] , last_engines , last_engines_count ) ;
2008-06-02 19:52:41 +00:00
} else {
2011-08-21 12:05:47 +00:00
SetCustomEngineSprites ( engine , CT_DEFAULT , _cur . grffile - > spritegroups [ groupid ] ) ;
SetEngineGRF ( engine , _cur . grffile ) ;
2004-08-09 17:04:08 +00:00
}
}
}
2007-03-23 20:01:25 +00:00
2010-01-22 13:02:15 +00:00
static void CanalMapSpriteGroup ( ByteReader * buf , uint8 idcount )
2007-05-06 18:14:33 +00:00
{
2008-06-10 21:59:22 +00:00
CanalFeature * cfs = AllocaM ( CanalFeature , idcount ) ;
2008-06-02 19:52:41 +00:00
for ( uint i = 0 ; i < idcount ; i + + ) {
2010-01-23 13:17:30 +00:00
cfs [ i ] = ( CanalFeature ) buf - > ReadByte ( ) ;
2008-06-02 19:52:41 +00:00
}
2010-01-23 13:17:30 +00:00
uint8 cidcount = buf - > ReadByte ( ) ;
2010-01-22 13:02:15 +00:00
buf - > Skip ( cidcount * 3 ) ;
2008-06-02 19:52:41 +00:00
2010-01-23 13:17:30 +00:00
uint16 groupid = buf - > ReadWord ( ) ;
2008-06-02 19:35:30 +00:00
if ( ! IsValidGroupID ( groupid , " CanalMapSpriteGroup " ) ) return ;
2007-05-06 18:14:33 +00:00
for ( uint i = 0 ; i < idcount ; i + + ) {
2008-06-02 19:52:41 +00:00
CanalFeature cf = cfs [ i ] ;
2007-05-06 18:14:33 +00:00
if ( cf > = CF_END ) {
grfmsg ( 1 , " CanalMapSpriteGroup: Canal subset %d out of range, skipping " , cf ) ;
continue ;
}
2011-08-21 12:05:47 +00:00
_water_feature [ cf ] . grffile = _cur . grffile ;
_water_feature [ cf ] . group = _cur . grffile - > spritegroups [ groupid ] ;
2007-05-06 18:14:33 +00:00
}
}
2010-01-22 13:02:15 +00:00
static void StationMapSpriteGroup ( ByteReader * buf , uint8 idcount )
2007-03-23 20:01:25 +00:00
{
2008-06-10 21:59:22 +00:00
uint8 * stations = AllocaM ( uint8 , idcount ) ;
2007-03-23 20:01:25 +00:00
for ( uint i = 0 ; i < idcount ; i + + ) {
2010-01-23 13:17:30 +00:00
stations [ i ] = buf - > ReadByte ( ) ;
2008-06-02 19:52:41 +00:00
}
2007-10-09 15:28:40 +00:00
2010-01-23 13:17:30 +00:00
uint8 cidcount = buf - > ReadByte ( ) ;
2008-06-02 19:52:41 +00:00
for ( uint c = 0 ; c < cidcount ; c + + ) {
2010-01-23 13:17:30 +00:00
uint8 ctype = buf - > ReadByte ( ) ;
uint16 groupid = buf - > ReadWord ( ) ;
2008-06-02 19:52:41 +00:00
if ( ! IsValidGroupID ( groupid , " StationMapSpriteGroup " ) ) continue ;
2007-10-09 15:28:40 +00:00
2010-04-24 11:52:58 +00:00
ctype = TranslateCargo ( GSF_STATIONS , ctype ) ;
2008-06-02 19:52:41 +00:00
if ( ctype = = CT_INVALID ) continue ;
2007-03-23 20:01:25 +00:00
2008-06-02 19:52:41 +00:00
for ( uint i = 0 ; i < idcount ; i + + ) {
2011-08-21 12:05:47 +00:00
StationSpec * statspec = _cur . grffile - > stations = = NULL ? NULL : _cur . grffile - > stations [ stations [ i ] ] ;
2007-03-23 20:01:25 +00:00
2008-06-02 19:52:41 +00:00
if ( statspec = = NULL ) {
grfmsg ( 1 , " StationMapSpriteGroup: Station with ID 0x%02X does not exist, skipping " , stations [ i ] ) ;
2008-08-31 15:46:14 +00:00
continue ;
2008-06-02 19:52:41 +00:00
}
2007-03-23 20:01:25 +00:00
2011-08-21 12:05:47 +00:00
statspec - > grf_prop . spritegroup [ ctype ] = _cur . grffile - > spritegroups [ groupid ] ;
2007-03-23 20:01:25 +00:00
}
}
2010-01-23 13:17:30 +00:00
uint16 groupid = buf - > ReadWord ( ) ;
2008-06-02 19:52:41 +00:00
if ( ! IsValidGroupID ( groupid , " StationMapSpriteGroup " ) ) return ;
2007-03-23 20:01:25 +00:00
2008-06-02 19:52:41 +00:00
for ( uint i = 0 ; i < idcount ; i + + ) {
2011-08-21 12:05:47 +00:00
StationSpec * statspec = _cur . grffile - > stations = = NULL ? NULL : _cur . grffile - > stations [ stations [ i ] ] ;
2007-03-23 20:01:25 +00:00
2008-06-02 19:52:41 +00:00
if ( statspec = = NULL ) {
grfmsg ( 1 , " StationMapSpriteGroup: Station with ID 0x%02X does not exist, skipping " , stations [ i ] ) ;
continue ;
2007-03-23 20:01:25 +00:00
}
2008-06-02 19:52:41 +00:00
2010-08-07 21:39:30 +00:00
if ( statspec - > grf_prop . grffile ! = NULL ) {
grfmsg ( 1 , " StationMapSpriteGroup: Station with ID 0x%02X mapped multiple times, skipping " , stations [ i ] ) ;
continue ;
}
2011-08-21 12:05:47 +00:00
statspec - > grf_prop . spritegroup [ CT_DEFAULT ] = _cur . grffile - > spritegroups [ groupid ] ;
statspec - > grf_prop . grffile = _cur . grffile ;
2010-08-07 20:52:45 +00:00
statspec - > grf_prop . local_id = stations [ i ] ;
2010-08-07 22:07:07 +00:00
StationClass : : Assign ( statspec ) ;
2007-03-23 20:01:25 +00:00
}
}
2010-01-22 13:02:15 +00:00
static void TownHouseMapSpriteGroup ( ByteReader * buf , uint8 idcount )
2007-03-23 20:01:25 +00:00
{
2008-06-10 21:59:22 +00:00
uint8 * houses = AllocaM ( uint8 , idcount ) ;
2008-06-02 19:52:41 +00:00
for ( uint i = 0 ; i < idcount ; i + + ) {
2010-01-23 13:17:30 +00:00
houses [ i ] = buf - > ReadByte ( ) ;
2008-06-02 19:52:41 +00:00
}
/* Skip the cargo type section, we only care about the default group */
2010-01-23 13:17:30 +00:00
uint8 cidcount = buf - > ReadByte ( ) ;
2010-01-22 13:02:15 +00:00
buf - > Skip ( cidcount * 3 ) ;
2008-06-02 19:52:41 +00:00
2010-01-23 13:17:30 +00:00
uint16 groupid = buf - > ReadWord ( ) ;
2008-06-02 19:35:30 +00:00
if ( ! IsValidGroupID ( groupid , " TownHouseMapSpriteGroup " ) ) return ;
2007-03-23 20:01:25 +00:00
2011-08-21 12:05:47 +00:00
if ( _cur . grffile - > housespec = = NULL ) {
2010-01-08 19:14:33 +00:00
grfmsg ( 1 , " TownHouseMapSpriteGroup: No houses defined, skipping " ) ;
return ;
}
2007-03-23 20:01:25 +00:00
for ( uint i = 0 ; i < idcount ; i + + ) {
2011-08-21 12:05:47 +00:00
HouseSpec * hs = _cur . grffile - > housespec [ houses [ i ] ] ;
2007-03-23 20:01:25 +00:00
if ( hs = = NULL ) {
2008-08-31 15:46:14 +00:00
grfmsg ( 1 , " TownHouseMapSpriteGroup: House %d undefined, skipping. " , houses [ i ] ) ;
continue ;
2007-03-23 20:01:25 +00:00
}
2011-08-21 12:05:47 +00:00
hs - > grf_prop . spritegroup [ 0 ] = _cur . grffile - > spritegroups [ groupid ] ;
2007-03-23 20:01:25 +00:00
}
}
2010-01-22 13:02:15 +00:00
static void IndustryMapSpriteGroup ( ByteReader * buf , uint8 idcount )
2007-07-10 00:10:19 +00:00
{
2008-06-10 21:59:22 +00:00
uint8 * industries = AllocaM ( uint8 , idcount ) ;
2008-06-02 19:52:41 +00:00
for ( uint i = 0 ; i < idcount ; i + + ) {
2010-01-23 13:17:30 +00:00
industries [ i ] = buf - > ReadByte ( ) ;
2008-06-02 19:52:41 +00:00
}
/* Skip the cargo type section, we only care about the default group */
2010-01-23 13:17:30 +00:00
uint8 cidcount = buf - > ReadByte ( ) ;
2010-01-22 13:02:15 +00:00
buf - > Skip ( cidcount * 3 ) ;
2008-06-02 19:52:41 +00:00
2010-01-23 13:17:30 +00:00
uint16 groupid = buf - > ReadWord ( ) ;
2008-06-02 19:35:30 +00:00
if ( ! IsValidGroupID ( groupid , " IndustryMapSpriteGroup " ) ) return ;
2007-07-10 00:10:19 +00:00
2011-08-21 12:05:47 +00:00
if ( _cur . grffile - > industryspec = = NULL ) {
2010-01-08 19:14:33 +00:00
grfmsg ( 1 , " IndustryMapSpriteGroup: No industries defined, skipping " ) ;
return ;
}
2007-07-10 00:10:19 +00:00
for ( uint i = 0 ; i < idcount ; i + + ) {
2011-08-21 12:05:47 +00:00
IndustrySpec * indsp = _cur . grffile - > industryspec [ industries [ i ] ] ;
2007-07-10 00:10:19 +00:00
if ( indsp = = NULL ) {
2008-08-31 15:46:14 +00:00
grfmsg ( 1 , " IndustryMapSpriteGroup: Industry %d undefined, skipping " , industries [ i ] ) ;
continue ;
2007-07-10 00:10:19 +00:00
}
2011-08-21 12:05:47 +00:00
indsp - > grf_prop . spritegroup [ 0 ] = _cur . grffile - > spritegroups [ groupid ] ;
2007-07-10 00:10:19 +00:00
}
}
2010-01-22 13:02:15 +00:00
static void IndustrytileMapSpriteGroup ( ByteReader * buf , uint8 idcount )
2007-07-10 00:10:19 +00:00
{
2008-06-10 21:59:22 +00:00
uint8 * indtiles = AllocaM ( uint8 , idcount ) ;
2008-06-02 19:52:41 +00:00
for ( uint i = 0 ; i < idcount ; i + + ) {
2010-01-23 13:17:30 +00:00
indtiles [ i ] = buf - > ReadByte ( ) ;
2008-06-02 19:52:41 +00:00
}
/* Skip the cargo type section, we only care about the default group */
2010-01-23 13:17:30 +00:00
uint8 cidcount = buf - > ReadByte ( ) ;
2010-01-22 13:02:15 +00:00
buf - > Skip ( cidcount * 3 ) ;
2008-06-02 19:52:41 +00:00
2010-01-23 13:17:30 +00:00
uint16 groupid = buf - > ReadWord ( ) ;
2008-06-02 19:35:30 +00:00
if ( ! IsValidGroupID ( groupid , " IndustrytileMapSpriteGroup " ) ) return ;
2007-07-10 00:10:19 +00:00
2011-08-21 12:05:47 +00:00
if ( _cur . grffile - > indtspec = = NULL ) {
2010-01-08 19:14:33 +00:00
grfmsg ( 1 , " IndustrytileMapSpriteGroup: No industry tiles defined, skipping " ) ;
return ;
}
2007-07-10 00:10:19 +00:00
for ( uint i = 0 ; i < idcount ; i + + ) {
2011-08-21 12:05:47 +00:00
IndustryTileSpec * indtsp = _cur . grffile - > indtspec [ indtiles [ i ] ] ;
2007-07-10 00:10:19 +00:00
if ( indtsp = = NULL ) {
2008-08-31 15:46:14 +00:00
grfmsg ( 1 , " IndustrytileMapSpriteGroup: Industry tile %d undefined, skipping " , indtiles [ i ] ) ;
continue ;
2007-07-10 00:10:19 +00:00
}
2011-08-21 12:05:47 +00:00
indtsp - > grf_prop . spritegroup [ 0 ] = _cur . grffile - > spritegroups [ groupid ] ;
2007-07-10 00:10:19 +00:00
}
}
2007-03-23 20:55:45 +00:00
2010-01-22 13:02:15 +00:00
static void CargoMapSpriteGroup ( ByteReader * buf , uint8 idcount )
2007-03-23 20:55:45 +00:00
{
2008-06-10 21:59:22 +00:00
CargoID * cargos = AllocaM ( CargoID , idcount ) ;
2008-06-02 19:52:41 +00:00
for ( uint i = 0 ; i < idcount ; i + + ) {
2010-01-23 13:17:30 +00:00
cargos [ i ] = buf - > ReadByte ( ) ;
2008-06-02 19:52:41 +00:00
}
/* Skip the cargo type section, we only care about the default group */
2010-01-23 13:17:30 +00:00
uint8 cidcount = buf - > ReadByte ( ) ;
2010-01-22 13:02:15 +00:00
buf - > Skip ( cidcount * 3 ) ;
2008-06-02 19:52:41 +00:00
2010-01-23 13:17:30 +00:00
uint16 groupid = buf - > ReadWord ( ) ;
2008-06-02 19:35:30 +00:00
if ( ! IsValidGroupID ( groupid , " CargoMapSpriteGroup " ) ) return ;
2007-03-23 20:55:45 +00:00
for ( uint i = 0 ; i < idcount ; i + + ) {
2008-06-02 19:52:41 +00:00
CargoID cid = cargos [ i ] ;
2007-03-23 20:55:45 +00:00
if ( cid > = NUM_CARGO ) {
2008-06-02 19:52:41 +00:00
grfmsg ( 1 , " CargoMapSpriteGroup: Cargo ID %d out of range, skipping " , cid ) ;
2007-03-23 20:55:45 +00:00
continue ;
}
2009-07-16 19:00:13 +00:00
CargoSpec * cs = CargoSpec : : Get ( cid ) ;
2011-08-21 12:05:47 +00:00
cs - > grffile = _cur . grffile ;
cs - > group = _cur . grffile - > spritegroups [ groupid ] ;
2007-03-23 20:55:45 +00:00
}
}
2010-08-28 18:22:24 +00:00
static void ObjectMapSpriteGroup ( ByteReader * buf , uint8 idcount )
{
2011-08-21 12:05:47 +00:00
if ( _cur . grffile - > objectspec = = NULL ) {
2010-08-28 18:22:24 +00:00
grfmsg ( 1 , " ObjectMapSpriteGroup: No object tiles defined, skipping " ) ;
return ;
}
uint8 * objects = AllocaM ( uint8 , idcount ) ;
for ( uint i = 0 ; i < idcount ; i + + ) {
objects [ i ] = buf - > ReadByte ( ) ;
}
uint8 cidcount = buf - > ReadByte ( ) ;
for ( uint c = 0 ; c < cidcount ; c + + ) {
uint8 ctype = buf - > ReadByte ( ) ;
uint16 groupid = buf - > ReadWord ( ) ;
if ( ! IsValidGroupID ( groupid , " ObjectMapSpriteGroup " ) ) continue ;
ctype = TranslateCargo ( GSF_OBJECTS , ctype ) ;
if ( ctype = = CT_INVALID ) continue ;
for ( uint i = 0 ; i < idcount ; i + + ) {
2011-08-21 12:05:47 +00:00
ObjectSpec * spec = _cur . grffile - > objectspec [ objects [ i ] ] ;
2010-08-28 18:22:24 +00:00
if ( spec = = NULL ) {
grfmsg ( 1 , " ObjectMapSpriteGroup: Object with ID 0x%02X undefined, skipping " , objects [ i ] ) ;
continue ;
}
2011-08-21 12:05:47 +00:00
spec - > grf_prop . spritegroup [ ctype ] = _cur . grffile - > spritegroups [ groupid ] ;
2010-08-28 18:22:24 +00:00
}
}
uint16 groupid = buf - > ReadWord ( ) ;
if ( ! IsValidGroupID ( groupid , " ObjectMapSpriteGroup " ) ) return ;
for ( uint i = 0 ; i < idcount ; i + + ) {
2011-08-21 12:05:47 +00:00
ObjectSpec * spec = _cur . grffile - > objectspec [ objects [ i ] ] ;
2010-08-28 18:22:24 +00:00
if ( spec = = NULL ) {
grfmsg ( 1 , " ObjectMapSpriteGroup: Object with ID 0x%02X undefined, skipping " , objects [ i ] ) ;
continue ;
}
if ( spec - > grf_prop . grffile ! = NULL ) {
grfmsg ( 1 , " ObjectMapSpriteGroup: Object with ID 0x%02X mapped multiple times, skipping " , objects [ i ] ) ;
continue ;
}
2011-08-21 12:05:47 +00:00
spec - > grf_prop . spritegroup [ 0 ] = _cur . grffile - > spritegroups [ groupid ] ;
spec - > grf_prop . grffile = _cur . grffile ;
2010-08-28 18:22:24 +00:00
spec - > grf_prop . local_id = objects [ i ] ;
}
}
2010-02-07 22:22:54 +00:00
static void RailTypeMapSpriteGroup ( ByteReader * buf , uint8 idcount )
{
uint8 * railtypes = AllocaM ( uint8 , idcount ) ;
for ( uint i = 0 ; i < idcount ; i + + ) {
2011-08-21 12:05:47 +00:00
railtypes [ i ] = _cur . grffile - > railtype_map [ buf - > ReadByte ( ) ] ;
2010-02-07 22:22:54 +00: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 ] ;
for ( uint i = 0 ; i < idcount ; i + + ) {
if ( railtypes [ i ] ! = INVALID_RAILTYPE ) {
RailtypeInfo * rti = & _railtypes [ railtypes [ i ] ] ;
2011-08-21 12:05:47 +00:00
rti - > grffile [ ctype ] = _cur . grffile ;
rti - > group [ ctype ] = _cur . grffile - > spritegroups [ groupid ] ;
2010-02-07 22:22:54 +00:00
}
}
}
/* Railtypes do not use the default group. */
buf - > ReadWord ( ) ;
}
2010-03-18 23:15:38 +00:00
static void AirportMapSpriteGroup ( ByteReader * buf , uint8 idcount )
{
uint8 * airports = AllocaM ( uint8 , idcount ) ;
for ( uint i = 0 ; i < idcount ; i + + ) {
airports [ i ] = buf - > ReadByte ( ) ;
}
/* 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 ;
2011-08-21 12:05:47 +00:00
if ( _cur . grffile - > airportspec = = NULL ) {
2010-03-18 23:15:38 +00:00
grfmsg ( 1 , " AirportMapSpriteGroup: No airports defined, skipping " ) ;
return ;
}
for ( uint i = 0 ; i < idcount ; i + + ) {
2011-08-21 12:05:47 +00:00
AirportSpec * as = _cur . grffile - > airportspec [ airports [ i ] ] ;
2010-03-18 23:15:38 +00:00
if ( as = = NULL ) {
grfmsg ( 1 , " AirportMapSpriteGroup: Airport %d undefined, skipping " , airports [ i ] ) ;
continue ;
}
2011-08-21 12:05:47 +00:00
as - > grf_prop . spritegroup [ 0 ] = _cur . grffile - > spritegroups [ groupid ] ;
2010-03-18 23:15:38 +00:00
}
}
2010-02-22 14:16:19 +00:00
static void AirportTileMapSpriteGroup ( ByteReader * buf , uint8 idcount )
{
uint8 * airptiles = AllocaM ( uint8 , idcount ) ;
for ( uint i = 0 ; i < idcount ; i + + ) {
airptiles [ i ] = buf - > ReadByte ( ) ;
}
/* 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 ;
2011-08-21 12:05:47 +00:00
if ( _cur . grffile - > airtspec = = NULL ) {
2010-02-22 14:16:19 +00:00
grfmsg ( 1 , " AirportTileMapSpriteGroup: No airport tiles defined, skipping " ) ;
return ;
}
for ( uint i = 0 ; i < idcount ; i + + ) {
2011-08-21 12:05:47 +00:00
AirportTileSpec * airtsp = _cur . grffile - > airtspec [ airptiles [ i ] ] ;
2010-02-22 14:16:19 +00:00
if ( airtsp = = NULL ) {
grfmsg ( 1 , " AirportTileMapSpriteGroup: Airport tile %d undefined, skipping " , airptiles [ i ] ) ;
continue ;
}
2011-08-21 12:05:47 +00:00
airtsp - > grf_prop . spritegroup [ 0 ] = _cur . grffile - > spritegroups [ groupid ] ;
2010-02-22 14:16:19 +00:00
}
}
2007-03-23 20:55:45 +00:00
2007-03-23 20:01:25 +00:00
/* Action 0x03 */
2010-01-22 13:02:15 +00:00
static void FeatureMapSpriteGroup ( ByteReader * buf )
2007-03-23 20:01:25 +00: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 )
* B ids the IDs for which this definition applies
* 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 ) */
2011-08-21 12:05:47 +00:00
if ( _cur . grffile - > spritegroups = = NULL ) {
2008-02-12 13:23:57 +00:00
grfmsg ( 1 , " FeatureMapSpriteGroup: No sprite groups to work on! Skipping " ) ;
return ;
}
2010-01-23 13:17:30 +00:00
uint8 feature = buf - > ReadByte ( ) ;
uint8 idcount = buf - > ReadByte ( ) ;
2007-03-23 20:01:25 +00:00
/* If idcount is zero, this is a feature callback */
if ( idcount = = 0 ) {
2008-06-02 19:52:41 +00:00
/* Skip number of cargo ids? */
2010-01-23 13:17:30 +00:00
buf - > ReadByte ( ) ;
uint16 groupid = buf - > ReadWord ( ) ;
2011-05-01 21:02:18 +00:00
if ( ! IsValidGroupID ( groupid , " FeatureMapSpriteGroup " ) ) return ;
2008-02-12 13:23:57 +00:00
grfmsg ( 6 , " FeatureMapSpriteGroup: Adding generic feature callback for feature %d " , feature ) ;
2011-08-21 12:05:47 +00:00
AddGenericCallback ( feature , _cur . grffile , _cur . grffile - > spritegroups [ groupid ] ) ;
2007-03-23 20:01:25 +00:00
return ;
}
2009-11-24 13:15:58 +00:00
/* Mark the feature as used by the grf (generic callbacks do not count) */
2011-08-21 12:05:47 +00:00
SetBit ( _cur . grffile - > grf_features , feature ) ;
2009-11-24 13:15:58 +00:00
2008-06-02 19:52:41 +00:00
grfmsg ( 6 , " FeatureMapSpriteGroup: Feature %d, %d ids " , feature , idcount ) ;
2007-03-23 20:01:25 +00:00
switch ( feature ) {
2010-04-24 11:52:58 +00:00
case GSF_TRAINS :
case GSF_ROADVEHICLES :
case GSF_SHIPS :
2007-03-23 20:01:25 +00:00
case GSF_AIRCRAFT :
2008-06-02 19:52:41 +00:00
VehicleMapSpriteGroup ( buf , feature , idcount ) ;
2007-03-23 20:01:25 +00:00
return ;
2010-04-24 11:52:58 +00:00
case GSF_CANALS :
2008-06-02 19:52:41 +00:00
CanalMapSpriteGroup ( buf , idcount ) ;
2007-05-06 18:14:33 +00:00
return ;
2010-04-24 11:52:58 +00:00
case GSF_STATIONS :
2008-06-02 19:52:41 +00:00
StationMapSpriteGroup ( buf , idcount ) ;
2007-03-23 20:01:25 +00:00
return ;
2010-04-24 11:52:58 +00:00
case GSF_HOUSES :
2008-06-02 19:52:41 +00:00
TownHouseMapSpriteGroup ( buf , idcount ) ;
2007-03-23 20:01:25 +00:00
return ;
2007-07-10 00:10:19 +00:00
case GSF_INDUSTRIES :
2008-06-02 19:52:41 +00:00
IndustryMapSpriteGroup ( buf , idcount ) ;
2007-07-10 00:10:19 +00:00
return ;
case GSF_INDUSTRYTILES :
2008-06-02 19:52:41 +00:00
IndustrytileMapSpriteGroup ( buf , idcount ) ;
2007-07-10 00:10:19 +00:00
return ;
2007-03-23 20:55:45 +00:00
case GSF_CARGOS :
2008-06-02 19:52:41 +00:00
CargoMapSpriteGroup ( buf , idcount ) ;
2007-03-23 20:55:45 +00:00
return ;
2010-03-18 23:15:38 +00:00
case GSF_AIRPORTS :
AirportMapSpriteGroup ( buf , idcount ) ;
return ;
2010-08-28 18:22:24 +00:00
case GSF_OBJECTS :
ObjectMapSpriteGroup ( buf , idcount ) ;
break ;
2010-02-07 22:22:54 +00:00
case GSF_RAILTYPES :
RailTypeMapSpriteGroup ( buf , idcount ) ;
break ;
2010-02-22 14:16:19 +00:00
case GSF_AIRPORTTILES :
AirportTileMapSpriteGroup ( buf , idcount ) ;
return ;
2007-03-23 20:01:25 +00:00
default :
grfmsg ( 1 , " FeatureMapSpriteGroup: Unsupported feature %d, skipping " , feature ) ;
return ;
}
}
2004-11-12 15:15:56 +00:00
/* Action 0x04 */
2010-01-22 13:02:15 +00:00
static void FeatureNewName ( ByteReader * buf )
2004-08-09 17:04:08 +00:00
{
/* <04> <veh-type> <language-id> <num-veh> <offset> <data...>
*
2006-04-21 03:00:20 +00: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 10:23:39 +00: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 17:04:08 +00:00
* B num - veh number of vehicles which are getting a new name
2005-08-15 22:48:33 +00:00
* B / W offset number of the first vehicle that gets a new name
2006-04-21 03:00:20 +00:00
* Byte : ID of vehicle to change
* Word : ID of string to change / add
2004-08-09 17:04:08 +00:00
* S data new texts , each of them zero - terminated , after
* which the next name begins . */
2007-03-10 09:55:11 +00:00
2011-08-21 12:05:47 +00:00
bool new_scheme = _cur . grffile - > grf_version > = 7 ;
2004-08-09 17:04:08 +00:00
2010-01-23 13:17:30 +00:00
uint8 feature = buf - > ReadByte ( ) ;
uint8 lang = buf - > ReadByte ( ) ;
uint8 num = buf - > ReadByte ( ) ;
2007-11-19 21:02:30 +00:00
bool generic = HasBit ( lang , 7 ) ;
2008-06-12 09:25:04 +00:00
uint16 id ;
if ( generic ) {
2010-01-23 13:17:30 +00:00
id = buf - > ReadWord ( ) ;
2008-06-12 09:25:04 +00:00
} else if ( feature < = GSF_AIRCRAFT ) {
2010-03-10 07:31:08 +00:00
id = buf - > ReadExtendedByte ( ) ;
2008-06-12 09:25:04 +00:00
} else {
2010-01-23 13:17:30 +00:00
id = buf - > ReadByte ( ) ;
2008-06-12 09:25:04 +00:00
}
2006-10-28 18:42:57 +00:00
2007-11-19 21:32:20 +00:00
ClrBit ( lang , 7 ) ;
2005-08-15 22:48:33 +00:00
2007-03-10 09:55:11 +00:00
uint16 endid = id + num ;
2004-08-09 17:04:08 +00:00
2006-12-26 17:36:18 +00:00
grfmsg ( 6 , " FeatureNewName: About to rename engines %d..%d (feature %d) in language 0x%02X " ,
2004-11-13 18:08:45 +00:00
id , endid , feature , lang ) ;
2010-01-22 13:02:15 +00:00
for ( ; id < endid & & buf - > HasData ( ) ; id + + ) {
2010-01-23 13:17:30 +00:00
const char * name = buf - > ReadString ( ) ;
2007-05-11 19:22:43 +00:00
grfmsg ( 8 , " FeatureNewName: 0x%04X <- %s " , id , name ) ;
2006-04-21 03:00:20 +00:00
2007-05-11 19:22:43 +00:00
switch ( feature ) {
2010-04-24 11:52:58 +00:00
case GSF_TRAINS :
case GSF_ROADVEHICLES :
case GSF_SHIPS :
2007-05-11 19:22:43 +00:00
case GSF_AIRCRAFT :
2008-06-03 09:58:13 +00:00
if ( ! generic ) {
2011-08-21 12:05:47 +00:00
Engine * e = GetNewEngine ( _cur . grffile , ( VehicleType ) feature , id , HasBit ( _cur . grfconfig - > flags , GCF_STATIC ) ) ;
2009-03-08 18:08:30 +00:00
if ( e = = NULL ) break ;
2011-08-21 12:05:47 +00:00
StringID string = AddGRFString ( _cur . grffile - > grfid , e - > index , lang , new_scheme , name , e - > info . string_id ) ;
2008-04-29 21:31:29 +00:00
e - > info . string_id = string ;
2007-05-11 19:22:43 +00:00
} else {
2011-08-21 12:05:47 +00:00
AddGRFString ( _cur . grffile - > grfid , id , lang , new_scheme , name , STR_UNDEFINED ) ;
2006-04-21 20:59:04 +00:00
}
2007-05-11 19:22:43 +00:00
break ;
2006-04-21 03:00:20 +00:00
2007-07-06 20:36:32 +00:00
case GSF_INDUSTRIES : {
2011-08-21 12:05:47 +00:00
AddGRFString ( _cur . grffile - > grfid , id , lang , new_scheme , name , STR_UNDEFINED ) ;
2007-07-06 20:36:32 +00:00
break ;
}
2010-04-24 11:52:58 +00:00
case GSF_HOUSES :
2007-05-11 19:22:43 +00:00
default :
switch ( GB ( id , 8 , 8 ) ) {
case 0xC4 : // Station class name
2011-08-21 12:05:47 +00:00
if ( _cur . grffile - > stations = = NULL | | _cur . grffile - > stations [ GB ( id , 0 , 8 ) ] = = NULL ) {
2007-05-11 19:22:43 +00:00
grfmsg ( 1 , " FeatureNewName: Attempt to name undefined station 0x%X, ignoring " , GB ( id , 0 , 8 ) ) ;
} else {
2011-08-21 12:05:47 +00:00
StationClassID cls_id = _cur . grffile - > stations [ GB ( id , 0 , 8 ) ] - > cls_id ;
StationClass : : SetName ( cls_id , AddGRFString ( _cur . grffile - > grfid , id , lang , new_scheme , name , STR_UNDEFINED ) ) ;
2007-03-19 11:27:30 +00:00
}
2007-05-11 19:22:43 +00:00
break ;
2007-03-19 11:27:30 +00:00
2007-05-11 19:22:43 +00:00
case 0xC5 : // Station name
2011-08-21 12:05:47 +00:00
if ( _cur . grffile - > stations = = NULL | | _cur . grffile - > stations [ GB ( id , 0 , 8 ) ] = = NULL ) {
2007-05-11 19:22:43 +00:00
grfmsg ( 1 , " FeatureNewName: Attempt to name undefined station 0x%X, ignoring " , GB ( id , 0 , 8 ) ) ;
} else {
2011-08-21 12:05:47 +00:00
_cur . grffile - > stations [ GB ( id , 0 , 8 ) ] - > name = AddGRFString ( _cur . grffile - > grfid , id , lang , new_scheme , name , STR_UNDEFINED ) ;
2007-05-11 19:22:43 +00:00
}
break ;
2006-09-01 02:56:02 +00:00
2010-02-22 14:17:17 +00:00
case 0xC7 : // Airporttile name
2011-08-21 12:05:47 +00:00
if ( _cur . grffile - > airtspec = = NULL | | _cur . grffile - > airtspec [ GB ( id , 0 , 8 ) ] = = NULL ) {
2010-02-22 14:17:17 +00:00
grfmsg ( 1 , " FeatureNewName: Attempt to name undefined airport tile 0x%X, ignoring " , GB ( id , 0 , 8 ) ) ;
} else {
2011-08-21 12:05:47 +00:00
_cur . grffile - > airtspec [ GB ( id , 0 , 8 ) ] - > name = AddGRFString ( _cur . grffile - > grfid , id , lang , new_scheme , name , STR_UNDEFINED ) ;
2010-02-22 14:17:17 +00:00
}
break ;
2007-05-11 19:22:43 +00:00
case 0xC9 : // House name
2011-08-21 12:05:47 +00:00
if ( _cur . grffile - > housespec = = NULL | | _cur . grffile - > housespec [ GB ( id , 0 , 8 ) ] = = NULL ) {
2007-05-11 19:22:43 +00:00
grfmsg ( 1 , " FeatureNewName: Attempt to name undefined house 0x%X, ignoring. " , GB ( id , 0 , 8 ) ) ;
} else {
2011-08-21 12:05:47 +00:00
_cur . grffile - > housespec [ GB ( id , 0 , 8 ) ] - > building_name = AddGRFString ( _cur . grffile - > grfid , id , lang , new_scheme , name , STR_UNDEFINED ) ;
2007-05-11 19:22:43 +00:00
}
break ;
case 0xD0 :
2008-02-29 08:40:49 +00:00
case 0xD1 :
case 0xD2 :
case 0xD3 :
2007-05-11 19:22:43 +00:00
case 0xDC :
2011-08-21 12:05:47 +00:00
AddGRFString ( _cur . grffile - > grfid , id , lang , new_scheme , name , STR_UNDEFINED ) ;
2007-05-11 19:22:43 +00:00
break ;
default :
grfmsg ( 7 , " FeatureNewName: Unsupported ID (0x%04X) " , id ) ;
break ;
}
break ;
2004-11-13 18:08:45 +00:00
}
2004-08-09 17:04:08 +00:00
}
}
2007-11-15 07:42:25 +00:00
/**
* Sanitize incoming sprite offsets for Action 5 graphics replacements .
* @ 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
*/
static uint16 SanitizeSpriteOffset ( uint16 & num , uint16 offset , int max_sprites , const char * name )
{
if ( offset > = max_sprites ) {
grfmsg ( 1 , " GraphicsNew: %s sprite offset must be less than %i, skipping " , name , max_sprites ) ;
uint orig_num = num ;
num = 0 ;
return orig_num ;
}
if ( offset + num > max_sprites ) {
grfmsg ( 4 , " GraphicsNew: %s sprite overflow, truncating... " , name ) ;
uint orig_num = num ;
num = max ( max_sprites - offset , 0 ) ;
return orig_num - num ;
}
return 0 ;
}
2010-08-19 19:23:38 +00: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 */
/* 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_FIXED , SPR_SIGNALS_BASE , 48 , PRESIGNAL_SEMAPHORE_AND_PBS_SPRITE_COUNT , " Signal graphics " } ,
/* 0x05 */ { A5BLOCK_FIXED , SPR_ELRAIL_BASE , 48 , ELRAIL_SPRITE_COUNT , " Catenary graphics " } ,
/* 0x06 */ { A5BLOCK_FIXED , SPR_SLOPES_BASE , 74 , NORMAL_AND_HALFTILE_FOUNDATION_SPRITE_COUNT , " Foundation graphics " } ,
/* 0x07 */ { A5BLOCK_INVALID , 0 , 75 , 0 , " TTDP GUI graphics " } , // Not used by OTTD.
/* 0x08 */ { A5BLOCK_FIXED , SPR_CANALS_BASE , 65 , CANALS_SPRITE_COUNT , " Canal graphics " } ,
/* 0x09 */ { A5BLOCK_FIXED , SPR_ONEWAY_BASE , 6 , ONEWAY_SPRITE_COUNT , " One way road graphics " } ,
/* 0x0A */ { A5BLOCK_FIXED , SPR_2CCMAP_BASE , 256 , TWOCCMAP_SPRITE_COUNT , " 2CC colour maps " } ,
/* 0x0B */ { A5BLOCK_FIXED , SPR_TRAMWAY_BASE , 113 , 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_FIXED , SPR_TRACKS_FOR_SLOPES_BASE , 12 , TRACKS_FOR_SLOPES_SPRITE_COUNT , " Sloped rail track " } ,
/* 0x10 */ { A5BLOCK_FIXED , SPR_AIRPORTX_BASE , 15 , AIRPORTX_SPRITE_COUNT , " Airport graphics " } ,
/* 0x11 */ { A5BLOCK_FIXED , SPR_ROADSTOP_BASE , 8 , ROADSTOP_SPRITE_COUNT , " Road stop graphics " } ,
/* 0x12 */ { A5BLOCK_FIXED , SPR_AQUEDUCT_BASE , 8 , AQUEDUCT_SPRITE_COUNT , " Aqueduct graphics " } ,
/* 0x13 */ { A5BLOCK_FIXED , SPR_AUTORAIL_BASE , 55 , AUTORAIL_SPRITE_COUNT , " Autorail graphics " } ,
/* 0x14 */ { A5BLOCK_ALLOW_OFFSET , SPR_FLAGS_BASE , 1 , FLAGS_SPRITE_COUNT , " Flag graphics " } ,
/* 0x15 */ { A5BLOCK_ALLOW_OFFSET , SPR_OPENTTD_BASE , 1 , OPENTTD_SPRITE_COUNT , " OpenTTD GUI graphics " } ,
/* 0x16 */ { A5BLOCK_ALLOW_OFFSET , SPR_AIRPORT_PREVIEW_BASE , 1 , SPR_AIRPORT_PREVIEW_COUNT , " Airport preview graphics " } ,
} ;
2004-11-12 15:15:56 +00:00
/* Action 0x05 */
2010-01-22 13:02:15 +00:00
static void GraphicsNew ( ByteReader * buf )
2004-08-09 17:04:08 +00:00
{
/* <05> <graphics-type> <num-sprites> <other data...>
*
* B graphics - type What set of graphics the sprites define .
2005-08-15 22:48:33 +00:00
* E num - sprites How many sprites are in this set ?
2004-08-09 17:04:08 +00:00
* V other data Graphics type specific data . Currently unused . */
/* TODO */
2004-11-12 15:15:56 +00:00
2010-01-23 13:17:30 +00:00
uint8 type = buf - > ReadByte ( ) ;
2010-03-10 07:31:08 +00:00
uint16 num = buf - > ReadExtendedByte ( ) ;
uint16 offset = HasBit ( type , 7 ) ? buf - > ReadExtendedByte ( ) : 0 ;
2007-11-19 21:32:20 +00:00
ClrBit ( type , 7 ) ; // Clear the high bit as that only indicates whether there is an offset.
2004-11-12 15:15:56 +00:00
2011-08-21 12:05:47 +00:00
if ( ( type = = 0x0D ) & & ( num = = 10 ) & & _cur . grffile - > is_ottdfile ) {
2010-08-19 19:23:38 +00:00
/* Special not-TTDP-compatible case used in openttd.grf
2008-01-10 18:02:15 +00:00
* Missing shore sprites and initialisation of SPR_SHORE_BASE */
2010-08-19 19:23:38 +00:00
grfmsg ( 2 , " GraphicsNew: Loading 10 missing shore sprites from extra grf. " ) ;
2011-08-21 12:05:47 +00:00
LoadNextSprite ( SPR_SHORE_BASE + 0 , _cur . file_index , _cur . nfo_line + + ) ; // SLOPE_STEEP_S
LoadNextSprite ( SPR_SHORE_BASE + 5 , _cur . file_index , _cur . nfo_line + + ) ; // SLOPE_STEEP_W
LoadNextSprite ( SPR_SHORE_BASE + 7 , _cur . file_index , _cur . nfo_line + + ) ; // SLOPE_WSE
LoadNextSprite ( SPR_SHORE_BASE + 10 , _cur . file_index , _cur . nfo_line + + ) ; // SLOPE_STEEP_N
LoadNextSprite ( SPR_SHORE_BASE + 11 , _cur . file_index , _cur . nfo_line + + ) ; // SLOPE_NWS
LoadNextSprite ( SPR_SHORE_BASE + 13 , _cur . file_index , _cur . nfo_line + + ) ; // SLOPE_ENW
LoadNextSprite ( SPR_SHORE_BASE + 14 , _cur . file_index , _cur . nfo_line + + ) ; // SLOPE_SEN
LoadNextSprite ( SPR_SHORE_BASE + 15 , _cur . file_index , _cur . nfo_line + + ) ; // SLOPE_STEEP_E
LoadNextSprite ( SPR_SHORE_BASE + 16 , _cur . file_index , _cur . nfo_line + + ) ; // SLOPE_EW
LoadNextSprite ( SPR_SHORE_BASE + 17 , _cur . file_index , _cur . nfo_line + + ) ; // SLOPE_NS
2008-01-24 14:49:40 +00:00
if ( _loaded_newgrf_features . shore = = SHORE_REPLACE_NONE ) _loaded_newgrf_features . shore = SHORE_REPLACE_ONLY_NEW ;
2008-01-10 18:02:15 +00:00
return ;
}
2007-11-15 07:42:25 +00:00
2008-01-10 18:02:15 +00:00
/* Supported type? */
2010-08-19 19:23:38 +00:00
if ( ( type > = lengthof ( _action5_types ) ) | | ( _action5_types [ type ] . block_type = = A5BLOCK_INVALID ) ) {
2008-01-10 18:02:15 +00:00
grfmsg ( 2 , " GraphicsNew: Custom graphics (type 0x%02X) sprite block of length %u (unimplemented, ignoring) " , type , num ) ;
2011-08-21 12:05:47 +00:00
_cur . skip_sprites = num ;
2008-01-10 18:02:15 +00:00
return ;
}
2007-11-15 07:42:25 +00:00
2010-08-19 19:23:38 +00:00
const Action5Type * action5_type = & _action5_types [ type ] ;
2007-11-15 07:42:25 +00:00
2008-01-10 18:02:15 +00:00
/* Ignore offset if not allowed */
if ( ( action5_type - > block_type ! = A5BLOCK_ALLOW_OFFSET ) & & ( offset ! = 0 ) ) {
grfmsg ( 1 , " GraphicsNew: %s (type 0x%02X) do not allow an <offset> field. Ignoring offset. " , action5_type - > name , type ) ;
offset = 0 ;
2006-04-23 15:57:34 +00:00
}
2008-01-10 18:02:15 +00:00
/* 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 ) ) {
grfmsg ( 1 , " GraphicsNew: %s (type 0x%02X) count must be at least %d. Only %d were specified. Skipping. " , action5_type - > name , type , action5_type - > min_sprites , num ) ;
2011-08-21 12:05:47 +00:00
_cur . skip_sprites = num ;
2008-01-10 18:02:15 +00:00
return ;
2006-05-01 16:54:18 +00:00
}
2006-04-23 15:57:34 +00:00
2008-01-10 18:02:15 +00:00
/* Load at most max_sprites sprites. Skip remaining sprites. (for compatibility with TTDP and future extentions) */
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. */
grfmsg ( 2 , " GraphicsNew: Replacing sprites %d to %d of %s (type 0x%02X) at SpriteID 0x%04X " , offset , offset + num - 1 , action5_type - > name , type , replace ) ;
2006-04-23 15:57:34 +00:00
for ( ; num > 0 ; num - - ) {
2011-08-21 12:05:47 +00:00
_cur . nfo_line + + ;
LoadNextSprite ( replace = = 0 ? _cur . spriteid + + : replace + + , _cur . file_index , _cur . nfo_line ) ;
2006-04-23 15:57:34 +00:00
}
2007-11-15 07:42:25 +00:00
2008-01-24 14:49:40 +00:00
if ( type = = 0x0D ) _loaded_newgrf_features . shore = SHORE_REPLACE_ACTION_5 ;
2011-08-21 12:05:47 +00:00
_cur . skip_sprites = skip_num ;
2004-08-09 17:04:08 +00:00
}
2007-04-26 06:49:32 +00:00
/* Action 0x05 (SKIP) */
2010-01-22 13:02:15 +00:00
static void SkipAct5 ( ByteReader * buf )
2007-04-26 06:49:32 +00:00
{
/* Ignore type byte */
2010-01-23 13:17:30 +00:00
buf - > ReadByte ( ) ;
2007-04-26 06:49:32 +00:00
/* Skip the sprites of this action */
2011-08-21 12:05:47 +00:00
_cur . skip_sprites = buf - > ReadExtendedByte ( ) ;
2007-04-26 06:49:32 +00:00
2011-08-21 12:05:47 +00:00
grfmsg ( 3 , " SkipAct5: Skipping %d sprites " , _cur . skip_sprites ) ;
2007-04-26 06:49:32 +00:00
}
2010-08-19 19:23:38 +00:00
/**
* Check whether we are ( obviously ) missing some of the extra
* ( Action 0x05 ) sprites that we like to use .
* When missing sprites are found a warning will be shown .
*/
void CheckForMissingSprites ( )
{
/* Don't break out quickly, but allow to check the other
* sprites as well , so we can give the best information . */
bool missing = false ;
for ( uint8 i = 0 ; i < lengthof ( _action5_types ) ; i + + ) {
const Action5Type * type = & _action5_types [ i ] ;
if ( type - > block_type = = A5BLOCK_INVALID ) continue ;
for ( uint j = 0 ; j < type - > max_sprites ; j + + ) {
if ( ! SpriteExists ( type - > sprite_base + j ) ) {
DEBUG ( grf , 0 , " %s sprites are missing " , type - > name ) ;
missing = true ;
/* No need to log more of the same. */
break ;
}
}
}
if ( missing ) {
ShowErrorMessage ( STR_NEWGRF_ERROR_MISSING_SPRITES , INVALID_STRING_ID , WL_CRITICAL ) ;
}
}
2008-02-12 15:43:10 +00: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 .
* @ return true iff the variable is known and the value is returned in ' value ' .
*/
bool GetGlobalVariable ( byte param , uint32 * value )
2006-05-10 21:34:57 +00:00
{
switch ( param ) {
2008-02-12 15:43:10 +00:00
case 0x00 : // current date
* value = max ( _date - DAYS_TILL_ORIGINAL_BASE_YEAR , 0 ) ;
return true ;
case 0x01 : // current year
* value = Clamp ( _cur_year , ORIGINAL_BASE_YEAR , ORIGINAL_MAX_YEAR ) - ORIGINAL_BASE_YEAR ;
return true ;
2008-06-20 21:14:10 +00: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)
YearMonthDay ymd ;
ConvertDateToYMD ( _date , & ymd ) ;
Date start_of_year = ConvertYMDToDate ( ymd . year , 0 , 1 ) ;
* value = ymd . month | ( ymd . day - 1 ) < < 8 | ( IsLeapYear ( ymd . year ) ? 1 < < 15 : 0 ) | ( _date - start_of_year ) < < 16 ;
2008-02-12 15:43:10 +00:00
return true ;
2008-06-20 21:14:10 +00:00
}
2008-02-12 15:43:10 +00:00
case 0x03 : // current climate, 0=temp, 1=arctic, 2=trop, 3=toyland
2008-05-29 15:13:28 +00:00
* value = _settings_game . game_creation . landscape ;
2008-02-12 15:43:10 +00:00
return true ;
case 0x06 : // road traffic side, bit 4 clear=left, set=right
2008-05-29 15:13:28 +00:00
* value = _settings_game . vehicle . road_side < < 4 ;
2008-02-12 15:43:10 +00:00
return true ;
case 0x09 : // date fraction
2009-10-20 19:30:50 +00:00
* value = _date_fract * 885 ;
2008-02-12 15:43:10 +00:00
return true ;
case 0x0A : // animation counter
* value = _tick_counter ;
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 12:05:47 +00:00
* value = _cur . grfconfig - > palette & GRFP_USE_MASK ;
2008-02-12 15:43:10 +00:00
return true ;
case 0x0E : // Y-offset for train sprites
2011-08-21 12:05:47 +00:00
* value = _cur . grffile - > traininfo_vehicle_pitch ;
2008-02-12 15:43:10 +00:00
return true ;
case 0x0F : // Rail track type cost factors
* value = 0 ;
2008-08-24 23:10:23 +00:00
SB ( * value , 0 , 8 , GetRailTypeInfo ( RAILTYPE_RAIL ) - > cost_multiplier ) ; // normal rail
2008-05-29 15:13:28 +00:00
if ( _settings_game . vehicle . disable_elrails ) {
2008-02-12 15:43:10 +00:00
/* skip elrail multiplier - disabled */
2008-08-24 23:10:23 +00:00
SB ( * value , 8 , 8 , GetRailTypeInfo ( RAILTYPE_MONO ) - > cost_multiplier ) ; // monorail
2008-02-12 15:43:10 +00:00
} else {
2008-08-24 23:10:23 +00:00
SB ( * value , 8 , 8 , GetRailTypeInfo ( RAILTYPE_ELECTRIC ) - > cost_multiplier ) ; // electified railway
2008-02-12 15:43:10 +00:00
/* Skip monorail multiplier - no space in result */
}
2008-08-24 23:10:23 +00:00
SB ( * value , 16 , 8 , GetRailTypeInfo ( RAILTYPE_MAGLEV ) - > cost_multiplier ) ; // maglev
2008-02-12 15:43:10 +00:00
return true ;
case 0x11 : // current rail tool type
2011-07-22 18:59:10 +00:00
* value = 0 ; // constant fake value to avoid desync
2008-02-12 15:43:10 +00: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 18:59:10 +00:00
* value = 0x3F ; // constant fake value to avoid desync
2008-02-12 15:43:10 +00: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-17 22:00:13 +00:00
/* Add the local flags */
assert ( ! HasBit ( * value , GMB_TRAIN_WIDTH_32_PIXELS ) ) ;
2011-08-21 12:05:47 +00:00
if ( _cur . grffile - > traininfo_vehicle_width = = VEHICLEINFO_FULL_VEHICLE_WIDTH ) SetBit ( * value , GMB_TRAIN_WIDTH_32_PIXELS ) ;
2008-02-12 15:43:10 +00:00
return true ;
2011-07-22 18:59:10 +00:00
/* case 0x1F: // locale dependent settings not implemented to avoid desync */
2006-12-18 15:34:12 +00:00
2008-02-12 15:43:10 +00:00
case 0x20 : // snow line height
2008-05-29 15:13:28 +00:00
* value = _settings_game . game_creation . landscape = = LT_ARCTIC ? GetSnowLine ( ) : 0xFF ;
2008-02-12 15:43:10 +00:00
return true ;
2006-05-10 21:34:57 +00:00
2008-05-04 21:53:36 +00:00
case 0x21 : // OpenTTD version
2008-02-12 15:43:10 +00:00
* value = _openttd_newgrf_version ;
return true ;
2008-03-27 17:06:35 +00:00
case 0x22 : // difficulty level
2008-05-29 15:13:28 +00:00
* value = _settings_game . difficulty . diff_level ;
2008-03-27 17:06:35 +00:00
return true ;
2008-06-04 00:17:48 +00:00
case 0x23 : // long format date
* value = _date ;
return true ;
case 0x24 : // long format year
* value = _cur_year ;
return true ;
2008-02-12 15:43:10 +00:00
default : return false ;
}
}
static uint32 GetParamVal ( byte param , uint32 * cond_val )
{
/* First handle variable common with VarAction2 */
uint32 value ;
if ( GetGlobalVariable ( param - 0x80 , & value ) ) return value ;
/* Non-common variable */
switch ( param ) {
2007-06-07 07:20:44 +00:00
case 0x84 : { // GRF loading stage
uint32 res = 0 ;
2011-08-21 12:05:47 +00: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 07:20:44 +00:00
return res ;
}
2006-05-10 21:34:57 +00:00
2007-03-21 03:06:21 +00:00
case 0x85 : // TTDPatch flags, only for bit tests
2006-05-11 09:54:11 +00:00
if ( cond_val = = NULL ) {
/* Supported in Action 0x07 and 0x09, not 0x0D */
return 0 ;
} else {
uint32 param_val = _ttdpatch_flags [ * cond_val / 0x20 ] ;
* cond_val % = 0x20 ;
return param_val ;
}
2006-05-10 21:34:57 +00:00
2007-03-21 03:06:21 +00:00
case 0x88 : // GRF ID check
2006-05-10 21:34:57 +00:00
return 0 ;
2008-02-12 15:43:10 +00:00
/* case 0x99: Global ID offest not implemented */
2007-10-21 14:59:05 +00:00
2006-05-10 21:34:57 +00:00
default :
/* GRF Parameter */
2011-08-21 12:05:47 +00:00
if ( param < 0x80 ) return _cur . grffile - > GetParam ( param ) ;
2006-05-10 21:34:57 +00:00
/* In-game variable. */
2006-12-26 17:36:18 +00:00
grfmsg ( 1 , " Unsupported in-game variable 0x%02X " , param ) ;
2007-01-10 18:56:51 +00:00
return UINT_MAX ;
2006-05-10 21:34:57 +00:00
}
}
2006-05-23 20:59:01 +00:00
/* Action 0x06 */
2010-01-22 13:02:15 +00:00
static void CfgApply ( ByteReader * buf )
2006-05-23 20:59:01 +00: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 */
2008-05-27 21:41:00 +00:00
size_t pos = FioGetPos ( ) ;
2006-05-23 20:59:01 +00:00
uint16 num = FioReadWord ( ) ;
uint8 type = FioReadByte ( ) ;
2008-08-19 09:08:45 +00:00
byte * preload_sprite = NULL ;
2006-05-23 20:59:01 +00:00
/* Check if the sprite is a pseudo sprite. We can't operate on real sprites. */
if ( type = = 0xFF ) {
2008-08-19 09:08:45 +00:00
preload_sprite = MallocT < byte > ( num ) ;
FioReadBlock ( preload_sprite , num ) ;
2006-05-23 20:59:01 +00:00
}
/* Reset the file position to the start of the next sprite */
FioSeekTo ( pos , SEEK_SET ) ;
if ( type ! = 0xFF ) {
2006-12-26 17:36:18 +00:00
grfmsg ( 2 , " CfgApply: Ignoring (next sprite is real, unsupported) " ) ;
2008-08-19 09:08:45 +00:00
free ( preload_sprite ) ;
2006-05-23 20:59:01 +00:00
return ;
}
2011-08-21 12:05:47 +00:00
GRFLocation location ( _cur . grfconfig - > ident . grfid , _cur . nfo_line + 1 ) ;
2008-08-19 09:08:45 +00:00
GRFLineToSpriteOverride : : iterator it = _grf_line_to_action6_sprite_override . find ( location ) ;
if ( it ! = _grf_line_to_action6_sprite_override . end ( ) ) {
free ( preload_sprite ) ;
preload_sprite = _grf_line_to_action6_sprite_override [ location ] ;
} else {
_grf_line_to_action6_sprite_override [ location ] = preload_sprite ;
}
2006-05-23 20:59:01 +00:00
/* Now perform the Action 0x06 on our data. */
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 13:17:30 +00:00
param_num = buf - > ReadByte ( ) ;
2006-05-23 20:59:01 +00: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 13:17:30 +00:00
param_size = buf - > ReadByte ( ) ;
2006-05-23 20:59:01 +00:00
/* Bit 7 of param_size indicates we should add to the original value
* instead of replacing it . */
2007-11-19 21:02:30 +00:00
add_value = HasBit ( param_size , 7 ) ;
2006-05-23 20:59:01 +00:00
param_size = GB ( param_size , 0 , 7 ) ;
/* Where to apply the data to within the pseudo sprite data. */
2010-03-10 07:31:08 +00:00
offset = buf - > ReadExtendedByte ( ) ;
2006-05-23 20:59:01 +00: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 12:05:47 +00:00
if ( param_num < 0x80 & & ( param_num + ( param_size - 1 ) / 4 ) > = _cur . grffile - > param_end ) {
2006-12-26 17:36:18 +00:00
grfmsg ( 2 , " CfgApply: Ignoring (param %d not set) " , ( param_num + ( param_size - 1 ) / 4 ) ) ;
2006-05-23 20:59:01 +00:00
break ;
}
2006-12-26 17:36:18 +00:00
grfmsg ( 8 , " CfgApply: Applying %u bytes from parameter 0x%02X at offset 0x%04X " , param_size , param_num , offset ) ;
2006-05-23 20:59:01 +00:00
2007-05-19 22:37:24 +00:00
bool carry = false ;
2008-12-13 19:43:57 +00:00
for ( i = 0 ; i < param_size & & offset + i < num ; i + + ) {
2006-05-23 20:59:01 +00:00
uint32 value = GetParamVal ( param_num + i / 4 , NULL ) ;
2007-05-19 22:37:24 +00:00
/* Reset carry flag for each iteration of the variable (only really
* matters if param_size is greater than 4 ) */
2011-02-04 08:29:14 +00:00
if ( i % 4 = = 0 ) carry = false ;
2006-05-23 20:59:01 +00:00
if ( add_value ) {
2008-08-19 09:08:45 +00: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-19 22:37:24 +00:00
/* Check if the addition overflowed */
carry = new_value > = 256 ;
2006-05-23 20:59:01 +00:00
} else {
2008-08-19 09:08:45 +00:00
preload_sprite [ offset + i ] = GB ( value , ( i % 4 ) * 8 , 8 ) ;
2006-05-23 20:59:01 +00:00
}
}
}
}
2008-06-19 10:20:45 +00: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 .
* @ param c the NewGRF to disable .
*/
static void DisableStaticNewGRFInfluencingNonStaticNewGRFs ( GRFConfig * c )
{
2011-05-11 20:22:04 +00:00
GRFError * error = DisableGrf ( STR_NEWGRF_ERROR_STATIC_GRF_CAUSES_DESYNC , c ) ;
2011-08-21 12:05:47 +00:00
error - > data = strdup ( _cur . grfconfig - > GetName ( ) ) ;
2008-06-19 10:20:45 +00:00
}
2009-03-15 00:32:18 +00:00
/* Action 0x07
* Action 0x09 */
2010-01-22 13:02:15 +00:00
static void SkipIf ( ByteReader * buf )
2004-08-09 17:04:08 +00: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 */
/* TODO: More params. More condition types. */
2005-08-13 16:23:07 +00:00
uint32 cond_val = 0 ;
2006-05-10 21:34:57 +00:00
uint32 mask = 0 ;
2004-11-12 18:43:12 +00:00
bool result ;
2004-11-12 15:36:49 +00:00
2010-01-23 13:17:30 +00:00
uint8 param = buf - > ReadByte ( ) ;
uint8 paramsize = buf - > ReadByte ( ) ;
uint8 condtype = buf - > ReadByte ( ) ;
2004-11-12 15:36:49 +00:00
2004-11-12 17:27:30 +00:00
if ( condtype < 2 ) {
2005-04-01 10:43:20 +00:00
/* Always 1 for bit tests, the given value should be ignored. */
2004-11-12 17:27:30 +00:00
paramsize = 1 ;
}
2005-04-01 10:43:20 +00:00
switch ( paramsize ) {
2010-01-23 13:17:30 +00: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 10:43:20 +00:00
default : break ;
}
2004-11-12 17:27:30 +00:00
2011-08-21 12:05:47 +00:00
if ( param < 0x80 & & _cur . grffile - > param_end < = param ) {
2007-04-16 20:56:19 +00:00
grfmsg ( 7 , " SkipIf: Param %d undefined, skipping test " , param ) ;
2006-05-10 21:34:57 +00:00
return ;
}
2005-08-13 16:23:07 +00:00
2007-03-10 09:55:11 +00:00
uint32 param_val = GetParamVal ( param , & cond_val ) ;
2006-05-10 21:34:57 +00:00
2007-04-16 20:56:19 +00:00
grfmsg ( 7 , " SkipIf: Test condtype %d, param 0x%08X, condval 0x%08X " , condtype , param_val , cond_val ) ;
2006-12-04 13:45:20 +00:00
2007-10-30 17:24:04 +00:00
/*
* Parameter ( variable in specs ) 0x88 can only have GRF ID checking
2010-10-17 20:29:25 +00:00
* conditions , except conditions 0x0B , 0x0C ( cargo availability ) and
* 0x0D , 0x0E ( Rail type availability ) as those ignore the parameter .
* So , when the condition type is one of those , the specific variable
* 0x88 code is skipped , so the " general " code for the cargo
* availability conditions kicks in .
2007-10-30 17:24:04 +00:00
*/
2010-10-17 20:29:25 +00:00
if ( param = = 0x88 & & ( condtype < 0x0B | | condtype > 0x0E ) ) {
2007-01-01 01:40:56 +00:00
/* GRF ID checks */
2006-12-04 13:45:20 +00:00
2009-01-16 19:53:52 +00:00
GRFConfig * c = GetGRFConfig ( cond_val , mask ) ;
2008-06-19 10:20:45 +00:00
2011-08-21 12:05:47 +00:00
if ( c ! = NULL & & HasBit ( c - > flags , GCF_STATIC ) & & ! HasBit ( _cur . grfconfig - > flags , GCF_STATIC ) & & _networking ) {
2008-06-19 10:20:45 +00:00
DisableStaticNewGRFInfluencingNonStaticNewGRFs ( c ) ;
c = NULL ;
}
2006-12-04 13:45:20 +00:00
2007-01-01 01:40:56 +00:00
if ( condtype ! = 10 & & c = = NULL ) {
2007-04-16 20:56:19 +00:00
grfmsg ( 7 , " SkipIf: GRFID 0x%08X unknown, skipping test " , BSWAP32 ( cond_val ) ) ;
2007-01-01 01:40:56 +00:00
return ;
2006-12-04 13:45:20 +00:00
}
2007-01-01 01:40:56 +00:00
switch ( condtype ) {
2007-10-30 17:24:04 +00:00
/* Tests 0x06 to 0x0A are only for param 0x88, GRFID checks */
case 0x06 : // Is GRFID active?
2007-03-06 19:33:28 +00:00
result = c - > status = = GCS_ACTIVATED ;
2007-01-01 01:40:56 +00:00
break ;
2006-12-04 13:45:20 +00:00
2007-10-30 17:24:04 +00:00
case 0x07 : // Is GRFID non-active?
2007-03-06 19:33:28 +00:00
result = c - > status ! = GCS_ACTIVATED ;
2007-01-01 01:40:56 +00:00
break ;
2007-10-30 17:24:04 +00:00
case 0x08 : // GRFID is not but will be active?
2007-03-06 19:33:28 +00:00
result = c - > status = = GCS_INITIALISED ;
2007-01-01 01:40:56 +00:00
break ;
2007-10-30 17:24:04 +00:00
case 0x09 : // GRFID is or will be active?
2007-03-06 19:33:28 +00:00
result = c - > status = = GCS_ACTIVATED | | c - > status = = GCS_INITIALISED ;
2007-01-01 01:40:56 +00:00
break ;
2007-10-30 17:24:04 +00:00
case 0x0A : // GRFID is not nor will be active
2007-01-01 01:40:56 +00:00
/* This is the only condtype that doesn't get ignored if the GRFID is not found */
2007-03-06 19:33:28 +00:00
result = c = = NULL | | c - > flags = = GCS_DISABLED | | c - > status = = GCS_NOT_FOUND ;
2007-01-01 01:40:56 +00:00
break ;
2007-10-30 17:24:04 +00:00
default : grfmsg ( 1 , " SkipIf: Unsupported GRF condition type %02X. Ignoring " , condtype ) ; return ;
2006-12-04 13:45:20 +00:00
}
2007-01-01 01:40:56 +00:00
} else {
/* Parameter or variable tests */
switch ( condtype ) {
2007-10-30 17:24:04 +00:00
case 0x00 : result = ! ! ( param_val & ( 1 < < cond_val ) ) ;
2007-01-01 01:40:56 +00:00
break ;
2007-10-30 17:24:04 +00:00
case 0x01 : result = ! ( param_val & ( 1 < < cond_val ) ) ;
2007-01-01 01:40:56 +00:00
break ;
2007-10-30 17:24:04 +00:00
case 0x02 : result = ( param_val & mask ) = = cond_val ;
2007-01-01 01:40:56 +00:00
break ;
2007-10-30 17:24:04 +00:00
case 0x03 : result = ( param_val & mask ) ! = cond_val ;
2007-01-01 01:40:56 +00:00
break ;
2007-10-30 17:24:04 +00:00
case 0x04 : result = ( param_val & mask ) < cond_val ;
2007-01-01 01:40:56 +00:00
break ;
2007-10-30 17:24:04 +00:00
case 0x05 : result = ( param_val & mask ) > cond_val ;
2007-01-01 01:40:56 +00:00
break ;
2007-10-30 17:24:04 +00:00
case 0x0B : result = GetCargoIDByLabel ( BSWAP32 ( cond_val ) ) = = CT_INVALID ;
2007-05-19 19:13:12 +00:00
break ;
2007-10-30 17:24:04 +00:00
case 0x0C : result = GetCargoIDByLabel ( BSWAP32 ( cond_val ) ) ! = CT_INVALID ;
2007-05-19 19:13:12 +00:00
break ;
2009-02-08 18:29:22 +00:00
case 0x0D : result = GetRailTypeByLabel ( BSWAP32 ( cond_val ) ) = = INVALID_RAILTYPE ;
break ;
case 0x0E : result = GetRailTypeByLabel ( BSWAP32 ( cond_val ) ) ! = INVALID_RAILTYPE ;
break ;
2006-12-04 13:45:20 +00:00
2007-10-30 17:24:04 +00:00
default : grfmsg ( 1 , " SkipIf: Unsupported condition type %02X. Ignoring " , condtype ) ; return ;
2007-01-01 01:40:56 +00:00
}
2004-11-12 15:36:49 +00:00
}
2004-08-09 17:04:08 +00:00
2005-03-30 12:30:24 +00:00
if ( ! result ) {
2007-04-16 20:56:19 +00:00
grfmsg ( 2 , " SkipIf: Not skipping sprites, test was false " ) ;
2004-11-12 15:36:49 +00:00
return ;
2004-11-12 17:27:30 +00:00
}
2010-01-23 13:17:30 +00:00
uint8 numsprites = buf - > ReadByte ( ) ;
2006-04-15 21:27:59 +00: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 . */
2007-03-10 09:55:11 +00:00
GRFLabel * choice = NULL ;
2011-08-21 12:05:47 +00:00
for ( GRFLabel * label = _cur . grffile - > label ; label ! = NULL ; label = label - > next ) {
2006-04-15 21:27:59 +00:00
if ( label - > label ! = numsprites ) continue ;
/* Remember a goto before the current line */
if ( choice = = NULL ) choice = label ;
/* If we find a label here, this is definitely good */
2011-08-21 12:05:47 +00:00
if ( label - > nfo_line > _cur . nfo_line ) {
2006-04-15 21:27:59 +00:00
choice = label ;
break ;
}
}
if ( choice ! = NULL ) {
2007-04-16 20:56:19 +00:00
grfmsg ( 2 , " SkipIf: Jumping to label 0x%0X at line %d, test was true " , choice - > label , choice - > nfo_line ) ;
2006-04-15 21:27:59 +00:00
FioSeekTo ( choice - > pos , SEEK_SET ) ;
2011-08-21 12:05:47 +00:00
_cur . nfo_line = choice - > nfo_line ;
2006-04-15 21:27:59 +00:00
return ;
}
2007-04-16 20:56:19 +00:00
grfmsg ( 2 , " SkipIf: Skipping %d sprites, test was true " , numsprites ) ;
2011-08-21 12:05:47 +00:00
_cur . skip_sprites = numsprites ;
if ( _cur . skip_sprites = = 0 ) {
2004-11-12 15:36:49 +00: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 12:05:47 +00:00
_cur . skip_sprites = - 1 ;
2007-02-21 17:54:30 +00:00
/* If an action 8 hasn't been encountered yet, disable the grf. */
2011-08-21 12:05:47 +00:00
if ( _cur . grfconfig - > status ! = ( _cur . stage < GLS_RESERVE ? GCS_INITIALISED : GCS_ACTIVATED ) ) {
2011-05-11 20:22:04 +00:00
DisableGrf ( ) ;
2007-06-17 11:01:58 +00:00
}
2004-08-09 17:04:08 +00:00
}
}
2006-12-10 21:39:38 +00:00
2007-01-03 11:33:54 +00:00
/* Action 0x08 (GLS_FILESCAN) */
2010-01-22 13:02:15 +00:00
static void ScanInfo ( ByteReader * buf )
2006-12-04 08:30:04 +00:00
{
2011-01-15 21:13:47 +00:00
uint8 grf_version = buf - > ReadByte ( ) ;
uint32 grfid = buf - > ReadDWord ( ) ;
const char * name = buf - > ReadString ( ) ;
2006-12-04 08:30:04 +00:00
2011-08-21 12:05:47 +00:00
_cur . grfconfig - > ident . grfid = grfid ;
2006-12-10 21:39:38 +00:00
2011-02-14 21:41:52 +00:00
if ( grf_version < 2 | | grf_version > 7 ) {
2011-08-21 12:05:47 +00:00
SetBit ( _cur . grfconfig - > flags , GCF_INVALID ) ;
DEBUG ( grf , 0 , " %s: NewGRF \" %s \" (GRFID %08X) uses GRF version %d, which is incompatible with this version of OpenTTD. " , _cur . grfconfig - > filename , name , BSWAP32 ( grfid ) , grf_version ) ;
2011-01-15 21:13:47 +00:00
}
2006-12-12 19:38:41 +00:00
/* GRF IDs starting with 0xFF are reserved for internal TTDPatch use */
2011-08-21 12:05:47 +00:00
if ( GB ( grfid , 24 , 8 ) = = 0xFF ) SetBit ( _cur . grfconfig - > flags , GCF_SYSTEM ) ;
2006-12-12 19:38:41 +00:00
2011-08-21 12:05:47 +00:00
AddGRFTextToList ( & _cur . grfconfig - > name - > text , 0x7F , grfid , name ) ;
2006-12-10 21:39:38 +00:00
2010-01-22 13:02:15 +00:00
if ( buf - > HasData ( ) ) {
2010-01-23 13:17:30 +00:00
const char * info = buf - > ReadString ( ) ;
2011-08-21 12:05:47 +00:00
AddGRFTextToList ( & _cur . grfconfig - > info - > text , 0x7F , grfid , info ) ;
2006-12-10 21:39:38 +00:00
}
2006-12-04 08:30:04 +00:00
2006-12-12 19:38:41 +00:00
/* GLS_INFOSCAN only looks for the action 8, so we can skip the rest of the file */
2011-08-21 12:05:47 +00:00
_cur . skip_sprites = - 1 ;
2006-12-04 08:30:04 +00:00
}
2006-04-23 18:03:55 +00:00
/* Action 0x08 */
2010-01-22 13:02:15 +00:00
static void GRFInfo ( ByteReader * buf )
2004-08-09 17:04:08 +00: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 15:36:49 +00:00
2010-01-23 13:17:30 +00:00
uint8 version = buf - > ReadByte ( ) ;
uint32 grfid = buf - > ReadDWord ( ) ;
const char * name = buf - > ReadString ( ) ;
2004-11-12 22:28:19 +00:00
2011-08-21 12:05:47 +00:00
if ( _cur . stage < GLS_RESERVE & & _cur . grfconfig - > status ! = GCS_UNKNOWN ) {
2011-05-11 20:22:04 +00:00
DisableGrf ( STR_NEWGRF_ERROR_MULTIPLE_ACTION_8 ) ;
2009-01-10 19:30:40 +00:00
return ;
}
2011-08-21 12:05:47 +00:00
if ( _cur . grffile - > grfid ! = grfid ) {
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 ) ) ;
_cur . grffile - > grfid = grfid ;
2010-11-10 17:04:32 +00:00
}
2011-08-21 12:05:47 +00:00
_cur . grffile - > grf_version = version ;
_cur . grfconfig - > status = _cur . stage < GLS_RESERVE ? GCS_INITIALISED : GCS_ACTIVATED ;
2004-11-12 22:28:19 +00:00
2006-05-01 21:45:35 +00:00
/* Do swap the GRFID for displaying purposes since people expect that */
2011-08-21 12:05:47 +00:00
DEBUG ( grf , 1 , " GRFInfo: Loaded GRFv%d set %08X - %s (palette: %s, version: %i) " , version , BSWAP32 ( grfid ) , name , ( _cur . grfconfig - > palette & GRFP_USE_MASK ) ? " Windows " : " DOS " , _cur . grfconfig - > version ) ;
2004-08-09 17:04:08 +00:00
}
2006-04-23 18:03:55 +00:00
/* Action 0x0A */
2010-01-22 13:02:15 +00:00
static void SpriteReplace ( ByteReader * buf )
2004-08-09 17:04:08 +00: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 18:47:19 +00:00
2010-01-23 13:17:30 +00:00
uint8 num_sets = buf - > ReadByte ( ) ;
2004-11-12 18:47:19 +00:00
2007-03-10 09:55:11 +00:00
for ( uint i = 0 ; i < num_sets ; i + + ) {
2010-01-23 13:17:30 +00:00
uint8 num_sprites = buf - > ReadByte ( ) ;
uint16 first_sprite = buf - > ReadWord ( ) ;
2004-11-14 13:59:11 +00:00
2006-12-26 17:36:18 +00:00
grfmsg ( 2 , " SpriteReplace: [Set %d] Changing %d sprites, beginning with %d " ,
2005-08-15 11:39:13 +00:00
i , num_sprites , first_sprite
) ;
2004-11-12 18:47:19 +00:00
2007-03-10 09:55:11 +00:00
for ( uint j = 0 ; j < num_sprites ; j + + ) {
2008-01-03 02:39:06 +00:00
int load_index = first_sprite + j ;
2011-08-21 12:05:47 +00:00
_cur . nfo_line + + ;
LoadNextSprite ( load_index , _cur . file_index , _cur . nfo_line ) ; // XXX
2008-01-03 02:39:06 +00:00
2008-01-24 14:49:40 +00: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 18:47:19 +00:00
}
}
2004-08-09 17:04:08 +00:00
}
2007-04-26 06:49:32 +00:00
/* Action 0x0A (SKIP) */
2010-01-22 13:02:15 +00:00
static void SkipActA ( ByteReader * buf )
2007-04-26 06:49:32 +00:00
{
2010-01-23 13:17:30 +00:00
uint8 num_sets = buf - > ReadByte ( ) ;
2007-04-26 06:49:32 +00:00
for ( uint i = 0 ; i < num_sets ; i + + ) {
/* Skip the sprites this replaces */
2011-08-21 12:05:47 +00:00
_cur . skip_sprites + = buf - > ReadByte ( ) ;
2007-04-26 06:49:32 +00:00
/* But ignore where they go */
2010-01-23 13:17:30 +00:00
buf - > ReadWord ( ) ;
2007-04-26 06:49:32 +00:00
}
2011-08-21 12:05:47 +00:00
grfmsg ( 3 , " SkipActA: Skipping %d sprites " , _cur . skip_sprites ) ;
2007-04-26 06:49:32 +00:00
}
2006-05-02 09:31:59 +00:00
/* Action 0x0B */
2010-01-22 13:02:15 +00:00
static void GRFLoadError ( ByteReader * buf )
2004-08-09 17:04:08 +00:00
{
/* <0B> <severity> <language-id> <message-id> [<message...> 00] [<data...>] 00 [<parnum>]
*
* B severity 00 : notice , contine loading grf file
* 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 16:34:37 +00: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 14:59:05 +00:00
STR_NEWGRF_ERROR_LOAD_AFTER ,
STR_NEWGRF_ERROR_OTTD_VERSION_NUMBER ,
2004-08-09 17:04:08 +00:00
} ;
2006-12-26 17:22:47 +00:00
2007-02-24 16:34:37 +00: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 17:22:47 +00:00
} ;
2004-08-09 17:04:08 +00:00
2007-02-24 16:34:37 +00:00
/* For now we can only show one message per newgrf file. */
2011-08-21 12:05:47 +00:00
if ( _cur . grfconfig - > error ! = NULL ) return ;
2007-02-20 17:52:43 +00:00
2010-01-23 13:17:30 +00:00
byte severity = buf - > ReadByte ( ) ;
byte lang = buf - > ReadByte ( ) ;
byte message_id = buf - > ReadByte ( ) ;
2007-02-20 17:52:43 +00:00
2007-06-12 13:22:14 +00:00
/* Skip the error if it isn't valid for the current language. */
2011-08-21 12:05:47 +00:00
if ( ! CheckGrfLangID ( lang , _cur . grffile - > grf_version ) ) return ;
2007-06-12 13:22:14 +00:00
2007-02-21 17:20:44 +00:00
/* Skip the error until the activation stage unless bit 7 of the severity
* is set . */
2011-08-21 12:05:47 +00:00
if ( ! HasBit ( severity , 7 ) & & _cur . stage = = GLS_INIT ) {
grfmsg ( 7 , " GRFLoadError: Skipping non-fatal GRFLoadError in stage %d " , _cur . stage ) ;
2004-11-17 02:01:12 +00:00
return ;
2006-04-19 07:50:16 +00:00
}
2007-11-19 21:32:20 +00:00
ClrBit ( severity , 7 ) ;
2007-02-21 17:20:44 +00:00
if ( severity > = lengthof ( sevstr ) ) {
2007-02-24 16:34:37 +00:00
grfmsg ( 7 , " GRFLoadError: Invalid severity id %d. Setting to 2 (non-fatal error). " , severity ) ;
2007-02-21 17:20:44 +00: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 20:22:04 +00:00
DisableGrf ( ) ;
2007-02-21 17:20:44 +00:00
}
if ( message_id > = lengthof ( msgstr ) & & message_id ! = 0xFF ) {
2007-02-24 16:34:37 +00:00
grfmsg ( 7 , " GRFLoadError: Invalid message id. " ) ;
2007-02-21 17:20:44 +00:00
return ;
}
2010-01-22 13:02:15 +00:00
if ( buf - > Remaining ( ) < = 1 ) {
2007-02-24 16:34:37 +00:00
grfmsg ( 7 , " GRFLoadError: No message data supplied. " ) ;
2007-02-21 17:20:44 +00:00
return ;
}
2004-11-17 02:01:12 +00:00
2010-02-25 15:54:40 +00:00
GRFError * error = new GRFError ( sevstr [ severity ] ) ;
2007-02-24 16:34:37 +00:00
if ( message_id = = 0xFF ) {
/* This is a custom error message. */
2010-01-22 13:02:15 +00:00
if ( buf - > HasData ( ) ) {
2010-01-23 13:17:30 +00:00
const char * message = buf - > ReadString ( ) ;
2007-02-24 16:34:37 +00:00
2011-08-21 12:05:47 +00:00
error - > custom_message = TranslateTTDPatchCodes ( _cur . grffile - > grfid , lang , message ) ;
2010-01-22 13:02:15 +00:00
} else {
grfmsg ( 7 , " GRFLoadError: No custom message supplied. " ) ;
error - > custom_message = strdup ( " " ) ;
}
2007-02-24 16:34:37 +00:00
} else {
error - > message = msgstr [ message_id ] ;
}
2010-01-22 13:02:15 +00:00
if ( buf - > HasData ( ) ) {
2010-01-23 13:17:30 +00:00
const char * data = buf - > ReadString ( ) ;
2007-02-24 16:34:37 +00:00
2011-08-21 12:05:47 +00:00
error - > data = TranslateTTDPatchCodes ( _cur . grffile - > grfid , lang , data ) ;
2009-12-30 20:31:43 +00:00
} else {
grfmsg ( 7 , " GRFLoadError: No message data supplied. " ) ;
error - > data = strdup ( " " ) ;
2007-02-24 16:34:37 +00:00
}
2007-02-21 23:18:08 +00:00
2007-02-24 16:34:37 +00:00
/* Only two parameter numbers can be used in the string. */
uint i = 0 ;
2010-01-22 13:02:15 +00:00
for ( ; i < 2 & & buf - > HasData ( ) ; i + + ) {
2010-01-23 13:17:30 +00:00
uint param_number = buf - > ReadByte ( ) ;
2011-08-21 12:05:47 +00:00
error - > param_value [ i ] = _cur . grffile - > GetParam ( param_number ) ;
2007-02-21 23:18:08 +00:00
}
2007-02-24 16:34:37 +00:00
error - > num_params = i ;
2007-02-21 23:18:08 +00:00
2011-08-21 12:05:47 +00:00
_cur . grfconfig - > error = error ;
2004-08-09 17:04:08 +00:00
}
2006-05-02 09:31:59 +00:00
/* Action 0x0C */
2010-01-22 13:02:15 +00:00
static void GRFComment ( ByteReader * buf )
2004-08-09 17:04:08 +00:00
{
/* <0C> [<ignored...>]
*
* V ignored Anything following the 0 C is ignored */
2006-04-15 21:27:59 +00:00
2010-01-22 13:02:15 +00:00
if ( ! buf - > HasData ( ) ) return ;
2006-04-15 21:27:59 +00:00
2010-01-23 13:17:30 +00:00
const char * text = buf - > ReadString ( ) ;
2010-01-22 13:02:15 +00:00
grfmsg ( 2 , " GRFComment: %s " , text ) ;
2004-08-09 17:04:08 +00:00
}
2007-01-04 19:24:42 +00:00
/* Action 0x0D (GLS_SAFETYSCAN) */
2010-01-22 13:02:15 +00:00
static void SafeParamSet ( ByteReader * buf )
2007-01-04 19:24:42 +00:00
{
2010-01-23 13:17:30 +00:00
uint8 target = buf - > ReadByte ( ) ;
2007-01-04 19:24:42 +00:00
/* Only writing GRF parameters is considered safe */
if ( target < 0x80 ) return ;
/* 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 12:05:47 +00:00
SetBit ( _cur . grfconfig - > flags , GCF_UNSAFE ) ;
2007-01-04 19:24:42 +00:00
/* Skip remainder of GRF */
2011-08-21 12:05:47 +00:00
_cur . skip_sprites = - 1 ;
2007-01-04 19:24:42 +00:00
}
2007-04-20 21:19:28 +00:00
static uint32 GetPatchVariable ( uint8 param )
{
switch ( param ) {
/* start year - 1920 */
2008-05-29 15:13:28 +00:00
case 0x0B : return max ( _settings_game . game_creation . starting_year , ORIGINAL_BASE_YEAR ) - ORIGINAL_BASE_YEAR ;
2008-01-23 17:08:35 +00:00
2007-04-20 21:19:28 +00:00
/* freight trains weight factor */
2008-05-29 15:13:28 +00:00
case 0x0E : return _settings_game . vehicle . freight_trains ;
2008-01-23 17:08:35 +00:00
2007-04-20 21:19:28 +00:00
/* empty wagon speed increase */
case 0x0F : return 0 ;
2008-01-23 17:08:35 +00:00
2008-02-27 21:33:52 +00: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 15:13:28 +00:00
switch ( _settings_game . vehicle . plane_speed ) {
2008-02-27 21:33:52 +00:00
default :
case 4 : return 1 ;
case 3 : return 2 ;
case 2 : return 2 ;
case 1 : return 4 ;
}
2008-01-23 17:08:35 +00:00
2009-02-09 02:57:15 +00:00
/* 2CC colourmap base sprite */
2007-04-20 21:19:28 +00:00
case 0x11 : return SPR_2CCMAP_BASE ;
2008-01-23 17:08:35 +00: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 ;
2008-01-23 17:40:37 +00:00
byte log_X = MapLogX ( ) - 6 ; // substraction is required to make the minimal size (64) zero based
2008-01-23 17:08:35 +00:00
byte log_Y = MapLogY ( ) - 6 ;
byte max_edge = max ( log_X , log_Y ) ;
if ( log_X = = log_Y ) { // we have a squared map, since both edges are identical
2009-07-16 10:13:33 +00:00
SetBit ( map_bits , 0 ) ;
2008-01-23 17:08:35 +00:00
} else {
if ( max_edge = = log_Y ) SetBit ( map_bits , 1 ) ; // edge Y been the biggest, mark it
}
return ( map_bits < < 24 ) | ( min ( log_X , log_Y ) < < 20 ) | ( max_edge < < 16 ) |
( log_X < < 12 ) | ( log_Y < < 8 ) | ( log_X + log_Y ) ;
}
2007-04-20 21:19:28 +00:00
default :
grfmsg ( 2 , " ParamSet: Unknown Patch variable 0x%02X. " , param ) ;
return 0 ;
}
}
2007-06-02 08:56:21 +00: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 12:05:47 +00:00
return grm [ _cur . grffile - > GetParam ( target ) ] ;
2007-06-02 08:56:21 +00:00
}
/* With an operation of 2 or 3, we want to reserve a specific block of IDs */
2011-08-21 12:05:47 +00:00
if ( op = = 2 | | op = = 3 ) start = _cur . grffile - > GetParam ( target ) ;
2007-06-02 08:56:21 +00: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 ) {
grfmsg ( 2 , " ParamSet: GRM: Reserving %d %s at %d " , count , type , start ) ;
2011-08-21 12:05:47 +00:00
for ( uint i = 0 ; i < count ; i + + ) grm [ start + i ] = _cur . grffile - > grfid ;
2007-06-02 08:56:21 +00:00
}
return start ;
}
/* Unable to allocate */
if ( op ! = 4 & & op ! = 5 ) {
/* Deactivate GRF */
grfmsg ( 0 , " ParamSet: GRM: Unable to allocate %d %s, deactivating " , count , type ) ;
2011-05-11 20:23:37 +00:00
DisableGrf ( STR_NEWGRF_ERROR_GRM_FAILED ) ;
2007-06-02 08:56:21 +00:00
return UINT_MAX ;
}
grfmsg ( 1 , " ParamSet: GRM: Unable to allocate %d %s " , count , type ) ;
return UINT_MAX ;
}
2004-11-12 15:15:56 +00:00
/* Action 0x0D */
2010-01-22 13:02:15 +00:00
static void ParamSet ( ByteReader * buf )
2004-08-09 17:04:08 +00: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 18:57:46 +00:00
2010-01-23 13:17:30 +00:00
uint8 target = buf - > ReadByte ( ) ;
uint8 oper = buf - > ReadByte ( ) ;
uint32 src1 = buf - > ReadByte ( ) ;
uint32 src2 = buf - > ReadByte ( ) ;
2004-11-12 18:57:46 +00:00
2007-03-10 09:55:11 +00:00
uint32 data = 0 ;
2010-01-23 13:17:30 +00:00
if ( buf - > Remaining ( ) > = 4 ) data = buf - > ReadDWord ( ) ;
2004-11-14 13:59:11 +00:00
2004-11-12 18:57:46 +00: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 21:02:30 +00:00
if ( HasBit ( oper , 7 ) ) {
2011-08-21 12:05:47 +00:00
if ( target < 0x80 & & target < _cur . grffile - > param_end ) {
2007-04-16 20:56:19 +00:00
grfmsg ( 7 , " ParamSet: Param %u already defined, skipping " , target ) ;
2004-11-12 18:57:46 +00:00
return ;
2006-05-10 21:41:16 +00:00
}
2007-03-10 09:55:11 +00:00
oper = GB ( oper , 0 , 7 ) ;
2004-11-12 18:57:46 +00:00
}
2006-05-10 21:55:44 +00:00
if ( src2 = = 0xFE ) {
if ( GB ( data , 0 , 8 ) = = 0xFF ) {
if ( data = = 0x0000FFFF ) {
/* Patch variables */
2007-04-20 21:19:28 +00:00
src1 = GetPatchVariable ( src1 ) ;
2006-05-10 21:55:44 +00:00
} else {
/* GRF Resource Management */
2008-05-05 19:02:10 +00:00
uint8 op = src1 ;
uint8 feature = GB ( data , 8 , 8 ) ;
uint16 count = GB ( data , 16 , 16 ) ;
2011-08-21 12:05:47 +00:00
if ( _cur . stage = = GLS_RESERVE ) {
2008-05-05 19:02:10 +00:00
if ( feature = = 0x08 ) {
/* General sprites */
if ( op = = 0 ) {
/* Check if the allocated sprites will fit below the original sprite limit */
2011-08-21 12:05:47 +00:00
if ( _cur . spriteid + count > = 16384 ) {
2008-05-05 19:02:10 +00:00
grfmsg ( 0 , " ParamSet: GRM: Unable to allocate %d sprites; try changing NewGRF order " , count ) ;
2011-05-11 20:23:37 +00:00
DisableGrf ( STR_NEWGRF_ERROR_GRM_FAILED ) ;
2008-05-05 19:02:10 +00:00
return ;
}
2006-10-27 13:15:21 +00:00
2008-05-05 19:02:10 +00:00
/* Reserve space at the current sprite ID */
2011-08-21 12:05:47 +00:00
grfmsg ( 4 , " ParamSet: GRM: Allocated %d sprites at %d " , count , _cur . spriteid ) ;
_grm_sprites [ GRFLocation ( _cur . grffile - > grfid , _cur . nfo_line ) ] = _cur . spriteid ;
_cur . spriteid + = count ;
2008-05-05 19:02:10 +00:00
}
}
/* Ignore GRM result during reservation */
src1 = 0 ;
2011-08-21 12:05:47 +00:00
} else if ( _cur . stage = = GLS_ACTIVATION ) {
2006-10-27 13:15:21 +00:00
switch ( feature ) {
2007-03-21 03:06:21 +00:00
case 0x00 : // Trains
case 0x01 : // Road Vehicles
case 0x02 : // Ships
case 0x03 : // Aircraft
2008-05-29 15:13:28 +00:00
if ( ! _settings_game . vehicle . dynamic_engines ) {
2008-04-29 21:31:29 +00:00
src1 = PerformGRM ( & _grm_engines [ _engine_offsets [ feature ] ] , _engine_counts [ feature ] , count , op , target , " vehicles " ) ;
2011-08-21 12:05:47 +00:00
if ( _cur . skip_sprites = = - 1 ) return ;
2008-04-29 21:31:29 +00:00
} else {
2009-03-15 00:32:18 +00:00
/* GRM does not apply for dynamic engine allocation. */
2008-04-29 21:31:29 +00:00
switch ( op ) {
case 2 :
case 3 :
2011-08-21 12:05:47 +00:00
src1 = _cur . grffile - > GetParam ( target ) ;
2008-04-29 21:31:29 +00:00
break ;
default :
src1 = 0 ;
break ;
}
}
2006-10-27 13:15:21 +00:00
break ;
2007-03-21 03:06:21 +00:00
case 0x08 : // General sprites
2006-10-27 13:15:21 +00:00
switch ( op ) {
case 0 :
2008-05-05 19:02:10 +00:00
/* Return space reserved during reservation stage */
2011-08-21 12:05:47 +00:00
src1 = _grm_sprites [ GRFLocation ( _cur . grffile - > grfid , _cur . nfo_line ) ] ;
2008-05-05 19:02:10 +00:00
grfmsg ( 4 , " ParamSet: GRM: Using pre-allocated sprites at %d " , src1 ) ;
2006-10-27 13:15:21 +00:00
break ;
case 1 :
2011-08-21 12:05:47 +00:00
src1 = _cur . spriteid ;
2006-10-27 13:15:21 +00:00
break ;
default :
2007-04-16 20:56:19 +00:00
grfmsg ( 1 , " ParamSet: GRM: Unsupported operation %d for general sprites " , op ) ;
2006-10-27 13:15:21 +00:00
return ;
}
break ;
2007-06-02 08:56:21 +00:00
case 0x0B : // Cargo
2007-10-07 08:48:31 +00:00
/* There are two ranges: one for cargo IDs and one for cargo bitmasks */
src1 = PerformGRM ( _grm_cargos , NUM_CARGO * 2 , count , op , target , " cargos " ) ;
2011-08-21 12:05:47 +00:00
if ( _cur . skip_sprites = = - 1 ) return ;
2007-06-02 08:56:21 +00:00
break ;
2007-04-16 20:56:19 +00:00
default : grfmsg ( 1 , " ParamSet: GRM: Unsupported feature 0x%X " , feature ) ; return ;
2006-10-27 13:15:21 +00:00
}
2008-05-05 19:02:10 +00:00
} else {
/* Ignore GRM during initialization */
src1 = 0 ;
2006-10-27 13:15:21 +00:00
}
2006-05-10 21:55:44 +00:00
}
} else {
/* Read another GRF File's parameter */
const GRFFile * file = GetFileByGRFID ( data ) ;
2008-06-19 10:20:45 +00:00
GRFConfig * c = GetGRFConfig ( data ) ;
2011-08-21 12:05:47 +00:00
if ( c ! = NULL & & HasBit ( c - > flags , GCF_STATIC ) & & ! HasBit ( _cur . grfconfig - > flags , GCF_STATIC ) & & _networking ) {
2008-06-19 10:20:45 +00:00
/* Disable the read GRF if it is a static NewGRF. */
DisableStaticNewGRFInfluencingNonStaticNewGRFs ( c ) ;
src1 = 0 ;
2010-01-03 19:29:56 +00:00
} else if ( file = = NULL | | ( c ! = NULL & & c - > status = = GCS_DISABLED ) ) {
2006-05-10 21:55:44 +00:00
src1 = 0 ;
2010-08-28 21:45:44 +00:00
} else if ( src1 = = 0xFE ) {
src1 = c - > version ;
2006-05-10 21:55:44 +00:00
} else {
2010-01-03 19:29:56 +00:00
src1 = file - > GetParam ( src1 ) ;
2006-05-10 21:55:44 +00:00
}
}
2004-11-12 18:57:46 +00:00
} else {
2006-05-10 21:55:44 +00: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 . */
2006-05-11 09:54:11 +00:00
src1 = ( src1 = = 0xFF ) ? data : GetParamVal ( src1 , NULL ) ;
src2 = ( src2 = = 0xFF ) ? data : GetParamVal ( src2 , NULL ) ;
2004-11-12 18:57:46 +00:00
}
/* TODO: You can access the parameters of another GRF file by using
* source2 = FE , source1 = the other GRF ' s parameter number and data = GRF
* ID . This is only valid with operation 00 ( set ) . If the GRF ID
* cannot be found , a value of 0 is used for the parameter value
* instead . */
2007-03-10 09:55:11 +00:00
uint32 res ;
2004-11-12 18:57:46 +00:00
switch ( oper ) {
case 0x00 :
2005-08-15 21:19:28 +00:00
res = src1 ;
2004-11-12 18:57:46 +00:00
break ;
2005-08-15 21:19:28 +00:00
2004-11-12 18:57:46 +00:00
case 0x01 :
2005-08-15 21:19:28 +00:00
res = src1 + src2 ;
2004-11-12 18:57:46 +00:00
break ;
2005-08-15 21:19:28 +00:00
2004-11-12 18:57:46 +00:00
case 0x02 :
2005-08-15 21:19:28 +00:00
res = src1 - src2 ;
2004-11-12 18:57:46 +00:00
break ;
2005-08-15 21:19:28 +00:00
2004-11-12 18:57:46 +00:00
case 0x03 :
2005-08-15 21:19:28 +00:00
res = src1 * src2 ;
2004-11-12 18:57:46 +00:00
break ;
2005-08-15 21:19:28 +00:00
2004-11-12 18:57:46 +00:00
case 0x04 :
2005-08-15 21:19:28 +00:00
res = ( int32 ) src1 * ( int32 ) src2 ;
2004-11-12 18:57:46 +00:00
break ;
2005-08-15 21:19:28 +00:00
2004-11-12 18:57:46 +00:00
case 0x05 :
2006-06-27 21:25:53 +00:00
if ( ( int32 ) src2 < 0 ) {
2005-08-15 21:19:28 +00:00
res = src1 > > - ( int32 ) src2 ;
2006-06-27 21:25:53 +00:00
} else {
2005-08-15 21:19:28 +00:00
res = src1 < < src2 ;
2006-06-27 21:25:53 +00:00
}
2004-11-12 18:57:46 +00:00
break ;
2005-08-15 21:19:28 +00:00
2004-11-12 18:57:46 +00:00
case 0x06 :
2006-06-27 21:25:53 +00:00
if ( ( int32 ) src2 < 0 ) {
2005-08-15 21:19:28 +00:00
res = ( int32 ) src1 > > - ( int32 ) src2 ;
2006-06-27 21:25:53 +00:00
} else {
2005-08-15 21:19:28 +00:00
res = ( int32 ) src1 < < src2 ;
2006-06-27 21:25:53 +00:00
}
2004-11-12 18:57:46 +00:00
break ;
2005-08-15 21:19:28 +00:00
2007-03-21 03:06:21 +00:00
case 0x07 : // Bitwise AND
2005-09-21 19:07:58 +00:00
res = src1 & src2 ;
break ;
2007-03-21 03:06:21 +00:00
case 0x08 : // Bitwise OR
2005-09-21 19:07:58 +00:00
res = src1 | src2 ;
break ;
2007-03-21 03:06:21 +00:00
case 0x09 : // Unsigned division
2005-09-21 19:07:58 +00:00
if ( src2 = = 0 ) {
res = src1 ;
} else {
res = src1 / src2 ;
}
break ;
2007-03-21 03:06:21 +00:00
case 0x0A : // Signed divison
2005-09-21 19:07:58 +00:00
if ( src2 = = 0 ) {
res = src1 ;
} else {
res = ( int32 ) src1 / ( int32 ) src2 ;
}
break ;
2007-03-21 03:06:21 +00:00
case 0x0B : // Unsigned modulo
2005-09-21 19:07:58 +00:00
if ( src2 = = 0 ) {
res = src1 ;
} else {
res = src1 % src2 ;
}
break ;
2007-03-21 03:06:21 +00:00
case 0x0C : // Signed modulo
2005-09-21 19:07:58 +00:00
if ( src2 = = 0 ) {
res = src1 ;
} else {
res = ( int32 ) src1 % ( int32 ) src2 ;
}
break ;
2006-12-26 17:36:18 +00:00
default : grfmsg ( 0 , " ParamSet: Unknown operation %d, skipping " , oper ) ; return ;
2005-08-15 21:19:28 +00:00
}
switch ( target ) {
case 0x8E : // Y-Offset for train sprites
2011-08-21 12:05:47 +00:00
_cur . grffile - > traininfo_vehicle_pitch = res ;
2005-08-15 21:19:28 +00:00
break ;
2008-08-24 23:10:23 +00: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 15:13:28 +00:00
if ( _settings_game . vehicle . disable_elrails ) {
2008-08-24 23:10:23 +00:00
_railtypes [ RAILTYPE_ELECTRIC ] . cost_multiplier = GB ( res , 0 , 8 ) ;
_railtypes [ RAILTYPE_MONO ] . cost_multiplier = GB ( res , 8 , 8 ) ;
2007-10-14 21:20:12 +00:00
} else {
2008-08-24 23:10:23 +00:00
_railtypes [ RAILTYPE_ELECTRIC ] . cost_multiplier = GB ( res , 8 , 8 ) ;
_railtypes [ RAILTYPE_MONO ] . cost_multiplier = GB ( res , 16 , 8 ) ;
2007-10-14 21:20:12 +00:00
}
2008-08-24 23:10:23 +00:00
_railtypes [ RAILTYPE_MAGLEV ] . cost_multiplier = GB ( res , 16 , 8 ) ;
2007-10-14 21:20:12 +00:00
break ;
2008-08-24 23:10:23 +00:00
}
2007-10-14 21:20:12 +00:00
/* @todo implement */
2005-08-15 21:19:28 +00:00
case 0x93 : // Tile refresh offset to left
case 0x94 : // Tile refresh offset to right
case 0x95 : // Tile refresh offset upwards
case 0x96 : // Tile refresh offset downwards
case 0x97 : // Snow line height
case 0x99 : // Global ID offset
2006-12-26 17:36:18 +00:00
grfmsg ( 7 , " ParamSet: Skipping unimplemented target 0x%02X " , target ) ;
2005-08-15 21:19:28 +00:00
break ;
2007-03-21 03:06:21 +00:00
case 0x9E : // Miscellaneous GRF features
2006-05-11 12:41:02 +00:00
_misc_grf_features = res ;
2009-07-17 22:00:13 +00:00
2006-05-14 20:58:12 +00:00
/* Set train list engine width */
2011-08-21 12:05:47 +00:00
_cur . grffile - > traininfo_vehicle_width = HasGrfMiscBit ( GMB_TRAIN_WIDTH_32_PIXELS ) ? VEHICLEINFO_FULL_VEHICLE_WIDTH : TRAININFO_DEFAULT_VEHICLE_WIDTH ;
2009-07-17 22:00:13 +00:00
/* Remove the local flags from the global flags */
ClrBit ( _misc_grf_features , GMB_TRAIN_WIDTH_32_PIXELS ) ;
2006-05-11 12:41:02 +00:00
break ;
2008-02-12 15:43:10 +00:00
case 0x9F : // locale-dependent settings
grfmsg ( 7 , " ParamSet: Skipping unimplemented target 0x%02X " , target ) ;
break ;
2005-08-15 21:19:28 +00:00
default :
if ( target < 0x80 ) {
2011-08-21 12:05:47 +00:00
_cur . grffile - > param [ target ] = res ;
2010-01-03 19:29:56 +00:00
/* param is zeroed by default */
2011-08-21 12:05:47 +00:00
if ( target + 1U > _cur . grffile - > param_end ) _cur . grffile - > param_end = target + 1 ;
2005-08-15 21:19:28 +00:00
} else {
2006-12-26 17:36:18 +00:00
grfmsg ( 7 , " ParamSet: Skipping unknown target 0x%02X " , target ) ;
2005-08-15 21:19:28 +00:00
}
break ;
2004-11-12 18:57:46 +00:00
}
2004-08-09 17:04:08 +00:00
}
2007-01-03 11:33:54 +00:00
/* Action 0x0E (GLS_SAFETYSCAN) */
2010-01-22 13:02:15 +00:00
static void SafeGRFInhibit ( ByteReader * buf )
2007-01-03 11:33:54 +00:00
{
/* <0E> <num> <grfids...>
*
* B num Number of GRFIDs that follow
* D grfids GRFIDs of the files to deactivate */
2010-01-23 13:17:30 +00:00
uint8 num = buf - > ReadByte ( ) ;
2007-01-03 11:33:54 +00:00
2007-03-10 09:55:11 +00:00
for ( uint i = 0 ; i < num ; i + + ) {
2010-01-23 13:17:30 +00:00
uint32 grfid = buf - > ReadDWord ( ) ;
2007-01-03 11:33:54 +00:00
/* GRF is unsafe it if tries to deactivate other GRFs */
2011-08-21 12:05:47 +00:00
if ( grfid ! = _cur . grfconfig - > ident . grfid ) {
SetBit ( _cur . grfconfig - > flags , GCF_UNSAFE ) ;
2007-01-03 11:33:54 +00:00
/* Skip remainder of GRF */
2011-08-21 12:05:47 +00:00
_cur . skip_sprites = - 1 ;
2007-01-03 11:33:54 +00:00
return ;
}
}
}
2006-05-02 09:31:59 +00:00
/* Action 0x0E */
2010-01-22 13:02:15 +00:00
static void GRFInhibit ( ByteReader * buf )
2004-08-09 17:04:08 +00:00
{
/* <0E> <num> <grfids...>
*
* B num Number of GRFIDs that follow
* D grfids GRFIDs of the files to deactivate */
2004-11-12 22:39:00 +00:00
2010-01-23 13:17:30 +00:00
uint8 num = buf - > ReadByte ( ) ;
2004-11-12 22:39:00 +00:00
2007-03-10 09:55:11 +00:00
for ( uint i = 0 ; i < num ; i + + ) {
2010-01-23 13:17:30 +00:00
uint32 grfid = buf - > ReadDWord ( ) ;
2006-12-14 15:14:29 +00:00
GRFConfig * file = GetGRFConfig ( grfid ) ;
2004-11-12 22:39:00 +00:00
/* Unset activation flag */
2011-08-21 12:05:47 +00:00
if ( file ! = NULL & & file ! = _cur . grfconfig ) {
2006-12-26 17:36:18 +00:00
grfmsg ( 2 , " GRFInhibit: Deactivating file '%s' " , file - > filename ) ;
2011-05-11 20:23:37 +00:00
GRFError * error = DisableGrf ( STR_NEWGRF_ERROR_FORCEFULLY_DISABLED , file ) ;
2011-08-21 12:05:47 +00:00
error - > data = strdup ( _cur . grfconfig - > GetName ( ) ) ;
2004-11-12 22:39:00 +00:00
}
}
2004-08-09 17:04:08 +00:00
}
2007-06-18 23:00:55 +00:00
/* Action 0x0F */
2010-01-22 13:02:15 +00:00
static void FeatureTownName ( ByteReader * buf )
2007-06-18 23:00:55 +00: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 12:05:47 +00:00
uint32 grfid = _cur . grffile - > grfid ;
2007-06-18 23:00:55 +00:00
GRFTownName * townname = AddGRFTownName ( grfid ) ;
2010-01-23 13:17:30 +00:00
byte id = buf - > ReadByte ( ) ;
2007-06-18 23:00:55 +00:00
grfmsg ( 6 , " FeatureTownName: definition 0x%02X " , id & 0x7F ) ;
2007-11-19 21:02:30 +00:00
if ( HasBit ( id , 7 ) ) {
2007-06-18 23:00:55 +00:00
/* Final definition */
2007-11-19 21:32:20 +00:00
ClrBit ( id , 7 ) ;
2011-08-21 12:05:47 +00:00
bool new_scheme = _cur . grffile - > grf_version > = 7 ;
2007-06-18 23:00:55 +00:00
2010-01-23 13:17:30 +00:00
byte lang = buf - > ReadByte ( ) ;
2007-06-18 23:00:55 +00:00
byte nb_gen = townname - > nb_gen ;
do {
2007-11-19 21:32:20 +00:00
ClrBit ( lang , 7 ) ;
2007-06-18 23:00:55 +00:00
2010-01-23 13:17:30 +00:00
const char * name = buf - > ReadString ( ) ;
2008-12-24 21:25:09 +00:00
2010-11-13 19:15:26 +00:00
char * lang_name = TranslateTTDPatchCodes ( grfid , lang , name ) ;
2008-12-24 21:25:09 +00:00
grfmsg ( 6 , " FeatureTownName: lang 0x%X -> '%s' " , lang , lang_name ) ;
free ( lang_name ) ;
2007-06-18 23:00:55 +00:00
townname - > name [ nb_gen ] = AddGRFString ( grfid , id , lang , new_scheme , name , STR_UNDEFINED ) ;
2010-01-23 13:17:30 +00:00
lang = buf - > ReadByte ( ) ;
2007-06-18 23:00:55 +00:00
} while ( lang ! = 0 ) ;
townname - > id [ nb_gen ] = id ;
townname - > nb_gen + + ;
}
2010-01-23 13:17:30 +00:00
byte nb = buf - > ReadByte ( ) ;
2009-05-10 17:27:25 +00:00
grfmsg ( 6 , " FeatureTownName: %u parts " , nb ) ;
2007-06-18 23:00:55 +00:00
townname - > nbparts [ id ] = nb ;
townname - > partlist [ id ] = CallocT < NamePartList > ( nb ) ;
for ( int i = 0 ; i < nb ; i + + ) {
2010-01-23 13:17:30 +00:00
byte nbtext = buf - > ReadByte ( ) ;
townname - > partlist [ id ] [ i ] . bitstart = buf - > ReadByte ( ) ;
townname - > partlist [ id ] [ i ] . bitcount = buf - > ReadByte ( ) ;
2007-06-18 23:00:55 +00:00
townname - > partlist [ id ] [ i ] . maxprob = 0 ;
townname - > partlist [ id ] [ i ] . partcount = nbtext ;
townname - > partlist [ id ] [ i ] . parts = CallocT < NamePart > ( nbtext ) ;
grfmsg ( 6 , " FeatureTownName: part %d contains %d texts and will use GB(seed, %d, %d) " , i , nbtext , townname - > partlist [ id ] [ i ] . bitstart , townname - > partlist [ id ] [ i ] . bitcount ) ;
for ( int j = 0 ; j < nbtext ; j + + ) {
2010-01-23 13:17:30 +00:00
byte prob = buf - > ReadByte ( ) ;
2007-06-18 23:00:55 +00:00
2007-11-19 21:02:30 +00:00
if ( HasBit ( prob , 7 ) ) {
2010-01-23 13:17:30 +00:00
byte ref_id = buf - > ReadByte ( ) ;
2007-06-18 23:00:55 +00:00
if ( townname - > nbparts [ ref_id ] = = 0 ) {
grfmsg ( 0 , " FeatureTownName: definition 0x%02X doesn't exist, deactivating " , ref_id ) ;
DelGRFTownName ( grfid ) ;
2011-05-11 20:22:04 +00:00
DisableGrf ( STR_NEWGRF_ERROR_INVALID_ID ) ;
2007-06-18 23:00:55 +00:00
return ;
}
grfmsg ( 6 , " FeatureTownName: part %d, text %d, uses intermediate definition 0x%02X (with probability %d) " , i , j , ref_id , prob & 0x7F ) ;
townname - > partlist [ id ] [ i ] . parts [ j ] . data . id = ref_id ;
} else {
2010-01-23 13:17:30 +00:00
const char * text = buf - > ReadString ( ) ;
2010-11-13 19:15:26 +00:00
townname - > partlist [ id ] [ i ] . parts [ j ] . data . text = TranslateTTDPatchCodes ( grfid , 0 , text ) ;
2007-06-18 23:00:55 +00:00
grfmsg ( 6 , " FeatureTownName: part %d, text %d, '%s' (with probability %d) " , i , j , townname - > partlist [ id ] [ i ] . parts [ j ] . data . text , prob ) ;
}
townname - > partlist [ id ] [ i ] . parts [ j ] . prob = prob ;
townname - > partlist [ id ] [ i ] . maxprob + = GB ( prob , 0 , 7 ) ;
}
grfmsg ( 6 , " FeatureTownName: part %d, total probability %d " , i , townname - > partlist [ id ] [ i ] . maxprob ) ;
}
}
2006-05-02 09:31:59 +00:00
/* Action 0x10 */
2010-01-22 13:02:15 +00:00
static void DefineGotoLabel ( ByteReader * buf )
2006-04-15 21:27:59 +00:00
{
/* <10> <label> [<comment>]
*
* B label The label to define
* V comment Optional comment - ignored */
2010-01-23 13:17:30 +00:00
byte nfo_label = buf - > ReadByte ( ) ;
2006-04-15 21:27:59 +00:00
2007-03-10 09:55:11 +00:00
GRFLabel * label = MallocT < GRFLabel > ( 1 ) ;
2010-01-22 13:02:15 +00:00
label - > label = nfo_label ;
2011-08-21 12:05:47 +00:00
label - > nfo_line = _cur . nfo_line ;
2006-04-15 21:27:59 +00:00
label - > pos = FioGetPos ( ) ;
label - > next = NULL ;
/* Set up a linked list of goto targets which we will search in an Action 0x7/0x9 */
2011-08-21 12:05:47 +00:00
if ( _cur . grffile - > label = = NULL ) {
_cur . grffile - > label = label ;
2006-04-15 21:27:59 +00:00
} else {
/* Attach the label to the end of the list */
GRFLabel * l ;
2011-08-21 12:05:47 +00:00
for ( l = _cur . grffile - > label ; l - > next ! = NULL ; l = l - > next ) { }
2006-04-15 21:27:59 +00:00
l - > next = label ;
}
2006-12-26 17:36:18 +00:00
grfmsg ( 2 , " DefineGotoLabel: GOTO target with label 0x%02X " , label - > label ) ;
2006-04-15 21:27:59 +00:00
}
2004-11-12 19:03:37 +00:00
2006-09-27 18:17:01 +00:00
/* Action 0x11 */
2010-01-22 13:02:15 +00:00
static void GRFSound ( ByteReader * buf )
2006-09-27 18:17:01 +00:00
{
/* <11> <num>
*
* W num Number of sound files that follow */
2010-01-23 13:17:30 +00:00
uint16 num = buf - > ReadWord ( ) ;
2006-09-27 18:17:01 +00:00
2011-08-21 12:05:47 +00:00
_cur . data_blocks = num ;
_cur . data_type = GDT_SOUND ;
2006-09-27 18:17:01 +00:00
2011-08-21 12:05:47 +00:00
if ( _cur . grffile - > sound_offset = = 0 ) {
_cur . grffile - > sound_offset = GetNumSounds ( ) ;
_cur . grffile - > num_sounds = num ;
2010-04-20 05:52:51 +00:00
}
2006-09-27 18:17:01 +00:00
}
2008-01-28 15:55:59 +00:00
/* Action 0x11 (SKIP) */
2010-01-22 13:02:15 +00:00
static void SkipAct11 ( ByteReader * buf )
2008-01-28 15:55:59 +00:00
{
/* <11> <num>
*
* W num Number of sound files that follow */
2011-08-21 12:05:47 +00:00
_cur . skip_sprites = buf - > ReadWord ( ) ;
2008-01-28 15:55:59 +00:00
2011-08-21 12:05:47 +00:00
grfmsg ( 3 , " SkipAct11: Skipping %d sprites " , _cur . skip_sprites ) ;
2008-01-28 15:55:59 +00:00
}
2010-01-22 13:02:15 +00:00
static void ImportGRFSound ( ByteReader * buf )
2006-10-12 07:42:37 +00:00
{
const GRFFile * file ;
2009-05-17 19:27:50 +00:00
SoundEntry * sound = AllocateSound ( ) ;
2010-01-23 13:17:30 +00:00
uint32 grfid = buf - > ReadDWord ( ) ;
SoundID sound_id = buf - > ReadWord ( ) ;
2006-10-12 07:42:37 +00:00
file = GetFileByGRFID ( grfid ) ;
if ( file = = NULL | | file - > sound_offset = = 0 ) {
2006-12-26 17:36:18 +00:00
grfmsg ( 1 , " ImportGRFSound: Source file not available " ) ;
2006-10-12 07:42:37 +00:00
return ;
}
2010-04-20 05:52:51 +00:00
if ( sound_id > = file - > num_sounds ) {
2009-05-17 19:27:50 +00:00
grfmsg ( 1 , " ImportGRFSound: Sound effect %d is invalid " , sound_id ) ;
2006-10-12 07:42:37 +00:00
return ;
}
2009-05-17 19:27:50 +00:00
grfmsg ( 2 , " ImportGRFSound: Copying sound %d (%d) from file %X " , sound_id , file - > sound_offset + sound_id , grfid ) ;
2006-10-12 07:42:37 +00:00
2009-05-17 19:27:50 +00:00
* sound = * GetSound ( file - > sound_offset + sound_id ) ;
2006-10-12 07:42:37 +00:00
/* Reset volume and priority, which TTDPatch doesn't copy */
2009-05-17 19:27:50 +00:00
sound - > volume = 128 ;
sound - > priority = 0 ;
2006-10-12 07:42:37 +00:00
}
/* 'Action 0xFE' */
2010-01-22 13:02:15 +00:00
static void GRFImportBlock ( ByteReader * buf )
2006-10-12 07:42:37 +00:00
{
2011-08-21 12:05:47 +00:00
if ( _cur . data_blocks = = 0 ) {
2006-12-26 17:36:18 +00:00
grfmsg ( 2 , " GRFImportBlock: Unexpected import block, skipping " ) ;
2006-10-12 07:42:37 +00:00
return ;
}
2011-08-21 12:05:47 +00:00
_cur . data_blocks - - ;
2006-10-12 07:42:37 +00:00
/* XXX 'Action 0xFE' isn't really specified. It is only mentioned for
* importing sounds , so this is probably all wrong . . . */
2011-08-21 12:05:47 +00:00
if ( buf - > ReadByte ( ) ! = _cur . data_type ) {
2006-12-26 17:36:18 +00:00
grfmsg ( 1 , " GRFImportBlock: Import type mismatch " ) ;
2006-10-12 07:42:37 +00:00
}
2011-08-21 12:05:47 +00:00
switch ( _cur . data_type ) {
2010-01-22 13:02:15 +00:00
case GDT_SOUND : ImportGRFSound ( buf ) ; break ;
2009-05-26 15:46:24 +00:00
default : NOT_REACHED ( ) ;
2006-10-12 07:42:37 +00:00
}
}
2010-01-22 13:02:15 +00:00
static void LoadGRFSound ( ByteReader * buf )
2006-09-27 18:17:01 +00:00
{
/* Allocate a sound entry. This is done even if the data is not loaded
* so that the indices used elsewhere are still correct . */
2009-05-17 19:27:50 +00:00
SoundEntry * sound = AllocateSound ( ) ;
2006-09-27 18:17:01 +00:00
2010-01-23 13:17:30 +00:00
if ( buf - > ReadDWord ( ) ! = BSWAP32 ( ' RIFF ' ) ) {
2006-12-26 17:36:18 +00:00
grfmsg ( 1 , " LoadGRFSound: Missing RIFF header " ) ;
2006-09-27 18:17:01 +00:00
return ;
}
2010-01-23 13:17:30 +00:00
uint32 total_size = buf - > ReadDWord ( ) ;
2010-01-22 13:02:15 +00:00
if ( total_size > buf - > Remaining ( ) ) {
2009-11-07 18:22:00 +00:00
grfmsg ( 1 , " LoadGRFSound: RIFF was truncated " ) ;
return ;
}
2006-09-27 18:17:01 +00:00
2010-01-23 13:17:30 +00:00
if ( buf - > ReadDWord ( ) ! = BSWAP32 ( ' WAVE ' ) ) {
2006-12-26 17:36:18 +00:00
grfmsg ( 1 , " LoadGRFSound: Invalid RIFF type " ) ;
2006-09-27 18:17:01 +00:00
return ;
}
2009-11-07 18:22:00 +00:00
while ( total_size > = 8 ) {
2010-01-23 13:17:30 +00:00
uint32 tag = buf - > ReadDWord ( ) ;
uint32 size = buf - > ReadDWord ( ) ;
2009-11-07 18:22:00 +00:00
total_size - = 8 ;
if ( total_size < size ) {
grfmsg ( 1 , " LoadGRFSound: Invalid RIFF " ) ;
return ;
}
total_size - = size ;
2006-09-27 18:17:01 +00:00
switch ( tag ) {
2007-03-21 03:06:21 +00:00
case ' tmf ' : // 'fmt '
2006-09-27 18:17:01 +00:00
/* Audio format, must be 1 (PCM) */
2010-01-23 13:17:30 +00:00
if ( size < 16 | | buf - > ReadWord ( ) ! = 1 ) {
2006-12-26 17:36:18 +00:00
grfmsg ( 1 , " LoadGRFSound: Invalid audio format " ) ;
2006-09-27 18:17:01 +00:00
return ;
}
2010-01-23 13:17:30 +00:00
sound - > channels = buf - > ReadWord ( ) ;
sound - > rate = buf - > ReadDWord ( ) ;
buf - > ReadDWord ( ) ;
buf - > ReadWord ( ) ;
sound - > bits_per_sample = buf - > ReadWord ( ) ;
2006-09-27 18:17:01 +00:00
2009-11-07 18:22:00 +00:00
/* The rest will be skipped */
size - = 16 ;
2006-09-27 18:17:01 +00:00
break ;
2007-03-21 03:06:21 +00:00
case ' atad ' : // 'data'
2009-05-17 19:27:50 +00:00
sound - > file_size = size ;
2010-01-22 13:02:15 +00:00
sound - > file_offset = FioGetPos ( ) - buf - > Remaining ( ) ;
2011-08-21 12:05:47 +00:00
sound - > file_slot = _cur . file_index ;
2006-09-27 18:17:01 +00:00
/* Set default volume and priority */
2009-05-17 19:27:50 +00:00
sound - > volume = 0x80 ;
sound - > priority = 0 ;
2006-09-27 18:17:01 +00:00
2009-05-17 19:27:50 +00:00
grfmsg ( 2 , " LoadGRFSound: channels %u, sample rate %u, bits per sample %u, length %u " , sound - > channels , sound - > rate , sound - > bits_per_sample , size ) ;
2009-11-07 18:22:00 +00:00
return ; // the fmt chunk has to appear before data, so we are finished
2006-09-27 18:17:01 +00:00
default :
2009-11-07 18:22:00 +00:00
/* Skip unknown chunks */
break ;
2006-09-27 18:17:01 +00:00
}
2009-11-07 18:22:00 +00:00
/* Skip rest of chunk */
2010-01-23 13:17:30 +00:00
for ( ; size > 0 ; size - - ) buf - > ReadByte ( ) ;
2006-09-27 18:17:01 +00:00
}
2009-11-07 18:22:00 +00:00
grfmsg ( 1 , " LoadGRFSound: RIFF does not contain any sound data " ) ;
/* Clear everything that was read */
MemSetT ( sound , 0 ) ;
2006-09-27 18:17:01 +00:00
}
2006-11-16 22:05:33 +00:00
/* Action 0x12 */
2010-01-22 13:02:15 +00:00
static void LoadFontGlyph ( ByteReader * buf )
2006-11-16 22:05:33 +00: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 13:17:30 +00:00
uint8 num_def = buf - > ReadByte ( ) ;
2006-11-16 22:05:33 +00:00
2007-03-10 09:55:11 +00:00
for ( uint i = 0 ; i < num_def ; i + + ) {
2010-01-23 13:17:30 +00:00
FontSize size = ( FontSize ) buf - > ReadByte ( ) ;
uint8 num_char = buf - > ReadByte ( ) ;
uint16 base_char = buf - > ReadWord ( ) ;
2006-11-16 22:05:33 +00:00
2006-12-26 17:36:18 +00:00
grfmsg ( 7 , " LoadFontGlyph: Loading %u glyph(s) at 0x%04X for size %u " , num_char , base_char , size ) ;
2006-11-16 22:05:33 +00:00
2007-03-10 09:55:11 +00:00
for ( uint c = 0 ; c < num_char ; c + + ) {
2011-08-21 12:05:47 +00:00
SetUnicodeGlyph ( size , base_char + c , _cur . spriteid ) ;
_cur . nfo_line + + ;
LoadNextSprite ( _cur . spriteid + + , _cur . file_index , _cur . nfo_line ) ;
2006-11-16 22:05:33 +00:00
}
}
}
2008-01-28 15:55:59 +00:00
/* Action 0x12 (SKIP) */
2010-01-22 13:02:15 +00:00
static void SkipAct12 ( ByteReader * buf )
2008-01-28 15:55:59 +00: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 13:17:30 +00:00
uint8 num_def = buf - > ReadByte ( ) ;
2008-01-28 15:55:59 +00:00
for ( uint i = 0 ; i < num_def ; i + + ) {
/* Ignore 'size' byte */
2010-01-23 13:17:30 +00:00
buf - > ReadByte ( ) ;
2008-01-28 15:55:59 +00:00
/* Sum up number of characters */
2011-08-21 12:05:47 +00:00
_cur . skip_sprites + = buf - > ReadByte ( ) ;
2008-01-28 15:55:59 +00:00
/* Ignore 'base_char' word */
2010-01-23 13:17:30 +00:00
buf - > ReadWord ( ) ;
2008-01-28 15:55:59 +00:00
}
2011-08-21 12:05:47 +00:00
grfmsg ( 3 , " SkipAct12: Skipping %d sprites " , _cur . skip_sprites ) ;
2008-01-28 15:55:59 +00:00
}
2007-03-06 22:00:13 +00:00
/* Action 0x13 */
2010-01-22 13:02:15 +00:00
static void TranslateGRFStrings ( ByteReader * buf )
2007-03-06 22:00:13 +00: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 13:17:30 +00:00
uint32 grfid = buf - > ReadDWord ( ) ;
2007-03-06 22:00:13 +00:00
const GRFConfig * c = GetGRFConfig ( grfid ) ;
if ( c = = NULL | | ( c - > status ! = GCS_INITIALISED & & c - > status ! = GCS_ACTIVATED ) ) {
2007-04-16 20:56:19 +00:00
grfmsg ( 7 , " TranslateGRFStrings: GRFID 0x%08x unknown, skipping action 13 " , BSWAP32 ( grfid ) ) ;
2007-03-06 22:00:13 +00: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 20:22:04 +00:00
GRFError * error = DisableGrf ( STR_NEWGRF_ERROR_LOAD_AFTER ) ;
2007-06-12 13:22:14 +00:00
char tmp [ 256 ] ;
GetString ( tmp , STR_NEWGRF_ERROR_AFTER_TRANSLATED_FILE , lastof ( tmp ) ) ;
2011-05-11 20:22:04 +00:00
error - > data = strdup ( tmp ) ;
2007-03-06 22:00:13 +00:00
return ;
}
2010-01-23 13:17:30 +00:00
byte num_strings = buf - > ReadByte ( ) ;
uint16 first_id = buf - > ReadWord ( ) ;
2007-03-06 22:00:13 +00:00
if ( ! ( ( first_id > = 0xD000 & & first_id + num_strings < = 0xD3FF ) | | ( first_id > = 0xDC00 & & first_id + num_strings < = 0xDCFF ) ) ) {
2007-04-16 20:56:19 +00:00
grfmsg ( 7 , " TranslateGRFStrings: Attempting to set out-of-range string IDs in action 13 (first: 0x%4X, number: 0x%2X) " , first_id , num_strings ) ;
2007-03-06 22:00:13 +00:00
return ;
}
2010-01-22 13:02:15 +00:00
for ( uint i = 0 ; i < num_strings & & buf - > HasData ( ) ; i + + ) {
2010-01-23 13:17:30 +00:00
const char * string = buf - > ReadString ( ) ;
2007-03-06 22:00:13 +00:00
2010-01-22 13:02:15 +00:00
if ( StrEmpty ( string ) ) {
2007-03-06 22:00:13 +00:00
grfmsg ( 7 , " TranslateGRFString: Ignoring empty string. " ) ;
continue ;
}
/* Since no language id is supplied 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 . 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 . */
AddGRFString ( grfid , first_id + i , 0x7F , true , string , STR_UNDEFINED ) ;
}
}
2010-07-31 09:35:42 +00:00
/** Callback function for 'INFO'->'NAME' to add a translation to the newgrf name. */
static bool ChangeGRFName ( byte langid , const char * str )
{
2011-08-21 12:05:47 +00:00
AddGRFTextToList ( & _cur . grfconfig - > name - > text , langid , _cur . grfconfig - > ident . grfid , str ) ;
2010-07-31 09:35:42 +00:00
return true ;
}
/** Callback function for 'INFO'->'DESC' to add a translation to the newgrf description. */
static bool ChangeGRFDescription ( byte langid , const char * str )
{
2011-08-21 12:05:47 +00:00
AddGRFTextToList ( & _cur . grfconfig - > info - > text , langid , _cur . grfconfig - > ident . grfid , str ) ;
2010-07-31 09:35:42 +00:00
return true ;
}
2010-07-31 09:35:49 +00:00
/** Callback function for 'INFO'->'NPAR' to set the number of valid parameters. */
static bool ChangeGRFNumUsedParams ( size_t len , ByteReader * buf )
{
if ( len ! = 1 ) {
grfmsg ( 2 , " StaticGRFInfo: expected only 1 byte for 'INFO'->'NPAR' but got " PRINTF_SIZE " , ignoring this field " , len ) ;
buf - > Skip ( len ) ;
} else {
2011-08-21 12:05:47 +00:00
_cur . grfconfig - > num_valid_params = min ( buf - > ReadByte ( ) , lengthof ( _cur . grfconfig - > param ) ) ;
2010-07-31 09:35:49 +00:00
}
return true ;
}
2010-07-31 09:44:38 +00:00
/** Callback function for 'INFO'->'PALS' to set the number of valid parameters. */
static bool ChangeGRFPalette ( size_t len , ByteReader * buf )
{
if ( len ! = 1 ) {
grfmsg ( 2 , " StaticGRFInfo: expected only 1 byte for 'INFO'->'PALS' but got " PRINTF_SIZE " , ignoring this field " , len ) ;
buf - > Skip ( len ) ;
} else {
char data = buf - > ReadByte ( ) ;
2011-05-03 20:19:57 +00:00
GRFPalette pal = GRFP_GRF_UNSET ;
2010-07-31 09:44:38 +00:00
switch ( data ) {
case ' * ' :
2011-05-03 20:19:57 +00: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 09:44:38 +00:00
default :
grfmsg ( 2 , " StaticGRFInfo: unexpected value '%02x' for 'INFO'->'PALS', ignoring this field " , data ) ;
break ;
}
2011-05-03 20:19:57 +00:00
if ( pal ! = GRFP_GRF_UNSET ) {
2011-08-21 12:05:47 +00:00
_cur . grfconfig - > palette & = ~ GRFP_GRF_MASK ;
_cur . grfconfig - > palette | = pal ;
2011-05-03 20:19:57 +00:00
}
2010-07-31 09:44:38 +00:00
}
return true ;
}
2010-07-31 14:40:50 +00:00
/** Callback function for 'INFO'->'VRSN' to the version of the NewGRF. */
static bool ChangeGRFVersion ( size_t len , ByteReader * buf )
{
if ( len ! = 4 ) {
grfmsg ( 2 , " StaticGRFInfo: expected 4 bytes for 'INFO'->'VRSN' but got " PRINTF_SIZE " , ignoring this field " , len ) ;
buf - > Skip ( len ) ;
} else {
2010-10-17 12:14:49 +00:00
/* Set min_loadable_version as well (default to minimal compatibility) */
2011-08-21 12:05:47 +00:00
_cur . grfconfig - > version = _cur . grfconfig - > min_loadable_version = buf - > ReadDWord ( ) ;
2010-07-31 14:40:50 +00:00
}
return true ;
}
2010-10-17 12:14:49 +00: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 ) {
grfmsg ( 2 , " StaticGRFInfo: expected 4 bytes for 'INFO'->'MINV' but got " PRINTF_SIZE " , ignoring this field " , len ) ;
buf - > Skip ( len ) ;
} else {
2011-08-21 12:05:47 +00:00
_cur . grfconfig - > min_loadable_version = buf - > ReadDWord ( ) ;
if ( _cur . grfconfig - > version = = 0 ) {
2010-10-17 12:14:49 +00:00
grfmsg ( 2 , " StaticGRFInfo: 'MINV' defined before 'VRSN' or 'VRSN' set to 0, ignoring this field " ) ;
2011-08-21 12:05:47 +00:00
_cur . grfconfig - > min_loadable_version = 0 ;
2010-10-17 12:14:49 +00:00
}
2011-08-21 12:05:47 +00:00
if ( _cur . grfconfig - > version < _cur . grfconfig - > min_loadable_version ) {
grfmsg ( 2 , " StaticGRFInfo: 'MINV' defined as %d, limiting it to 'VRSN' " , _cur . grfconfig - > min_loadable_version ) ;
_cur . grfconfig - > min_loadable_version = _cur . grfconfig - > version ;
2010-10-17 12:14:49 +00:00
}
}
return true ;
}
2010-07-31 09:51:04 +00: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 )
{
2011-08-21 12:05:47 +00:00
AddGRFTextToList ( & _cur_parameter - > name , langid , _cur . grfconfig - > ident . grfid , str ) ;
2010-07-31 09:51:04 +00: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 )
{
2011-08-21 12:05:47 +00:00
AddGRFTextToList ( & _cur_parameter - > desc , langid , _cur . grfconfig - > ident . grfid , str ) ;
2010-07-31 09:51:04 +00: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 ) {
grfmsg ( 2 , " StaticGRFInfo: expected 1 byte for 'INFO'->'PARA'->'TYPE' but got " PRINTF_SIZE " , ignoring this field " , len ) ;
buf - > Skip ( len ) ;
} else {
GRFParameterType type = ( GRFParameterType ) buf - > ReadByte ( ) ;
if ( type < PTYPE_END ) {
_cur_parameter - > type = type ;
} else {
grfmsg ( 3 , " StaticGRFInfo: unknown parameter type %d, ignoring this field " , type ) ;
}
}
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 ) {
grfmsg ( 2 , " StaticGRFInfo: 'INFO'->'PARA'->'LIMI' is only valid for parameters with type uint/enum, ignoring this field " ) ;
buf - > Skip ( len ) ;
} else if ( len ! = 8 ) {
grfmsg ( 2 , " StaticGRFInfo: expected 8 bytes for 'INFO'->'PARA'->'LIMI' but got " PRINTF_SIZE " , ignoring this field " , len ) ;
buf - > Skip ( len ) ;
} else {
_cur_parameter - > min_value = buf - > ReadDWord ( ) ;
_cur_parameter - > max_value = buf - > ReadDWord ( ) ;
}
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 ) {
grfmsg ( 2 , " StaticGRFInfo: expected 1 to 3 bytes for 'INFO'->'PARA'->'MASK' but got " PRINTF_SIZE " , ignoring this field " , len ) ;
buf - > Skip ( len ) ;
} else {
byte param_nr = buf - > ReadByte ( ) ;
2011-08-21 12:05:47 +00:00
if ( param_nr > = lengthof ( _cur . grfconfig - > param ) ) {
2010-07-31 09:51:04 +00:00
grfmsg ( 2 , " StaticGRFInfo: invalid parameter number in 'INFO'->'PARA'->'MASK', param %d, ignoring this field " , param_nr ) ;
buf - > Skip ( len - 1 ) ;
} else {
_cur_parameter - > param_nr = param_nr ;
if ( len > = 2 ) _cur_parameter - > first_bit = min ( buf - > ReadByte ( ) , 31 ) ;
if ( len > = 3 ) _cur_parameter - > num_bit = min ( buf - > ReadByte ( ) , 32 - _cur_parameter - > first_bit ) ;
}
}
return true ;
}
2010-08-23 21:53:55 +00:00
/** Callback function for 'INFO'->'PARAM'->param_num->'DFLT' to set the default value. */
2010-08-23 21:47:07 +00:00
static bool ChangeGRFParamDefault ( size_t len , ByteReader * buf )
{
if ( len ! = 4 ) {
grfmsg ( 2 , " StaticGRFInfo: expected 4 bytes for 'INFO'->'PARA'->'DEFA' but got " PRINTF_SIZE " , ignoring this field " , len ) ;
buf - > Skip ( len ) ;
} else {
_cur_parameter - > def_value = buf - > ReadDWord ( ) ;
}
2011-08-21 12:05:47 +00:00
_cur . grfconfig - > has_param_defaults = true ;
2010-08-23 21:47:07 +00:00
return true ;
}
2010-07-31 09:51:04 +00:00
2010-07-31 09:35:42 +00: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 09:51:04 +00:00
static bool SkipUnknownInfo ( ByteReader * buf , byte type ) ;
2010-07-31 12:05:41 +00:00
static bool HandleNodes ( ByteReader * buf , AllowedSubtags * tags ) ;
2010-07-31 09:51:04 +00: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 ) {
grfmsg ( 2 , " StaticGRFInfo: all child nodes of 'INFO'->'PARA'->param_num->'VALU' should have type 't' and the value/bit number as id " ) ;
if ( ! SkipUnknownInfo ( buf , type ) ) return false ;
2011-02-24 18:13:59 +00:00
type = buf - > ReadByte ( ) ;
continue ;
2010-07-31 09:51:04 +00:00
}
byte langid = buf - > ReadByte ( ) ;
const char * name_string = buf - > ReadString ( ) ;
SmallPair < uint32 , GRFText * > * val_name = _cur_parameter - > value_names . Find ( id ) ;
if ( val_name ! = _cur_parameter - > value_names . End ( ) ) {
2011-08-21 12:05:47 +00:00
AddGRFTextToList ( & val_name - > second , langid , _cur . grfconfig - > ident . grfid , name_string ) ;
2010-07-31 09:51:04 +00:00
} else {
GRFText * list = NULL ;
2011-08-21 12:05:47 +00:00
AddGRFTextToList ( & list , langid , _cur . grfconfig - > ident . grfid , name_string ) ;
2010-07-31 09:51:04 +00:00
_cur_parameter - > value_names . Insert ( id , list ) ;
}
type = buf - > ReadByte ( ) ;
}
return true ;
}
AllowedSubtags _tags_parameters [ ] = {
AllowedSubtags ( ' NAME ' , ChangeGRFParamName ) ,
AllowedSubtags ( ' DESC ' , ChangeGRFParamDescription ) ,
AllowedSubtags ( ' TYPE ' , ChangeGRFParamType ) ,
AllowedSubtags ( ' LIMI ' , ChangeGRFParamLimits ) ,
AllowedSubtags ( ' MASK ' , ChangeGRFParamMask ) ,
AllowedSubtags ( ' VALU ' , ChangeGRFParamValueNames ) ,
2010-08-23 21:53:55 +00:00
AllowedSubtags ( ' DFLT ' , ChangeGRFParamDefault ) ,
2010-07-31 09:51:04 +00: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 12:05:47 +00:00
if ( type ! = ' C ' | | id > = _cur . grfconfig - > num_valid_params ) {
2010-07-31 09:51:04 +00:00
grfmsg ( 2 , " StaticGRFInfo: all child nodes of 'INFO'->'PARA' should have type 'C' and their parameter number as id " ) ;
2011-02-24 18:13:59 +00:00
if ( ! SkipUnknownInfo ( buf , type ) ) return false ;
type = buf - > ReadByte ( ) ;
continue ;
2010-07-31 09:51:04 +00:00
}
2011-08-21 12:05:47 +00:00
if ( id > = _cur . grfconfig - > param_info . Length ( ) ) {
uint num_to_add = id - _cur . grfconfig - > param_info . Length ( ) + 1 ;
GRFParameterInfo * * newdata = _cur . grfconfig - > param_info . Append ( num_to_add ) ;
2010-07-31 09:51:04 +00:00
MemSetT < GRFParameterInfo * > ( newdata , 0 , num_to_add ) ;
}
2011-08-21 12:05:47 +00:00
if ( _cur . grfconfig - > param_info [ id ] = = NULL ) {
_cur . grfconfig - > param_info [ id ] = new GRFParameterInfo ( id ) ;
2010-07-31 09:51:04 +00:00
}
2011-08-21 12:05:47 +00:00
_cur_parameter = _cur . grfconfig - > param_info [ id ] ;
2010-07-31 09:51:04 +00:00
/* Read all parameter-data and process each node. */
2010-07-31 12:05:41 +00:00
if ( ! HandleNodes ( buf , _tags_parameters ) ) return false ;
2010-07-31 09:51:04 +00:00
type = buf - > ReadByte ( ) ;
}
return true ;
}
2010-07-31 09:35:42 +00:00
AllowedSubtags _tags_info [ ] = {
AllowedSubtags ( ' NAME ' , ChangeGRFName ) ,
AllowedSubtags ( ' DESC ' , ChangeGRFDescription ) ,
2010-07-31 09:35:49 +00:00
AllowedSubtags ( ' NPAR ' , ChangeGRFNumUsedParams ) ,
2010-07-31 09:44:38 +00:00
AllowedSubtags ( ' PALS ' , ChangeGRFPalette ) ,
2010-07-31 14:40:50 +00:00
AllowedSubtags ( ' VRSN ' , ChangeGRFVersion ) ,
2010-10-17 12:14:49 +00:00
AllowedSubtags ( ' MINV ' , ChangeGRFMinVersion ) ,
2010-07-31 09:51:04 +00:00
AllowedSubtags ( ' PARA ' , HandleParameterInfo ) ,
2010-07-31 09:35:42 +00:00
AllowedSubtags ( )
} ;
AllowedSubtags _tags_root [ ] = {
AllowedSubtags ( ' INFO ' , _tags_info ) ,
AllowedSubtags ( )
} ;
/**
* Try to skip the current node and all subnodes ( if it ' s a branch node ) .
* @ return True if we could skip the node , false if an error occured .
*/
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 ;
}
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 12:05:41 +00:00
return HandleNodes ( buf , tag - > handler . u . subtags ) ;
2010-07-31 09:35:42 +00:00
}
}
}
2011-03-04 21:14:00 +00:00
grfmsg ( 2 , " StaticGRFInfo: unknown type/id combination found, type=%c, id=%x " , type , id ) ;
2010-07-31 09:35:42 +00:00
return SkipUnknownInfo ( buf , type ) ;
}
2010-07-31 12:05:41 +00: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 ;
}
2010-07-31 09:35:42 +00:00
/* Action 0x14 */
static void StaticGRFInfo ( ByteReader * buf )
{
/* <14> <type> <id> <text/data...> */
2010-07-31 12:05:41 +00:00
HandleNodes ( buf , _tags_root ) ;
2010-07-31 09:35:42 +00:00
}
2006-09-27 18:17:01 +00:00
/* 'Action 0xFF' */
2010-01-22 13:02:15 +00:00
static void GRFDataBlock ( ByteReader * buf )
2006-09-27 18:17:01 +00:00
{
2009-11-07 18:22:00 +00:00
/* <FF> <name_len> <name> '\0' <data> */
2011-08-21 12:05:47 +00:00
if ( _cur . data_blocks = = 0 ) {
2006-12-26 17:36:18 +00:00
grfmsg ( 2 , " GRFDataBlock: unexpected data block, skipping " ) ;
2006-09-27 18:17:01 +00:00
return ;
}
2010-01-23 13:17:30 +00:00
uint8 name_len = buf - > ReadByte ( ) ;
2010-01-22 13:02:15 +00:00
const char * name = reinterpret_cast < const char * > ( buf - > Data ( ) ) ;
buf - > Skip ( name_len ) ;
2009-11-07 18:22:00 +00:00
/* Test string termination */
2010-01-23 13:17:30 +00:00
if ( buf - > ReadByte ( ) ! = 0 ) {
2009-11-07 18:22:00 +00:00
grfmsg ( 2 , " GRFDataBlock: Name not properly terminated " ) ;
return ;
}
2006-12-26 17:36:18 +00:00
grfmsg ( 2 , " GRFDataBlock: block name '%s'... " , name ) ;
2006-09-27 18:17:01 +00:00
2011-08-21 12:05:47 +00:00
_cur . data_blocks - - ;
2006-09-27 18:17:01 +00:00
2011-08-21 12:05:47 +00:00
switch ( _cur . data_type ) {
2010-01-22 13:02:15 +00:00
case GDT_SOUND : LoadGRFSound ( buf ) ; break ;
2009-05-26 15:46:24 +00:00
default : NOT_REACHED ( ) ;
2006-09-27 18:17:01 +00:00
}
}
2006-12-12 19:38:41 +00:00
/* Used during safety scan on unsafe actions */
2010-01-22 13:02:15 +00:00
static void GRFUnsafe ( ByteReader * buf )
2006-12-12 19:38:41 +00:00
{
2011-08-21 12:05:47 +00:00
SetBit ( _cur . grfconfig - > flags , GCF_UNSAFE ) ;
2006-12-12 19:38:41 +00:00
2007-01-03 11:33:54 +00:00
/* Skip remainder of GRF */
2011-08-21 12:05:47 +00:00
_cur . skip_sprites = - 1 ;
2006-12-12 19:38:41 +00:00
}
2007-03-07 11:47:46 +00:00
static void InitializeGRFSpecial ( )
2004-11-12 19:03:37 +00:00
{
2009-06-21 13:26:30 +00:00
_ttdpatch_flags [ 0 ] = ( ( _settings_game . station . never_expire_airports ? 1 : 0 ) < < 0x0C ) // keepsmallairport
2009-06-18 15:59:21 +00:00
| ( 1 < < 0x0D ) // newairports
| ( 1 < < 0x0E ) // largestations
2011-02-04 15:37:23 +00:00
| ( ( _settings_game . construction . max_bridge_length > 16 ? 1 : 0 ) < < 0x0F ) // longbridges
2009-06-18 15:59:21 +00:00
| ( 0 < < 0x10 ) // loadtime
| ( 1 < < 0x12 ) // presignals
| ( 1 < < 0x13 ) // extpresignals
2008-05-29 15:13:28 +00:00
| ( ( _settings_game . vehicle . never_expire_vehicles ? 1 : 0 ) < < 0x16 ) // enginespersist
2009-06-18 15:59:21 +00:00
| ( 1 < < 0x1B ) // multihead
| ( 1 < < 0x1D ) // lowmemory
| ( 1 < < 0x1E ) ; // generalfixes
2008-05-25 19:17:03 +00:00
2008-05-29 15:13:28 +00:00
_ttdpatch_flags [ 1 ] = ( ( _settings_game . economy . station_noise_level ? 1 : 0 ) < < 0x07 ) // moreairports - based on units of noise
2011-02-06 21:31:33 +00:00
| ( 1 < < 0x08 ) // mammothtrains
2009-06-18 15:59:21 +00:00
| ( 1 < < 0x09 ) // trainrefit
| ( 0 < < 0x0B ) // subsidiaries
2008-05-29 15:13:28 +00:00
| ( ( _settings_game . order . gradual_loading ? 1 : 0 ) < < 0x0C ) // gradualloading
2009-06-18 15:59:21 +00:00
| ( 1 < < 0x12 ) // unifiedmaglevmode - set bit 0 mode. Not revelant to OTTD
| ( 1 < < 0x13 ) // unifiedmaglevmode - set bit 1 mode
| ( 1 < < 0x14 ) // bridgespeedlimits
| ( 1 < < 0x16 ) // eternalgame
| ( 1 < < 0x17 ) // newtrains
| ( 1 < < 0x18 ) // newrvs
| ( 1 < < 0x19 ) // newships
| ( 1 < < 0x1A ) // newplanes
2008-05-29 15:13:28 +00:00
| ( ( _settings_game . construction . signal_side ? 1 : 0 ) < < 0x1B ) // signalsontrafficside
| ( ( _settings_game . vehicle . disable_elrails ? 0 : 1 ) < < 0x1C ) ; // electrifiedrailway
2008-05-25 19:17:03 +00:00
2009-06-18 15:59:21 +00:00
_ttdpatch_flags [ 2 ] = ( 1 < < 0x01 ) // loadallgraphics - obsolote
| ( 1 < < 0x03 ) // semaphores
2011-01-04 11:06:39 +00:00
| ( 1 < < 0x0A ) // newobjects
2009-06-18 15:59:21 +00:00
| ( 0 < < 0x0B ) // enhancedgui
| ( 0 < < 0x0C ) // newagerating
2008-05-29 15:13:28 +00:00
| ( ( _settings_game . construction . build_on_slopes ? 1 : 0 ) < < 0x0D ) // buildonslopes
2009-06-18 15:59:21 +00:00
| ( 1 < < 0x0E ) // fullloadany
| ( 1 < < 0x0F ) // planespeed
| ( 0 < < 0x10 ) // moreindustriesperclimate - obsolete
| ( 0 < < 0x11 ) // moretoylandfeatures
| ( 1 < < 0x12 ) // newstations
| ( 1 < < 0x13 ) // tracktypecostdiff
| ( 1 < < 0x14 ) // manualconvert
2008-05-29 15:13:28 +00:00
| ( ( _settings_game . construction . build_on_slopes ? 1 : 0 ) < < 0x15 ) // buildoncoasts
2009-06-18 15:59:21 +00:00
| ( 1 < < 0x16 ) // canals
| ( 1 < < 0x17 ) // newstartyear
2008-05-29 15:13:28 +00:00
| ( ( _settings_game . vehicle . freight_trains > 1 ? 1 : 0 ) < < 0x18 ) // freighttrains
2009-06-18 15:59:21 +00:00
| ( 1 < < 0x19 ) // newhouses
| ( 1 < < 0x1A ) // newbridges
| ( 1 < < 0x1B ) // newtownnames
| ( 1 < < 0x1C ) // moreanimation
2008-05-29 15:13:28 +00:00
| ( ( _settings_game . vehicle . wagon_speed_limits ? 1 : 0 ) < < 0x1D ) // wagonspeedlimits
2009-06-18 15:59:21 +00:00
| ( 1 < < 0x1E ) // newshistory
| ( 0 < < 0x1F ) ; // custombridgeheads
2008-05-25 19:17:03 +00:00
2009-06-18 15:59:21 +00:00
_ttdpatch_flags [ 3 ] = ( 0 < < 0x00 ) // newcargodistribution
| ( 1 < < 0x01 ) // windowsnap
2009-02-04 20:17:25 +00:00
| ( ( _settings_game . economy . allow_town_roads | | _generating_world ? 0 : 1 ) < < 0x02 ) // townbuildnoroad
2009-06-18 15:59:21 +00:00
| ( 1 < < 0x03 ) // pathbasedsignalling
| ( 0 < < 0x04 ) // aichoosechance
| ( 1 < < 0x05 ) // resolutionwidth
| ( 1 < < 0x06 ) // resolutionheight
| ( 1 < < 0x07 ) // newindustries
2008-05-29 15:13:28 +00:00
| ( ( _settings_game . order . improved_load ? 1 : 0 ) < < 0x08 ) // fifoloading
2009-06-18 15:59:21 +00:00
| ( 0 < < 0x09 ) // townroadbranchprob
| ( 0 < < 0x0A ) // tempsnowline
| ( 1 < < 0x0B ) // newcargo
| ( 1 < < 0x0C ) // enhancemultiplayer
| ( 1 < < 0x0D ) // onewayroads
2011-02-04 14:11:14 +00:00
| ( 1 < < 0x0E ) // irregularstations
2009-06-18 15:59:21 +00:00
| ( 1 < < 0x0F ) // statistics
| ( 1 < < 0x10 ) // newsounds
| ( 1 < < 0x11 ) // autoreplace
| ( 1 < < 0x12 ) // autoslope
| ( 0 < < 0x13 ) // followvehicle
| ( 1 < < 0x14 ) // trams
| ( 0 < < 0x15 ) // enhancetunnels
| ( 1 < < 0x16 ) // shortrvs
| ( 1 < < 0x17 ) // articulatedrvs
2008-05-29 15:13:28 +00:00
| ( ( _settings_game . vehicle . dynamic_engines ? 1 : 0 ) < < 0x18 ) // dynamic engines
2009-06-18 15:59:21 +00:00
| ( 1 < < 0x1E ) // variablerunningcosts
| ( 1 < < 0x1F ) ; // any switch is on
2004-11-12 19:03:37 +00:00
}
2007-03-07 11:47:46 +00:00
static void ResetCustomStations ( )
2005-11-12 00:19:34 +00:00
{
2009-11-23 13:14:45 +00:00
const GRFFile * const * end = _grf_files . End ( ) ;
for ( GRFFile * * file = _grf_files . Begin ( ) ; file ! = end ; file + + ) {
StationSpec * * & stations = ( * file ) - > stations ;
if ( stations = = NULL ) continue ;
2007-03-10 09:55:11 +00:00
for ( uint i = 0 ; i < MAX_STATIONS ; i + + ) {
2009-11-23 13:14:45 +00:00
if ( stations [ i ] = = NULL ) continue ;
StationSpec * statspec = stations [ i ] ;
2006-05-03 21:07:44 +00:00
2011-05-14 17:25:45 +00:00
delete [ ] statspec - > renderdata ;
2005-11-12 00:19:34 +00:00
2006-11-08 12:28:57 +00:00
/* Release platforms and layouts */
if ( ! statspec - > copied_layouts ) {
2007-03-10 09:55:11 +00:00
for ( uint l = 0 ; l < statspec - > lengths ; l + + ) {
for ( uint p = 0 ; p < statspec - > platforms [ l ] ; p + + ) {
2006-11-08 12:28:57 +00:00
free ( statspec - > layouts [ l ] [ p ] ) ;
}
free ( statspec - > layouts [ l ] ) ;
}
free ( statspec - > layouts ) ;
free ( statspec - > platforms ) ;
}
2006-05-06 22:20:16 +00:00
/* Release this station */
free ( statspec ) ;
2005-11-12 00:19:34 +00:00
}
2006-04-11 10:05:52 +00:00
/* Free and reset the station data */
2009-11-23 13:14:45 +00:00
free ( stations ) ;
stations = NULL ;
2005-11-12 00:19:34 +00:00
}
}
2007-03-19 11:27:30 +00:00
static void ResetCustomHouses ( )
{
2009-11-23 13:14:45 +00:00
const GRFFile * const * end = _grf_files . End ( ) ;
for ( GRFFile * * file = _grf_files . Begin ( ) ; file ! = end ; file + + ) {
HouseSpec * * & housespec = ( * file ) - > housespec ;
if ( housespec = = NULL ) continue ;
2009-07-05 20:59:42 +00:00
for ( uint i = 0 ; i < HOUSE_MAX ; i + + ) {
2009-11-23 13:14:45 +00:00
free ( housespec [ i ] ) ;
2007-06-10 23:40:29 +00:00
}
2007-03-19 11:27:30 +00:00
2009-11-23 13:14:45 +00:00
free ( housespec ) ;
housespec = NULL ;
2007-03-19 11:27:30 +00:00
}
}
2010-02-22 14:16:19 +00:00
static void ResetCustomAirports ( )
{
const GRFFile * const * end = _grf_files . End ( ) ;
for ( GRFFile * * file = _grf_files . Begin ( ) ; file ! = end ; file + + ) {
2010-03-18 23:15:38 +00:00
AirportSpec * * aslist = ( * file ) - > airportspec ;
if ( aslist ! = NULL ) {
for ( uint i = 0 ; i < NUM_AIRPORTS ; i + + ) {
AirportSpec * as = aslist [ i ] ;
if ( as ! = NULL ) {
/* We need to remove the tiles layouts */
for ( int j = 0 ; j < as - > num_table ; j + + ) {
/* remove the individual layouts */
free ( ( void * ) as - > table [ j ] ) ;
}
free ( ( void * ) as - > table ) ;
2011-08-19 20:44:22 +00:00
free ( ( void * ) as - > depot_table ) ;
2010-03-18 23:15:38 +00:00
free ( as ) ;
}
}
free ( aslist ) ;
( * file ) - > airportspec = NULL ;
}
2010-02-22 14:16:19 +00:00
AirportTileSpec * * & airporttilespec = ( * file ) - > airtspec ;
if ( airporttilespec ! = NULL ) {
for ( uint i = 0 ; i < NUM_AIRPORTTILES ; i + + ) {
free ( airporttilespec [ i ] ) ;
}
free ( airporttilespec ) ;
airporttilespec = NULL ;
}
}
}
2007-05-31 03:35:11 +00:00
static void ResetCustomIndustries ( )
{
2009-11-23 13:14:45 +00:00
const GRFFile * const * end = _grf_files . End ( ) ;
for ( GRFFile * * file = _grf_files . Begin ( ) ; file ! = end ; file + + ) {
IndustrySpec * * & industryspec = ( * file ) - > industryspec ;
IndustryTileSpec * * & indtspec = ( * file ) - > indtspec ;
2007-05-31 03:35:11 +00:00
/* We are verifiying both tiles and industries specs loaded from the grf file
* First , let ' s deal with industryspec */
2009-11-23 13:14:45 +00:00
if ( industryspec ! = NULL ) {
2009-07-05 20:59:42 +00:00
for ( uint i = 0 ; i < NUM_INDUSTRYTYPES ; i + + ) {
2009-11-23 13:14:45 +00:00
IndustrySpec * ind = industryspec [ i ] ;
2009-07-05 20:59:42 +00:00
if ( ind = = NULL ) continue ;
2007-05-31 03:35:11 +00:00
2009-07-05 20:59:42 +00:00
/* We need to remove the sounds array */
if ( HasBit ( ind - > cleanup_flag , CLEAN_RANDOMSOUNDS ) ) {
free ( ( void * ) ind - > random_sounds ) ;
}
2007-05-31 15:40:36 +00:00
2009-07-05 20:59:42 +00:00
/* We need to remove the tiles layouts */
2011-03-03 20:26:19 +00:00
CleanIndustryTileTable ( ind ) ;
2009-07-05 20:59:42 +00:00
free ( ind ) ;
2007-05-31 03:35:11 +00:00
}
2009-11-23 13:14:45 +00:00
free ( industryspec ) ;
industryspec = NULL ;
2007-05-31 03:35:11 +00:00
}
2009-11-23 13:14:45 +00:00
if ( indtspec = = NULL ) continue ;
2009-07-05 20:59:42 +00:00
for ( uint i = 0 ; i < NUM_INDUSTRYTILES ; i + + ) {
2009-11-23 13:14:45 +00:00
free ( indtspec [ i ] ) ;
2007-05-31 03:35:11 +00:00
}
2009-07-05 20:59:42 +00:00
2009-11-23 13:14:45 +00:00
free ( indtspec ) ;
indtspec = NULL ;
2007-05-31 03:35:11 +00:00
}
}
2010-08-28 18:21:09 +00:00
static void ResetCustomObjects ( )
{
const GRFFile * const * end = _grf_files . End ( ) ;
for ( GRFFile * * file = _grf_files . Begin ( ) ; file ! = end ; file + + ) {
ObjectSpec * * & objectspec = ( * file ) - > objectspec ;
if ( objectspec = = NULL ) continue ;
for ( uint i = 0 ; i < NUM_OBJECTS ; i + + ) {
free ( objectspec [ i ] ) ;
}
free ( objectspec ) ;
objectspec = NULL ;
}
}
2007-03-07 11:47:46 +00:00
static void ResetNewGRF ( )
2006-10-26 19:16:10 +00:00
{
2009-11-23 13:14:45 +00:00
const GRFFile * const * end = _grf_files . End ( ) ;
for ( GRFFile * * file = _grf_files . Begin ( ) ; file ! = end ; file + + ) {
GRFFile * f = * file ;
2006-10-26 19:16:10 +00:00
free ( f - > filename ) ;
2007-05-25 06:39:12 +00:00
free ( f - > cargo_list ) ;
2009-02-08 18:29:22 +00:00
free ( f - > railtype_list ) ;
2010-11-16 12:47:22 +00:00
delete [ ] f - > language_map ;
2006-10-26 19:16:10 +00:00
free ( f ) ;
}
2009-11-23 13:14:45 +00:00
_grf_files . Clear ( ) ;
2011-08-21 12:05:47 +00:00
_cur . grffile = NULL ;
2006-10-26 19:16:10 +00:00
}
2007-06-12 09:41:12 +00:00
static void ResetNewGRFErrors ( )
{
for ( GRFConfig * c = _grfconfig ; c ! = NULL ; c = c - > next ) {
2007-11-19 21:02:30 +00:00
if ( ! HasBit ( c - > flags , GCF_COPY ) & & c - > error ! = NULL ) {
2010-02-25 15:54:40 +00:00
delete c - > error ;
2007-06-12 09:41:12 +00:00
c - > error = NULL ;
}
}
}
2005-09-26 18:43:58 +00:00
/**
* Reset all NewGRF loaded data
* TODO
*/
2010-09-06 18:20:07 +00:00
void ResetNewGRFData ( )
2005-09-26 18:43:58 +00:00
{
2006-04-21 03:00:20 +00:00
CleanUpStrings ( ) ;
2007-06-18 23:00:55 +00:00
CleanUpGRFTownNames ( ) ;
2006-04-21 03:00:20 +00:00
2007-03-21 03:06:21 +00:00
/* Copy/reset original engine info data */
2008-01-08 18:25:51 +00:00
SetupEngines ( ) ;
2005-10-01 17:38:48 +00:00
2008-02-05 04:48:56 +00:00
/* Copy/reset original bridge info data */
ResetBridges ( ) ;
2005-10-04 21:42:00 +00:00
2008-08-24 23:00:11 +00:00
/* Reset rail type information */
ResetRailTypes ( ) ;
2008-04-22 18:20:41 +00:00
/* Allocate temporary refit/cargo class data */
2009-05-16 23:44:36 +00:00
_gted = CallocT < GRFTempEngineData > ( Engine : : GetPoolSize ( ) ) ;
2005-11-06 13:42:26 +00:00
2010-03-23 11:05:28 +00:00
/* Fill rail type label temporary data for default trains */
Engine * e ;
FOR_ALL_ENGINES_OF_TYPE ( e , VEH_TRAIN ) {
_gted [ e - > index ] . railtypelabel = GetRailTypeInfo ( e - > u . rail . railtype ) - > label ;
}
2007-03-21 03:06:21 +00:00
/* Reset GRM reservations */
2006-10-27 13:15:21 +00:00
memset ( & _grm_engines , 0 , sizeof ( _grm_engines ) ) ;
2007-06-02 08:56:21 +00:00
memset ( & _grm_cargos , 0 , sizeof ( _grm_cargos ) ) ;
2006-10-27 13:15:21 +00:00
2008-02-12 13:23:57 +00:00
/* Reset generic feature callback lists */
ResetGenericCallbacks ( ) ;
2007-03-21 03:06:21 +00:00
/* Reset price base data */
2005-10-12 07:27:56 +00:00
ResetPriceBaseMultipliers ( ) ;
2005-11-12 00:19:34 +00:00
2006-08-25 00:41:10 +00:00
/* Reset the curencies array */
ResetCurrencies ( ) ;
2007-03-19 11:27:30 +00:00
/* Reset the house array */
ResetCustomHouses ( ) ;
ResetHouses ( ) ;
2007-05-29 17:41:59 +00:00
/* Reset the industries structures*/
2007-05-31 03:35:11 +00:00
ResetCustomIndustries ( ) ;
2007-05-29 17:41:59 +00:00
ResetIndustries ( ) ;
2010-08-28 17:29:12 +00:00
/* Reset the objects. */
2010-08-28 17:32:30 +00:00
ObjectClass : : Reset ( ) ;
2010-08-28 18:21:09 +00:00
ResetCustomObjects ( ) ;
2010-08-28 17:29:12 +00:00
ResetObjects ( ) ;
2007-03-21 03:06:21 +00:00
/* Reset station classes */
2010-08-07 22:07:07 +00:00
StationClass : : Reset ( ) ;
2005-11-12 00:19:34 +00:00
ResetCustomStations ( ) ;
2006-01-02 22:40:39 +00:00
2010-02-22 14:15:48 +00:00
/* Reset airport-related structures */
2010-08-07 22:08:20 +00:00
AirportClass : : Reset ( ) ;
2010-02-22 14:16:19 +00:00
ResetCustomAirports ( ) ;
2010-03-05 23:21:23 +00:00
AirportSpec : : ResetAirports ( ) ;
2010-02-22 14:15:48 +00:00
AirportTileSpec : : ResetAirportTiles ( ) ;
2008-01-21 20:41:04 +00:00
/* Reset canal sprite groups and flags */
memset ( _water_feature , 0 , sizeof ( _water_feature ) ) ;
2007-05-06 18:14:33 +00:00
2007-03-20 13:47:00 +00:00
/* Reset the snowline table. */
ClearSnowLine ( ) ;
2006-10-26 19:16:10 +00:00
/* Reset NewGRF files */
ResetNewGRF ( ) ;
2007-06-12 09:41:12 +00:00
/* Reset NewGRF errors. */
ResetNewGRFErrors ( ) ;
2007-02-20 22:09:21 +00:00
/* Set up the default cargo types */
2008-05-29 15:13:28 +00:00
SetupCargoForClimate ( _settings_game . game_creation . landscape ) ;
2007-02-20 22:09:21 +00:00
2006-05-14 20:58:12 +00:00
/* Reset misc GRF features and train list display variables */
_misc_grf_features = 0 ;
2007-05-31 19:13:41 +00:00
2007-06-15 23:55:52 +00:00
_loaded_newgrf_features . has_2CC = false ;
2010-09-25 21:59:22 +00:00
_loaded_newgrf_features . used_liveries = 1 < < LS_DEFAULT ;
2007-06-15 23:55:52 +00:00
_loaded_newgrf_features . has_newhouses = false ;
2007-10-02 01:12:06 +00:00
_loaded_newgrf_features . has_newindustries = false ;
2008-01-24 14:49:40 +00:00
_loaded_newgrf_features . shore = SHORE_REPLACE_NONE ;
2006-05-14 20:58:12 +00:00
2008-04-29 21:31:29 +00:00
/* Clear all GRF overrides */
_grf_id_overrides . clear ( ) ;
2006-09-27 18:17:01 +00:00
InitializeSoundPool ( ) ;
2009-05-22 15:13:50 +00:00
_spritegroup_pool . CleanPool ( ) ;
2005-09-26 18:43:58 +00:00
}
2011-07-29 22:23:17 +00: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 ( ) ;
}
2007-02-24 23:20:21 +00:00
static void BuildCargoTranslationMap ( )
{
2011-08-21 12:05:47 +00:00
memset ( _cur . grffile - > cargo_map , 0xFF , sizeof ( _cur . grffile - > cargo_map ) ) ;
2007-02-24 23:20:21 +00:00
for ( CargoID c = 0 ; c < NUM_CARGO ; c + + ) {
2009-07-16 19:00:13 +00:00
const CargoSpec * cs = CargoSpec : : Get ( c ) ;
2007-02-24 23:20:21 +00:00
if ( ! cs - > IsValid ( ) ) continue ;
2011-08-21 12:05:47 +00:00
if ( _cur . grffile - > cargo_max = = 0 ) {
2007-02-24 23:20:21 +00:00
/* Default translation table, so just a straight mapping to bitnum */
2011-08-21 12:05:47 +00:00
_cur . grffile - > cargo_map [ c ] = cs - > bitnum ;
2007-02-24 23:20:21 +00:00
} else {
/* Check the translation table for this cargo's label */
2011-08-21 12:05:47 +00:00
for ( uint i = 0 ; i < _cur . grffile - > cargo_max ; i + + ) {
if ( cs - > label = = _cur . grffile - > cargo_list [ i ] ) {
_cur . grffile - > cargo_map [ c ] = i ;
2007-02-24 23:20:21 +00:00
break ;
}
}
}
}
}
2011-05-01 20:46:36 +00:00
static void InitNewGRFFile ( const GRFConfig * config )
2004-11-12 23:26:02 +00:00
{
2007-03-10 09:55:11 +00:00
GRFFile * newfile = GetFileByFilename ( config - > filename ) ;
2004-11-22 22:52:01 +00:00
if ( newfile ! = NULL ) {
2004-11-22 22:51:19 +00:00
/* We already loaded it once. */
2011-08-21 12:05:47 +00:00
_cur . grffile = newfile ;
2004-11-22 22:51:19 +00:00
return ;
}
2007-01-11 17:29:39 +00:00
newfile = CallocT < GRFFile > ( 1 ) ;
2004-11-12 22:28:19 +00:00
2007-06-23 21:12:09 +00:00
newfile - > filename = strdup ( config - > filename ) ;
2010-11-10 17:01:29 +00:00
newfile - > grfid = config - > ident . grfid ;
2004-11-12 22:28:19 +00:00
2009-07-17 22:00:13 +00:00
/* Initialise local settings to defaults */
newfile - > traininfo_vehicle_pitch = 0 ;
newfile - > traininfo_vehicle_width = TRAININFO_DEFAULT_VEHICLE_WIDTH ;
2009-11-24 13:15:58 +00:00
/* Mark price_base_multipliers as 'not set' */
for ( Price i = PR_BEGIN ; i < PR_END ; i + + ) {
2009-11-24 23:34:46 +00:00
newfile - > price_base_multipliers [ i ] = INVALID_PRICE_MODIFIER ;
2009-11-24 13:15:58 +00:00
}
2010-02-03 07:58:43 +00:00
/* Initialise rail type map with default rail types */
memset ( newfile - > railtype_map , INVALID_RAILTYPE , sizeof newfile - > railtype_map ) ;
newfile - > railtype_map [ 0 ] = RAILTYPE_RAIL ;
newfile - > railtype_map [ 1 ] = RAILTYPE_ELECTRIC ;
newfile - > railtype_map [ 2 ] = RAILTYPE_MONO ;
newfile - > railtype_map [ 3 ] = RAILTYPE_MAGLEV ;
2010-01-03 19:29:56 +00:00
/* Copy the initial parameter list
* ' Uninitialised ' parameters are zeroed as that is their default value when dynamically creating them . */
2009-09-04 23:02:52 +00:00
assert_compile ( lengthof ( newfile - > param ) = = lengthof ( config - > param ) & & lengthof ( config - > param ) = = 0x80 ) ;
2010-01-03 19:29:56 +00:00
memset ( newfile - > param , 0 , sizeof ( newfile - > param ) ) ;
assert ( config - > num_params < = lengthof ( config - > param ) ) ;
2006-04-22 13:17:13 +00:00
newfile - > param_end = config - > num_params ;
2010-01-03 19:29:56 +00:00
if ( newfile - > param_end > 0 ) {
MemCpyT ( newfile - > param , config - > param , newfile - > param_end ) ;
}
2006-04-22 13:17:13 +00:00
2011-08-21 12:05:47 +00:00
* _grf_files . Append ( ) = _cur . grffile = newfile ;
2004-11-12 22:28:19 +00:00
}
2006-11-06 10:11:03 +00:00
2010-08-01 19:22:34 +00:00
/**
* List of what cargo labels are refittable for the given the vehicle - type .
2010-08-01 19:44:49 +00:00
* Only currently active labels are applied .
*/
2007-02-24 22:01:18 +00:00
static const CargoLabel _default_refitmasks_rail [ ] = {
' PASS ' , ' COAL ' , ' MAIL ' , ' LVST ' , ' GOOD ' , ' GRAI ' , ' WHEA ' , ' MAIZ ' , ' WOOD ' ,
' IORE ' , ' STEL ' , ' VALU ' , ' GOLD ' , ' DIAM ' , ' PAPR ' , ' FOOD ' , ' FRUT ' , ' CORE ' ,
' WATR ' , ' SUGR ' , ' TOYS ' , ' BATT ' , ' SWET ' , ' TOFF ' , ' COLA ' , ' CTCD ' , ' BUBL ' ,
' PLST ' , ' FZDR ' ,
0 } ;
static const CargoLabel _default_refitmasks_road [ ] = {
0 } ;
static const CargoLabel _default_refitmasks_ships [ ] = {
' COAL ' , ' MAIL ' , ' LVST ' , ' GOOD ' , ' GRAI ' , ' WHEA ' , ' MAIZ ' , ' WOOD ' , ' IORE ' ,
' STEL ' , ' VALU ' , ' GOLD ' , ' DIAM ' , ' PAPR ' , ' FOOD ' , ' FRUT ' , ' CORE ' , ' WATR ' ,
' RUBR ' , ' SUGR ' , ' TOYS ' , ' BATT ' , ' SWET ' , ' TOFF ' , ' COLA ' , ' CTCD ' , ' BUBL ' ,
' PLST ' , ' FZDR ' ,
0 } ;
static const CargoLabel _default_refitmasks_aircraft [ ] = {
' PASS ' , ' MAIL ' , ' GOOD ' , ' VALU ' , ' GOLD ' , ' DIAM ' , ' FOOD ' , ' FRUT ' , ' SUGR ' ,
' TOYS ' , ' BATT ' , ' SWET ' , ' TOFF ' , ' COLA ' , ' CTCD ' , ' BUBL ' , ' PLST ' , ' FZDR ' ,
0 } ;
2009-09-20 23:11:01 +00:00
static const CargoLabel * const _default_refitmasks [ ] = {
2007-02-24 22:01:18 +00:00
_default_refitmasks_rail ,
_default_refitmasks_road ,
_default_refitmasks_ships ,
_default_refitmasks_aircraft ,
2006-11-06 10:11:03 +00:00
} ;
2005-11-06 13:42:26 +00:00
/**
* Precalculate refit masks from cargo classes for all vehicles .
*/
2007-03-07 11:47:46 +00:00
static void CalculateRefitMasks ( )
2005-11-06 13:42:26 +00:00
{
2008-04-29 21:31:29 +00:00
Engine * e ;
FOR_ALL_ENGINES ( e ) {
EngineID engine = e - > index ;
EngineInfo * ei = & e - > info ;
2005-11-06 13:42:26 +00:00
uint32 mask = 0 ;
uint32 not_mask = 0 ;
2007-03-10 16:21:29 +00:00
uint32 xor_mask = 0 ;
2009-09-28 19:24:20 +00:00
/* Did the newgrf specify any refitting? If not, use defaults. */
if ( _gted [ engine ] . refitmask_valid ) {
if ( ei - > refit_mask ! = 0 ) {
2011-04-10 17:21:43 +00:00
const GRFFile * file = _gted [ engine ] . refitmask_grf ;
if ( file = = NULL ) file = e - > grf_prop . grffile ;
2009-09-28 19:24:20 +00:00
if ( file ! = NULL & & file - > cargo_max ! = 0 ) {
/* Apply cargo translation table to the refit mask */
uint num_cargo = min ( 32 , file - > cargo_max ) ;
for ( uint i = 0 ; i < num_cargo ; i + + ) {
if ( ! HasBit ( ei - > refit_mask , i ) ) continue ;
2007-03-10 16:21:29 +00:00
2009-09-28 19:24:20 +00:00
CargoID c = GetCargoIDByLabel ( file - > cargo_list [ i ] ) ;
if ( c = = CT_INVALID ) continue ;
2007-03-10 16:21:29 +00:00
2009-09-28 19:24:20 +00:00
SetBit ( xor_mask , c ) ;
}
} else {
/* No cargo table, so use the cargo bitnum values */
const CargoSpec * cs ;
FOR_ALL_CARGOSPECS ( cs ) {
if ( HasBit ( ei - > refit_mask , cs - > bitnum ) ) SetBit ( xor_mask , cs - > Index ( ) ) ;
}
2007-03-10 16:21:29 +00:00
}
2009-09-28 19:24:20 +00:00
}
if ( _gted [ engine ] . cargo_allowed ! = 0 ) {
/* Build up the list of cargo types from the set cargo classes. */
2009-07-16 20:40:06 +00:00
const CargoSpec * cs ;
FOR_ALL_CARGOSPECS ( cs ) {
2009-09-28 19:24:20 +00: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 16:21:29 +00:00
}
}
2009-09-28 19:24:20 +00:00
} else {
/* Don't apply default refit mask to wagons nor engines with no capacity */
2008-04-29 21:31:29 +00:00
if ( e - > type ! = VEH_TRAIN | | ( e - > u . rail . capacity ! = 0 & & e - > u . rail . railveh_type ! = RAILVEH_WAGON ) ) {
const CargoLabel * cl = _default_refitmasks [ e - > type ] ;
2007-02-24 22:01:18 +00:00
for ( uint i = 0 ; ; i + + ) {
if ( cl [ i ] = = 0 ) break ;
CargoID cargo = GetCargoIDByLabel ( cl [ i ] ) ;
if ( cargo = = CT_INVALID ) continue ;
2007-11-20 13:35:54 +00:00
SetBit ( xor_mask , cargo ) ;
2007-02-24 22:01:18 +00:00
}
2006-02-18 14:41:24 +00:00
}
2005-11-06 13:42:26 +00:00
}
2008-03-27 17:51:20 +00:00
ei - > refit_mask = ( ( mask & ~ not_mask ) ^ xor_mask ) & _cargo_mask ;
2007-02-24 19:26:45 +00:00
/* Check if this engine's cargo type is valid. If not, set to the first refittable
2009-08-17 18:33:48 +00:00
* cargo type . Finally disable the vehicle , if there is still no cargo . */
if ( ei - > cargo_type = = CT_INVALID & & ei - > refit_mask ! = 0 ) ei - > cargo_type = ( CargoID ) FindFirstBit ( ei - > refit_mask ) ;
2011-03-13 22:05:29 +00:00
if ( ei - > cargo_type = = CT_INVALID ) ei - > climates = 0 ;
2009-10-04 20:33:18 +00: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 13:42:26 +00:00
}
}
2004-11-12 19:03:37 +00:00
2010-05-21 20:51:00 +00:00
/** Check for invalid engines */
static void FinaliseEngineArray ( )
{
Engine * e ;
FOR_ALL_ENGINES ( e ) {
2010-08-10 15:54:53 +00:00
if ( e - > grf_prop . grffile = = NULL ) {
2010-05-21 20:51:00 +00: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 21:59:22 +00:00
2011-02-13 19:49:53 +00:00
/* When the train does not set property 27 (misc flags), but it
* is overridden by a NewGRF graphically we want to disable the
* flipping possibility . */
if ( e - > type = = VEH_TRAIN & & ! _gted [ e - > index ] . prop27_set & & e - > grf_prop . grffile ! = NULL & & is_custom_sprite ( e - > u . rail . image_index ) ) {
ClrBit ( e - > info . misc_flags , EF_RAIL_FLIPS ) ;
}
2010-09-25 21:59:22 +00:00
/* Skip wagons, there livery is defined via the engine */
if ( e - > type ! = VEH_TRAIN | | e - > u . rail . railveh_type ! = RAILVEH_WAGON ) {
LiveryScheme ls = GetEngineLiveryScheme ( e - > index , INVALID_ENGINE , NULL ) ;
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 20:51:00 +00:00
}
}
/** Check for invalid cargos */
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 13:08:53 +00: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 .
*/
static bool IsHouseSpecValid ( HouseSpec * hs , const HouseSpec * next1 , const HouseSpec * next2 , const HouseSpec * next3 , const char * filename )
{
if ( ( ( hs - > building_flags & BUILDING_HAS_2_TILES ) ! = 0 & &
( next1 = = NULL | | ! next1 - > enabled | | ( next1 - > building_flags & BUILDING_HAS_1_TILE ) ! = 0 ) ) | |
( ( hs - > building_flags & BUILDING_HAS_4_TILES ) ! = 0 & &
( next2 = = NULL | | ! next2 - > enabled | | ( next2 - > building_flags & BUILDING_HAS_1_TILE ) ! = 0 | |
next3 = = NULL | | ! next3 - > enabled | | ( next3 - > building_flags & BUILDING_HAS_1_TILE ) ! = 0 ) ) ) {
hs - > enabled = false ;
if ( filename ! = NULL ) DEBUG ( grf , 1 , " FinaliseHouseArray: %s defines house %d as multitile, but no suitable tiles follow. Disabling house. " , filename , hs - > grf_prop . local_id ) ;
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 ;
if ( filename ! = NULL ) DEBUG ( grf , 1 , " FinaliseHouseArray: %s defines multitile house %d with non-zero population on additional tiles. Disabling house. " , filename , hs - > grf_prop . local_id ) ;
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 . */
if ( filename ! = NULL & & ( hs - > building_flags & BUILDING_HAS_1_TILE ) ! = ( HouseSpec : : Get ( hs - > grf_prop . subst_id ) - > building_flags & BUILDING_HAS_1_TILE ) ) {
hs - > enabled = false ;
DEBUG ( grf , 1 , " FinaliseHouseArray: %s defines house %d with different house size then it's substitute type. Disabling house. " , filename , hs - > grf_prop . local_id ) ;
return false ;
}
2010-09-13 13:27:29 +00: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 ;
if ( filename ! = NULL ) DEBUG ( grf , 1 , " FinaliseHouseArray: %s defines house %d without a size but marked it as available. Disabling house. " , filename , hs - > grf_prop . local_id ) ;
return false ;
}
2010-09-13 13:08:53 +00:00
return true ;
}
2011-04-30 19:44:20 +00: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 )
{
Year min_year = MAX_YEAR ;
for ( int i = 0 ; i < HOUSE_MAX ; i + + ) {
HouseSpec * hs = HouseSpec : : Get ( i ) ;
if ( hs = = NULL | | ! hs - > enabled ) continue ;
if ( ( hs - > building_availability & bitmask ) ! = bitmask ) continue ;
if ( hs - > min_year < min_year ) min_year = hs - > min_year ;
}
if ( min_year = = 0 ) return ;
for ( int i = 0 ; i < HOUSE_MAX ; i + + ) {
HouseSpec * hs = HouseSpec : : Get ( i ) ;
if ( hs = = NULL | | ! hs - > enabled ) continue ;
if ( ( hs - > building_availability & bitmask ) ! = bitmask ) continue ;
if ( hs - > min_year = = min_year ) hs - > min_year = 0 ;
}
}
2010-08-01 19:22:34 +00:00
/**
* Add all new houses to the house array . House properties can be set at any
2007-03-19 11:27:30 +00: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 19:44:49 +00:00
* the TTDPatch behaviour described below that we need to emulate .
*/
2007-03-19 11:27:30 +00: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 07:14:25 +00:00
* the dates are left alone .
* On the other hand , why 1930 ? Just ' fix ' the houses with the lowest
* minimum introduction date to 0.
*/
2009-11-23 13:14:45 +00:00
const GRFFile * const * end = _grf_files . End ( ) ;
for ( GRFFile * * file = _grf_files . Begin ( ) ; file ! = end ; file + + ) {
HouseSpec * * & housespec = ( * file ) - > housespec ;
if ( housespec = = NULL ) continue ;
2007-03-19 11:27:30 +00:00
for ( int i = 0 ; i < HOUSE_MAX ; i + + ) {
2009-11-23 13:14:45 +00:00
HouseSpec * hs = housespec [ i ] ;
2009-05-10 22:16:51 +00:00
if ( hs = = NULL ) continue ;
2009-11-23 13:14:45 +00:00
const HouseSpec * next1 = ( i + 1 < HOUSE_MAX ? housespec [ i + 1 ] : NULL ) ;
const HouseSpec * next2 = ( i + 2 < HOUSE_MAX ? housespec [ i + 2 ] : NULL ) ;
const HouseSpec * next3 = ( i + 3 < HOUSE_MAX ? housespec [ i + 3 ] : NULL ) ;
2009-05-10 22:16:51 +00:00
2010-09-13 13:08:53 +00:00
if ( ! IsHouseSpecValid ( hs , next1 , next2 , next3 , ( * file ) - > filename ) ) continue ;
2009-05-22 18:44:22 +00:00
2009-05-10 22:16:51 +00:00
_house_mngr . SetEntitySpec ( hs ) ;
2007-03-19 11:27:30 +00:00
}
}
2010-09-13 13:08:53 +00:00
for ( int i = 0 ; i < HOUSE_MAX ; i + + ) {
HouseSpec * hs = HouseSpec : : Get ( i ) ;
const HouseSpec * next1 = ( i + 1 < HOUSE_MAX ? HouseSpec : : Get ( i + 1 ) : NULL ) ;
const HouseSpec * next2 = ( i + 2 < HOUSE_MAX ? HouseSpec : : Get ( i + 2 ) : NULL ) ;
const HouseSpec * next3 = ( i + 3 < HOUSE_MAX ? HouseSpec : : Get ( i + 3 ) : NULL ) ;
/* 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 . */
2011-02-13 21:44:00 +00:00
if ( ! IsHouseSpecValid ( hs , next1 , next2 , next3 , NULL ) ) {
/* 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 12:39:45 +00:00
hs - > building_flags = TILE_NO_FLAG ;
2011-02-13 21:44:00 +00:00
}
2010-09-13 13:08:53 +00:00
}
2011-04-30 19:44:20 +00: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 11:27:30 +00:00
2011-04-30 19:44:20 +00: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 11:27:30 +00:00
}
}
2010-08-01 19:22:34 +00:00
/**
* Add all new industries to the industry array . Industry properties can be set at any
2007-07-06 20:40:58 +00:00
* time in the GRF file , so we can only add a industry spec to the industry array
2010-08-01 19:44:49 +00:00
* after the file has finished loading .
*/
2007-07-06 20:40:58 +00:00
static void FinaliseIndustriesArray ( )
{
2009-11-23 13:14:45 +00:00
const GRFFile * const * end = _grf_files . End ( ) ;
for ( GRFFile * * file = _grf_files . Begin ( ) ; file ! = end ; file + + ) {
IndustrySpec * * & industryspec = ( * file ) - > industryspec ;
IndustryTileSpec * * & indtspec = ( * file ) - > indtspec ;
if ( industryspec ! = NULL ) {
2007-07-06 20:40:58 +00:00
for ( int i = 0 ; i < NUM_INDUSTRYTYPES ; i + + ) {
2009-11-23 13:14:45 +00:00
IndustrySpec * indsp = industryspec [ i ] ;
2007-07-06 20:40:58 +00:00
2007-07-10 00:10:19 +00:00
if ( indsp ! = NULL & & indsp - > enabled ) {
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 ;
2008-02-02 03:23:26 +00:00
if ( indsp - > station_name ! = STR_NULL ) {
/* STR_NULL (0) can be set by grf. It has a meaning regarding assignation of the
2008-08-25 20:10:45 +00:00
* station ' s name . Don ' t want to lose the value , therefore , do not process . */
2008-02-02 03:23:26 +00:00
strid = GetGRFStringID ( indsp - > grf_prop . grffile - > grfid , indsp - > station_name ) ;
if ( strid ! = STR_UNDEFINED ) indsp - > station_name = strid ;
}
2007-07-06 20:40:58 +00:00
_industry_mngr . SetEntitySpec ( indsp ) ;
_loaded_newgrf_features . has_newindustries = true ;
}
}
}
2009-11-23 13:14:45 +00:00
if ( indtspec ! = NULL ) {
2007-07-06 20:40:58 +00:00
for ( int i = 0 ; i < NUM_INDUSTRYTILES ; i + + ) {
2009-11-23 13:14:45 +00:00
IndustryTileSpec * indtsp = indtspec [ i ] ;
2007-07-06 20:40:58 +00:00
if ( indtsp ! = NULL ) {
_industile_mngr . SetEntitySpec ( indtsp ) ;
}
}
}
}
2007-07-11 22:57:47 +00:00
for ( uint j = 0 ; j < NUM_INDUSTRYTYPES ; j + + ) {
IndustrySpec * indsp = & _industry_specs [ j ] ;
if ( indsp - > enabled & & indsp - > grf_prop . grffile ! = NULL ) {
for ( uint i = 0 ; i < 3 ; i + + ) {
indsp - > conflicting [ i ] = MapNewGRFIndustryType ( indsp - > conflicting [ i ] , indsp - > grf_prop . grffile - > grfid ) ;
}
}
2010-05-21 20:51:00 +00:00
if ( ! indsp - > enabled ) {
indsp - > name = STR_NEWGRF_INVALID_INDUSTRYTYPE ;
}
2007-07-11 22:57:47 +00:00
}
2007-07-06 20:40:58 +00:00
}
2007-03-22 23:19:40 +00:00
2010-08-28 18:21:09 +00: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 ( )
{
const GRFFile * const * end = _grf_files . End ( ) ;
for ( GRFFile * * file = _grf_files . Begin ( ) ; file ! = end ; file + + ) {
ObjectSpec * * & objectspec = ( * file ) - > objectspec ;
if ( objectspec ! = NULL ) {
for ( int i = 0 ; i < NUM_OBJECTS ; i + + ) {
2010-12-02 18:51:44 +00:00
if ( objectspec [ i ] ! = NULL & & objectspec [ i ] - > grf_prop . grffile ! = NULL & & objectspec [ i ] - > enabled ) {
2010-08-28 18:21:09 +00:00
_object_mngr . SetEntitySpec ( objectspec [ i ] ) ;
}
}
}
}
}
2010-02-22 14:16:19 +00: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 ( )
{
const GRFFile * const * end = _grf_files . End ( ) ;
for ( GRFFile * * file = _grf_files . Begin ( ) ; file ! = end ; file + + ) {
2010-03-18 23:12:38 +00:00
AirportSpec * * & airportspec = ( * file ) - > airportspec ;
if ( airportspec ! = NULL ) {
for ( int i = 0 ; i < NUM_AIRPORTS ; i + + ) {
if ( airportspec [ i ] ! = NULL & & airportspec [ i ] - > enabled ) {
_airport_mngr . SetEntitySpec ( airportspec [ i ] ) ;
}
}
}
2010-02-22 14:16:19 +00:00
AirportTileSpec * * & airporttilespec = ( * file ) - > airtspec ;
if ( airporttilespec ! = NULL ) {
2010-05-13 10:14:29 +00:00
for ( uint i = 0 ; i < NUM_AIRPORTTILES ; i + + ) {
2010-02-22 14:16:19 +00:00
if ( airporttilespec [ i ] ! = NULL & & airporttilespec [ i ] - > enabled ) {
_airporttile_mngr . SetEntitySpec ( airporttilespec [ i ] ) ;
}
}
}
}
}
2004-08-09 17:04:08 +00: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 00:32:18 +00:00
* partial implementation yet ) .
* XXX : We consider GRF files trusted . It would be trivial to exploit OTTD by
2004-08-09 17:04:08 +00: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 13:06:49 +00:00
static void DecodeSpecialSprite ( byte * buf , uint num , GrfLoadingStage stage )
2004-08-09 17:04:08 +00:00
{
2005-08-15 11:39:13 +00: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 21:27:59 +00:00
* during stage 1 , whilst action 3 is carried out during stage 2 ( to
2005-08-15 11:39:13 +00: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 21:27:59 +00:00
* 1 and 2 in stage 2 now , let ' s hope that won ' t get us into problems .
2009-03-15 00:32:18 +00:00
* - - pasky
* We need a pre - stage to set up GOTO labels of Action 0x10 because the grf
2006-04-15 21:27:59 +00: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 22:54:57 +00:00
static const SpecialSpriteHandler handlers [ ] [ GLS_END ] = {
2007-11-17 15:03:19 +00:00
/* 0x00 */ { NULL , SafeChangeInfo , NULL , NULL , ReserveChangeInfo , FeatureChangeInfo , } ,
2007-05-05 10:47:35 +00:00
/* 0x01 */ { SkipAct1 , SkipAct1 , SkipAct1 , SkipAct1 , SkipAct1 , NewSpriteSet , } ,
/* 0x02 */ { NULL , NULL , NULL , NULL , NULL , NewSpriteGroup , } ,
2007-03-22 23:19:40 +00:00
/* 0x03 */ { NULL , GRFUnsafe , NULL , NULL , NULL , FeatureMapSpriteGroup , } ,
/* 0x04 */ { NULL , NULL , NULL , NULL , NULL , FeatureNewName , } ,
2007-04-26 06:49:32 +00:00
/* 0x05 */ { SkipAct5 , SkipAct5 , SkipAct5 , SkipAct5 , SkipAct5 , GraphicsNew , } ,
2007-03-24 23:43:33 +00:00
/* 0x06 */ { NULL , NULL , NULL , CfgApply , CfgApply , CfgApply , } ,
2007-03-22 23:19:40 +00:00
/* 0x07 */ { NULL , NULL , NULL , NULL , SkipIf , SkipIf , } ,
2007-06-05 21:41:31 +00:00
/* 0x08 */ { ScanInfo , NULL , NULL , GRFInfo , GRFInfo , GRFInfo , } ,
2007-03-22 23:19:40 +00:00
/* 0x09 */ { NULL , NULL , NULL , SkipIf , SkipIf , SkipIf , } ,
2007-04-26 06:49:32 +00:00
/* 0x0A */ { SkipActA , SkipActA , SkipActA , SkipActA , SkipActA , SpriteReplace , } ,
2007-03-22 23:19:40 +00:00
/* 0x0B */ { NULL , NULL , NULL , GRFLoadError , GRFLoadError , GRFLoadError , } ,
/* 0x0C */ { NULL , NULL , NULL , GRFComment , NULL , GRFComment , } ,
/* 0x0D */ { NULL , SafeParamSet , NULL , ParamSet , ParamSet , ParamSet , } ,
/* 0x0E */ { NULL , SafeGRFInhibit , NULL , GRFInhibit , GRFInhibit , GRFInhibit , } ,
2007-06-18 23:00:55 +00:00
/* 0x0F */ { NULL , GRFUnsafe , NULL , FeatureTownName , NULL , NULL , } ,
2007-03-22 23:19:40 +00:00
/* 0x10 */ { NULL , NULL , DefineGotoLabel , NULL , NULL , NULL , } ,
2008-01-28 15:55:59 +00:00
/* 0x11 */ { SkipAct11 , GRFUnsafe , SkipAct11 , SkipAct11 , SkipAct11 , GRFSound , } ,
/* 0x12 */ { SkipAct12 , SkipAct12 , SkipAct12 , SkipAct12 , SkipAct12 , LoadFontGlyph , } ,
2007-03-22 23:19:40 +00:00
/* 0x13 */ { NULL , NULL , NULL , NULL , NULL , TranslateGRFStrings , } ,
2010-07-31 09:35:42 +00:00
/* 0x14 */ { StaticGRFInfo , NULL , NULL , NULL , NULL , NULL , } ,
2004-08-09 17:04:08 +00:00
} ;
2005-08-15 11:39:13 +00:00
2011-08-21 12:05:47 +00:00
GRFLocation location ( _cur . grfconfig - > ident . grfid , _cur . nfo_line ) ;
2004-08-09 17:04:08 +00:00
2008-08-19 09:08:45 +00:00
GRFLineToSpriteOverride : : iterator it = _grf_line_to_action6_sprite_override . find ( location ) ;
if ( it = = _grf_line_to_action6_sprite_override . end ( ) ) {
2009-03-09 13:06:49 +00:00
/* No preloaded sprite to work with; read the
2006-05-23 20:59:01 +00:00
* pseudo sprite content . */
FioReadBlock ( buf , num ) ;
} else {
/* Use the preloaded sprite data. */
2008-08-19 09:08:45 +00:00
buf = _grf_line_to_action6_sprite_override [ location ] ;
2006-12-26 17:36:18 +00:00
grfmsg ( 7 , " DecodeSpecialSprite: Using preloaded pseudo sprite data " ) ;
2006-05-23 20:59:01 +00:00
/* Skip the real (original) content of this action. */
FioSeekTo ( num , SEEK_CUR ) ;
}
2004-11-12 15:15:56 +00:00
2010-01-22 13:02:15 +00:00
ByteReader br ( buf , buf + num ) ;
ByteReader * bufp = & br ;
2005-08-15 11:39:13 +00:00
2010-01-22 13:02:15 +00:00
try {
2010-01-23 13:17:30 +00:00
byte action = bufp - > ReadByte ( ) ;
2010-01-22 13:02:15 +00:00
if ( action = = 0xFF ) {
grfmsg ( 7 , " DecodeSpecialSprite: Handling data block in stage %d " , stage ) ;
GRFDataBlock ( bufp ) ;
} else if ( action = = 0xFE ) {
grfmsg ( 7 , " DecodeSpecialSprite: Handling import block in stage %d " , stage ) ;
GRFImportBlock ( bufp ) ;
} else if ( action > = lengthof ( handlers ) ) {
grfmsg ( 7 , " DecodeSpecialSprite: Skipping unknown action 0x%02X " , action ) ;
} else if ( handlers [ action ] [ stage ] = = NULL ) {
grfmsg ( 7 , " DecodeSpecialSprite: Skipping action 0x%02X in stage %d " , action , stage ) ;
} else {
grfmsg ( 7 , " DecodeSpecialSprite: Handling action 0x%02X in stage %d " , action , stage ) ;
handlers [ action ] [ stage ] ( bufp ) ;
}
} catch ( . . . ) {
grfmsg ( 1 , " DecodeSpecialSprite: Tried to read past end of pseudo-sprite data " ) ;
2011-05-11 20:22:04 +00:00
DisableGrf ( STR_NEWGRF_ERROR_READ_BOUNDS ) ;
2005-08-15 11:39:13 +00:00
}
}
2004-11-12 23:26:02 +00:00
2008-01-04 15:39:08 +00:00
void LoadNewGRFFile ( GRFConfig * config , uint file_index , GrfLoadingStage stage )
2005-08-15 11:39:13 +00:00
{
2007-06-23 21:12:09 +00:00
const char * filename = config - > filename ;
2005-08-15 11:39:13 +00:00
uint16 num ;
/* 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 . */
2006-12-12 19:38:41 +00:00
if ( stage ! = GLS_FILESCAN & & stage ! = GLS_SAFETYSCAN & & stage ! = GLS_LABELSCAN ) {
2011-08-21 12:05:47 +00:00
_cur . grffile = GetFileByFilename ( filename ) ;
if ( _cur . grffile = = NULL ) usererror ( " File '%s' lost in cache. \n " , filename ) ;
2007-06-05 21:41:31 +00:00
if ( stage = = GLS_RESERVE & & config - > status ! = GCS_INITIALISED ) return ;
2007-11-19 21:02:30 +00:00
if ( stage = = GLS_ACTIVATION & & ! HasBit ( config - > flags , GCF_RESERVED ) ) return ;
2011-08-21 12:05:47 +00:00
_cur . grffile - > is_ottdfile = config - > IsOpenTTDBaseGRF ( ) ;
2005-08-15 11:39:13 +00:00
}
2007-10-29 23:02:31 +00:00
if ( file_index > LAST_GRF_SLOT ) {
DEBUG ( grf , 0 , " '%s' is not loaded as the maximum number of GRFs has been reached " , filename ) ;
config - > status = GCS_DISABLED ;
2010-02-25 15:54:40 +00:00
config - > error = new GRFError ( STR_NEWGRF_ERROR_MSG_FATAL , STR_NEWGRF_ERROR_TOO_MANY_NEWGRFS_LOADED ) ;
2007-10-29 23:02:31 +00:00
return ;
}
2005-08-15 11:39:13 +00:00
FioOpenFile ( file_index , filename ) ;
2011-08-21 12:05:47 +00:00
_cur . file_index = file_index ; // XXX
_palette_remap_grf [ _cur . file_index ] = ( config - > palette & GRFP_USE_MASK ) ;
2005-08-15 11:39:13 +00:00
2011-08-21 12:05:47 +00:00
_cur . grfconfig = config ;
2006-12-04 08:30:04 +00:00
2007-04-16 20:56:19 +00:00
DEBUG ( grf , 2 , " LoadNewGRFFile: Reading NewGRF-file '%s' " , filename ) ;
2005-08-15 11:39:13 +00: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 . */
if ( FioReadWord ( ) = = 4 & & FioReadByte ( ) = = 0xFF ) {
FioReadDword ( ) ;
} else {
2007-04-16 20:56:19 +00:00
DEBUG ( grf , 7 , " LoadNewGRFFile: Custom .grf has invalid format " ) ;
2006-12-04 08:30:04 +00:00
return ;
2005-08-15 11:39:13 +00:00
}
2011-08-21 12:05:47 +00:00
_cur . skip_sprites = 0 ; // XXX
_cur . nfo_line = 0 ;
2005-08-15 11:39:13 +00:00
2009-03-09 13:06:49 +00:00
ReusableBuffer < byte > buf ;
2005-08-15 11:39:13 +00:00
while ( ( num = FioReadWord ( ) ) ! = 0 ) {
byte type = FioReadByte ( ) ;
2011-08-21 12:05:47 +00:00
_cur . nfo_line + + ;
2005-08-15 11:39:13 +00:00
if ( type = = 0xFF ) {
2011-08-21 12:05:47 +00:00
if ( _cur . skip_sprites = = 0 ) {
2009-03-09 13:06:49 +00:00
DecodeSpecialSprite ( buf . Allocate ( num ) , num , stage ) ;
2006-11-01 17:42:34 +00:00
/* Stop all processing if we are to skip the remaining sprites */
2011-08-21 12:05:47 +00:00
if ( _cur . skip_sprites = = - 1 ) break ;
2006-11-01 17:42:34 +00:00
2005-08-15 11:39:13 +00:00
continue ;
} else {
FioSkipBytes ( num ) ;
}
} else {
2011-08-21 12:05:47 +00:00
if ( _cur . skip_sprites = = 0 ) {
2008-08-29 20:03:51 +00:00
grfmsg ( 0 , " LoadNewGRFFile: Unexpected sprite, disabling " ) ;
2011-05-11 20:22:04 +00:00
DisableGrf ( STR_NEWGRF_ERROR_UNEXPECTED_SPRITE ) ;
2008-08-29 20:03:51 +00:00
break ;
}
2005-08-15 11:39:13 +00:00
FioSkipBytes ( 7 ) ;
2008-08-30 09:46:52 +00:00
SkipSpriteData ( type , num - 8 ) ;
2004-11-12 23:26:02 +00:00
}
2005-08-15 11:39:13 +00:00
2011-08-21 12:05:47 +00:00
if ( _cur . skip_sprites > 0 ) _cur . skip_sprites - - ;
2004-08-09 17:04:08 +00:00
}
2005-08-15 11:39:13 +00:00
}
2004-08-09 17:04:08 +00:00
2008-01-24 14:49:40 +00: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.
* Should not happen , as openttd ( w / d ) . grf includes some . */
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 15:13:15 +00:00
DupSprite ( SPR_ORIGINALSHORE_START + 0 , SPR_SHORE_BASE + 4 ) ; // SLOPE_E
2008-01-24 14:49:40 +00: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
}
}
2009-11-24 13:15:58 +00: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 18:27:43 +00:00
/** Features, to which '_grf_id_overrides' applies. Currently vehicle features only. */
2010-04-24 11:52:58 +00:00
static const uint32 override_features = ( 1 < < GSF_TRAINS ) | ( 1 < < GSF_ROADVEHICLES ) | ( 1 < < GSF_SHIPS ) | ( 1 < < GSF_AIRCRAFT ) ;
2009-11-24 13:15:58 +00:00
/* Evaluate grf overrides */
int num_grfs = _grf_files . Length ( ) ;
int * grf_overrides = AllocaM ( int , num_grfs ) ;
for ( int i = 0 ; i < num_grfs ; i + + ) {
grf_overrides [ i ] = - 1 ;
GRFFile * source = _grf_files [ i ] ;
uint32 override = _grf_id_overrides [ source - > grfid ] ;
if ( override = = 0 ) continue ;
GRFFile * dest = GetFileByGRFID ( override ) ;
if ( dest = = NULL ) continue ;
grf_overrides [ i ] = _grf_files . FindIndex ( dest ) ;
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-24 23:34:46 +00:00
if ( ! HasBit ( features , _price_base_specs [ p ] . grf_feature ) | | source - > price_base_multipliers [ p ] = = INVALID_PRICE_MODIFIER ) continue ;
2009-11-24 13:15:58 +00:00
DEBUG ( grf , 3 , " '%s' overrides price base multiplier %d of '%s' " , source - > filename , p , dest - > filename ) ;
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-24 23:34:46 +00:00
if ( ! HasBit ( features , _price_base_specs [ p ] . grf_feature ) | | dest - > price_base_multipliers [ p ] ! = INVALID_PRICE_MODIFIER ) continue ;
2009-11-24 13:15:58 +00:00
DEBUG ( grf , 3 , " Price base multiplier %d from '%s' propagated to '%s' " , p , source - > filename , dest - > filename ) ;
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 ] ) {
DEBUG ( grf , 3 , " Price base multiplier %d from '%s' propagated to '%s' " , p , dest - > filename , source - > filename ) ;
}
source - > price_base_multipliers [ p ] = dest - > price_base_multipliers [ p ] ;
}
}
2009-11-24 22:15:42 +00:00
/* Apply fallback prices */
2009-11-24 13:15:58 +00:00
const GRFFile * const * end = _grf_files . End ( ) ;
2009-11-24 22:15:42 +00:00
for ( GRFFile * * file = _grf_files . Begin ( ) ; file ! = end ; file + + ) {
PriceMultipliers & price_base_multipliers = ( * file ) - > price_base_multipliers ;
for ( Price p = PR_BEGIN ; p < PR_END ; p + + ) {
Price fallback_price = _price_base_specs [ p ] . fallback_price ;
2009-11-24 23:34:46 +00:00
if ( fallback_price ! = INVALID_PRICE & & price_base_multipliers [ p ] = = INVALID_PRICE_MODIFIER ) {
2009-11-24 22:15:42 +00: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 */
2009-11-24 13:15:58 +00:00
for ( GRFFile * * file = _grf_files . Begin ( ) ; file ! = end ; file + + ) {
PriceMultipliers & price_base_multipliers = ( * file ) - > price_base_multipliers ;
for ( Price p = PR_BEGIN ; p < PR_END ; p + + ) {
2009-11-24 23:34:46 +00:00
if ( price_base_multipliers [ p ] = = INVALID_PRICE_MODIFIER ) {
2009-11-24 13:15:58 +00:00
/* No multiplier was set; set it to a neutral value */
price_base_multipliers [ p ] = 0 ;
} else {
if ( ! HasBit ( ( * file ) - > grf_features , _price_base_specs [ p ] . grf_feature ) ) {
/* The grf does not define any objects of the feature,
* so it must be a difficulty setting . Apply it globally */
DEBUG ( grf , 3 , " '%s' sets global price base multiplier %d " , ( * file ) - > filename , p ) ;
SetPriceBaseMultiplier ( p , price_base_multipliers [ p ] ) ;
price_base_multipliers [ p ] = 0 ;
} else {
DEBUG ( grf , 3 , " '%s' sets local price base multiplier %d " , ( * file ) - > filename , p ) ;
}
}
}
}
}
2007-03-07 11:47:46 +00:00
void InitDepotWindowBlockSizes ( ) ;
2005-08-15 11:39:13 +00:00
2008-01-17 02:10:26 +00:00
extern void InitGRFTownGeneratorNames ( ) ;
2007-06-18 23:00:55 +00:00
2007-03-19 11:27:30 +00:00
static void AfterLoadGRFs ( )
{
2008-04-17 11:47:22 +00:00
for ( StringIDToGRFIDMapping : : iterator it = _string_to_grf_mapping . begin ( ) ; it ! = _string_to_grf_mapping . end ( ) ; it + + ) {
* ( ( * it ) . first ) = MapGRFStringID ( ( * it ) . second , * ( ( * it ) . first ) ) ;
}
_string_to_grf_mapping . clear ( ) ;
2008-08-19 09:08:45 +00:00
/* Free the action 6 override sprites. */
for ( GRFLineToSpriteOverride : : iterator it = _grf_line_to_action6_sprite_override . begin ( ) ; it ! = _grf_line_to_action6_sprite_override . end ( ) ; it + + ) {
free ( ( * it ) . second ) ;
}
_grf_line_to_action6_sprite_override . clear ( ) ;
2010-05-21 20:51:00 +00:00
/* Polish cargos */
FinaliseCargoArray ( ) ;
2007-03-19 11:27:30 +00:00
/* Pre-calculate all refit masks after loading GRF files. */
CalculateRefitMasks ( ) ;
2010-05-21 20:51:00 +00:00
/* Polish engines */
FinaliseEngineArray ( ) ;
2007-03-19 11:27:30 +00:00
/* Set the block size in the depot windows based on vehicle sprite sizes */
InitDepotWindowBlockSizes ( ) ;
/* Add all new houses to the house array. */
FinaliseHouseArray ( ) ;
2007-03-22 23:19:40 +00:00
2007-07-06 20:40:58 +00:00
/* Add all new industries to the industry array. */
FinaliseIndustriesArray ( ) ;
2010-08-28 18:21:09 +00:00
/* Add all new objects to the object array. */
FinaliseObjectsArray ( ) ;
2010-04-01 19:48:28 +00:00
InitializeSortedCargoSpecs ( ) ;
2010-03-28 12:25:09 +00:00
/* Sort the list of industry types. */
SortIndustryTypes ( ) ;
2007-04-26 20:20:12 +00:00
/* Create dynamic list of industry legends for smallmap_gui.cpp */
BuildIndustriesLegend ( ) ;
2010-02-22 14:16:19 +00:00
/* Add all new airports to the airports array. */
FinaliseAirportsArray ( ) ;
2010-03-05 23:21:35 +00:00
BindAirportSpecs ( ) ;
2010-02-22 14:16:19 +00:00
2007-06-18 23:00:55 +00:00
/* Update the townname generators list */
2008-01-17 02:10:26 +00:00
InitGRFTownGeneratorNames ( ) ;
2008-01-24 14:49:40 +00:00
2008-04-29 21:31:29 +00:00
/* Run all queued vehicle list order changes */
2008-07-20 21:23:28 +00:00
CommitVehicleListOrderChanges ( ) ;
2008-04-29 21:31:29 +00:00
2008-01-24 14:49:40 +00:00
/* Load old shore sprites in new position, if they were replaced by ActionA */
ActivateOldShore ( ) ;
2008-04-22 18:20:41 +00:00
2010-02-07 22:22:54 +00:00
/* Set up custom rail types */
InitRailTypes ( ) ;
2009-01-06 14:45:38 +00:00
Engine * e ;
FOR_ALL_ENGINES_OF_TYPE ( e , VEH_ROAD ) {
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 ;
}
}
2010-03-23 11:05:28 +00:00
FOR_ALL_ENGINES_OF_TYPE ( e , VEH_TRAIN ) {
RailType railtype = GetRailTypeByLabel ( _gted [ e - > index ] . railtypelabel ) ;
if ( railtype = = INVALID_RAILTYPE ) {
/* Rail type is not available, so disable this engine */
2011-03-13 22:05:29 +00:00
e - > info . climates = 0 ;
2010-03-23 11:05:28 +00:00
} else {
e - > u . rail . railtype = railtype ;
}
}
2009-01-08 21:48:59 +00:00
SetYearEngineAgingStops ( ) ;
2009-11-24 13:15:58 +00:00
FinalisePriceBaseMultipliers ( ) ;
2008-04-22 18:20:41 +00:00
/* Deallocate temporary loading data */
free ( _gted ) ;
2008-05-05 19:02:10 +00:00
_grm_sprites . clear ( ) ;
2007-03-19 11:27:30 +00:00
}
2005-08-15 11:39:13 +00:00
void LoadNewGRF ( uint load_index , uint file_index )
{
2008-12-29 20:36:12 +00: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 . */
Date date = _date ;
Year year = _cur_year ;
DateFract date_fract = _date_fract ;
uint16 tick_counter = _tick_counter ;
byte display_opt = _display_opt ;
if ( _networking ) {
_cur_year = _settings_game . game_creation . starting_year ;
_date = ConvertYMDToDate ( _cur_year , 0 , 1 ) ;
_date_fract = 0 ;
_tick_counter = 0 ;
_display_opt = 0 ;
}
2006-01-30 14:50:55 +00:00
InitializeGRFSpecial ( ) ;
2005-08-15 11:39:13 +00:00
2005-09-26 18:43:58 +00:00
ResetNewGRFData ( ) ;
2007-12-29 23:49:48 +00: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
* and a previously disabled NewGRF becomes useable . If it would not
* be reset , the NewGRF would remain disabled even though it should
* have been enabled .
*/
for ( GRFConfig * c = _grfconfig ; c ! = NULL ; c = c - > next ) {
if ( c - > status ! = GCS_NOT_FOUND ) c - > status = GCS_UNKNOWN ;
}
2011-08-21 12:05:47 +00:00
_cur . spriteid = load_index ;
2008-05-05 19:02:10 +00:00
2005-08-15 11:39:13 +00: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 22:21:46 +00:00
for ( GrfLoadingStage stage = GLS_LABELSCAN ; stage < = GLS_ACTIVATION ; stage + + ) {
2008-03-04 10:41:16 +00:00
/* Set activated grfs back to will-be-activated between reservation- and activation-stage.
* This ensures that action7 / 9 conditions 0x06 - 0x0A work correctly . */
for ( GRFConfig * c = _grfconfig ; c ! = NULL ; c = c - > next ) {
if ( c - > status = = GCS_ACTIVATED ) c - > status = GCS_INITIALISED ;
}
2005-09-09 06:39:47 +00:00
uint slot = file_index ;
2005-08-15 11:39:13 +00:00
2011-08-21 12:05:47 +00:00
_cur . stage = stage ;
2007-03-10 09:55:11 +00:00
for ( GRFConfig * c = _grfconfig ; c ! = NULL ; c = c - > next ) {
2007-03-06 19:33:28 +00:00
if ( c - > status = = GCS_DISABLED | | c - > status = = GCS_NOT_FOUND ) continue ;
2007-11-19 21:02:30 +00:00
if ( stage > GLS_INIT & & HasBit ( c - > flags , GCF_INIT_ONLY ) ) continue ;
2006-12-04 08:30:04 +00:00
2009-06-18 09:47:06 +00:00
if ( ! FioCheckFileExists ( c - > filename ) ) {
DEBUG ( grf , 0 , " NewGRF file is missing '%s'; disabling " , c - > filename ) ;
c - > status = GCS_NOT_FOUND ;
continue ;
}
2006-04-22 13:17:13 +00:00
2011-05-01 20:46:36 +00:00
if ( stage = = GLS_LABELSCAN ) InitNewGRFFile ( c ) ;
2008-01-04 15:39:08 +00:00
LoadNewGRFFile ( c , slot + + , stage ) ;
2007-06-07 18:13:31 +00:00
if ( stage = = GLS_RESERVE ) {
2007-11-20 13:35:54 +00:00
SetBit ( c - > flags , GCF_RESERVED ) ;
2007-11-04 22:21:46 +00:00
} else if ( stage = = GLS_ACTIVATION ) {
2007-11-19 21:32:20 +00:00
ClrBit ( c - > flags , GCF_RESERVED ) ;
2011-08-21 12:05:47 +00:00
assert ( GetFileByGRFID ( c - > ident . grfid ) = = _cur . grffile ) ;
ClearTemporaryNewGRFData ( _cur . grffile ) ;
2007-02-24 23:20:21 +00:00
BuildCargoTranslationMap ( ) ;
2011-08-21 12:05:47 +00:00
DEBUG ( sprite , 2 , " LoadNewGRF: Currently %i sprites are loaded " , _cur . spriteid ) ;
2009-01-24 17:02:59 +00: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 12:05:47 +00:00
ClearTemporaryNewGRFData ( _cur . grffile ) ;
2007-01-02 20:00:10 +00:00
}
2005-08-15 11:39:13 +00:00
}
}
2005-11-06 13:42:26 +00:00
2007-03-19 11:27:30 +00:00
/* Call any functions that should be run after GRFs have been loaded. */
AfterLoadGRFs ( ) ;
2008-12-29 20:36:12 +00:00
/* Now revert back to the original situation */
_cur_year = year ;
_date = date ;
_date_fract = date_fract ;
_tick_counter = tick_counter ;
_display_opt = display_opt ;
2004-08-09 17:04:08 +00:00
}
2007-04-27 21:35:02 +00:00
bool HasGrfMiscBit ( GrfMiscBit bit )
{
2007-11-19 21:02:30 +00:00
return HasBit ( _misc_grf_features , bit ) ;
2007-04-27 21:35:02 +00:00
}