From b0f1fd5239583fdffcc7a7c3514268df7a084681 Mon Sep 17 00:00:00 2001 From: ErikasKontenis Date: Sun, 29 Dec 2019 20:24:02 +0200 Subject: [PATCH] fix onReceiveMail notification: https://github.com/TwistedScorpio/Nostalrius/issues/53 --- src/container.h | 4 ++-- src/item.h | 7 +++++++ src/mailbox.cpp | 2 +- src/player.cpp | 12 +++++++----- src/player.h | 4 ++-- src/tile.cpp | 21 +++++++++++++++++++++ src/tile.h | 2 ++ 7 files changed, 42 insertions(+), 10 deletions(-) diff --git a/src/container.h b/src/container.h index 495c92f..2330c26 100644 --- a/src/container.h +++ b/src/container.h @@ -65,10 +65,10 @@ class Container : public Item, public Cylinder return this; } - virtual DepotLocker* getDepotLocker() { + virtual DepotLocker* getDepotLocker() override { return nullptr; } - virtual const DepotLocker* getDepotLocker() const { + virtual const DepotLocker* getDepotLocker() const override { return nullptr; } diff --git a/src/item.h b/src/item.h index e82a50f..87b2a04 100644 --- a/src/item.h +++ b/src/item.h @@ -32,6 +32,7 @@ class Container; class Depot; class Teleport; class Mailbox; +class DepotLocker; class Door; class MagicField; class BedItem; @@ -376,6 +377,12 @@ class Item : virtual public Thing virtual const Mailbox* getMailbox() const { return nullptr; } + virtual DepotLocker* getDepotLocker() { + return nullptr; + } + virtual const DepotLocker* getDepotLocker() const { + return nullptr; + } virtual Door* getDoor() { return nullptr; } diff --git a/src/mailbox.cpp b/src/mailbox.cpp index 8b21a45..31cd67f 100644 --- a/src/mailbox.cpp +++ b/src/mailbox.cpp @@ -114,7 +114,7 @@ bool Mailbox::sendItem(Item* item) const if (g_game.internalMoveItem(item->getParent(), depotLocker, INDEX_WHEREEVER, item, item->getItemCount(), nullptr, FLAG_NOLIMIT) == RETURNVALUE_NOERROR) { g_game.transformItem(item, item->getID() + 1); - player->onReceiveMail(); + player->onReceiveMail(town->getID()); return true; } } diff --git a/src/player.cpp b/src/player.cpp index 49bb168..50ab614 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -567,14 +567,14 @@ bool Player::canSeeCreature(const Creature* creature) const return true; } -void Player::onReceiveMail() const +void Player::onReceiveMail(uint32_t townId) const { - if (isNearDepotBox()) { + if (isNearDepotBox(townId)) { sendTextMessage(MESSAGE_EVENT_ADVANCE, "New mail has arrived."); } } -bool Player::isNearDepotBox() const +bool Player::isNearDepotBox(uint32_t townId) const { const Position& pos = getPosition(); for (int32_t cx = -1; cx <= 1; ++cx) { @@ -584,8 +584,10 @@ bool Player::isNearDepotBox() const continue; } - if (tile->hasFlag(TILESTATE_DEPOT)) { - return true; + if (DepotLocker* depotLocker = tile->getDepotLocker()) { + if (depotLocker->getDepotId() == townId) { + return true; + } } } } diff --git a/src/player.h b/src/player.h index 3f39f9c..f088f13 100644 --- a/src/player.h +++ b/src/player.h @@ -433,8 +433,8 @@ class Player final : public Creature, public Cylinder void removeConditionSuppressions(uint32_t conditions); DepotLocker* getDepotLocker(uint32_t depotId, bool autoCreate); - void onReceiveMail() const; - bool isNearDepotBox() const; + void onReceiveMail(uint32_t townId) const; + bool isNearDepotBox(uint32_t townId) const; bool canSee(const Position& pos) const final; bool canSeeCreature(const Creature* creature) const final; diff --git a/src/tile.cpp b/src/tile.cpp index 5b2afbb..6240678 100644 --- a/src/tile.cpp +++ b/src/tile.cpp @@ -212,6 +212,27 @@ Mailbox* Tile::getMailbox() const return nullptr; } +DepotLocker* Tile::getDepotLocker() const +{ + if (!hasFlag(TILESTATE_DEPOT)) { + return nullptr; + } + + if (ground && ground->getDepotLocker()) { + return ground->getDepotLocker(); + } + + if (const TileItemVector* items = getItemList()) { + for (auto it = items->rbegin(), end = items->rend(); it != end; ++it) { + if ((*it)->getDepotLocker()) { + return (*it)->getDepotLocker(); + } + } + } + return nullptr; +} + + BedItem* Tile::getBedItem() const { if (!hasFlag(TILESTATE_BED)) { diff --git a/src/tile.h b/src/tile.h index 99f3ffd..0b68759 100644 --- a/src/tile.h +++ b/src/tile.h @@ -29,6 +29,7 @@ class Creature; class Teleport; class Mailbox; +class DepotLocker; class MagicField; class QTreeLeafNode; class BedItem; @@ -169,6 +170,7 @@ class Tile : public Cylinder MagicField* getFieldItem() const; Teleport* getTeleportItem() const; Mailbox* getMailbox() const; + DepotLocker* getDepotLocker() const; BedItem* getBedItem() const; Creature* getTopCreature() const;