ManaPlus
pets.cpp
Go to the documentation of this file.
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 
32 
33 #include "const/gui/chat.h"
34 
36 
38 
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 {
57  if (id == BeingId_zero)
58  return nullptr;
59  return actorManager->findBeing(id);
60 }
61 
63 {
64  petHandler->emote(CAST_U8(atoi(event.args.c_str())));
65  return true;
66 }
67 
69 {
71  return false;
72 
73  std::string args = event.args;
74  if (findCutFirst(args, "/me "))
75  args = textToMe(args);
77  return true;
78 }
79 
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(),
92  return false;
93  }
94 
95  petHandler->setName(args);
96  return true;
97 }
98 
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)
110  if (Game::instance() != nullptr)
112  return true;
113  }
114 
115  return false;
116 }
117 
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,
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 
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 
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 
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 
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 
185 {
187  return true;
188 }
189 
191 {
193  return true;
194 }
195 
197 {
199  return true;
200 }
201 
203 {
205  return true;
206 }
207 
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 
222 {
223  if (petHandler != nullptr)
224  petHandler->feed();
225  return true;
226 }
227 
229 {
230  if (petHandler != nullptr)
231  petHandler->dropLoot();
232  return true;
233 }
234 
236 {
237  if (petHandler != nullptr)
239  return true;
240 }
241 
243 {
244  if (petHandler != nullptr)
245  petHandler->unequip();
246  return true;
247 }
248 
249 } // namespace Actions
#define _(s)
Definition: gettext.h:34
bool petDirectDown(InputEvent &event)
Definition: pets.cpp:40
int getTileY() const
Definition: being.h:173
bool commandEmotePet(InputEvent &event)
Definition: pets.cpp:30
#define CAST_U8
Definition: cast.h:26
bool petEmote(InputEvent &event)
Definition: pets.cpp:33
bool petMoveRight(InputEvent &event)
Definition: pets.cpp:38
bool petDropLoot(InputEvent &event)
Definition: pets.cpp:45
void setValidSpeed()
Definition: game.cpp:1264
#define fromInt(val, name)
Definition: intdefines.h:45
Net::PetHandler * petHandler
Definition: net.cpp:88
static const Being * getPet()
Definition: pets.cpp:54
Being * findNearestByName(const std::string &name, const ActorTypeT &type) const
bool petFeed(InputEvent &event)
Definition: pets.cpp:44
int BeingId
Definition: beingid.h:29
bool petUnequip(InputEvent &event)
Definition: pets.cpp:47
bool petDirectRight(InputEvent &event)
Definition: pets.cpp:42
virtual void feed() const =0
#define impHandler0(name)
Definition: actiondef.h:33
virtual void move(const int x, const int y) const =0
virtual void catchPet(const Being *const being) const =0
bool petDirectLeft(InputEvent &event)
Definition: pets.cpp:41
virtual void setDirection(const unsigned char type) const =0
Net::ServerFeatures * serverFeatures
Definition: net.cpp:94
bool petDirectUp(InputEvent &event)
Definition: pets.cpp:39
bool parse2Int(const std::string &args, int &x, int &y)
bool petMoveLeft(InputEvent &event)
Definition: pets.cpp:37
virtual bool haveTalkPet() const =0
virtual void dropLoot() const =0
LocalPlayer * localPlayer
virtual void talkPet(const std::string &text, const std::string &channel) const =0
Being * findBeing(const BeingId id) const
bool petReturnToEgg(InputEvent &event)
Definition: pets.cpp:46
unsigned char getEmote(const size_t index) const
Definition: emoteshortcut.h:59
virtual void returnToEgg() const =0
const std::string & getName() const
Definition: being.h:231
bool setPetName(InputEvent &event)
Definition: pets.cpp:32
const std::string GENERAL_CHANNEL
Definition: chat.h:28
std::string textToMe(const std::string &str)
Definition: chatutils.cpp:198
EmoteShortcut * emoteShortcut
bool petMoveDown(InputEvent &event)
Definition: pets.cpp:36
Net::ChatHandler * chatHandler
Definition: net.cpp:80
InputActionReplayListener inputActionReplayListener
void openDialog(const std::string &caption, const std::string &text, const InputActionT action0)
void setTarget(Being *const target)
virtual void emote(const uint8_t emoteId)=0
bool petMoveUp(InputEvent &event)
Definition: pets.cpp:35
static Game * instance()
Definition: game.h:81
bool catchPet(InputEvent &event)
Definition: pets.cpp:34
int getTileX() const
Definition: being.h:167
BeingId getPetBeingId()
Definition: playerinfo.cpp:577
const BeingId BeingId_zero
Definition: beingid.h:29
bool talkPet(InputEvent &event)
Definition: pets.cpp:31
bool findCutFirst(std::string &str1, const std::string &str2)
bool petMove(InputEvent &event)
Definition: pets.cpp:43
Definition: being.h:93
virtual void setName(const std::string &name) const =0
#define impHandler(name)
Definition: actiondef.h:32
ActorManager * actorManager
Being * getTarget() const
virtual void unequip() const =0