Translations update
[openttd/fttd.git] / src / genworld.cpp
blob0f3b3b41ac51014871e375c540ebea9f060b4d2a
1 /* $Id$ */
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 "string.h"
14 #include "landscape.h"
15 #include "company_func.h"
16 #include "genworld.h"
17 #include "gfxinit.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"
24 #include "water.h"
25 #include "video/video_driver.hpp"
26 #include "tilehighlight_func.h"
27 #include "saveload/saveload.h"
28 #include "map/ground.h"
29 #include "town.h"
30 #include "newgrf.h"
31 #include "core/random_func.hpp"
32 #include "core/backup_type.hpp"
33 #include "progress.h"
34 #include "error.h"
35 #include "game/game.hpp"
36 #include "game/game_instance.hpp"
39 void GenerateClearTile();
40 void GenerateIndustries();
41 void GenerateObjects();
42 void GenerateTrees();
44 void StartupEconomy();
45 void StartupCompanies();
46 void StartupDisasters();
48 void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settings);
50 /**
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!
56 GenWorldInfo _gw;
58 /** Whether we are generating the map or not. */
59 bool _generating_world;
61 /**
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;
70 /**
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);
82 _gw.proc = NULL;
83 _gw.abortp = NULL;
84 _gw.threaded = false;
86 DeleteWindowByClass(WC_MODAL_PROGRESS);
87 ShowFirstError();
88 MarkWholeScreenDirty();
91 /**
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);
99 try {
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. */
113 StartupEconomy();
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);
130 } else {
131 GenerateLandscape(_gw.mode);
132 GenerateClearTile();
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();
139 return;
141 GenerateIndustries();
142 GenerateObjects();
143 GenerateTrees();
147 /* These are probably pointless when inside the scenario editor. */
148 SetGeneratingWorldProgress(GWP_GAME_INIT, 3);
149 StartupCompanies();
150 IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
151 StartupEngines();
152 IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
153 StartupDisasters();
154 _generating_world = false;
156 /* No need to run the tile loop in the scenario editor. */
157 if (_gw.mode != GWM_EMPTY) {
158 uint i;
160 SetGeneratingWorldProgress(GWP_RUNTILELOOP, 0x500);
161 for (i = 0; i < 0x500; i++) {
162 RunTileLoop();
163 _tick_counter++;
164 IncreaseGeneratingWorldProgress(GWP_RUNTILELOOP);
167 if (_game_mode != GM_EDITOR) {
168 Game::StartNew();
170 if (Game::GetInstance() != NULL) {
171 SetGeneratingWorldProgress(GWP_RUNSCRIPT, 2500);
172 _generating_world = true;
173 for (i = 0; i < 2500; i++) {
174 Game::GameLoop();
175 IncreaseGeneratingWorldProgress(GWP_RUNSCRIPT);
176 if (Game::GetInstance()->IsSleeping()) break;
178 _generating_world = false;
183 BasePersistentStorageArray::SwitchMode(PSM_LEAVE_GAMELOOP);
185 ResetPointerMode();
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);
194 CleanupGeneration();
195 _modal_progress_work_mutex->EndCritical();
197 ShowNewGRFError();
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) {
203 char name[MAX_PATH];
204 bstrfmt (name, "dmp_cmds_%08x_%08x.sav", _settings_game.game_creation.generation_seed, _date);
205 SaveGame(name, AUTOSAVE_DIR, false);
207 } catch (...) {
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();
212 throw;
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)
223 _gw.proc = 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)
233 _gw.abortp = 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;
247 _gw.thread->Join();
248 delete _gw.thread;
249 _gw.thread = NULL;
250 _gw.threaded = false;
251 _modal_progress_work_mutex->BeginCritical();
252 _modal_progress_paint_mutex->BeginCritical();
256 * Initializes the abortion process
258 void AbortGeneratingWorld()
260 _gw.abort = true;
264 * Is the generation being aborted?
265 * @return the 'aborted' status
267 bool IsGeneratingWorldAborted()
269 return _gw.abort;
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();
282 CleanupGeneration();
284 if (_gw.thread != NULL) _gw.thread->Exit();
286 SwitchToMode(_switch_mode);
290 * Generate a world.
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;
299 _gw.mode = mode;
300 _gw.size_x = size_x;
301 _gw.size_y = size_y;
302 SetModalProgress(true);
303 _gw.abort = false;
304 _gw.abortp = NULL;
305 _gw.lc = _local_company;
306 _gw.quit_thread = false;
307 _gw.threaded = true;
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! */
316 GfxLoadSprites();
317 LoadStringWidthTable();
319 /* Re-init the windowing system */
320 ResetWindowSystem();
322 /* Create toolbars */
323 SetupColoursAndInitialWindow();
324 SetPointerMode (POINTER_NONE, WC_MAIN_WINDOW, 0, SPR_CURSOR_ZZZ);
326 if (_gw.thread != NULL) {
327 _gw.thread->Join();
328 delete _gw.thread;
329 _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();
338 return;
341 UnshowCriticalError();
342 /* Remove any open window */
343 DeleteAllNonVitalWindows();
344 /* Hide vital windows, because we don't allow to use them */
345 HideVitalWindows();
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);