# genworld.cpp

Go to the documentation of this file.

    00001 /* $Id: genworld.cpp 20872 2010-10-02 14:51:33Z rubidium $ */
    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 "openttd.h"
    00014 #include "landscape.h"
    00015 #include "company_func.h"
    00016 #include "variables.h"
    00017 #include "thread/thread.h"
    00018 #include "command_func.h"
    00019 #include "genworld.h"
    00020 #include "gfxinit.h"
    00021 #include "window_func.h"
    00022 #include "network/network.h"
    00023 #include "heightmap.h"
    00024 #include "viewport_func.h"
    00025 #include "gfx_func.h"
    00026 #include "date_func.h"
    00027 #include "engine_func.h"
    00028 #include "newgrf_storage.h"
    00029 #include "water.h"
    00030 #include "blitter/factory.hpp"
    00031 #include "tilehighlight_func.h"
    00032 #include "saveload/saveload.h"
    00033 #include "void_map.h"
    00034 #include "town.h"
    00035 #include "newgrf.h"
    00036 #include "core/random_func.hpp"
    00037
    00038 #include "table/sprites.h"
    00039
    00040 void GenerateClearTile();
    00041 void GenerateIndustries();
    00042 void GenerateUnmovables();
    00043 void GenerateTrees();
    00044
    00045 void StartupEconomy();
    00046 void StartupCompanies();
    00047 void StartupDisasters();
    00048
    00049 void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settings);
    00050
    00057 GenWorldInfo _gw;
    00058
    00060 ThreadMutex *_genworld_mapgen_mutex = ThreadMutex::New();
    00062 ThreadMutex *_genworld_paint_mutex = ThreadMutex::New();
    00063
    00068 bool IsGenerateWorldThreaded()
    00069 {
    00070   return _gw.threaded && !_gw.quit_thread;
    00071 }
    00072
    00077 static void CleanupGeneration()
    00078 {
    00079   _generating_world = false;
    00080
    00081   if (_cursor.sprite == SPR_CURSOR_ZZZ) SetMouseCursor(SPR_CURSOR_MOUSE, PAL_NONE);
    00082   /* Show all vital windows again, because we have hidden them */
    00083   if (_gw.threaded && _game_mode != GM_MENU) ShowVitalWindows();
    00084   _gw.active   = false;
    00085   _gw.proc     = NULL;
    00086   _gw.abortp   = NULL;
    00087   _gw.threaded = false;
    00088
    00089   DeleteWindowById(WC_GENERATE_PROGRESS_WINDOW, 0);
    00090   MarkWholeScreenDirty();
    00091 }
    00092
    00096 static void _GenerateWorld(void *)
    00097 {
    00098   try {
    00099     _generating_world = true;
    00100     _genworld_mapgen_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_UNMOVABLE, 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_UNMOVABLE);
    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           HandleGeneratingWorldAbortion();
    00135           return;
    00136         }
    00137         GenerateIndustries();
    00138         GenerateUnmovables();
    00139         GenerateTrees();
    00140       }
    00141     }
    00142
    00143     ClearStorageChanges(true);
    00144
    00145     /* These are probably pointless when inside the scenario editor. */
    00146     SetGeneratingWorldProgress(GWP_GAME_INIT, 3);
    00147     StartupCompanies();
    00148     IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
    00149     StartupEngines();
    00150     IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
    00151     StartupDisasters();
    00152     _generating_world = false;
    00153
    00154     /* No need to run the tile loop in the scenario editor. */
    00155     if (_gw.mode != GWM_EMPTY) {
    00156       uint i;
    00157
    00158       SetGeneratingWorldProgress(GWP_RUNTILELOOP, 0x500);
    00159       for (i = 0; i < 0x500; i++) {
    00160         RunTileLoop();
    00161         IncreaseGeneratingWorldProgress(GWP_RUNTILELOOP);
    00162       }
    00163     }
    00164
    00165     ResetObjectToPlace();
    00166     _local_company = _gw.lc;
    00167
    00168     SetGeneratingWorldProgress(GWP_GAME_START, 1);
    00169     /* Call any callback */
    00170     if (_gw.proc != NULL) _gw.proc();
    00171     IncreaseGeneratingWorldProgress(GWP_GAME_START);
    00172
    00173     CleanupGeneration();
    00174     _genworld_mapgen_mutex->EndCritical();
    00175
    00176     ShowNewGRFError();
    00177
    00178     if (_network_dedicated) DEBUG(net, 0, "Map generated, starting game");
    00179     DEBUG(desync, 1, "new_map: %08x", _settings_game.game_creation.generation_seed);
    00180
    00181     if (_debug_desync_level > 0) {
    00182       char name[MAX_PATH];
    00183       snprintf(name, lengthof(name), "dmp_cmds_%08x_%08x.sav", _settings_game.game_creation.generation_seed, _date);
    00184       SaveOrLoad(name, SL_SAVE, AUTOSAVE_DIR);
    00185     }
    00186   } catch (...) {
    00187     _generating_world = false;
    00188     _genworld_mapgen_mutex->EndCritical();
    00189     throw;
    00190   }
    00191 }
    00192
    00198 void GenerateWorldSetCallback(GWDoneProc *proc)
    00199 {
    00200   _gw.proc = proc;
    00201 }
    00202
    00208 void GenerateWorldSetAbortCallback(GWAbortProc *proc)
    00209 {
    00210   _gw.abortp = proc;
    00211 }
    00212
    00217 void WaitTillGeneratedWorld()
    00218 {
    00219   if (_gw.thread == NULL) return;
    00220
    00221   _genworld_mapgen_mutex->EndCritical();
    00222   _genworld_paint_mutex->EndCritical();
    00223   _gw.quit_thread = true;
    00224   _gw.thread->Join();
    00225   delete _gw.thread;
    00226   _gw.thread   = NULL;
    00227   _gw.threaded = false;
    00228   _genworld_mapgen_mutex->BeginCritical();
    00229   _genworld_paint_mutex->BeginCritical();
    00230 }
    00231
    00235 void AbortGeneratingWorld()
    00236 {
    00237   _gw.abort = true;
    00238 }
    00239
    00244 bool IsGeneratingWorldAborted()
    00245 {
    00246   return _gw.abort;
    00247 }
    00248
    00252 void HandleGeneratingWorldAbortion()
    00253 {
    00254   /* Clean up - in SE create an empty map, otherwise, go to intro menu */
    00255   _switch_mode = (_game_mode == GM_EDITOR) ? SM_EDITOR : SM_MENU;
    00256
    00257   if (_gw.abortp != NULL) _gw.abortp();
    00258
    00259   CleanupGeneration();
    00260
    00261   if (_gw.thread != NULL) _gw.thread->Exit();
    00262
    00263   extern void SwitchToMode(SwitchMode new_mode);
    00264   SwitchToMode(_switch_mode);
    00265 }
    00266
    00274 void GenerateWorld(GenWorldMode mode, uint size_x, uint size_y, bool reset_settings)
    00275 {
    00276   if (_gw.active) return;
    00277   _gw.mode   = mode;
    00278   _gw.size_x = size_x;
    00279   _gw.size_y = size_y;
    00280   _gw.active = true;
    00281   _gw.abort  = false;
    00282   _gw.abortp = NULL;
    00283   _gw.lc     = _local_company;
    00284   _gw.quit_thread   = false;
    00285   _gw.threaded      = true;
    00286
    00287   /* This disables some commands and stuff */
    00288   SetLocalCompany(COMPANY_SPECTATOR);
    00289   /* Make sure everything is done via OWNER_NONE */
    00290   _current_company = OWNER_NONE;
    00291
    00292   /* Set the date before loading sprites as some newgrfs check it */
    00293   SetDate(ConvertYMDToDate(_settings_game.game_creation.starting_year, 0, 1));
    00294
    00295   InitializeGame(_gw.size_x, _gw.size_y, false, reset_settings);
    00296   PrepareGenerateWorldProgress();
    00297
    00298   /* Load the right landscape stuff */
    00299   GfxLoadSprites();
    00300   LoadStringWidthTable();
    00301
    00302   /* Re-init the windowing system */
    00303   ResetWindowSystem();
    00304
    00305   /* Create toolbars */
    00306   SetupColoursAndInitialWindow();
    00307   SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);
    00308
    00309   if (_gw.thread != NULL) {
    00310     _gw.thread->Join();
    00311     delete _gw.thread;
    00312     _gw.thread = NULL;
    00313   }
    00314
    00315   if (BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() == 0 ||
    00316       !ThreadObject::New(&_GenerateWorld, NULL, &_gw.thread)) {
    00317     DEBUG(misc, 1, "Cannot create genworld thread, reverting to single-threaded mode");
    00318     _gw.threaded = false;
    00319     _genworld_mapgen_mutex->EndCritical();
    00320     _GenerateWorld(NULL);
    00321     _genworld_mapgen_mutex->BeginCritical();
    00322     return;
    00323   }
    00324
    00325   /* Remove any open window */
    00326   DeleteAllNonVitalWindows();
    00327   /* Hide vital windows, because we don't allow to use them */
    00328   HideVitalWindows();
    00329
    00330   /* Don't show the dialog if we don't have a thread */
    00331   ShowGenerateWorldProgress();
    00332
    00333   /* Centre the view on the map */
    00334   if (FindWindowById(WC_MAIN_WINDOW, 0) != NULL) {
    00335     ScrollMainWindowToTile(TileXY(MapSizeX() / 2, MapSizeY() / 2), true);
    00336   }
    00337 }