Hotfix: Conditional order comparator dropdown list was broken.
[openttd-joker.git] / src / genworld.cpp
blob8931cef8e3121a6cea8e9a90720529a958b39675
1 /* $Id: genworld.cpp 26371 2014-02-23 22:03:08Z frosch $ */
3 /*
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/>.
8 */
10 /** @file genworld.cpp Functions to generate a map. */
12 #include "stdafx.h"
13 #include "landscape.h"
14 #include "company_func.h"
15 #include "genworld.h"
16 #include "gfxinit.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"
23 #include "water.h"
24 #include "video/video_driver.hpp"
25 #include "tilehighlight_func.h"
26 #include "saveload/saveload.h"
27 #include "void_map.h"
28 #include "town.h"
29 #include "newgrf.h"
30 #include "core/random_func.hpp"
31 #include "core/backup_type.hpp"
32 #include "progress.h"
33 #include "error.h"
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();
44 void GenerateTrees();
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);
53 /**
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!
59 GenWorldInfo _gw;
61 /** Whether we are generating the map or not. */
62 bool _generating_world;
64 /**
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;
73 /**
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);
85 _gw.proc = NULL;
86 _gw.abortp = NULL;
87 _gw.threaded = false;
89 DeleteWindowByClass(WC_MODAL_PROGRESS);
90 ShowFirstError();
91 MarkWholeScreenDirty();
94 /**
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);
102 try {
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. */
116 StartupEconomy();
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);
133 } else {
134 GenerateLandscape(_gw.mode);
135 GenerateClearTile();
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();
142 return;
144 GenerateIndustries();
145 GenerateObjects();
146 GenerateTrees();
147 GeneratePublicRoads();
151 /* These are probably pointless when inside the scenario editor. */
152 SetGeneratingWorldProgress(GWP_GAME_INIT, 3);
153 StartupCompanies();
154 IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
155 StartupEngines();
156 IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
157 StartupDisasters();
158 _generating_world = false;
160 /* No need to run the tile loop in the scenario editor. */
161 if (_gw.mode != GWM_EMPTY) {
162 uint i;
164 SetGeneratingWorldProgress(GWP_RUNTILELOOP, 0x500);
165 for (i = 0; i < 0x500; i++) {
166 RunTileLoop();
167 _tick_counter++;
168 IncreaseGeneratingWorldProgress(GWP_RUNTILELOOP);
171 if (_game_mode != GM_EDITOR) {
172 Game::StartNew();
174 if (Game::GetInstance() != NULL) {
175 SetGeneratingWorldProgress(GWP_RUNSCRIPT, 2500);
176 _generating_world = true;
177 for (i = 0; i < 2500; i++) {
178 Game::GameLoop();
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 != NULL) _gw.proc();
196 IncreaseGeneratingWorldProgress(GWP_GAME_START);
198 CleanupGeneration();
199 _modal_progress_work_mutex->EndCritical();
201 ShowNewGRFError();
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) {
207 char name[MAX_PATH];
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);
211 } catch (...) {
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();
216 throw;
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)
227 _gw.proc = 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)
237 _gw.abortp = 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 == NULL) return;
248 _modal_progress_work_mutex->EndCritical();
249 _modal_progress_paint_mutex->EndCritical();
250 _gw.quit_thread = true;
251 _gw.thread->Join();
252 delete _gw.thread;
253 _gw.thread = NULL;
254 _gw.threaded = false;
255 _modal_progress_work_mutex->BeginCritical();
256 _modal_progress_paint_mutex->BeginCritical();
260 * Initializes the abortion process
262 void AbortGeneratingWorld()
264 _gw.abort = true;
268 * Is the generation being aborted?
269 * @return the 'aborted' status
271 bool IsGeneratingWorldAborted()
273 return _gw.abort;
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 != NULL) _gw.abortp();
286 CleanupGeneration();
288 if (_gw.thread != NULL) _gw.thread->Exit();
290 SwitchToMode(_switch_mode);
294 * Generate a world.
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;
303 _gw.mode = mode;
304 _gw.size_x = size_x;
305 _gw.size_y = size_y;
306 SetModalProgress(true);
307 _gw.abort = false;
308 _gw.abortp = NULL;
309 _gw.lc = _local_company;
310 _gw.quit_thread = false;
311 _gw.threaded = true;
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! */
320 GfxLoadSprites();
321 LoadStringWidthTable();
323 /* Re-init the windowing system */
324 ResetWindowSystem();
326 /* Create toolbars */
327 SetupColoursAndInitialWindow();
328 SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);
330 if (_gw.thread != NULL) {
331 _gw.thread->Join();
332 delete _gw.thread;
333 _gw.thread = NULL;
336 if (!VideoDriver::GetInstance()->HasGUI() || !ThreadObject::New(&_GenerateWorld, NULL, &_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(NULL);
341 _modal_progress_work_mutex->BeginCritical();
342 return;
345 UnshowCriticalError();
346 /* Remove any open window */
347 DeleteAllNonVitalWindows();
348 /* Hide vital windows, because we don't allow to use them */
349 HideVitalWindows();
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) != NULL) {
356 ScrollMainWindowToTile(TileXY(MapSizeX() / 2, MapSizeY() / 2), true);