~ubuntu-branches/debian/squeeze/openttd/squeeze

« back to all changes in this revision

Viewing changes to src/saveload/afterload.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Matthijs Kooijman, Matthijs Kooijman
  • Date: 2009-10-01 22:52:59 UTC
  • mfrom: (1.1.8 upstream) (2.1.6 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091001225259-5kpkp4sthbszpyif
[ Matthijs Kooijman ]
* New upstream release
* Use printf instead of echo -en in openttd-wrapper to make it POSIX
  compatible (Closes: #547758).
* Remove three patches that are now included in upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: afterload.cpp 16539 2009-06-08 22:53:39Z rubidium $ */
 
1
/* $Id: afterload.cpp 17346 2009-09-01 13:16:53Z rubidium $ */
2
2
 
3
3
/** @file afterload.cpp Code updating data after game load */
4
4
 
11
11
#include "../roadveh.h"
12
12
#include "../string_func.h"
13
13
#include "../gamelog.h"
 
14
#include "../gamelog_internal.h"
14
15
#include "../network/network.h"
15
16
#include "../gfxinit.h"
16
17
#include "../functions.h"
28
29
#include "../company_func.h"
29
30
#include "../road_cmd.h"
30
31
#include "../ai/ai.hpp"
 
32
#include "../animated_tile_func.h"
31
33
 
 
34
#include "../economy_base.h"
32
35
#include "table/strings.h"
33
36
 
34
37
#include "saveload_internal.h"
36
39
#include <signal.h>
37
40
 
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();
41
44
 
42
45
/**
238
241
 
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;
242
246
 
243
247
static void CDECL HandleSavegameLoadCrash(int signum);
244
248
 
249
253
static void SetSignalHandlers()
250
254
{
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);
253
258
}
254
259
 
255
260
/**
259
264
{
260
265
        signal(SIGSEGV, _prev_segfault);
261
266
        signal(SIGABRT, _prev_abort);
 
267
        signal(SIGFPE,  _prev_fpe);
 
268
}
 
269
 
 
270
/**
 
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.
 
274
 */
 
275
static const GRFIdentifier *GetOverriddenIdentifier(const GRFConfig *c)
 
276
{
 
277
        const LoggedAction *la = &_gamelog_action[_gamelog_actions - 1];
 
278
        if (la->at != GLAT_LOAD) return c;
 
279
 
 
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;
 
283
        }
 
284
 
 
285
        return c;
262
286
}
263
287
 
264
288
/**
286
310
                        "savegame still crashes when all NewGRFs are found you should file a\n"
287
311
                        "bug report. The missing NewGRFs are:\n");
288
312
 
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);
291
316
                        char buf[40];
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);
294
319
                }
295
320
                if (c->status == GCS_NOT_FOUND) {
296
321
                        char buf[40];
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);
299
324
                }
300
325
        }
301
326
 
302
327
        ShowInfo(buffer);
303
328
 
304
 
        SignalHandlerPointer call = signum == SIGSEGV ? _prev_segfault : _prev_abort;
 
329
        SignalHandlerPointer call = NULL;
 
330
        switch (signum) {
 
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();
 
335
        }
305
336
        if (call != NULL) call(signum);
306
337
}
307
338
 
405
436
                }
406
437
        }
407
438
 
 
439
        /* Update all waypoints */
 
440
        if (CheckSavegameVersion(12)) FixOldWaypoints();
 
441
 
408
442
        if (CheckSavegameVersion(84)) {
409
443
                FOR_ALL_COMPANIES(c) {
410
444
                        c->name = CopyFromOldName(c->name_1);
429
463
                Waypoint *wp;
430
464
                FOR_ALL_WAYPOINTS(wp) {
431
465
                        wp->name = CopyFromOldName(wp->string);
432
 
                        wp->string = STR_EMPTY;
 
466
                        wp->string = STR_NULL;
433
467
                }
434
468
        }
435
469
 
507
541
                }
508
542
        }
509
543
 
510
 
        /* Update all waypoints */
511
 
        if (CheckSavegameVersion(12)) FixOldWaypoints();
