00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "landscape.h"
00014 #include "company_func.h"
00015 #include "genworld.h"
00016 #include "gfxinit.h"
00017 #include "window_func.h"
00018 #include "network/network.h"
00019 #include "heightmap.h"
00020 #include "viewport_func.h"
00021 #include "gfx_func.h"
00022 #include "date_func.h"
00023 #include "engine_func.h"
00024 #include "newgrf_storage.h"
00025 #include "water.h"
00026 #include "video/video_driver.hpp"
00027 #include "tilehighlight_func.h"
00028 #include "saveload/saveload.h"
00029 #include "void_map.h"
00030 #include "town.h"
00031 #include "newgrf.h"
00032 #include "core/random_func.hpp"
00033 #include "core/backup_type.hpp"
00034 #include "progress.h"
00035 #include "error.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 bool _generating_world;
00060
00065 bool IsGenerateWorldThreaded()
00066 {
00067 return _gw.threaded && !_gw.quit_thread;
00068 }
00069
00074 static void CleanupGeneration()
00075 {
00076 _generating_world = false;
00077
00078 if (_cursor.sprite == SPR_CURSOR_ZZZ) SetMouseCursor(SPR_CURSOR_MOUSE, PAL_NONE);
00079
00080 if (_gw.threaded && _game_mode != GM_MENU) ShowVitalWindows();
00081 SetModalProgress(false);
00082 _gw.proc = NULL;
00083 _gw.abortp = NULL;
00084 _gw.threaded = false;
00085
00086 DeleteWindowById(WC_MODAL_PROGRESS, 0);
00087 ShowFirstError();
00088 MarkWholeScreenDirty();
00089 }
00090
00094 static void _GenerateWorld(void *)
00095 {
00096
00097 Backup<CompanyByte> _cur_company(_current_company, OWNER_NONE, FILE_LINE);
00098
00099 try {
00100 _generating_world = true;
00101 _modal_progress_work_mutex->BeginCritical();
00102 if (_network_dedicated) DEBUG(net, 0, "Generating map, please wait...");
00103
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
00111 StartupEconomy();
00112
00113
00114 if (_gw.mode == GWM_EMPTY) {
00115 SetGeneratingWorldProgress(GWP_OBJECT, 1);
00116
00117
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
00124 if (_game_mode != GM_MENU) FlatEmptyWorld(_settings_game.game_creation.se_flat_world_height);
00125
00126 ConvertGroundTilesIntoWaterTiles();
00127 IncreaseGeneratingWorldProgress(GWP_OBJECT);
00128 } else {
00129 GenerateLandscape(_gw.mode);
00130 GenerateClearTile();
00131
00132
00133 if (_game_mode != GM_EDITOR) {
00134 if (!GenerateTowns(_settings_game.economy.town_layout)) {
00135 _cur_company.Restore();
00136 HandleGeneratingWorldAbortion();
00137 return;
00138 }
00139 GenerateIndustries();
00140 GenerateObjects();
00141 GenerateTrees();
00142 }
00143 }
00144
00145 ClearStorageChanges(true);
00146
00147
00148 SetGeneratingWorldProgress(GWP_GAME_INIT, 3);
00149 StartupCompanies();
00150 IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
00151 StartupEngines();
00152 IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
00153 StartupDisasters();
00154 _generating_world = false;
00155
00156
00157 if (_gw.mode != GWM_EMPTY) {
00158 uint i;
00159
00160 SetGeneratingWorldProgress(GWP_RUNTILELOOP, 0x500);
00161 for (i = 0; i < 0x500; i++) {
00162 RunTileLoop();
00163 IncreaseGeneratingWorldProgress(GWP_RUNTILELOOP);
00164 }
00165 }
00166
00167 ResetObjectToPlace();
00168 _cur_company.Trash();
00169 _current_company = _local_company = _gw.lc;
00170
00171 SetGeneratingWorldProgress(GWP_GAME_START, 1);
00172
00173 if (_gw.proc != NULL) _gw.proc();
00174 IncreaseGeneratingWorldProgress(GWP_GAME_START);
00175
00176 CleanupGeneration();
00177 _modal_progress_work_mutex->EndCritical();
00178
00179 ShowNewGRFError();
00180
00181 if (_network_dedicated) DEBUG(net, 0, "Map generated, starting game");
00182 DEBUG(desync, 1, "new_map: %08x", _settings_game.game_creation.generation_seed);
00183
00184 if (_debug_desync_level > 0) {
00185 char name[MAX_PATH];
00186 snprintf(name, lengthof(name), "dmp_cmds_%08x_%08x.sav", _settings_game.game_creation.generation_seed, _date);
00187 SaveOrLoad(name, SL_SAVE, AUTOSAVE_DIR, false);
00188 }
00189 } catch (...) {
00190 if (_cur_company.IsValid()) _cur_company.Restore();
00191 _generating_world = false;
00192 _modal_progress_work_mutex->EndCritical();
00193 throw;
00194 }
00195 }
00196
00202 void GenerateWorldSetCallback(GWDoneProc *proc)
00203 {
00204 _gw.proc = proc;
00205 }
00206
00212 void GenerateWorldSetAbortCallback(GWAbortProc *proc)
00213 {
00214 _gw.abortp = proc;
00215 }
00216
00221 void WaitTillGeneratedWorld()
00222 {
00223 if (_gw.thread == NULL) return;
00224
00225 _modal_progress_work_mutex->EndCritical();
00226 _modal_progress_paint_mutex->EndCritical();
00227 _gw.quit_thread = true;
00228 _gw.thread->Join();
00229 delete _gw.thread;
00230 _gw.thread = NULL;
00231 _gw.threaded = false;
00232 _modal_progress_work_mutex->BeginCritical();
00233 _modal_progress_paint_mutex->BeginCritical();
00234 }
00235
00239 void AbortGeneratingWorld()
00240 {
00241 _gw.abort = true;
00242 }
00243
00248 bool IsGeneratingWorldAborted()
00249 {
00250 return _gw.abort;
00251 }
00252
00256 void HandleGeneratingWorldAbortion()
00257 {
00258
00259 _switch_mode = (_game_mode == GM_EDITOR) ? SM_EDITOR : SM_MENU;
00260
00261 if (_gw.abortp != NULL) _gw.abortp();
00262
00263 CleanupGeneration();
00264
00265 if (_gw.thread != NULL) _gw.thread->Exit();
00266
00267 SwitchToMode(_switch_mode);
00268 }
00269
00277 void GenerateWorld(GenWorldMode mode, uint size_x, uint size_y, bool reset_settings)
00278 {
00279 if (HasModalProgress()) return;
00280 _gw.mode = mode;
00281 _gw.size_x = size_x;
00282 _gw.size_y = size_y;
00283 SetModalProgress(true);
00284 _gw.abort = false;
00285 _gw.abortp = NULL;
00286 _gw.lc = _local_company;
00287 _gw.quit_thread = false;
00288 _gw.threaded = true;
00289
00290
00291 SetLocalCompany(COMPANY_SPECTATOR);
00292
00293 InitializeGame(_gw.size_x, _gw.size_y, true, reset_settings);
00294 PrepareGenerateWorldProgress();
00295
00296
00297 GfxLoadSprites();
00298 LoadStringWidthTable();
00299
00300
00301 ResetWindowSystem();
00302
00303
00304 SetupColoursAndInitialWindow();
00305 SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);
00306
00307 if (_gw.thread != NULL) {
00308 _gw.thread->Join();
00309 delete _gw.thread;
00310 _gw.thread = NULL;
00311 }
00312
00313 if (!_video_driver->HasGUI() || !ThreadObject::New(&_GenerateWorld, NULL, &_gw.thread)) {
00314 DEBUG(misc, 1, "Cannot create genworld thread, reverting to single-threaded mode");
00315 _gw.threaded = false;
00316 _modal_progress_work_mutex->EndCritical();
00317 _GenerateWorld(NULL);
00318 _modal_progress_work_mutex->BeginCritical();
00319 return;
00320 }
00321
00322 UnshowCriticalError();
00323
00324 DeleteAllNonVitalWindows();
00325
00326 HideVitalWindows();
00327
00328
00329 ShowGenerateWorldProgress();
00330
00331
00332 if (FindWindowById(WC_MAIN_WINDOW, 0) != NULL) {
00333 ScrollMainWindowToTile(TileXY(MapSizeX() / 2, MapSizeY() / 2), true);
00334 }
00335 }