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