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