# genworld.cpp

Go to the documentation of this file.

    00001 /* $Id: genworld.cpp 15495 2009-02-14 23:17:32Z yexo $ */
    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
    00088 static void _GenerateWorld(void *arg)
    00089 {
    00090   try {
    00091     _generating_world = true;
    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         GenerateTowns(_settings_game.economy.town_layout);
    00125         GenerateIndustries();
    00126         GenerateUnmovables();
    00127         GenerateTrees();
    00128       }
    00129     }
    00130
    00131     ClearStorageChanges(true);
    00132
    00133     /* These are probably pointless when inside the scenario editor. */
    00134     SetGeneratingWorldProgress(GWP_GAME_INIT, 3);
    00135     StartupCompanies();
    00136     IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
    00137     StartupEngines();
    00138     IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
    00139     StartupDisasters();
    00140     _generating_world = false;
    00141
    00142     /* No need to run the tile loop in the scenario editor. */
    00143     if (_gw.mode != GW_EMPTY) {
    00144       uint i;
    00145
    00146       SetGeneratingWorldProgress(GWP_RUNTILELOOP, 0x500);
    00147       for (i = 0; i < 0x500; i++) {
    00148         RunTileLoop();
    00149         IncreaseGeneratingWorldProgress(GWP_RUNTILELOOP);
    00150       }
    00151     }
    00152
    00153     ResetObjectToPlace();
    00154     _local_company = _gw.lc;
    00155
    00156     SetGeneratingWorldProgress(GWP_GAME_START, 1);
    00157     /* Call any callback */
    00158     if (_gw.proc != NULL) _gw.proc();
    00159     IncreaseGeneratingWorldProgress(GWP_GAME_START);
    00160
    00161     if (_cursor.sprite == SPR_CURSOR_ZZZ) SetMouseCursor(SPR_CURSOR_MOUSE, PAL_NONE);
    00162     /* Show all vital windows again, because we have hidden them */
    00163     if (_gw.threaded && _game_mode != GM_MENU) ShowVitalWindows();
    00164     _gw.active   = false;
    00165     _gw.proc     = NULL;
    00166     _gw.threaded = false;
    00167
    00168     DeleteWindowById(WC_GENERATE_PROGRESS_WINDOW, 0);
    00169     MarkWholeScreenDirty();
    00170
    00171     if (_network_dedicated) DEBUG(net, 0, "Map generated, starting game");
    00172     DEBUG(desync, 1, "new_map: %i\n", _settings_game.game_creation.generation_seed);
    00173
    00174     if (_settings_client.gui.pause_on_newgame && _game_mode == GM_NORMAL) DoCommandP(0, 1, 0, CMD_PAUSE);
    00175     if (_debug_desync_level > 0) {
    00176       char name[MAX_PATH];
    00177       snprintf(name, lengthof(name), "dmp_cmds_%08x_%08x.sav", _settings_game.game_creation.generation_seed, _date);
    00178       SaveOrLoad(name, SL_SAVE, AUTOSAVE_DIR);
    00179     }
    00180   } catch (...) {
    00181     _generating_world = false;
    00182     throw;
    00183   }
    00184 }
    00185
    00190 void GenerateWorldSetCallback(gw_done_proc *proc)
    00191 {
    00192   _gw.proc = proc;
    00193 }
    00194
    00199 void GenerateWorldSetAbortCallback(gw_abort_proc *proc)
    00200 {
    00201   _gw.abortp = proc;
    00202 }
    00203
    00208 void WaitTillGeneratedWorld()
    00209 {
    00210   if (_gw.thread == NULL) return;
    00211   _gw.quit_thread = true;
    00212   _gw.thread->Join();
    00213   delete _gw.thread;
    00214   _gw.thread   = NULL;
    00215   _gw.threaded = false;
    00216 }
    00217
    00221 void AbortGeneratingWorld()
    00222 {
    00223   _gw.abort = true;
    00224 }
    00225
    00229 bool IsGeneratingWorldAborted()
    00230 {
    00231   return _gw.abort;
    00232 }
    00233
    00237 void HandleGeneratingWorldAbortion()
    00238 {
    00239   /* Clean up - in SE create an empty map, otherwise, go to intro menu */
    00240   _switch_mode = (_game_mode == GM_EDITOR) ? SM_EDITOR : SM_MENU;
    00241
    00242   if (_gw.abortp != NULL) _gw.abortp();
    00243
    00244   if (_cursor.sprite == SPR_CURSOR_ZZZ) SetMouseCursor(SPR_CURSOR_MOUSE, PAL_NONE);
    00245   /* Show all vital windows again, because we have hidden them */
    00246   if (_gw.threaded && _game_mode != GM_MENU) ShowVitalWindows();
    00247
    00248   _gw.active   = false;
    00249   _gw.proc     = NULL;
    00250   _gw.abortp   = NULL;
    00251   _gw.threaded = false;
    00252
    00253   DeleteWindowById(WC_GENERATE_PROGRESS_WINDOW, 0);
    00254   MarkWholeScreenDirty();
    00255
    00256   _gw.thread->Exit();
    00257 }
    00258
    00265 void GenerateWorld(GenerateWorldMode mode, uint size_x, uint size_y)
    00266 {
    00267   if (_gw.active) return;
    00268   _gw.mode   = mode;
    00269   _gw.size_x = size_x;
    00270   _gw.size_y = size_y;
    00271   _gw.active = true;
    00272   _gw.abort  = false;
    00273   _gw.abortp = NULL;
    00274   _gw.lc     = _local_company;
    00275   _gw.wait_for_draw = false;
    00276   _gw.quit_thread   = false;
    00277   _gw.threaded      = true;
    00278
    00279   /* This disables some commands and stuff */
    00280   SetLocalCompany(COMPANY_SPECTATOR);
    00281   /* Make sure everything is done via OWNER_NONE */
    00282   _current_company = OWNER_NONE;
    00283
    00284   /* Set the date before loading sprites as some newgrfs check it */
    00285   SetDate(ConvertYMDToDate(_settings_game.game_creation.starting_year, 0, 1));
    00286
    00287   /* Load the right landscape stuff */
    00288   GfxLoadSprites();
    00289   LoadStringWidthTable();
    00290
    00291   InitializeGame(_gw.size_x, _gw.size_y, false);
    00292   PrepareGenerateWorldProgress();
    00293
    00294   /* Re-init the windowing system */
    00295   ResetWindowSystem();
    00296
    00297   /* Create toolbars */
    00298   SetupColoursAndInitialWindow();
    00299
    00300   if (_gw.thread != NULL) {
    00301     _gw.thread->Join();
    00302     delete _gw.thread;
    00303     _gw.thread = NULL;
    00304   }
    00305
    00306   if (BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() == 0 ||
    00307       !ThreadObject::New(&_GenerateWorld, NULL, &_gw.thread)) {
    00308     DEBUG(misc, 1, "Cannot create genworld thread, reverting to single-threaded mode");
    00309     _gw.threaded = false;
    00310     _GenerateWorld(NULL);
    00311     return;
    00312   }
    00313
    00314   /* Remove any open window */
    00315   DeleteAllNonVitalWindows();
    00316   /* Hide vital windows, because we don't allow to use them */
    00317   HideVitalWindows();
    00318
    00319   /* Don't show the dialog if we don't have a thread */
    00320   ShowGenerateWorldProgress();
    00321
    00322   /* Centre the view on the map */
    00323   if (FindWindowById(WC_MAIN_WINDOW, 0) != NULL) {
    00324     ScrollMainWindowToTile(TileXY(MapSizeX() / 2, MapSizeY() / 2), true);
    00325   }
    00326 }