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