Minetest  5.4.0
unit_sao.h
Go to the documentation of this file.
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 #pragma once
22 
23 #include "object_properties.h"
24 #include "serveractiveobject.h"
25 
27 {
28 public:
29  UnitSAO(ServerEnvironment *env, v3f pos);
30  virtual ~UnitSAO() = default;
31 
32  u16 getHP() const { return m_hp; }
33  // Use a function, if isDead can be defined by other conditions
34  bool isDead() const { return m_hp == 0; }
35 
36  // Rotation
37  void setRotation(v3f rotation) { m_rotation = rotation; }
38  const v3f &getRotation() const { return m_rotation; }
39  v3f getRadRotation() { return m_rotation * core::DEGTORAD; }
40 
41  // Deprecated
42  f32 getRadYawDep() const { return (m_rotation.Y + 90.) * core::DEGTORAD; }
43 
44  // Armor groups
45  inline bool isImmortal() const
46  {
47  return itemgroup_get(getArmorGroups(), "immortal");
48  }
49  void setArmorGroups(const ItemGroupList &armor_groups);
50  const ItemGroupList &getArmorGroups() const;
51 
52  // Animation
53  void setAnimation(v2f frame_range, float frame_speed, float frame_blend,
54  bool frame_loop);
55  void getAnimation(v2f *frame_range, float *frame_speed, float *frame_blend,
56  bool *frame_loop);
57  void setAnimationSpeed(float frame_speed);
58 
59  // Bone position
60  void setBonePosition(const std::string &bone, v3f position, v3f rotation);
61  void getBonePosition(const std::string &bone, v3f *position, v3f *rotation);
62 
63  // Attachments
65  inline bool isAttached() const { return getParent(); }
66  void setAttachment(int parent_id, const std::string &bone, v3f position,
67  v3f rotation, bool force_visible);
68  void getAttachment(int *parent_id, std::string *bone, v3f *position,
69  v3f *rotation, bool *force_visible) const;
70  void clearChildAttachments();
71  void clearParentAttachment();
72  void addAttachmentChild(int child_id);
73  void removeAttachmentChild(int child_id);
74  const std::unordered_set<int> &getAttachmentChildIds() const;
75 
76  // Object properties
79  void sendOutdatedData();
80 
81  // Update packets
82  std::string generateUpdateAttachmentCommand() const;
83  std::string generateUpdateAnimationSpeedCommand() const;
84  std::string generateUpdateAnimationCommand() const;
85  std::string generateUpdateArmorGroupsCommand() const;
86  static std::string generateUpdatePositionCommand(const v3f &position,
87  const v3f &velocity, const v3f &acceleration, const v3f &rotation,
88  bool do_interpolate, bool is_movement_end, f32 update_interval);
89  std::string generateSetPropertiesCommand(const ObjectProperties &prop) const;
90  static std::string generateUpdateBonePositionCommand(const std::string &bone,
91  const v3f &position, const v3f &rotation);
92  void sendPunchCommand();
93 
94 protected:
95  u16 m_hp = 1;
96 
98 
100 
101  // Object properties
102  bool m_properties_sent = true;
104 
105  // Stores position and rotation for each bone name
106  std::unordered_map<std::string, core::vector2d<v3f>> m_bone_position;
107 
109 
110 private:
111  void onAttach(int parent_id);
112  void onDetach(int parent_id);
113 
114  std::string generatePunchCommand(u16 result_hp) const;
115 
116  // Armor groups
117  bool m_armor_groups_sent = false;
118 
119  // Animation
121  float m_animation_speed = 0.0f;
122  float m_animation_blend = 0.0f;
123  bool m_animation_loop = true;
124  bool m_animation_sent = false;
126 
127  // Bone positions
128  bool m_bone_position_sent = false;
129 
130  // Attachments
131  std::unordered_set<int> m_attachment_child_ids;
132  std::string m_attachment_bone = "";
135  bool m_attachment_sent = false;
136  bool m_force_visible = false;
137 };
Definition: serveractiveobject.h:52
Definition: serverenvironment.h:194
Definition: unit_sao.h:27
void sendPunchCommand()
Definition: unit_sao.cpp:345
bool isDead() const
Definition: unit_sao.h:34
std::string generateUpdateAnimationSpeedCommand() const
Definition: unit_sao.cpp:267
std::string generateSetPropertiesCommand(const ObjectProperties &prop) const
Definition: unit_sao.cpp:327
ItemGroupList m_armor_groups
Definition: unit_sao.h:99
std::unordered_set< int > m_attachment_child_ids
Definition: unit_sao.h:131
ObjectProperties * accessObjectProperties()
Definition: unit_sao.cpp:230
UnitSAO(ServerEnvironment *env, v3f pos)
Definition: unit_sao.cpp:25
void setArmorGroups(const ItemGroupList &armor_groups)
Definition: unit_sao.cpp:41
std::string generateUpdateAnimationCommand() const
Definition: unit_sao.cpp:277
bool m_properties_sent
Definition: unit_sao.h:102
void clearChildAttachments()
Definition: unit_sao.cpp:159
void clearParentAttachment()
Definition: unit_sao.cpp:169
void removeAttachmentChild(int child_id)
Definition: unit_sao.cpp:188
ObjectProperties m_prop
Definition: unit_sao.h:103
v3f m_attachment_rotation
Definition: unit_sao.h:134
bool m_bone_position_sent
Definition: unit_sao.h:128
bool isAttached() const
Definition: unit_sao.h:65
f32 getRadYawDep() const
Definition: unit_sao.h:42
int m_attachment_parent_id
Definition: unit_sao.h:108
static std::string generateUpdatePositionCommand(const v3f &position, const v3f &velocity, const v3f &acceleration, const v3f &rotation, bool do_interpolate, bool is_movement_end, f32 update_interval)
Definition: unit_sao.cpp:303
void getBonePosition(const std::string &bone, v3f *position, v3f *rotation)
Definition: unit_sao.cpp:85
void notifyObjectPropertiesModified()
Definition: unit_sao.cpp:235
u16 m_hp
Definition: unit_sao.h:95
void setAnimation(v2f frame_range, float frame_speed, float frame_blend, bool frame_loop)
Definition: unit_sao.cpp:52
std::string m_attachment_bone
Definition: unit_sao.h:132
float m_animation_blend
Definition: unit_sao.h:122
u16 getHP() const
Definition: unit_sao.h:32
v2f m_animation_range
Definition: unit_sao.h:120
v3f m_attachment_position
Definition: unit_sao.h:133
void setRotation(v3f rotation)
Definition: unit_sao.h:37
bool m_force_visible
Definition: unit_sao.h:136
virtual ~UnitSAO()=default
void sendOutdatedData()
Definition: unit_sao.cpp:92
bool m_animation_speed_sent
Definition: unit_sao.h:125
const v3f & getRotation() const
Definition: unit_sao.h:38
v3f getRadRotation()
Definition: unit_sao.h:39
std::string generateUpdateArmorGroupsCommand() const
Definition: unit_sao.cpp:291
float m_animation_speed
Definition: unit_sao.h:121
static std::string generateUpdateBonePositionCommand(const std::string &bone, const v3f &position, const v3f &rotation)
Definition: unit_sao.cpp:254
bool m_armor_groups_sent
Definition: unit_sao.h:117
const ItemGroupList & getArmorGroups() const
Definition: unit_sao.cpp:47
void getAnimation(v2f *frame_range, float *frame_speed, float *frame_blend, bool *frame_loop)
Definition: unit_sao.cpp:63
ServerActiveObject * getParent() const
Definition: unit_sao.cpp:31
bool m_attachment_sent
Definition: unit_sao.h:135
const std::unordered_set< int > & getAttachmentChildIds() const
Definition: unit_sao.cpp:193
void setAnimationSpeed(float frame_speed)
Definition: unit_sao.cpp:72
bool isImmortal() const
Definition: unit_sao.h:45
void onDetach(int parent_id)
Definition: unit_sao.cpp:214
std::unordered_map< std::string, core::vector2d< v3f > > m_bone_position
Definition: unit_sao.h:106
void addAttachmentChild(int child_id)
Definition: unit_sao.cpp:183
void setAttachment(int parent_id, const std::string &bone, v3f position, v3f rotation, bool force_visible)
Definition: unit_sao.cpp:124
void setBonePosition(const std::string &bone, v3f position, v3f rotation)
Definition: unit_sao.cpp:78
std::string generateUpdateAttachmentCommand() const
Definition: unit_sao.cpp:240
void onAttach(int parent_id)
Definition: unit_sao.cpp:198
bool m_animation_sent
Definition: unit_sao.h:124
std::string generatePunchCommand(u16 result_hp) const
Definition: unit_sao.cpp:335
void getAttachment(int *parent_id, std::string *bone, v3f *position, v3f *rotation, bool *force_visible) const
Definition: unit_sao.cpp:149
v3f m_rotation
Definition: unit_sao.h:97
bool m_animation_loop
Definition: unit_sao.h:123
core::vector2d< f32 > v2f
Definition: irr_v2d.h:26
core::vector3df v3f
Definition: irr_v3d.h:26
std::unordered_map< std::string, int > ItemGroupList
Definition: itemgroup.h:25
static int itemgroup_get(const ItemGroupList &groups, const std::string &name)
Definition: itemgroup.h:27
Definition: object_properties.h:30