# newgrf.cpp File Reference

`#include "stdafx.h"`
`#include <stdarg.h>`
`#include "openttd.h"`
`#include "debug.h"`
`#include "fileio.h"`
`#include "engine.h"`
`#include "spritecache.h"`
`#include "station.h"`
`#include "sprite.h"`
`#include "newgrf.h"`
`#include "variables.h"`
`#include "bridge.h"`
`#include "town.h"`
`#include "newgrf_engine.h"`
`#include "newgrf_text.h"`
`#include "fontcache.h"`
`#include "currency.h"`
`#include "landscape.h"`
`#include "newgrf_config.h"`
`#include "newgrf_house.h"`
`#include "newgrf_sound.h"`
`#include "newgrf_spritegroup.h"`
`#include "cargotype.h"`
`#include "industry.h"`
`#include "newgrf_canal.h"`
`#include "newgrf_commons.h"`
`#include "newgrf_townname.h"`
`#include "newgrf_industries.h"`
`#include "gfxinit.h"`
`#include "fios.h"`
`#include "rail.h"`
`#include "strings_func.h"`
`#include "gfx_func.h"`
`#include "date_func.h"`
`#include "vehicle_func.h"`
`#include "sound_func.h"`
`#include "string_func.h"`
`#include "road_func.h"`
`#include "player_base.h"`
`#include "settings_type.h"`
`#include "network/network.h"`
`#include "map_func.h"`
`#include "table/strings.h"`
`#include "table/sprites.h"`
`#include "table/town_land.h"`
`#include "table/build_industry.h"`
`#include "table/landscape_sprite.h"`

Go to the source code of this file.

