Merge real population from cmbase

This commit is contained in:
dP
2020-06-20 01:36:12 +03:00
12 changed files with 84 additions and 65 deletions
+14 -6
View File
@@ -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<std::type_index, up<TypeDispatcherBase>> dispacthers;
std::map<std::type_index, up<TypeDispatcherBase>> dispatchers;
template<typename T>
TypeDispatcher<T> &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<TypeDispatcher<T>>();
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<TypeDispatcher<T> *>((*p).second.get()));
}
+22
View File
@@ -50,12 +50,34 @@ Game::Game() {
this->events.listen<event::HouseBuilt>([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<event::HouseCleared>([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<event::HouseCompleted>([this] (const event::HouseCompleted &event) {
event.town->cm.houses_constructing--;
});
this->events.listen<event::TownCachesRebuilt>([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
-15
View File
@@ -35,21 +35,6 @@ public:
auto bs = (b == this->towns_growth_tiles.end() ? TownGrowthTileState::NONE : (*b).second);
return max(as, bs);
}
void rebuild_town_caches() {
for (Town *town : Town::Iterate()) {
town->cm.houses_constructing = 0;
town->cache.potential_pop = 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->cache.potential_pop += HouseSpec::Get(house_id)->population;
}
}
};
} // namespace citymania
+1
View File
@@ -8,6 +8,7 @@ namespace citymania {
extern up<Game> _game;
void ResetGame();
void SwitchToMode(SwitchMode new_mode);
template <typename T>
+33 -34
View File
@@ -6,38 +6,6 @@
#include <type_traits>
#include <utility>
/* C++14 implementation of make_unique */
namespace std {
template<class T> struct _Unique_if {
typedef unique_ptr<T> _Single_object;
};
template<class T> struct _Unique_if<T[]> {
typedef unique_ptr<T[]> _Unknown_bound;
};
template<class T, size_t N> struct _Unique_if<T[N]> {
typedef void _Known_bound;
};
template<class T, class... Args>
typename _Unique_if<T>::_Single_object
make_unique(Args&&... args) {
return unique_ptr<T>(new T(std::forward<Args>(args)...));
}
template<class T>
typename _Unique_if<T>::_Unknown_bound
make_unique(size_t n) {
typedef typename remove_extent<T>::type U;
return unique_ptr<T>(new U[n]());
}
template<class T, class... Args>
typename _Unique_if<T>::_Known_bound
make_unique(Args&&...) = delete;
} // namespace std
namespace citymania {
// Make smart pointers easier to type
@@ -45,8 +13,39 @@ template<class T> using up=std::unique_ptr<T>;
template<class T> using sp=std::shared_ptr<T>;
template<class T> using wp=std::weak_ptr<T>;
template<typename T> const auto make_up = std::make_unique<T>;
template<typename T> const auto make_sp = std::make_shared<T>;
/* C++14 implementation of make_unique */
template<class T> struct _Unique_if {
typedef std::unique_ptr<T> _Single_object;
};
template<class T> struct _Unique_if<T[]> {
typedef std::unique_ptr<T[]> _Unknown_bound;
};
template<class T, size_t N> struct _Unique_if<T[N]> {
typedef void _Known_bound;
};
template<class T, class... Args>
typename _Unique_if<T>::_Single_object
make_up(Args&&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
template<class T>
typename _Unique_if<T>::_Unknown_bound
make_up(size_t n) {
typedef typename std::remove_extent<T>::type U;
return std::unique_ptr<T>(new U[n]());
}
template<class T, class... Args>
typename _Unique_if<T>::_Known_bound
make_up(Args&&...) = delete;
// template<typename T> const auto make_up = std::make_unique<T>;
// template<typename T> const auto make_sp = std::make_shared<T>;
} // namespace citymania
+1
View File
@@ -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
uint16 hs_total_prev = 0; ///< number of skipped house buildings (HS) in total at the end of last month
uint16 hs_last_month = 0; ///< number of skipped house buildings (HS) during last month
+2
View File
@@ -331,6 +331,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.
+1 -1
View File
@@ -88,4 +88,4 @@ const byte _openttd_revision_tagged = 1;
const uint32 _openttd_newgrf_version = 1 << 28 | 10 << 24 | 0 << 20 | 1 << 19 | 28004;
const char _citymania_version[] = "20200608-master-m2816c38d3d 08.06.20";
const char _citymania_version[] = "20200619-master-m2010f236c6 20.06.20";
+2 -1
View File
@@ -18,6 +18,7 @@
#include "newgrf_sl.h"
#include "../citymania/cm_main.hpp"
#include "../safeguards.h"
/**
@@ -52,7 +53,7 @@ void RebuildTownCaches()
UpdateTownCargoes(town);
}
UpdateTownCargoBitmap();
citymania::_game->rebuild_town_caches();
citymania::Emit(citymania::event::TownCachesRebuilt());
}
/**
-1
View File
@@ -50,7 +50,6 @@ extern TownPool _town_pool;
struct TownCache {
uint32 num_houses; ///< Amount of houses
uint32 population; ///< Current population of people
uint32 potential_pop; ///< Potential population (if all houses are finished)
TrackedViewportSign sign; ///< Location of name sign, UpdateVirtCoord updates this
PartOfSubsidy part_of_subsidy; ///< Is this town a source/destination of a subsidy?
uint32 squared_town_zone_radius[HZB_END]; ///< UpdateTownRadius updates this given the house count
+4 -3
View File
@@ -2865,7 +2865,6 @@ static bool BuildTownHouse(Town *t, TileIndex tile)
/* build the house */
t->cache.num_houses++;
t->cache.potential_pop += hs->population;
/* Special houses that there can be only one of. */
t->flags |= oneof;
@@ -2959,12 +2958,12 @@ 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);
}
t->cache.num_houses--;
t->cache.potential_pop -= hs->population;
/* Clear flags for houses that only may exist once/town. */
if (hs->building_flags & BUILDING_IS_CHURCH) {
@@ -2986,6 +2985,8 @@ void ClearTownHouse(Town *t, TileIndex tile)
/* Update cargo acceptance. */
UpdateTownCargoes(t, tile);
citymania::Emit(citymania::event::HouseCleared{t, tile, hs, is_completed});
}
/**
+4 -4
View File
@@ -817,8 +817,8 @@ private:
/** Sort by real population (default descending, as big towns are of the most interest). */
static bool TownRealPopulationSorter(const Town * const &a, const Town * const &b)
{
uint32 a_population = a->cache.potential_pop;
uint32 b_population = b->cache.potential_pop;
uint32 a_population = a->cm.real_population;
uint32 b_population = b->cm.real_population;
if (a_population == b_population) return TownDirectoryWindow::TownNameSorter(a, b);
return a_population < b_population;
}
@@ -944,7 +944,7 @@ public:
SetDParam(0, t->index);
SetDParam(1, t->cache.population);
SetDParam(2, t->cache.potential_pop);
SetDParam(2, t->cm.real_population);
SetDParam(3, t->cache.num_houses);
/* CITIES DIFFERENT COLOUR*/
DrawString(text_left, text_right, y + (this->resize.step_height - FONT_HEIGHT_NORMAL) / 2, t->larger_town ? STR_TOWN_DIRECTORY_CITY_COLOUR : STR_TOWN_DIRECTORY_TOWN_COLOUR);
@@ -1390,7 +1390,7 @@ void InitializeTownGui()
//CB
static void DrawExtraTownInfo (const Rect &r, uint &y, Town *town, uint line, bool show_house_states_info) {
//real pop and rating
SetDParam(0, town->cache.potential_pop);
SetDParam(0, town->cm.real_population);
SetDParam(1, town->ratings[_current_company]);
DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += line, STR_TOWN_VIEW_REALPOP_RATE);
//town stats