Skip to content

Commit

Permalink
Fix normal summon teleporting to the protection zone (#295)
Browse files Browse the repository at this point in the history
Created "checkSummonMove" for verify if summon/familiar can move/teleport
Created hasSummons() for check if summons list is empty
Small improvement on summon/familiar teleport

Resolves #159
  • Loading branch information
dudantas authored Apr 17, 2022
1 parent 5714e14 commit d0003ec
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 14 deletions.
45 changes: 31 additions & 14 deletions src/creatures/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,36 @@ void Creature::onAttackedCreatureChangeZone(ZoneType_t zone)
}
}

void Creature::checkSummonMove(const Position& newPos, bool teleportSummon) const
{
if (hasSummons()) {
// Check if any of our summons is out of range (+/- 2 floors or 30 tiles away)
std::forward_list<Creature*> despawnMonsterList;
for (Creature* creature : getSummons()) {
if (!creature) {
continue;
}

const Monster* monster = creature->getMonster();
// Check is is familiar for teleport to the master, if "teleportSummon" is true, this is not executed
const Position& pos = creature->getPosition();
if (!teleportSummon && !monster->isFamiliar()) {
SPDLOG_DEBUG("[Creature::checkSummonMove] - Creature name {}", creature->getName());
continue;
}

if (Position::getDistanceZ(newPos, pos) > 0 || (std::max<int32_t>(Position::getDistanceX(newPos, pos), Position::getDistanceY(newPos, pos)) > 15))
{
g_game().internalTeleport(creature, creature->getMaster()->getPosition(), true);
}
}

for (Creature* despawnCreature : despawnMonsterList) {
g_game().removeCreature(despawnCreature, true);
}
}
}

void Creature::onCreatureMove(Creature* creature, const Tile* newTile, const Position& newPos,
const Tile* oldTile, const Position& oldPos, bool teleport)
{
Expand All @@ -465,20 +495,7 @@ void Creature::onCreatureMove(Creature* creature, const Tile* newTile, const Pos
stopEventWalk();
}

if (!summons.empty()) {
//check if any of our summons is out of range (+/- 2 floors or 30 tiles away)
std::forward_list<Creature*> despawnMonsterList;
for (Creature* summon : summons) {
const Position& pos = summon->getPosition();
if (Position::getDistanceZ(newPos, pos) > 0 || (std::max<int32_t>(Position::getDistanceX(newPos, pos), Position::getDistanceY(newPos, pos)) > 15)) {
g_game().internalTeleport(summon, summon->getMaster()->getPosition(), true);
}
}

for (Creature* despawnCreature : despawnMonsterList) {
g_game().removeCreature(despawnCreature, true);
}
}
checkSummonMove(newPos, false);

if (Player* player = creature->getPlayer()) {
if (player->isExerciseTraining()){
Expand Down
25 changes: 25 additions & 0 deletions src/creatures/creature.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ class Creature : virtual public Thing
bool isSummon() const {
return master != nullptr;
}

/**
* hasBeenSummoned doesn't guarantee master still exists
*/
Expand Down Expand Up @@ -410,6 +411,16 @@ class Creature : virtual public Thing

virtual void onCreatureAppear(Creature* creature, bool isLogin);
virtual void onRemoveCreature(Creature* creature, bool isLogout);

/**
* @brief Check if the summon can move/spawn and if the familiar can teleport to the master
*
* @param newPos New position to teleport
* @param teleportSummon Can teleport normal summon? Default value is "false"
* @return true
* @return false
*/
void checkSummonMove(const Position& newPos, bool teleportSummon = false) const;
virtual void onCreatureMove(Creature* creature, const Tile* newTile, const Position& newPos,
const Tile* oldTile, const Position& oldPos, bool teleport);

Expand All @@ -427,6 +438,20 @@ class Creature : virtual public Thing
size_t getSummonCount() const {
return summons.size();
}

/**
* @brief Check if the "summons" list is empty
*
* @return true = not empty
* @return false = empty
*/
bool hasSummons() const {
if (!summons.empty()) {
return true;
}
return false;
}

void setDropLoot(bool newLootDrop) {
this->lootDrop = newLootDrop;
}
Expand Down

0 comments on commit d0003ec

Please sign in to comment.