Minimap: Add new HUD flag for minimap radar mode
[oweals/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 "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
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         inventory.addList("hand", 1);
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;
75
76         hud_hotbar_itemcount = HUD_HOTBAR_ITEMCOUNT_DEFAULT;
77 }
78
79 Player::~Player()
80 {
81         clearHud();
82 }
83
84 u32 Player::addHud(HudElement *toadd)
85 {
86         MutexAutoLock lock(m_mutex);
87
88         u32 id = getFreeHudID();
89
90         if (id < hud.size())
91                 hud[id] = toadd;
92         else
93                 hud.push_back(toadd);
94
95         return id;
96 }
97
98 HudElement* Player::getHud(u32 id)
99 {
100         MutexAutoLock lock(m_mutex);
101
102         if (id < hud.size())
103                 return hud[id];
104
105         return NULL;
106 }
107
108 HudElement* Player::removeHud(u32 id)
109 {
110         MutexAutoLock lock(m_mutex);
111
112         HudElement* retval = NULL;
113         if (id < hud.size()) {
114                 retval = hud[id];
115                 hud[id] = NULL;
116         }
117         return retval;
118 }
119
120 void Player::clearHud()
121 {
122         MutexAutoLock lock(m_mutex);
123
124         while(!hud.empty()) {
125                 delete hud.back();
126                 hud.pop_back();
127         }
128 }