Skip to content

Commit e341033

Browse files
authored
Merge pull request #1509 from OutpostUniverse/Refactor/Morale_Change_Reasons
Refactor/morale change reasons
2 parents 5bc953f + 8dabcac commit e341033

File tree

6 files changed

+58
-51
lines changed

6 files changed

+58
-51
lines changed

OPHD/States/MapViewStateIO.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ void MapViewState::save(const std::string& filePath)
182182
));
183183

184184
auto moraleChangeReasons = new NAS2D::Xml::XmlElement("morale_change");
185-
auto& moraleChangeList = mPopulationPanel.moraleReasonList();
185+
auto& moraleChangeList = mMorale.moraleChangeJournal();
186186
for (auto& [message, value] : moraleChangeList)
187187
{
188188
moraleChangeReasons->linkEndChild(NAS2D::dictionaryToAttributes(
@@ -229,7 +229,7 @@ void MapViewState::load(const std::string& filePath)
229229
mBtnToggleRouteOverlay.toggle(false);
230230
mBtnTogglePoliceOverlay.toggle(false);
231231
mBtnToggleHeightmap.toggle(false);
232-
mPopulationPanel.clearMoraleReasons();
232+
mMorale.closeJournal();
233233

234234
if (!NAS2D::Utility<NAS2D::Filesystem>::get().exists(filePath))
235235
{
@@ -596,6 +596,6 @@ void MapViewState::readMoraleChanges(NAS2D::Xml::XmlElement* moraleChangeElement
596596
const auto message = dictionary.get("message");
597597
const auto val = dictionary.get<int>("val");
598598

599-
mPopulationPanel.addMoraleReason(message, val);
599+
mMorale.journalMoraleChange({message, val});
600600
}
601601
}

OPHD/States/MapViewStateTurn.cpp

+17-28
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ void MapViewState::updateCommercial()
171171
}
172172
}
173173

174-
mMorale.adjustMorale(commercialCount - luxuryCount);
174+
mMorale.journalMoraleChange({"Commercial - Luxury", commercialCount - luxuryCount});
175175
}
176176

177177

@@ -203,43 +203,30 @@ void MapViewState::updateMorale()
203203
}
204204

205205
// positive
206-
mMorale.adjustMorale(birthCount);
207-
mMorale.adjustMorale(parkCount);
208-
mMorale.adjustMorale(recreationCount);
209-
mMorale.adjustMorale(commercialCount);
206+
mMorale.journalMoraleChange({moraleString(MoraleIndexs::Births), birthCount});
207+
mMorale.journalMoraleChange({moraleString(MoraleIndexs::Parks), parkCount});
208+
mMorale.journalMoraleChange({moraleString(MoraleIndexs::Recreation), recreationCount});
209+
mMorale.journalMoraleChange({moraleString(MoraleIndexs::Commercial), commercialCount});
210210

211211
// negative
212-
mMorale.adjustMorale(-deathCount);
213-
mMorale.adjustMorale(-residentialOverCapacityHit);
214-
mMorale.adjustMorale(-bioWasteAccumulation * 2);
215-
mMorale.adjustMorale(-structuresDisabled);
216-
mMorale.adjustMorale(-structuresDestroyed);
217-
mMorale.adjustMorale(-foodProductionHit);
218-
219-
mPopulationPanel.clearMoraleReasons();
220-
mPopulationPanel.addMoraleReason(moraleString(MoraleIndexs::Births), birthCount);
221-
mPopulationPanel.addMoraleReason(moraleString(MoraleIndexs::Deaths), -deathCount);
222-
mPopulationPanel.addMoraleReason(moraleString(MoraleIndexs::NoFoodProduction), -foodProductionHit);
223-
mPopulationPanel.addMoraleReason(moraleString(MoraleIndexs::Parks), parkCount);
224-
mPopulationPanel.addMoraleReason(moraleString(MoraleIndexs::Recreation), recreationCount);
225-
mPopulationPanel.addMoraleReason(moraleString(MoraleIndexs::Commercial), commercialCount);
226-
mPopulationPanel.addMoraleReason(moraleString(MoraleIndexs::ResidentialOverflow), -residentialOverCapacityHit);
227-
mPopulationPanel.addMoraleReason(moraleString(MoraleIndexs::BiowasteOverflow), bioWasteAccumulation * -2);
228-
mPopulationPanel.addMoraleReason(moraleString(MoraleIndexs::StructuresDisabled), -structuresDisabled);
229-
mPopulationPanel.addMoraleReason(moraleString(MoraleIndexs::StructuresDestroyed), -structuresDestroyed);
212+
mMorale.journalMoraleChange({moraleString(MoraleIndexs::Deaths), -deathCount});
213+
mMorale.journalMoraleChange({moraleString(MoraleIndexs::ResidentialOverflow), -residentialOverCapacityHit});
214+
mMorale.journalMoraleChange({moraleString(MoraleIndexs::BiowasteOverflow), -bioWasteAccumulation * 2}); // TODO 2 is a magic number
215+
mMorale.journalMoraleChange({moraleString(MoraleIndexs::StructuresDisabled), -structuresDisabled});
216+
mMorale.journalMoraleChange({moraleString(MoraleIndexs::StructuresDestroyed), -structuresDestroyed});
217+
mMorale.journalMoraleChange({"Food Production Issues", -foodProductionHit});
218+
230219

231220
for (const auto& moraleReason : mCrimeRateUpdate.moraleChanges())
232221
{
233-
mPopulationPanel.addMoraleReason(moraleReason.first, moraleReason.second);
234-
mMorale.adjustMorale(moraleReason.second);
222+
mMorale.journalMoraleChange({moraleReason.first, moraleReason.second});
235223
}
236224

