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 
34 
36 
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 {
55  if (id == BeingId_zero)
56  return nullptr;
57  return actorManager->findBeing(id);
58 }
59 
61 {
62  petHandler->emote(CAST_U8(atoi(event.args.c_str())));
63  return true;
64 }
65 
67 {
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 
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(),
90  return false;
91  }
92 
93  petHandler->setName(args);
94  return true;
95 }
96 
98 {
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)
108  if (Game::instance() != nullptr)
110  return true;
111  }
112 
113  return false;
114 }
115 
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,
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 
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 
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 
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 
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 
183 {
185  return true;
186 }
187 
189 {
191  return true;
192 }
193 
195 {
197  return true;
198 }
199 
201 {
203  return true;
204 }
205 
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 
220 {
221  if (petHandler != nullptr)
222  petHandler->feed();
223  return true;
224 }
225 
227 {
228  if (petHandler != nullptr)
229  petHandler->dropLoot();
230  return true;
231 }
232 
234 {
235  if (petHandler != nullptr)
237  return true;
238 }
239 
241 {
242  if (petHandler != nullptr)
243  petHandler->unequip();
244  return true;
245 }
246 
247 } // 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:1272
#define fromInt(val, name)
Definition: intdefines.h:45
Net::PetHandler * petHandler
Definition: net.cpp:92
static const Being * getPet()
Definition: pets.cpp:52
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:98
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
virtual void talkPet(const std::string &text) const =0
LocalPlayer * localPlayer
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
std::string textToMe(const std::string &str)
Definition: chatutils.cpp:202
EmoteShortcut * emoteShortcut
bool petMoveDown(InputEvent &event)
Definition: pets.cpp:36
Net::ChatHandler * chatHandler
Definition: net.cpp:83
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:578
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