## Data Structures
struct | GRFLocation
## Defines
#define | TEXID_TO_STRINGID(begin, end, stringid) if (str >= begin && str <= end) return str + (stringid - begin)
## Typedefs
typedef void(* | SpecialSpriteHandler )(byte *buf, int len)
typedef std::map< GRFLocation, byte * > | GRFLineToSpriteOverride
typedef bool(* | VCI_Handler )(uint engine, int numinfo, int prop, byte **buf, int len)
## Enumerations
enum | GrfDataType { GDT_SOUND }
enum | { MAX_STATIONS = 256 }
## Functions
void CDECL | grfmsg (int severity, const char *str,...)
| DEBUG() function dedicated to newGRF debugging messages Function is essentialy the same as DEBUG(grf, severity, .
static bool | check_length (int real, int wanted, const char *str)
static byte | grf_load_byte (byte **buf)
static uint16 | grf_load_word (byte **buf)
static uint16 | grf_load_extended (byte **buf)
static uint32 | grf_load_dword (byte **buf)
static uint32 | grf_load_var (byte size, byte **buf)
static const char * | grf_load_string (byte **buf, size_t max_len)
static GRFFile * | GetFileByGRFID (uint32 grfid)
static GRFFile * | GetFileByFilename (const char *filename)
static void | ClearTemporaryNewGRFData ()
| Reset all NewGRFData that was used only while processing data.
StringID | MapGRFStringID (uint32 grfid, StringID str)
| Used when setting an object's property to map to the GRF's strings while taking in consideration the "drift" between TTDPatch string system and OpenTTD's one.
static uint8 | MapDOSColour (uint8 colour)
static void | MapSpriteMappingRecolour (PalSpriteID *grf_sprite)
| Map the colour modifiers of TTDPatch to those that Open is using.
static void | dewagonize (int condition, int engine)
static bool | RailVehicleChangeInfo (uint engine, int numinfo, int prop, byte **bufp, int len)
static bool | RoadVehicleChangeInfo (uint engine, int numinfo, int prop, byte **bufp, int len)
static bool | ShipVehicleChangeInfo (uint engine, int numinfo, int prop, byte **bufp, int len)
static bool | AircraftVehicleChangeInfo (uint engine, int numinfo, int prop, byte **bufp, int len)
static bool | StationChangeInfo (uint stid, int numinfo, int prop, byte **bufp, int len)
static bool | CanalChangeInfo (uint id, int numinfo, int prop, byte **bufp, int len)
static bool | BridgeChangeInfo (uint brid, int numinfo, int prop, byte **bufp, int len)
static bool | TownHouseChangeInfo (uint hid, int numinfo, int prop, byte **bufp, int len)
static bool | GlobalVarChangeInfo (uint gvid, int numinfo, int prop, byte **bufp, int len)
static bool | CargoChangeInfo (uint cid, int numinfo, int prop, byte **bufp, int len)
static bool | SoundEffectChangeInfo (uint sid, int numinfo, int prop, byte **bufp, int len)
static bool | IndustrytilesChangeInfo (uint indtid, int numinfo, int prop, byte **bufp, int len)
static bool | IndustriesChangeInfo (uint indid, int numinfo, int prop, byte **bufp, int len)
static void | FeatureChangeInfo (byte *buf, int len)
static void | SafeChangeInfo (byte *buf, int len)
static void | ReserveChangeInfo (byte *buf, int len)
static const SpriteGroup * | NewCallBackResultSpriteGroup (uint16 value)
| Creates a spritegroup representing a callback result.
static const SpriteGroup * | NewResultSpriteGroup (SpriteID sprite, byte num_sprites)
| Creates a spritegroup representing a sprite number result.
static void | NewSpriteSet (byte *buf, int len)
static void | SkipAct1 (byte *buf, int len)
static const SpriteGroup * | GetGroupFromGroupID (byte setid, byte type, uint16 groupid)
static const SpriteGroup * | CreateGroupFromGroupID (byte feature, byte setid, byte type, uint16 spriteid, uint16 num_sprites)
static void | NewSpriteGroup (byte *buf, int len)
static CargoID | TranslateCargo (uint8 feature, uint8 ctype)
static void | VehicleMapSpriteGroup (byte *buf, byte feature, uint8 idcount, uint8 cidcount, bool wagover)
static void | CanalMapSpriteGroup (byte *buf, uint8 idcount, uint8 cidcount)
static void | StationMapSpriteGroup (byte *buf, uint8 idcount, uint8 cidcount)
static void | TownHouseMapSpriteGroup (byte *buf, uint8 idcount, uint8 cidcount)
static void | IndustryMapSpriteGroup (byte *buf, uint8 idcount, uint8 cidcount)
static void | IndustrytileMapSpriteGroup (byte *buf, uint8 idcount, uint8 cidcount)
static void | CargoMapSpriteGroup (byte *buf, uint8 idcount, uint8 cidcount)
static void | FeatureMapSpriteGroup (byte *buf, int len)
static void | FeatureNewName (byte *buf, int len)
static uint16 | SanitizeSpriteOffset (uint16 &num, uint16 offset, int max_sprites, const char *name)
| Sanitize incoming sprite offsets for Action 5 graphics replacements.
static void | GraphicsNew (byte *buf, int len)
static void | SkipAct5 (byte *buf, int len)
bool | GetGlobalVariable (byte param, uint32 *value)
| Reads a variable common to VarAction2 and Action7/9/D.
static uint32 | GetParamVal (byte param, uint32 *cond_val)
static void | CfgApply (byte *buf, int len)
static void | DisableStaticNewGRFInfluencingNonStaticNewGRFs (GRFConfig *c)
| Disable a static NewGRF when it is influencing another (non-static) NewGRF as this could cause desyncs.
static void | SkipIf (byte *buf, int len)
static void | ScanInfo (byte *buf, int len)
static void | GRFInfo (byte *buf, int len)
static void | SpriteReplace (byte *buf, int len)
static void | SkipActA (byte *buf, int len)
static void | GRFLoadError (byte *buf, int len)
static void | GRFComment (byte *buf, int len)
static void | SafeParamSet (byte *buf, int len)
static uint32 | GetPatchVariable (uint8 param)
static uint32 | PerformGRM (uint32 *grm, uint16 num_ids, uint16 count, uint8 op, uint8 target, const char *type)
static void | ParamSet (byte *buf, int len)
static void | SafeGRFInhibit (byte *buf, int len)
static void | GRFInhibit (byte *buf, int len)
static void | FeatureTownName (byte *buf, int len)
static void | DefineGotoLabel (byte *buf, int len)
static void | GRFSound (byte *buf, int len)
static void | SkipAct11 (byte *buf, int len)
static void | ImportGRFSound (byte *buf, int len)
static void | GRFImportBlock (byte *buf, int len)
static void | LoadGRFSound (byte *buf, int len)
static void | LoadFontGlyph (byte *buf, int len)
static void | SkipAct12 (byte *buf, int len)
static void | TranslateGRFStrings (byte *buf, int len)
static void | GRFDataBlock (byte *buf, int len)
static void | GRFUnsafe (byte *buf, int len)
static void | InitializeGRFSpecial ()
static void | ResetCustomStations ()
static void | ResetCustomHouses ()
static void | ResetCustomIndustries ()
static void | ResetNewGRF ()
static void | ResetNewGRFErrors ()
static void | ResetNewGRFData ()
| Reset all NewGRF loaded data TODO.
static void | BuildCargoTranslationMap ()
static void | InitNewGRFFile (const GRFConfig *config, int sprite_offset)
static void | CalculateRefitMasks ()
| Precalculate refit masks from cargo classes for all vehicles.
static void | FinaliseHouseArray ()
| Add all new houses to the house array.
static void | FinaliseIndustriesArray ()
| Add all new industries to the industry array.
static void | MapNewCargoStrings ()
| Each cargo string needs to be mapped from TTDPatch to OpenTTD string IDs.
static void | DecodeSpecialSprite (uint num, GrfLoadingStage stage)
void | LoadNewGRFFile (GRFConfig *config, uint file_index, GrfLoadingStage stage)
static void | ActivateOldShore ()
| Relocates the old shore sprites at new positions.
void | InitDepotWindowBlockSizes ()
void | InitGRFTownGeneratorNames ()
static void | AfterLoadGRFs ()
void | LoadNewGRF (uint load_index, uint file_index)
bool | HasGrfMiscBit (GrfMiscBit bit)
## Variables
static int | _skip_sprites
static uint | _file_index
static GRFFile * | _cur_grffile
GRFFile * | _first_grffile
static SpriteID | _cur_spriteid
static GrfLoadingStage | _cur_stage
static uint32 | _nfo_line
static GRFConfig * | _cur_grfconfig
static byte | _misc_grf_features = 0
static uint32 | _ttdpatch_flags [8]
GRFLoadedFeatures | _loaded_newgrf_features
static byte | _grf_data_blocks
static GrfDataType | _grf_data_type
static const uint | _vehcounts [4]
static const uint | _vehshifts [4]
static uint16 | cargo_allowed [TOTAL_NUM_ENGINES]
static uint16 | cargo_disallowed [TOTAL_NUM_ENGINES]
static uint32 | _grm_engines [TOTAL_NUM_ENGINES]
static uint32 | _grm_cargos [NUM_CARGO *2]
GRFLineToSpriteOverride | _grf_line_to_action6_sprite_override
static const CargoLabel | _default_refitmasks_rail []
| List of what cargo labels are refittable for the given the vehicle-type.
static const CargoLabel | _default_refitmasks_road []
static const CargoLabel | _default_refitmasks_ships []
static const CargoLabel | _default_refitmasks_aircraft []
static const CargoLabel * | _default_refitmasks []

## Detailed Description

Definition in file newgrf.cpp.

## Function Documentation

static void ActivateOldShore | ( |  | ) | `[static]`

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)

