Fixed a few problems in the ladder update, and changed the speed to account for gravity
[oweals/minetest.git] / src / player.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
14
15 You should have received a copy of the GNU 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 "map.h"
22 #include "connection.h"
23 #include "constants.h"
24 #include "utility.h"
25
26
27 Player::Player():
28         touching_ground(false),
29         in_water(false),
30         in_water_stable(false),
31         swimming_up(false),
32         inventory_backup(NULL),
33         craftresult_is_preview(true),
34         hp(20),
35         peer_id(PEER_ID_INEXISTENT),
36         m_pitch(0),
37         m_yaw(0),
38         m_speed(0,0,0),
39         m_position(0,0,0)
40 {
41         updateName("<not set>");
42         resetInventory();
43 }
44
45 Player::~Player()
46 {
47         delete inventory_backup;
48 }
49
50 void Player::resetInventory()
51 {
52         inventory.clear();
53         inventory.addList("main", PLAYER_INVENTORY_SIZE);
54         inventory.addList("craft", 9);
55         inventory.addList("craftresult", 1);
56 }
57
58 // Y direction is ignored
59 void Player::accelerate(v3f target_speed, f32 max_increase)
60 {
61         v3f d_wanted = target_speed - m_speed;
62         d_wanted.Y = 0;
63         f32 dl_wanted = d_wanted.getLength();
64         f32 dl = dl_wanted;
65         if(dl > max_increase)
66                 dl = max_increase;
67         
68         v3f d = d_wanted.normalize() * dl;
69
70         m_speed.X += d.X;
71         m_speed.Z += d.Z;
72         //m_speed += d;
73
74 #if 0 // old code
75         if(m_speed.X < target_speed.X - max_increase)
76                 m_speed.X += max_increase;
77         else if(m_speed.X > target_speed.X + max_increase)
78                 m_speed.X -= max_increase;
79         else if(m_speed.X < target_speed.X)
80                 m_speed.X = target_speed.X;
81         else if(m_speed.X > target_speed.X)
82                 m_speed.X = target_speed.X;
83
84         if(m_speed.Z < target_speed.Z - max_increase)
85                 m_speed.Z += max_increase;
86         else if(m_speed.Z > target_speed.Z + max_increase)
87                 m_speed.Z -= max_increase;
88         else if(m_speed.Z < target_speed.Z)
89                 m_speed.Z = target_speed.Z;
90         else if(m_speed.Z > target_speed.Z)
91                 m_speed.Z = target_speed.Z;
92 #endif
93 }
94
95 void Player::serialize(std::ostream &os)
96 {
97         // Utilize a Settings object for storing values
98         Settings args;
99         args.setS32("version", 1);
100         args.set("name", m_name);
101         //args.set("password", m_password);
102         args.setFloat("pitch", m_pitch);
103         args.setFloat("yaw", m_yaw);
104         args.setV3F("position", m_position);
105         args.setBool("craftresult_is_preview", craftresult_is_preview);
106         args.setS32("hp", hp);
107
108         args.writeLines(os);
109
110         os<<"PlayerArgsEnd\n";
111         
112         // If actual inventory is backed up due to creative mode, save it
113         // instead of the dummy creative mode inventory
114         if(inventory_backup)
115                 inventory_backup->serialize(os);
116         else
117                 inventory.serialize(os);
118 }
119
120 void Player::deSerialize(std::istream &is)
121 {
122         Settings args;
123         
124         for(;;)
125         {
126                 if(is.eof())
127                         throw SerializationError
128                                         ("Player::deSerialize(): PlayerArgsEnd not found");
129                 std::string line;
130                 std::getline(is, line);
131                 std::string trimmedline = trim(line);
132                 if(trimmedline == "PlayerArgsEnd")
133                         break;
134                 args.parseConfigLine(line);
135         }
136
137         //args.getS32("version");
138         std::string name = args.get("name");
139         updateName(name.c_str());
140         /*std::string password = "";
141         if(args.exists("password"))
142                 password = args.get("password");
143         updatePassword(password.c_str());*/
144         m_pitch = args.getFloat("pitch");
145         m_yaw = args.getFloat("yaw");
146         m_position = args.getV3F("position");
147         try{
148                 craftresult_is_preview = args.getBool("craftresult_is_preview");
149         }catch(SettingNotFoundException &e){
150                 craftresult_is_preview = true;
151         }
152         try{
153                 hp = args.getS32("hp");
154         }catch(SettingNotFoundException &e){
155                 hp = 20;
156         }
157         /*try{
158                 std::string sprivs = args.get("privs");
159                 if(sprivs == "all")
160                 {
161                         privs = PRIV_ALL;
162                 }
163                 else
164                 {
165                         std::istringstream ss(sprivs);
166                         ss>>privs;
167                 }
168         }catch(SettingNotFoundException &e){
169                 privs = PRIV_DEFAULT;
170         }*/
171
172         inventory.deSerialize(is);
173 }
174
175 /*
176         RemotePlayer
177 */
178
179 #ifndef SERVER
180
181 RemotePlayer::RemotePlayer(
182                 scene::ISceneNode* parent,
183                 IrrlichtDevice *device,
184                 s32 id):
185         scene::ISceneNode(parent, (device==NULL)?NULL:device->getSceneManager(), id),
186         m_text(NULL)
187 {
188         m_box = core::aabbox3d<f32>(-BS/2,0,-BS/2,BS/2,BS*2,BS/2);
189
190         if(parent != NULL && device != NULL)
191         {
192                 // ISceneNode stores a member called SceneManager
193                 scene::ISceneManager* mgr = SceneManager;
194                 video::IVideoDriver* driver = mgr->getVideoDriver();
195                 gui::IGUIEnvironment* gui = device->getGUIEnvironment();
196
197                 // Add a text node for showing the name
198                 wchar_t wname[1] = {0};
199                 m_text = mgr->addTextSceneNode(gui->getBuiltInFont(),
200                                 wname, video::SColor(255,255,255,255), this);
201                 m_text->setPosition(v3f(0, (f32)BS*2.1, 0));
202
203                 // Attach a simple mesh to the player for showing an image
204                 scene::SMesh *mesh = new scene::SMesh();
205                 { // Front
206                 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
207                 video::SColor c(255,255,255,255);
208                 video::S3DVertex vertices[4] =
209                 {
210                         video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
211                         video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
212                         video::S3DVertex(BS/2,BS*2,0, 0,0,0, c, 1,0),
213                         video::S3DVertex(-BS/2,BS*2,0, 0,0,0, c, 0,0),
214                 };
215                 u16 indices[] = {0,1,2,2,3,0};
216                 buf->append(vertices, 4, indices, 6);
217                 // Set material
218                 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
219                 //buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
220                 buf->getMaterial().setTexture(0, driver->getTexture(getTexturePath("player.png").c_str()));
221                 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
222                 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
223                 //buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
224                 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
225                 // Add to mesh
226                 mesh->addMeshBuffer(buf);
227                 buf->drop();
228                 }
229                 { // Back
230                 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
231                 video::SColor c(255,255,255,255);
232                 video::S3DVertex vertices[4] =
233                 {
234                         video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
235                         video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
236                         video::S3DVertex(-BS/2,BS*2,0, 0,0,0, c, 0,0),
237                         video::S3DVertex(BS/2,BS*2,0, 0,0,0, c, 1,0),
238                 };
239                 u16 indices[] = {0,1,2,2,3,0};
240                 buf->append(vertices, 4, indices, 6);
241                 // Set material
242                 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
243                 //buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
244                 buf->getMaterial().setTexture(0, driver->getTexture(getTexturePath("player_back.png").c_str()));
245                 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
246                 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
247                 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
248                 // Add to mesh
249                 mesh->addMeshBuffer(buf);
250                 buf->drop();
251                 }
252                 m_node = mgr->addMeshSceneNode(mesh, this);
253                 mesh->drop();
254                 m_node->setPosition(v3f(0,0,0));
255         }
256 }
257
258 RemotePlayer::~RemotePlayer()
259 {
260         if(SceneManager != NULL)
261                 ISceneNode::remove();
262 }
263
264 void RemotePlayer::updateName(const char *name)
265 {
266         Player::updateName(name);
267         if(m_text != NULL)
268         {
269                 wchar_t wname[PLAYERNAME_SIZE];
270                 mbstowcs(wname, m_name, strlen(m_name)+1);
271                 m_text->setText(wname);
272         }
273 }
274
275 void RemotePlayer::move(f32 dtime, Map &map, f32 pos_max_d)
276 {
277         m_pos_animation_time_counter += dtime;
278         m_pos_animation_counter += dtime;
279         v3f movevector = m_position - m_oldpos;
280         f32 moveratio;
281         if(m_pos_animation_time < 0.001)
282                 moveratio = 1.0;
283         else
284                 moveratio = m_pos_animation_counter / m_pos_animation_time;
285         if(moveratio > 1.5)
286                 moveratio = 1.5;
287         m_showpos = m_oldpos + movevector * moveratio;
288         
289         ISceneNode::setPosition(m_showpos);
290 }
291
292 #endif
293
294 #ifndef SERVER
295 /*
296         LocalPlayer
297 */
298
299 LocalPlayer::LocalPlayer():
300         m_sneak_node(32767,32767,32767),
301         m_sneak_node_exists(false)
302 {
303         // Initialize hp to 0, so that no hearts will be shown if server
304         // doesn't support health points
305         hp = 0;
306 }
307
308 LocalPlayer::~LocalPlayer()
309 {
310 }
311
312 void LocalPlayer::move(f32 dtime, Map &map, f32 pos_max_d,
313                 core::list<CollisionInfo> *collision_info)
314 {
315         v3f position = getPosition();
316         v3f oldpos = position;
317         v3s16 oldpos_i = floatToInt(oldpos, BS);
318
319         v3f old_speed = m_speed;
320
321         /*std::cout<<"oldpos_i=("<<oldpos_i.X<<","<<oldpos_i.Y<<","
322                         <<oldpos_i.Z<<")"<<std::endl;*/
323
324         /*
325                 Calculate new position
326         */
327         position += m_speed * dtime;
328
329         // Skip collision detection if a special movement mode is used
330         bool free_move = g_settings.getBool("free_move");
331         if(free_move)
332         {
333                 setPosition(position);
334                 return;
335         }
336
337         /*
338                 Collision detection
339         */
340         
341         // Player position in nodes
342         v3s16 pos_i = floatToInt(position, BS);
343         
344         /*
345                 Check if player is in water (the oscillating value)
346         */
347         try{
348                 // If in water, the threshold of coming out is at higher y
349                 if(in_water)
350                 {
351                         v3s16 pp = floatToInt(position + v3f(0,BS*0.1,0), BS);
352                         in_water = content_liquid(map.getNode(pp).getContent());
353                 }
354                 // If not in water, the threshold of going in is at lower y
355                 else
356                 {
357                         v3s16 pp = floatToInt(position + v3f(0,BS*0.5,0), BS);
358                         in_water = content_liquid(map.getNode(pp).getContent());
359                 }
360         }
361         catch(InvalidPositionException &e)
362         {
363                 in_water = false;
364         }
365
366         /*
367                 Check if player is in water (the stable value)
368         */
369         try{
370                 v3s16 pp = floatToInt(position + v3f(0,0,0), BS);
371                 in_water_stable = content_liquid(map.getNode(pp).getContent());
372         }
373         catch(InvalidPositionException &e)
374         {
375                 in_water_stable = false;
376         }
377
378         /*
379                 Check if player is climbing
380         */
381
382         try {
383                 v3s16 pp = floatToInt(position + v3f(0,0.5*BS,0), BS);
384                 v3s16 pp2 = floatToInt(position + v3f(0,-0.2*BS,0), BS);
385                 is_climbing = (content_features(map.getNode(pp).getContent()).climbable ||
386                                content_features(map.getNode(pp2).getContent()).climbable);
387         }
388         catch(InvalidPositionException &e)
389         {
390                 is_climbing = false;
391         }
392
393         /*
394                 Collision uncertainty radius
395                 Make it a bit larger than the maximum distance of movement
396         */
397         //f32 d = pos_max_d * 1.1;
398         // A fairly large value in here makes moving smoother
399         f32 d = 0.15*BS;
400
401         // This should always apply, otherwise there are glitches
402         assert(d > pos_max_d);
403
404         float player_radius = BS*0.35;
405         float player_height = BS*1.7;
406         
407         // Maximum distance over border for sneaking
408         f32 sneak_max = BS*0.4;
409
410         /*
411                 If sneaking, player has larger collision radius to keep from
412                 falling
413         */
414         /*if(control.sneak)
415                 player_radius = sneak_max + d*1.1;*/
416         
417         /*
418                 If sneaking, keep in range from the last walked node and don't
419                 fall off from it
420         */
421         if(control.sneak && m_sneak_node_exists)
422         {
423                 f32 maxd = 0.5*BS + sneak_max;
424                 v3f lwn_f = intToFloat(m_sneak_node, BS);
425                 position.X = rangelim(position.X, lwn_f.X-maxd, lwn_f.X+maxd);
426                 position.Z = rangelim(position.Z, lwn_f.Z-maxd, lwn_f.Z+maxd);
427                 
428                 f32 min_y = lwn_f.Y + 0.5*BS;
429                 if(position.Y < min_y)
430                 {
431                         position.Y = min_y;
432
433                         //v3f old_speed = m_speed;
434
435                         if(m_speed.Y < 0)
436                                 m_speed.Y = 0;
437
438                         /*if(collision_info)
439                         {
440                                 // Report fall collision
441                                 if(old_speed.Y < m_speed.Y - 0.1)
442                                 {
443                                         CollisionInfo info;
444                                         info.t = COLLISION_FALL;
445                                         info.speed = m_speed.Y - old_speed.Y;
446                                         collision_info->push_back(info);
447                                 }
448                         }*/
449                 }
450         }
451
452         /*
453                 Calculate player collision box (new and old)
454         */
455         core::aabbox3d<f32> playerbox(
456                 position.X - player_radius,
457                 position.Y - 0.0,
458                 position.Z - player_radius,
459                 position.X + player_radius,
460                 position.Y + player_height,
461                 position.Z + player_radius
462         );
463         core::aabbox3d<f32> playerbox_old(
464                 oldpos.X - player_radius,
465                 oldpos.Y - 0.0,
466                 oldpos.Z - player_radius,
467                 oldpos.X + player_radius,
468                 oldpos.Y + player_height,
469                 oldpos.Z + player_radius
470         );
471
472         /*
473                 If the player's feet touch the topside of any node, this is
474                 set to true.
475
476                 Player is allowed to jump when this is true.
477         */
478         touching_ground = false;
479
480         /*std::cout<<"Checking collisions for ("
481                         <<oldpos_i.X<<","<<oldpos_i.Y<<","<<oldpos_i.Z
482                         <<") -> ("
483                         <<pos_i.X<<","<<pos_i.Y<<","<<pos_i.Z
484                         <<"):"<<std::endl;*/
485         
486         /*
487                 Go through every node around the player
488         */
489         for(s16 y = oldpos_i.Y - 1; y <= oldpos_i.Y + 2; y++)
490         for(s16 z = oldpos_i.Z - 1; z <= oldpos_i.Z + 1; z++)
491         for(s16 x = oldpos_i.X - 1; x <= oldpos_i.X + 1; x++)
492         {
493                 try{
494                         // Player collides into walkable nodes
495                         if(content_walkable(map.getNode(v3s16(x,y,z)).getContent()) == false)
496                                 continue;
497                 }
498                 catch(InvalidPositionException &e)
499                 {
500                         // Doing nothing here will block the player from
501                         // walking over map borders
502                 }
503
504                 core::aabbox3d<f32> nodebox = getNodeBox(v3s16(x,y,z), BS);
505                 
506                 /*
507                         See if the player is touching ground.
508
509                         Player touches ground if player's minimum Y is near node's
510                         maximum Y and player's X-Z-area overlaps with the node's
511                         X-Z-area.
512
513                         Use 0.15*BS so that it is easier to get on a node.
514                 */
515                 if(
516                                 //fabs(nodebox.MaxEdge.Y-playerbox.MinEdge.Y) < d
517                                 fabs(nodebox.MaxEdge.Y-playerbox.MinEdge.Y) < 0.15*BS
518                                 && nodebox.MaxEdge.X-d > playerbox.MinEdge.X
519                                 && nodebox.MinEdge.X+d < playerbox.MaxEdge.X
520                                 && nodebox.MaxEdge.Z-d > playerbox.MinEdge.Z
521                                 && nodebox.MinEdge.Z+d < playerbox.MaxEdge.Z
522                 ){
523                         touching_ground = true;
524                 }
525                 
526                 // If player doesn't intersect with node, ignore node.
527                 if(playerbox.intersectsWithBox(nodebox) == false)
528                         continue;
529                 
530                 /*
531                         Go through every axis
532                 */
533                 v3f dirs[3] = {
534                         v3f(0,0,1), // back-front
535                         v3f(0,1,0), // top-bottom
536                         v3f(1,0,0), // right-left
537                 };
538                 for(u16 i=0; i<3; i++)
539                 {
540                         /*
541                                 Calculate values along the axis
542                         */
543                         f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[i]);
544                         f32 nodemin = nodebox.MinEdge.dotProduct(dirs[i]);
545                         f32 playermax = playerbox.MaxEdge.dotProduct(dirs[i]);
546                         f32 playermin = playerbox.MinEdge.dotProduct(dirs[i]);
547                         f32 playermax_old = playerbox_old.MaxEdge.dotProduct(dirs[i]);
548                         f32 playermin_old = playerbox_old.MinEdge.dotProduct(dirs[i]);
549                         
550                         /*
551                                 Check collision for the axis.
552                                 Collision happens when player is going through a surface.
553                         */
554                         /*f32 neg_d = d;
555                         f32 pos_d = d;
556                         // Make it easier to get on top of a node
557                         if(i == 1)
558                                 neg_d = 0.15*BS;
559                         bool negative_axis_collides =
560                                 (nodemax > playermin && nodemax <= playermin_old + neg_d
561                                         && m_speed.dotProduct(dirs[i]) < 0);
562                         bool positive_axis_collides =
563                                 (nodemin < playermax && nodemin >= playermax_old - pos_d
564                                         && m_speed.dotProduct(dirs[i]) > 0);*/
565                         bool negative_axis_collides =
566                                 (nodemax > playermin && nodemax <= playermin_old + d
567                                         && m_speed.dotProduct(dirs[i]) < 0);
568                         bool positive_axis_collides =
569                                 (nodemin < playermax && nodemin >= playermax_old - d
570                                         && m_speed.dotProduct(dirs[i]) > 0);
571                         bool main_axis_collides =
572                                         negative_axis_collides || positive_axis_collides;
573                         
574                         /*
575                                 Check overlap of player and node in other axes
576                         */
577                         bool other_axes_overlap = true;
578                         for(u16 j=0; j<3; j++)
579                         {
580                                 if(j == i)
581                                         continue;
582                                 f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[j]);
583                                 f32 nodemin = nodebox.MinEdge.dotProduct(dirs[j]);
584                                 f32 playermax = playerbox.MaxEdge.dotProduct(dirs[j]);
585                                 f32 playermin = playerbox.MinEdge.dotProduct(dirs[j]);
586                                 if(!(nodemax - d > playermin && nodemin + d < playermax))
587                                 {
588                                         other_axes_overlap = false;
589                                         break;
590                                 }
591                         }
592                         
593                         /*
594                                 If this is a collision, revert the position in the main
595                                 direction.
596                         */
597                         if(other_axes_overlap && main_axis_collides)
598                         {
599                                 //v3f old_speed = m_speed;
600
601                                 m_speed -= m_speed.dotProduct(dirs[i]) * dirs[i];
602                                 position -= position.dotProduct(dirs[i]) * dirs[i];
603                                 position += oldpos.dotProduct(dirs[i]) * dirs[i];
604                                 
605                                 /*if(collision_info)
606                                 {
607                                         // Report fall collision
608                                         if(old_speed.Y < m_speed.Y - 0.1)
609                                         {
610                                                 CollisionInfo info;
611                                                 info.t = COLLISION_FALL;
612                                                 info.speed = m_speed.Y - old_speed.Y;
613                                                 collision_info->push_back(info);
614                                         }
615                                 }*/
616                         }
617                 
618                 }
619         } // xyz
620
621         /*
622                 Check the nodes under the player to see from which node the
623                 player is sneaking from, if any.
624         */
625         {
626                 v3s16 pos_i_bottom = floatToInt(position - v3f(0,BS/2,0), BS);
627                 v2f player_p2df(position.X, position.Z);
628                 f32 min_distance_f = 100000.0*BS;
629                 // If already seeking from some node, compare to it.
630                 /*if(m_sneak_node_exists)
631                 {
632                         v3f sneaknode_pf = intToFloat(m_sneak_node, BS);
633                         v2f sneaknode_p2df(sneaknode_pf.X, sneaknode_pf.Z);
634                         f32 d_horiz_f = player_p2df.getDistanceFrom(sneaknode_p2df);
635                         f32 d_vert_f = fabs(sneaknode_pf.Y + BS*0.5 - position.Y);
636                         // Ignore if player is not on the same level (likely dropped)
637                         if(d_vert_f < 0.15*BS)
638                                 min_distance_f = d_horiz_f;
639                 }*/
640                 v3s16 new_sneak_node = m_sneak_node;
641                 for(s16 x=-1; x<=1; x++)
642                 for(s16 z=-1; z<=1; z++)
643                 {
644                         v3s16 p = pos_i_bottom + v3s16(x,0,z);
645                         v3f pf = intToFloat(p, BS);
646                         v2f node_p2df(pf.X, pf.Z);
647                         f32 distance_f = player_p2df.getDistanceFrom(node_p2df);
648                         f32 max_axis_distance_f = MYMAX(
649                                         fabs(player_p2df.X-node_p2df.X),
650                                         fabs(player_p2df.Y-node_p2df.Y));
651                                         
652                         if(distance_f > min_distance_f ||
653                                         max_axis_distance_f > 0.5*BS + sneak_max + 0.1*BS)
654                                 continue;
655
656                         try{
657                                 // The node to be sneaked on has to be walkable
658                                 if(content_walkable(map.getNode(p).getContent()) == false)
659                                         continue;
660                                 // And the node above it has to be nonwalkable
661                                 if(content_walkable(map.getNode(p+v3s16(0,1,0)).getContent()) == true)
662                                         continue;
663                         }
664                         catch(InvalidPositionException &e)
665                         {
666                                 continue;
667                         }
668
669                         min_distance_f = distance_f;
670                         new_sneak_node = p;
671                 }
672                 
673                 bool sneak_node_found = (min_distance_f < 100000.0*BS*0.9);
674                 
675                 if(control.sneak && m_sneak_node_exists)
676                 {
677                         if(sneak_node_found)
678                                 m_sneak_node = new_sneak_node;
679                 }
680                 else
681                 {
682                         m_sneak_node = new_sneak_node;
683                         m_sneak_node_exists = sneak_node_found;
684                 }
685
686                 /*
687                         If sneaking, the player's collision box can be in air, so
688                         this has to be set explicitly
689                 */
690                 if(sneak_node_found && control.sneak)
691                         touching_ground = true;
692         }
693         
694         /*
695                 Set new position
696         */
697         setPosition(position);
698         
699         /*
700                 Report collisions
701         */
702         if(collision_info)
703         {
704                 // Report fall collision
705                 if(old_speed.Y < m_speed.Y - 0.1)
706                 {
707                         CollisionInfo info;
708                         info.t = COLLISION_FALL;
709                         info.speed = m_speed.Y - old_speed.Y;
710                         collision_info->push_back(info);
711                 }
712         }
713 }
714
715 void LocalPlayer::move(f32 dtime, Map &map, f32 pos_max_d)
716 {
717         move(dtime, map, pos_max_d, NULL);
718 }
719
720 void LocalPlayer::applyControl(float dtime)
721 {
722         // Clear stuff
723         swimming_up = false;
724
725         // Random constants
726         f32 walk_acceleration = 4.0 * BS;
727         f32 walkspeed_max = 4.0 * BS;
728         
729         setPitch(control.pitch);
730         setYaw(control.yaw);
731         
732         v3f move_direction = v3f(0,0,1);
733         move_direction.rotateXZBy(getYaw());
734         
735         v3f speed = v3f(0,0,0);
736
737         bool free_move = g_settings.getBool("free_move");
738         bool fast_move = g_settings.getBool("fast_move");
739         bool continuous_forward = g_settings.getBool("continuous_forward");
740
741         if(free_move)
742         {
743                 v3f speed = getSpeed();
744                 speed.Y = 0;
745                 setSpeed(speed);
746         }
747
748         // Whether superspeed mode is used or not
749         bool superspeed = false;
750         
751         // If free movement and fast movement, always move fast
752         if(free_move && fast_move)
753                 superspeed = true;
754         
755         // Auxiliary button 1 (E)
756         if(control.aux1)
757         {
758                 if(free_move)
759                 {
760                         // In free movement mode, aux1 descends
761                         v3f speed = getSpeed();
762                         if(fast_move)
763                                 speed.Y = -20*BS;
764                         else
765                                 speed.Y = -walkspeed_max;
766                         setSpeed(speed);
767                 }
768                 else
769                 {
770                         // If not free movement but fast is allowed, aux1 is
771                         // "Turbo button"
772                         if(fast_move)
773                                 superspeed = true;
774                 }
775         }
776
777         if(continuous_forward)
778                 speed += move_direction;
779
780         if(control.up)
781         {
782                 if(continuous_forward)
783                         superspeed = true;
784                 else
785                         speed += move_direction;
786         }
787         if(control.down)
788         {
789                 speed -= move_direction;
790         }
791         if(control.left)
792         {
793                 speed += move_direction.crossProduct(v3f(0,1,0));
794         }
795         if(control.right)
796         {
797                 speed += move_direction.crossProduct(v3f(0,-1,0));
798         }
799         if(control.jump)
800         {
801                 if(free_move)
802                 {
803                         v3f speed = getSpeed();
804                         if(fast_move)
805                                 speed.Y = 20*BS;
806                         else
807                                 speed.Y = walkspeed_max;
808                         setSpeed(speed);
809                 }
810                 else if(touching_ground)
811                 {
812                         v3f speed = getSpeed();
813                         /*
814                                 NOTE: The d value in move() affects jump height by
815                                 raising the height at which the jump speed is kept
816                                 at its starting value
817                         */
818                         speed.Y = 6.5*BS;
819                         setSpeed(speed);
820                 }
821                 // Use the oscillating value for getting out of water
822                 // (so that the player doesn't fly on the surface)
823                 else if(in_water)
824                 {
825                         v3f speed = getSpeed();
826                         speed.Y = 1.5*BS;
827                         setSpeed(speed);
828                         swimming_up = true;
829                 }
830         }
831
832         if (is_climbing) {
833                 if (control.up || control.left || control.right || control.down) {
834                         v3f speed = getSpeed();
835                         speed.Y = 2.5*BS;
836                         setSpeed(speed);
837                 }
838                 else {
839                         v3f speed = getSpeed();
840                         speed.Y = -2*BS;
841                         setSpeed(speed);
842                 }
843         }
844
845         // The speed of the player (Y is ignored)
846         if(superspeed)
847                 speed = speed.normalize() * walkspeed_max * 5.0;
848         else if(control.sneak)
849                 speed = speed.normalize() * walkspeed_max / 3.0;
850         else
851                 speed = speed.normalize() * walkspeed_max;
852         
853         f32 inc = walk_acceleration * BS * dtime;
854         
855         // Faster acceleration if fast and free movement
856         if(free_move && fast_move)
857                 inc = walk_acceleration * BS * dtime * 10;
858         
859         // Accelerate to target speed with maximum increment
860         accelerate(speed, inc);
861 }
862 #endif
863