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