player.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 "threading/mutex_auto_lock.h"
  18. #include "util/numeric.h"
  19. #include "hud.h"
  20. #include "constants.h"
  21. #include "gamedef.h"
  22. #include "settings.h"
  23. #include "log.h"
  24. #include "porting.h" // strlcpy
  25. Player::Player(const char *name, IItemDefManager *idef):
  26. inventory(idef)
  27. {
  28. strlcpy(m_name, name, PLAYERNAME_SIZE);
  29. inventory.clear();
  30. inventory.addList("main", PLAYER_INVENTORY_SIZE);
  31. InventoryList *craft = inventory.addList("craft", 9);
  32. craft->setWidth(3);
  33. inventory.addList("craftpreview", 1);
  34. inventory.addList("craftresult", 1);
  35. inventory.setModified(false);
  36. // Can be redefined via Lua
  37. inventory_formspec = "size[8,7.5]"
  38. //"image[1,0.6;1,2;player.png]"
  39. "list[current_player;main;0,3.5;8,4;]"
  40. "list[current_player;craft;3,0;3,3;]"
  41. "listring[]"
  42. "list[current_player;craftpreview;7,1;1,1;]";
  43. // Initialize movement settings at default values, so movement can work
  44. // if the server fails to send them
  45. movement_acceleration_default = 3 * BS;
  46. movement_acceleration_air = 2 * BS;
  47. movement_acceleration_fast = 10 * BS;
  48. movement_speed_walk = 4 * BS;
  49. movement_speed_crouch = 1.35 * BS;
  50. movement_speed_fast = 20 * BS;
  51. movement_speed_climb = 2 * BS;
  52. movement_speed_jump = 6.5 * BS;
  53. movement_liquid_fluidity = 1 * BS;
  54. movement_liquid_fluidity_smooth = 0.5 * BS;
  55. movement_liquid_sink = 10 * BS;
  56. movement_gravity = 9.81 * BS;
  57. local_animation_speed = 0.0;
  58. hud_flags =
  59. HUD_FLAG_HOTBAR_VISIBLE | HUD_FLAG_HEALTHBAR_VISIBLE |
  60. HUD_FLAG_CROSSHAIR_VISIBLE | HUD_FLAG_WIELDITEM_VISIBLE |
  61. HUD_FLAG_BREATHBAR_VISIBLE | HUD_FLAG_MINIMAP_VISIBLE |
  62. HUD_FLAG_MINIMAP_RADAR_VISIBLE;
  63. hud_hotbar_itemcount = HUD_HOTBAR_ITEMCOUNT_DEFAULT;
  64. m_player_settings.readGlobalSettings();
  65. // Register player setting callbacks
  66. for (const std::string &name : m_player_settings.setting_names)
  67. g_settings->registerChangedCallback(name,
  68. &Player::settingsChangedCallback, &m_player_settings);
  69. }
  70. Player::~Player()
  71. {
  72. // m_player_settings becomes invalid, remove callbacks
  73. for (const std::string &name : m_player_settings.setting_names)
  74. g_settings->deregisterChangedCallback(name,
  75. &Player::settingsChangedCallback, &m_player_settings);
  76. clearHud();
  77. }
  78. void Player::setWieldIndex(u16 index)
  79. {
  80. const InventoryList *mlist = inventory.getList("main");
  81. m_wield_index = MYMIN(index, mlist ? mlist->getSize() : 0);
  82. }
  83. ItemStack &Player::getWieldedItem(ItemStack *selected, ItemStack *hand) const
  84. {
  85. assert(selected);
  86. const InventoryList *mlist = inventory.getList("main"); // TODO: Make this generic
  87. const InventoryList *hlist = inventory.getList("hand");
  88. if (mlist && m_wield_index < mlist->getSize())
  89. *selected = mlist->getItem(m_wield_index);
  90. if (hand && hlist)
  91. *hand = hlist->getItem(0);
  92. // Return effective tool item
  93. return (hand && selected->name.empty()) ? *hand : *selected;
  94. }
  95. u32 Player::addHud(HudElement *toadd)
  96. {
  97. MutexAutoLock lock(m_mutex);
  98. u32 id = getFreeHudID();
  99. if (id < hud.size())
  100. hud[id] = toadd;
  101. else
  102. hud.push_back(toadd);
  103. return id;
  104. }
  105. HudElement* Player::getHud(u32 id)
  106. {
  107. MutexAutoLock lock(m_mutex);
  108. if (id < hud.size())
  109. return hud[id];
  110. return NULL;
  111. }
  112. HudElement* Player::removeHud(u32 id)
  113. {
  114. MutexAutoLock lock(m_mutex);
  115. HudElement* retval = NULL;
  116. if (id < hud.size()) {
  117. retval = hud[id];
  118. hud[id] = NULL;
  119. }
  120. return retval;
  121. }
  122. void Player::clearHud()
  123. {
  124. MutexAutoLock lock(m_mutex);
  125. while(!hud.empty()) {
  126. delete hud.back();
  127. hud.pop_back();
  128. }
  129. }
  130. void PlayerSettings::readGlobalSettings()
  131. {
  132. free_move = g_settings->getBool("free_move");
  133. pitch_move = g_settings->getBool("pitch_move");
  134. fast_move = g_settings->getBool("fast_move");
  135. continuous_forward = g_settings->getBool("continuous_forward");
  136. always_fly_fast = g_settings->getBool("always_fly_fast");
  137. aux1_descends = g_settings->getBool("aux1_descends");
  138. noclip = g_settings->getBool("noclip");
  139. autojump = g_settings->getBool("autojump");
  140. }
  141. void Player::settingsChangedCallback(const std::string &name, void *data)
  142. {
  143. ((PlayerSettings *)data)->readGlobalSettings();
  144. }