]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/collision.cpp
Handle particle spawners in env and delete expired ids
[dragonfireclient.git] / src / collision.cpp
index a2d17d51ab50b554884ffb112807e084a4cb76ed..74c0c25c3e08d7ac7f7a7c6a4b583eaa5827658f 100644 (file)
@@ -185,6 +185,13 @@ bool wouldCollideWithCeiling(
        return false;
 }
 
+static inline void getNeighborConnectingFace(v3s16 p, INodeDefManager *nodedef,
+               Map *map, MapNode n, int v, int *neighbors)
+{
+       MapNode n2 = map->getNodeNoEx(p);
+       if (nodedef->nodeboxConnects(n, n2, v))
+               *neighbors |= v;
+}
 
 collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
                f32 pos_max_d, const aabb3f &box_0,
@@ -235,7 +242,7 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
        std::vector<v3s16> node_positions;
        {
        //TimeTaker tt2("collisionMoveSimple collect boxes");
-    ScopeProfiler sp(g_profiler, "collisionMoveSimple collect boxes avg", SPT_AVG);
+       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);
@@ -248,9 +255,8 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
 
        bool any_position_valid = false;
 
-       // The order is important here, must be y first
-       for(s16 y = max_y; y >= min_y; y--)
        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);
@@ -262,12 +268,41 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
                        // Object collides into walkable nodes
 
                        any_position_valid = true;
-                       const ContentFeatures &f = gamedef->getNodeDefManager()->get(n);
+                       INodeDefManager *nodedef = gamedef->getNodeDefManager();
+                       const ContentFeatures &f = nodedef->get(n);
                        if(f.walkable == false)
                                continue;
                        int n_bouncy_value = itemgroup_get(f.groups, "bouncy");
 
-                       std::vector<aabb3f> nodeboxes = n.getCollisionBoxes(gamedef->ndef());
+                       int neighbors = 0;
+                       if (f.drawtype == NDT_NODEBOX && f.node_box.type == NODEBOX_CONNECTED) {
+                               v3s16 p2 = p;
+
+                               p2.Y++;
+                               getNeighborConnectingFace(p2, nodedef, map, n, 1, &neighbors);
+
+                               p2 = p;
+                               p2.Y--;
+                               getNeighborConnectingFace(p2, nodedef, map, n, 2, &neighbors);
+
+                               p2 = p;
+                               p2.Z--;
+                               getNeighborConnectingFace(p2, nodedef, map, n, 4, &neighbors);
+
+                               p2 = p;
+                               p2.X--;
+                               getNeighborConnectingFace(p2, nodedef, map, n, 8, &neighbors);
+
+                               p2 = p;
+                               p2.Z++;
+                               getNeighborConnectingFace(p2, nodedef, map, n, 16, &neighbors);
+
+                               p2 = p;
+                               p2.X++;
+                               getNeighborConnectingFace(p2, nodedef, map, n, 32, &neighbors);
+                       }
+                       std::vector<aabb3f> nodeboxes;
+                       n.getCollisionBoxes(gamedef->ndef(), &nodeboxes, neighbors);
                        for(std::vector<aabb3f>::iterator
                                        i = nodeboxes.begin();
                                        i != nodeboxes.end(); ++i)
@@ -297,8 +332,10 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
 
        // Do not move if world has not loaded yet, since custom node boxes
        // are not available for collision detection.
-       if (!any_position_valid)
+       if (!any_position_valid) {
+               *speed_f = v3f(0, 0, 0);
                return result;
+       }
 
        } // tt2
 
