GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/progs/manaplus/actions/pets.cpp Lines: 1 102 1.0 %
Date: 2021-03-17 Branches: 0 76 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2012-2019  The ManaPlus Developers
4
 *  Copyright (C) 2019-2021  Andrei Karas
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#include "actions/pets.h"
23
24
#include "actormanager.h"
25
#include "game.h"
26
27
#include "actions/actiondef.h"
28
29
#include "being/localplayer.h"
30
#include "being/playerinfo.h"
31
32
#include "enums/being/beingdirection.h"
33
34
#include "input/inputactionoperators.h"
35
36
#include "listeners/inputactionreplaylistener.h"
37
38
#include "gui/shortcut/emoteshortcut.h"
39
40
#include "net/chathandler.h"
41
#include "net/pethandler.h"
42
#include "net/serverfeatures.h"
43
44
#include "utils/chatutils.h"
45
#include "utils/gettext.h"
46
#include "utils/stringutils.h"
47
48
#include "debug.h"
49
50
namespace Actions
51
{
52
53
static const Being *getPet()
54
{
55
    const BeingId id = PlayerInfo::getPetBeingId();
56
    if (id == BeingId_zero)
57
        return nullptr;
58
    return actorManager->findBeing(id);
59
}
60
61
impHandler(commandEmotePet)
62
{
63
    petHandler->emote(CAST_U8(atoi(event.args.c_str())));
64
    return true;
65
}
66
67
impHandler(talkPet)
68
{
69
    if (!serverFeatures->haveTalkPet())
70
        return false;
71
72
    std::string args = event.args;
73
    if (findCutFirst(args, "/me "))
74
        args = textToMe(args);
75
    chatHandler->talkPet(args);
76
    return true;
77
}
78
79
impHandler(setPetName)
80
{
81
    const std::string args = event.args;
82
    if (args.empty())
83
    {
84
        const Being *const pet = getPet();
85
        if (pet == nullptr)
86
            return false;
87
        // TRANSLATORS: dialog header
88
        inputActionReplayListener.openDialog(_("Rename your pet"),
89
            pet->getName(),
90
            InputAction::PET_SET_NAME);
91
        return false;
92
    }
93
94
    petHandler->setName(args);
95
    return true;
96
}
97
98
impHandler(petEmote)
99
{
100
    if (!serverFeatures->haveTalkPet())
101
        return false;
102
103
    if (event.action >= InputAction::PET_EMOTE_1
104
        && event.action <= InputAction::PET_EMOTE_48)
105
    {
106
        if (emoteShortcut != nullptr)
107
        {
108
            const int emotion = event.action - InputAction::PET_EMOTE_1;
109
            petHandler->emote(emoteShortcut->getEmote(emotion));
110
        }
111
        if (Game::instance() != nullptr)
112
            Game::instance()->setValidSpeed();
113
        return true;
114
    }
115
116
    return false;
117
}
118
119
impHandler(catchPet)
120
{
121
    if ((localPlayer == nullptr) || (actorManager == nullptr))
122
        return false;
123
124
    Being *target = nullptr;
125
    const std::string args = event.args;
126
    if (!args.empty())
127
    {
128
        if (args[0] == ':')
129
        {
130
            target = actorManager->findBeing(fromInt(atoi(
131
                args.substr(1).c_str()), BeingId));
132
        }
133
        else
134
        {
135
            target = actorManager->findNearestByName(args,
136
                ActorType::Unknown);
137
        }
138
    }
139
140
    if (target == nullptr)
141
        target = localPlayer->getTarget();
142
    else
143
        localPlayer->setTarget(target);
144
    if (target != nullptr)
145
        petHandler->catchPet(target);
146
    return true;
147
}
148
149
impHandler0(petMoveUp)
150
{
151
    const Being *const pet = getPet();
152
    if (pet == nullptr)
153
        return false;
154
    petHandler->move(pet->getTileX(), pet->getTileY() - 1);
155
    return true;
156
}
157
158
impHandler0(petMoveDown)
159
{
160
    const Being *const pet = getPet();
161
    if (pet == nullptr)
162
        return false;
163
    petHandler->move(pet->getTileX(), pet->getTileY() + 1);
164
    return true;
165
}
166
167
impHandler0(petMoveLeft)
168
{
169
    const Being *const pet = getPet();
170
    if (pet == nullptr)
171
        return false;
172
    petHandler->move(pet->getTileX() - 1, pet->getTileY());
173
    return true;
174
}
175
176
impHandler0(petMoveRight)
177
{
178
    const Being *const pet = getPet();
179
    if (pet == nullptr)
180
        return false;
181
    petHandler->move(pet->getTileX() + 1, pet->getTileY());
182
    return true;
183
}
184
185
impHandler0(petDirectUp)
186
{
187
    petHandler->setDirection(BeingDirection::UP);
188
    return true;
189
}
190
191
impHandler0(petDirectDown)
192
{
193
    petHandler->setDirection(BeingDirection::DOWN);
194
    return true;
195
}
196
197
impHandler0(petDirectLeft)
198
{
199
    petHandler->setDirection(BeingDirection::LEFT);
200
    return true;
201
}
202
203
impHandler0(petDirectRight)
204
{
205
    petHandler->setDirection(BeingDirection::RIGHT);
206
    return true;
207
}
208
209
impHandler(petMove)
210
{
211
    int x = 0;
212
    int y = 0;
213
214
    if (parse2Int(event.args, x, y))
215
    {
216
        petHandler->move(x, y);
217
        return true;
218
    }
219
    return false;
220
}
221
222
impHandler0(petFeed)
223
{
224
    if (petHandler != nullptr)
225
        petHandler->feed();
226
    return true;
227
}
228
229
impHandler0(petDropLoot)
230
{
231
    if (petHandler != nullptr)
232
        petHandler->dropLoot();
233
    return true;
234
}
235
236
impHandler0(petReturnToEgg)
237
{
238
    if (petHandler != nullptr)
239
        petHandler->returnToEgg();
240
    return true;
241
}
242
243
impHandler0(petUnequip)
244
{
245
    if (petHandler != nullptr)
246
        petHandler->unequip();
247
    return true;
248
}
249
250
2
}  // namespace Actions