]> git.lizzy.rs Git - minetest.git/blob - src/util/pointedthing.cpp
Fix typos and en_US/en_GB inconsistency in various files (#12902)
[minetest.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 v3f &normal,
28         u16 box_id, 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         box_id(box_id),
36         distanceSq(distSq)
37 {}
38
39 PointedThing::PointedThing(u16 id, const v3f &point,
40   const v3f &normal, const v3f &raw_normal, f32 distSq) :
41         type(POINTEDTHING_OBJECT),
42         object_id(id),
43         intersection_point(point),
44         intersection_normal(normal),
45         raw_intersection_normal(raw_normal),
46         distanceSq(distSq)
47 {}
48
49 std::string PointedThing::dump() const
50 {
51         std::ostringstream os(std::ios::binary);
52         switch (type) {
53         case POINTEDTHING_NOTHING:
54                 os << "[nothing]";
55                 break;
56         case POINTEDTHING_NODE:
57         {
58                 const v3s16 &u = node_undersurface;
59                 const v3s16 &a = node_abovesurface;
60                 os << "[node under=" << u.X << "," << u.Y << "," << u.Z << " above="
61                         << a.X << "," << a.Y << "," << a.Z << "]";
62         }
63                 break;
64         case POINTEDTHING_OBJECT:
65                 os << "[object " << object_id << "]";
66                 break;
67         default:
68                 os << "[unknown PointedThing]";
69         }
70         return os.str();
71 }
72
73 void PointedThing::serialize(std::ostream &os) const
74 {
75         writeU8(os, 0); // version
76         writeU8(os, (u8)type);
77         switch (type) {
78         case POINTEDTHING_NOTHING:
79                 break;
80         case POINTEDTHING_NODE:
81                 writeV3S16(os, node_undersurface);
82                 writeV3S16(os, node_abovesurface);
83                 break;
84         case POINTEDTHING_OBJECT:
85                 writeU16(os, object_id);
86                 break;
87         }
88 }
89
90 void PointedThing::deSerialize(std::istream &is)
91 {
92         int version = readU8(is);
93         if (version != 0) throw SerializationError(
94                         "unsupported PointedThing version");
95         type = (PointedThingType) readU8(is);
96         switch (type) {
97         case POINTEDTHING_NOTHING:
98                 break;
99         case POINTEDTHING_NODE:
100                 node_undersurface = readV3S16(is);
101                 node_abovesurface = readV3S16(is);
102                 break;
103         case POINTEDTHING_OBJECT:
104                 object_id = readU16(is);
105                 break;
106         default:
107                 throw SerializationError("unsupported PointedThingType");
108         }
109 }
110
111 bool PointedThing::operator==(const PointedThing &pt2) const
112 {
113         if (type != pt2.type)
114         {
115                 return false;
116         }
117         if (type == POINTEDTHING_NODE)
118         {
119                 if ((node_undersurface != pt2.node_undersurface)
120                                 || (node_abovesurface != pt2.node_abovesurface)
121                                 || (node_real_undersurface != pt2.node_real_undersurface))
122                         return false;
123         }
124         else if (type == POINTEDTHING_OBJECT)
125         {
126                 if (object_id != pt2.object_id)
127                         return false;
128         }
129         return true;
130 }
131
132 bool PointedThing::operator!=(const PointedThing &pt2) const
133 {
134         return !(*this == pt2);
135 }