Calculate houses_constructing and real_population

This commit is contained in:
dP
2020-06-20 01:25:33 +03:00
parent 62afdc2ddd
commit ce96863776
8 changed files with 81 additions and 41 deletions

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()));
}

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

View File

@@ -8,6 +8,7 @@ namespace citymania {
extern up<Game> _game;
void ResetGame();
void SwitchToMode(SwitchMode new_mode);
template <typename T>

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

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
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

View File

@@ -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.

View File

@@ -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());
}
/**

View File

@@ -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});
}
/**