mailbox.cpp

main 153 lines · 3.9 KB Raw
Alex Alex Commit Server upload 18/10/2026 23:38
1/**
2 * The Forgotten Server - a free and open-source MMORPG server emulator
3 * Copyright (C) 2019 Mark Samman <mark.samman@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include "otpch.h"
21
22#include "mailbox.h"
23#include "game.h"
24#include "iologindata.h"
25
26extern Game g_game;
27
28ReturnValue Mailbox::queryAdd(int32_t, const Thing& thing, uint32_t, uint32_t, Creature*) const
29{
30 const Item* item = thing.getItem();
31 if (item && Mailbox::canSend(item)) {
32 return RETURNVALUE_NOERROR;
33 }
34 return RETURNVALUE_NOTPOSSIBLE;
35}
36
37ReturnValue Mailbox::queryMaxCount(int32_t, const Thing&, uint32_t count, uint32_t& maxQueryCount, uint32_t) const
38{
39 maxQueryCount = std::max<uint32_t>(1, count);
40 return RETURNVALUE_NOERROR;
41}
42
43ReturnValue Mailbox::queryRemove(const Thing&, uint32_t, uint32_t) const
44{
45 return RETURNVALUE_NOTPOSSIBLE;
46}
47
48Cylinder* Mailbox::queryDestination(int32_t&, const Thing&, Item**, uint32_t&)
49{
50 return this;
51}
52
53void Mailbox::addThing(Thing* thing)
54{
55 return addThing(0, thing);
56}
57
58void Mailbox::addThing(int32_t, Thing* thing)
59{
60 Item* item = thing->getItem();
61 if (item && Mailbox::canSend(item)) {
62 sendItem(item);
63 }
64}
65
66void Mailbox::updateThing(Thing*, uint16_t, uint32_t)
67{
68 //
69}
70
71void Mailbox::replaceThing(uint32_t, Thing*)
72{
73 //
74}
75
76void Mailbox::removeThing(Thing*, uint32_t)
77{
78 //
79}
80
81void Mailbox::postAddNotification(Thing* thing, const Cylinder* oldParent, int32_t index, cylinderlink_t)
82{
83 getParent()->postAddNotification(thing, oldParent, index, LINK_PARENT);
84}
85
86void Mailbox::postRemoveNotification(Thing* thing, const Cylinder* newParent, int32_t index, cylinderlink_t)
87{
88 getParent()->postRemoveNotification(thing, newParent, index, LINK_PARENT);
89}
90
91bool Mailbox::sendItem(Item* item) const
92{
93 std::string receiver;
94 if (!getReceiver(item, receiver)) {
95 return false;
96 }
97
98 /**No need to continue if its still empty**/
99 if (receiver.empty()) {
100 return false;
101 }
102
103 Player* player = g_game.getPlayerByName(receiver);
104 if (player) {
105 if (g_game.internalMoveItem(item->getParent(), player->getInbox(), INDEX_WHEREEVER,
106 item, item->getItemCount(), nullptr, FLAG_NOLIMIT) == RETURNVALUE_NOERROR) {
107 g_game.transformItem(item, item->getID() + 1);
108 player->onReceiveMail();
109 return true;
110 }
111 } else {
112 Player tmpPlayer(nullptr);
113 if (!IOLoginData::loadPlayerByName(&tmpPlayer, receiver)) {
114 return false;
115 }
116
117 if (g_game.internalMoveItem(item->getParent(), tmpPlayer.getInbox(), INDEX_WHEREEVER,
118 item, item->getItemCount(), nullptr, FLAG_NOLIMIT) == RETURNVALUE_NOERROR) {
119 g_game.transformItem(item, item->getID() + 1);
120 IOLoginData::savePlayer(&tmpPlayer);
121 return true;
122 }
123 }
124 return false;
125}
126
127bool Mailbox::getReceiver(Item* item, std::string& name) const
128{
129 const Container* container = item->getContainer();
130 if (container) {
131 for (Item* containerItem : container->getItemList()) {
132 if (containerItem->getID() == ITEM_LABEL && getReceiver(containerItem, name)) {
133 return true;
134 }
135 }
136 return false;
137 }
138
139 const std::string& text = item->getText();
140 if (text.empty()) {
141 return false;
142 }
143
144 name = getFirstLine(text);
145 trimString(name);
146 return true;
147}
148
149bool Mailbox::canSend(const Item* item)
150{
151 return item->getID() == ITEM_PARCEL || item->getID() == ITEM_LETTER;
152}
153
Top