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 combat null pointers #2452

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
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
70 changes: 45 additions & 25 deletions src/creatures/combat/combat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,21 +179,32 @@ ConditionType_t Combat::DamageToConditionType(CombatType_t type) {
}

bool Combat::isPlayerCombat(std::shared_ptr<Creature> target) {
if (target->getPlayer()) {
return true;
}
if (!target) {
return false;
}

if (target->isSummon() && target->getMaster()->getPlayer()) {
return true;
}
if (target->getPlayer()) {
return true;
}

return false;
if (target->isSummon()) {
auto master = target->getMaster();
if (master && master->getPlayer()) {
return true;
}
}

return false;
}

ReturnValue Combat::canTargetCreature(std::shared_ptr<Player> player, std::shared_ptr<Creature> target) {
if (player == target) {
return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER;
}
if (!player || !target) {
return RETURNVALUE_NOERROR;
}

if (player == target) {
return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER;
}

if (!player->hasFlag(PlayerFlags_t::IgnoreProtectionZone)) {
// pz-zone
Expand Down Expand Up @@ -239,20 +250,25 @@ ReturnValue Combat::canTargetCreature(std::shared_ptr<Player> player, std::share
}

ReturnValue Combat::canDoCombat(std::shared_ptr<Creature> caster, std::shared_ptr<Tile> tile, bool aggressive) {
if (tile->hasProperty(CONST_PROP_BLOCKPROJECTILE)) {
return RETURNVALUE_NOTENOUGHROOM;
}
if (aggressive && tile->hasFlag(TILESTATE_PROTECTIONZONE)) {
return RETURNVALUE_ACTIONNOTPERMITTEDINPROTECTIONZONE;
}
if (!tile) {
return RETURNVALUE_NOERROR;
}

if (tile->hasFlag(TILESTATE_FLOORCHANGE)) {
return RETURNVALUE_NOTENOUGHROOM;
}
if (tile->hasProperty(CONST_PROP_BLOCKPROJECTILE)) {
return RETURNVALUE_NOTENOUGHROOM;
}

if (tile->getTeleportItem()) {
return RETURNVALUE_NOTENOUGHROOM;
}
if (aggressive && tile->hasFlag(TILESTATE_PROTECTIONZONE)) {
return RETURNVALUE_ACTIONNOTPERMITTEDINPROTECTIONZONE;
}

if (tile->hasFlag(TILESTATE_FLOORCHANGE)) {
return RETURNVALUE_NOTENOUGHROOM;
}

if (tile->getTeleportItem()) {
return RETURNVALUE_NOTENOUGHROOM;
}

if (caster) {
const Position &casterPosition = caster->getPosition();
Expand All @@ -278,6 +294,10 @@ bool Combat::isInPvpZone(std::shared_ptr<Creature> attacker, std::shared_ptr<Cre
}

bool Combat::isProtected(std::shared_ptr<Player> attacker, std::shared_ptr<Player> target) {
if (!attacker || !target) {
return true;
}

uint32_t protectionLevel = g_configManager().getNumber(PROTECTION_LEVEL, __FUNCTION__);
if (target->getLevel() < protectionLevel || attacker->getLevel() < protectionLevel) {
return true;
Expand All @@ -295,9 +315,9 @@ bool Combat::isProtected(std::shared_ptr<Player> attacker, std::shared_ptr<Playe
}

ReturnValue Combat::canDoCombat(std::shared_ptr<Creature> attacker, std::shared_ptr<Creature> target, bool aggressive) {
if (!aggressive) {
return RETURNVALUE_NOERROR;
}
if (!target || !aggressive) {
return RETURNVALUE_NOERROR;
}

auto targetPlayer = target ? target->getPlayer() : nullptr;
if (target) {
Expand Down
1 change: 0 additions & 1 deletion src/creatures/players/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1925,7 +1925,6 @@ void Player::onWalk(Direction &dir) {

Creature::onWalk(dir);
setNextActionTask(nullptr);
setNextAction(OTSYS_TIME() + getStepDuration(dir));
}

void Player::onCreatureMove(const std::shared_ptr<Creature> &creature, const std::shared_ptr<Tile> &newTile, const Position &newPos, const std::shared_ptr<Tile> &oldTile, const Position &oldPos, bool teleport) {
Expand Down
Loading