genworld.cpp

Go to the documentation of this file.
00001 /* $Id$ */
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 "landscape.h"
00014 #include "company_func.h"
00015 #include "thread/thread.h"
00016 #include "genworld.h"
00017 #include "gfxinit.h"
00018 #include "window_func.h"
00019 #include "network/network.h"
00020 #include "heightmap.h"
00021 #include "viewport_func.h"
00022 #include "gfx_func.h"
00023 #include "date_func.h"
00024 #include "engine_func.h"
00025 #include "newgrf_storage.h"
00026 #include "water.h"
00027 #include "blitter/factory.hpp"
00028 #include "tilehighlight_func.h"
00029 #include "saveload/saveload.h"
00030 #include "void_map.h"
00031 #include "town.h"
00032 #include "newgrf.h"
00033 #include "core/random_func.hpp"
00034 #include "core/backup_type.hpp"
00035 #include "cargodest_func.h"
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         UpdateCargoLinks();
00147       }
00148     }
00149 
00150     ClearStorageChanges(true);
00151 
00152     /* These are probably pointless when inside the scenario editor. */
00153     SetGeneratingWorldProgress(GWP_GAME_INIT, 3);
00154     StartupCompanies();
00155     IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
00156     StartupEngines();
00157     IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
00158     StartupDisasters();
00159     _generating_world = false;
00160 
00161     /* No need to run the tile loop in the scenario editor. */
00162     if (_gw.mode != GWM_EMPTY) {
00163       uint i;
00164 
00165       SetGeneratingWorldProgress(GWP_RUNTILELOOP, 0x500);
00166       for (i = 0; i < 0x500; i++) {
00167         RunTileLoop();
00168         IncreaseGeneratingWorldProgress(GWP_RUNTILELOOP);
00169       }
00170     }
00171 
00172     ResetObjectToPlace();
00173     _cur_company.Trash();
00174     _current_company = _local_company = _gw.lc;
00175 
00176     SetGeneratingWorldProgress(GWP_GAME_START, 1);
00177     /* Call any callback */
00178     if (_gw.proc != NULL) _gw.proc();
00179     IncreaseGeneratingWorldProgress(GWP_GAME_START);
00180 
00181     CleanupGeneration();
00182     _genworld_mapgen_mutex->EndCritical();
00183 
00184     ShowNewGRFError();
00185 
00186     if (_network_dedicated) DEBUG(net, 0, "Map generated, starting game");
00187     DEBUG(desync, 1, "new_map: %08x", _settings_game.game_creation.generation_seed);
00188 
00189     if (_debug_desync_level > 0) {
00190       char name[MAX_PATH];
00191       snprintf(name, lengthof(name), "dmp_cmds_%08x_%08x.sav", _settings_game.game_creation.generation_seed, _date);
00192       SaveOrLoad(name, SL_SAVE, AUTOSAVE_DIR, false);
00193     }
00194   } catch (...) {
00195     if (_cur_company.IsValid()) _cur_company.Restore();
00196     _generating_world = false;
00197     _genworld_mapgen_mutex->EndCritical();
00198     throw;
00199   }
00200 }
00201 
00207 void GenerateWorldSetCallback(GWDoneProc *proc)
00208 {
00209   _gw.proc = proc;
00210 }
00211 
00217 void GenerateWorldSetAbortCallback(GWAbortProc *proc)
00218 {
00219   _gw.abortp = proc;
00220 }
00221 
00226 void WaitTillGeneratedWorld()
00227 {
00228   if (_gw.thread == NULL) return;
00229 
00230   _genworld_mapgen_mutex->EndCritical();
00231   _genworld_paint_mutex->EndCritical();
00232   _gw.quit_thread = true;
00233   _gw.thread->Join();
00234   delete _gw.thread;
00235   _gw.thread   = NULL;
00236   _gw.threaded = false;
00237   _genworld_mapgen_mutex->BeginCritical();
00238   _genworld_paint_mutex->BeginCritical();
00239 }
00240 
00244 void AbortGeneratingWorld()
00245 {
00246   _gw.abort = true;
00247 }
00248 
00253 bool IsGeneratingWorldAborted()
00254 {
00255   return _gw.abort;
00256 }
00257 
00261 void HandleGeneratingWorldAbortion()
00262 {
00263   /* Clean up - in SE create an empty map, otherwise, go to intro menu */
00264   _switch_mode = (_game_mode == GM_EDITOR) ? SM_EDITOR : SM_MENU;
00265 
00266   if (_gw.abortp != NULL) _gw.abortp();
00267 
00268   CleanupGeneration();
00269 
00270   if (_gw.thread != NULL) _gw.thread->Exit();
00271 
00272   extern void SwitchToMode(SwitchMode new_mode);
00273   SwitchToMode(_switch_mode);
00274 }
00275 
00283 void GenerateWorld(GenWorldMode mode, uint size_x, uint size_y, bool reset_settings)
00284 {
00285   if (_gw.active) return;
00286   _gw.mode   = mode;
00287   _gw.size_x = size_x;
00288   _gw.size_y = size_y;
00289   _gw.active = true;
00290   _gw.abort  = false;
00291   _gw.abortp = NULL;
00292   _gw.lc     = _local_company;
00293   _gw.quit_thread   = false;
00294   _gw.threaded      = true;
00295 
00296   /* This disables some commands and stuff */
00297   SetLocalCompany(COMPANY_SPECTATOR);
00298 
00299   InitializeGame(_gw.size_x, _gw.size_y, true, reset_settings);
00300   PrepareGenerateWorldProgress();
00301 
00302   /* Load the right landscape stuff, and the NewGRFs! */
00303   GfxLoadSprites();
00304   LoadStringWidthTable();
00305 
00306   /* Re-init the windowing system */
00307   ResetWindowSystem();
00308 
00309   /* Create toolbars */
00310   SetupColoursAndInitialWindow();
00311   SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);
00312 
00313   if (_gw.thread != NULL) {
00314     _gw.thread->Join();
00315     delete _gw.thread;
00316     _gw.thread = NULL;
00317   }
00318 
00319   if (BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() == 0 ||
00320       !ThreadObject::New(&_GenerateWorld, NULL, &_gw.thread)) {
00321     DEBUG(misc, 1, "Cannot create genworld thread, reverting to single-threaded mode");
00322     _gw.threaded = false;
00323     _genworld_mapgen_mutex->EndCritical();
00324     _GenerateWorld(NULL);
00325     _genworld_mapgen_mutex->BeginCritical();
00326     return;
00327   }
00328 
00329   /* Remove any open window */
00330   DeleteAllNonVitalWindows();
00331   /* Hide vital windows, because we don't allow to use them */
00332   HideVitalWindows();
00333 
00334   /* Don't show the dialog if we don't have a thread */
00335   ShowGenerateWorldProgress();
00336 
00337   /* Centre the view on the map */
00338   if (FindWindowById(WC_MAIN_WINDOW, 0) != NULL) {
00339     ScrollMainWindowToTile(TileXY(MapSizeX() / 2, MapSizeY() / 2), true);
00340   }
00341 }

Generated on Fri Jun 3 05:18:51 2011 for OpenTTD by  doxygen 1.6.1