]> git.lizzy.rs Git - minetest.git/blob - src/server/unit_sao.cpp
ef0e87f2cec1ecd0e49534f18fe4f6214fcc5385
[minetest.git] / src / server / unit_sao.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2013-2020 Minetest core developers & community
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "unit_sao.h"
22 #include "scripting_server.h"
23 #include "serverenvironment.h"
24
25 UnitSAO::UnitSAO(ServerEnvironment *env, v3f pos) : ServerActiveObject(env, pos)
26 {
27         // Initialize something to armor groups
28         m_armor_groups["fleshy"] = 100;
29 }
30
31 ServerActiveObject *UnitSAO::getParent() const
32 {
33         if (!m_attachment_parent_id)
34                 return nullptr;
35         // Check if the parent still exists
36         ServerActiveObject *obj = m_env->getActiveObject(m_attachment_parent_id);
37
38         return obj;
39 }
40
41 void UnitSAO::setArmorGroups(const ItemGroupList &armor_groups)
42 {
43         m_armor_groups = armor_groups;
44         m_armor_groups_sent = false;
45 }
46
47 const ItemGroupList &UnitSAO::getArmorGroups() const
48 {
49         return m_armor_groups;
50 }
51
52 void UnitSAO::setAnimation(
53                 v2f frame_range, float frame_speed, float frame_blend, bool frame_loop)
54 {
55         // store these so they can be updated to clients
56         m_animation_range = frame_range;
57         m_animation_speed = frame_speed;
58         m_animation_blend = frame_blend;
59         m_animation_loop = frame_loop;
60         m_animation_sent = false;
61 }
62
63 void UnitSAO::getAnimation(v2f *frame_range, float *frame_speed, float *frame_blend,
64                 bool *frame_loop)
65 {
66         *frame_range = m_animation_range;
67         *frame_speed = m_animation_speed;
68         *frame_blend = m_animation_blend;
69         *frame_loop = m_animation_loop;
70 }
71
72 void UnitSAO::setAnimationSpeed(float frame_speed)
73 {
74         m_animation_speed = frame_speed;
75         m_animation_speed_sent = false;
76 }
77
78 void UnitSAO::setBonePosition(const std::string &bone, v3f position, v3f rotation)
79 {
80         // store these so they can be updated to clients
81         m_bone_position[bone] = core::vector2d<v3f>(position, rotation);
82         m_bone_position_sent = false;
83 }
84
85 void UnitSAO::getBonePosition(const std::string &bone, v3f *position, v3f *rotation)
86 {
87         *position = m_bone_position[bone].X;
88         *rotation = m_bone_position[bone].Y;
89 }
90
91 // clang-format off
92 void UnitSAO::sendOutdatedData()
93 {
94         if (!m_armor_groups_sent) {
95                 m_armor_groups_sent = true;
96                 m_messages_out.emplace(getId(), true, generateUpdateArmorGroupsCommand());
97         }
98
99         if (!m_animation_sent) {
100                 m_animation_sent = true;
101                 m_animation_speed_sent = true;
102                 m_messages_out.emplace(getId(), true, generateUpdateAnimationCommand());
103         } else if (!m_animation_speed_sent) {
104                 // Animation speed is also sent when 'm_animation_sent == false'
105                 m_animation_speed_sent = true;
106                 m_messages_out.emplace(getId(), true, generateUpdateAnimationSpeedCommand());
107         }
108
109         if (!m_bone_position_sent) {
110                 m_bone_position_sent = true;
111                 for (const auto &bone_pos : m_bone_position) {
112                         m_messages_out.emplace(getId(), true, generateUpdateBonePositionCommand(
113                                 bone_pos.first, bone_pos.second.X, bone_pos.second.Y));
114                 }
115         }
116
117         if (!m_attachment_sent) {
118                 m_attachment_sent = true;
119                 m_messages_out.emplace(getId(), true, generateUpdateAttachmentCommand());
120         }
121 }
122 // clang-format on
123
124 void UnitSAO::setAttachment(
125                 int parent_id, const std::string &bone, v3f position, v3f rotation)
126 {
127         // Attachments need to be handled on both the server and client.
128         // If we just attach on the server, we can only copy the position of the parent.
129         // Attachments are still sent to clients at an interval so players might see them
130         // lagging, plus we can't read and attach to skeletal bones. If we just attach on
131         // the client, the server still sees the child at its original location. This
132         // breaks some things so we also give the server the most accurate representation
133         // even if players only see the client changes.
134
135         int old_parent = m_attachment_parent_id;
136         m_attachment_parent_id = parent_id;
137         m_attachment_bone = bone;
138         m_attachment_position = position;
139         m_attachment_rotation = rotation;
140         m_attachment_sent = false;
141
142         if (parent_id != old_parent) {
143                 onDetach(old_parent);
144                 onAttach(parent_id);
145         }
146 }
147
148 void UnitSAO::getAttachment(
149                 int *parent_id, std::string *bone, v3f *position, v3f *rotation) const
150 {
151         *parent_id = m_attachment_parent_id;
152         *bone = m_attachment_bone;
153         *position = m_attachment_position;
154         *rotation = m_attachment_rotation;
155 }
156
157 void UnitSAO::clearChildAttachments()
158 {
159         for (int child_id : m_attachment_child_ids) {
160                 // Child can be NULL if it was deleted earlier
161                 if (ServerActiveObject *child = m_env->getActiveObject(child_id))
162                         child->setAttachment(0, "", v3f(0, 0, 0), v3f(0, 0, 0));
163         }
164         m_attachment_child_ids.clear();
165 }
166
167 void UnitSAO::clearParentAttachment()
168 {
169         ServerActiveObject *parent = nullptr;
170         if (m_attachment_parent_id) {
171                 parent = m_env->getActiveObject(m_attachment_parent_id);
172                 setAttachment(0, "", m_attachment_position, m_attachment_rotation);
173         } else {
174                 setAttachment(0, "", v3f(0, 0, 0), v3f(0, 0, 0));
175         }
176         // Do it
177         if (parent)
178                 parent->removeAttachmentChild(m_id);
179 }
180
181 void UnitSAO::addAttachmentChild(int child_id)
182 {
183         m_attachment_child_ids.insert(child_id);
184 }
185
186 void UnitSAO::removeAttachmentChild(int child_id)
187 {
188         m_attachment_child_ids.erase(child_id);
189 }
190
191 const std::unordered_set<int> &UnitSAO::getAttachmentChildIds() const
192 {
193         return m_attachment_child_ids;
194 }
195
196 void UnitSAO::onAttach(int parent_id)
197 {
198         if (!parent_id)
199                 return;
200
201         ServerActiveObject *parent = m_env->getActiveObject(parent_id);
202
203         if (!parent || parent->isGone())
204                 return; // Do not try to notify soon gone parent
205
206         if (parent->getType() == ACTIVEOBJECT_TYPE_LUAENTITY) {
207                 // Call parent's on_attach field
208                 m_env->getScriptIface()->luaentity_on_attach_child(parent_id, this);
209         }
210 }
211
212 void UnitSAO::onDetach(int parent_id)
213 {
214         if (!parent_id)
215                 return;
216
217         ServerActiveObject *parent = m_env->getActiveObject(parent_id);
218         if (getType() == ACTIVEOBJECT_TYPE_LUAENTITY)
219                 m_env->getScriptIface()->luaentity_on_detach(m_id, parent);
220
221         if (!parent || parent->isGone())
222                 return; // Do not try to notify soon gone parent
223
224         if (parent->getType() == ACTIVEOBJECT_TYPE_LUAENTITY)
225                 m_env->getScriptIface()->luaentity_on_detach_child(parent_id, this);
226 }
227
228 ObjectProperties *UnitSAO::accessObjectProperties()
229 {
230         return &m_prop;
231 }
232
233 void UnitSAO::notifyObjectPropertiesModified()
234 {
235         m_properties_sent = false;
236 }
237
238 std::string UnitSAO::generateUpdateAttachmentCommand() const
239 {
240         std::ostringstream os(std::ios::binary);
241         // command
242         writeU8(os, AO_CMD_ATTACH_TO);
243         // parameters
244         writeS16(os, m_attachment_parent_id);
245         os << serializeString(m_attachment_bone);
246         writeV3F32(os, m_attachment_position);
247         writeV3F32(os, m_attachment_rotation);
248         return os.str();
249 }
250
251 std::string UnitSAO::generateUpdateBonePositionCommand(
252                 const std::string &bone, const v3f &position, const v3f &rotation)
253 {
254         std::ostringstream os(std::ios::binary);
255         // command
256         writeU8(os, AO_CMD_SET_BONE_POSITION);
257         // parameters
258         os << serializeString(bone);
259         writeV3F32(os, position);
260         writeV3F32(os, rotation);
261         return os.str();
262 }
263
264 std::string UnitSAO::generateUpdateAnimationSpeedCommand() const
265 {
266         std::ostringstream os(std::ios::binary);
267         // command
268         writeU8(os, AO_CMD_SET_ANIMATION_SPEED);
269         // parameters
270         writeF32(os, m_animation_speed);
271         return os.str();
272 }
273
274 std::string UnitSAO::generateUpdateAnimationCommand() const
275 {
276         std::ostringstream os(std::ios::binary);
277         // command
278         writeU8(os, AO_CMD_SET_ANIMATION);
279         // parameters
280         writeV2F32(os, m_animation_range);
281         writeF32(os, m_animation_speed);
282         writeF32(os, m_animation_blend);
283         // these are sent inverted so we get true when the server sends nothing
284         writeU8(os, !m_animation_loop);
285         return os.str();
286 }
287
288 std::string UnitSAO::generateUpdateArmorGroupsCommand() const
289 {
290         std::ostringstream os(std::ios::binary);
291         writeU8(os, AO_CMD_UPDATE_ARMOR_GROUPS);
292         writeU16(os, m_armor_groups.size());
293         for (const auto &armor_group : m_armor_groups) {
294                 os << serializeString(armor_group.first);
295                 writeS16(os, armor_group.second);
296         }
297         return os.str();
298 }
299
300 std::string UnitSAO::generateUpdatePositionCommand(const v3f &position,
301                 const v3f &velocity, const v3f &acceleration, const v3f &rotation,
302                 bool do_interpolate, bool is_movement_end, f32 update_interval)
303 {
304         std::ostringstream os(std::ios::binary);
305         // command
306         writeU8(os, AO_CMD_UPDATE_POSITION);
307         // pos
308         writeV3F32(os, position);
309         // velocity
310         writeV3F32(os, velocity);
311         // acceleration
312         writeV3F32(os, acceleration);
313         // rotation
314         writeV3F32(os, rotation);
315         // do_interpolate
316         writeU8(os, do_interpolate);
317         // is_end_position (for interpolation)
318         writeU8(os, is_movement_end);
319         // update_interval (for interpolation)
320         writeF32(os, update_interval);
321         return os.str();
322 }
323
324 std::string UnitSAO::generateSetPropertiesCommand(const ObjectProperties &prop) const
325 {
326         std::ostringstream os(std::ios::binary);
327         writeU8(os, AO_CMD_SET_PROPERTIES);
328         prop.serialize(os);
329         return os.str();
330 }
331
332 std::string UnitSAO::generatePunchCommand(u16 result_hp) const
333 {
334         std::ostringstream os(std::ios::binary);
335         // command
336         writeU8(os, AO_CMD_PUNCHED);
337         // result_hp
338         writeU16(os, result_hp);
339         return os.str();
340 }
341
342 void UnitSAO::sendPunchCommand()
343 {
344         m_messages_out.emplace(getId(), true, generatePunchCommand(getHP()));
345 }