Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check null pointer from master on no-pvp combat. #3355

Closed
wants to merge 8 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 43 additions & 81 deletions src/creatures/combat/combat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,111 +330,73 @@ ReturnValue Combat::canDoCombat(const std::shared_ptr<Creature> &attacker, const
}

const auto &targetPlayer = target ? target->getPlayer() : nullptr;
const auto &attackerPlayer = attacker ? attacker->getPlayer() : nullptr;

if (target && target->isSummon() && target->getMaster() && target->getMaster()->getPlayer() && !isInPvpZone(attacker, target)) {
return RETURNVALUE_YOUMAYNOTATTACKTHISCREATURE;
}

if (g_game().getWorldType() == WORLD_TYPE_NO_PVP) {
if ((attackerPlayer || (attacker->getMaster() && attacker->getMaster()->getPlayer())) && targetPlayer && !isInPvpZone(attacker, target)) {
return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER;
}
}

if (target) {
const std::shared_ptr<Tile> &tile = target->getTile();
if (tile->hasProperty(CONST_PROP_BLOCKPROJECTILE)) {
return RETURNVALUE_NOTENOUGHROOM;
}

if (targetPlayer && tile->hasFlag(TILESTATE_PROTECTIONZONE)) {
const auto permittedOnPz = targetPlayer->hasPermittedConditionInPZ();
return permittedOnPz ? RETURNVALUE_NOERROR : RETURNVALUE_ACTIONNOTPERMITTEDINPROTECTIONZONE;
}
}

