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 "../gfxinit.h"
20 #include "../viewport_func.h"
21 #include "../industry.h"
22 #include "../clear_map.h"
23 #include "../vehicle_func.h"
24 #include "../string_func.h"
25 #include "../date_func.h"
26 #include "../roadveh.h"
28 #include "../station_base.h"
29 #include "../waypoint_base.h"
30 #include "../roadstop_base.h"
31 #include "../tunnelbridge_map.h"
32 #include "../pathfinder/yapf/yapf_cache.h"
33 #include "../elrail_func.h"
34 #include "../signs_func.h"
35 #include "../aircraft.h"
36 #include "../object_map.h"
37 #include "../object_base.h"
38 #include "../tree_map.h"
39 #include "../company_func.h"
40 #include "../road_cmd.h"
41 #include "../ai/ai.hpp"
42 #include "../ai/ai_gui.hpp"
44 #include "../economy_base.h"
45 #include "../animated_tile_func.h"
46 #include "../subsidy_base.h"
47 #include "../subsidy_func.h"
48 #include "../newgrf.h"
49 #include "../engine_func.h"
50 #include "../rail_gui.h"
51 #include "../core/backup_type.hpp"
52 #include "../smallmap_gui.h"
53 #include "../news_func.h"
54 #include "../order_backup.h"
56 #include "../disaster_vehicle.h"
63 #include "../safeguards.h"
82 if (include_invalid_water_class) {
99 bool has_water =
false;
100 bool has_canal =
false;
101 bool has_river =
false;
110 }
else if (!
IsLock(neighbour)) {
134 if (!has_water && !has_canal && !has_river && include_invalid_water_class) {
139 if (has_river && !has_canal) {
141 }
else if (has_canal || !has_water) {
148 static void ConvertTownOwner()
168 static void UpdateExclusiveRights()
186 static const byte convert_currency[] = {
195 static void UpdateCurrencies()
203 static void UpdateVoidTiles()
213 return rt >= min ? (
RailType)(rt + 1): rt;
243 ResetViewportAfterLoadGame();
246 FOR_ALL_COMPANIES(c) {
263 FOR_ALL_INDUSTRIES(i) {
264 if (i->
psa != NULL) {
270 FOR_ALL_STATIONS(s) {
278 for (std::list<PersistentStorage *>::iterator it = t->psa_list.begin(); it != t->psa_list.end(); ++it) {
304 typedef void (CDECL *SignalHandlerPointer)(int);
305 static SignalHandlerPointer _prev_segfault = NULL;
306 static SignalHandlerPointer _prev_abort = NULL;
307 static SignalHandlerPointer _prev_fpe = NULL;
327 signal(SIGSEGV, _prev_segfault);
328 signal(SIGABRT, _prev_abort);
329 signal(SIGFPE, _prev_fpe);
375 p +=
seprintf(p,
lastof(buffer),
"Loading your savegame caused OpenTTD to crash.\n");
381 if (_saveload_crash_with_missing_newgrfs) {
383 "This is most likely caused by a missing NewGRF or a NewGRF that\n"
384 "has been loaded as replacement for a missing NewGRF. OpenTTD\n"
385 "cannot easily determine whether a replacement NewGRF is of a newer\n"
386 "or older version.\n"
387 "It will load a NewGRF with the same GRF ID as the missing NewGRF.\n"
388 "This means that if the author makes incompatible NewGRFs with the\n"
389 "same GRF ID OpenTTD cannot magically do the right thing. In most\n"
390 "cases OpenTTD will load the savegame and not crash, but this is an\n"
392 "Please load the savegame with the appropriate NewGRFs installed.\n"
393 "The missing/compatible NewGRFs are:\n");
400 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);
405 p +=
seprintf(p,
lastof(buffer),
"NewGRF %08X (%s) not found; checksum %s.\n",
BSWAP32(c->ident.grfid), c->filename, buf);
410 "This is probably caused by a corruption in the savegame.\n"
411 "Please file a bug report and attach this savegame.\n");
416 SignalHandlerPointer call = NULL;
418 case SIGSEGV: call = _prev_segfault;
break;
419 case SIGABRT: call = _prev_abort;
break;
420 case SIGFPE: call = _prev_fpe;
break;
421 default: NOT_REACHED();
423 if (call != NULL) call(signum);
481 int entry_x = v->
x_pos;
482 int entry_y = v->
y_pos;
489 default: NOT_REACHED();
491 byte entry_z = GetSlopePixelZ(entry_x, entry_y);
496 byte middle_z = GetSlopePixelZ(middle_x, middle_y);
499 if (middle_z == entry_z)
return 0;
533 if (_cur_tileloop_tile == 0) _cur_tileloop_tile = 1;
545 DEBUG(net, 0,
"The loading savegame was paused due to an error state.");
546 DEBUG(net, 0,
" The savegame cannot be used for multiplayer!");
569 FOR_ALL_STATIONS(st) {
572 for (
TileIndex t = 0; t < map_size; t++) {
574 if (
_m[t].m5 > 7)
continue;
579 assert(dx >= 0 && dy >= 0);
589 for (
TileIndex t = 0; t < map_size; t++) {
591 SB(
_m[t].type, 0, 2,
GB(
_me[t].m6, 0, 2));
592 SB(
_me[t].m6, 0, 2, 0);
594 SB(
_m[t].type, 2, 2,
GB(
_me[t].m6, 6, 2));
595 SB(
_me[t].m6, 6, 2, 0);
597 SB(
_m[t].type, 2, 2, 0);
616 for (
TileIndex t = 0; t < map_size; t++) {
625 FOR_ALL_COMPANIES(c) {
627 if (c->
name != NULL) c->
name_1 = STR_SV_UNNAMED;
633 FOR_ALL_STATIONS(st) {
636 if (st->
name != NULL) st->
string_id = STR_SV_STNAME_FALLBACK;
652 FOR_ALL_STATIONS(st) {
660 FOR_ALL_COMPANIES(c) {
767 FOR_ALL_STATIONS(st) {
780 FOR_ALL_COMPANIES(c) {
834 st = STATION_AIRPORT;
842 }
else if (gfx == 75) {
848 }
else if (gfx == 82) {
852 st = STATION_AIRPORT;
865 SB(
_me[t].m6, 3, 3, st);
872 for (
TileIndex t = 0; t < map_size; t++) {
878 bst->
rect.BeforeAddTile(t, StationRect::ADD_FORCE);
907 case STATION_OILRIG: {
943 for (
TileIndex t = 0; t < map_size; t++) {
973 FOR_ALL_TOWNS(t) UpdateTownMaxPass(t);
980 FOR_ALL_COMPANIES(c) {
1000 for (
TileIndex t = 0; t < map_size; t++) {
1006 uint tmp =
GB(
_m[t].m4, 0, 4);
1007 SB(
_m[t].m4, 0, 4,
GB(
_m[t].m2, 0, 4));
1008 SB(
_m[t].m2, 0, 4, tmp);
1030 for (
TileIndex t = 0; t < map_size; t++) {
1033 SB(
_m[t].m5, 6, 2,
GB(
_m[t].m5, 4, 2));
1037 SB(
_m[t].m4, 0, 4,
GB(
_m[t].m5, 0, 4));
1038 SB(
_m[t].m4, 4, 4, 0);
1039 SB(
_me[t].m6, 2, 4, 0);
1042 SB(
_m[t].m4, 5, 2,
GB(
_m[t].m5, 2, 2));
1070 for (
TileIndex t = 0; t < map_size; t++) {
1074 SB(
_me[t].m7, 5, 1,
GB(
_m[t].m3, 7, 1));
1078 SB(
_me[t].m7, 0, 4,
GB(
_m[t].m3, 0, 4));
1079 SB(
_me[t].m6, 3, 3,
GB(
_m[t].m3, 4, 3));
1080 SB(
_m[t].m3, 0, 4,
GB(
_m[t].m4, 4, 4));
1081 SB(
_m[t].m3, 4, 4,
GB(
_m[t].m5, 0, 4));
1082 SB(
_m[t].m5, 0, 4,
GB(
_m[t].m4, 0, 4));
1086 SB(
_me[t].m7, 0, 5,
GB(
_m[t].m4, 0, 5));
1087 SB(
_me[t].m6, 3, 3,
GB(
_m[t].m3, 4, 3));
1088 SB(
_m[t].m3, 4, 4,
GB(
_m[t].m5, 0, 4));
1089 SB(
_m[t].m5, 0, 1,
GB(
_m[t].m4, 6, 1));
1090 SB(
_m[t].m5, 5, 1,
GB(
_m[t].m4, 5, 1));
1108 SB(
_m[t].m3, 4, 4,
_m[t].m1);
1118 SB(
_me[t].m7, 0, 5, o);
1121 SB(
_me[t].m6, 2, 4,
GB(
_m[t].m2, 4, 4));
1122 SB(
_me[t].m7, 5, 1,
GB(
_m[t].m4, 7, 1));
1136 for (
TileIndex t = 0; t < map_size; t++) {
1162 if (
GB(
_m[t].m5, 3, 2) == 0) {
1179 uint north_south =
GB(
_m[t].m5, 5, 1);
1183 _m[t].
m5 = 1 << 7 | type << 2 | dir;
1227 for (
TileIndex t = 0; t < map_size; t++) {
1280 for (
TileIndex t = 0; t < map_size; t++) {
1298 SB(
_m[t].m4, 4, 4, 0);
1315 FOR_ALL_ROADVEHICLES(rv) {
1322 FOR_ALL_STATIONS(st) {
1335 FOR_ALL_COMPANIES(c) {
1372 for (
TileIndex t = 0; t < map_size; t++) {
1379 FOR_ALL_INDUSTRIES(i) {
1383 for (j = 0; j != 50; j++) PlantRandomFarmField(i);
1393 FOR_ALL_ORDERS(order) {
1412 for (
TileIndex t = 0; t < map_size; t++) {
1417 SB(
_m[t].m5, 3, 2,
GB(
_m[t].m3, 6, 2));
1418 SB(
_m[t].m3, 6, 2, 0);
1449 for (
TileIndex t = 0; t < map_size; t++) {
1452 case GFX_POWERPLANT_SPARKS:
1456 case GFX_OILWELL_ANIMATED_1:
1457 case GFX_OILWELL_ANIMATED_2:
1458 case GFX_OILWELL_ANIMATED_3:
1462 case GFX_COAL_MINE_TOWER_ANIMATED:
1463 case GFX_COPPER_MINE_TOWER_ANIMATED:
1464 case GFX_GOLD_MINE_TOWER_ANIMATED:
1512 for (
TileIndex t = 0; t < map_size; t++) {
1550 FOR_ALL_STATIONS(st) {
1551 std::list<Vehicle *>::iterator iter;
1552 for (iter = st->loading_vehicles.begin(); iter != st->loading_vehicles.end();) {
1574 for (
TileIndex t = 0; t < map_size; t++) {
1578 SB(
_m[t].m2, 4, 4, 0);
1580 SB(
_m[t].m2, 4, 3,
GB(
_m[t].m2, 0, 3));
1588 FOR_ALL_ROADVEHICLES(rv) {
1604 for (
TileIndex t = 0; t < map_size; t++) {
1621 for (
TileIndex t = 0; t < map_size; t++) {
1630 FOR_ALL_STATIONS(st) {
1641 FOR_ALL_INDUSTRIES(i) {
1657 for (
TileIndex t = 0; t < map_size; t++) {
1686 FOR_ALL_ORDERS(order) {
1709 FOR_ALL_COMPANIES(c) {
1710 for (uint i = 0; i < 4; i++) {
1720 for (
TileIndex t = 0; t < map_size; t++) {
1724 case STATION_OILRIG:
1728 SB(
_m[t].m3, 0, 2, 0);
1739 SB(
_m[t].m3, 0, 2, 0);
1754 for (
TileIndex t = 0; t < map_size; t++) {
1776 for (
TileIndex t = 0; t < map_size; t++) {
1785 for (
TileIndex t = 0; t < map_size; t++) {
1853 for (
TileIndex t = 0; t < map_size; t++) {
1856 SB(
_m[t].m3, 5, 1, 0);
1865 FOR_ALL_ROADVEHICLES(v) {
1874 for (
TileIndex t = 0; t < map_size; t++) {
1898 for (
TileIndex t = 0; t < map_size; t++) {
1943 for (
TileIndex t = 0; t < map_size; t++) {
1962 FOR_ALL_STATIONS(st) {
1978 FOR_ALL_COMPANIES(c) {
1983 FOR_ALL_ENGINES(e) {
1995 for (
TileIndex t = 0; t < map_size; t++) {
2007 for (
TileIndex t = 0; t < map_size; t++) {
2023 for (
TileIndex t = 0; t < map_size; t++) {
2030 uint offset =
_m[t].
m3;
2034 SB(
_me[t].m6, 2, 4, 0);
2044 SlError(STR_ERROR_TOO_MANY_OBJECTS);
2088 case 5: layout = 1;
break;
2089 case 0: layout = 2;
break;
2100 FOR_ALL_STATIONS(st) {
2116 FOR_ALL_COMPANIES(c) {
2140 FOR_ALL_STATIONS(st) {
2141 std::list<Vehicle *>::iterator iter;
2142 for (iter = st->loading_vehicles.begin(); iter != st->loading_vehicles.end(); ++iter) {
2165 for (uint j = 0; !
remove && j < i; j++) {
2166 remove = _animated_tile_list[i] == _animated_tile_list[j];
2196 FOR_ALL_SUBSIDIES(s) {
2234 if (ss != NULL && sd != NULL && ss->
owner == sd->
owner &&
2284 if (t->force_proceed !=
TFP_NONE) {
2293 for (
TileIndex t = 0; t < map_size; t++) {
2303 uint density =
GB(
_m[t].m2, 6, 2);
2304 uint ground =
GB(
_m[t].m2, 4, 2);
2305 uint counter =
GB(
_m[t].m2, 0, 4);
2306 _m[t].
m2 = ground << 6 | density << 4 | counter;
2326 struct AirportTileConversion {
2330 static const AirportTileConversion atc[] = {
2341 for (
TileIndex t = 0; t < map_size; t++) {
2345 for (uint i = 0; i <
lengthof(atc); i++) {
2346 if (old_gfx < atc[i].old_start) {
2350 if (old_gfx < atc[i].old_start + atc[i].num_frames) {
2355 offset += atc[i].num_frames - 1;
2363 FOR_ALL_STATIONS(st) {
2372 for (
TileIndex t = 0; t < map_size; t++) {
2381 FOR_ALL_DEPOTS(d) d->
town_cn = UINT16_MAX;
2416 for (
TileIndex t = 0; t < map_size; t++) {
2420 uint per_proc =
_me[t].
m7;
2422 SB(
_m[t].m3, 5, 1, 0);
2423 SB(
_me[t].m6, 2, 6,
min(per_proc, 63));
2428 uint rand =
_me[t].
m7;
2449 FOR_ALL_OBJECTS(o) {
2456 for (
TileIndex t = 0; t < map_size; t++) {
2511 hidden = frame >= _tunnel_visibility_frame[dir];
2514 hidden = frame <
TILE_SIZE - _tunnel_visibility_frame[dir];
2532 default: NOT_REACHED();
2540 default: NOT_REACHED();
2548 FOR_ALL_ROADVEHICLES(rv) {
2565 if (!
HasBit(t->flags, 5))
continue;
2573 FOR_ALL_COMPANIES(c) {
2704 FOR_ALL_INDUSTRIES(ind) {
2705 assert(ind->
psa != NULL);
2708 bool is_empty =
true;
2709 for (uint i = 0; i <
sizeof(ind->
psa->
storage); i++) {
2727 FOR_ALL_STATIONS(st) {
2732 bool is_empty =
true;
2758 for (
TileIndex t = 0; t < map_size; t++) {
2761 uint fence =
GB(
_m[t].m4, 5, 3);
2765 fence =
GB(
_m[t].m4, 2, 3);
2769 SB(
_m[t].m4, 2, 3, 0);
2770 SB(
_m[t].m4, 5, 3, 0);
2797 _saved_scrollpos_zoom = _saved_scrollpos_zoom + ZOOM_LVL_SHIFT;
2798 _saved_scrollpos_x *= ZOOM_LVL_BASE;
2799 _saved_scrollpos_y *= ZOOM_LVL_BASE;
2809 for (
TileIndex t = 0; t < map_size; t++) {
2815 FOR_ALL_TOWNS(town) {
2822 for (
TileIndex t = 0; t < map_size; t++) {
2842 FOR_ALL_COMPANIES(c) {
2848 extern uint8 _old_diff_level;
2865 for (
TileIndex t = 0; t < map_size; t++) {
2874 extern uint8 _old_units;
2885 for (
TileIndex t = 0; t < map_size; t++) {
2903 FOR_ALL_ROADVEHICLES(v) {
2905 skip_frames.
Clear();
2907 uint prev_tile_skip = 0;
2910 if (u->tile != prev_tile) {
2911 prev_tile_skip = cur_skip;
2912 prev_tile = u->tile;
2914 cur_skip = prev_tile_skip;
2917 uint *this_skip = skip_frames.
Append();
2918 *this_skip = prev_tile_skip;
2924 if (u->frame <= (roadside ? 9 : 5)) *this_skip = cur_skip;
2929 if (u->frame <= (roadside ? 5 : 9)) *this_skip = cur_skip;
2934 if (u->frame <= (roadside ? 4 : 2)) *this_skip = cur_skip;
2941 while (cur_skip > skip_frames[0]) {
2944 for (uint *it = skip_frames.
Begin(); it != skip_frames.
End(); ++it, prev = u, u = u->
Next()) {
2946 if (*it >= cur_skip) IndividualRoadVehicleController(u, prev);
2977 AfterLoadLabelMaps();
3011 AfterLoadStations();