Skip to content

Commit

Permalink
Remove redundant "names" std::unordered_map from Player::getCorpse()
Browse files Browse the repository at this point in the history
I've noticed redundant allocation of std::unordered_map together with its unneeded loop.

The edited fragment of method, which is about creating corpse's description (Item* Player::getCorpse(Creature* lastHitCreature, Creature* mostDamageCreature)),
does not need to check names[lastHitCreature->getName()] == 1 , which is the only place where the names map is actually used.

It just serves no visible purpose, maybe it was meant to check if lastHitCreature is in getKillers(), which is a check if the lastHitCreature has hit the dead person in last minute... which is not needed, because lastHitCreature by its definition means that it is true.
  • Loading branch information
Tofame committed Dec 28, 2024
1 parent ceaeb5c commit aa3f1fc
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2872,27 +2872,24 @@ Item* Player::getCorpse(Creature* lastHitCreature, Creature* mostDamageCreature)
{
Item* corpse = Creature::getCorpse(lastHitCreature, mostDamageCreature);
if (corpse && corpse->getContainer()) {
std::unordered_map<std::string, uint16_t> names;
for (const auto& killer : getKillers()) {
++names[killer->getName()];
}
size_t killersSize = getKillers().size();

if (lastHitCreature) {
if (!mostDamageCreature) {
corpse->setSpecialDescription(fmt::format("You recognize {:s}. {:s} was killed by {:s}{:s}", getNameDescription(), getSex() == PLAYERSEX_FEMALE ? "She" : "He", lastHitCreature->getNameDescription(), names.size() > 1 ? " and others." : "."));
} else if (lastHitCreature != mostDamageCreature && names[lastHitCreature->getName()] == 1) {
corpse->setSpecialDescription(fmt::format("You recognize {:s}. {:s} was killed by {:s}, {:s}{:s}", getNameDescription(), getSex() == PLAYERSEX_FEMALE ? "She" : "He", mostDamageCreature->getNameDescription(), lastHitCreature->getNameDescription(), names.size() > 2 ? " and others." : "."));
corpse->setSpecialDescription(fmt::format("You recognize {:s}. {:s} was killed by {:s}{:s}", getNameDescription(), getSex() == PLAYERSEX_FEMALE ? "She" : "He", lastHitCreature->getNameDescription(), killersSize > 1 ? " and others." : "."));
} else if (lastHitCreature != mostDamageCreature) {
corpse->setSpecialDescription(fmt::format("You recognize {:s}. {:s} was killed by {:s}, {:s}{:s}", getNameDescription(), getSex() == PLAYERSEX_FEMALE ? "She" : "He", mostDamageCreature->getNameDescription(), lastHitCreature->getNameDescription(), killersSize > 2 ? " and others." : "."));
} else {
corpse->setSpecialDescription(fmt::format("You recognize {:s}. {:s} was killed by {:s} and others.", getNameDescription(), getSex() == PLAYERSEX_FEMALE ? "She" : "He", mostDamageCreature->getNameDescription()));
}
} else if (mostDamageCreature) {
if (names.size() > 1) {
if (killersSize > 1) {
corpse->setSpecialDescription(fmt::format("You recognize {:s}. {:s} was killed by something evil, {:s}, and others", getNameDescription(), getSex() == PLAYERSEX_FEMALE ? "She" : "He", mostDamageCreature->getNameDescription()));
} else {
corpse->setSpecialDescription(fmt::format("You recognize {:s}. {:s} was killed by something evil and others", getNameDescription(), getSex() == PLAYERSEX_FEMALE ? "She" : "He", mostDamageCreature->getNameDescription()));
}
} else {
corpse->setSpecialDescription(fmt::format("You recognize {:s}. {:s} was killed by something evil {:s}", getNameDescription(), getSex() == PLAYERSEX_FEMALE ? "She" : "He", names.size() ? " and others." : "."));
corpse->setSpecialDescription(fmt::format("You recognize {:s}. {:s} was killed by something evil {:s}", getNameDescription(), getSex() == PLAYERSEX_FEMALE ? "She" : "He", killersSize ? " and others." : "."));
}
}
return corpse;
Expand Down

0 comments on commit aa3f1fc

Please sign in to comment.