@@ -383,13 +420,12 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
 
        while(dtime > BS * 1e-10) {
                //TimeTaker tt3("collisionMoveSimple dtime loop");
-        ScopeProfiler sp(g_profiler, "collisionMoveSimple dtime loop avg", SPT_AVG);
+               ScopeProfiler sp(g_profiler, "collisionMoveSimple dtime loop avg", SPT_AVG);
 
                // Avoid infinite loop
                loopcount++;
                if (loopcount >= 100) {
                        warningstream << "collisionMoveSimple: Loop count exceeded, aborting to avoid infiniite loop" << std::endl;
-                       dtime = 0;
                        break;
                }
 
@@ -399,23 +435,21 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
 
                int nearest_collided = -1;
                f32 nearest_dtime = dtime;
-               u32 nearest_boxindex = -1;
+               int nearest_boxindex = -1;
 
                /*
                        Go through every nodebox, find nearest collision
                */
                for (u32 boxindex = 0; boxindex < cboxes.size(); boxindex++) {
+                       // Ignore if already stepped up this nodebox.
+                       if(is_step_up[boxindex])
+                               continue;
+
                        // Find nearest collision of the two boxes (raytracing-like)
                        f32 dtime_tmp;
                        int collided = axisAlignedCollision(
                                        cboxes[boxindex], movingbox, *speed_f, d, &dtime_tmp);
 
-                       // Ignore if already stepped up this nodebox.
-                       if (is_step_up[boxindex]) {
-                               pos_f->Y += (cboxes[boxindex].MaxEdge.Y - movingbox.MinEdge.Y);
-                               continue;
-                       }
-
                        if (collided == -1 || dtime_tmp >= nearest_dtime)
                                continue;
 
@@ -465,12 +499,10 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
                                is_collision = false;
 
                        CollisionInfo info;
-                       if (is_object[nearest_boxindex]) {
+                       if (is_object[nearest_boxindex])
                                info.type = COLLISION_OBJECT;
-                               result.standing_on_object = true;
-                       } else {
+                       else
                                info.type = COLLISION_NODE;
-                       }
 
                        info.node_p = node_positions[nearest_boxindex];
                        info.bouncy = bouncy;
@@ -488,13 +520,12 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
                                        speed_f->X = 0;
                                result.collides = true;
                                result.collides_xz = true;
-                       } else if(nearest_collided == 1) { // Y
-                               if (fabs(speed_f->Y) > BS * 3) {
+                       }
+                       else if(nearest_collided == 1) { // Y
+                               if (fabs(speed_f->Y) > BS * 3)
                                        speed_f->Y *= bounce;
-                               } else {
+                               else
                                        speed_f->Y = 0;
-                                       result.touching_ground = true;
-                               }
                                result.collides = true;
                        } else if(nearest_collided == 2) { // Z
                                if (fabs(speed_f->Z) > BS * 3)
@@ -515,5 +546,43 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
                }
        }
 
+       /*
+               Final touches: Check if standing on ground, step up stairs.
+       */
+       aabb3f box = box_0;
+       box.MinEdge += *pos_f;
+       box.MaxEdge += *pos_f;
+       for (u32 boxindex = 0; boxindex < cboxes.size(); boxindex++) {
+               const aabb3f& cbox = cboxes[boxindex];
+
+               /*
+                       See if the object is touching ground.
+
+                       Object touches ground if object's minimum Y is near node's
+                       maximum Y and object's X-Z-area overlaps with the node's
+                       X-Z-area.
+
+                       Use 0.15*BS so that it is easier to get on a node.
+               */
+               if (cbox.MaxEdge.X - d > box.MinEdge.X && cbox.MinEdge.X + d < box.MaxEdge.X &&
+                               cbox.MaxEdge.Z - d > box.MinEdge.Z &&
+                               cbox.MinEdge.Z + d < box.MaxEdge.Z) {
+                       if (is_step_up[boxindex]) {
+                               pos_f->Y += (cbox.MaxEdge.Y - box.MinEdge.Y);
+                               box = box_0;
+                               box.MinEdge += *pos_f;
+                               box.MaxEdge += *pos_f;
+                       }
+                       if (fabs(cbox.MaxEdge.Y - box.MinEdge.Y) < 0.15 * BS) {
+                               result.touching_ground = true;
+
+                               if (is_object[boxindex])
+                                       result.standing_on_object = true;
+                               if (is_unloaded[boxindex])
+                                       result.standing_on_unloaded = true;
+                       }
+               }
+       }
+
        return result;
 }