player.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 "irrlichttypes_bloated.h"
  18. #include "inventory.h"
  19. #include "constants.h"
  20. #include "network/networkprotocol.h"
  21. #include "util/basic_macros.h"
  22. #include <list>
  23. #include <mutex>
  24. #define PLAYERNAME_SIZE 20
  25. #define PLAYERNAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"
  26. #define PLAYERNAME_ALLOWED_CHARS_USER_EXPL "'a' to 'z', 'A' to 'Z', '0' to '9', '-', '_'"
  27. struct PlayerFovSpec
  28. {
  29. f32 fov;
  30. // Whether to multiply the client's FOV or to override it
  31. bool is_multiplier;
  32. // The time to be take to trasition to the new FOV value.
  33. // Transition is instantaneous if omitted. Omitted by default.
  34. f32 transition_time;
  35. };
  36. struct PlayerControl
  37. {
  38. PlayerControl() = default;
  39. PlayerControl(
  40. bool a_up, bool a_down, bool a_left, bool a_right,
  41. bool a_jump, bool a_aux1, bool a_sneak,
  42. bool a_zoom,
  43. bool a_dig, bool a_place,
  44. float a_pitch, float a_yaw,
  45. float a_movement_speed, float a_movement_direction
  46. )
  47. {
  48. // Encode direction keys into a single value so nobody uses it accidentally
  49. // as movement_{speed,direction} is supposed to be the source of truth.
  50. direction_keys = (a_up&1) | ((a_down&1) << 1) |
  51. ((a_left&1) << 2) | ((a_right&1) << 3);
  52. jump = a_jump;
  53. aux1 = a_aux1;
  54. sneak = a_sneak;
  55. zoom = a_zoom;
  56. dig = a_dig;
  57. place = a_place;
  58. pitch = a_pitch;
  59. yaw = a_yaw;
  60. movement_speed = a_movement_speed;
  61. movement_direction = a_movement_direction;
  62. }
  63. #ifndef SERVER
  64. // For client use
  65. u32 getKeysPressed() const;
  66. inline bool isMoving() const { return movement_speed > 0.001f; }
  67. #endif
  68. // For server use
  69. void unpackKeysPressed(u32 keypress_bits);
  70. u8 direction_keys = 0;
  71. bool jump = false;
  72. bool aux1 = false;
  73. bool sneak = false;
  74. bool zoom = false;
  75. bool dig = false;
  76. bool place = false;
  77. // Note: These four are NOT available on the server
  78. float pitch = 0.0f;
  79. float yaw = 0.0f;
  80. float movement_speed = 0.0f;
  81. float movement_direction = 0.0f;
  82. };
  83. struct PlayerPhysicsOverride
  84. {
  85. float speed = 1.f;
  86. float jump = 1.f;
  87. float gravity = 1.f;
  88. bool sneak = true;
  89. bool sneak_glitch = false;
  90. // "Temporary" option for old move code
  91. bool new_move = true;
  92. };
  93. struct PlayerSettings
  94. {
  95. bool free_move = false;
  96. bool pitch_move = false;
  97. bool fast_move = false;
  98. bool continuous_forward = false;
  99. bool always_fly_fast = false;
  100. bool aux1_descends = false;
  101. bool noclip = false;
  102. bool autojump = false;
  103. const std::string setting_names[8] = {
  104. "free_move", "pitch_move", "fast_move", "continuous_forward", "always_fly_fast",
  105. "aux1_descends", "noclip", "autojump"
  106. };
  107. void readGlobalSettings();
  108. };
  109. class Map;
  110. struct CollisionInfo;
  111. struct HudElement;
  112. class Environment;
  113. class Player
  114. {
  115. public:
  116. Player(const char *name, IItemDefManager *idef);
  117. virtual ~Player() = 0;
  118. DISABLE_CLASS_COPY(Player);
  119. virtual void move(f32 dtime, Environment *env, f32 pos_max_d)
  120. {}
  121. virtual void move(f32 dtime, Environment *env, f32 pos_max_d,
  122. std::vector<CollisionInfo> *collision_info)
  123. {}
  124. // in BS-space
  125. v3f getSpeed() const
  126. {
  127. return m_speed;
  128. }
  129. // in BS-space
  130. void setSpeed(v3f speed)
  131. {
  132. m_speed = speed;
  133. }
  134. const char *getName() const { return m_name; }
  135. u32 getFreeHudID()
  136. {
  137. size_t size = hud.size();
  138. for (size_t i = 0; i != size; i++) {
  139. if (!hud[i])
  140. return i;
  141. }
  142. return size;
  143. }
  144. v3f eye_offset_first;
  145. v3f eye_offset_third;
  146. Inventory inventory;
  147. f32 movement_acceleration_default;
  148. f32 movement_acceleration_air;
  149. f32 movement_acceleration_fast;
  150. f32 movement_speed_walk;
  151. f32 movement_speed_crouch;
  152. f32 movement_speed_fast;
  153. f32 movement_speed_climb;
  154. f32 movement_speed_jump;
  155. f32 movement_liquid_fluidity;
  156. f32 movement_liquid_fluidity_smooth;
  157. f32 movement_liquid_sink;
  158. f32 movement_gravity;
  159. v2s32 local_animations[4];
  160. float local_animation_speed;
  161. std::string inventory_formspec;
  162. std::string formspec_prepend;
  163. PlayerControl control;
  164. const PlayerControl& getPlayerControl() { return control; }
  165. PlayerPhysicsOverride physics_override;
  166. PlayerSettings &getPlayerSettings() { return m_player_settings; }
  167. static void settingsChangedCallback(const std::string &name, void *data);
  168. // Returns non-empty `selected` ItemStack. `hand` is a fallback, if specified
  169. ItemStack &getWieldedItem(ItemStack *selected, ItemStack *hand) const;
  170. void setWieldIndex(u16 index);
  171. u16 getWieldIndex() const { return m_wield_index; }
  172. void setFov(const PlayerFovSpec &spec)
  173. {
  174. m_fov_override_spec = spec;
  175. }
  176. const PlayerFovSpec &getFov() const
  177. {
  178. return m_fov_override_spec;
  179. }
  180. HudElement* getHud(u32 id);
  181. u32 addHud(HudElement* hud);
  182. HudElement* removeHud(u32 id);
  183. void clearHud();
  184. u32 hud_flags;
  185. s32 hud_hotbar_itemcount;
  186. protected:
  187. char m_name[PLAYERNAME_SIZE];
  188. v3f m_speed; // velocity; in BS-space
  189. u16 m_wield_index = 0;
  190. PlayerFovSpec m_fov_override_spec = { 0.0f, false, 0.0f };
  191. std::vector<HudElement *> hud;
  192. private:
  193. // Protect some critical areas
  194. // hud for example can be modified by EmergeThread
  195. // and ServerThread
  196. std::mutex m_mutex;
  197. PlayerSettings m_player_settings;
  198. };