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