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