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 "industry.h"
00030 #include "town.h"
00031 #include "town_map.h"
00032 #include "economy_func.h"
00033 #include "core/mem_func.hpp"
00034 #include "newgrf.h"
00035 #include "core/random_func.hpp"
00036 #include "core/backup_type.hpp"
00037 #include "progress.h"
00038 #include "error.h"
00039 #include "game/game.hpp"
00040 #include "game/game_instance.hpp"
00041
00042
00043 void GenerateClearTile();
00044 void GenerateIndustries();
00045 void GenerateObjects();
00046 void GenerateTrees();
00047
00048 void StartupEconomy();
00049 void StartupCompanies();
00050 void StartupDisasters();
00051
00052 void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settings);
00053
00060 GenWorldInfo _gw;
00061
00063 bool _generating_world;
00064
00069 bool IsGenerateWorldThreaded()
00070 {
00071 return _gw.threaded && !_gw.quit_thread;
00072 }
00073
00078 static void CleanupGeneration()
00079 {
00080 _generating_world = false;
00081
00082 if (_cursor.sprite == SPR_CURSOR_ZZZ) SetMouseCursor(SPR_CURSOR_MOUSE, PAL_NONE);
00083
00084 if (_gw.threaded && _game_mode != GM_MENU) ShowVitalWindows();
00085 SetModalProgress(false);
00086 _gw.proc = NULL;
00087 _gw.abortp = NULL;
00088 _gw.threaded = false;
00089
00090 DeleteWindowByClass(WC_MODAL_PROGRESS);
00091 ShowFirstError();
00092 MarkWholeScreenDirty();
00093 }
00094
00095 static bool HeadToHead()
00096 {
00097 uint per_map_x = MapSizeX(), per_map_y = MapSizeY();
00098 Tile* m_landscape = _m;
00099 TileExtended *me_landscape = _me;
00100 _m = NULL;
00101 _me = NULL;
00102
00103 AllocateMap(MapSizeX(), MapSizeY() * _settings_game.game_creation.head_to_head_areas);
00104 Tile *m_final = _m;
00105 TileExtended *me_final = _me;
00106
00107 _m = NULL;
00108 _me = NULL;
00109
00110 AllocateMap(per_map_x, per_map_y);
00111
00112
00113 Randomizer rand_backup = _random;
00114 for (_head_to_head = 1; _head_to_head <= _settings_game.game_creation.head_to_head_areas; _head_to_head++) {
00115 MemCpyT<Tile>(_m, m_landscape, MapSize());
00116 MemCpyT<TileExtended>(_me, me_landscape, MapSize());
00117 _random = rand_backup;
00118 if (!GenerateTowns(_settings_game.economy.town_layout)) {
00119 HandleGeneratingWorldAbortion();
00120 free(m_landscape);
00121 free(me_landscape);
00122 free(m_final);
00123 free(me_final);
00124 return false;
00125 }
00126 GenerateIndustries();
00127 GenerateObjects();
00128 GenerateTrees();
00129
00130 #define FIX_TILE_INDEX(tile) (TileX(tile) + (TileY(tile) + (_head_to_head - 1) * per_map_y) * per_map_x)
00131
00132 for (TileIndex tile = 0; tile < MapSize(); tile++) {
00133 uint new_index = FIX_TILE_INDEX(tile);
00134 m_final[new_index] = _m[tile];
00135 me_final[new_index] = _me[tile];
00136 }
00137
00138 Industry *i;
00139 FOR_ALL_INDUSTRIES(i) i->location.tile = FIX_TILE_INDEX(i->location.tile);
00140 Town *t;
00141 FOR_ALL_TOWNS(t) t->xy = FIX_TILE_INDEX(t->xy);
00142 }
00143 free(m_landscape);
00144 free(me_landscape);
00145 free(_m);
00146 free(_me);
00147 _m = m_final;
00148 _me = me_final;
00149 _head_to_head = 0;
00150 extern uint _map_size_x, _map_size_y, _map_log_x, _map_log_y, _map_size;
00151 _map_size_x = per_map_x;
00152 _map_size_y = per_map_y * _settings_game.game_creation.head_to_head_areas;
00153 _map_log_x = FindFirstBit(_map_size_x);
00154 _map_log_y = FindFirstBit(_map_size_y);
00155 _map_size = _map_size_x * _map_size_y;
00156 _map_tile_mask = _map_size - 1;
00157 Town *t;
00158 FOR_ALL_TOWNS(t) t->UpdateVirtCoord();
00159 return true;
00160 }
00161
00165 static void _GenerateWorld(void *)
00166 {
00167 _settings_game.game_creation.head_to_head_areas = max<uint8>(_settings_game.game_creation.head_to_head_areas, 1);
00168
00169 Backup<CompanyByte> _cur_company(_current_company, OWNER_NONE, FILE_LINE);
00170
00171 try {
00172 _generating_world = true;
00173 _modal_progress_work_mutex->BeginCritical();
00174 if (_network_dedicated) DEBUG(net, 0, "Generating map, please wait...");
00175
00176 if (_settings_game.game_creation.generation_seed == GENERATE_NEW_SEED) _settings_game.game_creation.generation_seed = _settings_newgame.game_creation.generation_seed = InteractiveRandom();
00177 _random.SetSeed(_settings_game.game_creation.generation_seed);
00178 SetGeneratingWorldProgress(GWP_MAP_INIT, 2);
00179 SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);
00180
00181 IncreaseGeneratingWorldProgress(GWP_MAP_INIT);
00182
00183 StartupEconomy();
00184
00185
00186 if (_gw.mode == GWM_EMPTY) {
00187 SetGeneratingWorldProgress(GWP_OBJECT, 1);
00188
00189
00190 if (_settings_game.construction.freeform_edges) {
00191 for (uint row = 0; row < MapSizeY(); row++) MakeVoid(TileXY(0, row));
00192 for (uint col = 0; col < MapSizeX(); col++) MakeVoid(TileXY(col, 0));
00193 }
00194
00195
00196 if (_game_mode != GM_MENU) FlatEmptyWorld(_settings_game.game_creation.se_flat_world_height);
00197
00198 ConvertGroundTilesIntoWaterTiles();
00199 IncreaseGeneratingWorldProgress(GWP_OBJECT);
00200 } else {
00201 GenerateLandscape(_gw.mode);
00202 GenerateClearTile();
00203
00204
00205 if (_game_mode != GM_EDITOR) {
00206 if (_settings_game.game_creation.head_to_head_areas == 1) {
00207 _head_to_head = 1;
00208
00209 if (!GenerateTowns(_settings_game.economy.town_layout)) {
00210 _cur_company.Restore();
00211 HandleGeneratingWorldAbortion();
00212 return;
00213 }
00214 GenerateIndustries();
00215 GenerateObjects();
00216 GenerateTrees();
00217 } else {
00218 if (!HeadToHead()) return;
00219
00220 StartupIndustryDailyChanges(false);
00221 }
00222 }
00223 }
00224
00225 ClearStorageChanges(true);
00226
00227
00228 SetGeneratingWorldProgress(GWP_GAME_INIT, 3);
00229 StartupCompanies();
00230 IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
00231 StartupEngines();
00232 IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
00233 StartupDisasters();
00234 _generating_world = false;
00235
00236
00237 if (_gw.mode != GWM_EMPTY) {
00238 uint i;
00239
00240 SetGeneratingWorldProgress(GWP_RUNTILELOOP, 0x500);
00241 for (i = 0; i < 0x500; i++) {
00242 RunTileLoop();
00243 _tick_counter++;
00244 IncreaseGeneratingWorldProgress(GWP_RUNTILELOOP);
00245 }
00246
00247 if (_game_mode != GM_EDITOR) {
00248 Game::StartNew();
00249
00250 if (Game::GetInstance() != NULL) {
00251 SetGeneratingWorldProgress(GWP_RUNSCRIPT, 2500);
00252 _generating_world = true;
00253 for (i = 0; i < 2500; i++) {
00254 Game::GameLoop();
00255 IncreaseGeneratingWorldProgress(GWP_RUNSCRIPT);
00256 if (Game::GetInstance()->IsSleeping()) break;
00257 }
00258 _generating_world = false;
00259 }
00260 }
00261 }
00262
00263 ResetObjectToPlace();
00264 _cur_company.Trash();
00265 _current_company = _local_company = _gw.lc;
00266
00267 SetGeneratingWorldProgress(GWP_GAME_START, 1);
00268
00269 if (_gw.proc != NULL) _gw.proc();
00270 IncreaseGeneratingWorldProgress(GWP_GAME_START);
00271
00272 CleanupGeneration();
00273 _modal_progress_work_mutex->EndCritical();
00274
00275 ShowNewGRFError();
00276
00277 if (_network_dedicated) DEBUG(net, 0, "Map generated, starting game");
00278 DEBUG(desync, 1, "new_map: %08x", _settings_game.game_creation.generation_seed);
00279
00280 if (_debug_desync_level > 0) {
00281 char name[MAX_PATH];
00282 snprintf(name, lengthof(name), "dmp_cmds_%08x_%08x.sav", _settings_game.game_creation.generation_seed, _date);
00283 SaveOrLoad(name, SL_SAVE, AUTOSAVE_DIR, false);
00284 }
00285 } catch (...) {
00286 if (_cur_company.IsValid()) _cur_company.Restore();
00287 _generating_world = false;
00288 _modal_progress_work_mutex->EndCritical();
00289 throw;
00290 }
00291 }
00292
00298 void GenerateWorldSetCallback(GWDoneProc *proc)
00299 {
00300 _gw.proc = proc;
00301 }
00302
00308 void GenerateWorldSetAbortCallback(GWAbortProc *proc)
00309 {
00310 _gw.abortp = proc;
00311 }
00312
00317 void WaitTillGeneratedWorld()
00318 {
00319 if (_gw.thread == NULL) return;
00320
00321 _modal_progress_work_mutex->EndCritical();
00322 _modal_progress_paint_mutex->EndCritical();
00323 _gw.quit_thread = true;
00324 _gw.thread->Join();
00325 delete _gw.thread;
00326 _gw.thread = NULL;
00327 _gw.threaded = false;
00328 _modal_progress_work_mutex->BeginCritical();
00329 _modal_progress_paint_mutex->BeginCritical();
00330 }
00331
00335 void AbortGeneratingWorld()
00336 {
00337 _gw.abort = true;
00338 }
00339
00344 bool IsGeneratingWorldAborted()
00345 {
00346 return _gw.abort;
00347 }
00348
00352 void HandleGeneratingWorldAbortion()
00353 {
00354
00355 _switch_mode = (_game_mode == GM_EDITOR) ? SM_EDITOR : SM_MENU;
00356
00357 if (_gw.abortp != NULL) _gw.abortp();
00358
00359 CleanupGeneration();
00360
00361 if (_gw.thread != NULL) _gw.thread->Exit();
00362
00363 SwitchToMode(_switch_mode);
00364 }
00365
00373 void GenerateWorld(GenWorldMode mode, uint size_x, uint size_y, bool reset_settings)
00374 {
00375 if (HasModalProgress()) return;
00376 _gw.mode = mode;
00377 _gw.size_x = size_x;
00378 _gw.size_y = size_y;
00379 SetModalProgress(true);
00380 _gw.abort = false;
00381 _gw.abortp = NULL;
00382 _gw.lc = _local_company;
00383 _gw.quit_thread = false;
00384 _gw.threaded = true;
00385
00386
00387 SetLocalCompany(COMPANY_SPECTATOR);
00388
00389 InitializeGame(_gw.size_x, _gw.size_y, true, reset_settings);
00390 PrepareGenerateWorldProgress();
00391
00392
00393 GfxLoadSprites();
00394 LoadStringWidthTable();
00395
00396
00397 ResetWindowSystem();
00398
00399
00400 SetupColoursAndInitialWindow();
00401 SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);
00402
00403 if (_gw.thread != NULL) {
00404 _gw.thread->Join();
00405 delete _gw.thread;
00406 _gw.thread = NULL;
00407 }
00408
00409 if (!_video_driver->HasGUI() || !ThreadObject::New(&_GenerateWorld, NULL, &_gw.thread)) {
00410 DEBUG(misc, 1, "Cannot create genworld thread, reverting to single-threaded mode");
00411 _gw.threaded = false;
00412 _modal_progress_work_mutex->EndCritical();
00413 _GenerateWorld(NULL);
00414 _modal_progress_work_mutex->BeginCritical();
00415 return;
00416 }
00417
00418 UnshowCriticalError();
00419
00420 DeleteAllNonVitalWindows();
00421
00422 HideVitalWindows();
00423
00424
00425 ShowGenerateWorldProgress();
00426
00427
00428 if (FindWindowById(WC_MAIN_WINDOW, 0) != NULL) {
00429 ScrollMainWindowToTile(TileXY(MapSizeX() / 2, MapSizeY() / 2), true);
00430 }
00431 }