LocalPlayer::accelerateHorizontal: cleanups
[oweals/minetest.git] / src / collision.cpp
index 8e5dbcc9b4497e7e45f9fd824fe26821afa6a3a9..9d0e8b361f78f38f2f4dea614a6d9e95233d0dd2 100644 (file)
@@ -22,11 +22,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "map.h"
 #include "nodedef.h"
 #include "gamedef.h"
-#include "log.h"
-#include "environment.h"
+#ifndef SERVER
+#include "clientenvironment.h"
+#endif
+#include "serverenvironment.h"
 #include "serverobject.h"
-#include <vector>
-#include <set>
 #include "util/timetaker.h"
 #include "profiler.h"
 
@@ -39,7 +39,6 @@ struct NearbyCollisionInfo {
        NearbyCollisionInfo(bool is_ul, bool is_obj, int bouncy,
                        const v3s16 &pos, const aabb3f &box) :
                is_unloaded(is_ul),
-               is_step_up(false),
                is_object(is_obj),
                bouncy(bouncy),
                position(pos),
@@ -47,7 +46,7 @@ struct NearbyCollisionInfo {
        {}
 
        bool is_unloaded;
-       bool is_step_up;
+       bool is_step_up = false;
        bool is_object;
        int bouncy;
        v3s16 position;
@@ -258,20 +257,25 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
        //TimeTaker tt2("collisionMoveSimple collect boxes");
        ScopeProfiler sp(g_profiler, "collisionMoveSimple collect boxes avg", SPT_AVG);
 
-       v3s16 oldpos_i = floatToInt(*pos_f, BS);
-       v3s16 newpos_i = floatToInt(*pos_f + *speed_f * dtime, BS);
-       s16 min_x = MYMIN(oldpos_i.X, newpos_i.X) + (box_0.MinEdge.X / BS) - 1;
-       s16 min_y = MYMIN(oldpos_i.Y, newpos_i.Y) + (box_0.MinEdge.Y / BS) - 1;
-       s16 min_z = MYMIN(oldpos_i.Z, newpos_i.Z) + (box_0.MinEdge.Z / BS) - 1;
-       s16 max_x = MYMAX(oldpos_i.X, newpos_i.X) + (box_0.MaxEdge.X / BS) + 1;
-       s16 max_y = MYMAX(oldpos_i.Y, newpos_i.Y) + (box_0.MaxEdge.Y / BS) + 1;
-       s16 max_z = MYMAX(oldpos_i.Z, newpos_i.Z) + (box_0.MaxEdge.Z / BS) + 1;
+       v3f newpos_f = *pos_f + *speed_f * dtime;
+       v3f minpos_f(
+               MYMIN(pos_f->X, newpos_f.X),
+               MYMIN(pos_f->Y, newpos_f.Y) + 0.01 * BS, // bias rounding, player often at +/-n.5
+               MYMIN(pos_f->Z, newpos_f.Z)
+       );
+       v3f maxpos_f(
+               MYMAX(pos_f->X, newpos_f.X),
+               MYMAX(pos_f->Y, newpos_f.Y),
+               MYMAX(pos_f->Z, newpos_f.Z)
+       );
+       v3s16 min = floatToInt(minpos_f + box_0.MinEdge, BS) - v3s16(1, 1, 1);
+       v3s16 max = floatToInt(maxpos_f + box_0.MaxEdge, BS) + v3s16(1, 1, 1);
 
        bool any_position_valid = false;
 
-       for(s16 x = min_x; x <= max_x; x++)
-       for(s16 y = min_y; y <= max_y; y++)
-       for(s16 z = min_z; z <= max_z; z++)
+       for(s16 x = min.X; x <= max.X; x++)
+       for(s16 y = min.Y; y <= max.Y; y++)
+       for(s16 z = min.Z; z <= max.Z; z++)
        {
                v3s16 p(x,y,z);
 
@@ -469,7 +473,6 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
                                                        d));
 
                        // Get bounce multiplier
-                       bool bouncy = (nearest_info.bouncy >= 1);
                        float bounce = -(float)nearest_info.bouncy / 100.0;
 
                        // Move to the point of collision and reduce dtime by nearest_dtime
@@ -499,7 +502,6 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
                                info.type = COLLISION_NODE;
 
                        info.node_p = nearest_info.position;
-                       info.bouncy = bouncy;
                        info.old_speed = *speed_f;
 
                        // Set the speed component that caused the collision to zero
@@ -513,7 +515,6 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
                                else
                                        speed_f->X = 0;
                                result.collides = true;
-                               result.collides_xz = true;
                        } else if (nearest_collided == 1) { // Y
                                if(fabs(speed_f->Y) > BS * 3)
                                        speed_f->Y *= bounce;
@@ -526,7 +527,6 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
                                else
                                        speed_f->Z = 0;
                                result.collides = true;
-                               result.collides_xz = true;
                        }
 
                        info.new_speed = *speed_f;
@@ -572,8 +572,6 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
 
                                if (box_info.is_object)
                                        result.standing_on_object = true;
-                               if (box_info.is_unloaded)
-                                       result.standing_on_unloaded = true;
                        }
                }
        }