]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/raycast.cpp
Fix hash implementation for SerializedBlockCache
[dragonfireclient.git] / src / raycast.cpp
index 58102c9936b850ac1771fd356f703425607faebf..ebc40235db815042b8d785732af2a9720a5230c1 100644 (file)
@@ -17,11 +17,59 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
+#include "raycast.h"
 #include "irr_v3d.h"
 #include "irr_aabb3d.h"
+#include "constants.h"
+
+bool RaycastSort::operator() (const PointedThing &pt1,
+       const PointedThing &pt2) const
+{
+       // "nothing" can not be sorted
+       assert(pt1.type != POINTEDTHING_NOTHING);
+       assert(pt2.type != POINTEDTHING_NOTHING);
+       f32 pt1_distSq = pt1.distanceSq;
+
+       // Add some bonus when one of them is an object
+       if (pt1.type != pt2.type) {
+               if (pt1.type == POINTEDTHING_OBJECT)
+                       pt1_distSq -= BS * BS;
+               else if (pt2.type == POINTEDTHING_OBJECT)
+                       pt1_distSq += BS * BS;
+       }
+
+       // returns false if pt1 is nearer than pt2
+       if (pt1_distSq < pt2.distanceSq) {
+               return false;
+       }
+
+       if (pt1_distSq == pt2.distanceSq) {
+               // Sort them to allow only one order
+               if (pt1.type == POINTEDTHING_OBJECT)
+                       return (pt2.type == POINTEDTHING_OBJECT
+                               && pt1.object_id < pt2.object_id);
+
+               return (pt2.type == POINTEDTHING_OBJECT
+                               || pt1.node_undersurface < pt2.node_undersurface);
+       }
+       return true;
+}
+
+
+RaycastState::RaycastState(const core::line3d<f32> &shootline,
+       bool objects_pointable, bool liquids_pointable) :
+       m_shootline(shootline),
+       m_iterator(shootline.start / BS, shootline.getVector() / BS),
+       m_previous_node(m_iterator.m_current_node_pos),
+       m_objects_pointable(objects_pointable),
+       m_liquids_pointable(liquids_pointable)
+{
+}
+
 
 bool boxLineCollision(const aabb3f &box, const v3f &start,
-               const v3f &dir, v3f *collision_point, v3s16 *collision_normal) {
+       const v3f &dir, v3f *collision_point, v3s16 *collision_normal)
+{
        if (box.isPointInside(start)) {
                *collision_point = start;
                collision_normal->set(0, 0, 0);