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 "date_func.h"
00022 #include "engine_func.h"
00023 #include "water.h"
00024 #include "video/video_driver.hpp"
00025 #include "tilehighlight_func.h"
00026 #include "saveload/saveload.h"
00027 #include "void_map.h"
00028 #include "town.h"
00029 #include "newgrf.h"
00030 #include "core/random_func.hpp"
00031 #include "core/backup_type.hpp"
00032 #include "progress.h"
00033 #include "error.h"
00034 #include "game/game.hpp"
00035 #include "game/game_instance.hpp"
00036
00037
00038 void GenerateClearTile();
00039 void GenerateIndustries();
00040 void GenerateObjects();
00041 void GenerateTrees();
00042
00043 void StartupEconomy();
00044 void StartupCompanies();
00045 void StartupDisasters();
00046
00047 void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settings);
00048
00055 GenWorldInfo _gw;
00056
00058 bool _generating_world;
00059
00064 bool IsGenerateWorldThreaded()
00065 {
00066 return _gw.threaded && !_gw.quit_thread;
00067 }
00068
00073 static void CleanupGeneration()
00074 {
00075 _generating_world = false;
00076
00077 if (_cursor.sprite == SPR_CURSOR_ZZZ) SetMouseCursor(SPR_CURSOR_MOUSE, PAL_NONE);
00078
00079 if (_gw.threaded && _game_mode != GM_MENU) ShowVitalWindows();
00080 SetModalProgress(false);
00081 _gw.proc = NULL;
00082 _gw.abortp = NULL;
00083 _gw.threaded = false;
00084
00085 DeleteWindowByClass(WC_MODAL_PROGRESS);
00086 ShowFirstError();
00087 MarkWholeScreenDirty();
00088 }
00089
00093 static void _GenerateWorld(void *)
00094 {
00095
00096 Backup<CompanyByte> _cur_company(_current_company, OWNER_NONE, FILE_LINE);
00097
00098 try {
00099 _generating_world = true;
00100 _modal_progress_work_mutex->BeginCritical();
00101 if (_network_dedicated) DEBUG(net, 0, "Generating map, please wait...");
00102
00103 if (_settings_game.game_creation.generation_seed == GENERATE_NEW_SEED) _settings_game.game_creation.generation_seed = _settings_newgame.game_creation.generation_seed = InteractiveRandom();
00104 _random.SetSeed(_settings_game.game_creation.generation_seed);
00105 SetGeneratingWorldProgress(GWP_MAP_INIT, 2);
00106 SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);
00107
00108 IncreaseGeneratingWorldProgress(GWP_MAP_INIT);
00109
00110 StartupEconomy();
00111
00112
00113 if (_gw.mode == GWM_EMPTY) {
00114 SetGeneratingWorldProgress(GWP_OBJECT, 1);
00115
00116
00117 if (_settings_game.construction.freeform_edges) {
00118 for (uint row = 0; row < MapSizeY(); row++) MakeVoid(TileXY(0, row));
00119 for (uint col = 0; col < MapSizeX(); col++) MakeVoid(TileXY(col, 0));
00120 }
00121
00122
00123 if (_game_mode != GM_MENU) FlatEmptyWorld(_settings_game.game_creation.se_flat_world_height);
00124
00125 ConvertGroundTilesIntoWaterTiles();
00126 IncreaseGeneratingWorldProgress(GWP_OBJECT);
00127 } else {
00128 GenerateLandscape(_gw.mode);
00129 GenerateClearTile();
00130
00131
00132 if (_game_mode != GM_EDITOR) {
00133 if (!GenerateTowns(_settings_game.economy.town_layout)) {
00134 _cur_company.Restore();
00135 HandleGeneratingWorldAbortion();
00136 return;
00137 }
00138 GenerateIndustries();
00139 GenerateObjects();
00140 GenerateTrees();
00141 }
00142 }
00143
00144 ClearStorageChanges(true);
00145
00146
00147 SetGeneratingWorldProgress(GWP_GAME_INIT, 3);
00148 StartupCompanies();
00149 IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
00150 StartupEngines();
00151 IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
00152 StartupDisasters();
00153 _generating_world = false;
00154
00155
00156 if (_gw.mode != GWM_EMPTY) {
00157 uint i;
00158
00159 SetGeneratingWorldProgress(GWP_RUNTILELOOP, 0x500);
00160 for (i = 0; i < 0x500; i++) {
00161 RunTileLoop();
00162 IncreaseGeneratingWorldProgress(GWP_RUNTILELOOP);
00163 }
00164
00165 if (_game_mode != GM_EDITOR) {
00166 Game::StartNew();
00167
00168 if (Game::GetInstance() != NULL) {
00169 SetGeneratingWorldProgress(GWP_RUNSCRIPT, 2500);
00170 _generating_world = true;
00171 for (i = 0; i < 2500; i++) {
00172 Game::GameLoop();
00173 IncreaseGeneratingWorldProgress(GWP_RUNSCRIPT);
00174 if (Game::GetInstance()->IsSleeping()) break;
00175 }
00176 _generating_world = false;
00177 }
00178 }
00179 }
00180
00181 ResetObjectToPlace();
00182 _cur_company.Trash();
00183 _current_company = _local_company = _gw.lc;
00184
00185 SetGeneratingWorldProgress(GWP_GAME_START, 1);
00186
00187 if (_gw.proc != NULL) _gw.proc();
00188 IncreaseGeneratingWorldProgress(GWP_GAME_START);
00189
00190 CleanupGeneration();
00191 _modal_progress_work_mutex->EndCritical();
00192
00193 ShowNewGRFError();
00194
00195 if (_network_dedicated) DEBUG(net, 0, "Map generated, starting game");
00196 DEBUG(desync, 1, "new_map: %08x", _settings_game.game_creation.generation_seed);
00197
00198 if (_debug_desync_level > 0) {
00199 char name[MAX_PATH];
00200 snprintf(name, lengthof(name), "dmp_cmds_%08x_%08x.sav", _settings_game.game_creation.generation_seed, _date);
00201 SaveOrLoad(name, SL_SAVE, AUTOSAVE_DIR, false);
00202 }
00203 } catch (...) {
00204 if (_cur_company.IsValid()) _cur_company.Restore();
00205 _generating_world = false;
00206 _modal_progress_work_mutex->EndCritical();
00207 throw;
00208 }
00209 }
00210
00216 void GenerateWorldSetCallback(GWDoneProc *proc)
00217 {
00218 _gw.proc = proc;
00219 }
00220
00226 void GenerateWorldSetAbortCallback(GWAbortProc *proc)
00227 {
00228 _gw.abortp = proc;
00229 }
00230
00235 void WaitTillGeneratedWorld()
00236 {
00237 if (_gw.thread == NULL) return;
00238
00239 _modal_progress_work_mutex->EndCritical();
00240 _modal_progress_paint_mutex->EndCritical();
00241 _gw.quit_thread = true;
00242 _gw.thread->Join();
00243 delete _gw.thread;
00244 _gw.thread = NULL;
00245 _gw.threaded = false;
00246 _modal_progress_work_mutex->BeginCritical();
00247 _modal_progress_paint_mutex->BeginCritical();
00248 }
00249
00253 void AbortGeneratingWorld()
00254 {
00255 _gw.abort = true;
00256 }
00257
00262 bool IsGeneratingWorldAborted()
00263 {
00264 return _gw.abort;
00265 }
00266
00270 void HandleGeneratingWorldAbortion()
00271 {
00272
00273 _switch_mode = (_game_mode == GM_EDITOR) ? SM_EDITOR : SM_MENU;
00274
00275 if (_gw.abortp != NULL) _gw.abortp();
00276
00277 CleanupGeneration();
00278
00279 if (_gw.thread != NULL) _gw.thread->Exit();
00280
00281 SwitchToMode(_switch_mode);
00282 }
00283
00291 void GenerateWorld(GenWorldMode mode, uint size_x, uint size_y, bool reset_settings)
00292 {
00293 if (HasModalProgress()) return;
00294 _gw.mode = mode;
00295 _gw.size_x = size_x;
00296 _gw.size_y = size_y;
00297 SetModalProgress(true);
00298 _gw.abort = false;
00299 _gw.abortp = NULL;
00300 _gw.lc = _local_company;
00301 _gw.quit_thread = false;
00302 _gw.threaded = true;
00303
00304
00305 SetLocalCompany(COMPANY_SPECTATOR);
00306
00307 InitializeGame(_gw.size_x, _gw.size_y, true, reset_settings);
00308 PrepareGenerateWorldProgress();
00309
00310
00311 GfxLoadSprites();
00312 LoadStringWidthTable();
00313
00314
00315 ResetWindowSystem();
00316
00317
00318 SetupColoursAndInitialWindow();
00319 SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);
00320
00321 if (_gw.thread != NULL) {
00322 _gw.thread->Join();
00323 delete _gw.thread;
00324 _gw.thread = NULL;
00325 }
00326
00327 if (!_video_driver->HasGUI() || !ThreadObject::New(&_GenerateWorld, NULL, &_gw.thread)) {
00328 DEBUG(misc, 1, "Cannot create genworld thread, reverting to single-threaded mode");
00329 _gw.threaded = false;
00330 _modal_progress_work_mutex->EndCritical();
00331 _GenerateWorld(NULL);
00332 _modal_progress_work_mutex->BeginCritical();
00333 return;
00334 }
00335
00336 UnshowCriticalError();
00337
00338 DeleteAllNonVitalWindows();
00339
00340 HideVitalWindows();
00341
00342
00343 ShowGenerateWorldProgress();
00344
00345
00346 if (FindWindowById(WC_MAIN_WINDOW, 0) != NULL) {
00347 ScrollMainWindowToTile(TileXY(MapSizeX() / 2, MapSizeY() / 2), true);
00348 }
00349 }