]> git.lizzy.rs Git - dragonfireclient.git/blob - src/util/pointedthing.cpp
Fix myfloor(0.0)
[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():
27         type(POINTEDTHING_NOTHING),
28         node_undersurface(0,0,0),
29         node_abovesurface(0,0,0),
30         node_real_undersurface(0,0,0),
31         intersection_point(0,0,0),
32         intersection_normal(0,0,0),
33         object_id(-1)
34 {}
35
36 std::string PointedThing::dump() const
37 {
38         std::ostringstream os(std::ios::binary);
39         if (type == POINTEDTHING_NOTHING) {
40                 os<<"[nothing]";
41         } else if (type == POINTEDTHING_NODE) {
42                 const v3s16 &u = node_undersurface;
43                 const v3s16 &a = node_abovesurface;
44                 os<<"[node under="<<u.X<<","<<u.Y<<","<<u.Z
45                         << " above="<<a.X<<","<<a.Y<<","<<a.Z<<"]";
46         } else if (type == POINTEDTHING_OBJECT) {
47                 os<<"[object "<<object_id<<"]";
48         } else {
49                 os<<"[unknown PointedThing]";
50         }
51         return os.str();
52 }
53
54 void PointedThing::serialize(std::ostream &os) const
55 {
56         writeU8(os, 0); // version
57         writeU8(os, (u8)type);
58         switch (type) {
59         case POINTEDTHING_NOTHING:
60                 break;
61         case POINTEDTHING_NODE:
62                 writeV3S16(os, node_undersurface);
63                 writeV3S16(os, node_abovesurface);
64                 break;
65         case POINTEDTHING_OBJECT:
66                 writeS16(os, object_id);
67                 break;
68         }
69 }
70
71 void PointedThing::deSerialize(std::istream &is)
72 {
73         int version = readU8(is);
74         if (version != 0) throw SerializationError(
75                         "unsupported PointedThing version");
76         type = (PointedThingType) readU8(is);
77         switch (type) {
78         case POINTEDTHING_NOTHING:
79                 break;
80         case POINTEDTHING_NODE:
81                 node_undersurface = readV3S16(is);
82                 node_abovesurface = readV3S16(is);
83                 break;
84         case POINTEDTHING_OBJECT:
85                 object_id = readS16(is);
86                 break;
87         default:
88                 throw SerializationError("unsupported PointedThingType");
89         }
90 }
91
92 bool PointedThing::operator==(const PointedThing &pt2) const
93 {
94         if (type != pt2.type)
95         {
96                 return false;
97         }
98         if (type == POINTEDTHING_NODE)
99         {
100                 if ((node_undersurface != pt2.node_undersurface)
101                                 || (node_abovesurface != pt2.node_abovesurface)
102                                 || (node_real_undersurface != pt2.node_real_undersurface))
103                         return false;
104         }
105         else if (type == POINTEDTHING_OBJECT)
106         {
107                 if (object_id != pt2.object_id)
108                         return false;
109         }
110         return true;
111 }
112
113 bool PointedThing::operator!=(const PointedThing &pt2) const
114 {
115         return !(*this == pt2);
116 }
117