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