# afterload.cpp

Go to the documentation of this file.

    00001 /* $Id: afterload.cpp 16355 2009-05-18 20:17:28Z rubidium $ */
    00002
    00005 #include "../stdafx.h"
    00006 #include "../void_map.h"
    00007 #include "../signs_base.h"
    00008 #include "../window_func.h"
    00009 #include "../fios.h"
    00010 #include "../train.h"
    00011 #include "../string_func.h"
    00012 #include "../gamelog.h"
    00013 #include "../network/network.h"
    00014 #include "../gfxinit.h"
    00015 #include "../functions.h"
    00016 #include "../industry_map.h"
    00017 #include "../town_map.h"
    00018 #include "../clear_map.h"
    00019 #include "../vehicle_func.h"
    00020 #include "../newgrf_station.h"
    00021 #include "../yapf/yapf.hpp"
    00022 #include "../elrail_func.h"
    00023 #include "../signs_func.h"
    00024 #include "../aircraft.h"
    00025 #include "../unmovable_map.h"
    00026 #include "../tree_map.h"
    00027 #include "../company_func.h"
    00028 #include "../road_cmd.h"
    00029 #include "../ai/ai.hpp"
    00030
    00031 #include "table/strings.h"
    00032
    00033 #include "saveload_internal.h"
    00034
    00035 #include <signal.h>
    00036
    00037 extern StringID _switch_mode_errorstr;
    00038 extern Company *DoStartupNewCompany(bool is_ai);
    00039 extern void InitializeRailGUI();
    00040
    00052 void SetWaterClassDependingOnSurroundings(TileIndex t, bool include_invalid_water_class)
    00053 {
    00054   /* If the slope is not flat, we always assume 'land' (if allowed). Also for one-corner-raised-shores.
    00055    * Note: Wrt. autosloping under industry tiles this is the most fool-proof behaviour. */
    00056   if (GetTileSlope(t, NULL) != SLOPE_FLAT) {
    00057     if (include_invalid_water_class) {
    00058       SetWaterClass(t, WATER_CLASS_INVALID);
    00059       return;
    00060     } else {
    00061       NOT_REACHED();
    00062     }
    00063   }
    00064
    00065   /* Mark tile dirty in all cases */
    00066   MarkTileDirtyByTile(t);
    00067
    00068   if (TileX(t) == 0 || TileY(t) == 0 || TileX(t) == MapMaxX() - 1 || TileY(t) == MapMaxY() - 1) {
    00069     /* tiles at map borders are always WATER_CLASS_SEA */
    00070     SetWaterClass(t, WATER_CLASS_SEA);
    00071     return;
    00072   }
    00073
    00074   bool has_water = false;
    00075   bool has_canal = false;
    00076   bool has_river = false;
    00077
    00078   for (DiagDirection dir = DIAGDIR_BEGIN; dir < DIAGDIR_END; dir++) {
    00079     TileIndex neighbour = TileAddByDiagDir(t, dir);
    00080     switch (GetTileType(neighbour)) {
    00081       case MP_WATER:
    00082         /* clear water and shipdepots have already a WaterClass associated */
    00083         if (IsCoast(neighbour)) {
    00084           has_water = true;
    00085         } else if (!IsLock(neighbour)) {
    00086           switch (GetWaterClass(neighbour)) {
    00087             case WATER_CLASS_SEA:   has_water = true; break;
    00088             case WATER_CLASS_CANAL: has_canal = true; break;
    00089             case WATER_CLASS_RIVER: has_river = true; break;
    00090             default: NOT_REACHED();
    00091           }
    00092         }
    00093         break;
    00094
    00095       case MP_RAILWAY:
    00096         /* Shore or flooded halftile */
    00097         has_water |= (GetRailGroundType(neighbour) == RAIL_GROUND_WATER);
    00098         break;
    00099
    00100       case MP_TREES:
    00101         /* trees on shore */
    00102         has_water |= (GetTreeGround(neighbour) == TREE_GROUND_SHORE);
    00103         break;
    00104
    00105       default: break;
    00106     }
    00107   }
    00108
    00109   if (!has_water && !has_canal && !has_river && include_invalid_water_class) {
    00110     SetWaterClass(t, WATER_CLASS_INVALID);
    00111     return;
    00112   }
    00113
    00114   if (has_river && !has_canal) {
    00115     SetWaterClass(t, WATER_CLASS_RIVER);
    00116   } else if (has_canal || !has_water) {
    00117     SetWaterClass(t, WATER_CLASS_CANAL);
    00118   } else {
    00119     SetWaterClass(t, WATER_CLASS_SEA);
    00120   }
    00121 }
    00122
    00123 static void ConvertTownOwner()
    00124 {
    00125   for (TileIndex tile = 0; tile != MapSize(); tile++) {
    00126     switch (GetTileType(tile)) {
    00127       case MP_ROAD:
    00128         if (GB(_m[tile].m5, 4, 2) == ROAD_TILE_CROSSING && HasBit(_m[tile].m3, 7)) {
    00129           _m[tile].m3 = OWNER_TOWN;
    00130         }
    00131         /* FALLTHROUGH */
    00132
    00133       case MP_TUNNELBRIDGE:
    00134         if (GetTileOwner(tile) & 0x80) SetTileOwner(tile, OWNER_TOWN);
    00135         break;
    00136
    00137       default: break;
    00138     }
    00139   }
    00140 }
    00141
    00142 /* since savegame version 4.1, exclusive transport rights are stored at towns */
    00143 static void UpdateExclusiveRights()
    00144 {
    00145   Town *t;
    00146
    00147   FOR_ALL_TOWNS(t) {
    00148     t->exclusivity = INVALID_COMPANY;
    00149   }
    00150
    00151   /* FIXME old exclusive rights status is not being imported (stored in s->blocked_months_obsolete)
    00152    *   could be implemented this way:
    00153    * 1.) Go through all stations
    00154    *     Build an array town_blocked[ town_id ][ company_id ]
    00155    *     that stores if at least one station in that town is blocked for a company
    00156    * 2.) Go through that array, if you find a town that is not blocked for
    00157    *     one company, but for all others, then give him exclusivity.
    00158    */
    00159 }
    00160
    00161 static const byte convert_currency[] = {
    00162    0,  1, 12,  8,  3,
    00163   10, 14, 19,  4,  5,
    00164    9, 11, 13,  6, 17,
    00165   16, 22, 21,  7, 15,
    00166   18,  2, 20,
    00167 };
    00168
    00169 /* since savegame version 4.2 the currencies are arranged differently */
    00170 static void UpdateCurrencies()
    00171 {
    00172   _settings_game.locale.currency = convert_currency[_settings_game.locale.currency];
    00173 }
    00174
    00175 /* Up to revision 1413 the invisible tiles at the southern border have not been
    00176  * MP_VOID, even though they should have. This is fixed by this function
    00177  */
    00178 static void UpdateVoidTiles()
    00179 {
    00180   uint i;
    00181
    00182   for (i = 0; i < MapMaxY(); ++i) MakeVoid(i * MapSizeX() + MapMaxX());
    00183   for (i = 0; i < MapSizeX(); ++i) MakeVoid(MapSizeX() * MapMaxY() + i);
    00184 }
    00185
    00186 static inline RailType UpdateRailType(RailType rt, RailType min)
    00187 {
    00188   return rt >= min ? (RailType)(rt + 1): rt;
    00189 }
    00190
    00202 static bool InitializeWindowsAndCaches()
    00203 {
    00204   /* Initialize windows */
    00205   ResetWindowSystem();
    00206   SetupColoursAndInitialWindow();
    00207
    00208   ResetViewportAfterLoadGame();
    00209
    00210   /* Update coordinates of the signs. */
    00211   UpdateAllStationVirtCoord();
    00212   UpdateAllSignVirtCoords();
    00213   UpdateAllTownVirtCoords();
    00214   UpdateAllWaypointSigns();
    00215
    00216   Company *c;
    00217   FOR_ALL_COMPANIES(c) {
    00218     /* For each company, verify (while loading a scenario) that the inauguration date is the current year and set it
    00219      * accordingly if it is not the case.  No need to set it on companies that are not been used already,
    00220      * thus the MIN_YEAR (which is really nothing more than Zero, initialized value) test */
    00221     if (_file_to_saveload.filetype == FT_SCENARIO && c->inaugurated_year != MIN_YEAR) {
    00222       c->inaugurated_year = _cur_year;
    00223     }
    00224   }
    00225
    00226   SetCachedEngineCounts();
    00227
    00228   /* Towns have a noise controlled number of airports system
    00229    * So each airport's noise value must be added to the town->noise_reached value
    00230    * Reset each town's noise_reached value to '0' before. */
    00231   UpdateAirportsNoise();
    00232
    00233   CheckTrainsLengths();
    00234
    00235   return true;
    00236 }
    00237
    00238 typedef void (CDECL *SignalHandlerPointer)(int);
    00239 static SignalHandlerPointer _prev_segfault = NULL;
    00240 static SignalHandlerPointer _prev_abort = NULL;
    00241
    00242 static void CDECL HandleSavegameLoadCrash(int signum);
    00243
    00248 static void SetSignalHandlers()
    00249 {
    00250   _prev_segfault = signal(SIGSEGV, HandleSavegameLoadCrash);
    00251   _prev_abort = signal(SIGABRT, HandleSavegameLoadCrash);
    00252 }
    00253
    00257 static void ResetSignalHandlers()
    00258 {
    00259   signal(SIGSEGV, _prev_segfault);
    00260   signal(SIGABRT, _prev_abort);
    00261 }
    00262
    00269 static void CDECL HandleSavegameLoadCrash(int signum)
    00270 {
    00271   ResetSignalHandlers();
    00272
    00273   char buffer[8192];
    00274   char *p = buffer;
    00275   p += seprintf(p, lastof(buffer),
    00276       "Loading your savegame caused OpenTTD to crash.\n"
    00277       "This is most likely caused by a missing NewGRF or a NewGRF that has been\n"
    00278       "loaded as replacement for a missing NewGRF. OpenTTD cannot easily\n"
    00279       "determine whether a replacement NewGRF is of a newer or older version.\n"
    00280       "It will load a NewGRF with the same GRF ID as the missing NewGRF. This\n"
    00281       "means that if the author makes incompatible NewGRFs with the same GRF ID\n"
    00282       "OpenTTD cannot magically do the right thing. In most cases OpenTTD will\n"
    00283       "load the savegame and not crash, but this is an exception.\n"
    00284       "Please load the savegame with the appropriate NewGRFs. When loading a\n"
    00285       "savegame still crashes when all NewGRFs are found you should file a\n"
    00286       "bug report. The missing NewGRFs are:\n");
    00287
    00288   for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
    00289     if (HasBit(c->flags, GCF_COMPATIBLE)) {
    00290       char buf[40];
    00291       md5sumToString(buf, lastof(buf), c->md5sum);
    00292       p += seprintf(p, lastof(buffer), "NewGRF %08X (%s) not found; checksum %s. Tried another NewGRF with same GRF ID\n", BSWAP32(c->grfid), c->filename, buf);
    00293     }
    00294     if (c->status == GCS_NOT_FOUND) {
    00295       char buf[40];
    00296       md5sumToString(buf, lastof(buf), c->md5sum);
    00297       p += seprintf(p, lastof(buffer), "NewGRF %08X (%s) not found; checksum %s\n", BSWAP32(c->grfid), c->filename, buf);
    00298     }
    00299   }
    00300
    00301   ShowInfo(buffer);
    00302
    00303   SignalHandlerPointer call = signum == SIGSEGV ? _prev_segfault : _prev_abort;
    00304   if (call != NULL) call(signum);
    00305 }
    00306
    00312 static void FixOwnerOfRailTrack(TileIndex t)
    00313 {
    00314   assert(!IsValidCompanyID(GetTileOwner(t)) && (IsLevelCrossingTile(t) || IsPlainRailTile(t)));
    00315
    00316   /* remove leftover rail piece from crossing (from very old savegames) */
    00317   Vehicle *v = NULL, *w;
    00318   FOR_ALL_VEHICLES(w) {
    00319     if (w->type == VEH_TRAIN && w->tile == t) {
    00320       v = w;
    00321       break;
    00322     }
    00323   }
    00324
    00325   if (v != NULL) {
    00326     /* when there is a train on crossing (it could happen in TTD), set owner of crossing to train owner */
    00327     SetTileOwner(t, v->owner);
    00328     return;
    00329   }
    00330
    00331   /* try to find any connected rail */
    00332   for (DiagDirection dd = DIAGDIR_BEGIN; dd < DIAGDIR_END; dd++) {
    00333     TileIndex tt = t + TileOffsByDiagDir(dd);
    00334     if (GetTileTrackStatus(t, TRANSPORT_RAIL, 0, dd) != 0 &&
    00335         GetTileTrackStatus(tt, TRANSPORT_RAIL, 0, ReverseDiagDir(dd)) != 0 &&
    00336         IsValidCompanyID(GetTileOwner(tt))) {
    00337       SetTileOwner(t, GetTileOwner(tt));
    00338       return;
    00339     }
    00340   }
    00341
    00342   if (IsLevelCrossingTile(t)) {
    00343     /* else change the crossing to normal road (road vehicles won't care) */
    00344     MakeRoadNormal(t, GetCrossingRoadBits(t), GetRoadTypes(t), GetTownIndex(t),
    00345       GetRoadOwner(t, ROADTYPE_ROAD), GetRoadOwner(t, ROADTYPE_TRAM));
    00346     return;
    00347   }
    00348
    00349   /* if it's not a crossing, make it clean land */
    00350   MakeClear(t, CLEAR_GRASS, 0);
    00351 }
    00352
    00353 bool AfterLoadGame()
    00354 {
    00355   SetSignalHandlers();
    00356
    00357   TileIndex map_size = MapSize();
    00358   Company *c;
    00359
    00360   if (CheckSavegameVersion(98)) GamelogOldver();
    00361
    00362   GamelogTestRevision();
    00363   GamelogTestMode();
    00364
    00365   if (CheckSavegameVersion(98)) GamelogGRFAddList(_grfconfig);
    00366
    00367   /* in very old versions, size of train stations was stored differently */
    00368   if (CheckSavegameVersion(2)) {
    00369     Station *st;
    00370     FOR_ALL_STATIONS(st) {
    00371       if (st->train_tile != 0 && st->trainst_h == 0) {
    00372         uint n = _savegame_type == SGT_OTTD ? 4 : 3; // OTTD uses 4 bits per dimensions, TTD 3 bits
    00373         uint w = GB(st->trainst_w, n, n);
    00374         uint h = GB(st->trainst_w, 0, n);
    00375
    00376         if (GetRailStationAxis(st->train_tile) != AXIS_X) Swap(w, h);
    00377
    00378         st->trainst_w = w;
    00379         st->trainst_h = h;
    00380
    00381         assert(GetStationIndex(st->train_tile + TileDiffXY(w - 1, h - 1)) == st->index);
    00382       }
    00383     }
    00384   }
    00385
    00386   /* in version 2.1 of the savegame, town owner was unified. */
    00387   if (CheckSavegameVersionOldStyle(2, 1)) ConvertTownOwner();
    00388
    00389   /* from version 4.1 of the savegame, exclusive rights are stored at towns */
    00390   if (CheckSavegameVersionOldStyle(4, 1)) UpdateExclusiveRights();
    00391
    00392   /* from version 4.2 of the savegame, currencies are in a different order */
    00393   if (CheckSavegameVersionOldStyle(4, 2)) UpdateCurrencies();
    00394
    00395   /* In old version there seems to be a problem that water is owned by
    00396    * OWNER_NONE, not OWNER_WATER.. I can't replicate it for the current
    00397    * (4.3) version, so I just check when versions are older, and then
    00398    * walk through the whole map.. */
    00399   if (CheckSavegameVersionOldStyle(4, 3)) {
    00400     for (TileIndex t = 0; t < map_size; t++) {
    00401       if (IsTileType(t, MP_WATER) && GetTileOwner(t) >= MAX_COMPANIES) {
    00402         SetTileOwner(t, OWNER_WATER);
    00403       }
    00404     }
    00405   }
    00406
    00407   if (CheckSavegameVersion(84)) {
    00408     FOR_ALL_COMPANIES(c) {
    00409       c->name = CopyFromOldName(c->name_1);
    00410       if (c->name != NULL) c->name_1 = STR_SV_UNNAMED;
    00411       c->president_name = CopyFromOldName(c->president_name_1);
    00412       if (c->president_name != NULL) c->president_name_1 = SPECSTR_PRESIDENT_NAME;
    00413     }
    00414
    00415     Station *st;
    00416     FOR_ALL_STATIONS(st) {
    00417       st->name = CopyFromOldName(st->string_id);
    00418       /* generating new name would be too much work for little effect, use the station name fallback */
    00419       if (st->name != NULL) st->string_id = STR_SV_STNAME_FALLBACK;
    00420     }
    00421
    00422     Town *t;
    00423     FOR_ALL_TOWNS(t) {
    00424       t->name = CopyFromOldName(t->townnametype);
    00425       if (t->name != NULL) t->townnametype = SPECSTR_TOWNNAME_START + _settings_game.game_creation.town_name;
    00426     }
    00427
    00428     Waypoint *wp;
    00429     FOR_ALL_WAYPOINTS(wp) {
    00430       wp->name = CopyFromOldName(wp->string);
    00431       wp->string = STR_EMPTY;
    00432     }
    00433   }
    00434
    00435   /* From this point the old names array is cleared. */
    00436   ResetOldNames();
    00437
    00438   if (CheckSavegameVersion(106)) {
    00439     /* no station is determined by 'tile == INVALID_TILE' now (instead of '0') */
    00440     Station *st;
    00441     FOR_ALL_STATIONS(st) {
    00442       if (st->airport_tile == 0) st->airport_tile = INVALID_TILE;
    00443       if (st->dock_tile    == 0) st->dock_tile    = INVALID_TILE;
    00444       if (st->train_tile   == 0) st->train_tile   = INVALID_TILE;
    00445     }
    00446
    00447     /* the same applies to Company::location_of_HQ */
    00448     Company *c;
    00449     FOR_ALL_COMPANIES(c) {
    00450       if (c->location_of_HQ == 0 || (CheckSavegameVersion(4) && c->location_of_HQ == 0xFFFF)) {
    00451         c->location_of_HQ = INVALID_TILE;
    00452       }
    00453     }
    00454   }
    00455
    00456   /* convert road side to my format. */
    00457   if (_settings_game.vehicle.road_side) _settings_game.vehicle.road_side = 1;
    00458
    00459   /* Check if all NewGRFs are present, we are very strict in MP mode */
    00460   GRFListCompatibility gcf_res = IsGoodGRFConfigList();
    00461   if (_networking && gcf_res != GLC_ALL_GOOD) {
    00462     SetSaveLoadError(STR_NETWORK_ERR_CLIENT_NEWGRF_MISMATCH);
    00463     /* Restore the signals */
    00464     ResetSignalHandlers();
    00465     return false;
    00466   }
    00467
    00468   switch (gcf_res) {
    00469     case GLC_COMPATIBLE: _switch_mode_errorstr = STR_NEWGRF_COMPATIBLE_LOAD_WARNING; break;
    00470     case GLC_NOT_FOUND: _switch_mode_errorstr = STR_NEWGRF_DISABLED_WARNING; _pause_game = -1; break;
    00471     default: break;
    00472   }
    00473
    00474   /* Update current year
    00475    * must be done before loading sprites as some newgrfs check it */
    00476   SetDate(_date);
    00477
    00478   /* Force dynamic engines off when loading older savegames */
    00479   if (CheckSavegameVersion(95)) _settings_game.vehicle.dynamic_engines = 0;
    00480
    00481   /* Load the sprites */
    00482   GfxLoadSprites();
    00483   LoadStringWidthTable();
    00484
    00485   /* Copy temporary data to Engine pool */
    00486   CopyTempEngineData();
    00487
    00488   /* Connect front and rear engines of multiheaded trains and converts
    00489    * subtype to the new format */
    00490   if (CheckSavegameVersionOldStyle(17, 1)) ConvertOldMultiheadToNew();
    00491
    00492   /* Connect front and rear engines of multiheaded trains */
    00493   ConnectMultiheadedTrains();
    00494
    00495   /* reinit the landscape variables (landscape might have changed) */
    00496   InitializeLandscapeVariables(true);
    00497
    00498   /* Update all vehicles */
    00499   AfterLoadVehicles(true);
    00500
    00501   /* Make sure there is an AI attached to an AI company */
    00502   {
    00503     Company *c;
    00504     FOR_ALL_COMPANIES(c) {
    00505       if (c->is_ai && c->ai_instance == NULL) AI::StartNew(c->index);
    00506     }
    00507   }
    00508
    00509   /* Update all waypoints */
    00510   if (CheckSavegameVersion(12)) FixOldWaypoints();
    00511
    00512   /* make sure there is a town in the game */
    00513   if (_game_mode == GM_NORMAL && !ClosestTownFromTile(0, UINT_MAX)) {
    00514     SetSaveLoadError(STR_NO_TOWN_IN_SCENARIO);
    00515     /* Restore the signals */
    00516     ResetSignalHandlers();
    00517     return false;
    00518   }
    00519
    00520   /* The void tiles on the southern border used to belong to a wrong class (pre 4.3).
    00521    * This problem appears in savegame version 21 too, see r3455. But after loading the
    00522    * savegame and saving again, the buggy map array could be converted to new savegame
    00523    * version. It didn't show up before r12070. */
    00524   if (CheckSavegameVersion(87)) UpdateVoidTiles();
    00525
    00526   /* If Load Scenario / New (Scenario) Game is used,
    00527    *  a company does not exist yet. So create one here.
    00528    * 1 exeption: network-games. Those can have 0 companies
    00529    *   But this exeption is not true for non dedicated network_servers! */
    00530   if (!IsValidCompanyID(COMPANY_FIRST) && (!_networking || (_networking && _network_server && !_network_dedicated)))
    00531     DoStartupNewCompany(false);
    00532
    00533   if (CheckSavegameVersion(72)) {
    00534     /* Locks/shiplifts in very old savegames had OWNER_WATER as owner */
    00535     for (TileIndex t = 0; t < MapSize(); t++) {
    00536       switch (GetTileType(t)) {
    00537         default: break;
    00538
    00539         case MP_WATER:
    00540           if (GetWaterTileType(t) == WATER_TILE_LOCK && GetTileOwner(t) == OWNER_WATER) SetTileOwner(t, OWNER_NONE);
    00541           break;
    00542
    00543         case MP_STATION: {
    00544           if (HasBit(_m[t].m6, 3)) SetBit(_m[t].m6, 2);
    00545           StationGfx gfx = GetStationGfx(t);
    00546           StationType st;
    00547           if (       IsInsideMM(gfx,   0,   8)) { // Railway station
    00548             st = STATION_RAIL;
    00549             SetStationGfx(t, gfx - 0);
    00550           } else if (IsInsideMM(gfx,   8,  67)) { // Airport
    00551             st = STATION_AIRPORT;
    00552             SetStationGfx(t, gfx - 8);
    00553           } else if (IsInsideMM(gfx,  67,  71)) { // Truck
    00554             st = STATION_TRUCK;
    00555             SetStationGfx(t, gfx - 67);
    00556           } else if (IsInsideMM(gfx,  71,  75)) { // Bus
    00557             st = STATION_BUS;
    00558             SetStationGfx(t, gfx - 71);
    00559           } else if (gfx == 75) {                    // Oil rig
    00560             st = STATION_OILRIG;
    00561             SetStationGfx(t, gfx - 75);
    00562           } else if (IsInsideMM(gfx,  76,  82)) { // Dock
    00563             st = STATION_DOCK;
    00564             SetStationGfx(t, gfx - 76);
    00565           } else if (gfx == 82) {                    // Buoy
    00566             st = STATION_BUOY;
    00567             SetStationGfx(t, gfx - 82);
    00568           } else if (IsInsideMM(gfx,  83, 168)) { // Extended airport
    00569             st = STATION_AIRPORT;
    00570             SetStationGfx(t, gfx - 83 + 67 - 8);
    00571           } else if (IsInsideMM(gfx, 168, 170)) { // Drive through truck
    00572             st = STATION_TRUCK;
    00573             SetStationGfx(t, gfx - 168 + GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET);
    00574           } else if (IsInsideMM(gfx, 170, 172)) { // Drive through bus
    00575             st = STATION_BUS;
    00576             SetStationGfx(t, gfx - 170 + GFX_TRUCK_BUS_DRIVETHROUGH_OFFSET);
    00577           } else {
    00578             /* Restore the signals */
    00579             ResetSignalHandlers();
    00580             return false;
    00581           }
    00582           SB(_m[t].m6, 3, 3, st);
    00583         } break;
    00584       }
    00585     }
    00586   }
    00587
    00588   for (TileIndex t = 0; t < map_size; t++) {
    00589     switch (GetTileType(t)) {
    00590       case MP_STATION: {
    00591         Station *st = GetStationByTile(t);
    00592
    00593         /* Set up station spread; buoys do not have one */
    00594         if (!IsBuoy(t)) st->rect.BeforeAddTile(t, StationRect::ADD_FORCE);
    00595
    00596         switch (GetStationType(t)) {
    00597           case STATION_TRUCK:
    00598           case STATION_BUS:
    00599             if (CheckSavegameVersion(6)) {
    00600               /* From this version on there can be multiple road stops of the
    00601                * same type per station. Convert the existing stops to the new
    00602                * internal data structure. */
    00603               RoadStop *rs = new RoadStop(t);
    00604               if (rs == NULL) error("Too many road stops in savegame");
    00605
    00606               RoadStop **head =
    00607                 IsTruckStop(t) ? &st->truck_stops : &st->bus_stops;
    00608               *head = rs;
    00609             }
    00610             break;
    00611
    00612           case STATION_OILRIG: {
    00613             /* Very old savegames sometimes have phantom oil rigs, i.e.
    00614              * an oil rig which got shut down, but not completly removed from
    00615              * the map
    00616              */
    00617             TileIndex t1 = TILE_ADDXY(t, 0, 1);
    00618             if (IsTileType(t1, MP_INDUSTRY) &&
    00619                 GetIndustryGfx(t1) == GFX_OILRIG_1) {
    00620               /* The internal encoding of oil rigs was changed twice.
    00621                * It was 3 (till 2.2) and later 5 (till 5.1).
    00622                * Setting it unconditionally does not hurt.
    00623                */
    00624               GetStationByTile(t)->airport_type = AT_OILRIG;
    00625             } else {
    00626               DeleteOilRig(t);
    00627             }
    00628             break;
    00629           }
    00630
    00631           default: break;
    00632         }
    00633         break;
    00634       }
    00635
    00636       default: break;
    00637     }
    00638   }
    00639
    00640   /* In version 2.2 of the savegame, we have new airports, so status of all aircraft is reset.
    00641    * This has to be called after the oilrig airport_type update above ^^^ ! */
    00642   if (CheckSavegameVersionOldStyle(2, 2)) UpdateOldAircraft();
    00643
    00644   /* In version 6.1 we put the town index in the map-array. To do this, we need
    00645    *  to use m2 (16bit big), so we need to clean m2, and that is where this is
    00646    *  all about ;) */
    00647   if (CheckSavegameVersionOldStyle(6, 1)) {
    00648     for (TileIndex t = 0; t < map_size; t++) {
    00649       switch (GetTileType(t)) {
    00650         case MP_HOUSE:
    00651           _m[t].m4 = _m[t].m2;
    00652           SetTownIndex(t, CalcClosestTownFromTile(t)->index);
    00653           break;
    00654
    00655         case MP_ROAD:
    00656           _m[t].m4 |= (_m[t].m2 << 4);
    00657           if ((GB(_m[t].m5, 4, 2) == ROAD_TILE_CROSSING ? (Owner)_m[t].m3 : GetTileOwner(t)) == OWNER_TOWN) {
    00658             SetTownIndex(t, CalcClosestTownFromTile(t)->index);
    00659           } else {
    00660             SetTownIndex(t, 0);
    00661           }
    00662           break;
    00663
    00664         default: break;
    00665       }
    00666     }
    00667   }
    00668
    00669   /* Force the freeform edges to false for old savegames. */
    00670   if (CheckSavegameVersion(111)) {
    00671     _settings_game.construction.freeform_edges = false;
    00672   }
    00673
    00674   /* From version 9.0, we update the max passengers of a town (was sometimes negative
    00675    *  before that. */
    00676   if (CheckSavegameVersion(9)) {
    00677     Town *t;
    00678     FOR_ALL_TOWNS(t) UpdateTownMaxPass(t);
    00679   }
    00680
    00681   /* From version 16.0, we included autorenew on engines, which are now saved, but
    00682    *  of course, we do need to initialize them for older savegames. */
    00683   if (CheckSavegameVersion(16)) {
    00684     FOR_ALL_COMPANIES(c) {
    00685       c->engine_renew_list   = NULL;
    00686       c->engine_renew        = false;
    00687       c->engine_renew_months = -6;
    00688       c->engine_renew_money  = 100000;
    00689     }
    00690
    00691     /* When loading a game, _local_company is not yet set to the correct value.
    00692      * However, in a dedicated server we are a spectator, so nothing needs to
    00693      * happen. In case we are not a dedicated server, the local company always
    00694      * becomes company 0, unless we are in the scenario editor where all the
    00695      * companies are 'invalid'.
    00696      */
    00697     if (!_network_dedicated && IsValidCompanyID(COMPANY_FIRST)) {
    00698       c = GetCompany(COMPANY_FIRST);
    00699       c->engine_renew        = _settings_client.gui.autorenew;
    00700       c->engine_renew_months = _settings_client.gui.autorenew_months;
    00701       c->engine_renew_money  = _settings_client.gui.autorenew_money;
    00702     }
    00703   }
    00704
    00705   if (CheckSavegameVersion(48)) {
    00706     for (TileIndex t = 0; t < map_size; t++) {
    00707       switch (GetTileType(t)) {
    00708         case MP_RAILWAY:
    00709           if (IsPlainRailTile(t)) {
    00710             /* Swap ground type and signal type for plain rail tiles, so the
    00711              * ground type uses the same bits as for depots and waypoints. */
    00712             uint tmp = GB(_m[t].m4, 0, 4);
    00713             SB(_m[t].m4, 0, 4, GB(_m[t].m2, 0, 4));
    00714             SB(_m[t].m2, 0, 4, tmp);
    00715           } else if (HasBit(_m[t].m5, 2)) {
    00716             /* Split waypoint and depot rail type and remove the subtype. */
    00717             ClrBit(_m[t].m5, 2);
    00718             ClrBit(_m[t].m5, 6);
    00719           }
    00720           break;
    00721
    00722         case MP_ROAD:
    00723           /* Swap m3 and m4, so the track type for rail crossings is the
    00724            * same as for normal rail. */
    00725           Swap(_m[t].m3, _m[t].m4);
    00726           break;
    00727
    00728         default: break;
    00729       }
    00730     }
    00731   }
    00732
    00733   if (CheckSavegameVersion(61)) {
    00734     /* Added the RoadType */
    00735     bool old_bridge = CheckSavegameVersion(42);
    00736     for (TileIndex t = 0; t < map_size; t++) {
    00737       switch (GetTileType(t)) {
    00738         case MP_ROAD:
    00739           SB(_m[t].m5, 6, 2, GB(_m[t].m5, 4, 2));
    00740           switch (GetRoadTileType(t)) {
    00741             default: NOT_REACHED();
    00742             case ROAD_TILE_NORMAL:
    00743               SB(_m[t].m4, 0, 4, GB(_m[t].m5, 0, 4));
    00744               SB(_m[t].m4, 4, 4, 0);
    00745               SB(_m[t].m6, 2, 4, 0);
    00746               break;
    00747             case ROAD_TILE_CROSSING:
    00748               SB(_m[t].m4, 5, 2, GB(_m[t].m5, 2, 2));
    00749               break;
    00750             case ROAD_TILE_DEPOT:    break;
    00751           }
    00752           SetRoadTypes(t, ROADTYPES_ROAD);
    00753           break;
    00754
    00755         case MP_STATION:
    00756           if (IsRoadStop(t)) SetRoadTypes(t, ROADTYPES_ROAD);
    00757           break;
    00758
    00759         case MP_TUNNELBRIDGE:
    00760           /* Middle part of "old" bridges */
    00761           if (old_bridge && IsBridge(t) && HasBit(_m[t].m5, 6)) break;
    00762           if (((old_bridge && IsBridge(t)) ? (TransportType)GB(_m[t].m5, 1, 2) : GetTunnelBridgeTransportType(t)) == TRANSPORT_ROAD) {
    00763             SetRoadTypes(t, ROADTYPES_ROAD);
    00764           }
    00765           break;
    00766
    00767         default: break;
    00768       }
    00769     }
    00770   }
    00771
    00772   if (CheckSavegameVersion(114)) {
    00773     bool fix_roadtypes = !CheckSavegameVersion(61);
    00774     bool old_bridge = CheckSavegameVersion(42);
    00775
    00776     for (TileIndex t = 0; t < map_size; t++) {
    00777       switch (GetTileType(t)) {
    00778         case MP_ROAD:
    00779           if (fix_roadtypes) SetRoadTypes(t, (RoadTypes)GB(_me[t].m7, 5, 3));
    00780           SB(_me[t].m7, 5, 1, GB(_m[t].m3, 7, 1)); // snow/desert
    00781           switch (GetRoadTileType(t)) {
    00782             default: NOT_REACHED();
    00783             case ROAD_TILE_NORMAL:
    00784               SB(_me[t].m7, 0, 4, GB(_m[t].m3, 0, 4)); // road works
    00785               SB(_m[t].m6, 3, 3, GB(_m[t].m3, 4, 3));  // ground
    00786               SB(_m[t].m3, 0, 4, GB(_m[t].m4, 4, 4));  // tram bits
    00787               SB(_m[t].m3, 4, 4, GB(_m[t].m5, 0, 4));  // tram owner
    00788               SB(_m[t].m5, 0, 4, GB(_m[t].m4, 0, 4));  // road bits
    00789               break;
    00790
    00791             case ROAD_TILE_CROSSING:
    00792               SB(_me[t].m7, 0, 5, GB(_m[t].m4, 0, 5)); // road owner
    00793               SB(_m[t].m6, 3, 3, GB(_m[t].m3, 4, 3));  // ground
    00794               SB(_m[t].m3, 4, 4, GB(_m[t].m5, 0, 4));  // tram owner
    00795               SB(_m[t].m5, 0, 1, GB(_m[t].m4, 6, 1));  // road axis
    00796               SB(_m[t].m5, 5, 1, GB(_m[t].m4, 5, 1));  // crossing state
    00797               break;
    00798
    00799             case ROAD_TILE_DEPOT:
    00800               break;
    00801           }
    00802           if (!HasTownOwnedRoad(t)) {
    00803             const Town *town = CalcClosestTownFromTile(t);
    00804             if (town != NULL) SetTownIndex(t, town->index);
    00805           }
    00806           _m[t].m4 = 0;
    00807           break;
    00808
    00809         case MP_STATION:
    00810           if (!IsRoadStop(t)) break;
    00811
    00812           if (fix_roadtypes) SetRoadTypes(t, (RoadTypes)GB(_m[t].m3, 0, 3));
    00813           SB(_me[t].m7, 0, 5, HasBit(_m[t].m6, 2) ? OWNER_TOWN : GetTileOwner(t));
    00814           SB(_m[t].m3, 4, 4, _m[t].m1);
    00815           _m[t].m4 = 0;
    00816           break;
    00817
    00818         case MP_TUNNELBRIDGE:
    00819           if (old_bridge && IsBridge(t) && HasBit(_m[t].m5, 6)) break;
    00820           if (((old_bridge && IsBridge(t)) ? (TransportType)GB(_m[t].m5, 1, 2) : GetTunnelBridgeTransportType(t)) == TRANSPORT_ROAD) {
    00821             if (fix_roadtypes) SetRoadTypes(t, (RoadTypes)GB(_m[t].m3, 0, 3));
    00822
    00823             Owner o = GetTileOwner(t);
    00824             SB(_me[t].m7, 0, 5, o); // road owner
    00825             SB(_m[t].m3, 4, 4, o == OWNER_NONE ? OWNER_TOWN : o); // tram owner
    00826           }
    00827           SB(_m[t].m6, 2, 4, GB(_m[t].m2, 4, 4)); // bridge type
    00828           SB(_me[t].m7, 5, 1, GB(_m[t].m4, 7, 1)); // snow/desert
    00829
    00830           _m[t].m2 = 0;
    00831           _m[t].m4 = 0;
    00832           break;
    00833
    00834         default: break;
    00835       }
    00836     }
    00837   }
    00838
    00839   if (CheckSavegameVersion(42)) {
    00840     Vehicle *v;
    00841
    00842     for (TileIndex t = 0; t < map_size; t++) {
    00843       if (MayHaveBridgeAbove(t)) ClearBridgeMiddle(t);
    00844       if (IsBridgeTile(t)) {
    00845         if (HasBit(_m[t].m5, 6)) { // middle part
    00846           Axis axis = (Axis)GB(_m[t].m5, 0, 1);
    00847
    00848           if (HasBit(_m[t].m5, 5)) { // transport route under bridge?
    00849             if (GB(_m[t].m5, 3, 2) == TRANSPORT_RAIL) {
    00850               MakeRailNormal(
    00851                 t,
    00852                 GetTileOwner(t),
    00853                 axis == AXIS_X ? TRACK_BIT_Y : TRACK_BIT_X,
    00854                 GetRailType(t)
    00855               );
    00856             } else {
    00857               TownID town = IsTileOwner(t, OWNER_TOWN) ? ClosestTownFromTile(t, UINT_MAX)->index : 0;
    00858
    00859               MakeRoadNormal(
    00860                 t,
    00861                 axis == AXIS_X ? ROAD_Y : ROAD_X,
    00862                 ROADTYPES_ROAD,
    00863                 town,
    00864                 GetTileOwner(t), OWNER_NONE
    00865               );
    00866             }
    00867           } else {
    00868             if (GB(_m[t].m5, 3, 2) == 0) {
    00869               MakeClear(t, CLEAR_GRASS, 3);
    00870             } else {
    00871               if (GetTileSlope(t, NULL) != SLOPE_FLAT) {
    00872                 MakeShore(t);
    00873               } else {
    00874                 if (GetTileOwner(t) == OWNER_WATER) {
    00875                   MakeWater(t);
    00876                 } else {
    00877                   MakeCanal(t, GetTileOwner(t), Random());
    00878                 }
    00879               }
    00880             }
    00881           }
    00882           SetBridgeMiddle(t, axis);
    00883         } else { // ramp
    00884           Axis axis = (Axis)GB(_m[t].m5, 0, 1);
    00885           uint north_south = GB(_m[t].m5, 5, 1);
    00886           DiagDirection dir = ReverseDiagDir(XYNSToDiagDir(axis, north_south));
    00887           TransportType type = (TransportType)GB(_m[t].m5, 1, 2);
    00888
    00889           _m[t].m5 = 1 << 7 | type << 2 | dir;
    00890         }
    00891       }
    00892     }
    00893
    00894     FOR_ALL_VEHICLES(v) {
    00895       if (v->type != VEH_TRAIN && v->type != VEH_ROAD) continue;
    00896       if (IsBridgeTile(v->tile)) {
    00897         DiagDirection dir = GetTunnelBridgeDirection(v->tile);
    00898
    00899         if (dir != DirToDiagDir(v->direction)) continue;
    00900         switch (dir) {
    00901           default: NOT_REACHED();
    00902           case DIAGDIR_NE: if ((v->x_pos & 0xF) !=  0)            continue; break;
    00903           case DIAGDIR_SE: if ((v->y_pos & 0xF) != TILE_SIZE - 1) continue; break;
    00904           case DIAGDIR_SW: if ((v->x_pos & 0xF) != TILE_SIZE - 1) continue; break;
    00905           case DIAGDIR_NW: if ((v->y_pos & 0xF) !=  0)            continue; break;
    00906         }
    00907       } else if (v->z_pos > GetSlopeZ(v->x_pos, v->y_pos)) {
    00908         v->tile = GetNorthernBridgeEnd(v->tile);
    00909       } else {
    00910         continue;
    00911       }
    00912       if (v->type == VEH_TRAIN) {
    00913         v->u.rail.track = TRACK_BIT_WORMHOLE;
    00914       } else {
    00915         v->u.road.state = RVSB_WORMHOLE;
    00916       }
    00917     }
    00918   }
    00919
    00920   /* Elrails got added in rev 24 */
    00921   if (CheckSavegameVersion(24)) {
    00922     Vehicle *v;
    00923     RailType min_rail = RAILTYPE_ELECTRIC;
    00924
    00925     FOR_ALL_VEHICLES(v) {
    00926       if (v->type == VEH_TRAIN) {
    00927         RailType rt = RailVehInfo(v->engine_type)->railtype;
    00928
    00929         v->u.rail.railtype = rt;
    00930         if (rt == RAILTYPE_ELECTRIC) min_rail = RAILTYPE_RAIL;
    00931       }
    00932     }
    00933
    00934     /* .. so we convert the entire map from normal to elrail (so maintain "fairness") */
    00935     for (TileIndex t = 0; t < map_size; t++) {
    00936       switch (GetTileType(t)) {
    00937         case MP_RAILWAY:
    00938           SetRailType(t, UpdateRailType(GetRailType(t), min_rail));
    00939           break;
    00940
    00941         case MP_ROAD:
    00942           if (IsLevelCrossing(t)) {
    00943             SetRailType(t, UpdateRailType(GetRailType(t), min_rail));
    00944           }
    00945           break;
    00946
    00947         case MP_STATION:
    00948           if (IsRailwayStation(t)) {
    00949             SetRailType(t, UpdateRailType(GetRailType(t), min_rail));
    00950           }
    00951           break;
    00952
    00953         case MP_TUNNELBRIDGE:
    00954           if (GetTunnelBridgeTransportType(t) == TRANSPORT_RAIL) {
    00955             SetRailType(t, UpdateRailType(GetRailType(t), min_rail));
    00956           }
    00957           break;
    00958
    00959         default:
    00960           break;
    00961       }
    00962     }
    00963
    00964     FOR_ALL_VEHICLES(v) {
    00965       if (v->type == VEH_TRAIN && (IsFrontEngine(v) || IsFreeWagon(v))) TrainConsistChanged(v, true);
    00966     }
    00967
    00968   }
    00969
    00970   /* In version 16.1 of the savegame a company can decide if trains, which get
    00971    * replaced, shall keep their old length. In all prior versions, just default
    00972    * to false */
    00973   if (CheckSavegameVersionOldStyle(16, 1)) {
    00974     FOR_ALL_COMPANIES(c) c->renew_keep_length = false;
    00975   }
    00976
    00977   /* In version 17, ground type is moved from m2 to m4 for depots and
    00978    * waypoints to make way for storing the index in m2. The custom graphics
    00979    * id which was stored in m4 is now saved as a grf/id reference in the
    00980    * waypoint struct. */
    00981   if (CheckSavegameVersion(17)) {
    00982     Waypoint *wp;
    00983
    00984     FOR_ALL_WAYPOINTS(wp) {
    00985       if (wp->deleted == 0) {
    00986         const StationSpec *statspec = NULL;
    00987
    00988         if (HasBit(_m[wp->xy].m3, 4))
    00989           statspec = GetCustomStationSpec(STAT_CLASS_WAYP, _m[wp->xy].m4 + 1);
    00990
    00991         if (statspec != NULL) {
    00992           wp->stat_id = _m[wp->xy].m4 + 1;
    00993           wp->grfid = statspec->grffile->grfid;
    00994           wp->localidx = statspec->localidx;
    00995         } else {
    00996           /* No custom graphics set, so set to default. */
    00997           wp->stat_id = 0;
    00998           wp->grfid = 0;
    00999           wp->localidx = 0;
    01000         }
    01001
    01002         /* Move ground type bits from m2 to m4. */
    01003         _m[wp->xy].m4 = GB(_m[wp->xy].m2, 0, 4);
    01004         /* Store waypoint index in the tile. */
    01005         _m[wp->xy].m2 = wp->index;
    01006       }
    01007     }
    01008   } else {
    01009     /* As of version 17, we recalculate the custom graphic ID of waypoints
    01010      * from the GRF ID / station index. */
    01011     AfterLoadWaypoints();
    01012   }
    01013
    01014   /* From version 15, we moved a semaphore bit from bit 2 to bit 3 in m4, making
    01015    *  room for PBS. Now in version 21 move it back :P. */
    01016   if (CheckSavegameVersion(21) && !CheckSavegameVersion(15)) {
    01017     for (TileIndex t = 0; t < map_size; t++) {
    01018       switch (GetTileType(t)) {
    01019         case MP_RAILWAY:
    01020           if (HasSignals(t)) {
    01021             /* convert PBS signals to combo-signals */
    01022             if (HasBit(_m[t].m2, 2)) SetSignalType(t, TRACK_X, SIGTYPE_COMBO);
    01023
    01024             /* move the signal variant back */
    01025             SetSignalVariant(t, TRACK_X, HasBit(_m[t].m2, 3) ? SIG_SEMAPHORE : SIG_ELECTRIC);
    01026             ClrBit(_m[t].m2, 3);
    01027           }
    01028
    01029           /* Clear PBS reservation on track */
    01030           if (!IsRailDepotTile(t)) {
    01031             SB(_m[t].m4, 4, 4, 0);
    01032           } else {
    01033             ClrBit(_m[t].m3, 6);
    01034           }
    01035           break;
    01036
    01037         case MP_STATION: // Clear PBS reservation on station
    01038           ClrBit(_m[t].m3, 6);
    01039           break;
    01040
    01041         default: break;
    01042       }
    01043     }
    01044   }
    01045
    01046   if (CheckSavegameVersion(25)) {
    01047     Vehicle *v;
    01048     FOR_ALL_VEHICLES(v) {
    01049       if (v->type == VEH_ROAD) {
    01050         v->vehstatus &= ~0x40;
    01051         v->u.road.slot = NULL;
    01052         v->u.road.slot_age = 0;
    01053       }
    01054     }
    01055   } else {
    01056     Vehicle *v;
    01057     FOR_ALL_VEHICLES(v) {
    01058       if (v->type == VEH_ROAD && v->u.road.slot != NULL) v->u.road.slot->num_vehicles++;
    01059     }
    01060   }
    01061
    01062   if (CheckSavegameVersion(26)) {
    01063     Station *st;
    01064     FOR_ALL_STATIONS(st) {
    01065       st->last_vehicle_type = VEH_INVALID;
    01066     }
    01067   }
    01068
    01069   YapfNotifyTrackLayoutChange(INVALID_TILE, INVALID_TRACK);
    01070
    01071   if (CheckSavegameVersion(34)) FOR_ALL_COMPANIES(c) ResetCompanyLivery(c);
    01072
    01073   FOR_ALL_COMPANIES(c) {
    01074     c->avail_railtypes = GetCompanyRailtypes(c->index);
    01075     c->avail_roadtypes = GetCompanyRoadtypes(c->index);
    01076   }
    01077
    01078   if (!CheckSavegameVersion(27)) AfterLoadStations();
    01079
    01080   /* Time starts at 0 instead of 1920.
    01081    * Account for this in older games by adding an offset */
    01082   if (CheckSavegameVersion(31)) {
    01083     Station *st;
    01084     Waypoint *wp;
    01085     Engine *e;
    01086     Industry *i;
    01087     Vehicle *v;
    01088
    01089     _date += DAYS_TILL_ORIGINAL_BASE_YEAR;
    01090     _cur_year += ORIGINAL_BASE_YEAR;
    01091
    01092     FOR_ALL_STATIONS(st)  st->build_date      += DAYS_TILL_ORIGINAL_BASE_YEAR;
    01093     FOR_ALL_WAYPOINTS(wp) wp->build_date      += DAYS_TILL_ORIGINAL_BASE_YEAR;
    01094     FOR_ALL_ENGINES(e)    e->intro_date       += DAYS_TILL_ORIGINAL_BASE_YEAR;
    01095     FOR_ALL_COMPANIES(c)  c->inaugurated_year += ORIGINAL_BASE_YEAR;
    01096     FOR_ALL_INDUSTRIES(i) i->last_prod_year   += ORIGINAL_BASE_YEAR;
    01097
    01098     FOR_ALL_VEHICLES(v) {
    01099       v->date_of_last_service += DAYS_TILL_ORIGINAL_BASE_YEAR;
    01100       v->build_year += ORIGINAL_BASE_YEAR;
    01101     }
    01102   }
    01103
    01104   /* From 32 on we save the industry who made the farmland.
    01105    *  To give this prettyness to old savegames, we remove all farmfields and
    01106    *  plant new ones. */
    01107   if (CheckSavegameVersion(32)) {
    01108     Industry *i;
    01109
    01110     for (TileIndex t = 0; t < map_size; t++) {
    01111       if (IsTileType(t, MP_CLEAR) && IsClearGround(t, CLEAR_FIELDS)) {
    01112         /* remove fields */
    01113         MakeClear(t, CLEAR_GRASS, 3);
    01114       } else if (IsTileType(t, MP_CLEAR) || IsTileType(t, MP_TREES)) {
    01115         /* remove fences around fields */
    01116         SetFenceSE(t, 0);
    01117         SetFenceSW(t, 0);
    01118       }
    01119     }
    01120
    01121     FOR_ALL_INDUSTRIES(i) {
    01122       uint j;
    01123
    01124       if (GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_PLANT_ON_BUILT) {
    01125         for (j = 0; j != 50; j++) PlantRandomFarmField(i);
    01126       }
    01127     }
    01128   }
    01129
    01130   /* Setting no refit flags to all orders in savegames from before refit in orders were added */
    01131   if (CheckSavegameVersion(36)) {
    01132     Order *order;
    01133     Vehicle *v;
    01134
    01135     FOR_ALL_ORDERS(order) {
    01136       order->SetRefit(CT_NO_REFIT);
    01137     }
    01138
    01139     FOR_ALL_VEHICLES(v) {
    01140       v->current_order.SetRefit(CT_NO_REFIT);
    01141     }
    01142   }
    01143
    01144   /* from version 38 we have optional elrails, since we cannot know the
    01145    * preference of a user, let elrails enabled; it can be disabled manually */
    01146   if (CheckSavegameVersion(38)) _settings_game.vehicle.disable_elrails = false;
    01147   /* do the same as when elrails were enabled/disabled manually just now */
    01148   SettingsDisableElrail(_settings_game.vehicle.disable_elrails);
    01149   InitializeRailGUI();
    01150
    01151   /* From version 53, the map array was changed for house tiles to allow
    01152    * space for newhouses grf features. A new byte, m7, was also added. */
    01153   if (CheckSavegameVersion(53)) {
    01154     for (TileIndex t = 0; t < map_size; t++) {
    01155       if (IsTileType(t, MP_HOUSE)) {
    01156         if (GB(_m[t].m3, 6, 2) != TOWN_HOUSE_COMPLETED) {
    01157           /* Move the construction stage from m3[7..6] to m5[5..4].
    01158            * The construction counter does not have to move. */
    01159           SB(_m[t].m5, 3, 2, GB(_m[t].m3, 6, 2));
    01160           SB(_m[t].m3, 6, 2, 0);
    01161
    01162           /* The "house is completed" bit is now in m6[2]. */
    01163           SetHouseCompleted(t, false);
    01164         } else {
    01165           /* The "lift has destination" bit has been moved from
    01166            * m5[7] to m7[0]. */
    01167           SB(_me[t].m7, 0, 1, HasBit(_m[t].m5, 7));
    01168           ClrBit(_m[t].m5, 7);
    01169
    01170           /* The "lift is moving" bit has been removed, as it does
    01171            * the same job as the "lift has destination" bit. */
    01172           ClrBit(_m[t].m1, 7);
    01173
    01174           /* The position of the lift goes from m1[7..0] to m6[7..2],
    01175            * making m1 totally free, now. The lift position does not
    01176            * have to be a full byte since the maximum value is 36. */
    01177           SetLiftPosition(t, GB(_m[t].m1, 0, 6 ));
    01178
    01179           _m[t].m1 = 0;
    01180           _m[t].m3 = 0;
    01181           SetHouseCompleted(t, true);
    01182         }
    01183       }
    01184     }
    01185   }
    01186
    01187   /* Check and update house and town values */
    01188   UpdateHousesAndTowns();
    01189
    01190   if (CheckSavegameVersion(43)) {
    01191     for (TileIndex t = 0; t < map_size; t++) {
    01192       if (IsTileType(t, MP_INDUSTRY)) {
    01193         switch (GetIndustryGfx(t)) {
    01194           case GFX_POWERPLANT_SPARKS:
    01195             SetIndustryAnimationState(t, GB(_m[t].m1, 2, 5));
    01196             break;
    01197
    01198           case GFX_OILWELL_ANIMATED_1:
    01199           case GFX_OILWELL_ANIMATED_2:
    01200           case GFX_OILWELL_ANIMATED_3:
    01201             SetIndustryAnimationState(t, GB(_m[t].m1, 0, 2));
    01202             break;
    01203
    01204           case GFX_COAL_MINE_TOWER_ANIMATED:
    01205           case GFX_COPPER_MINE_TOWER_ANIMATED:
    01206           case GFX_GOLD_MINE_TOWER_ANIMATED:
    01207              SetIndustryAnimationState(t, _m[t].m1);
    01208              break;
    01209
    01210           default: // No animation states to change
    01211             break;
    01212         }
    01213       }
    01214     }
    01215   }
    01216
    01217   if (CheckSavegameVersion(44)) {
    01218     Vehicle *v;
    01219     /* If we remove a station while cargo from it is still enroute, payment calculation will assume
    01220      * 0, 0 to be the source of the cargo, resulting in very high payments usually. v->source_xy
    01221      * stores the coordinates, preserving them even if the station is removed. However, if a game is loaded
    01222      * where this situation exists, the cargo-source information is lost. in this case, we set the source
    01223      * to the current tile of the vehicle to prevent excessive profits
    01224      */
    01225     FOR_ALL_VEHICLES(v) {
    01226       const CargoList::List *packets = v->cargo.Packets();
    01227       for (CargoList::List::const_iterator it = packets->begin(); it != packets->end(); it++) {
    01228         CargoPacket *cp = *it;
    01229         cp->source_xy = IsValidStationID(cp->source) ? GetStation(cp->source)->xy : v->tile;
    01230         cp->loaded_at_xy = cp->source_xy;
    01231       }
    01232       v->cargo.InvalidateCache();
    01233     }
    01234
    01235     /* Store position of the station where the goods come from, so there
    01236      * are no very high payments when stations get removed. However, if the
    01237      * station where the goods came from is already removed, the source
    01238      * information is lost. In that case we set it to the position of this
    01239      * station */
    01240     Station *st;
    01241     FOR_ALL_STATIONS(st) {
    01242       for (CargoID c = 0; c < NUM_CARGO; c++) {
    01243         GoodsEntry *ge = &st->goods[c];
    01244
    01245         const CargoList::List *packets = ge->cargo.Packets();
    01246         for (CargoList::List::const_iterator it = packets->begin(); it != packets->end(); it++) {
    01247           CargoPacket *cp = *it;
    01248           cp->source_xy = IsValidStationID(cp->source) ? GetStation(cp->source)->xy : st->xy;
    01249           cp->loaded_at_xy = cp->source_xy;
    01250         }
    01251       }
    01252     }
    01253   }
    01254
    01255   if (CheckSavegameVersion(45)) {
    01256     Vehicle *v;
    01257     /* Originally just the fact that some cargo had been paid for was
    01258      * stored to stop people cheating and cashing in several times. This
    01259      * wasn't enough though as it was cleared when the vehicle started
    01260      * loading again, even if it didn't actually load anything, so now the
    01261      * amount of cargo that has been paid for is stored. */
    01262     FOR_ALL_VEHICLES(v) {
    01263       const CargoList::List *packets = v->cargo.Packets();
    01264       for (CargoList::List::const_iterator it = packets->begin(); it != packets->end(); it++) {
    01265         CargoPacket *cp = *it;
    01266         cp->paid_for = HasBit(v->vehicle_flags, 2);
    01267       }
    01268       ClrBit(v->vehicle_flags, 2);
    01269       v->cargo.InvalidateCache();
    01270     }
    01271   }
    01272
    01273   /* Buoys do now store the owner of the previous water tile, which can never
    01274    * be OWNER_NONE. So replace OWNER_NONE with OWNER_WATER. */
    01275   if (CheckSavegameVersion(46)) {
    01276     Station *st;
    01277     FOR_ALL_STATIONS(st) {
    01278       if (st->IsBuoy() && IsTileOwner(st->xy, OWNER_NONE) && TileHeight(st->xy) == 0) SetTileOwner(st->xy, OWNER_WATER);
    01279     }
    01280   }
    01281
    01282   if (CheckSavegameVersion(50)) {
    01283     Vehicle *v;
    01284     /* Aircraft units changed from 8 mph to 1 km/h */
    01285     FOR_ALL_VEHICLES(v) {
    01286       if (v->type == VEH_AIRCRAFT && v->subtype <= AIR_AIRCRAFT) {
    01287         const AircraftVehicleInfo *avi = AircraftVehInfo(v->engine_type);
    01288         v->cur_speed *= 129;
    01289         v->cur_speed /= 10;
    01290         v->max_speed = avi->max_speed;
    01291         v->acceleration = avi->acceleration;
    01292       }
    01293     }
    01294   }
    01295
    01296   if (CheckSavegameVersion(49)) FOR_ALL_COMPANIES(c) c->face = ConvertFromOldCompanyManagerFace(c->face);
    01297
    01298   if (CheckSavegameVersion(52)) {
    01299     for (TileIndex t = 0; t < map_size; t++) {
    01300       if (IsStatueTile(t)) {
    01301         _m[t].m2 = CalcClosestTownFromTile(t)->index;
    01302       }
    01303     }
    01304   }
    01305
    01306   /* A setting containing the proportion of towns that grow twice as
    01307    * fast was added in version 54. From version 56 this is now saved in the
    01308    * town as cities can be built specifically in the scenario editor. */
    01309   if (CheckSavegameVersion(56)) {
    01310     Town *t;
    01311
    01312     FOR_ALL_TOWNS(t) {
    01313       if (_settings_game.economy.larger_towns != 0 && (t->index % _settings_game.economy.larger_towns) == 0) {
    01314         t->larger_town = true;
    01315       }
    01316     }
    01317   }
    01318
    01319   if (CheckSavegameVersion(57)) {
    01320     Vehicle *v;
    01321     /* Added a FIFO queue of vehicles loading at stations */
    01322     FOR_ALL_VEHICLES(v) {
    01323       if ((v->type != VEH_TRAIN || IsFrontEngine(v)) &&  // for all locs
    01324           !(v->vehstatus & (VS_STOPPED | VS_CRASHED)) && // not stopped or crashed
    01325           v->current_order.IsType(OT_LOADING)) {         // loading
    01326         GetStation(v->last_station_visited)->loading_vehicles.push_back(v);
    01327
    01328         /* The loading finished flag is *only* set when actually completely
    01329          * finished. Because the vehicle is loading, it is not finished. */
    01330         ClrBit(v->vehicle_flags, VF_LOADING_FINISHED);
    01331       }
    01332     }
    01333   } else if (CheckSavegameVersion(59)) {
    01334     /* For some reason non-loading vehicles could be in the station's loading vehicle list */
    01335
    01336     Station *st;
    01337     FOR_ALL_STATIONS(st) {
    01338       std::list<Vehicle *>::iterator iter;
    01339       for (iter = st->loading_vehicles.begin(); iter != st->loading_vehicles.end();) {
    01340         Vehicle *v = *iter;
    01341         iter++;
    01342         if (!v->current_order.IsType(OT_LOADING)) st->loading_vehicles.remove(v);
    01343       }
    01344     }
    01345   }
    01346
    01347   if (CheckSavegameVersion(58)) {
    01348     /* Setting difficulty number_industries other than zero get bumped to +1
    01349      * since a new option (very low at position1) has been added */
    01350     if (_settings_game.difficulty.number_industries > 0) {
    01351       _settings_game.difficulty.number_industries++;
    01352     }
    01353
    01354     /* Same goes for number of towns, although no test is needed, just an increment */
    01355     _settings_game.difficulty.number_towns++;
    01356   }
    01357
    01358   if (CheckSavegameVersion(64)) {
    01359     /* copy the signal type/variant and move signal states bits */
    01360     for (TileIndex t = 0; t < map_size; t++) {
    01361       if (IsTileType(t, MP_RAILWAY) && HasSignals(t)) {
    01362         SetSignalStates(t, GB(_m[t].m2, 4, 4));
    01363         SetSignalVariant(t, INVALID_TRACK, GetSignalVariant(t, TRACK_X));
    01364         SetSignalType(t, INVALID_TRACK, GetSignalType(t, TRACK_X));
    01365         ClrBit(_m[t].m2, 7);
    01366       }
    01367     }
    01368   }
    01369
    01370   if (CheckSavegameVersion(69)) {
    01371     /* In some old savegames a bit was cleared when it should not be cleared */
    01372     Vehicle *v;
    01373     FOR_ALL_VEHICLES(v) {
    01374       if (v->type == VEH_ROAD && (v->u.road.state == 250 || v->u.road.state == 251)) {
    01375         SetBit(v->u.road.state, RVS_IS_STOPPING);
    01376       }
    01377     }
    01378   }
    01379
    01380   if (CheckSavegameVersion(70)) {
    01381     /* Added variables to support newindustries */
    01382     Industry *i;
    01383     FOR_ALL_INDUSTRIES(i) i->founder = OWNER_NONE;
    01384   }
    01385
    01386   /* From version 82, old style canals (above sealevel (0), WATER owner) are no longer supported.
    01387       Replace the owner for those by OWNER_NONE. */
    01388   if (CheckSavegameVersion(82)) {
    01389     for (TileIndex t = 0; t < map_size; t++) {
    01390       if (IsTileType(t, MP_WATER) &&
    01391           GetWaterTileType(t) == WATER_TILE_CLEAR &&
    01392           GetTileOwner(t) == OWNER_WATER &&
    01393           TileHeight(t) != 0) {
    01394         SetTileOwner(t, OWNER_NONE);
    01395       }
    01396     }
    01397   }
    01398
    01399   /*
    01400    * Add the 'previous' owner to the ship depots so we can reset it with
    01401    * the correct values when it gets destroyed. This prevents that
    01402    * someone can remove canals owned by somebody else and it prevents
    01403    * making floods using the removal of ship depots.
    01404    */
    01405   if (CheckSavegameVersion(83)) {
    01406     for (TileIndex t = 0; t < map_size; t++) {
    01407       if (IsTileType(t, MP_WATER) && IsShipDepot(t)) {
    01408         _m[t].m4 = (TileHeight(t) == 0) ? OWNER_WATER : OWNER_NONE;
    01409       }
    01410     }
    01411   }
    01412
    01413   if (CheckSavegameVersion(74)) {
    01414     Station *st;
    01415     FOR_ALL_STATIONS(st) {
    01416       for (CargoID c = 0; c < NUM_CARGO; c++) {
    01417         st->goods[c].last_speed = 0;
    01418         if (st->goods[c].cargo.Count() != 0) SetBit(st->goods[c].acceptance_pickup, GoodsEntry::PICKUP);
    01419       }
    01420     }
    01421   }
    01422
    01423   if (CheckSavegameVersion(78)) {
    01424     Industry *i;
    01425     uint j;
    01426     FOR_ALL_INDUSTRIES(i) {
    01427       const IndustrySpec *indsp = GetIndustrySpec(i->type);
    01428       for (j = 0; j < lengthof(i->produced_cargo); j++) {
    01429         i->produced_cargo[j] = indsp->produced_cargo[j];
    01430       }
    01431       for (j = 0; j < lengthof(i->accepts_cargo); j++) {
    01432         i->accepts_cargo[j] = indsp->accepts_cargo[j];
    01433       }
    01434     }
    01435   }
    01436
    01437   /* Before version 81, the density of grass was always stored as zero, and
    01438    * grassy trees were always drawn fully grassy. Furthermore, trees on rough
    01439    * land used to have zero density, now they have full density. Therefore,
    01440    * make all grassy/rough land trees have a density of 3. */
    01441   if (CheckSavegameVersion(81)) {
    01442     for (TileIndex t = 0; t < map_size; t++) {
    01443       if (GetTileType(t) == MP_TREES) {
    01444         TreeGround groundType = GetTreeGround(t);
    01445         if (groundType != TREE_GROUND_SNOW_DESERT) SetTreeGroundDensity(t, groundType, 3);
    01446       }
    01447     }
    01448   }
    01449
    01450
    01451   if (CheckSavegameVersion(93)) {
    01452     /* Rework of orders. */
    01453     Order *order;
    01454     FOR_ALL_ORDERS(order) order->ConvertFromOldSavegame();
    01455
    01456     Vehicle *v;
    01457     FOR_ALL_VEHICLES(v) {
    01458       if (v->orders.list != NULL && v->orders.list->GetFirstOrder() != NULL && !v->orders.list->GetFirstOrder()->IsValid()) {
    01459         v->orders.list->FreeChain();
    01460         v->orders.list = NULL;
    01461       }
    01462
    01463       v->current_order.ConvertFromOldSavegame();
    01464       if (v->type == VEH_ROAD && v->IsPrimaryVehicle() && v->FirstShared() == v) {
    01465         FOR_VEHICLE_ORDERS(v, order) order->SetNonStopType(ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS);
    01466       }
    01467     }
    01468   } else if (CheckSavegameVersion(94)) {
    01469     /* Unload and transfer are now mutual exclusive. */
    01470     Order *order;
    01471     FOR_ALL_ORDERS(order) {
    01472       if ((order->GetUnloadType() & (OUFB_UNLOAD | OUFB_TRANSFER)) == (OUFB_UNLOAD | OUFB_TRANSFER)) {
    01473         order->SetUnloadType(OUFB_TRANSFER);
    01474         order->SetLoadType(OLFB_NO_LOAD);
    01475       }
    01476     }
    01477
    01478     Vehicle *v;
    01479     FOR_ALL_VEHICLES(v) {
    01480       if ((v->current_order.GetUnloadType() & (OUFB_UNLOAD | OUFB_TRANSFER)) == (OUFB_UNLOAD | OUFB_TRANSFER)) {
    01481         v->current_order.SetUnloadType(OUFB_TRANSFER);
    01482         v->current_order.SetLoadType(OLFB_NO_LOAD);
    01483       }
    01484     }
    01485   }
    01486
    01487   if (CheckSavegameVersion(84)) {
    01488     /* Update go to buoy orders because they are just waypoints */
    01489     Order *order;
    01490     FOR_ALL_ORDERS(order) {
    01491       if (order->IsType(OT_GOTO_STATION) && GetStation(order->GetDestination())->IsBuoy()) {
    01492         order->SetLoadType(OLF_LOAD_IF_POSSIBLE);
    01493         order->SetUnloadType(OUF_UNLOAD_IF_POSSIBLE);
    01494       }
    01495     }
    01496
    01497     /* Set all share owners to INVALID_COMPANY for
    01498      * 1) all inactive companies
    01499      *     (when inactive companies were stored in the savegame - TTD, TTDP and some
    01500      *      *really* old revisions of OTTD; else it is already set in InitializeCompanies())
    01501      * 2) shares that are owned by inactive companies or self
    01502      *     (caused by cheating clients in earlier revisions) */
    01503     FOR_ALL_COMPANIES(c) {
    01504       for (uint i = 0; i < 4; i++) {
    01505         CompanyID company = c->share_owners[i];
    01506         if (company == INVALID_COMPANY) continue;
    01507         if (!IsValidCompanyID(company) || company == c->index) c->share_owners[i] = INVALID_COMPANY;
    01508       }
    01509     }
    01510   }
    01511
    01512   if (CheckSavegameVersion(86)) {
    01513     for (TileIndex t = 0; t < map_size; t++) {
    01514       /* Move river flag and update canals to use water class */
    01515       if (IsTileType(t, MP_WATER)) {
    01516         if (GetWaterClass(t) != WATER_CLASS_RIVER) {
    01517           if (IsWater(t)) {
    01518             Owner o = GetTileOwner(t);
    01519             if (o == OWNER_WATER) {
    01520               MakeWater(t);
    01521             } else {
    01522               MakeCanal(t, o, Random());
    01523             }
    01524           } else if (IsShipDepot(t)) {
    01525             Owner o = (Owner)_m[t].m4; // Original water owner
    01526             SetWaterClass(t, o == OWNER_WATER ? WATER_CLASS_SEA : WATER_CLASS_CANAL);
    01527           }
    01528         }
    01529       }
    01530     }
    01531
    01532     /* Update locks, depots, docks and buoys to have a water class based
    01533      * on its neighbouring tiles. Done after river and canal updates to
    01534      * ensure neighbours are correct. */
    01535     for (TileIndex t = 0; t < map_size; t++) {
    01536       if (GetTileSlope(t, NULL) != SLOPE_FLAT) continue;
    01537
    01538       if (IsTileType(t, MP_WATER) && IsLock(t)) SetWaterClassDependingOnSurroundings(t, false);
    01539       if (IsTileType(t, MP_STATION) && (IsDock(t) || IsBuoy(t))) SetWaterClassDependingOnSurroundings(t, false);
    01540     }
    01541   }
    01542
    01543   if (CheckSavegameVersion(87)) {
    01544     for (TileIndex t = 0; t < map_size; t++) {
    01545       /* skip oil rigs at borders! */
    01546       if ((IsTileType(t, MP_WATER) || IsBuoyTile(t)) &&
    01547           (TileX(t) == 0 || TileY(t) == 0 || TileX(t) == MapMaxX() - 1 || TileY(t) == MapMaxY() - 1)) {
    01548         /* Some version 86 savegames have wrong water class at map borders (under buoy, or after removing buoy).
    01549          * This conversion has to be done before buoys with invalid owner are removed. */
    01550         SetWaterClass(t, WATER_CLASS_SEA);
    01551       }
    01552
    01553       if (IsBuoyTile(t) || IsDriveThroughStopTile(t) || IsTileType(t, MP_WATER)) {
    01554         Owner o = GetTileOwner(t);
    01555         if (o < MAX_COMPANIES && !IsValidCompanyID(o)) {
    01556           _current_company = o;
    01557           ChangeTileOwner(t, o, INVALID_OWNER);
    01558         }
    01559         if (IsBuoyTile(t)) {
    01560           /* reset buoy owner to OWNER_NONE in the station struct
    01561            * (even if it is owned by active company) */
    01562           GetStationByTile(t)->owner = OWNER_NONE;
    01563         }
    01564       } else if (IsTileType(t, MP_ROAD)) {
    01565         /* works for all RoadTileType */
    01566         for (RoadType rt = ROADTYPE_ROAD; rt < ROADTYPE_END; rt++) {
    01567           /* update even non-existing road types to update tile owner too */
    01568           Owner o = GetRoadOwner(t, rt);
    01569           if (o < MAX_COMPANIES && !IsValidCompanyID(o)) SetRoadOwner(t, rt, OWNER_NONE);
    01570         }
    01571         if (IsLevelCrossing(t)) {
    01572           if (!IsValidCompanyID(GetTileOwner(t))) FixOwnerOfRailTrack(t);
    01573         }
    01574       } else if (IsTileType(t, MP_RAILWAY) && IsPlainRailTile(t)) {
    01575         if (!IsValidCompanyID(GetTileOwner(t))) FixOwnerOfRailTrack(t);
    01576       }
    01577     }
    01578
    01579     /* Convert old PF settings to new */
    01580     if (_settings_game.pf.yapf.rail_use_yapf || CheckSavegameVersion(28)) {
    01581       _settings_game.pf.pathfinder_for_trains = VPF_YAPF;
    01582     } else {
    01583       _settings_game.pf.pathfinder_for_trains = (_settings_game.pf.new_pathfinding_all ? VPF_NPF : VPF_NTP);
    01584     }
    01585
    01586     if (_settings_game.pf.yapf.road_use_yapf || CheckSavegameVersion(28)) {
    01587       _settings_game.pf.pathfinder_for_roadvehs = VPF_YAPF;
    01588     } else {
    01589       _settings_game.pf.pathfinder_for_roadvehs = (_settings_game.pf.new_pathfinding_all ? VPF_NPF : VPF_OPF);
    01590     }
    01591
    01592     if (_settings_game.pf.yapf.ship_use_yapf) {
    01593       _settings_game.pf.pathfinder_for_ships = VPF_YAPF;
    01594     } else {
    01595       _settings_game.pf.pathfinder_for_ships = (_settings_game.pf.new_pathfinding_all ? VPF_NPF : VPF_OPF);
    01596     }
    01597   }
    01598
    01599   if (CheckSavegameVersion(88)) {
    01600     /* Profits are now with 8 bit fract */
    01601     Vehicle *v;
    01602     FOR_ALL_VEHICLES(v) {
    01603       v->profit_this_year <<= 8;
    01604       v->profit_last_year <<= 8;
    01605       v->running_ticks = 0;
    01606     }
    01607   }
    01608
    01609   if (CheckSavegameVersion(91)) {
    01610     /* Increase HouseAnimationFrame from 5 to 7 bits */
    01611     for (TileIndex t = 0; t < map_size; t++) {
    01612       if (IsTileType(t, MP_HOUSE) && GetHouseType(t) >= NEW_HOUSE_OFFSET) {
    01613         SetHouseAnimationFrame(t, GB(_m[t].m6, 3, 5));
    01614       }
    01615     }
    01616   }
    01617
    01618   if (CheckSavegameVersion(62)) {
    01619     /* Remove all trams from savegames without tram support.
    01620      * There would be trams without tram track under causing crashes sooner or later. */
    01621     Vehicle *v;
    01622     FOR_ALL_VEHICLES(v) {
    01623       if (v->type == VEH_ROAD && v->First() == v &&
    01624           HasBit(EngInfo(v->engine_type)->misc_flags, EF_ROAD_TRAM)) {
    01625         if (_switch_mode_errorstr == INVALID_STRING_ID || _switch_mode_errorstr == STR_NEWGRF_COMPATIBLE_LOAD_WARNING) {
    01626           _switch_mode_errorstr = STR_LOADGAME_REMOVED_TRAMS;
    01627         }
    01628         delete v;
    01629       }
    01630     }
    01631   }
    01632
    01633   if (CheckSavegameVersion(99)) {
    01634     for (TileIndex t = 0; t < map_size; t++) {
    01635       /* Set newly introduced WaterClass of industry tiles */
    01636       if (IsTileType(t, MP_STATION) && IsOilRig(t)) {
    01637         SetWaterClassDependingOnSurroundings(t, true);
    01638       }
    01639       if (IsTileType(t, MP_INDUSTRY)) {
    01640         if ((GetIndustrySpec(GetIndustryType(t))->behaviour & INDUSTRYBEH_BUILT_ONWATER) != 0) {
    01641           SetWaterClassDependingOnSurroundings(t, true);
    01642         } else {
    01643           SetWaterClass(t, WATER_CLASS_INVALID);
    01644         }
    01645       }
    01646
    01647       /* Replace "house construction year" with "house age" */
    01648       if (IsTileType(t, MP_HOUSE) && IsHouseCompleted(t)) {
    01649         _m[t].m5 = Clamp(_cur_year - (_m[t].m5 + ORIGINAL_BASE_YEAR), 0, 0xFF);
    01650       }
    01651     }
    01652   }
    01653
    01654   /* Move the signal variant back up one bit for PBS. We don't convert the old PBS
    01655    * format here, as an old layout wouldn't work properly anyway. To be safe, we
    01656    * clear any possible PBS reservations as well. */
    01657   if (CheckSavegameVersion(100)) {
    01658     for (TileIndex t = 0; t < map_size; t++) {
    01659       switch (GetTileType(t)) {
    01660         case MP_RAILWAY:
    01661           if (HasSignals(t)) {
    01662             /* move the signal variant */
    01663             SetSignalVariant(t, TRACK_UPPER, HasBit(_m[t].m2, 2) ? SIG_SEMAPHORE : SIG_ELECTRIC);
    01664             SetSignalVariant(t, TRACK_LOWER, HasBit(_m[t].m2, 6) ? SIG_SEMAPHORE : SIG_ELECTRIC);
    01665             ClrBit(_m[t].m2, 2);
    01666             ClrBit(_m[t].m2, 6);
    01667           }
    01668
    01669           /* Clear PBS reservation on track */
    01670           if (IsRailDepot(t) ||IsRailWaypoint(t)) {
    01671             SetDepotWaypointReservation(t, false);
    01672           } else {
    01673             SetTrackReservation(t, TRACK_BIT_NONE);
    01674           }
    01675           break;
    01676
    01677         case MP_ROAD: // Clear PBS reservation on crossing
    01678           if (IsLevelCrossing(t)) SetCrossingReservation(t, false);
    01679           break;
    01680
    01681         case MP_STATION: // Clear PBS reservation on station
    01682           if (IsRailwayStation(t)) SetRailwayStationReservation(t, false);
    01683           break;
    01684
    01685         case MP_TUNNELBRIDGE: // Clear PBS reservation on tunnels/birdges
    01686           if (GetTunnelBridgeTransportType(t) == TRANSPORT_RAIL) SetTunnelBridgeReservation(t, false);
    01687           break;
    01688
    01689         default: break;
    01690       }
    01691     }
    01692   }
    01693
    01694   /* Reserve all tracks trains are currently on. */
    01695   if (CheckSavegameVersion(101)) {
    01696     Vehicle *v;
    01697     FOR_ALL_VEHICLES(v) {
    01698       if (v->type == VEH_TRAIN) {
    01699         if ((v->u.rail.track & TRACK_BIT_WORMHOLE) == TRACK_BIT_WORMHOLE) {
    01700           TryReserveRailTrack(v->tile, DiagDirToDiagTrack(GetTunnelBridgeDirection(v->tile)));
    01701         } else if ((v->u.rail.track & TRACK_BIT_MASK) != TRACK_BIT_NONE) {
    01702           TryReserveRailTrack(v->tile, TrackBitsToTrack(v->u.rail.track));
    01703         }
    01704       }
    01705     }
    01706   }
    01707
    01708   if (CheckSavegameVersion(102)) {
    01709     for (TileIndex t = 0; t < map_size; t++) {
    01710       /* Now all crossings should be in correct state */
    01711       if (IsLevelCrossingTile(t)) UpdateLevelCrossing(t, false);
    01712     }
    01713   }
    01714
    01715   if (CheckSavegameVersion(103)) {
    01716     /* Non-town-owned roads now store the closest town */
    01717     UpdateNearestTownForRoadTiles(false);
    01718
    01719     /* signs with invalid owner left from older savegames */
    01720     Sign *si;
    01721     FOR_ALL_SIGNS(si) {
    01722       if (si->owner != OWNER_NONE && !IsValidCompanyID(si->owner)) si->owner = OWNER_NONE;
    01723     }
    01724
    01725     /* Station can get named based on an industry type, but the current ones
    01726      * are not, so mark them as if they are not named by an industry. */
    01727     Station *st;
    01728     FOR_ALL_STATIONS(st) {
    01729       st->indtype = IT_INVALID;
    01730     }
    01731   }
    01732
    01733   if (CheckSavegameVersion(104)) {
    01734     Vehicle *v;
    01735     FOR_ALL_VEHICLES(v) {
    01736       /* Set engine_type of shadow and rotor */
    01737       if (v->type == VEH_AIRCRAFT && !IsNormalAircraft(v)) {
    01738         v->engine_type = v->First()->engine_type;
    01739       }
    01740     }
    01741
    01742     /* More companies ... */
    01743     Company *c;
    01744     FOR_ALL_COMPANIES(c) {
    01745       if (c->bankrupt_asked == 0xFF) c->bankrupt_asked = 0xFFFF;
    01746     }
    01747
    01748     Engine *e;
    01749     FOR_ALL_ENGINES(e) {
    01750       if (e->company_avail == 0xFF) e->company_avail = 0xFFFF;
    01751     }
    01752
    01753     Town *t;
    01754     FOR_ALL_TOWNS(t) {
    01755       if (t->have_ratings == 0xFF) t->have_ratings = 0xFFFF;
    01756       for (uint i = 8; i != MAX_COMPANIES; i++) t->ratings[i] = RATING_INITIAL;
    01757     }
    01758   }
    01759
    01760   if (CheckSavegameVersion(112)) {
    01761     for (TileIndex t = 0; t < map_size; t++) {
    01762       /* Check for HQ bit being set, instead of using map accessor,
    01763        * since we've already changed it code-wise */
    01764       if (IsTileType(t, MP_UNMOVABLE) && HasBit(_m[t].m5, 7)) {
    01765         /* Move size and part identification of HQ out of the m5 attribute,
    01766          * on new locations */
    01767         uint8 old_m5 = _m[t].m5;
    01768         _m[t].m5 = UNMOVABLE_HQ;
    01769         SetCompanyHQSize(t, GB(old_m5, 2, 3));
    01770         SetCompanyHQSection(t, GB(old_m5, 0, 2));
    01771       }
    01772     }
    01773   }
    01774
    01775   if (CheckSavegameVersion(113)) {
    01776     /* allow_town_roads is added, set it if town_layout wasn't TL_NO_ROADS */
    01777     if (_settings_game.economy.town_layout == 0) { // was TL_NO_ROADS
    01778       _settings_game.economy.allow_town_roads = false;
    01779       _settings_game.economy.town_layout = TL_BETTER_ROADS;
    01780     } else {
    01781       _settings_game.economy.allow_town_roads = true;
    01782       _settings_game.economy.town_layout = _settings_game.economy.town_layout - 1;
    01783     }
    01784
    01785     /* Initialize layout of all towns. Older versions were using different
    01786      * generator for random town layout, use it if needed. */
    01787     Town *t;
    01788     FOR_ALL_TOWNS(t) {
    01789       if (_settings_game.economy.town_layout != TL_RANDOM) {
    01790         t->layout = _settings_game.economy.town_layout;
    01791         continue;
    01792       }
    01793
    01794       /* Use old layout randomizer code */
    01795       byte layout = TileHash(TileX(t->xy), TileY(t->xy)) % 6;
    01796       switch (layout) {
    01797         default: break;
    01798         case 5: layout = 1; break;
    01799         case 0: layout = 2; break;
    01800       }
    01801       t->layout = layout - 1;
    01802     }
    01803   }
    01804
    01805   if (CheckSavegameVersion(114)) {
    01806     /* There could be (deleted) stations with invalid owner, set owner to OWNER NONE.
    01807      * The conversion affects oil rigs and buoys too, but it doesn't matter as
    01808      * they have st->owner == OWNER_NONE already. */
    01809     Station *st;
    01810     FOR_ALL_STATIONS(st) {
    01811       if (!IsValidCompanyID(st->owner)) st->owner = OWNER_NONE;
    01812     }
    01813
    01814     /* Give owners to waypoints, based on rail tracks it is sitting on.
    01815      * If none is available, specify OWNER_NONE.
    01816      * This code was in CheckSavegameVersion(101) in the past, but in some cases,
    01817      * the owner of waypoints could be incorrect. */
    01818     Waypoint *wp;
    01819     FOR_ALL_WAYPOINTS(wp) {
    01820       Owner owner = IsTileType(wp->xy, MP_RAILWAY) ? GetTileOwner(wp->xy) : OWNER_NONE;
    01821       wp->owner = IsValidCompanyID(owner) ? owner : OWNER_NONE;
    01822     }
    01823   }
    01824
    01825   GamelogPrintDebug(1);
    01826
    01827   bool ret = InitializeWindowsAndCaches();
    01828   /* Restore the signals */
    01829   ResetSignalHandlers();
    01830   return ret;
    01831 }
    01832
    01839 void ReloadNewGRFData()
    01840 {
    01841   /* reload grf data */
    01842   GfxLoadSprites();
    01843   LoadStringWidthTable();
    01844   ResetEconomy();
    01845   /* reload vehicles */
    01846   ResetVehiclePosHash();
    01847   AfterLoadVehicles(false);
    01848   StartupEngines();
    01849   SetCachedEngineCounts();
    01850   /* update station and waypoint graphics */
    01851   AfterLoadWaypoints();
    01852   AfterLoadStations();
    01853   /* Check and update house and town values */
    01854   UpdateHousesAndTowns();
    01855   /* Update livery selection windows */
    01856   for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) InvalidateWindowData(WC_COMPANY_COLOUR, i, _loaded_newgrf_features.has_2CC);
    01857   /* redraw the whole screen */
    01858   MarkWholeScreenDirty();
    01859   CheckTrainsLengths();
    01860 }