GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/ea/npcrecv.cpp Lines: 1 118 0.8 %
Date: 2017-11-29 Branches: 0 89 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/ea/npcrecv.h"
24
25
#include "gui/viewport.h"
26
27
#include "gui/windows/npcdialog.h"
28
29
#include "net/messagein.h"
30
#include "net/npchandler.h"
31
32
#include "utils/langs.h"
33
34
#include "debug.h"
35
36
namespace Ea
37
{
38
39
namespace NpcRecv
40
{
41
    NpcDialog *mDialog = nullptr;
42
    bool mRequestLang = false;
43
}  // namespace NpcRecv
44
45
void NpcRecv::processNpcChoice(Net::MessageIn &msg)
46
{
47
    msg.readInt16("len");
48
    npcHandler->getNpc(msg, NpcAction::Other);
49
    mRequestLang = false;
50
51
    if (mDialog != nullptr)
52
    {
53
        mDialog->choiceRequest();
54
        mDialog->parseListItems(msg.readString(msg.getLength() - 8,
55
            "select items"));
56
    }
57
    else
58
    {
59
        msg.readString(msg.getLength() - 8, "select items");
60
    }
61
}
62
63
void NpcRecv::processNpcMessage(Net::MessageIn &msg)
64
{
65
    msg.readInt16("len");
66
    npcHandler->getNpc(msg, NpcAction::Other);
67
    mRequestLang = false;
68
69
    const std::string message = msg.readString(msg.getLength() - 8, "message");
70
    // ignore future legacy npc commands.
71
    if (message.size() > 3 && message.substr(0, 3) == "###")
72
        return;
73
    if (mDialog != nullptr)
74
        mDialog->addText(message);
75
}
76
77
void NpcRecv::processNpcClose(Net::MessageIn &msg)
78
{
79
    // Show the close button
80
    npcHandler->getNpc(msg, NpcAction::Close);
81
    mRequestLang = false;
82
    if (mDialog != nullptr)
83
        mDialog->showCloseButton();
84
}
85
86
void NpcRecv::processNpcNext(Net::MessageIn &msg)
87
{
88
    // Show the next button
89
    npcHandler->getNpc(msg, NpcAction::Next);
90
    mRequestLang = false;
91
    if (mDialog != nullptr)
92
        mDialog->showNextButton();
93
}
94
95
void NpcRecv::processNpcIntInput(Net::MessageIn &msg)
96
{
97
    // Request for an integer
98
    npcHandler->getNpc(msg, NpcAction::Other);
99
    mRequestLang = false;
100
    if (mDialog != nullptr)
101
        mDialog->integerRequest(0);
102
}
103
104
void NpcRecv::processNpcStrInput(Net::MessageIn &msg)
105
{
106
    // Request for a string
107
    BeingId npcId = npcHandler->getNpc(msg, NpcAction::Other);
108
    if (mRequestLang)
109
    {
110
        mRequestLang = false;
111
        npcHandler->stringInput(npcId, getLangSimple());
112
    }
113
    else if (mDialog != nullptr)
114
    {
115
        mDialog->textRequest("");
116
    }
117
}
118
119
void NpcRecv::processNpcCommand(Net::MessageIn &msg)
120
{
121
    const BeingId npcId = npcHandler->getNpc(msg, NpcAction::Other);
122
    mRequestLang = false;
123
124
    const int cmd = msg.readInt16("cmd");
125
    const BeingId id = msg.readBeingId("id");
126
    const int x = msg.readInt16("x");
127
    const int y = msg.readInt16("y");
128
    switch (cmd)
129
    {
130
        case 0:
131
            mRequestLang = true;
132
            break;
133
134
        case 1:
135
            if (viewport != nullptr)
136
                viewport->moveCameraToActor(npcId);
137
            break;
138
139
        case 2:
140
            if (viewport != nullptr)
141
            {
142
                if (id == BeingId_zero)
143
                    viewport->moveCameraToPosition(x, y);
144
                else
145
                    viewport->moveCameraToActor(id, x, y);
146
            }
147
            break;
148
149
        case 3:
150
            if (viewport != nullptr)
151
                viewport->returnCamera();
152
            break;
153
154
        case 4:
155
            if (viewport != nullptr)
156
            {
157
                viewport->moveCameraRelative(x, y);
158
            }
159
            break;
160
        case 5:  // close dialog
161
            if (mDialog != nullptr)
162
                mDialog->restoreCamera();
163
            npcHandler->closeDialog(npcId);
164
            break;
165
        case 6:  // show avatar
166
            if (mDialog != nullptr)
167
            {
168
                mDialog->showAvatar(fromInt(id, BeingTypeId));
169
            }
170
            break;
171
        case 7:  // set avatar direction
172
            if (mDialog != nullptr)
173
            {
174
                mDialog->setAvatarDirection(
175
                    Net::MessageIn::fromServerDirection(
176
                    CAST_U8(id)));
177
            }
178
            break;
179
        case 8:  // set avatar action
180
            if (mDialog != nullptr)
181
                mDialog->setAvatarAction(toInt(id, int));
182
            break;
183
        case 9:  // clear npc dialog
184
            if (mDialog != nullptr)
185
                mDialog->clearRows();
186
            break;
187
        case 10:  // send selected item id
188
        {
189
            int invSize = toInt(id, int);
190
            if (invSize == 0)
191
                invSize = 1;
192
            if (mDialog != nullptr)
193
                mDialog->itemRequest(invSize);
194
            break;
195
        }
196
        case 11:  // send selected item index
197
        {
198
            int invSize = toInt(id, int);
199
            if (invSize == 0)
200
                invSize = 1;
201
            if (mDialog != nullptr)
202
                mDialog->itemIndexRequest(invSize);
203
            break;
204
        }
205
        case 12:  // send complex items
206
        {
207
            int invSize = toInt(id, int);
208
            if (invSize == 0)
209
                invSize = 1;
210
            if (mDialog != nullptr)
211
                mDialog->itemCraftRequest(invSize);
212
            break;
213
        }
214
        case 14:
215
        {
216
            const NpcDialogs::iterator it = NpcDialog::mNpcDialogs.find(npcId);
217
            if (it != NpcDialog::mNpcDialogs.end())
218
            {
219
                NpcDialog *const dialog = (*it).second;
220
                if (dialog != nullptr)
221
                    dialog->close();
222
                if (dialog == Ea::NpcRecv::mDialog)
223
                    Ea::NpcRecv::mDialog = nullptr;
224
                NpcDialog::mNpcDialogs.erase(it);
225
            }
226
            break;
227
        }
228
        default:
229
            UNIMPLEMENTEDPACKETFIELD(cmd);
230
            break;
231
    }
232
}
233
234
void NpcRecv::processChangeTitle(Net::MessageIn &msg)
235
{
236
    msg.readInt16("len");
237
    npcHandler->getNpc(msg, NpcAction::Other);
238
    mRequestLang = false;
239
    if (mDialog != nullptr)
240
    {
241
        const std::string str = msg.readString(-1, "title");
242
        mDialog->setCaption(str);
243
    }
244
}
245
246
4
}  // namespace Ea