pointedthing.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "pointedthing.h"
  17. #include "serialize.h"
  18. #include "exceptions.h"
  19. #include <sstream>
  20. PointedThing::PointedThing(const v3s16 &under, const v3s16 &above,
  21. const v3s16 &real_under, const v3f &point, const v3s16 &normal,
  22. f32 distSq):
  23. type(POINTEDTHING_NODE),
  24. node_undersurface(under),
  25. node_abovesurface(above),
  26. node_real_undersurface(real_under),
  27. intersection_point(point),
  28. intersection_normal(normal),
  29. distanceSq(distSq)
  30. {}
  31. PointedThing::PointedThing(s16 id, const v3f &point, const v3s16 &normal,
  32. f32 distSq) :
  33. type(POINTEDTHING_OBJECT),
  34. object_id(id),
  35. intersection_point(point),
  36. intersection_normal(normal),
  37. distanceSq(distSq)
  38. {}
  39. std::string PointedThing::dump() const
  40. {
  41. std::ostringstream os(std::ios::binary);
  42. switch (type) {
  43. case POINTEDTHING_NOTHING:
  44. os << "[nothing]";
  45. break;
  46. case POINTEDTHING_NODE:
  47. {
  48. const v3s16 &u = node_undersurface;
  49. const v3s16 &a = node_abovesurface;
  50. os << "[node under=" << u.X << "," << u.Y << "," << u.Z << " above="
  51. << a.X << "," << a.Y << "," << a.Z << "]";
  52. }
  53. break;
  54. case POINTEDTHING_OBJECT:
  55. os << "[object " << object_id << "]";
  56. break;
  57. default:
  58. os << "[unknown PointedThing]";
  59. }
  60. return os.str();
  61. }
  62. void PointedThing::serialize(std::ostream &os) const
  63. {
  64. writeU8(os, 0); // version
  65. writeU8(os, (u8)type);
  66. switch (type) {
  67. case POINTEDTHING_NOTHING:
  68. break;
  69. case POINTEDTHING_NODE:
  70. writeV3S16(os, node_undersurface);
  71. writeV3S16(os, node_abovesurface);
  72. break;
  73. case POINTEDTHING_OBJECT:
  74. writeS16(os, object_id);
  75. break;
  76. }
  77. }
  78. void PointedThing::deSerialize(std::istream &is)
  79. {
  80. int version = readU8(is);
  81. if (version != 0) throw SerializationError(
  82. "unsupported PointedThing version");
  83. type = (PointedThingType) readU8(is);
  84. switch (type) {
  85. case POINTEDTHING_NOTHING:
  86. break;
  87. case POINTEDTHING_NODE:
  88. node_undersurface = readV3S16(is);
  89. node_abovesurface = readV3S16(is);
  90. break;
  91. case POINTEDTHING_OBJECT:
  92. object_id = readS16(is);
  93. break;
  94. default:
  95. throw SerializationError("unsupported PointedThingType");
  96. }
  97. }
  98. bool PointedThing::operator==(const PointedThing &pt2) const
  99. {
  100. if (type != pt2.type)
  101. {
  102. return false;
  103. }
  104. if (type == POINTEDTHING_NODE)
  105. {
  106. if ((node_undersurface != pt2.node_undersurface)
  107. || (node_abovesurface != pt2.node_abovesurface)
  108. || (node_real_undersurface != pt2.node_real_undersurface))
  109. return false;
  110. }
  111. else if (type == POINTEDTHING_OBJECT)
  112. {
  113. if (object_id != pt2.object_id)
  114. return false;
  115. }
  116. return true;
  117. }
  118. bool PointedThing::operator!=(const PointedThing &pt2) const
  119. {
  120. return !(*this == pt2);
  121. }