player.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 PlayerControl
  28. {
  29. PlayerControl() = default;
  30. PlayerControl(
  31. bool a_up,
  32. bool a_down,
  33. bool a_left,
  34. bool a_right,
  35. bool a_jump,
  36. bool a_aux1,
  37. bool a_sneak,
  38. bool a_zoom,
  39. bool a_LMB,
  40. bool a_RMB,
  41. float a_pitch,
  42. float a_yaw,
  43. float a_sidew_move_joystick_axis,
  44. float a_forw_move_joystick_axis
  45. )
  46. {
  47. up = a_up;
  48. down = a_down;
  49. left = a_left;
  50. right = a_right;
  51. jump = a_jump;
  52. aux1 = a_aux1;
  53. sneak = a_sneak;
  54. zoom = a_zoom;
  55. LMB = a_LMB;
  56. RMB = a_RMB;
  57. pitch = a_pitch;
  58. yaw = a_yaw;
  59. sidew_move_joystick_axis = a_sidew_move_joystick_axis;
  60. forw_move_joystick_axis = a_forw_move_joystick_axis;
  61. }
  62. bool up = false;
  63. bool down = false;
  64. bool left = false;
  65. bool right = false;
  66. bool jump = false;
  67. bool aux1 = false;
  68. bool sneak = false;
  69. bool zoom = false;
  70. bool LMB = false;
  71. bool RMB = false;
  72. float pitch = 0.0f;
  73. float yaw = 0.0f;
  74. float sidew_move_joystick_axis = 0.0f;
  75. float forw_move_joystick_axis = 0.0f;
  76. };
  77. struct PlayerSettings
  78. {
  79. bool free_move = false;
  80. bool fast_move = false;
  81. bool continuous_forward = false;
  82. bool always_fly_fast = false;
  83. bool aux1_descends = false;
  84. bool noclip = false;
  85. void readGlobalSettings();
  86. };
  87. class Map;
  88. struct CollisionInfo;
  89. struct HudElement;
  90. class Environment;
  91. class Player
  92. {
  93. public:
  94. Player(const char *name, IItemDefManager *idef);
  95. virtual ~Player() = 0;
  96. DISABLE_CLASS_COPY(Player);
  97. virtual void move(f32 dtime, Environment *env, f32 pos_max_d)
  98. {}
  99. virtual void move(f32 dtime, Environment *env, f32 pos_max_d,
  100. std::vector<CollisionInfo> *collision_info)
  101. {}
  102. const v3f &getSpeed() const
  103. {
  104. return m_speed;
  105. }
  106. void setSpeed(const v3f &speed)
  107. {
  108. m_speed = speed;
  109. }
  110. const char *getName() const { return m_name; }
  111. u32 getFreeHudID()
  112. {
  113. size_t size = hud.size();
  114. for (size_t i = 0; i != size; i++) {
  115. if (!hud[i])
  116. return i;
  117. }
  118. return size;
  119. }
  120. v3f eye_offset_first;
  121. v3f eye_offset_third;
  122. Inventory inventory;
  123. f32 movement_acceleration_default;
  124. f32 movement_acceleration_air;
  125. f32 movement_acceleration_fast;
  126. f32 movement_speed_walk;
  127. f32 movement_speed_crouch;
  128. f32 movement_speed_fast;
  129. f32 movement_speed_climb;
  130. f32 movement_speed_jump;
  131. f32 movement_liquid_fluidity;
  132. f32 movement_liquid_fluidity_smooth;
  133. f32 movement_liquid_sink;
  134. f32 movement_gravity;
  135. v2s32 local_animations[4];
  136. float local_animation_speed;
  137. std::string inventory_formspec;
  138. std::string formspec_prepend;
  139. PlayerControl control;
  140. const PlayerControl& getPlayerControl() { return control; }
  141. PlayerSettings &getPlayerSettings() { return m_player_settings; }
  142. static void settingsChangedCallback(const std::string &name, void *data);
  143. u32 keyPressed = 0;
  144. HudElement* getHud(u32 id);
  145. u32 addHud(HudElement* hud);
  146. HudElement* removeHud(u32 id);
  147. void clearHud();
  148. u32 hud_flags;
  149. s32 hud_hotbar_itemcount;
  150. protected:
  151. char m_name[PLAYERNAME_SIZE];
  152. v3f m_speed;
  153. std::vector<HudElement *> hud;
  154. private:
  155. // Protect some critical areas
  156. // hud for example can be modified by EmergeThread
  157. // and ServerThread
  158. std::mutex m_mutex;
  159. PlayerSettings m_player_settings;
  160. };