2 // SPDX-License-Identifier: LGPL-2.1-or-later
3 // Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 // Copyright (C) 2013-2020 Minetest core developers & community
8 #include "object_properties.h"
9 #include "serveractiveobject.h"
10 #include <quaternion.h>
11 #include "util/numeric.h"
13 class UnitSAO
: public ServerActiveObject
16 UnitSAO(ServerEnvironment
*env
, v3f pos
);
17 virtual ~UnitSAO() = default;
19 u16
getHP() const override
{ return m_hp
; }
20 // Use a function, if isDead can be defined by other conditions
21 bool isDead() const { return m_hp
== 0; }
24 void setRotation(v3f rotation
) { m_rotation
= rotation
; }
25 const v3f
&getRotation() const { return m_rotation
; }
26 const v3f
getTotalRotation() const {
27 // This replicates what happens clientside serverside
29 setPitchYawRoll(rot
, -m_rotation
);
31 // First rotate by m_rotation, then rotate by the automatic rotate yaw
32 (core::quaternion(v3f(0, -m_rotation_add_yaw
* core::DEGTORAD
, 0))
33 * core::quaternion(rot
.getRotationDegrees() * core::DEGTORAD
))
35 return res
* core::RADTODEG
;
37 v3f
getRadRotation() { return m_rotation
* core::DEGTORAD
; }
40 f32
getRadYawDep() const { return (m_rotation
.Y
+ 90.) * core::DEGTORAD
; }
43 inline bool isImmortal() const
45 return itemgroup_get(getArmorGroups(), "immortal");
47 void setArmorGroups(const ItemGroupList
&armor_groups
) override
;
48 const ItemGroupList
&getArmorGroups() const override
;
51 void setAnimation(v2f frame_range
, float frame_speed
, float frame_blend
,
52 bool frame_loop
) override
;
53 void getAnimation(v2f
*frame_range
, float *frame_speed
, float *frame_blend
,
54 bool *frame_loop
) override
;
55 void setAnimationSpeed(float frame_speed
) override
;
58 void setBoneOverride(const std::string
&bone
, const BoneOverride
&props
) override
;
59 BoneOverride
getBoneOverride(const std::string
&bone
) override
;
60 const std::unordered_map
<std::string
, BoneOverride
>
61 &getBoneOverrides() const override
{ return m_bone_override
; };
64 ServerActiveObject
*getParent() const override
;
65 inline bool isAttached() const { return m_attachment_parent_id
!= 0; }
66 void setAttachment(object_t parent_id
, const std::string
&bone
, v3f position
,
67 v3f rotation
, bool force_visible
) override
;
68 void getAttachment(object_t
*parent_id
, std::string
*bone
, v3f
*position
,
69 v3f
*rotation
, bool *force_visible
) const override
;
70 void clearChildAttachments() override
;
71 void addAttachmentChild(object_t child_id
) override
;
72 void removeAttachmentChild(object_t child_id
) override
;
73 const std::unordered_set
<object_t
> &getAttachmentChildIds() const override
{
74 return m_attachment_child_ids
;
78 ObjectProperties
*accessObjectProperties() override
;
79 void notifyObjectPropertiesModified() override
;
80 void sendOutdatedData();
83 std::string
generateUpdateAttachmentCommand() const;
84 std::string
generateUpdateAnimationSpeedCommand() const;
85 std::string
generateUpdateAnimationCommand() const;
86 std::string
generateUpdateArmorGroupsCommand() const;
87 static std::string
generateUpdatePositionCommand(const v3f
&position
,
88 const v3f
&velocity
, const v3f
&acceleration
, const v3f
&rotation
,
89 bool do_interpolate
, bool is_movement_end
, f32 update_interval
);
90 std::string
generateSetPropertiesCommand(const ObjectProperties
&prop
) const;
91 static std::string
generateUpdateBoneOverrideCommand(
92 const std::string
&bone
, const BoneOverride
&props
);
93 void sendPunchCommand();
99 f32 m_rotation_add_yaw
= 0;
101 ItemGroupList m_armor_groups
;
104 bool m_properties_sent
= true;
105 ObjectProperties m_prop
;
107 // Stores position and rotation for each bone name
108 std::unordered_map
<std::string
, BoneOverride
> m_bone_override
;
110 object_t m_attachment_parent_id
= 0;
112 void clearAnyAttachments();
113 virtual void onMarkedForDeactivation() override
{
114 ServerActiveObject::onMarkedForDeactivation();
115 clearAnyAttachments();
117 virtual void onMarkedForRemoval() override
{
118 ServerActiveObject::onMarkedForRemoval();
119 clearAnyAttachments();
123 void onAttach(ServerActiveObject
*parent
);
124 void onDetach(ServerActiveObject
*parent
);
126 std::string
generatePunchCommand(u16 result_hp
) const;
128 // Used to detect nested calls to setAttachments(), which can happen due to
130 u8 m_attachment_call_counter
= 0;
133 bool m_armor_groups_sent
= false;
136 v2f m_animation_range
;
137 float m_animation_speed
= 0.0f
;
138 float m_animation_blend
= 0.0f
;
139 bool m_animation_loop
= true;
140 bool m_animation_sent
= false;
141 bool m_animation_speed_sent
= false;
144 bool m_bone_override_sent
= false;
147 std::unordered_set
<object_t
> m_attachment_child_ids
;
148 std::string m_attachment_bone
= "";
149 v3f m_attachment_position
;
150 v3f m_attachment_rotation
;
151 bool m_attachment_sent
= false;
152 bool m_force_visible
= false;