object_properties.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. Minetest
  3. Copyright (C) 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 "object_properties.h"
  17. #include "irrlichttypes_bloated.h"
  18. #include "exceptions.h"
  19. #include "util/serialize.h"
  20. #include "util/basic_macros.h"
  21. #include <sstream>
  22. ObjectProperties::ObjectProperties()
  23. {
  24. textures.emplace_back("unknown_object.png");
  25. colors.emplace_back(255,255,255,255);
  26. }
  27. std::string ObjectProperties::dump()
  28. {
  29. std::ostringstream os(std::ios::binary);
  30. os << "hp_max=" << hp_max;
  31. os << ", breath_max=" << breath_max;
  32. os << ", physical=" << physical;
  33. os << ", collideWithObjects=" << collideWithObjects;
  34. os << ", collisionbox=" << PP(collisionbox.MinEdge) << "," << PP(collisionbox.MaxEdge);
  35. os << ", visual=" << visual;
  36. os << ", mesh=" << mesh;
  37. os << ", visual_size=" << PP(visual_size);
  38. os << ", textures=[";
  39. for (const std::string &texture : textures) {
  40. os << "\"" << texture << "\" ";
  41. }
  42. os << "]";
  43. os << ", colors=[";
  44. for (const video::SColor &color : colors) {
  45. os << "\"" << color.getAlpha() << "," << color.getRed() << ","
  46. << color.getGreen() << "," << color.getBlue() << "\" ";
  47. }
  48. os << "]";
  49. os << ", spritediv=" << PP2(spritediv);
  50. os << ", initial_sprite_basepos=" << PP2(initial_sprite_basepos);
  51. os << ", is_visible=" << is_visible;
  52. os << ", makes_footstep_sound=" << makes_footstep_sound;
  53. os << ", automatic_rotate="<< automatic_rotate;
  54. os << ", backface_culling="<< backface_culling;
  55. os << ", glow=" << glow;
  56. os << ", nametag=" << nametag;
  57. os << ", nametag_color=" << "\"" << nametag_color.getAlpha() << "," << nametag_color.getRed()
  58. << "," << nametag_color.getGreen() << "," << nametag_color.getBlue() << "\" ";
  59. os << ", selectionbox=" << PP(selectionbox.MinEdge) << "," << PP(selectionbox.MaxEdge);
  60. os << ", pointable=" << pointable;
  61. os << ", static_save=" << static_save;
  62. os << ", eye_height=" << eye_height;
  63. os << ", zoom_fov=" << zoom_fov;
  64. os << ", use_texture_alpha=" << use_texture_alpha;
  65. os << ", damage_texture_modifier=" << damage_texture_modifier;
  66. os << ", shaded=" << shaded;
  67. return os.str();
  68. }
  69. void ObjectProperties::serialize(std::ostream &os) const
  70. {
  71. writeU8(os, 4); // PROTOCOL_VERSION >= 37
  72. writeU16(os, hp_max);
  73. writeU8(os, physical);
  74. writeF32(os, 0.f); // Removed property (weight)
  75. writeV3F32(os, collisionbox.MinEdge);
  76. writeV3F32(os, collisionbox.MaxEdge);
  77. writeV3F32(os, selectionbox.MinEdge);
  78. writeV3F32(os, selectionbox.MaxEdge);
  79. writeU8(os, pointable);
  80. os << serializeString(visual);
  81. writeV3F32(os, visual_size);
  82. writeU16(os, textures.size());
  83. for (const std::string &texture : textures) {
  84. os << serializeString(texture);
  85. }
  86. writeV2S16(os, spritediv);
  87. writeV2S16(os, initial_sprite_basepos);
  88. writeU8(os, is_visible);
  89. writeU8(os, makes_footstep_sound);
  90. writeF32(os, automatic_rotate);
  91. // Added in protocol version 14
  92. os << serializeString(mesh);
  93. writeU16(os, colors.size());
  94. for (video::SColor color : colors) {
  95. writeARGB8(os, color);
  96. }
  97. writeU8(os, collideWithObjects);
  98. writeF32(os, stepheight);
  99. writeU8(os, automatic_face_movement_dir);
  100. writeF32(os, automatic_face_movement_dir_offset);
  101. writeU8(os, backface_culling);
  102. os << serializeString(nametag);
  103. writeARGB8(os, nametag_color);
  104. writeF32(os, automatic_face_movement_max_rotation_per_sec);
  105. os << serializeString(infotext);
  106. os << serializeString(wield_item);
  107. writeS8(os, glow);
  108. writeU16(os, breath_max);
  109. writeF32(os, eye_height);
  110. writeF32(os, zoom_fov);
  111. writeU8(os, use_texture_alpha);
  112. os << serializeString(damage_texture_modifier);
  113. writeU8(os, shaded);
  114. // Add stuff only at the bottom.
  115. // Never remove anything, because we don't want new versions of this
  116. }
  117. void ObjectProperties::deSerialize(std::istream &is)
  118. {
  119. int version = readU8(is);
  120. if (version != 4)
  121. throw SerializationError("unsupported ObjectProperties version");
  122. hp_max = readU16(is);
  123. physical = readU8(is);
  124. readU32(is); // removed property (weight)
  125. collisionbox.MinEdge = readV3F32(is);
  126. collisionbox.MaxEdge = readV3F32(is);
  127. selectionbox.MinEdge = readV3F32(is);
  128. selectionbox.MaxEdge = readV3F32(is);
  129. pointable = readU8(is);
  130. visual = deSerializeString(is);
  131. visual_size = readV3F32(is);
  132. textures.clear();
  133. u32 texture_count = readU16(is);
  134. for (u32 i = 0; i < texture_count; i++){
  135. textures.push_back(deSerializeString(is));
  136. }
  137. spritediv = readV2S16(is);
  138. initial_sprite_basepos = readV2S16(is);
  139. is_visible = readU8(is);
  140. makes_footstep_sound = readU8(is);
  141. automatic_rotate = readF32(is);
  142. mesh = deSerializeString(is);
  143. colors.clear();
  144. u32 color_count = readU16(is);
  145. for (u32 i = 0; i < color_count; i++){
  146. colors.push_back(readARGB8(is));
  147. }
  148. collideWithObjects = readU8(is);
  149. stepheight = readF32(is);
  150. automatic_face_movement_dir = readU8(is);
  151. automatic_face_movement_dir_offset = readF32(is);
  152. backface_culling = readU8(is);
  153. nametag = deSerializeString(is);
  154. nametag_color = readARGB8(is);
  155. automatic_face_movement_max_rotation_per_sec = readF32(is);
  156. infotext = deSerializeString(is);
  157. wield_item = deSerializeString(is);
  158. glow = readS8(is);
  159. breath_max = readU16(is);
  160. eye_height = readF32(is);
  161. zoom_fov = readF32(is);
  162. use_texture_alpha = readU8(is);
  163. try {
  164. damage_texture_modifier = deSerializeString(is);
  165. u8 tmp = readU8(is);
  166. if (is.eof())
  167. throw SerializationError("");
  168. shaded = tmp;
  169. } catch (SerializationError &e) {}
  170. }