512
 
 
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);
533
564
 
 
565
        /* Fix the cache for cargo payments. */
 
566
        CargoPayment *cp;
 
567
        FOR_ALL_CARGO_PAYMENTS(cp) {
 
568
                cp->front->cargo_payment = cp;
 
569
                cp->current_station = cp->front->last_station_visited;
 
570
        }
 
571
 
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)) {
709
747
                                case MP_RAILWAY:
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);
1268
 
                        }
1269
1302
                        ClrBit(v->vehicle_flags, 2);
1270
1303
                        v->cargo.InvalidateCache();
1271
1304
                }
1572
1605
                                if (IsLevelCrossing(t)) {
1573
1606
                                        if (!IsValidCompanyID(GetTileOwner(t))) FixOwnerOfRailTrack(t);
1574
1607
                                }
1575
 
                        } else if (IsTileType(t, MP_RAILWAY) && IsPlainRailTile(t)) {
 
1608
                        } else if (IsPlainRailTile(t)) {
1576
1609
                                if (!IsValidCompanyID(GetTileOwner(t))) FixOwnerOfRailTrack(t);
1577
1610
                        }
1578
1611
                }
1836
1869
                }
1837
1870
        }
1838
1871
 
 
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.
 
1876
                 */
 
1877
                Station *st;
 
1878
                FOR_ALL_STATIONS(st) {
 
1879
                        std::list<Vehicle *>::iterator iter;
 
1880
                        for (iter = st->loading_vehicles.begin(); iter != st->loading_vehicles.end(); ++iter) {
 
1881
                                Vehicle *v = *iter;
 
1882
                                assert(v->cargo_payment == NULL);
 
1883
                                v->cargo_payment = new CargoPayment(v);
 
1884
                        }
 
1885
                }
 
1886
        }
 
1887
 
 
1888
        if (CheckSavegameVersion(122)) {
 
1889
                /* Animated tiles would sometimes not be actually animated or
 
1890
                 * in case of old savegames duplicate. */
 
1891
 
 
1892
                extern TileIndex *_animated_tile_list;
 
1893
                extern uint _animated_tile_count;
 
1894
 
 
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;
 
1898
 
 
1899
                        /* and remove if duplicate */
 
1900
                        for (uint j = 0; !remove && j < i; j++) {
 
1901
                                remove = _animated_tile_list[i] == _animated_tile_list[j];
 
1902
                        }
 
1903
 
 
1904
                        if (remove) {
 
1905
                                DeleteAnimatedTile(_animated_tile_list[i]);
 
1906
                        } else {
 
1907
                                i++;
 
1908
                        }
 
1909
                }
 
1910
 
 
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;
 
1914
                        if (s->age >= 12) {
 
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;
 
1919
                        } else {
 
1920
                                const CargoSpec *cs = GetCargo(s->cargo_type);
 
1921
                                switch (cs->town_effect) {
 
1922
                                        case TE_PASSENGERS:
 
1923
                                        case TE_MAIL:
 
1924
                                                /* Town -> Town */
 
1925
                                                if (IsValidTownID(s->from) && IsValidTownID(s->to)) continue;
 
1926
                                                break;
 
1927
                                        case TE_GOODS:
 
1928
                                        case TE_FOOD:
 
1929
                                                /* Industry -> Town */
 
1930
                                                if (IsValidIndustryID(s->from) && IsValidTownID(s->to)) continue;
 
1931
                                                break;
 
1932
                                        default:
 
1933
                                                /* Industry -> Industry */
 
1934
                                                if (IsValidIndustryID(s->from) && IsValidIndustryID(s->to)) continue;
 
1935
                                                break;
 
1936
                                }
 
1937
                        }
 
1938
                        s->cargo_type = CT_INVALID;
 
1939
                }
 
1940
        }
 
1941
 
1839
1942
        GamelogPrintDebug(1);
1840
1943
 
1841
1944
        bool ret = InitializeWindowsAndCaches();
1862
1965
        StartupEngines();
1863
1966
        SetCachedEngineCounts();
1864
1967
        /* update station and waypoint graphics */
 
1968
 
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();