Update readme.md
[openttd-joker.git] / src / saveload / misc_sl.cpp
blob188b646aa4f4421688a7c3b0a908e877060fb894
1 /* $Id: misc_sl.cpp 23740 2012-01-03 21:32:51Z rubidium $ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file misc_sl.cpp Saving and loading of things that didn't fit anywhere else */
12 #include "../stdafx.h"
13 #include "../date_func.h"
14 #include "../zoom_func.h"
15 #include "../window_gui.h"
16 #include "../window_func.h"
17 #include "../viewport_func.h"
18 #include "../gfx_func.h"
19 #include "../core/random_func.hpp"
20 #include "../fios.h"
22 #include "saveload.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 ZoomLevelByte _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 = (ZoomLevel)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 SaveLoadGlobVarList _date_desc[] = {
74 SLEG_CONDVAR(_date, SLE_FILE_U16 | SLE_VAR_I32, 0, 30),
75 SLEG_CONDVAR(_date, SLE_INT32, 31, SL_MAX_VERSION),
76 SLEG_VAR(_date_fract, SLE_UINT16),
77 SLEG_VAR(_tick_counter, SLE_UINT16),
78 SLE_CONDNULL(2, 0, 156), // _vehicle_id_ctr_day
79 SLEG_CONDVAR(_age_cargo_skip_counter, SLE_UINT8, 0, 161),
80 SLE_CONDNULL(1, 0, 45),
81 SLEG_CONDVAR(_cur_tileloop_tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
82 SLEG_CONDVAR(_cur_tileloop_tile, SLE_UINT32, 6, SL_MAX_VERSION),
83 SLEG_VAR(_disaster_delay, SLE_UINT16),
84 SLE_CONDNULL(2, 0, 119),
85 SLEG_VAR(_random.state[0], SLE_UINT32),
86 SLEG_VAR(_random.state[1], SLE_UINT32),
87 SLE_CONDNULL(1, 0, 9),
88 SLE_CONDNULL(4, 10, 119),
89 SLEG_VAR(_cur_company_tick_index, SLE_FILE_U8 | SLE_VAR_U32),
90 SLEG_CONDVAR(_next_competitor_start, SLE_FILE_U16 | SLE_VAR_U32, 0, 108),
91 SLEG_CONDVAR(_next_competitor_start, SLE_UINT32, 109, SL_MAX_VERSION),
92 SLEG_VAR(_trees_tick_ctr, SLE_UINT8),
93 SLEG_CONDVAR(_pause_mode, SLE_UINT8, 4, SL_MAX_VERSION),
94 SLE_CONDNULL(4, 11, 119),
95 SLEG_END()
98 static const SaveLoadGlobVarList _date_check_desc[] = {
99 SLEG_CONDVAR(_load_check_data.current_date, SLE_FILE_U16 | SLE_VAR_I32, 0, 30),
100 SLEG_CONDVAR(_load_check_data.current_date, SLE_INT32, 31, SL_MAX_VERSION),
101 SLE_NULL(2), // _date_fract
102 SLE_NULL(2), // _tick_counter
103 SLE_CONDNULL(2, 0, 156), // _vehicle_id_ctr_day
104 SLE_CONDNULL(1, 0, 161), // _age_cargo_skip_counter
105 SLE_CONDNULL(1, 0, 45),
106 SLE_CONDNULL(2, 0, 5), // _cur_tileloop_tile
107 SLE_CONDNULL(4, 6, SL_MAX_VERSION), // _cur_tileloop_tile
108 SLE_NULL(2), // _disaster_delay
109 SLE_CONDNULL(2, 0, 119),
110 SLE_NULL(4), // _random.state[0]
111 SLE_NULL(4), // _random.state[1]
112 SLE_CONDNULL(1, 0, 9),
113 SLE_CONDNULL(4, 10, 119),
114 SLE_NULL(1), // _cur_company_tick_index
115 SLE_CONDNULL(2, 0, 108), // _next_competitor_start
116 SLE_CONDNULL(4, 109, SL_MAX_VERSION), // _next_competitor_start
117 SLE_NULL(1), // _trees_tick_ctr
118 SLE_CONDNULL(1, 4, SL_MAX_VERSION), // _pause_mode
119 SLE_CONDNULL(4, 11, 119),
120 SLEG_END()
123 /* Save load date related variables as well as persistent tick counters
124 * XXX: currently some unrelated stuff is just put here */
125 static void SaveLoad_DATE()
127 SlGlobList(_date_desc);
130 static void Check_DATE()
132 SlGlobList(_date_check_desc);
133 if (IsSavegameVersionBefore(31)) {
134 _load_check_data.current_date += DAYS_TILL_ORIGINAL_BASE_YEAR;
139 static const SaveLoadGlobVarList _view_desc[] = {
140 SLEG_CONDVAR(_saved_scrollpos_x, SLE_FILE_I16 | SLE_VAR_I32, 0, 5),
141 SLEG_CONDVAR(_saved_scrollpos_x, SLE_INT32, 6, SL_MAX_VERSION),
142 SLEG_CONDVAR(_saved_scrollpos_y, SLE_FILE_I16 | SLE_VAR_I32, 0, 5),
143 SLEG_CONDVAR(_saved_scrollpos_y, SLE_INT32, 6, SL_MAX_VERSION),
144 SLEG_VAR(_saved_scrollpos_zoom, SLE_UINT8),
145 SLEG_END()
148 static void SaveLoad_VIEW()
150 SlGlobList(_view_desc);
153 extern const ChunkHandler _misc_chunk_handlers[] = {
154 { 'DATE', SaveLoad_DATE, SaveLoad_DATE, nullptr, Check_DATE, CH_RIFF},
155 { 'VIEW', SaveLoad_VIEW, SaveLoad_VIEW, nullptr, nullptr, CH_RIFF | CH_LAST},