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
00036 #include "table/sprites.h"
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 DeleteWindowById(WC_MODAL_PROGRESS, 0);
00086 MarkWholeScreenDirty();
00087 }
00088
00092 static void _GenerateWorld(void *)
00093 {
00094
00095 Backup<CompanyByte> _cur_company(_current_company, OWNER_NONE, FILE_LINE);
00096
00097 try {
00098 _generating_world = true;
00099 _modal_progress_work_mutex->BeginCritical();
00100 if (_network_dedicated) DEBUG(net, 0, "Generating map, please wait...");
00101
00102 if (_settings_game.game_creation.generation_seed == GENERATE_NEW_SEED) _settings_game.game_creation.generation_seed = _settings_newgame.game_creation.generation_seed = InteractiveRandom();
00103 _random.SetSeed(_settings_game.game_creation.generation_seed);
00104 SetGeneratingWorldProgress(GWP_MAP_INIT, 2);
00105 SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);
00106
00107 IncreaseGeneratingWorldProgress(GWP_MAP_INIT);
00108
00109 StartupEconomy();
00110
00111
00112 if (_gw.mode == GWM_EMPTY) {
00113 SetGeneratingWorldProgress(GWP_OBJECT, 1);
00114
00115
00116 if (_settings_game.construction.freeform_edges) {
00117 for (uint row = 0; row < MapSizeY(); row++) MakeVoid(TileXY(0, row));
00118 for (uint col = 0; col < MapSizeX(); col++) MakeVoid(TileXY(col, 0));
00119 }
00120
00121
00122 if (_game_mode != GM_MENU) FlatEmptyWorld(_settings_game.game_creation.se_flat_world_height);
00123
00124 ConvertGroundTilesIntoWaterTiles();
00125 IncreaseGeneratingWorldProgress(GWP_OBJECT);
00126 } else {
00127 GenerateLandscape(_gw.mode);
00128 GenerateClearTile();
00129
00130
00131 if (_game_mode != GM_EDITOR) {
00132 if (!GenerateTowns(_settings_game.economy.town_layout)) {
00133 _cur_company.Restore();
00134 HandleGeneratingWorldAbortion();
00135 return;
00136 }
00137 GenerateIndustries();
00138 GenerateObjects();
00139 GenerateTrees();
00140 }
00141 }
00142
00143 ClearStorageChanges(true);
00144
00145
00146 SetGeneratingWorldProgress(GWP_GAME_INIT, 3);
00147 StartupCompanies();
00148 IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
00149 StartupEngines();
00150 IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
00151 StartupDisasters();
00152 _generating_world = false;
00153
00154
00155 if (_gw.mode != GWM_EMPTY) {
00156 uint i;
00157
00158 SetGeneratingWorldProgress(GWP_RUNTILELOOP, 0x500);
00159 for (i = 0; i < 0x500; i++) {
00160 RunTileLoop();
00161 IncreaseGeneratingWorldProgress(GWP_RUNTILELOOP);
00162 }
00163 }
00164
00165 ResetObjectToPlace();
00166 _cur_company.Trash();
00167 _current_company = _local_company = _gw.lc;
00168
00169 SetGeneratingWorldProgress(GWP_GAME_START, 1);
00170
00171 if (_gw.proc != NULL) _gw.proc();
00172 IncreaseGeneratingWorldProgress(GWP_GAME_START);
00173
00174 CleanupGeneration();
00175 _modal_progress_work_mutex->EndCritical();
00176
00177 ShowNewGRFError();
00178
00179 if (_network_dedicated) DEBUG(net, 0, "Map generated, starting game");
00180 DEBUG(desync, 1, "new_map: %08x", _settings_game.game_creation.generation_seed);
00181
00182 if (_debug_desync_level > 0) {
00183 char name[MAX_PATH];
00184 snprintf(name, lengthof(name), "dmp_cmds_%08x_%08x.sav", _settings_game.game_creation.generation_seed, _date);
00185 SaveOrLoad(name, SL_SAVE, AUTOSAVE_DIR, false);
00186 }
00187 } catch (...) {
00188 if (_cur_company.IsValid()) _cur_company.Restore();
00189 _generating_world = false;
00190 _modal_progress_work_mutex->EndCritical();
00191 throw;
00192 }
00193 }
00194
00200 void GenerateWorldSetCallback(GWDoneProc *proc)
00201 {
00202 _gw.proc = proc;
00203 }
00204
00210 void GenerateWorldSetAbortCallback(GWAbortProc *proc)
00211 {
00212 _gw.abortp = proc;
00213 }
00214
00219 void WaitTillGeneratedWorld()
00220 {
00221 if (_gw.thread == NULL) return;
00222
00223 _modal_progress_work_mutex->EndCritical();
00224 _modal_progress_paint_mutex->EndCritical();
00225 _gw.quit_thread = true;
00226 _gw.thread->Join();
00227 delete _gw.thread;
00228 _gw.thread = NULL;
00229 _gw.threaded = false;
00230 _modal_progress_work_mutex->BeginCritical();
00231 _modal_progress_paint_mutex->BeginCritical();
00232 }
00233
00237 void AbortGeneratingWorld()
00238 {
00239 _gw.abort = true;
00240 }
00241
00246 bool IsGeneratingWorldAborted()
00247 {
00248 return _gw.abort;
00249 }
00250
00254 void HandleGeneratingWorldAbortion()
00255 {
00256
00257 _switch_mode = (_game_mode == GM_EDITOR) ? SM_EDITOR : SM_MENU;
00258
00259 if (_gw.abortp != NULL) _gw.abortp();
00260
00261 CleanupGeneration();
00262
00263 if (_gw.thread != NULL) _gw.thread->Exit();
00264
00265 extern void SwitchToMode(SwitchMode new_mode);
00266 SwitchToMode(_switch_mode);
00267 }
00268
00276 void GenerateWorld(GenWorldMode mode, uint size_x, uint size_y, bool reset_settings)
00277 {
00278 if (HasModalProgress()) return;
00279 _gw.mode = mode;
00280 _gw.size_x = size_x;
00281 _gw.size_y = size_y;
00282 SetModalProgress(true);
00283 _gw.abort = false;
00284 _gw.abortp = NULL;
00285 _gw.lc = _local_company;
00286 _gw.quit_thread = false;
00287 _gw.threaded = true;
00288
00289
00290 SetLocalCompany(COMPANY_SPECTATOR);
00291
00292 InitializeGame(_gw.size_x, _gw.size_y, true, reset_settings);
00293 PrepareGenerateWorldProgress();
00294
00295
00296 GfxLoadSprites();
00297 LoadStringWidthTable();
00298
00299
00300 ResetWindowSystem();
00301
00302
00303 SetupColoursAndInitialWindow();
00304 SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);
00305
00306 if (_gw.thread != NULL) {
00307 _gw.thread->Join();
00308 delete _gw.thread;
00309 _gw.thread = NULL;
00310 }
00311
00312 if (!_video_driver->HasGUI() || !ThreadObject::New(&_GenerateWorld, NULL, &_gw.thread)) {
00313 DEBUG(misc, 1, "Cannot create genworld thread, reverting to single-threaded mode");
00314 _gw.threaded = false;
00315 _modal_progress_work_mutex->EndCritical();
00316 _GenerateWorld(NULL);
00317 _modal_progress_work_mutex->BeginCritical();
00318 return;
00319 }
00320
00321
00322 DeleteAllNonVitalWindows();
00323
00324 HideVitalWindows();
00325
00326
00327 ShowGenerateWorldProgress();
00328
00329
00330 if (FindWindowById(WC_MAIN_WINDOW, 0) != NULL) {
00331 ScrollMainWindowToTile(TileXY(MapSizeX() / 2, MapSizeY() / 2), true);
00332 }
00333 }