Skip to content

Commit

Permalink
fix: critical hit damage calculation (#1913)
Browse files Browse the repository at this point in the history
Resolves #1893
Resolves #1888
  • Loading branch information
luan authored Nov 25, 2023
1 parent 3432915 commit 8100402
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
9 changes: 4 additions & 5 deletions src/creatures/combat/combat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2024,13 +2024,12 @@ void Combat::applyExtensions(std::shared_ptr<Creature> caster, std::shared_ptr<C

// Critical hit
uint16_t chance = 0;
int32_t multiplier = 50;
int32_t bonus = 50;
auto player = caster->getPlayer();
auto monster = caster->getMonster();
if (player) {
chance = player->getSkillLevel(SKILL_CRITICAL_HIT_CHANCE);
multiplier = player->getSkillLevel(SKILL_CRITICAL_HIT_DAMAGE);

bonus = player->getSkillLevel(SKILL_CRITICAL_HIT_DAMAGE);
if (target) {
uint16_t playerCharmRaceid = player->parseRacebyCharm(CHARM_LOW, false, 0);
if (playerCharmRaceid != 0) {
Expand All @@ -2048,8 +2047,8 @@ void Combat::applyExtensions(std::shared_ptr<Creature> caster, std::shared_ptr<C
chance = monster->critChance();
}

multiplier += damage.criticalDamage;
multiplier = 1 + multiplier / 100;
bonus += damage.criticalDamage;
double multiplier = 1.0 + static_cast<double>(bonus) / 100;
chance += (uint16_t)damage.criticalChance;

if (chance != 0 && uniform_random(1, 100) <= chance) {
Expand Down
20 changes: 11 additions & 9 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6625,10 +6625,8 @@ bool Game::combatChangeHealth(std::shared_ptr<Creature> attacker, std::shared_pt
}
}

std::string attackMsg = fmt::format("{} attack", damage.critical ? "critical " : " ");
std::stringstream ss;
ss << (damage.critical ? "critical " : " ") << "attack";
std::string attackMsg = ss.str();
ss.str({});

if (target->hasCondition(CONDITION_MANASHIELD) && damage.primary.type != COMBAT_UNDEFINEDDAMAGE) {
int32_t manaDamage = std::min<int32_t>(target->getMana(), healthChange);
Expand Down Expand Up @@ -6915,17 +6913,19 @@ void Game::buildMessageAsSpectator(
) const {
if (spectatorMessage.empty()) {
ss.str({});
auto attackMsg = damage.critical ? "critical " : "";
auto article = damage.critical ? "a" : "an";
ss << ucfirst(target->getNameDescription()) << " loses " << damageString;
if (attacker) {
ss << " due to ";
if (attacker == target) {
if (targetPlayer) {
ss << targetPlayer->getPossessivePronoun() << " own attack";
ss << targetPlayer->getPossessivePronoun() << " own " << attackMsg << "attack";
} else {
ss << "its own attack";
ss << "its own " << attackMsg << "attack";
}
} else {
ss << "an attack by " << attacker->getNameDescription();
ss << article << " " << attackMsg << "attack by " << attacker->getNameDescription();
}
}
ss << '.';
Expand All @@ -6945,13 +6945,15 @@ void Game::buildMessageAsTarget(
const std::string &damageString
) const {
ss.str({});
auto attackMsg = damage.critical ? "critical " : "";
auto article = damage.critical ? "a" : "an";
ss << "You lose " << damageString;
if (!attacker) {
ss << '.';
} else if (targetPlayer == attackerPlayer) {
ss << " due to your own attack.";
ss << " due to your own " << attackMsg << "attack.";
} else {
ss << " due to an attack by " << attacker->getNameDescription() << '.';
ss << " due to " << article << " " << attackMsg << "attack by " << attacker->getNameDescription() << '.';
}
if (damage.extension) {
ss << " " << damage.exString;
Expand All @@ -6965,7 +6967,7 @@ void Game::buildMessageAsAttacker(
std::stringstream &ss, const std::string &damageString
) const {
ss.str({});
ss << ucfirst(target->getNameDescription()) << " loses " << damageString << " due to your attack.";
ss << ucfirst(target->getNameDescription()) << " loses " << damageString << " due to your " << (damage.critical ? "critical " : " ") << "attack.";
if (damage.extension) {
ss << " " << damage.exString;
}
Expand Down

0 comments on commit 8100402

Please sign in to comment.