genworld.cpp

Go to the documentation of this file.
00001 /* $Id: genworld.cpp 23611 2011-12-19 20:56:50Z truebrain $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #include "stdafx.h"
00013 #include "landscape.h"
00014 #include "company_func.h"
00015 #include "genworld.h"
00016 #include "gfxinit.h"
00017 #include "window_func.h"
00018 #include "network/network.h"
00019 #include "heightmap.h"
00020 #include "viewport_func.h"
00021 #include "gfx_func.h"
00022 #include "date_func.h"
00023 #include "engine_func.h"
00024 #include "newgrf_storage.h"
00025 #include "water.h"
00026 #include "video/video_driver.hpp"
00027 #include "tilehighlight_func.h"
00028 #include "saveload/saveload.h"
00029 #include "void_map.h"
00030 #include "town.h"
00031 #include "newgrf.h"
00032 #include "core/random_func.hpp"
00033 #include "core/backup_type.hpp"
00034 #include "progress.h"
00035 #include "error.h"
00036 #include "game/game.hpp"
00037 #include "game/game_instance.hpp"
00038 
00039 #include "table/sprites.h"
00040 
00041 void GenerateClearTile();
00042 void GenerateIndustries();
00043 void GenerateObjects();
00044 void GenerateTrees();
00045 
00046 void StartupEconomy();
00047 void StartupCompanies();
00048 void StartupDisasters();
00049 
00050 void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settings);
00051 
00058 GenWorldInfo _gw;
00059 
00061 bool _generating_world;
00062 
00067 bool IsGenerateWorldThreaded()
00068 {
00069   return _gw.threaded && !_gw.quit_thread;
00070 }
00071 
00076 static void CleanupGeneration()
00077 {
00078   _generating_world = false;
00079 
00080   if (_cursor.sprite == SPR_CURSOR_ZZZ) SetMouseCursor(SPR_CURSOR_MOUSE, PAL_NONE);
00081   /* Show all vital windows again, because we have hidden them */
00082   if (_gw.threaded && _game_mode != GM_MENU) ShowVitalWindows();
00083   SetModalProgress(false);
00084   _gw.proc     = NULL;
00085   _gw.abortp   = NULL;
00086   _gw.threaded = false;
00087 
00088   DeleteWindowByClass(WC_MODAL_PROGRESS);
00089   ShowFirstError();
00090   MarkWholeScreenDirty();
00091 }
00092 
00096 static void _GenerateWorld(void *)
00097 {
00098   /* Make sure everything is done via OWNER_NONE. */
00099   Backup<CompanyByte> _cur_company(_current_company, OWNER_NONE, FILE_LINE);
00100 
00101   try {
00102     _generating_world = true;
00103     _modal_progress_work_mutex->BeginCritical();
00104     if (_network_dedicated) DEBUG(net, 0, "Generating map, please wait...");
00105     /* Set the Random() seed to generation_seed so we produce the same map with the same seed */
00106     if (_settings_game.game_creation.generation_seed == GENERATE_NEW_SEED) _settings_game.game_creation.generation_seed = _settings_newgame.game_creation.generation_seed = InteractiveRandom();
00107     _random.SetSeed(_settings_game.game_creation.generation_seed);
00108     SetGeneratingWorldProgress(GWP_MAP_INIT, 2);
00109     SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);
00110 
00111     IncreaseGeneratingWorldProgress(GWP_MAP_INIT);
00112     /* Must start economy early because of the costs. */
00113     StartupEconomy();
00114 
00115     /* Don't generate landscape items when in the scenario editor. */
00116     if (_gw.mode == GWM_EMPTY) {
00117       SetGeneratingWorldProgress(GWP_OBJECT, 1);
00118 
00119       /* Make sure the tiles at the north border are void tiles if needed. */
00120       if (_settings_game.construction.freeform_edges) {
00121         for (uint row = 0; row < MapSizeY(); row++) MakeVoid(TileXY(0, row));
00122         for (uint col = 0; col < MapSizeX(); col++) MakeVoid(TileXY(col, 0));
00123       }
00124 
00125       /* Make the map the height of the setting */
00126       if (_game_mode != GM_MENU) FlatEmptyWorld(_settings_game.game_creation.se_flat_world_height);
00127 
00128       ConvertGroundTilesIntoWaterTiles();
00129       IncreaseGeneratingWorldProgress(GWP_OBJECT);
00130     } else {
00131       GenerateLandscape(_gw.mode);
00132       GenerateClearTile();
00133 
00134       /* only generate towns, tree and industries in newgame mode. */
00135       if (_game_mode != GM_EDITOR) {
00136         if (!GenerateTowns(_settings_game.economy.town_layout)) {
00137           _cur_company.Restore();
00138           HandleGeneratingWorldAbortion();
00139           return;
00140         }
00141         GenerateIndustries();
00142         GenerateObjects();
00143         GenerateTrees();
00144       }
00145     }
00146 
00147     ClearStorageChanges(true);
00148 
00149     /* These are probably pointless when inside the scenario editor. */
00150     SetGeneratingWorldProgress(GWP_GAME_INIT, 3);
00151     StartupCompanies();
00152     IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
00153     StartupEngines();
00154     IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
00155     StartupDisasters();
00156     _generating_world = false;
00157 
00158     /* No need to run the tile loop in the scenario editor. */
00159     if (_gw.mode != GWM_EMPTY) {
00160       uint i;
00161 
00162       SetGeneratingWorldProgress(GWP_RUNTILELOOP, 0x500);
00163       for (i = 0; i < 0x500; i++) {
00164         RunTileLoop();
00165         IncreaseGeneratingWorldProgress(GWP_RUNTILELOOP);
00166       }
00167 
00168       if (_game_mode != GM_EDITOR) {
00169         Game::StartNew();
00170 
00171         if (Game::GetInstance() != NULL) {
00172           SetGeneratingWorldProgress(GWP_RUNSCRIPT, 2500);
00173           _generating_world = true;
00174           for (i = 0; i < 2500; i++) {
00175             Game::GameLoop();
00176             IncreaseGeneratingWorldProgress(GWP_RUNSCRIPT);
00177             if (Game::GetInstance()->IsSleeping()) break;
00178           }
00179           _generating_world = false;
00180         }
00181       }
00182     }
00183 
00184     ResetObjectToPlace();
00185     _cur_company.Trash();
00186     _current_company = _local_company = _gw.lc;
00187 
00188     SetGeneratingWorldProgress(GWP_GAME_START, 1);
00189     /* Call any callback */
00190     if (_gw.proc != NULL) _gw.proc();
00191     IncreaseGeneratingWorldProgress(GWP_GAME_START);
00192 
00193     CleanupGeneration();
00194     _modal_progress_work_mutex->EndCritical();
00195 
00196     ShowNewGRFError();
00197 
00198     if (_network_dedicated) DEBUG(net, 0, "Map generated, starting game");
00199     DEBUG(desync, 1, "new_map: %08x", _settings_game.game_creation.generation_seed);
00200 
00201     if (_debug_desync_level > 0) {
00202       char name[MAX_PATH];
00203       snprintf(name, lengthof(name), "dmp_cmds_%08x_%08x.sav", _settings_game.game_creation.generation_seed, _date);
00204       SaveOrLoad(name, SL_SAVE, AUTOSAVE_DIR, false);
00205     }
00206   } catch (...) {
00207     if (_cur_company.IsValid()) _cur_company.Restore();
00208     _generating_world = false;
00209     _modal_progress_work_mutex->EndCritical();
00210     throw;
00211   }
00212 }
00213 
00219 void GenerateWorldSetCallback(GWDoneProc *proc)
00220 {
00221   _gw.proc = proc;
00222 }
00223 
00229 void GenerateWorldSetAbortCallback(GWAbortProc *proc)
00230 {
00231   _gw.abortp = proc;
00232 }
00233 
00238 void WaitTillGeneratedWorld()
00239 {
00240   if (_gw.thread == NULL) return;
00241 
00242   _modal_progress_work_mutex->EndCritical();
00243   _modal_progress_paint_mutex->EndCritical();
00244   _gw.quit_thread = true;
00245   _gw.thread->Join();
00246   delete _gw.thread;
00247   _gw.thread   = NULL;
00248   _gw.threaded = false;
00249   _modal_progress_work_mutex->BeginCritical();
00250   _modal_progress_paint_mutex->BeginCritical();
00251 }
00252 
00256 void AbortGeneratingWorld()
00257 {
00258   _gw.abort = true;
00259 }
00260 
00265 bool IsGeneratingWorldAborted()
00266 {
00267   return _gw.abort;
00268 }
00269 
00273 void HandleGeneratingWorldAbortion()
00274 {
00275   /* Clean up - in SE create an empty map, otherwise, go to intro menu */
00276   _switch_mode = (_game_mode == GM_EDITOR) ? SM_EDITOR : SM_MENU;
00277 
00278   if (_gw.abortp != NULL) _gw.abortp();
00279 
00280   CleanupGeneration();
00281 
00282   if (_gw.thread != NULL) _gw.thread->Exit();
00283 
00284   SwitchToMode(_switch_mode);
00285 }
00286 
00294 void GenerateWorld(GenWorldMode mode, uint size_x, uint size_y, bool reset_settings)
00295 {
00296   if (HasModalProgress()) return;
00297   _gw.mode   = mode;
00298   _gw.size_x = size_x;
00299   _gw.size_y = size_y;
00300   SetModalProgress(true);
00301   _gw.abort  = false;
00302   _gw.abortp = NULL;
00303   _gw.lc     = _local_company;
00304   _gw.quit_thread   = false;
00305   _gw.threaded      = true;
00306 
00307   /* This disables some commands and stuff */
00308   SetLocalCompany(COMPANY_SPECTATOR);
00309 
00310   InitializeGame(_gw.size_x, _gw.size_y, true, reset_settings);
00311   PrepareGenerateWorldProgress();
00312 
00313   /* Load the right landscape stuff, and the NewGRFs! */
00314   GfxLoadSprites();
00315   LoadStringWidthTable();
00316 
00317   /* Re-init the windowing system */
00318   ResetWindowSystem();
00319 
00320   /* Create toolbars */
00321   SetupColoursAndInitialWindow();
00322   SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);
00323 
00324   if (_gw.thread != NULL) {
00325     _gw.thread->Join();
00326     delete _gw.thread;
00327     _gw.thread = NULL;
00328   }
00329 
00330   if (!_video_driver->HasGUI() || !ThreadObject::New(&_GenerateWorld, NULL, &_gw.thread)) {
00331     DEBUG(misc, 1, "Cannot create genworld thread, reverting to single-threaded mode");
00332     _gw.threaded = false;
00333     _modal_progress_work_mutex->EndCritical();
00334     _GenerateWorld(NULL);
00335     _modal_progress_work_mutex->BeginCritical();
00336     return;
00337   }
00338 
00339   UnshowCriticalError();
00340   /* Remove any open window */
00341   DeleteAllNonVitalWindows();
00342   /* Hide vital windows, because we don't allow to use them */
00343   HideVitalWindows();
00344 
00345   /* Don't show the dialog if we don't have a thread */
00346   ShowGenerateWorldProgress();
00347 
00348   /* Centre the view on the map */
00349   if (FindWindowById(WC_MAIN_WINDOW, 0) != NULL) {
00350     ScrollMainWindowToTile(TileXY(MapSizeX() / 2, MapSizeY() / 2), true);
00351   }
00352 }