]> git.lizzy.rs Git - minetest.git/blob - src/player.cpp
Dual wielding
[minetest.git] / src / player.cpp
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 <cmath>
23 #include "threading/mutex_auto_lock.h"
24 #include "util/numeric.h"
25 #include "hud.h"
26 #include "constants.h"
27 #include "gamedef.h"
28 #include "settings.h"
29 #include "log.h"
30 #include "porting.h"  // strlcpy
31
32
33 Player::Player(const char *name, IItemDefManager *idef):
34         inventory(idef)
35 {
36         strlcpy(m_name, name, PLAYERNAME_SIZE);
37
38         inventory.clear();
39         inventory.addList("main", PLAYER_INVENTORY_SIZE);
40         InventoryList *craft = inventory.addList("craft", 9);
41         craft->setWidth(3);
42         inventory.addList("craftpreview", 1);
43         inventory.addList("craftresult", 1);
44         inventory.setModified(false);
45
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;]"
51                 "listring[]"
52                 "list[current_player;craftpreview;7,1;1,1;]";
53
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;
69
70         hud_flags =
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   |
75                 HUD_FLAG_CHAT_VISIBLE;
76
77         hud_hotbar_itemcount = HUD_HOTBAR_ITEMCOUNT_DEFAULT;
78
79         m_player_settings.readGlobalSettings();
80         // Register player setting callbacks
81         for (const std::string &name : m_player_settings.setting_names)
82                 g_settings->registerChangedCallback(name,
83                         &Player::settingsChangedCallback, &m_player_settings);
84 }
85
86 Player::~Player()
87 {
88         // m_player_settings becomes invalid, remove callbacks
89         for (const std::string &name : m_player_settings.setting_names)
90                 g_settings->deregisterChangedCallback(name,
91                         &Player::settingsChangedCallback, &m_player_settings);
92         clearHud();
93 }
94
95 void Player::setWieldIndex(u16 index)
96 {
97         const InventoryList *mlist = inventory.getList("main");
98         m_wield_index = MYMIN(index, mlist ? mlist->getSize() : 0);
99 }
100
101 ItemStack &Player::getWieldedItem(ItemStack *selected, ItemStack *hand) const
102 {
103         assert(selected);
104
105         const InventoryList *mlist = inventory.getList("main"); // TODO: Make this generic
106         const InventoryList *hlist = inventory.getList("hand");
107
108         if (mlist && m_wield_index < mlist->getSize())
109                 *selected = mlist->getItem(m_wield_index);
110
111         if (hand && hlist)
112                 *hand = hlist->getItem(0);
113
114         // Return effective tool item
115         return (hand && selected->name.empty()) ? *hand : *selected;
116 }
117
118 bool Player::getOffhandWieldedItem(ItemStack *offhand, ItemStack *place, IItemDefManager *idef, const PointedThing &pointed) const
119 {
120         assert(offhand);
121
122         ItemStack main;
123
124         const InventoryList *mlist = inventory.getList("main");
125         const InventoryList *olist = inventory.getList("offhand");
126
127         if (olist)
128                 *offhand = olist->getItem(0);
129
130         if (mlist && m_wield_index < mlist->getSize())
131                 main = mlist->getItem(m_wield_index);
132
133         const ItemDefinition &main_def = main.getDefinition(idef);
134         const ItemDefinition &offhand_def = offhand->getDefinition(idef);
135         bool main_usable, offhand_usable;
136
137         // figure out which item to use for placements
138
139         if (pointed.type == POINTEDTHING_NODE) {
140                 // an item can be used on nodes if it has a place handler or prediction
141                 main_usable = main_def.has_on_place || main_def.node_placement_prediction != "";
142                 offhand_usable = offhand_def.has_on_place || offhand_def.node_placement_prediction != "";
143         } else {
144                 // an item can be used on anything else if it has a secondary use handler
145                 main_usable = main_def.has_on_secondary_use;
146                 offhand_usable = offhand_def.has_on_secondary_use;
147         }
148
149         // main hand has priority
150         bool use_offhand = offhand_usable && !main_usable;
151
152         if (place)
153                 *place = use_offhand ? *offhand : main;
154
155         return use_offhand;
156 }
157
158 u32 Player::addHud(HudElement *toadd)
159 {
160         MutexAutoLock lock(m_mutex);
161
162         u32 id = getFreeHudID();
163
164         if (id < hud.size())
165                 hud[id] = toadd;
166         else
167                 hud.push_back(toadd);
168
169         return id;
170 }
171
172 HudElement* Player::getHud(u32 id)
173 {
174         MutexAutoLock lock(m_mutex);
175
176         if (id < hud.size())
177                 return hud[id];
178
179         return NULL;
180 }
181
182 HudElement* Player::removeHud(u32 id)
183 {
184         MutexAutoLock lock(m_mutex);
185
186         HudElement* retval = NULL;
187         if (id < hud.size()) {
188                 retval = hud[id];
189                 hud[id] = NULL;
190         }
191         return retval;
192 }
193
194 void Player::clearHud()
195 {
196         MutexAutoLock lock(m_mutex);
197
198         while(!hud.empty()) {
199                 delete hud.back();
200                 hud.pop_back();
201         }
202 }
203
204 #ifndef SERVER
205
206 u32 PlayerControl::getKeysPressed() const
207 {
208         u32 keypress_bits =
209                 ( (u32)(jump  & 1) << 4) |
210                 ( (u32)(aux1  & 1) << 5) |
211                 ( (u32)(sneak & 1) << 6) |
212                 ( (u32)(dig   & 1) << 7) |
213                 ( (u32)(place & 1) << 8) |
214                 ( (u32)(zoom  & 1) << 9)
215         ;
216
217         // If any direction keys are pressed pass those through
218         if (direction_keys != 0)
219         {
220                 keypress_bits |= direction_keys;
221         }
222         // Otherwise set direction keys based on joystick movement (for mod compatibility)
223         else if (isMoving())
224         {
225                 float abs_d;
226
227                 // (absolute value indicates forward / backward)
228                 abs_d = abs(movement_direction);
229                 if (abs_d < 3.0f / 8.0f * M_PI)
230                         keypress_bits |= (u32)1; // Forward
231                 if (abs_d > 5.0f / 8.0f * M_PI)
232                         keypress_bits |= (u32)1 << 1; // Backward
233
234                 // rotate entire coordinate system by 90 degree
235                 abs_d = movement_direction + M_PI_2;
236                 if (abs_d >= M_PI)
237                         abs_d -= 2 * M_PI;
238                 abs_d = abs(abs_d);
239                 // (value now indicates left / right)
240                 if (abs_d < 3.0f / 8.0f * M_PI)
241                         keypress_bits |= (u32)1 << 2; // Left
242                 if (abs_d > 5.0f / 8.0f * M_PI)
243                         keypress_bits |= (u32)1 << 3; // Right
244         }
245
246         return keypress_bits;
247 }
248
249 #endif
250
251 void PlayerControl::unpackKeysPressed(u32 keypress_bits)
252 {
253         direction_keys = keypress_bits & 0xf;
254         jump  = keypress_bits & (1 << 4);
255         aux1  = keypress_bits & (1 << 5);
256         sneak = keypress_bits & (1 << 6);
257         dig   = keypress_bits & (1 << 7);
258         place = keypress_bits & (1 << 8);
259         zoom  = keypress_bits & (1 << 9);
260 }
261
262 void PlayerSettings::readGlobalSettings()
263 {
264         free_move = g_settings->getBool("free_move");
265         pitch_move = g_settings->getBool("pitch_move");
266         fast_move = g_settings->getBool("fast_move");
267         continuous_forward = g_settings->getBool("continuous_forward");
268         always_fly_fast = g_settings->getBool("always_fly_fast");
269         aux1_descends = g_settings->getBool("aux1_descends");
270         noclip = g_settings->getBool("noclip");
271         autojump = g_settings->getBool("autojump");
272 }
273
274 void Player::settingsChangedCallback(const std::string &name, void *data)
275 {
276         ((PlayerSettings *)data)->readGlobalSettings();
277 }