3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2013-2020 Minetest core developers & community
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.
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.
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.
22 #include "scripting_server.h"
23 #include "serverenvironment.h"
25 UnitSAO::UnitSAO(ServerEnvironment *env, v3f pos) : ServerActiveObject(env, pos)
27 // Initialize something to armor groups
28 m_armor_groups["fleshy"] = 100;
31 ServerActiveObject *UnitSAO::getParent() const
33 if (!m_attachment_parent_id)
35 // Check if the parent still exists
36 ServerActiveObject *obj = m_env->getActiveObject(m_attachment_parent_id);
41 void UnitSAO::setArmorGroups(const ItemGroupList &armor_groups)
43 m_armor_groups = armor_groups;
44 m_armor_groups_sent = false;
47 const ItemGroupList &UnitSAO::getArmorGroups() const
49 return m_armor_groups;
52 void UnitSAO::setAnimation(
53 v2f frame_range, float frame_speed, float frame_blend, bool frame_loop)
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;
63 void UnitSAO::getAnimation(v2f *frame_range, float *frame_speed, float *frame_blend,
66 *frame_range = m_animation_range;
67 *frame_speed = m_animation_speed;
68 *frame_blend = m_animation_blend;
69 *frame_loop = m_animation_loop;
72 void UnitSAO::setAnimationSpeed(float frame_speed)
74 m_animation_speed = frame_speed;
75 m_animation_speed_sent = false;
78 void UnitSAO::setBonePosition(const std::string &bone, v3f position, v3f rotation)
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;
85 void UnitSAO::getBonePosition(const std::string &bone, v3f *position, v3f *rotation)
87 *position = m_bone_position[bone].X;
88 *rotation = m_bone_position[bone].Y;
92 void UnitSAO::sendOutdatedData()
94 if (!m_armor_groups_sent) {
95 m_armor_groups_sent = true;
96 m_messages_out.emplace(getId(), true, generateUpdateArmorGroupsCommand());
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());
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));
117 if (!m_attachment_sent) {
118 m_attachment_sent = true;
119 m_messages_out.emplace(getId(), true, generateUpdateAttachmentCommand());
124 void UnitSAO::setAttachment(int parent_id, const std::string &bone, v3f position,
125 v3f rotation, bool force_visible)
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.
135 int old_parent = m_attachment_parent_id;
136 m_attachment_parent_id = parent_id;
138 // The detach callbacks might call to setAttachment() again.
139 // Ensure the attachment params are applied after this callback is run.
140 if (parent_id != old_parent)
141 onDetach(old_parent);
143 m_attachment_parent_id = parent_id;
144 m_attachment_bone = bone;
145 m_attachment_position = position;
146 m_attachment_rotation = rotation;
147 m_force_visible = force_visible;
148 m_attachment_sent = false;
150 if (parent_id != old_parent)
154 void UnitSAO::getAttachment(int *parent_id, std::string *bone, v3f *position,
155 v3f *rotation, bool *force_visible) const
157 *parent_id = m_attachment_parent_id;
158 *bone = m_attachment_bone;
159 *position = m_attachment_position;
160 *rotation = m_attachment_rotation;
161 *force_visible = m_force_visible;
164 void UnitSAO::clearChildAttachments()
166 for (int child_id : m_attachment_child_ids) {
167 // Child can be NULL if it was deleted earlier
168 if (ServerActiveObject *child = m_env->getActiveObject(child_id))
169 child->setAttachment(0, "", v3f(0, 0, 0), v3f(0, 0, 0), false);
171 m_attachment_child_ids.clear();
174 void UnitSAO::clearParentAttachment()
176 ServerActiveObject *parent = nullptr;
177 if (m_attachment_parent_id) {
178 parent = m_env->getActiveObject(m_attachment_parent_id);
179 setAttachment(0, "", m_attachment_position, m_attachment_rotation, false);
181 setAttachment(0, "", v3f(0, 0, 0), v3f(0, 0, 0), false);
185 parent->removeAttachmentChild(m_id);
188 void UnitSAO::addAttachmentChild(int child_id)
190 m_attachment_child_ids.insert(child_id);
193 void UnitSAO::removeAttachmentChild(int child_id)
195 m_attachment_child_ids.erase(child_id);
198 const std::unordered_set<int> &UnitSAO::getAttachmentChildIds() const
200 return m_attachment_child_ids;
203 void UnitSAO::onAttach(int parent_id)
208 ServerActiveObject *parent = m_env->getActiveObject(parent_id);
210 if (!parent || parent->isGone())
211 return; // Do not try to notify soon gone parent
213 if (parent->getType() == ACTIVEOBJECT_TYPE_LUAENTITY) {
214 // Call parent's on_attach field
215 m_env->getScriptIface()->luaentity_on_attach_child(parent_id, this);
219 void UnitSAO::onDetach(int parent_id)
224 ServerActiveObject *parent = m_env->getActiveObject(parent_id);
225 if (getType() == ACTIVEOBJECT_TYPE_LUAENTITY)
226 m_env->getScriptIface()->luaentity_on_detach(m_id, parent);
228 if (!parent || parent->isGone())
229 return; // Do not try to notify soon gone parent
231 if (parent->getType() == ACTIVEOBJECT_TYPE_LUAENTITY)
232 m_env->getScriptIface()->luaentity_on_detach_child(parent_id, this);
235 ObjectProperties *UnitSAO::accessObjectProperties()
240 void UnitSAO::notifyObjectPropertiesModified()
242 m_properties_sent = false;
245 std::string UnitSAO::generateUpdateAttachmentCommand() const
247 std::ostringstream os(std::ios::binary);
249 writeU8(os, AO_CMD_ATTACH_TO);
251 writeS16(os, m_attachment_parent_id);
252 os << serializeString16(m_attachment_bone);
253 writeV3F32(os, m_attachment_position);
254 writeV3F32(os, m_attachment_rotation);
255 writeU8(os, m_force_visible);
259 std::string UnitSAO::generateUpdateBonePositionCommand(
260 const std::string &bone, const v3f &position, const v3f &rotation)
262 std::ostringstream os(std::ios::binary);
264 writeU8(os, AO_CMD_SET_BONE_POSITION);
266 os << serializeString16(bone);
267 writeV3F32(os, position);
268 writeV3F32(os, rotation);
272 std::string UnitSAO::generateUpdateAnimationSpeedCommand() const
274 std::ostringstream os(std::ios::binary);
276 writeU8(os, AO_CMD_SET_ANIMATION_SPEED);
278 writeF32(os, m_animation_speed);
282 std::string UnitSAO::generateUpdateAnimationCommand() const
284 std::ostringstream os(std::ios::binary);
286 writeU8(os, AO_CMD_SET_ANIMATION);
288 writeV2F32(os, m_animation_range);
289 writeF32(os, m_animation_speed);
290 writeF32(os, m_animation_blend);
291 // these are sent inverted so we get true when the server sends nothing
292 writeU8(os, !m_animation_loop);
296 std::string UnitSAO::generateUpdateArmorGroupsCommand() const
298 std::ostringstream os(std::ios::binary);
299 writeU8(os, AO_CMD_UPDATE_ARMOR_GROUPS);
300 writeU16(os, m_armor_groups.size());
301 for (const auto &armor_group : m_armor_groups) {
302 os << serializeString16(armor_group.first);
303 writeS16(os, armor_group.second);
308 std::string UnitSAO::generateUpdatePositionCommand(const v3f &position,
309 const v3f &velocity, const v3f &acceleration, const v3f &rotation,
310 bool do_interpolate, bool is_movement_end, f32 update_interval)
312 std::ostringstream os(std::ios::binary);
314 writeU8(os, AO_CMD_UPDATE_POSITION);
316 writeV3F32(os, position);
318 writeV3F32(os, velocity);
320 writeV3F32(os, acceleration);
322 writeV3F32(os, rotation);
324 writeU8(os, do_interpolate);
325 // is_end_position (for interpolation)
326 writeU8(os, is_movement_end);
327 // update_interval (for interpolation)
328 writeF32(os, update_interval);
332 std::string UnitSAO::generateSetPropertiesCommand(const ObjectProperties &prop) const
334 std::ostringstream os(std::ios::binary);
335 writeU8(os, AO_CMD_SET_PROPERTIES);
340 std::string UnitSAO::generatePunchCommand(u16 result_hp) const
342 std::ostringstream os(std::ios::binary);
344 writeU8(os, AO_CMD_PUNCHED);
346 writeU16(os, result_hp);
350 void UnitSAO::sendPunchCommand()
352 m_messages_out.emplace(getId(), true, generatePunchCommand(getHP()));