player.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 PlayerSettings
  84. {
  85. bool free_move = false;
  86. bool pitch_move = false;
  87. bool fast_move = false;
  88. bool continuous_forward = false;
  89. bool always_fly_fast = false;
  90. bool aux1_descends = false;
  91. bool noclip = false;
  92. bool autojump = false;
  93. const std::string setting_names[8] = {
  94. "free_move", "pitch_move", "fast_move", "continuous_forward", "always_fly_fast",
  95. "aux1_descends", "noclip", "autojump"
  96. };
  97. void readGlobalSettings();
  98. };
  99. class Map;
  100. struct CollisionInfo;
  101. struct HudElement;
  102. class Environment;
  103. class Player
  104. {
  105. public:
  106. Player(const char *name, IItemDefManager *idef);
  107. virtual ~Player() = 0;
  108. DISABLE_CLASS_COPY(Player);
  109. virtual void move(f32 dtime, Environment *env, f32 pos_max_d)
  110. {}
  111. virtual void move(f32 dtime, Environment *env, f32 pos_max_d,
  112. std::vector<CollisionInfo> *collision_info)
  113. {}
  114. v3f getSpeed() const
  115. {
  116. return m_speed;
  117. }
  118. void setSpeed(v3f speed)
  119. {
  120. clampToF1000(speed);
  121. m_speed = speed;
  122. }
  123. const char *getName() const { return m_name; }
  124. u32 getFreeHudID()
  125. {
  126. size_t size = hud.size();
  127. for (size_t i = 0; i != size; i++) {
  128. if (!hud[i])
  129. return i;
  130. }
  131. return size;
  132. }
  133. v3f eye_offset_first;
  134. v3f eye_offset_third;
  135. Inventory inventory;
  136. f32 movement_acceleration_default;
  137. f32 movement_acceleration_air;
  138. f32 movement_acceleration_fast;
  139. f32 movement_speed_walk;
  140. f32 movement_speed_crouch;
  141. f32 movement_speed_fast;
  142. f32 movement_speed_climb;
  143. f32 movement_speed_jump;
  144. f32 movement_liquid_fluidity;
  145. f32 movement_liquid_fluidity_smooth;
  146. f32 movement_liquid_sink;
  147. f32 movement_gravity;
  148. v2s32 local_animations[4];
  149. float local_animation_speed;
  150. std::string inventory_formspec;
  151. std::string formspec_prepend;
  152. PlayerControl control;
  153. const PlayerControl& getPlayerControl() { return control; }
  154. PlayerSettings &getPlayerSettings() { return m_player_settings; }
  155. static void settingsChangedCallback(const std::string &name, void *data);
  156. // Returns non-empty `selected` ItemStack. `hand` is a fallback, if specified
  157. ItemStack &getWieldedItem(ItemStack *selected, ItemStack *hand) const;
  158. void setWieldIndex(u16 index);
  159. u16 getWieldIndex() const { return m_wield_index; }
  160. void setFov(const PlayerFovSpec &spec)
  161. {
  162. m_fov_override_spec = spec;
  163. }
  164. const PlayerFovSpec &getFov() const
  165. {
  166. return m_fov_override_spec;
  167. }
  168. HudElement* getHud(u32 id);
  169. u32 addHud(HudElement* hud);
  170. HudElement* removeHud(u32 id);
  171. void clearHud();
  172. u32 hud_flags;
  173. s32 hud_hotbar_itemcount;
  174. protected:
  175. char m_name[PLAYERNAME_SIZE];
  176. v3f m_speed;
  177. u16 m_wield_index = 0;
  178. PlayerFovSpec m_fov_override_spec = { 0.0f, false, 0.0f };
  179. std::vector<HudElement *> hud;
  180. private:
  181. // Protect some critical areas
  182. // hud for example can be modified by EmergeThread
  183. // and ServerThread
  184. std::mutex m_mutex;
  185. PlayerSettings m_player_settings;
  186. };