From ce96863776a7e40fed504a8387a386ccb2cb668a Mon Sep 17 00:00:00 2001 From: dP Date: Sat, 20 Jun 2020 01:25:33 +0300 Subject: [PATCH] Calculate houses_constructing and real_population --- src/citymania/cm_event.hpp | 20 +++++--- src/citymania/cm_game.cpp | 22 ++++++++ src/citymania/cm_main.hpp | 1 + src/citymania/cm_type.hpp | 67 ++++++++++++------------- src/citymania/extensions/cmext_town.hpp | 2 + src/openttd.cpp | 2 + src/saveload/town_sl.cpp | 3 ++ src/town_cmd.cpp | 5 +- 8 files changed, 81 insertions(+), 41 deletions(-) diff --git a/src/citymania/cm_event.hpp b/src/citymania/cm_event.hpp index bf2c95bd8b..7f8a6d106f 100644 --- a/src/citymania/cm_event.hpp +++ b/src/citymania/cm_event.hpp @@ -17,8 +17,7 @@ namespace citymania { namespace event { -struct NewMonth { -}; +struct NewMonth {}; struct TownGrowthSucceeded { Town *town; @@ -31,6 +30,8 @@ struct TownGrowthFailed { TileIndex tile; }; +struct TownCachesRebuilt {}; + struct HouseRebuilt { Town *town; TileIndex tile; @@ -43,6 +44,13 @@ struct HouseBuilt { const HouseSpec *house_spec; }; +struct HouseCleared { + Town *town; + TileIndex tile; + const HouseSpec *house_spec; + bool was_completed; ///< whether house was completed before destruction +}; + struct HouseCompleted { Town *town; TileIndex tile; @@ -115,14 +123,14 @@ protected: class Dispatcher { protected: - std::map> dispacthers; + std::map> dispatchers; template TypeDispatcher &get_dispatcher() { - auto p = this->dispacthers.find(typeid(T)); - if (p == this->dispacthers.end()) { + auto p = this->dispatchers.find(typeid(T)); + if (p == this->dispatchers.end()) { auto x = make_up>(); - p = this->dispacthers.emplace_hint(p, typeid(T), std::move(x)); + p = this->dispatchers.emplace_hint(p, typeid(T), std::move(x)); } return *(static_cast *>((*p).second.get())); } diff --git a/src/citymania/cm_game.cpp b/src/citymania/cm_game.cpp index 187449f9d3..d544ac0d00 100644 --- a/src/citymania/cm_game.cpp +++ b/src/citymania/cm_game.cpp @@ -50,12 +50,34 @@ Game::Game() { this->events.listen([this] (const event::HouseBuilt &event) { event.town->cm.houses_constructing++; + event.town->cm.real_population += event.house_spec->population; this->towns_growth_tiles[event.tile] = TownGrowthTileState::NEW_HOUSE; }); + this->events.listen([this] (const event::HouseCleared &event) { + if (!event.was_completed) + event.town->cm.houses_constructing--; + event.town->cm.real_population -= event.house_spec->population; + }); + this->events.listen([this] (const event::HouseCompleted &event) { event.town->cm.houses_constructing--; }); + + this->events.listen([this] (const event::TownCachesRebuilt &event) { + for (Town *town : Town::Iterate()) { + town->cm.real_population = 0; + town->cm.houses_constructing = 0; + } + for (TileIndex t = 0; t < MapSize(); t++) { + if (!IsTileType(t, MP_HOUSE)) continue; + Town *town = Town::GetByTile(t); + if (!IsHouseCompleted(t)) + town->cm.houses_constructing++; + HouseID house_id = GetHouseType(t); + town->cm.real_population += HouseSpec::Get(house_id)->population; + } + }); } } // namespace citymania \ No newline at end of file diff --git a/src/citymania/cm_main.hpp b/src/citymania/cm_main.hpp index 32cb44fc2f..d32a676fbc 100644 --- a/src/citymania/cm_main.hpp +++ b/src/citymania/cm_main.hpp @@ -8,6 +8,7 @@ namespace citymania { extern up _game; +void ResetGame(); void SwitchToMode(SwitchMode new_mode); template diff --git a/src/citymania/cm_type.hpp b/src/citymania/cm_type.hpp index 56bd4c7e45..445e8e9347 100644 --- a/src/citymania/cm_type.hpp +++ b/src/citymania/cm_type.hpp @@ -6,38 +6,6 @@ #include #include -/* C++14 implementation of make_unique */ -namespace std { - template struct _Unique_if { - typedef unique_ptr _Single_object; - }; - - template struct _Unique_if { - typedef unique_ptr _Unknown_bound; - }; - - template struct _Unique_if { - typedef void _Known_bound; - }; - - template - typename _Unique_if::_Single_object - make_unique(Args&&... args) { - return unique_ptr(new T(std::forward(args)...)); - } - - template - typename _Unique_if::_Unknown_bound - make_unique(size_t n) { - typedef typename remove_extent::type U; - return unique_ptr(new U[n]()); - } - - template - typename _Unique_if::_Known_bound - make_unique(Args&&...) = delete; -} // namespace std - namespace citymania { // Make smart pointers easier to type @@ -45,8 +13,39 @@ template using up=std::unique_ptr; template using sp=std::shared_ptr; template using wp=std::weak_ptr; -template const auto make_up = std::make_unique; -template const auto make_sp = std::make_shared; +/* C++14 implementation of make_unique */ +template struct _Unique_if { + typedef std::unique_ptr _Single_object; +}; + +template struct _Unique_if { + typedef std::unique_ptr _Unknown_bound; +}; + +template struct _Unique_if { + typedef void _Known_bound; +}; + +template + typename _Unique_if::_Single_object + make_up(Args&&... args) { + return std::unique_ptr(new T(std::forward(args)...)); + } + +template + typename _Unique_if::_Unknown_bound + make_up(size_t n) { + typedef typename std::remove_extent::type U; + return std::unique_ptr(new U[n]()); + } + +template + typename _Unique_if::_Known_bound + make_up(Args&&...) = delete; + + +// template const auto make_up = std::make_unique; +// template const auto make_sp = std::make_shared; } // namespace citymania diff --git a/src/citymania/extensions/cmext_town.hpp b/src/citymania/extensions/cmext_town.hpp index ac0476ca24..dc51ccd17d 100644 --- a/src/citymania/extensions/cmext_town.hpp +++ b/src/citymania/extensions/cmext_town.hpp @@ -8,6 +8,7 @@ namespace ext { class Town { public: bool growing_by_chance = false; ///< whether town is growing due to 1/12 chance + uint32 real_population = 0; ///< population including unfinished houses uint32 hs_total = 0; ///< number of skipped house buildings (HS) in total uint32 hs_total_prev = 0; ///< number of skipped house buildings (HS) in total at the end of last month uint32 hs_last_month = 0; ///< number of skipped house buildings (HS) during last month @@ -22,6 +23,7 @@ public: uint32 houses_reconstructed_last_month = 0; ///< number of houses rebuild last month uint32 houses_demolished_this_month = 0; ///< number of houses demolished this month uint32 houses_demolished_last_month = 0; ///< number of houses demolished last month + }; } // namespace citymania diff --git a/src/openttd.cpp b/src/openttd.cpp index 615ce7f822..82b2924ca3 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -330,6 +330,8 @@ static void LoadIntroGame(bool load_newgrfs = true) ResetWindowSystem(); SetupColoursAndInitialWindow(); + citymania::ResetGame(); + /* Load the default opening screen savegame */ if (SaveOrLoad("opntitle.dat", SLO_LOAD, DFT_GAME_FILE, BASESET_DIR) != SL_OK) { GenerateWorld(GWM_EMPTY, 64, 64); // if failed loading, make empty world. diff --git a/src/saveload/town_sl.cpp b/src/saveload/town_sl.cpp index 6fe1439b4e..0f758779c0 100644 --- a/src/saveload/town_sl.cpp +++ b/src/saveload/town_sl.cpp @@ -17,6 +17,8 @@ #include "saveload.h" #include "newgrf_sl.h" +#include "../citymania/cm_main.hpp" + #include "../safeguards.h" /** @@ -51,6 +53,7 @@ void RebuildTownCaches() UpdateTownCargoes(town); } UpdateTownCargoBitmap(); + citymania::Emit(citymania::event::TownCachesRebuilt()); } /** diff --git a/src/town_cmd.cpp b/src/town_cmd.cpp index 5db55683c7..69fcbeecc8 100644 --- a/src/town_cmd.cpp +++ b/src/town_cmd.cpp @@ -2685,7 +2685,8 @@ void ClearTownHouse(Town *t, TileIndex tile) const HouseSpec *hs = HouseSpec::Get(house); /* Remove population from the town if the house is finished. */ - if (IsHouseCompleted(tile)) { + bool is_completed = IsHouseCompleted(tile); + if (is_completed) { ChangePopulation(t, -hs->population); } @@ -2710,6 +2711,8 @@ void ClearTownHouse(Town *t, TileIndex tile) /* Update cargo acceptance. */ UpdateTownCargoes(t, tile); + + citymania::Emit(citymania::event::HouseCleared{t, tile, hs, is_completed}); } /**