]> git.lizzy.rs Git - minetest.git/blob - src/util/pointedthing.cpp
Remove no virtual dtor warnings, make MapgenParams contain actual NoiseParams
[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 <sstream>
24
25 PointedThing::PointedThing():
26         type(POINTEDTHING_NOTHING),
27         node_undersurface(0,0,0),
28         node_abovesurface(0,0,0),
29         object_id(-1)
30 {}
31
32 std::string PointedThing::dump() const
33 {
34         std::ostringstream os(std::ios::binary);
35         if(type == POINTEDTHING_NOTHING)
36         {
37                 os<<"[nothing]";
38         }
39         else if(type == POINTEDTHING_NODE)
40         {
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<<"]";
45         }
46         else if(type == POINTEDTHING_OBJECT)
47         {
48                 os<<"[object "<<object_id<<"]";
49         }
50         else
51         {
52                 os<<"[unknown PointedThing]";
53         }
54         return os.str();
55 }
56
57 void PointedThing::serialize(std::ostream &os) const
58 {
59         writeU8(os, 0); // version
60         writeU8(os, (u8)type);
61         if(type == POINTEDTHING_NOTHING)
62         {
63                 // nothing
64         }
65         else if(type == POINTEDTHING_NODE)
66         {
67                 writeV3S16(os, node_undersurface);
68                 writeV3S16(os, node_abovesurface);
69         }
70         else if(type == POINTEDTHING_OBJECT)
71         {
72                 writeS16(os, object_id);
73         }
74 }
75
76 void PointedThing::deSerialize(std::istream &is)
77 {
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)
83         {
84                 // nothing
85         }
86         else if(type == POINTEDTHING_NODE)
87         {
88                 node_undersurface = readV3S16(is);
89                 node_abovesurface = readV3S16(is);
90         }
91         else if(type == POINTEDTHING_OBJECT)
92         {
93                 object_id = readS16(is);
94         }
95         else
96         {
97                 throw SerializationError(
98                         "unsupported PointedThingType");
99         }
100 }
101
102 bool PointedThing::operator==(const PointedThing &pt2) const
103 {
104         if(type != pt2.type)
105                 return false;
106         if(type == POINTEDTHING_NODE)
107         {
108                 if(node_undersurface != pt2.node_undersurface)
109                         return false;
110                 if(node_abovesurface != pt2.node_abovesurface)
111                         return false;
112         }
113         else if(type == POINTEDTHING_OBJECT)
114         {
115                 if(object_id != pt2.object_id)
116                         return false;
117         }
118         return true;
119 }
120
121 bool PointedThing::operator!=(const PointedThing &pt2) const
122 {
123         return !(*this == pt2);
124 }
125