1 /* $Id: genworld.cpp 26371 2014-02-23 22:03:08Z frosch $ */
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"
30 #include "core/random_func.hpp"
31 #include "core/backup_type.hpp"
34 #include "game/game.hpp"
35 #include "game/game_instance.hpp"
36 #include "string_func.h"
38 #include "safeguards.h"
41 void GenerateClearTile();
42 void GenerateIndustries();
43 void GenerateObjects();
45 void GeneratePublicRoads();
47 void StartupEconomy();
48 void StartupCompanies();
49 void StartupDisasters();
51 void InitializeGame(uint size_x
, uint size_y
, bool reset_date
, bool reset_settings
);
54 * Please only use this variable in genworld.h and genworld.cpp and
55 * nowhere else. For speed improvements we need it to be global, but
56 * in no way the meaning of it is to use it anywhere else besides
57 * in the genworld.h and genworld.cpp!
61 /** Whether we are generating the map or not. */
62 bool _generating_world
;
65 * Tells if the world generation is done in a thread or not.
66 * @return the 'threaded' status
68 bool IsGenerateWorldThreaded()
70 return _gw
.threaded
&& !_gw
.quit_thread
;
74 * Clean up the 'mess' of generation. That is, show windows again, reset
75 * thread variables, and delete the progress window.
77 static void CleanupGeneration()
79 _generating_world
= false;
81 SetMouseCursorBusy(false);
82 /* Show all vital windows again, because we have hidden them */
83 if (_gw
.threaded
&& _game_mode
!= GM_MENU
) ShowVitalWindows();
84 SetModalProgress(false);
89 DeleteWindowByClass(WC_MODAL_PROGRESS
);
91 MarkWholeScreenDirty();
95 * The internal, real, generate function.
97 static void _GenerateWorld(void *)
99 /* Make sure everything is done via OWNER_NONE. */
100 Backup
<CompanyByte
> _cur_company(_current_company
, OWNER_NONE
, FILE_LINE
);
103 _generating_world
= true;
104 _modal_progress_work_mutex
->BeginCritical();
105 if (_network_dedicated
) DEBUG(net
, 1, "Generating map, please wait...");
106 /* Set the Random() seed to generation_seed so we produce the same map with the same seed */
107 if (_settings_game
.game_creation
.generation_seed
== GENERATE_NEW_SEED
) _settings_game
.game_creation
.generation_seed
= _settings_newgame
.game_creation
.generation_seed
= InteractiveRandom();
108 _random
.SetSeed(_settings_game
.game_creation
.generation_seed
);
109 SetGeneratingWorldProgress(GWP_MAP_INIT
, 2);
110 SetObjectToPlace(SPR_CURSOR_ZZZ
, PAL_NONE
, HT_NONE
, WC_MAIN_WINDOW
, 0);
112 BasePersistentStorageArray::SwitchMode(PSM_ENTER_GAMELOOP
);
114 IncreaseGeneratingWorldProgress(GWP_MAP_INIT
);
115 /* Must start economy early because of the costs. */
118 /* Don't generate landscape items when in the scenario editor. */
119 if (_gw
.mode
== GWM_EMPTY
) {
120 SetGeneratingWorldProgress(GWP_OBJECT
, 1);
122 /* Make sure the tiles at the north border are void tiles if needed. */
123 if (_settings_game
.construction
.freeform_edges
) {
124 for (uint row
= 0; row
< MapSizeY(); row
++) MakeVoid(TileXY(0, row
));
125 for (uint col
= 0; col
< MapSizeX(); col
++) MakeVoid(TileXY(col
, 0));
128 /* Make the map the height of the setting */
129 if (_game_mode
!= GM_MENU
) FlatEmptyWorld(_settings_game
.game_creation
.se_flat_world_height
);
131 ConvertGroundTilesIntoWaterTiles();
132 IncreaseGeneratingWorldProgress(GWP_OBJECT
);
134 GenerateLandscape(_gw
.mode
);
137 /* only generate towns, tree and industries in newgame mode. */
138 if (_game_mode
!= GM_EDITOR
) {
139 if (!GenerateTowns(_settings_game
.economy
.town_layout
)) {
140 _cur_company
.Restore();
141 HandleGeneratingWorldAbortion();
144 GenerateIndustries();
147 GeneratePublicRoads();
151 /* These are probably pointless when inside the scenario editor. */
152 SetGeneratingWorldProgress(GWP_GAME_INIT
, 3);
154 IncreaseGeneratingWorldProgress(GWP_GAME_INIT
);
156 IncreaseGeneratingWorldProgress(GWP_GAME_INIT
);
158 _generating_world
= false;
160 /* No need to run the tile loop in the scenario editor. */
161 if (_gw
.mode
!= GWM_EMPTY
) {
164 SetGeneratingWorldProgress(GWP_RUNTILELOOP
, 0x500);
165 for (i
= 0; i
< 0x500; i
++) {
168 IncreaseGeneratingWorldProgress(GWP_RUNTILELOOP
);
171 if (_game_mode
!= GM_EDITOR
) {
174 if (Game::GetInstance() != nullptr) {
175 SetGeneratingWorldProgress(GWP_RUNSCRIPT
, 2500);
176 _generating_world
= true;
177 for (i
= 0; i
< 2500; i
++) {
179 IncreaseGeneratingWorldProgress(GWP_RUNSCRIPT
);
180 if (Game::GetInstance()->IsSleeping()) break;
182 _generating_world
= false;
187 BasePersistentStorageArray::SwitchMode(PSM_LEAVE_GAMELOOP
);
189 ResetObjectToPlace();
190 _cur_company
.Trash();
191 _current_company
= _local_company
= _gw
.lc
;
193 SetGeneratingWorldProgress(GWP_GAME_START
, 1);
194 /* Call any callback */
195 if (_gw
.proc
!= nullptr) _gw
.proc();
196 IncreaseGeneratingWorldProgress(GWP_GAME_START
);
199 _modal_progress_work_mutex
->EndCritical();
203 if (_network_dedicated
) DEBUG(net
, 1, "Map generated, starting game");
204 DEBUG(desync
, 1, "new_map: %08x", _settings_game
.game_creation
.generation_seed
);
206 if (_debug_desync_level
> 0) {
208 seprintf(name
, lastof(name
), "dmp_cmds_%08x_%08x.sav", _settings_game
.game_creation
.generation_seed
, _date
);
209 SaveOrLoad(name
, SLO_SAVE
, DFT_GAME_FILE
, AUTOSAVE_DIR
, false);
212 BasePersistentStorageArray::SwitchMode(PSM_LEAVE_GAMELOOP
, true);
213 if (_cur_company
.IsValid()) _cur_company
.Restore();
214 _generating_world
= false;
215 _modal_progress_work_mutex
->EndCritical();
221 * Set here the function, if any, that you want to be called when landscape
222 * generation is done.
223 * @param proc callback procedure
225 void GenerateWorldSetCallback(GWDoneProc
*proc
)
231 * Set here the function, if any, that you want to be called when landscape
232 * generation is aborted.
233 * @param proc callback procedure
235 void GenerateWorldSetAbortCallback(GWAbortProc
*proc
)
241 * This will wait for the thread to finish up his work. It will not continue
242 * till the work is done.
244 void WaitTillGeneratedWorld()
246 if (_gw
.thread
== nullptr) return;
248 _modal_progress_work_mutex
->EndCritical();
249 _modal_progress_paint_mutex
->EndCritical();
250 _gw
.quit_thread
= true;
253 _gw
.thread
= nullptr;
254 _gw
.threaded
= false;
255 _modal_progress_work_mutex
->BeginCritical();
256 _modal_progress_paint_mutex
->BeginCritical();
260 * Initializes the abortion process
262 void AbortGeneratingWorld()
268 * Is the generation being aborted?
269 * @return the 'aborted' status
271 bool IsGeneratingWorldAborted()
277 * Really handle the abortion, i.e. clean up some of the mess
279 void HandleGeneratingWorldAbortion()
281 /* Clean up - in SE create an empty map, otherwise, go to intro menu */
282 _switch_mode
= (_game_mode
== GM_EDITOR
) ? SM_EDITOR
: SM_MENU
;
284 if (_gw
.abortp
!= nullptr) _gw
.abortp();
288 if (_gw
.thread
!= nullptr) _gw
.thread
->Exit();
290 SwitchToMode(_switch_mode
);
295 * @param mode The mode of world generation (see GenWorldMode).
296 * @param size_x The X-size of the map.
297 * @param size_y The Y-size of the map.
298 * @param reset_settings Whether to reset the game configuration (used for restart)
300 void GenerateWorld(GenWorldMode mode
, uint size_x
, uint size_y
, bool reset_settings
)
302 if (HasModalProgress()) return;
306 SetModalProgress(true);
308 _gw
.abortp
= nullptr;
309 _gw
.lc
= _local_company
;
310 _gw
.quit_thread
= false;
313 /* This disables some commands and stuff */
314 SetLocalCompany(COMPANY_SPECTATOR
);
316 InitializeGame(_gw
.size_x
, _gw
.size_y
, true, reset_settings
);
317 PrepareGenerateWorldProgress();
319 /* Load the right landscape stuff, and the NewGRFs! */
321 LoadStringWidthTable();
323 /* Re-init the windowing system */
326 /* Create toolbars */
327 SetupColoursAndInitialWindow();
328 SetObjectToPlace(SPR_CURSOR_ZZZ
, PAL_NONE
, HT_NONE
, WC_MAIN_WINDOW
, 0);
330 if (_gw
.thread
!= nullptr) {
333 _gw
.thread
= nullptr;
336 if (!VideoDriver::GetInstance()->HasGUI() || !ThreadObject::New(&_GenerateWorld
, nullptr, &_gw
.thread
, "ottd:genworld")) {
337 DEBUG(misc
, 1, "Cannot create genworld thread, reverting to single-threaded mode");
338 _gw
.threaded
= false;
339 _modal_progress_work_mutex
->EndCritical();
340 _GenerateWorld(nullptr);
341 _modal_progress_work_mutex
->BeginCritical();
345 UnshowCriticalError();
346 /* Remove any open window */
347 DeleteAllNonVitalWindows();
348 /* Hide vital windows, because we don't allow to use them */
351 /* Don't show the dialog if we don't have a thread */
352 ShowGenerateWorldProgress();
354 /* Centre the view on the map */
355 if (FindWindowById(WC_MAIN_WINDOW
, 0) != nullptr) {
356 ScrollMainWindowToTile(TileXY(MapSizeX() / 2, MapSizeY() / 2), true);