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