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