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