OpenTTD Source  12.0-beta2
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 {
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. */
62  /* Ensure zoom level is allowed */
65  }
66 
67  DoZoomInOutWindow(ZOOM_NONE, w); // update button status
69 }
70 
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_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),
89 };
90 
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),
94 };
95 
96 /* Save load date related variables as well as persistent tick counters
97  * XXX: currently some unrelated stuff is just put here */
99  DATEChunkHandler() : ChunkHandler('DATE', CH_TABLE) {}
100 
101  void Save() const override
102  {
103  SlTableHeader(_date_desc);
104 
105  SlSetArrayIndex(0);
106  SlGlobList(_date_desc);
107  }
108 
109  void LoadCommon(const SaveLoadTable &slt, const SaveLoadCompatTable &slct) const
110  {
111  const std::vector<SaveLoad> oslt = SlCompatTableHeader(slt, slct);
112 
114  SlGlobList(oslt);
115  if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() != -1) SlErrorCorrupt("Too many DATE entries");
116  }
117 
118  void Load() const override
119  {
120  this->LoadCommon(_date_desc, _date_sl_compat);
121  }
122 
123 
124  void LoadCheck(size_t) const override
125  {
126  this->LoadCommon(_date_check_desc, _date_check_sl_compat);
127 
130  }
131  }
132 };
133 
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),
140 };
141 
143  VIEWChunkHandler() : ChunkHandler('VIEW', CH_TABLE) {}
144 
145  void Save() const override
146  {
147  SlTableHeader(_view_desc);
148 
149  SlSetArrayIndex(0);
150  SlGlobList(_view_desc);
151  }
152 
153  void Load() const override
154  {
155  const std::vector<SaveLoad> slt = SlCompatTableHeader(_view_desc, _view_sl_compat);
156 
158  SlGlobList(slt);
159  if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() != -1) SlErrorCorrupt("Too many DATE entries");
160  }
161 };
162 
163 static const DATEChunkHandler DATE;
164 static const VIEWChunkHandler VIEW;
165 static const ChunkHandlerRef misc_chunk_handlers[] = {
166  DATE,
167  VIEW,
168 };
169 
170 extern const ChunkHandlerTable _misc_chunk_handlers(misc_chunk_handlers);
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:83
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:82
SL_MIN_VERSION
@ SL_MIN_VERSION
First savegame version.
Definition: saveload.h:35
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:443
_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:321
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:261
_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:1146
DATEChunkHandler::Load
void Load() const override
Load the chunk.
Definition: misc_sl.cpp:118
_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: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:52
ZOOM_LVL_MAX
@ ZOOM_LVL_MAX
Maximum zoom level.
Definition: zoom_type.h:48
DATEChunkHandler::Save
void Save() const override
Save the chunk.
Definition: misc_sl.cpp:101
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:406
GUISettings::zoom_max
ZoomLevel zoom_max
maximum zoom out level
Definition: settings_type.h:130
SlGlobList
void SlGlobList(const SaveLoadTable &slt)
Save or Load (a list of) global variables.
Definition: saveload.cpp:2083
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
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:937
_trees_tick_ctr
byte _trees_tick_ctr
Determines when to consider building more trees.
Definition: tree_cmd.cpp:53
_pause_mode
PauseMode _pause_mode
The current pause mode.
Definition: gfx.cpp:47
SLV_109
@ SLV_109
109 15075
Definition: saveload.h:177
_tick_counter
uint16 _tick_counter
Ever incrementing (and sometimes wrapping) tick counter for setting off various events.
Definition: date.cpp:30
IsSavegameVersionBefore
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:1024
_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:263
VIEWChunkHandler
Definition: misc_sl.cpp:142
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:342
GUISettings::zoom_min
ZoomLevel zoom_min
minimum zoom out level
Definition: settings_type.h:129
VIEWChunkHandler::Load
void Load() const override
Load the chunk.
Definition: misc_sl.cpp:153
_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:124
ViewportData::scrollpos_x
int32 scrollpos_x
Currently shown x coordinate (virtual screen coordinate of topleft corner of the viewport).
Definition: window_gui.h:260
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_CONDVAR
#define SLEG_CONDVAR(name, variable, type, from, to)
Storage of a global variable in some savegame versions.
Definition: saveload.h:849
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:364
_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:43
DATEChunkHandler
Definition: misc_sl.cpp:98
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:262
MarkWholeScreenDirty
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition: gfx.cpp:1689
SlCompatTableHeader
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition: saveload.cpp:2029
Window
Data structure for an opened window.
Definition: window_gui.h:279
VIEWChunkHandler::Save
void Save() const override
Save the chunk.
Definition: misc_sl.cpp:145
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:1891
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:653
misc_sl_compat.h
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:670
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition: settings_type.h:593
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