player.h 6.4 KB

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