# genworld.cpp

Go to the documentation of this file.

    00001 /* $Id: genworld.cpp 15689 2009-03-12 15:14:22Z rubidium $ */
    00002
    00005 #include "stdafx.h"
    00006 #include "openttd.h"
    00007 #include "landscape.h"
    00008 #include "company_func.h"
    00009 #include "variables.h"
    00010 #include "thread.h"
    00011 #include "command_func.h"
    00012 #include "genworld.h"
    00013 #include "gfxinit.h"
    00014 #include "window_func.h"
    00015 #include "network/network.h"
    00016 #include "heightmap.h"
    00017 #include "viewport_func.h"
    00018 #include "gfx_func.h"
    00019 #include "date_func.h"
    00020 #include "core/random_func.hpp"
    00021 #include "engine_func.h"
    00022 #include "newgrf_storage.h"
    00023 #include "water.h"
    00024 #include "blitter/factory.hpp"
    00025 #include "tilehighlight_func.h"
    00026 #include "saveload/saveload.h"
    00027 #include "void_map.h"
    00028 #include "settings_type.h"
    00029 #include "town.h"
    00030
    00031 #include "table/sprites.h"
    00032
    00033 void GenerateClearTile();
    00034 void GenerateIndustries();
    00035 void GenerateUnmovables();
    00036 void GenerateTrees();
    00037
    00038 void StartupEconomy();
    00039 void StartupCompanies();
    00040 void StartupDisasters();
    00041
    00042 void InitializeGame(uint size_x, uint size_y, bool reset_date);
    00043
    00044 /* Please only use this variable in genworld.h and genworld.c and
    00045  *  nowhere else. For speed improvements we need it to be global, but
    00046  *  in no way the meaning of it is to use it anywhere else besides
    00047  *  in the genworld.h and genworld.c! -- TrueLight */
    00048 gw_info _gw;
    00049
    00057 void SetGeneratingWorldPaintStatus(bool status)
    00058 {
    00059   _gw.wait_for_draw = status;
    00060 }
    00061
    00068 bool IsGeneratingWorldReadyForPaint()
    00069 {
    00070   /* If we are in quit_thread mode, ignore this and always return false. This
    00071    *  forces the screen to not be drawn, and the GUI not to wait for a draw. */
    00072   if (!_gw.active || _gw.quit_thread || !_gw.threaded) return false;
    00073
    00074   return _gw.wait_for_draw;
    00075 }
    00076
    00080 bool IsGenerateWorldThreaded()
    00081 {
    00082   return _gw.threaded && !_gw.quit_thread;
    00083 }
    00084
    00089 static void CleanupGeneration()
    00090 {
    00091   _generating_world = false;
    00092
    00093   if (_cursor.sprite == SPR_CURSOR_ZZZ) SetMouseCursor(SPR_CURSOR_MOUSE, PAL_NONE);
    00094   /* Show all vital windows again, because we have hidden them */
    00095   if (_gw.threaded && _game_mode != GM_MENU) ShowVitalWindows();
    00096   _gw.active   = false;
    00097   _gw.proc     = NULL;
    00098   _gw.abortp   = NULL;
    00099   _gw.threaded = false;
    00100
    00101   DeleteWindowById(WC_GENERATE_PROGRESS_WINDOW, 0);
    00102   MarkWholeScreenDirty();
    00103 }
    00104
    00108 static void _GenerateWorld(void *arg)
    00109 {
    00110   try {
    00111     _generating_world = true;
    00112     if (_network_dedicated) DEBUG(net, 0, "Generating map, please wait...");
    00113     /* Set the Random() seed to generation_seed so we produce the same map with the same seed */
    00114     if (_settings_game.game_creation.generation_seed == GENERATE_NEW_SEED) _settings_game.game_creation.generation_seed = _settings_newgame.game_creation.generation_seed = InteractiveRandom();
    00115     _random.SetSeed(_settings_game.game_creation.generation_seed);
    00116     SetGeneratingWorldProgress(GWP_MAP_INIT, 2);
    00117     SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, VHM_NONE, WC_MAIN_WINDOW, 0);
    00118
    00119     IncreaseGeneratingWorldProgress(GWP_MAP_INIT);
    00120     /* Must start economy early because of the costs. */
    00121     StartupEconomy();
    00122
    00123     /* Don't generate landscape items when in the scenario editor. */
    00124     if (_gw.mode == GW_EMPTY) {
    00125       SetGeneratingWorldProgress(GWP_UNMOVABLE, 1);
    00126
    00127       /* Make sure the tiles at the north border are void tiles if needed. */
    00128       if (_settings_game.construction.freeform_edges) {
    00129         for (uint row = 0; row < MapSizeY(); row++) MakeVoid(TileXY(0, row));
    00130         for (uint col = 0; col < MapSizeX(); col++) MakeVoid(TileXY(col, 0));
    00131       }
    00132
    00133       /* Make the map the height of the setting */
    00134       if (_game_mode != GM_MENU) FlatEmptyWorld(_settings_game.game_creation.se_flat_world_height);
    00135
    00136       ConvertGroundTilesIntoWaterTiles();
    00137       IncreaseGeneratingWorldProgress(GWP_UNMOVABLE);
    00138     } else {
    00139       GenerateLandscape(_gw.mode);
    00140       GenerateClearTile();
    00141
    00142       /* only generate towns, tree and industries in newgame mode. */
    00143       if (_game_mode != GM_EDITOR) {
    00144         if (!GenerateTowns(_settings_game.economy.town_layout)) {
    00145           HandleGeneratingWorldAbortion();
    00146           return;
    00147         }
    00148         GenerateIndustries();
    00149         GenerateUnmovables();
    00150         GenerateTrees();
    00151       }
    00152     }
    00153
    00154     ClearStorageChanges(true);
    00155
    00156     /* These are probably pointless when inside the scenario editor. */
    00157     SetGeneratingWorldProgress(GWP_GAME_INIT, 3);
    00158     StartupCompanies();
    00159     IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
    00160     StartupEngines();
    00161     IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
    00162     StartupDisasters();
    00163     _generating_world = false;
    00164
    00165     /* No need to run the tile loop in the scenario editor. */
    00166     if (_gw.mode != GW_EMPTY) {
    00167       uint i;
    00168
    00169       SetGeneratingWorldProgress(GWP_RUNTILELOOP, 0x500);
    00170       for (i = 0; i < 0x500; i++) {
    00171         RunTileLoop();
    00172         IncreaseGeneratingWorldProgress(GWP_RUNTILELOOP);
    00173       }
    00174     }
    00175
    00176     ResetObjectToPlace();
    00177     _local_company = _gw.lc;
    00178
    00179     SetGeneratingWorldProgress(GWP_GAME_START, 1);
    00180     /* Call any callback */
    00181     if (_gw.proc != NULL) _gw.proc();
    00182     IncreaseGeneratingWorldProgress(GWP_GAME_START);
    00183
    00184     CleanupGeneration();
    00185
    00186     if (_network_dedicated) DEBUG(net, 0, "Map generated, starting game");
    00187     DEBUG(desync, 1, "new_map: %i\n", _settings_game.game_creation.generation_seed);
    00188
    00189     if (_settings_client.gui.pause_on_newgame && _game_mode == GM_NORMAL) DoCommandP(0, 1, 0, CMD_PAUSE);
    00190     if (_debug_desync_level > 0) {
    00191       char name[MAX_PATH];
    00192       snprintf(name, lengthof(name), "dmp_cmds_%08x_%08x.sav", _settings_game.game_creation.generation_seed, _date);
    00193       SaveOrLoad(name, SL_SAVE, AUTOSAVE_DIR);
    00194     }
    00195   } catch (...) {
    00196     _generating_world = false;
    00197     throw;
    00198   }
    00199 }
    00200
    00205 void GenerateWorldSetCallback(gw_done_proc *proc)
    00206 {
    00207   _gw.proc = proc;
    00208 }
    00209
    00214 void GenerateWorldSetAbortCallback(gw_abort_proc *proc)
    00215 {
    00216   _gw.abortp = proc;
    00217 }
    00218
    00223 void WaitTillGeneratedWorld()
    00224 {
    00225   if (_gw.thread == NULL) return;
    00226   _gw.quit_thread = true;
    00227   _gw.thread->Join();
    00228   delete _gw.thread;
    00229   _gw.thread   = NULL;
    00230   _gw.threaded = false;
    00231 }
    00232
    00236 void AbortGeneratingWorld()
    00237 {
    00238   _gw.abort = true;
    00239 }
    00240
    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
    00273 void GenerateWorld(GenerateWorldMode mode, uint size_x, uint size_y)
    00274 {
    00275   if (_gw.active) return;
    00276   _gw.mode   = mode;
    00277   _gw.size_x = size_x;
    00278   _gw.size_y = size_y;
    00279   _gw.active = true;
    00280   _gw.abort  = false;
    00281   _gw.abortp = NULL;
    00282   _gw.lc     = _local_company;
    00283   _gw.wait_for_draw = false;
    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   /* Load the right landscape stuff */
    00296   GfxLoadSprites();
    00297   LoadStringWidthTable();
    00298
    00299   InitializeGame(_gw.size_x, _gw.size_y, false);
    00300   PrepareGenerateWorldProgress();
    00301
    00302   /* Re-init the windowing system */
    00303   ResetWindowSystem();
    00304
    00305   /* Create toolbars */
    00306   SetupColoursAndInitialWindow();
    00307
    00308   if (_gw.thread != NULL) {
    00309     _gw.thread->Join();
    00310     delete _gw.thread;
    00311     _gw.thread = NULL;
    00312   }
    00313
    00314   if (BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() == 0 ||
    00315       !ThreadObject::New(&_GenerateWorld, NULL, &_gw.thread)) {
    00316     DEBUG(misc, 1, "Cannot create genworld thread, reverting to single-threaded mode");
    00317     _gw.threaded = false;
    00318     _GenerateWorld(NULL);
    00319     return;
    00320   }
    00321
    00322   /* Remove any open window */
    00323   DeleteAllNonVitalWindows();
    00324   /* Hide vital windows, because we don't allow to use them */
    00325   HideVitalWindows();
    00326
    00327   /* Don't show the dialog if we don't have a thread */
    00328   ShowGenerateWorldProgress();
    00329
    00330   /* Centre the view on the map */
    00331   if (FindWindowById(WC_MAIN_WINDOW, 0) != NULL) {
    00332     ScrollMainWindowToTile(TileXY(MapSizeX() / 2, MapSizeY() / 2), true);
    00333   }
    00334 }