misc_sl.cpp

Go to the documentation of this file.
00001 /* $Id: misc_sl.cpp 23740 2012-01-03 21:32:51Z rubidium $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #include "../stdafx.h"
00013 #include "../date_func.h"
00014 #include "../zoom_func.h"
00015 #include "../window_gui.h"
00016 #include "../window_func.h"
00017 #include "../viewport_func.h"
00018 #include "../gfx_func.h"
00019 #include "../core/random_func.hpp"
00020 #include "../fios.h"
00021 
00022 #include "saveload.h"
00023 
00024 extern TileIndex _cur_tileloop_tile;
00025 extern uint16 _disaster_delay;
00026 extern byte _trees_tick_ctr;
00027 
00028 /* Keep track of current game position */
00029 int _saved_scrollpos_x;
00030 int _saved_scrollpos_y;
00031 ZoomLevelByte _saved_scrollpos_zoom;
00032 
00033 void SaveViewportBeforeSaveGame()
00034 {
00035   const Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
00036 
00037   if (w != NULL) {
00038     _saved_scrollpos_x = w->viewport->scrollpos_x;
00039     _saved_scrollpos_y = w->viewport->scrollpos_y;
00040     _saved_scrollpos_zoom = w->viewport->zoom;
00041   }
00042 }
00043 
00044 void ResetViewportAfterLoadGame()
00045 {
00046   Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
00047 
00048   w->viewport->scrollpos_x = _saved_scrollpos_x;
00049   w->viewport->scrollpos_y = _saved_scrollpos_y;
00050   w->viewport->dest_scrollpos_x = _saved_scrollpos_x;
00051   w->viewport->dest_scrollpos_y = _saved_scrollpos_y;
00052 
00053   ViewPort *vp = w->viewport;
00054   vp->zoom = (ZoomLevel)min(_saved_scrollpos_zoom, ZOOM_LVL_MAX);
00055   vp->virtual_width = ScaleByZoom(vp->width, vp->zoom);
00056   vp->virtual_height = ScaleByZoom(vp->height, vp->zoom);
00057 
00058   /* If zoom_max is ZOOM_LVL_MIN then the setting has not been loaded yet, therefore all levels are allowed. */
00059   if (_settings_client.gui.zoom_max != ZOOM_LVL_MIN) {
00060     /* Ensure zoom level is allowed */
00061     while (vp->zoom < _settings_client.gui.zoom_min) DoZoomInOutWindow(ZOOM_OUT, w);
00062     while (vp->zoom > _settings_client.gui.zoom_max) DoZoomInOutWindow(ZOOM_IN, w);
00063   }
00064 
00065   DoZoomInOutWindow(ZOOM_NONE, w); // update button status
00066   MarkWholeScreenDirty();
00067 }
00068 
00069 byte _age_cargo_skip_counter; 
00070 
00071 static const SaveLoadGlobVarList _date_desc[] = {
00072   SLEG_CONDVAR(_date,                   SLE_FILE_U16 | SLE_VAR_I32,  0,  30),
00073   SLEG_CONDVAR(_date,                   SLE_INT32,                  31, SL_MAX_VERSION),
00074       SLEG_VAR(_date_fract,             SLE_UINT16),
00075       SLEG_VAR(_tick_counter,           SLE_UINT16),
00076   SLE_CONDNULL(2, 0, 156), // _vehicle_id_ctr_day
00077   SLEG_CONDVAR(_age_cargo_skip_counter, SLE_UINT8,                   0, 161),
00078   SLE_CONDNULL(1, 0, 45),
00079   SLEG_CONDVAR(_cur_tileloop_tile,      SLE_FILE_U16 | SLE_VAR_U32,  0, 5),
00080   SLEG_CONDVAR(_cur_tileloop_tile,      SLE_UINT32,                  6, SL_MAX_VERSION),
00081       SLEG_VAR(_disaster_delay,         SLE_UINT16),
00082   SLE_CONDNULL(2, 0, 119),
00083       SLEG_VAR(_random.state[0],        SLE_UINT32),
00084       SLEG_VAR(_random.state[1],        SLE_UINT32),
00085   SLE_CONDNULL(1,  0,   9),
00086   SLE_CONDNULL(4, 10, 119),
00087       SLEG_VAR(_cur_company_tick_index, SLE_FILE_U8  | SLE_VAR_U32),
00088   SLEG_CONDVAR(_next_competitor_start,  SLE_FILE_U16 | SLE_VAR_U32,  0, 108),
00089   SLEG_CONDVAR(_next_competitor_start,  SLE_UINT32,                109, SL_MAX_VERSION),
00090       SLEG_VAR(_trees_tick_ctr,         SLE_UINT8),
00091   SLEG_CONDVAR(_pause_mode,             SLE_UINT8,                   4, SL_MAX_VERSION),
00092   SLE_CONDNULL(4, 11, 119),
00093       SLEG_END()
00094 };
00095 
00096 static const SaveLoadGlobVarList _date_check_desc[] = {
00097   SLEG_CONDVAR(_load_check_data.current_date,  SLE_FILE_U16 | SLE_VAR_I32,  0,  30),
00098   SLEG_CONDVAR(_load_check_data.current_date,  SLE_INT32,                  31, SL_MAX_VERSION),
00099       SLE_NULL(2),                       // _date_fract
00100       SLE_NULL(2),                       // _tick_counter
00101   SLE_CONDNULL(2, 0, 156),               // _vehicle_id_ctr_day
00102   SLE_CONDNULL(1, 0, 161),               // _age_cargo_skip_counter
00103   SLE_CONDNULL(1, 0, 45),
00104   SLE_CONDNULL(2, 0, 5),                 // _cur_tileloop_tile
00105   SLE_CONDNULL(4, 6, SL_MAX_VERSION),    // _cur_tileloop_tile
00106       SLE_NULL(2),                       // _disaster_delay
00107   SLE_CONDNULL(2, 0, 119),
00108       SLE_NULL(4),                       // _random.state[0]
00109       SLE_NULL(4),                       // _random.state[1]
00110   SLE_CONDNULL(1,  0,   9),
00111   SLE_CONDNULL(4, 10, 119),
00112       SLE_NULL(1),                       // _cur_company_tick_index
00113   SLE_CONDNULL(2, 0, 108),               // _next_competitor_start
00114   SLE_CONDNULL(4, 109, SL_MAX_VERSION),  // _next_competitor_start
00115       SLE_NULL(1),                       // _trees_tick_ctr
00116   SLE_CONDNULL(1, 4, SL_MAX_VERSION),    // _pause_mode
00117   SLE_CONDNULL(4, 11, 119),
00118       SLEG_END()
00119 };
00120 
00121 /* Save load date related variables as well as persistent tick counters
00122  * XXX: currently some unrelated stuff is just put here */
00123 static void SaveLoad_DATE()
00124 {
00125   SlGlobList(_date_desc);
00126 }
00127 
00128 static void Check_DATE()
00129 {
00130   SlGlobList(_date_check_desc);
00131   if (IsSavegameVersionBefore(31)) {
00132     _load_check_data.current_date += DAYS_TILL_ORIGINAL_BASE_YEAR;
00133   }
00134 }
00135 
00136 
00137 static const SaveLoadGlobVarList _view_desc[] = {
00138   SLEG_CONDVAR(_saved_scrollpos_x,    SLE_FILE_I16 | SLE_VAR_I32, 0, 5),
00139   SLEG_CONDVAR(_saved_scrollpos_x,    SLE_INT32,                  6, SL_MAX_VERSION),
00140   SLEG_CONDVAR(_saved_scrollpos_y,    SLE_FILE_I16 | SLE_VAR_I32, 0, 5),
00141   SLEG_CONDVAR(_saved_scrollpos_y,    SLE_INT32,                  6, SL_MAX_VERSION),
00142       SLEG_VAR(_saved_scrollpos_zoom, SLE_UINT8),
00143       SLEG_END()
00144 };
00145 
00146 static void SaveLoad_VIEW()
00147 {
00148   SlGlobList(_view_desc);
00149 }
00150 
00151 extern const ChunkHandler _misc_chunk_handlers[] = {
00152   { 'DATE', SaveLoad_DATE, SaveLoad_DATE, NULL, Check_DATE, CH_RIFF},
00153   { 'VIEW', SaveLoad_VIEW, SaveLoad_VIEW, NULL, NULL,       CH_RIFF | CH_LAST},
00154 };