237225
mPopulationPanel.crimeRate(mCrimeRateUpdate.meanCrimeRate());
238226

239227
for (const auto& moraleReason : mCrimeExecution.moraleChanges())
240228
{
241-
mPopulationPanel.addMoraleReason(moraleReason.first, moraleReason.second);
242-
mMorale.adjustMorale(moraleReason.second);
229+
mMorale.journalMoraleChange({moraleReason.first, moraleReason.second});
243230
}
244231
}
245232

@@ -380,7 +367,7 @@ void MapViewState::checkColonyShip()
380367
{
381368
if (mLandersColonist > 0 || mLandersCargo > 0)
382369
{
383-
mMorale.adjustMorale(-(mLandersColonist * 50) * ColonyShipDeorbitMoraleLossMultiplier.at(mDifficulty));
370+
mMorale.journalMoraleChange({"Deorbit Disaster!", -(mLandersColonist * 50) * ColonyShipDeorbitMoraleLossMultiplier.at(mDifficulty)});
384371

385372
mLandersColonist = 0;
386373
mLandersCargo = 0;
@@ -695,6 +682,8 @@ void MapViewState::nextTurn()
695682
mNotificationWindow.hide();
696683
mNotificationArea.clear();
697684

685+
mMorale.closeJournal();
686+
698687
clearMode();
699688

700689
mPopulationPool.clear();

OPHD/UI/PopulationPanel.cpp

+5-10
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,6 @@ PopulationPanel::PopulationPanel(const Population& pop, const PopulationPool& po
9393
size({windowWidth, windowHeight});
9494
}
9595

96-
void PopulationPanel::addMoraleReason(const std::string& str, int val)
97-
{
98-
if (val == 0) { return; }
99-
mMoraleChangeReasons.push_back(std::make_pair(str, val));
100-
}
101-
10296

10397
void PopulationPanel::update()
10498
{
@@ -187,14 +181,15 @@ void PopulationPanel::update()
187181

188182
position.y += constants::Margin;
189183

190-
for (auto& item : mMoraleChangeReasons)
184+
for (auto& entry : mMorale.moraleChangeJournal())
191185
{
192-
renderer.drawText(mFont, item.first, position);
186+
if (entry.value == 0) { continue; }
187+
renderer.drawText(mFont, entry.description , position);
193188

194-
const auto text = formatDiff(item.second);
189+
const auto text = formatDiff(entry.value);
195190
const NAS2D::Point<int> labelPosition = {rect().position.x + rect().size.x - mFont.width(text) - 5 , position.y};
196191

197-
renderer.drawText(mFont, text, labelPosition, trend[trendIndex(item.second)]);
192+
renderer.drawText(mFont, text, labelPosition, trend[trendIndex(entry.value)]);
198193
position.y += fontHeight;
199194
}
200195
}

OPHD/UI/PopulationPanel.h

-7
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,6 @@ class PopulationPanel : public Control
2828

2929
void crimeRate(int val) { mCrimeRate = val; }
3030
int crimeRate() const { return mCrimeRate; }
31-
32-
void addMoraleReason(const std::string& str, int val);
33-
34-
const auto& moraleReasonList() const { return mMoraleChangeReasons; }
35-
36-
void clearMoraleReasons() { mMoraleChangeReasons.clear(); }
37-
3831
void update() override;
3932

4033
private:

libOPHD/Population/Morale.cpp

+19-2
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,32 @@ int Morale::previousMorale() const
3030
}
3131

3232

33-
void Morale::adjustMorale(int diff)
33+
void Morale::journalMoraleChange(const MoraleChangeEntry& entry)
3434
{
35-
mMoraleAccumulator += diff;
35+
mMoraleChangeJournal.push_back(entry);
36+
}
37+
38+
const std::vector<MoraleChangeEntry>& Morale::moraleChangeJournal() const
39+
{
40+
return mMoraleChangeJournal;
41+
}
42+
43+
44+
void Morale::closeJournal()
45+
{
46+
mMoraleChangeJournal.clear();
3647
}
3748

3849

3950
void Morale::commitMoraleChanges()
4051
{
4152
mPreviousMorale = currentMorale();
53+
54+
for (const auto& entry : mMoraleChangeJournal)
55+
{
56+
mMoraleAccumulator += entry.value;
57+
}
58+
4259
mCurrentMorale = std::clamp(mCurrentMorale + mMoraleAccumulator, 0, constants::MaximumMorale);
4360
mMoraleAccumulator = 0;
4461
}

libOPHD/Population/Morale.h

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#pragma once
22

33
#include <cstdint>
4+
#include <vector>
5+
#include <string>
46

57
/**
68
* Morale modifier values.
@@ -13,14 +15,24 @@ struct MoraleModifier
1315
int mortalityRate{0};
1416
};
1517

18+
struct MoraleChangeEntry
19+
{
20+
std::string description{};
21+
int value{0};
22+
};
23+
1624
class Morale
1725
{
1826
public:
1927
Morale();
2028
Morale(int currentMorale, int previousMorale);
2129
int currentMorale() const;
2230
int previousMorale() const;
23-
void adjustMorale(int diff);
31+
void journalMoraleChange(const MoraleChangeEntry& entry);
32+
const std::vector<MoraleChangeEntry>& moraleChangeJournal() const;
33+
34+
// Clears the morale change journal vector of all entries
35+
void closeJournal();
2436

2537
// Should be called after all morale calculations are done for the turn to apply the morale changes but before its needed to update the UI
2638
void commitMoraleChanges();
@@ -30,4 +42,5 @@ class Morale
3042
int mCurrentMorale;
3143
int mPreviousMorale;
3244
int mMoraleAccumulator;
45+
std::vector<MoraleChangeEntry> mMoraleChangeJournal;
3346
};

0 commit comments

Comments
 (0)