28
29
#include "../company_func.h"
29
30
#include "../road_cmd.h"
30
31
#include "../ai/ai.hpp"
32
#include "../animated_tile_func.h"
34
#include "../economy_base.h"
32
35
#include "table/strings.h"
34
37
#include "saveload_internal.h"
36
39
#include <signal.h>
38
41
extern StringID _switch_mode_errorstr;
39
extern Company *DoStartupNewCompany(bool is_ai);
42
extern Company *DoStartupNewCompany(bool is_ai, CompanyID company = INVALID_COMPANY);
40
43
extern void InitializeRailGUI();
239
242
typedef void (CDECL *SignalHandlerPointer)(int);
240
243
static SignalHandlerPointer _prev_segfault = NULL;
241
static SignalHandlerPointer _prev_abort = NULL;
244
static SignalHandlerPointer _prev_abort = NULL;
245
static SignalHandlerPointer _prev_fpe = NULL;
243
247
static void CDECL HandleSavegameLoadCrash(int signum);
249
253
static void SetSignalHandlers()
251
255
_prev_segfault = signal(SIGSEGV, HandleSavegameLoadCrash);
252
_prev_abort = signal(SIGABRT, HandleSavegameLoadCrash);
256
_prev_abort = signal(SIGABRT, HandleSavegameLoadCrash);
257
_prev_fpe = signal(SIGFPE, HandleSavegameLoadCrash);
260
265
signal(SIGSEGV, _prev_segfault);
261
266
signal(SIGABRT, _prev_abort);
267
signal(SIGFPE, _prev_fpe);
271
* Try to find the overridden GRF identifier of the given GRF.
272
* @param c the GRF to get the 'previous' version of.
273
* @return the GRF identifier or \a c if none could be found.
275
static const GRFIdentifier *GetOverriddenIdentifier(const GRFConfig *c)
277
const LoggedAction *la = &_gamelog_action[_gamelog_actions - 1];
278
if (la->at != GLAT_LOAD) return c;
280
const LoggedChange *lcend = &la->change[la->changes];
281
for (const LoggedChange *lc = la->change; lc != lcend; lc++) {
282
if (lc->ct == GLCT_GRFCOMPAT && lc->grfcompat.grfid == c->grfid) return &lc->grfcompat;
286
310
"savegame still crashes when all NewGRFs are found you should file a\n"
287
311
"bug report. The missing NewGRFs are:\n");
289
for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
313
for (const GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
290
314
if (HasBit(c->flags, GCF_COMPATIBLE)) {
315
const GRFIdentifier *replaced = GetOverriddenIdentifier(c);
292
md5sumToString(buf, lastof(buf), c->md5sum);
293
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);
317
md5sumToString(buf, lastof(buf), replaced->md5sum);
318
p += seprintf(p, lastof(buffer), "NewGRF %08X (checksum %s) not found.\n Loaded NewGRF \"%s\" with same GRF ID instead.\n", BSWAP32(c->grfid), buf, c->filename);
295
320
if (c->status == GCS_NOT_FOUND) {
297
322
md5sumToString(buf, lastof(buf), c->md5sum);
298
p += seprintf(p, lastof(buffer), "NewGRF %08X (%s) not found; checksum %s\n", BSWAP32(c->grfid), c->filename, buf);
323
p += seprintf(p, lastof(buffer), "NewGRF %08X (%s) not found; checksum %s.\n", BSWAP32(c->grfid), c->filename, buf);
302
327
ShowInfo(buffer);
304
SignalHandlerPointer call = signum == SIGSEGV ? _prev_segfault : _prev_abort;
329
SignalHandlerPointer call = NULL;
331
case SIGSEGV: call = _prev_segfault; break;
332
case SIGABRT: call = _prev_abort; break;
333
case SIGFPE: call = _prev_fpe; break;
334
default: NOT_REACHED();
305
336
if (call != NULL) call(signum);
510
/* Update all waypoints */
511
if (CheckSavegameVersion(12)) FixOldWaypoints();
513
544
/* make sure there is a town in the game */
514
545
if (_game_mode == GM_NORMAL && !ClosestTownFromTile(0, UINT_MAX)) {
515
546
SetSaveLoadError(STR_NO_TOWN_IN_SCENARIO);
531
562
if (!IsValidCompanyID(COMPANY_FIRST) && (!_networking || (_networking && _network_server && !_network_dedicated)))
532
563
DoStartupNewCompany(false);
565
/* Fix the cache for cargo payments. */
567
FOR_ALL_CARGO_PAYMENTS(cp) {
568
cp->front->cargo_payment = cp;
569
cp->current_station = cp->front->last_station_visited;
534
572
if (CheckSavegameVersion(72)) {
535
573
/* Locks/shiplifts in very old savegames had OWNER_WATER as owner */
536
574
for (TileIndex t = 0; t < MapSize(); t++) {
707
745
for (TileIndex t = 0; t < map_size; t++) {
708
746
switch (GetTileType(t)) {
710
if (IsPlainRailTile(t)) {
748
if (IsPlainRail(t)) {
711
749
/* Swap ground type and signal type for plain rail tiles, so the
712
750
* ground type uses the same bits as for depots and waypoints. */
713
751
uint tmp = GB(_m[t].m4, 0, 4);
1259
1297
* stored to stop people cheating and cashing in several times. This
1260
1298
* wasn't enough though as it was cleared when the vehicle started
1261
1299
* loading again, even if it didn't actually load anything, so now the
1262
* amount of cargo that has been paid for is stored. */
1300
* amount that has been paid is stored. */
1263
1301
FOR_ALL_VEHICLES(v) {
1264
const CargoList::List *packets = v->cargo.Packets();
1265
for (CargoList::List::const_iterator it = packets->begin(); it != packets->end(); it++) {
1266
CargoPacket *cp = *it;
1267
cp->paid_for = HasBit(v->vehicle_flags, 2);
1269
1302
ClrBit(v->vehicle_flags, 2);
1270
1303
v->cargo.InvalidateCache();
1572
1605
if (IsLevelCrossing(t)) {
1573
1606
if (!IsValidCompanyID(GetTileOwner(t))) FixOwnerOfRailTrack(t);
1575
} else if (IsTileType(t, MP_RAILWAY) && IsPlainRailTile(t)) {
1608
} else if (IsPlainRailTile(t)) {
1576
1609
if (!IsValidCompanyID(GetTileOwner(t))) FixOwnerOfRailTrack(t);
1872
if (!_cargo_payment_savegame) {
1873
/* We didn't store cargo payment yet, so make them for vehicles that are
1874
* currently at a station and loading/unloading. If they don't get any
1875
* payment anymore they just removed in the next load/unload cycle.
1878
FOR_ALL_STATIONS(st) {
1879
std::list<Vehicle *>::iterator iter;
1880
for (iter = st->loading_vehicles.begin(); iter != st->loading_vehicles.end(); ++iter) {
1882
assert(v->cargo_payment == NULL);
1883
v->cargo_payment = new CargoPayment(v);
1888
if (CheckSavegameVersion(122)) {
1889
/* Animated tiles would sometimes not be actually animated or
1890
* in case of old savegames duplicate. */
1892
extern TileIndex *_animated_tile_list;
1893
extern uint _animated_tile_count;
1895
for (uint i = 0; i < _animated_tile_count; /* Nothing */) {
1896
/* Remove if tile is not animated */
1897
bool remove = _tile_type_procs[GetTileType(_animated_tile_list[i])]->animate_tile_proc == NULL;
1899
/* and remove if duplicate */
1900
for (uint j = 0; !remove && j < i; j++) {
1901
remove = _animated_tile_list[i] == _animated_tile_list[j];
1905
DeleteAnimatedTile(_animated_tile_list[i]);
1911
/* Delete invalid subsidies possibly present in old versions (but converted to new savegame) */
1912
for (Subsidy *s = _subsidies; s < endof(_subsidies); s++) {
1913
if (s->cargo_type == CT_INVALID) continue;
1915
/* Station -> Station */
1916
const Station *from = IsValidStationID(s->from) ? GetStation(s->from) : NULL;
1917
const Station *to = IsValidStationID(s->to) ? GetStation(s->to) : NULL;
1918
if (from != NULL && to != NULL && from->owner == to->owner && IsValidCompanyID(from->owner)) continue;
1920
const CargoSpec *cs = GetCargo(s->cargo_type);
1921
switch (cs->town_effect) {
1925
if (IsValidTownID(s->from) && IsValidTownID(s->to)) continue;
1929
/* Industry -> Town */
1930
if (IsValidIndustryID(s->from) && IsValidTownID(s->to)) continue;
1933
/* Industry -> Industry */
1934
if (IsValidIndustryID(s->from) && IsValidIndustryID(s->to)) continue;
1938
s->cargo_type = CT_INVALID;
1839
1942
GamelogPrintDebug(1);
1841
1944
bool ret = InitializeWindowsAndCaches();
1862
1965
StartupEngines();
1863
1966
SetCachedEngineCounts();
1864
1967
/* update station and waypoint graphics */
1865
1969
AfterLoadWaypoints();
1866
1970
AfterLoadStations();
1867
1971
/* Check and update house and town values */
1868
1972
UpdateHousesAndTowns();
1869
1973
/* Update livery selection windows */
1870
for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) InvalidateWindowData(WC_COMPANY_COLOUR, i, _loaded_newgrf_features.has_2CC);
1974
for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) InvalidateWindowData(WC_COMPANY_COLOUR, i);
1871
1975
/* redraw the whole screen */
1872
1976
MarkWholeScreenDirty();
1873
1977
CheckTrainsLengths();