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

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