OpenTTD Source: src/saveload/misc_sl.cpp Source File

OpenTTD Source 13.2.1

misc_sl.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10 #include "../stdafx.h"
11
12 #include "saveload.h"
13 #include "compat/misc_sl_compat.h"
14
15 #include "../date_func.h"
16 #include "../zoom_func.h"
17 #include "../window_gui.h"
18 #include "../window_func.h"
19 #include "../viewport_func.h"
20 #include "../gfx_func.h"
21 #include "../core/random_func.hpp"
22 #include "../fios.h"
23
24 #include "../safeguards.h"
25
26 extern TileIndex _cur_tileloop_tile;
27 extern uint16 _disaster_delay;
28 extern byte _trees_tick_ctr;
29
30 /* Keep track of current game position */
31 int _saved_scrollpos_x;
32 int _saved_scrollpos_y;
33 ZoomLevel _saved_scrollpos_zoom;
34
35 void SaveViewportBeforeSaveGame()
36 {
37 const Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
38
39 if (w != nullptr) {
40 _saved_scrollpos_x = w->viewport->scrollpos_x;
41 _saved_scrollpos_y = w->viewport->scrollpos_y;
42 _saved_scrollpos_zoom = w->viewport->zoom;
43 }
44 }
45
46 void ResetViewportAfterLoadGame()
47 {
48 Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
49
50 w->viewport->scrollpos_x = _saved_scrollpos_x;
51 w->viewport->scrollpos_y = _saved_scrollpos_y;
52 w->viewport->dest_scrollpos_x = _saved_scrollpos_x;
53 w->viewport->dest_scrollpos_y = _saved_scrollpos_y;
54
55 Viewport *vp = w->viewport;
56 vp->zoom = std::min(_saved_scrollpos_zoom, ZOOM_LVL_MAX);
57 vp->virtual_width = ScaleByZoom(vp->width, vp->zoom);
58 vp->virtual_height = ScaleByZoom(vp->height, vp->zoom);
59
60 /* If zoom_max is ZOOM_LVL_MIN then the setting has not been loaded yet, therefore all levels are allowed. */
61 if (_settings_client.gui.zoom_max != ZOOM_LVL_MIN) {
62 /* Ensure zoom level is allowed */
63 while (vp->zoom < _settings_client.gui.zoom_min) DoZoomInOutWindow(ZOOM_OUT, w);
64 while (vp->zoom > _settings_client.gui.zoom_max) DoZoomInOutWindow(ZOOM_IN, w);
65 }
66
67 DoZoomInOutWindow(ZOOM_NONE, w); // update button status
68 MarkWholeScreenDirty();
69 }
70
71 byte _age_cargo_skip_counter;
72
73 static const SaveLoad _date_desc[] = {
74 SLEG_CONDVAR("date", _date, SLE_FILE_U16 | SLE_VAR_I32, SL_MIN_VERSION, SLV_31),
75 SLEG_CONDVAR("date", _date, SLE_INT32, SLV_31, SL_MAX_VERSION),
76 SLEG_VAR("date_fract", _date_fract, SLE_UINT16),
77 SLEG_CONDVAR("tick_counter", _tick_counter, SLE_FILE_U16 | SLE_VAR_U64, SL_MIN_VERSION, SLV_U64_TICK_COUNTER),
78 SLEG_CONDVAR("tick_counter", _tick_counter, SLE_UINT64, SLV_U64_TICK_COUNTER, SL_MAX_VERSION),
79 SLEG_CONDVAR("age_cargo_skip_counter", _age_cargo_skip_counter, SLE_UINT8, SL_MIN_VERSION, SLV_162),
80 SLEG_CONDVAR("cur_tileloop_tile", _cur_tileloop_tile, SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_6),
81 SLEG_CONDVAR("cur_tileloop_tile", _cur_tileloop_tile, SLE_UINT32, SLV_6, SL_MAX_VERSION),
82 SLEG_VAR("next_disaster_start", _disaster_delay, SLE_UINT16),
83 SLEG_VAR("random_state[0]", _random.state[0], SLE_UINT32),
84 SLEG_VAR("random_state[1]", _random.state[1], SLE_UINT32),
85 SLEG_VAR("company_tick_counter", _cur_company_tick_index, SLE_FILE_U8 | SLE_VAR_U32),
86 SLEG_CONDVAR("next_competitor_start", _next_competitor_start, SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_109),
87 SLEG_CONDVAR("next_competitor_start", _next_competitor_start, SLE_UINT32, SLV_109, SL_MAX_VERSION),
88 SLEG_VAR("trees_tick_counter", _trees_tick_ctr, SLE_UINT8),
89 SLEG_CONDVAR("pause_mode", _pause_mode, SLE_UINT8, SLV_4, SL_MAX_VERSION),
90 };
91
92 static const SaveLoad _date_check_desc[] = {
93 SLEG_CONDVAR("date", _load_check_data.current_date, SLE_FILE_U16 | SLE_VAR_I32, SL_MIN_VERSION, SLV_31),
94 SLEG_CONDVAR("date", _load_check_data.current_date, SLE_INT32, SLV_31, SL_MAX_VERSION),
95 };
96
97 /* Save load date related variables as well as persistent tick counters
98 * XXX: currently some unrelated stuff is just put here */
99 struct DATEChunkHandler : ChunkHandler {
100 DATEChunkHandler() : ChunkHandler('DATE', CH_TABLE) {}
101
102 void Save() const override
103 {
104 SlTableHeader(_date_desc);
105
106 SlSetArrayIndex(0);
107 SlGlobList(_date_desc);
108 }
109
110 void LoadCommon(const SaveLoadTable &slt, const SaveLoadCompatTable &slct) const
111 {
112 const std::vector<SaveLoad> oslt = SlCompatTableHeader(slt, slct);
113
114 if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() == -1) return;
115 SlGlobList(oslt);
116 if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() != -1) SlErrorCorrupt("Too many DATE entries");
117 }
118
119 void Load() const override
120 {
121 this->LoadCommon(_date_desc, _date_sl_compat);
122 }
123
124
125 void LoadCheck(size_t) const override
126 {
127 this->LoadCommon(_date_check_desc, _date_check_sl_compat);
128
129 if (IsSavegameVersionBefore(SLV_31)) {
130 _load_check_data.current_date += DAYS_TILL_ORIGINAL_BASE_YEAR;
131 }
132 }
133 };
134
135 static const SaveLoad _view_desc[] = {
136 SLEG_CONDVAR("x", _saved_scrollpos_x, SLE_FILE_I16 | SLE_VAR_I32, SL_MIN_VERSION, SLV_6),
137 SLEG_CONDVAR("x", _saved_scrollpos_x, SLE_INT32, SLV_6, SL_MAX_VERSION),
138 SLEG_CONDVAR("y", _saved_scrollpos_y, SLE_FILE_I16 | SLE_VAR_I32, SL_MIN_VERSION, SLV_6),
139 SLEG_CONDVAR("y", _saved_scrollpos_y, SLE_INT32, SLV_6, SL_MAX_VERSION),
140 SLEG_VAR("zoom", _saved_scrollpos_zoom, SLE_UINT8),
141 };
142
143 struct VIEWChunkHandler : ChunkHandler {
144 VIEWChunkHandler() : ChunkHandler('VIEW', CH_TABLE) {}
145
146 void Save() const override
147 {
148 SlTableHeader(_view_desc);
149
150 SlSetArrayIndex(0);
151 SlGlobList(_view_desc);
152 }
153
154 void Load() const override
155 {
156 const std::vector<SaveLoad> slt = SlCompatTableHeader(_view_desc, _view_sl_compat);
157
158 if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() == -1) return;
159 SlGlobList(slt);
160 if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() != -1) SlErrorCorrupt("Too many DATE entries");
161 }
162 };
163
164 static const DATEChunkHandler DATE;
165 static const VIEWChunkHandler VIEW;
166 static const ChunkHandlerRef misc_chunk_handlers[] = {
167 DATE,
168 VIEW,
169 };
170
171 extern const ChunkHandlerTable _misc_chunk_handlers(misc_chunk_handlers);
SLV_RIFF_TO_ARRAY
@ SLV_RIFF_TO_ARRAY
294 PR#9375 Changed many CH_RIFF chunks to CH_ARRAY chunks.
Definition: saveload.h:336
SLV_162
@ SLV_162
162 22713
Definition: saveload.h:241
ZOOM_OUT
@ ZOOM_OUT
Zoom out (get helicopter view).
Definition: viewport_type.h:74
SL_MIN_VERSION
@ SL_MIN_VERSION
First savegame version.
Definition: saveload.h:35
SLV_U64_TICK_COUNTER
@ SLV_U64_TICK_COUNTER
300 PR#10035 Make _tick_counter 64bit to avoid wrapping.
Definition: saveload.h:344
Viewport::width
int width
Screen width of the viewport.
Definition: viewport_type.h:25
ChunkHandlerRef
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition: saveload.h:449
_date_fract
DateFract _date_fract
Fractional part of the day.
Definition: date.cpp:29
Window::viewport
ViewportData * viewport
Pointer to viewport data, if present.
Definition: window_gui.h:255
Viewport::height
int height
Screen height of the viewport.
Definition: viewport_type.h:26
ViewportData::scrollpos_y
int32 scrollpos_y
Currently shown y coordinate (virtual screen coordinate of topleft corner of the viewport).
Definition: window_gui.h:195
_load_check_data
LoadCheckData _load_check_data
Data loaded from save during SL_LOAD_CHECK.
Definition: fios_gui.cpp:39
ZOOM_NONE
@ ZOOM_NONE
Hack, used to update the button status.
Definition: viewport_type.h:75
FindWindowById
Window * FindWindowById(WindowClass cls, WindowNumber number)
Find a window by its class and window number.
Definition: window.cpp:1161
TileIndex
The index/ID of a Tile.
Definition: tile_type.h:85
DATEChunkHandler::Load
void Load() const override
Load the chunk.
Definition: misc_sl.cpp:119
_date_check_sl_compat
const SaveLoadCompat _date_check_sl_compat[]
Original field order for _date_check_desc.
Definition: misc_sl_compat.h:38
DoZoomInOutWindow
bool DoZoomInOutWindow(ZoomStateChange how, Window *w)
Zooms a viewport in a window in or out.
Definition: main_gui.cpp:92
saveload.h
_random
Randomizer _random
Random used in the game state calculations.
Definition: random_func.cpp:25
ZoomLevel
ZoomLevel
All zoom levels we know.
Definition: zoom_type.h:19
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:53
ZOOM_LVL_MAX
@ ZOOM_LVL_MAX
Maximum zoom level.
Definition: zoom_type.h:46
DATEChunkHandler::Save
void Save() const override
Save the chunk.
Definition: misc_sl.cpp:102
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:412
GUISettings::zoom_max
ZoomLevel zoom_max
maximum zoom out level
Definition: settings_type.h:135
SlGlobList
void SlGlobList(const SaveLoadTable &slt)
Save or Load (a list of) global variables.
Definition: saveload.cpp:2084
ZOOM_LVL_MIN
@ ZOOM_LVL_MIN
Minimum zoom level.
Definition: zoom_type.h:45
ZOOM_IN
@ ZOOM_IN
Zoom in (get more detailed view).
Definition: viewport_type.h:73
Viewport
Data structure for viewport, display of a part of the world.
Definition: viewport_type.h:22
_date
Date _date
Current date in days (day counter)
Definition: date.cpp:28
span
A trimmed down version of what std::span will be in C++20.
Definition: span_type.hpp:60
SLV_31
@ SLV_31
31 5999
Definition: saveload.h:84
SLEG_VAR
#define SLEG_VAR(name, variable, type)
Storage of a global variable in every savegame version.
Definition: saveload.h:943
_trees_tick_ctr
byte _trees_tick_ctr
Determines when to consider building more trees.
Definition: tree_cmd.cpp:55
_pause_mode
PauseMode _pause_mode
The current pause mode.
Definition: gfx.cpp:50
SLV_109
@ SLV_109
109 15075
Definition: saveload.h:177
IsSavegameVersionBefore
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:1030
_date_sl_compat
const SaveLoadCompat _date_sl_compat[]
Original field order for _date_desc.
Definition: misc_sl_compat.h:16
Viewport::virtual_width
int virtual_width
width << zoom
Definition: viewport_type.h:30
ViewportData::dest_scrollpos_y
int32 dest_scrollpos_y
Current destination y coordinate to display (virtual screen coordinate of topleft corner of the viewp...
Definition: window_gui.h:197
VIEWChunkHandler
Definition: misc_sl.cpp:143
Randomizer::state
uint32 state[2]
The state of the randomizer.
Definition: random_func.hpp:23
_cur_company_tick_index
uint _cur_company_tick_index
used to generate a name for one company that doesn't have a name yet per tick
Definition: company_cmd.cpp:51
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:348
GUISettings::zoom_min
ZoomLevel zoom_min
minimum zoom out level
Definition: settings_type.h:134
VIEWChunkHandler::Load
void Load() const override
Load the chunk.
Definition: misc_sl.cpp:154
_next_competitor_start
uint _next_competitor_start
the number of ticks before the next AI is started
Definition: company_cmd.cpp:50
DATEChunkHandler::LoadCheck
void LoadCheck(size_t) const override
Load the chunk for game preview.
Definition: misc_sl.cpp:125
ViewportData::scrollpos_x
int32 scrollpos_x
Currently shown x coordinate (virtual screen coordinate of topleft corner of the viewport).
Definition: window_gui.h:194
ScaleByZoom
static int ScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift left (when zoom > ZOOM_LVL_NORMAL) When shifting right,...
Definition: zoom_func.h:22
_disaster_delay
uint16 _disaster_delay
Delay counter for considering the next disaster.
Definition: disaster_vehicle.cpp:55
SLEG_CONDVAR
#define SLEG_CONDVAR(name, variable, type, from, to)
Storage of a global variable in some savegame versions.
Definition: saveload.h:855
SLV_6
@ SLV_6
6.0 1721 6.1 1768
Definition: saveload.h:50
_view_sl_compat
const SaveLoadCompat _view_sl_compat[]
Original field order for _view_desc.
Definition: misc_sl_compat.h:62
SlErrorCorrupt
void NORETURN SlErrorCorrupt(const char *msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:365
_age_cargo_skip_counter
byte _age_cargo_skip_counter
Skip aging of cargo? Used before savegame version 162.
Definition: misc_sl.cpp:71
WC_MAIN_WINDOW
@ WC_MAIN_WINDOW
Main window; Window numbers:
Definition: window_type.h:44
DATEChunkHandler
Definition: misc_sl.cpp:99
Viewport::zoom
ZoomLevel zoom
The zoom level of the viewport.
Definition: viewport_type.h:33
ViewportData::dest_scrollpos_x
int32 dest_scrollpos_x
Current destination x coordinate to display (virtual screen coordinate of topleft corner of the viewp...
Definition: window_gui.h:196
MarkWholeScreenDirty
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition: gfx.cpp:1767
SlCompatTableHeader
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition: saveload.cpp:2030
Window
Data structure for an opened window.
Definition: window_gui.h:213
VIEWChunkHandler::Save
void Save() const override
Save the chunk.
Definition: misc_sl.cpp:146
Viewport::virtual_height
int virtual_height
height << zoom
Definition: viewport_type.h:31
SlTableHeader
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition: saveload.cpp:1892
_tick_counter
uint64 _tick_counter
Ever incrementing tick counter for setting off various events.
Definition: date.cpp:30
SLV_4
@ SLV_4
4.0 1 4.1 122 0.3.3, 0.3.4 4.2 1222 0.3.5 4.3 1417 4.4 1426
Definition: saveload.h:41
SaveLoad
SaveLoad type struct.
Definition: saveload.h:659
misc_sl_compat.h
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:671
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition: settings_type.h:604
DAYS_TILL_ORIGINAL_BASE_YEAR
#define DAYS_TILL_ORIGINAL_BASE_YEAR
The offset in days from the '_date == 0' till 'ConvertYMDToDate(ORIGINAL_BASE_YEAR,...
Definition: date_type.h:81