genworld.cpp
Go to the documentation of this file.00001
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
00045
00046
00047
00048 gw_info _gw;
00049
00057 void SetGeneratingWorldPaintStatus(bool status)
00058 {
00059 _gw.wait_for_draw = status;
00060 }
00061
00068 bool IsGeneratingWorldReadyForPaint()
00069 {
00070
00071
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
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
00101 StartupEconomy();
00102
00103
00104 if (_gw.mode == GW_EMPTY) {
00105 SetGeneratingWorldProgress(GWP_UNMOVABLE, 1);
00106
00107
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
00117 if (_game_mode != GM_EDITOR) {
00118 GenerateTowns();
00119 GenerateIndustries();
00120 GenerateUnmovables();
00121 GenerateTrees();
00122 }
00123 }
00124
00125 ClearStorageChanges(true);
00126
00127
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
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
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
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
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
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
00268 SetLocalCompany(COMPANY_SPECTATOR);
00269
00270 _current_company = OWNER_NONE;
00271
00272
00273 SetDate(ConvertYMDToDate(_settings_game.game_creation.starting_year, 0, 1));
00274
00275
00276 GfxLoadSprites();
00277 LoadStringWidthTable();
00278
00279 InitializeGame(_gw.size_x, _gw.size_y, false);
00280 PrepareGenerateWorldProgress();
00281
00282
00283 ResetWindowSystem();
00284
00285
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
00303 DeleteAllNonVitalWindows();
00304
00305 HideVitalWindows();
00306
00307
00308 ShowGenerateWorldProgress();
00309
00310
00311 if (FindWindowById(WC_MAIN_WINDOW, 0) != NULL) {
00312 ScrollMainWindowToTile(TileXY(MapSizeX() / 2, MapSizeY() / 2), true);
00313 }
00314 }