1 #include "ResourceCenter.h"
2 
3 #include "../util/Directories.h"
4 #include "../util/Logger.h"
5 #include "../util/OptionsDB.h"
6 #include "../util/AppInterface.h"
7 #include "../Empire/Empire.h"
8 #include "Fleet.h"
9 #include "Planet.h"
10 #include "System.h"
11 #include "Building.h"
12 #include "Enums.h"
13 
14 #include <stdexcept>
15 
16 namespace {
17     static const std::string EMPTY_STRING;
18 }
19 
ResourceCenter()20 ResourceCenter::ResourceCenter() :
21     m_focus(),
22     m_last_turn_focus_changed(INVALID_GAME_TURN),
23     m_focus_turn_initial(),
24     m_last_turn_focus_changed_turn_initial(INVALID_GAME_TURN)
25 {}
26 
~ResourceCenter()27 ResourceCenter::~ResourceCenter()
28 {}
29 
ResourceCenter(const ResourceCenter & rhs)30 ResourceCenter::ResourceCenter(const ResourceCenter& rhs) :
31     m_focus(rhs.m_focus),
32     m_last_turn_focus_changed(rhs.m_last_turn_focus_changed),
33     m_focus_turn_initial(rhs.m_focus_turn_initial),
34     m_last_turn_focus_changed_turn_initial(rhs.m_last_turn_focus_changed_turn_initial)
35 {}
36 
Copy(std::shared_ptr<const ResourceCenter> copied_object,Visibility vis)37 void ResourceCenter::Copy(std::shared_ptr<const ResourceCenter> copied_object, Visibility vis) {
38     if (copied_object.get() == this)
39         return;
40     if (!copied_object) {
41         ErrorLogger() << "ResourceCenter::Copy passed a null object";
42         return;
43     }
44 
45     if (vis >= VIS_PARTIAL_VISIBILITY) {
46         this->m_focus = copied_object->m_focus;
47         this->m_last_turn_focus_changed = copied_object->m_last_turn_focus_changed;
48         this->m_focus_turn_initial = copied_object->m_focus_turn_initial;
49         this->m_last_turn_focus_changed_turn_initial = copied_object->m_last_turn_focus_changed_turn_initial;
50     }
51 }
52 
Copy(std::shared_ptr<const ResourceCenter> copied_object)53 void ResourceCenter::Copy(std::shared_ptr<const ResourceCenter> copied_object)
54 { Copy(copied_object, VIS_FULL_VISIBILITY); }
55 
Init()56 void ResourceCenter::Init() {
57     //DebugLogger() << "ResourceCenter::Init";
58     AddMeter(METER_INDUSTRY);
59     AddMeter(METER_RESEARCH);
60     AddMeter(METER_TRADE);
61     AddMeter(METER_CONSTRUCTION);
62     AddMeter(METER_TARGET_INDUSTRY);
63     AddMeter(METER_TARGET_RESEARCH);
64     AddMeter(METER_TARGET_TRADE);
65     AddMeter(METER_TARGET_CONSTRUCTION);
66     m_focus.clear();
67     m_last_turn_focus_changed = INVALID_GAME_TURN;
68     m_focus_turn_initial.clear();
69     m_last_turn_focus_changed_turn_initial = INVALID_GAME_TURN;
70 }
71 
Focus() const72 const std::string& ResourceCenter::Focus() const
73 { return m_focus; }
74 
TurnsSinceFocusChange() const75 int ResourceCenter::TurnsSinceFocusChange() const {
76     if (m_last_turn_focus_changed == INVALID_GAME_TURN)
77         return 0;
78     int current_turn = CurrentTurn();
79     if (current_turn == INVALID_GAME_TURN)
80         return 0;
81     return current_turn - m_last_turn_focus_changed;
82 }
83 
AvailableFoci() const84 std::vector<std::string> ResourceCenter::AvailableFoci() const
85 { return std::vector<std::string>(); }
86 
FocusIcon(const std::string & focus_name) const87 const std::string& ResourceCenter::FocusIcon(const std::string& focus_name) const
88 { return EMPTY_STRING; }
89 
Dump(unsigned short ntabs) const90 std::string ResourceCenter::Dump(unsigned short ntabs) const {
91     std::stringstream os;
92     os << "ResourceCenter focus: " << m_focus << " last changed on turn: " << m_last_turn_focus_changed;
93     return os.str();
94 }
95 
SetFocus(const std::string & focus)96 void ResourceCenter::SetFocus(const std::string& focus) {
97     if (focus == m_focus)
98         return;
99     if (focus.empty()) {
100         ClearFocus();
101         return;
102     }
103     auto avail_foci = AvailableFoci();
104     auto foci_it = std::find(avail_foci.begin(), avail_foci.end(), focus);
105     if (foci_it != avail_foci.end()) {
106         m_focus = focus;
107         if (m_focus == m_focus_turn_initial)
108             m_last_turn_focus_changed = m_last_turn_focus_changed_turn_initial;
109         else
110             m_last_turn_focus_changed = CurrentTurn();
111         ResourceCenterChangedSignal();
112         return;
113     }
114     ErrorLogger() << "ResourceCenter::SetFocus Exploiter!-- unavailable focus " << focus << " attempted to be set for object w/ dump string: " << Dump();
115 }
116 
ClearFocus()117 void ResourceCenter::ClearFocus() {
118     m_focus.clear();
119     m_last_turn_focus_changed = CurrentTurn();
120     ResourceCenterChangedSignal();
121 }
122 
UpdateFocusHistory()123 void ResourceCenter::UpdateFocusHistory() {
124     TraceLogger() << "ResourceCenter::UpdateFocusHistory: focus: " << m_focus
125                   << "  initial focus: " << m_focus_turn_initial
126                   << "  turns since change initial: " << m_last_turn_focus_changed_turn_initial;
127     if (m_focus != m_focus_turn_initial) {
128         m_focus_turn_initial = m_focus;
129         m_last_turn_focus_changed_turn_initial = m_last_turn_focus_changed;
130     }
131 }
132 
ResourceCenterResetTargetMaxUnpairedMeters()133 void ResourceCenter::ResourceCenterResetTargetMaxUnpairedMeters() {
134     GetMeter(METER_TARGET_INDUSTRY)->ResetCurrent();
135     GetMeter(METER_TARGET_RESEARCH)->ResetCurrent();
136     GetMeter(METER_TARGET_TRADE)->ResetCurrent();
137     GetMeter(METER_TARGET_CONSTRUCTION)->ResetCurrent();
138 }
139 
ResourceCenterClampMeters()140 void ResourceCenter::ResourceCenterClampMeters() {
141     GetMeter(METER_TARGET_INDUSTRY)->ClampCurrentToRange();
142     GetMeter(METER_TARGET_RESEARCH)->ClampCurrentToRange();
143     GetMeter(METER_TARGET_TRADE)->ClampCurrentToRange();
144     GetMeter(METER_TARGET_CONSTRUCTION)->ClampCurrentToRange();
145 
146     GetMeter(METER_INDUSTRY)->ClampCurrentToRange();
147     GetMeter(METER_RESEARCH)->ClampCurrentToRange();
148     GetMeter(METER_TRADE)->ClampCurrentToRange();
149     GetMeter(METER_CONSTRUCTION)->ClampCurrentToRange();
150 }
151 
Reset()152 void ResourceCenter::Reset() {
153     m_focus.clear();
154     m_last_turn_focus_changed = INVALID_GAME_TURN;
155 
156     GetMeter(METER_INDUSTRY)->Reset();
157     GetMeter(METER_RESEARCH)->Reset();
158     GetMeter(METER_TRADE)->Reset();
159     GetMeter(METER_CONSTRUCTION)->Reset();
160 
161     GetMeter(METER_TARGET_INDUSTRY)->Reset();
162     GetMeter(METER_TARGET_RESEARCH)->Reset();
163     GetMeter(METER_TARGET_TRADE)->Reset();
164     GetMeter(METER_TARGET_CONSTRUCTION)->Reset();
165 }
166