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/>.
8 /** @file genworld.cpp Functions to generate a map. */
11 #include "landscape.h"
12 #include "company_func.h"
15 #include "window_func.h"
16 #include "network/network.h"
17 #include "heightmap.h"
18 #include "viewport_func.h"
19 #include "date_func.h"
20 #include "engine_func.h"
22 #include "video/video_driver.hpp"
23 #include "tilehighlight_func.h"
24 #include "saveload/saveload.h"
28 #include "core/random_func.hpp"
29 #include "core/backup_type.hpp"
32 #include "game/game.hpp"
33 #include "game/game_instance.hpp"
34 #include "string_func.h"
38 #include "safeguards.h"
41 void GenerateClearTile();
42 void GenerateIndustries();
43 void GenerateObjects();
46 void StartupEconomy();
47 void StartupCompanies();
48 void StartupDisasters();
50 void InitializeGame(uint size_x
, uint size_y
, bool reset_date
, bool reset_settings
);
53 * Please only use this variable in genworld.h and genworld.cpp and
54 * nowhere else. For speed improvements we need it to be global, but
55 * in no way the meaning of it is to use it anywhere else besides
56 * in the genworld.h and genworld.cpp!
60 /** Whether we are generating the map or not. */
61 bool _generating_world
;
63 class AbortGenerateWorldSignal
{ };
66 * Generation is done; show windows again and delete the progress window.
68 static void CleanupGeneration()
70 _generating_world
= false;
72 SetMouseCursorBusy(false);
73 /* Show all vital windows again, because we have hidden them */
74 if (_game_mode
!= GM_MENU
) ShowVitalWindows();
75 SetModalProgress(false);
79 CloseWindowByClass(WC_MODAL_PROGRESS
);
81 MarkWholeScreenDirty();
85 * The internal, real, generate function.
87 static void _GenerateWorld()
89 /* Make sure everything is done via OWNER_NONE. */
90 Backup
<CompanyID
> _cur_company(_current_company
, OWNER_NONE
, FILE_LINE
);
93 _generating_world
= true;
94 if (_network_dedicated
) Debug(net
, 3, "Generating map, please wait...");
95 /* Set the Random() seed to generation_seed so we produce the same map with the same seed */
96 if (_settings_game
.game_creation
.generation_seed
== GENERATE_NEW_SEED
) _settings_game
.game_creation
.generation_seed
= _settings_newgame
.game_creation
.generation_seed
= InteractiveRandom();
97 _random
.SetSeed(_settings_game
.game_creation
.generation_seed
);
98 SetGeneratingWorldProgress(GWP_MAP_INIT
, 2);
99 SetObjectToPlace(SPR_CURSOR_ZZZ
, PAL_NONE
, HT_NONE
, WC_MAIN_WINDOW
, 0);
101 BasePersistentStorageArray::SwitchMode(PSM_ENTER_GAMELOOP
);
103 IncreaseGeneratingWorldProgress(GWP_MAP_INIT
);
104 /* Must start economy early because of the costs. */
107 /* Don't generate landscape items when in the scenario editor. */
108 if (_gw
.mode
== GWM_EMPTY
) {
109 SetGeneratingWorldProgress(GWP_OBJECT
, 1);
111 /* Make sure the tiles at the north border are void tiles if needed. */
112 if (_settings_game
.construction
.freeform_edges
) {
113 for (uint x
= 0; x
< MapSizeX(); x
++) MakeVoid(TileXY(x
, 0));
114 for (uint y
= 0; y
< MapSizeY(); y
++) MakeVoid(TileXY(0, y
));
117 /* Make the map the height of the setting */
118 if (_game_mode
!= GM_MENU
) FlatEmptyWorld(_settings_game
.game_creation
.se_flat_world_height
);
120 ConvertGroundTilesIntoWaterTiles();
121 IncreaseGeneratingWorldProgress(GWP_OBJECT
);
123 _settings_game
.game_creation
.snow_line_height
= DEF_SNOWLINE_HEIGHT
;
125 GenerateLandscape(_gw
.mode
);
128 /* Only generate towns, tree and industries in newgame mode. */
129 if (_game_mode
!= GM_EDITOR
) {
130 if (!GenerateTowns(_settings_game
.economy
.town_layout
)) {
131 HandleGeneratingWorldAbortion();
134 GenerateIndustries();
140 /* These are probably pointless when inside the scenario editor. */
141 SetGeneratingWorldProgress(GWP_GAME_INIT
, 3);
143 IncreaseGeneratingWorldProgress(GWP_GAME_INIT
);
145 IncreaseGeneratingWorldProgress(GWP_GAME_INIT
);
147 _generating_world
= false;
149 /* No need to run the tile loop in the scenario editor. */
150 if (_gw
.mode
!= GWM_EMPTY
) {
153 SetGeneratingWorldProgress(GWP_RUNTILELOOP
, 0x500);
154 for (i
= 0; i
< 0x500; i
++) {
157 IncreaseGeneratingWorldProgress(GWP_RUNTILELOOP
);
160 if (_game_mode
!= GM_EDITOR
) {
163 if (Game::GetInstance() != nullptr) {
164 SetGeneratingWorldProgress(GWP_RUNSCRIPT
, 2500);
165 _generating_world
= true;
166 for (i
= 0; i
< 2500; i
++) {
168 IncreaseGeneratingWorldProgress(GWP_RUNSCRIPT
);
169 if (Game::GetInstance()->IsSleeping()) break;
171 _generating_world
= false;
176 BasePersistentStorageArray::SwitchMode(PSM_LEAVE_GAMELOOP
);
178 ResetObjectToPlace();
179 _cur_company
.Trash();
180 _current_company
= _local_company
= _gw
.lc
;
182 SetGeneratingWorldProgress(GWP_GAME_START
, 1);
183 /* Call any callback */
184 if (_gw
.proc
!= nullptr) _gw
.proc();
185 IncreaseGeneratingWorldProgress(GWP_GAME_START
);
191 if (_network_dedicated
) Debug(net
, 3, "Map generated, starting game");
192 Debug(desync
, 1, "new_map: {:08x}", _settings_game
.game_creation
.generation_seed
);
194 if (_debug_desync_level
> 0) {
196 seprintf(name
, lastof(name
), "dmp_cmds_%08x_%08x.sav", _settings_game
.game_creation
.generation_seed
, _date
);
197 SaveOrLoad(name
, SLO_SAVE
, DFT_GAME_FILE
, AUTOSAVE_DIR
, false);
199 } catch (AbortGenerateWorldSignal
&) {
202 BasePersistentStorageArray::SwitchMode(PSM_LEAVE_GAMELOOP
, true);
203 if (_cur_company
.IsValid()) _cur_company
.Restore();
205 if (_network_dedicated
) {
206 /* Exit the game to prevent a return to main menu. */
207 Debug(net
, 0, "Generating map failed; closing server");
210 SwitchToMode(_switch_mode
);
216 * Set here the function, if any, that you want to be called when landscape
217 * generation is done.
218 * @param proc callback procedure
220 void GenerateWorldSetCallback(GWDoneProc
*proc
)
226 * Set here the function, if any, that you want to be called when landscape
227 * generation is aborted.
228 * @param proc callback procedure
230 void GenerateWorldSetAbortCallback(GWAbortProc
*proc
)
236 * Initializes the abortion process
238 void AbortGeneratingWorld()
244 * Is the generation being aborted?
245 * @return the 'aborted' status
247 bool IsGeneratingWorldAborted()
249 return _gw
.abort
|| _exit_game
;
253 * Really handle the abortion, i.e. clean up some of the mess
255 void HandleGeneratingWorldAbortion()
257 /* Clean up - in SE create an empty map, otherwise, go to intro menu */
258 _switch_mode
= (_game_mode
== GM_EDITOR
) ? SM_EDITOR
: SM_MENU
;
260 if (_gw
.abortp
!= nullptr) _gw
.abortp();
262 throw AbortGenerateWorldSignal();
267 * @param mode The mode of world generation (see GenWorldMode).
268 * @param size_x The X-size of the map.
269 * @param size_y The Y-size of the map.
270 * @param reset_settings Whether to reset the game configuration (used for restart)
272 void GenerateWorld(GenWorldMode mode
, uint size_x
, uint size_y
, bool reset_settings
)
274 if (HasModalProgress()) return;
278 SetModalProgress(true);
280 _gw
.abortp
= nullptr;
281 _gw
.lc
= _local_company
;
283 /* This disables some commands and stuff */
284 SetLocalCompany(COMPANY_SPECTATOR
);
286 InitializeGame(_gw
.size_x
, _gw
.size_y
, true, reset_settings
);
287 PrepareGenerateWorldProgress();
289 if (_settings_game
.construction
.map_height_limit
== 0) {
290 uint estimated_height
= 0;
292 if (_gw
.mode
== GWM_EMPTY
&& _game_mode
!= GM_MENU
) {
293 estimated_height
= _settings_game
.game_creation
.se_flat_world_height
;
294 } else if (_gw
.mode
== GWM_HEIGHTMAP
) {
295 estimated_height
= _settings_game
.game_creation
.heightmap_height
;
296 } else if (_settings_game
.game_creation
.land_generator
== LG_TERRAGENESIS
) {
297 estimated_height
= GetEstimationTGPMapHeight();
299 estimated_height
= 0;
302 _settings_game
.construction
.map_height_limit
= std::max(MAP_HEIGHT_LIMIT_AUTO_MINIMUM
, std::min(MAX_MAP_HEIGHT_LIMIT
, estimated_height
+ MAP_HEIGHT_LIMIT_AUTO_CEILING_ROOM
));
305 /* Load the right landscape stuff, and the NewGRFs! */
307 LoadStringWidthTable();
309 /* Re-init the windowing system */
312 /* Create toolbars */
313 SetupColoursAndInitialWindow();
314 SetObjectToPlace(SPR_CURSOR_ZZZ
, PAL_NONE
, HT_NONE
, WC_MAIN_WINDOW
, 0);
316 UnshowCriticalError();
317 CloseAllNonVitalWindows();
320 ShowGenerateWorldProgress();
322 /* Centre the view on the map */
323 if (FindWindowById(WC_MAIN_WINDOW
, 0) != nullptr) {
324 ScrollMainWindowToTile(TileXY(MapSizeX() / 2, MapSizeY() / 2), true);