]> git.lizzy.rs Git - dragonfireclient.git/blob - src/util/pointedthing.cpp
Expose getPointedThing to Lua
[dragonfireclient.git] / src / util / pointedthing.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser 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 "pointedthing.h"
21
22 #include "serialize.h"
23 #include "../exceptions.h"
24 #include <sstream>
25
26 PointedThing::PointedThing(const v3s16 &under, const v3s16 &above,
27         const v3s16 &real_under, const v3f &point, const v3s16 &normal,
28         f32 distSq):
29         type(POINTEDTHING_NODE),
30         node_undersurface(under),
31         node_abovesurface(above),
32         node_real_undersurface(real_under),
33         intersection_point(point),
34         intersection_normal(normal),
35         distanceSq(distSq)
36 {}
37
38 PointedThing::PointedThing(s16 id, const v3f &point, const v3s16 &normal,
39         f32 distSq) :
40         type(POINTEDTHING_OBJECT),
41         object_id(id),
42         intersection_point(point),
43         intersection_normal(normal),
44         distanceSq(distSq)
45 {}
46
47 std::string PointedThing::dump() const
48 {
49         std::ostringstream os(std::ios::binary);
50         switch (type) {
51         case POINTEDTHING_NOTHING:
52                 os << "[nothing]";
53                 break;
54         case POINTEDTHING_NODE:
55         {
56                 const v3s16 &u = node_undersurface;
57                 const v3s16 &a = node_abovesurface;
58                 os << "[node under=" << u.X << "," << u.Y << "," << u.Z << " above="
59                         << a.X << "," << a.Y << "," << a.Z << "]";
60         }
61                 break;
62         case POINTEDTHING_OBJECT:
63                 os << "[object " << object_id << "]";
64                 break;
65         default:
66                 os << "[unknown PointedThing]";
67         }
68         return os.str();
69 }
70
71 void PointedThing::serialize(std::ostream &os) const
72 {
73         writeU8(os, 0); // version
74         writeU8(os, (u8)type);
75         switch (type) {
76         case POINTEDTHING_NOTHING:
77                 break;
78         case POINTEDTHING_NODE:
79                 writeV3S16(os, node_undersurface);
80                 writeV3S16(os, node_abovesurface);
81                 break;
82         case POINTEDTHING_OBJECT:
83                 writeS16(os, object_id);
84                 break;
85         }
86 }
87
88 void PointedThing::deSerialize(std::istream &is)
89 {
90         int version = readU8(is);
91         if (version != 0) throw SerializationError(
92                         "unsupported PointedThing version");
93         type = (PointedThingType) readU8(is);
94         switch (type) {
95         case POINTEDTHING_NOTHING:
96                 break;
97         case POINTEDTHING_NODE:
98                 node_undersurface = readV3S16(is);
99                 node_abovesurface = readV3S16(is);
100                 break;
101         case POINTEDTHING_OBJECT:
102                 object_id = readS16(is);
103                 break;
104         default:
105                 throw SerializationError("unsupported PointedThingType");
106         }
107 }
108
109 bool PointedThing::operator==(const PointedThing &pt2) const
110 {
111         if (type != pt2.type)
112         {
113                 return false;
114         }
115         if (type == POINTEDTHING_NODE)
116         {
117                 if ((node_undersurface != pt2.node_undersurface)
118                                 || (node_abovesurface != pt2.node_abovesurface)
119                                 || (node_real_undersurface != pt2.node_real_undersurface))
120                         return false;
121         }
122         else if (type == POINTEDTHING_OBJECT)
123         {
124                 if (object_id != pt2.object_id)
125                         return false;
126         }
127         return true;
128 }
129
130 bool PointedThing::operator!=(const PointedThing &pt2) const
131 {
132         return !(*this == pt2);
133 }