# afterload.cpp

Go to the documentation of this file.

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