-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailbox.cpp
183 lines (155 loc) · 4.46 KB
/
mailbox.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* Tibia GIMUD Server - a free and open-source MMORPG server emulator
* Copyright (C) 2017 Alejandro Mujica <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "otpch.h"
#include "mailbox.h"
#include "game.h"
#include "player.h"
#include "iologindata.h"
#include "town.h"
extern Game g_game;
ReturnValue Mailbox::queryAdd(int32_t, const Thing& thing, uint32_t, uint32_t, Creature* creature) const
{
const Item* item = thing.getItem();
if (item && Mailbox::canSend(item)) {
return RETURNVALUE_NOERROR;
}
return RETURNVALUE_NOTPOSSIBLE;
}
ReturnValue Mailbox::queryMaxCount(int32_t, const Thing&, uint32_t count, uint32_t& maxQueryCount, uint32_t) const
{
maxQueryCount = std::max<uint32_t>(1, count);
return RETURNVALUE_NOERROR;
}
ReturnValue Mailbox::queryRemove(const Thing&, uint32_t, uint32_t) const
{
return RETURNVALUE_NOTPOSSIBLE;
}
Cylinder* Mailbox::queryDestination(int32_t&, const Thing&, Item**, uint32_t&)
{
return this;
}
void Mailbox::addThing(Thing* thing)
{
return addThing(0, thing);
}
void Mailbox::addThing(int32_t, Thing* thing)
{
Item* item = thing->getItem();
if (item && Mailbox::canSend(item)) {
sendItem(item);
}
}
void Mailbox::updateThing(Thing*, uint16_t, uint32_t)
{
//
}
void Mailbox::replaceThing(uint32_t, Thing*)
{
//
}
void Mailbox::removeThing(Thing*, uint32_t)
{
//
}
void Mailbox::postAddNotification(Thing* thing, const Cylinder* oldParent, int32_t index, cylinderlink_t)
{
getParent()->postAddNotification(thing, oldParent, index, LINK_PARENT);
}
void Mailbox::postRemoveNotification(Thing* thing, const Cylinder* newParent, int32_t index, cylinderlink_t)
{
getParent()->postRemoveNotification(thing, newParent, index, LINK_PARENT);
}
bool Mailbox::sendItem(Item* item) const
{
std::string receiver;
std::string townName;
if (!getDestination(item, receiver, townName)) {
return false;
}
if (receiver.empty() || townName.empty()) {
return false;
}
Town* town = g_game.map.towns.getTown(townName);
if (!town) {
return false;
}
Player* player = g_game.getPlayerByName(receiver);
if (player) {
DepotLocker* depotLocker = player->getDepotLocker(town->getID(), true);
if (depotLocker) {
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();
return true;
}
}
} else {
Player tmpPlayer(nullptr);
if (!IOLoginData::loadPlayerByName(&tmpPlayer, receiver)) {
return false;
}
DepotLocker* depotLocker = tmpPlayer.getDepotLocker(town->getID(), true);
if (depotLocker) {
if (g_game.internalMoveItem(item->getParent(), depotLocker, INDEX_WHEREEVER,
item, item->getItemCount(), nullptr, FLAG_NOLIMIT) == RETURNVALUE_NOERROR) {
g_game.transformItem(item, item->getID() + 1);
IOLoginData::savePlayer(&tmpPlayer);
return true;
}
}
}
return false;
}
bool Mailbox::getDestination(Item* item, std::string& name, std::string& town) const
{
const Container* container = item->getContainer();
if (container) {
for (Item* containerItem : container->getItemList()) {
if (containerItem->getID() == ITEM_LABEL && getDestination(containerItem, name, town)) {
return true;
}
}
return false;
}
const std::string& text = item->getText();
if (text.empty()) {
return false;
}
std::istringstream iss(text, std::istringstream::in);
std::string temp;
uint32_t currentLine = 1;
while (getline(iss, temp, '\n')) {
if (currentLine == 1) {
name = temp;
} else if (currentLine == 2) {
town = temp;
} else {
break;
}
++currentLine;
}
trimString(name);
trimString(town);
return true;
}
bool Mailbox::canSend(const Item* item)
{
return item->getID() == ITEM_PARCEL || item->getID() == ITEM_LETTER;
}