ManaPlus
pets.cpp
Go to the documentation of this file.
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 
33 
35 
37 
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 {
56  if (id == BeingId_zero)
57  return nullptr;
58  return actorManager->findBeing(id);
59 }
60 
62 {
63  petHandler->emote(CAST_U8(atoi(event.args.c_str())));
64  return true;
65 }
66 
68 {
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 
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(),
91  return false;
92  }
93 
94  petHandler->setName(args);
95  return true;
96 }
97 
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;
110  }
111  if (Game::instance() != nullptr)
113  return true;
114  }
115 
116  return false;
117 }
118 
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,
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 
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 
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 
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 
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 
186 {
188  return true;
189 }
190 
192 {
194  return true;
195 }
196 
198 {
200  return true;
201 }
202 
204 {
206  return true;
207 }
208 
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 
223 {
224  if (petHandler != nullptr)
225  petHandler->feed();
226  return true;
227 }
228 
230 {
231  if (petHandler != nullptr)
232  petHandler->dropLoot();
233  return true;
234 }
235 
237 {
238  if (petHandler != nullptr)
240  return true;
241 }
242 
244 {
245  if (petHandler != nullptr)
246  petHandler->unequip();
247  return true;
248 }
249 
250 } // namespace Actions
#define impHandler0(name)
Definition: actiondef.h:34
#define impHandler(name)
Definition: actiondef.h:33
ActorManager * actorManager
int BeingId
Definition: beingid.h:30
const BeingId BeingId_zero
Definition: beingid.h:30
#define CAST_U8
Definition: cast.h:27
Net::ChatHandler * chatHandler
Definition: net.cpp:86
std::string textToMe(const std::string &str)
Definition: chatutils.cpp:203
Being * findBeing(const BeingId id) const
Being * findNearestByName(const std::string &name, const ActorTypeT &type) const
Definition: being.h:96
int getTileX() const
Definition: being.h:168
int getTileY() const
Definition: being.h:174
const std::string & getName() const
Definition: being.h:232
unsigned char getEmote(const size_t index) const
Definition: emoteshortcut.h:60
static Game * instance()
Definition: game.h:82
void setValidSpeed()
Definition: game.cpp:1273
void openDialog(const std::string &caption, const std::string &text, const InputActionT action0)
void setTarget(Being *const target)
Being * getTarget() const
virtual void talkPet(const std::string &text) const =0
virtual void setName(const std::string &name) const =0
virtual void unequip() const =0
virtual void setDirection(const unsigned char type) const =0
virtual void emote(const uint8_t emoteId)=0
virtual void catchPet(const Being *const being) const =0
virtual void returnToEgg() const =0
virtual void move(const int x, const int y) const =0
virtual void dropLoot() const =0
virtual void feed() const =0
virtual bool haveTalkPet() const =0
EmoteShortcut * emoteShortcut
#define _(s)
Definition: gettext.h:35
InputActionReplayListener inputActionReplayListener
#define fromInt(val, name)
Definition: intdefines.h:46
LocalPlayer * localPlayer
bool petDirectRight(InputEvent &event)
Definition: pets.cpp:43
bool petMoveLeft(InputEvent &event)
Definition: pets.cpp:38
bool petEmote(InputEvent &event)
Definition: pets.cpp:34
bool petMoveDown(InputEvent &event)
Definition: pets.cpp:37
bool petUnequip(InputEvent &event)
Definition: pets.cpp:48
bool petDropLoot(InputEvent &event)
Definition: pets.cpp:46
bool petDirectLeft(InputEvent &event)
Definition: pets.cpp:42
bool commandEmotePet(InputEvent &event)
Definition: pets.cpp:31
bool setPetName(InputEvent &event)
Definition: pets.cpp:33
static const Being * getPet()
Definition: pets.cpp:53
bool petReturnToEgg(InputEvent &event)
Definition: pets.cpp:47
bool petMoveUp(InputEvent &event)
Definition: pets.cpp:36
bool petDirectDown(InputEvent &event)
Definition: pets.cpp:41
bool petMoveRight(InputEvent &event)
Definition: pets.cpp:39
bool petFeed(InputEvent &event)
Definition: pets.cpp:45
bool catchPet(InputEvent &event)
Definition: pets.cpp:35
bool petDirectUp(InputEvent &event)
Definition: pets.cpp:40
bool petMove(InputEvent &event)
Definition: pets.cpp:44
bool talkPet(InputEvent &event)
Definition: pets.cpp:32
BeingId getPetBeingId()
Definition: playerinfo.cpp:581
Net::PetHandler * petHandler
Definition: net.cpp:95
Net::ServerFeatures * serverFeatures
Definition: net.cpp:101
bool parse2Int(const std::string &args, int &x, int &y)
bool findCutFirst(std::string &str1, const std::string &str2)