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