diff --git a/src/genworld.cpp b/src/genworld.cpp index 4898567549..e145e63112 100644 --- a/src/genworld.cpp +++ b/src/genworld.cpp @@ -57,13 +57,16 @@ void StartupDisasters(); void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settings); -/** - * Please only use this variable in genworld.h and genworld.cpp and - * nowhere else. For speed improvements we need it to be global, but - * in no way the meaning of it is to use it anywhere else besides - * in the genworld.h and genworld.cpp! - */ -GenWorldInfo _gw; +/** Properties of current genworld process */ +struct GenWorldInfo { + static inline bool abort; ///< Whether to abort the thread ASAP + static inline GenWorldMode mode; ///< What mode are we making a world in + static inline CompanyID lc; ///< The local_company before generating + static inline uint size_x; ///< X-size of the map + static inline uint size_y; ///< Y-size of the map + static inline GWDoneProc *proc; ///< Proc that is called when done (can be nullptr) + static inline GWAbortProc *abortp; ///< Proc that is called when aborting (can be nullptr) +}; /** Whether we are generating the map or not. */ bool _generating_world; @@ -79,8 +82,8 @@ static void CleanupGeneration() SetMouseCursorBusy(false); SetModalProgress(false); - _gw.proc = nullptr; - _gw.abortp = nullptr; + GenWorldInfo::proc = nullptr; + GenWorldInfo::abortp = nullptr; CloseWindowByClass(WC_MODAL_PROGRESS); ShowFirstError(); @@ -117,8 +120,8 @@ static void _GenerateWorld() bool landscape_generated = false; /* Don't generate landscape items when in the scenario editor. */ - if (_gw.mode != GWM_EMPTY) { - landscape_generated = GenerateLandscape(_gw.mode); + if (GenWorldInfo::mode != GWM_EMPTY) { + landscape_generated = GenerateLandscape(GenWorldInfo::mode); } if (!landscape_generated) { @@ -162,7 +165,7 @@ static void _GenerateWorld() _generating_world = false; /* No need to run the tile loop in the scenario editor. */ - if (_gw.mode != GWM_EMPTY) { + if (GenWorldInfo::mode != GWM_EMPTY) { uint i; SetGeneratingWorldProgress(GWP_RUNTILELOOP, 0x500); @@ -192,13 +195,13 @@ static void _GenerateWorld() ResetObjectToPlace(); _cur_company.Trash(); - _current_company = _local_company = _gw.lc; + _current_company = _local_company = GenWorldInfo::lc; /* Show all vital windows again, because we have hidden them. */ if (_game_mode != GM_MENU) ShowVitalWindows(); SetGeneratingWorldProgress(GWP_GAME_START, 1); /* Call any callback */ - if (_gw.proc != nullptr) _gw.proc(); + if (GenWorldInfo::proc != nullptr) GenWorldInfo::proc(); IncreaseGeneratingWorldProgress(GWP_GAME_START); CleanupGeneration(); @@ -235,7 +238,7 @@ static void _GenerateWorld() */ void GenerateWorldSetCallback(GWDoneProc *proc) { - _gw.proc = proc; + GenWorldInfo::proc = proc; } /** @@ -245,7 +248,7 @@ void GenerateWorldSetCallback(GWDoneProc *proc) */ void GenerateWorldSetAbortCallback(GWAbortProc *proc) { - _gw.abortp = proc; + GenWorldInfo::abortp = proc; } /** @@ -253,7 +256,7 @@ void GenerateWorldSetAbortCallback(GWAbortProc *proc) */ void AbortGeneratingWorld() { - _gw.abort = true; + GenWorldInfo::abort = true; } /** @@ -262,7 +265,7 @@ void AbortGeneratingWorld() */ bool IsGeneratingWorldAborted() { - return _gw.abort || _exit_game; + return GenWorldInfo::abort || _exit_game; } /** @@ -273,7 +276,7 @@ void HandleGeneratingWorldAbortion() /* Clean up - in SE create an empty map, otherwise, go to intro menu */ _switch_mode = (_game_mode == GM_EDITOR) ? SM_EDITOR : SM_MENU; - if (_gw.abortp != nullptr) _gw.abortp(); + if (GenWorldInfo::abortp != nullptr) GenWorldInfo::abortp(); throw AbortGenerateWorldSignal(); } @@ -288,26 +291,26 @@ void HandleGeneratingWorldAbortion() void GenerateWorld(GenWorldMode mode, uint size_x, uint size_y, bool reset_settings) { if (HasModalProgress()) return; - _gw.mode = mode; - _gw.size_x = size_x; - _gw.size_y = size_y; + GenWorldInfo::mode = mode; + GenWorldInfo::size_x = size_x; + GenWorldInfo::size_y = size_y; SetModalProgress(true); - _gw.abort = false; - _gw.abortp = nullptr; - _gw.lc = _local_company; + GenWorldInfo::abort = false; + GenWorldInfo::abortp = nullptr; + GenWorldInfo::lc = _local_company; /* This disables some commands and stuff */ SetLocalCompany(COMPANY_SPECTATOR); - InitializeGame(_gw.size_x, _gw.size_y, true, reset_settings); + InitializeGame(GenWorldInfo::size_x, GenWorldInfo::size_y, true, reset_settings); PrepareGenerateWorldProgress(); if (_settings_game.construction.map_height_limit == 0) { uint estimated_height = 0; - if (_gw.mode == GWM_EMPTY && _game_mode != GM_MENU) { + if (GenWorldInfo::mode == GWM_EMPTY && _game_mode != GM_MENU) { estimated_height = _settings_game.game_creation.se_flat_world_height; - } else if (_gw.mode == GWM_HEIGHTMAP) { + } else if (GenWorldInfo::mode == GWM_HEIGHTMAP) { estimated_height = _settings_game.game_creation.heightmap_height; } else if (_settings_game.game_creation.land_generator == LG_TERRAGENESIS) { estimated_height = GetEstimationTGPMapHeight(); diff --git a/src/genworld.h b/src/genworld.h index 97bbdcfef1..0bfd8455ec 100644 --- a/src/genworld.h +++ b/src/genworld.h @@ -57,17 +57,6 @@ static const uint MAP_HEIGHT_LIMIT_AUTO_CEILING_ROOM = 15; ///< When map height typedef void GWDoneProc(); ///< Procedure called when the genworld process finishes typedef void GWAbortProc(); ///< Called when genworld is aborted -/** Properties of current genworld process */ -struct GenWorldInfo { - bool abort; ///< Whether to abort the thread ASAP - GenWorldMode mode; ///< What mode are we making a world in - CompanyID lc; ///< The local_company before generating - uint size_x; ///< X-size of the map - uint size_y; ///< Y-size of the map - GWDoneProc *proc; ///< Proc that is called when done (can be nullptr) - GWAbortProc *abortp; ///< Proc that is called when aborting (can be nullptr) -}; - /** Current stage of world generation process */ enum GenWorldProgress : uint8_t { GWP_MAP_INIT, ///< Initialize/allocate the map, start economy diff --git a/src/genworld_gui.cpp b/src/genworld_gui.cpp index ec082d5a08..713461eebe 100644 --- a/src/genworld_gui.cpp +++ b/src/genworld_gui.cpp @@ -1334,14 +1334,12 @@ static WindowDesc _generate_progress_desc( ); struct GenWorldStatus { - uint percent; - StringID cls; - uint current; - uint total; + static inline uint percent; + static inline StringID cls; + static inline uint current; + static inline uint total; }; -static GenWorldStatus _gws; - static const StringID _generation_class_table[] = { STR_GENERATION_WORLD_GENERATION, STR_GENERATION_LANDSCAPE_GENERATION, @@ -1418,19 +1416,19 @@ struct GenerateProgressWindow : public Window { /* Draw the % complete with a bar and a text */ DrawFrameRect(r, COLOUR_GREY, {FrameFlag::BorderOnly, FrameFlag::Lowered}); Rect br = r.Shrink(WidgetDimensions::scaled.bevel); - DrawFrameRect(br.WithWidth(br.Width() * _gws.percent / 100, _current_text_dir == TD_RTL), COLOUR_MAUVE, {}); - SetDParam(0, _gws.percent); + DrawFrameRect(br.WithWidth(br.Width() * GenWorldStatus::percent / 100, _current_text_dir == TD_RTL), COLOUR_MAUVE, {}); + SetDParam(0, GenWorldStatus::percent); DrawString(br.left, br.right, CenterBounds(br.top, br.bottom, GetCharacterHeight(FS_NORMAL)), STR_GENERATION_PROGRESS, TC_FROMSTRING, SA_HOR_CENTER); break; } case WID_GP_PROGRESS_TEXT: /* Tell which class we are generating */ - DrawString(r.left, r.right, r.top, _gws.cls, TC_FROMSTRING, SA_HOR_CENTER); + DrawString(r.left, r.right, r.top, GenWorldStatus::cls, TC_FROMSTRING, SA_HOR_CENTER); /* And say where we are in that class */ - SetDParam(0, _gws.current); - SetDParam(1, _gws.total); + SetDParam(0, GenWorldStatus::current); + SetDParam(1, GenWorldStatus::total); DrawString(r.left, r.right, r.top + GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.vsep_normal, STR_GENERATION_PROGRESS_NUM, TC_FROMSTRING, SA_HOR_CENTER); } } @@ -1441,10 +1439,10 @@ struct GenerateProgressWindow : public Window { */ void PrepareGenerateWorldProgress() { - _gws.cls = STR_GENERATION_WORLD_GENERATION; - _gws.current = 0; - _gws.total = 0; - _gws.percent = 0; + GenWorldStatus::cls = STR_GENERATION_WORLD_GENERATION; + GenWorldStatus::current = 0; + GenWorldStatus::total = 0; + GenWorldStatus::percent = 0; } /** @@ -1474,33 +1472,33 @@ static void _SetGeneratingWorldProgress(GenWorldProgress cls, uint progress, uin } if (total == 0) { - assert(_gws.cls == _generation_class_table[cls]); - _gws.current += progress; - assert(_gws.current <= _gws.total); + assert(GenWorldStatus::cls == _generation_class_table[cls]); + GenWorldStatus::current += progress; + assert(GenWorldStatus::current <= GenWorldStatus::total); } else { - _gws.cls = _generation_class_table[cls]; - _gws.current = progress; - _gws.total = total; - _gws.percent = percent_table[cls]; + GenWorldStatus::cls = _generation_class_table[cls]; + GenWorldStatus::current = progress; + GenWorldStatus::total = total; + GenWorldStatus::percent = percent_table[cls]; } /* Percentage is about the number of completed tasks, so 'current - 1' */ - _gws.percent = percent_table[cls] + (percent_table[cls + 1] - percent_table[cls]) * (_gws.current == 0 ? 0 : _gws.current - 1) / _gws.total; + GenWorldStatus::percent = percent_table[cls] + (percent_table[cls + 1] - percent_table[cls]) * (GenWorldStatus::current == 0 ? 0 : GenWorldStatus::current - 1) / GenWorldStatus::total; if (_network_dedicated) { static uint last_percent = 0; /* Never display 0% */ - if (_gws.percent == 0) return; + if (GenWorldStatus::percent == 0) return; /* Reset if percent is lower than the last recorded */ - if (_gws.percent < last_percent) last_percent = 0; + if (GenWorldStatus::percent < last_percent) last_percent = 0; /* Display every 5%, but 6% is also very valid.. just not smaller steps than 5% */ - if (_gws.percent % 5 != 0 && _gws.percent <= last_percent + 5) return; + if (GenWorldStatus::percent % 5 != 0 && GenWorldStatus::percent <= last_percent + 5) return; /* Never show steps smaller than 2%, even if it is a mod 5% */ - if (_gws.percent <= last_percent + 2) return; + if (GenWorldStatus::percent <= last_percent + 2) return; - Debug(net, 3, "Map generation percentage complete: {}", _gws.percent); - last_percent = _gws.percent; + Debug(net, 3, "Map generation percentage complete: {}", GenWorldStatus::percent); + last_percent = GenWorldStatus::percent; return; }