content_cao.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include <map>
  18. #include "irrlichttypes_extrabloated.h"
  19. #include "clientobject.h"
  20. #include "object_properties.h"
  21. #include "itemgroup.h"
  22. #include "constants.h"
  23. #include <cassert>
  24. class Camera;
  25. class Client;
  26. struct Nametag;
  27. struct MinimapMarker;
  28. /*
  29. SmoothTranslator
  30. */
  31. template<typename T>
  32. struct SmoothTranslator
  33. {
  34. T val_old;
  35. T val_current;
  36. T val_target;
  37. f32 anim_time = 0;
  38. f32 anim_time_counter = 0;
  39. bool aim_is_end = true;
  40. SmoothTranslator() = default;
  41. void init(T current);
  42. void update(T new_target, bool is_end_position = false,
  43. float update_interval = -1);
  44. void translate(f32 dtime);
  45. };
  46. struct SmoothTranslatorWrapped : SmoothTranslator<f32>
  47. {
  48. void translate(f32 dtime);
  49. };
  50. struct SmoothTranslatorWrappedv3f : SmoothTranslator<v3f>
  51. {
  52. void translate(f32 dtime);
  53. };
  54. class GenericCAO : public ClientActiveObject
  55. {
  56. private:
  57. // Only set at initialization
  58. std::string m_name = "";
  59. bool m_is_player = false;
  60. bool m_is_local_player = false;
  61. // Property-ish things
  62. ObjectProperties m_prop;
  63. //
  64. scene::ISceneManager *m_smgr = nullptr;
  65. Client *m_client = nullptr;
  66. aabb3f m_selection_box = aabb3f(-BS/3.,-BS/3.,-BS/3., BS/3.,BS/3.,BS/3.);
  67. scene::IMeshSceneNode *m_meshnode = nullptr;
  68. scene::IAnimatedMeshSceneNode *m_animated_meshnode = nullptr;
  69. WieldMeshSceneNode *m_wield_meshnode = nullptr;
  70. scene::IBillboardSceneNode *m_spritenode = nullptr;
  71. scene::IDummyTransformationSceneNode *m_matrixnode = nullptr;
  72. Nametag *m_nametag = nullptr;
  73. MinimapMarker *m_marker = nullptr;
  74. v3f m_position = v3f(0.0f, 10.0f * BS, 0);
  75. v3f m_velocity;
  76. v3f m_acceleration;
  77. v3f m_rotation;
  78. u16 m_hp = 1;
  79. SmoothTranslator<v3f> pos_translator;
  80. SmoothTranslatorWrappedv3f rot_translator;
  81. // Spritesheet/animation stuff
  82. v2f m_tx_size = v2f(1,1);
  83. v2s16 m_tx_basepos;
  84. bool m_initial_tx_basepos_set = false;
  85. bool m_tx_select_horiz_by_yawpitch = false;
  86. v2s32 m_animation_range;
  87. float m_animation_speed = 15.0f;
  88. float m_animation_blend = 0.0f;
  89. bool m_animation_loop = true;
  90. // stores position and rotation for each bone name
  91. std::unordered_map<std::string, core::vector2d<v3f>> m_bone_position;
  92. int m_attachment_parent_id = 0;
  93. std::unordered_set<int> m_attachment_child_ids;
  94. std::string m_attachment_bone = "";
  95. v3f m_attachment_position;
  96. v3f m_attachment_rotation;
  97. bool m_attached_to_local = false;
  98. bool m_force_visible = false;
  99. int m_anim_frame = 0;
  100. int m_anim_num_frames = 1;
  101. float m_anim_framelength = 0.2f;
  102. float m_anim_timer = 0.0f;
  103. ItemGroupList m_armor_groups;
  104. float m_reset_textures_timer = -1.0f;
  105. // stores texture modifier before punch update
  106. std::string m_previous_texture_modifier = "";
  107. // last applied texture modifier
  108. std::string m_current_texture_modifier = "";
  109. bool m_visuals_expired = false;
  110. float m_step_distance_counter = 0.0f;
  111. video::SColor m_last_light = video::SColor(0xFFFFFFFF);
  112. bool m_is_visible = false;
  113. // Material
  114. video::E_MATERIAL_TYPE m_material_type;
  115. // Settings
  116. bool m_enable_shaders = false;
  117. bool visualExpiryRequired(const ObjectProperties &newprops) const;
  118. public:
  119. GenericCAO(Client *client, ClientEnvironment *env);
  120. ~GenericCAO();
  121. static ClientActiveObject* create(Client *client, ClientEnvironment *env)
  122. {
  123. return new GenericCAO(client, env);
  124. }
  125. inline ActiveObjectType getType() const
  126. {
  127. return ACTIVEOBJECT_TYPE_GENERIC;
  128. }
  129. inline const ItemGroupList &getGroups() const
  130. {
  131. return m_armor_groups;
  132. }
  133. void initialize(const std::string &data);
  134. void processInitData(const std::string &data);
  135. bool getCollisionBox(aabb3f *toset) const;
  136. bool collideWithObjects() const;
  137. virtual bool getSelectionBox(aabb3f *toset) const;
  138. const v3f getPosition() const;
  139. inline const v3f &getRotation() const { return m_rotation; }
  140. bool isImmortal() const;
  141. inline const ObjectProperties &getProperties() const { return m_prop; }
  142. scene::ISceneNode *getSceneNode() const;
  143. scene::IAnimatedMeshSceneNode *getAnimatedMeshSceneNode() const;
  144. // m_matrixnode controls the position and rotation of the child node
  145. // for all scene nodes, as a workaround for an Irrlicht problem with
  146. // rotations. The child node's position can't be used because it's
  147. // rotated, and must remain as 0.
  148. // Note that m_matrixnode.setPosition() shouldn't be called. Use
  149. // m_matrixnode->getRelativeTransformationMatrix().setTranslation()
  150. // instead (aka getPosRotMatrix().setTranslation()).
  151. inline core::matrix4 &getPosRotMatrix()
  152. {
  153. assert(m_matrixnode);
  154. return m_matrixnode->getRelativeTransformationMatrix();
  155. }
  156. inline const core::matrix4 *getAbsolutePosRotMatrix() const
  157. {
  158. if (!m_matrixnode)
  159. return nullptr;
  160. return &m_matrixnode->getAbsoluteTransformation();
  161. }
  162. inline f32 getStepHeight() const
  163. {
  164. return m_prop.stepheight;
  165. }
  166. inline bool isLocalPlayer() const
  167. {
  168. return m_is_local_player;
  169. }
  170. inline bool isVisible() const
  171. {
  172. return m_is_visible;
  173. }
  174. inline void setVisible(bool toset)
  175. {
  176. m_is_visible = toset;
  177. }
  178. void setChildrenVisible(bool toset);
  179. void setAttachment(int parent_id, const std::string &bone, v3f position,
  180. v3f rotation, bool force_visible);
  181. void getAttachment(int *parent_id, std::string *bone, v3f *position,
  182. v3f *rotation, bool *force_visible) const;
  183. void clearChildAttachments();
  184. void clearParentAttachment();
  185. void addAttachmentChild(int child_id);
  186. void removeAttachmentChild(int child_id);
  187. ClientActiveObject *getParent() const;
  188. const std::unordered_set<int> &getAttachmentChildIds() const
  189. { return m_attachment_child_ids; }
  190. void updateAttachments();
  191. void removeFromScene(bool permanent);
  192. void addToScene(ITextureSource *tsrc, scene::ISceneManager *smgr);
  193. inline void expireVisuals()
  194. {
  195. m_visuals_expired = true;
  196. }
  197. void updateLight(u32 day_night_ratio);
  198. void setNodeLight(const video::SColor &light);
  199. /* Get light position(s).
  200. * returns number of positions written into pos[], which must have space
  201. * for at least 3 vectors. */
  202. u16 getLightPosition(v3s16 *pos);
  203. void updateNametag();
  204. void updateMarker();
  205. void updateNodePos();
  206. void step(float dtime, ClientEnvironment *env);
  207. void updateTexturePos();
  208. // ffs this HAS TO BE a string copy! See #5739 if you think otherwise
  209. // Reason: updateTextures(m_previous_texture_modifier);
  210. void updateTextures(std::string mod);
  211. void updateAnimation();
  212. void updateAnimationSpeed();
  213. void updateBonePosition();
  214. void processMessage(const std::string &data);
  215. bool directReportPunch(v3f dir, const ItemStack *punchitem=NULL,
  216. float time_from_last_punch=1000000);
  217. std::string debugInfoText();
  218. std::string infoText()
  219. {
  220. return m_prop.infotext;
  221. }
  222. void updateMeshCulling();
  223. };