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"
40 struct SmoothTranslator
46 f32 anim_time_counter = 0;
47 bool aim_is_end = true;
49 SmoothTranslator() = default;
53 void update(T new_target, bool is_end_position = false,
54 float update_interval = -1);
56 void translate(f32 dtime);
59 struct SmoothTranslatorWrapped : SmoothTranslator<f32>
61 void translate(f32 dtime);
64 struct SmoothTranslatorWrappedv3f : SmoothTranslator<v3f>
66 void translate(f32 dtime);
69 class GenericCAO : public ClientActiveObject
72 // Only set at initialization
73 std::string m_name = "";
74 bool m_is_player = false;
75 bool m_is_local_player = false;
76 // Property-ish things
77 ObjectProperties m_prop;
79 scene::ISceneManager *m_smgr = nullptr;
80 Client *m_client = nullptr;
81 aabb3f m_selection_box = aabb3f(-BS/3.,-BS/3.,-BS/3., BS/3.,BS/3.,BS/3.);
82 scene::IMeshSceneNode *m_meshnode = nullptr;
83 scene::IAnimatedMeshSceneNode *m_animated_meshnode = nullptr;
84 WieldMeshSceneNode *m_wield_meshnode = nullptr;
85 scene::IBillboardSceneNode *m_spritenode = nullptr;
86 scene::IDummyTransformationSceneNode *m_matrixnode = nullptr;
87 Nametag *m_nametag = nullptr;
88 MinimapMarker *m_marker = nullptr;
89 v3f m_position = v3f(0.0f, 10.0f * BS, 0);
94 SmoothTranslator<v3f> pos_translator;
95 SmoothTranslatorWrappedv3f rot_translator;
96 // Spritesheet/animation stuff
97 v2f m_tx_size = v2f(1,1);
99 bool m_initial_tx_basepos_set = false;
100 bool m_tx_select_horiz_by_yawpitch = false;
101 v2s32 m_animation_range;
102 float m_animation_speed = 15.0f;
103 float m_animation_blend = 0.0f;
104 bool m_animation_loop = true;
105 // stores position and rotation for each bone name
106 std::unordered_map<std::string, core::vector2d<v3f>> m_bone_position;
108 int m_attachment_parent_id = 0;
109 std::unordered_set<int> m_attachment_child_ids;
110 std::string m_attachment_bone = "";
111 v3f m_attachment_position;
112 v3f m_attachment_rotation;
113 bool m_attached_to_local = false;
114 bool m_force_visible = false;
116 int m_anim_frame = 0;
117 int m_anim_num_frames = 1;
118 float m_anim_framelength = 0.2f;
119 float m_anim_timer = 0.0f;
120 ItemGroupList m_armor_groups;
121 float m_reset_textures_timer = -1.0f;
122 // stores texture modifier before punch update
123 std::string m_previous_texture_modifier = "";
124 // last applied texture modifier
125 std::string m_current_texture_modifier = "";
126 bool m_visuals_expired = false;
127 float m_step_distance_counter = 0.0f;
128 video::SColor m_last_light = video::SColor(0xFFFFFFFF);
129 bool m_is_visible = false;
132 video::E_MATERIAL_TYPE m_material_type;
134 bool m_enable_shaders = false;
136 bool visualExpiryRequired(const ObjectProperties &newprops) const;
139 GenericCAO(Client *client, ClientEnvironment *env);
143 static ClientActiveObject* create(Client *client, ClientEnvironment *env)
145 return new GenericCAO(client, env);
148 inline ActiveObjectType getType() const
150 return ACTIVEOBJECT_TYPE_GENERIC;
152 inline const ItemGroupList &getGroups() const
154 return m_armor_groups;
156 void initialize(const std::string &data);
158 void processInitData(const std::string &data);
160 bool getCollisionBox(aabb3f *toset) const;
162 bool collideWithObjects() const;
164 virtual bool getSelectionBox(aabb3f *toset) const;
166 const v3f getPosition() const;
168 void setPosition(const v3f &pos)
170 pos_translator.val_current = pos;
173 inline const v3f &getRotation() const { return m_rotation; }
175 bool isImmortal() const;
177 inline const ObjectProperties &getProperties() const { return m_prop; }
179 scene::ISceneNode *getSceneNode() const;
181 scene::IAnimatedMeshSceneNode *getAnimatedMeshSceneNode() const;
183 // m_matrixnode controls the position and rotation of the child node
184 // for all scene nodes, as a workaround for an Irrlicht problem with
185 // rotations. The child node's position can't be used because it's
186 // rotated, and must remain as 0.
187 // Note that m_matrixnode.setPosition() shouldn't be called. Use
188 // m_matrixnode->getRelativeTransformationMatrix().setTranslation()
189 // instead (aka getPosRotMatrix().setTranslation()).
190 inline core::matrix4 &getPosRotMatrix()
192 assert(m_matrixnode);
193 return m_matrixnode->getRelativeTransformationMatrix();
196 inline const core::matrix4 *getAbsolutePosRotMatrix() const
200 return &m_matrixnode->getAbsoluteTransformation();
203 inline f32 getStepHeight() const
205 return m_prop.stepheight;
208 inline bool isLocalPlayer() const
210 return m_is_local_player;
213 inline bool isVisible() const
218 inline void setVisible(bool toset)
220 m_is_visible = toset;
223 void setChildrenVisible(bool toset);
224 void setAttachment(int parent_id, const std::string &bone, v3f position,
225 v3f rotation, bool force_visible);
226 void getAttachment(int *parent_id, std::string *bone, v3f *position,
227 v3f *rotation, bool *force_visible) const;
228 void clearChildAttachments();
229 void clearParentAttachment();
230 void addAttachmentChild(int child_id);
231 void removeAttachmentChild(int child_id);
232 ClientActiveObject *getParent() const;
233 const std::unordered_set<int> &getAttachmentChildIds() const
234 { return m_attachment_child_ids; }
235 void updateAttachments();
237 void removeFromScene(bool permanent);
239 void addToScene(ITextureSource *tsrc, scene::ISceneManager *smgr);
241 inline void expireVisuals()
243 m_visuals_expired = true;
246 void updateLight(u32 day_night_ratio);
248 void setNodeLight(const video::SColor &light);
250 /* Get light position(s).
251 * returns number of positions written into pos[], which must have space
252 * for at least 3 vectors. */
253 u16 getLightPosition(v3s16 *pos);
255 void updateNametag();
259 void updateNodePos();
261 void step(float dtime, ClientEnvironment *env);
263 void updateTexturePos();
265 // ffs this HAS TO BE a string copy! See #5739 if you think otherwise
266 // Reason: updateTextures(m_previous_texture_modifier);
267 void updateTextures(std::string mod);
269 void updateAnimation();
271 void updateAnimationSpeed();
273 void updateBonePosition();
275 void processMessage(const std::string &data);
277 bool directReportPunch(v3f dir, const ItemStack *punchitem=NULL,
278 float time_from_last_punch=1000000);
280 std::string debugInfoText();
282 std::string infoText()
284 return m_prop.infotext;
287 void updateMeshCulling();