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