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