activeobject.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 "irr_aabb3d.h"
  18. #include "irr_v3d.h"
  19. #include <quaternion.h>
  20. #include <string>
  21. #include <unordered_map>
  22. enum ActiveObjectType {
  23. ACTIVEOBJECT_TYPE_INVALID = 0,
  24. ACTIVEOBJECT_TYPE_TEST = 1,
  25. // Obsolete stuff
  26. // ACTIVEOBJECT_TYPE_ITEM = 2,
  27. // ACTIVEOBJECT_TYPE_RAT = 3,
  28. // ACTIVEOBJECT_TYPE_OERKKI1 = 4,
  29. // ACTIVEOBJECT_TYPE_FIREFLY = 5,
  30. // ACTIVEOBJECT_TYPE_MOBV2 = 6,
  31. // End obsolete stuff
  32. ACTIVEOBJECT_TYPE_LUAENTITY = 7,
  33. // Special type, not stored as a static object
  34. ACTIVEOBJECT_TYPE_PLAYER = 100,
  35. // Special type, only exists as CAO
  36. ACTIVEOBJECT_TYPE_GENERIC = 101,
  37. };
  38. // Other types are defined in content_object.h
  39. struct ActiveObjectMessage
  40. {
  41. ActiveObjectMessage(u16 id_, bool reliable_=true, std::string_view data_ = "") :
  42. id(id_),
  43. reliable(reliable_),
  44. datastring(data_)
  45. {}
  46. u16 id;
  47. bool reliable;
  48. std::string datastring;
  49. };
  50. enum ActiveObjectCommand {
  51. AO_CMD_SET_PROPERTIES,
  52. AO_CMD_UPDATE_POSITION,
  53. AO_CMD_SET_TEXTURE_MOD,
  54. AO_CMD_SET_SPRITE,
  55. AO_CMD_PUNCHED,
  56. AO_CMD_UPDATE_ARMOR_GROUPS,
  57. AO_CMD_SET_ANIMATION,
  58. AO_CMD_SET_BONE_POSITION,
  59. AO_CMD_ATTACH_TO,
  60. AO_CMD_SET_PHYSICS_OVERRIDE,
  61. AO_CMD_OBSOLETE1,
  62. // ^ UPDATE_NAMETAG_ATTRIBUTES deprecated since 0.4.14, removed in 5.3.0
  63. AO_CMD_SPAWN_INFANT,
  64. AO_CMD_SET_ANIMATION_SPEED
  65. };
  66. struct BoneOverride
  67. {
  68. struct PositionProperty
  69. {
  70. v3f previous;
  71. v3f vector;
  72. bool absolute = false;
  73. f32 interp_timer = 0;
  74. } position;
  75. v3f getPosition(v3f anim_pos) const {
  76. f32 progress = dtime_passed / position.interp_timer;
  77. if (progress > 1.0f || position.interp_timer == 0.0f)
  78. progress = 1.0f;
  79. return position.vector.getInterpolated(position.previous, progress)
  80. + (position.absolute ? v3f() : anim_pos);
  81. }
  82. struct RotationProperty
  83. {
  84. core::quaternion previous;
  85. core::quaternion next;
  86. bool absolute = false;
  87. f32 interp_timer = 0;
  88. } rotation;
  89. v3f getRotationEulerDeg(v3f anim_rot_euler) const {
  90. core::quaternion rot;
  91. f32 progress = dtime_passed / rotation.interp_timer;
  92. if (progress > 1.0f || rotation.interp_timer == 0.0f)
  93. progress = 1.0f;
  94. rot.slerp(rotation.previous, rotation.next, progress);
  95. if (!rotation.absolute) {
  96. core::quaternion anim_rot(anim_rot_euler * core::DEGTORAD);
  97. rot = rot * anim_rot; // first rotate by anim. bone rot., then rot.
  98. }
  99. v3f rot_euler;
  100. rot.toEuler(rot_euler);
  101. return rot_euler * core::RADTODEG;
  102. }
  103. struct ScaleProperty
  104. {
  105. v3f previous;
  106. v3f vector{1, 1, 1};
  107. bool absolute = false;
  108. f32 interp_timer = 0;
  109. } scale;
  110. v3f getScale(v3f anim_scale) const {
  111. f32 progress = dtime_passed / scale.interp_timer;
  112. if (progress > 1.0f || scale.interp_timer == 0.0f)
  113. progress = 1.0f;
  114. return scale.vector.getInterpolated(scale.previous, progress)
  115. * (scale.absolute ? v3f(1) : anim_scale);
  116. }
  117. f32 dtime_passed = 0;
  118. bool isIdentity() const
  119. {
  120. return !position.absolute && position.vector == v3f()
  121. && !rotation.absolute && rotation.next == core::quaternion()
  122. && !scale.absolute && scale.vector == v3f(1);
  123. }
  124. };
  125. typedef std::unordered_map<std::string, BoneOverride> BoneOverrideMap;
  126. /*
  127. Parent class for ServerActiveObject and ClientActiveObject
  128. */
  129. class ActiveObject
  130. {
  131. public:
  132. ActiveObject(u16 id):
  133. m_id(id)
  134. {
  135. }
  136. u16 getId() const
  137. {
  138. return m_id;
  139. }
  140. void setId(u16 id)
  141. {
  142. m_id = id;
  143. }
  144. virtual ActiveObjectType getType() const = 0;
  145. /*!
  146. * Returns the collision box of the object.
  147. * This box is translated by the object's
  148. * location.
  149. * The box's coordinates are world coordinates.
  150. * @returns true if the object has a collision box.
  151. */
  152. virtual bool getCollisionBox(aabb3f *toset) const = 0;
  153. /*!
  154. * Returns the selection box of the object.
  155. * This box is not translated when the
  156. * object moves.
  157. * The box's coordinates are world coordinates.
  158. * @returns true if the object has a selection box.
  159. */
  160. virtual bool getSelectionBox(aabb3f *toset) const = 0;
  161. virtual bool collideWithObjects() const = 0;
  162. virtual void setAttachment(int parent_id, const std::string &bone, v3f position,
  163. v3f rotation, bool force_visible) {}
  164. virtual void getAttachment(int *parent_id, std::string *bone, v3f *position,
  165. v3f *rotation, bool *force_visible) const {}
  166. virtual void clearChildAttachments() {}
  167. virtual void clearParentAttachment() {}
  168. virtual void addAttachmentChild(int child_id) {}
  169. virtual void removeAttachmentChild(int child_id) {}
  170. protected:
  171. u16 m_id; // 0 is invalid, "no id"
  172. };