GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/tmwa/npchandler.cpp Lines: 1 95 1.1 %
Date: 2017-11-29 Branches: 0 94 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-2017  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/tmwa/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/tmwa/messageout.h"
36
#include "net/tmwa/protocolout.h"
37
38
#include "net/ea/npcrecv.h"
39
40
#include "debug.h"
41
42
namespace TmwAthena
43
{
44
45
NpcHandler::NpcHandler() :
46
    Ea::NpcHandler()
47
{
48
    npcHandler = this;
49
}
50
51
NpcHandler::~NpcHandler()
52
{
53
    npcHandler = nullptr;
54
}
55
56
void NpcHandler::talk(const Being *const being) const
57
{
58
    if (being == nullptr)
59
        return;
60
    createOutPacket(CMSG_NPC_TALK);
61
    outMsg.writeBeingId(being->getId(), "npc id");
62
    outMsg.writeInt8(0, "unused");
63
}
64
65
void NpcHandler::nextDialog(const BeingId npcId) const
66
{
67
    createOutPacket(CMSG_NPC_NEXT_REQUEST);
68
    outMsg.writeBeingId(npcId, "npc id");
69
}
70
71
void NpcHandler::closeDialog(const BeingId npcId)
72
{
73
    createOutPacket(CMSG_NPC_CLOSE);
74
    outMsg.writeBeingId(npcId, "npc id");
75
76
    const NpcDialogs::iterator it = NpcDialog::mNpcDialogs.find(npcId);
77
    if (it != NpcDialog::mNpcDialogs.end())
78
    {
79
        NpcDialog *const dialog = (*it).second;
80
        if (dialog != nullptr)
81
            dialog->close();
82
        if (dialog == Ea::NpcRecv::mDialog)
83
            Ea::NpcRecv::mDialog = nullptr;
84
        NpcDialog::mNpcDialogs.erase(it);
85
    }
86
}
87
88
void NpcHandler::listInput(const BeingId npcId,
89
                           const unsigned char value) const
90
{
91
    createOutPacket(CMSG_NPC_LIST_CHOICE);
92
    outMsg.writeBeingId(npcId, "npc id");
93
    outMsg.writeInt8(value, "value");
94
}
95
96
void NpcHandler::integerInput(const BeingId npcId,
97
                              const int value) const
98
{
99
    createOutPacket(CMSG_NPC_INT_RESPONSE);
100
    outMsg.writeBeingId(npcId, "npc id");
101
    outMsg.writeInt32(value, "value");
102
}
103
104
void NpcHandler::stringInput(const BeingId npcId,
105
                             const std::string &value) const
106
{
107
    createOutPacket(CMSG_NPC_STR_RESPONSE);
108
    outMsg.writeInt16(CAST_S16(value.length() + 9), "len");
109
    outMsg.writeBeingId(npcId, "npc id");
110
    outMsg.writeString(value, CAST_S32(value.length()), "value");
111
    outMsg.writeInt8(0, "null byte");
112
}
113
114
void NpcHandler::buy(const Being *const being) const
115
{
116
    if (being == nullptr)
117
        return;
118
    createOutPacket(CMSG_NPC_BUY_SELL_REQUEST);
119
    outMsg.writeBeingId(being->getId(), "npc id");
120
    outMsg.writeInt8(0, "action");
121
}
122
123
void NpcHandler::buy(const BeingId beingId) const
124
{
125
    createOutPacket(CMSG_NPC_BUY_SELL_REQUEST);
126
    outMsg.writeBeingId(beingId, "npc id");
127
    outMsg.writeInt8(0, "action");
128
}
129
130
void NpcHandler::sell(const BeingId beingId) const
131
{
132
    createOutPacket(CMSG_NPC_BUY_SELL_REQUEST);
133
    outMsg.writeBeingId(beingId, "npc id");
134
    outMsg.writeInt8(1, "action");
135
}
136
137
void NpcHandler::buyItem(const BeingId beingId A_UNUSED,
138
                         const int itemId,
139
                         const ItemColor color A_UNUSED,
140
                         const int amount) const
141
{
142
    createOutPacket(CMSG_NPC_BUY_REQUEST);
143
    outMsg.writeInt16(8, "len");
144
    outMsg.writeInt16(CAST_S16(amount), "amount");
145
    outMsg.writeInt16(CAST_S16(itemId), "item id");
146
}
147
148
void NpcHandler::buyItems(STD_VECTOR<ShopItem*> &items A_UNUSED) const
149
{
150
}
151
152
void NpcHandler::sellItem(const BeingId beingId A_UNUSED,
153
                          const int itemId,
154
                          const int amount) const
155
{
156
    createOutPacket(CMSG_NPC_SELL_REQUEST);
157
    outMsg.writeInt16(8, "len");
158
    outMsg.writeInt16(CAST_S16(
159
        itemId + INVENTORY_OFFSET), "index");
160
    outMsg.writeInt16(CAST_S16(amount), "amount");
161
}
162
163
void NpcHandler::sellItems(STD_VECTOR<ShopItem*> &items A_UNUSED) const
164
{
165
}
166
167
void NpcHandler::completeProgressBar() const
168
{
169
}
170
171
void NpcHandler::produceMix(const int nameId A_UNUSED,
172
                            const int materialId1 A_UNUSED,
173
                            const int materialId2 A_UNUSED,
174
                            const int materialId3 A_UNUSED) const
175
{
176
}
177
178
void NpcHandler::cooking(const CookingTypeT type A_UNUSED,
179
                         const int nameId A_UNUSED) const
180
{
181
}
182
183
void NpcHandler::repair(const int index A_UNUSED) const
184
{
185
}
186
187
void NpcHandler::refine(const int index A_UNUSED) const
188
{
189
}
190
191
void NpcHandler::identify(const int index A_UNUSED) const
192
{
193
}
194
195
void NpcHandler::selectArrow(const int nameId A_UNUSED) const
196
{
197
}
198
199
void NpcHandler::selectAutoSpell(const int skillId A_UNUSED) const
200
{
201
}
202
203
BeingId NpcHandler::getNpc(Net::MessageIn &msg,
204
                           const NpcActionT action)
205
{
206
    const BeingId npcId = msg.readBeingId("npc id");
207
208
    const NpcDialogs::const_iterator diag = NpcDialog::mNpcDialogs.find(npcId);
209
    Ea::NpcRecv::mDialog = nullptr;
210
211
    if (diag == NpcDialog::mNpcDialogs.end())
212
    {
213
        // +++ must be removed packet id checks from here
214
        // Empty dialogs don't help
215
        if (action == NpcAction::Close)
216
        {
217
            closeDialog(npcId);
218
            return npcId;
219
        }
220
        else if (action == NpcAction::Next)
221
        {
222
            nextDialog(npcId);
223
            return npcId;
224
        }
225
        else
226
        {
227
            CREATEWIDGETV(Ea::NpcRecv::mDialog, NpcDialog, npcId);
228
            Ea::NpcRecv::mDialog->saveCamera();
229
            if (localPlayer != nullptr)
230
                localPlayer->stopWalking(false);
231
            NpcDialog::mNpcDialogs[npcId] = Ea::NpcRecv::mDialog;
232
        }
233
    }
234
    else
235
    {
236
        NpcDialog *const dialog = diag->second;
237
        if (Ea::NpcRecv::mDialog != nullptr && Ea::NpcRecv::mDialog != dialog)
238
            Ea::NpcRecv::mDialog->restoreCamera();
239
        Ea::NpcRecv::mDialog = dialog;
240
        if (Ea::NpcRecv::mDialog != nullptr)
241
            Ea::NpcRecv::mDialog->saveCamera();
242
    }
243
    return npcId;
244
}
245
246
4
}  // namespace TmwAthena