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) {
770 FOR_ALL_STATIONS(st) {
783 FOR_ALL_COMPANIES(c) {
837 st = STATION_AIRPORT;
845 }
else if (gfx == 75) {
851 }
else if (gfx == 82) {
855 st = STATION_AIRPORT;
868 SB(
_me[t].m6, 3, 3, st);
875 for (
TileIndex t = 0; t < map_size; t++) {
881 bst->
rect.BeforeAddTile(t, StationRect::ADD_FORCE);
910 case STATION_OILRIG: {
946 for (
TileIndex t = 0; t < map_size; t++) {
976 FOR_ALL_TOWNS(t) UpdateTownMaxPass(t);
983 FOR_ALL_COMPANIES(c) {
1003 for (
TileIndex t = 0; t < map_size; t++) {
1009 uint tmp =
GB(
_m[t].m4, 0, 4);
1010 SB(
_m[t].m4, 0, 4,
GB(
_m[t].m2, 0, 4));
1011 SB(
_m[t].m2, 0, 4, tmp);
1033 for (
TileIndex t = 0; t < map_size; t++) {
1036 SB(
_m[t].m5, 6, 2,
GB(
_m[t].m5, 4, 2));
1040 SB(
_m[t].m4, 0, 4,
GB(
_m[t].m5, 0, 4));
1041 SB(
_m[t].m4, 4, 4, 0);
1042 SB(
_me[t].m6, 2, 4, 0);
1045 SB(
_m[t].m4, 5, 2,
GB(
_m[t].m5, 2, 2));
1073 for (
TileIndex t = 0; t < map_size; t++) {
1077 SB(
_me[t].m7, 5, 1,
GB(
_m[t].m3, 7, 1));
1081 SB(
_me[t].m7, 0, 4,
GB(
_m[t].m3, 0, 4));
1082 SB(
_me[t].m6, 3, 3,
GB(
_m[t].m3, 4, 3));
1083 SB(
_m[t].m3, 0, 4,
GB(
_m[t].m4, 4, 4));
1084 SB(
_m[t].m3, 4, 4,
GB(
_m[t].m5, 0, 4));
1085 SB(
_m[t].m5, 0, 4,
GB(
_m[t].m4, 0, 4));
1089 SB(
_me[t].m7, 0, 5,
GB(
_m[t].m4, 0, 5));
1090 SB(
_me[t].m6, 3, 3,
GB(
_m[t].m3, 4, 3));
1091 SB(
_m[t].m3, 4, 4,
GB(
_m[t].m5, 0, 4));
1092 SB(
_m[t].m5, 0, 1,
GB(
_m[t].m4, 6, 1));
1093 SB(
_m[t].m5, 5, 1,
GB(
_m[t].m4, 5, 1));
1111 SB(
_m[t].m3, 4, 4,
_m[t].m1);
1121 SB(
_me[t].m7, 0, 5, o);
1124 SB(
_me[t].m6, 2, 4,
GB(
_m[t].m2, 4, 4));
1125 SB(
_me[t].m7, 5, 1,
GB(
_m[t].m4, 7, 1));
1139 for (
TileIndex t = 0; t < map_size; t++) {
1165 if (
GB(
_m[t].m5, 3, 2) == 0) {
1182 uint north_south =
GB(
_m[t].m5, 5, 1);
1186 _m[t].
m5 = 1 << 7 | type << 2 | dir;
1230 for (
TileIndex t = 0; t < map_size; t++) {
1283 for (
TileIndex t = 0; t < map_size; t++) {
1301 SB(
_m[t].m4, 4, 4, 0);
1318 FOR_ALL_ROADVEHICLES(rv) {
1325 FOR_ALL_STATIONS(st) {
1338 FOR_ALL_COMPANIES(c) {
1375 for (
TileIndex t = 0; t < map_size; t++) {
1382 FOR_ALL_INDUSTRIES(i) {
1386 for (j = 0; j != 50; j++) PlantRandomFarmField(i);
1396 FOR_ALL_ORDERS(order) {
1415 for (
TileIndex t = 0; t < map_size; t++) {
1420 SB(
_m[t].m5, 3, 2,
GB(
_m[t].m3, 6, 2));
1421 SB(
_m[t].m3, 6, 2, 0);
1452 for (
TileIndex t = 0; t < map_size; t++) {
1455 case GFX_POWERPLANT_SPARKS:
1459 case GFX_OILWELL_ANIMATED_1:
1460 case GFX_OILWELL_ANIMATED_2:
1461 case GFX_OILWELL_ANIMATED_3:
1465 case GFX_COAL_MINE_TOWER_ANIMATED:
1466 case GFX_COPPER_MINE_TOWER_ANIMATED:
1467 case GFX_GOLD_MINE_TOWER_ANIMATED:
1515 for (
TileIndex t = 0; t < map_size; t++) {
1553 FOR_ALL_STATIONS(st) {
1554 std::list<Vehicle *>::iterator iter;
1555 for (iter = st->loading_vehicles.begin(); iter != st->loading_vehicles.end();) {
1577 for (
TileIndex t = 0; t < map_size; t++) {
1581 SB(
_m[t].m2, 4, 4, 0);
1583 SB(
_m[t].m2, 4, 3,
GB(
_m[t].m2, 0, 3));
1591 FOR_ALL_ROADVEHICLES(rv) {
1607 for (
TileIndex t = 0; t < map_size; t++) {
1624 for (
TileIndex t = 0; t < map_size; t++) {
1633 FOR_ALL_STATIONS(st) {
1644 FOR_ALL_INDUSTRIES(i) {
1660 for (
TileIndex t = 0; t < map_size; t++) {
1689 FOR_ALL_ORDERS(order) {
1712 FOR_ALL_COMPANIES(c) {
1713 for (uint i = 0; i < 4; i++) {
1723 for (
TileIndex t = 0; t < map_size; t++) {
1727 case STATION_OILRIG:
1731 SB(
_m[t].m3, 0, 2, 0);
1742 SB(
_m[t].m3, 0, 2, 0);
1757 for (
TileIndex t = 0; t < map_size; t++) {
1779 for (
TileIndex t = 0; t < map_size; t++) {
1788 for (
TileIndex t = 0; t < map_size; t++) {
1856 for (
TileIndex t = 0; t < map_size; t++) {
1859 SB(
_m[t].m3, 5, 1, 0);
1868 FOR_ALL_ROADVEHICLES(v) {
1877 for (
TileIndex t = 0; t < map_size; t++) {
1901 for (
TileIndex t = 0; t < map_size; t++) {
1946 for (
TileIndex t = 0; t < map_size; t++) {
1965 FOR_ALL_STATIONS(st) {
1981 FOR_ALL_COMPANIES(c) {
1986 FOR_ALL_ENGINES(e) {
1998 for (
TileIndex t = 0; t < map_size; t++) {
2010 for (
TileIndex t = 0; t < map_size; t++) {
2026 for (
TileIndex t = 0; t < map_size; t++) {
2033 uint offset =
_m[t].
m3;
2037 SB(
_me[t].m6, 2, 4, 0);
2047 SlError(STR_ERROR_TOO_MANY_OBJECTS);
2091 case 5: layout = 1;
break;
2092 case 0: layout = 2;
break;
2103 FOR_ALL_STATIONS(st) {
2119 FOR_ALL_COMPANIES(c) {
2143 FOR_ALL_STATIONS(st) {
2144 std::list<Vehicle *>::iterator iter;
2145 for (iter = st->loading_vehicles.begin(); iter != st->loading_vehicles.end(); ++iter) {
2168 for (uint j = 0; !
remove && j < i; j++) {
2169 remove = _animated_tile_list[i] == _animated_tile_list[j];
2199 FOR_ALL_SUBSIDIES(s) {
2237 if (ss != NULL && sd != NULL && ss->
owner == sd->
owner &&
2287 if (t->force_proceed !=
TFP_NONE) {
2296 for (
TileIndex t = 0; t < map_size; t++) {
2306 uint density =
GB(
_m[t].m2, 6, 2);
2307 uint ground =
GB(
_m[t].m2, 4, 2);
2308 uint counter =
GB(
_m[t].m2, 0, 4);
2309 _m[t].
m2 = ground << 6 | density << 4 | counter;
2329 struct AirportTileConversion {
2333 static const AirportTileConversion atc[] = {
2344 for (
TileIndex t = 0; t < map_size; t++) {
2348 for (uint i = 0; i <
lengthof(atc); i++) {
2349 if (old_gfx < atc[i].old_start) {
2353 if (old_gfx < atc[i].old_start + atc[i].num_frames) {
2358 offset += atc[i].num_frames - 1;
2366 FOR_ALL_STATIONS(st) {
2375 for (
TileIndex t = 0; t < map_size; t++) {
2384 FOR_ALL_DEPOTS(d) d->
town_cn = UINT16_MAX;
2419 for (
TileIndex t = 0; t < map_size; t++) {
2423 uint per_proc =
_me[t].
m7;
2425 SB(
_m[t].m3, 5, 1, 0);
2426 SB(
_me[t].m6, 2, 6,
min(per_proc, 63));
2431 uint rand =
_me[t].
m7;
2452 FOR_ALL_OBJECTS(o) {
2459 for (
TileIndex t = 0; t < map_size; t++) {
2514 hidden = frame >= _tunnel_visibility_frame[dir];
2517 hidden = frame <
TILE_SIZE - _tunnel_visibility_frame[dir];
2535 default: NOT_REACHED();
2543 default: NOT_REACHED();
2551 FOR_ALL_ROADVEHICLES(rv) {
2568 if (!
HasBit(t->flags, 5))
continue;
2576 FOR_ALL_COMPANIES(c) {
2707 FOR_ALL_INDUSTRIES(ind) {
2708 assert(ind->
psa != NULL);
2711 bool is_empty =
true;
2712 for (uint i = 0; i <
sizeof(ind->
psa->
storage); i++) {
2730 FOR_ALL_STATIONS(st) {
2735 bool is_empty =
true;
2761 for (
TileIndex t = 0; t < map_size; t++) {
2764 uint fence =
GB(
_m[t].m4, 5, 3);
2768 fence =
GB(
_m[t].m4, 2, 3);
2772 SB(
_m[t].m4, 2, 3, 0);
2773 SB(
_m[t].m4, 5, 3, 0);
2800 _saved_scrollpos_zoom = _saved_scrollpos_zoom + ZOOM_LVL_SHIFT;
2801 _saved_scrollpos_x *= ZOOM_LVL_BASE;
2802 _saved_scrollpos_y *= ZOOM_LVL_BASE;
2812 for (
TileIndex t = 0; t < map_size; t++) {
2818 FOR_ALL_TOWNS(town) {
2825 for (
TileIndex t = 0; t < map_size; t++) {
2845 FOR_ALL_COMPANIES(c) {
2851 extern uint8 _old_diff_level;
2868 for (
TileIndex t = 0; t < map_size; t++) {
2877 extern uint8 _old_units;
2888 for (
TileIndex t = 0; t < map_size; t++) {
2906 FOR_ALL_ROADVEHICLES(v) {
2908 skip_frames.
Clear();
2910 uint prev_tile_skip = 0;
2913 if (u->tile != prev_tile) {
2914 prev_tile_skip = cur_skip;
2915 prev_tile = u->tile;
2917 cur_skip = prev_tile_skip;
2920 uint *this_skip = skip_frames.
Append();
2921 *this_skip = prev_tile_skip;
2927 if (u->frame <= (roadside ? 9 : 5)) *this_skip = cur_skip;
2932 if (u->frame <= (roadside ? 5 : 9)) *this_skip = cur_skip;
2937 if (u->frame <= (roadside ? 4 : 2)) *this_skip = cur_skip;
2944 while (cur_skip > skip_frames[0]) {
2947 for (uint *it = skip_frames.
Begin(); it != skip_frames.
End(); ++it, prev = u, u = u->
Next()) {
2949 if (*it >= cur_skip) IndividualRoadVehicleController(u, prev);
2963 #ifndef DEBUG_DUMP_COMMANDS
2982 AfterLoadLabelMaps();
3016 AfterLoadStations();