ManaPlus
homunculushandler.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2011-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 
23 
24 #include "being/playerinfo.h"
25 
26 #include "net/serverfeatures.h"
27 
28 #include "net/eathena/messageout.h"
30 
31 #include "debug.h"
32 
33 extern int packetVersion;
34 
35 namespace EAthena
36 {
37 
39 {
40  homunculusHandler = this;
41 }
42 
44 {
45  homunculusHandler = nullptr;
46 }
47 
48 void HomunculusHandler::setName(const std::string &name) const
49 {
50  createOutPacket(CMSG_HOMUNCULUS_SET_NAME);
51  outMsg.writeString(name, 24, "name");
52 }
53 
55 {
57  if (id == BeingId_zero)
58  return;
59  createOutPacket(CMSG_HOMMERC_MOVE_TO_MASTER);
60  outMsg.writeBeingId(id, "homunculus id");
61 }
62 
63 void HomunculusHandler::move(const int x, const int y) const
64 {
66  if (id == BeingId_zero)
67  return;
68  createOutPacket(CMSG_HOMMERC_MOVE_TO);
69  outMsg.writeBeingId(id, "homunculus id");
70  outMsg.writeCoordinates(CAST_U16(x),
71  CAST_U16(y),
72  1U, "position");
73 }
74 
75 void HomunculusHandler::attack(const BeingId targetId,
76  const Keep keep) const
77 {
79  if (id == BeingId_zero)
80  return;
81  createOutPacket(CMSG_HOMMERC_ATTACK);
82  outMsg.writeBeingId(id, "homunculus id");
83  outMsg.writeBeingId(targetId, "target id");
84  outMsg.writeInt8(CAST_S8(keep == Keep_true ? 1 : 0), "keep");
85 }
86 
88 {
89  if (packetVersion < 20050425)
90  return;
91  createOutPacket(CMSG_HOMUNCULUS_MENU);
92  outMsg.writeInt16(0, "type");
93  outMsg.writeInt8(1, "command"); // feed
94 }
95 
97 {
98  if (packetVersion < 20050425)
99  return;
100  createOutPacket(CMSG_HOMUNCULUS_MENU);
101  outMsg.writeInt16(0, "type");
102  outMsg.writeInt8(2, "command"); // delete
103 }
104 
105 void HomunculusHandler::talk(const std::string &restrict text) const
106 {
107  if (!serverFeatures->haveMovePet())
108  return;
109  if (text.empty())
110  return;
111  std::string msg = text;
112  if (msg.size() > 500)
113  msg = msg.substr(0, 500);
114  const size_t sz = msg.size();
115 
116  createOutPacket(CMSG_HOMMERC_TALK);
117  outMsg.writeInt16(CAST_S16(sz + 4 + 1), "len");
118  outMsg.writeString(msg, CAST_S32(sz), "message");
119  outMsg.writeInt8(0, "zero byte");
120 }
121 
122 void HomunculusHandler::emote(const uint8_t emoteId) const
123 {
124  if (!serverFeatures->haveMovePet())
125  return;
126  createOutPacket(CMSG_HOMMERC_EMOTE);
127  outMsg.writeInt8(emoteId, "emote id");
128 }
129 
130 void HomunculusHandler::setDirection(const unsigned char type) const
131 {
132  if (!serverFeatures->haveMovePet())
133  return;
134  createOutPacket(CMSG_HOMMERC_DIRECTION);
135  outMsg.writeInt32(0, "pet id");
136  outMsg.writeInt8(0, "head direction");
137  outMsg.writeInt8(0, "unused");
138  outMsg.writeInt8(MessageOut::toServerDirection(type),
139  "pet direction");
140 }
141 
142 } // namespace EAthena
int BeingId
Definition: beingid.h:30
const BeingId BeingId_zero
Definition: beingid.h:30
#define CAST_U16
Definition: cast.h:29
#define CAST_S8
Definition: cast.h:26
#define CAST_S16
Definition: cast.h:28
#define CAST_S32
Definition: cast.h:30
void setName(const std::string &name) const
void talk(const std::string &text) const
void setDirection(const unsigned char type) const
void emote(const uint8_t emoteId) const
void attack(const BeingId targetId, const Keep keep) const
void move(const int x, const int y) const
static unsigned char toServerDirection(unsigned char direction) A_CONST
Definition: messageout.cpp:253
virtual bool haveMovePet() const =0
int packetVersion
Definition: client.cpp:125
Net::HomunculusHandler * homunculusHandler
Definition: net.cpp:112
const bool Keep_true
Definition: keep.h:30
bool Keep
Definition: keep.h:30
#define restrict
Definition: localconsts.h:165
#define createOutPacket(name)
Definition: messageout.h:37
bool msg(InputEvent &event)
Definition: chat.cpp:39
BeingId getHomunculusId()
Definition: playerinfo.cpp:608
Net::ServerFeatures * serverFeatures
Definition: net.cpp:101