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 
26 class UnitSAO : public ServerActiveObject
27 {
28 public:
29 	UnitSAO(ServerEnvironment *env, v3f pos);
30 	virtual ~UnitSAO() = default;
31 
getHP()32 	u16 getHP() const { return m_hp; }
33 	// Use a function, if isDead can be defined by other conditions
isDead()34 	bool isDead() const { return m_hp == 0; }
35 
36 	// Rotation
setRotation(v3f rotation)37 	void setRotation(v3f rotation) { m_rotation = rotation; }
getRotation()38 	const v3f &getRotation() const { return m_rotation; }
getRadRotation()39 	v3f getRadRotation() { return m_rotation * core::DEGTORAD; }
40 
41 	// Deprecated
getRadYawDep()42 	f32 getRadYawDep() const { return (m_rotation.Y + 90.) * core::DEGTORAD; }
43 
44 	// Armor groups
isImmortal()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
64 	ServerActiveObject *getParent() const;
isAttached()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
77 	ObjectProperties *accessObjectProperties();
78 	void notifyObjectPropertiesModified();
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 
97 	v3f m_rotation;
98 
99 	ItemGroupList m_armor_groups;
100 
101 	// Object properties
102 	bool m_properties_sent = true;
103 	ObjectProperties m_prop;
104 
105 	// Stores position and rotation for each bone name
106 	std::unordered_map<std::string, core::vector2d<v3f>> m_bone_position;
107 
108 	int m_attachment_parent_id = 0;
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
120 	v2f m_animation_range;
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;
125 	bool m_animation_speed_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 = "";
133 	v3f m_attachment_position;
134 	v3f m_attachment_rotation;
135 	bool m_attachment_sent = false;
136 	bool m_force_visible = false;
137 };
138