GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/eathena/npchandler.cpp Lines: 1 181 0.6 %
Date: 2018-07-14 Branches: 0 210 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2018  The ManaPlus Developers
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#include "net/eathena/npchandler.h"
24
25
#include "being/localplayer.h"
26
27
#include "const/net/inventory.h"
28
29
#include "gui/windows/npcdialog.h"
30
31
#include "gui/widgets/createwidget.h"
32
33
#include "net/messagein.h"
34
35
#include "net/ea/npcrecv.h"
36
37
#include "net/eathena/messageout.h"
38
#include "net/eathena/npcrecv.h"
39
40
#include "net/eathena/protocolout.h"
41
42
#include "utils/foreach.h"
43
44
#include "resources/item/shopitem.h"
45
46
#include "debug.h"
47
48
extern int packetVersion;
49
extern int packetVersionMain;
50
extern int packetVersionRe;
51
52
namespace EAthena
53
{
54
55
NpcHandler::NpcHandler() :
56
    Ea::NpcHandler()
57
{
58
    npcHandler = this;
59
}
60
61
NpcHandler::~NpcHandler()
62
{
63
    npcHandler = nullptr;
64
}
65
66
void NpcHandler::talk(const Being *const being) const
67
{
68
    if (being == nullptr)
69
        return;
70
    createOutPacket(CMSG_NPC_TALK);
71
    outMsg.writeBeingId(being->getId(), "npc id");
72
    outMsg.writeInt8(0, "unused");
73
    EAthena::NpcRecv::mNpcTypeId = being->getSubType();
74
}
75
76
void NpcHandler::nextDialog(const BeingId npcId) const
77
{
78
    createOutPacket(CMSG_NPC_NEXT_REQUEST);
79
    outMsg.writeBeingId(npcId, "npc id");
80
}
81
82
void NpcHandler::closeDialog(const BeingId npcId)
83
{
84
    createOutPacket(CMSG_NPC_CLOSE);
85
    outMsg.writeBeingId(npcId, "npc id");
86
87
    const NpcDialogs::iterator it = NpcDialog::mNpcDialogs.find(npcId);
88
    if (it != NpcDialog::mNpcDialogs.end())
89
    {
90
        NpcDialog *const dialog = (*it).second;
91
        if (dialog != nullptr)
92
            dialog->close();
93
        if (dialog == Ea::NpcRecv::mDialog)
94
            Ea::NpcRecv::mDialog = nullptr;
95
        NpcDialog::mNpcDialogs.erase(it);
96
    }
97
}
98
99
void NpcHandler::listInput(const BeingId npcId,
100
                           const unsigned char value) const
101
{
102
    createOutPacket(CMSG_NPC_LIST_CHOICE);
103
    outMsg.writeBeingId(npcId, "npc id");
104
    outMsg.writeInt8(value, "value");
105
}
106
107
void NpcHandler::integerInput(const BeingId npcId,
108
                              const int value) const
109
{
110
    createOutPacket(CMSG_NPC_INT_RESPONSE);
111
    outMsg.writeBeingId(npcId, "npc id");
112
    outMsg.writeInt32(value, "value");
113
}
114
115
void NpcHandler::stringInput(const BeingId npcId,
116
                             const std::string &value) const
117
{
118
    createOutPacket(CMSG_NPC_STR_RESPONSE);
119
    if (packetVersion >= 20151029)
120
    {
121
        outMsg.writeInt16(CAST_S16(value.length() + 8), "len");
122
        outMsg.writeBeingId(npcId, "npc id");
123
        outMsg.writeString(value, CAST_S32(value.length()), "value");
124
    }
125
    else
126
    {
127
        outMsg.writeInt16(CAST_S16(value.length() + 9), "len");
128
        outMsg.writeBeingId(npcId, "npc id");
129
        outMsg.writeString(value, CAST_S32(value.length()), "value");
130
        outMsg.writeInt8(0, "null byte");
131
    }
132
}
133
134
void NpcHandler::buy(const Being *const being) const
135
{
136
    if (being == nullptr)
137
        return;
138
    createOutPacket(CMSG_NPC_BUY_SELL_REQUEST);
139
    outMsg.writeBeingId(being->getId(), "npc id");
140
    outMsg.writeInt8(0, "action");
141
    EAthena::NpcRecv::mNpcTypeId = being->getSubType();
142
}
143
144
void NpcHandler::buy(const BeingId beingId) const
145
{
146
    createOutPacket(CMSG_NPC_BUY_SELL_REQUEST);
147
    outMsg.writeBeingId(beingId, "npc id");
148
    outMsg.writeInt8(0, "action");
149
    EAthena::NpcRecv::mNpcTypeId = BeingTypeId_zero;
150
}
151
152
void NpcHandler::sell(const BeingId beingId) const
153
{
154
    createOutPacket(CMSG_NPC_BUY_SELL_REQUEST);
155
    outMsg.writeBeingId(beingId, "npc id");
156
    outMsg.writeInt8(1, "action");
157
}
158
159
void NpcHandler::buyItem(const BeingId beingId A_UNUSED,
160
                         const int itemId,
161
                         const ItemColor color A_UNUSED,
162
                         const int amount) const
163
{
164
    createOutPacket(CMSG_NPC_BUY_REQUEST);
165
    outMsg.writeInt16(8, "len");
166
    outMsg.writeInt16(CAST_S16(amount), "amount");
167
    outMsg.writeInt16(CAST_S16(itemId), "item id");
168
}
169
170
void NpcHandler::buyItems(STD_VECTOR<ShopItem*> &items) const
171
{
172
    int cnt = 0;
173
    const int pairSize = 4;
174
175
    FOR_EACH (STD_VECTOR<ShopItem*>::iterator, it, items)
176
    {
177
        ShopItem *const item = *it;
178
        const int usedQuantity = item->getUsedQuantity();
179
        const ItemTypeT type = item->getType();
180
        if (usedQuantity == 0)
181
            continue;
182
        if (type == ItemType::Weapon ||
183
            type == ItemType::Armor ||
184
            type == ItemType::PetEgg ||
185
            type == ItemType::PetArmor)
186
        {
187
            cnt += item->getUsedQuantity();
188
        }
189
        else
190
        {
191
            cnt ++;
192
        }
193
    }
194
195
    if (cnt > 100)
196
        return;
197
198
    createOutPacket(CMSG_NPC_BUY_REQUEST);
199
    outMsg.writeInt16(CAST_S16(4 + pairSize * cnt), "len");
200
    FOR_EACH (STD_VECTOR<ShopItem*>::iterator, it, items)
201
    {
202
        ShopItem *const item = *it;
203
        const int usedQuantity = item->getUsedQuantity();
204
        if (usedQuantity == 0)
205
            continue;
206
        item->increaseUsedQuantity(-usedQuantity);
207
        item->update();
208
        const ItemTypeT type = item->getType();
209
        if (type == ItemType::Weapon ||
210
            type == ItemType::Armor ||
211
            type == ItemType::PetEgg ||
212
            type == ItemType::PetArmor)
213
        {
214
            for (int f = 0; f < usedQuantity; f ++)
215
            {
216
                outMsg.writeInt16(CAST_S16(1), "amount");
217
                outMsg.writeInt16(CAST_S16(item->getId()),
218
                    "item id");
219
            }
220
        }
221
        else
222
        {
223
            outMsg.writeInt16(CAST_S16(usedQuantity), "amount");
224
            outMsg.writeInt16(CAST_S16(item->getId()), "item id");
225
        }
226
    }
227
}
228
229
void NpcHandler::sellItem(const BeingId beingId A_UNUSED,
230
                          const int itemId, const int amount) const
231
{
232
    createOutPacket(CMSG_NPC_SELL_REQUEST);
233
    outMsg.writeInt16(8, "len");
234
    outMsg.writeInt16(CAST_S16(itemId + INVENTORY_OFFSET),
235
        "item index");
236
    outMsg.writeInt16(CAST_S16(amount), "amount");
237
}
238
239
void NpcHandler::sellItems(STD_VECTOR<ShopItem*> &items) const
240
{
241
    const int pairSize = 4;
242
    int cnt = 0;
243
244
    FOR_EACH (STD_VECTOR<ShopItem*>::iterator, it, items)
245
    {
246
        ShopItem *const item = *it;
247
        const int usedQuantity = item->getUsedQuantity();
248
        if (usedQuantity == 0)
249
            continue;
250
        cnt ++;
251
    }
252
253
    createOutPacket(CMSG_NPC_SELL_REQUEST);
254
    outMsg.writeInt16(CAST_S16(4 + pairSize * cnt), "len");
255
    FOR_EACH (STD_VECTOR<ShopItem*>::iterator, it, items)
256
    {
257
        ShopItem *const item = *it;
258
        const int usedQuantity = item->getUsedQuantity();
259
        if (usedQuantity == 0)
260
            continue;
261
        item->increaseUsedQuantity(-usedQuantity);
262
        item->update();
263
        outMsg.writeInt16(CAST_S16(
264
            item->getCurrentInvIndex() + INVENTORY_OFFSET),
265
            "item index");
266
        outMsg.writeInt16(CAST_S16(usedQuantity), "amount");
267
    }
268
}
269
270
void NpcHandler::completeProgressBar() const
271
{
272
    createOutPacket(CMSG_NPC_COMPLETE_PROGRESS_BAR);
273
}
274
275
void NpcHandler::produceMix(const int nameId,
276
                            const int materialId1,
277
                            const int materialId2,
278
                            const int materialId3) const
279
{
280
    createOutPacket(CMSG_NPC_PRODUCE_MIX);
281
    outMsg.writeInt16(CAST_S16(nameId), "name id");
282
    outMsg.writeInt16(CAST_S16(materialId1), "material 1");
283
    outMsg.writeInt16(CAST_S16(materialId2), "material 2");
284
    outMsg.writeInt16(CAST_S16(materialId3), "material 3");
285
}
286
287
void NpcHandler::cooking(const CookingTypeT type,
288
                         const int nameId) const
289
{
290
    createOutPacket(CMSG_NPC_COOKING);
291
    outMsg.writeInt16(CAST_S16(type), "type");
292
    outMsg.writeInt16(CAST_S16(nameId), "name id");
293
}
294
295
void NpcHandler::repair(const int index) const
296
{
297
    createOutPacket(CMSG_NPC_REPAIR);
298
    outMsg.writeInt16(CAST_S16(index), "index");
299
}
300
301
void NpcHandler::refine(const int index) const
302
{
303
    createOutPacket(CMSG_NPC_REFINE);
304
    outMsg.writeInt32(index, "index");
305
}
306
307
void NpcHandler::identify(const int index) const
308
{
309
    createOutPacket(CMSG_NPC_IDENTIFY);
310
    outMsg.writeInt16(CAST_S16(index), "index");
311
}
312
313
void NpcHandler::selectArrow(const int nameId) const
314
{
315
    createOutPacket(CMSG_NPC_SELECT_ARROW);
316
    outMsg.writeInt16(CAST_S16(nameId), "name id");
317
}
318
319
void NpcHandler::selectAutoSpell(const int skillId) const
320
{
321
    createOutPacket(CMSG_NPC_SELECT_AUTO_SPELL);
322
    outMsg.writeInt32(CAST_S16(skillId), "skill id");
323
}
324
325
BeingId NpcHandler::getNpc(Net::MessageIn &msg,
326
                           const NpcActionT action)
327
{
328
    const BeingId npcId = msg.readBeingId("npc id");
329
330
    const NpcDialogs::const_iterator diag = NpcDialog::mNpcDialogs.find(npcId);
331
332
    if (diag == NpcDialog::mNpcDialogs.end())
333
    {
334
        Ea::NpcRecv::mDialog = nullptr;
335
        // Empty dialogs don't help
336
        if (action == NpcAction::Close)
337
        {
338
            closeDialog(npcId);
339
            return npcId;
340
        }
341
        else if (action == NpcAction::Next)
342
        {
343
            nextDialog(npcId);
344
            return npcId;
345
        }
346
        else
347
        {
348
            CREATEWIDGETV(Ea::NpcRecv::mDialog, NpcDialog, npcId);
349
            Ea::NpcRecv::mDialog->saveCamera();
350
            if (localPlayer != nullptr)
351
                localPlayer->stopWalking(false);
352
            NpcDialog::mNpcDialogs[npcId] = Ea::NpcRecv::mDialog;
353
        }
354
    }
355
    else
356
    {
357
        NpcDialog *const dialog = diag->second;
358
        if (Ea::NpcRecv::mDialog != nullptr && Ea::NpcRecv::mDialog != dialog)
359
            Ea::NpcRecv::mDialog->restoreCamera();
360
        Ea::NpcRecv::mDialog = dialog;
361
        if (Ea::NpcRecv::mDialog != nullptr)
362
            Ea::NpcRecv::mDialog->saveCamera();
363
    }
364
    return npcId;
365
}
366
367
void NpcHandler::requestAirship(const std::string &mapName,
368
                                const int itemId) const
369
{
370
    if (packetVersionRe < 20180321 &&
371
        packetVersionMain < 20180620)
372
    {
373
        return;
374
    }
375
    createOutPacket(CMSG_PRIVATE_AIRSHIP_REQUEST);
376
    outMsg.writeString(mapName, 16, "map name");
377
    outMsg.writeInt16(CAST_S16(itemId), "item");
378
}
379
380
2
}  // namespace EAthena