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. */
13 #include "landscape.h"
14 #include "company_func.h"
17 #include "window_func.h"
18 #include "network/network.h"
19 #include "heightmap.h"
20 #include "viewport_func.h"
21 #include "date_func.h"
22 #include "engine_func.h"
24 #include "video/video_driver.hpp"
25 #include "tilehighlight_func.h"
26 #include "saveload/saveload.h"
27 #include "map/ground.h"
30 #include "core/random_func.hpp"
31 #include "core/backup_type.hpp"
34 #include "game/game.hpp"
35 #include "game/game_instance.hpp"
38 void GenerateClearTile();
39 void GenerateIndustries();
40 void GenerateObjects();
43 void StartupEconomy();
44 void StartupCompanies();
45 void StartupDisasters();
47 void InitializeGame(uint size_x
, uint size_y
, bool reset_date
, bool reset_settings
);
50 * Please only use this variable in genworld.h and genworld.cpp and
51 * nowhere else. For speed improvements we need it to be global, but
52 * in no way the meaning of it is to use it anywhere else besides
53 * in the genworld.h and genworld.cpp!
57 /** Whether we are generating the map or not. */
58 bool _generating_world
;
61 * Tells if the world generation is done in a thread or not.
62 * @return the 'threaded' status
64 bool IsGenerateWorldThreaded()
66 return _gw
.threaded
&& !_gw
.quit_thread
;
70 * Clean up the 'mess' of generation. That is, show windows again, reset
71 * thread variables, and delete the progress window.
73 static void CleanupGeneration()
75 _generating_world
= false;
77 if (_cursor
.sprite
== SPR_CURSOR_ZZZ
) SetMouseCursor(SPR_CURSOR_MOUSE
, PAL_NONE
);
78 /* Show all vital windows again, because we have hidden them */
79 if (_gw
.threaded
&& _game_mode
!= GM_MENU
) ShowVitalWindows();
80 SetModalProgress(false);
85 DeleteWindowByClass(WC_MODAL_PROGRESS
);
87 MarkWholeScreenDirty();
91 * The internal, real, generate function.
93 static void _GenerateWorld(void *)
95 /* Make sure everything is done via OWNER_NONE. */
96 Backup
<CompanyByte
> _cur_company(_current_company
, OWNER_NONE
, FILE_LINE
);
99 _generating_world
= true;
100 _modal_progress_work_mutex
->BeginCritical();
101 if (_network_dedicated
) DEBUG(net
, 0, "Generating map, please wait...");
102 /* Set the Random() seed to generation_seed so we produce the same map with the same seed */
103 if (_settings_game
.game_creation
.generation_seed
== GENERATE_NEW_SEED
) _settings_game
.game_creation
.generation_seed
= _settings_newgame
.game_creation
.generation_seed
= InteractiveRandom();
104 _random
.SetSeed(_settings_game
.game_creation
.generation_seed
);
105 SetGeneratingWorldProgress(GWP_MAP_INIT
, 2);
106 SetObjectToPlace(SPR_CURSOR_ZZZ
, PAL_NONE
, HT_NONE
, WC_MAIN_WINDOW
, 0);
108 IncreaseGeneratingWorldProgress(GWP_MAP_INIT
);
109 /* Must start economy early because of the costs. */
112 /* Don't generate landscape items when in the scenario editor. */
113 if (_gw
.mode
== GWM_EMPTY
) {
114 SetGeneratingWorldProgress(GWP_OBJECT
, 1);
116 /* Make sure the tiles at the north border are void tiles if needed. */
117 if (_settings_game
.construction
.freeform_edges
) {
118 for (uint row
= 0; row
< MapSizeY(); row
++) MakeVoid(TileXY(0, row
));
119 for (uint col
= 0; col
< MapSizeX(); col
++) MakeVoid(TileXY(col
, 0));
122 /* Make the map the height of the setting */
123 if (_game_mode
!= GM_MENU
) FlatEmptyWorld(_settings_game
.game_creation
.se_flat_world_height
);
125 ConvertGroundTilesIntoWaterTiles();
126 IncreaseGeneratingWorldProgress(GWP_OBJECT
);
128 GenerateLandscape(_gw
.mode
);
131 /* only generate towns, tree and industries in newgame mode. */
132 if (_game_mode
!= GM_EDITOR
) {
133 if (!GenerateTowns(_settings_game
.economy
.town_layout
)) {
134 _cur_company
.Restore();
135 HandleGeneratingWorldAbortion();
138 GenerateIndustries();
144 ClearPersistentStorageChanges(true);
146 /* These are probably pointless when inside the scenario editor. */
147 SetGeneratingWorldProgress(GWP_GAME_INIT
, 3);
149 IncreaseGeneratingWorldProgress(GWP_GAME_INIT
);
151 IncreaseGeneratingWorldProgress(GWP_GAME_INIT
);
153 _generating_world
= false;
155 /* No need to run the tile loop in the scenario editor. */
156 if (_gw
.mode
!= GWM_EMPTY
) {
159 SetGeneratingWorldProgress(GWP_RUNTILELOOP
, 0x500);
160 for (i
= 0; i
< 0x500; i
++) {
163 IncreaseGeneratingWorldProgress(GWP_RUNTILELOOP
);
166 if (_game_mode
!= GM_EDITOR
) {
169 if (Game::GetInstance() != NULL
) {
170 SetGeneratingWorldProgress(GWP_RUNSCRIPT
, 2500);
171 _generating_world
= true;
172 for (i
= 0; i
< 2500; i
++) {
174 IncreaseGeneratingWorldProgress(GWP_RUNSCRIPT
);
175 if (Game::GetInstance()->IsSleeping()) break;
177 _generating_world
= false;
182 ResetObjectToPlace();
183 _cur_company
.Trash();
184 _current_company
= _local_company
= _gw
.lc
;
186 SetGeneratingWorldProgress(GWP_GAME_START
, 1);
187 /* Call any callback */
188 if (_gw
.proc
!= NULL
) _gw
.proc();
189 IncreaseGeneratingWorldProgress(GWP_GAME_START
);
192 _modal_progress_work_mutex
->EndCritical();
196 if (_network_dedicated
) DEBUG(net
, 0, "Map generated, starting game");
197 DEBUG(desync
, 1, "new_map: %08x", _settings_game
.game_creation
.generation_seed
);
199 if (_debug_desync_level
> 0) {
201 snprintf(name
, lengthof(name
), "dmp_cmds_%08x_%08x.sav", _settings_game
.game_creation
.generation_seed
, _date
);
202 SaveGame(name
, AUTOSAVE_DIR
, false);
205 if (_cur_company
.IsValid()) _cur_company
.Restore();
206 _generating_world
= false;
207 _modal_progress_work_mutex
->EndCritical();
213 * Set here the function, if any, that you want to be called when landscape
214 * generation is done.
215 * @param proc callback procedure
217 void GenerateWorldSetCallback(GWDoneProc
*proc
)
223 * Set here the function, if any, that you want to be called when landscape
224 * generation is aborted.
225 * @param proc callback procedure
227 void GenerateWorldSetAbortCallback(GWAbortProc
*proc
)
233 * This will wait for the thread to finish up his work. It will not continue
234 * till the work is done.
236 void WaitTillGeneratedWorld()
238 if (_gw
.thread
== NULL
) return;
240 _modal_progress_work_mutex
->EndCritical();
241 _modal_progress_paint_mutex
->EndCritical();
242 _gw
.quit_thread
= true;
246 _gw
.threaded
= false;
247 _modal_progress_work_mutex
->BeginCritical();
248 _modal_progress_paint_mutex
->BeginCritical();
252 * Initializes the abortion process
254 void AbortGeneratingWorld()
260 * Is the generation being aborted?
261 * @return the 'aborted' status
263 bool IsGeneratingWorldAborted()
269 * Really handle the abortion, i.e. clean up some of the mess
271 void HandleGeneratingWorldAbortion()
273 /* Clean up - in SE create an empty map, otherwise, go to intro menu */
274 _switch_mode
= (_game_mode
== GM_EDITOR
) ? SM_EDITOR
: SM_MENU
;
276 if (_gw
.abortp
!= NULL
) _gw
.abortp();
280 if (_gw
.thread
!= NULL
) _gw
.thread
->Exit();
282 SwitchToMode(_switch_mode
);
287 * @param mode The mode of world generation (see GenWorldMode).
288 * @param size_x The X-size of the map.
289 * @param size_y The Y-size of the map.
290 * @param reset_settings Whether to reset the game configuration (used for restart)
292 void GenerateWorld(GenWorldMode mode
, uint size_x
, uint size_y
, bool reset_settings
)
294 if (HasModalProgress()) return;
298 SetModalProgress(true);
301 _gw
.lc
= _local_company
;
302 _gw
.quit_thread
= false;
305 /* This disables some commands and stuff */
306 SetLocalCompany(COMPANY_SPECTATOR
);
308 InitializeGame(_gw
.size_x
, _gw
.size_y
, true, reset_settings
);
309 PrepareGenerateWorldProgress();
311 /* Load the right landscape stuff, and the NewGRFs! */
313 LoadStringWidthTable();
315 /* Re-init the windowing system */
318 /* Create toolbars */
319 SetupColoursAndInitialWindow();
320 SetObjectToPlace(SPR_CURSOR_ZZZ
, PAL_NONE
, HT_NONE
, WC_MAIN_WINDOW
, 0);
322 if (_gw
.thread
!= NULL
) {
328 if (!_video_driver
->HasGUI() || !ThreadObject::New(&_GenerateWorld
, NULL
, &_gw
.thread
)) {
329 DEBUG(misc
, 1, "Cannot create genworld thread, reverting to single-threaded mode");
330 _gw
.threaded
= false;
331 _modal_progress_work_mutex
->EndCritical();
332 _GenerateWorld(NULL
);
333 _modal_progress_work_mutex
->BeginCritical();
337 UnshowCriticalError();
338 /* Remove any open window */
339 DeleteAllNonVitalWindows();
340 /* Hide vital windows, because we don't allow to use them */
343 /* Don't show the dialog if we don't have a thread */
344 ShowGenerateWorldProgress();
346 /* Centre the view on the map */
347 if (FindWindowById(WC_MAIN_WINDOW
, 0) != NULL
) {
348 ScrollMainWindowToTile(TileXY(MapSizeX() / 2, MapSizeY() / 2), true);