localplayer.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 "player.h"
  18. #include "environment.h"
  19. #include "constants.h"
  20. #include "settings.h"
  21. #include <list>
  22. class Client;
  23. class Environment;
  24. class GenericCAO;
  25. class ClientActiveObject;
  26. class ClientEnvironment;
  27. class IGameDef;
  28. enum LocalPlayerAnimations
  29. {
  30. NO_ANIM,
  31. WALK_ANIM,
  32. DIG_ANIM,
  33. WD_ANIM
  34. }; // no local animation, walking, digging, both
  35. class LocalPlayer : public Player
  36. {
  37. public:
  38. LocalPlayer(Client *client, const char *name);
  39. virtual ~LocalPlayer() = default;
  40. ClientActiveObject *parent = nullptr;
  41. // Initialize hp to 0, so that no hearts will be shown if server
  42. // doesn't support health points
  43. u16 hp = 0;
  44. bool isAttached = false;
  45. bool touching_ground = false;
  46. // This oscillates so that the player jumps a bit above the surface
  47. bool in_liquid = false;
  48. // This is more stable and defines the maximum speed of the player
  49. bool in_liquid_stable = false;
  50. // Gets the viscosity of water to calculate friction
  51. u8 liquid_viscosity = 0;
  52. bool is_climbing = false;
  53. bool swimming_vertical = false;
  54. float physics_override_speed = 1.0f;
  55. float physics_override_jump = 1.0f;
  56. float physics_override_gravity = 1.0f;
  57. bool physics_override_sneak = true;
  58. bool physics_override_sneak_glitch = false;
  59. // Temporary option for old move code
  60. bool physics_override_new_move = true;
  61. v3f overridePosition;
  62. void move(f32 dtime, Environment *env, f32 pos_max_d);
  63. void move(f32 dtime, Environment *env, f32 pos_max_d,
  64. std::vector<CollisionInfo> *collision_info);
  65. // Temporary option for old move code
  66. void old_move(f32 dtime, Environment *env, f32 pos_max_d,
  67. std::vector<CollisionInfo> *collision_info);
  68. void applyControl(float dtime, Environment *env);
  69. v3s16 getStandingNodePos();
  70. v3s16 getFootstepNodePos();
  71. // Used to check if anything changed and prevent sending packets if not
  72. v3f last_position;
  73. v3f last_speed;
  74. float last_pitch = 0.0f;
  75. float last_yaw = 0.0f;
  76. unsigned int last_keyPressed = 0;
  77. u8 last_camera_fov = 0;
  78. u8 last_wanted_range = 0;
  79. float camera_impact = 0.0f;
  80. bool makes_footstep_sound = true;
  81. int last_animation = NO_ANIM;
  82. float last_animation_speed;
  83. std::string hotbar_image = "";
  84. std::string hotbar_selected_image = "";
  85. video::SColor light_color = video::SColor(255, 255, 255, 255);
  86. float hurt_tilt_timer = 0.0f;
  87. float hurt_tilt_strength = 0.0f;
  88. GenericCAO *getCAO() const { return m_cao; }
  89. void setCAO(GenericCAO *toset)
  90. {
  91. assert(!m_cao); // Pre-condition
  92. m_cao = toset;
  93. }
  94. u32 maxHudId() const { return hud.size(); }
  95. u16 getBreath() const { return m_breath; }
  96. void setBreath(u16 breath) { m_breath = breath; }
  97. v3s16 getLightPosition() const;
  98. void setYaw(f32 yaw) { m_yaw = yaw; }
  99. f32 getYaw() const { return m_yaw; }
  100. void setPitch(f32 pitch) { m_pitch = pitch; }
  101. f32 getPitch() const { return m_pitch; }
  102. inline void setPosition(const v3f &position)
  103. {
  104. m_position = position;
  105. m_sneak_node_exists = false;
  106. }
  107. v3f getPosition() const { return m_position; }
  108. v3f getEyePosition() const { return m_position + getEyeOffset(); }
  109. v3f getEyeOffset() const;
  110. void setEyeHeight(float eye_height) { m_eye_height = eye_height; }
  111. void setCollisionbox(const aabb3f &box) { m_collisionbox = box; }
  112. float getZoomFOV() const { return m_zoom_fov; }
  113. void setZoomFOV(float zoom_fov) { m_zoom_fov = zoom_fov; }
  114. private:
  115. void accelerateHorizontal(const v3f &target_speed, const f32 max_increase);
  116. void accelerateVertical(const v3f &target_speed, const f32 max_increase);
  117. bool updateSneakNode(Map *map, const v3f &position, const v3f &sneak_max);
  118. float getSlipFactor(Environment *env, const v3f &speedH);
  119. v3f m_position;
  120. v3s16 m_standing_node;
  121. v3s16 m_sneak_node = v3s16(32767, 32767, 32767);
  122. // Stores the top bounding box of m_sneak_node
  123. aabb3f m_sneak_node_bb_top = aabb3f(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
  124. // Whether the player is allowed to sneak
  125. bool m_sneak_node_exists = false;
  126. // Whether a "sneak ladder" structure is detected at the players pos
  127. // see detectSneakLadder() in the .cpp for more info (always false if disabled)
  128. bool m_sneak_ladder_detected = false;
  129. // ***** Variables for temporary option of the old move code *****
  130. // Stores the max player uplift by m_sneak_node
  131. f32 m_sneak_node_bb_ymax = 0.0f;
  132. // Whether recalculation of m_sneak_node and its top bbox is needed
  133. bool m_need_to_get_new_sneak_node = true;
  134. // Node below player, used to determine whether it has been removed,
  135. // and its old type
  136. v3s16 m_old_node_below = v3s16(32767, 32767, 32767);
  137. std::string m_old_node_below_type = "air";
  138. // ***** End of variables for temporary option *****
  139. bool m_can_jump = false;
  140. u16 m_breath = PLAYER_MAX_BREATH_DEFAULT;
  141. f32 m_yaw = 0.0f;
  142. f32 m_pitch = 0.0f;
  143. bool camera_barely_in_ceiling = false;
  144. aabb3f m_collisionbox = aabb3f(-BS * 0.30f, 0.0f, -BS * 0.30f, BS * 0.30f,
  145. BS * 1.75f, BS * 0.30f);
  146. float m_eye_height = 1.625f;
  147. float m_zoom_fov = 0.0f;
  148. GenericCAO *m_cao = nullptr;
  149. Client *m_client;
  150. };