Skip to content

Commit

Permalink
Minor code cleanup (otland#1890)
Browse files Browse the repository at this point in the history
* Minor code cleanup

* Make lastHitIsPlayer a static function
  • Loading branch information
dbjorkholm authored and marksamman committed Aug 26, 2016
1 parent da1db22 commit 949baa1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 56 deletions.
2 changes: 0 additions & 2 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2399,8 +2399,6 @@ void Game::playerUpdateHouseWindow(uint32_t playerId, uint8_t listId, uint32_t w
House* house = player->getEditHouse(internalWindowTextId, internalListId);
if (house && house->canEditAccessList(internalListId, player) && internalWindowTextId == windowTextId && listId == 0) {
house->setAccessList(internalListId, text);
player->setEditHouse(nullptr);
return;
}

player->setEditHouse(nullptr);
Expand Down
84 changes: 30 additions & 54 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1990,33 +1990,24 @@ void Player::death(Creature* lastHitCreature)

if (skillLoss) {
uint8_t unfairFightReduction = 100;

if (lastHitCreature) {
Player* lastHitPlayer = lastHitCreature->getPlayer();
if (!lastHitPlayer) {
Creature* lastHitMaster = lastHitCreature->getMaster();
if (lastHitMaster) {
lastHitPlayer = lastHitMaster->getPlayer();
}
}

if (lastHitPlayer) {
uint32_t sumLevels = 0;
uint32_t inFightTicks = g_config.getNumber(ConfigManager::PZ_LOCKED);
for (const auto& it : damageMap) {
CountBlock_t cb = it.second;
if ((OTSYS_TIME() - cb.ticks) <= inFightTicks) {
Player* damageDealer = g_game.getPlayerByID(it.first);
if (damageDealer) {
sumLevels += damageDealer->getLevel();
}
bool lastHitPlayer = Player::lastHitIsPlayer(lastHitCreature);

if (lastHitPlayer) {
uint32_t sumLevels = 0;
uint32_t inFightTicks = g_config.getNumber(ConfigManager::PZ_LOCKED);
for (const auto& it : damageMap) {
CountBlock_t cb = it.second;
if ((OTSYS_TIME() - cb.ticks) <= inFightTicks) {
Player* damageDealer = g_game.getPlayerByID(it.first);
if (damageDealer) {
sumLevels += damageDealer->getLevel();
}
}
}

if (sumLevels > level) {
double reduce = level / static_cast<double>(sumLevels);
unfairFightReduction = std::max<uint8_t>(20, std::floor((reduce * 100) + 0.5));
}
if (sumLevels > level) {
double reduce = level / static_cast<double>(sumLevels);
unfairFightReduction = std::max<uint8_t>(20, std::floor((reduce * 100) + 0.5));
}
}

Expand Down Expand Up @@ -2113,20 +2104,6 @@ void Player::death(Creature* lastHitCreature)

std::bitset<6> bitset(blessings);
if (bitset[5]) {
Player* lastHitPlayer;

if (lastHitCreature) {
lastHitPlayer = lastHitCreature->getPlayer();
if (!lastHitPlayer) {
Creature* lastHitMaster = lastHitCreature->getMaster();
if (lastHitMaster) {
lastHitPlayer = lastHitMaster->getPlayer();
}
}
} else {
lastHitPlayer = nullptr;
}

if (lastHitPlayer) {
bitset.reset(5);
blessings = bitset.to_ulong();
Expand Down Expand Up @@ -2190,22 +2167,7 @@ void Player::death(Creature* lastHitCreature)

bool Player::dropCorpse(Creature* lastHitCreature, Creature* mostDamageCreature, bool lastHitUnjustified, bool mostDamageUnjustified)
{
if (getZone() != ZONE_PVP) {
return Creature::dropCorpse(lastHitCreature, mostDamageCreature, lastHitUnjustified, mostDamageUnjustified);
}

Player* lastHitPlayer = nullptr;
if (lastHitCreature) {
lastHitPlayer = lastHitCreature->getPlayer();
if (!lastHitPlayer) {
Creature* lastHitMaster = lastHitCreature->getMaster();
if (lastHitMaster) {
lastHitPlayer = lastHitMaster->getPlayer();
}
}
}

if (!lastHitPlayer) {
if (getZone() != ZONE_PVP || !Player::lastHitIsPlayer(lastHitCreature)) {
return Creature::dropCorpse(lastHitCreature, mostDamageCreature, lastHitUnjustified, mostDamageUnjustified);
}

Expand Down Expand Up @@ -3702,6 +3664,20 @@ bool Player::isAttackable() const
return !hasFlag(PlayerFlag_CannotBeAttacked);
}

bool Player::lastHitIsPlayer(Creature* lastHitCreature)
{
if (!lastHitCreature) {
return false;
}

if (lastHitCreature->getPlayer()) {
return true;
}

Creature* lastHitMaster = lastHitCreature->getMaster();
return lastHitMaster && lastHitMaster->getPlayer();
}

void Player::changeHealth(int32_t healthChange, bool sendHealthChange/* = true*/)
{
Creature::changeHealth(healthChange, sendHealthChange);
Expand Down
1 change: 1 addition & 0 deletions src/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@ class Player final : public Creature, public Cylinder
bool isImmune(ConditionType_t type) const final;
bool hasShield() const;
bool isAttackable() const final;
static bool lastHitIsPlayer(Creature* lastHitCreature);

void changeHealth(int32_t healthChange, bool sendHealthChange = true) final;
void changeMana(int32_t manaChange) final;
Expand Down

0 comments on commit 949baa1

Please sign in to comment.