Luanti 5.10.0-dev
 
Loading...
Searching...
No Matches
unit_sao.h
Go to the documentation of this file.
1// Luanti
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
5
6#pragma once
7
8#include "object_properties.h"
10#include <quaternion.h>
11#include "util/numeric.h"
12
14{
15public:
16 UnitSAO(ServerEnvironment *env, v3f pos);
17 virtual ~UnitSAO() = default;
18
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; }
22
23 // Rotation
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
28 core::matrix4 rot;
30 v3f res;
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))
34 .toEuler(res);
35 return res * core::RADTODEG;
36 }
37 v3f getRadRotation() { return m_rotation * core::DEGTORAD; }
38
39 // Deprecated
40 f32 getRadYawDep() const { return (m_rotation.Y + 90.) * core::DEGTORAD; }
41
42 // Armor groups
43 inline bool isImmortal() const
44 {
45 return itemgroup_get(getArmorGroups(), "immortal");
46 }
47 void setArmorGroups(const ItemGroupList &armor_groups) override;
48 const ItemGroupList &getArmorGroups() const override;
49
50 // Animation
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;
56
57 // Bone position
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; };
62
63 // Attachments
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 {
75 }
76
77 // Object properties
79 void notifyObjectPropertiesModified() override;
80 void sendOutdatedData();
81
82 // Update packets
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();
94
95protected:
96 u16 m_hp = 1;
97
100
102
103 // Object properties
104 bool m_properties_sent = true;
106
107 // Stores position and rotation for each bone name
108 std::unordered_map<std::string, BoneOverride> m_bone_override;
109
111
112 void clearAnyAttachments();
121
122private:
123 void onAttach(ServerActiveObject *parent);
124 void onDetach(ServerActiveObject *parent);
125
126 std::string generatePunchCommand(u16 result_hp) const;
127
128 // Used to detect nested calls to setAttachments(), which can happen due to
129 // Lua callbacks
131
132 // Armor groups
134
135 // Animation
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;
142
143 // Bone positions
145
146 // Attachments
147 std::unordered_set<object_t> m_attachment_child_ids;
148 std::string m_attachment_bone = "";
151 bool m_attachment_sent = false;
152 bool m_force_visible = false;
153};
u16 object_t
Definition activeobject.h:143
Definition serveractiveobject.h:41
virtual void onMarkedForDeactivation()
Definition serveractiveobject.h:244
virtual void onMarkedForRemoval()
Definition serveractiveobject.h:245
Definition serverenvironment.h:219
Definition unit_sao.h:14
void setArmorGroups(const ItemGroupList &armor_groups) override
Definition unit_sao.cpp:27
static std::string generateUpdateBoneOverrideCommand(const std::string &bone, const BoneOverride &props)
Definition unit_sao.cpp:300
bool m_bone_override_sent
Definition unit_sao.h:144
void sendPunchCommand()
Definition unit_sao.cpp:400
void setAnimation(v2f frame_range, float frame_speed, float frame_blend, bool frame_loop) override
Definition unit_sao.cpp:40
void clearChildAttachments() override
Definition unit_sao.cpp:221
f32 m_rotation_add_yaw
Definition unit_sao.h:99
u16 getHP() const override
Definition unit_sao.h:19
bool isDead() const
Definition unit_sao.h:21
u8 m_attachment_call_counter
Definition unit_sao.h:130
std::string generateUpdateAnimationSpeedCommand() const
Definition unit_sao.cpp:322
std::string generateSetPropertiesCommand(const ObjectProperties &prop) const
Definition unit_sao.cpp:382
ItemGroupList m_armor_groups
Definition unit_sao.h:101
std::unordered_map< std::string, BoneOverride > m_bone_override
Definition unit_sao.h:108
UnitSAO(ServerEnvironment *env, v3f pos)
Definition unit_sao.cpp:11
virtual void onMarkedForRemoval() override
Definition unit_sao.h:117
void notifyObjectPropertiesModified() override
Definition unit_sao.cpp:281
std::string generateUpdateAnimationCommand() const
Definition unit_sao.cpp:332
const v3f & getRotation() const
Definition unit_sao.h:25
bool m_properties_sent
Definition unit_sao.h:104
const ItemGroupList & getArmorGroups() const override
Definition unit_sao.cpp:35
std::unordered_set< object_t > m_attachment_child_ids
Definition unit_sao.h:147
ObjectProperties m_prop
Definition unit_sao.h:105
v3f m_attachment_rotation
Definition unit_sao.h:150
bool isAttached() const
Definition unit_sao.h:65
f32 getRadYawDep() const
Definition unit_sao.h:40
void onAttach(ServerActiveObject *parent)
Definition unit_sao.cpp:246
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:358
ObjectProperties * accessObjectProperties() override
Definition unit_sao.cpp:276
u16 m_hp
Definition unit_sao.h:96
void setBoneOverride(const std::string &bone, const BoneOverride &props) override
Definition unit_sao.cpp:68
std::string m_attachment_bone
Definition unit_sao.h:148
const std::unordered_map< std::string, BoneOverride > & getBoneOverrides() const override
Definition unit_sao.h:61
float m_animation_blend
Definition unit_sao.h:138
BoneOverride getBoneOverride(const std::string &bone) override
Definition unit_sao.cpp:75
v2f m_animation_range
Definition unit_sao.h:136
v3f m_attachment_position
Definition unit_sao.h:149
void setRotation(v3f rotation)
Definition unit_sao.h:24
void getAttachment(object_t *parent_id, std::string *bone, v3f *position, v3f *rotation, bool *force_visible) const override
Definition unit_sao.cpp:200
ServerActiveObject * getParent() const override
Definition unit_sao.cpp:17
bool m_force_visible
Definition unit_sao.h:152
virtual ~UnitSAO()=default
void removeAttachmentChild(object_t child_id) override
Definition unit_sao.cpp:241
void sendOutdatedData()
Definition unit_sao.cpp:84
bool m_animation_speed_sent
Definition unit_sao.h:141
void setAnimationSpeed(float frame_speed) override
Definition unit_sao.cpp:60
v3f getRadRotation()
Definition unit_sao.h:37
std::string generateUpdateArmorGroupsCommand() const
Definition unit_sao.cpp:346
float m_animation_speed
Definition unit_sao.h:137
bool m_armor_groups_sent
Definition unit_sao.h:133
void setAttachment(object_t parent_id, const std::string &bone, v3f position, v3f rotation, bool force_visible) override
Definition unit_sao.cpp:115
bool m_attachment_sent
Definition unit_sao.h:151
const v3f getTotalRotation() const
Definition unit_sao.h:26
bool isImmortal() const
Definition unit_sao.h:43
std::string generateUpdateAttachmentCommand() const
Definition unit_sao.cpp:286
bool m_animation_sent
Definition unit_sao.h:140
std::string generatePunchCommand(u16 result_hp) const
Definition unit_sao.cpp:390
virtual void onMarkedForDeactivation() override
Definition unit_sao.h:113
object_t m_attachment_parent_id
Definition unit_sao.h:110
void clearAnyAttachments()
Definition unit_sao.cpp:210
v3f m_rotation
Definition unit_sao.h:98
const std::unordered_set< object_t > & getAttachmentChildIds() const override
Definition unit_sao.h:73
void onDetach(ServerActiveObject *parent)
Definition unit_sao.cpp:259
void getAnimation(v2f *frame_range, float *frame_speed, float *frame_blend, bool *frame_loop) override
Definition unit_sao.cpp:51
void addAttachmentChild(object_t child_id) override
Definition unit_sao.cpp:236
bool m_animation_loop
Definition unit_sao.h:139
core::vector2d< f32 > v2f
Definition irr_v2d.h:11
core::vector3df v3f
Definition irr_v3d.h:11
std::unordered_map< std::string, int > ItemGroupList
Definition itemgroup.h:10
static int itemgroup_get(const ItemGroupList &groups, const std::string &name)
Definition itemgroup.h:12
ServerActiveObject::object_t object_t
Definition l_object.cpp:28
void setPitchYawRoll(core::matrix4 &m, v3f rot)
Definition numeric.h:462
Definition activeobject.h:63
Definition object_properties.h:15