Definition at line 5800 of file newgrf.cpp.

References SHORE_REPLACE_ACTION_5, SHORE_REPLACE_ACTION_A, and SHORE_REPLACE_NONE.

static void DisableStaticNewGRFInfluencingNonStaticNewGRFs | ( | GRFConfig * | c | ) | `[static]`

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.

Parameters:

  | c | the NewGRF to disable.

Definition at line 3728 of file newgrf.cpp.

References ClearTemporaryNewGRFData(), and GCS_DISABLED.

static void FinaliseHouseArray | ( |  | ) | `[static]`

Add all new houses to the house array.

House properties can be set at any 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 the TTDPatch behaviour described below that we need to emulate.

Definition at line 5510 of file newgrf.cpp.

static void FinaliseIndustriesArray | ( |  | ) | `[static]`

Add all new industries to the industry array.

Industry properties can be set at any time in the GRF file, so we can only add a industry spec to the industry array after the file has finished loading.

Definition at line 5548 of file newgrf.cpp.

References IndustrySpec::closure_text, IndustrySpec::conflicting, IndustrySpec::enabled, GetGRFStringID(), IndustrySpec::grf_prop, GRFFileProps::grffile, GRFMappedStringID::MapString(), IndustrySpec::name, IndustrySpec::new_industry_text, NUM_INDUSTRYTILES, IndustrySpec::production_down_text, IndustrySpec::production_up_text, and IndustrySpec::station_name.

bool GetGlobalVariable | ( | byte | param,
|  | uint32 * | value |
| ) |  |  |

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.

Parameters:

  | param | variable number (as for VarAction2, for Action7/9/D you have to subtract 0x80 first).
  | value | returns the value of the variable.

Returns:
  true iff the variable is known and the value is returned in 'value'.

