Skip to content

Commit

Permalink
Merge pull request #1497 from OutpostUniverse/refactorCrimeExecution
Browse files Browse the repository at this point in the history
Refactor and bug fix for `CrimeExecution`
  • Loading branch information
DanRStevens authored Jan 10, 2025
2 parents 21ed5cd + 7245e80 commit 6878dd7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 53 deletions.
78 changes: 45 additions & 33 deletions OPHD/States/CrimeExecution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,48 @@
#include <NAS2D/Utility.h>


CrimeExecution::CrimeExecution(NotificationArea& notificationArea) : mNotificationArea(notificationArea) {}
namespace
{
constexpr int stealingScale = 4;
const std::map<Difficulty, int> stealingMultipliers
{
{Difficulty::Beginner, 2},
{Difficulty::Easy, 3},
{Difficulty::Medium, 4},
{Difficulty::Hard, 6}
};

const std::vector<std::string> stealingResoureReasons
{
"There are no identified suspects",
"An investigation has been opened",
"A local crime syndicate is under investigation",
"A suspect was aprehended but the goods remain unaccounted for",
"A separatist political movement has claimed responsibility",
"The rebel faction is suspected in preparation for a splinter colony"
};


std::string getReasonForStealing()
{
return stealingResoureReasons[randomNumber.generate<std::size_t>(0, stealingResoureReasons.size() - 1)];
}


int calcAmountForStealing(Difficulty difficulty, int low, int high, int max)
{
auto stealRandom = randomNumber.generate(low, high);
auto stealModified = stealRandom * stealingMultipliers.at(difficulty) / stealingScale;
auto stealClipped = std::min(stealModified, max);
return stealClipped;
}
}


CrimeExecution::CrimeExecution(NotificationArea& notificationArea) :
mNotificationArea{notificationArea}
{
}


void CrimeExecution::executeCrimes(const std::vector<Structure*>& structuresCommittingCrime)
Expand All @@ -18,11 +59,6 @@ void CrimeExecution::executeCrimes(const std::vector<Structure*>& structuresComm

for (auto& structure : structuresCommittingCrime)
{
if (structure == nullptr)
{
continue;
}

switch (structure->structureId())
{
case StructureID::SID_AGRIDOME:
Expand All @@ -48,13 +84,8 @@ void CrimeExecution::stealFood(FoodProduction& structure)
{
if (structure.foodLevel() > 0)
{
int foodStolen = calcAmountForStealing(5, 15);
if (foodStolen > structure.foodLevel())
{
foodStolen = structure.foodLevel();
}

structure.foodLevel(-foodStolen);
int foodStolen = calcAmountForStealing(mDifficulty, 5, 15, structure.foodLevel());
structure.foodLevel(structure.foodLevel() - foodStolen);

const auto& structureTile = NAS2D::Utility<StructureManager>::get().tileFromStructure(&structure);

Expand Down Expand Up @@ -90,12 +121,7 @@ void CrimeExecution::stealResources(Structure& structure, const std::array<std::

auto indexToStealFrom = randomNumber.generate<std::size_t>(0, resourceIndicesWithStock.size() - 1);

int amountStolen = calcAmountForStealing(2, 5);
if (amountStolen > structure.storage().resources[indexToStealFrom])
{
amountStolen = structure.storage().resources[indexToStealFrom];
}

int amountStolen = calcAmountForStealing(mDifficulty, 2, 5, structure.storage().resources[indexToStealFrom]);
structure.storage().resources[indexToStealFrom] -= amountStolen;

const auto& structureTile = NAS2D::Utility<StructureManager>::get().tileFromStructure(&structure);
Expand All @@ -120,17 +146,3 @@ void CrimeExecution::vandalize(Structure& structure)
structureTile.xyz(),
NotificationArea::NotificationType::Warning});
}


int CrimeExecution::calcAmountForStealing(int unadjustedMin, int unadjustedMax)
{
auto amountToSteal = randomNumber.generate(unadjustedMin, unadjustedMax);

return static_cast<int>(stealingMultipliers.at(mDifficulty) * amountToSteal);
}


std::string CrimeExecution::getReasonForStealing()
{
return stealingResoureReasons[randomNumber.generate<std::size_t>(0, stealingResoureReasons.size() - 1)];
}
20 changes: 0 additions & 20 deletions OPHD/States/CrimeExecution.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,9 @@ class CrimeExecution
std::vector<std::pair<std::string, int>> moraleChanges() const { return mMoraleChanges; }

private:
const static inline std::map<Difficulty, double> stealingMultipliers
{
{Difficulty::Beginner, 0.5f},
{Difficulty::Easy, 0.75f},
{Difficulty::Medium, 1.0f},
{Difficulty::Hard, 1.5f}
};

const static inline std::vector<std::string> stealingResoureReasons
{
"There are no identified suspects",
"An investigation has been opened",
"A local crime syndicate is under investigation",
"A suspect was aprehended but the goods remain unaccounted for",
"A separatist political movement has claimed responsibility",
"The rebel faction is suspected in preparation for a splinter colony"
};

Difficulty mDifficulty{Difficulty::Medium};
NotificationArea& mNotificationArea;
std::vector<std::pair<std::string, int>> mMoraleChanges;

void stealResources(Structure& structure, const std::array<std::string, 4>& resourceNames);
int calcAmountForStealing(int unadjustedMin, int unadjustedMax);
std::string getReasonForStealing();
};

0 comments on commit 6878dd7

Please sign in to comment.