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