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