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

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2012-2018  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
        const int emotion = event.action - InputAction::PET_EMOTE_1;
106
        if (emoteShortcut != nullptr)
107
            petHandler->emote(emoteShortcut->getEmote(emotion));
108
        if (Game::instance() != nullptr)
109
            Game::instance()->setValidSpeed();
110
        return true;
111
    }
112
113
    return false;
114
}
115
116
impHandler(catchPet)
117
{
118
    if ((localPlayer == nullptr) || (actorManager == nullptr))
119
        return false;
120
121
    Being *target = nullptr;
122
    const std::string args = event.args;
123
    if (!args.empty())
124
    {
125
        if (args[0] == ':')
126
        {
127
            target = actorManager->findBeing(fromInt(atoi(
128
                args.substr(1).c_str()), BeingId));
129
        }
130
        else
131
        {
132
            target = actorManager->findNearestByName(args,
133
                ActorType::Unknown);
134
        }
135
    }
136
137
    if (target == nullptr)
138
        target = localPlayer->getTarget();
139
    else
140
        localPlayer->setTarget(target);
141
    if (target != nullptr)
142
        petHandler->catchPet(target);
143
    return true;
144
}
145
146
impHandler0(petMoveUp)
147
{
148
    const Being *const pet = getPet();
149
    if (pet == nullptr)
150
        return false;
151
    petHandler->move(pet->getTileX(), pet->getTileY() - 1);
152
    return true;
153
}
154
155
impHandler0(petMoveDown)
156
{
157
    const Being *const pet = getPet();
158
    if (pet == nullptr)
159
        return false;
160
    petHandler->move(pet->getTileX(), pet->getTileY() + 1);
161
    return true;
162
}
163
164
impHandler0(petMoveLeft)
165
{
166
    const Being *const pet = getPet();
167
    if (pet == nullptr)
168
        return false;
169
    petHandler->move(pet->getTileX() - 1, pet->getTileY());
170
    return true;
171
}
172
173
impHandler0(petMoveRight)
174
{
175
    const Being *const pet = getPet();
176
    if (pet == nullptr)
177
        return false;
178
    petHandler->move(pet->getTileX() + 1, pet->getTileY());
179
    return true;
180
}
181
182
impHandler0(petDirectUp)
183
{
184
    petHandler->setDirection(BeingDirection::UP);
185
    return true;
186
}
187
188
impHandler0(petDirectDown)
189
{
190
    petHandler->setDirection(BeingDirection::DOWN);
191
    return true;
192
}
193
194
impHandler0(petDirectLeft)
195
{
196
    petHandler->setDirection(BeingDirection::LEFT);
197
    return true;
198
}
199
200
impHandler0(petDirectRight)
201
{
202
    petHandler->setDirection(BeingDirection::RIGHT);
203
    return true;
204
}
205
206
impHandler(petMove)
207
{
208
    int x = 0;
209
    int y = 0;
210
211
    if (parse2Int(event.args, x, y))
212
    {
213
        petHandler->move(x, y);
214
        return true;
215
    }
216
    return false;
217
}
218
219
impHandler0(petFeed)
220
{
221
    if (petHandler != nullptr)
222
        petHandler->feed();
223
    return true;
224
}
225
226
impHandler0(petDropLoot)
227
{
228
    if (petHandler != nullptr)
229
        petHandler->dropLoot();
230
    return true;
231
}
232
233
impHandler0(petReturnToEgg)
234
{
235
    if (petHandler != nullptr)
236
        petHandler->returnToEgg();
237
    return true;
238
}
239
240
impHandler0(petUnequip)
241
{
242
    if (petHandler != nullptr)
243
        petHandler->unequip();
244
    return true;
245
}
246
247
2
}  // namespace Actions