]> git.lizzy.rs Git - minetest.git/blobdiff - src/util/pointedthing.cpp
Simplify bit flip in sha1.cpp
[minetest.git] / src / util / pointedthing.cpp
index d5cc8100376d0972e93894f5d335125173393a2e..b906264d0afeddd4129e434f0dbdcc668940a435 100644 (file)
@@ -1,6 +1,6 @@
 /*
 Minetest
-Copyright (C) 2010-2012 celeron55, Perttu Ahola <celeron55@gmail.com>
+Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU Lesser General Public License as published by
@@ -20,36 +20,51 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "pointedthing.h"
 
 #include "serialize.h"
+#include "exceptions.h"
 #include <sstream>
 
-PointedThing::PointedThing():
-       type(POINTEDTHING_NOTHING),
-       node_undersurface(0,0,0),
-       node_abovesurface(0,0,0),
-       object_id(-1)
+PointedThing::PointedThing(const v3s16 &under, const v3s16 &above,
+       const v3s16 &real_under, const v3f &point, const v3s16 &normal,
+       u16 box_id, f32 distSq):
+       type(POINTEDTHING_NODE),
+       node_undersurface(under),
+       node_abovesurface(above),
+       node_real_undersurface(real_under),
+       intersection_point(point),
+       intersection_normal(normal),
+       box_id(box_id),
+       distanceSq(distSq)
+{}
+
+PointedThing::PointedThing(s16 id, const v3f &point, const v3s16 &normal,
+       f32 distSq) :
+       type(POINTEDTHING_OBJECT),
+       object_id(id),
+       intersection_point(point),
+       intersection_normal(normal),
+       distanceSq(distSq)
 {}
 
 std::string PointedThing::dump() const
 {
        std::ostringstream os(std::ios::binary);
-       if(type == POINTEDTHING_NOTHING)
-       {
-               os<<"[nothing]";
-       }
-       else if(type == POINTEDTHING_NODE)
+       switch (type) {
+       case POINTEDTHING_NOTHING:
+               os << "[nothing]";
+               break;
+       case POINTEDTHING_NODE:
        {
                const v3s16 &u = node_undersurface;
                const v3s16 &a = node_abovesurface;
-               os<<"[node under="<<u.X<<","<<u.Y<<","<<u.Z
-                       << " above="<<a.X<<","<<a.Y<<","<<a.Z<<"]";
-       }
-       else if(type == POINTEDTHING_OBJECT)
-       {
-               os<<"[object "<<object_id<<"]";
+               os << "[node under=" << u.X << "," << u.Y << "," << u.Z << " above="
+                       << a.X << "," << a.Y << "," << a.Z << "]";
        }
-       else
-       {
-               os<<"[unknown PointedThing]";
+               break;
+       case POINTEDTHING_OBJECT:
+               os << "[object " << object_id << "]";
+               break;
+       default:
+               os << "[unknown PointedThing]";
        }
        return os.str();
 }
@@ -58,61 +73,56 @@ void PointedThing::serialize(std::ostream &os) const
 {
        writeU8(os, 0); // version
        writeU8(os, (u8)type);
-       if(type == POINTEDTHING_NOTHING)
-       {
-               // nothing
-       }
-       else if(type == POINTEDTHING_NODE)
-       {
+       switch (type) {
+       case POINTEDTHING_NOTHING:
+               break;
+       case POINTEDTHING_NODE:
                writeV3S16(os, node_undersurface);
                writeV3S16(os, node_abovesurface);
-       }
-       else if(type == POINTEDTHING_OBJECT)
-       {
+               break;
+       case POINTEDTHING_OBJECT:
                writeS16(os, object_id);
+               break;
        }
 }
 
 void PointedThing::deSerialize(std::istream &is)
 {
        int version = readU8(is);
-       if(version != 0) throw SerializationError(
+       if (version != 0) throw SerializationError(
                        "unsupported PointedThing version");
        type = (PointedThingType) readU8(is);
-       if(type == POINTEDTHING_NOTHING)
-       {
-               // nothing
-       }
-       else if(type == POINTEDTHING_NODE)
-       {
+       switch (type) {
+       case POINTEDTHING_NOTHING:
+               break;
+       case POINTEDTHING_NODE:
                node_undersurface = readV3S16(is);
                node_abovesurface = readV3S16(is);
-       }
-       else if(type == POINTEDTHING_OBJECT)
-       {
+               break;
+       case POINTEDTHING_OBJECT:
                object_id = readS16(is);
-       }
-       else
-       {
-               throw SerializationError(
-                       "unsupported PointedThingType");
+               break;
+       default:
+               throw SerializationError("unsupported PointedThingType");
        }
 }
 
 bool PointedThing::operator==(const PointedThing &pt2) const
 {
-       if(type != pt2.type)
+       if (type != pt2.type)
+       {
                return false;
-       if(type == POINTEDTHING_NODE)
+       }
+       if (type == POINTEDTHING_NODE)
        {
-               if(node_undersurface != pt2.node_undersurface)
-                       return false;
-               if(node_abovesurface != pt2.node_abovesurface)
+               if ((node_undersurface != pt2.node_undersurface)
+                               || (node_abovesurface != pt2.node_abovesurface)
+                               || (node_real_undersurface != pt2.node_real_undersurface))
                        return false;
        }
-       else if(type == POINTEDTHING_OBJECT)
+       else if (type == POINTEDTHING_OBJECT)
        {
-               if(object_id != pt2.object_id)
+               if (object_id != pt2.object_id)
                        return false;
        }
        return true;
@@ -122,4 +132,3 @@ bool PointedThing::operator!=(const PointedThing &pt2) const
 {
        return !(*this == pt2);
 }
-