OpenTTD
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 {
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 = (ZoomLevel)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. */
60  /* Ensure zoom level is allowed */
63  }
64 
65  DoZoomInOutWindow(ZOOM_NONE, w); // update button status
67 }
68 
70 
71 static const SaveLoadGlobVarList _date_desc[] = {
72  SLEG_CONDVAR(_date, SLE_FILE_U16 | SLE_VAR_I32, SL_MIN_VERSION, SLV_31),
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),
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),
83  SLEG_VAR(_random.state[0], SLE_UINT32),
84  SLEG_VAR(_random.state[1], SLE_UINT32),
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),
90  SLEG_VAR(_trees_tick_ctr, SLE_UINT8),
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
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
108  SLE_NULL(4), // _random.state[0]
109  SLE_NULL(4), // _random.state[1]
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
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);
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 };
#define SLE_CONDNULL(length, from, to)
Empty space in some savegame versions.
Definition: saveload.h:642
bool DoZoomInOutWindow(ZoomStateChange how, Window *w)
Zooms a viewport in a window in or out.
Definition: main_gui.cpp:136
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:763
157 21862
Definition: saveload.h:230
#define SLEG_END()
End marker of global variables save or load.
Definition: saveload.h:755
#define DAYS_TILL_ORIGINAL_BASE_YEAR
The offset in days from the &#39;_date == 0&#39; till &#39;ConvertYMDToDate(ORIGINAL_BASE_YEAR, 0, 1)&#39;.
Definition: date_type.h:80
Zoom out (get helicopter view).
Definition: viewport_type.h:82
int height
Screen height of the viewport.
Definition: viewport_type.h:26
#define SLE_NULL(length)
Empty space in every savegame version.
Definition: saveload.h:634
Window * FindWindowById(WindowClass cls, WindowNumber number)
Find a window by its class and window number.
Definition: window.cpp:1130
120 16439
Definition: saveload.h:186
int virtual_height
height << zoom
Definition: viewport_type.h:31
Minimum zoom level.
Definition: zoom_type.h:45
byte _trees_tick_ctr
Determines when to consider building more trees.
Definition: tree_cmd.cpp:51
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
Zoom in (get more detailed view).
Definition: viewport_type.h:81
#define SLEG_VAR(variable, type)
Storage of a global variable in every savegame version.
Definition: saveload.h:716
11.0 2033 11.1 2041
Definition: saveload.h:52
Maximum zoom level.
Definition: zoom_type.h:46
162 22713
Definition: saveload.h:236
uint _cur_company_tick_index
used to generate a name for one company that doesn&#39;t have a name yet per tick
Definition: company_cmd.cpp:49
Data structure for an opened window.
Definition: window_gui.h:276
Main window; Window numbers:
Definition: window_type.h:44
LoadCheckData _load_check_data
Data loaded from save during SL_LOAD_CHECK.
Definition: fios_gui.cpp:38
Functions/types related to saving and loading games.
DateFract _date_fract
Fractional part of the day.
Definition: date.cpp:27
Highest possible saveload version.
Definition: saveload.h:305
First savegame version.
Definition: saveload.h:30
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:78
int32 scrollpos_x
Currently shown x coordinate (virtual screen coordinate of topleft corner of the viewport).
Definition: window_gui.h:258
ZoomLevel zoom_max
maximum zoom out level
int virtual_width
width << zoom
Definition: viewport_type.h:30
109 15075
Definition: saveload.h:172
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:40
10.0 2030
Definition: saveload.h:51
PauseMode _pause_mode
The current pause mode.
Definition: gfx.cpp:47
6.0 1721 6.1 1768
Definition: saveload.h:45
int32 dest_scrollpos_y
Current destination y coordinate to display (virtual screen coordinate of topleft corner of the viewp...
Definition: window_gui.h:261
Handlers and description of chunk.
Definition: saveload.h:356
#define SLEG_CONDVAR(variable, type, from, to)
Storage of a global variable in some savegame versions.
Definition: saveload.h:671
uint16 _tick_counter
Ever incrementing (and sometimes wrapping) tick counter for setting off various events.
Definition: date.cpp:28
int32 dest_scrollpos_x
Current destination x coordinate to display (virtual screen coordinate of topleft corner of the viewp...
Definition: window_gui.h:260
GUISettings gui
settings related to the GUI
Data structure for viewport, display of a part of the world.
Definition: viewport_type.h:22
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:78
Randomizer _random
Random used in the game state calculations.
Definition: random_func.cpp:25
uint _next_competitor_start
the number of ticks before the next AI is started
Definition: company_cmd.cpp:48
uint16 _disaster_delay
Delay counter for considering the next disaster.
void SlGlobList(const SaveLoadGlobVarList *sldg)
Save or Load (a list of) global variables.
Definition: saveload.cpp:1564
SaveLoad type struct.
Definition: saveload.h:496
ZoomLevel zoom
The zoom level of the viewport.
Definition: viewport_type.h:33
ZoomLevel
All zoom levels we know.
Definition: zoom_type.h:19
uint32 state[2]
The state of the randomizer.
Definition: random_func.hpp:23
ZoomLevel zoom_min
minimum zoom out level
ViewportData * viewport
Pointer to viewport data, if present.
Definition: window_gui.h:326
31 5999
Definition: saveload.h:79
Date _date
Current date in days (day counter)
Definition: date.cpp:26
46 8705
Definition: saveload.h:97
int32 scrollpos_y
Currently shown y coordinate (virtual screen coordinate of topleft corner of the viewport).
Definition: window_gui.h:259
Hack, used to update the button status.
Definition: viewport_type.h:83
byte _age_cargo_skip_counter
Skip aging of cargo? Used before savegame version 162.
Definition: misc_sl.cpp:69
Last chunk in this array.
Definition: saveload.h:391
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:36
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition: gfx.cpp:1462
int width
Screen width of the viewport.
Definition: viewport_type.h:25