]> git.lizzy.rs Git - dragonfireclient.git/blob - unit_sao.cpp
acbdd478aa4ad0486bae29b407bf1c4022ebf73f
[dragonfireclient.git] / 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(int parent_id, const std::string &bone, v3f position,
125                 v3f rotation, bool force_visible)
126 {
127         auto *obj = parent_id ? m_env->getActiveObject(parent_id) : nullptr;
128         if (obj) {
129                 // Do checks to avoid circular references
130                 // The chain of wanted parent must not refer or contain "this"
131                 for (obj = obj->getParent(); obj; obj = obj->getParent()) {
132                         if (obj == this) {
133                                 warningstream << "Mod bug: Attempted to attach object " << m_id << " to parent "
134                                         << parent_id << " but former is an (in)direct parent of latter." << std::endl;
135                                 return;
136                         }
137                 }
138         }
139
140         // Attachments need to be handled on both the server and client.
141         // If we just attach on the server, we can only copy the position of the parent.
142         // Attachments are still sent to clients at an interval so players might see them
143         // lagging, plus we can't read and attach to skeletal bones. If we just attach on
144         // the client, the server still sees the child at its original location. This
145         // breaks some things so we also give the server the most accurate representation
146         // even if players only see the client changes.
147
148         int old_parent = m_attachment_parent_id;
149         m_attachment_parent_id = parent_id;
150
151         // The detach callbacks might call to setAttachment() again.
152         // Ensure the attachment params are applied after this callback is run.
153         if (parent_id != old_parent)
154                 onDetach(old_parent);
155
156         m_attachment_parent_id = parent_id;
157         m_attachment_bone = bone;
158         m_attachment_position = position;
159         m_attachment_rotation = rotation;
160         m_force_visible = force_visible;
161         m_attachment_sent = false;
162
163         if (parent_id != old_parent)
164                 onAttach(parent_id);
165 }
166
167 void UnitSAO::getAttachment(int *parent_id, std::string *bone, v3f *position,
168                 v3f *rotation, bool *force_visible) const
169 {
170         *parent_id = m_attachment_parent_id;
171         *bone = m_attachment_bone;
172         *position = m_attachment_position;
173         *rotation = m_attachment_rotation;
174         *force_visible = m_force_visible;
175 }
176
177 void UnitSAO::clearChildAttachments()
178 {
179         for (int child_id : m_attachment_child_ids) {
180                 // Child can be NULL if it was deleted earlier
181                 if (ServerActiveObject *child = m_env->getActiveObject(child_id))
182                         child->setAttachment(0, "", v3f(0, 0, 0), v3f(0, 0, 0), false);
183         }
184         m_attachment_child_ids.clear();
185 }
186
187 void UnitSAO::clearParentAttachment()
188 {
189         ServerActiveObject *parent = nullptr;
190         if (m_attachment_parent_id) {
191                 parent = m_env->getActiveObject(m_attachment_parent_id);
192                 setAttachment(0, "", m_attachment_position, m_attachment_rotation, false);
193         } else {
194                 setAttachment(0, "", v3f(0, 0, 0), v3f(0, 0, 0), false);
195         }
196         // Do it
197         if (parent)
198                 parent->removeAttachmentChild(m_id);
199 }
200
201 void UnitSAO::addAttachmentChild(int child_id)
202 {
203         m_attachment_child_ids.insert(child_id);
204 }
205
206 void UnitSAO::removeAttachmentChild(int child_id)
207 {
208         m_attachment_child_ids.erase(child_id);
209 }
210
211 const std::unordered_set<int> &UnitSAO::getAttachmentChildIds() const
212 {
213         return m_attachment_child_ids;
214 }
215
216 void UnitSAO::onAttach(int parent_id)
217 {
218         if (!parent_id)
219                 return;
220
221         ServerActiveObject *parent = m_env->getActiveObject(parent_id);
222
223         if (!parent || parent->isGone())
224                 return; // Do not try to notify soon gone parent
225
226         if (parent->getType() == ACTIVEOBJECT_TYPE_LUAENTITY) {
227                 // Call parent's on_attach field
228                 m_env->getScriptIface()->luaentity_on_attach_child(parent_id, this);
229         }
230 }
231
232 void UnitSAO::onDetach(int parent_id)
233 {
234         if (!parent_id)
235                 return;
236
237         ServerActiveObject *parent = m_env->getActiveObject(parent_id);
238         if (getType() == ACTIVEOBJECT_TYPE_LUAENTITY)
239                 m_env->getScriptIface()->luaentity_on_detach(m_id, parent);
240
241         if (!parent || parent->isGone())
242                 return; // Do not try to notify soon gone parent
243
244         if (parent->getType() == ACTIVEOBJECT_TYPE_LUAENTITY)
245                 m_env->getScriptIface()->luaentity_on_detach_child(parent_id, this);
246 }
247
248 ObjectProperties *UnitSAO::accessObjectProperties()
249 {
250         return &m_prop;
251 }
252
253 void UnitSAO::notifyObjectPropertiesModified()
254 {
255         m_properties_sent = false;
256 }
257
258 std::string UnitSAO::generateUpdateAttachmentCommand() const
259 {
260         std::ostringstream os(std::ios::binary);
261         // command
262         writeU8(os, AO_CMD_ATTACH_TO);
263         // parameters
264         writeS16(os, m_attachment_parent_id);
265         os << serializeString16(m_attachment_bone);
266         writeV3F32(os, m_attachment_position);
267         writeV3F32(os, m_attachment_rotation);
268         writeU8(os, m_force_visible);
269         return os.str();
270 }
271
272 std::string UnitSAO::generateUpdateBonePositionCommand(
273                 const std::string &bone, const v3f &position, const v3f &rotation)
274 {
275         std::ostringstream os(std::ios::binary);
276         // command
277         writeU8(os, AO_CMD_SET_BONE_POSITION);
278         // parameters
279         os << serializeString16(bone);
280         writeV3F32(os, position);
281         writeV3F32(os, rotation);
282         return os.str();
283 }
284
285 std::string UnitSAO::generateUpdateAnimationSpeedCommand() const
286 {
287         std::ostringstream os(std::ios::binary);
288         // command
289         writeU8(os, AO_CMD_SET_ANIMATION_SPEED);
290         // parameters
291         writeF32(os, m_animation_speed);
292         return os.str();
293 }
294
295 std::string UnitSAO::generateUpdateAnimationCommand() const
296 {
297         std::ostringstream os(std::ios::binary);
298         // command
299         writeU8(os, AO_CMD_SET_ANIMATION);
300         // parameters
301         writeV2F32(os, m_animation_range);
302         writeF32(os, m_animation_speed);
303         writeF32(os, m_animation_blend);
304         // these are sent inverted so we get true when the server sends nothing
305         writeU8(os, !m_animation_loop);
306         return os.str();
307 }
308
309 std::string UnitSAO::generateUpdateArmorGroupsCommand() const
310 {
311         std::ostringstream os(std::ios::binary);
312         writeU8(os, AO_CMD_UPDATE_ARMOR_GROUPS);
313         writeU16(os, m_armor_groups.size());
314         for (const auto &armor_group : m_armor_groups) {
315                 os << serializeString16(armor_group.first);
316                 writeS16(os, armor_group.second);
317         }
318         return os.str();
319 }
320
321 std::string UnitSAO::generateUpdatePositionCommand(const v3f &position,
322                 const v3f &velocity, const v3f &acceleration, const v3f &rotation,
323                 bool do_interpolate, bool is_movement_end, f32 update_interval)
324 {
325         std::ostringstream os(std::ios::binary);
326         // command
327         writeU8(os, AO_CMD_UPDATE_POSITION);
328         // pos
329         writeV3F32(os, position);
330         // velocity
331         writeV3F32(os, velocity);
332         // acceleration
333         writeV3F32(os, acceleration);
334         // rotation
335         writeV3F32(os, rotation);
336         // do_interpolate
337         writeU8(os, do_interpolate);
338         // is_end_position (for interpolation)
339         writeU8(os, is_movement_end);
340         // update_interval (for interpolation)
341         writeF32(os, update_interval);
342         return os.str();
343 }
344
345 std::string UnitSAO::generateSetPropertiesCommand(const ObjectProperties &prop) const
346 {
347         std::ostringstream os(std::ios::binary);
348         writeU8(os, AO_CMD_SET_PROPERTIES);
349         prop.serialize(os);
350         return os.str();
351 }
352
353 std::string UnitSAO::generatePunchCommand(u16 result_hp) const
354 {
355         std::ostringstream os(std::ios::binary);
356         // command
357         writeU8(os, AO_CMD_PUNCHED);
358         // result_hp
359         writeU16(os, result_hp);
360         return os.str();
361 }
362
363 void UnitSAO::sendPunchCommand()
364 {
365         m_messages_out.emplace(getId(), true, generatePunchCommand(getHP()));
366 }