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

OpenTTD Source 14.0-beta3

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 "../timer/timer_game_calendar.h"
16 #include "../timer/timer_game_economy.h"
17 #include "../zoom_func.h"
18 #include "../window_gui.h"
19 #include "../window_func.h"
20 #include "../viewport_func.h"
21 #include "../gfx_func.h"
22 #include "../core/random_func.hpp"
23 #include "../fios.h"
24 #include "../timer/timer.h"
25 #include "../timer/timer_game_tick.h"
26
27 #include "../safeguards.h"
28
29 extern TileIndex _cur_tileloop_tile;
30 extern uint16_t _disaster_delay;
31 extern byte _trees_tick_ctr;
32 extern std::string _savegame_id;
33
34 /* Keep track of current game position */
35 int _saved_scrollpos_x;
36 int _saved_scrollpos_y;
37 ZoomLevel _saved_scrollpos_zoom;
38
39 void SaveViewportBeforeSaveGame()
40 {
41 /* Don't use GetMainWindow() in case the window does not exist. */
42 const Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
43 if (w == nullptr || w->viewport == nullptr) {
44 /* Ensure saved position is clearly invalid. */
45 _saved_scrollpos_x = INT_MAX;
46 _saved_scrollpos_y = INT_MAX;
47 _saved_scrollpos_zoom = ZOOM_LVL_END;
48 } else {
49 _saved_scrollpos_x = w->viewport->scrollpos_x;
50 _saved_scrollpos_y = w->viewport->scrollpos_y;
51 _saved_scrollpos_zoom = w->viewport->zoom;
52 }
53 }
54
55 void ResetViewportAfterLoadGame()
56 {
57 Window *w = GetMainWindow();
58
59 w->viewport->scrollpos_x = _saved_scrollpos_x;
60 w->viewport->scrollpos_y = _saved_scrollpos_y;
61 w->viewport->dest_scrollpos_x = _saved_scrollpos_x;
62 w->viewport->dest_scrollpos_y = _saved_scrollpos_y;
63
64 Viewport *vp = w->viewport;
65 vp->zoom = std::min(_saved_scrollpos_zoom, ZOOM_LVL_MAX);
66 vp->virtual_width = ScaleByZoom(vp->width, vp->zoom);
67 vp->virtual_height = ScaleByZoom(vp->height, vp->zoom);
68
69 /* If zoom_max is ZOOM_LVL_MIN then the setting has not been loaded yet, therefore all levels are allowed. */
70 if (_settings_client.gui.zoom_max != ZOOM_LVL_MIN) {
71 /* Ensure zoom level is allowed */
72 while (vp->zoom < _settings_client.gui.zoom_min) DoZoomInOutWindow(ZOOM_OUT, w);
73 while (vp->zoom > _settings_client.gui.zoom_max) DoZoomInOutWindow(ZOOM_IN, w);
74 }
75
76 DoZoomInOutWindow(ZOOM_NONE, w); // update button status
77 MarkWholeScreenDirty();
78 }
79
80 byte _age_cargo_skip_counter;
81 extern TimeoutTimer<TimerGameTick> _new_competitor_timeout;
82
83 static const SaveLoad _date_desc[] = {
84 SLEG_CONDVAR("date", TimerGameCalendar::date, SLE_FILE_U16 | SLE_VAR_I32, SL_MIN_VERSION, SLV_31),
85 SLEG_CONDVAR("date", TimerGameCalendar::date, SLE_INT32, SLV_31, SL_MAX_VERSION),
86 SLEG_VAR("date_fract", TimerGameCalendar::date_fract, SLE_UINT16),
87 SLEG_CONDVAR("tick_counter", TimerGameTick::counter, SLE_FILE_U16 | SLE_VAR_U64, SL_MIN_VERSION, SLV_U64_TICK_COUNTER),
88 SLEG_CONDVAR("tick_counter", TimerGameTick::counter, SLE_UINT64, SLV_U64_TICK_COUNTER, SL_MAX_VERSION),
89 SLEG_CONDVAR("economy_date", TimerGameEconomy::date, SLE_INT32, SLV_ECONOMY_DATE, SL_MAX_VERSION),
90 SLEG_CONDVAR("economy_date_fract", TimerGameEconomy::date_fract, SLE_UINT16, SLV_ECONOMY_DATE, SL_MAX_VERSION),
91 SLEG_CONDVAR("calendar_sub_date_fract", TimerGameCalendar::sub_date_fract, SLE_UINT16, SLV_CALENDAR_SUB_DATE_FRACT, SL_MAX_VERSION),
92 SLEG_CONDVAR("age_cargo_skip_counter", _age_cargo_skip_counter, SLE_UINT8, SL_MIN_VERSION, SLV_162),
93 SLEG_CONDVAR("cur_tileloop_tile", _cur_tileloop_tile, SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_6),
94 SLEG_CONDVAR("cur_tileloop_tile", _cur_tileloop_tile, SLE_UINT32, SLV_6, SL_MAX_VERSION),
95 SLEG_VAR("next_disaster_start", _disaster_delay, SLE_UINT16),
96 SLEG_VAR("random_state[0]", _random.state[0], SLE_UINT32),
97 SLEG_VAR("random_state[1]", _random.state[1], SLE_UINT32),
98 SLEG_VAR("company_tick_counter", _cur_company_tick_index, SLE_FILE_U8 | SLE_VAR_U32),
99 SLEG_VAR("trees_tick_counter", _trees_tick_ctr, SLE_UINT8),
100 SLEG_CONDVAR("pause_mode", _pause_mode, SLE_UINT8, SLV_4, SL_MAX_VERSION),
101 SLEG_CONDSSTR("id", _savegame_id, SLE_STR, SLV_SAVEGAME_ID, SL_MAX_VERSION),
102 /* For older savegames, we load the current value as the "period"; afterload will set the "fired" and "elapsed". */
103 SLEG_CONDVAR("next_competitor_start", _new_competitor_timeout.period, SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_109),
104 SLEG_CONDVAR("next_competitor_start", _new_competitor_timeout.period, SLE_UINT32, SLV_109, SLV_AI_START_DATE),
105 SLEG_CONDVAR("competitors_interval", _new_competitor_timeout.period, SLE_UINT32, SLV_AI_START_DATE, SL_MAX_VERSION),
106 SLEG_CONDVAR("competitors_interval_elapsed", _new_competitor_timeout.storage.elapsed, SLE_UINT32, SLV_AI_START_DATE, SL_MAX_VERSION),
107 SLEG_CONDVAR("competitors_interval_fired", _new_competitor_timeout.fired, SLE_BOOL, SLV_AI_START_DATE, SL_MAX_VERSION),
108 };
109
110 static const SaveLoad _date_check_desc[] = {
111 SLEG_CONDVAR("date", _load_check_data.current_date, SLE_FILE_U16 | SLE_VAR_I32, SL_MIN_VERSION, SLV_31),
112 SLEG_CONDVAR("date", _load_check_data.current_date, SLE_INT32, SLV_31, SL_MAX_VERSION),
113 };
114
115 /* Save load date related variables as well as persistent tick counters
116 * XXX: currently some unrelated stuff is just put here */
117 struct DATEChunkHandler : ChunkHandler {
118 DATEChunkHandler() : ChunkHandler('DATE', CH_TABLE) {}
119
120 void Save() const override
121 {
122 SlTableHeader(_date_desc);
123
124 SlSetArrayIndex(0);
125 SlGlobList(_date_desc);
126 }
127
128 void LoadCommon(const SaveLoadTable &slt, const SaveLoadCompatTable &slct) const
129 {
130 const std::vector<SaveLoad> oslt = SlCompatTableHeader(slt, slct);
131
132 if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() == -1) return;
133 SlGlobList(oslt);
134 if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() != -1) SlErrorCorrupt("Too many DATE entries");
135 }
136
137 void Load() const override
138 {
139 this->LoadCommon(_date_desc, _date_sl_compat);
140 }
141
142
143 void LoadCheck(size_t) const override
144 {
145 this->LoadCommon(_date_check_desc, _date_check_sl_compat);
146
147 if (IsSavegameVersionBefore(SLV_31)) {
148 _load_check_data.current_date += CalendarTime::DAYS_TILL_ORIGINAL_BASE_YEAR;
149 }
150 }
151 };
152
153 static const SaveLoad _view_desc[] = {
154 SLEG_CONDVAR("x", _saved_scrollpos_x, SLE_FILE_I16 | SLE_VAR_I32, SL_MIN_VERSION, SLV_6),
155 SLEG_CONDVAR("x", _saved_scrollpos_x, SLE_INT32, SLV_6, SL_MAX_VERSION),
156 SLEG_CONDVAR("y", _saved_scrollpos_y, SLE_FILE_I16 | SLE_VAR_I32, SL_MIN_VERSION, SLV_6),
157 SLEG_CONDVAR("y", _saved_scrollpos_y, SLE_INT32, SLV_6, SL_MAX_VERSION),
158 SLEG_VAR("zoom", _saved_scrollpos_zoom, SLE_UINT8),
159 };
160
161 struct VIEWChunkHandler : ChunkHandler {
162 VIEWChunkHandler() : ChunkHandler('VIEW', CH_TABLE) {}
163
164 void Save() const override
165 {
166 SlTableHeader(_view_desc);
167
168 SlSetArrayIndex(0);
169 SlGlobList(_view_desc);
170 }
171
172 void Load() const override
173 {
174 const std::vector<SaveLoad> slt = SlCompatTableHeader(_view_desc, _view_sl_compat);
175
176 if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() == -1) return;
177 SlGlobList(slt);
178 if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() != -1) SlErrorCorrupt("Too many DATE entries");
179 }
180 };
181
182 static const DATEChunkHandler DATE;
183 static const VIEWChunkHandler VIEW;
184 static const ChunkHandlerRef misc_chunk_handlers[] = {
185 DATE,
186 VIEW,
187 };
188
189 extern const ChunkHandlerTable _misc_chunk_handlers(misc_chunk_handlers);
TimerGameTick::counter
static TickCounter counter
Monotonic counter, in ticks, since start of game.
Definition: timer_game_tick.h:33
TimerGameConst< struct Calendar >::DAYS_TILL_ORIGINAL_BASE_YEAR
static constexpr TimerGame< struct Calendar >::Date DAYS_TILL_ORIGINAL_BASE_YEAR
The date of the first day of the original base year.
Definition: timer_game_common.h:184
ZOOM_OUT
@ ZOOM_OUT
Zoom out (get helicopter view).
Definition: viewport_type.h:74
SaveLoadTable
std::span< const struct SaveLoad > SaveLoadTable
A table of SaveLoad entries.
Definition: saveload.h:494
SLV_U64_TICK_COUNTER
@ SLV_U64_TICK_COUNTER
300 PR#10035 Make tick counter 64bit to avoid wrapping.
Definition: saveload.h:340
SLEG_CONDSSTR
#define SLEG_CONDSSTR(name, variable, type, from, to)
Storage of a global std::string in some savegame versions.
Definition: saveload.h:1078
TimerGameCalendar::date_fract
static DateFract date_fract
Fractional part of the day.
Definition: timer_game_calendar.h:35
ZOOM_LVL_END
@ ZOOM_LVL_END
End for iteration.
Definition: zoom_type.h:28
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:488
SL_MIN_VERSION
@ SL_MIN_VERSION
First savegame version.
Definition: saveload.h:31
Window::viewport
ViewportData * viewport
Pointer to viewport data, if present.
Definition: window_gui.h:312
Viewport::height
int height
Screen height of the viewport.
Definition: viewport_type.h:26
_load_check_data
LoadCheckData _load_check_data
Data loaded from save during SL_LOAD_CHECK.
Definition: fios_gui.cpp:40
ZOOM_NONE
@ ZOOM_NONE
Hack, used to update the button status.
Definition: viewport_type.h:75
SlErrorCorrupt
void SlErrorCorrupt(const std::string &msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:362
FindWindowById
Window * FindWindowById(WindowClass cls, WindowNumber number)
Find a window by its class and window number.
Definition: window.cpp:1099
SLV_ECONOMY_DATE
@ SLV_ECONOMY_DATE
326 PR#10700 Split calendar and economy timers and dates.
Definition: saveload.h:371
DATEChunkHandler::Load
void Load() const override
Load the chunk.
Definition: misc_sl.cpp:137
_date_check_sl_compat
const SaveLoadCompat _date_check_sl_compat[]
Original field order for _date_check_desc.
Definition: misc_sl_compat.h:38
_new_competitor_timeout
TimeoutTimer< TimerGameTick > _new_competitor_timeout
Start a new competitor company if possible.
DoZoomInOutWindow
bool DoZoomInOutWindow(ZoomStateChange how, Window *w)
Zooms a viewport in a window in or out.
Definition: main_gui.cpp:93
saveload.h
SLV_109
@ SLV_109
109 15075
Definition: saveload.h:173
_random
Randomizer _random
Random used in the game state calculations.
Definition: random_func.cpp:37
ZoomLevel
ZoomLevel
All zoom levels we know.
Definition: zoom_type.h:19
TimerGameEconomy::date_fract
static DateFract date_fract
Fractional part of the day.
Definition: timer_game_economy.h:38
SaveLoadCompatTable
std::span< const struct SaveLoadCompat > SaveLoadCompatTable
A table of SaveLoadCompat entries.
Definition: saveload.h:497
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:54
ZOOM_LVL_MAX
@ ZOOM_LVL_MAX
Maximum zoom level.
Definition: zoom_type.h:44
ViewportData::dest_scrollpos_y
int32_t dest_scrollpos_y
Current destination y coordinate to display (virtual screen coordinate of topleft corner of the viewp...
Definition: window_gui.h:251
DATEChunkHandler::Save
void Save() const override
Save the chunk.
Definition: misc_sl.cpp:120
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
TimerGameCalendar::sub_date_fract
static uint16_t sub_date_fract
Subpart of date_fract that we use when calendar days are slower than economy days.
Definition: timer_game_calendar.h:36
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:442
GUISettings::zoom_max
ZoomLevel zoom_max
maximum zoom out level
Definition: settings_type.h:158
SLV_6
@ SLV_6
6.0 1721 6.1 1768
Definition: saveload.h:46
SlGlobList
void SlGlobList(const SaveLoadTable &slt)
Save or Load (a list of) global variables.
Definition: saveload.cpp:1897
SLV_CALENDAR_SUB_DATE_FRACT
@ SLV_CALENDAR_SUB_DATE_FRACT
328 PR#11428 Add sub_date_fract to measure calendar days.
Definition: saveload.h:373
ZOOM_LVL_MIN
@ ZOOM_LVL_MIN
Minimum zoom level.
Definition: zoom_type.h:43
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
SLV_162
@ SLV_162
162 22713
Definition: saveload.h:237
IsSavegameVersionBefore
bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:1203
ViewportData::dest_scrollpos_x
int32_t dest_scrollpos_x
Current destination x coordinate to display (virtual screen coordinate of topleft corner of the viewp...
Definition: window_gui.h:250
SLEG_VAR
#define SLEG_VAR(name, variable, type)
Storage of a global variable in every savegame version.
Definition: saveload.h:1124
_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
ViewportData::scrollpos_y
int32_t scrollpos_y
Currently shown y coordinate (virtual screen coordinate of topleft corner of the viewport).
Definition: window_gui.h:249
SLV_31
@ SLV_31
31 5999
Definition: saveload.h:80
SLV_RIFF_TO_ARRAY
@ SLV_RIFF_TO_ARRAY
294 PR#9375 Changed many CH_RIFF chunks to CH_ARRAY chunks.
Definition: saveload.h:332
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:379
_date_sl_compat
const SaveLoadCompat _date_sl_compat[]
Original field order for _date_desc.
Definition: misc_sl_compat.h:16
_disaster_delay
uint16_t _disaster_delay
Delay counter for considering the next disaster.
Definition: disaster_vehicle.cpp:57
Viewport::virtual_width
int virtual_width
width << zoom
Definition: viewport_type.h: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:37
VIEWChunkHandler
Definition: misc_sl.cpp:161
_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:53
GUISettings::zoom_min
ZoomLevel zoom_min
minimum zoom out level
Definition: settings_type.h:157
_savegame_id
std::string _savegame_id
Unique ID of the current savegame.
Definition: misc.cpp:39
VIEWChunkHandler::Load
void Load() const override
Load the chunk.
Definition: misc_sl.cpp:172
DATEChunkHandler::LoadCheck
void LoadCheck(size_t) const override
Load the chunk for game preview.
Definition: misc_sl.cpp:143
TimeoutTimer
A timeout timer will fire once after the interval.
Definition: timer.h:116
SLEG_CONDVAR
#define SLEG_CONDVAR(name, variable, type, from, to)
Storage of a global variable in some savegame versions.
Definition: saveload.h:1047
_view_sl_compat
const SaveLoadCompat _view_sl_compat[]
Original field order for _view_desc.
Definition: misc_sl_compat.h:62
GetMainWindow
Window * GetMainWindow()
Get the main window, i.e.
Definition: window.cpp:1128
_age_cargo_skip_counter
byte _age_cargo_skip_counter
Skip aging of cargo? Used before savegame version 162.
Definition: misc_sl.cpp:80
ViewportData::scrollpos_x
int32_t scrollpos_x
Currently shown x coordinate (virtual screen coordinate of topleft corner of the viewport).
Definition: window_gui.h:248
ChunkHandlerTable
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition: saveload.h:491
WC_MAIN_WINDOW
@ WC_MAIN_WINDOW
Main window; Window numbers:
Definition: window_type.h:51
DATEChunkHandler
Definition: misc_sl.cpp:117
SLV_AI_START_DATE
@ SLV_AI_START_DATE
309 PR#10653 Removal of individual AI start dates and added a generic one.
Definition: saveload.h:350
Viewport::zoom
ZoomLevel zoom
The zoom level of the viewport.
Definition: viewport_type.h:33
MarkWholeScreenDirty
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition: gfx.cpp:1552
Randomizer::state
uint32_t state[2]
The state of the randomizer.
Definition: random_func.hpp:23
SlCompatTableHeader
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition: saveload.cpp:1843
TimerGameCalendar::date
static Date date
Current date in days (day counter).
Definition: timer_game_calendar.h:34
SLV_SAVEGAME_ID
@ SLV_SAVEGAME_ID
313 PR#10719 Add an unique ID to every savegame (used to deduplicate surveys).
Definition: saveload.h:355
Window
Data structure for an opened window.
Definition: window_gui.h:267
VIEWChunkHandler::Save
void Save() const override
Save the chunk.
Definition: misc_sl.cpp:164
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:1705
SaveLoad
SaveLoad type struct.
Definition: saveload.h:694
ScaleByZoom
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
misc_sl_compat.h
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:647
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition: settings_type.h:636
TimerGameEconomy::date
static Date date
Current date in days (day counter).
Definition: timer_game_economy.h:37