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"
23 #include "../exceptions.h"
26 PointedThing::PointedThing():
27 type(POINTEDTHING_NOTHING),
28 node_undersurface(0,0,0),
29 node_abovesurface(0,0,0),
33 std::string PointedThing::dump() const
35 std::ostringstream os(std::ios::binary);
36 if(type == POINTEDTHING_NOTHING)
40 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<<"]";
47 else if(type == POINTEDTHING_OBJECT)
49 os<<"[object "<<object_id<<"]";
53 os<<"[unknown PointedThing]";
58 void PointedThing::serialize(std::ostream &os) const
60 writeU8(os, 0); // version
61 writeU8(os, (u8)type);
62 if(type == POINTEDTHING_NOTHING)
66 else if(type == POINTEDTHING_NODE)
68 writeV3S16(os, node_undersurface);
69 writeV3S16(os, node_abovesurface);
71 else if(type == POINTEDTHING_OBJECT)
73 writeS16(os, object_id);
77 void PointedThing::deSerialize(std::istream &is)
79 int version = readU8(is);
80 if(version != 0) throw SerializationError(
81 "unsupported PointedThing version");
82 type = (PointedThingType) readU8(is);
83 if(type == POINTEDTHING_NOTHING)
87 else if(type == POINTEDTHING_NODE)
89 node_undersurface = readV3S16(is);
90 node_abovesurface = readV3S16(is);
92 else if(type == POINTEDTHING_OBJECT)
94 object_id = readS16(is);
98 throw SerializationError(
99 "unsupported PointedThingType");
103 bool PointedThing::operator==(const PointedThing &pt2) const
107 if(type == POINTEDTHING_NODE)
109 if(node_undersurface != pt2.node_undersurface)
111 if(node_abovesurface != pt2.node_abovesurface)
114 else if(type == POINTEDTHING_OBJECT)
116 if(object_id != pt2.object_id)
122 bool PointedThing::operator!=(const PointedThing &pt2) const
124 return !(*this == pt2);