00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "openttd.h"
00014 #include "landscape.h"
00015 #include "company_func.h"
00016 #include "thread/thread.h"
00017 #include "genworld.h"
00018 #include "gfxinit.h"
00019 #include "window_func.h"
00020 #include "network/network.h"
00021 #include "heightmap.h"
00022 #include "viewport_func.h"
00023 #include "gfx_func.h"
00024 #include "date_func.h"
00025 #include "engine_func.h"
00026 #include "newgrf_storage.h"
00027 #include "water.h"
00028 #include "blitter/factory.hpp"
00029 #include "tilehighlight_func.h"
00030 #include "saveload/saveload.h"
00031 #include "void_map.h"
00032 #include "town.h"
00033 #include "newgrf.h"
00034 #include "core/random_func.hpp"
00035 #include "core/backup_type.hpp"
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 ThreadMutex *_genworld_mapgen_mutex = ThreadMutex::New();
00061 ThreadMutex *_genworld_paint_mutex = ThreadMutex::New();
00062
00064 bool _generating_world;
00065
00070 bool IsGenerateWorldThreaded()
00071 {
00072 return _gw.threaded && !_gw.quit_thread;
00073 }
00074
00079 static void CleanupGeneration()
00080 {
00081 _generating_world = false;
00082
00083 if (_cursor.sprite == SPR_CURSOR_ZZZ) SetMouseCursor(SPR_CURSOR_MOUSE, PAL_NONE);
00084
00085 if (_gw.threaded && _game_mode != GM_MENU) ShowVitalWindows();
00086 _gw.active = false;
00087 _gw.proc = NULL;
00088 _gw.abortp = NULL;
00089 _gw.threaded = false;
00090
00091 DeleteWindowById(WC_GENERATE_PROGRESS_WINDOW, 0);
00092 MarkWholeScreenDirty();
00093 }
00094
00098 static void _GenerateWorld(void *)
00099 {
00100
00101 Backup<CompanyByte> _cur_company(_current_company, OWNER_NONE, FILE_LINE);
00102
00103 try {
00104 _generating_world = true;
00105 _genworld_mapgen_mutex->BeginCritical();
00106 if (_network_dedicated) DEBUG(net, 0, "Generating map, please wait...");
00107
00108 if (_settings_game.game_creation.generation_seed == GENERATE_NEW_SEED) _settings_game.game_creation.generation_seed = _settings_newgame.game_creation.generation_seed = InteractiveRandom();
00109 _random.SetSeed(_settings_game.game_creation.generation_seed);
00110 SetGeneratingWorldProgress(GWP_MAP_INIT, 2);
00111 SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);
00112
00113 IncreaseGeneratingWorldProgress(GWP_MAP_INIT);
00114
00115 StartupEconomy();
00116
00117
00118 if (_gw.mode == GWM_EMPTY) {
00119 SetGeneratingWorldProgress(GWP_OBJECT, 1);
00120
00121
00122 if (_settings_game.construction.freeform_edges) {
00123 for (uint row = 0; row < MapSizeY(); row++) MakeVoid(TileXY(0, row));
00124 for (uint col = 0; col < MapSizeX(); col++) MakeVoid(TileXY(col, 0));
00125 }
00126
00127
00128 if (_game_mode != GM_MENU) FlatEmptyWorld(_settings_game.game_creation.se_flat_world_height);
00129
00130 ConvertGroundTilesIntoWaterTiles();
00131 IncreaseGeneratingWorldProgress(GWP_OBJECT);
00132 } else {
00133 GenerateLandscape(_gw.mode);
00134 GenerateClearTile();
00135
00136
00137 if (_game_mode != GM_EDITOR) {
00138 if (!GenerateTowns(_settings_game.economy.town_layout)) {
00139 _cur_company.Restore();
00140 HandleGeneratingWorldAbortion();
00141 return;
00142 }
00143 GenerateIndustries();
00144 GenerateObjects();
00145 GenerateTrees();
00146 }
00147 }
00148
00149 ClearStorageChanges(true);
00150
00151
00152 SetGeneratingWorldProgress(GWP_GAME_INIT, 3);
00153 StartupCompanies();
00154 IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
00155 StartupEngines();
00156 IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
00157 StartupDisasters();
00158 _generating_world = false;
00159
00160
00161 if (_gw.mode != GWM_EMPTY) {
00162 uint i;
00163
00164 SetGeneratingWorldProgress(GWP_RUNTILELOOP, 0x500);
00165 for (i = 0; i < 0x500; i++) {
00166 RunTileLoop();
00167 IncreaseGeneratingWorldProgress(GWP_RUNTILELOOP);
00168 }
00169 }
00170
00171 ResetObjectToPlace();
00172 _cur_company.Trash();
00173 _current_company = _local_company = _gw.lc;
00174
00175 SetGeneratingWorldProgress(GWP_GAME_START, 1);
00176
00177 if (_gw.proc != NULL) _gw.proc();
00178 IncreaseGeneratingWorldProgress(GWP_GAME_START);
00179
00180 CleanupGeneration();
00181 _genworld_mapgen_mutex->EndCritical();
00182
00183 ShowNewGRFError();
00184
00185 if (_network_dedicated) DEBUG(net, 0, "Map generated, starting game");
00186 DEBUG(desync, 1, "new_map: %08x", _settings_game.game_creation.generation_seed);
00187
00188 if (_debug_desync_level > 0) {
00189 char name[MAX_PATH];
00190 snprintf(name, lengthof(name), "dmp_cmds_%08x_%08x.sav", _settings_game.game_creation.generation_seed, _date);
00191 SaveOrLoad(name, SL_SAVE, AUTOSAVE_DIR, false);
00192 }
00193 } catch (...) {
00194 if (_cur_company.IsValid()) _cur_company.Restore();
00195 _generating_world = false;
00196 _genworld_mapgen_mutex->EndCritical();
00197 throw;
00198 }
00199 }
00200
00206 void GenerateWorldSetCallback(GWDoneProc *proc)
00207 {
00208 _gw.proc = proc;
00209 }
00210
00216 void GenerateWorldSetAbortCallback(GWAbortProc *proc)
00217 {
00218 _gw.abortp = proc;
00219 }
00220
00225 void WaitTillGeneratedWorld()
00226 {
00227 if (_gw.thread == NULL) return;
00228
00229 _genworld_mapgen_mutex->EndCritical();
00230 _genworld_paint_mutex->EndCritical();
00231 _gw.quit_thread = true;
00232 _gw.thread->Join();
00233 delete _gw.thread;
00234 _gw.thread = NULL;
00235 _gw.threaded = false;
00236 _genworld_mapgen_mutex->BeginCritical();
00237 _genworld_paint_mutex->BeginCritical();
00238 }
00239
00243 void AbortGeneratingWorld()
00244 {
00245 _gw.abort = true;
00246 }
00247
00252 bool IsGeneratingWorldAborted()
00253 {
00254 return _gw.abort;
00255 }
00256
00260 void HandleGeneratingWorldAbortion()
00261 {
00262
00263 _switch_mode = (_game_mode == GM_EDITOR) ? SM_EDITOR : SM_MENU;
00264
00265 if (_gw.abortp != NULL) _gw.abortp();
00266
00267 CleanupGeneration();
00268
00269 if (_gw.thread != NULL) _gw.thread->Exit();
00270
00271 extern void SwitchToMode(SwitchMode new_mode);
00272 SwitchToMode(_switch_mode);
00273 }
00274
00282 void GenerateWorld(GenWorldMode mode, uint size_x, uint size_y, bool reset_settings)
00283 {
00284 if (_gw.active) return;
00285 _gw.mode = mode;
00286 _gw.size_x = size_x;
00287 _gw.size_y = size_y;
00288 _gw.active = true;
00289 _gw.abort = false;
00290 _gw.abortp = NULL;
00291 _gw.lc = _local_company;
00292 _gw.quit_thread = false;
00293 _gw.threaded = true;
00294
00295
00296 SetLocalCompany(COMPANY_SPECTATOR);
00297
00298 InitializeGame(_gw.size_x, _gw.size_y, true, reset_settings);
00299 PrepareGenerateWorldProgress();
00300
00301
00302 GfxLoadSprites();
00303 LoadStringWidthTable();
00304
00305
00306 ResetWindowSystem();
00307
00308
00309 SetupColoursAndInitialWindow();
00310 SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);
00311
00312 if (_gw.thread != NULL) {
00313 _gw.thread->Join();
00314 delete _gw.thread;
00315 _gw.thread = NULL;
00316 }
00317
00318 if (BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() == 0 ||
00319 !ThreadObject::New(&_GenerateWorld, NULL, &_gw.thread)) {
00320 DEBUG(misc, 1, "Cannot create genworld thread, reverting to single-threaded mode");
00321 _gw.threaded = false;
00322 _genworld_mapgen_mutex->EndCritical();
00323 _GenerateWorld(NULL);
00324 _genworld_mapgen_mutex->BeginCritical();
00325 return;
00326 }
00327
00328
00329 DeleteAllNonVitalWindows();
00330
00331 HideVitalWindows();
00332
00333
00334 ShowGenerateWorldProgress();
00335
00336
00337 if (FindWindowById(WC_MAIN_WINDOW, 0) != NULL) {
00338 ScrollMainWindowToTile(TileXY(MapSizeX() / 2, MapSizeY() / 2), true);
00339 }
00340 }