Line data Source code
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 1167 : PointedThing::PointedThing():
27 : type(POINTEDTHING_NOTHING),
28 : node_undersurface(0,0,0),
29 : node_abovesurface(0,0,0),
30 1167 : object_id(-1)
31 1167 : {}
32 :
33 57 : std::string PointedThing::dump() const
34 : {
35 114 : std::ostringstream os(std::ios::binary);
36 57 : if(type == POINTEDTHING_NOTHING)
37 : {
38 15 : os<<"[nothing]";
39 : }
40 42 : else if(type == POINTEDTHING_NODE)
41 : {
42 42 : const v3s16 &u = node_undersurface;
43 42 : const v3s16 &a = node_abovesurface;
44 42 : os<<"[node under="<<u.X<<","<<u.Y<<","<<u.Z
45 84 : << " above="<<a.X<<","<<a.Y<<","<<a.Z<<"]";
46 : }
47 0 : else if(type == POINTEDTHING_OBJECT)
48 : {
49 0 : os<<"[object "<<object_id<<"]";
50 : }
51 : else
52 : {
53 0 : os<<"[unknown PointedThing]";
54 : }
55 114 : return os.str();
56 : }
57 :
58 0 : void PointedThing::serialize(std::ostream &os) const
59 : {
60 0 : writeU8(os, 0); // version
61 0 : writeU8(os, (u8)type);
62 0 : if(type == POINTEDTHING_NOTHING)
63 : {
64 : // nothing
65 : }
66 0 : else if(type == POINTEDTHING_NODE)
67 : {
68 0 : writeV3S16(os, node_undersurface);
69 0 : writeV3S16(os, node_abovesurface);
70 : }
71 0 : else if(type == POINTEDTHING_OBJECT)
72 : {
73 0 : writeS16(os, object_id);
74 : }
75 0 : }
76 :
77 0 : void PointedThing::deSerialize(std::istream &is)
78 : {
79 0 : int version = readU8(is);
80 0 : if(version != 0) throw SerializationError(
81 0 : "unsupported PointedThing version");
82 0 : type = (PointedThingType) readU8(is);
83 0 : if(type == POINTEDTHING_NOTHING)
84 : {
85 : // nothing
86 : }
87 0 : else if(type == POINTEDTHING_NODE)
88 : {
89 0 : node_undersurface = readV3S16(is);
90 0 : node_abovesurface = readV3S16(is);
91 : }
92 0 : else if(type == POINTEDTHING_OBJECT)
93 : {
94 0 : object_id = readS16(is);
95 : }
96 : else
97 : {
98 : throw SerializationError(
99 0 : "unsupported PointedThingType");
100 : }
101 0 : }
102 :
103 1166 : bool PointedThing::operator==(const PointedThing &pt2) const
104 : {
105 1166 : if(type != pt2.type)
106 30 : return false;
107 1136 : if(type == POINTEDTHING_NODE)
108 : {
109 270 : if(node_undersurface != pt2.node_undersurface)
110 16 : return false;
111 254 : if(node_abovesurface != pt2.node_abovesurface)
112 11 : return false;
113 : }
114 866 : else if(type == POINTEDTHING_OBJECT)
115 : {
116 0 : if(object_id != pt2.object_id)
117 0 : return false;
118 : }
119 1109 : return true;
120 : }
121 :
122 1166 : bool PointedThing::operator!=(const PointedThing &pt2) const
123 : {
124 1166 : return !(*this == pt2);
125 3 : }
126 :
|