Fix: Don't try to rename OWNER_DEITY signs in-game (#9716)
[openttd-github.git] / src / saveload / misc_sl.cpp
blob779e1009893fb39742d14e7007bf2cb23ee4409e
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 */
8 /** @file misc_sl.cpp Saving and loading of things that didn't fit anywhere else */
10 #include "../stdafx.h"
12 #include "saveload.h"
13 #include "compat/misc_sl_compat.h"
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"
24 #include "../safeguards.h"
26 extern TileIndex _cur_tileloop_tile;
27 extern uint16 _disaster_delay;
28 extern byte _trees_tick_ctr;
30 /* Keep track of current game position */
31 int _saved_scrollpos_x;
32 int _saved_scrollpos_y;
33 ZoomLevel _saved_scrollpos_zoom;
35 void SaveViewportBeforeSaveGame()
37 const Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
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;
46 void ResetViewportAfterLoadGame()
48 Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
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;
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);
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);
67 DoZoomInOutWindow(ZOOM_NONE, w); // update button status
68 MarkWholeScreenDirty();
71 byte _age_cargo_skip_counter; ///< Skip aging of cargo? Used before savegame version 162.
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_VAR("tick_counter", _tick_counter, SLE_UINT16),
78 SLEG_CONDVAR("age_cargo_skip_counter", _age_cargo_skip_counter, SLE_UINT8, SL_MIN_VERSION, SLV_162),
79 SLEG_CONDVAR("cur_tileloop_tile", _cur_tileloop_tile, SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_6),
80 SLEG_CONDVAR("cur_tileloop_tile", _cur_tileloop_tile, SLE_UINT32, SLV_6, SL_MAX_VERSION),
81 SLEG_VAR("next_disaster_start", _disaster_delay, SLE_UINT16),
82 SLEG_VAR("random_state[0]", _random.state[0], SLE_UINT32),
83 SLEG_VAR("random_state[1]", _random.state[1], SLE_UINT32),
84 SLEG_VAR("company_tick_counter", _cur_company_tick_index, SLE_FILE_U8 | SLE_VAR_U32),
85 SLEG_CONDVAR("next_competitor_start", _next_competitor_start, SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_109),
86 SLEG_CONDVAR("next_competitor_start", _next_competitor_start, SLE_UINT32, SLV_109, SL_MAX_VERSION),
87 SLEG_VAR("trees_tick_counter", _trees_tick_ctr, SLE_UINT8),
88 SLEG_CONDVAR("pause_mode", _pause_mode, SLE_UINT8, SLV_4, SL_MAX_VERSION),
91 static const SaveLoad _date_check_desc[] = {
92 SLEG_CONDVAR("date", _load_check_data.current_date, SLE_FILE_U16 | SLE_VAR_I32, SL_MIN_VERSION, SLV_31),
93 SLEG_CONDVAR("date", _load_check_data.current_date, SLE_INT32, SLV_31, SL_MAX_VERSION),
96 /* Save load date related variables as well as persistent tick counters
97 * XXX: currently some unrelated stuff is just put here */
98 struct DATEChunkHandler : ChunkHandler {
99 DATEChunkHandler() : ChunkHandler('DATE', CH_TABLE) {}
101 void Save() const override
103 SlTableHeader(_date_desc);
105 SlSetArrayIndex(0);
106 SlGlobList(_date_desc);
109 void LoadCommon(const SaveLoadTable &slt, const SaveLoadCompatTable &slct) const
111 const std::vector<SaveLoad> oslt = SlCompatTableHeader(slt, slct);
113 if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() == -1) return;
114 SlGlobList(oslt);
115 if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() != -1) SlErrorCorrupt("Too many DATE entries");
118 void Load() const override
120 this->LoadCommon(_date_desc, _date_sl_compat);
124 void LoadCheck(size_t) const override
126 this->LoadCommon(_date_check_desc, _date_check_sl_compat);
128 if (IsSavegameVersionBefore(SLV_31)) {
129 _load_check_data.current_date += DAYS_TILL_ORIGINAL_BASE_YEAR;
134 static const SaveLoad _view_desc[] = {
135 SLEG_CONDVAR("x", _saved_scrollpos_x, SLE_FILE_I16 | SLE_VAR_I32, SL_MIN_VERSION, SLV_6),
136 SLEG_CONDVAR("x", _saved_scrollpos_x, SLE_INT32, SLV_6, SL_MAX_VERSION),
137 SLEG_CONDVAR("y", _saved_scrollpos_y, SLE_FILE_I16 | SLE_VAR_I32, SL_MIN_VERSION, SLV_6),
138 SLEG_CONDVAR("y", _saved_scrollpos_y, SLE_INT32, SLV_6, SL_MAX_VERSION),
139 SLEG_VAR("zoom", _saved_scrollpos_zoom, SLE_UINT8),
142 struct VIEWChunkHandler : ChunkHandler {
143 VIEWChunkHandler() : ChunkHandler('VIEW', CH_TABLE) {}
145 void Save() const override
147 SlTableHeader(_view_desc);
149 SlSetArrayIndex(0);
150 SlGlobList(_view_desc);
153 void Load() const override
155 const std::vector<SaveLoad> slt = SlCompatTableHeader(_view_desc, _view_sl_compat);
157 if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() == -1) return;
158 SlGlobList(slt);
159 if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() != -1) SlErrorCorrupt("Too many DATE entries");
163 static const DATEChunkHandler DATE;
164 static const VIEWChunkHandler VIEW;
165 static const ChunkHandlerRef misc_chunk_handlers[] = {
166 DATE,
167 VIEW,
170 extern const ChunkHandlerTable _misc_chunk_handlers(misc_chunk_handlers);