1 #include "PopCenter.h"
2 
3 #include "../util/OptionsDB.h"
4 #include "../util/Directories.h"
5 #include "../util/Logger.h"
6 #include "Meter.h"
7 #include "Enums.h"
8 
9 #include <algorithm>
10 #include <stdexcept>
11 
12 namespace {
13     const double MINIMUM_POP_CENTER_POPULATION = 0.01001;  // rounds up to 0.1 when showing 2 digits, down to 0.05 or 50.0 when showing 3
14 }
15 
16 class Species;
17 const Species* GetSpecies(const std::string& name);
18 
PopCenter(const std::string & species_name)19 PopCenter::PopCenter(const std::string& species_name) :
20     m_species_name(species_name)
21 {}
22 
PopCenter()23 PopCenter::PopCenter()
24 {}
25 
~PopCenter()26 PopCenter::~PopCenter()
27 {}
28 
Copy(std::shared_ptr<const PopCenter> copied_object,Visibility vis)29 void PopCenter::Copy(std::shared_ptr<const PopCenter> copied_object, Visibility vis) {
30     if (copied_object.get() == this)
31         return;
32     if (!copied_object) {
33         ErrorLogger() << "PopCenter::Copy passed a null object";
34         return;
35     }
36 
37     if (vis >= VIS_PARTIAL_VISIBILITY) {
38         this->m_species_name =      copied_object->m_species_name;
39     }
40 }
41 
Copy(std::shared_ptr<const PopCenter> copied_object)42 void PopCenter::Copy(std::shared_ptr<const PopCenter> copied_object)
43 { Copy(copied_object, VIS_FULL_VISIBILITY); }
44 
Init()45 void PopCenter::Init() {
46     AddMeter(METER_POPULATION);
47     AddMeter(METER_TARGET_POPULATION);
48     AddMeter(METER_HAPPINESS);
49     AddMeter(METER_TARGET_HAPPINESS);
50 }
51 
Dump(unsigned short ntabs) const52 std::string PopCenter::Dump(unsigned short ntabs) const {
53     std::stringstream os;
54     os << " species: " << m_species_name << "  ";
55     return os.str();
56 }
57 
Populated() const58 bool PopCenter::Populated() const
59 { return GetMeter(METER_POPULATION)->Current() >= MINIMUM_POP_CENTER_POPULATION; }
60 
PopCenterResetTargetMaxUnpairedMeters()61 void PopCenter::PopCenterResetTargetMaxUnpairedMeters() {
62     GetMeter(METER_TARGET_POPULATION)->ResetCurrent();
63     GetMeter(METER_TARGET_HAPPINESS)->ResetCurrent();
64 }
65 
PopCenterPopGrowthProductionResearchPhase()66 void PopCenter::PopCenterPopGrowthProductionResearchPhase() {
67     if (m_species_name.empty()) {
68         // No changes to population or happiness
69         return;
70     }
71 
72     // Should be run after meter update but before a backpropagation, so check current, not initial, meter values
73 
74     if (!Populated()) {
75         // if population falls below threshold, kill off the remainder
76         Depopulate();
77     }
78 }
79 
PopCenterClampMeters()80 void PopCenter::PopCenterClampMeters()
81 { GetMeter(METER_POPULATION)->ClampCurrentToRange(); }
82 
Reset()83 void PopCenter::Reset() {
84     GetMeter(METER_POPULATION)->Reset();
85     GetMeter(METER_TARGET_POPULATION)->Reset();
86     GetMeter(METER_HAPPINESS)->Reset();
87     GetMeter(METER_TARGET_HAPPINESS)->Reset();
88     m_species_name.clear();
89 }
90 
Depopulate()91 void PopCenter::Depopulate() {
92     GetMeter(METER_POPULATION)->Reset();
93     GetMeter(METER_HAPPINESS)->Reset();
94 }
95 
SetSpecies(const std::string & species_name)96 void PopCenter::SetSpecies(const std::string& species_name) {
97     if (!species_name.empty() && !GetSpecies(species_name))
98         ErrorLogger() << "PopCenter::SetSpecies couldn't get species with name " << species_name;
99     m_species_name = species_name;
100 }
101