3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "irrlichttypes_extrabloated.h"
24 #include "clientobject.h"
25 #include "object_properties.h"
26 #include "itemgroup.h"
27 #include "constants.h"
39 struct SmoothTranslator
45 f32 anim_time_counter = 0;
46 bool aim_is_end = true;
48 SmoothTranslator() = default;
52 void update(T new_target, bool is_end_position = false,
53 float update_interval = -1);
55 void translate(f32 dtime);
58 struct SmoothTranslatorWrapped : SmoothTranslator<f32>
60 void translate(f32 dtime);
63 struct SmoothTranslatorWrappedv3f : SmoothTranslator<v3f>
65 void translate(f32 dtime);
68 class GenericCAO : public ClientActiveObject
71 // Only set at initialization
72 std::string m_name = "";
73 bool m_is_player = false;
74 bool m_is_local_player = false;
75 // Property-ish things
76 ObjectProperties m_prop;
78 scene::ISceneManager *m_smgr = nullptr;
79 Client *m_client = nullptr;
80 aabb3f m_selection_box = aabb3f(-BS/3.,-BS/3.,-BS/3., BS/3.,BS/3.,BS/3.);
81 scene::IMeshSceneNode *m_meshnode = nullptr;
82 scene::IAnimatedMeshSceneNode *m_animated_meshnode = nullptr;
83 WieldMeshSceneNode *m_wield_meshnode = nullptr;
84 scene::IBillboardSceneNode *m_spritenode = nullptr;
85 scene::IDummyTransformationSceneNode *m_matrixnode = nullptr;
86 Nametag *m_nametag = nullptr;
87 v3f m_position = v3f(0.0f, 10.0f * BS, 0);
92 SmoothTranslator<v3f> pos_translator;
93 SmoothTranslatorWrappedv3f rot_translator;
94 // Spritesheet/animation stuff
95 v2f m_tx_size = v2f(1,1);
97 bool m_initial_tx_basepos_set = false;
98 bool m_tx_select_horiz_by_yawpitch = false;
99 v2s32 m_animation_range;
100 float m_animation_speed = 15.0f;
101 float m_animation_blend = 0.0f;
102 bool m_animation_loop = true;
103 // stores position and rotation for each bone name
104 std::unordered_map<std::string, core::vector2d<v3f>> m_bone_position;
106 int m_attachment_parent_id = 0;
107 std::unordered_set<int> m_attachment_child_ids;
108 std::string m_attachment_bone = "";
109 v3f m_attachment_position;
110 v3f m_attachment_rotation;
111 bool m_attached_to_local = false;
113 int m_anim_frame = 0;
114 int m_anim_num_frames = 1;
115 float m_anim_framelength = 0.2f;
116 float m_anim_timer = 0.0f;
117 ItemGroupList m_armor_groups;
118 float m_reset_textures_timer = -1.0f;
119 // stores texture modifier before punch update
120 std::string m_previous_texture_modifier = "";
121 // last applied texture modifier
122 std::string m_current_texture_modifier = "";
123 bool m_visuals_expired = false;
124 float m_step_distance_counter = 0.0f;
125 u8 m_last_light = 255;
126 bool m_is_visible = false;
129 video::E_MATERIAL_TYPE m_material_type;
131 bool m_enable_shaders = false;
133 bool visualExpiryRequired(const ObjectProperties &newprops) const;
136 GenericCAO(Client *client, ClientEnvironment *env);
140 static ClientActiveObject* create(Client *client, ClientEnvironment *env)
142 return new GenericCAO(client, env);
145 inline ActiveObjectType getType() const
147 return ACTIVEOBJECT_TYPE_GENERIC;
149 inline const ItemGroupList &getGroups() const
151 return m_armor_groups;
153 void initialize(const std::string &data);
155 void processInitData(const std::string &data);
157 bool getCollisionBox(aabb3f *toset) const;
159 bool collideWithObjects() const;
161 virtual bool getSelectionBox(aabb3f *toset) const;
163 const v3f getPosition() const;
165 void setPosition(const v3f &pos)
167 pos_translator.val_current = pos;
170 inline const v3f &getRotation() const { return m_rotation; }
172 const bool isImmortal();
174 scene::ISceneNode *getSceneNode() const;
176 scene::IAnimatedMeshSceneNode *getAnimatedMeshSceneNode() const;
178 // m_matrixnode controls the position and rotation of the child node
179 // for all scene nodes, as a workaround for an Irrlicht problem with
180 // rotations. The child node's position can't be used because it's
181 // rotated, and must remain as 0.
182 // Note that m_matrixnode.setPosition() shouldn't be called. Use
183 // m_matrixnode->getRelativeTransformationMatrix().setTranslation()
184 // instead (aka getPosRotMatrix().setTranslation()).
185 inline core::matrix4 &getPosRotMatrix()
187 assert(m_matrixnode);
188 return m_matrixnode->getRelativeTransformationMatrix();
191 inline const core::matrix4 &getAbsolutePosRotMatrix() const
193 assert(m_matrixnode);
194 return m_matrixnode->getAbsoluteTransformation();
197 inline f32 getStepHeight() const
199 return m_prop.stepheight;
202 inline bool isLocalPlayer() const
204 return m_is_local_player;
207 inline bool isVisible() const
212 inline void setVisible(bool toset)
214 m_is_visible = toset;
217 void setChildrenVisible(bool toset);
218 void setAttachment(int parent_id, const std::string &bone, v3f position, v3f rotation);
219 void getAttachment(int *parent_id, std::string *bone, v3f *position,
220 v3f *rotation) const;
221 void clearChildAttachments();
222 void clearParentAttachment();
223 void addAttachmentChild(int child_id);
224 void removeAttachmentChild(int child_id);
225 ClientActiveObject *getParent() const;
226 const std::unordered_set<int> &getAttachmentChildIds() const
227 { return m_attachment_child_ids; }
228 void updateAttachments();
230 void removeFromScene(bool permanent);
232 void addToScene(ITextureSource *tsrc);
234 inline void expireVisuals()
236 m_visuals_expired = true;
239 void updateLight(u8 light_at_pos);
241 void updateLightNoCheck(u8 light_at_pos);
243 void setNodeLight(u8 light);
245 v3s16 getLightPosition();
247 void updateNametag();
249 void updateNodePos();
251 void step(float dtime, ClientEnvironment *env);
253 void updateTexturePos();
255 // ffs this HAS TO BE a string copy! See #5739 if you think otherwise
256 // Reason: updateTextures(m_previous_texture_modifier);
257 void updateTextures(std::string mod);
259 void updateAnimation();
261 void updateAnimationSpeed();
263 void updateBonePosition();
265 void processMessage(const std::string &data);
267 bool directReportPunch(v3f dir, const ItemStack *punchitem=NULL,
268 float time_from_last_punch=1000000);
270 std::string debugInfoText();
272 std::string infoText()
274 return m_prop.infotext;