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 u8 m_last_light
= 255;
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 inline const v3f
getAcceleration() const
177 return m_acceleration
;
180 inline const v3f
getVelocity() const
185 inline const u16
getHp() const
190 const bool isImmortal();
192 scene::ISceneNode
*getSceneNode() const;
194 scene::IAnimatedMeshSceneNode
*getAnimatedMeshSceneNode() const;
196 // m_matrixnode controls the position and rotation of the child node
197 // for all scene nodes, as a workaround for an Irrlicht problem with
198 // rotations. The child node's position can't be used because it's
199 // rotated, and must remain as 0.
200 // Note that m_matrixnode.setPosition() shouldn't be called. Use
201 // m_matrixnode->getRelativeTransformationMatrix().setTranslation()
202 // instead (aka getPosRotMatrix().setTranslation()).
203 inline core::matrix4
&getPosRotMatrix()
205 assert(m_matrixnode
);
206 return m_matrixnode
->getRelativeTransformationMatrix();
209 inline const core::matrix4
*getAbsolutePosRotMatrix() const
213 return &m_matrixnode
->getAbsoluteTransformation();
216 inline f32
getStepHeight() const
218 return m_prop
.stepheight
;
221 inline std::string
getName() const
226 inline bool isPlayer() const
231 inline bool isLocalPlayer() const
233 return m_is_local_player
;
236 inline bool isVisible() const
241 inline void setVisible(bool toset
)
243 m_is_visible
= toset
;
246 void setChildrenVisible(bool toset
);
247 void setAttachment(int parent_id
, const std::string
&bone
, v3f position
,
248 v3f rotation
, bool force_visible
);
249 void getAttachment(int *parent_id
, std::string
*bone
, v3f
*position
,
250 v3f
*rotation
, bool *force_visible
) const;
251 void clearChildAttachments();
252 void clearParentAttachment();
253 void addAttachmentChild(int child_id
);
254 void removeAttachmentChild(int child_id
);
255 ClientActiveObject
*getParent() const;
256 int getParentId() const { return m_attachment_parent_id
; }
257 const std::unordered_set
<int> &getAttachmentChildIds() const
258 { return m_attachment_child_ids
; }
259 void updateAttachments();
261 void removeFromScene(bool permanent
);
263 void addToScene(ITextureSource
*tsrc
);
265 inline void expireVisuals()
267 m_visuals_expired
= true;
270 void updateLight(u32 day_night_ratio
);
272 void setNodeLight(u8 light
);
274 /* Get light position(s).
275 * returns number of positions written into pos[], which must have space
276 * for at least 3 vectors. */
277 u16
getLightPosition(v3s16
*pos
);
279 void updateNametag();
283 void updateNodePos();
285 void step(float dtime
, ClientEnvironment
*env
);
287 void updateTexturePos();
289 // ffs this HAS TO BE a string copy! See #5739 if you think otherwise
290 // Reason: updateTextures(m_previous_texture_modifier);
291 void updateTextures(std::string mod
);
293 void updateAnimation();
295 void updateAnimationSpeed();
297 void updateBonePosition();
299 void processMessage(const std::string
&data
);
301 bool directReportPunch(v3f dir
, const ItemStack
*punchitem
=NULL
,
302 float time_from_last_punch
=1000000);
304 std::string
debugInfoText();
306 std::string
infoText()
308 return m_prop
.infotext
;
311 float m_waiting_for_reattach
;
313 ObjectProperties
*getProperties()
318 void updateMeshCulling();