Definition at line 3472 of file newgrf.cpp.

References Clamp(), DAYS_TILL_ORIGINAL_BASE_YEAR, GB(), max(), ORIGINAL_BASE_YEAR, ORIGINAL_MAX_YEAR, and SB().

static void GraphicsNew | ( | byte * | buf,
|  | int | len |
| ) |  |  | `[static]`

< Only allow replacing a whole block of sprites. (TTDP compatible)

< Allow replacing any subset by specifiing an offset.

< unknown/not-implemented type

< How is this Action5 type processed?

< Load the sprites starting from this sprite.

< If the Action5 contains less sprites, the whole block will be ignored.

< If the Action5 contains more sprites, only the first max_sprites sprites will be used.

< Name for error messages.

Definition at line 3334 of file newgrf.cpp.

References ClrBit(), grfmsg(), HasBit(), SanitizeSpriteOffset(), SHORE_REPLACE_ACTION_5, SHORE_REPLACE_NONE, and SHORE_REPLACE_ONLY_NEW.

void CDECL grfmsg | ( | int | severity,
|  | const char * | str,
|  |  | ... |
| ) |  |  |

DEBUG() function dedicated to newGRF debugging messages Function is essentialy the same as DEBUG(grf, severity, .

..) with the 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.

Parameters:

  | severity | debugging severity level, see debug.h
  | str | message in printf() format

Definition at line 151 of file newgrf.cpp.

References buf.

Referenced by AddGRFString(), AllocateStationClass(), GraphicsNew(), RailVehicleChangeInfo(), RoadVehicleChangeInfo(), SanitizeSpriteOffset(), ShipVehicleChangeInfo(), and TownHouseChangeInfo().

StringID MapGRFStringID | ( | uint32 | grfid,
|  | StringID | str |
| ) |  |  |

Used when setting an object's property to map to the GRF's strings while taking in consideration the "drift" between TTDPatch string system and OpenTTD's one.

Parameters:

  | grfid | Id of the grf file
  | str | StringID that we want to have the equivalent in OoenTTD

Returns:
  the properly adjusted StringID

Definition at line 269 of file newgrf.cpp.

References GB(), and GetGRFStringID().

Referenced by ChangeIndustryProduction(), and GRFMappedStringID::MapString().

static void MapNewCargoStrings | ( |  | ) | `[static]`

Each cargo string needs to be mapped from TTDPatch to OpenTTD string IDs.

This is done after loading so that strings from Action 4 will be mapped properly.

Definition at line 5617 of file newgrf.cpp.

static void MapSpriteMappingRecolour | ( | PalSpriteID * | grf_sprite | ) | `[static]`

Map the colour modifiers of TTDPatch to those that Open is using.

Parameters:

  | grf_sprite | pointer to the structure been modified

Definition at line 341 of file newgrf.cpp.

References ClrBit(), HasBit(), PALETTE_MODIFIER_COLOR, PALETTE_MODIFIER_TRANSPARENT, SetBit(), and SPRITE_MODIFIER_OPAQUE.

static const SpriteGroup* NewCallBackResultSpriteGroup | ( | uint16 | value | ) | `[static]`

Creates a spritegroup representing a callback result.

Parameters:

  | value | The value that was used to represent this callback result

Returns:
  A spritegroup representing that callback result

Definition at line 2358 of file newgrf.cpp.

static const SpriteGroup* NewResultSpriteGroup | ( | SpriteID | sprite,
|  | byte | num_sprites |
| ) |  |  | `[static]`

Creates a spritegroup representing a sprite number result.

Parameters:

  | sprite | The sprite number.
  | num_sprites | The number of sprites per set.

Returns:
  A spritegroup representing the sprite number result.

Definition at line 2383 of file newgrf.cpp.

static bool RailVehicleChangeInfo | ( | uint | engine,
|  | int | numinfo,
|  | int | prop,
|  | byte ** | bufp,
|  | int | len |
| ) |  |  | `[static]`

Todo:
  Air drag for trains.

See also:
  note in engine.h about rvi->visual_effect

Definition at line 376 of file newgrf.cpp.

References EngineInfo::base_intro, EngineInfo::callbackmask, EF_USES_2CC, grfmsg(), HasBit(), EngineInfo::misc_flags, RAILTYPE_ELECTRIC, RAILTYPE_MAGLEV, RAILTYPE_MONO, RAILTYPE_RAIL, RAILVEH_MULTIHEAD, RAILVEH_SINGLEHEAD, RAILVEH_WAGON, EngineInfo::refit_cost, EngineInfo::refit_mask, EngineInfo::retire_early, and SB().

