3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
20 #include "pointedthing.h"
22 #include "serialize.h"
25 PointedThing::PointedThing():
26 type(POINTEDTHING_NOTHING),
27 node_undersurface(0,0,0),
28 node_abovesurface(0,0,0),
32 std::string PointedThing::dump() const
34 std::ostringstream os(std::ios::binary);
35 if(type == POINTEDTHING_NOTHING)
39 else if(type == POINTEDTHING_NODE)
41 const v3s16 &u = node_undersurface;
42 const v3s16 &a = node_abovesurface;
43 os<<"[node under="<<u.X<<","<<u.Y<<","<<u.Z
44 << " above="<<a.X<<","<<a.Y<<","<<a.Z<<"]";
46 else if(type == POINTEDTHING_OBJECT)
48 os<<"[object "<<object_id<<"]";
52 os<<"[unknown PointedThing]";
57 void PointedThing::serialize(std::ostream &os) const
59 writeU8(os, 0); // version
60 writeU8(os, (u8)type);
61 if(type == POINTEDTHING_NOTHING)
65 else if(type == POINTEDTHING_NODE)
67 writeV3S16(os, node_undersurface);
68 writeV3S16(os, node_abovesurface);
70 else if(type == POINTEDTHING_OBJECT)
72 writeS16(os, object_id);
76 void PointedThing::deSerialize(std::istream &is)
78 int version = readU8(is);
79 if(version != 0) throw SerializationError(
80 "unsupported PointedThing version");
81 type = (PointedThingType) readU8(is);
82 if(type == POINTEDTHING_NOTHING)
86 else if(type == POINTEDTHING_NODE)
88 node_undersurface = readV3S16(is);
89 node_abovesurface = readV3S16(is);
91 else if(type == POINTEDTHING_OBJECT)
93 object_id = readS16(is);
97 throw SerializationError(
98 "unsupported PointedThingType");
102 bool PointedThing::operator==(const PointedThing &pt2) const
106 if(type == POINTEDTHING_NODE)
108 if(node_undersurface != pt2.node_undersurface)
110 if(node_abovesurface != pt2.node_abovesurface)
113 else if(type == POINTEDTHING_OBJECT)
115 if(object_id != pt2.object_id)
121 bool PointedThing::operator!=(const PointedThing &pt2) const
123 return !(*this == pt2);