(svn r23005) -Fix (r23004): Of course there's still the 16-sprite version for shore...
[openttd/fttd.git] / src / genworld.cpp
blobf768e39af509c239f3615211fb7212405a3913f5
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 "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 "gfx_func.h"
22 #include "date_func.h"
23 #include "engine_func.h"
24 #include "newgrf_storage.h"
25 #include "water.h"
26 #include "video/video_driver.hpp"
27 #include "tilehighlight_func.h"
28 #include "saveload/saveload.h"
29 #include "void_map.h"
30 #include "town.h"
31 #include "newgrf.h"
32 #include "core/random_func.hpp"
33 #include "core/backup_type.hpp"
34 #include "progress.h"
36 #include "table/sprites.h"
38 void GenerateClearTile();
39 void GenerateIndustries();
40 void GenerateObjects();
41 void GenerateTrees();
43 void StartupEconomy();
44 void StartupCompanies();
45 void StartupDisasters();
47 void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settings);
49 /**
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! -- TrueLight
55 GenWorldInfo _gw;
57 /** Whether we are generating the map or not. */
58 bool _generating_world;
60 /**
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;
69 /**
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);
81 _gw.proc = NULL;
82 _gw.abortp = NULL;
83 _gw.threaded = false;
85 DeleteWindowById(WC_MODAL_PROGRESS, 0);
86 MarkWholeScreenDirty();
89 /**
90 * The internal, real, generate function.
92 static void _GenerateWorld(void *)
94 /* Make sure everything is done via OWNER_NONE. */
95 Backup<CompanyByte> _cur_company(_current_company, OWNER_NONE, FILE_LINE);
97 try {
98 _generating_world = true;
99 _modal_progress_work_mutex->BeginCritical();
100 if (_network_dedicated) DEBUG(net, 0, "Generating map, please wait...");
101 /* Set the Random() seed to generation_seed so we produce the same map with the same seed */
102 if (_settings_game.game_creation.generation_seed == GENERATE_NEW_SEED) _settings_game.game_creation.generation_seed = _settings_newgame.game_creation.generation_seed = InteractiveRandom();
103 _random.SetSeed(_settings_game.game_creation.generation_seed);
104 SetGeneratingWorldProgress(GWP_MAP_INIT, 2);
105 SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);
107 IncreaseGeneratingWorldProgress(GWP_MAP_INIT);
108 /* Must start economy early because of the costs. */
109 StartupEconomy();
111 /* Don't generate landscape items when in the scenario editor. */
112 if (_gw.mode == GWM_EMPTY) {
113 SetGeneratingWorldProgress(GWP_OBJECT, 1);
115 /* Make sure the tiles at the north border are void tiles if needed. */
116 if (_settings_game.construction.freeform_edges) {
117 for (uint row = 0; row < MapSizeY(); row++) MakeVoid(TileXY(0, row));
118 for (uint col = 0; col < MapSizeX(); col++) MakeVoid(TileXY(col, 0));
121 /* Make the map the height of the setting */
122 if (_game_mode != GM_MENU) FlatEmptyWorld(_settings_game.game_creation.se_flat_world_height);
124 ConvertGroundTilesIntoWaterTiles();
125 IncreaseGeneratingWorldProgress(GWP_OBJECT);
126 } else {
127 GenerateLandscape(_gw.mode);
128 GenerateClearTile();
130 /* only generate towns, tree and industries in newgame mode. */
131 if (_game_mode != GM_EDITOR) {
132 if (!GenerateTowns(_settings_game.economy.town_layout)) {
133 _cur_company.Restore();
134 HandleGeneratingWorldAbortion();
135 return;
137 GenerateIndustries();
138 GenerateObjects();
139 GenerateTrees();
143 ClearStorageChanges(true);
145 /* These are probably pointless when inside the scenario editor. */
146 SetGeneratingWorldProgress(GWP_GAME_INIT, 3);
147 StartupCompanies();
148 IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
149 StartupEngines();
150 IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
151 StartupDisasters();
152 _generating_world = false;
154 /* No need to run the tile loop in the scenario editor. */
155 if (_gw.mode != GWM_EMPTY) {
156 uint i;
158 SetGeneratingWorldProgress(GWP_RUNTILELOOP, 0x500);
159 for (i = 0; i < 0x500; i++) {
160 RunTileLoop();
161 IncreaseGeneratingWorldProgress(GWP_RUNTILELOOP);
165 ResetObjectToPlace();
166 _cur_company.Trash();
167 _current_company = _local_company = _gw.lc;
169 SetGeneratingWorldProgress(GWP_GAME_START, 1);
170 /* Call any callback */
171 if (_gw.proc != NULL) _gw.proc();
172 IncreaseGeneratingWorldProgress(GWP_GAME_START);
174 CleanupGeneration();
175 _modal_progress_work_mutex->EndCritical();
177 ShowNewGRFError();
179 if (_network_dedicated) DEBUG(net, 0, "Map generated, starting game");
180 DEBUG(desync, 1, "new_map: %08x", _settings_game.game_creation.generation_seed);
182 if (_debug_desync_level > 0) {
183 char name[MAX_PATH];
184 snprintf(name, lengthof(name), "dmp_cmds_%08x_%08x.sav", _settings_game.game_creation.generation_seed, _date);
185 SaveOrLoad(name, SL_SAVE, AUTOSAVE_DIR, false);
187 } catch (...) {
188 if (_cur_company.IsValid()) _cur_company.Restore();
189 _generating_world = false;
190 _modal_progress_work_mutex->EndCritical();
191 throw;
196 * Set here the function, if any, that you want to be called when landscape
197 * generation is done.
198 * @param proc callback procedure
200 void GenerateWorldSetCallback(GWDoneProc *proc)
202 _gw.proc = proc;
206 * Set here the function, if any, that you want to be called when landscape
207 * generation is aborted.
208 * @param proc callback procedure
210 void GenerateWorldSetAbortCallback(GWAbortProc *proc)
212 _gw.abortp = proc;
216 * This will wait for the thread to finish up his work. It will not continue
217 * till the work is done.
219 void WaitTillGeneratedWorld()
221 if (_gw.thread == NULL) return;
223 _modal_progress_work_mutex->EndCritical();
224 _modal_progress_paint_mutex->EndCritical();
225 _gw.quit_thread = true;
226 _gw.thread->Join();
227 delete _gw.thread;
228 _gw.thread = NULL;
229 _gw.threaded = false;
230 _modal_progress_work_mutex->BeginCritical();
231 _modal_progress_paint_mutex->BeginCritical();
235 * Initializes the abortion process
237 void AbortGeneratingWorld()
239 _gw.abort = true;
243 * Is the generation being aborted?
244 * @return the 'aborted' status
246 bool IsGeneratingWorldAborted()
248 return _gw.abort;
252 * Really handle the abortion, i.e. clean up some of the mess
254 void HandleGeneratingWorldAbortion()
256 /* Clean up - in SE create an empty map, otherwise, go to intro menu */
257 _switch_mode = (_game_mode == GM_EDITOR) ? SM_EDITOR : SM_MENU;
259 if (_gw.abortp != NULL) _gw.abortp();
261 CleanupGeneration();
263 if (_gw.thread != NULL) _gw.thread->Exit();
265 extern void SwitchToMode(SwitchMode new_mode);
266 SwitchToMode(_switch_mode);
270 * Generate a world.
271 * @param mode The mode of world generation (see GenWorldMode).
272 * @param size_x The X-size of the map.
273 * @param size_y The Y-size of the map.
274 * @param reset_settings Whether to reset the game configuration (used for restart)
276 void GenerateWorld(GenWorldMode mode, uint size_x, uint size_y, bool reset_settings)
278 if (HasModalProgress()) return;
279 _gw.mode = mode;
280 _gw.size_x = size_x;
281 _gw.size_y = size_y;
282 SetModalProgress(true);
283 _gw.abort = false;
284 _gw.abortp = NULL;
285 _gw.lc = _local_company;
286 _gw.quit_thread = false;
287 _gw.threaded = true;
289 /* This disables some commands and stuff */
290 SetLocalCompany(COMPANY_SPECTATOR);
292 InitializeGame(_gw.size_x, _gw.size_y, true, reset_settings);
293 PrepareGenerateWorldProgress();
295 /* Load the right landscape stuff, and the NewGRFs! */
296 GfxLoadSprites();
297 LoadStringWidthTable();
299 /* Re-init the windowing system */
300 ResetWindowSystem();
302 /* Create toolbars */
303 SetupColoursAndInitialWindow();
304 SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);
306 if (_gw.thread != NULL) {
307 _gw.thread->Join();
308 delete _gw.thread;
309 _gw.thread = NULL;
312 if (!_video_driver->HasGUI() || !ThreadObject::New(&_GenerateWorld, NULL, &_gw.thread)) {
313 DEBUG(misc, 1, "Cannot create genworld thread, reverting to single-threaded mode");
314 _gw.threaded = false;
315 _modal_progress_work_mutex->EndCritical();
316 _GenerateWorld(NULL);
317 _modal_progress_work_mutex->BeginCritical();
318 return;
321 /* Remove any open window */
322 DeleteAllNonVitalWindows();
323 /* Hide vital windows, because we don't allow to use them */
324 HideVitalWindows();
326 /* Don't show the dialog if we don't have a thread */
327 ShowGenerateWorldProgress();
329 /* Centre the view on the map */
330 if (FindWindowById(WC_MAIN_WINDOW, 0) != NULL) {
331 ScrollMainWindowToTile(TileXY(MapSizeX() / 2, MapSizeY() / 2), true);