static bool RoadVehicleChangeInfo | ( | uint | engine,
|  | int | numinfo,
|  | int | prop,
|  | byte ** | bufp,
|  | int | len |
| ) |  |  | `[static]`

Todo:
  Support for road vehicles realistic power computations (called rvpower in TTDPatch) is just missing in OTTD yet. --pasky

Todo:
  Tractive effort and air drag for road vehicles.

Definition at line 609 of file newgrf.cpp.

References EngineInfo::base_intro, EngineInfo::callbackmask, EF_USES_2CC, grfmsg(), HasBit(), EngineInfo::misc_flags, EngineInfo::refit_cost, EngineInfo::refit_mask, and EngineInfo::retire_early.

static uint16 SanitizeSpriteOffset | ( | uint16 & | num,
|  | uint16 | offset,
|  | int | max_sprites,
|  | const char * | name |
| ) |  |  | `[static]`

Sanitize incoming sprite offsets for Action 5 graphics replacements.

Parameters:

  | num | the number of sprites to load.
  | offset | offset from the base.
  | max_sprites | the maximum number of sprites that can be loaded in this action 5.
  | name | used for error warnings.

Returns:
  the number of sprites that is going to be skipped

Definition at line 3313 of file newgrf.cpp.

References grfmsg(), and max().

Referenced by GraphicsNew().

static bool ShipVehicleChangeInfo | ( | uint | engine,
|  | int | numinfo,
|  | int | prop,
|  | byte ** | bufp,
|  | int | len |
| ) |  |  | `[static]`

Todo:
  Speed fractions for ships on oceans and canals

Definition at line 739 of file newgrf.cpp.

References EngineInfo::base_intro, EngineInfo::callbackmask, EF_USES_2CC, grfmsg(), HasBit(), EngineInfo::misc_flags, EngineInfo::refit_cost, EngineInfo::refit_mask, and EngineInfo::retire_early.

static bool TownHouseChangeInfo | ( | uint | hid,
|  | int | numinfo,
|  | int | prop,
|  | byte ** | bufp,
|  | int | len |
| ) |  |  | `[static]`

New houses do not (currently) expect to have a default start date before 1930, as this breaks the build date stuff.

See also:
  FinaliseHouseArray() for more details.

Definition at line 1287 of file newgrf.cpp.

References abs(), Clamp(), GB(), grfmsg(), and ORIGINAL_BASE_YEAR.

## Variable Documentation

const CargoLabel* _default_refitmasks[]`[static]`

Initial value:

     {
      _default_refitmasks_rail,
      _default_refitmasks_road,
      _default_refitmasks_ships,
      _default_refitmasks_aircraft,
    }

Definition at line 5409 of file newgrf.cpp.

const CargoLabel _default_refitmasks_aircraft[]`[static]`

Initial value:

     {
      'PASS', 'MAIL', 'GOOD', 'VALU', 'GOLD', 'DIAM', 'FOOD', 'FRUT', 'SUGR',
      'TOYS', 'BATT', 'SWET', 'TOFF', 'COLA', 'CTCD', 'BUBL', 'PLST', 'FZDR',
      0 }

Definition at line 5404 of file newgrf.cpp.

const CargoLabel _default_refitmasks_rail[]`[static]`

Initial value:

     {
      '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 }

List of what cargo labels are refittable for the given the vehicle-type.

Only currently active labels are applied.

Definition at line 5387 of file newgrf.cpp.

const CargoLabel _default_refitmasks_road[]`[static]`

Initial value:

     {
      0 }

Definition at line 5394 of file newgrf.cpp.

const CargoLabel _default_refitmasks_ships[]`[static]`

Initial value:

     {
      '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 }

Definition at line 5397 of file newgrf.cpp.

const uint _vehcounts[4]`[static]`

Initial value:

     {
          NUM_TRAIN_ENGINES,
           NUM_ROAD_ENGINES,
           NUM_SHIP_ENGINES,
       NUM_AIRCRAFT_ENGINES
    }

Definition at line 96 of file newgrf.cpp.

const uint _vehshifts[4]`[static]`

Initial value:

     {
          0,
           ROAD_ENGINES_INDEX,
           SHIP_ENGINES_INDEX,
       AIRCRAFT_ENGINES_INDEX,
    }

Definition at line 103 of file newgrf.cpp.