# newgrf.cpp

Go to the documentation of this file.

    00001 /* $Id: newgrf.cpp 18046 2009-11-11 21:45:50Z rubidium $ */
    00002
    00005 #include "stdafx.h"
    00006
    00007 #include <stdarg.h>
    00008
    00009 #include "openttd.h"
    00010 #include "debug.h"
    00011 #include "fileio_func.h"
    00012 #include "engine_func.h"
    00013 #include "engine_base.h"
    00014 #include "spritecache.h"
    00015 #include "variables.h"
    00016 #include "bridge.h"
    00017 #include "town.h"
    00018 #include "newgrf_engine.h"
    00019 #include "newgrf_text.h"
    00020 #include "fontcache.h"
    00021 #include "currency.h"
    00022 #include "landscape.h"
    00023 #include "newgrf_house.h"
    00024 #include "newgrf_sound.h"
    00025 #include "newgrf_station.h"
    00026 #include "industry.h"
    00027 #include "newgrf_canal.h"
    00028 #include "newgrf_commons.h"
    00029 #include "newgrf_townname.h"
    00030 #include "newgrf_industries.h"
    00031 #include "rev.h"
    00032 #include "fios.h"
    00033 #include "rail.h"
    00034 #include "strings_func.h"
    00035 #include "gfx_func.h"
    00036 #include "date_func.h"
    00037 #include "vehicle_func.h"
    00038 #include "sound_func.h"
    00039 #include "string_func.h"
    00040 #include "network/network.h"
    00041 #include "map_func.h"
    00042 #include <map>
    00043 #include "core/alloc_type.hpp"
    00044 #include "core/mem_func.hpp"
    00045
    00046 #include "table/strings.h"
    00047 #include "table/build_industry.h"
    00048
    00049 /* TTDPatch extended GRF format codec
    00050  * (c) Petr Baudis 2004 (GPL'd)
    00051  * Changes by Florian octo Forster are (c) by the OpenTTD development team.
    00052  *
    00053  * Contains portions of documentation by TTDPatch team.
    00054  * Thanks especially to Josef Drexler for the documentation as well as a lot
    00055  * of help at #tycoon. Also thanks to Michael Blunck for is GRF files which
    00056  * served as subject to the initial testing of this codec. */
    00057
    00058
    00059 static int _skip_sprites; // XXX
    00060 static uint _file_index; // XXX
    00061
    00062 static GRFFile *_cur_grffile;
    00063 GRFFile *_first_grffile;
    00064 static SpriteID _cur_spriteid;
    00065 static GrfLoadingStage _cur_stage;
    00066 static uint32 _nfo_line;
    00067
    00068 static GRFConfig *_cur_grfconfig;
    00069
    00070 /* Miscellaneous GRF features, set by Action 0x0D, parameter 0x9E */
    00071 static byte _misc_grf_features = 0;
    00072
    00073 /* 32 * 8 = 256 flags. Apparently TTDPatch uses this many.. */
    00074 static uint32 _ttdpatch_flags[8];
    00075
    00076 /* Indicates which are the newgrf features currently loaded ingame */
    00077 GRFLoadedFeatures _loaded_newgrf_features;
    00078
    00079 enum GrfDataType {
    00080   GDT_SOUND,
    00081 };
    00082
    00083 static byte _grf_data_blocks;
    00084 static GrfDataType _grf_data_type;
    00085
    00086
    00087 typedef void (*SpecialSpriteHandler)(byte *buf, size_t len);
    00088
    00089 enum {
    00090   MAX_STATIONS = 256,
    00091 };
    00092
    00093 /* Temporary data used when loading only */
    00094 struct GRFTempEngineData {
    00095   uint16 cargo_allowed;
    00096   uint16 cargo_disallowed;
    00097   bool refitmask_valid;
    00098   uint8 rv_max_speed;
    00099 };
    00100
    00101 static GRFTempEngineData *_gted;
    00102
    00103 /* Contains the GRF ID of the owner of a vehicle if it has been reserved.
    00104  * GRM for vehicles is only used if dynamic engine allocation is disabled,
    00105  * so 256 is the number of original engines. */
    00106 static uint32 _grm_engines[256];
    00107
    00108 /* Contains the GRF ID of the owner of a cargo if it has been reserved */
    00109 static uint32 _grm_cargos[NUM_CARGO * 2];
    00110
    00111 struct GRFLocation {
    00112   uint32 grfid;
    00113   uint32 nfoline;
    00114
    00115   GRFLocation(uint32 grfid, uint32 nfoline) : grfid(grfid), nfoline(nfoline) { }
    00116
    00117   bool operator<(const GRFLocation &other) const
    00118   {
    00119     return this->grfid < other.grfid || (this->grfid == other.grfid && this->nfoline < other.nfoline);
    00120   }
    00121
    00122   bool operator==(const GRFLocation &other) const
    00123   {
    00124     return this->grfid == other.grfid && this->nfoline == other.nfoline;
    00125   }
    00126 };
    00127
    00128 static std::map<GRFLocation, SpriteID> _grm_sprites;
    00129 typedef std::map<GRFLocation, byte*> GRFLineToSpriteOverride;
    00130 GRFLineToSpriteOverride _grf_line_to_action6_sprite_override;
    00131
    00140 void CDECL grfmsg(int severity, const char *str, ...)
    00141 {
    00142   char buf[1024];
    00143   va_list va;
    00144
    00145   va_start(va, str);
    00146   vsnprintf(buf, sizeof(buf), str, va);
    00147   va_end(va);
    00148
    00149   DEBUG(grf, severity, "[%s:%d] %s", _cur_grfconfig->filename, _nfo_line, buf);
    00150 }
    00151
    00152 static inline bool check_length(size_t real, size_t wanted, const char *str)
    00153 {
    00154   if (real >= wanted) return true;
    00155   grfmsg(0, "%s: Invalid pseudo sprite length " PRINTF_SIZE " (expected " PRINTF_SIZE ")!", str, real, wanted);
    00156   return false;
    00157 }
    00158
    00159 static inline byte grf_load_byte(byte **buf)
    00160 {
    00161   return *(*buf)++;
    00162 }
    00163
    00164 static uint16 grf_load_word(byte **buf)
    00165 {
    00166   uint16 val = grf_load_byte(buf);
    00167   return val | (grf_load_byte(buf) << 8);
    00168 }
    00169
    00170 static uint16 grf_load_extended(byte** buf)
    00171 {
    00172   uint16 val;
    00173   val = grf_load_byte(buf);
    00174   if (val == 0xFF) val = grf_load_word(buf);
    00175   return val;
    00176 }
    00177
    00178 static uint32 grf_load_dword(byte **buf)
    00179 {
    00180   uint32 val = grf_load_word(buf);
    00181   return val | (grf_load_word(buf) << 16);
    00182 }
    00183
    00184 static uint32 grf_load_var(byte size, byte **buf)
    00185 {
    00186   switch (size) {
    00187     case 1: return grf_load_byte(buf);
    00188     case 2: return grf_load_word(buf);
    00189     case 4: return grf_load_dword(buf);
    00190     default:
    00191       NOT_REACHED();
    00192       return 0;
    00193   }
    00194 }
    00195
    00196 static const char *grf_load_string(byte **buf, size_t max_len)
    00197 {
    00198   const char *string   = *(const char **)buf;
    00199   size_t string_length = ttd_strnlen(string, max_len);
    00200
    00201   if (string_length == max_len) {
    00202     /* String was not NUL terminated, so make sure it is now. */
    00203     (*buf)[string_length - 1] = '\0';
    00204     grfmsg(7, "String was not terminated with a zero byte.");
    00205   } else {
    00206     /* Increase the string length to include the NUL byte. */
    00207     string_length++;
    00208   }
    00209   *buf += string_length;
    00210
    00211   return string;
    00212 }
    00213
    00214 static GRFFile *GetFileByGRFID(uint32 grfid)
    00215 {
    00216   GRFFile *file;
    00217
    00218   for (file = _first_grffile; file != NULL; file = file->next) {
    00219     if (file->grfid == grfid) break;
    00220   }
    00221   return file;
    00222 }
    00223
    00224 static GRFFile *GetFileByFilename(const char *filename)
    00225 {
    00226   GRFFile *file;
    00227
    00228   for (file = _first_grffile; file != NULL; file = file->next) {
    00229     if (strcmp(file->filename, filename) == 0) break;
    00230   }
    00231   return file;
    00232 }
    00233
    00235 static void ClearTemporaryNewGRFData(GRFFile *gf)
    00236 {
    00237   /* Clear the GOTO labels used for GRF processing */
    00238   for (GRFLabel *l = gf->label; l != NULL;) {
    00239     GRFLabel *l2 = l->next;
    00240     free(l);
    00241     l = l2;
    00242   }
    00243   gf->label = NULL;
    00244
    00245   /* Clear the list of spritegroups */
    00246   free(gf->spritegroups);
    00247   gf->spritegroups = NULL;
    00248   gf->spritegroups_count = 0;
    00249 }
    00250
    00251
    00252 typedef std::map<StringID *, uint32> StringIDToGRFIDMapping;
    00253 StringIDToGRFIDMapping _string_to_grf_mapping;
    00254
    00261 StringID MapGRFStringID(uint32 grfid, StringID str)
    00262 {
    00263   /* StringID table for TextIDs 0x4E->0x6D */
    00264   static const StringID units_volume[] = {
    00265     STR_NOTHING,    STR_PASSENGERS, STR_TONS,       STR_BAGS,
    00266     STR_LITERS,     STR_ITEMS,      STR_CRATES,     STR_TONS,
    00267     STR_TONS,       STR_TONS,       STR_TONS,       STR_BAGS,
    00268     STR_TONS,       STR_TONS,       STR_TONS,       STR_BAGS,
    00269     STR_TONS,       STR_TONS,       STR_BAGS,       STR_LITERS,
    00270     STR_TONS,       STR_LITERS,     STR_TONS,       STR_NOTHING,
    00271     STR_BAGS,       STR_LITERS,     STR_TONS,       STR_NOTHING,
    00272     STR_TONS,       STR_NOTHING,    STR_LITERS,     STR_NOTHING
    00273   };
    00274
    00275   /* 0xD0 and 0xDC stand for all the TextIDs in the range
    00276    * of 0xD000 (misc graphics texts) and 0xDC00 (misc persistent texts).
    00277    * These strings are unique to each grf file, and thus require to be used with the
    00278    * grfid in which they are declared */
    00279   switch (GB(str, 8, 8)) {
    00280     case 0xD0: case 0xD1: case 0xD2: case 0xD3:
    00281     case 0xDC:
    00282       return GetGRFStringID(grfid, str);
    00283
    00284     case 0xD4: case 0xD5: case 0xD6: case 0xD7:
    00285       /* Strings embedded via 0x81 have 0x400 added to them (no real
    00286        * explanation why...) */
    00287       return GetGRFStringID(grfid, str - 0x400);
    00288
    00289     default: break;
    00290   }
    00291
    00292 #define TEXID_TO_STRINGID(begin, end, stringid) if (str >= begin && str <= end) return str + (stringid - begin)
    00293   /* We have some changes in our cargo strings, resulting in some missing. */
    00294   TEXID_TO_STRINGID(0x000E, 0x002D, STR_000E);
    00295   TEXID_TO_STRINGID(0x002E, 0x004D, STR_002E);
    00296   if (str >= 0x004E && str <= 0x006D) str = units_volume[str - 0x004E];
    00297   TEXID_TO_STRINGID(0x006E, 0x008D, STR_QUANTITY_NOTHING);
    00298   TEXID_TO_STRINGID(0x008E, 0x00AD, STR_ABBREV_NOTHING);
    00299
    00300   /* Map building names according to our lang file changes. There are several
    00301    * ranges of house ids, all of which need to be remapped to allow newgrfs
    00302    * to use original house names. */
    00303   TEXID_TO_STRINGID(0x200F, 0x201F, STR_200F_TALL_OFFICE_BLOCK);
    00304   TEXID_TO_STRINGID(0x2036, 0x2041, STR_2036_COTTAGES);
    00305   TEXID_TO_STRINGID(0x2059, 0x205C, STR_2059_IGLOO);
    00306
    00307   /* Same thing for industries, since the introduction of 4 new strings above STR_482A_PRODUCTION_LAST_MONTH */
    00308   TEXID_TO_STRINGID(0x482A, 0x483B, STR_482A_PRODUCTION_LAST_MONTH);
    00309 #undef TEXTID_TO_STRINGID
    00310
    00311   if (str == STR_NULL) return STR_EMPTY;
    00312
    00313   return str;
    00314 }
    00315
    00316 static inline uint8 MapDOSColour(uint8 colour)
    00317 {
    00318   extern const byte _palmap_d2w[];
    00319   return (_use_palette == PAL_DOS ? colour : _palmap_d2w[colour]);
    00320 }
    00321
    00322 static std::map<uint32, uint32> _grf_id_overrides;
    00323
    00324 static void SetNewGRFOverride(uint32 source_grfid, uint32 target_grfid)
    00325 {
    00326   _grf_id_overrides[source_grfid] = target_grfid;
    00327   grfmsg(5, "SetNewGRFOverride: Added override of 0x%X to 0x%X", BSWAP32(source_grfid), BSWAP32(target_grfid));
    00328 }
    00329
    00338 static Engine *GetNewEngine(const GRFFile *file, VehicleType type, uint16 internal_id, bool static_access = false)
    00339 {
    00340   /* Hack for add-on GRFs that need to modify another GRF's engines. This lets
    00341    * them use the same engine slots. */
    00342   uint32 scope_grfid = INVALID_GRFID; // If not using dynamic_engines, all newgrfs share their ID range
    00343   if (_settings_game.vehicle.dynamic_engines) {
    00344     /* If dynamic_engies is enabled, there can be multiple independent ID ranges. */
    00345     scope_grfid = file->grfid;
    00346     uint32 override = _grf_id_overrides[file->grfid];
    00347     if (override != 0) {
    00348       scope_grfid = override;
    00349       const GRFFile *grf_match = GetFileByGRFID(override);
    00350       if (grf_match == NULL) {
    00351         grfmsg(5, "Tried mapping from GRFID %x to %x but target is not loaded", BSWAP32(file->grfid), BSWAP32(override));
    00352       } else {
    00353         grfmsg(5, "Mapping from GRFID %x to %x", BSWAP32(file->grfid), BSWAP32(override));
    00354       }
    00355     }
    00356
    00357     /* Check if the engine is registered in the override manager */
    00358     EngineID engine = _engine_mngr.GetID(type, internal_id, scope_grfid);
    00359     if (engine != INVALID_ENGINE) {
    00360       Engine *e = GetEngine(engine);
    00361       if (e->grffile == NULL) e->grffile = file;
    00362       return e;
    00363     }
    00364   }
    00365
    00366   /* Check if there is an unreserved slot */
    00367   EngineID engine = _engine_mngr.GetID(type, internal_id, INVALID_GRFID);
    00368   if (engine != INVALID_ENGINE) {
    00369     Engine *e = GetEngine(engine);
    00370
    00371     if (e->grffile == NULL) {
    00372       e->grffile = file;
    00373       grfmsg(5, "Replaced engine at index %d for GRFID %x, type %d, index %d", e->index, BSWAP32(file->grfid), type, internal_id);
    00374     }
    00375
    00376     /* Reserve the engine slot */
    00377     if (!static_access) {
    00378       EngineIDMapping *eid = _engine_mngr.Get(engine);
    00379       eid->grfid           = scope_grfid; // Note: this is INVALID_GRFID if dynamic_engines is disabled, so no reservation
    00380     }
    00381
    00382     return e;
    00383   }
    00384
    00385   if (static_access) return NULL;
    00386
    00387   uint engine_pool_size = GetEnginePoolSize();
    00388
    00389   /* ... it's not, so create a new one based off an existing engine */
    00390   Engine *e = new Engine(type, internal_id);
    00391   e->grffile = file;
    00392
    00393   /* Reserve the engine slot */
    00394   assert(_engine_mngr.Length() == e->index);
    00395   EngineIDMapping *eid = _engine_mngr.Append();
    00396   eid->type            = type;
    00397   eid->grfid           = scope_grfid; // Note: this is INVALID_GRFID if dynamic_engines is disabled, so no reservation
    00398   eid->internal_id     = internal_id;
    00399   eid->substitute_id   = min(internal_id, _engine_counts[type]); // substitute_id == _engine_counts[subtype] means "no substitute"
    00400
    00401   if (engine_pool_size != GetEnginePoolSize()) {
    00402     /* Resize temporary engine data ... */
    00403     _gted = ReallocT(_gted, GetEnginePoolSize());
    00404
    00405     /* and blank the new block. */
    00406     size_t len = (GetEnginePoolSize() - engine_pool_size) * sizeof(*_gted);
    00407     memset(_gted + engine_pool_size, 0, len);
    00408   }
    00409
    00410   grfmsg(5, "Created new engine at index %d for GRFID %x, type %d, index %d", e->index, BSWAP32(file->grfid), type, internal_id);
    00411
    00412   return e;
    00413 }
    00414
    00415 EngineID GetNewEngineID(const GRFFile *file, VehicleType type, uint16 internal_id)
    00416 {
    00417   uint32 scope_grfid = INVALID_GRFID; // If not using dynamic_engines, all newgrfs share their ID range
    00418   if (_settings_game.vehicle.dynamic_engines) {
    00419     scope_grfid = file->grfid;
    00420     uint32 override = _grf_id_overrides[file->grfid];
    00421     if (override != 0) scope_grfid = override;
    00422   }
    00423
    00424   return _engine_mngr.GetID(type, internal_id, scope_grfid);
    00425 }
    00426
    00430 static void MapSpriteMappingRecolour(PalSpriteID *grf_sprite)
    00431 {
    00432   if (HasBit(grf_sprite->pal, 14)) {
    00433     ClrBit(grf_sprite->pal, 14);
    00434     SetBit(grf_sprite->sprite, SPRITE_MODIFIER_OPAQUE);
    00435   }
    00436
    00437   if (HasBit(grf_sprite->sprite, 14)) {
    00438     ClrBit(grf_sprite->sprite, 14);
    00439     SetBit(grf_sprite->sprite, PALETTE_MODIFIER_TRANSPARENT);
    00440   }
    00441
    00442   if (HasBit(grf_sprite->sprite, 15)) {
    00443     ClrBit(grf_sprite->sprite, 15);
    00444     SetBit(grf_sprite->sprite, PALETTE_MODIFIER_COLOUR);
    00445   }
    00446 }
    00447
    00448 enum ChangeInfoResult {
    00449   CIR_SUCCESS,
    00450   CIR_UNHANDLED,
    00451   CIR_UNKNOWN,
    00452   CIR_INVALID_ID,
    00453 };
    00454
    00455 typedef ChangeInfoResult (*VCI_Handler)(uint engine, int numinfo, int prop, byte **buf, int len);
    00456
    00457 static ChangeInfoResult CommonVehicleChangeInfo(EngineInfo *ei, int prop, byte **buf)
    00458 {
    00459   switch (prop) {
    00460     case 0x00: // Introduction date
    00461       ei->base_intro = grf_load_word(buf) + DAYS_TILL_ORIGINAL_BASE_YEAR;
    00462       break;
    00463
    00464     case 0x02: // Decay speed
    00465       ei->decay_speed = grf_load_byte(buf);
    00466       break;
    00467
    00468     case 0x03: // Vehicle life
    00469       ei->lifelength = grf_load_byte(buf);
    00470       break;
    00471
    00472     case 0x04: // Model life
    00473       ei->base_life = grf_load_byte(buf);
    00474       break;
    00475
    00476     case 0x06: // Climates available
    00477       ei->climates = grf_load_byte(buf);
    00478       /* Sometimes a GRF wants hidden vehicles. Setting climates to
    00479        * zero may cause the ID to be reallocated. */
    00480       if (ei->climates == 0) ei->climates = 0x80;
    00481       break;
    00482
    00483     case 0x07: // Loading speed
    00484       /* Amount of cargo loaded during a vehicle's "loading tick" */
    00485       ei->load_amount = grf_load_byte(buf);
    00486       break;
    00487
    00488     default:
    00489       return CIR_UNKNOWN;
    00490   }
    00491
    00492   return CIR_SUCCESS;
    00493 }
    00494
    00495 static ChangeInfoResult RailVehicleChangeInfo(uint engine, int numinfo, int prop, byte **bufp, int len)
    00496 {
    00497   byte *buf = *bufp;
    00498   ChangeInfoResult ret = CIR_SUCCESS;
    00499
    00500   for (int i = 0; i < numinfo; i++) {
    00501     Engine *e = GetNewEngine(_cur_grffile, VEH_TRAIN, engine + i);
    00502     EngineInfo *ei = &e->info;
    00503     RailVehicleInfo *rvi = &e->u.rail;
    00504
    00505     switch (prop) {
    00506       case 0x05: { // Track type
    00507         uint8 tracktype = grf_load_byte(&buf);
    00508
    00509         if (tracktype < _cur_grffile->railtype_max) {
    00510           RailType railtype = GetRailTypeByLabel(_cur_grffile->railtype_list[tracktype]);
    00511           if (railtype == INVALID_RAILTYPE) {
    00512             /* Rail type is not available, so disable this engine */
    00513             ei[i].climates = 0x80;
    00514           } else {
    00515             rvi[i].railtype = railtype;
    00516           }
    00517           break;
    00518         }
    00519
    00520         switch (tracktype) {
    00521           case 0: rvi->railtype = rvi->engclass >= 2 ? RAILTYPE_ELECTRIC : RAILTYPE_RAIL; break;
    00522           case 1: rvi->railtype = RAILTYPE_MONO; break;
    00523           case 2: rvi->railtype = RAILTYPE_MAGLEV; break;
    00524           default:
    00525             grfmsg(1, "RailVehicleChangeInfo: Invalid track type %d specified, ignoring", tracktype);
    00526             break;
    00527         }
    00528       } break;
    00529
    00530       case 0x08: // AI passenger service
    00531         /* Tells the AI that this engine is designed for
    00532          * passenger services and shouldn't be used for freight. */
    00533         rvi->ai_passenger_only = grf_load_byte(&buf);
    00534         break;
    00535
    00536       case 0x09: { // Speed (1 unit is 1 kmh)
    00537         uint16 speed = grf_load_word(&buf);
    00538         if (speed == 0xFFFF) speed = 0;
    00539
    00540         rvi->max_speed = speed;
    00541       } break;
    00542
    00543       case 0x0B: // Power
    00544         rvi->power = grf_load_word(&buf);
    00545
    00546         /* Set engine / wagon state based on power */
    00547         if (rvi->power != 0) {
    00548           if (rvi->railveh_type == RAILVEH_WAGON) {
    00549             rvi->railveh_type = RAILVEH_SINGLEHEAD;
    00550           }
    00551         } else {
    00552           rvi->railveh_type = RAILVEH_WAGON;
    00553         }
    00554         break;
    00555
    00556       case 0x0D: // Running cost factor
    00557         rvi->running_cost = grf_load_byte(&buf);
    00558         break;
    00559
    00560       case 0x0E: { // Running cost base
    00561         uint32 base = grf_load_dword(&buf);
    00562
    00563         /* These magic numbers are used in GRFs to specify the base cost:
    00564          * http://wiki.ttdpatch.net/tiki-index.php?page=BaseCosts
    00565          */
    00566         if (base == 0) {
    00567           rvi->running_cost_class = 0xFF;
    00568         } else if (base < 0x4B34 || base > 0x4C54 || (base - 0x4B34) % 6 != 0) {
    00569           grfmsg(1, "RailVehicleChangeInfo: Unsupported running cost base 0x%04X, ignoring", base);
    00570         } else {
    00571           /* Convert the magic number to an index into the price data */
    00572           rvi->running_cost_class = (base - 0x4B34) / 6;
    00573         }
    00574       } break;
    00575
    00576       case 0x12: { // Sprite ID
    00577         uint8 spriteid = grf_load_byte(&buf);
    00578
    00579         /* TTD sprite IDs point to a location in a 16bit array, but we use it
    00580          * as an array index, so we need it to be half the original value. */
    00581         if (spriteid < 0xFD) spriteid >>= 1;
    00582
    00583         rvi->image_index = spriteid;
    00584       } break;
    00585
    00586       case 0x13: { // Dual-headed
    00587         uint8 dual = grf_load_byte(&buf);
    00588
    00589         if (dual != 0) {
    00590           rvi->railveh_type = RAILVEH_MULTIHEAD;
    00591         } else {
    00592           rvi->railveh_type = rvi->power == 0 ?
    00593             RAILVEH_WAGON : RAILVEH_SINGLEHEAD;
    00594         }
    00595       } break;
    00596
    00597       case 0x14: // Cargo capacity
    00598         rvi->capacity = grf_load_byte(&buf);
    00599         break;
    00600
    00601       case 0x15: { // Cargo type
    00602         uint8 ctype = grf_load_byte(&buf);
    00603
    00604         if (ctype < NUM_CARGO && HasBit(_cargo_mask, ctype)) {
    00605           rvi->cargo_type = ctype;
    00606         } else if (ctype == 0xFF) {
    00607           /* 0xFF is specified as 'use first refittable' */
    00608           rvi->cargo_type = CT_INVALID;
    00609         } else {
    00610           rvi->cargo_type = CT_INVALID;
    00611           grfmsg(2, "RailVehicleChangeInfo: Invalid cargo type %d, using first refittable", ctype);
    00612         }
    00613       } break;
    00614
    00615       case 0x16: // Weight
    00616         SB(rvi->weight, 0, 8, grf_load_byte(&buf));
    00617         break;
    00618
    00619       case 0x17: // Cost factor
    00620         rvi->cost_factor = grf_load_byte(&buf);
    00621         break;
    00622
    00623       case 0x18: // AI rank
    00624         rvi->ai_rank = grf_load_byte(&buf);
    00625         break;
    00626
    00627       case 0x19: { // Engine traction type
    00628         /* What do the individual numbers mean?
    00629          * 0x00 .. 0x07: Steam
    00630          * 0x08 .. 0x27: Diesel
    00631          * 0x28 .. 0x31: Electric
    00632          * 0x32 .. 0x37: Monorail
    00633          * 0x38 .. 0x41: Maglev
    00634          */
    00635         uint8 traction = grf_load_byte(&buf);
    00636         EngineClass engclass;
    00637
    00638         if (traction <= 0x07) {
    00639           engclass = EC_STEAM;
    00640         } else if (traction <= 0x27) {
    00641           engclass = EC_DIESEL;
    00642         } else if (traction <= 0x31) {
    00643           engclass = EC_ELECTRIC;
    00644         } else if (traction <= 0x37) {
    00645           engclass = EC_MONORAIL;
    00646         } else if (traction <= 0x41) {
    00647           engclass = EC_MAGLEV;
    00648         } else {
    00649           break;
    00650         }
    00651
    00652         if (_cur_grffile->railtype_max == 0) {
    00653           /* Use traction type to select between normal and electrified
    00654            * rail only when no translation list is in place. */
    00655           if (rvi->railtype == RAILTYPE_RAIL     && engclass >= EC_ELECTRIC) rvi->railtype = RAILTYPE_ELECTRIC;
    00656           if (rvi->railtype == RAILTYPE_ELECTRIC && engclass  < EC_ELECTRIC) rvi->railtype = RAILTYPE_RAIL;
    00657         }
    00658
    00659         rvi->engclass = engclass;
    00660       } break;
    00661
    00662       case 0x1A: // Alter purchase list sort order
    00663         AlterVehicleListOrder(e->index, grf_load_extended(&buf));
    00664         break;
    00665
    00666       case 0x1B: // Powered wagons power bonus
    00667         rvi->pow_wag_power = grf_load_word(&buf);
    00668         break;
    00669
    00670       case 0x1C: // Refit cost
    00671         ei->refit_cost = grf_load_byte(&buf);
    00672         break;
    00673
    00674       case 0x1D: // Refit cargo
    00675         ei->refit_mask = grf_load_dword(&buf);
    00676         _gted[e->index].refitmask_valid = true;
    00677         break;
    00678
    00679       case 0x1E: // Callback
    00680         ei->callbackmask = grf_load_byte(&buf);
    00681         break;
    00682
    00683       case 0x1F: // Tractive effort coefficient
    00684         rvi->tractive_effort = grf_load_byte(&buf);
    00685         break;
    00686
    00687       case 0x20: // Air drag
    00689         grf_load_byte(&buf);
    00690         ret = CIR_UNHANDLED;
    00691         break;
    00692
    00693       case 0x21: // Shorter vehicle
    00694         rvi->shorten_factor = grf_load_byte(&buf);
    00695         break;
    00696
    00697       case 0x22: // Visual effect
    00699         rvi->visual_effect = grf_load_byte(&buf);
    00700         break;
    00701
    00702       case 0x23: // Powered wagons weight bonus
    00703         rvi->pow_wag_weight = grf_load_byte(&buf);
    00704         break;
    00705
    00706       case 0x24: { // High byte of vehicle weight
    00707         byte weight = grf_load_byte(&buf);
    00708
    00709         if (weight > 4) {
    00710           grfmsg(2, "RailVehicleChangeInfo: Nonsensical weight of %d tons, ignoring", weight << 8);
    00711         } else {
    00712           SB(rvi->weight, 8, 8, weight);
    00713         }
    00714       } break;
    00715
    00716       case 0x25: // User-defined bit mask to set when checking veh. var. 42
    00717         rvi->user_def_data = grf_load_byte(&buf);
    00718         break;
    00719
    00720       case 0x26: // Retire vehicle early
    00721         ei->retire_early = grf_load_byte(&buf);
    00722         break;
    00723
    00724       case 0x27: // Miscellaneous flags
    00725         ei->misc_flags = grf_load_byte(&buf);
    00726         _loaded_newgrf_features.has_2CC |= HasBit(ei->misc_flags, EF_USES_2CC);
    00727         break;
    00728
    00729       case 0x28: // Cargo classes allowed
    00730         _gted[e->index].cargo_allowed = grf_load_word(&buf);
    00731         _gted[e->index].refitmask_valid = true;
    00732         break;
    00733
    00734       case 0x29: // Cargo classes disallowed
    00735         _gted[e->index].cargo_disallowed = grf_load_word(&buf);
    00736         _gted[e->index].refitmask_valid = true;
    00737         break;
    00738
    00739       case 0x2A: // Long format introduction date (days since year 0)
    00740         ei->base_intro = grf_load_dword(&buf);
    00741         break;
    00742
    00743       default:
    00744         ret = CommonVehicleChangeInfo(ei, prop, &buf);
    00745         break;
    00746     }
    00747   }
    00748
    00749   *bufp = buf;
    00750   return ret;
    00751 }
    00752
    00753 static ChangeInfoResult RoadVehicleChangeInfo(uint engine, int numinfo, int prop, byte **bufp, int len)
    00754 {
    00755   byte *buf = *bufp;
    00756   ChangeInfoResult ret = CIR_SUCCESS;
    00757
    00758   for (int i = 0; i < numinfo; i++) {
    00759     Engine *e = GetNewEngine(_cur_grffile, VEH_ROAD, engine + i);
    00760     EngineInfo *ei = &e->info;
    00761     RoadVehicleInfo *rvi = &e->u.road;
    00762
    00763     switch (prop) {
    00764       case 0x08: // Speed (1 unit is 0.5 kmh)
    00765         rvi->max_speed = grf_load_byte(&buf);
    00766         break;
    00767
    00768       case 0x09: // Running cost factor
    00769         rvi->running_cost = grf_load_byte(&buf);
    00770         break;
    00771
    00772       case 0x0A: { // Running cost base
    00773         uint32 base = grf_load_dword(&buf);
    00774
    00775         /* These magic numbers are used in GRFs to specify the base cost:
    00776          * http://wiki.ttdpatch.net/tiki-index.php?page=BaseCosts
    00777          */
    00778         if (base == 0) {
    00779           rvi->running_cost_class = 0xFF;
    00780         } else if (base < 0x4B34 || base > 0x4C54 || (base - 0x4B34) % 6 != 0) {
    00781           grfmsg(1, "RailVehicleChangeInfo: Unsupported running cost base 0x%04X, ignoring", base);
    00782         } else {
    00783           /* Convert the magic number to an index into the price data */
    00784           rvi->running_cost_class = (base - 0x4B34) / 6;
    00785         }
    00786
    00787         break;
    00788       }
    00789
    00790       case 0x0E: { // Sprite ID
    00791         uint8 spriteid = grf_load_byte(&buf);
    00792
    00793         /* cars have different custom id in the GRF file */
    00794         if (spriteid == 0xFF) spriteid = 0xFD;
    00795
    00796         if (spriteid < 0xFD) spriteid >>= 1;
    00797
    00798         rvi->image_index = spriteid;
    00799       } break;
    00800
    00801       case 0x0F: // Cargo capacity
    00802         rvi->capacity = grf_load_byte(&buf);
    00803         break;
    00804
    00805       case 0x10: { // Cargo type
    00806         uint8 cargo = grf_load_byte(&buf);
    00807
    00808         if (cargo < NUM_CARGO && HasBit(_cargo_mask, cargo)) {
    00809           rvi->cargo_type = cargo;
    00810         } else if (cargo == 0xFF) {
    00811           rvi->cargo_type = CT_INVALID;
    00812         } else {
    00813           rvi->cargo_type = CT_INVALID;
    00814           grfmsg(2, "RoadVehicleChangeInfo: Invalid cargo type %d, using first refittable", cargo);
    00815         }
    00816       } break;
    00817
    00818       case 0x11: // Cost factor
    00819         rvi->cost_factor = grf_load_byte(&buf);
    00820         break;
    00821
    00822       case 0x12: // SFX
    00823         rvi->sfx = (SoundFx)grf_load_byte(&buf);
    00824         break;
    00825
    00826       case 0x13: // Power in 10hp
    00827         rvi->power = grf_load_byte(&buf);
    00828         break;
    00829
    00830       case 0x14: // Weight in 1/4 tons
    00831         rvi->weight = grf_load_byte(&buf);
    00832         break;
    00833
    00834       case 0x15: // Speed in mph/0.8
    00835         _gted[e->index].rv_max_speed = grf_load_byte(&buf);
    00836         break;
    00837
    00838       case 0x16: // Cargos available for refitting
    00839         ei->refit_mask = grf_load_dword(&buf);
    00840         _gted[e->index].refitmask_valid = true;
    00841         break;
    00842
    00843       case 0x17: // Callback mask
    00844         ei->callbackmask = grf_load_byte(&buf);
    00845         break;
    00846
    00847       case 0x18: // Tractive effort
    00848         rvi->tractive_effort = grf_load_byte(&buf);
    00849         break;
    00850
    00851       case 0x19: // Air drag
    00852         rvi->air_drag = grf_load_byte(&buf);
    00853         break;
    00854
    00855       case 0x1A: // Refit cost
    00856         ei->refit_cost = grf_load_byte(&buf);
    00857         break;
    00858
    00859       case 0x1B: // Retire vehicle early
    00860         ei->retire_early = grf_load_byte(&buf);
    00861         break;
    00862
    00863       case 0x1C: // Miscellaneous flags
    00864         ei->misc_flags = grf_load_byte(&buf);
    00865         _loaded_newgrf_features.has_2CC |= HasBit(ei->misc_flags, EF_USES_2CC);
    00866         break;
    00867
    00868       case 0x1D: // Cargo classes allowed
    00869         _gted[e->index].cargo_allowed = grf_load_word(&buf);
    00870         _gted[e->index].refitmask_valid = true;
    00871         break;
    00872
    00873       case 0x1E: // Cargo classes disallowed
    00874         _gted[e->index].cargo_disallowed = grf_load_word(&buf);
    00875         _gted[e->index].refitmask_valid = true;
    00876         break;
    00877
    00878       case 0x1F: // Long format introduction date (days since year 0)
    00879         ei->base_intro = grf_load_dword(&buf);
    00880         break;
    00881
    00882       case 0x20: // Alter purchase list sort order
    00883         AlterVehicleListOrder(e->index, grf_load_extended(&buf));
    00884         break;
    00885
    00886       default:
    00887         ret = CommonVehicleChangeInfo(ei, prop, &buf);
    00888         break;
    00889     }
    00890   }
    00891
    00892   *bufp = buf;
    00893   return ret;
    00894 }
    00895
    00896 static ChangeInfoResult ShipVehicleChangeInfo(uint engine, int numinfo, int prop, byte **bufp, int len)
    00897 {
    00898   byte *buf = *bufp;
    00899   ChangeInfoResult ret = CIR_SUCCESS;
    00900
    00901   for (int i = 0; i < numinfo; i++) {
    00902     Engine *e = GetNewEngine(_cur_grffile, VEH_SHIP, engine + i);
    00903     EngineInfo *ei = &e->info;
    00904     ShipVehicleInfo *svi = &e->u.ship;
    00905
    00906     switch (prop) {
    00907       case 0x08: { // Sprite ID
    00908         uint8 spriteid = grf_load_byte(&buf);
    00909
    00910         /* ships have different custom id in the GRF file */
    00911         if (spriteid == 0xFF) spriteid = 0xFD;
    00912
    00913         if (spriteid < 0xFD) spriteid >>= 1;
    00914
    00915         svi->image_index = spriteid;
    00916       } break;
    00917
    00918       case 0x09: // Refittable
    00919         svi->refittable = (grf_load_byte(&buf) != 0);
    00920         break;
    00921
    00922       case 0x0A: // Cost factor
    00923         svi->cost_factor = grf_load_byte(&buf);
    00924         break;
    00925
    00926       case 0x0B: // Speed (1 unit is 0.5 kmh)
    00927         svi->max_speed = grf_load_byte(&buf);
    00928         break;
    00929
    00930       case 0x0C: { // Cargo type
    00931         uint8 cargo = grf_load_byte(&buf);
    00932
    00933         if (cargo < NUM_CARGO && HasBit(_cargo_mask, cargo)) {
    00934           svi->cargo_type = cargo;
    00935         } else if (cargo == 0xFF) {
    00936           svi->cargo_type = CT_INVALID;
    00937         } else {
    00938           svi->cargo_type = CT_INVALID;
    00939           grfmsg(2, "ShipVehicleChangeInfo: Invalid cargo type %d, using first refittable", cargo);
    00940         }
    00941       } break;
    00942
    00943       case 0x0D: // Cargo capacity
    00944         svi->capacity = grf_load_word(&buf);
    00945         break;
    00946
    00947       case 0x0F: // Running cost factor
    00948         svi->running_cost = grf_load_byte(&buf);
    00949         break;
    00950
    00951       case 0x10: // SFX
    00952         svi->sfx = (SoundFx)grf_load_byte(&buf);
    00953         break;
    00954
    00955       case 0x11: // Cargos available for refitting
    00956         ei->refit_mask = grf_load_dword(&buf);
    00957         _gted[e->index].refitmask_valid = true;
    00958         break;
    00959
    00960       case 0x12: // Callback mask
    00961         ei->callbackmask = grf_load_byte(&buf);
    00962         break;
    00963
    00964       case 0x13: // Refit cost
    00965         ei->refit_cost = grf_load_byte(&buf);
    00966         break;
    00967
    00968       case 0x14: // Ocean speed fraction
    00969       case 0x15: // Canal speed fraction
    00971         grf_load_byte(&buf);
    00972         ret = CIR_UNHANDLED;
    00973         break;
    00974
    00975       case 0x16: // Retire vehicle early
    00976         ei->retire_early = grf_load_byte(&buf);
    00977         break;
    00978
    00979       case 0x17: // Miscellaneous flags
    00980         ei->misc_flags = grf_load_byte(&buf);
    00981         _loaded_newgrf_features.has_2CC |= HasBit(ei->misc_flags, EF_USES_2CC);
    00982         break;
    00983
    00984       case 0x18: // Cargo classes allowed
    00985         _gted[e->index].cargo_allowed = grf_load_word(&buf);
    00986         _gted[e->index].refitmask_valid = true;
    00987         break;
    00988
    00989       case 0x19: // Cargo classes disallowed
    00990         _gted[e->index].cargo_disallowed = grf_load_word(&buf);
    00991         _gted[e->index].refitmask_valid = true;
    00992         break;
    00993
    00994       case 0x1A: // Long format introduction date (days since year 0)
    00995         ei->base_intro = grf_load_dword(&buf);
    00996         break;
    00997
    00998       case 0x1B: // Alter purchase list sort order
    00999         AlterVehicleListOrder(e->index, grf_load_extended(&buf));
    01000         break;
    01001
    01002       default:
    01003         ret = CommonVehicleChangeInfo(ei, prop, &buf);
    01004         break;
    01005     }
    01006   }
    01007
    01008   *bufp = buf;
    01009   return ret;
    01010 }
    01011
    01012 static ChangeInfoResult AircraftVehicleChangeInfo(uint engine, int numinfo, int prop, byte **bufp, int len)
    01013 {
    01014   byte *buf = *bufp;
    01015   ChangeInfoResult ret = CIR_SUCCESS;
    01016
    01017   for (int i = 0; i < numinfo; i++) {
    01018     Engine *e = GetNewEngine(_cur_grffile, VEH_AIRCRAFT, engine + i);
    01019     EngineInfo *ei = &e->info;
    01020     AircraftVehicleInfo *avi = &e->u.air;
    01021
    01022     switch (prop) {
    01023       case 0x08: { // Sprite ID
    01024         uint8 spriteid = grf_load_byte(&buf);
    01025
    01026         /* aircraft have different custom id in the GRF file */
    01027         if (spriteid == 0xFF) spriteid = 0xFD;
    01028
    01029         if (spriteid < 0xFD) spriteid >>= 1;
    01030
    01031         avi->image_index = spriteid;
    01032       } break;
    01033
    01034       case 0x09: // Helicopter
    01035         if (grf_load_byte(&buf) == 0) {
    01036           avi->subtype = AIR_HELI;
    01037         } else {
    01038           SB(avi->subtype, 0, 1, 1); // AIR_CTOL
    01039         }
    01040         break;
    01041
    01042       case 0x0A: // Large
    01043         SB(avi->subtype, 1, 1, (grf_load_byte(&buf) != 0 ? 1 : 0)); // AIR_FAST
    01044         break;
    01045
    01046       case 0x0B: // Cost factor
    01047         avi->cost_factor = grf_load_byte(&buf);
    01048         break;
    01049
    01050       case 0x0C: // Speed (1 unit is 8 mph, we translate to 1 unit is 1 km/h)
    01051         avi->max_speed = (grf_load_byte(&buf) * 129) / 10;
    01052         break;
    01053
    01054       case 0x0D: // Acceleration
    01055         avi->acceleration = (grf_load_byte(&buf) * 129) / 10;
    01056         break;
    01057
    01058       case 0x0E: // Running cost factor
    01059         avi->running_cost = grf_load_byte(&buf);
    01060         break;
    01061
    01062       case 0x0F: // Passenger capacity
    01063         avi->passenger_capacity = grf_load_word(&buf);
    01064         break;
    01065
    01066       case 0x11: // Mail capacity
    01067         avi->mail_capacity = grf_load_byte(&buf);
    01068         break;
    01069
    01070       case 0x12: // SFX
    01071         avi->sfx = (SoundFx)grf_load_byte(&buf);
    01072         break;
    01073
    01074       case 0x13: // Cargos available for refitting
    01075         ei->refit_mask = grf_load_dword(&buf);
    01076         _gted[e->index].refitmask_valid = true;
    01077         break;
    01078
    01079       case 0x14: // Callback mask
    01080         ei->callbackmask = grf_load_byte(&buf);
    01081         break;
    01082
    01083       case 0x15: // Refit cost
    01084         ei->refit_cost = grf_load_byte(&buf);
    01085         break;
    01086
    01087       case 0x16: // Retire vehicle early
    01088         ei->retire_early = grf_load_byte(&buf);
    01089         break;
    01090
    01091       case 0x17: // Miscellaneous flags
    01092         ei->misc_flags = grf_load_byte(&buf);
    01093         _loaded_newgrf_features.has_2CC |= HasBit(ei->misc_flags, EF_USES_2CC);
    01094         break;
    01095
    01096       case 0x18: // Cargo classes allowed
    01097         _gted[e->index].cargo_allowed = grf_load_word(&buf);
    01098         _gted[e->index].refitmask_valid = true;
    01099         break;
    01100
    01101       case 0x19: // Cargo classes disallowed
    01102         _gted[e->index].cargo_disallowed = grf_load_word(&buf);
    01103         _gted[e->index].refitmask_valid = true;
    01104         break;
    01105
    01106       case 0x1A: // Long format introduction date (days since year 0)
    01107         ei->base_intro = grf_load_dword(&buf);
    01108         break;
    01109
    01110       case 0x1B: // Alter purchase list sort order
    01111         AlterVehicleListOrder(e->index, grf_load_extended(&buf));
    01112         break;
    01113
    01114       default:
    01115         ret = CommonVehicleChangeInfo(ei, prop, &buf);
    01116         break;
    01117     }
    01118   }
    01119
    01120   *bufp = buf;
    01121   return ret;
    01122 }
    01123
    01124 static ChangeInfoResult StationChangeInfo(uint stid, int numinfo, int prop, byte **bufp, int len)
    01125 {
    01126   byte *buf = *bufp;
    01127   ChangeInfoResult ret = CIR_SUCCESS;
    01128
    01129   if (stid + numinfo > MAX_STATIONS) {
    01130     grfmsg(1, "StationChangeInfo: Station %u is invalid, max %u, ignoring", stid + numinfo, MAX_STATIONS);
    01131     return CIR_INVALID_ID;
    01132   }
    01133
    01134   /* Allocate station specs if necessary */
    01135   if (_cur_grffile->stations == NULL) _cur_grffile->stations = CallocT<StationSpec*>(MAX_STATIONS);
    01136
    01137   for (int i = 0; i < numinfo; i++) {
    01138     StationSpec *statspec = _cur_grffile->stations[stid + i];
    01139
    01140     /* Check that the station we are modifying is defined. */
    01141     if (statspec == NULL && prop != 0x08) {
    01142       grfmsg(2, "StationChangeInfo: Attempt to modify undefined station %u, ignoring", stid + i);
    01143       return CIR_INVALID_ID;
    01144     }
    01145
    01146     switch (prop) {
    01147       case 0x08: { // Class ID
    01148         StationSpec **spec = &_cur_grffile->stations[stid + i];
    01149
    01150         /* Property 0x08 is special; it is where the station is allocated */
    01151         if (*spec == NULL) *spec = CallocT<StationSpec>(1);
    01152
    01153         /* Swap classid because we read it in BE meaning WAYP or DFLT */
    01154         uint32 classid = grf_load_dword(&buf);
    01155         (*spec)->sclass = AllocateStationClass(BSWAP32(classid));
    01156       } break;
    01157
    01158       case 0x09: // Define sprite layout
    01159         statspec->tiles = grf_load_extended(&buf);
    01160         statspec->renderdata = CallocT<DrawTileSprites>(statspec->tiles);
    01161         statspec->copied_renderdata = false;
    01162
    01163         for (uint t = 0; t < statspec->tiles; t++) {
    01164           DrawTileSprites *dts = &statspec->renderdata[t];
    01165           uint seq_count = 0;
    01166
    01167           dts->seq = NULL;
    01168           dts->ground.sprite = grf_load_word(&buf);
    01169           dts->ground.pal = grf_load_word(&buf);
    01170           if (dts->ground.sprite == 0) continue;
    01171           if (HasBit(dts->ground.pal, 15)) {
    01172             ClrBit(dts->ground.pal, 15);
    01173             SetBit(dts->ground.sprite, SPRITE_MODIFIER_USE_OFFSET);
    01174           }
    01175
    01176           MapSpriteMappingRecolour(&dts->ground);
    01177
    01178           while (buf < *bufp + len) {
    01179             DrawTileSeqStruct *dtss;
    01180
    01181             /* no relative bounding box support */
    01182             dts->seq = ReallocT((DrawTileSeqStruct*)dts->seq, ++seq_count);
    01183             dtss = (DrawTileSeqStruct*) &dts->seq[seq_count - 1];
    01184
    01185             dtss->delta_x = grf_load_byte(&buf);
    01186             if ((byte) dtss->delta_x == 0x80) break;
    01187             dtss->delta_y = grf_load_byte(&buf);
    01188             dtss->delta_z = grf_load_byte(&buf);
    01189             dtss->size_x = grf_load_byte(&buf);
    01190             dtss->size_y = grf_load_byte(&buf);
    01191             dtss->size_z = grf_load_byte(&buf);
    01192             dtss->image.sprite = grf_load_word(&buf);
    01193             dtss->image.pal = grf_load_word(&buf);
    01194
    01195             /* Remap flags as ours collide */
    01196             if (HasBit(dtss->image.pal, 15)) {
    01197               ClrBit(dtss->image.pal, 15);
    01198               SetBit(dtss->image.sprite, SPRITE_MODIFIER_USE_OFFSET);
    01199             }
    01200
    01201             MapSpriteMappingRecolour(&dtss->image);
    01202           }
    01203         }
    01204         break;
    01205
    01206       case 0x0A: { // Copy sprite layout
    01207         byte srcid = grf_load_byte(&buf);
    01208         const StationSpec *srcstatspec = _cur_grffile->stations[srcid];
    01209
    01210         statspec->tiles = srcstatspec->tiles;
    01211         statspec->renderdata = srcstatspec->renderdata;
    01212         statspec->copied_renderdata = true;
    01213       } break;
    01214
    01215       case 0x0B: // Callback mask
    01216         statspec->callbackmask = grf_load_byte(&buf);
    01217         break;
    01218
    01219       case 0x0C: // Disallowed number of platforms
    01220         statspec->disallowed_platforms = grf_load_byte(&buf);
    01221         break;
    01222
    01223       case 0x0D: // Disallowed platform lengths
    01224         statspec->disallowed_lengths = grf_load_byte(&buf);
    01225         break;
    01226
    01227       case 0x0E: // Define custom layout
    01228         statspec->copied_layouts = false;
    01229
    01230         while (buf < *bufp + len) {
    01231           byte length = grf_load_byte(&buf);
    01232           byte number = grf_load_byte(&buf);
    01233           StationLayout layout;
    01234           uint l, p;
    01235
    01236           if (length == 0 || number == 0) break;
    01237
    01238           if (length > statspec->lengths) {
    01239             statspec->platforms = ReallocT(statspec->platforms, length);
    01240             memset(statspec->platforms + statspec->lengths, 0, length - statspec->lengths);
    01241
    01242             statspec->layouts = ReallocT(statspec->layouts, length);
    01243             memset(statspec->layouts + statspec->lengths, 0,
    01244                    (length - statspec->lengths) * sizeof(*statspec->layouts));
    01245
    01246             statspec->lengths = length;
    01247           }
    01248           l = length - 1; // index is zero-based
    01249
    01250           if (number > statspec->platforms[l]) {
    01251             statspec->layouts[l] = ReallocT(statspec->layouts[l], number);
    01252             /* We expect NULL being 0 here, but C99 guarantees that. */
    01253             memset(statspec->layouts[l] + statspec->platforms[l], 0,
    01254                    (number - statspec->platforms[l]) * sizeof(**statspec->layouts));
    01255
    01256             statspec->platforms[l] = number;
    01257           }
    01258
    01259           p = 0;
    01260           layout = MallocT<byte>(length * number);
    01261           for (l = 0; l < length; l++) {
    01262             for (p = 0; p < number; p++) {
    01263               layout[l * number + p] = grf_load_byte(&buf);
    01264             }
    01265           }
    01266
    01267           l--;
    01268           p--;
    01269           free(statspec->layouts[l][p]);
    01270           statspec->layouts[l][p] = layout;
    01271         }
    01272         break;
    01273
    01274       case 0x0F: { // Copy custom layout
    01275         byte srcid = grf_load_byte(&buf);
    01276         const StationSpec *srcstatspec = _cur_grffile->stations[srcid];
    01277
    01278         statspec->lengths   = srcstatspec->lengths;
    01279         statspec->platforms = srcstatspec->platforms;
    01280         statspec->layouts   = srcstatspec->layouts;
    01281         statspec->copied_layouts = true;
    01282       } break;
    01283
    01284       case 0x10: // Little/lots cargo threshold
    01285         statspec->cargo_threshold = grf_load_word(&buf);
    01286         break;
    01287
    01288       case 0x11: // Pylon placement
    01289         statspec->pylons = grf_load_byte(&buf);
    01290         break;
    01291
    01292       case 0x12: // Cargo types for random triggers
    01293         statspec->cargo_triggers = grf_load_dword(&buf);
    01294         break;
    01295
    01296       case 0x13: // General flags
    01297         statspec->flags = grf_load_byte(&buf);
    01298         break;
    01299
    01300       case 0x14: // Overhead wire placement
    01301         statspec->wires = grf_load_byte(&buf);
    01302         break;
    01303
    01304       case 0x15: // Blocked tiles
    01305         statspec->blocked = grf_load_byte(&buf);
    01306         break;
    01307
    01308       case 0x16: // Animation info
    01309         statspec->anim_frames = grf_load_byte(&buf);
    01310         statspec->anim_status = grf_load_byte(&buf);
    01311         break;
    01312
    01313       case 0x17: // Animation speed
    01314         statspec->anim_speed = grf_load_byte(&buf);
    01315         break;
    01316
    01317       case 0x18: // Animation triggers
    01318         statspec->anim_triggers = grf_load_word(&buf);
    01319         break;
    01320
    01321       default:
    01322         ret = CIR_UNKNOWN;
    01323         break;
    01324     }
    01325   }
    01326
    01327   *bufp = buf;
    01328   return ret;
    01329 }
    01330
    01331 static ChangeInfoResult CanalChangeInfo(uint id, int numinfo, int prop, byte **bufp, int len)
    01332 {
    01333   byte *buf = *bufp;
    01334   ChangeInfoResult ret = CIR_SUCCESS;
    01335
    01336   if (id + numinfo > CF_END) {
    01337     grfmsg(1, "CanalChangeInfo: Canal feature %u is invalid, max %u, ignoreing", id + numinfo, CF_END);
    01338     return CIR_INVALID_ID;
    01339   }
    01340
    01341   for (int i = 0; i < numinfo; i++) {
    01342     WaterFeature *wf = &_water_feature[id + i];
    01343
    01344     switch (prop) {
    01345       case 0x08:
    01346         wf->callbackmask = grf_load_byte(&buf);
    01347         break;
    01348
    01349       case 0x09:
    01350         wf->flags = grf_load_byte(&buf);
    01351         break;
    01352
    01353       default:
    01354         ret = CIR_UNKNOWN;
    01355         break;
    01356     }
    01357   }
    01358
    01359   *bufp = buf;
    01360   return ret;
    01361 }
    01362
    01363 static ChangeInfoResult BridgeChangeInfo(uint brid, int numinfo, int prop, byte **bufp, int len)
    01364 {
    01365   byte *buf = *bufp;
    01366   ChangeInfoResult ret = CIR_SUCCESS;
    01367
    01368   if (brid + numinfo > MAX_BRIDGES) {
    01369     grfmsg(1, "BridgeChangeInfo: Bridge %u is invalid, max %u, ignoring", brid + numinfo, MAX_BRIDGES);
    01370     return CIR_INVALID_ID;
    01371   }
    01372
    01373   for (int i = 0; i < numinfo; i++) {
    01374     BridgeSpec *bridge = &_bridge[brid + i];
    01375
    01376     switch (prop) {
    01377       case 0x08: { // Year of availability
    01378         /* We treat '0' as always available */
    01379         byte year = grf_load_byte(&buf);
    01380         bridge->avail_year = (year > 0 ? ORIGINAL_BASE_YEAR + year : 0);
    01381         break;
    01382       }
    01383
    01384       case 0x09: // Minimum length
    01385         bridge->min_length = grf_load_byte(&buf);
    01386         break;
    01387
    01388       case 0x0A: // Maximum length
    01389         bridge->max_length = grf_load_byte(&buf);
    01390         break;
    01391
    01392       case 0x0B: // Cost factor
    01393         bridge->price = grf_load_byte(&buf);
    01394         break;
    01395
    01396       case 0x0C: // Maximum speed
    01397         bridge->speed = grf_load_word(&buf);
    01398         break;
    01399
    01400       case 0x0D: { // Bridge sprite tables
    01401         byte tableid = grf_load_byte(&buf);
    01402         byte numtables = grf_load_byte(&buf);
    01403
    01404         if (bridge->sprite_table == NULL) {
    01405           /* Allocate memory for sprite table pointers and zero out */
    01406           bridge->sprite_table = CallocT<PalSpriteID*>(7);
    01407         }
    01408
    01409         for (; numtables-- != 0; tableid++) {
    01410           if (tableid >= 7) { // skip invalid data
    01411             grfmsg(1, "BridgeChangeInfo: Table %d >= 7, skipping", tableid);
    01412             for (byte sprite = 0; sprite < 32; sprite++) grf_load_dword(&buf);
    01413             continue;
    01414           }
    01415
    01416           if (bridge->sprite_table[tableid] == NULL) {
    01417             bridge->sprite_table[tableid] = MallocT<PalSpriteID>(32);
    01418           }
    01419
    01420           for (byte sprite = 0; sprite < 32; sprite++) {
    01421             SpriteID image = grf_load_word(&buf);
    01422             SpriteID pal   = grf_load_word(&buf);
    01423
    01424             bridge->sprite_table[tableid][sprite].sprite = image;
    01425             bridge->sprite_table[tableid][sprite].pal    = pal;
    01426
    01427             MapSpriteMappingRecolour(&bridge->sprite_table[tableid][sprite]);
    01428           }
    01429         }
    01430       } break;
    01431
    01432       case 0x0E: // Flags; bit 0 - disable far pillars
    01433         bridge->flags = grf_load_byte(&buf);
    01434         break;
    01435
    01436       case 0x0F: // Long format year of availability (year since year 0)
    01437         bridge->avail_year = Clamp(grf_load_dword(&buf), MIN_YEAR, MAX_YEAR);
    01438         break;
    01439
    01440       case 0x10: { // purchase string
    01441         StringID newone = GetGRFStringID(_cur_grffile->grfid, grf_load_word(&buf));
    01442         if (newone != STR_UNDEFINED) bridge->material = newone;
    01443         } break;
    01444
    01445       case 0x11: // description of bridge with rails or roads
    01446       case 0x12: {
    01447         StringID newone = GetGRFStringID(_cur_grffile->grfid, grf_load_word(&buf));
    01448         if (newone != STR_UNDEFINED) bridge->transport_name[prop - 0x11] = newone;
    01449         } break;
    01450
    01451       case 0x13: // 16 bits cost multiplier
    01452         bridge->price = grf_load_word(&buf);
    01453         break;
    01454
    01455       default:
    01456         ret = CIR_UNKNOWN;
    01457         break;
    01458     }
    01459   }
    01460
    01461   *bufp = buf;
    01462   return ret;
    01463 }
    01464
    01465 static ChangeInfoResult TownHouseChangeInfo(uint hid, int numinfo, int prop, byte **bufp, int len)
    01466 {
    01467   byte *buf = *bufp;
    01468   ChangeInfoResult ret = CIR_SUCCESS;
    01469
    01470   if (hid + numinfo > HOUSE_MAX) {
    01471     grfmsg(1, "TownHouseChangeInfo: Too many houses loaded (%u), max (%u). Ignoring.", hid + numinfo, HOUSE_MAX);
    01472     return CIR_INVALID_ID;
    01473   }
    01474
    01475   /* Allocate house specs if they haven't been allocated already. */
    01476   if (_cur_grffile->housespec == NULL) {
    01477     _cur_grffile->housespec = CallocT<HouseSpec*>(HOUSE_MAX);
    01478   }
    01479
    01480   for (int i = 0; i < numinfo; i++) {
    01481     HouseSpec *housespec = _cur_grffile->housespec[hid + i];
    01482
    01483     if (prop != 0x08 && housespec == NULL) {
    01484       grfmsg(2, "TownHouseChangeInfo: Attempt to modify undefined house %u. Ignoring.", hid + i);
    01485       return CIR_INVALID_ID;
    01486     }
    01487
    01488     switch (prop) {
    01489       case 0x08: { // Substitute building type, and definition of a new house
    01490         HouseSpec **house = &_cur_grffile->housespec[hid + i];
    01491         byte subs_id = grf_load_byte(&buf);
    01492
    01493         if (subs_id == 0xFF) {
    01494           /* Instead of defining a new house, a substitute house id
    01495            * of 0xFF disables the old house with the current id. */
    01496           _house_specs[hid + i].enabled = false;
    01497           continue;
    01498         } else if (subs_id >= NEW_HOUSE_OFFSET) {
    01499           /* The substitute id must be one of the original houses. */
    01500           grfmsg(2, "TownHouseChangeInfo: Attempt to use new house %u as substitute house for %u. Ignoring.", subs_id, hid + i);
    01501           continue;
    01502         }
    01503
    01504         /* Allocate space for this house. */
    01505         if (*house == NULL) *house = CallocT<HouseSpec>(1);
    01506
    01507         housespec = *house;
    01508
    01509         memcpy(housespec, &_house_specs[subs_id], sizeof(_house_specs[subs_id]));
    01510
    01511         housespec->enabled = true;
    01512         housespec->local_id = hid + i;
    01513         housespec->substitute_id = subs_id;
    01514         housespec->grffile = _cur_grffile;
    01515         housespec->random_colour[0] = 0x04;  // those 4 random colours are the base colour
    01516         housespec->random_colour[1] = 0x08;  // for all new houses
    01517         housespec->random_colour[2] = 0x0C;  // they stand for red, blue, orange and green
    01518         housespec->random_colour[3] = 0x06;
    01519
    01520         /* Make sure that the third cargo type is valid in this
    01521          * climate. This can cause problems when copying the properties
    01522          * of a house that accepts food, where the new house is valid
    01523          * in the temperate climate. */
    01524         if (!GetCargo(housespec->accepts_cargo[2])->IsValid()) {
    01525           housespec->cargo_acceptance[2] = 0;
    01526         }
    01527
    01533         if (housespec->min_year < 1930) housespec->min_year = 1930;
    01534
    01535         _loaded_newgrf_features.has_newhouses = true;
    01536       } break;
    01537
    01538       case 0x09: // Building flags
    01539         housespec->building_flags = (BuildingFlags)grf_load_byte(&buf);
    01540         break;
    01541
    01542       case 0x0A: { // Availability years
    01543         uint16 years = grf_load_word(&buf);
    01544         housespec->min_year = GB(years, 0, 8) > 150 ? MAX_YEAR : ORIGINAL_BASE_YEAR + GB(years, 0, 8);
    01545         housespec->max_year = GB(years, 8, 8) > 150 ? MAX_YEAR : ORIGINAL_BASE_YEAR + GB(years, 8, 8);
    01546       } break;
    01547
    01548       case 0x0B: // Population
    01549         housespec->population = grf_load_byte(&buf);
    01550         break;
    01551
    01552       case 0x0C: // Mail generation multiplier
    01553         housespec->mail_generation = grf_load_byte(&buf);
    01554         break;
    01555
    01556       case 0x0D: // Passenger acceptance
    01557       case 0x0E: // Mail acceptance
    01558         housespec->cargo_acceptance[prop - 0x0D] = grf_load_byte(&buf);
    01559         break;
    01560
    01561       case 0x0F: { // Goods/candy, food/fizzy drinks acceptance
    01562         int8 goods = grf_load_byte(&buf);
    01563
    01564         /* If value of goods is negative, it means in fact food or, if in toyland, fizzy_drink acceptance.
    01565          * Else, we have "standard" 3rd cargo type, goods or candy, for toyland once more */
    01566         CargoID cid = (goods >= 0) ? ((_settings_game.game_creation.landscape == LT_TOYLAND) ? CT_CANDY : CT_GOODS) :
    01567             ((_settings_game.game_creation.landscape == LT_TOYLAND) ? CT_FIZZY_DRINKS : CT_FOOD);
    01568
    01569         /* Make sure the cargo type is valid in this climate. */
    01570         if (!GetCargo(cid)->IsValid()) goods = 0;
    01571
    01572         housespec->accepts_cargo[2] = cid;
    01573         housespec->cargo_acceptance[2] = abs(goods); // but we do need positive value here
    01574       } break;
    01575
    01576       case 0x10: // Local authority rating decrease on removal
    01577         housespec->remove_rating_decrease = grf_load_word(&buf);
    01578         break;
    01579
    01580       case 0x11: // Removal cost multiplier
    01581         housespec->removal_cost = grf_load_byte(&buf);
    01582         break;
    01583
    01584       case 0x12: // Building name ID
    01585         housespec->building_name = grf_load_word(&buf);
    01586         _string_to_grf_mapping[&housespec->building_name] = _cur_grffile->grfid;
    01587         break;
    01588
    01589       case 0x13: // Building availability mask
    01590         housespec->building_availability = (HouseZones)grf_load_word(&buf);
    01591         break;
    01592
    01593       case 0x14: // House callback flags
    01594         housespec->callback_mask = grf_load_byte(&buf);
    01595         break;
    01596
    01597       case 0x15: { // House override byte
    01598         byte override = grf_load_byte(&buf);
    01599
    01600         /* The house being overridden must be an original house. */
    01601         if (override >= NEW_HOUSE_OFFSET) {
    01602           grfmsg(2, "TownHouseChangeInfo: Attempt to override new house %u with house id %u. Ignoring.", override, hid + i);
    01603           continue;
    01604         }
    01605
    01606         _house_mngr.Add(hid + i, _cur_grffile->grfid, override);
    01607       } break;
    01608
    01609       case 0x16: // Periodic refresh multiplier
    01610         housespec->processing_time = grf_load_byte(&buf);
    01611         break;
    01612
    01613       case 0x17: // Four random colours to use
    01614         for (uint j = 0; j < 4; j++) housespec->random_colour[j] = grf_load_byte(&buf);
    01615         break;
    01616
    01617       case 0x18: // Relative probability of appearing
    01618         housespec->probability = grf_load_byte(&buf);
    01619         break;
    01620
    01621       case 0x19: // Extra flags
    01622         housespec->extra_flags = (HouseExtraFlags)grf_load_byte(&buf);
    01623         break;
    01624
    01625       case 0x1A: // Animation frames
    01626         housespec->animation_frames = grf_load_byte(&buf);
    01627         break;
    01628
    01629       case 0x1B: // Animation speed
    01630         housespec->animation_speed = Clamp(grf_load_byte(&buf), 2, 16);
    01631         break;
    01632
    01633       case 0x1C: // Class of the building type
    01634         housespec->class_id = AllocateHouseClassID(grf_load_byte(&buf), _cur_grffile->grfid);
    01635         break;
    01636
    01637       case 0x1D: // Callback flags 2
    01638         housespec->callback_mask |= (grf_load_byte(&buf) << 8);
    01639         break;
    01640
    01641       case 0x1E: { // Accepted cargo types
    01642         uint32 cargotypes = grf_load_dword(&buf);
    01643
    01644         /* Check if the cargo types should not be changed */
    01645         if (cargotypes == 0xFFFFFFFF) break;
    01646
    01647         for (uint j = 0; j < 3; j++) {
    01648           /* Get the cargo number from the 'list' */
    01649           uint8 cargo_part = GB(cargotypes, 8 * j, 8);
    01650           CargoID cargo = GetCargoTranslation(cargo_part, _cur_grffile);
    01651
    01652           if (cargo == CT_INVALID) {
    01653             /* Disable acceptance of invalid cargo type */
    01654             housespec->cargo_acceptance[j] = 0;
    01655           } else {
    01656             housespec->accepts_cargo[j] = cargo;
    01657           }
    01658         }
    01659       } break;
    01660
    01661       case 0x1F: // Minimum life span
    01662         housespec->minimum_life = grf_load_byte(&buf);
    01663         break;
    01664
    01665       case 0x20: { // @todo Cargo acceptance watch list
    01666         byte count = grf_load_byte(&buf);
    01667         for (byte j = 0; j < count; j++) grf_load_byte(&buf);
    01668         ret = CIR_UNHANDLED;
    01669       } break;
    01670
    01671       case 0x21: // long introduction year
    01672         housespec->min_year = grf_load_word(&buf);
    01673         break;
    01674
    01675       case 0x22: // long maximum year
    01676         housespec->max_year = grf_load_word(&buf);
    01677         break;
    01678
    01679       default:
    01680         ret = CIR_UNKNOWN;
    01681         break;
    01682     }
    01683   }
    01684
    01685   *bufp = buf;
    01686   return ret;
    01687 }
    01688
    01689 static ChangeInfoResult GlobalVarChangeInfo(uint gvid, int numinfo, int prop, byte **bufp, int len)
    01690 {
    01691   byte *buf = *bufp;
    01692   ChangeInfoResult ret = CIR_SUCCESS;
    01693
    01694   for (int i = 0; i < numinfo; i++) {
    01695     switch (prop) {
    01696       case 0x08: { // Cost base factor
    01697         byte factor = grf_load_byte(&buf);
    01698         uint price = gvid + i;
    01699
    01700         if (price < NUM_PRICES) {
    01701           SetPriceBaseMultiplier(price, factor);
    01702         } else {
    01703           grfmsg(1, "GlobalVarChangeInfo: Price %d out of range, ignoring", price);
    01704         }
    01705       } break;
    01706
    01707       case 0x09: // Cargo translation table
    01708         /* This is loaded during the reservation stage, so just skip it here. */
    01709         /* Each entry is 4 bytes. */
    01710         buf += 4;
    01711         break;
    01712
    01713       case 0x0A: { // Currency display names
    01714         uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
    01715         StringID newone = GetGRFStringID(_cur_grffile->grfid, grf_load_word(&buf));
    01716
    01717         if ((newone != STR_UNDEFINED) && (curidx < NUM_CURRENCY)) {
    01718           _currency_specs[curidx].name = newone;
    01719         }
    01720       } break;
    01721
    01722       case 0x0B: { // Currency multipliers
    01723         uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
    01724         uint32 rate = grf_load_dword(&buf);
    01725
    01726         if (curidx < NUM_CURRENCY) {
    01727           /* TTDPatch uses a multiple of 1000 for its conversion calculations,
    01728            * which OTTD does not. For this reason, divide grf value by 1000,
    01729            * to be compatible */
    01730           _currency_specs[curidx].rate = rate / 1000;
    01731         } else {
    01732           grfmsg(1, "GlobalVarChangeInfo: Currency multipliers %d out of range, ignoring", curidx);
    01733         }
    01734       } break;
    01735
    01736       case 0x0C: { // Currency options
    01737         uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
    01738         uint16 options = grf_load_word(&buf);
    01739
    01740         if (curidx < NUM_CURRENCY) {
    01741           _currency_specs[curidx].separator = GB(options, 0, 8);
    01742           /* By specifying only one bit, we prevent errors,
    01743            * since newgrf specs said that only 0 and 1 can be set for symbol_pos */
    01744           _currency_specs[curidx].symbol_pos = GB(options, 8, 1);
    01745         } else {
    01746           grfmsg(1, "GlobalVarChangeInfo: Currency option %d out of range, ignoring", curidx);
    01747         }
    01748       } break;
    01749
    01750       case 0x0D: { // Currency prefix symbol
    01751         uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
    01752         uint32 tempfix = grf_load_dword(&buf);
    01753
    01754         if (curidx < NUM_CURRENCY) {
    01755           memcpy(_currency_specs[curidx].prefix, &tempfix, 4);
    01756           _currency_specs[curidx].prefix[4] = 0;
    01757         } else {
    01758           grfmsg(1, "GlobalVarChangeInfo: Currency symbol %d out of range, ignoring", curidx);
    01759         }
    01760       } break;
    01761
    01762       case 0x0E: { // Currency suffix symbol
    01763         uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
    01764         uint32 tempfix = grf_load_dword(&buf);
    01765
    01766         if (curidx < NUM_CURRENCY) {
    01767           memcpy(&_currency_specs[curidx].suffix, &tempfix, 4);
    01768           _currency_specs[curidx].suffix[4] = 0;
    01769         } else {
    01770           grfmsg(1, "GlobalVarChangeInfo: Currency symbol %d out of range, ignoring", curidx);
    01771         }
    01772       } break;
    01773
    01774       case 0x0F: { //  Euro introduction dates
    01775         uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
    01776         Year year_euro = grf_load_word(&buf);
    01777
    01778         if (curidx < NUM_CURRENCY) {
    01779           _currency_specs[curidx].to_euro = year_euro;
    01780         } else {
    01781           grfmsg(1, "GlobalVarChangeInfo: Euro intro date %d out of range, ignoring", curidx);
    01782         }
    01783       } break;
    01784
    01785       case 0x10: // Snow line height table
    01786         if (numinfo > 1 || IsSnowLineSet()) {
    01787           grfmsg(1, "GlobalVarChangeInfo: The snowline can only be set once (%d)", numinfo);
    01788         } else if (len < SNOW_LINE_MONTHS * SNOW_LINE_DAYS) {
    01789           grfmsg(1, "GlobalVarChangeInfo: Not enough entries set in the snowline table (%d)", len);
    01790         } else {
    01791           byte table[SNOW_LINE_MONTHS][SNOW_LINE_DAYS];
    01792
    01793           for (uint i = 0; i < SNOW_LINE_MONTHS; i++) {
    01794             for (uint j = 0; j < SNOW_LINE_DAYS; j++) {
    01795               table[i][j] = grf_load_byte(&buf);
    01796             }
    01797           }
    01798           SetSnowLine(table);
    01799         }
    01800         break;
    01801
    01802       case 0x11: // GRF match for engine allocation
    01803         /* This is loaded during the reservation stage, so just skip it here. */
    01804         /* Each entry is 8 bytes. */
    01805         buf += 8;
    01806         break;
    01807
    01808       case 0x12: // Rail type translation table
    01809         /* This is loaded during the reservation stage, so just skip it here. */
    01810         /* Each entry is 4 bytes. */
    01811         buf += 4;
    01812         break;
    01813
    01814       default:
    01815         ret = CIR_UNKNOWN;
    01816         break;
    01817     }
    01818   }
    01819
    01820   *bufp = buf;
    01821   return ret;
    01822 }
    01823
    01824 static ChangeInfoResult GlobalVarReserveInfo(uint gvid, int numinfo, int prop, byte **bufp, int len)
    01825 {
    01826   byte *buf = *bufp;
    01827   ChangeInfoResult ret = CIR_SUCCESS;
    01828
    01829   for (int i = 0; i < numinfo; i++) {
    01830     switch (prop) {
    01831       case 0x08: // Cost base factor
    01832         grf_load_byte(&buf);
    01833         break;
    01834
    01835       case 0x09: { // Cargo Translation Table
    01836         if (i == 0) {
    01837           if (gvid != 0) {
    01838             grfmsg(1, "ReserveChangeInfo: Cargo translation table must start at zero");
    01839             return CIR_INVALID_ID;
    01840           }
    01841
    01842           free(_cur_grffile->cargo_list);
    01843           _cur_grffile->cargo_max = numinfo;
    01844           _cur_grffile->cargo_list = MallocT<CargoLabel>(numinfo);
    01845         }
    01846
    01847         CargoLabel cl = grf_load_dword(&buf);
    01848         _cur_grffile->cargo_list[i] = BSWAP32(cl);
    01849         break;
    01850       }
    01851
    01852       case 0x0A: // Currency display names
    01853       case 0x0C: // Currency options
    01854       case 0x0F: // Euro introduction dates
    01855         grf_load_word(&buf);
    01856         break;
    01857
    01858       case 0x0B: // Currency multipliers
    01859       case 0x0D: // Currency prefix symbol
    01860       case 0x0E: // Currency suffix symbol
    01861         grf_load_dword(&buf);
    01862         break;
    01863
    01864       case 0x10: // Snow line height table
    01865         buf += SNOW_LINE_MONTHS * SNOW_LINE_DAYS;
    01866         break;
    01867
    01868       case 0x11: { // GRF match for engine allocation
    01869         uint32 s = grf_load_dword(&buf);
    01870         uint32 t = grf_load_dword(&buf);
    01871         SetNewGRFOverride(s, t);
    01872         break;
    01873       }
    01874
    01875       case 0x12: { // Rail type translation table
    01876         if (i == 0) {
    01877           if (gvid != 0) {
    01878             grfmsg(1, "ReserveChangeInfo: Rail type translation table must start at zero");
    01879             return CIR_INVALID_ID;
    01880           }
    01881
    01882           free(_cur_grffile->railtype_list);
    01883           _cur_grffile->railtype_max = numinfo;
    01884           _cur_grffile->railtype_list = MallocT<RailTypeLabel>(numinfo);
    01885         }
    01886
    01887         RailTypeLabel rtl = grf_load_dword(&buf);
    01888         _cur_grffile->railtype_list[i] = BSWAP32(rtl);
    01889         break;
    01890       }
    01891
    01892       default:
    01893         ret = CIR_UNKNOWN;
    01894         break;
    01895     }
    01896   }
    01897
    01898   *bufp = buf;
    01899   return ret;
    01900 }
    01901
    01902
    01903 static ChangeInfoResult CargoChangeInfo(uint cid, int numinfo, int prop, byte **bufp, int len)
    01904 {
    01905   byte *buf = *bufp;
    01906   ChangeInfoResult ret = CIR_SUCCESS;
    01907
    01908   if (cid + numinfo > NUM_CARGO) {
    01909     grfmsg(2, "CargoChangeInfo: Cargo type %d out of range (max %d)", cid + numinfo, NUM_CARGO - 1);
    01910     return CIR_INVALID_ID;
    01911   }
    01912
    01913   for (int i = 0; i < numinfo; i++) {
    01914     CargoSpec *cs = &_cargo[cid + i];
    01915
    01916     switch (prop) {
    01917       case 0x08: // Bit number of cargo
    01918         cs->bitnum = grf_load_byte(&buf);
    01919         if (cs->IsValid()) {
    01920           cs->grffile = _cur_grffile;
    01921           SetBit(_cargo_mask, cid + i);
    01922         } else {
    01923           ClrBit(_cargo_mask, cid + i);
    01924         }
    01925         break;
    01926
    01927       case 0x09: // String ID for cargo type name
    01928         cs->name = grf_load_word(&buf);
    01929         _string_to_grf_mapping[&cs->name] = _cur_grffile->grfid;
    01930         break;
    01931
    01932       case 0x0A: // String for 1 unit of cargo
    01933         cs->name_single = grf_load_word(&buf);
    01934         _string_to_grf_mapping[&cs->name_single] = _cur_grffile->grfid;
    01935         break;
    01936
    01937       case 0x0B:
    01938         /* String for units of cargo. This is different in OpenTTD to TTDPatch
    01939          * (e.g. 10 tonnes of coal) */
    01940         cs->units_volume = grf_load_word(&buf);
    01941         _string_to_grf_mapping[&cs->units_volume] = _cur_grffile->grfid;
    01942         break;
    01943
    01944       case 0x0C: // String for quantity of cargo (e.g. 10 tonnes of coal)
    01945         cs->quantifier = grf_load_word(&buf);
    01946         _string_to_grf_mapping[&cs->quantifier] = _cur_grffile->grfid;
    01947         break;
    01948
    01949       case 0x0D: // String for two letter cargo abbreviation
    01950         cs->abbrev = grf_load_word(&buf);
    01951         _string_to_grf_mapping[&cs->abbrev] = _cur_grffile->grfid;
    01952         break;
    01953
    01954       case 0x0E: // Sprite ID for cargo icon
    01955         cs->sprite = grf_load_word(&buf);
    01956         break;
    01957
    01958       case 0x0F: // Weight of one unit of cargo
    01959         cs->weight = grf_load_byte(&buf);
    01960         break;
    01961
    01962       case 0x10: // Used for payment calculation
    01963         cs->transit_days[0] = grf_load_byte(&buf);
    01964         break;
    01965
    01966       case 0x11: // Used for payment calculation
    01967         cs->transit_days[1] = grf_load_byte(&buf);
    01968         break;
    01969
    01970       case 0x12: // Base cargo price
    01971         cs->initial_payment = grf_load_dword(&buf);
    01972         break;
    01973
    01974       case 0x13: // Colour for station rating bars
    01975         cs->rating_colour = MapDOSColour(grf_load_byte(&buf));
    01976         break;
    01977
    01978       case 0x14: // Colour for cargo graph
    01979         cs->legend_colour = MapDOSColour(grf_load_byte(&buf));
    01980         break;
    01981
    01982       case 0x15: // Freight status
    01983         cs->is_freight = (grf_load_byte(&buf) != 0);
    01984         break;
    01985
    01986       case 0x16: // Cargo classes
    01987         cs->classes = grf_load_word(&buf);
    01988         break;
    01989
    01990       case 0x17: // Cargo label
    01991         cs->label = grf_load_dword(&buf);
    01992         cs->label = BSWAP32(cs->label);
    01993         break;
    01994
    01995       case 0x18: { // Town growth substitute type
    01996         uint8 substitute_type = grf_load_byte(&buf);
    01997
    01998         switch (substitute_type) {
    01999           case 0x00: cs->town_effect = TE_PASSENGERS; break;
    02000           case 0x02: cs->town_effect = TE_MAIL; break;
    02001           case 0x05: cs->town_effect = TE_GOODS; break;
    02002           case 0x09: cs->town_effect = TE_WATER; break;
    02003           case 0x0B: cs->town_effect = TE_FOOD; break;
    02004           default:
    02005             grfmsg(1, "CargoChangeInfo: Unknown town growth substitute value %d, setting to none.", substitute_type);
    02006           case 0xFF: cs->town_effect = TE_NONE; break;
    02007         }
    02008       } break;
    02009
    02010       case 0x19: // Town growth coefficient
    02011         cs->multipliertowngrowth = grf_load_word(&buf);
    02012         break;
    02013
    02014       case 0x1A: // Bitmask of callbacks to use
    02015         cs->callback_mask = grf_load_byte(&buf);
    02016         break;
    02017
    02018       default:
    02019         ret = CIR_UNKNOWN;
    02020         break;
    02021     }
    02022   }
    02023
    02024   *bufp = buf;
    02025   return ret;
    02026 }
    02027
    02028
    02029 static ChangeInfoResult SoundEffectChangeInfo(uint sid, int numinfo, int prop, byte **bufp, int len)
    02030 {
    02031   byte *buf = *bufp;
    02032   ChangeInfoResult ret = CIR_SUCCESS;
    02033
    02034   if (_cur_grffile->sound_offset == 0) {
    02035     grfmsg(1, "SoundEffectChangeInfo: No effects defined, skipping");
    02036     return CIR_INVALID_ID;
    02037   }
    02038
    02039   for (int i = 0; i < numinfo; i++) {
    02040     uint sound = sid + i + _cur_grffile->sound_offset - GetNumOriginalSounds();
    02041
    02042     if (sound >= GetNumSounds()) {
    02043       grfmsg(1, "SoundEffectChangeInfo: Sound %d not defined (max %d)", sound, GetNumSounds());
    02044       return CIR_INVALID_ID;
    02045     }
    02046
    02047     switch (prop) {
    02048       case 0x08: // Relative volume
    02049         GetSound(sound)->volume = grf_load_byte(&buf);
    02050         break;
    02051
    02052       case 0x09: // Priority
    02053         GetSound(sound)->priority = grf_load_byte(&buf);
    02054         break;
    02055
    02056       case 0x0A: { // Override old sound
    02057         uint orig_sound = grf_load_byte(&buf);
    02058
    02059         if (orig_sound >= ORIGINAL_SAMPLE_COUNT) {
    02060           grfmsg(1, "SoundEffectChangeInfo: Original sound %d not defined (max %d)", orig_sound, ORIGINAL_SAMPLE_COUNT);
    02061         } else {
    02062           FileEntry *newfe = GetSound(sound);
    02063           FileEntry *oldfe = GetSound(orig_sound);
    02064
    02065           /* Literally copy the data of the new sound over the original */
    02066           *oldfe = *newfe;
    02067         }
    02068       } break;
    02069
    02070       default:
    02071         ret = CIR_UNKNOWN;
    02072         break;
    02073     }
    02074   }
    02075
    02076   *bufp = buf;
    02077   return ret;
    02078 }
    02079
    02080 static ChangeInfoResult IndustrytilesChangeInfo(uint indtid, int numinfo, int prop, byte **bufp, int len)
    02081 {
    02082   byte *buf = *bufp;
    02083   ChangeInfoResult ret = CIR_SUCCESS;
    02084
    02085   if (indtid + numinfo > NUM_INDUSTRYTILES) {
    02086     grfmsg(1, "IndustryTilesChangeInfo: Too many industry tiles loaded (%u), max (%u). Ignoring.", indtid + numinfo, NUM_INDUSTRYTILES);
    02087     return CIR_INVALID_ID;
    02088   }
    02089
    02090   /* Allocate industry tile specs if they haven't been allocated already. */
    02091   if (_cur_grffile->indtspec == NULL) {
    02092     _cur_grffile->indtspec = CallocT<IndustryTileSpec*>(NUM_INDUSTRYTILES);
    02093   }
    02094
    02095   for (int i = 0; i < numinfo; i++) {
    02096     IndustryTileSpec *tsp = _cur_grffile->indtspec[indtid + i];
    02097
    02098     if (prop != 0x08 && tsp == NULL) {
    02099       grfmsg(2, "IndustryTilesChangeInfo: Attempt to modify undefined industry tile %u. Ignoring.", indtid + i);
    02100       return CIR_INVALID_ID;
    02101     }
    02102
    02103     switch (prop) {
    02104       case 0x08: { // Substitute industry tile type
    02105         IndustryTileSpec **tilespec = &_cur_grffile->indtspec[indtid + i];
    02106         byte subs_id = grf_load_byte(&buf);
    02107
    02108         if (subs_id >= NEW_INDUSTRYTILEOFFSET) {
    02109           /* The substitute id must be one of the original industry tile. */
    02110           grfmsg(2, "IndustryTilesChangeInfo: Attempt to use new industry tile %u as substitute industry tile for %u. Ignoring.", subs_id, indtid + i);
    02111           continue;
    02112         }
    02113
    02114         /* Allocate space for this industry. */
    02115         if (*tilespec == NULL) {
    02116           int tempid;
    02117           *tilespec = CallocT<IndustryTileSpec>(1);
    02118           tsp = *tilespec;
    02119
    02120           memcpy(tsp, &_industry_tile_specs[subs_id], sizeof(_industry_tile_specs[subs_id]));
    02121           tsp->enabled = true;
    02122
    02123           /* A copied tile should not have the animation infos copied too.
    02124            * The anim_state should be left untouched, though
    02125            * It is up to the author to animate them himself */
    02126           tsp->anim_production = INDUSTRYTILE_NOANIM;
    02127           tsp->anim_next = INDUSTRYTILE_NOANIM;
    02128
    02129           tsp->grf_prop.local_id = indtid + i;
    02130           tsp->grf_prop.subst_id = subs_id;
    02131           tsp->grf_prop.grffile = _cur_grffile;
    02132           tempid = _industile_mngr.AddEntityID(indtid + i, _cur_grffile->grfid, subs_id); // pre-reserve the tile slot
    02133         }
    02134       } break;
    02135
    02136       case 0x09: { // Industry tile override
    02137         byte ovrid = grf_load_byte(&buf);
    02138
    02139         /* The industry being overridden must be an original industry. */
    02140         if (ovrid >= NEW_INDUSTRYTILEOFFSET) {
    02141           grfmsg(2, "IndustryTilesChangeInfo: Attempt to override new industry tile %u with industry tile id %u. Ignoring.", ovrid, indtid + i);
    02142           continue;
    02143         }
    02144
    02145         _industile_mngr.Add(indtid + i, _cur_grffile->grfid, ovrid);
    02146       } break;
    02147
    02148       case 0x0A: // Tile acceptance
    02149       case 0x0B:
    02150       case 0x0C: {
    02151         uint16 acctp = grf_load_word(&buf);
    02152         tsp->accepts_cargo[prop - 0x0A] = GetCargoTranslation(GB(acctp, 0, 8), _cur_grffile);
    02153         tsp->acceptance[prop - 0x0A] = GB(acctp, 8, 8);
    02154       } break;
    02155
    02156       case 0x0D: // Land shape flags
    02157         tsp->slopes_refused = (Slope)grf_load_byte(&buf);
    02158         break;
    02159
    02160       case 0x0E: // Callback flags
    02161         tsp->callback_flags = grf_load_byte(&buf);
    02162         break;
    02163
    02164       case 0x0F: // Animation information
    02165         tsp->animation_info = grf_load_word(&buf);
    02166         break;
    02167
    02168       case 0x10: // Animation speed
    02169         tsp->animation_speed = grf_load_byte(&buf);
    02170         break;
    02171
    02172       case 0x11: // Triggers for callback 25
    02173         tsp->animation_triggers = grf_load_byte(&buf);
    02174         break;
    02175
    02176       case 0x12: // Special flags
    02177         tsp->animation_special_flags = grf_load_byte(&buf);
    02178         break;
    02179
    02180       default:
    02181         ret = CIR_UNKNOWN;
    02182         break;
    02183     }
    02184   }
    02185
    02186   *bufp = buf;
    02187   return ret;
    02188 }
    02189
    02196 static bool ValidateIndustryLayout(const IndustryTileTable *layout, int size)
    02197 {
    02198   for (int i = 0; i < size - 1; i++) {
    02199     for (int j = i + 1; j < size; j++) {
    02200       if (layout[i].ti.x == layout[j].ti.x &&
    02201           layout[i].ti.y == layout[j].ti.y) {
    02202         return false;
    02203       }
    02204     }
    02205   }
    02206   return true;
    02207 }
    02208
    02209 static ChangeInfoResult IndustriesChangeInfo(uint indid, int numinfo, int prop, byte **bufp, int len)
    02210 {
    02211   byte *buf = *bufp;
    02212   ChangeInfoResult ret = CIR_SUCCESS;
    02213
    02214   if (indid + numinfo > NUM_INDUSTRYTYPES) {
    02215     grfmsg(1, "IndustriesChangeInfo: Too many industries loaded (%u), max (%u). Ignoring.", indid + numinfo, NUM_INDUSTRYTYPES);
    02216     return CIR_INVALID_ID;
    02217   }
    02218
    02219   grfmsg(1, "IndustriesChangeInfo: newid %u", indid);
    02220
    02221   /* Allocate industry specs if they haven't been allocated already. */
    02222   if (_cur_grffile->industryspec == NULL) {
    02223     _cur_grffile->industryspec = CallocT<IndustrySpec*>(NUM_INDUSTRYTYPES);
    02224   }
    02225
    02226   for (int i = 0; i < numinfo; i++) {
    02227     IndustrySpec *indsp = _cur_grffile->industryspec[indid + i];
    02228
    02229     if (prop != 0x08 && indsp == NULL) {
    02230       grfmsg(2, "IndustriesChangeInfo: Attempt to modify undefined industry %u. Ignoring.", indid + i);
    02231       return CIR_INVALID_ID;
    02232     }
    02233
    02234     switch (prop) {
    02235       case 0x08: { // Substitute industry type
    02236         IndustrySpec **indspec = &_cur_grffile->industryspec[indid + i];
    02237         byte subs_id = grf_load_byte(&buf);
    02238
    02239         if (subs_id == 0xFF) {
    02240           /* Instead of defining a new industry, a substitute industry id
    02241            * of 0xFF disables the old industry with the current id. */
    02242           _industry_specs[indid + i].enabled = false;
    02243           continue;
    02244         } else if (subs_id >= NEW_INDUSTRYOFFSET) {
    02245           /* The substitute id must be one of the original industry. */
    02246           grfmsg(2, "_industry_specs: Attempt to use new industry %u as substitute industry for %u. Ignoring.", subs_id, indid + i);
    02247           continue;
    02248         }
    02249
    02250         /* Allocate space for this industry.
    02251          * Only need to do it once. If ever it is called again, it should not
    02252          * do anything */
    02253         if (*indspec == NULL) {
    02254           *indspec = CallocT<IndustrySpec>(1);
    02255           indsp = *indspec;
    02256
    02257           memcpy(indsp, &_origin_industry_specs[subs_id], sizeof(_industry_specs[subs_id]));
    02258           indsp->enabled = true;
    02259           indsp->grf_prop.local_id = indid + i;
    02260           indsp->grf_prop.subst_id = subs_id;
    02261           indsp->grf_prop.grffile = _cur_grffile;
    02262           /* If the grf industry needs to check its surounding upon creation, it should
    02263            * rely on callbacks, not on the original placement functions */
    02264           indsp->check_proc = CHECK_NOTHING;
    02265         }
    02266       } break;
    02267
    02268       case 0x09: { // Industry type override
    02269         byte ovrid = grf_load_byte(&buf);
    02270
    02271         /* The industry being overridden must be an original industry. */
    02272         if (ovrid >= NEW_INDUSTRYOFFSET) {
    02273           grfmsg(2, "IndustriesChangeInfo: Attempt to override new industry %u with industry id %u. Ignoring.", ovrid, indid + i);
    02274           continue;
    02275         }
    02276         indsp->grf_prop.override = ovrid;
    02277         _industry_mngr.Add(indid + i, _cur_grffile->grfid, ovrid);
    02278       } break;
    02279
    02280       case 0x0A: { // Set industry layout(s)
    02281         indsp->num_table = grf_load_byte(&buf); // Number of layaouts
    02282         uint32 defsize = grf_load_dword(&buf);  // Total size of the definition
    02283         IndustryTileTable **tile_table = CallocT<IndustryTileTable*>(indsp->num_table); // Table with tiles to compose an industry
    02284         IndustryTileTable *itt = CallocT<IndustryTileTable>(defsize); // Temporary array to read the tile layouts from the GRF
    02285         int size;
    02286         IndustryTileTable *copy_from;
    02287
    02288         for (byte j = 0; j < indsp->num_table; j++) {
    02289           for (int k = 0;; k++) {
    02290             itt[k].ti.x = grf_load_byte(&buf); // Offsets from northermost tile
    02291
    02292             if (itt[k].ti.x == 0xFE && k == 0) {
    02293               /* This means we have to borrow the layout from an old industry */
    02294               IndustryType type = grf_load_byte(&buf);  // industry holding required layout
    02295               byte laynbr = grf_load_byte(&buf);        // layout number to borrow
    02296
    02297               copy_from = (IndustryTileTable*)_origin_industry_specs[type].table[laynbr];
    02298               for (size = 1;; size++) {
    02299                 if (copy_from[size - 1].ti.x == -0x80 && copy_from[size - 1].ti.y == 0) break;
    02300               }
    02301               break;
    02302             }
    02303
    02304             itt[k].ti.y = grf_load_byte(&buf); // Or table definition finalisation
    02305
    02306             if (itt[k].ti.x == 0 && itt[k].ti.y == 0x80) {
    02307               /*  Not the same terminator.  The one we are using is rather
    02308                x= -80, y = x .  So, adjust it. */
    02309               itt[k].ti.x = -0x80;
    02310               itt[k].ti.y =  0;
    02311               itt[k].gfx  =  0;
    02312
    02313               size = k + 1;
    02314               copy_from = itt;
    02315               break;
    02316             }
    02317
    02318             itt[k].gfx = grf_load_byte(&buf);
    02319
    02320             if (itt[k].gfx == 0xFE) {
    02321               /* Use a new tile from this GRF */
    02322               int local_tile_id = grf_load_word(&buf);
    02323
    02324               /* Read the ID from the _industile_mngr. */
    02325               int tempid = _industile_mngr.GetID(local_tile_id, _cur_grffile->grfid);
    02326
    02327               if (tempid == INVALID_INDUSTRYTILE) {
    02328                 grfmsg(2, "IndustriesChangeInfo: Attempt to use industry tile %u with industry id %u, not yet defined. Ignoring.", local_tile_id, indid);
    02329               } else {
    02330                 /* Declared as been valid, can be used */
    02331                 itt[k].gfx = tempid;
    02332                 size = k + 1;
    02333                 copy_from = itt;
    02334               }
    02335             } else if (itt[k].gfx == 0xFF) {
    02336               itt[k].ti.x = (int8)GB(itt[k].ti.x, 0, 8);
    02337               itt[k].ti.y = (int8)GB(itt[k].ti.y, 0, 8);
    02338             }
    02339           }
    02340
    02341           if (!ValidateIndustryLayout(copy_from, size)) {
    02342             /* The industry layout was not valid, so skip this one. */
    02343             grfmsg(1, "IndustriesChangeInfo: Invalid industry layout for industry id %u. Ignoring", indid);
    02344             indsp->num_table--;
    02345             j--;
    02346           } else {
    02347             tile_table[j] = CallocT<IndustryTileTable>(size);
    02348             memcpy(tile_table[j], copy_from, sizeof(*copy_from) * size);
    02349           }
    02350         }
    02351         /* Install final layout construction in the industry spec */
    02352         indsp->table = tile_table;
    02353         SetBit(indsp->cleanup_flag, 1);
    02354         free(itt);
    02355       } break;
    02356
    02357       case 0x0B: // Industry production flags
    02358         indsp->life_type = (IndustryLifeType)grf_load_byte(&buf);
    02359         break;
    02360
    02361       case 0x0C: // Industry closure message
    02362         indsp->closure_text = grf_load_word(&buf);
    02363         _string_to_grf_mapping[&indsp->closure_text] = _cur_grffile->grfid;
    02364         break;
    02365
    02366       case 0x0D: // Production increase message
    02367         indsp->production_up_text = grf_load_word(&buf);
    02368         _string_to_grf_mapping[&indsp->production_up_text] = _cur_grffile->grfid;
    02369         break;
    02370
    02371       case 0x0E: // Production decrease message
    02372         indsp->production_down_text = grf_load_word(&buf);
    02373         _string_to_grf_mapping[&indsp->production_down_text] = _cur_grffile->grfid;
    02374         break;
    02375
    02376       case 0x0F: // Fund cost multiplier
    02377         indsp->cost_multiplier = grf_load_byte(&buf);
    02378         break;
    02379
    02380       case 0x10: // Production cargo types
    02381         for (byte j = 0; j < 2; j++) {
    02382           indsp->produced_cargo[j] = GetCargoTranslation(grf_load_byte(&buf), _cur_grffile);
    02383         }
    02384         break;
    02385
    02386       case 0x11: // Acceptance cargo types
    02387         for (byte j = 0; j < 3; j++) {
    02388           indsp->accepts_cargo[j] = GetCargoTranslation(grf_load_byte(&buf), _cur_grffile);
    02389         }
    02390         grf_load_byte(&buf); // Unnused, eat it up
    02391         break;
    02392
    02393       case 0x12: // Production multipliers
    02394       case 0x13:
    02395         indsp->production_rate[prop - 0x12] = grf_load_byte(&buf);
    02396         break;
    02397
    02398       case 0x14: // Minimal amount of cargo distributed
    02399         indsp->minimal_cargo = grf_load_byte(&buf);
    02400         break;
    02401
    02402       case 0x15: { // Random sound effects
    02403         indsp->number_of_sounds = grf_load_byte(&buf);
    02404         uint8 *sounds = MallocT<uint8>(indsp->number_of_sounds);
    02405
    02406         for (uint8 j = 0; j < indsp->number_of_sounds; j++) sounds[j] = grf_load_byte(&buf);
    02407         indsp->random_sounds = sounds;
    02408         SetBit(indsp->cleanup_flag, 0);
    02409       } break;
    02410
    02411       case 0x16: // Conflicting industry types
    02412         for (byte j = 0; j < 3; j++) indsp->conflicting[j] = grf_load_byte(&buf);
    02413         break;
    02414
    02415       case 0x17: // Probability in random game
    02416         indsp->appear_creation[_settings_game.game_creation.landscape] = grf_load_byte(&buf);
    02417         break;
    02418
    02419       case 0x18: // Probability during gameplay
    02420         indsp->appear_ingame[_settings_game.game_creation.landscape] = grf_load_byte(&buf);
    02421         break;
    02422
    02423       case 0x19: // Map colour
    02424         indsp->map_colour = MapDOSColour(grf_load_byte(&buf));
    02425         break;
    02426
    02427       case 0x1A: // Special industry flags to define special behavior
    02428         indsp->behaviour = (IndustryBehaviour)grf_load_dword(&buf);
    02429         break;
    02430
    02431       case 0x1B: // New industry text ID
    02432         indsp->new_industry_text = grf_load_word(&buf);
    02433         _string_to_grf_mapping[&indsp->new_industry_text] = _cur_grffile->grfid;
    02434         break;
    02435
    02436       case 0x1C: // Input cargo multipliers for the three input cargo types
    02437       case 0x1D:
    02438       case 0x1E: {
    02439           uint32 multiples = grf_load_dword(&buf);
    02440           indsp->input_cargo_multiplier[prop - 0x1C][0] = GB(multiples, 0, 16);
    02441           indsp->input_cargo_multiplier[prop - 0x1C][1] = GB(multiples, 16, 16);
    02442         } break;
    02443
    02444       case 0x1F: // Industry name
    02445         indsp->name = grf_load_word(&buf);
    02446         _string_to_grf_mapping[&indsp->name] = _cur_grffile->grfid;
    02447         break;
    02448
    02449       case 0x20: // Prospecting success chance
    02450         indsp->prospecting_chance = grf_load_dword(&buf);
    02451         break;
    02452
    02453       case 0x21:   // Callback flags
    02454       case 0x22: { // Callback additional flags
    02455         byte aflag = grf_load_byte(&buf);
    02456         SB(indsp->callback_flags, (prop - 0x21) * 8, 8, aflag);
    02457       } break;
    02458
    02459       case 0x23: // removal cost multiplier
    02460         indsp->removal_cost_multiplier = grf_load_dword(&buf);
    02461         break;
    02462
    02463       case 0x24: // name for nearby station
    02464         indsp->station_name = grf_load_word(&buf);
    02465         _string_to_grf_mapping[&indsp->station_name] = _cur_grffile->grfid;
    02466         break;
    02467
    02468       default:
    02469         ret = CIR_UNKNOWN;
    02470         break;
    02471     }
    02472   }
    02473
    02474   *bufp = buf;
    02475   return ret;
    02476 }
    02477
    02478 static bool HandleChangeInfoResult(const char *caller, ChangeInfoResult cir, uint8 feature, uint8 property)
    02479 {
    02480   switch (cir) {
    02481     default: NOT_REACHED();
    02482
    02483     case CIR_SUCCESS:
    02484       return false;
    02485
    02486     case CIR_UNHANDLED:
    02487       grfmsg(1, "%s: Ignoring property 0x%02X of feature 0x%02X (not implemented)", caller, property, feature);
    02488       return false;
    02489
    02490     case CIR_UNKNOWN:
    02491       grfmsg(0, "%s: Unknown property 0x%02X of feature 0x%02X, disabling", caller, property, feature);
    02492       /* Fall through */
    02493
    02494     case CIR_INVALID_ID:
    02495       /* No debug message for an invalid ID, as it has already been output */
    02496       _skip_sprites = -1;
    02497       _cur_grfconfig->status = GCS_DISABLED;
    02498       _cur_grfconfig->error  = CallocT<GRFError>(1);
    02499       _cur_grfconfig->error->severity = STR_NEWGRF_ERROR_MSG_FATAL;
    02500       _cur_grfconfig->error->message  = (cir == CIR_INVALID_ID) ? STR_NEWGRF_ERROR_INVALID_ID : STR_NEWGRF_ERROR_UNKNOWN_PROPERTY;
    02501       return true;
    02502   }
    02503 }
    02504
    02505 /* Action 0x00 */
    02506 static void FeatureChangeInfo(byte *buf, size_t len)
    02507 {
    02508   byte *bufend = buf + len;
    02509
    02510   /* <00> <feature> <num-props> <num-info> <id> (<property <new-info>)...
    02511    *
    02512    * B feature       0, 1, 2 or 3 for trains, road vehicles, ships or planes
    02513    *                 4 for defining new train station sets
    02514    * B num-props     how many properties to change per vehicle/station
    02515    * B num-info      how many vehicles/stations to change
    02516    * E id            ID of first vehicle/station to change, if num-info is
    02517    *                 greater than one, this one and the following
    02518    *                 vehicles/stations will be changed
    02519    * B property      what property to change, depends on the feature
    02520    * V new-info      new bytes of info (variable size; depends on properties) */
    02521   /* TODO: Bridges, town houses. */
    02522
    02523   static const VCI_Handler handler[] = {
    02524     /* GSF_TRAIN */        RailVehicleChangeInfo,
    02525     /* GSF_ROAD */         RoadVehicleChangeInfo,
    02526     /* GSF_SHIP */         ShipVehicleChangeInfo,
    02527     /* GSF_AIRCRAFT */     AircraftVehicleChangeInfo,
    02528     /* GSF_STATION */      StationChangeInfo,
    02529     /* GSF_CANAL */        CanalChangeInfo,
    02530     /* GSF_BRIDGE */       BridgeChangeInfo,
    02531     /* GSF_TOWNHOUSE */    TownHouseChangeInfo,
    02532     /* GSF_GLOBALVAR */    GlobalVarChangeInfo,
    02533     /* GSF_INDUSTRYTILES */IndustrytilesChangeInfo,
    02534     /* GSF_INDUSTRIES */   IndustriesChangeInfo,
    02535     /* GSF_CARGOS */       NULL, // Cargo is handled during reservation
    02536     /* GSF_SOUNDFX */      SoundEffectChangeInfo,
    02537   };
    02538
    02539   if (!check_length(len, 6, "FeatureChangeInfo")) return;
    02540   buf++;
    02541   uint8 feature  = grf_load_byte(&buf);
    02542   uint8 numprops = grf_load_byte(&buf);
    02543   uint numinfo  = grf_load_byte(&buf);
    02544   uint engine   = grf_load_extended(&buf);
    02545
    02546   grfmsg(6, "FeatureChangeInfo: feature %d, %d properties, to apply to %d+%d",
    02547                  feature, numprops, engine, numinfo);
    02548
    02549   if (feature >= lengthof(handler) || handler[feature] == NULL) {
    02550     grfmsg(1, "FeatureChangeInfo: Unsupported feature %d, skipping", feature);
    02551     return;
    02552   }
    02553
    02554   while (numprops-- && buf < bufend) {
    02555     uint8 prop = grf_load_byte(&buf);
    02556
    02557     ChangeInfoResult cir = handler[feature](engine, numinfo, prop, &buf, bufend - buf);
    02558     if (HandleChangeInfoResult("FeatureChangeInfo", cir, feature, prop)) return;
    02559   }
    02560 }
    02561
    02562 /* Action 0x00 (GLS_SAFETYSCAN) */
    02563 static void SafeChangeInfo(byte *buf, size_t len)
    02564 {
    02565   if (!check_length(len, 6, "SafeChangeInfo")) return;
    02566   buf++;
    02567   uint8 feature  = grf_load_byte(&buf);
    02568   uint8 numprops = grf_load_byte(&buf);
    02569   uint numinfo = grf_load_byte(&buf);
    02570   grf_load_extended(&buf); // id
    02571
    02572   if (feature == GSF_BRIDGE && numprops == 1) {
    02573     uint8 prop = grf_load_byte(&buf);
    02574     /* Bridge property 0x0D is redefinition of sprite layout tables, which
    02575      * is considered safe. */
    02576     if (prop == 0x0D) return;
    02577   } else if (feature == GSF_GLOBALVAR && numprops == 1) {
    02578     uint8 prop = grf_load_byte(&buf);
    02579     /* Engine ID Mappings are safe, if the source is static */
    02580     if (prop == 0x11) {
    02581       bool is_safe = true;
    02582       for (uint i = 0; i < numinfo; i++) {
    02583         uint32 s = grf_load_dword(&buf);
    02584         grf_load_dword(&buf); // dest
    02585         const GRFConfig *grfconfig = GetGRFConfig(s);
    02586         if (grfconfig != NULL && !HasBit(grfconfig->flags, GCF_STATIC)) {
    02587           is_safe = false;
    02588           break;
    02589         }
    02590       }
    02591       if (is_safe) return;
    02592     }
    02593   }
    02594
    02595   SetBit(_cur_grfconfig->flags, GCF_UNSAFE);
    02596
    02597   /* Skip remainder of GRF */
    02598   _skip_sprites = -1;
    02599 }
    02600
    02601 /* Action 0x00 (GLS_RESERVE) */
    02602 static void ReserveChangeInfo(byte *buf, size_t len)
    02603 {
    02604   byte *bufend = buf + len;
    02605
    02606   if (!check_length(len, 6, "ReserveChangeInfo")) return;
    02607   buf++;
    02608   uint8 feature  = grf_load_byte(&buf);
    02609
    02610   if (feature != GSF_CARGOS && feature != GSF_GLOBALVAR) return;
    02611
    02612   uint8 numprops = grf_load_byte(&buf);
    02613   uint8 numinfo  = grf_load_byte(&buf);
    02614   uint8 index    = grf_load_extended(&buf);
    02615
    02616   while (numprops-- && buf < bufend) {
    02617     uint8 prop = grf_load_byte(&buf);
    02618     ChangeInfoResult cir = CIR_SUCCESS;
    02619
    02620     switch (feature) {
    02621       default: NOT_REACHED();
    02622       case GSF_CARGOS:
    02623         cir = CargoChangeInfo(index, numinfo, prop, &buf, bufend - buf);
    02624         break;
    02625
    02626       case GSF_GLOBALVAR:
    02627         cir = GlobalVarReserveInfo(index, numinfo, prop, &buf, bufend - buf);
    02628         break;
    02629     }
    02630
    02631     if (HandleChangeInfoResult("ReserveChangeInfo", cir, feature, prop)) return;
    02632   }
    02633 }
    02634
    02640 static const SpriteGroup *NewCallBackResultSpriteGroup(uint16 value)
    02641 {
    02642   SpriteGroup *group = AllocateSpriteGroup();
    02643
    02644   group->type = SGT_CALLBACK;
    02645
    02646   /* Old style callback results have the highest byte 0xFF so signify it is a callback result
    02647    * New style ones only have the highest bit set (allows 15-bit results, instead of just 8) */
    02648   if ((value >> 8) == 0xFF) {
    02649     value &= ~0xFF00;
    02650   } else {
    02651     value &= ~0x8000;
    02652   }
    02653
    02654   group->g.callback.result = value;
    02655
    02656   return group;
    02657 }
    02658
    02665 static const SpriteGroup *NewResultSpriteGroup(SpriteID sprite, byte num_sprites)
    02666 {
    02667   SpriteGroup *group = AllocateSpriteGroup();
    02668   group->type = SGT_RESULT;
    02669   group->g.result.sprite = sprite;
    02670   group->g.result.num_sprites = num_sprites;
    02671   return group;
    02672 }
    02673
    02674 /* Action 0x01 */
    02675 static void NewSpriteSet(byte *buf, size_t len)
    02676 {
    02677   /* <01> <feature> <num-sets> <num-ent>
    02678    *
    02679    * B feature       feature to define sprites for
    02680    *                 0, 1, 2, 3: veh-type, 4: train stations
    02681    * B num-sets      number of sprite sets
    02682    * E num-ent       how many entries per sprite set
    02683    *                 For vehicles, this is the number of different
    02684    *                         vehicle directions in each sprite set
    02685    *                         Set num-dirs=8, unless your sprites are symmetric.
    02686    *                         In that case, use num-dirs=4.
    02687    */
    02688
    02689   if (!check_length(len, 4, "NewSpriteSet")) return;
    02690   buf++;
    02691   uint8 feature   = grf_load_byte(&buf);
    02692   uint8 num_sets  = grf_load_byte(&buf);
    02693   uint16 num_ents = grf_load_extended(&buf);
    02694
    02695   _cur_grffile->spriteset_start = _cur_spriteid;
    02696   _cur_grffile->spriteset_feature = feature;
    02697   _cur_grffile->spriteset_numsets = num_sets;
    02698   _cur_grffile->spriteset_numents = num_ents;
    02699
    02700   grfmsg(7, "New sprite set at %d of type %d, consisting of %d sets with %d views each (total %d)",
    02701     _cur_spriteid, feature, num_sets, num_ents, num_sets * num_ents
    02702   );
    02703
    02704   for (int i = 0; i < num_sets * num_ents; i++) {
    02705     _nfo_line++;
    02706     LoadNextSprite(_cur_spriteid++, _file_index, _nfo_line);
    02707   }
    02708 }
    02709
    02710 /* Action 0x01 (SKIP) */
    02711 static void SkipAct1(byte *buf, size_t len)
    02712 {
    02713   if (!check_length(len, 4, "SkipAct1")) return;
    02714   buf++;
    02715   grf_load_byte(&buf);
    02716   uint8 num_sets  = grf_load_byte(&buf);
    02717   uint16 num_ents = grf_load_extended(&buf);
    02718
    02719   _skip_sprites = num_sets * num_ents;
    02720
    02721   grfmsg(3, "SkipAct1: Skipping %d sprites", _skip_sprites);
    02722 }
    02723
    02724 /* Helper function to either create a callback or link to a previously
    02725  * defined spritegroup. */
    02726 static const SpriteGroup *GetGroupFromGroupID(byte setid, byte type, uint16 groupid)
    02727 {
    02728   if (HasBit(groupid, 15)) return NewCallBackResultSpriteGroup(groupid);
    02729
    02730   if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
    02731     grfmsg(1, "GetGroupFromGroupID(0x%02X:0x%02X): Groupid 0x%04X does not exist, leaving empty", setid, type, groupid);
    02732     return NULL;
    02733   }
    02734
    02735   return _cur_grffile->spritegroups[groupid];
    02736 }
    02737
    02738 /* Helper function to either create a callback or a result sprite group. */
    02739 static const SpriteGroup *CreateGroupFromGroupID(byte feature, byte setid, byte type, uint16 spriteid, uint16 num_sprites)
    02740 {
    02741   if (HasBit(spriteid, 15)) return NewCallBackResultSpriteGroup(spriteid);
    02742
    02743   if (spriteid >= _cur_grffile->spriteset_numsets) {
    02744     grfmsg(1, "CreateGroupFromGroupID(0x%02X:0x%02X): Sprite set %u invalid, max %u", setid, type, spriteid, _cur_grffile->spriteset_numsets);
    02745     return NULL;
    02746   }
    02747
    02748   /* Check if the sprite is within range. This can fail if the Action 0x01
    02749    * is skipped, as TTDPatch mandates that Action 0x02s must be processed.
    02750    * We don't have that rule, but must live by the Patch... */
    02751   if (_cur_grffile->spriteset_start + spriteid * num_sprites + num_sprites > _cur_spriteid) {
    02752     grfmsg(1, "CreateGroupFromGroupID(0x%02X:0x%02X): Real Sprite IDs 0x%04X - 0x%04X do not (all) exist (max 0x%04X), leaving empty",
    02753         setid, type,
    02754         _cur_grffile->spriteset_start + spriteid * num_sprites,
    02755         _cur_grffile->spriteset_start + spriteid * num_sprites + num_sprites - 1, _cur_spriteid - 1);
    02756     return NULL;
    02757   }
    02758
    02759   if (feature != _cur_grffile->spriteset_feature) {
    02760     grfmsg(1, "CreateGroupFromGroupID(0x%02X:0x%02X): Sprite set feature 0x%02X does not match action feature 0x%02X, skipping",
    02761         setid, type,
    02762         _cur_grffile->spriteset_feature, feature);
    02763     return NULL;
    02764   }
    02765
    02766   return NewResultSpriteGroup(_cur_grffile->spriteset_start + spriteid * num_sprites, num_sprites);
    02767 }
    02768
    02769 /* Action 0x02 */
    02770 static void NewSpriteGroup(byte *buf, size_t len)
    02771 {
    02772   /* <02> <feature> <set-id> <type/num-entries> <feature-specific-data...>
    02773    *
    02774    * B feature       see action 1
    02775    * B set-id        ID of this particular definition
    02776    * B type/num-entries
    02777    *                 if 80 or greater, this is a randomized or variational
    02778    *                 list definition, see below
    02779    *                 otherwise it specifies a number of entries, the exact
    02780    *                 meaning depends on the feature
    02781    * V feature-specific-data (huge mess, don't even look it up --pasky) */
    02782   SpriteGroup *group = NULL;
    02783   byte *bufend = buf + len;
    02784
    02785   if (!check_length(len, 5, "NewSpriteGroup")) return;
    02786   buf++;
    02787
    02788   uint8 feature = grf_load_byte(&buf);
    02789   uint8 setid   = grf_load_byte(&buf);
    02790   uint8 type    = grf_load_byte(&buf);
    02791
    02792   if (setid >= _cur_grffile->spritegroups_count) {
    02793     /* Allocate memory for new sprite group references. */
    02794     _cur_grffile->spritegroups = ReallocT(_cur_grffile->spritegroups, setid + 1);
    02795     /* Initialise new space to NULL */
    02796     for (; _cur_grffile->spritegroups_count < (setid + 1); _cur_grffile->spritegroups_count++)
    02797       _cur_grffile->spritegroups[_cur_grffile->spritegroups_count] = NULL;
    02798   }
    02799
    02800   switch (type) {
    02801     /* Deterministic Sprite Group */
    02802     case 0x81: // Self scope, byte
    02803     case 0x82: // Parent scope, byte
    02804     case 0x85: // Self scope, word
    02805     case 0x86: // Parent scope, word
    02806     case 0x89: // Self scope, dword
    02807     case 0x8A: // Parent scope, dword
    02808     {
    02809       byte varadjust;
    02810       byte varsize;
    02811
    02812       /* Check we can load the var size parameter */
    02813       if (!check_length(bufend - buf, 1, "NewSpriteGroup (Deterministic) (1)")) return;
    02814
    02815       group = AllocateSpriteGroup();
    02816       group->type = SGT_DETERMINISTIC;
    02817       group->g.determ.var_scope = HasBit(type, 1) ? VSG_SCOPE_PARENT : VSG_SCOPE_SELF;
    02818
    02819       switch (GB(type, 2, 2)) {
    02820         default: NOT_REACHED();
    02821         case 0: group->g.determ.size = DSG_SIZE_BYTE;  varsize = 1; break;
    02822         case 1: group->g.determ.size = DSG_SIZE_WORD;  varsize = 2; break;
    02823         case 2: group->g.determ.size = DSG_SIZE_DWORD; varsize = 4; break;
    02824       }
    02825
    02826       if (!check_length(bufend - buf, 5 + varsize, "NewSpriteGroup (Deterministic) (2)")) return;
    02827
    02828       /* Loop through the var adjusts. Unfortunately we don't know how many we have
    02829        * from the outset, so we shall have to keep reallocing. */
    02830       do {
    02831         DeterministicSpriteGroupAdjust *adjust;
    02832
    02833         if (group->g.determ.num_adjusts > 0) {
    02834           if (!check_length(bufend - buf, 2 + varsize + 3, "NewSpriteGroup (Deterministic) (3)")) return;
    02835         }
    02836
    02837         group->g.determ.num_adjusts++;
    02838         group->g.determ.adjusts = ReallocT(group->g.determ.adjusts, group->g.determ.num_adjusts);
    02839
    02840         adjust = &group->g.determ.adjusts[group->g.determ.num_adjusts - 1];
    02841
    02842         /* The first var adjust doesn't have an operation specified, so we set it to add. */
    02843         adjust->operation = group->g.determ.num_adjusts == 1 ? DSGA_OP_ADD : (DeterministicSpriteGroupAdjustOperation)grf_load_byte(&buf);
    02844         adjust->variable  = grf_load_byte(&buf);
    02845         if (adjust->variable == 0x7E) {
    02846           /* Link subroutine group */
    02847           adjust->subroutine = GetGroupFromGroupID(setid, type, grf_load_byte(&buf));
    02848         } else {
    02849           adjust->parameter = IsInsideMM(adjust->variable, 0x60, 0x80) ? grf_load_byte(&buf) : 0;
    02850         }
    02851
    02852         varadjust = grf_load_byte(&buf);
    02853         adjust->shift_num = GB(varadjust, 0, 5);
    02854         adjust->type      = (DeterministicSpriteGroupAdjustType)GB(varadjust, 6, 2);
    02855         adjust->and_mask  = grf_load_var(varsize, &buf);
    02856
    02857         if (adjust->type != DSGA_TYPE_NONE) {
    02858           adjust->add_val    = grf_load_var(varsize, &buf);
    02859           adjust->divmod_val = grf_load_var(varsize, &buf);
    02860         } else {
    02861           adjust->add_val    = 0;
    02862           adjust->divmod_val = 0;
    02863         }
    02864
    02865         /* Continue reading var adjusts while bit 5 is set. */
    02866       } while (HasBit(varadjust, 5));
    02867
    02868       group->g.determ.num_ranges = grf_load_byte(&buf);
    02869       if (group->g.determ.num_ranges > 0) group->g.determ.ranges = CallocT<DeterministicSpriteGroupRange>(group->g.determ.num_ranges);
    02870
    02871       if (!check_length(bufend - buf, 2 + (2 + 2 * varsize) * group->g.determ.num_ranges, "NewSpriteGroup (Deterministic)")) return;
    02872
    02873       for (uint i = 0; i < group->g.determ.num_ranges; i++) {
    02874         group->g.determ.ranges[i].group = GetGroupFromGroupID(setid, type, grf_load_word(&buf));
    02875         group->g.determ.ranges[i].low   = grf_load_var(varsize, &buf);
    02876         group->g.determ.ranges[i].high  = grf_load_var(varsize, &buf);
    02877       }
    02878
    02879       group->g.determ.default_group = GetGroupFromGroupID(setid, type, grf_load_word(&buf));
    02880       break;
    02881     }
    02882
    02883     /* Randomized Sprite Group */
    02884     case 0x80: // Self scope
    02885     case 0x83: // Parent scope
    02886     case 0x84: // Relative scope
    02887     {
    02888       if (!check_length(bufend - buf, HasBit(type, 2) ? 8 : 7, "NewSpriteGroup (Randomized) (1)")) return;
    02889
    02890       group = AllocateSpriteGroup();
    02891       group->type = SGT_RANDOMIZED;
    02892       group->g.random.var_scope = HasBit(type, 1) ? VSG_SCOPE_PARENT : VSG_SCOPE_SELF;
    02893
    02894       if (HasBit(type, 2)) {
    02895         if (feature <= GSF_AIRCRAFT) group->g.random.var_scope = VSG_SCOPE_RELATIVE;
    02896         group->g.random.count = grf_load_byte(&buf);
    02897       }
    02898
    02899       uint8 triggers = grf_load_byte(&buf);
    02900       group->g.random.triggers       = GB(triggers, 0, 7);
    02901       group->g.random.cmp_mode       = HasBit(triggers, 7) ? RSG_CMP_ALL : RSG_CMP_ANY;
    02902       group->g.random.lowest_randbit = grf_load_byte(&buf);
    02903       group->g.random.num_groups     = grf_load_byte(&buf);
    02904       group->g.random.groups = CallocT<const SpriteGroup*>(group->g.random.num_groups);
    02905
    02906       if (!check_length(bufend - buf, 2 * group->g.random.num_groups, "NewSpriteGroup (Randomized) (2)")) return;
    02907
    02908       for (uint i = 0; i < group->g.random.num_groups; i++) {
    02909         group->g.random.groups[i] = GetGroupFromGroupID(setid, type, grf_load_word(&buf));
    02910       }
    02911
    02912       break;
    02913     }
    02914
    02915     /* Neither a variable or randomized sprite group... must be a real group */
    02916     default:
    02917     {
    02918
    02919
    02920       switch (feature) {
    02921         case GSF_TRAIN:
    02922         case GSF_ROAD:
    02923         case GSF_SHIP:
    02924         case GSF_AIRCRAFT:
    02925         case GSF_STATION:
    02926         case GSF_CANAL:
    02927         case GSF_CARGOS:
    02928         {
    02929           byte sprites     = _cur_grffile->spriteset_numents;
    02930           byte num_loaded  = type;
    02931           byte num_loading = grf_load_byte(&buf);
    02932
    02933           if (_cur_grffile->spriteset_start == 0) {
    02934             grfmsg(0, "NewSpriteGroup: No sprite set to work on! Skipping");
    02935             return;
    02936           }
    02937
    02938           if (!check_length(bufend - buf, 2 * num_loaded + 2 * num_loading, "NewSpriteGroup (Real) (1)")) return;
    02939
    02940           group = AllocateSpriteGroup();
    02941           group->type = SGT_REAL;
    02942
    02943           group->g.real.num_loaded  = num_loaded;
    02944           group->g.real.num_loading = num_loading;
    02945           if (num_loaded  > 0) group->g.real.loaded = CallocT<const SpriteGroup*>(num_loaded);
    02946           if (num_loading > 0) group->g.real.loading = CallocT<const SpriteGroup*>(num_loading);
    02947
    02948           grfmsg(6, "NewSpriteGroup: New SpriteGroup 0x%02X, %u views, %u loaded, %u loading",
    02949               setid, sprites, num_loaded, num_loading);
    02950
    02951           for (uint i = 0; i < num_loaded; i++) {
    02952             uint16 spriteid = grf_load_word(&buf);
    02953             group->g.real.loaded[i] = CreateGroupFromGroupID(feature, setid, type, spriteid, sprites);
    02954             grfmsg(8, "NewSpriteGroup: + rg->loaded[%i]  = subset %u", i, spriteid);
    02955           }
    02956
    02957           for (uint i = 0; i < num_loading; i++) {
    02958             uint16 spriteid = grf_load_word(&buf);
    02959             group->g.real.loading[i] = CreateGroupFromGroupID(feature, setid, type, spriteid, sprites);
    02960             grfmsg(8, "NewSpriteGroup: + rg->loading[%i] = subset %u", i, spriteid);
    02961           }
    02962
    02963           break;
    02964         }
    02965
    02966         case GSF_TOWNHOUSE:
    02967         case GSF_INDUSTRYTILES: {
    02968           byte sprites     = _cur_grffile->spriteset_numents;
    02969           byte num_sprites = max((uint8)1, type);
    02970           uint i;
    02971
    02972           group = AllocateSpriteGroup();
    02973           group->type = SGT_TILELAYOUT;
    02974           group->g.layout.num_sprites = sprites;
    02975           group->g.layout.dts = CallocT<DrawTileSprites>(1);
    02976
    02977           /* Groundsprite */
    02978           group->g.layout.dts->ground.sprite = grf_load_word(&buf);
    02979           group->g.layout.dts->ground.pal    = grf_load_word(&buf);
    02980
    02981           /* Remap transparent/colour modifier bits */
    02982           MapSpriteMappingRecolour(&group->g.layout.dts->ground);
    02983
    02984           if (HasBit(group->g.layout.dts->ground.pal, 15)) {
    02985             /* Bit 31 set means this is a custom sprite, so rewrite it to the
    02986              * last spriteset defined. */
    02987             SpriteID sprite = _cur_grffile->spriteset_start + GB(group->g.layout.dts->ground.sprite, 0, 14) * sprites;
    02988             SB(group->g.layout.dts->ground.sprite, 0, SPRITE_WIDTH, sprite);
    02989             ClrBit(group->g.layout.dts->ground.pal, 15);
    02990           }
    02991
    02992           group->g.layout.dts->seq = CallocT<DrawTileSeqStruct>(num_sprites + 1);
    02993
    02994           for (i = 0; i < num_sprites; i++) {
    02995             DrawTileSeqStruct *seq = (DrawTileSeqStruct*)&group->g.layout.dts->seq[i];
    02996
    02997             seq->image.sprite = grf_load_word(&buf);
    02998             seq->image.pal   = grf_load_word(&buf);
    02999             seq->delta_x = grf_load_byte(&buf);
    03000             seq->delta_y = grf_load_byte(&buf);
    03001
    03002             MapSpriteMappingRecolour(&seq->image);
    03003
    03004             if (HasBit(seq->image.pal, 15)) {
    03005               /* Bit 31 set means this is a custom sprite, so rewrite it to the
    03006                * last spriteset defined. */
    03007               SpriteID sprite = _cur_grffile->spriteset_start + GB(seq->image.sprite, 0, 14) * sprites;
    03008               SB(seq->image.sprite, 0, SPRITE_WIDTH, sprite);
    03009               ClrBit(seq->image.pal, 15);
    03010             }
    03011
    03012             if (type > 0) {
    03013               seq->delta_z = grf_load_byte(&buf);
    03014               if ((byte)seq->delta_z == 0x80) continue;
    03015             }
    03016
    03017             seq->size_x = grf_load_byte(&buf);
    03018             seq->size_y = grf_load_byte(&buf);
    03019             seq->size_z = grf_load_byte(&buf);
    03020           }
    03021
    03022           /* Set the terminator value. */
    03023           ((DrawTileSeqStruct*)group->g.layout.dts->seq)[i].delta_x = (byte)0x80;
    03024
    03025           break;
    03026         }
    03027
    03028         case GSF_INDUSTRIES: {
    03029           if (type > 1) {
    03030             grfmsg(1, "NewSpriteGroup: Unsupported industry production version %d, skipping", type);
    03031             break;
    03032           }
    03033
    03034           group = AllocateSpriteGroup();
    03035           group->type = SGT_INDUSTRY_PRODUCTION;
    03036           group->g.indprod.version = type;
    03037           if (type == 0) {
    03038             for (uint i = 0; i < 3; i++) {
    03039               group->g.indprod.substract_input[i] = (int16)grf_load_word(&buf); // signed
    03040             }
    03041             for (uint i = 0; i < 2; i++) {
    03042               group->g.indprod.add_output[i] = grf_load_word(&buf); // unsigned
    03043             }
    03044             group->g.indprod.again = grf_load_byte(&buf);
    03045           } else {
    03046             for (uint i = 0; i < 3; i++) {
    03047               group->g.indprod.substract_input[i] = grf_load_byte(&buf);
    03048             }
    03049             for (uint i = 0; i < 2; i++) {
    03050               group->g.indprod.add_output[i] = grf_load_byte(&buf);
    03051             }
    03052             group->g.indprod.again = grf_load_byte(&buf);
    03053           }
    03054           break;
    03055         }
    03056
    03057         /* Loading of Tile Layout and Production Callback groups would happen here */
    03058         default: grfmsg(1, "NewSpriteGroup: Unsupported feature %d, skipping", feature);
    03059       }
    03060     }
    03061   }
    03062
    03063   _cur_grffile->spritegroups[setid] = group;
    03064 }
    03065
    03066 static CargoID TranslateCargo(uint8 feature, uint8 ctype)
    03067 {
    03068   /* Special cargo types for purchase list and stations */
    03069   if (feature == GSF_STATION && ctype == 0xFE) return CT_DEFAULT_NA;
    03070   if (ctype == 0xFF) return CT_PURCHASE;
    03071
    03072   if (_cur_grffile->cargo_max == 0) {
    03073     /* No cargo table, so use bitnum values */
    03074     if (ctype >= 32) {
    03075       grfmsg(1, "TranslateCargo: Cargo bitnum %d out of range (max 31), skipping.", ctype);
    03076       return CT_INVALID;
    03077     }
    03078
    03079     for (CargoID c = 0; c < NUM_CARGO; c++) {
    03080       const CargoSpec *cs = GetCargo(c);
    03081       if (!cs->IsValid()) continue;
    03082
    03083       if (cs->bitnum == ctype) {
    03084         grfmsg(6, "TranslateCargo: Cargo bitnum %d mapped to cargo type %d.", ctype, c);
    03085         return c;
    03086       }
    03087     }
    03088
    03089     grfmsg(5, "TranslateCargo: Cargo bitnum %d not available in this climate, skipping.", ctype);
    03090     return CT_INVALID;
    03091   }
    03092
    03093   /* Check if the cargo type is out of bounds of the cargo translation table */
    03094   if (ctype >= _cur_grffile->cargo_max) {
    03095     grfmsg(1, "TranslateCargo: Cargo type %d out of range (max %d), skipping.", ctype, _cur_grffile->cargo_max - 1);
    03096     return CT_INVALID;
    03097   }
    03098
    03099   /* Look up the cargo label from the translation table */
    03100   CargoLabel cl = _cur_grffile->cargo_list[ctype];
    03101   if (cl == 0) {
    03102     grfmsg(5, "TranslateCargo: Cargo type %d not available in this climate, skipping.", ctype);
    03103     return CT_INVALID;
    03104   }
    03105
    03106   ctype = GetCargoIDByLabel(cl);
    03107   if (ctype == CT_INVALID) {
    03108     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));
    03109     return CT_INVALID;
    03110   }
    03111
    03112   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);
    03113   return ctype;
    03114 }
    03115
    03116
    03117 static bool IsValidGroupID(uint16 groupid, const char *function)
    03118 {
    03119   if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
    03120     grfmsg(1, "%s: Spriteset 0x%04X out of range (maximum 0x%02X) or empty, skipping.", function, groupid, _cur_grffile->spritegroups_count - 1);
    03121     return false;
    03122   }
    03123
    03124   return true;
    03125 }
    03126
    03127 static void VehicleMapSpriteGroup(byte *buf, byte feature, uint8 idcount)
    03128 {
    03129   static EngineID *last_engines;
    03130   static uint last_engines_count;
    03131   bool wagover = false;
    03132
    03133   /* Test for 'wagon override' flag */
    03134   if (HasBit(idcount, 7)) {
    03135     wagover = true;
    03136     /* Strip off the flag */
    03137     idcount = GB(idcount, 0, 7);
    03138
    03139     if (last_engines_count == 0) {
    03140       grfmsg(0, "VehicleMapSpriteGroup: WagonOverride: No engine to do override with");
    03141       return;
    03142     }
    03143
    03144     grfmsg(6, "VehicleMapSpriteGroup: WagonOverride: %u engines, %u wagons",
    03145         last_engines_count, idcount);
    03146   } else {
    03147     if (last_engines_count != idcount) {
    03148       last_engines = ReallocT(last_engines, idcount);
    03149       last_engines_count = idcount;
    03150     }
    03151   }
    03152
    03153   EngineID *engines = AllocaM(EngineID, idcount);
    03154   for (uint i = 0; i < idcount; i++) {
    03155     engines[i] = GetNewEngine(_cur_grffile, (VehicleType)feature, grf_load_extended(&buf))->index;
    03156     if (!wagover) last_engines[i] = engines[i];
    03157   }
    03158
    03159   uint8 cidcount = grf_load_byte(&buf);
    03160   for (uint c = 0; c < cidcount; c++) {
    03161     uint8 ctype = grf_load_byte(&buf);
    03162     uint16 groupid = grf_load_word(&buf);
    03163     if (!IsValidGroupID(groupid, "VehicleMapSpriteGroup")) continue;
    03164
    03165     grfmsg(8, "VehicleMapSpriteGroup: * [%d] Cargo type 0x%X, group id 0x%02X", c, ctype, groupid);
    03166
    03167     ctype = TranslateCargo(feature, ctype);
    03168     if (ctype == CT_INVALID) continue;
    03169
    03170     for (uint i = 0; i < idcount; i++) {
    03171       EngineID engine = engines[i];
    03172
    03173       grfmsg(7, "VehicleMapSpriteGroup: [%d] Engine %d...", i, engine);
    03174
    03175       if (wagover) {
    03176         SetWagonOverrideSprites(engine, ctype, _cur_grffile->spritegroups[groupid], last_engines, last_engines_count);
    03177       } else {
    03178         SetCustomEngineSprites(engine, ctype, _cur_grffile->spritegroups[groupid]);
    03179       }
    03180     }
    03181   }
    03182
    03183   uint16 groupid = grf_load_word(&buf);
    03184   if (!IsValidGroupID(groupid, "VehicleMapSpriteGroup")) return;
    03185
    03186   grfmsg(8, "-- Default group id 0x%04X", groupid);
    03187
    03188   for (uint i = 0; i < idcount; i++) {
    03189     EngineID engine = engines[i];
    03190
    03191     if (wagover) {
    03192       SetWagonOverrideSprites(engine, CT_DEFAULT, _cur_grffile->spritegroups[groupid], last_engines, last_engines_count);
    03193     } else {
    03194       SetCustomEngineSprites(engine, CT_DEFAULT, _cur_grffile->spritegroups[groupid]);
    03195       SetEngineGRF(engine, _cur_grffile);
    03196     }
    03197   }
    03198 }
    03199
    03200
    03201 static void CanalMapSpriteGroup(byte *buf, uint8 idcount)
    03202 {
    03203   CanalFeature *cfs = AllocaM(CanalFeature, idcount);
    03204   for (uint i = 0; i < idcount; i++) {
    03205     cfs[i] = (CanalFeature)grf_load_byte(&buf);
    03206   }
    03207
    03208   uint8 cidcount = grf_load_byte(&buf);
    03209   buf += cidcount * 3;
    03210
    03211   uint16 groupid = grf_load_word(&buf);
    03212   if (!IsValidGroupID(groupid, "CanalMapSpriteGroup")) return;
    03213
    03214   for (uint i = 0; i < idcount; i++) {
    03215     CanalFeature cf = cfs[i];
    03216
    03217     if (cf >= CF_END) {
    03218       grfmsg(1, "CanalMapSpriteGroup: Canal subset %d out of range, skipping", cf);
    03219       continue;
    03220     }
    03221
    03222     _water_feature[cf].grffile = _cur_grffile;
    03223     _water_feature[cf].group = _cur_grffile->spritegroups[groupid];
    03224   }
    03225 }
    03226
    03227
    03228 static void StationMapSpriteGroup(byte *buf, uint8 idcount)
    03229 {
    03230   uint8 *stations = AllocaM(uint8, idcount);
    03231   for (uint i = 0; i < idcount; i++) {
    03232     stations[i] = grf_load_byte(&buf);
    03233   }
    03234
    03235   uint8 cidcount = grf_load_byte(&buf);
    03236   for (uint c = 0; c < cidcount; c++) {
    03237     uint8 ctype = grf_load_byte(&buf);
    03238     uint16 groupid = grf_load_word(&buf);
    03239     if (!IsValidGroupID(groupid, "StationMapSpriteGroup")) continue;
    03240
    03241     ctype = TranslateCargo(GSF_STATION, ctype);
    03242     if (ctype == CT_INVALID) continue;
    03243
    03244     for (uint i = 0; i < idcount; i++) {
    03245       StationSpec *statspec = _cur_grffile->stations[stations[i]];
    03246
    03247       if (statspec == NULL) {
    03248         grfmsg(1, "StationMapSpriteGroup: Station with ID 0x%02X does not exist, skipping", stations[i]);
    03249         continue;
    03250       }
    03251
    03252       statspec->spritegroup[ctype] = _cur_grffile->spritegroups[groupid];
    03253     }
    03254   }
    03255
    03256   uint16 groupid = grf_load_word(&buf);
    03257   if (!IsValidGroupID(groupid, "StationMapSpriteGroup")) return;
    03258
    03259   for (uint i = 0; i < idcount; i++) {
    03260     StationSpec *statspec = _cur_grffile->stations[stations[i]];
    03261
    03262     if (statspec == NULL) {
    03263       grfmsg(1, "StationMapSpriteGroup: Station with ID 0x%02X does not exist, skipping", stations[i]);
    03264       continue;
    03265     }
    03266
    03267     statspec->spritegroup[CT_DEFAULT] = _cur_grffile->spritegroups[groupid];
    03268     statspec->grffile = _cur_grffile;
    03269     statspec->localidx = stations[i];
    03270     SetCustomStationSpec(statspec);
    03271   }
    03272 }
    03273
    03274
    03275 static void TownHouseMapSpriteGroup(byte *buf, uint8 idcount)
    03276 {
    03277   uint8 *houses = AllocaM(uint8, idcount);
    03278   for (uint i = 0; i < idcount; i++) {
    03279     houses[i] = grf_load_byte(&buf);
    03280   }
    03281
    03282   /* Skip the cargo type section, we only care about the default group */
    03283   uint8 cidcount = grf_load_byte(&buf);
    03284   buf += cidcount * 3;
    03285
    03286   uint16 groupid = grf_load_word(&buf);
    03287   if (!IsValidGroupID(groupid, "TownHouseMapSpriteGroup")) return;
    03288
    03289   for (uint i = 0; i < idcount; i++) {
    03290     HouseSpec *hs = _cur_grffile->housespec[houses[i]];
    03291
    03292     if (hs == NULL) {
    03293       grfmsg(1, "TownHouseMapSpriteGroup: House %d undefined, skipping.", houses[i]);
    03294       continue;
    03295     }
    03296
    03297     hs->spritegroup = _cur_grffile->spritegroups[groupid];
    03298   }
    03299 }
    03300
    03301 static void IndustryMapSpriteGroup(byte *buf, uint8 idcount)
    03302 {
    03303   uint8 *industries = AllocaM(uint8, idcount);
    03304   for (uint i = 0; i < idcount; i++) {
    03305     industries[i] = grf_load_byte(&buf);
    03306   }
    03307
    03308   /* Skip the cargo type section, we only care about the default group */
    03309   uint8 cidcount = grf_load_byte(&buf);
    03310   buf += cidcount * 3;
    03311
    03312   uint16 groupid = grf_load_word(&buf);
    03313   if (!IsValidGroupID(groupid, "IndustryMapSpriteGroup")) return;
    03314
    03315   for (uint i = 0; i < idcount; i++) {
    03316     IndustrySpec *indsp = _cur_grffile->industryspec[industries[i]];
    03317
    03318     if (indsp == NULL) {
    03319       grfmsg(1, "IndustryMapSpriteGroup: Industry %d undefined, skipping", industries[i]);
    03320       continue;
    03321     }
    03322
    03323     indsp->grf_prop.spritegroup = _cur_grffile->spritegroups[groupid];
    03324   }
    03325 }
    03326
    03327 static void IndustrytileMapSpriteGroup(byte *buf, uint8 idcount)
    03328 {
    03329   uint8 *indtiles = AllocaM(uint8, idcount);
    03330   for (uint i = 0; i < idcount; i++) {
    03331     indtiles[i] = grf_load_byte(&buf);
    03332   }
    03333
    03334   /* Skip the cargo type section, we only care about the default group */
    03335   uint8 cidcount = grf_load_byte(&buf);
    03336   buf += cidcount * 3;
    03337
    03338   uint16 groupid = grf_load_word(&buf);
    03339   if (!IsValidGroupID(groupid, "IndustrytileMapSpriteGroup")) return;
    03340
    03341   for (uint i = 0; i < idcount; i++) {
    03342     IndustryTileSpec *indtsp = _cur_grffile->indtspec[indtiles[i]];
    03343
    03344     if (indtsp == NULL) {
    03345       grfmsg(1, "IndustrytileMapSpriteGroup: Industry tile %d undefined, skipping", indtiles[i]);
    03346       continue;
    03347     }
    03348
    03349     indtsp->grf_prop.spritegroup = _cur_grffile->spritegroups[groupid];
    03350   }
    03351 }
    03352
    03353 static void CargoMapSpriteGroup(byte *buf, uint8 idcount)
    03354 {
    03355   CargoID *cargos = AllocaM(CargoID, idcount);
    03356   for (uint i = 0; i < idcount; i++) {
    03357     cargos[i] = grf_load_byte(&buf);
    03358   }
    03359
    03360   /* Skip the cargo type section, we only care about the default group */
    03361   uint8 cidcount = grf_load_byte(&buf);
    03362   buf += cidcount * 3;
    03363
    03364   uint16 groupid = grf_load_word(&buf);
    03365   if (!IsValidGroupID(groupid, "CargoMapSpriteGroup")) return;
    03366
    03367   for (uint i = 0; i < idcount; i++) {
    03368     CargoID cid = cargos[i];
    03369
    03370     if (cid >= NUM_CARGO) {
    03371       grfmsg(1, "CargoMapSpriteGroup: Cargo ID %d out of range, skipping", cid);
    03372       continue;
    03373     }
    03374
    03375     CargoSpec *cs = &_cargo[cid];
    03376     cs->grffile = _cur_grffile;
    03377     cs->group = _cur_grffile->spritegroups[groupid];
    03378   }
    03379 }
    03380
    03381
    03382 /* Action 0x03 */
    03383 static void FeatureMapSpriteGroup(byte *buf, size_t len)
    03384 {
    03385   /* <03> <feature> <n-id> <ids>... <num-cid> [<cargo-type> <cid>]... <def-cid>
    03386    * id-list    := [<id>] [id-list]
    03387    * cargo-list := <cargo-type> <cid> [cargo-list]
    03388    *
    03389    * B feature       see action 0
    03390    * B n-id          bits 0-6: how many IDs this definition applies to
    03391    *                 bit 7: if set, this is a wagon override definition (see below)
    03392    * B ids           the IDs for which this definition applies
    03393    * B num-cid       number of cargo IDs (sprite group IDs) in this definition
    03394    *                 can be zero, in that case the def-cid is used always
    03395    * B cargo-type    type of this cargo type (e.g. mail=2, wood=7, see below)
    03396    * W cid           cargo ID (sprite group ID) for this type of cargo
    03397    * W def-cid       default cargo ID (sprite group ID) */
    03398
    03399   if (_cur_grffile->spritegroups == 0) {
    03400     grfmsg(1, "FeatureMapSpriteGroup: No sprite groups to work on! Skipping");
    03401     return;
    03402   }
    03403
    03404   if (!check_length(len, 6, "FeatureMapSpriteGroup")) return;
    03405
    03406   buf++;
    03407   uint8 feature = grf_load_byte(&buf);
    03408   uint8 idcount = grf_load_byte(&buf);
    03409
    03410   /* If idcount is zero, this is a feature callback */
    03411   if (idcount == 0) {
    03412     /* Skip number of cargo ids? */
    03413     grf_load_byte(&buf);
    03414     uint16 groupid = grf_load_word(&buf);
    03415
    03416     grfmsg(6, "FeatureMapSpriteGroup: Adding generic feature callback for feature %d", feature);
    03417
    03418     AddGenericCallback(feature, _cur_grffile, _cur_grffile->spritegroups[groupid]);
    03419     return;
    03420   }
    03421
    03422   grfmsg(6, "FeatureMapSpriteGroup: Feature %d, %d ids", feature, idcount);
    03423
    03424   switch (feature) {
    03425     case GSF_TRAIN:
    03426     case GSF_ROAD:
    03427     case GSF_SHIP:
    03428     case GSF_AIRCRAFT:
    03429       VehicleMapSpriteGroup(buf, feature, idcount);
    03430       return;
    03431
    03432     case GSF_CANAL:
    03433       CanalMapSpriteGroup(buf, idcount);
    03434       return;
    03435
    03436     case GSF_STATION:
    03437       StationMapSpriteGroup(buf, idcount);
    03438       return;
    03439
    03440     case GSF_TOWNHOUSE:
    03441       TownHouseMapSpriteGroup(buf, idcount);
    03442       return;
    03443
    03444     case GSF_INDUSTRIES:
    03445       IndustryMapSpriteGroup(buf, idcount);
    03446       return;
    03447
    03448     case GSF_INDUSTRYTILES:
    03449       IndustrytileMapSpriteGroup(buf, idcount);
    03450       return;
    03451
    03452     case GSF_CARGOS:
    03453       CargoMapSpriteGroup(buf, idcount);
    03454       return;
    03455
    03456     default:
    03457       grfmsg(1, "FeatureMapSpriteGroup: Unsupported feature %d, skipping", feature);
    03458       return;
    03459   }
    03460 }
    03461
    03462 /* Action 0x04 */
    03463 static void FeatureNewName(byte *buf, size_t len)
    03464 {
    03465   /* <04> <veh-type> <language-id> <num-veh> <offset> <data...>
    03466    *
    03467    * B veh-type      see action 0 (as 00..07, + 0A
    03468    *                 But IF veh-type = 48, then generic text
    03469    * B language-id   If bit 6 is set, This is the extended language scheme,
    03470                      with up to 64 language.
    03471                      Otherwise, it is a mapping where set bits have meaning
    03472                      0 = american, 1 = english, 2 = german, 3 = french, 4 = spanish
    03473                      Bit 7 set means this is a generic text, not a vehicle one (or else)
    03474    * B num-veh       number of vehicles which are getting a new name
    03475    * B/W offset      number of the first vehicle that gets a new name
    03476    *                 Byte : ID of vehicle to change
    03477    *                 Word : ID of string to change/add
    03478    * S data          new texts, each of them zero-terminated, after
    03479    *                 which the next name begins. */
    03480
    03481   bool new_scheme = _cur_grffile->grf_version >= 7;
    03482
    03483   if (!check_length(len, 6, "FeatureNewName")) return;
    03484   buf++;
    03485   uint8 feature  = grf_load_byte(&buf);
    03486   uint8 lang     = grf_load_byte(&buf);
    03487   uint8 num      = grf_load_byte(&buf);
    03488   bool generic   = HasBit(lang, 7);
    03489   uint16 id;
    03490   if (generic) {
    03491     id = grf_load_word(&buf);
    03492   } else if (feature <= GSF_AIRCRAFT) {
    03493     id = grf_load_extended(&buf);
    03494   } else {
    03495     id = grf_load_byte(&buf);
    03496   }
    03497
    03498   ClrBit(lang, 7);
    03499
    03500   uint16 endid = id + num;
    03501
    03502   grfmsg(6, "FeatureNewName: About to rename engines %d..%d (feature %d) in language 0x%02X",
    03503                  id, endid, feature, lang);
    03504
    03505   len -= generic ? 6 : 5;
    03506
    03507   for (; id < endid && len > 0; id++) {
    03508     const char *name   = grf_load_string(&buf, len);
    03509     size_t name_length = strlen(name) + 1;
    03510
    03511     len -= (int)name_length;
    03512
    03513     grfmsg(8, "FeatureNewName: 0x%04X <- %s", id, name);
    03514
    03515     switch (feature) {
    03516       case GSF_TRAIN:
    03517       case GSF_ROAD:
    03518       case GSF_SHIP:
    03519       case GSF_AIRCRAFT:
    03520         if (!generic) {
    03521           Engine *e = GetNewEngine(_cur_grffile, (VehicleType)feature, id, HasBit(_cur_grfconfig->flags, GCF_STATIC));
    03522           if (e == NULL) break;
    03523           StringID string = AddGRFString(_cur_grffile->grfid, e->index, lang, new_scheme, name, e->info.string_id);
    03524           e->info.string_id = string;
    03525         } else {
    03526           AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED);
    03527         }
    03528         break;
    03529
    03530       case GSF_INDUSTRIES: {
    03531         AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED);
    03532         break;
    03533       }
    03534
    03535       case GSF_TOWNHOUSE:
    03536       default:
    03537         switch (GB(id, 8, 8)) {
    03538           case 0xC4: // Station class name
    03539             if (_cur_grffile->stations == NULL || _cur_grffile->stations[GB(id, 0, 8)] == NULL) {
    03540               grfmsg(1, "FeatureNewName: Attempt to name undefined station 0x%X, ignoring", GB(id, 0, 8));
    03541             } else {
    03542               StationClassID sclass = _cur_grffile->stations[GB(id, 0, 8)]->sclass;
    03543               SetStationClassName(sclass, AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED));
    03544             }
    03545             break;
    03546
    03547           case 0xC5: // Station name
    03548             if (_cur_grffile->stations == NULL || _cur_grffile->stations[GB(id, 0, 8)] == NULL) {
    03549               grfmsg(1, "FeatureNewName: Attempt to name undefined station 0x%X, ignoring", GB(id, 0, 8));
    03550             } else {
    03551               _cur_grffile->stations[GB(id, 0, 8)]->name = AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED);
    03552             }
    03553             break;
    03554
    03555           case 0xC9: // House name
    03556             if (_cur_grffile->housespec == NULL || _cur_grffile->housespec[GB(id, 0, 8)] == NULL) {
    03557               grfmsg(1, "FeatureNewName: Attempt to name undefined house 0x%X, ignoring.", GB(id, 0, 8));
    03558             } else {
    03559               _cur_grffile->housespec[GB(id, 0, 8)]->building_name = AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED);
    03560             }
    03561             break;
    03562
    03563           case 0xD0:
    03564           case 0xD1:
    03565           case 0xD2:
    03566           case 0xD3:
    03567           case 0xDC:
    03568             AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED);
    03569             break;
    03570
    03571           default:
    03572             grfmsg(7, "FeatureNewName: Unsupported ID (0x%04X)", id);
    03573             break;
    03574         }
    03575         break;
    03576
    03577 #if 0
    03578         case GSF_CANAL :
    03579         case GSF_BRIDGE :
    03580           AddGRFString(_cur_spriteid, id, lang, name);
    03581           switch (GB(id, 8, 8)) {
    03582             case 0xC9: // House name
    03583             default:
    03584               grfmsg(7, "FeatureNewName: Unsupported ID (0x%04X)", id);
    03585           }
    03586           break;
    03587
    03588         default :
    03589           grfmsg(7, "FeatureNewName: Unsupported feature (0x%02X)", feature);
    03590           break;
    03591 #endif
    03592     }
    03593   }
    03594 }
    03595
    03604 static uint16 SanitizeSpriteOffset(uint16& num, uint16 offset, int max_sprites, const char *name)
    03605 {
    03606
    03607   if (offset >= max_sprites) {
    03608     grfmsg(1, "GraphicsNew: %s sprite offset must be less than %i, skipping", name, max_sprites);
    03609     uint orig_num = num;
    03610     num = 0;
    03611     return orig_num;
    03612   }
    03613
    03614   if (offset + num > max_sprites) {
    03615     grfmsg(4, "GraphicsNew: %s sprite overflow, truncating...", name);
    03616     uint orig_num = num;
    03617     num = max(max_sprites - offset, 0);
    03618     return orig_num - num;
    03619   }
    03620
    03621   return 0;
    03622 }
    03623
    03624 /* Action 0x05 */
    03625 static void GraphicsNew(byte *buf, size_t len)
    03626 {
    03627   /* <05> <graphics-type> <num-sprites> <other data...>
    03628    *
    03629    * B graphics-type What set of graphics the sprites define.
    03630    * E num-sprites   How many sprites are in this set?
    03631    * V other data    Graphics type specific data.  Currently unused. */
    03632   /* TODO */
    03633
    03634   enum Action5BlockType {
    03635     A5BLOCK_FIXED,
    03636     A5BLOCK_ALLOW_OFFSET,
    03637     A5BLOCK_INVALID,
    03638   };
    03639   struct Action5Type {
    03640     Action5BlockType block_type;
    03641     SpriteID sprite_base;
    03642     uint16 min_sprites;
    03643     uint16 max_sprites;
    03644     const char *name;
    03645   };
    03646
    03647   static const Action5Type action5_types[] = {
    03648     /* Note: min_sprites should not be changed. Therefore these constants are directly here and not in sprites.h */
    03649     /* 0x00 */ { A5BLOCK_INVALID,      0,                            0, 0,                                           "Type 0x00"             },
    03650     /* 0x01 */ { A5BLOCK_INVALID,      0,                            0, 0,                                           "Type 0x01"             },
    03651     /* 0x02 */ { A5BLOCK_INVALID,      0,                            0, 0,                                           "Type 0x02"             },
    03652     /* 0x03 */ { A5BLOCK_INVALID,      0,                            0, 0,                                           "Type 0x03"             },
    03653     /* 0x04 */ { A5BLOCK_FIXED,        SPR_SIGNALS_BASE,            48, PRESIGNAL_SEMAPHORE_AND_PBS_SPRITE_COUNT,    "Signal graphics"       },
    03654     /* 0x05 */ { A5BLOCK_FIXED,        SPR_ELRAIL_BASE,             48, ELRAIL_SPRITE_COUNT,                         "Catenary graphics"     },
    03655     /* 0x06 */ { A5BLOCK_FIXED,        SPR_SLOPES_BASE,             74, NORMAL_AND_HALFTILE_FOUNDATION_SPRITE_COUNT, "Foundation graphics"   },
    03656     /* 0x07 */ { A5BLOCK_INVALID,      0,                           75, 0,                                           "TTDP GUI graphics"     }, // Not used by OTTD.
    03657     /* 0x08 */ { A5BLOCK_FIXED,        SPR_CANALS_BASE,             65, CANALS_SPRITE_COUNT,                         "Canal graphics"        },
    03658     /* 0x09 */ { A5BLOCK_FIXED,        SPR_ONEWAY_BASE,              6, ONEWAY_SPRITE_COUNT,                         "One way road graphics" },
    03659     /* 0x0A */ { A5BLOCK_FIXED,        SPR_2CCMAP_BASE,            256, TWOCCMAP_SPRITE_COUNT,                       "2CC colour maps"       },
    03660     /* 0x0B */ { A5BLOCK_FIXED,        SPR_TRAMWAY_BASE,           113, TRAMWAY_SPRITE_COUNT,                        "Tramway graphics"      },
    03661     /* 0x0C */ { A5BLOCK_INVALID,      0,                          133, 0,                                           "Snowy temperate tree"  }, // Not yet used by OTTD.
    03662     /* 0x0D */ { A5BLOCK_FIXED,        SPR_SHORE_BASE,              16, SPR_SHORE_SPRITE_COUNT,                      "Shore graphics"        },
    03663     /* 0x0E */ { A5BLOCK_INVALID,      0,                            0, 0,                                           "New Signals graphics"  }, // Not yet used by OTTD.
    03664     /* 0x0F */ { A5BLOCK_FIXED,        SPR_TRACKS_FOR_SLOPES_BASE,  12, TRACKS_FOR_SLOPES_SPRITE_COUNT,              "Sloped rail track"     },
    03665     /* 0x10 */ { A5BLOCK_FIXED,        SPR_AIRPORTX_BASE,           15, AIRPORTX_SPRITE_COUNT,                       "Airport graphics"      },
    03666     /* 0x11 */ { A5BLOCK_FIXED,        SPR_ROADSTOP_BASE,            8, ROADSTOP_SPRITE_COUNT,                       "Road stop graphics"    },
    03667     /* 0x12 */ { A5BLOCK_FIXED,        SPR_AQUEDUCT_BASE,            8, AQUEDUCT_SPRITE_COUNT,                       "Aqueduct graphics"     },
    03668     /* 0x13 */ { A5BLOCK_FIXED,        SPR_AUTORAIL_BASE,           55, AUTORAIL_SPRITE_COUNT,                       "Autorail graphics"     },
    03669     /* 0x14 */ { A5BLOCK_ALLOW_OFFSET, SPR_FLAGS_BASE,               1, FLAGS_SPRITE_COUNT,                          "Flag graphics"         },
    03670     /* 0x15 */ { A5BLOCK_ALLOW_OFFSET, SPR_OPENTTD_BASE,             1, OPENTTD_SPRITE_COUNT,                        "OpenTTD GUI graphics"  },
    03671   };
    03672
    03673   if (!check_length(len, 2, "GraphicsNew")) return;
    03674   buf++;
    03675   uint8 type = grf_load_byte(&buf);
    03676   uint16 num = grf_load_extended(&buf);
    03677   uint16 offset = HasBit(type, 7) ? grf_load_extended(&buf) : 0;
    03678   ClrBit(type, 7); // Clear the high bit as that only indicates whether there is an offset.
    03679
    03680   if ((type == 0x0D) && (num == 10) && _cur_grffile->is_ottdfile) {
    03681     /* Special not-TTDP-compatible case used in openttd(d/w).grf
    03682      * Missing shore sprites and initialisation of SPR_SHORE_BASE */
    03683     grfmsg(2, "GraphicsNew: Loading 10 missing shore sprites from openttd(d/w).grf.");
    03684     LoadNextSprite(SPR_SHORE_BASE +  0, _file_index, _nfo_line++); // SLOPE_STEEP_S
    03685     LoadNextSprite(SPR_SHORE_BASE +  5, _file_index, _nfo_line++); // SLOPE_STEEP_W
    03686     LoadNextSprite(SPR_SHORE_BASE +  7, _file_index, _nfo_line++); // SLOPE_WSE
    03687     LoadNextSprite(SPR_SHORE_BASE + 10, _file_index, _nfo_line++); // SLOPE_STEEP_N
    03688     LoadNextSprite(SPR_SHORE_BASE + 11, _file_index, _nfo_line++); // SLOPE_NWS
    03689     LoadNextSprite(SPR_SHORE_BASE + 13, _file_index, _nfo_line++); // SLOPE_ENW
    03690     LoadNextSprite(SPR_SHORE_BASE + 14, _file_index, _nfo_line++); // SLOPE_SEN
    03691     LoadNextSprite(SPR_SHORE_BASE + 15, _file_index, _nfo_line++); // SLOPE_STEEP_E
    03692     LoadNextSprite(SPR_SHORE_BASE + 16, _file_index, _nfo_line++); // SLOPE_EW
    03693     LoadNextSprite(SPR_SHORE_BASE + 17, _file_index, _nfo_line++); // SLOPE_NS
    03694     if (_loaded_newgrf_features.shore == SHORE_REPLACE_NONE) _loaded_newgrf_features.shore = SHORE_REPLACE_ONLY_NEW;
    03695     return;
    03696   }
    03697
    03698   /* Supported type? */
    03699   if ((type >= lengthof(action5_types)) || (action5_types[type].block_type == A5BLOCK_INVALID)) {
    03700     grfmsg(2, "GraphicsNew: Custom graphics (type 0x%02X) sprite block of length %u (unimplemented, ignoring)", type, num);
    03701     _skip_sprites = num;
    03702     return;
    03703   }
    03704
    03705   const Action5Type *action5_type = &action5_types[type];
    03706
    03707   /* Ignore offset if not allowed */
    03708   if ((action5_type->block_type != A5BLOCK_ALLOW_OFFSET) && (offset != 0)) {
    03709     grfmsg(1, "GraphicsNew: %s (type 0x%02X) do not allow an <offset> field. Ignoring offset.", action5_type->name, type);
    03710     offset = 0;
    03711   }
    03712
    03713   /* Ignore action5 if too few sprites are specified. (for TTDP compatibility)
    03714    * This does not make sense, if <offset> is allowed */
    03715   if ((action5_type->block_type == A5BLOCK_FIXED) && (num < action5_type->min_sprites)) {
    03716     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);
    03717     _skip_sprites = num;
    03718     return;
    03719   }
    03720
    03721   /* Load at most max_sprites sprites. Skip remaining sprites. (for compatibility with TTDP and future extentions) */
    03722   uint16 skip_num = SanitizeSpriteOffset(num, offset, action5_type->max_sprites, action5_type->name);
    03723   SpriteID replace = action5_type->sprite_base + offset;
    03724
    03725   /* Load <num> sprites starting from <replace>, then skip <skip_num> sprites. */
    03726   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);
    03727
    03728   for (; num > 0; num--) {
    03729     _nfo_line++;
    03730     LoadNextSprite(replace == 0 ? _cur_spriteid++ : replace++, _file_index, _nfo_line);
    03731   }
    03732
    03733   if (type == 0x0D) _loaded_newgrf_features.shore = SHORE_REPLACE_ACTION_5;
    03734
    03735   _skip_sprites = skip_num;
    03736 }
    03737
    03738 /* Action 0x05 (SKIP) */
    03739 static void SkipAct5(byte *buf, size_t len)
    03740 {
    03741   if (!check_length(len, 2, "SkipAct5")) return;
    03742   buf++;
    03743
    03744   /* Ignore type byte */
    03745   grf_load_byte(&buf);
    03746
    03747   /* Skip the sprites of this action */
    03748   _skip_sprites = grf_load_extended(&buf);
    03749
    03750   grfmsg(3, "SkipAct5: Skipping %d sprites", _skip_sprites);
    03751 }
    03752
    03763 bool GetGlobalVariable(byte param, uint32 *value)
    03764 {
    03765   switch (param) {
    03766     case 0x00: // current date
    03767       *value = max(_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0);
    03768       return true;
    03769
    03770     case 0x01: // current year
    03771       *value = Clamp(_cur_year, ORIGINAL_BASE_YEAR, ORIGINAL_MAX_YEAR) - ORIGINAL_BASE_YEAR;
    03772       return true;
    03773
    03774     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)
    03775       YearMonthDay ymd;
    03776       ConvertDateToYMD(_date, &ymd);
    03777       Date start_of_year = ConvertYMDToDate(ymd.year, 0, 1);
    03778       *value = ymd.month | (ymd.day - 1) << 8 | (IsLeapYear(ymd.year) ? 1 << 15 : 0) | (_date - start_of_year) << 16;
    03779       return true;
    03780     }
    03781
    03782     case 0x03: // current climate, 0=temp, 1=arctic, 2=trop, 3=toyland
    03783       *value = _settings_game.game_creation.landscape;
    03784       return true;
    03785
    03786     case 0x06: // road traffic side, bit 4 clear=left, set=right
    03787       *value = _settings_game.vehicle.road_side << 4;
    03788       return true;
    03789
    03790     case 0x09: // date fraction
    03791       *value = _date_fract * 885;
    03792       return true;
    03793
    03794     case 0x0A: // animation counter
    03795       *value = _tick_counter;
    03796       return true;
    03797
    03798     case 0x0B: { // TTDPatch version
    03799       uint major    = 2;
    03800       uint minor    = 6;
    03801       uint revision = 1; // special case: 2.0.1 is 2.0.10
    03802       uint build    = 1382;
    03803       *value = (major << 24) | (minor << 20) | (revision << 16) | build;
    03804       return true;
    03805     }
    03806
    03807     case 0x0D: // TTD Version, 00=DOS, 01=Windows
    03808       *value = _cur_grfconfig->windows_paletted;
    03809       return true;
    03810
    03811     case 0x0E: // Y-offset for train sprites
    03812       *value = _traininfo_vehicle_pitch;
    03813       return true;
    03814
    03815     case 0x0F: // Rail track type cost factors
    03816       *value = 0;
    03817       SB(*value, 0, 8, GetRailTypeInfo(RAILTYPE_RAIL)->cost_multiplier); // normal rail
    03818       if (_settings_game.vehicle.disable_elrails) {
    03819         /* skip elrail multiplier - disabled */
    03820         SB(*value, 8, 8, GetRailTypeInfo(RAILTYPE_MONO)->cost_multiplier); // monorail
    03821       } else {
    03822         SB(*value, 8, 8, GetRailTypeInfo(RAILTYPE_ELECTRIC)->cost_multiplier); // electified railway
    03823         /* Skip monorail multiplier - no space in result */
    03824       }
    03825       SB(*value, 16, 8, GetRailTypeInfo(RAILTYPE_MAGLEV)->cost_multiplier); // maglev
    03826       return true;
    03827
    03828     case 0x11: // current rail tool type
    03829       *value = 0;
    03830       return true;
    03831
    03832     case 0x12: // Game mode
    03833       *value = _game_mode;
    03834       return true;
    03835
    03836     /* case 0x13: // Tile refresh offset to left    not implemented */
    03837     /* case 0x14: // Tile refresh offset to right   not implemented */
    03838     /* case 0x15: // Tile refresh offset upwards    not implemented */
    03839     /* case 0x16: // Tile refresh offset downwards  not implemented */
    03840     /* case 0x17: // temperate snow line            not implemented */
    03841
    03842     case 0x1A: // Always -1
    03843       *value = UINT_MAX;
    03844       return true;
    03845
    03846     case 0x1B: // Display options
    03847       *value = GB(_display_opt, 0, 6);
    03848       return true;
    03849
    03850     case 0x1D: // TTD Platform, 00=TTDPatch, 01=OpenTTD
    03851       *value = 1;
    03852       return true;
    03853
    03854     case 0x1E: // Miscellaneous GRF features
    03855       *value = _misc_grf_features;
    03856       return true;
    03857
    03858     /* case 0x1F: // locale dependent settings not implemented */
    03859
    03860     case 0x20: // snow line height
    03861       *value = _settings_game.game_creation.landscape == LT_ARCTIC ? GetSnowLine() : 0xFF;
    03862       return true;
    03863
    03864     case 0x21: // OpenTTD version
    03865       *value = _openttd_newgrf_version;
    03866       return true;
    03867
    03868     case 0x22: // difficulty level
    03869       *value = _settings_game.difficulty.diff_level;
    03870       return true;
    03871
    03872     case 0x23: // long format date
    03873       *value = _date;
    03874       return true;
    03875
    03876     case 0x24: // long format year
    03877       *value = _cur_year;
    03878       return true;
    03879
    03880     default: return false;
    03881   }
    03882 }
    03883
    03884 static uint32 GetParamVal(byte param, uint32 *cond_val)
    03885 {
    03886   /* First handle variable common with VarAction2 */
    03887   uint32 value;
    03888   if (GetGlobalVariable(param - 0x80, &value)) return value;
    03889
    03890   /* Non-common variable */
    03891   switch (param) {
    03892     case 0x84: { // GRF loading stage
    03893       uint32 res = 0;
    03894
    03895       if (_cur_stage > GLS_INIT) SetBit(res, 0);
    03896       if (_cur_stage == GLS_RESERVE) SetBit(res, 8);
    03897       if (_cur_stage == GLS_ACTIVATION) SetBit(res, 9);
    03898       return res;
    03899     }
    03900
    03901     case 0x85: // TTDPatch flags, only for bit tests
    03902       if (cond_val == NULL) {
    03903         /* Supported in Action 0x07 and 0x09, not 0x0D */
    03904         return 0;
    03905       } else {
    03906         uint32 param_val = _ttdpatch_flags[*cond_val / 0x20];
    03907         *cond_val %= 0x20;
    03908         return param_val;
    03909       }
    03910
    03911     case 0x88: // GRF ID check
    03912       return 0;
    03913
    03914     /* case 0x99: Global ID offest not implemented */
    03915
    03916     default:
    03917       /* GRF Parameter */
    03918       if (param < 0x80) return _cur_grffile->param[param];
    03919
    03920       /* In-game variable. */
    03921       grfmsg(1, "Unsupported in-game variable 0x%02X", param);
    03922       return UINT_MAX;
    03923   }
    03924 }
    03925
    03926 /* Action 0x06 */
    03927 static void CfgApply(byte *buf, size_t len)
    03928 {
    03929   /* <06> <param-num> <param-size> <offset> ... <FF>
    03930    *
    03931    * B param-num     Number of parameter to substitute (First = "zero")
    03932    *                 Ignored if that parameter was not specified in newgrf.cfg
    03933    * B param-size    How many bytes to replace.  If larger than 4, the
    03934    *                 bytes of the following parameter are used.  In that
    03935    *                 case, nothing is applied unless *all* parameters
    03936    *                 were specified.
    03937    * B offset        Offset into data from beginning of next sprite
    03938    *                 to place where parameter is to be stored. */
    03939
    03940   /* Preload the next sprite */
    03941   size_t pos = FioGetPos();
    03942   uint16 num = FioReadWord();
    03943   uint8 type = FioReadByte();
    03944   byte *preload_sprite = NULL;
    03945
    03946   /* Check if the sprite is a pseudo sprite. We can't operate on real sprites. */
    03947   if (type == 0xFF) {
    03948     preload_sprite = MallocT<byte>(num);
    03949     FioReadBlock(preload_sprite, num);
    03950   }
    03951
    03952   /* Reset the file position to the start of the next sprite */
    03953   FioSeekTo(pos, SEEK_SET);
    03954
    03955   if (type != 0xFF) {
    03956     grfmsg(2, "CfgApply: Ignoring (next sprite is real, unsupported)");
    03957     free(preload_sprite);
    03958     return;
    03959   }
    03960
    03961   GRFLocation location(_cur_grfconfig->grfid, _nfo_line + 1);
    03962   GRFLineToSpriteOverride::iterator it = _grf_line_to_action6_sprite_override.find(location);
    03963   if (it != _grf_line_to_action6_sprite_override.end()) {
    03964     free(preload_sprite);
    03965     preload_sprite = _grf_line_to_action6_sprite_override[location];
    03966   } else {
    03967     _grf_line_to_action6_sprite_override[location] = preload_sprite;
    03968   }
    03969
    03970   /* Now perform the Action 0x06 on our data. */
    03971   buf++;
    03972
    03973   for (;;) {
    03974     uint i;
    03975     uint param_num;
    03976     uint param_size;
    03977     uint offset;
    03978     bool add_value;
    03979
    03980     /* Read the parameter to apply. 0xFF indicates no more data to change. */
    03981     param_num = grf_load_byte(&buf);
    03982     if (param_num == 0xFF) break;
    03983
    03984     /* Get the size of the parameter to use. If the size covers multiple
    03985      * double words, sequential parameter values are used. */
    03986     param_size = grf_load_byte(&buf);
    03987
    03988     /* Bit 7 of param_size indicates we should add to the original value
    03989      * instead of replacing it. */
    03990     add_value  = HasBit(param_size, 7);
    03991     param_size = GB(param_size, 0, 7);
    03992
    03993     /* Where to apply the data to within the pseudo sprite data. */
    03994     offset     = grf_load_extended(&buf);
    03995
    03996     /* If the parameter is a GRF parameter (not an internal variable) check
    03997      * if it (and all further sequential parameters) has been defined. */
    03998     if (param_num < 0x80 && (param_num + (param_size - 1) / 4) >= _cur_grffile->param_end) {
    03999       grfmsg(2, "CfgApply: Ignoring (param %d not set)", (param_num + (param_size - 1) / 4));
    04000       break;
    04001     }
    04002
    04003     grfmsg(8, "CfgApply: Applying %u bytes from parameter 0x%02X at offset 0x%04X", param_size, param_num, offset);
    04004
    04005     bool carry = false;
    04006     for (i = 0; i < param_size && offset + i < num; i++) {
    04007       uint32 value = GetParamVal(param_num + i / 4, NULL);
    04008       /* Reset carry flag for each iteration of the variable (only really
    04009        * matters if param_size is greater than 4) */
    04010       if (i == 0) carry = false;
    04011
    04012       if (add_value) {
    04013         uint new_value = preload_sprite[offset + i] + GB(value, (i % 4) * 8, 8) + (carry ? 1 : 0);
    04014         preload_sprite[offset + i] = GB(new_value, 0, 8);
    04015         /* Check if the addition overflowed */
    04016         carry = new_value >= 256;
    04017       } else {
    04018         preload_sprite[offset + i] = GB(value, (i % 4) * 8, 8);
    04019       }
    04020     }
    04021   }
    04022 }
    04023
    04033 static void DisableStaticNewGRFInfluencingNonStaticNewGRFs(GRFConfig *c)
    04034 {
    04035   if (c->error != NULL) {
    04036     free(c->error->custom_message);
    04037     free(c->error->data);
    04038     free(c->error);
    04039   }
    04040   c->status = GCS_DISABLED;
    04041   c->error  = CallocT<GRFError>(1);
    04042   c->error->data = strdup(_cur_grfconfig->name);
    04043   c->error->severity = STR_NEWGRF_ERROR_MSG_FATAL;
    04044   c->error->message  = STR_NEWGRF_ERROR_STATIC_GRF_CAUSES_DESYNC;
    04045
    04046   ClearTemporaryNewGRFData(GetFileByGRFID(c->grfid));
    04047 }
    04048
    04049 /* Action 0x07
    04050  * Action 0x09 */
    04051 static void SkipIf(byte *buf, size_t len)
    04052 {
    04053   /* <07/09> <param-num> <param-size> <condition-type> <value> <num-sprites>
    04054    *
    04055    * B param-num
    04056    * B param-size
    04057    * B condition-type
    04058    * V value
    04059    * B num-sprites */
    04060   /* TODO: More params. More condition types. */
    04061   uint32 cond_val = 0;
    04062   uint32 mask = 0;
    04063   bool result;
    04064
    04065   if (!check_length(len, 6, "SkipIf")) return;
    04066   buf++;
    04067   uint8 param     = grf_load_byte(&buf);
    04068   uint8 paramsize = grf_load_byte(&buf);
    04069   uint8 condtype  = grf_load_byte(&buf);
    04070
    04071   if (condtype < 2) {
    04072     /* Always 1 for bit tests, the given value should be ignored. */
    04073     paramsize = 1;
    04074   }
    04075
    04076   switch (paramsize) {
    04077     case 8: cond_val = grf_load_dword(&buf); mask = grf_load_dword(&buf); break;
    04078     case 4: cond_val = grf_load_dword(&buf); mask = 0xFFFFFFFF; break;
    04079     case 2: cond_val = grf_load_word(&buf);  mask = 0x0000FFFF; break;
    04080     case 1: cond_val = grf_load_byte(&buf);  mask = 0x000000FF; break;
    04081     default: break;
    04082   }
    04083
    04084   if (param < 0x80 && _cur_grffile->param_end <= param) {
    04085     grfmsg(7, "SkipIf: Param %d undefined, skipping test", param);
    04086     return;
    04087   }
    04088
    04089   uint32 param_val = GetParamVal(param, &cond_val);
    04090
    04091   grfmsg(7, "SkipIf: Test condtype %d, param 0x%08X, condval 0x%08X", condtype, param_val, cond_val);
    04092
    04093   /*
    04094    * Parameter (variable in specs) 0x88 can only have GRF ID checking
    04095    * conditions, except conditions 0x0B and 0x0C (cargo availability)
    04096    * as those ignore the parameter. So, when the condition type is
    04097    * either of those, the specific variable 0x88 code is skipped, so
    04098    * the "general" code for the cargo availability conditions kicks in.
    04099    */
    04100   if (param == 0x88 && condtype != 0x0B && condtype != 0x0C) {
    04101     /* GRF ID checks */
    04102
    04103     GRFConfig *c = GetGRFConfig(cond_val, mask);
    04104
    04105     if (c != NULL && HasBit(c->flags, GCF_STATIC) && !HasBit(_cur_grfconfig->flags, GCF_STATIC) && c->status != GCS_DISABLED && _networking) {
    04106       DisableStaticNewGRFInfluencingNonStaticNewGRFs(c);
    04107       c = NULL;
    04108     }
    04109
    04110     if (condtype != 10 && c == NULL) {
    04111       grfmsg(7, "SkipIf: GRFID 0x%08X unknown, skipping test", BSWAP32(cond_val));
    04112       return;
    04113     }
    04114
    04115     switch (condtype) {
    04116       /* Tests 0x06 to 0x0A are only for param 0x88, GRFID checks */
    04117       case 0x06: // Is GRFID active?
    04118         result = c->status == GCS_ACTIVATED;
    04119         break;
    04120
    04121       case 0x07: // Is GRFID non-active?
    04122         result = c->status != GCS_ACTIVATED;
    04123         break;
    04124
    04125       case 0x08: // GRFID is not but will be active?
    04126         result = c->status == GCS_INITIALISED;
    04127         break;
    04128
    04129       case 0x09: // GRFID is or will be active?
    04130         result = c->status == GCS_ACTIVATED || c->status == GCS_INITIALISED;
    04131         break;
    04132
    04133       case 0x0A: // GRFID is not nor will be active
    04134         /* This is the only condtype that doesn't get ignored if the GRFID is not found */
    04135         result = c == NULL || c->flags == GCS_DISABLED || c->status == GCS_NOT_FOUND;
    04136         break;
    04137
    04138       default: grfmsg(1, "SkipIf: Unsupported GRF condition type %02X. Ignoring", condtype); return;
    04139     }
    04140   } else {
    04141     /* Parameter or variable tests */
    04142     switch (condtype) {
    04143       case 0x00: result = !!(param_val & (1 << cond_val));
    04144         break;
    04145       case 0x01: result = !(param_val & (1 << cond_val));
    04146         break;
    04147       case 0x02: result = (param_val & mask) == cond_val;
    04148         break;
    04149       case 0x03: result = (param_val & mask) != cond_val;
    04150         break;
    04151       case 0x04: result = (param_val & mask) < cond_val;
    04152         break;
    04153       case 0x05: result = (param_val & mask) > cond_val;
    04154         break;
    04155       case 0x0B: result = GetCargoIDByLabel(BSWAP32(cond_val)) == CT_INVALID;
    04156         break;
    04157       case 0x0C: result = GetCargoIDByLabel(BSWAP32(cond_val)) != CT_INVALID;
    04158         break;
    04159       case 0x0D: result = GetRailTypeByLabel(BSWAP32(cond_val)) == INVALID_RAILTYPE;
    04160         break;
    04161       case 0x0E: result = GetRailTypeByLabel(BSWAP32(cond_val)) != INVALID_RAILTYPE;
    04162         break;
    04163
    04164       default: grfmsg(1, "SkipIf: Unsupported condition type %02X. Ignoring", condtype); return;
    04165     }
    04166   }
    04167
    04168   if (!result) {
    04169     grfmsg(2, "SkipIf: Not skipping sprites, test was false");
    04170     return;
    04171   }
    04172
    04173   uint8 numsprites = grf_load_byte(&buf);
    04174
    04175   /* numsprites can be a GOTO label if it has been defined in the GRF
    04176    * file. The jump will always be the first matching label that follows
    04177    * the current nfo_line. If no matching label is found, the first matching
    04178    * label in the file is used. */
    04179   GRFLabel *choice = NULL;
    04180   for (GRFLabel *label = _cur_grffile->label; label != NULL; label = label->next) {
    04181     if (label->label != numsprites) continue;
    04182
    04183     /* Remember a goto before the current line */
    04184     if (choice == NULL) choice = label;
    04185     /* If we find a label here, this is definitely good */
    04186     if (label->nfo_line > _nfo_line) {
    04187       choice = label;
    04188       break;
    04189     }
    04190   }
    04191
    04192   if (choice != NULL) {
    04193     grfmsg(2, "SkipIf: Jumping to label 0x%0X at line %d, test was true", choice->label, choice->nfo_line);
    04194     FioSeekTo(choice->pos, SEEK_SET);
    04195     _nfo_line = choice->nfo_line;
    04196     return;
    04197   }
    04198
    04199   grfmsg(2, "SkipIf: Skipping %d sprites, test was true", numsprites);
    04200   _skip_sprites = numsprites;
    04201   if (_skip_sprites == 0) {
    04202     /* Zero means there are no sprites to skip, so
    04203      * we use -1 to indicate that all further
    04204      * sprites should be skipped. */
    04205     _skip_sprites = -1;
    04206
    04207     /* If an action 8 hasn't been encountered yet, disable the grf. */
    04208     if (_cur_grfconfig->status != GCS_ACTIVATED) {
    04209       _cur_grfconfig->status = GCS_DISABLED;
    04210       ClearTemporaryNewGRFData(_cur_grffile);
    04211     }
    04212   }
    04213 }
    04214
    04215
    04216 /* Action 0x08 (GLS_FILESCAN) */
    04217 static void ScanInfo(byte *buf, size_t len)
    04218 {
    04219   if (!check_length(len, 8, "Info")) return;
    04220   buf++;
    04221   grf_load_byte(&buf);
    04222   uint32 grfid  = grf_load_dword(&buf);
    04223
    04224   _cur_grfconfig->grfid = grfid;
    04225
    04226   /* GRF IDs starting with 0xFF are reserved for internal TTDPatch use */
    04227   if (GB(grfid, 24, 8) == 0xFF) SetBit(_cur_grfconfig->flags, GCF_SYSTEM);
    04228
    04229   len -= 6;
    04230   const char *name = grf_load_string(&buf, len);
    04231   _cur_grfconfig->name = TranslateTTDPatchCodes(grfid, name);
    04232
    04233   len -= strlen(name) + 1;
    04234   if (len > 0) {
    04235     const char *info = grf_load_string(&buf, len);
    04236     _cur_grfconfig->info = TranslateTTDPatchCodes(grfid, info);
    04237   }
    04238
    04239   /* GLS_INFOSCAN only looks for the action 8, so we can skip the rest of the file */
    04240   _skip_sprites = -1;
    04241 }
    04242
    04243 /* Action 0x08 */
    04244 static void GRFInfo(byte *buf, size_t len)
    04245 {
    04246   /* <08> <version> <grf-id> <name> <info>
    04247    *
    04248    * B version       newgrf version, currently 06
    04249    * 4*B grf-id      globally unique ID of this .grf file
    04250    * S name          name of this .grf set
    04251    * S info          string describing the set, and e.g. author and copyright */
    04252
    04253   if (!check_length(len, 8, "GRFInfo")) return;
    04254   buf++;
    04255   uint8 version    = grf_load_byte(&buf);
    04256   uint32 grfid     = grf_load_dword(&buf);
    04257   const char *name = grf_load_string(&buf, len - 6);
    04258
    04259   if (_cur_stage < GLS_RESERVE && _cur_grfconfig->status != GCS_UNKNOWN) {
    04260     _cur_grfconfig->status = GCS_DISABLED;
    04261     _cur_grfconfig->error  = CallocT<GRFError>(1);
    04262     _cur_grfconfig->error->severity = STR_NEWGRF_ERROR_MSG_FATAL;
    04263     _cur_grfconfig->error->message  = STR_NEWGRF_ERROR_MULTIPLE_ACTION_8;
    04264
    04265     _skip_sprites = -1;
    04266     return;
    04267   }
    04268
    04269   _cur_grffile->grfid = grfid;
    04270   _cur_grffile->grf_version = version;
    04271   _cur_grfconfig->status = _cur_stage < GLS_RESERVE ? GCS_INITIALISED : GCS_ACTIVATED;
    04272
    04273   /* Do swap the GRFID for displaying purposes since people expect that */
    04274   DEBUG(grf, 1, "GRFInfo: Loaded GRFv%d set %08X - %s (palette: %s)", version, BSWAP32(grfid), name, _cur_grfconfig->windows_paletted ? "Windows" : "DOS");
    04275 }
    04276
    04277 /* Action 0x0A */
    04278 static void SpriteReplace(byte *buf, size_t len)
    04279 {
    04280   /* <0A> <num-sets> <set1> [<set2> ...]
    04281    * <set>: <num-sprites> <first-sprite>
    04282    *
    04283    * B num-sets      How many sets of sprites to replace.
    04284    * Each set:
    04285    * B num-sprites   How many sprites are in this set
    04286    * W first-sprite  First sprite number to replace */
    04287
    04288   buf++; // skip action byte
    04289   uint8 num_sets = grf_load_byte(&buf);
    04290
    04291   for (uint i = 0; i < num_sets; i++) {
    04292     uint8 num_sprites = grf_load_byte(&buf);
    04293     uint16 first_sprite = grf_load_word(&buf);
    04294
    04295     grfmsg(2, "SpriteReplace: [Set %d] Changing %d sprites, beginning with %d",
    04296       i, num_sprites, first_sprite
    04297     );
    04298
    04299     for (uint j = 0; j < num_sprites; j++) {
    04300       int load_index = first_sprite + j;
    04301       _nfo_line++;
    04302       LoadNextSprite(load_index, _file_index, _nfo_line); // XXX
    04303
    04304       /* Shore sprites now located at different addresses.
    04305        * So detect when the old ones get replaced. */
    04306       if (IsInsideMM(load_index, SPR_ORIGINALSHORE_START, SPR_ORIGINALSHORE_END + 1)) {
    04307         if (_loaded_newgrf_features.shore != SHORE_REPLACE_ACTION_5) _loaded_newgrf_features.shore = SHORE_REPLACE_ACTION_A;
    04308       }
    04309     }
    04310   }
    04311 }
    04312
    04313 /* Action 0x0A (SKIP) */
    04314 static void SkipActA(byte *buf, size_t len)
    04315 {
    04316   buf++;
    04317   uint8 num_sets = grf_load_byte(&buf);
    04318
    04319   for (uint i = 0; i < num_sets; i++) {
    04320     /* Skip the sprites this replaces */
    04321     _skip_sprites += grf_load_byte(&buf);
    04322     /* But ignore where they go */
    04323     grf_load_word(&buf);
    04324   }
    04325
    04326   grfmsg(3, "SkipActA: Skipping %d sprites", _skip_sprites);
    04327 }
    04328
    04329 /* Action 0x0B */
    04330 static void GRFLoadError(byte *buf, size_t len)
    04331 {
    04332   /* <0B> <severity> <language-id> <message-id> [<message...> 00] [<data...>] 00 [<parnum>]
    04333    *
    04334    * B severity      00: notice, contine loading grf file
    04335    *                 01: warning, continue loading grf file
    04336    *                 02: error, but continue loading grf file, and attempt
    04337    *                     loading grf again when loading or starting next game
    04338    *                 03: error, abort loading and prevent loading again in
    04339    *                     the future (only when restarting the patch)
    04340    * B language-id   see action 4, use 1F for built-in error messages
    04341    * B message-id    message to show, see below
    04342    * S message       for custom messages (message-id FF), text of the message
    04343    *                 not present for built-in messages.
    04344    * V data          additional data for built-in (or custom) messages
    04345    * B parnum        parameter numbers to be shown in the message (maximum of 2) */
    04346
    04347   static const StringID msgstr[] = {
    04348     STR_NEWGRF_ERROR_VERSION_NUMBER,
    04349     STR_NEWGRF_ERROR_DOS_OR_WINDOWS,
    04350     STR_NEWGRF_ERROR_UNSET_SWITCH,
    04351     STR_NEWGRF_ERROR_INVALID_PARAMETER,
    04352     STR_NEWGRF_ERROR_LOAD_BEFORE,
    04353     STR_NEWGRF_ERROR_LOAD_AFTER,
    04354     STR_NEWGRF_ERROR_OTTD_VERSION_NUMBER,
    04355   };
    04356
    04357   static const StringID sevstr[] = {
    04358     STR_NEWGRF_ERROR_MSG_INFO,
    04359     STR_NEWGRF_ERROR_MSG_WARNING,
    04360     STR_NEWGRF_ERROR_MSG_ERROR,
    04361     STR_NEWGRF_ERROR_MSG_FATAL
    04362   };
    04363
    04364   if (!check_length(len, 6, "GRFLoadError")) return;
    04365
    04366   /* For now we can only show one message per newgrf file. */
    04367   if (_cur_grfconfig->error != NULL) return;
    04368
    04369   buf++; // Skip the action byte.
    04370   byte severity   = grf_load_byte(&buf);
    04371   byte lang       = grf_load_byte(&buf);
    04372   byte message_id = grf_load_byte(&buf);
    04373   len -= 4;
    04374
    04375   /* Skip the error if it isn't valid for the current language. */
    04376   if (!CheckGrfLangID(lang, _cur_grffile->grf_version)) return;
    04377
    04378   /* Skip the error until the activation stage unless bit 7 of the severity
    04379    * is set. */
    04380   if (!HasBit(severity, 7) && _cur_stage == GLS_INIT) {
    04381     grfmsg(7, "GRFLoadError: Skipping non-fatal GRFLoadError in stage %d", _cur_stage);
    04382     return;
    04383   }
    04384   ClrBit(severity, 7);
    04385
    04386   if (severity >= lengthof(sevstr)) {
    04387     grfmsg(7, "GRFLoadError: Invalid severity id %d. Setting to 2 (non-fatal error).", severity);
    04388     severity = 2;
    04389   } else if (severity == 3) {
    04390     /* This is a fatal error, so make sure the GRF is deactivated and no
    04391      * more of it gets loaded. */
    04392     _cur_grfconfig->status = GCS_DISABLED;
    04393     ClearTemporaryNewGRFData(_cur_grffile);
    04394     _skip_sprites = -1;
    04395   }
    04396
    04397   if (message_id >= lengthof(msgstr) && message_id != 0xFF) {
    04398     grfmsg(7, "GRFLoadError: Invalid message id.");
    04399     return;
    04400   }
    04401
    04402   if (len <= 1) {
    04403     grfmsg(7, "GRFLoadError: No message data supplied.");
    04404     return;
    04405   }
    04406
    04407   GRFError *error = CallocT<GRFError>(1);
    04408
    04409   error->severity = sevstr[severity];
    04410
    04411   if (message_id == 0xFF) {
    04412     /* This is a custom error message. */
    04413     const char *message = grf_load_string(&buf, len);
    04414     len -= (strlen(message) + 1);
    04415
    04416     error->custom_message = TranslateTTDPatchCodes(_cur_grffile->grfid, message);
    04417   } else {
    04418     error->message = msgstr[message_id];
    04419   }
    04420
    04421   if (len > 0) {
    04422     const char *data = grf_load_string(&buf, len);
    04423     len -= (strlen(data) + 1);
    04424
    04425     error->data = TranslateTTDPatchCodes(_cur_grffile->grfid, data);
    04426   }
    04427
    04428   /* Only two parameter numbers can be used in the string. */
    04429   uint i = 0;
    04430   for (; i < 2 && len > 0; i++) {
    04431     uint param_number = grf_load_byte(&buf);
    04432     error->param_value[i] = (param_number < _cur_grffile->param_end ? _cur_grffile->param[param_number] : 0);
    04433     len--;
    04434   }
    04435   error->num_params = i;
    04436
    04437   _cur_grfconfig->error = error;
    04438 }
    04439
    04440 /* Action 0x0C */
    04441 static void GRFComment(byte *buf, size_t len)
    04442 {
    04443   /* <0C> [<ignored...>]
    04444    *
    04445    * V ignored       Anything following the 0C is ignored */
    04446
    04447   if (len == 1) return;
    04448
    04449   size_t text_len = len - 1;
    04450   const char *text = (const char*)(buf + 1);
    04451   grfmsg(2, "GRFComment: %.*s", (int)text_len, text);
    04452 }
    04453
    04454 /* Action 0x0D (GLS_SAFETYSCAN) */
    04455 static void SafeParamSet(byte *buf, size_t len)
    04456 {
    04457   if (!check_length(len, 5, "SafeParamSet")) return;
    04458   buf++;
    04459   uint8 target = grf_load_byte(&buf);
    04460
    04461   /* Only writing GRF parameters is considered safe */
    04462   if (target < 0x80) return;
    04463
    04464   /* GRM could be unsafe, but as here it can only happen after other GRFs
    04465    * are loaded, it should be okay. If the GRF tried to use the slots it
    04466    * reserved, it would be marked unsafe anyway. GRM for (e.g. bridge)
    04467    * sprites  is considered safe. */
    04468
    04469   SetBit(_cur_grfconfig->flags, GCF_UNSAFE);
    04470
    04471   /* Skip remainder of GRF */
    04472   _skip_sprites = -1;
    04473 }
    04474
    04475
    04476 static uint32 GetPatchVariable(uint8 param)
    04477 {
    04478   switch (param) {
    04479     /* start year - 1920 */
    04480     case 0x0B: return max(_settings_game.game_creation.starting_year, ORIGINAL_BASE_YEAR) - ORIGINAL_BASE_YEAR;
    04481
    04482     /* freight trains weight factor */
    04483     case 0x0E: return _settings_game.vehicle.freight_trains;
    04484
    04485     /* empty wagon speed increase */
    04486     case 0x0F: return 0;
    04487
    04488     /* plane speed factor; our patch option is reversed from TTDPatch's,
    04489      * the following is good for 1x, 2x and 4x (most common?) and...
    04490      * well not really for 3x. */
    04491     case 0x10:
    04492       switch (_settings_game.vehicle.plane_speed) {
    04493         default:
    04494         case 4: return 1;
    04495         case 3: return 2;
    04496         case 2: return 2;
    04497         case 1: return 4;
    04498       }
    04499
    04500
    04501     /* 2CC colourmap base sprite */
    04502     case 0x11: return SPR_2CCMAP_BASE;
    04503
    04504     /* map size: format = -MABXYSS
    04505      * M  : the type of map
    04506      *       bit 0 : set   : squared map. Bit 1 is now not relevant
    04507      *               clear : rectangle map. Bit 1 will indicate the bigger edge of the map
    04508      *       bit 1 : set   : Y is the bigger edge. Bit 0 is clear
    04509      *               clear : X is the bigger edge.
    04510      * A  : minimum edge(log2) of the map
    04511      * B  : maximum edge(log2) of the map
    04512      * XY : edges(log2) of each side of the map.
    04513      * SS : combination of both X and Y, thus giving the size(log2) of the map
    04514      */
    04515     case 0x13: {
    04516       byte map_bits = 0;
    04517       byte log_X = MapLogX() - 6; // substraction is required to make the minimal size (64) zero based
    04518       byte log_Y = MapLogY() - 6;
    04519       byte max_edge = max(log_X, log_Y);
    04520
    04521       if (log_X == log_Y) { // we have a squared map, since both edges are identical
    04522         SetBit(map_bits ,0);
    04523       } else {
    04524         if (max_edge == log_Y) SetBit(map_bits, 1); // edge Y been the biggest, mark it
    04525       }
    04526
    04527       return (map_bits << 24) | (min(log_X, log_Y) << 20) | (max_edge << 16) |
    04528         (log_X << 12) | (log_Y << 8) | (log_X + log_Y);
    04529     }
    04530
    04531     default:
    04532       grfmsg(2, "ParamSet: Unknown Patch variable 0x%02X.", param);
    04533       return 0;
    04534   }
    04535 }
    04536
    04537
    04538 static uint32 PerformGRM(uint32 *grm, uint16 num_ids, uint16 count, uint8 op, uint8 target, const char *type)
    04539 {
    04540   uint start = 0;
    04541   uint size  = 0;
    04542
    04543   if (op == 6) {
    04544     /* Return GRFID of set that reserved ID */
    04545     return grm[_cur_grffile->param[target]];
    04546   }
    04547
    04548   /* With an operation of 2 or 3, we want to reserve a specific block of IDs */
    04549   if (op == 2 || op == 3) start = _cur_grffile->param[target];
    04550
    04551   for (uint i = start; i < num_ids; i++) {
    04552     if (grm[i] == 0) {
    04553       size++;
    04554     } else {
    04555       if (op == 2 || op == 3) break;
    04556       start = i + 1;
    04557       size = 0;
    04558     }
    04559
    04560     if (size == count) break;
    04561   }
    04562
    04563   if (size == count) {
    04564     /* Got the slot... */
    04565     if (op == 0 || op == 3) {
    04566       grfmsg(2, "ParamSet: GRM: Reserving %d %s at %d", count, type, start);
    04567       for (uint i = 0; i < count; i++) grm[start + i] = _cur_grffile->grfid;
    04568     }
    04569     return start;
    04570   }
    04571
    04572   /* Unable to allocate */
    04573   if (op != 4 && op != 5) {
    04574     /* Deactivate GRF */
    04575     grfmsg(0, "ParamSet: GRM: Unable to allocate %d %s, deactivating", count, type);
    04576     _cur_grfconfig->status = GCS_DISABLED;
    04577     ClearTemporaryNewGRFData(_cur_grffile);
    04578     _skip_sprites = -1;
    04579     return UINT_MAX;
    04580   }
    04581
    04582   grfmsg(1, "ParamSet: GRM: Unable to allocate %d %s", count, type);
    04583   return UINT_MAX;
    04584 }
    04585
    04586
    04587 /* Action 0x0D */
    04588 static void ParamSet(byte *buf, size_t len)
    04589 {
    04590   /* <0D> <target> <operation> <source1> <source2> [<data>]
    04591    *
    04592    * B target        parameter number where result is stored
    04593    * B operation     operation to perform, see below
    04594    * B source1       first source operand
    04595    * B source2       second source operand
    04596    * D data          data to use in the calculation, not necessary
    04597    *                 if both source1 and source2 refer to actual parameters
    04598    *
    04599    * Operations
    04600    * 00      Set parameter equal to source1
    04601    * 01      Addition, source1 + source2
    04602    * 02      Subtraction, source1 - source2
    04603    * 03      Unsigned multiplication, source1 * source2 (both unsigned)
    04604    * 04      Signed multiplication, source1 * source2 (both signed)
    04605    * 05      Unsigned bit shift, source1 by source2 (source2 taken to be a
    04606    *         signed quantity; left shift if positive and right shift if
    04607    *         negative, source1 is unsigned)
    04608    * 06      Signed bit shift, source1 by source2
    04609    *         (source2 like in 05, and source1 as well)
    04610    */
    04611
    04612   if (!check_length(len, 5, "ParamSet")) return;
    04613   buf++;
    04614   uint8 target = grf_load_byte(&buf);
    04615   uint8 oper   = grf_load_byte(&buf);
    04616   uint32 src1  = grf_load_byte(&buf);
    04617   uint32 src2  = grf_load_byte(&buf);
    04618
    04619   uint32 data = 0;
    04620   if (len >= 8) data = grf_load_dword(&buf);
    04621
    04622   /* You can add 80 to the operation to make it apply only if the target
    04623    * is not defined yet.  In this respect, a parameter is taken to be
    04624    * defined if any of the following applies:
    04625    * - it has been set to any value in the newgrf(w).cfg parameter list
    04626    * - it OR A PARAMETER WITH HIGHER NUMBER has been set to any value by
    04627    *   an earlier action D */
    04628   if (HasBit(oper, 7)) {
    04629     if (target < 0x80 && target < _cur_grffile->param_end) {
    04630       grfmsg(7, "ParamSet: Param %u already defined, skipping", target);
    04631       return;
    04632     }
    04633
    04634     oper = GB(oper, 0, 7);
    04635   }
    04636
    04637   if (src2 == 0xFE) {
    04638     if (GB(data, 0, 8) == 0xFF) {
    04639       if (data == 0x0000FFFF) {
    04640         /* Patch variables */
    04641         src1 = GetPatchVariable(src1);
    04642       } else {
    04643         /* GRF Resource Management */
    04644         uint8  op      = src1;
    04645         uint8  feature = GB(data, 8, 8);
    04646         uint16 count   = GB(data, 16, 16);
    04647
    04648         if (_cur_stage == GLS_RESERVE) {
    04649           if (feature == 0x08) {
    04650             /* General sprites */
    04651             if (op == 0) {
    04652               /* Check if the allocated sprites will fit below the original sprite limit */
    04653               if (_cur_spriteid + count >= 16384) {
    04654                 grfmsg(0, "ParamSet: GRM: Unable to allocate %d sprites; try changing NewGRF order", count);
    04655                 _cur_grfconfig->status = GCS_DISABLED;
    04656                 ClearTemporaryNewGRFData(_cur_grffile);
    04657                 _skip_sprites = -1;
    04658                 return;
    04659               }
    04660
    04661               /* Reserve space at the current sprite ID */
    04662               grfmsg(4, "ParamSet: GRM: Allocated %d sprites at %d", count, _cur_spriteid);
    04663               _grm_sprites[GRFLocation(_cur_grffile->grfid, _nfo_line)] = _cur_spriteid;
    04664               _cur_spriteid += count;
    04665             }
    04666           }
    04667           /* Ignore GRM result during reservation */
    04668           src1 = 0;
    04669         } else if (_cur_stage == GLS_ACTIVATION) {
    04670           switch (feature) {
    04671             case 0x00: // Trains
    04672             case 0x01: // Road Vehicles
    04673             case 0x02: // Ships
    04674             case 0x03: // Aircraft
    04675               if (!_settings_game.vehicle.dynamic_engines) {
    04676                 src1 = PerformGRM(&_grm_engines[_engine_offsets[feature]], _engine_counts[feature], count, op, target, "vehicles");
    04677                 if (_skip_sprites == -1) return;
    04678               } else {
    04679                 /* GRM does not apply for dynamic engine allocation. */
    04680                 switch (op) {
    04681                   case 2:
    04682                   case 3:
    04683                     src1 = _cur_grffile->param[target];
    04684                     break;
    04685
    04686                   default:
    04687                     src1 = 0;
    04688                     break;
    04689                 }
    04690               }
    04691               break;
    04692
    04693             case 0x08: // General sprites
    04694               switch (op) {
    04695                 case 0:
    04696                   /* Return space reserved during reservation stage */
    04697                   src1 = _grm_sprites[GRFLocation(_cur_grffile->grfid, _nfo_line)];
    04698                   grfmsg(4, "ParamSet: GRM: Using pre-allocated sprites at %d", src1);
    04699                   break;
    04700
    04701                 case 1:
    04702                   src1 = _cur_spriteid;
    04703                   break;
    04704
    04705                 default:
    04706                   grfmsg(1, "ParamSet: GRM: Unsupported operation %d for general sprites", op);
    04707                   return;
    04708               }
    04709               break;
    04710
    04711             case 0x0B: // Cargo
    04712               /* There are two ranges: one for cargo IDs and one for cargo bitmasks */
    04713               src1 = PerformGRM(_grm_cargos, NUM_CARGO * 2, count, op, target, "cargos");
    04714               if (_skip_sprites == -1) return;
    04715               break;
    04716
    04717             default: grfmsg(1, "ParamSet: GRM: Unsupported feature 0x%X", feature); return;
    04718           }
    04719         } else {
    04720           /* Ignore GRM during initialization */
    04721           src1 = 0;
    04722         }
    04723       }
    04724     } else {
    04725       /* Read another GRF File's parameter */
    04726       const GRFFile *file = GetFileByGRFID(data);
    04727       GRFConfig *c = GetGRFConfig(data);
    04728       if (c != NULL && HasBit(c->flags, GCF_STATIC) && !HasBit(_cur_grfconfig->flags, GCF_STATIC) && _networking) {
    04729         /* Disable the read GRF if it is a static NewGRF. */
    04730         DisableStaticNewGRFInfluencingNonStaticNewGRFs(c);
    04731         src1 = 0;
    04732       } else if (file == NULL || src1 >= file->param_end || (c != NULL && c->status == GCS_DISABLED)) {
    04733         src1 = 0;
    04734       } else {
    04735         src1 = file->param[src1];
    04736       }
    04737     }
    04738   } else {
    04739     /* The source1 and source2 operands refer to the grf parameter number
    04740      * like in action 6 and 7.  In addition, they can refer to the special
    04741      * variables available in action 7, or they can be FF to use the value
    04742      * of <data>.  If referring to parameters that are undefined, a value
    04743      * of 0 is used instead.  */
    04744     src1 = (src1 == 0xFF) ? data : GetParamVal(src1, NULL);
    04745     src2 = (src2 == 0xFF) ? data : GetParamVal(src2, NULL);
    04746   }
    04747
    04748   /* TODO: You can access the parameters of another GRF file by using
    04749    * source2=FE, source1=the other GRF's parameter number and data=GRF
    04750    * ID.  This is only valid with operation 00 (set).  If the GRF ID
    04751    * cannot be found, a value of 0 is used for the parameter value
    04752    * instead. */
    04753
    04754   uint32 res;
    04755   switch (oper) {
    04756     case 0x00:
    04757       res = src1;
    04758       break;
    04759
    04760     case 0x01:
    04761       res = src1 + src2;
    04762       break;
    04763
    04764     case 0x02:
    04765       res = src1 - src2;
    04766       break;
    04767
    04768     case 0x03:
    04769       res = src1 * src2;
    04770       break;
    04771
    04772     case 0x04:
    04773       res = (int32)src1 * (int32)src2;
    04774       break;
    04775
    04776     case 0x05:
    04777       if ((int32)src2 < 0) {
    04778         res = src1 >> -(int32)src2;
    04779       } else {
    04780         res = src1 << src2;
    04781       }
    04782       break;
    04783
    04784     case 0x06:
    04785       if ((int32)src2 < 0) {
    04786         res = (int32)src1 >> -(int32)src2;
    04787       } else {
    04788         res = (int32)src1 << src2;
    04789       }
    04790       break;
    04791
    04792     case 0x07: // Bitwise AND
    04793       res = src1 & src2;
    04794       break;
    04795
    04796     case 0x08: // Bitwise OR
    04797       res = src1 | src2;
    04798       break;
    04799
    04800     case 0x09: // Unsigned division
    04801       if (src2 == 0) {
    04802         res = src1;
    04803       } else {
    04804         res = src1 / src2;
    04805       }
    04806       break;
    04807
    04808     case 0x0A: // Signed divison
    04809       if (src2 == 0) {
    04810         res = src1;
    04811       } else {
    04812         res = (int32)src1 / (int32)src2;
    04813       }
    04814       break;
    04815
    04816     case 0x0B: // Unsigned modulo
    04817       if (src2 == 0) {
    04818         res = src1;
    04819       } else {
    04820         res = src1 % src2;
    04821       }
    04822       break;
    04823
    04824     case 0x0C: // Signed modulo
    04825       if (src2 == 0) {
    04826         res = src1;
    04827       } else {
    04828         res = (int32)src1 % (int32)src2;
    04829       }
    04830       break;
    04831
    04832     default: grfmsg(0, "ParamSet: Unknown operation %d, skipping", oper); return;
    04833   }
    04834
    04835   switch (target) {
    04836     case 0x8E: // Y-Offset for train sprites
    04837       _traininfo_vehicle_pitch = res;
    04838       break;
    04839
    04840     case 0x8F: { // Rail track type cost factors
    04841       extern RailtypeInfo _railtypes[RAILTYPE_END];
    04842       _railtypes[RAILTYPE_RAIL].cost_multiplier = GB(res, 0, 8);
    04843       if (_settings_game.vehicle.disable_elrails) {
    04844         _railtypes[RAILTYPE_ELECTRIC].cost_multiplier = GB(res, 0, 8);
    04845         _railtypes[RAILTYPE_MONO].cost_multiplier = GB(res, 8, 8);
    04846       } else {
    04847         _railtypes[RAILTYPE_ELECTRIC].cost_multiplier = GB(res, 8, 8);
    04848         _railtypes[RAILTYPE_MONO].cost_multiplier = GB(res, 16, 8);
    04849       }
    04850       _railtypes[RAILTYPE_MAGLEV].cost_multiplier = GB(res, 16, 8);
    04851       break;
    04852     }
    04853
    04854     /* @todo implement */
    04855     case 0x93: // Tile refresh offset to left
    04856     case 0x94: // Tile refresh offset to right
    04857     case 0x95: // Tile refresh offset upwards
    04858     case 0x96: // Tile refresh offset downwards
    04859     case 0x97: // Snow line height
    04860     case 0x99: // Global ID offset
    04861       grfmsg(7, "ParamSet: Skipping unimplemented target 0x%02X", target);
    04862       break;
    04863
    04864     case 0x9E: // Miscellaneous GRF features
    04865       _misc_grf_features = res;
    04866       /* Set train list engine width */
    04867       _traininfo_vehicle_width = HasGrfMiscBit(GMB_TRAIN_WIDTH_32_PIXELS) ? 32 : 29;
    04868       break;
    04869
    04870     case 0x9F: // locale-dependent settings
    04871       grfmsg(7, "ParamSet: Skipping unimplemented target 0x%02X", target);
    04872       break;
    04873
    04874     default:
    04875       if (target < 0x80) {
    04876         _cur_grffile->param[target] = res;
    04877         if (target + 1U > _cur_grffile->param_end) _cur_grffile->param_end = target + 1;
    04878       } else {
    04879         grfmsg(7, "ParamSet: Skipping unknown target 0x%02X", target);
    04880       }
    04881       break;
    04882   }
    04883 }
    04884
    04885 /* Action 0x0E (GLS_SAFETYSCAN) */
    04886 static void SafeGRFInhibit(byte *buf, size_t len)
    04887 {
    04888   /* <0E> <num> <grfids...>
    04889    *
    04890    * B num           Number of GRFIDs that follow
    04891    * D grfids        GRFIDs of the files to deactivate */
    04892
    04893   if (!check_length(len, 2, "GRFInhibit")) return;
    04894   buf++;
    04895   uint8 num = grf_load_byte(&buf);
    04896   if (!check_length(len, 2 + 4 * num, "GRFInhibit")) return;
    04897
    04898   for (uint i = 0; i < num; i++) {
    04899     uint32 grfid = grf_load_dword(&buf);
    04900
    04901     /* GRF is unsafe it if tries to deactivate other GRFs */
    04902     if (grfid != _cur_grfconfig->grfid) {
    04903       SetBit(_cur_grfconfig->flags, GCF_UNSAFE);
    04904
    04905       /* Skip remainder of GRF */
    04906       _skip_sprites = -1;
    04907
    04908       return;
    04909     }
    04910   }
    04911 }
    04912
    04913 /* Action 0x0E */
    04914 static void GRFInhibit(byte *buf, size_t len)
    04915 {
    04916   /* <0E> <num> <grfids...>
    04917    *
    04918    * B num           Number of GRFIDs that follow
    04919    * D grfids        GRFIDs of the files to deactivate */
    04920
    04921   if (!check_length(len, 2, "GRFInhibit")) return;
    04922   buf++;
    04923   uint8 num = grf_load_byte(&buf);
    04924   if (!check_length(len, 2 + 4 * num, "GRFInhibit")) return;
    04925
    04926   for (uint i = 0; i < num; i++) {
    04927     uint32 grfid = grf_load_dword(&buf);
    04928     GRFConfig *file = GetGRFConfig(grfid);
    04929
    04930     /* Unset activation flag */
    04931     if (file != NULL && file != _cur_grfconfig) {
    04932       grfmsg(2, "GRFInhibit: Deactivating file '%s'", file->filename);
    04933       file->status = GCS_DISABLED;
    04934     }
    04935   }
    04936 }
    04937
    04938 /* Action 0x0F */
    04939 static void FeatureTownName(byte *buf, size_t len)
    04940 {
    04941   /* <0F> <id> <style-name> <num-parts> <parts>
    04942    *
    04943    * B id          ID of this definition in bottom 7 bits (final definition if bit 7 set)
    04944    * V style-name  Name of the style (only for final definition)
    04945    * B num-parts   Number of parts in this definition
    04946    * V parts       The parts */
    04947
    04948   if (!check_length(len, 1, "FeatureTownName: definition ID")) return;
    04949   buf++; len--;
    04950
    04951   uint32 grfid = _cur_grffile->grfid;
    04952
    04953   GRFTownName *townname = AddGRFTownName(grfid);
    04954
    04955   byte id = grf_load_byte(&buf);
    04956   len--;
    04957   grfmsg(6, "FeatureTownName: definition 0x%02X", id & 0x7F);
    04958
    04959   if (HasBit(id, 7)) {
    04960     /* Final definition */
    04961     ClrBit(id, 7);
    04962     bool new_scheme = _cur_grffile->grf_version >= 7;
    04963
    04964     if (!check_length(len, 1, "FeatureTownName: lang_id")) return;
    04965     byte lang = grf_load_byte(&buf);
    04966     len--;
    04967
    04968     byte nb_gen = townname->nb_gen;
    04969     do {
    04970       ClrBit(lang, 7);
    04971
    04972       if (!check_length(len, 1, "FeatureTownName: style name")) return;
    04973       const char *name = grf_load_string(&buf, len);
    04974       len -= strlen(name) + 1;
    04975
    04976       char *lang_name = TranslateTTDPatchCodes(grfid, name);
    04977       grfmsg(6, "FeatureTownName: lang 0x%X -> '%s'", lang, lang_name);
    04978       free(lang_name);
    04979
    04980       townname->name[nb_gen] = AddGRFString(grfid, id, lang, new_scheme, name, STR_UNDEFINED);
    04981
    04982       if (!check_length(len, 1, "FeatureTownName: lang_id")) return;
    04983       lang = grf_load_byte(&buf);
    04984       len--;
    04985     } while (lang != 0);
    04986     townname->id[nb_gen] = id;
    04987     townname->nb_gen++;
    04988   }
    04989
    04990   if (!check_length(len, 1, "FeatureTownName: number of parts")) return;
    04991   byte nb = grf_load_byte(&buf);
    04992   len--;
    04993   grfmsg(6, "FeatureTownName: %u parts", nb);
    04994
    04995   townname->nbparts[id] = nb;
    04996   townname->partlist[id] = CallocT<NamePartList>(nb);
    04997
    04998   for (int i = 0; i < nb; i++) {
    04999     if (!check_length(len, 3, "FeatureTownName: parts header")) return;
    05000     byte nbtext =  grf_load_byte(&buf);
    05001     townname->partlist[id][i].bitstart  = grf_load_byte(&buf);
    05002     townname->partlist[id][i].bitcount  = grf_load_byte(&buf);
    05003     townname->partlist[id][i].maxprob   = 0;
    05004     townname->partlist[id][i].partcount = nbtext;
    05005     townname->partlist[id][i].parts     = CallocT<NamePart>(nbtext);
    05006     len -= 3;
    05007     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);
    05008
    05009     for (int j = 0; j < nbtext; j++) {
    05010       if (!check_length(len, 2, "FeatureTownName: part")) return;
    05011       byte prob = grf_load_byte(&buf);
    05012       len--;
    05013
    05014       if (HasBit(prob, 7)) {
    05015         byte ref_id = grf_load_byte(&buf);
    05016         len--;
    05017
    05018         if (townname->nbparts[ref_id] == 0) {
    05019           grfmsg(0, "FeatureTownName: definition 0x%02X doesn't exist, deactivating", ref_id);
    05020           DelGRFTownName(grfid);
    05021           _cur_grfconfig->status = GCS_DISABLED;
    05022           ClearTemporaryNewGRFData(_cur_grffile);
    05023           _skip_sprites = -1;
    05024           return;
    05025         }
    05026
    05027         grfmsg(6, "FeatureTownName: part %d, text %d, uses intermediate definition 0x%02X (with probability %d)", i, j, ref_id, prob & 0x7F);
    05028         townname->partlist[id][i].parts[j].data.id = ref_id;
    05029       } else {
    05030         const char *text = grf_load_string(&buf, len);
    05031         len -= strlen(text) + 1;
    05032         townname->partlist[id][i].parts[j].data.text = TranslateTTDPatchCodes(grfid, text);
    05033         grfmsg(6, "FeatureTownName: part %d, text %d, '%s' (with probability %d)", i, j, townname->partlist[id][i].parts[j].data.text, prob);
    05034       }
    05035       townname->partlist[id][i].parts[j].prob = prob;
    05036       townname->partlist[id][i].maxprob += GB(prob, 0, 7);
    05037     }
    05038     grfmsg(6, "FeatureTownName: part %d, total probability %d", i, townname->partlist[id][i].maxprob);
    05039   }
    05040 }
    05041
    05042 /* Action 0x10 */
    05043 static void DefineGotoLabel(byte *buf, size_t len)
    05044 {
    05045   /* <10> <label> [<comment>]
    05046    *
    05047    * B label      The label to define
    05048    * V comment    Optional comment - ignored */
    05049
    05050   if (!check_length(len, 1, "DefineGotoLabel")) return;
    05051   buf++; len--;
    05052
    05053   GRFLabel *label = MallocT<GRFLabel>(1);
    05054   label->label    = grf_load_byte(&buf);
    05055   label->nfo_line = _nfo_line;
    05056   label->pos      = FioGetPos();
    05057   label->next     = NULL;
    05058
    05059   /* Set up a linked list of goto targets which we will search in an Action 0x7/0x9 */
    05060   if (_cur_grffile->label == NULL) {
    05061     _cur_grffile->label = label;
    05062   } else {
    05063     /* Attach the label to the end of the list */
    05064     GRFLabel *l;
    05065     for (l = _cur_grffile->label; l->next != NULL; l = l->next) {}
    05066     l->next = label;
    05067   }
    05068
    05069   grfmsg(2, "DefineGotoLabel: GOTO target with label 0x%02X", label->label);
    05070 }
    05071
    05072 /* Action 0x11 */
    05073 static void GRFSound(byte *buf, size_t len)
    05074 {
    05075   /* <11> <num>
    05076    *
    05077    * W num      Number of sound files that follow */
    05078
    05079   if (!check_length(len, 1, "GRFSound")) return;
    05080   buf++;
    05081   uint16 num = grf_load_word(&buf);
    05082
    05083   _grf_data_blocks = num;
    05084   _grf_data_type   = GDT_SOUND;
    05085
    05086   if (_cur_grffile->sound_offset == 0) _cur_grffile->sound_offset = GetNumSounds();
    05087 }
    05088
    05089 /* Action 0x11 (SKIP) */
    05090 static void SkipAct11(byte *buf, size_t len)
    05091 {
    05092   /* <11> <num>
    05093    *
    05094    * W num      Number of sound files that follow */
    05095
    05096   if (!check_length(len, 1, "SkipAct11")) return;
    05097   buf++;
    05098   _skip_sprites = grf_load_word(&buf);
    05099
    05100   grfmsg(3, "SkipAct11: Skipping %d sprites", _skip_sprites);
    05101 }
    05102
    05103 static void ImportGRFSound(byte *buf, int len)
    05104 {
    05105   const GRFFile *file;
    05106   FileEntry *se = AllocateFileEntry();
    05107   uint32 grfid = grf_load_dword(&buf);
    05108   uint16 sound = grf_load_word(&buf);
    05109
    05110   file = GetFileByGRFID(grfid);
    05111   if (file == NULL || file->sound_offset == 0) {
    05112     grfmsg(1, "ImportGRFSound: Source file not available");
    05113     return;
    05114   }
    05115
    05116   if (file->sound_offset + sound >= GetNumSounds()) {
    05117     grfmsg(1, "ImportGRFSound: Sound effect %d is invalid", sound);
    05118     return;
    05119   }
    05120
    05121   grfmsg(2, "ImportGRFSound: Copying sound %d (%d) from file %X", sound, file->sound_offset + sound, grfid);
    05122
    05123   *se = *GetSound(file->sound_offset + sound);
    05124
    05125   /* Reset volume and priority, which TTDPatch doesn't copy */
    05126   se->volume   = 128;
    05127   se->priority = 0;
    05128 }
    05129
    05130 /* 'Action 0xFE' */
    05131 static void GRFImportBlock(byte *buf, int len)
    05132 {
    05133   if (_grf_data_blocks == 0) {
    05134     grfmsg(2, "GRFImportBlock: Unexpected import block, skipping");
    05135     return;
    05136   }
    05137
    05138   buf++;
    05139
    05140   _grf_data_blocks--;
    05141
    05142   /* XXX 'Action 0xFE' isn't really specified. It is only mentioned for
    05143    * importing sounds, so this is probably all wrong... */
    05144   if (grf_load_byte(&buf) != _grf_data_type) {
    05145     grfmsg(1, "GRFImportBlock: Import type mismatch");
    05146   }
    05147
    05148   switch (_grf_data_type) {
    05149     case GDT_SOUND: ImportGRFSound(buf, len - 1); break;
    05150     default: NOT_REACHED(); break;
    05151   }
    05152 }
    05153
    05154 static void LoadGRFSound(byte *buf, uint len)
    05155 {
    05156   byte *buf_start = buf;
    05157
    05158   /* Allocate a sound entry. This is done even if the data is not loaded
    05159    * so that the indices used elsewhere are still correct. */
    05160   FileEntry *se = AllocateFileEntry();
    05161
    05162   if (grf_load_dword(&buf) != BSWAP32('RIFF')) {
    05163     grfmsg(1, "LoadGRFSound: Missing RIFF header");
    05164     return;
    05165   }
    05166
    05167   uint32 total_size = grf_load_dword(&buf);
    05168   if (total_size > len + 8) {
    05169     grfmsg(1, "LoadGRFSound: RIFF was truncated");
    05170     return;
    05171   }
    05172
    05173   if (grf_load_dword(&buf) != BSWAP32('WAVE')) {
    05174     grfmsg(1, "LoadGRFSound: Invalid RIFF type");
    05175     return;
    05176   }
    05177
    05178   while (total_size >= 8) {
    05179     uint32 tag  = grf_load_dword(&buf);
    05180     uint32 size = grf_load_dword(&buf);
    05181     total_size -= 8;
    05182     if (total_size < size) {
    05183       grfmsg(1, "LoadGRFSound: Invalid RIFF");
    05184       return;
    05185     }
    05186     total_size -= size;
    05187
    05188     switch (tag) {
    05189       case ' tmf': // 'fmt '
    05190         /* Audio format, must be 1 (PCM) */
    05191         if (size < 16 || grf_load_word(&buf) != 1) {
    05192           grfmsg(1, "LoadGRFSound: Invalid audio format");
    05193           return;
    05194         }
    05195         se->channels = grf_load_word(&buf);
    05196         se->rate = grf_load_dword(&buf);
    05197         grf_load_dword(&buf);
    05198         grf_load_word(&buf);
    05199         se->bits_per_sample = grf_load_word(&buf);
    05200
    05201         /* The rest will be skipped */
    05202         size -= 16;
    05203         break;
    05204
    05205       case 'atad': // 'data'
    05206         se->file_size   = size;
    05207         se->file_offset = FioGetPos() - (len - (buf - buf_start));
    05208         se->file_slot   = _file_index;
    05209
    05210         /* Set default volume and priority */
    05211         se->volume = 0x80;
    05212         se->priority = 0;
    05213
    05214         grfmsg(2, "LoadGRFSound: channels %u, sample rate %u, bits per sample %u, length %u", se->channels, se->rate, se->bits_per_sample, size);
    05215         return; // the fmt chunk has to appear before data, so we are finished
    05216
    05217       default:
    05218         /* Skip unknown chunks */
    05219         break;
    05220     }
    05221
    05222     /* Skip rest of chunk */
    05223     for (; size > 0; size--) grf_load_byte(&buf);
    05224   }
    05225
    05226   grfmsg(1, "LoadGRFSound: RIFF does not contain any sound data");
    05227
    05228   /* Clear everything that was read */
    05229   MemSetT(se, 0);
    05230 }
    05231
    05232 /* Action 0x12 */
    05233 static void LoadFontGlyph(byte *buf, size_t len)
    05234 {
    05235   /* <12> <num_def> <font_size> <num_char> <base_char>
    05236    *
    05237    * B num_def      Number of definitions
    05238    * B font_size    Size of font (0 = normal, 1 = small, 2 = large)
    05239    * B num_char     Number of consecutive glyphs
    05240    * W base_char    First character index */
    05241
    05242   buf++; len--;
    05243   if (!check_length(len, 1, "LoadFontGlyph")) return;
    05244
    05245   uint8 num_def = grf_load_byte(&buf);
    05246
    05247   if (!check_length(len, 1 + num_def * 4, "LoadFontGlyph")) return;
    05248
    05249   for (uint i = 0; i < num_def; i++) {
    05250     FontSize size    = (FontSize)grf_load_byte(&buf);
    05251     uint8  num_char  = grf_load_byte(&buf);
    05252     uint16 base_char = grf_load_word(&buf);
    05253
    05254     grfmsg(7, "LoadFontGlyph: Loading %u glyph(s) at 0x%04X for size %u", num_char, base_char, size);
    05255
    05256     for (uint c = 0; c < num_char; c++) {
    05257       SetUnicodeGlyph(size, base_char + c, _cur_spriteid);
    05258       _nfo_line++;
    05259       LoadNextSprite(_cur_spriteid++, _file_index, _nfo_line);
    05260     }
    05261   }
    05262 }
    05263
    05264 /* Action 0x12 (SKIP) */
    05265 static void SkipAct12(byte *buf, size_t len)
    05266 {
    05267   /* <12> <num_def> <font_size> <num_char> <base_char>
    05268    *
    05269    * B num_def      Number of definitions
    05270    * B font_size    Size of font (0 = normal, 1 = small, 2 = large)
    05271    * B num_char     Number of consecutive glyphs
    05272    * W base_char    First character index */
    05273
    05274   buf++; len--;
    05275   if (!check_length(len, 1, "SkipAct12")) return;
    05276   uint8 num_def = grf_load_byte(&buf);
    05277
    05278   if (!check_length(len, 1 + num_def * 4, "SkipAct12")) return;
    05279
    05280   for (uint i = 0; i < num_def; i++) {
    05281     /* Ignore 'size' byte */
    05282     grf_load_byte(&buf);
    05283
    05284     /* Sum up number of characters */
    05285     _skip_sprites += grf_load_byte(&buf);
    05286
    05287     /* Ignore 'base_char' word */
    05288     grf_load_word(&buf);
    05289   }
    05290
    05291   grfmsg(3, "SkipAct12: Skipping %d sprites", _skip_sprites);
    05292 }
    05293
    05294 /* Action 0x13 */
    05295 static void TranslateGRFStrings(byte *buf, size_t len)
    05296 {
    05297   /* <13> <grfid> <num-ent> <offset> <text...>
    05298    *
    05299    * 4*B grfid     The GRFID of the file whose texts are to be translated
    05300    * B   num-ent   Number of strings
    05301    * W   offset    First text ID
    05302    * S   text...   Zero-terminated strings */
    05303
    05304   buf++; len--;
    05305   if (!check_length(len, 7, "TranslateGRFString")) return;
    05306
    05307   uint32 grfid = grf_load_dword(&buf);
    05308   const GRFConfig *c = GetGRFConfig(grfid);
    05309   if (c == NULL || (c->status != GCS_INITIALISED && c->status != GCS_ACTIVATED)) {
    05310     grfmsg(7, "TranslateGRFStrings: GRFID 0x%08x unknown, skipping action 13", BSWAP32(grfid));
    05311     return;
    05312   }
    05313
    05314   if (c->status == GCS_INITIALISED) {
    05315     /* If the file is not active but will be activated later, give an error
    05316      * and disable this file. */
    05317     GRFError *error = CallocT<GRFError>(1);
    05318
    05319     char tmp[256];
    05320     GetString(tmp, STR_NEWGRF_ERROR_AFTER_TRANSLATED_FILE, lastof(tmp));
    05321     error->data = strdup(tmp);
    05322
    05323     error->message  = STR_NEWGRF_ERROR_LOAD_AFTER;
    05324     error->severity = STR_NEWGRF_ERROR_MSG_FATAL;
    05325
    05326     if (_cur_grfconfig->error != NULL) free(_cur_grfconfig->error);
    05327     _cur_grfconfig->error = error;
    05328
    05329     _cur_grfconfig->status = GCS_DISABLED;
    05330     ClearTemporaryNewGRFData(_cur_grffile);
    05331     _skip_sprites = -1;
    05332     return;
    05333   }
    05334
    05335   byte num_strings = grf_load_byte(&buf);
    05336   uint16 first_id  = grf_load_word(&buf);
    05337
    05338   if (!((first_id >= 0xD000 && first_id + num_strings <= 0xD3FF) || (first_id >= 0xDC00 && first_id + num_strings <= 0xDCFF))) {
    05339     grfmsg(7, "TranslateGRFStrings: Attempting to set out-of-range string IDs in action 13 (first: 0x%4X, number: 0x%2X)", first_id, num_strings);
    05340     return;
    05341   }
    05342
    05343   len -= 7;
    05344
    05345   for (uint i = 0; i < num_strings && len > 0; i++) {
    05346     const char *string   = grf_load_string(&buf, len);
    05347     size_t string_length = strlen(string) + 1;
    05348
    05349     len -= (int)string_length;
    05350
    05351     if (string_length == 1) {
    05352       grfmsg(7, "TranslateGRFString: Ignoring empty string.");
    05353       continue;
    05354     }
    05355
    05356     /* Since no language id is supplied this string has to be added as a
    05357      * generic string, thus the language id of 0x7F. For this to work
    05358      * new_scheme has to be true as well. A language id of 0x7F will be
    05359      * overridden by a non-generic id, so this will not change anything if
    05360      * a string has been provided specifically for this language. */
    05361     AddGRFString(grfid, first_id + i, 0x7F, true, string, STR_UNDEFINED);
    05362   }
    05363 }
    05364
    05365 /* 'Action 0xFF' */
    05366 static void GRFDataBlock(byte *buf, int len)
    05367 {
    05368   /* <FF> <name_len> <name> '\0' <data> */
    05369
    05370   if (_grf_data_blocks == 0) {
    05371     grfmsg(2, "GRFDataBlock: unexpected data block, skipping");
    05372     return;
    05373   }
    05374
    05375   if (!check_length(len, 3, "GRFDataBlock")) return;
    05376
    05377   buf++;
    05378   uint8 name_len = grf_load_byte(&buf);
    05379   const char *name = (const char *)buf;
    05380   buf += name_len;
    05381
    05382   /* Test string termination */
    05383   if (grf_load_byte(&buf) != 0) {
    05384     grfmsg(2, "GRFDataBlock: Name not properly terminated");
    05385     return;
    05386   }
    05387
    05388   if (!check_length(len, 3 + name_len, "GRFDataBlock")) return;
    05389
    05390   grfmsg(2, "GRFDataBlock: block name '%s'...", name);
    05391
    05392   _grf_data_blocks--;
    05393
    05394   switch (_grf_data_type) {
    05395     case GDT_SOUND: LoadGRFSound(buf, len - name_len - 3); break;
    05396     default: NOT_REACHED(); break;
    05397   }
    05398 }
    05399
    05400
    05401 /* Used during safety scan on unsafe actions */
    05402 static void GRFUnsafe(byte *buf, size_t len)
    05403 {
    05404   SetBit(_cur_grfconfig->flags, GCF_UNSAFE);
    05405
    05406   /* Skip remainder of GRF */
    05407   _skip_sprites = -1;
    05408 }
    05409
    05410
    05411 static void InitializeGRFSpecial()
    05412 {
    05413   _ttdpatch_flags[0] =  ((_settings_game.station.always_small_airport ? 1 : 0) << 0x0C)  // keepsmallairport
    05414                      |                                                 (1 << 0x0D)  // newairports
    05415                      |                                                 (1 << 0x0E)  // largestations
    05416                      |      ((_settings_game.construction.longbridges ? 1 : 0) << 0x0F)  // longbridges
    05417                      |                                                 (0 << 0x10)  // loadtime
    05418                      |                                                 (1 << 0x12)  // presignals
    05419                      |                                                 (1 << 0x13)  // extpresignals
    05420                      | ((_settings_game.vehicle.never_expire_vehicles ? 1 : 0) << 0x16)  // enginespersist
    05421                      |                                                 (1 << 0x1B)  // multihead
    05422                      |                                                 (1 << 0x1D)  // lowmemory
    05423                      |                                                 (1 << 0x1E); // generalfixes
    05424
    05425   _ttdpatch_flags[1] =   ((_settings_game.economy.station_noise_level ? 1 : 0) << 0x07)  // moreairports - based on units of noise
    05426                      |        ((_settings_game.vehicle.mammoth_trains ? 1 : 0) << 0x08)  // mammothtrains
    05427                      |                                                 (1 << 0x09)  // trainrefit
    05428                      |                                                 (0 << 0x0B)  // subsidiaries
    05429                      |         ((_settings_game.order.gradual_loading ? 1 : 0) << 0x0C)  // gradualloading
    05430                      |                                                 (1 << 0x12)  // unifiedmaglevmode - set bit 0 mode. Not revelant to OTTD
    05431                      |                                                 (1 << 0x13)  // unifiedmaglevmode - set bit 1 mode
    05432                      |                                                 (1 << 0x14)  // bridgespeedlimits
    05433                      |                                                 (1 << 0x16)  // eternalgame
    05434                      |                                                 (1 << 0x17)  // newtrains
    05435                      |                                                 (1 << 0x18)  // newrvs
    05436                      |                                                 (1 << 0x19)  // newships
    05437                      |                                                 (1 << 0x1A)  // newplanes
    05438                      |      ((_settings_game.construction.signal_side ? 1 : 0) << 0x1B)  // signalsontrafficside
    05439                      |       ((_settings_game.vehicle.disable_elrails ? 0 : 1) << 0x1C); // electrifiedrailway
    05440
    05441   _ttdpatch_flags[2] =                                                 (1 << 0x01)  // loadallgraphics - obsolote
    05442                      |                                                 (1 << 0x03)  // semaphores
    05443                      |                                                 (0 << 0x0B)  // enhancedgui
    05444                      |                                                 (0 << 0x0C)  // newagerating
    05445                      |  ((_settings_game.construction.build_on_slopes ? 1 : 0) << 0x0D)  // buildonslopes
    05446                      |                                                 (1 << 0x0E)  // fullloadany
    05447                      |                                                 (1 << 0x0F)  // planespeed
    05448                      |                                                 (0 << 0x10)  // moreindustriesperclimate - obsolete
    05449                      |                                                 (0 << 0x11)  // moretoylandfeatures
    05450                      |                                                 (1 << 0x12)  // newstations
    05451                      |                                                 (1 << 0x13)  // tracktypecostdiff
    05452                      |                                                 (1 << 0x14)  // manualconvert
    05453                      |  ((_settings_game.construction.build_on_slopes ? 1 : 0) << 0x15)  // buildoncoasts
    05454                      |                                                 (1 << 0x16)  // canals
    05455                      |                                                 (1 << 0x17)  // newstartyear
    05456                      |    ((_settings_game.vehicle.freight_trains > 1 ? 1 : 0) << 0x18)  // freighttrains
    05457                      |                                                 (1 << 0x19)  // newhouses
    05458                      |                                                 (1 << 0x1A)  // newbridges
    05459                      |                                                 (1 << 0x1B)  // newtownnames
    05460                      |                                                 (1 << 0x1C)  // moreanimation
    05461                      |    ((_settings_game.vehicle.wagon_speed_limits ? 1 : 0) << 0x1D)  // wagonspeedlimits
    05462                      |                                                 (1 << 0x1E)  // newshistory
    05463                      |                                                 (0 << 0x1F); // custombridgeheads
    05464
    05465   _ttdpatch_flags[3] =                                                 (0 << 0x00)  // newcargodistribution
    05466                      |                                                 (1 << 0x01)  // windowsnap
    05467                      | ((_settings_game.economy.allow_town_roads || _generating_world ? 0 : 1) << 0x02)  // townbuildnoroad
    05468                      |                                                 (1 << 0x03)  // pathbasedsignalling
    05469                      |                                                 (0 << 0x04)  // aichoosechance
    05470                      |                                                 (1 << 0x05)  // resolutionwidth
    05471                      |                                                 (1 << 0x06)  // resolutionheight
    05472                      |                                                 (1 << 0x07)  // newindustries
    05473                      |           ((_settings_game.order.improved_load ? 1 : 0) << 0x08)  // fifoloading
    05474                      |                                                 (0 << 0x09)  // townroadbranchprob
    05475                      |                                                 (0 << 0x0A)  // tempsnowline
    05476                      |                                                 (1 << 0x0B)  // newcargo
    05477                      |                                                 (1 << 0x0C)  // enhancemultiplayer
    05478                      |                                                 (1 << 0x0D)  // onewayroads
    05479                      |   ((_settings_game.station.nonuniform_stations ? 1 : 0) << 0x0E)  // irregularstations
    05480                      |                                                 (1 << 0x0F)  // statistics
    05481                      |                                                 (1 << 0x10)  // newsounds
    05482                      |                                                 (1 << 0x11)  // autoreplace
    05483                      |                                                 (1 << 0x12)  // autoslope
    05484                      |                                                 (0 << 0x13)  // followvehicle
    05485                      |                                                 (1 << 0x14)  // trams
    05486                      |                                                 (0 << 0x15)  // enhancetunnels
    05487                      |                                                 (1 << 0x16)  // shortrvs
    05488                      |                                                 (1 << 0x17)  // articulatedrvs
    05489                      |       ((_settings_game.vehicle.dynamic_engines ? 1 : 0) << 0x18)  // dynamic engines
    05490                      |                                                 (1 << 0x1E)  // variablerunningcosts
    05491                      |                                                 (1 << 0x1F); // any switch is on
    05492 }
    05493
    05494 static void ResetCustomStations()
    05495 {
    05496   for (GRFFile *file = _first_grffile; file != NULL; file = file->next) {
    05497     if (file->stations == NULL) continue;
    05498     for (uint i = 0; i < MAX_STATIONS; i++) {
    05499       if (file->stations[i] == NULL) continue;
    05500       StationSpec *statspec = file->stations[i];
    05501
    05502       /* Release renderdata, if it wasn't copied from another custom station spec  */
    05503       if (!statspec->copied_renderdata) {
    05504         for (uint t = 0; t < statspec->tiles; t++) {
    05505           free((void*)statspec->renderdata[t].seq);
    05506         }
    05507         free(statspec->renderdata);
    05508       }
    05509
    05510       /* Release platforms and layouts */
    05511       if (!statspec->copied_layouts) {
    05512         for (uint l = 0; l < statspec->lengths; l++) {
    05513           for (uint p = 0; p < statspec->platforms[l]; p++) {
    05514             free(statspec->layouts[l][p]);
    05515           }
    05516           free(statspec->layouts[l]);
    05517         }
    05518         free(statspec->layouts);
    05519         free(statspec->platforms);
    05520       }
    05521
    05522       /* Release this station */
    05523       free(statspec);
    05524     }
    05525
    05526     /* Free and reset the station data */
    05527     free(file->stations);
    05528     file->stations = NULL;
    05529   }
    05530 }
    05531
    05532 static void ResetCustomHouses()
    05533 {
    05534   GRFFile *file;
    05535   uint i;
    05536
    05537   for (file = _first_grffile; file != NULL; file = file->next) {
    05538     if (file->housespec == NULL) continue;
    05539     for (i = 0; i < HOUSE_MAX; i++) {
    05540       free(file->housespec[i]);
    05541     }
    05542
    05543     free(file->housespec);
    05544     file->housespec = NULL;
    05545   }
    05546 }
    05547
    05548 static void ResetCustomIndustries()
    05549 {
    05550   GRFFile *file;
    05551
    05552   for (file = _first_grffile; file != NULL; file = file->next) {
    05553     uint i;
    05554     /* We are verifiying both tiles and industries specs loaded from the grf file
    05555      * First, let's deal with industryspec */
    05556     if (file->industryspec != NULL) {
    05557
    05558       for (i = 0; i < NUM_INDUSTRYTYPES; i++) {
    05559         IndustrySpec *ind = file->industryspec[i];
    05560
    05561         if (ind != NULL) {
    05562           /* We need to remove the sounds array */
    05563           if (HasBit(ind->cleanup_flag, CLEAN_RANDOMSOUNDS)) {
    05564             free((void*)ind->random_sounds);
    05565           }
    05566
    05567           /* We need to remove the tiles layouts */
    05568           if (HasBit(ind->cleanup_flag, CLEAN_TILELSAYOUT) && ind->table != NULL) {
    05569             for (int j = 0; j < ind->num_table; j++) {
    05570               /* remove the individual layouts */
    05571               if (ind->table[j] != NULL) {
    05572                 free((IndustryTileTable*)ind->table[j]);
    05573               }
    05574             }
    05575             /* remove the layouts pointers */
    05576             free((IndustryTileTable**)ind->table);
    05577             ind->table = NULL;
    05578           }
    05579
    05580           free(ind);
    05581           ind = NULL;
    05582         }
    05583       }
    05584
    05585       free(file->industryspec);
    05586       file->industryspec = NULL;
    05587     }
    05588
    05589     if (file->indtspec != NULL) {
    05590       for (i = 0; i < NUM_INDUSTRYTILES; i++) {
    05591         if (file->indtspec[i] != NULL) {
    05592           free(file->indtspec[i]);
    05593           file->indtspec[i] = NULL;
    05594         }
    05595       }
    05596
    05597       free(file->indtspec);
    05598       file->indtspec = NULL;
    05599     }
    05600   }
    05601 }
    05602
    05603 static void ResetNewGRF()
    05604 {
    05605   GRFFile *next;
    05606
    05607   for (GRFFile *f = _first_grffile; f != NULL; f = next) {
    05608     next = f->next;
    05609
    05610     free(f->filename);
    05611     free(f->cargo_list);
    05612     free(f->railtype_list);
    05613     free(f);
    05614   }
    05615
    05616   _first_grffile = NULL;
    05617   _cur_grffile   = NULL;
    05618 }
    05619
    05620 static void ResetNewGRFErrors()
    05621 {
    05622   for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
    05623     if (!HasBit(c->flags, GCF_COPY) && c->error != NULL) {
    05624       free(c->error->custom_message);
    05625       free(c->error->data);
    05626       free(c->error);
    05627       c->error = NULL;
    05628     }
    05629   }
    05630 }
    05631
    05636 static void ResetNewGRFData()
    05637 {
    05638   CleanUpStrings();
    05639   CleanUpGRFTownNames();
    05640
    05641   /* Copy/reset original engine info data */
    05642   SetupEngines();
    05643
    05644   /* Copy/reset original bridge info data */
    05645   ResetBridges();
    05646
    05647   /* Reset rail type information */
    05648   ResetRailTypes();
    05649
    05650   /* Allocate temporary refit/cargo class data */
    05651   _gted = CallocT<GRFTempEngineData>(GetEnginePoolSize());
    05652
    05653   /* Reset GRM reservations */
    05654   memset(&_grm_engines, 0, sizeof(_grm_engines));
    05655   memset(&_grm_cargos, 0, sizeof(_grm_cargos));
    05656
    05657   /* Reset generic feature callback lists */
    05658   ResetGenericCallbacks();
    05659
    05660   /* Reset price base data */
    05661   ResetPriceBaseMultipliers();
    05662
    05663   /* Reset the curencies array */
    05664   ResetCurrencies();
    05665
    05666   /* Reset the house array */
    05667   ResetCustomHouses();
    05668   ResetHouses();
    05669
    05670   /* Reset the industries structures*/
    05671   ResetCustomIndustries();
    05672   ResetIndustries();
    05673
    05674   /* Reset station classes */
    05675   ResetStationClasses();
    05676   ResetCustomStations();
    05677
    05678   /* Reset canal sprite groups and flags */
    05679   memset(_water_feature, 0, sizeof(_water_feature));
    05680
    05681   /* Reset the snowline table. */
    05682   ClearSnowLine();
    05683
    05684   /* Reset NewGRF files */
    05685   ResetNewGRF();
    05686
    05687   /* Reset NewGRF errors. */
    05688   ResetNewGRFErrors();
    05689
    05690   /* Set up the default cargo types */
    05691   SetupCargoForClimate(_settings_game.game_creation.landscape);
    05692
    05693   /* Reset misc GRF features and train list display variables */
    05694   _misc_grf_features = 0;
    05695   _traininfo_vehicle_pitch = 0;
    05696   _traininfo_vehicle_width = 29;
    05697
    05698   _loaded_newgrf_features.has_2CC           = false;
    05699   _loaded_newgrf_features.has_newhouses     = false;
    05700   _loaded_newgrf_features.has_newindustries = false;
    05701   _loaded_newgrf_features.shore             = SHORE_REPLACE_NONE;
    05702
    05703   /* Clear all GRF overrides */
    05704   _grf_id_overrides.clear();
    05705
    05706   InitializeSoundPool();
    05707   InitializeSpriteGroupPool();
    05708 }
    05709
    05710 static void BuildCargoTranslationMap()
    05711 {
    05712   memset(_cur_grffile->cargo_map, 0xFF, sizeof(_cur_grffile->cargo_map));
    05713
    05714   for (CargoID c = 0; c < NUM_CARGO; c++) {
    05715     const CargoSpec *cs = GetCargo(c);
    05716     if (!cs->IsValid()) continue;
    05717
    05718     if (_cur_grffile->cargo_max == 0) {
    05719       /* Default translation table, so just a straight mapping to bitnum */
    05720       _cur_grffile->cargo_map[c] = cs->bitnum;
    05721     } else {
    05722       /* Check the translation table for this cargo's label */
    05723       for (uint i = 0; i < _cur_grffile->cargo_max; i++) {
    05724         if (cs->label == _cur_grffile->cargo_list[i]) {
    05725           _cur_grffile->cargo_map[c] = i;
    05726           break;
    05727         }
    05728       }
    05729     }
    05730   }
    05731 }
    05732
    05733 static void InitNewGRFFile(const GRFConfig *config, int sprite_offset)
    05734 {
    05735   GRFFile *newfile = GetFileByFilename(config->filename);
    05736   if (newfile != NULL) {
    05737     /* We already loaded it once. */
    05738     newfile->sprite_offset = sprite_offset;
    05739     _cur_grffile = newfile;
    05740     return;
    05741   }
    05742
    05743   newfile = CallocT<GRFFile>(1);
    05744
    05745   if (newfile == NULL) error ("Out of memory");
    05746
    05747   newfile->filename = strdup(config->filename);
    05748   newfile->sprite_offset = sprite_offset;
    05749
    05750   /* Copy the initial parameter list */
    05751   assert(lengthof(newfile->param) == lengthof(config->param) && lengthof(config->param) == 0x80);
    05752   newfile->param_end = config->num_params;
    05753   memcpy(newfile->param, config->param, sizeof(newfile->param));
    05754
    05755   if (_first_grffile == NULL) {
    05756     _cur_grffile = newfile;
    05757     _first_grffile = newfile;
    05758   } else {
    05759     _cur_grffile->next = newfile;
    05760     _cur_grffile = newfile;
    05761   }
    05762 }
    05763
    05764
    05767 static const CargoLabel _default_refitmasks_rail[] = {
    05768   'PASS', 'COAL', 'MAIL', 'LVST', 'GOOD', 'GRAI', 'WHEA', 'MAIZ', 'WOOD',
    05769   'IORE', 'STEL', 'VALU', 'GOLD', 'DIAM', 'PAPR', 'FOOD', 'FRUT', 'CORE',
    05770   'WATR', 'SUGR', 'TOYS', 'BATT', 'SWET', 'TOFF', 'COLA', 'CTCD', 'BUBL',
    05771   'PLST', 'FZDR',
    05772   0 };
    05773
    05774 static const CargoLabel _default_refitmasks_road[] = {
    05775   0 };
    05776
    05777 static const CargoLabel _default_refitmasks_ships[] = {
    05778   'COAL', 'MAIL', 'LVST', 'GOOD', 'GRAI', 'WHEA', 'MAIZ', 'WOOD', 'IORE',
    05779   'STEL', 'VALU', 'GOLD', 'DIAM', 'PAPR', 'FOOD', 'FRUT', 'CORE', 'WATR',
    05780   'RUBR', 'SUGR', 'TOYS', 'BATT', 'SWET', 'TOFF', 'COLA', 'CTCD', 'BUBL',
    05781   'PLST', 'FZDR',
    05782   0 };
    05783
    05784 static const CargoLabel _default_refitmasks_aircraft[] = {
    05785   'PASS', 'MAIL', 'GOOD', 'VALU', 'GOLD', 'DIAM', 'FOOD', 'FRUT', 'SUGR',
    05786   'TOYS', 'BATT', 'SWET', 'TOFF', 'COLA', 'CTCD', 'BUBL', 'PLST', 'FZDR',
    05787   0 };
    05788
    05789 static const CargoLabel *_default_refitmasks[] = {
    05790   _default_refitmasks_rail,
    05791   _default_refitmasks_road,
    05792   _default_refitmasks_ships,
    05793   _default_refitmasks_aircraft,
    05794 };
    05795
    05796
    05800 static void CalculateRefitMasks()
    05801 {
    05802   Engine *e;
    05803
    05804   FOR_ALL_ENGINES(e) {
    05805     EngineID engine = e->index;
    05806     EngineInfo *ei = &e->info;
    05807     uint32 mask = 0;
    05808     uint32 not_mask = 0;
    05809     uint32 xor_mask = 0;
    05810
    05811     /* Did the newgrf specify any refitting? If not, use defaults. */
    05812     if (_gted[engine].refitmask_valid) {
    05813       if (ei->refit_mask != 0) {
    05814         const GRFFile *file = e->grffile;
    05815         if (file != NULL && file->cargo_max != 0) {
    05816           /* Apply cargo translation table to the refit mask */
    05817           uint num_cargo = min(32, file->cargo_max);
    05818           for (uint i = 0; i < num_cargo; i++) {
    05819             if (!HasBit(ei->refit_mask, i)) continue;
    05820
    05821             CargoID c = GetCargoIDByLabel(file->cargo_list[i]);
    05822             if (c == CT_INVALID) continue;
    05823
    05824             SetBit(xor_mask, c);
    05825           }
    05826         } else {
    05827           /* No cargo table, so use the cargo bitnum values */
    05828           for (CargoID c = 0; c < NUM_CARGO; c++) {
    05829             const CargoSpec *cs = GetCargo(c);
    05830             if (!cs->IsValid()) continue;
    05831
    05832             if (HasBit(ei->refit_mask, cs->bitnum)) SetBit(xor_mask, c);
    05833           }
    05834         }
    05835       }
    05836
    05837       if (_gted[engine].cargo_allowed != 0) {
    05838         /* Build up the list of cargo types from the set cargo classes. */
    05839         for (CargoID i = 0; i < NUM_CARGO; i++) {
    05840           const CargoSpec *cs = GetCargo(i);
    05841           if (_gted[engine].cargo_allowed    & cs->classes) SetBit(mask,     i);
    05842           if (_gted[engine].cargo_disallowed & cs->classes) SetBit(not_mask, i);
    05843         }
    05844       }
    05845     } else {
    05846       /* Don't apply default refit mask to wagons nor engines with no capacity */
    05847       if (e->type != VEH_TRAIN || (e->u.rail.capacity != 0 && e->u.rail.railveh_type != RAILVEH_WAGON)) {
    05848         const CargoLabel *cl = _default_refitmasks[e->type];
    05849         for (uint i = 0;; i++) {
    05850           if (cl[i] == 0) break;
    05851
    05852           CargoID cargo = GetCargoIDByLabel(cl[i]);
    05853           if (cargo == CT_INVALID) continue;
    05854
    05855           SetBit(xor_mask, cargo);
    05856         }
    05857       }
    05858     }
    05859
    05860     ei->refit_mask = ((mask & ~not_mask) ^ xor_mask) & _cargo_mask;
    05861
    05862     /* Check if this engine's cargo type is valid. If not, set to the first refittable
    05863      * cargo type. Apparently cargo_type isn't a common property... */
    05864     switch (e->type) {
    05865       default: NOT_REACHED();
    05866       case VEH_AIRCRAFT:
    05867         if (FindFirstRefittableCargo(engine) == CT_INVALID) ei->climates = 0x80;
    05868         break;
    05869
    05870       case VEH_TRAIN: {
    05871         RailVehicleInfo *rvi = &e->u.rail;
    05872         if (rvi->cargo_type == CT_INVALID) rvi->cargo_type = FindFirstRefittableCargo(engine);
    05873         if (rvi->cargo_type == CT_INVALID) ei->climates = 0x80;
    05874         break;
    05875       }
    05876       case VEH_ROAD: {
    05877         RoadVehicleInfo *rvi = &e->u.road;
    05878         if (rvi->cargo_type == CT_INVALID) rvi->cargo_type = FindFirstRefittableCargo(engine);
    05879         if (rvi->cargo_type == CT_INVALID) ei->climates = 0x80;
    05880         break;
    05881       }
    05882       case VEH_SHIP: {
    05883         ShipVehicleInfo *svi = &e->u.ship;
    05884         if (svi->cargo_type == CT_INVALID) svi->cargo_type = FindFirstRefittableCargo(engine);
    05885         if (svi->cargo_type == CT_INVALID) ei->climates = 0x80;
    05886         break;
    05887       }
    05888     }
    05889   }
    05890 }
    05891
    05896 static void FinaliseHouseArray()
    05897 {
    05898   /* If there are no houses with start dates before 1930, then all houses
    05899    * with start dates of 1930 have them reset to 0. This is in order to be
    05900    * compatible with TTDPatch, where if no houses have start dates before
    05901    * 1930 and the date is before 1930, the game pretends that this is 1930.
    05902    * If there have been any houses defined with start dates before 1930 then
    05903    * the dates are left alone.
    05904    * On the other hand, why 1930? Just 'fix' the houses with the lowest
    05905    * minimum introduction date to 0.
    05906    */
    05907   Year min_year = MAX_YEAR;
    05908
    05909   for (GRFFile *file = _first_grffile; file != NULL; file = file->next) {
    05910     if (file->housespec == NULL) continue;
    05911
    05912     for (int i = 0; i < HOUSE_MAX; i++) {
    05913       HouseSpec *hs = file->housespec[i];
    05914
    05915       if (hs == NULL) continue;
    05916
    05917       const HouseSpec *next1 = (i + 1 < HOUSE_MAX ? file->housespec[i + 1] : NULL);
    05918       const HouseSpec *next2 = (i + 2 < HOUSE_MAX ? file->housespec[i + 2] : NULL);
    05919       const HouseSpec *next3 = (i + 3 < HOUSE_MAX ? file->housespec[i + 3] : NULL);
    05920
    05921       if (((hs->building_flags & BUILDING_HAS_2_TILES) != 0 &&
    05922             (next1 == NULL || !next1->enabled || (next1->building_flags & BUILDING_HAS_1_TILE) != 0)) ||
    05923           ((hs->building_flags & BUILDING_HAS_4_TILES) != 0 &&
    05924             (next2 == NULL || !next2->enabled || (next2->building_flags & BUILDING_HAS_1_TILE) != 0 ||
    05925             next3 == NULL || !next3->enabled || (next3->building_flags & BUILDING_HAS_1_TILE) != 0))) {
    05926         hs->enabled = false;
    05927         DEBUG(grf, 1, "FinaliseHouseArray: %s defines house %d as multitile, but no suitable tiles follow. Disabling house.", file->filename, hs->local_id);
    05928         continue;
    05929       }
    05930
    05931       /* Some places sum population by only counting north tiles. Other places use all tiles causing desyncs.
    05932        * As the newgrf specs define population to be zero for non-north tiles, we just disable the offending house.
    05933        * If you want to allow non-zero populations somewhen, make sure to sum the population of all tiles in all places. */
    05934       if (((hs->building_flags & BUILDING_HAS_2_TILES) != 0 && next1->population != 0) ||
    05935           ((hs->building_flags & BUILDING_HAS_4_TILES) != 0 && (next2->population != 0 || next3->population != 0))) {
    05936         hs->enabled = false;
    05937         DEBUG(grf, 1, "FinaliseHouseArray: %s defines multitile house %d with non-zero population on additional tiles. Disabling house.", file->filename, hs->local_id);
    05938         continue;
    05939       }
    05940
    05941       _house_mngr.SetEntitySpec(hs);
    05942       if (hs->min_year < min_year) min_year = hs->min_year;
    05943     }
    05944   }
    05945
    05946   if (min_year != 0) {
    05947     for (int i = 0; i < HOUSE_MAX; i++) {
    05948       HouseSpec *hs = GetHouseSpecs(i);
    05949
    05950       if (hs->enabled && hs->min_year == min_year) hs->min_year = 0;
    05951     }
    05952   }
    05953 }
    05954
    05958 static void FinaliseIndustriesArray()
    05959 {
    05960   for (GRFFile *file = _first_grffile; file != NULL; file = file->next) {
    05961     if (file->industryspec != NULL) {
    05962       for (int i = 0; i < NUM_INDUSTRYTYPES; i++) {
    05963         IndustrySpec *indsp = file->industryspec[i];
    05964
    05965         if (indsp != NULL && indsp->enabled) {
    05966           StringID strid;
    05967           /* process the conversion of text at the end, so to be sure everything will be fine
    05968            * and available.  Check if it does not return undefind marker, which is a very good sign of a
    05969            * substitute industry who has not changed the string been examined, thus using it as such */
    05970           strid = GetGRFStringID(indsp->grf_prop.grffile->grfid, indsp->name);
    05971           if (strid != STR_UNDEFINED) indsp->name = strid;
    05972
    05973           strid = GetGRFStringID(indsp->grf_prop.grffile->grfid, indsp->closure_text);
    05974           if (strid != STR_UNDEFINED) indsp->closure_text = strid;
    05975
    05976           strid = GetGRFStringID(indsp->grf_prop.grffile->grfid, indsp->production_up_text);
    05977           if (strid != STR_UNDEFINED) indsp->production_up_text = strid;
    05978
    05979           strid = GetGRFStringID(indsp->grf_prop.grffile->grfid, indsp->production_down_text);
    05980           if (strid != STR_UNDEFINED) indsp->production_down_text = strid;
    05981
    05982           strid = GetGRFStringID(indsp->grf_prop.grffile->grfid, indsp->new_industry_text);
    05983           if (strid != STR_UNDEFINED) indsp->new_industry_text = strid;
    05984
    05985           if (indsp->station_name != STR_NULL) {
    05986             /* STR_NULL (0) can be set by grf.  It has a meaning regarding assignation of the
    05987              * station's name. Don't want to lose the value, therefore, do not process. */
    05988             strid = GetGRFStringID(indsp->grf_prop.grffile->grfid, indsp->station_name);
    05989             if (strid != STR_UNDEFINED) indsp->station_name = strid;
    05990           }
    05991
    05992           _industry_mngr.SetEntitySpec(indsp);
    05993           _loaded_newgrf_features.has_newindustries = true;
    05994         }
    05995       }
    05996     }
    05997
    05998     if (file->indtspec != NULL) {
    05999       for (int i = 0; i < NUM_INDUSTRYTILES; i++) {
    06000         IndustryTileSpec *indtsp = file->indtspec[i];
    06001         if (indtsp != NULL) {
    06002           _industile_mngr.SetEntitySpec(indtsp);
    06003         }
    06004       }
    06005     }
    06006   }
    06007
    06008   for (uint j = 0; j < NUM_INDUSTRYTYPES; j++) {
    06009     IndustrySpec *indsp = &_industry_specs[j];
    06010     if (indsp->enabled && indsp->grf_prop.grffile != NULL) {
    06011       for (uint i = 0; i < 3; i++) {
    06012         indsp->conflicting[i] = MapNewGRFIndustryType(indsp->conflicting[i], indsp->grf_prop.grffile->grfid);
    06013       }
    06014     }
    06015   }
    06016 }
    06017
    06018 /* Here we perform initial decoding of some special sprites (as are they
    06019  * described at http://www.ttdpatch.net/src/newgrf.txt, but this is only a very
    06020  * partial implementation yet).
    06021  * XXX: We consider GRF files trusted. It would be trivial to exploit OTTD by
    06022  * a crafted invalid GRF file. We should tell that to the user somehow, or
    06023  * better make this more robust in the future. */
    06024 static void DecodeSpecialSprite(byte *buf, uint num, GrfLoadingStage stage)
    06025 {
    06026   /* XXX: There is a difference between staged loading in TTDPatch and
    06027    * here.  In TTDPatch, for some reason actions 1 and 2 are carried out
    06028    * during stage 1, whilst action 3 is carried out during stage 2 (to
    06029    * "resolve" cargo IDs... wtf). This is a little problem, because cargo
    06030    * IDs are valid only within a given set (action 1) block, and may be
    06031    * overwritten after action 3 associates them. But overwriting happens
    06032    * in an earlier stage than associating, so...  We just process actions
    06033    * 1 and 2 in stage 2 now, let's hope that won't get us into problems.
    06034    * --pasky
    06035    * We need a pre-stage to set up GOTO labels of Action 0x10 because the grf
    06036    * is not in memory and scanning the file every time would be too expensive.
    06037    * In other stages we skip action 0x10 since it's already dealt with. */
    06038   static const SpecialSpriteHandler handlers[][GLS_END] = {
    06039     /* 0x00 */ { NULL,     SafeChangeInfo, NULL,       NULL,           ReserveChangeInfo, FeatureChangeInfo, },
    06040     /* 0x01 */ { SkipAct1, SkipAct1,  SkipAct1,        SkipAct1,       SkipAct1,          NewSpriteSet, },
    06041     /* 0x02 */ { NULL,     NULL,      NULL,            NULL,           NULL,              NewSpriteGroup, },
    06042     /* 0x03 */ { NULL,     GRFUnsafe, NULL,            NULL,           NULL,              FeatureMapSpriteGroup, },
    06043     /* 0x04 */ { NULL,     NULL,      NULL,            NULL,           NULL,              FeatureNewName, },
    06044     /* 0x05 */ { SkipAct5, SkipAct5,  SkipAct5,        SkipAct5,       SkipAct5,          GraphicsNew, },
    06045     /* 0x06 */ { NULL,     NULL,      NULL,            CfgApply,       CfgApply,          CfgApply, },
    06046     /* 0x07 */ { NULL,     NULL,      NULL,            NULL,           SkipIf,            SkipIf, },
    06047     /* 0x08 */ { ScanInfo, NULL,      NULL,            GRFInfo,        GRFInfo,           GRFInfo, },
    06048     /* 0x09 */ { NULL,     NULL,      NULL,            SkipIf,         SkipIf,            SkipIf, },
    06049     /* 0x0A */ { SkipActA, SkipActA,  SkipActA,        SkipActA,       SkipActA,          SpriteReplace, },
    06050     /* 0x0B */ { NULL,     NULL,      NULL,            GRFLoadError,   GRFLoadError,      GRFLoadError, },
    06051     /* 0x0C */ { NULL,     NULL,      NULL,            GRFComment,     NULL,              GRFComment, },
    06052     /* 0x0D */ { NULL,     SafeParamSet, NULL,         ParamSet,       ParamSet,          ParamSet, },
    06053     /* 0x0E */ { NULL,     SafeGRFInhibit, NULL,       GRFInhibit,     GRFInhibit,        GRFInhibit, },
    06054     /* 0x0F */ { NULL,     GRFUnsafe, NULL,            FeatureTownName, NULL,             NULL, },
    06055     /* 0x10 */ { NULL,     NULL,      DefineGotoLabel, NULL,           NULL,              NULL, },
    06056     /* 0x11 */ { SkipAct11,GRFUnsafe, SkipAct11,       SkipAct11,      SkipAct11,         GRFSound, },
    06057     /* 0x12 */ { SkipAct12, SkipAct12, SkipAct12,      SkipAct12,      SkipAct12,         LoadFontGlyph, },
    06058     /* 0x13 */ { NULL,     NULL,      NULL,            NULL,           NULL,              TranslateGRFStrings, },
    06059   };
    06060
    06061   GRFLocation location(_cur_grfconfig->grfid, _nfo_line);
    06062
    06063   GRFLineToSpriteOverride::iterator it = _grf_line_to_action6_sprite_override.find(location);
    06064   if (it == _grf_line_to_action6_sprite_override.end()) {
    06065     /* No preloaded sprite to work with; read the
    06066      * pseudo sprite content. */
    06067     FioReadBlock(buf, num);
    06068   } else {
    06069     /* Use the preloaded sprite data. */
    06070     buf = _grf_line_to_action6_sprite_override[location];
    06071     grfmsg(7, "DecodeSpecialSprite: Using preloaded pseudo sprite data");
    06072
    06073     /* Skip the real (original) content of this action. */
    06074     FioSeekTo(num, SEEK_CUR);
    06075   }
    06076
    06077   byte action = buf[0];
    06078
    06079   if (action == 0xFF) {
    06080     grfmsg(7, "DecodeSpecialSprite: Handling data block in stage %d", stage);
    06081     GRFDataBlock(buf, num);
    06082   } else if (action == 0xFE) {
    06083     grfmsg(7, "DecodeSpecialSprite: Handling import block in stage %d", stage);
    06084     GRFImportBlock(buf, num);
    06085   } else if (action >= lengthof(handlers)) {
    06086     grfmsg(7, "DecodeSpecialSprite: Skipping unknown action 0x%02X", action);
    06087   } else if (handlers[action][stage] == NULL) {
    06088     grfmsg(7, "DecodeSpecialSprite: Skipping action 0x%02X in stage %d", action, stage);
    06089   } else {
    06090     grfmsg(7, "DecodeSpecialSprite: Handling action 0x%02X in stage %d", action, stage);
    06091     handlers[action][stage](buf, num);
    06092   }
    06093 }
    06094
    06095
    06096 void LoadNewGRFFile(GRFConfig *config, uint file_index, GrfLoadingStage stage)
    06097 {
    06098   const char *filename = config->filename;
    06099   uint16 num;
    06100
    06101   /* A .grf file is activated only if it was active when the game was
    06102    * started.  If a game is loaded, only its active .grfs will be
    06103    * reactivated, unless "loadallgraphics on" is used.  A .grf file is
    06104    * considered active if its action 8 has been processed, i.e. its
    06105    * action 8 hasn't been skipped using an action 7.
    06106    *
    06107    * During activation, only actions 0, 1, 2, 3, 4, 5, 7, 8, 9, 0A and 0B are
    06108    * carried out.  All others are ignored, because they only need to be
    06109    * processed once at initialization.  */
    06110   if (stage != GLS_FILESCAN && stage != GLS_SAFETYSCAN && stage != GLS_LABELSCAN) {
    06111     _cur_grffile = GetFileByFilename(filename);
    06112     if (_cur_grffile == NULL) usererror("File '%s' lost in cache.\n", filename);
    06113     if (stage == GLS_RESERVE && config->status != GCS_INITIALISED) return;
    06114     if (stage == GLS_ACTIVATION && !HasBit(config->flags, GCF_RESERVED)) return;
    06115     _cur_grffile->is_ottdfile = config->IsOpenTTDBaseGRF();
    06116   }
    06117
    06118   if (file_index > LAST_GRF_SLOT) {
    06119     DEBUG(grf, 0, "'%s' is not loaded as the maximum number of GRFs has been reached", filename);
    06120     config->status = GCS_DISABLED;
    06121     config->error  = CallocT<GRFError>(1);
    06122     config->error->severity = STR_NEWGRF_ERROR_MSG_FATAL;
    06123     config->error->message  = STR_NEWGRF_ERROR_TOO_MANY_NEWGRFS_LOADED;
    06124     return;
    06125   }
    06126
    06127   FioOpenFile(file_index, filename);
    06128   _file_index = file_index; // XXX
    06129   _palette_remap_grf[_file_index] = (config->windows_paletted != (_use_palette == PAL_WINDOWS));
    06130
    06131   _cur_grfconfig = config;
    06132
    06133   DEBUG(grf, 2, "LoadNewGRFFile: Reading NewGRF-file '%s'", filename);
    06134
    06135   /* Skip the first sprite; we don't care about how many sprites this
    06136    * does contain; newest TTDPatches and George's longvehicles don't
    06137    * neither, apparently. */
    06138   if (FioReadWord() == 4 && FioReadByte() == 0xFF) {
    06139     FioReadDword();
    06140   } else {
    06141     DEBUG(grf, 7, "LoadNewGRFFile: Custom .grf has invalid format");
    06142     return;
    06143   }
    06144
    06145   _skip_sprites = 0; // XXX
    06146   _nfo_line = 0;
    06147
    06148   ReusableBuffer<byte> buf;
    06149
    06150   while ((num = FioReadWord()) != 0) {
    06151     byte type = FioReadByte();
    06152     _nfo_line++;
    06153
    06154     if (type == 0xFF) {
    06155       if (_skip_sprites == 0) {
    06156         DecodeSpecialSprite(buf.Allocate(num), num, stage);
    06157
    06158         /* Stop all processing if we are to skip the remaining sprites */
    06159         if (_skip_sprites == -1) break;
    06160
    06161         continue;
    06162       } else {
    06163         FioSkipBytes(num);
    06164       }
    06165     } else {
    06166       if (_skip_sprites == 0) {
    06167         grfmsg(0, "LoadNewGRFFile: Unexpected sprite, disabling");
    06168         config->status = GCS_DISABLED;
    06169         config->error  = CallocT<GRFError>(1);
    06170         config->error->severity = STR_NEWGRF_ERROR_MSG_FATAL;
    06171         config->error->message  = STR_NEWGRF_ERROR_UNEXPECTED_SPRITE;
    06172         break;
    06173       }
    06174
    06175       FioSkipBytes(7);
    06176       SkipSpriteData(type, num - 8);
    06177     }
    06178
    06179     if (_skip_sprites > 0) _skip_sprites--;
    06180   }
    06181 }
    06182
    06190 static void ActivateOldShore()
    06191 {
    06192   /* Use default graphics, if no shore sprites were loaded.
    06193    * Should not happen, as openttd(w/d).grf includes some. */
    06194   if (_loaded_newgrf_features.shore == SHORE_REPLACE_NONE) _loaded_newgrf_features.shore = SHORE_REPLACE_ACTION_A;
    06195
    06196   if (_loaded_newgrf_features.shore != SHORE_REPLACE_ACTION_5) {
    06197     DupSprite(SPR_ORIGINALSHORE_START +  1, SPR_SHORE_BASE +  1); // SLOPE_W
    06198     DupSprite(SPR_ORIGINALSHORE_START +  2, SPR_SHORE_BASE +  2); // SLOPE_S
    06199     DupSprite(SPR_ORIGINALSHORE_START +  6, SPR_SHORE_BASE +  3); // SLOPE_SW
    06200     DupSprite(SPR_ORIGINALSHORE_START     , SPR_SHORE_BASE +  4); // SLOPE_E
    06201     DupSprite(SPR_ORIGINALSHORE_START +  4, SPR_SHORE_BASE +  6); // SLOPE_SE
    06202     DupSprite(SPR_ORIGINALSHORE_START +  3, SPR_SHORE_BASE +  8); // SLOPE_N
    06203     DupSprite(SPR_ORIGINALSHORE_START +  7, SPR_SHORE_BASE +  9); // SLOPE_NW
    06204     DupSprite(SPR_ORIGINALSHORE_START +  5, SPR_SHORE_BASE + 12); // SLOPE_NE
    06205   }
    06206
    06207   if (_loaded_newgrf_features.shore == SHORE_REPLACE_ACTION_A) {
    06208     DupSprite(SPR_FLAT_GRASS_TILE + 16, SPR_SHORE_BASE +  0); // SLOPE_STEEP_S
    06209     DupSprite(SPR_FLAT_GRASS_TILE + 17, SPR_SHORE_BASE +  5); // SLOPE_STEEP_W
    06210     DupSprite(SPR_FLAT_GRASS_TILE +  7, SPR_SHORE_BASE +  7); // SLOPE_WSE
    06211     DupSprite(SPR_FLAT_GRASS_TILE + 15, SPR_SHORE_BASE + 10); // SLOPE_STEEP_N
    06212     DupSprite(SPR_FLAT_GRASS_TILE + 11, SPR_SHORE_BASE + 11); // SLOPE_NWS
    06213     DupSprite(SPR_FLAT_GRASS_TILE + 13, SPR_SHORE_BASE + 13); // SLOPE_ENW
    06214     DupSprite(SPR_FLAT_GRASS_TILE + 14, SPR_SHORE_BASE + 14); // SLOPE_SEN
    06215     DupSprite(SPR_FLAT_GRASS_TILE + 18, SPR_SHORE_BASE + 15); // SLOPE_STEEP_E
    06216
    06217     /* XXX - SLOPE_EW, SLOPE_NS are currently not used.
    06218      *       If they would be used somewhen, then these grass tiles will most like not look as needed */
    06219     DupSprite(SPR_FLAT_GRASS_TILE +  5, SPR_SHORE_BASE + 16); // SLOPE_EW
    06220     DupSprite(SPR_FLAT_GRASS_TILE + 10, SPR_SHORE_BASE + 17); // SLOPE_NS
    06221   }
    06222 }
    06223
    06224 void InitDepotWindowBlockSizes();
    06225
    06226 extern void InitGRFTownGeneratorNames();
    06227
    06228 static void AfterLoadGRFs()
    06229 {
    06230   for (StringIDToGRFIDMapping::iterator it = _string_to_grf_mapping.begin(); it != _string_to_grf_mapping.end(); it++) {
    06231     *((*it).first) = MapGRFStringID((*it).second, *((*it).first));
    06232   }
    06233   _string_to_grf_mapping.clear();
    06234
    06235   /* Free the action 6 override sprites. */
    06236   for (GRFLineToSpriteOverride::iterator it = _grf_line_to_action6_sprite_override.begin(); it != _grf_line_to_action6_sprite_override.end(); it++) {
    06237     free((*it).second);
    06238   }
    06239   _grf_line_to_action6_sprite_override.clear();
    06240
    06241   /* Pre-calculate all refit masks after loading GRF files. */
    06242   CalculateRefitMasks();
    06243
    06244   /* Set the block size in the depot windows based on vehicle sprite sizes */
    06245   InitDepotWindowBlockSizes();
    06246
    06247   /* Add all new houses to the house array. */
    06248   FinaliseHouseArray();
    06249
    06250   /* Add all new industries to the industry array. */
    06251   FinaliseIndustriesArray();
    06252
    06253   /* Create dynamic list of industry legends for smallmap_gui.cpp */
    06254   BuildIndustriesLegend();
    06255
    06256   /* Update the townname generators list */
    06257   InitGRFTownGeneratorNames();
    06258
    06259   /* Run all queued vehicle list order changes */
    06260   CommitVehicleListOrderChanges();
    06261
    06262   /* Load old shore sprites in new position, if they were replaced by ActionA */
    06263   ActivateOldShore();
    06264
    06265   Engine *e;
    06266   FOR_ALL_ENGINES_OF_TYPE(e, VEH_ROAD) {
    06267     if (_gted[e->index].rv_max_speed != 0) {
    06268       /* Set RV maximum speed from the mph/0.8 unit value */
    06269       e->u.road.max_speed = _gted[e->index].rv_max_speed * 4;
    06270     }
    06271   }
    06272
    06273   SetYearEngineAgingStops();
    06274
    06275   /* Deallocate temporary loading data */
    06276   free(_gted);
    06277   _grm_sprites.clear();
    06278 }
    06279
    06280 void LoadNewGRF(uint load_index, uint file_index)
    06281 {
    06282   /* In case of networking we need to "sync" the start values
    06283    * so all NewGRFs are loaded equally. For this we use the
    06284    * start date of the game and we set the counters, etc. to
    06285    * 0 so they're the same too. */
    06286   Date date            = _date;
    06287   Year year            = _cur_year;
    06288   DateFract date_fract = _date_fract;
    06289   uint16 tick_counter  = _tick_counter;
    06290   byte display_opt     = _display_opt;
    06291
    06292   if (_networking) {
    06293     _cur_year     = _settings_game.game_creation.starting_year;
    06294     _date         = ConvertYMDToDate(_cur_year, 0, 1);
    06295     _date_fract   = 0;
    06296     _tick_counter = 0;
    06297     _display_opt  = 0;
    06298   }
    06299
    06300   InitializeGRFSpecial();
    06301
    06302   ResetNewGRFData();
    06303
    06304   /*
    06305    * Reset the status of all files, so we can 'retry' to load them.
    06306    * This is needed when one for example rearranges the NewGRFs in-game
    06307    * and a previously disabled NewGRF becomes useable. If it would not
    06308    * be reset, the NewGRF would remain disabled even though it should
    06309    * have been enabled.
    06310    */
    06311   for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
    06312     if (c->status != GCS_NOT_FOUND) c->status = GCS_UNKNOWN;
    06313   }
    06314
    06315   _cur_spriteid = load_index;
    06316
    06317   /* Load newgrf sprites
    06318    * in each loading stage, (try to) open each file specified in the config
    06319    * and load information from it. */
    06320   for (GrfLoadingStage stage = GLS_LABELSCAN; stage <= GLS_ACTIVATION; stage++) {
    06321     /* Set activated grfs back to will-be-activated between reservation- and activation-stage.
    06322      * This ensures that action7/9 conditions 0x06 - 0x0A work correctly. */
    06323     for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
    06324       if (c->status == GCS_ACTIVATED) c->status = GCS_INITIALISED;
    06325     }
    06326
    06327     uint slot = file_index;
    06328
    06329     _cur_stage = stage;
    06330     for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
    06331       if (c->status == GCS_DISABLED || c->status == GCS_NOT_FOUND) continue;
    06332       if (stage > GLS_INIT && HasBit(c->flags, GCF_INIT_ONLY)) continue;
    06333
    06334       if (!FioCheckFileExists(c->filename)) {
    06335         DEBUG(grf, 0, "NewGRF file is missing '%s'; disabling", c->filename);
    06336         c->status = GCS_NOT_FOUND;
    06337         continue;
    06338       }
    06339
    06340       if (stage == GLS_LABELSCAN) InitNewGRFFile(c, _cur_spriteid);
    06341       LoadNewGRFFile(c, slot++, stage);
    06342       if (stage == GLS_RESERVE) {
    06343         SetBit(c->flags, GCF_RESERVED);
    06344       } else if (stage == GLS_ACTIVATION) {
    06345         ClrBit(c->flags, GCF_RESERVED);
    06346         assert(GetFileByGRFID(c->grfid) == _cur_grffile);
    06347         ClearTemporaryNewGRFData(_cur_grffile);
    06348         BuildCargoTranslationMap();
    06349         DEBUG(sprite, 2, "LoadNewGRF: Currently %i sprites are loaded", _cur_spriteid);
    06350       } else if (stage == GLS_INIT && HasBit(c->flags, GCF_INIT_ONLY)) {
    06351         /* We're not going to activate this, so free whatever data we allocated */
    06352         ClearTemporaryNewGRFData(_cur_grffile);
    06353       }
    06354     }
    06355   }
    06356
    06357   /* Call any functions that should be run after GRFs have been loaded. */
    06358   AfterLoadGRFs();
    06359
    06360   /* Now revert back to the original situation */
    06361   _cur_year     = year;
    06362   _date         = date;
    06363   _date_fract   = date_fract;
    06364   _tick_counter = tick_counter;
    06365   _display_opt  = display_opt;
    06366 }
    06367
    06368 bool HasGrfMiscBit(GrfMiscBit bit)
    06369 {
    06370   return HasBit(_misc_grf_features, bit);
    06371 }