player.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. inventory.addList("hand", 1);
  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;
  64. hud_hotbar_itemcount = HUD_HOTBAR_ITEMCOUNT_DEFAULT;
  65. }
  66. Player::~Player()
  67. {
  68. clearHud();
  69. }
  70. u32 Player::addHud(HudElement *toadd)
  71. {
  72. MutexAutoLock lock(m_mutex);
  73. u32 id = getFreeHudID();
  74. if (id < hud.size())
  75. hud[id] = toadd;
  76. else
  77. hud.push_back(toadd);
  78. return id;
  79. }
  80. HudElement* Player::getHud(u32 id)
  81. {
  82. MutexAutoLock lock(m_mutex);
  83. if (id < hud.size())
  84. return hud[id];
  85. return NULL;
  86. }
  87. HudElement* Player::removeHud(u32 id)
  88. {
  89. MutexAutoLock lock(m_mutex);
  90. HudElement* retval = NULL;
  91. if (id < hud.size()) {
  92. retval = hud[id];
  93. hud[id] = NULL;
  94. }
  95. return retval;
  96. }
  97. void Player::clearHud()
  98. {
  99. MutexAutoLock lock(m_mutex);
  100. while(!hud.empty()) {
  101. delete hud.back();
  102. hud.pop_back();
  103. }
  104. }