utility.h: Change Buffer's interface to be more compatible with SharedBuffer's interf...
[oweals/minetest.git] / src / player.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "player.h"
21 #include "map.h"
22 #include "connection.h"
23 #include "constants.h"
24 #include "utility.h"
25 #ifndef SERVER
26 #include <ITextSceneNode.h>
27 #endif
28 #include "settings.h"
29
30 Player::Player():
31         touching_ground(false),
32         in_water(false),
33         in_water_stable(false),
34         is_climbing(false),
35         swimming_up(false),
36         inventory_backup(NULL),
37         craftresult_is_preview(true),
38         hp(20),
39         peer_id(PEER_ID_INEXISTENT),
40         m_selected_item(0),
41         m_pitch(0),
42         m_yaw(0),
43         m_speed(0,0,0),
44         m_position(0,0,0)
45 {
46         updateName("<not set>");
47         resetInventory();
48 }
49
50 Player::~Player()
51 {
52         delete inventory_backup;
53 }
54
55 void Player::wieldItem(u16 item)
56 {
57         m_selected_item = item;
58 }
59
60 void Player::resetInventory()
61 {
62         inventory.clear();
63         inventory.addList("main", PLAYER_INVENTORY_SIZE);
64         inventory.addList("craft", 9);
65         inventory.addList("craftresult", 1);
66 }
67
68 // Y direction is ignored
69 void Player::accelerate(v3f target_speed, f32 max_increase)
70 {
71         v3f d_wanted = target_speed - m_speed;
72         d_wanted.Y = 0;
73         f32 dl_wanted = d_wanted.getLength();
74         f32 dl = dl_wanted;
75         if(dl > max_increase)
76                 dl = max_increase;
77         
78         v3f d = d_wanted.normalize() * dl;
79
80         m_speed.X += d.X;
81         m_speed.Z += d.Z;
82         //m_speed += d;
83
84 #if 0 // old code
85         if(m_speed.X < target_speed.X - max_increase)
86                 m_speed.X += max_increase;
87         else if(m_speed.X > target_speed.X + max_increase)
88                 m_speed.X -= max_increase;
89         else if(m_speed.X < target_speed.X)
90                 m_speed.X = target_speed.X;
91         else if(m_speed.X > target_speed.X)
92                 m_speed.X = target_speed.X;
93
94         if(m_speed.Z < target_speed.Z - max_increase)
95                 m_speed.Z += max_increase;
96         else if(m_speed.Z > target_speed.Z + max_increase)
97                 m_speed.Z -= max_increase;
98         else if(m_speed.Z < target_speed.Z)
99                 m_speed.Z = target_speed.Z;
100         else if(m_speed.Z > target_speed.Z)
101                 m_speed.Z = target_speed.Z;
102 #endif
103 }
104
105 void Player::serialize(std::ostream &os)
106 {
107         // Utilize a Settings object for storing values
108         Settings args;
109         args.setS32("version", 1);
110         args.set("name", m_name);
111         //args.set("password", m_password);
112         args.setFloat("pitch", m_pitch);
113         args.setFloat("yaw", m_yaw);
114         args.setV3F("position", m_position);
115         args.setBool("craftresult_is_preview", craftresult_is_preview);
116         args.setS32("hp", hp);
117
118         args.writeLines(os);
119
120         os<<"PlayerArgsEnd\n";
121         
122         // If actual inventory is backed up due to creative mode, save it
123         // instead of the dummy creative mode inventory
124         if(inventory_backup)
125                 inventory_backup->serialize(os);
126         else
127                 inventory.serialize(os);
128 }
129
130 void Player::deSerialize(std::istream &is)
131 {
132         Settings args;
133         
134         for(;;)
135         {
136                 if(is.eof())
137                         throw SerializationError
138                                         ("Player::deSerialize(): PlayerArgsEnd not found");
139                 std::string line;
140                 std::getline(is, line);
141                 std::string trimmedline = trim(line);
142                 if(trimmedline == "PlayerArgsEnd")
143                         break;
144                 args.parseConfigLine(line);
145         }
146
147         //args.getS32("version");
148         std::string name = args.get("name");
149         updateName(name.c_str());
150         /*std::string password = "";
151         if(args.exists("password"))
152                 password = args.get("password");
153         updatePassword(password.c_str());*/
154         m_pitch = args.getFloat("pitch");
155         m_yaw = args.getFloat("yaw");
156         m_position = args.getV3F("position");
157         try{
158                 craftresult_is_preview = args.getBool("craftresult_is_preview");
159         }catch(SettingNotFoundException &e){
160                 craftresult_is_preview = true;
161         }
162         try{
163                 hp = args.getS32("hp");
164         }catch(SettingNotFoundException &e){
165                 hp = 20;
166         }
167         /*try{
168                 std::string sprivs = args.get("privs");
169                 if(sprivs == "all")
170                 {
171                         privs = PRIV_ALL;
172                 }
173                 else
174                 {
175                         std::istringstream ss(sprivs);
176                         ss>>privs;
177                 }
178         }catch(SettingNotFoundException &e){
179                 privs = PRIV_DEFAULT;
180         }*/
181
182         inventory.deSerialize(is);
183 }
184
185 /*
186         RemotePlayer
187 */
188
189 #ifndef SERVER
190
191 RemotePlayer::RemotePlayer(
192                 scene::ISceneNode* parent,
193                 IrrlichtDevice *device,
194                 s32 id):
195         scene::ISceneNode(parent, (device==NULL)?NULL:device->getSceneManager(), id),
196         m_text(NULL)
197 {
198         m_box = core::aabbox3d<f32>(-BS/2,0,-BS/2,BS/2,BS*2,BS/2);
199
200         if(parent != NULL && device != NULL)
201         {
202                 // ISceneNode stores a member called SceneManager
203                 scene::ISceneManager* mgr = SceneManager;
204                 video::IVideoDriver* driver = mgr->getVideoDriver();
205                 gui::IGUIEnvironment* gui = device->getGUIEnvironment();
206
207                 // Add a text node for showing the name
208                 wchar_t wname[1] = {0};
209                 m_text = mgr->addTextSceneNode(gui->getBuiltInFont(),
210                                 wname, video::SColor(255,255,255,255), this);
211                 m_text->setPosition(v3f(0, (f32)BS*2.1, 0));
212
213                 // Attach a simple mesh to the player for showing an image
214                 scene::SMesh *mesh = new scene::SMesh();
215                 { // Front
216                 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
217                 video::SColor c(255,255,255,255);
218                 video::S3DVertex vertices[4] =
219                 {
220                         video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
221                         video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
222                         video::S3DVertex(BS/2,BS*2,0, 0,0,0, c, 1,0),
223                         video::S3DVertex(-BS/2,BS*2,0, 0,0,0, c, 0,0),
224                 };
225                 u16 indices[] = {0,1,2,2,3,0};
226                 buf->append(vertices, 4, indices, 6);
227                 // Set material
228                 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
229                 //buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
230                 buf->getMaterial().setTexture(0, driver->getTexture(getTexturePath("player.png").c_str()));
231                 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
232                 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
233                 //buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
234                 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
235                 // Add to mesh
236                 mesh->addMeshBuffer(buf);
237                 buf->drop();
238                 }
239                 { // Back
240                 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
241                 video::SColor c(255,255,255,255);
242                 video::S3DVertex vertices[4] =
243                 {
244                         video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
245                         video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
246                         video::S3DVertex(-BS/2,BS*2,0, 0,0,0, c, 0,0),
247                         video::S3DVertex(BS/2,BS*2,0, 0,0,0, c, 1,0),
248                 };
249                 u16 indices[] = {0,1,2,2,3,0};
250                 buf->append(vertices, 4, indices, 6);
251                 // Set material
252                 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
253                 //buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
254                 buf->getMaterial().setTexture(0, driver->getTexture(getTexturePath("player_back.png").c_str()));
255                 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
256                 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
257                 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
258                 // Add to mesh
259                 mesh->addMeshBuffer(buf);
260                 buf->drop();
261                 }
262                 m_node = mgr->addMeshSceneNode(mesh, this);
263                 mesh->drop();
264                 m_node->setPosition(v3f(0,0,0));
265         }
266 }
267
268 RemotePlayer::~RemotePlayer()
269 {
270         if(SceneManager != NULL)
271                 ISceneNode::remove();
272 }
273
274 void RemotePlayer::updateName(const char *name)
275 {
276         Player::updateName(name);
277         if(m_text != NULL)
278         {
279                 wchar_t wname[PLAYERNAME_SIZE];
280                 mbstowcs(wname, m_name, strlen(m_name)+1);
281                 m_text->setText(wname);
282         }
283 }
284
285 void RemotePlayer::move(f32 dtime, Map &map, f32 pos_max_d)
286 {
287         m_pos_animation_time_counter += dtime;
288         m_pos_animation_counter += dtime;
289         v3f movevector = m_position - m_oldpos;
290         f32 moveratio;
291         if(m_pos_animation_time < 0.001)
292                 moveratio = 1.0;
293         else
294                 moveratio = m_pos_animation_counter / m_pos_animation_time;
295         if(moveratio > 1.5)
296                 moveratio = 1.5;
297         m_showpos = m_oldpos + movevector * moveratio;
298         
299         ISceneNode::setPosition(m_showpos);
300 }
301
302 #endif
303
304 #ifndef SERVER
305 /*
306         LocalPlayer
307 */
308
309 LocalPlayer::LocalPlayer():
310         m_sneak_node(32767,32767,32767),
311         m_sneak_node_exists(false)
312 {
313         // Initialize hp to 0, so that no hearts will be shown if server
314         // doesn't support health points
315         hp = 0;
316 }
317
318 LocalPlayer::~LocalPlayer()
319 {
320 }
321
322 void LocalPlayer::move(f32 dtime, Map &map, f32 pos_max_d,
323                 core::list<CollisionInfo> *collision_info)
324 {
325         v3f position = getPosition();
326         v3f oldpos = position;
327         v3s16 oldpos_i = floatToInt(oldpos, BS);
328
329         v3f old_speed = m_speed;
330
331         /*std::cout<<"oldpos_i=("<<oldpos_i.X<<","<<oldpos_i.Y<<","
332                         <<oldpos_i.Z<<")"<<std::endl;*/
333
334         /*
335                 Calculate new position
336         */
337         position += m_speed * dtime;
338         
339         // Skip collision detection if a special movement mode is used
340         bool free_move = g_settings->getBool("free_move");
341         if(free_move)
342         {
343                 setPosition(position);
344                 return;
345         }
346
347         /*
348                 Collision detection
349         */
350         
351         // Player position in nodes
352         v3s16 pos_i = floatToInt(position, BS);
353         
354         /*
355                 Check if player is in water (the oscillating value)
356         */
357         try{
358                 // If in water, the threshold of coming out is at higher y
359                 if(in_water)
360                 {
361                         v3s16 pp = floatToInt(position + v3f(0,BS*0.1,0), BS);
362                         in_water = content_liquid(map.getNode(pp).getContent());
363                 }
364                 // If not in water, the threshold of going in is at lower y
365                 else
366                 {
367                         v3s16 pp = floatToInt(position + v3f(0,BS*0.5,0), BS);
368                         in_water = content_liquid(map.getNode(pp).getContent());
369                 }
370         }
371         catch(InvalidPositionException &e)
372         {
373                 in_water = false;
374         }
375
376         /*
377                 Check if player is in water (the stable value)
378         */
379         try{
380                 v3s16 pp = floatToInt(position + v3f(0,0,0), BS);
381                 in_water_stable = content_liquid(map.getNode(pp).getContent());
382         }
383         catch(InvalidPositionException &e)
384         {
385                 in_water_stable = false;
386         }
387
388         /*
389                 Check if player is climbing
390         */
391
392         try {
393                 v3s16 pp = floatToInt(position + v3f(0,0.5*BS,0), BS);
394                 v3s16 pp2 = floatToInt(position + v3f(0,-0.2*BS,0), BS);
395                 is_climbing = ((content_features(map.getNode(pp).getContent()).climbable ||
396                                 content_features(map.getNode(pp2).getContent()).climbable) && !free_move);
397         }
398         catch(InvalidPositionException &e)
399         {
400                 is_climbing = false;
401         }
402
403         /*
404                 Collision uncertainty radius
405                 Make it a bit larger than the maximum distance of movement
406         */
407         //f32 d = pos_max_d * 1.1;
408         // A fairly large value in here makes moving smoother
409         f32 d = 0.15*BS;
410
411         // This should always apply, otherwise there are glitches
412         assert(d > pos_max_d);
413
414         float player_radius = BS*0.35;
415         float player_height = BS*1.7;
416         
417         // Maximum distance over border for sneaking
418         f32 sneak_max = BS*0.4;
419
420         /*
421                 If sneaking, player has larger collision radius to keep from
422                 falling
423         */
424         /*if(control.sneak)
425                 player_radius = sneak_max + d*1.1;*/
426         
427         /*
428                 If sneaking, keep in range from the last walked node and don't
429                 fall off from it
430         */
431         if(control.sneak && m_sneak_node_exists)
432         {
433                 f32 maxd = 0.5*BS + sneak_max;
434                 v3f lwn_f = intToFloat(m_sneak_node, BS);
435                 position.X = rangelim(position.X, lwn_f.X-maxd, lwn_f.X+maxd);
436                 position.Z = rangelim(position.Z, lwn_f.Z-maxd, lwn_f.Z+maxd);
437                 
438                 f32 min_y = lwn_f.Y + 0.5*BS;
439                 if(position.Y < min_y)
440                 {
441                         position.Y = min_y;
442
443                         //v3f old_speed = m_speed;
444
445                         if(m_speed.Y < 0)
446                                 m_speed.Y = 0;
447
448                         /*if(collision_info)
449                         {
450                                 // Report fall collision
451                                 if(old_speed.Y < m_speed.Y - 0.1)
452                                 {
453                                         CollisionInfo info;
454                                         info.t = COLLISION_FALL;
455                                         info.speed = m_speed.Y - old_speed.Y;
456                                         collision_info->push_back(info);
457                                 }
458                         }*/
459                 }
460         }
461
462         /*
463                 Calculate player collision box (new and old)
464         */
465         core::aabbox3d<f32> playerbox(
466                 position.X - player_radius,
467                 position.Y - 0.0,
468                 position.Z - player_radius,
469                 position.X + player_radius,
470                 position.Y + player_height,
471                 position.Z + player_radius
472         );
473         core::aabbox3d<f32> playerbox_old(
474                 oldpos.X - player_radius,
475                 oldpos.Y - 0.0,
476                 oldpos.Z - player_radius,
477                 oldpos.X + player_radius,
478                 oldpos.Y + player_height,
479                 oldpos.Z + player_radius
480         );
481
482         /*
483                 If the player's feet touch the topside of any node, this is
484                 set to true.
485
486                 Player is allowed to jump when this is true.
487         */
488         touching_ground = false;
489
490         /*std::cout<<"Checking collisions for ("
491                         <<oldpos_i.X<<","<<oldpos_i.Y<<","<<oldpos_i.Z
492                         <<") -> ("
493                         <<pos_i.X<<","<<pos_i.Y<<","<<pos_i.Z
494                         <<"):"<<std::endl;*/
495         
496         bool standing_on_unloaded = false;
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                 bool is_unloaded = false;
506                 try{
507                         // Player collides into walkable nodes
508                         if(content_walkable(map.getNode(v3s16(x,y,z)).getContent()) == false)
509                                 continue;
510                 }
511                 catch(InvalidPositionException &e)
512                 {
513                         is_unloaded = true;
514                         // Doing nothing here will block the player from
515                         // walking over map borders
516                 }
517
518                 core::aabbox3d<f32> nodebox = getNodeBox(v3s16(x,y,z), BS);
519                 
520                 /*
521                         See if the player is touching ground.
522
523                         Player touches ground if player's minimum Y is near node's
524                         maximum Y and player's X-Z-area overlaps with the node's
525                         X-Z-area.
526
527                         Use 0.15*BS so that it is easier to get on a node.
528                 */
529                 if(
530                                 //fabs(nodebox.MaxEdge.Y-playerbox.MinEdge.Y) < d
531                                 fabs(nodebox.MaxEdge.Y-playerbox.MinEdge.Y) < 0.15*BS
532                                 && nodebox.MaxEdge.X-d > playerbox.MinEdge.X
533                                 && nodebox.MinEdge.X+d < playerbox.MaxEdge.X
534                                 && nodebox.MaxEdge.Z-d > playerbox.MinEdge.Z
535                                 && nodebox.MinEdge.Z+d < playerbox.MaxEdge.Z
536                 ){
537                         touching_ground = true;
538                         if(is_unloaded)
539                                 standing_on_unloaded = 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).getContent()) == false)
675                                         continue;
676                                 // And the node above it has to be nonwalkable
677                                 if(content_walkable(map.getNode(p+v3s16(0,1,0)).getContent()) == 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                 Report collisions
717         */
718         if(collision_info)
719         {
720                 // Report fall collision
721                 if(old_speed.Y < m_speed.Y - 0.1 && !standing_on_unloaded)
722                 {
723                         CollisionInfo info;
724                         info.t = COLLISION_FALL;
725                         info.speed = m_speed.Y - old_speed.Y;
726                         collision_info->push_back(info);
727                 }
728         }
729 }
730
731 void LocalPlayer::move(f32 dtime, Map &map, f32 pos_max_d)
732 {
733         move(dtime, map, pos_max_d, NULL);
734 }
735
736 void LocalPlayer::applyControl(float dtime)
737 {
738         // Clear stuff
739         swimming_up = false;
740
741         // Random constants
742         f32 walk_acceleration = 4.0 * BS;
743         f32 walkspeed_max = 4.0 * BS;
744         
745         setPitch(control.pitch);
746         setYaw(control.yaw);
747         
748         v3f move_direction = v3f(0,0,1);
749         move_direction.rotateXZBy(getYaw());
750         
751         v3f speed = v3f(0,0,0);
752
753         bool free_move = g_settings->getBool("free_move");
754         bool fast_move = g_settings->getBool("fast_move");
755         bool continuous_forward = g_settings->getBool("continuous_forward");
756
757         if(free_move || is_climbing)
758         {
759                 v3f speed = getSpeed();
760                 speed.Y = 0;
761                 setSpeed(speed);
762         }
763
764         // Whether superspeed mode is used or not
765         bool superspeed = false;
766         
767         // If free movement and fast movement, always move fast
768         if(free_move && fast_move)
769                 superspeed = true;
770         
771         // Auxiliary button 1 (E)
772         if(control.aux1)
773         {
774                 if(free_move)
775                 {
776                         // In free movement mode, aux1 descends
777                         v3f speed = getSpeed();
778                         if(fast_move)
779                                 speed.Y = -20*BS;
780                         else
781                                 speed.Y = -walkspeed_max;
782                         setSpeed(speed);
783                 }
784                 else if(is_climbing)
785                 {
786                         v3f speed = getSpeed();
787                         speed.Y = -3*BS;
788                         setSpeed(speed);
789                 }
790                 else
791                 {
792                         // If not free movement but fast is allowed, aux1 is
793                         // "Turbo button"
794                         if(fast_move)
795                                 superspeed = true;
796                 }
797         }
798
799         if(continuous_forward)
800                 speed += move_direction;
801
802         if(control.up)
803         {
804                 if(continuous_forward)
805                         superspeed = true;
806                 else
807                         speed += move_direction;
808         }
809         if(control.down)
810         {
811                 speed -= move_direction;
812         }
813         if(control.left)
814         {
815                 speed += move_direction.crossProduct(v3f(0,1,0));
816         }
817         if(control.right)
818         {
819                 speed += move_direction.crossProduct(v3f(0,-1,0));
820         }
821         if(control.jump)
822         {
823                 if(free_move)
824                 {
825                         v3f speed = getSpeed();
826                         if(fast_move)
827                                 speed.Y = 20*BS;
828                         else
829                                 speed.Y = walkspeed_max;
830                         setSpeed(speed);
831                 }
832                 else if(touching_ground)
833                 {
834                         v3f speed = getSpeed();
835                         /*
836                                 NOTE: The d value in move() affects jump height by
837                                 raising the height at which the jump speed is kept
838                                 at its starting value
839                         */
840                         speed.Y = 6.5*BS;
841                         setSpeed(speed);
842                 }
843                 // Use the oscillating value for getting out of water
844                 // (so that the player doesn't fly on the surface)
845                 else if(in_water)
846                 {
847                         v3f speed = getSpeed();
848                         speed.Y = 1.5*BS;
849                         setSpeed(speed);
850                         swimming_up = true;
851                 }
852                 else if(is_climbing)
853                 {
854                         v3f speed = getSpeed();
855                         speed.Y = 3*BS;
856                         setSpeed(speed);
857                 }
858         }
859
860         // The speed of the player (Y is ignored)
861         if(superspeed)
862                 speed = speed.normalize() * walkspeed_max * 5.0;
863         else if(control.sneak)
864                 speed = speed.normalize() * walkspeed_max / 3.0;
865         else
866                 speed = speed.normalize() * walkspeed_max;
867         
868         f32 inc = walk_acceleration * BS * dtime;
869         
870         // Faster acceleration if fast and free movement
871         if(free_move && fast_move)
872                 inc = walk_acceleration * BS * dtime * 10;
873         
874         // Accelerate to target speed with maximum increment
875         accelerate(speed, inc);
876 }
877 #endif
878