Move RemotePlayer code to its own cpp/header
[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         camera_barely_in_ceiling(false),
34         inventory(idef),
35         hp(PLAYER_MAX_HP),
36         peer_id(PEER_ID_INEXISTENT),
37         keyPressed(0),
38 // protected
39         m_breath(PLAYER_MAX_BREATH),
40         m_pitch(0),
41         m_yaw(0),
42         m_speed(0,0,0),
43         m_position(0,0,0),
44         m_collisionbox(-BS*0.30,0.0,-BS*0.30,BS*0.30,BS*1.75,BS*0.30)
45 {
46         strlcpy(m_name, name, PLAYERNAME_SIZE);
47
48         inventory.clear();
49         inventory.addList("main", PLAYER_INVENTORY_SIZE);
50         InventoryList *craft = inventory.addList("craft", 9);
51         craft->setWidth(3);
52         inventory.addList("craftpreview", 1);
53         inventory.addList("craftresult", 1);
54         inventory.setModified(false);
55
56         // Can be redefined via Lua
57         inventory_formspec = "size[8,7.5]"
58                 //"image[1,0.6;1,2;player.png]"
59                 "list[current_player;main;0,3.5;8,4;]"
60                 "list[current_player;craft;3,0;3,3;]"
61                 "listring[]"
62                 "list[current_player;craftpreview;7,1;1,1;]";
63
64         // Initialize movement settings at default values, so movement can work
65         // if the server fails to send them
66         movement_acceleration_default   = 3    * BS;
67         movement_acceleration_air       = 2    * BS;
68         movement_acceleration_fast      = 10   * BS;
69         movement_speed_walk             = 4    * BS;
70         movement_speed_crouch           = 1.35 * BS;
71         movement_speed_fast             = 20   * BS;
72         movement_speed_climb            = 2    * BS;
73         movement_speed_jump             = 6.5  * BS;
74         movement_liquid_fluidity        = 1    * BS;
75         movement_liquid_fluidity_smooth = 0.5  * BS;
76         movement_liquid_sink            = 10   * BS;
77         movement_gravity                = 9.81 * BS;
78         local_animation_speed           = 0.0;
79
80         hud_flags =
81                 HUD_FLAG_HOTBAR_VISIBLE    | HUD_FLAG_HEALTHBAR_VISIBLE |
82                 HUD_FLAG_CROSSHAIR_VISIBLE | HUD_FLAG_WIELDITEM_VISIBLE |
83                 HUD_FLAG_BREATHBAR_VISIBLE | HUD_FLAG_MINIMAP_VISIBLE;
84
85         hud_hotbar_itemcount = HUD_HOTBAR_ITEMCOUNT_DEFAULT;
86 }
87
88 Player::~Player()
89 {
90         clearHud();
91 }
92
93 v3s16 Player::getLightPosition() const
94 {
95         return floatToInt(m_position + v3f(0,BS+BS/2,0), BS);
96 }
97
98 u32 Player::addHud(HudElement *toadd)
99 {
100         MutexAutoLock lock(m_mutex);
101
102         u32 id = getFreeHudID();
103
104         if (id < hud.size())
105                 hud[id] = toadd;
106         else
107                 hud.push_back(toadd);
108
109         return id;
110 }
111
112 HudElement* Player::getHud(u32 id)
113 {
114         MutexAutoLock lock(m_mutex);
115
116         if (id < hud.size())
117                 return hud[id];
118
119         return NULL;
120 }
121
122 HudElement* Player::removeHud(u32 id)
123 {
124         MutexAutoLock lock(m_mutex);
125
126         HudElement* retval = NULL;
127         if (id < hud.size()) {
128                 retval = hud[id];
129                 hud[id] = NULL;
130         }
131         return retval;
132 }
133
134 void Player::clearHud()
135 {
136         MutexAutoLock lock(m_mutex);
137
138         while(!hud.empty()) {
139                 delete hud.back();
140                 hud.pop_back();
141         }
142 }