Merge pull request #482 from proller/liquid
[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 #include "constants.h"
22 #include "gamedef.h"
23 #include "connection.h" // PEER_ID_INEXISTENT
24 #include "settings.h"
25 #include "content_sao.h"
26 #include "util/numeric.h"
27
28 Player::Player(IGameDef *gamedef):
29         touching_ground(false),
30         in_liquid(false),
31         in_liquid_stable(false),
32         liquid_viscosity(0),
33         is_climbing(false),
34         swimming_vertical(false),
35         camera_barely_in_ceiling(false),
36         inventory(gamedef->idef()),
37         hp(PLAYER_MAX_HP),
38         peer_id(PEER_ID_INEXISTENT),
39 // protected
40         m_gamedef(gamedef),
41         m_pitch(0),
42         m_yaw(0),
43         m_speed(0,0,0),
44         m_position(0,0,0)
45 {
46         updateName("<not set>");
47         inventory.clear();
48         inventory.addList("main", PLAYER_INVENTORY_SIZE);
49         InventoryList *craft = inventory.addList("craft", 9);
50         craft->setWidth(3);
51         inventory.addList("craftpreview", 1);
52         inventory.addList("craftresult", 1);
53
54         // Can be redefined via Lua
55         inventory_formspec =  "size[8,7.5]"
56                 //"image[1,0.6;1,2;player.png]"
57                 "list[current_player;main;0,3.5;8,4;]"
58                 "list[current_player;craft;3,0;3,3;]"
59                 "list[current_player;craftpreview;7,1;1,1;]";
60
61         // Initialize movement settings at default values, so movement can work if the server fails to send them
62         movement_acceleration_default = 3 * BS;
63         movement_acceleration_air = 2 * BS;
64         movement_acceleration_fast = 10 * BS;
65         movement_speed_walk = 4 * BS;
66         movement_speed_crouch = 1.35 * BS;
67         movement_speed_fast = 20 * BS;
68         movement_speed_climb = 2 * BS;
69         movement_speed_jump = 6.5 * BS;
70         movement_liquid_fluidity = 1 * BS;
71         movement_liquid_fluidity_smooth = 0.5 * BS;
72         movement_liquid_sink = 10 * BS;
73         movement_gravity = 9.81 * BS;
74 }
75
76 Player::~Player()
77 {
78 }
79
80 // Horizontal acceleration (X and Z), Y direction is ignored
81 void Player::accelerateHorizontal(v3f target_speed, f32 max_increase)
82 {
83         if(max_increase == 0)
84                 return;
85
86         v3f d_wanted = target_speed - m_speed;
87         d_wanted.Y = 0;
88         f32 dl = d_wanted.getLength();
89         if(dl > max_increase)
90                 dl = max_increase;
91         
92         v3f d = d_wanted.normalize() * dl;
93
94         m_speed.X += d.X;
95         m_speed.Z += d.Z;
96
97 #if 0 // old code
98         if(m_speed.X < target_speed.X - max_increase)
99                 m_speed.X += max_increase;
100         else if(m_speed.X > target_speed.X + max_increase)
101                 m_speed.X -= max_increase;
102         else if(m_speed.X < target_speed.X)
103                 m_speed.X = target_speed.X;
104         else if(m_speed.X > target_speed.X)
105                 m_speed.X = target_speed.X;
106
107         if(m_speed.Z < target_speed.Z - max_increase)
108                 m_speed.Z += max_increase;
109         else if(m_speed.Z > target_speed.Z + max_increase)
110                 m_speed.Z -= max_increase;
111         else if(m_speed.Z < target_speed.Z)
112                 m_speed.Z = target_speed.Z;
113         else if(m_speed.Z > target_speed.Z)
114                 m_speed.Z = target_speed.Z;
115 #endif
116 }
117
118 // Vertical acceleration (Y), X and Z directions are ignored
119 void Player::accelerateVertical(v3f target_speed, f32 max_increase)
120 {
121         if(max_increase == 0)
122                 return;
123
124         f32 d_wanted = target_speed.Y - m_speed.Y;
125         if(d_wanted > max_increase)
126                 d_wanted = max_increase;
127         else if(d_wanted < -max_increase)
128                 d_wanted = -max_increase;
129
130         m_speed.Y += d_wanted;
131
132 #if 0 // old code
133         if(m_speed.Y < target_speed.Y - max_increase)
134                 m_speed.Y += max_increase;
135         else if(m_speed.Y > target_speed.Y + max_increase)
136                 m_speed.Y -= max_increase;
137         else if(m_speed.Y < target_speed.Y)
138                 m_speed.Y = target_speed.Y;
139         else if(m_speed.Y > target_speed.Y)
140                 m_speed.Y = target_speed.Y;
141 #endif
142 }
143
144 v3s16 Player::getLightPosition() const
145 {
146         return floatToInt(m_position + v3f(0,BS+BS/2,0), BS);
147 }
148
149 void Player::serialize(std::ostream &os)
150 {
151         // Utilize a Settings object for storing values
152         Settings args;
153         args.setS32("version", 1);
154         args.set("name", m_name);
155         //args.set("password", m_password);
156         args.setFloat("pitch", m_pitch);
157         args.setFloat("yaw", m_yaw);
158         args.setV3F("position", m_position);
159         args.setS32("hp", hp);
160
161         args.writeLines(os);
162
163         os<<"PlayerArgsEnd\n";
164         
165         inventory.serialize(os);
166 }
167
168 void Player::deSerialize(std::istream &is)
169 {
170         Settings args;
171         
172         for(;;)
173         {
174                 if(is.eof())
175                         throw SerializationError
176                                         ("Player::deSerialize(): PlayerArgsEnd not found");
177                 std::string line;
178                 std::getline(is, line);
179                 std::string trimmedline = trim(line);
180                 if(trimmedline == "PlayerArgsEnd")
181                         break;
182                 args.parseConfigLine(line);
183         }
184
185         //args.getS32("version"); // Version field value not used
186         std::string name = args.get("name");
187         updateName(name.c_str());
188         setPitch(args.getFloat("pitch"));
189         setYaw(args.getFloat("yaw"));
190         setPosition(args.getV3F("position"));
191         try{
192                 hp = args.getS32("hp");
193         }catch(SettingNotFoundException &e){
194                 hp = 20;
195         }
196
197         inventory.deSerialize(is);
198
199         if(inventory.getList("craftpreview") == NULL)
200         {
201                 // Convert players without craftpreview
202                 inventory.addList("craftpreview", 1);
203
204                 bool craftresult_is_preview = true;
205                 if(args.exists("craftresult_is_preview"))
206                         craftresult_is_preview = args.getBool("craftresult_is_preview");
207                 if(craftresult_is_preview)
208                 {
209                         // Clear craftresult
210                         inventory.getList("craftresult")->changeItem(0, ItemStack());
211                 }
212         }
213 }
214
215 /*
216         RemotePlayer
217 */
218
219
220
221
222
223 void RemotePlayer::setPosition(const v3f &position)
224 {
225         Player::setPosition(position);
226         if(m_sao)
227                 m_sao->setBasePosition(position);
228 }