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