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