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