genworld.cpp

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