Expose collision information to LuaEntity on_step
[oweals/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 void UnitSAO::setAttachment(
92                 int parent_id, const std::string &bone, v3f position, v3f rotation)
93 {
94         // Attachments need to be handled on both the server and client.
95         // If we just attach on the server, we can only copy the position of the parent.
96         // Attachments are still sent to clients at an interval so players might see them
97         // lagging, plus we can't read and attach to skeletal bones. If we just attach on
98         // the client, the server still sees the child at its original location. This
99         // breaks some things so we also give the server the most accurate representation
100         // even if players only see the client changes.
101
102         int old_parent = m_attachment_parent_id;
103         m_attachment_parent_id = parent_id;
104         m_attachment_bone = bone;
105         m_attachment_position = position;
106         m_attachment_rotation = rotation;
107         m_attachment_sent = false;
108
109         if (parent_id != old_parent) {
110                 onDetach(old_parent);
111                 onAttach(parent_id);
112         }
113 }
114
115 void UnitSAO::getAttachment(
116                 int *parent_id, std::string *bone, v3f *position, v3f *rotation) const
117 {
118         *parent_id = m_attachment_parent_id;
119         *bone = m_attachment_bone;
120         *position = m_attachment_position;
121         *rotation = m_attachment_rotation;
122 }
123
124 void UnitSAO::clearChildAttachments()
125 {
126         for (int child_id : m_attachment_child_ids) {
127                 // Child can be NULL if it was deleted earlier
128                 if (ServerActiveObject *child = m_env->getActiveObject(child_id))
129                         child->setAttachment(0, "", v3f(0, 0, 0), v3f(0, 0, 0));
130         }
131         m_attachment_child_ids.clear();
132 }
133
134 void UnitSAO::clearParentAttachment()
135 {
136         ServerActiveObject *parent = nullptr;
137         if (m_attachment_parent_id) {
138                 parent = m_env->getActiveObject(m_attachment_parent_id);
139                 setAttachment(0, "", m_attachment_position, m_attachment_rotation);
140         } else {
141                 setAttachment(0, "", v3f(0, 0, 0), v3f(0, 0, 0));
142         }
143         // Do it
144         if (parent)
145                 parent->removeAttachmentChild(m_id);
146 }
147
148 void UnitSAO::addAttachmentChild(int child_id)
149 {
150         m_attachment_child_ids.insert(child_id);
151 }
152
153 void UnitSAO::removeAttachmentChild(int child_id)
154 {
155         m_attachment_child_ids.erase(child_id);
156 }
157
158 const std::unordered_set<int> &UnitSAO::getAttachmentChildIds() const
159 {
160         return m_attachment_child_ids;
161 }
162
163 void UnitSAO::onAttach(int parent_id)
164 {
165         if (!parent_id)
166                 return;
167
168         ServerActiveObject *parent = m_env->getActiveObject(parent_id);
169
170         if (!parent || parent->isGone())
171                 return; // Do not try to notify soon gone parent
172
173         if (parent->getType() == ACTIVEOBJECT_TYPE_LUAENTITY) {
174                 // Call parent's on_attach field
175                 m_env->getScriptIface()->luaentity_on_attach_child(parent_id, this);
176         }
177 }
178
179 void UnitSAO::onDetach(int parent_id)
180 {
181         if (!parent_id)
182                 return;
183
184         ServerActiveObject *parent = m_env->getActiveObject(parent_id);
185         if (getType() == ACTIVEOBJECT_TYPE_LUAENTITY)
186                 m_env->getScriptIface()->luaentity_on_detach(m_id, parent);
187
188         if (!parent || parent->isGone())
189                 return; // Do not try to notify soon gone parent
190
191         if (parent->getType() == ACTIVEOBJECT_TYPE_LUAENTITY)
192                 m_env->getScriptIface()->luaentity_on_detach_child(parent_id, this);
193 }
194
195 ObjectProperties *UnitSAO::accessObjectProperties()
196 {
197         return &m_prop;
198 }
199
200 void UnitSAO::notifyObjectPropertiesModified()
201 {
202         m_properties_sent = false;
203 }
204
205 std::string UnitSAO::generateUpdateAttachmentCommand() const
206 {
207         std::ostringstream os(std::ios::binary);
208         // command
209         writeU8(os, AO_CMD_ATTACH_TO);
210         // parameters
211         writeS16(os, m_attachment_parent_id);
212         os << serializeString(m_attachment_bone);
213         writeV3F32(os, m_attachment_position);
214         writeV3F32(os, m_attachment_rotation);
215         return os.str();
216 }
217
218 std::string UnitSAO::generateUpdateBonePositionCommand(
219                 const std::string &bone, const v3f &position, const v3f &rotation)
220 {
221         std::ostringstream os(std::ios::binary);
222         // command
223         writeU8(os, AO_CMD_SET_BONE_POSITION);
224         // parameters
225         os << serializeString(bone);
226         writeV3F32(os, position);
227         writeV3F32(os, rotation);
228         return os.str();
229 }
230
231 std::string UnitSAO::generateUpdateAnimationSpeedCommand() const
232 {
233         std::ostringstream os(std::ios::binary);
234         // command
235         writeU8(os, AO_CMD_SET_ANIMATION_SPEED);
236         // parameters
237         writeF32(os, m_animation_speed);
238         return os.str();
239 }
240
241 std::string UnitSAO::generateUpdateAnimationCommand() const
242 {
243         std::ostringstream os(std::ios::binary);
244         // command
245         writeU8(os, AO_CMD_SET_ANIMATION);
246         // parameters
247         writeV2F32(os, m_animation_range);
248         writeF32(os, m_animation_speed);
249         writeF32(os, m_animation_blend);
250         // these are sent inverted so we get true when the server sends nothing
251         writeU8(os, !m_animation_loop);
252         return os.str();
253 }
254
255 std::string UnitSAO::generateUpdateArmorGroupsCommand() const
256 {
257         std::ostringstream os(std::ios::binary);
258         writeU8(os, AO_CMD_UPDATE_ARMOR_GROUPS);
259         writeU16(os, m_armor_groups.size());
260         for (const auto &armor_group : m_armor_groups) {
261                 os << serializeString(armor_group.first);
262                 writeS16(os, armor_group.second);
263         }
264         return os.str();
265 }
266
267 std::string UnitSAO::generateUpdatePositionCommand(const v3f &position,
268                 const v3f &velocity, const v3f &acceleration, const v3f &rotation,
269                 bool do_interpolate, bool is_movement_end, f32 update_interval)
270 {
271         std::ostringstream os(std::ios::binary);
272         // command
273         writeU8(os, AO_CMD_UPDATE_POSITION);
274         // pos
275         writeV3F32(os, position);
276         // velocity
277         writeV3F32(os, velocity);
278         // acceleration
279         writeV3F32(os, acceleration);
280         // rotation
281         writeV3F32(os, rotation);
282         // do_interpolate
283         writeU8(os, do_interpolate);
284         // is_end_position (for interpolation)
285         writeU8(os, is_movement_end);
286         // update_interval (for interpolation)
287         writeF32(os, update_interval);
288         return os.str();
289 }
290
291 std::string UnitSAO::generateSetPropertiesCommand(const ObjectProperties &prop) const
292 {
293         std::ostringstream os(std::ios::binary);
294         writeU8(os, AO_CMD_SET_PROPERTIES);
295         prop.serialize(os);
296         return os.str();
297 }
298
299 std::string UnitSAO::generatePunchCommand(u16 result_hp) const
300 {
301         std::ostringstream os(std::ios::binary);
302         // command
303         writeU8(os, AO_CMD_PUNCHED);
304         // result_hp
305         writeU16(os, result_hp);
306         return os.str();
307 }
308
309 void UnitSAO::sendPunchCommand()
310 {
311         m_messages_out.emplace(getId(), true, generatePunchCommand(getHP()));
312 }