4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file genworld.cpp Functions to generate a map. */
14 #include "landscape.h"
15 #include "company_func.h"
18 #include "window_func.h"
19 #include "network/network.h"
20 #include "heightmap.h"
21 #include "viewport_func.h"
22 #include "date_func.h"
23 #include "engine_func.h"
25 #include "video/video_driver.hpp"
26 #include "tilehighlight_func.h"
27 #include "saveload/saveload.h"
28 #include "map/ground.h"
31 #include "core/random_func.hpp"
32 #include "core/backup_type.hpp"
35 #include "game/game.hpp"
36 #include "game/game_instance.hpp"
39 void GenerateClearTile();
40 void GenerateIndustries();
41 void GenerateObjects();
44 void StartupEconomy();
45 void StartupCompanies();
46 void StartupDisasters();
48 void InitializeGame(uint size_x
, uint size_y
, bool reset_date
, bool reset_settings
);
51 * Please only use this variable in genworld.h and genworld.cpp and
52 * nowhere else. For speed improvements we need it to be global, but
53 * in no way the meaning of it is to use it anywhere else besides
54 * in the genworld.h and genworld.cpp!
58 /** Whether we are generating the map or not. */
59 bool _generating_world
;
62 * Tells if the world generation is done in a thread or not.
63 * @return the 'threaded' status
65 bool IsGenerateWorldThreaded()
67 return _gw
.threaded
&& !_gw
.quit_thread
;
71 * Clean up the 'mess' of generation. That is, show windows again, reset
72 * thread variables, and delete the progress window.
74 static void CleanupGeneration()
76 _generating_world
= false;
78 SetMouseCursorBusy(false);
79 /* Show all vital windows again, because we have hidden them */
80 if (_gw
.threaded
&& _game_mode
!= GM_MENU
) ShowVitalWindows();
81 SetModalProgress(false);
86 DeleteWindowByClass(WC_MODAL_PROGRESS
);
88 MarkWholeScreenDirty();
92 * The internal, real, generate function.
94 static void _GenerateWorld(void *)
96 /* Make sure everything is done via OWNER_NONE. */
97 Backup
<CompanyByte
> _cur_company(_current_company
, OWNER_NONE
, FILE_LINE
);
100 _generating_world
= true;
101 _modal_progress_work_mutex
->BeginCritical();
102 if (_network_dedicated
) DEBUG(net
, 1, "Generating map, please wait...");
103 /* Set the Random() seed to generation_seed so we produce the same map with the same seed */
104 if (_settings_game
.game_creation
.generation_seed
== GENERATE_NEW_SEED
) _settings_game
.game_creation
.generation_seed
= _settings_newgame
.game_creation
.generation_seed
= InteractiveRandom();
105 _random
.SetSeed(_settings_game
.game_creation
.generation_seed
);
106 SetGeneratingWorldProgress(GWP_MAP_INIT
, 2);
107 SetPointerMode (POINTER_NONE
, WC_MAIN_WINDOW
, 0, SPR_CURSOR_ZZZ
);
109 BasePersistentStorageArray::SwitchMode(PSM_ENTER_GAMELOOP
);
111 IncreaseGeneratingWorldProgress(GWP_MAP_INIT
);
112 /* Must start economy early because of the costs. */
115 /* Don't generate landscape items when in the scenario editor. */
116 if (_gw
.mode
== GWM_EMPTY
) {
117 SetGeneratingWorldProgress(GWP_OBJECT
, 1);
119 /* Make sure the tiles at the north border are void tiles if needed. */
120 if (_settings_game
.construction
.freeform_edges
) {
121 for (uint row
= 0; row
< MapSizeY(); row
++) MakeVoid(TileXY(0, row
));
122 for (uint col
= 0; col
< MapSizeX(); col
++) MakeVoid(TileXY(col
, 0));
125 /* Make the map the height of the setting */
126 if (_game_mode
!= GM_MENU
) FlatEmptyWorld(_settings_game
.game_creation
.se_flat_world_height
);
128 ConvertGroundTilesIntoWaterTiles();
129 IncreaseGeneratingWorldProgress(GWP_OBJECT
);
131 GenerateLandscape(_gw
.mode
);
134 /* only generate towns, tree and industries in newgame mode. */
135 if (_game_mode
!= GM_EDITOR
) {
136 if (!GenerateTowns(_settings_game
.economy
.town_layout
)) {
137 _cur_company
.Restore();
138 HandleGeneratingWorldAbortion();
141 GenerateIndustries();
147 /* These are probably pointless when inside the scenario editor. */
148 SetGeneratingWorldProgress(GWP_GAME_INIT
, 3);
150 IncreaseGeneratingWorldProgress(GWP_GAME_INIT
);
152 IncreaseGeneratingWorldProgress(GWP_GAME_INIT
);
154 _generating_world
= false;
156 /* No need to run the tile loop in the scenario editor. */
157 if (_gw
.mode
!= GWM_EMPTY
) {
160 SetGeneratingWorldProgress(GWP_RUNTILELOOP
, 0x500);
161 for (i
= 0; i
< 0x500; i
++) {
164 IncreaseGeneratingWorldProgress(GWP_RUNTILELOOP
);
167 if (_game_mode
!= GM_EDITOR
) {
170 if (Game::GetInstance() != NULL
) {
171 SetGeneratingWorldProgress(GWP_RUNSCRIPT
, 2500);
172 _generating_world
= true;
173 for (i
= 0; i
< 2500; i
++) {
175 IncreaseGeneratingWorldProgress(GWP_RUNSCRIPT
);
176 if (Game::GetInstance()->IsSleeping()) break;
178 _generating_world
= false;
183 BasePersistentStorageArray::SwitchMode(PSM_LEAVE_GAMELOOP
);
186 _cur_company
.Trash();
187 _current_company
= _local_company
= _gw
.lc
;
189 SetGeneratingWorldProgress(GWP_GAME_START
, 1);
190 /* Call any callback */
191 if (_gw
.proc
!= NULL
) _gw
.proc();
192 IncreaseGeneratingWorldProgress(GWP_GAME_START
);
195 _modal_progress_work_mutex
->EndCritical();
199 if (_network_dedicated
) DEBUG(net
, 1, "Map generated, starting game");
200 DEBUG(desync
, 1, "new_map: %08x", _settings_game
.game_creation
.generation_seed
);
202 if (_debug_desync_level
> 0) {
204 bstrfmt (name
, "dmp_cmds_%08x_%08x.sav", _settings_game
.game_creation
.generation_seed
, _date
);
205 SaveGame(name
, AUTOSAVE_DIR
, false);
208 BasePersistentStorageArray::SwitchMode(PSM_LEAVE_GAMELOOP
, true);
209 if (_cur_company
.IsValid()) _cur_company
.Restore();
210 _generating_world
= false;
211 _modal_progress_work_mutex
->EndCritical();
217 * Set here the function, if any, that you want to be called when landscape
218 * generation is done.
219 * @param proc callback procedure
221 void GenerateWorldSetCallback(GWDoneProc
*proc
)
227 * Set here the function, if any, that you want to be called when landscape
228 * generation is aborted.
229 * @param proc callback procedure
231 void GenerateWorldSetAbortCallback(GWAbortProc
*proc
)
237 * This will wait for the thread to finish up his work. It will not continue
238 * till the work is done.
240 void WaitTillGeneratedWorld()
242 if (_gw
.thread
== NULL
) return;
244 _modal_progress_work_mutex
->EndCritical();
245 _modal_progress_paint_mutex
->EndCritical();
246 _gw
.quit_thread
= true;
250 _gw
.threaded
= false;
251 _modal_progress_work_mutex
->BeginCritical();
252 _modal_progress_paint_mutex
->BeginCritical();
256 * Initializes the abortion process
258 void AbortGeneratingWorld()
264 * Is the generation being aborted?
265 * @return the 'aborted' status
267 bool IsGeneratingWorldAborted()
273 * Really handle the abortion, i.e. clean up some of the mess
275 void HandleGeneratingWorldAbortion()
277 /* Clean up - in SE create an empty map, otherwise, go to intro menu */
278 _switch_mode
= (_game_mode
== GM_EDITOR
) ? SM_EDITOR
: SM_MENU
;
280 if (_gw
.abortp
!= NULL
) _gw
.abortp();
284 if (_gw
.thread
!= NULL
) _gw
.thread
->Exit();
286 SwitchToMode(_switch_mode
);
291 * @param mode The mode of world generation (see GenWorldMode).
292 * @param size_x The X-size of the map.
293 * @param size_y The Y-size of the map.
294 * @param reset_settings Whether to reset the game configuration (used for restart)
296 void GenerateWorld(GenWorldMode mode
, uint size_x
, uint size_y
, bool reset_settings
)
298 if (HasModalProgress()) return;
302 SetModalProgress(true);
305 _gw
.lc
= _local_company
;
306 _gw
.quit_thread
= false;
309 /* This disables some commands and stuff */
310 SetLocalCompany(COMPANY_SPECTATOR
);
312 InitializeGame(_gw
.size_x
, _gw
.size_y
, true, reset_settings
);
313 PrepareGenerateWorldProgress();
315 /* Load the right landscape stuff, and the NewGRFs! */
317 LoadStringWidthTable();
319 /* Re-init the windowing system */
322 /* Create toolbars */
323 SetupColoursAndInitialWindow();
324 SetPointerMode (POINTER_NONE
, WC_MAIN_WINDOW
, 0, SPR_CURSOR_ZZZ
);
326 if (_gw
.thread
!= NULL
) {
332 if (!VideoDriver::GetActiveDriver()->HasGUI() || !ThreadObject::New(&_GenerateWorld
, NULL
, &_gw
.thread
, "ottd:genworld")) {
333 DEBUG(misc
, 1, "Cannot create genworld thread, reverting to single-threaded mode");
334 _gw
.threaded
= false;
335 _modal_progress_work_mutex
->EndCritical();
336 _GenerateWorld(NULL
);
337 _modal_progress_work_mutex
->BeginCritical();
341 UnshowCriticalError();
342 /* Remove any open window */
343 DeleteAllNonVitalWindows();
344 /* Hide vital windows, because we don't allow to use them */
347 /* Don't show the dialog if we don't have a thread */
348 ShowGenerateWorldProgress();
350 /* Centre the view on the map */
351 if (FindWindowById(WC_MAIN_WINDOW
, 0) != NULL
) {
352 ScrollMainWindowToTile(TileXY(MapSizeX() / 2, MapSizeY() / 2), true);