# genworld.cpp

Go to the documentation of this file.

    00001 /* $Id: genworld.cpp 19938 2010-06-05 21:13:35Z 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   _genworld_mapgen_mutex->EndCritical();
    00092 }
    00093
    00097 static void _GenerateWorld(void *)
    00098 {
    00099   try {
    00100     _generating_world = true;
    00101     _genworld_mapgen_mutex->BeginCritical();
    00102     if (_network_dedicated) DEBUG(net, 0, "Generating map, please wait...");
    00103     /* Set the Random() seed to generation_seed so we produce the same map with the same seed */
    00104     if (_settings_game.game_creation.generation_seed == GENERATE_NEW_SEED) _settings_game.game_creation.generation_seed = _settings_newgame.game_creation.generation_seed = InteractiveRandom();
    00105     _random.SetSeed(_settings_game.game_creation.generation_seed);
    00106     SetGeneratingWorldProgress(GWP_MAP_INIT, 2);
    00107     SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);
    00108
    00109     IncreaseGeneratingWorldProgress(GWP_MAP_INIT);
    00110     /* Must start economy early because of the costs. */
    00111     StartupEconomy();
    00112
    00113     /* Don't generate landscape items when in the scenario editor. */
    00114     if (_gw.mode == GWM_EMPTY) {
    00115       SetGeneratingWorldProgress(GWP_UNMOVABLE, 1);
    00116
    00117       /* Make sure the tiles at the north border are void tiles if needed. */
    00118       if (_settings_game.construction.freeform_edges) {
    00119         for (uint row = 0; row < MapSizeY(); row++) MakeVoid(TileXY(0, row));
    00120         for (uint col = 0; col < MapSizeX(); col++) MakeVoid(TileXY(col, 0));
    00121       }
    00122
    00123       /* Make the map the height of the setting */
    00124       if (_game_mode != GM_MENU) FlatEmptyWorld(_settings_game.game_creation.se_flat_world_height);
    00125
    00126       ConvertGroundTilesIntoWaterTiles();
    00127       IncreaseGeneratingWorldProgress(GWP_UNMOVABLE);
    00128     } else {
    00129       GenerateLandscape(_gw.mode);
    00130       GenerateClearTile();
    00131
    00132       /* only generate towns, tree and industries in newgame mode. */
    00133       if (_game_mode != GM_EDITOR) {
    00134         if (!GenerateTowns(_settings_game.economy.town_layout)) {
    00135           HandleGeneratingWorldAbortion();
    00136           return;
    00137         }
    00138         GenerateIndustries();
    00139         GenerateUnmovables();
    00140         GenerateTrees();
    00141       }
    00142     }
    00143
    00144     ClearStorageChanges(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         IncreaseGeneratingWorldProgress(GWP_RUNTILELOOP);
    00163       }
    00164     }
    00165
    00166     ResetObjectToPlace();
    00167     _local_company = _gw.lc;
    00168
    00169     SetGeneratingWorldProgress(GWP_GAME_START, 1);
    00170     /* Call any callback */
    00171     if (_gw.proc != NULL) _gw.proc();
    00172     IncreaseGeneratingWorldProgress(GWP_GAME_START);
    00173
    00174     CleanupGeneration();
    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 }