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

OpenTTD Source 1.11.0-RC1

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 #include "../date_func.h"
12 #include "../zoom_func.h"
13 #include "../window_gui.h"
14 #include "../window_func.h"
15 #include "../viewport_func.h"
16 #include "../gfx_func.h"
17 #include "../core/random_func.hpp"
18 #include "../fios.h"
19
20 #include "saveload.h"
21
22 #include "../safeguards.h"
23
24 extern TileIndex _cur_tileloop_tile;
25 extern uint16 _disaster_delay;
26 extern byte _trees_tick_ctr;
27
28 /* Keep track of current game position */
29 int _saved_scrollpos_x;
30 int _saved_scrollpos_y;
31 ZoomLevel _saved_scrollpos_zoom;
32
33 void SaveViewportBeforeSaveGame()
34 {
35 const Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
36
37 if (w != nullptr) {
38 _saved_scrollpos_x = w->viewport->scrollpos_x;
39 _saved_scrollpos_y = w->viewport->scrollpos_y;
40 _saved_scrollpos_zoom = w->viewport->zoom;
41 }
42 }
43
44 void ResetViewportAfterLoadGame()
45 {
46 Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
47
48 w->viewport->scrollpos_x = _saved_scrollpos_x;
49 w->viewport->scrollpos_y = _saved_scrollpos_y;
50 w->viewport->dest_scrollpos_x = _saved_scrollpos_x;
51 w->viewport->dest_scrollpos_y = _saved_scrollpos_y;
52
53 Viewport *vp = w->viewport;
54 vp->zoom = std::min(_saved_scrollpos_zoom, ZOOM_LVL_MAX);
55 vp->virtual_width = ScaleByZoom(vp->width, vp->zoom);
56 vp->virtual_height = ScaleByZoom(vp->height, vp->zoom);
57
58 /* If zoom_max is ZOOM_LVL_MIN then the setting has not been loaded yet, therefore all levels are allowed. */
59 if (_settings_client.gui.zoom_max != ZOOM_LVL_MIN) {
60 /* Ensure zoom level is allowed */
61 while (vp->zoom < _settings_client.gui.zoom_min) DoZoomInOutWindow(ZOOM_OUT, w);
62 while (vp->zoom > _settings_client.gui.zoom_max) DoZoomInOutWindow(ZOOM_IN, w);
63 }
64
65 DoZoomInOutWindow(ZOOM_NONE, w); // update button status
66 MarkWholeScreenDirty();
67 }
68
69 byte _age_cargo_skip_counter;
70
71 static const SaveLoadGlobVarList _date_desc[] = {
72 SLEG_CONDVAR(_date, SLE_FILE_U16 | SLE_VAR_I32, SL_MIN_VERSION, SLV_31),
73 SLEG_CONDVAR(_date, SLE_INT32, SLV_31, SL_MAX_VERSION),
74 SLEG_VAR(_date_fract, SLE_UINT16),
75 SLEG_VAR(_tick_counter, SLE_UINT16),
76 SLE_CONDNULL(2, SL_MIN_VERSION, SLV_157), // _vehicle_id_ctr_day
77 SLEG_CONDVAR(_age_cargo_skip_counter, SLE_UINT8, SL_MIN_VERSION, SLV_162),
78 SLE_CONDNULL(1, SL_MIN_VERSION, SLV_46),
79 SLEG_CONDVAR(_cur_tileloop_tile, SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_6),
80 SLEG_CONDVAR(_cur_tileloop_tile, SLE_UINT32, SLV_6, SL_MAX_VERSION),
81 SLEG_VAR(_disaster_delay, SLE_UINT16),
82 SLE_CONDNULL(2, SL_MIN_VERSION, SLV_120),
83 SLEG_VAR(_random.state[0], SLE_UINT32),
84 SLEG_VAR(_random.state[1], SLE_UINT32),
85 SLE_CONDNULL(1, SL_MIN_VERSION, SLV_10),
86 SLE_CONDNULL(4, SLV_10, SLV_120),
87 SLEG_VAR(_cur_company_tick_index, SLE_FILE_U8 | SLE_VAR_U32),
88 SLEG_CONDVAR(_next_competitor_start, SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_109),
89 SLEG_CONDVAR(_next_competitor_start, SLE_UINT32, SLV_109, SL_MAX_VERSION),
90 SLEG_VAR(_trees_tick_ctr, SLE_UINT8),
91 SLEG_CONDVAR(_pause_mode, SLE_UINT8, SLV_4, SL_MAX_VERSION),
92 SLE_CONDNULL(4, SLV_11, SLV_120),
93 SLEG_END()
94 };
95
96 static const SaveLoadGlobVarList _date_check_desc[] = {
97 SLEG_CONDVAR(_load_check_data.current_date, SLE_FILE_U16 | SLE_VAR_I32, SL_MIN_VERSION, SLV_31),
98 SLEG_CONDVAR(_load_check_data.current_date, SLE_INT32, SLV_31, SL_MAX_VERSION),
99 SLE_NULL(2), // _date_fract
100 SLE_NULL(2), // _tick_counter
101 SLE_CONDNULL(2, SL_MIN_VERSION, SLV_157), // _vehicle_id_ctr_day
102 SLE_CONDNULL(1, SL_MIN_VERSION, SLV_162), // _age_cargo_skip_counter
103 SLE_CONDNULL(1, SL_MIN_VERSION, SLV_46),
104 SLE_CONDNULL(2, SL_MIN_VERSION, SLV_6), // _cur_tileloop_tile
105 SLE_CONDNULL(4, SLV_6, SL_MAX_VERSION), // _cur_tileloop_tile
106 SLE_NULL(2), // _disaster_delay
107 SLE_CONDNULL(2, SL_MIN_VERSION, SLV_120),
108 SLE_NULL(4), // _random.state[0]
109 SLE_NULL(4), // _random.state[1]
110 SLE_CONDNULL(1, SL_MIN_VERSION, SLV_10),
111 SLE_CONDNULL(4, SLV_10, SLV_120),
112 SLE_NULL(1), // _cur_company_tick_index
113 SLE_CONDNULL(2, SL_MIN_VERSION, SLV_109), // _next_competitor_start
114 SLE_CONDNULL(4, SLV_109, SL_MAX_VERSION), // _next_competitor_start
115 SLE_NULL(1), // _trees_tick_ctr
116 SLE_CONDNULL(1, SLV_4, SL_MAX_VERSION), // _pause_mode
117 SLE_CONDNULL(4, SLV_11, SLV_120),
118 SLEG_END()
119 };
120
121 /* Save load date related variables as well as persistent tick counters
122 * XXX: currently some unrelated stuff is just put here */
123 static void SaveLoad_DATE()
124 {
125 SlGlobList(_date_desc);
126 }
127
128 static void Check_DATE()
129 {
130 SlGlobList(_date_check_desc);
131 if (IsSavegameVersionBefore(SLV_31)) {
132 _load_check_data.current_date += DAYS_TILL_ORIGINAL_BASE_YEAR;
133 }
134 }
135
136
137 static const SaveLoadGlobVarList _view_desc[] = {
138 SLEG_CONDVAR(_saved_scrollpos_x, SLE_FILE_I16 | SLE_VAR_I32, SL_MIN_VERSION, SLV_6),
139 SLEG_CONDVAR(_saved_scrollpos_x, SLE_INT32, SLV_6, SL_MAX_VERSION),
140 SLEG_CONDVAR(_saved_scrollpos_y, SLE_FILE_I16 | SLE_VAR_I32, SL_MIN_VERSION, SLV_6),
141 SLEG_CONDVAR(_saved_scrollpos_y, SLE_INT32, SLV_6, SL_MAX_VERSION),
142 SLEG_VAR(_saved_scrollpos_zoom, SLE_UINT8),
143 SLEG_END()
144 };
145
146 static void SaveLoad_VIEW()
147 {
148 SlGlobList(_view_desc);
149 }
150
151 extern const ChunkHandler _misc_chunk_handlers[] = {
152 { 'DATE', SaveLoad_DATE, SaveLoad_DATE, nullptr, Check_DATE, CH_RIFF},
153 { 'VIEW', SaveLoad_VIEW, SaveLoad_VIEW, nullptr, nullptr, CH_RIFF | CH_LAST},
154 };
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:78
SLV_162
@ SLV_162
162 22713
Definition: saveload.h:237
ZOOM_OUT
@ ZOOM_OUT
Zoom out (get helicopter view).
Definition: viewport_type.h:82
SL_MIN_VERSION
@ SL_MIN_VERSION
First savegame version.
Definition: saveload.h:31
Viewport::width
int width
Screen width of the viewport.
Definition: viewport_type.h:25
_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:326
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:259
_load_check_data
LoadCheckData _load_check_data
Data loaded from save during SL_LOAD_CHECK.
Definition: fios_gui.cpp:38
ZOOM_NONE
@ ZOOM_NONE
Hack, used to update the button status.
Definition: viewport_type.h:83
FindWindowById
Window * FindWindowById(WindowClass cls, WindowNumber number)
Find a window by its class and window number.
Definition: window.cpp:1133
SLV_157
@ SLV_157
157 21862
Definition: saveload.h:231
CH_LAST
@ CH_LAST
Last chunk in this array.
Definition: saveload.h:410
DoZoomInOutWindow
bool DoZoomInOutWindow(ZoomStateChange how, Window *w)
Zooms a viewport in a window in or out.
Definition: main_gui.cpp:91
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:21
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:79
ZOOM_LVL_MAX
@ ZOOM_LVL_MAX
Maximum zoom level.
Definition: zoom_type.h:48
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:379
SLE_NULL
#define SLE_NULL(length)
Empty space in every savegame version.
Definition: saveload.h:669
SLEG_CONDVAR
#define SLEG_CONDVAR(variable, type, from, to)
Storage of a global variable in some savegame versions.
Definition: saveload.h:707
GUISettings::zoom_max
ZoomLevel zoom_max
maximum zoom out level
Definition: settings_type.h:119
SLE_CONDNULL
#define SLE_CONDNULL(length, from, to)
Empty space in some savegame versions.
Definition: saveload.h:677
SLV_10
@ SLV_10
10.0 2030
Definition: saveload.h:52
ZOOM_LVL_MIN
@ ZOOM_LVL_MIN
Minimum zoom level.
Definition: zoom_type.h:47
ZOOM_IN
@ ZOOM_IN
Zoom in (get more detailed view).
Definition: viewport_type.h:81
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
SLV_31
@ SLV_31
31 5999
Definition: saveload.h:80
SLV_11
@ SLV_11
11.0 2033 11.1 2041
Definition: saveload.h:53
_trees_tick_ctr
byte _trees_tick_ctr
Determines when to consider building more trees.
Definition: tree_cmd.cpp:52
_pause_mode
PauseMode _pause_mode
The current pause mode.
Definition: gfx.cpp:47
SLV_46
@ SLV_46
46 8705
Definition: saveload.h:98
SLV_109
@ SLV_109
109 15075
Definition: saveload.h:173
_tick_counter
uint16 _tick_counter
Ever incrementing (and sometimes wrapping) tick counter for setting off various events.
Definition: date.cpp:30
SLV_120
@ SLV_120
120 16439
Definition: saveload.h:187
IsSavegameVersionBefore
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:815
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:261
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
SlGlobList
void SlGlobList(const SaveLoadGlobVarList *sldg)
Save or Load (a list of) global variables.
Definition: saveload.cpp:1630
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:328
GUISettings::zoom_min
ZoomLevel zoom_min
minimum zoom out level
Definition: settings_type.h:118
SLEG_VAR
#define SLEG_VAR(variable, type)
Storage of a global variable in every savegame version.
Definition: saveload.h:761
_next_competitor_start
uint _next_competitor_start
the number of ticks before the next AI is started
Definition: company_cmd.cpp:50
ViewportData::scrollpos_x
int32 scrollpos_x
Currently shown x coordinate (virtual screen coordinate of topleft corner of the viewport).
Definition: window_gui.h:258
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:54
SLEG_END
#define SLEG_END()
End marker of global variables save or load.
Definition: saveload.h:807
SLV_6
@ SLV_6
6.0 1721 6.1 1768
Definition: saveload.h:46
_age_cargo_skip_counter
byte _age_cargo_skip_counter
Skip aging of cargo? Used before savegame version 162.
Definition: misc_sl.cpp:69
WC_MAIN_WINDOW
@ WC_MAIN_WINDOW
Main window; Window numbers:
Definition: window_type.h:44
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:260
MarkWholeScreenDirty
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition: gfx.cpp:1597
Window
Data structure for an opened window.
Definition: window_gui.h:276
Viewport::virtual_height
int virtual_height
height << zoom
Definition: viewport_type.h:31
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
SaveLoad
SaveLoad type struct.
Definition: saveload.h:516
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition: settings_type.h:577
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:80