player.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. #include "player.h"
  17. #include <cmath>
  18. #include "threading/mutex_auto_lock.h"
  19. #include "util/numeric.h"
  20. #include "hud.h"
  21. #include "constants.h"
  22. #include "gamedef.h"
  23. #include "settings.h"
  24. #include "log.h"
  25. #include "porting.h" // strlcpy
  26. Player::Player(const char *name, IItemDefManager *idef):
  27. inventory(idef)
  28. {
  29. strlcpy(m_name, name, PLAYERNAME_SIZE);
  30. inventory.clear();
  31. inventory.addList("main", PLAYER_INVENTORY_SIZE);
  32. InventoryList *craft = inventory.addList("craft", 9);
  33. craft->setWidth(3);
  34. inventory.addList("craftpreview", 1);
  35. inventory.addList("craftresult", 1);
  36. inventory.setModified(false);
  37. // Can be redefined via Lua
  38. inventory_formspec = "size[8,7.5]"
  39. //"image[1,0.6;1,2;player.png]"
  40. "list[current_player;main;0,3.5;8,4;]"
  41. "list[current_player;craft;3,0;3,3;]"
  42. "listring[]"
  43. "list[current_player;craftpreview;7,1;1,1;]";
  44. // Initialize movement settings at default values, so movement can work
  45. // if the server fails to send them
  46. movement_acceleration_default = 3 * BS;
  47. movement_acceleration_air = 2 * BS;
  48. movement_acceleration_fast = 10 * BS;
  49. movement_speed_walk = 4 * BS;
  50. movement_speed_crouch = 1.35 * BS;
  51. movement_speed_fast = 20 * BS;
  52. movement_speed_climb = 2 * BS;
  53. movement_speed_jump = 6.5 * BS;
  54. movement_liquid_fluidity = 1 * BS;
  55. movement_liquid_fluidity_smooth = 0.5 * BS;
  56. movement_liquid_sink = 10 * BS;
  57. movement_gravity = 9.81 * BS;
  58. local_animation_speed = 0.0;
  59. hud_flags =
  60. HUD_FLAG_HOTBAR_VISIBLE | HUD_FLAG_HEALTHBAR_VISIBLE |
  61. HUD_FLAG_CROSSHAIR_VISIBLE | HUD_FLAG_WIELDITEM_VISIBLE |
  62. HUD_FLAG_BREATHBAR_VISIBLE | HUD_FLAG_MINIMAP_VISIBLE |
  63. HUD_FLAG_MINIMAP_RADAR_VISIBLE | HUD_FLAG_BASIC_DEBUG |
  64. HUD_FLAG_CHAT_VISIBLE;
  65. hud_hotbar_itemcount = HUD_HOTBAR_ITEMCOUNT_DEFAULT;
  66. }
  67. Player::~Player()
  68. {
  69. clearHud();
  70. }
  71. void Player::setWieldIndex(u16 index)
  72. {
  73. const InventoryList *mlist = inventory.getList("main");
  74. m_wield_index = MYMIN(index, mlist ? mlist->getSize() : 0);
  75. }
  76. ItemStack &Player::getWieldedItem(ItemStack *selected, ItemStack *hand) const
  77. {
  78. assert(selected);
  79. const InventoryList *mlist = inventory.getList("main"); // TODO: Make this generic
  80. const InventoryList *hlist = inventory.getList("hand");
  81. if (mlist && m_wield_index < mlist->getSize())
  82. *selected = mlist->getItem(m_wield_index);
  83. if (hand && hlist)
  84. *hand = hlist->getItem(0);
  85. // Return effective tool item
  86. return (hand && selected->name.empty()) ? *hand : *selected;
  87. }
  88. u32 Player::addHud(HudElement *toadd)
  89. {
  90. MutexAutoLock lock(m_mutex);
  91. u32 id = getFreeHudID();
  92. if (id < hud.size())
  93. hud[id] = toadd;
  94. else
  95. hud.push_back(toadd);
  96. return id;
  97. }
  98. HudElement* Player::getHud(u32 id)
  99. {
  100. MutexAutoLock lock(m_mutex);
  101. if (id < hud.size())
  102. return hud[id];
  103. return NULL;
  104. }
  105. void Player::hudApply(std::function<void(const std::vector<HudElement*>&)> f)
  106. {
  107. MutexAutoLock lock(m_mutex);
  108. f(hud);
  109. }
  110. HudElement* Player::removeHud(u32 id)
  111. {
  112. MutexAutoLock lock(m_mutex);
  113. HudElement* retval = NULL;
  114. if (id < hud.size()) {
  115. retval = hud[id];
  116. hud[id] = NULL;
  117. }
  118. return retval;
  119. }
  120. void Player::clearHud()
  121. {
  122. MutexAutoLock lock(m_mutex);
  123. while(!hud.empty()) {
  124. delete hud.back();
  125. hud.pop_back();
  126. }
  127. }
  128. #ifndef SERVER
  129. u32 PlayerControl::getKeysPressed() const
  130. {
  131. u32 keypress_bits =
  132. ( (u32)(jump & 1) << 4) |
  133. ( (u32)(aux1 & 1) << 5) |
  134. ( (u32)(sneak & 1) << 6) |
  135. ( (u32)(dig & 1) << 7) |
  136. ( (u32)(place & 1) << 8) |
  137. ( (u32)(zoom & 1) << 9)
  138. ;
  139. // If any direction keys are pressed pass those through
  140. if (direction_keys != 0)
  141. {
  142. keypress_bits |= direction_keys;
  143. }
  144. // Otherwise set direction keys based on joystick movement (for mod compatibility)
  145. else if (isMoving())
  146. {
  147. float abs_d;
  148. // (absolute value indicates forward / backward)
  149. abs_d = std::abs(movement_direction);
  150. if (abs_d < 3.0f / 8.0f * M_PI)
  151. keypress_bits |= (u32)1; // Forward
  152. if (abs_d > 5.0f / 8.0f * M_PI)
  153. keypress_bits |= (u32)1 << 1; // Backward
  154. // rotate entire coordinate system by 90 degree
  155. abs_d = movement_direction + M_PI_2;
  156. if (abs_d >= M_PI)
  157. abs_d -= 2 * M_PI;
  158. abs_d = std::abs(abs_d);
  159. // (value now indicates left / right)
  160. if (abs_d < 3.0f / 8.0f * M_PI)
  161. keypress_bits |= (u32)1 << 2; // Left
  162. if (abs_d > 5.0f / 8.0f * M_PI)
  163. keypress_bits |= (u32)1 << 3; // Right
  164. }
  165. return keypress_bits;
  166. }
  167. #endif
  168. void PlayerControl::unpackKeysPressed(u32 keypress_bits)
  169. {
  170. direction_keys = keypress_bits & 0xf;
  171. jump = keypress_bits & (1 << 4);
  172. aux1 = keypress_bits & (1 << 5);
  173. sneak = keypress_bits & (1 << 6);
  174. dig = keypress_bits & (1 << 7);
  175. place = keypress_bits & (1 << 8);
  176. zoom = keypress_bits & (1 << 9);
  177. }