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