genworld.cpp

Go to the documentation of this file.
00001 /* $Id: genworld.cpp 21833 2011-01-18 18:14:14Z 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 "thread/thread.h"
00017 #include "genworld.h"
00018 #include "gfxinit.h"
00019 #include "window_func.h"
00020 #include "network/network.h"
00021 #include "heightmap.h"
00022 #include "viewport_func.h"
00023 #include "gfx_func.h"
00024 #include "date_func.h"
00025 #include "engine_func.h"
00026 #include "newgrf_storage.h"
00027 #include "water.h"
00028 #include "blitter/factory.hpp"
00029 #include "tilehighlight_func.h"
00030 #include "saveload/saveload.h"
00031 #include "void_map.h"
00032 #include "town.h"
00033 #include "newgrf.h"
00034 #include "core/random_func.hpp"
00035 #include "core/backup_type.hpp"
00036 
00037 #include "table/sprites.h"
00038 
00039 void GenerateClearTile();
00040 void GenerateIndustries();
00041 void GenerateObjects();
00042 void GenerateTrees();
00043 
00044 void StartupEconomy();
00045 void StartupCompanies();
00046 void StartupDisasters();
00047 
00048 void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settings);
00049 
00056 GenWorldInfo _gw;
00057 
00059 ThreadMutex *_genworld_mapgen_mutex = ThreadMutex::New();
00061 ThreadMutex *_genworld_paint_mutex = ThreadMutex::New();
00062 
00064 bool _generating_world;
00065 
00070 bool IsGenerateWorldThreaded()
00071 {
00072   return _gw.threaded && !_gw.quit_thread;
00073 }
00074 
00079 static void CleanupGeneration()
00080 {
00081   _generating_world = false;
00082 
00083   if (_cursor.sprite == SPR_CURSOR_ZZZ) SetMouseCursor(SPR_CURSOR_MOUSE, PAL_NONE);
00084   /* Show all vital windows again, because we have hidden them */
00085   if (_gw.threaded && _game_mode != GM_MENU) ShowVitalWindows();
00086   _gw.active   = false;
00087   _gw.proc     = NULL;
00088   _gw.abortp   = NULL;
00089   _gw.threaded = false;
00090 
00091   DeleteWindowById(WC_GENERATE_PROGRESS_WINDOW, 0);
00092   MarkWholeScreenDirty();
00093 }
00094 
00098 static void _GenerateWorld(void *)
00099 {
00100   /* Make sure everything is done via OWNER_NONE. */
00101   Backup<CompanyByte> _cur_company(_current_company, OWNER_NONE, FILE_LINE);
00102 
00103   try {
00104     _generating_world = true;
00105     _genworld_mapgen_mutex->BeginCritical();
00106     if (_network_dedicated) DEBUG(net, 0, "Generating map, please wait...");
00107     /* Set the Random() seed to generation_seed so we produce the same map with the same seed */
00108     if (_settings_game.game_creation.generation_seed == GENERATE_NEW_SEED) _settings_game.game_creation.generation_seed = _settings_newgame.game_creation.generation_seed = InteractiveRandom();
00109     _random.SetSeed(_settings_game.game_creation.generation_seed);
00110     SetGeneratingWorldProgress(GWP_MAP_INIT, 2);
00111     SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);
00112 
00113     IncreaseGeneratingWorldProgress(GWP_MAP_INIT);
00114     /* Must start economy early because of the costs. */
00115     StartupEconomy();
00116 
00117     /* Don't generate landscape items when in the scenario editor. */
00118     if (_gw.mode == GWM_EMPTY) {
00119       SetGeneratingWorldProgress(GWP_OBJECT, 1);
00120 
00121       /* Make sure the tiles at the north border are void tiles if needed. */
00122       if (_settings_game.construction.freeform_edges) {
00123         for (uint row = 0; row < MapSizeY(); row++) MakeVoid(TileXY(0, row));
00124         for (uint col = 0; col < MapSizeX(); col++) MakeVoid(TileXY(col, 0));
00125       }
00126 
00127       /* Make the map the height of the setting */
00128       if (_game_mode != GM_MENU) FlatEmptyWorld(_settings_game.game_creation.se_flat_world_height);
00129 
00130       ConvertGroundTilesIntoWaterTiles();
00131       IncreaseGeneratingWorldProgress(GWP_OBJECT);
00132     } else {
00133       GenerateLandscape(_gw.mode);
00134       GenerateClearTile();
00135 
00136       /* only generate towns, tree and industries in newgame mode. */
00137       if (_game_mode != GM_EDITOR) {
00138         if (!GenerateTowns(_settings_game.economy.town_layout)) {
00139           _cur_company.Restore();
00140           HandleGeneratingWorldAbortion();
00141           return;
00142         }
00143         GenerateIndustries();
00144         GenerateObjects();
00145         GenerateTrees();
00146       }
00147     }
00148 
00149     ClearStorageChanges(true);
00150 
00151     /* These are probably pointless when inside the scenario editor. */
00152     SetGeneratingWorldProgress(GWP_GAME_INIT, 3);
00153     StartupCompanies();
00154     IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
00155     StartupEngines();
00156     IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
00157     StartupDisasters();
00158     _generating_world = false;
00159 
00160     /* No need to run the tile loop in the scenario editor. */
00161     if (_gw.mode != GWM_EMPTY) {
00162       uint i;
00163 
00164       SetGeneratingWorldProgress(GWP_RUNTILELOOP, 0x500);
00165       for (i = 0; i < 0x500; i++) {
00166         RunTileLoop();
00167         IncreaseGeneratingWorldProgress(GWP_RUNTILELOOP);
00168       }
00169     }
00170 
00171     ResetObjectToPlace();
00172     _cur_company.Trash();
00173     _current_company = _local_company = _gw.lc;
00174 
00175     SetGeneratingWorldProgress(GWP_GAME_START, 1);
00176     /* Call any callback */
00177     if (_gw.proc != NULL) _gw.proc();
00178     IncreaseGeneratingWorldProgress(GWP_GAME_START);
00179 
00180     CleanupGeneration();
00181     _genworld_mapgen_mutex->EndCritical();
00182 
00183     ShowNewGRFError();
00184 
00185     if (_network_dedicated) DEBUG(net, 0, "Map generated, starting game");
00186     DEBUG(desync, 1, "new_map: %08x", _settings_game.game_creation.generation_seed);
00187 
00188     if (_debug_desync_level > 0) {
00189       char name[MAX_PATH];
00190       snprintf(name, lengthof(name), "dmp_cmds_%08x_%08x.sav", _settings_game.game_creation.generation_seed, _date);
00191       SaveOrLoad(name, SL_SAVE, AUTOSAVE_DIR, false);
00192     }
00193   } catch (...) {
00194     if (_cur_company.IsValid()) _cur_company.Restore();
00195     _generating_world = false;
00196     _genworld_mapgen_mutex->EndCritical();
00197     throw;
00198   }
00199 }
00200 
00206 void GenerateWorldSetCallback(GWDoneProc *proc)
00207 {
00208   _gw.proc = proc;
00209 }
00210 
00216 void GenerateWorldSetAbortCallback(GWAbortProc *proc)
00217 {
00218   _gw.abortp = proc;
00219 }
00220 
00225 void WaitTillGeneratedWorld()
00226 {
00227   if (_gw.thread == NULL) return;
00228 
00229   _genworld_mapgen_mutex->EndCritical();
00230   _genworld_paint_mutex->EndCritical();
00231   _gw.quit_thread = true;
00232   _gw.thread->Join();
00233   delete _gw.thread;
00234   _gw.thread   = NULL;
00235   _gw.threaded = false;
00236   _genworld_mapgen_mutex->BeginCritical();
00237   _genworld_paint_mutex->BeginCritical();
00238 }
00239 
00243 void AbortGeneratingWorld()
00244 {
00245   _gw.abort = true;
00246 }
00247 
00252 bool IsGeneratingWorldAborted()
00253 {
00254   return _gw.abort;
00255 }
00256 
00260 void HandleGeneratingWorldAbortion()
00261 {
00262   /* Clean up - in SE create an empty map, otherwise, go to intro menu */
00263   _switch_mode = (_game_mode == GM_EDITOR) ? SM_EDITOR : SM_MENU;
00264 
00265   if (_gw.abortp != NULL) _gw.abortp();
00266 
00267   CleanupGeneration();
00268 
00269   if (_gw.thread != NULL) _gw.thread->Exit();
00270 
00271   extern void SwitchToMode(SwitchMode new_mode);
00272   SwitchToMode(_switch_mode);
00273 }
00274 
00282 void GenerateWorld(GenWorldMode mode, uint size_x, uint size_y, bool reset_settings)
00283 {
00284   if (_gw.active) return;
00285   _gw.mode   = mode;
00286   _gw.size_x = size_x;
00287   _gw.size_y = size_y;
00288   _gw.active = true;
00289   _gw.abort  = false;
00290   _gw.abortp = NULL;
00291   _gw.lc     = _local_company;
00292   _gw.quit_thread   = false;
00293   _gw.threaded      = true;
00294 
00295   /* This disables some commands and stuff */
00296   SetLocalCompany(COMPANY_SPECTATOR);
00297 
00298   InitializeGame(_gw.size_x, _gw.size_y, true, reset_settings);
00299   PrepareGenerateWorldProgress();
00300 
00301   /* Load the right landscape stuff, and the NewGRFs! */
00302   GfxLoadSprites();
00303   LoadStringWidthTable();
00304 
00305   /* Re-init the windowing system */
00306   ResetWindowSystem();
00307 
00308   /* Create toolbars */
00309   SetupColoursAndInitialWindow();
00310   SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);
00311 
00312   if (_gw.thread != NULL) {
00313     _gw.thread->Join();
00314     delete _gw.thread;
00315     _gw.thread = NULL;
00316   }
00317 
00318   if (BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() == 0 ||
00319       !ThreadObject::New(&_GenerateWorld, NULL, &_gw.thread)) {
00320     DEBUG(misc, 1, "Cannot create genworld thread, reverting to single-threaded mode");
00321     _gw.threaded = false;
00322     _genworld_mapgen_mutex->EndCritical();
00323     _GenerateWorld(NULL);
00324     _genworld_mapgen_mutex->BeginCritical();
00325     return;
00326   }
00327 
00328   /* Remove any open window */
00329   DeleteAllNonVitalWindows();
00330   /* Hide vital windows, because we don't allow to use them */
00331   HideVitalWindows();
00332 
00333   /* Don't show the dialog if we don't have a thread */
00334   ShowGenerateWorldProgress();
00335 
00336   /* Centre the view on the map */
00337   if (FindWindowById(WC_MAIN_WINDOW, 0) != NULL) {
00338     ScrollMainWindowToTile(TileXY(MapSizeX() / 2, MapSizeY() / 2), true);
00339   }
00340 }

Generated on Thu Jan 20 22:57:33 2011 for OpenTTD by  doxygen 1.6.1