3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "threading/mutex_auto_lock.h"
24 #include "util/numeric.h"
26 #include "constants.h"
30 #include "porting.h" // strlcpy
33 Player::Player(const char *name, IItemDefManager *idef):
36 strlcpy(m_name, name, PLAYERNAME_SIZE);
39 inventory.addList("main", PLAYER_INVENTORY_SIZE);
40 InventoryList *craft = inventory.addList("craft", 9);
42 inventory.addList("craftpreview", 1);
43 inventory.addList("craftresult", 1);
44 inventory.setModified(false);
46 // Can be redefined via Lua
47 inventory_formspec = "size[8,7.5]"
48 //"image[1,0.6;1,2;player.png]"
49 "list[current_player;main;0,3.5;8,4;]"
50 "list[current_player;craft;3,0;3,3;]"
52 "list[current_player;craftpreview;7,1;1,1;]";
54 // Initialize movement settings at default values, so movement can work
55 // if the server fails to send them
56 movement_acceleration_default = 3 * BS;
57 movement_acceleration_air = 2 * BS;
58 movement_acceleration_fast = 10 * BS;
59 movement_speed_walk = 4 * BS;
60 movement_speed_crouch = 1.35 * BS;
61 movement_speed_fast = 20 * BS;
62 movement_speed_climb = 2 * BS;
63 movement_speed_jump = 6.5 * BS;
64 movement_liquid_fluidity = 1 * BS;
65 movement_liquid_fluidity_smooth = 0.5 * BS;
66 movement_liquid_sink = 10 * BS;
67 movement_gravity = 9.81 * BS;
68 local_animation_speed = 0.0;
71 HUD_FLAG_HOTBAR_VISIBLE | HUD_FLAG_HEALTHBAR_VISIBLE |
72 HUD_FLAG_CROSSHAIR_VISIBLE | HUD_FLAG_WIELDITEM_VISIBLE |
73 HUD_FLAG_BREATHBAR_VISIBLE | HUD_FLAG_MINIMAP_VISIBLE |
74 HUD_FLAG_MINIMAP_RADAR_VISIBLE | HUD_FLAG_BASIC_DEBUG;
76 hud_hotbar_itemcount = HUD_HOTBAR_ITEMCOUNT_DEFAULT;
78 m_player_settings.readGlobalSettings();
79 // Register player setting callbacks
80 for (const std::string &name : m_player_settings.setting_names)
81 g_settings->registerChangedCallback(name,
82 &Player::settingsChangedCallback, &m_player_settings);
87 // m_player_settings becomes invalid, remove callbacks
88 for (const std::string &name : m_player_settings.setting_names)
89 g_settings->deregisterChangedCallback(name,
90 &Player::settingsChangedCallback, &m_player_settings);
94 void Player::setWieldIndex(u16 index)
96 const InventoryList *mlist = inventory.getList("main");
97 m_wield_index = MYMIN(index, mlist ? mlist->getSize() : 0);
100 ItemStack &Player::getWieldedItem(ItemStack *selected, ItemStack *hand) const
104 const InventoryList *mlist = inventory.getList("main"); // TODO: Make this generic
105 const InventoryList *hlist = inventory.getList("hand");
107 if (mlist && m_wield_index < mlist->getSize())
108 *selected = mlist->getItem(m_wield_index);
111 *hand = hlist->getItem(0);
113 // Return effective tool item
114 return (hand && selected->name.empty()) ? *hand : *selected;
117 u32 Player::addHud(HudElement *toadd)
119 MutexAutoLock lock(m_mutex);
121 u32 id = getFreeHudID();
126 hud.push_back(toadd);
131 HudElement* Player::getHud(u32 id)
133 MutexAutoLock lock(m_mutex);
141 HudElement* Player::removeHud(u32 id)
143 MutexAutoLock lock(m_mutex);
145 HudElement* retval = NULL;
146 if (id < hud.size()) {
153 void Player::clearHud()
155 MutexAutoLock lock(m_mutex);
157 while(!hud.empty()) {
165 u32 PlayerControl::getKeysPressed() const
168 ( (u32)(jump & 1) << 4) |
169 ( (u32)(aux1 & 1) << 5) |
170 ( (u32)(sneak & 1) << 6) |
171 ( (u32)(dig & 1) << 7) |
172 ( (u32)(place & 1) << 8) |
173 ( (u32)(zoom & 1) << 9)
176 // If any direction keys are pressed pass those through
177 if (direction_keys != 0)
179 keypress_bits |= direction_keys;
181 // Otherwise set direction keys based on joystick movement (for mod compatibility)
186 // (absolute value indicates forward / backward)
187 abs_d = abs(movement_direction);
188 if (abs_d < 3.0f / 8.0f * M_PI)
189 keypress_bits |= (u32)1; // Forward
190 if (abs_d > 5.0f / 8.0f * M_PI)
191 keypress_bits |= (u32)1 << 1; // Backward
193 // rotate entire coordinate system by 90 degree
194 abs_d = movement_direction + M_PI_2;
198 // (value now indicates left / right)
199 if (abs_d < 3.0f / 8.0f * M_PI)
200 keypress_bits |= (u32)1 << 2; // Left
201 if (abs_d > 5.0f / 8.0f * M_PI)
202 keypress_bits |= (u32)1 << 3; // Right
205 return keypress_bits;
210 void PlayerControl::unpackKeysPressed(u32 keypress_bits)
212 direction_keys = keypress_bits & 0xf;
213 jump = keypress_bits & (1 << 4);
214 aux1 = keypress_bits & (1 << 5);
215 sneak = keypress_bits & (1 << 6);
216 dig = keypress_bits & (1 << 7);
217 place = keypress_bits & (1 << 8);
218 zoom = keypress_bits & (1 << 9);
221 void PlayerSettings::readGlobalSettings()
223 freecam = g_settings->getBool("freecam");
224 free_move = g_settings->getBool("free_move") || freecam;
225 pitch_move = g_settings->getBool("pitch_move");
226 fast_move = g_settings->getBool("fast_move") || freecam;
227 continuous_forward = g_settings->getBool("continuous_forward");
228 always_fly_fast = g_settings->getBool("always_fly_fast") || freecam;
229 aux1_descends = g_settings->getBool("aux1_descends");
230 noclip = g_settings->getBool("noclip") || freecam;
231 autojump = g_settings->getBool("autojump");
234 void Player::settingsChangedCallback(const std::string &name, void *data)
236 ((PlayerSettings *)data)->readGlobalSettings();