# genworld.cpp

Go to the documentation of this file.

    00001 /* $Id: genworld.cpp 17158 2009-08-12 15:22:50Z 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, bool reset_settings);
    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
    00051 ThreadMutex *_genworld_mapgen_mutex = ThreadMutex::New();
    00053 ThreadMutex *_genworld_paint_mutex = ThreadMutex::New();
    00054
    00058 bool IsGenerateWorldThreaded()
    00059 {
    00060   return _gw.threaded && !_gw.quit_thread;
    00061 }
    00062
    00067 static void CleanupGeneration()
    00068 {
    00069   _generating_world = false;
    00070
    00071   if (_cursor.sprite == SPR_CURSOR_ZZZ) SetMouseCursor(SPR_CURSOR_MOUSE, PAL_NONE);
    00072   /* Show all vital windows again, because we have hidden them */
    00073   if (_gw.threaded && _game_mode != GM_MENU) ShowVitalWindows();
    00074   _gw.active   = false;
    00075   _gw.proc     = NULL;
    00076   _gw.abortp   = NULL;
    00077   _gw.threaded = false;
    00078
    00079   DeleteWindowById(WC_GENERATE_PROGRESS_WINDOW, 0);
    00080   MarkWholeScreenDirty();
    00081   _genworld_mapgen_mutex->EndCritical();
    00082 }
    00083
    00087 static void _GenerateWorld(void *arg)
    00088 {
    00089   try {
    00090     _generating_world = true;
    00091     _genworld_mapgen_mutex->BeginCritical();
    00092     if (_network_dedicated) DEBUG(net, 0, "Generating map, please wait...");
    00093     /* Set the Random() seed to generation_seed so we produce the same map with the same seed */
    00094     if (_settings_game.game_creation.generation_seed == GENERATE_NEW_SEED) _settings_game.game_creation.generation_seed = _settings_newgame.game_creation.generation_seed = InteractiveRandom();
    00095     _random.SetSeed(_settings_game.game_creation.generation_seed);
    00096     SetGeneratingWorldProgress(GWP_MAP_INIT, 2);
    00097     SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, VHM_NONE, WC_MAIN_WINDOW, 0);
    00098
    00099     IncreaseGeneratingWorldProgress(GWP_MAP_INIT);
    00100     /* Must start economy early because of the costs. */
    00101     StartupEconomy();
    00102
    00103     /* Don't generate landscape items when in the scenario editor. */
    00104     if (_gw.mode == GW_EMPTY) {
    00105       SetGeneratingWorldProgress(GWP_UNMOVABLE, 1);
    00106
    00107       /* Make sure the tiles at the north border are void tiles if needed. */
    00108       if (_settings_game.construction.freeform_edges) {
    00109         for (uint row = 0; row < MapSizeY(); row++) MakeVoid(TileXY(0, row));
    00110         for (uint col = 0; col < MapSizeX(); col++) MakeVoid(TileXY(col, 0));
    00111       }
    00112
    00113       /* Make the map the height of the setting */
    00114       if (_game_mode != GM_MENU) FlatEmptyWorld(_settings_game.game_creation.se_flat_world_height);
    00115
    00116       ConvertGroundTilesIntoWaterTiles();
    00117       IncreaseGeneratingWorldProgress(GWP_UNMOVABLE);
    00118     } else {
    00119       GenerateLandscape(_gw.mode);
    00120       GenerateClearTile();
    00121
    00122       /* only generate towns, tree and industries in newgame mode. */
    00123       if (_game_mode != GM_EDITOR) {
    00124         if (!GenerateTowns(_settings_game.economy.town_layout)) {
    00125           HandleGeneratingWorldAbortion();
    00126           return;
    00127         }
    00128         GenerateIndustries();
    00129         GenerateUnmovables();
    00130         GenerateTrees();
    00131       }
    00132     }
    00133
    00134     ClearStorageChanges(true);
    00135
    00136     /* These are probably pointless when inside the scenario editor. */
    00137     SetGeneratingWorldProgress(GWP_GAME_INIT, 3);
    00138     StartupCompanies();
    00139     IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
    00140     StartupEngines();
    00141     IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
    00142     StartupDisasters();
    00143     _generating_world = false;
    00144
    00145     /* No need to run the tile loop in the scenario editor. */
    00146     if (_gw.mode != GW_EMPTY) {
    00147       uint i;
    00148
    00149       SetGeneratingWorldProgress(GWP_RUNTILELOOP, 0x500);
    00150       for (i = 0; i < 0x500; i++) {
    00151         RunTileLoop();
    00152         IncreaseGeneratingWorldProgress(GWP_RUNTILELOOP);
    00153       }
    00154     }
    00155
    00156     ResetObjectToPlace();
    00157     _local_company = _gw.lc;
    00158
    00159     SetGeneratingWorldProgress(GWP_GAME_START, 1);
    00160     /* Call any callback */
    00161     if (_gw.proc != NULL) _gw.proc();
    00162     IncreaseGeneratingWorldProgress(GWP_GAME_START);
    00163
    00164     CleanupGeneration();
    00165
    00166     if (_network_dedicated) DEBUG(net, 0, "Map generated, starting game");
    00167     DEBUG(desync, 1, "new_map: %i\n", _settings_game.game_creation.generation_seed);
    00168
    00169     if (_settings_client.gui.pause_on_newgame && _game_mode == GM_NORMAL) DoCommandP(0, 1, 0, CMD_PAUSE);
    00170     if (_debug_desync_level > 0) {
    00171       char name[MAX_PATH];
    00172       snprintf(name, lengthof(name), "dmp_cmds_%08x_%08x.sav", _settings_game.game_creation.generation_seed, _date);
    00173       SaveOrLoad(name, SL_SAVE, AUTOSAVE_DIR);
    00174     }
    00175   } catch (...) {
    00176     _generating_world = false;
    00177     _genworld_mapgen_mutex->EndCritical();
    00178     throw;
    00179   }
    00180 }
    00181
    00186 void GenerateWorldSetCallback(gw_done_proc *proc)
    00187 {
    00188   _gw.proc = proc;
    00189 }
    00190
    00195 void GenerateWorldSetAbortCallback(gw_abort_proc *proc)
    00196 {
    00197   _gw.abortp = proc;
    00198 }
    00199
    00204 void WaitTillGeneratedWorld()
    00205 {
    00206   if (_gw.thread == NULL) return;
    00207
    00208   _genworld_mapgen_mutex->EndCritical();
    00209   _genworld_paint_mutex->EndCritical();
    00210   _gw.quit_thread = true;
    00211   _gw.thread->Join();
    00212   delete _gw.thread;
    00213   _gw.thread   = NULL;
    00214   _gw.threaded = false;
    00215   _genworld_mapgen_mutex->BeginCritical();
    00216   _genworld_paint_mutex->BeginCritical();
    00217 }
    00218
    00222 void AbortGeneratingWorld()
    00223 {
    00224   _gw.abort = true;
    00225 }
    00226
    00230 bool IsGeneratingWorldAborted()
    00231 {
    00232   return _gw.abort;
    00233 }
    00234
    00238 void HandleGeneratingWorldAbortion()
    00239 {
    00240   /* Clean up - in SE create an empty map, otherwise, go to intro menu */
    00241   _switch_mode = (_game_mode == GM_EDITOR) ? SM_EDITOR : SM_MENU;
    00242
    00243   if (_gw.abortp != NULL) _gw.abortp();
    00244
    00245   CleanupGeneration();
    00246
    00247   if (_gw.thread != NULL) _gw.thread->Exit();
    00248
    00249   extern void SwitchToMode(SwitchMode new_mode);
    00250   SwitchToMode(_switch_mode);
    00251 }
    00252
    00259 void GenerateWorld(GenerateWorldMode mode, uint size_x, uint size_y, bool reset_settings)
    00260 {
    00261   if (_gw.active) return;
    00262   _gw.mode   = mode;
    00263   _gw.size_x = size_x;
    00264   _gw.size_y = size_y;
    00265   _gw.active = true;
    00266   _gw.abort  = false;
    00267   _gw.abortp = NULL;
    00268   _gw.lc     = _local_company;
    00269   _gw.quit_thread   = false;
    00270   _gw.threaded      = true;
    00271
    00272   /* This disables some commands and stuff */
    00273   SetLocalCompany(COMPANY_SPECTATOR);
    00274   /* Make sure everything is done via OWNER_NONE */
    00275   _current_company = OWNER_NONE;
    00276
    00277   /* Set the date before loading sprites as some newgrfs check it */
    00278   SetDate(ConvertYMDToDate(_settings_game.game_creation.starting_year, 0, 1));
    00279
    00280   /* Load the right landscape stuff */
    00281   GfxLoadSprites();
    00282   LoadStringWidthTable();
    00283
    00284   InitializeGame(_gw.size_x, _gw.size_y, false, reset_settings);
    00285   PrepareGenerateWorldProgress();
    00286
    00287   /* Re-init the windowing system */
    00288   ResetWindowSystem();
    00289
    00290   /* Create toolbars */
    00291   SetupColoursAndInitialWindow();
    00292
    00293   if (_gw.thread != NULL) {
    00294     _gw.thread->Join();
    00295     delete _gw.thread;
    00296     _gw.thread = NULL;
    00297   }
    00298
    00299   if (BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() == 0 ||
    00300       !ThreadObject::New(&_GenerateWorld, NULL, &_gw.thread)) {
    00301     DEBUG(misc, 1, "Cannot create genworld thread, reverting to single-threaded mode");
    00302     _gw.threaded = false;
    00303     _genworld_mapgen_mutex->EndCritical();
    00304     _GenerateWorld(NULL);
    00305     _genworld_mapgen_mutex->BeginCritical();
    00306     return;
    00307   }
    00308
    00309   /* Remove any open window */
    00310   DeleteAllNonVitalWindows();
    00311   /* Hide vital windows, because we don't allow to use them */
    00312   HideVitalWindows();
    00313
    00314   /* Don't show the dialog if we don't have a thread */
    00315   ShowGenerateWorldProgress();
    00316
    00317   /* Centre the view on the map */
    00318   if (FindWindowById(WC_MAIN_WINDOW, 0) != NULL) {
    00319     ScrollMainWindowToTile(TileXY(MapSizeX() / 2, MapSizeY() / 2), true);
    00320   }
    00321 }