GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/progs/manaplus/actions/pets.cpp Lines: 1 99 1.0 %
Date: 2017-11-29 Branches: 2 80 2.5 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2012-2017  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 "const/gui/chat.h"
34
35
#include "input/inputactionoperators.h"
36
37
#include "listeners/inputactionreplaylistener.h"
38
39
#include "gui/shortcut/emoteshortcut.h"
40
41
#include "net/chathandler.h"
42
#include "net/pethandler.h"
43
#include "net/serverfeatures.h"
44
45
#include "utils/chatutils.h"
46
#include "utils/gettext.h"
47
#include "utils/stringutils.h"
48
49
#include "debug.h"
50
51
namespace Actions
52
{
53
54
static const Being *getPet()
55
{
56
    const BeingId id = PlayerInfo::getPetBeingId();
57
    if (id == BeingId_zero)
58
        return nullptr;
59
    return actorManager->findBeing(id);
60
}
61
62
impHandler(commandEmotePet)
63
{
64
    petHandler->emote(CAST_U8(atoi(event.args.c_str())));
65
    return true;
66
}
67
68
impHandler(talkPet)
69
{
70
    if (!serverFeatures->haveTalkPet())
71
        return false;
72
73
    std::string args = event.args;
74
    if (findCutFirst(args, "/me "))
75
        args = textToMe(args);
76
    chatHandler->talkPet(args, GENERAL_CHANNEL);
77
    return true;
78
}
79
80
impHandler(setPetName)
81
{
82
    const std::string args = event.args;
83
    if (args.empty())
84
    {
85
        const Being *const pet = getPet();
86
        if (pet == nullptr)
87
            return false;
88
        // TRANSLATORS: dialog header
89
        inputActionReplayListener.openDialog(_("Rename your pet"),
90
            pet->getName(),
91
            InputAction::PET_SET_NAME);
92
        return false;
93
    }
94
95
    petHandler->setName(args);
96
    return true;
97
}
98
99
impHandler(petEmote)
100
{
101
    if (!serverFeatures->haveTalkPet())
102
        return false;
103
104
    if (event.action >= InputAction::PET_EMOTE_1
105
        && event.action <= InputAction::PET_EMOTE_48)
106
    {
107
        const int emotion = event.action - InputAction::PET_EMOTE_1;
108
        if (emoteShortcut != nullptr)
109
            petHandler->emote(emoteShortcut->getEmote(emotion));
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
        }
136
    }
137
138
    if (target == nullptr)
139
        target = localPlayer->getTarget();
140
    else
141
        localPlayer->setTarget(target);
142
    if (target != nullptr)
143
        petHandler->catchPet(target);
144
    return true;
145
}
146
147
impHandler0(petMoveUp)
148
{
149
    const Being *const pet = getPet();
150
    if (pet == nullptr)
151
        return false;
152
    petHandler->move(pet->getTileX(), pet->getTileY() - 1);
153
    return true;
154
}
155
156
impHandler0(petMoveDown)
157
{
158
    const Being *const pet = getPet();
159
    if (pet == nullptr)
160
        return false;
161
    petHandler->move(pet->getTileX(), pet->getTileY() + 1);
162
    return true;
163
}
164
165
impHandler0(petMoveLeft)
166
{
167
    const Being *const pet = getPet();
168
    if (pet == nullptr)
169
        return false;
170
    petHandler->move(pet->getTileX() - 1, pet->getTileY());
171
    return true;
172
}
173
174
impHandler0(petMoveRight)
175
{
176
    const Being *const pet = getPet();
177
    if (pet == nullptr)
178
        return false;
179
    petHandler->move(pet->getTileX() + 1, pet->getTileY());
180
    return true;
181
}
182
183
impHandler0(petDirectUp)
184
{
185
    petHandler->setDirection(BeingDirection::UP);
186
    return true;
187
}
188
189
impHandler0(petDirectDown)
190
{
191
    petHandler->setDirection(BeingDirection::DOWN);
192
    return true;
193
}
194
195
impHandler0(petDirectLeft)
196
{
197
    petHandler->setDirection(BeingDirection::LEFT);
198
    return true;
199
}
200
201
impHandler0(petDirectRight)
202
{
203
    petHandler->setDirection(BeingDirection::RIGHT);
204
    return true;
205
}
206
207
impHandler(petMove)
208
{
209
    int x = 0;
210
    int y = 0;
211
212
    if (parse2Int(event.args, x, y))
213
    {
214
        petHandler->move(x, y);
215
        return true;
216
    }
217
    return false;
218
}
219
220
impHandler0(petFeed)
221
{
222
    if (petHandler != nullptr)
223
        petHandler->feed();
224
    return true;
225
}
226
227
impHandler0(petDropLoot)
228
{
229
    if (petHandler != nullptr)
230
        petHandler->dropLoot();
231
    return true;
232
}
233
234
impHandler0(petReturnToEgg)
235
{
236
    if (petHandler != nullptr)
237
        petHandler->returnToEgg();
238
    return true;
239
}
240
241
impHandler0(petUnequip)
242
{
243
    if (petHandler != nullptr)
244
        petHandler->unequip();
245
    return true;
246
}
247
248

6
}  // namespace Actions