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