if (attacker) {
const auto &attackerMaster = attacker->getMaster();
const auto &attackerPlayer = attacker->getPlayer();
if (targetPlayer) {
if (targetPlayer->hasFlag(PlayerFlags_t::CannotBeAttacked)) {
return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER;
}

const auto &targetPlayerTile = targetPlayer->getTile();
if (attackerPlayer) {
if (attackerPlayer->hasFlag(PlayerFlags_t::CannotAttackPlayer)) {
return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER;
}

if (isProtected(attackerPlayer, targetPlayer)) {
return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER;
}
if (attacker && targetPlayer) {
if (targetPlayer->hasFlag(PlayerFlags_t::CannotBeAttacked)) {
return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER;
}

// nopvp-zone
const auto &attackerTile = attackerPlayer->getTile();
if (targetPlayerTile && targetPlayerTile->hasFlag(TILESTATE_NOPVPZONE)) {
return RETURNVALUE_ACTIONNOTPERMITTEDINANOPVPZONE;
} else if (attackerTile && attackerTile->hasFlag(TILESTATE_NOPVPZONE) && targetPlayerTile && !targetPlayerTile->hasFlag(TILESTATE_NOPVPZONE | TILESTATE_PROTECTIONZONE)) {
return RETURNVALUE_ACTIONNOTPERMITTEDINANOPVPZONE;
}
const auto &targetPlayerTile = targetPlayer->getTile();
const auto &attackerTile = attackerPlayer->getTile();

if (attackerPlayer->getFaction() != FACTION_DEFAULT && attackerPlayer->getFaction() != FACTION_PLAYER && attackerPlayer->getFaction() == targetPlayer->getFaction()) {
return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER;
}
}
if (attackerPlayer->hasFlag(PlayerFlags_t::CannotAttackPlayer) || isProtected(attackerPlayer, targetPlayer)) {
return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER;
}

if (attackerMaster) {
if (const auto &masterAttackerPlayer = attackerMaster->getPlayer()) {
if (masterAttackerPlayer->hasFlag(PlayerFlags_t::CannotAttackPlayer)) {
return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER;
}
if (targetPlayerTile && targetPlayerTile->hasFlag(TILESTATE_NOPVPZONE) || (attackerTile && attackerTile->hasFlag(TILESTATE_NOPVPZONE) && !targetPlayerTile->hasFlag(TILESTATE_NOPVPZONE | TILESTATE_PROTECTIONZONE))) {
return RETURNVALUE_ACTIONNOTPERMITTEDINANOPVPZONE;
}

if (targetPlayerTile && targetPlayerTile->hasFlag(TILESTATE_NOPVPZONE)) {
return RETURNVALUE_ACTIONNOTPERMITTEDINANOPVPZONE;
}
if (attackerPlayer->getFaction() != FACTION_DEFAULT && attackerPlayer->getFaction() != FACTION_PLAYER && attackerPlayer->getFaction() == targetPlayer->getFaction()) {
return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER;
}
}

if (isProtected(masterAttackerPlayer, targetPlayer)) {
return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER;
}
}
}
if (attacker && target && target->getMonster()) {
if (attacker->getFaction() != FACTION_DEFAULT && attacker->getFaction() != FACTION_PLAYER && attacker->getMonster() && !attacker->getMonster()->isEnemyFaction(target->getFaction())) {
return RETURNVALUE_YOUMAYNOTATTACKTHISCREATURE;
}

if (attacker->getMonster() && (!attackerMaster || attackerMaster->getMonster())) {
if (attacker->getFaction() != FACTION_DEFAULT && !attacker->getMonster()->isEnemyFaction(targetPlayer->getFaction())) {
return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER;
}
}
} else if (target && target->getMonster()) {
if (attacker->getFaction() != FACTION_DEFAULT && attacker->getFaction() != FACTION_PLAYER && attacker->getMonster() && !attacker->getMonster()->isEnemyFaction(target->getFaction())) {
if (attackerPlayer) {
if (attackerPlayer->hasFlag(PlayerFlags_t::CannotAttackMonster)) {
return RETURNVALUE_YOUMAYNOTATTACKTHISCREATURE;
}

if (attackerPlayer) {
if (attackerPlayer->hasFlag(PlayerFlags_t::CannotAttackMonster)) {
return RETURNVALUE_YOUMAYNOTATTACKTHISCREATURE;
}

if (target->isSummon() && target->getMaster()->getPlayer() && target->getZoneType() == ZONE_NOPVP) {
return RETURNVALUE_ACTIONNOTPERMITTEDINANOPVPZONE;
}
} else if (attacker->getMonster()) {
const auto &targetMaster = target->getMaster();

if ((!targetMaster || !targetMaster->getPlayer()) && attacker->getFaction() == FACTION_DEFAULT) {
if (!attackerMaster || !attackerMaster->getPlayer()) {
return RETURNVALUE_YOUMAYNOTATTACKTHISCREATURE;
}
}
if (target->isSummon() && target->getMaster()->getPlayer() && target->getZoneType() == ZONE_NOPVP) {
return RETURNVALUE_ACTIONNOTPERMITTEDINANOPVPZONE;
}
} else if (target && target->getNpc()) {
return RETURNVALUE_YOUMAYNOTATTACKTHISCREATURE;
}

if (g_game().getWorldType() == WORLD_TYPE_NO_PVP) {
if (attacker->getPlayer() || (attackerMaster && attackerMaster->getPlayer())) {
if (targetPlayer) {
if (!isInPvpZone(attacker, target)) {
return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER;
}
}

if (target && target->isSummon() && target->getMaster()->getPlayer()) {
if (!isInPvpZone(attacker, target)) {
return RETURNVALUE_YOUMAYNOTATTACKTHISCREATURE;
}
}
} else if (attacker->getMonster()) {
if ((!target->getMaster() || !target->getMaster()->getPlayer()) && attacker->getFaction() == FACTION_DEFAULT) {
return RETURNVALUE_YOUMAYNOTATTACKTHISCREATURE;
}
}
} else if (target && target->getNpc()) {
return RETURNVALUE_YOUMAYNOTATTACKTHISCREATURE;
}

ReturnValue ret = g_events().eventCreatureOnTargetCombat(attacker, target);
if (ret == RETURNVALUE_NOERROR) {
ret = g_callbacks().checkCallbackWithReturnValue(EventCallback_t::creatureOnTargetCombat, &EventCallback::creatureOnTargetCombat, attacker, target);
Expand Down