2bd62dabf6f70d65d8ce684506dc9a63d3592bba
[oweals/minetest.git] / src / localplayer.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2012 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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser 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 "localplayer.h"
21
22 #include "main.h" // For g_settings
23 #include "event.h"
24 #include "collision.h"
25 #include "gamedef.h"
26 #include "nodedef.h"
27 #include "settings.h"
28 #include "map.h"
29 #include "util/numeric.h"
30
31 /*
32         LocalPlayer
33 */
34
35 LocalPlayer::LocalPlayer(IGameDef *gamedef):
36         Player(gamedef),
37         m_sneak_node(32767,32767,32767),
38         m_sneak_node_exists(false)
39 {
40         // Initialize hp to 0, so that no hearts will be shown if server
41         // doesn't support health points
42         hp = 0;
43 }
44
45 LocalPlayer::~LocalPlayer()
46 {
47 }
48
49 void LocalPlayer::move(f32 dtime, Map &map, f32 pos_max_d,
50                 core::list<CollisionInfo> *collision_info)
51 {
52         INodeDefManager *nodemgr = m_gamedef->ndef();
53
54         v3f position = getPosition();
55         v3f oldpos = position;
56         v3s16 oldpos_i = floatToInt(oldpos, BS);
57
58         v3f old_speed = m_speed;
59
60         /*std::cout<<"oldpos_i=("<<oldpos_i.X<<","<<oldpos_i.Y<<","
61                         <<oldpos_i.Z<<")"<<std::endl;*/
62
63         /*
64                 Calculate new position
65         */
66         position += m_speed * dtime;
67         
68         // Skip collision detection if a special movement mode is used
69         bool fly_allowed = m_gamedef->checkLocalPrivilege("fly");
70         bool free_move = fly_allowed && g_settings->getBool("free_move");
71         if(free_move)
72         {
73                 setPosition(position);
74                 return;
75         }
76
77         /*
78                 Collision detection
79         */
80         
81         // Player position in nodes
82         v3s16 pos_i = floatToInt(position, BS);
83         
84         /*
85                 Check if player is in water (the oscillating value)
86         */
87         try{
88                 // If in water, the threshold of coming out is at higher y
89                 if(in_water)
90                 {
91                         v3s16 pp = floatToInt(position + v3f(0,BS*0.1,0), BS);
92                         in_water = nodemgr->get(map.getNode(pp).getContent()).isLiquid();
93                 }
94                 // If not in water, the threshold of going in is at lower y
95                 else
96                 {
97                         v3s16 pp = floatToInt(position + v3f(0,BS*0.5,0), BS);
98                         in_water = nodemgr->get(map.getNode(pp).getContent()).isLiquid();
99                 }
100         }
101         catch(InvalidPositionException &e)
102         {
103                 in_water = false;
104         }
105
106         /*
107                 Check if player is in water (the stable value)
108         */
109         try{
110                 v3s16 pp = floatToInt(position + v3f(0,0,0), BS);
111                 in_water_stable = nodemgr->get(map.getNode(pp).getContent()).isLiquid();
112         }
113         catch(InvalidPositionException &e)
114         {
115                 in_water_stable = false;
116         }
117
118         /*
119                 Check if player is climbing
120         */
121
122         try {
123                 v3s16 pp = floatToInt(position + v3f(0,0.5*BS,0), BS);
124                 v3s16 pp2 = floatToInt(position + v3f(0,-0.2*BS,0), BS);
125                 is_climbing = ((nodemgr->get(map.getNode(pp).getContent()).climbable ||
126                 nodemgr->get(map.getNode(pp2).getContent()).climbable) && !free_move);
127         }
128         catch(InvalidPositionException &e)
129         {
130                 is_climbing = false;
131         }
132
133         /*
134                 Collision uncertainty radius
135                 Make it a bit larger than the maximum distance of movement
136         */
137         //f32 d = pos_max_d * 1.1;
138         // A fairly large value in here makes moving smoother
139         f32 d = 0.15*BS;
140
141         // This should always apply, otherwise there are glitches
142         assert(d > pos_max_d);
143
144         float player_radius = BS*0.30;
145         float player_height = BS*1.55;
146         
147         // Maximum distance over border for sneaking
148         f32 sneak_max = BS*0.4;
149
150         /*
151                 If sneaking, player has larger collision radius to keep from
152                 falling
153         */
154         /*if(control.sneak)
155                 player_radius = sneak_max + d*1.1;*/
156         
157         /*
158                 If sneaking, keep in range from the last walked node and don't
159                 fall off from it
160         */
161         if(control.sneak && m_sneak_node_exists)
162         {
163                 f32 maxd = 0.5*BS + sneak_max;
164                 v3f lwn_f = intToFloat(m_sneak_node, BS);
165                 position.X = rangelim(position.X, lwn_f.X-maxd, lwn_f.X+maxd);
166                 position.Z = rangelim(position.Z, lwn_f.Z-maxd, lwn_f.Z+maxd);
167                 
168                 f32 min_y = lwn_f.Y + 0.5*BS;
169                 if(position.Y < min_y)
170                 {
171                         position.Y = min_y;
172
173                         //v3f old_speed = m_speed;
174
175                         if(m_speed.Y < 0)
176                                 m_speed.Y = 0;
177
178                         /*if(collision_info)
179                         {
180                                 // Report fall collision
181                                 if(old_speed.Y < m_speed.Y - 0.1)
182                                 {
183                                         CollisionInfo info;
184                                         info.t = COLLISION_FALL;
185                                         info.speed = m_speed.Y - old_speed.Y;
186                                         collision_info->push_back(info);
187                                 }
188                         }*/
189                 }
190         }
191
192         /*
193                 Calculate player collision box (new and old)
194         */
195         core::aabbox3d<f32> playerbox(
196                 position.X - player_radius,
197                 position.Y - 0.0,
198                 position.Z - player_radius,
199                 position.X + player_radius,
200                 position.Y + player_height,
201                 position.Z + player_radius
202         );
203         core::aabbox3d<f32> playerbox_old(
204                 oldpos.X - player_radius,
205                 oldpos.Y - 0.0,
206                 oldpos.Z - player_radius,
207                 oldpos.X + player_radius,
208                 oldpos.Y + player_height,
209                 oldpos.Z + player_radius
210         );
211
212         /*
213                 If the player's feet touch the topside of any node, this is
214                 set to true.
215
216                 Player is allowed to jump when this is true.
217         */
218         bool touching_ground_was = touching_ground;
219         touching_ground = false;
220
221         /*std::cout<<"Checking collisions for ("
222                         <<oldpos_i.X<<","<<oldpos_i.Y<<","<<oldpos_i.Z
223                         <<") -> ("
224                         <<pos_i.X<<","<<pos_i.Y<<","<<pos_i.Z
225                         <<"):"<<std::endl;*/
226         
227         bool standing_on_unloaded = false;
228         
229         /*
230                 Go through every node around the player
231         */
232         for(s16 y = oldpos_i.Y - 1; y <= oldpos_i.Y + 2; y++)
233         for(s16 z = oldpos_i.Z - 1; z <= oldpos_i.Z + 1; z++)
234         for(s16 x = oldpos_i.X - 1; x <= oldpos_i.X + 1; x++)
235         {
236                 bool is_unloaded = false;
237                 try{
238                         // Player collides into walkable nodes
239                         if(nodemgr->get(map.getNode(v3s16(x,y,z))).walkable == false)
240                                 continue;
241                 }
242                 catch(InvalidPositionException &e)
243                 {
244                         is_unloaded = true;
245                         // Doing nothing here will block the player from
246                         // walking over map borders
247                 }
248
249                 core::aabbox3d<f32> nodebox = getNodeBox(v3s16(x,y,z), BS);
250                 
251                 /*
252                         See if the player is touching ground.
253
254                         Player touches ground if player's minimum Y is near node's
255                         maximum Y and player's X-Z-area overlaps with the node's
256                         X-Z-area.
257
258                         Use 0.15*BS so that it is easier to get on a node.
259                 */
260                 if(
261                                 //fabs(nodebox.MaxEdge.Y-playerbox.MinEdge.Y) < d
262                                 fabs(nodebox.MaxEdge.Y-playerbox.MinEdge.Y) < 0.15*BS
263                                 && nodebox.MaxEdge.X-d > playerbox.MinEdge.X
264                                 && nodebox.MinEdge.X+d < playerbox.MaxEdge.X
265                                 && nodebox.MaxEdge.Z-d > playerbox.MinEdge.Z
266                                 && nodebox.MinEdge.Z+d < playerbox.MaxEdge.Z
267                 ){
268                         touching_ground = true;
269                         if(is_unloaded)
270                                 standing_on_unloaded = true;
271                 }
272                 
273                 // If player doesn't intersect with node, ignore node.
274                 if(playerbox.intersectsWithBox(nodebox) == false)
275                         continue;
276                 
277                 /*
278                         Go through every axis
279                 */
280                 v3f dirs[3] = {
281                         v3f(0,0,1), // back-front
282                         v3f(0,1,0), // top-bottom
283                         v3f(1,0,0), // right-left
284                 };
285                 for(u16 i=0; i<3; i++)
286                 {
287                         /*
288                                 Calculate values along the axis
289                         */
290                         f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[i]);
291                         f32 nodemin = nodebox.MinEdge.dotProduct(dirs[i]);
292                         f32 playermax = playerbox.MaxEdge.dotProduct(dirs[i]);
293                         f32 playermin = playerbox.MinEdge.dotProduct(dirs[i]);
294                         f32 playermax_old = playerbox_old.MaxEdge.dotProduct(dirs[i]);
295                         f32 playermin_old = playerbox_old.MinEdge.dotProduct(dirs[i]);
296                         
297                         /*
298                                 Check collision for the axis.
299                                 Collision happens when player is going through a surface.
300                         */
301                         /*f32 neg_d = d;
302                         f32 pos_d = d;
303                         // Make it easier to get on top of a node
304                         if(i == 1)
305                                 neg_d = 0.15*BS;
306                         bool negative_axis_collides =
307                                 (nodemax > playermin && nodemax <= playermin_old + neg_d
308                                         && m_speed.dotProduct(dirs[i]) < 0);
309                         bool positive_axis_collides =
310                                 (nodemin < playermax && nodemin >= playermax_old - pos_d
311                                         && m_speed.dotProduct(dirs[i]) > 0);*/
312                         bool negative_axis_collides =
313                                 (nodemax > playermin && nodemax <= playermin_old + d
314                                         && m_speed.dotProduct(dirs[i]) < 0);
315                         bool positive_axis_collides =
316                                 (nodemin < playermax && nodemin >= playermax_old - d
317                                         && m_speed.dotProduct(dirs[i]) > 0);
318                         bool main_axis_collides =
319                                         negative_axis_collides || positive_axis_collides;
320                         
321                         /*
322                                 Check overlap of player and node in other axes
323                         */
324                         bool other_axes_overlap = true;
325                         for(u16 j=0; j<3; j++)
326                         {
327                                 if(j == i)
328                                         continue;
329                                 f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[j]);
330                                 f32 nodemin = nodebox.MinEdge.dotProduct(dirs[j]);
331                                 f32 playermax = playerbox.MaxEdge.dotProduct(dirs[j]);
332                                 f32 playermin = playerbox.MinEdge.dotProduct(dirs[j]);
333                                 if(!(nodemax - d > playermin && nodemin + d < playermax))
334                                 {
335                                         other_axes_overlap = false;
336                                         break;
337                                 }
338                         }
339                         
340                         /*
341                                 If this is a collision, revert the position in the main
342                                 direction.
343                         */
344                         if(other_axes_overlap && main_axis_collides)
345                         {
346                                 //v3f old_speed = m_speed;
347
348                                 m_speed -= m_speed.dotProduct(dirs[i]) * dirs[i];
349                                 position -= position.dotProduct(dirs[i]) * dirs[i];
350                                 position += oldpos.dotProduct(dirs[i]) * dirs[i];
351                                 
352                                 /*if(collision_info)
353                                 {
354                                         // Report fall collision
355                                         if(old_speed.Y < m_speed.Y - 0.1)
356                                         {
357                                                 CollisionInfo info;
358                                                 info.t = COLLISION_FALL;
359                                                 info.speed = m_speed.Y - old_speed.Y;
360                                                 collision_info->push_back(info);
361                                         }
362                                 }*/
363                         }
364                 
365                 }
366         } // xyz
367
368         /*
369                 Check the nodes under the player to see from which node the
370                 player is sneaking from, if any.
371         */
372         {
373                 v3s16 pos_i_bottom = floatToInt(position - v3f(0,BS/2,0), BS);
374                 v2f player_p2df(position.X, position.Z);
375                 f32 min_distance_f = 100000.0*BS;
376                 // If already seeking from some node, compare to it.
377                 /*if(m_sneak_node_exists)
378                 {
379                         v3f sneaknode_pf = intToFloat(m_sneak_node, BS);
380                         v2f sneaknode_p2df(sneaknode_pf.X, sneaknode_pf.Z);
381                         f32 d_horiz_f = player_p2df.getDistanceFrom(sneaknode_p2df);
382                         f32 d_vert_f = fabs(sneaknode_pf.Y + BS*0.5 - position.Y);
383                         // Ignore if player is not on the same level (likely dropped)
384                         if(d_vert_f < 0.15*BS)
385                                 min_distance_f = d_horiz_f;
386                 }*/
387                 v3s16 new_sneak_node = m_sneak_node;
388                 for(s16 x=-1; x<=1; x++)
389                 for(s16 z=-1; z<=1; z++)
390                 {
391                         v3s16 p = pos_i_bottom + v3s16(x,0,z);
392                         v3f pf = intToFloat(p, BS);
393                         v2f node_p2df(pf.X, pf.Z);
394                         f32 distance_f = player_p2df.getDistanceFrom(node_p2df);
395                         f32 max_axis_distance_f = MYMAX(
396                                         fabs(player_p2df.X-node_p2df.X),
397                                         fabs(player_p2df.Y-node_p2df.Y));
398                                         
399                         if(distance_f > min_distance_f ||
400                                         max_axis_distance_f > 0.5*BS + sneak_max + 0.1*BS)
401                                 continue;
402
403                         try{
404                                 // The node to be sneaked on has to be walkable
405                                 if(nodemgr->get(map.getNode(p)).walkable == false)
406                                         continue;
407                                 // And the node above it has to be nonwalkable
408                                 if(nodemgr->get(map.getNode(p+v3s16(0,1,0))).walkable == true)
409                                         continue;
410                         }
411                         catch(InvalidPositionException &e)
412                         {
413                                 continue;
414                         }
415
416                         min_distance_f = distance_f;
417                         new_sneak_node = p;
418                 }
419                 
420                 bool sneak_node_found = (min_distance_f < 100000.0*BS*0.9);
421                 
422                 if(control.sneak && m_sneak_node_exists)
423                 {
424                         if(sneak_node_found)
425                                 m_sneak_node = new_sneak_node;
426                 }
427                 else
428                 {
429                         m_sneak_node = new_sneak_node;
430                         m_sneak_node_exists = sneak_node_found;
431                 }
432
433                 /*
434                         If sneaking, the player's collision box can be in air, so
435                         this has to be set explicitly
436                 */
437                 if(sneak_node_found && control.sneak)
438                         touching_ground = true;
439         }
440         
441         /*
442                 Set new position
443         */
444         setPosition(position);
445         
446         /*
447                 Report collisions
448         */
449         if(collision_info)
450         {
451                 // Report fall collision
452                 if(old_speed.Y < m_speed.Y - 0.1 && !standing_on_unloaded)
453                 {
454                         CollisionInfo info;
455                         info.t = COLLISION_FALL;
456                         info.speed = m_speed.Y - old_speed.Y;
457                         collision_info->push_back(info);
458                 }
459         }
460
461         if(!touching_ground_was && touching_ground){
462                 MtEvent *e = new SimpleTriggerEvent("PlayerRegainGround");
463                 m_gamedef->event()->put(e);
464         }
465
466         {
467                 camera_barely_in_ceiling = false;
468                 v3s16 camera_np = floatToInt(getEyePosition(), BS);
469                 MapNode n = map.getNodeNoEx(camera_np);
470                 if(n.getContent() != CONTENT_IGNORE){
471                         if(nodemgr->get(n).walkable){
472                                 camera_barely_in_ceiling = true;
473                         }
474                 }
475         }
476 }
477
478 void LocalPlayer::move(f32 dtime, Map &map, f32 pos_max_d)
479 {
480         move(dtime, map, pos_max_d, NULL);
481 }
482
483 void LocalPlayer::applyControl(float dtime)
484 {
485         // Clear stuff
486         swimming_up = false;
487
488         // Random constants
489         f32 walk_acceleration = 4.0 * BS;
490         f32 walkspeed_max = 4.0 * BS;
491         
492         setPitch(control.pitch);
493         setYaw(control.yaw);
494         
495         v3f move_direction = v3f(0,0,1);
496         move_direction.rotateXZBy(getYaw());
497         
498         v3f speed = v3f(0,0,0);
499         
500         bool fly_allowed = m_gamedef->checkLocalPrivilege("fly");
501         bool fast_allowed = m_gamedef->checkLocalPrivilege("fast");
502
503         bool free_move = fly_allowed && g_settings->getBool("free_move");
504         bool fast_move = fast_allowed && g_settings->getBool("fast_move");
505         bool continuous_forward = g_settings->getBool("continuous_forward");
506
507         if(free_move || is_climbing)
508         {
509                 v3f speed = getSpeed();
510                 speed.Y = 0;
511                 setSpeed(speed);
512         }
513
514         // Whether superspeed mode is used or not
515         bool superspeed = false;
516         
517         // If free movement and fast movement, always move fast
518         if(free_move && fast_move)
519                 superspeed = true;
520         
521         // Auxiliary button 1 (E)
522         if(control.aux1)
523         {
524                 if(free_move)
525                 {
526                         // In free movement mode, aux1 descends
527                         v3f speed = getSpeed();
528                         if(fast_move)
529                                 speed.Y = -20*BS;
530                         else
531                                 speed.Y = -walkspeed_max;
532                         setSpeed(speed);
533                 }
534                 else if(is_climbing)
535                 {
536                         v3f speed = getSpeed();
537                         speed.Y = -3*BS;
538                         setSpeed(speed);
539                 }
540                 else
541                 {
542                         // If not free movement but fast is allowed, aux1 is
543                         // "Turbo button"
544                         if(fast_move)
545                                 superspeed = true;
546                 }
547         }
548
549         if(continuous_forward)
550                 speed += move_direction;
551
552         if(control.up)
553         {
554                 if(continuous_forward)
555                         superspeed = true;
556                 else
557                         speed += move_direction;
558         }
559         if(control.down)
560         {
561                 speed -= move_direction;
562         }
563         if(control.left)
564         {
565                 speed += move_direction.crossProduct(v3f(0,1,0));
566         }
567         if(control.right)
568         {
569                 speed += move_direction.crossProduct(v3f(0,-1,0));
570         }
571         if(control.jump)
572         {
573                 if(free_move)
574                 {
575                         v3f speed = getSpeed();
576                         if(fast_move)
577                                 speed.Y = 20*BS;
578                         else
579                                 speed.Y = walkspeed_max;
580                         setSpeed(speed);
581                 }
582                 else if(touching_ground)
583                 {
584                         /*
585                                 NOTE: The d value in move() affects jump height by
586                                 raising the height at which the jump speed is kept
587                                 at its starting value
588                         */
589                         v3f speed = getSpeed();
590                         if(speed.Y >= -0.5*BS)
591                         {
592                                 speed.Y = 6.5*BS;
593                                 setSpeed(speed);
594                                 
595                                 MtEvent *e = new SimpleTriggerEvent("PlayerJump");
596                                 m_gamedef->event()->put(e);
597                         }
598                 }
599                 // Use the oscillating value for getting out of water
600                 // (so that the player doesn't fly on the surface)
601                 else if(in_water)
602                 {
603                         v3f speed = getSpeed();
604                         speed.Y = 1.5*BS;
605                         setSpeed(speed);
606                         swimming_up = true;
607                 }
608                 else if(is_climbing)
609                 {
610                         v3f speed = getSpeed();
611                         speed.Y = 3*BS;
612                         setSpeed(speed);
613                 }
614         }
615
616         // The speed of the player (Y is ignored)
617         if(superspeed)
618                 speed = speed.normalize() * walkspeed_max * 5.0;
619         else if(control.sneak)
620                 speed = speed.normalize() * walkspeed_max / 3.0;
621         else
622                 speed = speed.normalize() * walkspeed_max;
623         
624         f32 inc = walk_acceleration * BS * dtime;
625         
626         // Faster acceleration if fast and free movement
627         if(free_move && fast_move)
628                 inc = walk_acceleration * BS * dtime * 10;
629         
630         // Accelerate to target speed with maximum increment
631         accelerate(speed, inc);
632 }
633
634 v3s16 LocalPlayer::getStandingNodePos()
635 {
636         if(m_sneak_node_exists)
637                 return m_sneak_node;
638         return floatToInt(getPosition(), BS);
639 }
640