2012-06-17 00:29:13 +02:00
|
|
|
/*
|
2013-02-24 18:40:43 +01:00
|
|
|
Minetest
|
2013-02-24 19:38:45 +01:00
|
|
|
Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
2012-06-17 00:29:13 +02:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pointedthing.h"
|
|
|
|
|
|
|
|
#include "serialize.h"
|
2013-08-11 04:09:45 +02:00
|
|
|
#include "../exceptions.h"
|
2012-06-17 00:29:13 +02:00
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
PointedThing::PointedThing():
|
|
|
|
type(POINTEDTHING_NOTHING),
|
|
|
|
node_undersurface(0,0,0),
|
|
|
|
node_abovesurface(0,0,0),
|
2017-01-04 19:18:40 +01:00
|
|
|
node_real_undersurface(0,0,0),
|
|
|
|
intersection_point(0,0,0),
|
|
|
|
intersection_normal(0,0,0),
|
2012-06-17 00:29:13 +02:00
|
|
|
object_id(-1)
|
|
|
|
{}
|
|
|
|
|
|
|
|
std::string PointedThing::dump() const
|
|
|
|
{
|
|
|
|
std::ostringstream os(std::ios::binary);
|
2017-01-04 19:18:40 +01:00
|
|
|
if (type == POINTEDTHING_NOTHING) {
|
2012-06-17 00:29:13 +02:00
|
|
|
os<<"[nothing]";
|
2017-01-04 19:18:40 +01:00
|
|
|
} else if (type == POINTEDTHING_NODE) {
|
2012-06-17 00:29:13 +02:00
|
|
|
const v3s16 &u = node_undersurface;
|
|
|
|
const v3s16 &a = node_abovesurface;
|
|
|
|
os<<"[node under="<<u.X<<","<<u.Y<<","<<u.Z
|
|
|
|
<< " above="<<a.X<<","<<a.Y<<","<<a.Z<<"]";
|
2017-01-04 19:18:40 +01:00
|
|
|
} else if (type == POINTEDTHING_OBJECT) {
|
2012-06-17 00:29:13 +02:00
|
|
|
os<<"[object "<<object_id<<"]";
|
2017-01-04 19:18:40 +01:00
|
|
|
} else {
|
2012-06-17 00:29:13 +02:00
|
|
|
os<<"[unknown PointedThing]";
|
|
|
|
}
|
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PointedThing::serialize(std::ostream &os) const
|
|
|
|
{
|
|
|
|
writeU8(os, 0); // version
|
|
|
|
writeU8(os, (u8)type);
|
2017-01-04 19:18:40 +01:00
|
|
|
switch (type) {
|
|
|
|
case POINTEDTHING_NOTHING:
|
|
|
|
break;
|
|
|
|
case POINTEDTHING_NODE:
|
2012-06-17 00:29:13 +02:00
|
|
|
writeV3S16(os, node_undersurface);
|
|
|
|
writeV3S16(os, node_abovesurface);
|
2017-01-04 19:18:40 +01:00
|
|
|
break;
|
|
|
|
case POINTEDTHING_OBJECT:
|
2012-06-17 00:29:13 +02:00
|
|
|
writeS16(os, object_id);
|
2017-01-04 19:18:40 +01:00
|
|
|
break;
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PointedThing::deSerialize(std::istream &is)
|
|
|
|
{
|
|
|
|
int version = readU8(is);
|
2017-01-04 19:18:40 +01:00
|
|
|
if (version != 0) throw SerializationError(
|
2012-06-17 00:29:13 +02:00
|
|
|
"unsupported PointedThing version");
|
|
|
|
type = (PointedThingType) readU8(is);
|
2017-01-04 19:18:40 +01:00
|
|
|
switch (type) {
|
|
|
|
case POINTEDTHING_NOTHING:
|
|
|
|
break;
|
|
|
|
case POINTEDTHING_NODE:
|
2012-06-17 00:29:13 +02:00
|
|
|
node_undersurface = readV3S16(is);
|
|
|
|
node_abovesurface = readV3S16(is);
|
2017-01-04 19:18:40 +01:00
|
|
|
break;
|
|
|
|
case POINTEDTHING_OBJECT:
|
2012-06-17 00:29:13 +02:00
|
|
|
object_id = readS16(is);
|
2017-01-04 19:18:40 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw SerializationError("unsupported PointedThingType");
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PointedThing::operator==(const PointedThing &pt2) const
|
|
|
|
{
|
2017-01-04 19:18:40 +01:00
|
|
|
if (type != pt2.type)
|
|
|
|
{
|
2012-06-17 00:29:13 +02:00
|
|
|
return false;
|
2017-01-04 19:18:40 +01:00
|
|
|
}
|
|
|
|
if (type == POINTEDTHING_NODE)
|
2012-06-17 00:29:13 +02:00
|
|
|
{
|
2017-01-04 19:18:40 +01:00
|
|
|
if ((node_undersurface != pt2.node_undersurface)
|
|
|
|
|| (node_abovesurface != pt2.node_abovesurface)
|
|
|
|
|| (node_real_undersurface != pt2.node_real_undersurface))
|
2012-06-17 00:29:13 +02:00
|
|
|
return false;
|
|
|
|
}
|
2017-01-04 19:18:40 +01:00
|
|
|
else if (type == POINTEDTHING_OBJECT)
|
2012-06-17 00:29:13 +02:00
|
|
|
{
|
2017-01-04 19:18:40 +01:00
|
|
|
if (object_id != pt2.object_id)
|
2012-06-17 00:29:13 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PointedThing::operator!=(const PointedThing &pt2) const
|
|
|
|
{
|
|
|
|
return !(*this == pt2);
|
|
|
|
}
|
|
|
|
|