12 #include "../stdafx.h"
13 #include "../void_map.h"
14 #include "../signs_base.h"
15 #include "../depot_base.h"
17 #include "../gamelog_internal.h"
18 #include "../network/network.h"
19 #include "../network/network_func.h"
20 #include "../gfxinit.h"
21 #include "../viewport_func.h"
22 #include "../industry.h"
23 #include "../clear_map.h"
24 #include "../vehicle_func.h"
25 #include "../string_func.h"
26 #include "../date_func.h"
27 #include "../roadveh.h"
29 #include "../station_base.h"
30 #include "../waypoint_base.h"
31 #include "../roadstop_base.h"
32 #include "../tunnelbridge_map.h"
33 #include "../pathfinder/yapf/yapf_cache.h"
34 #include "../elrail_func.h"
35 #include "../signs_func.h"
36 #include "../aircraft.h"
37 #include "../object_map.h"
38 #include "../object_base.h"
39 #include "../tree_map.h"
40 #include "../company_func.h"
41 #include "../road_cmd.h"
42 #include "../ai/ai.hpp"
43 #include "../ai/ai_gui.hpp"
45 #include "../economy_base.h"
46 #include "../animated_tile_func.h"
47 #include "../subsidy_base.h"
48 #include "../subsidy_func.h"
49 #include "../newgrf.h"
50 #include "../engine_func.h"
51 #include "../rail_gui.h"
52 #include "../core/backup_type.hpp"
53 #include "../smallmap_gui.h"
54 #include "../news_func.h"
55 #include "../order_backup.h"
57 #include "../disaster_vehicle.h"
64 #include "../safeguards.h"
83 if (include_invalid_water_class) {
100 bool has_water =
false;
101 bool has_canal =
false;
102 bool has_river =
false;
111 }
else if (!
IsLock(neighbour)) {
135 if (!has_water && !has_canal && !has_river && include_invalid_water_class) {
140 if (has_river && !has_canal) {
142 }
else if (has_canal || !has_water) {
149 static void ConvertTownOwner()
169 static void UpdateExclusiveRights()
187 static const byte convert_currency[] = {
196 static void UpdateCurrencies()
204 static void UpdateVoidTiles()
214 return rt >= min ? (
RailType)(rt + 1): rt;
244 ResetViewportAfterLoadGame();
247 FOR_ALL_COMPANIES(c) {
264 FOR_ALL_INDUSTRIES(i) {
265 if (i->
psa != NULL) {
271 FOR_ALL_STATIONS(s) {
279 for (std::list<PersistentStorage *>::iterator it = t->psa_list.begin(); it != t->psa_list.end(); ++it) {
305 typedef void (CDECL *SignalHandlerPointer)(int);
306 static SignalHandlerPointer _prev_segfault = NULL;
307 static SignalHandlerPointer _prev_abort = NULL;
308 static SignalHandlerPointer _prev_fpe = NULL;
328 signal(SIGSEGV, _prev_segfault);
329 signal(SIGABRT, _prev_abort);
330 signal(SIGFPE, _prev_fpe);
376 p +=
seprintf(p,
lastof(buffer),
"Loading your savegame caused OpenTTD to crash.\n");
382 if (_saveload_crash_with_missing_newgrfs) {
384 "This is most likely caused by a missing NewGRF or a NewGRF that\n"
385 "has been loaded as replacement for a missing NewGRF. OpenTTD\n"
386 "cannot easily determine whether a replacement NewGRF is of a newer\n"
387 "or older version.\n"
388 "It will load a NewGRF with the same GRF ID as the missing NewGRF.\n"
389 "This means that if the author makes incompatible NewGRFs with the\n"
390 "same GRF ID OpenTTD cannot magically do the right thing. In most\n"
391 "cases OpenTTD will load the savegame and not crash, but this is an\n"
393 "Please load the savegame with the appropriate NewGRFs installed.\n"
394 "The missing/compatible NewGRFs are:\n");
401 p +=
seprintf(p,
lastof(buffer),
"NewGRF %08X (checksum %s) not found.\n Loaded NewGRF \"%s\" with same GRF ID instead.\n",
BSWAP32(c->ident.grfid), buf, c->filename);
406 p +=
seprintf(p,
lastof(buffer),
"NewGRF %08X (%s) not found; checksum %s.\n",
BSWAP32(c->ident.grfid), c->filename, buf);
411 "This is probably caused by a corruption in the savegame.\n"
412 "Please file a bug report and attach this savegame.\n");
417 SignalHandlerPointer call = NULL;
419 case SIGSEGV: call = _prev_segfault;
break;
420 case SIGABRT: call = _prev_abort;
break;
421 case SIGFPE: call = _prev_fpe;
break;
422 default: NOT_REACHED();
424 if (call != NULL) call(signum);
482 int entry_x = v->
x_pos;
483 int entry_y = v->
y_pos;
490 default: NOT_REACHED();
492 byte entry_z = GetSlopePixelZ(entry_x, entry_y);
497 byte middle_z = GetSlopePixelZ(middle_x, middle_y);
500 if (middle_z == entry_z)
return 0;
534 if (_cur_tileloop_tile == 0) _cur_tileloop_tile = 1;
546 DEBUG(net, 0,
"The loading savegame was paused due to an error state.");
547 DEBUG(net, 0,
" The savegame cannot be used for multiplayer!");
570 FOR_ALL_STATIONS(st) {
573 for (
TileIndex t = 0; t < map_size; t++) {
575 if (
_m[t].m5 > 7)
continue;
580 assert(dx >= 0 && dy >= 0);
590 for (
TileIndex t = 0; t < map_size; t++) {
592 SB(
_m[t].type, 0, 2,
GB(
_me[t].m6, 0, 2));
593 SB(
_me[t].m6, 0, 2, 0);
595 SB(
_m[t].type, 2, 2,
GB(
_me[t].m6, 6, 2));
596 SB(
_me[t].m6, 6, 2, 0);
598 SB(
_m[t].type, 2, 2, 0);
617 for (
TileIndex t = 0; t < map_size; t++) {
626 FOR_ALL_COMPANIES(c) {
628 if (c->
name != NULL) c->
name_1 = STR_SV_UNNAMED;
634 FOR_ALL_STATIONS(st) {
637 if (st->
name != NULL) st->
string_id = STR_SV_STNAME_FALLBACK;
653 FOR_ALL_STATIONS(st) {
661 FOR_ALL_COMPANIES(c) {
768 FOR_ALL_STATIONS(st) {
781 FOR_ALL_COMPANIES(c) {
835 st = STATION_AIRPORT;
843 }
else if (gfx == 75) {
849 }
else if (gfx == 82) {
853 st = STATION_AIRPORT;
866 SB(
_me[t].m6, 3, 3, st);
873 for (
TileIndex t = 0; t < map_size; t++) {
879 bst->
rect.BeforeAddTile(t, StationRect::ADD_FORCE);
908 case STATION_OILRIG: {
944 for (
TileIndex t = 0; t < map_size; t++) {
974 FOR_ALL_TOWNS(t) UpdateTownMaxPass(t);
981 FOR_ALL_COMPANIES(c) {
1001 for (
TileIndex t = 0; t < map_size; t++) {
1007 uint tmp =
GB(
_m[t].m4, 0, 4);
1008 SB(
_m[t].m4, 0, 4,
GB(
_m[t].m2, 0, 4));
1009 SB(
_m[t].m2, 0, 4, tmp);
1031 for (
TileIndex t = 0; t < map_size; t++) {
1034 SB(
_m[t].m5, 6, 2,
GB(
_m[t].m5, 4, 2));
1038 SB(
_m[t].m4, 0, 4,
GB(
_m[t].m5, 0, 4));
1039 SB(
_m[t].m4, 4, 4, 0);
1040 SB(
_me[t].m6, 2, 4, 0);
1043 SB(
_m[t].m4, 5, 2,
GB(
_m[t].m5, 2, 2));
1071 for (
TileIndex t = 0; t < map_size; t++) {
1075 SB(
_me[t].m7, 5, 1,
GB(
_m[t].m3, 7, 1));
1079 SB(
_me[t].m7, 0, 4,
GB(
_m[t].m3, 0, 4));
1080 SB(
_me[t].m6, 3, 3,
GB(
_m[t].m3, 4, 3));
1081 SB(
_m[t].m3, 0, 4,
GB(
_m[t].m4, 4, 4));
1082 SB(
_m[t].m3, 4, 4,
GB(
_m[t].m5, 0, 4));
1083 SB(
_m[t].m5, 0, 4,
GB(
_m[t].m4, 0, 4));
1087 SB(
_me[t].m7, 0, 5,
GB(
_m[t].m4, 0, 5));
1088 SB(
_me[t].m6, 3, 3,
GB(
_m[t].m3, 4, 3));
1089 SB(
_m[t].m3, 4, 4,
GB(
_m[t].m5, 0, 4));
1090 SB(
_m[t].m5, 0, 1,
GB(
_m[t].m4, 6, 1));
1091 SB(
_m[t].m5, 5, 1,
GB(
_m[t].m4, 5, 1));
1109 SB(
_m[t].m3, 4, 4,
_m[t].m1);
1119 SB(
_me[t].m7, 0, 5, o);
1122 SB(
_me[t].m6, 2, 4,
GB(
_m[t].m2, 4, 4));
1123 SB(
_me[t].m7, 5, 1,
GB(
_m[t].m4, 7, 1));
1137 for (
TileIndex t = 0; t < map_size; t++) {
1163 if (
GB(
_m[t].m5, 3, 2) == 0) {
1180 uint north_south =
GB(
_m[t].m5, 5, 1);
1184 _m[t].
m5 = 1 << 7 | type << 2 | dir;
1228 for (
TileIndex t = 0; t < map_size; t++) {
1281 for (
TileIndex t = 0; t < map_size; t++) {
1299 SB(
_m[t].m4, 4, 4, 0);
1316 FOR_ALL_ROADVEHICLES(rv) {
1323 FOR_ALL_STATIONS(st) {
1336 FOR_ALL_COMPANIES(c) {
1373 for (
TileIndex t = 0; t < map_size; t++) {
1380 FOR_ALL_INDUSTRIES(i) {
1384 for (j = 0; j != 50; j++) PlantRandomFarmField(i);
1394 FOR_ALL_ORDERS(order) {
1413 for (
TileIndex t = 0; t < map_size; t++) {
1418 SB(
_m[t].m5, 3, 2,
GB(
_m[t].m3, 6, 2));
1419 SB(
_m[t].m3, 6, 2, 0);
1450 for (
TileIndex t = 0; t < map_size; t++) {
1453 case GFX_POWERPLANT_SPARKS:
1457 case GFX_OILWELL_ANIMATED_1:
1458 case GFX_OILWELL_ANIMATED_2:
1459 case GFX_OILWELL_ANIMATED_3:
1463 case GFX_COAL_MINE_TOWER_ANIMATED:
1464 case GFX_COPPER_MINE_TOWER_ANIMATED:
1465 case GFX_GOLD_MINE_TOWER_ANIMATED:
1513 for (
TileIndex t = 0; t < map_size; t++) {
1551 FOR_ALL_STATIONS(st) {
1552 std::list<Vehicle *>::iterator iter;
1553 for (iter = st->loading_vehicles.begin(); iter != st->loading_vehicles.end();) {
1575 for (
TileIndex t = 0; t < map_size; t++) {
1579 SB(
_m[t].m2, 4, 4, 0);
1581 SB(
_m[t].m2, 4, 3,
GB(
_m[t].m2, 0, 3));
1589 FOR_ALL_ROADVEHICLES(rv) {
1605 for (
TileIndex t = 0; t < map_size; t++) {
1622 for (
TileIndex t = 0; t < map_size; t++) {
1631 FOR_ALL_STATIONS(st) {
1642 FOR_ALL_INDUSTRIES(i) {
1658 for (
TileIndex t = 0; t < map_size; t++) {
1687 FOR_ALL_ORDERS(order) {
1710 FOR_ALL_COMPANIES(c) {
1711 for (uint i = 0; i < 4; i++) {
1721 for (
TileIndex t = 0; t < map_size; t++) {
1725 case STATION_OILRIG:
1729 SB(
_m[t].m3, 0, 2, 0);
1740 SB(
_m[t].m3, 0, 2, 0);
1755 for (
TileIndex t = 0; t < map_size; t++) {
1777 for (
TileIndex t = 0; t < map_size; t++) {
1786 for (
TileIndex t = 0; t < map_size; t++) {
1854 for (
TileIndex t = 0; t < map_size; t++) {
1857 SB(
_m[t].m3, 5, 1, 0);
1866 FOR_ALL_ROADVEHICLES(v) {
1875 for (
TileIndex t = 0; t < map_size; t++) {
1899 for (
TileIndex t = 0; t < map_size; t++) {
1944 for (
TileIndex t = 0; t < map_size; t++) {
1963 FOR_ALL_STATIONS(st) {
1979 FOR_ALL_COMPANIES(c) {
1984 FOR_ALL_ENGINES(e) {
1996 for (
TileIndex t = 0; t < map_size; t++) {
2008 for (
TileIndex t = 0; t < map_size; t++) {
2024 for (
TileIndex t = 0; t < map_size; t++) {
2031 uint offset =
_m[t].
m3;
2035 SB(
_me[t].m6, 2, 4, 0);
2045 SlError(STR_ERROR_TOO_MANY_OBJECTS);
2089 case 5: layout = 1;
break;
2090 case 0: layout = 2;
break;
2101 FOR_ALL_STATIONS(st) {
2117 FOR_ALL_COMPANIES(c) {
2141 FOR_ALL_STATIONS(st) {
2142 std::list<Vehicle *>::iterator iter;
2143 for (iter = st->loading_vehicles.begin(); iter != st->loading_vehicles.end(); ++iter) {
2166 for (uint j = 0; !
remove && j < i; j++) {
2167 remove = _animated_tile_list[i] == _animated_tile_list[j];
2197 FOR_ALL_SUBSIDIES(s) {
2235 if (ss != NULL && sd != NULL && ss->
owner == sd->
owner &&
2285 if (t->force_proceed !=
TFP_NONE) {
2294 for (
TileIndex t = 0; t < map_size; t++) {
2304 uint density =
GB(
_m[t].m2, 6, 2);
2305 uint ground =
GB(
_m[t].m2, 4, 2);
2306 uint counter =
GB(
_m[t].m2, 0, 4);
2307 _m[t].
m2 = ground << 6 | density << 4 | counter;
2327 struct AirportTileConversion {
2331 static const AirportTileConversion atc[] = {
2342 for (
TileIndex t = 0; t < map_size; t++) {
2346 for (uint i = 0; i <
lengthof(atc); i++) {
2347 if (old_gfx < atc[i].old_start) {
2351 if (old_gfx < atc[i].old_start + atc[i].num_frames) {
2356 offset += atc[i].num_frames - 1;
2364 FOR_ALL_STATIONS(st) {
2373 for (
TileIndex t = 0; t < map_size; t++) {
2382 FOR_ALL_DEPOTS(d) d->
town_cn = UINT16_MAX;
2417 for (
TileIndex t = 0; t < map_size; t++) {
2421 uint per_proc =
_me[t].
m7;
2423 SB(
_m[t].m3, 5, 1, 0);
2424 SB(
_me[t].m6, 2, 6,
min(per_proc, 63));
2429 uint rand =
_me[t].
m7;
2450 FOR_ALL_OBJECTS(o) {
2457 for (
TileIndex t = 0; t < map_size; t++) {
2512 hidden = frame >= _tunnel_visibility_frame[dir];
2515 hidden = frame <
TILE_SIZE - _tunnel_visibility_frame[dir];
2533 default: NOT_REACHED();
2541 default: NOT_REACHED();
2549 FOR_ALL_ROADVEHICLES(rv) {
2566 if (!
HasBit(t->flags, 5))
continue;
2574 FOR_ALL_COMPANIES(c) {
2705 FOR_ALL_INDUSTRIES(ind) {
2706 assert(ind->
psa != NULL);
2709 bool is_empty =
true;
2710 for (uint i = 0; i <
sizeof(ind->
psa->
storage); i++) {
2728 FOR_ALL_STATIONS(st) {
2733 bool is_empty =
true;
2759 for (
TileIndex t = 0; t < map_size; t++) {
2762 uint fence =
GB(
_m[t].m4, 5, 3);
2766 fence =
GB(
_m[t].m4, 2, 3);
2770 SB(
_m[t].m4, 2, 3, 0);
2771 SB(
_m[t].m4, 5, 3, 0);
2798 _saved_scrollpos_zoom = _saved_scrollpos_zoom + ZOOM_LVL_SHIFT;
2799 _saved_scrollpos_x *= ZOOM_LVL_BASE;
2800 _saved_scrollpos_y *= ZOOM_LVL_BASE;
2810 for (
TileIndex t = 0; t < map_size; t++) {
2816 FOR_ALL_TOWNS(town) {
2823 for (
TileIndex t = 0; t < map_size; t++) {
2843 FOR_ALL_COMPANIES(c) {
2849 extern uint8 _old_diff_level;
2866 for (
TileIndex t = 0; t < map_size; t++) {
2875 extern uint8 _old_units;
2886 for (
TileIndex t = 0; t < map_size; t++) {
2904 FOR_ALL_ROADVEHICLES(v) {
2906 skip_frames.
Clear();
2908 uint prev_tile_skip = 0;
2911 if (u->tile != prev_tile) {
2912 prev_tile_skip = cur_skip;
2913 prev_tile = u->tile;
2915 cur_skip = prev_tile_skip;
2918 uint *this_skip = skip_frames.
Append();
2919 *this_skip = prev_tile_skip;
2925 if (u->frame <= (roadside ? 9 : 5)) *this_skip = cur_skip;
2930 if (u->frame <= (roadside ? 5 : 9)) *this_skip = cur_skip;
2935 if (u->frame <= (roadside ? 4 : 2)) *this_skip = cur_skip;
2942 while (cur_skip > skip_frames[0]) {
2945 for (uint *it = skip_frames.
Begin(); it != skip_frames.
End(); ++it, prev = u, u = u->
Next()) {
2947 if (*it >= cur_skip) IndividualRoadVehicleController(u, prev);
2961 #ifndef DEBUG_DUMP_COMMANDS
2980 AfterLoadLabelMaps();
3014 AfterLoadStations();