genworld.cpp

Go to the documentation of this file.
00001 /* $Id: genworld.cpp 19603 2010-04-11 10:57:21Z 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 (_settings_client.gui.pause_on_newgame && _game_mode == GM_NORMAL) DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE);
00182     if (_debug_desync_level > 0) {
00183       char name[MAX_PATH];
00184       snprintf(name, lengthof(name), "dmp_cmds_%08x_%08x.sav", _settings_game.game_creation.generation_seed, _date);
00185       SaveOrLoad(name, SL_SAVE, AUTOSAVE_DIR);
00186     }
00187   } catch (...) {
00188     _generating_world = false;
00189     _genworld_mapgen_mutex->EndCritical();
00190     throw;
00191   }
00192 }
00193 
00199 void GenerateWorldSetCallback(GWDoneProc *proc)
00200 {
00201   _gw.proc = proc;
00202 }
00203 
00209 void GenerateWorldSetAbortCallback(GWAbortProc *proc)
00210 {
00211   _gw.abortp = proc;
00212 }
00213 
00218 void WaitTillGeneratedWorld()
00219 {
00220   if (_gw.thread == NULL) return;
00221 
00222   _genworld_mapgen_mutex->EndCritical();
00223   _genworld_paint_mutex->EndCritical();
00224   _gw.quit_thread = true;
00225   _gw.thread->Join();
00226   delete _gw.thread;
00227   _gw.thread   = NULL;
00228   _gw.threaded = false;
00229   _genworld_mapgen_mutex->BeginCritical();
00230   _genworld_paint_mutex->BeginCritical();
00231 }
00232 
00236 void AbortGeneratingWorld()
00237 {
00238   _gw.abort = true;
00239 }
00240 
00245 bool IsGeneratingWorldAborted()
00246 {
00247   return _gw.abort;
00248 }
00249 
00253 void HandleGeneratingWorldAbortion()
00254 {
00255   /* Clean up - in SE create an empty map, otherwise, go to intro menu */
00256   _switch_mode = (_game_mode == GM_EDITOR) ? SM_EDITOR : SM_MENU;
00257 
00258   if (_gw.abortp != NULL) _gw.abortp();
00259 
00260   CleanupGeneration();
00261 
00262   if (_gw.thread != NULL) _gw.thread->Exit();
00263 
00264   extern void SwitchToMode(SwitchMode new_mode);
00265   SwitchToMode(_switch_mode);
00266 }
00267 
00275 void GenerateWorld(GenWorldMode mode, uint size_x, uint size_y, bool reset_settings)
00276 {
00277   if (_gw.active) return;
00278   _gw.mode   = mode;
00279   _gw.size_x = size_x;
00280   _gw.size_y = size_y;
00281   _gw.active = true;
00282   _gw.abort  = false;
00283   _gw.abortp = NULL;
00284   _gw.lc     = _local_company;
00285   _gw.quit_thread   = false;
00286   _gw.threaded      = true;
00287 
00288   /* This disables some commands and stuff */
00289   SetLocalCompany(COMPANY_SPECTATOR);
00290   /* Make sure everything is done via OWNER_NONE */
00291   _current_company = OWNER_NONE;
00292 
00293   /* Set the date before loading sprites as some newgrfs check it */
00294   SetDate(ConvertYMDToDate(_settings_game.game_creation.starting_year, 0, 1));
00295 
00296   InitializeGame(_gw.size_x, _gw.size_y, false, reset_settings);
00297   PrepareGenerateWorldProgress();
00298 
00299   /* Load the right landscape stuff */
00300   GfxLoadSprites();
00301   LoadStringWidthTable();
00302 
00303   /* Re-init the windowing system */
00304   ResetWindowSystem();
00305 
00306   /* Create toolbars */
00307   SetupColoursAndInitialWindow();
00308   SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);
00309 
00310   if (_gw.thread != NULL) {
00311     _gw.thread->Join();
00312     delete _gw.thread;
00313     _gw.thread = NULL;
00314   }
00315 
00316   if (BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() == 0 ||
00317       !ThreadObject::New(&_GenerateWorld, NULL, &_gw.thread)) {
00318     DEBUG(misc, 1, "Cannot create genworld thread, reverting to single-threaded mode");
00319     _gw.threaded = false;
00320     _genworld_mapgen_mutex->EndCritical();
00321     _GenerateWorld(NULL);
00322     _genworld_mapgen_mutex->BeginCritical();
00323     return;
00324   }
00325 
00326   /* Remove any open window */
00327   DeleteAllNonVitalWindows();
00328   /* Hide vital windows, because we don't allow to use them */
00329   HideVitalWindows();
00330 
00331   /* Don't show the dialog if we don't have a thread */
00332   ShowGenerateWorldProgress();
00333 
00334   /* Centre the view on the map */
00335   if (FindWindowById(WC_MAIN_WINDOW, 0) != NULL) {
00336     ScrollMainWindowToTile(TileXY(MapSizeX() / 2, MapSizeY() / 2), true);
00337   }
00338 }

Generated on Sat Apr 17 23:24:48 2010 for OpenTTD by  doxygen 1.6.1