Skip to content

Commit

Permalink
📝 Added healed units percentage in the output file (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedSobhy01 authored May 16, 2024
1 parent 0dbb712 commit d767b1c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
3 changes: 3 additions & 0 deletions DEFS.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ struct GameStatistics
// Destructed Unit Counts
int destructedUnitCounts[UnitType::UNIT_TOTAL];

// Total healed units
int totalHealedUnits;

// Total infected Earth Soldiers
int totalInfectedESCount;

Expand Down
12 changes: 11 additions & 1 deletion Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,11 @@ void Game::countArmyStatistics(GameStatistics& gameStatistics, ArmyType armyType
// Total Army Units Count
gameStatistics.armyStatistics[armyType].totalUnitsCount++;

// Count the healed units
HealableUnit* healableUnit = dynamic_cast<HealableUnit*>(unit);
if (healableUnit && healableUnit->hasBeenHealedBefore())
gameStatistics.totalHealedUnits++;

// Count the infected Earth Soldiers
if (unitTypes[i] == UnitType::ES)
{
Expand Down Expand Up @@ -424,6 +429,11 @@ void Game::countKilledUnitsStatistics(GameStatistics& gameStatistics)
gameStatistics.armyStatistics[armyType].totalUnitsCount++;
gameStatistics.armyStatistics[armyType].totalDestructedUnitsCount++;

// Count the healed units
HealableUnit* healableUnit = dynamic_cast<HealableUnit*>(unit);
if (healableUnit && healableUnit->hasBeenHealedBefore())
gameStatistics.totalHealedUnits++;

// Count the infected Earth Soldiers
if (unitType == UnitType::ES)
{
Expand Down Expand Up @@ -509,6 +519,7 @@ void Game::generateOutputFile(const std::string& outputFileName)
fout << "Destructed EGs/Total EGs = " << calculatePercentage(gameStatistics.destructedUnitCounts[UnitType::EG], gameStatistics.unitCounts[UnitType::EG]) << "%" << std::endl;
fout << "Destructed EHs/Total EHs = " << calculatePercentage(gameStatistics.destructedUnitCounts[UnitType::EH], gameStatistics.unitCounts[UnitType::EH]) << "%" << std::endl;
fout << "Total Destructed Earth Units/Total Earth Units = " << calculatePercentage(gameStatistics.armyStatistics[ArmyType::EARTH].totalDestructedUnitsCount, gameStatistics.armyStatistics[ArmyType::EARTH].totalUnitsCount) << "%" << std::endl;
fout << "Total Healed Units/Total Earth Units = " << calculatePercentage(gameStatistics.totalHealedUnits, gameStatistics.armyStatistics[ArmyType::EARTH].totalUnitsCount) << "%" << std::endl;
fout << "Total Infected ESs/Total Earth Units = " << calculatePercentage(gameStatistics.totalInfectedESCount, gameStatistics.armyStatistics[ArmyType::EARTH].totalUnitsCount) << "%" << std::endl;
fout << "======================================================================" << std::endl;
fout << "Average of First Attack Delay = " << calculateRatio(gameStatistics.armyStatistics[ArmyType::EARTH].totalFirstAttackDelays, gameStatistics.armyStatistics[ArmyType::EARTH].totalUnitsCount) << std::endl;
Expand Down Expand Up @@ -637,7 +648,6 @@ int Game::getUnitsCount(ArmyType armyType, UnitType unitType) const
{
case ArmyType::EARTH:
return earthArmy.getUnitsCount(unitType);

case ArmyType::ALIEN:
return alienArmy.getUnitsCount(unitType);
case ArmyType::EARTH_ALLIED:
Expand Down
10 changes: 9 additions & 1 deletion UnitClasses/HealableUnit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "../Game.h"

HealableUnit::HealableUnit(Game* gamePtr, UnitType unitType, double health, int power, int attackCapacity)
: Unit(gamePtr, unitType, health, power, attackCapacity), UMLjoinTime(-1)
: Unit(gamePtr, unitType, health, power, attackCapacity), UMLjoinTime(-1), healedBefore(false)
{}

bool HealableUnit::needsHeal() const
Expand All @@ -27,8 +27,16 @@ bool HealableUnit::hasBeenInUMLbefore() const
return UMLjoinTime != -1;
}

bool HealableUnit::hasBeenHealedBefore() const
{
return healedBefore;
}

void HealableUnit::receiveHeal(double UHP)
{
// Register that the unit has been healed before
healedBefore = true;

// Infected units get twice the time to get healed
if (dynamic_cast<EarthSoldier*>(this) && dynamic_cast<EarthSoldier*>(this)->isInfected())
health += UHP / 2;
Expand Down
2 changes: 2 additions & 0 deletions UnitClasses/HealableUnit.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class HealableUnit: public Unit
{
protected:
int UMLjoinTime; // Time when the unit joined the UML
bool healedBefore; // Check if the unit has been healed before

public:
HealableUnit(Game*, UnitType, double, int, int);
Expand All @@ -18,6 +19,7 @@ class HealableUnit: public Unit
bool hasWaitedForTooLong() const; // Decides whether to kill or heal the unit based on its wait time
bool isHealed() const; // Checks if the unit's health has increased enough
bool hasBeenInUMLbefore() const; // Check if it has been inside uml before or not
bool hasBeenHealedBefore() const; // Check if the unit has been healed before or not

void receiveHeal(double); // Increase the health of the unit by "UHP"

Expand Down

0 comments on commit d767b1c

Please sign in to comment.