genworld.cpp

Go to the documentation of this file.
00001 /* $Id: genworld.cpp 14422 2008-09-30 20:51:04Z 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 "debug.h"
00017 #include "settings_func.h"
00018 #include "heightmap.h"
00019 #include "viewport_func.h"
00020 #include "gfx_func.h"
00021 #include "map_func.h"
00022 #include "date_func.h"
00023 #include "core/random_func.hpp"
00024 #include "engine_func.h"
00025 #include "settings_type.h"
00026 #include "newgrf_storage.h"
00027 #include "water.h"
00028 #include "tilehighlight_func.h"
00029 
00030 #include "table/sprites.h"
00031 
00032 void GenerateClearTile();
00033 void GenerateIndustries();
00034 void GenerateUnmovables();
00035 bool GenerateTowns();
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 the map the height of the patch setting */
00108       if (_game_mode != GM_MENU) FlatEmptyWorld(_settings_game.game_creation.se_flat_world_height);
00109 
00110       ConvertGroundTilesIntoWaterTiles();
00111       IncreaseGeneratingWorldProgress(GWP_UNMOVABLE);
00112     } else {
00113       GenerateLandscape(_gw.mode);
00114       GenerateClearTile();
00115 
00116       /* only generate towns, tree and industries in newgame mode. */
00117       if (_game_mode != GM_EDITOR) {
00118         GenerateTowns();
00119         GenerateIndustries();
00120         GenerateUnmovables();
00121         GenerateTrees();
00122       }
00123     }
00124 
00125     ClearStorageChanges(true);
00126 
00127     /* These are probably pointless when inside the scenario editor. */
00128     SetGeneratingWorldProgress(GWP_GAME_INIT, 3);
00129     StartupCompanies();
00130     IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
00131     StartupEngines();
00132     IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
00133     StartupDisasters();
00134     _generating_world = false;
00135 
00136     /* No need to run the tile loop in the scenario editor. */
00137     if (_gw.mode != GW_EMPTY) {
00138       uint i;
00139 
00140       SetGeneratingWorldProgress(GWP_RUNTILELOOP, 0x500);
00141       for (i = 0; i < 0x500; i++) {
00142         RunTileLoop();
00143         IncreaseGeneratingWorldProgress(GWP_RUNTILELOOP);
00144       }
00145     }
00146 
00147     ResetObjectToPlace();
00148     _local_company = _gw.lc;
00149 
00150     SetGeneratingWorldProgress(GWP_GAME_START, 1);
00151     /* Call any callback */
00152     if (_gw.proc != NULL) _gw.proc();
00153     IncreaseGeneratingWorldProgress(GWP_GAME_START);
00154 
00155     if (_cursor.sprite == SPR_CURSOR_ZZZ) SetMouseCursor(SPR_CURSOR_MOUSE, PAL_NONE);
00156     /* Show all vital windows again, because we have hidden them */
00157     if (_gw.threaded && _game_mode != GM_MENU) ShowVitalWindows();
00158     _gw.active   = false;
00159     _gw.proc     = NULL;
00160     _gw.threaded = false;
00161 
00162     DeleteWindowById(WC_GENERATE_PROGRESS_WINDOW, 0);
00163     MarkWholeScreenDirty();
00164 
00165     if (_network_dedicated) DEBUG(net, 0, "Map generated, starting game");
00166 
00167     if (_settings_client.gui.pause_on_newgame && _game_mode == GM_NORMAL) DoCommandP(0, 1, 0, NULL, CMD_PAUSE);
00168   } catch (...) {
00169     _generating_world = false;
00170     throw;
00171   }
00172 }
00173 
00178 void GenerateWorldSetCallback(gw_done_proc *proc)
00179 {
00180   _gw.proc = proc;
00181 }
00182 
00187 void GenerateWorldSetAbortCallback(gw_abort_proc *proc)
00188 {
00189   _gw.abortp = proc;
00190 }
00191 
00196 void WaitTillGeneratedWorld()
00197 {
00198   if (_gw.thread == NULL) return;
00199   _gw.quit_thread = true;
00200   _gw.thread->Join();
00201   delete _gw.thread;
00202   _gw.thread   = NULL;
00203   _gw.threaded = false;
00204 }
00205 
00209 void AbortGeneratingWorld()
00210 {
00211   _gw.abort = true;
00212 }
00213 
00217 bool IsGeneratingWorldAborted()
00218 {
00219   return _gw.abort;
00220 }
00221 
00225 void HandleGeneratingWorldAbortion()
00226 {
00227   /* Clean up - in SE create an empty map, otherwise, go to intro menu */
00228   _switch_mode = (_game_mode == GM_EDITOR) ? SM_EDITOR : SM_MENU;
00229 
00230   if (_gw.abortp != NULL) _gw.abortp();
00231 
00232   if (_cursor.sprite == SPR_CURSOR_ZZZ) SetMouseCursor(SPR_CURSOR_MOUSE, PAL_NONE);
00233   /* Show all vital windows again, because we have hidden them */
00234   if (_gw.threaded && _game_mode != GM_MENU) ShowVitalWindows();
00235 
00236   _gw.active   = false;
00237   _gw.proc     = NULL;
00238   _gw.abortp   = NULL;
00239   _gw.threaded = false;
00240 
00241   DeleteWindowById(WC_GENERATE_PROGRESS_WINDOW, 0);
00242   MarkWholeScreenDirty();
00243 
00244   _gw.thread->Exit();
00245 }
00246 
00253 void GenerateWorld(GenerateWorldMode mode, uint size_x, uint size_y)
00254 {
00255   if (_gw.active) return;
00256   _gw.mode   = mode;
00257   _gw.size_x = size_x;
00258   _gw.size_y = size_y;
00259   _gw.active = true;
00260   _gw.abort  = false;
00261   _gw.abortp = NULL;
00262   _gw.lc     = _local_company;
00263   _gw.wait_for_draw = false;
00264   _gw.quit_thread   = false;
00265   _gw.threaded      = true;
00266 
00267   /* This disables some commands and stuff */
00268   SetLocalCompany(COMPANY_SPECTATOR);
00269   /* Make sure everything is done via OWNER_NONE */
00270   _current_company = OWNER_NONE;
00271 
00272   /* Set the date before loading sprites as some newgrfs check it */
00273   SetDate(ConvertYMDToDate(_settings_game.game_creation.starting_year, 0, 1));
00274 
00275   /* Load the right landscape stuff */
00276   GfxLoadSprites();
00277   LoadStringWidthTable();
00278 
00279   InitializeGame(_gw.size_x, _gw.size_y, false);
00280   PrepareGenerateWorldProgress();
00281 
00282   /* Re-init the windowing system */
00283   ResetWindowSystem();
00284 
00285   /* Create toolbars */
00286   SetupColorsAndInitialWindow();
00287 
00288   if (_gw.thread != NULL) {
00289     _gw.thread->Join();
00290     delete _gw.thread;
00291     _gw.thread = NULL;
00292   }
00293 
00294   if (_network_dedicated ||
00295       (_gw.thread = ThreadObject::New(&_GenerateWorld, NULL)) == NULL) {
00296     DEBUG(misc, 1, "Cannot create genworld thread, reverting to single-threaded mode");
00297     _gw.threaded = false;
00298     _GenerateWorld(NULL);
00299     return;
00300   }
00301 
00302   /* Remove any open window */
00303   DeleteAllNonVitalWindows();
00304   /* Hide vital windows, because we don't allow to use them */
00305   HideVitalWindows();
00306 
00307   /* Don't show the dialog if we don't have a thread */
00308   ShowGenerateWorldProgress();
00309 
00310   /* Centre the view on the map */
00311   if (FindWindowById(WC_MAIN_WINDOW, 0) != NULL) {
00312     ScrollMainWindowToTile(TileXY(MapSizeX() / 2, MapSizeY() / 2), true);
00313   }
00314 }

Generated on Fri Nov 21 19:01:32 2008 for openttd by  doxygen 1.5.6