object_properties.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. static const video::SColor NULL_BGCOLOR{0, 1, 1, 1};
  23. ObjectProperties::ObjectProperties()
  24. {
  25. textures.emplace_back("no_texture.png");
  26. colors.emplace_back(255,255,255,255);
  27. }
  28. std::string ObjectProperties::dump()
  29. {
  30. std::ostringstream os(std::ios::binary);
  31. os << "hp_max=" << hp_max;
  32. os << ", breath_max=" << breath_max;
  33. os << ", physical=" << physical;
  34. os << ", collideWithObjects=" << collideWithObjects;
  35. os << ", collisionbox=" << PP(collisionbox.MinEdge) << "," << PP(collisionbox.MaxEdge);
  36. os << ", visual=" << visual;
  37. os << ", mesh=" << mesh;
  38. os << ", visual_size=" << PP(visual_size);
  39. os << ", textures=[";
  40. for (const std::string &texture : textures) {
  41. os << "\"" << texture << "\" ";
  42. }
  43. os << "]";
  44. os << ", colors=[";
  45. for (const video::SColor &color : colors) {
  46. os << "\"" << color.getAlpha() << "," << color.getRed() << ","
  47. << color.getGreen() << "," << color.getBlue() << "\" ";
  48. }
  49. os << "]";
  50. os << ", spritediv=" << PP2(spritediv);
  51. os << ", initial_sprite_basepos=" << PP2(initial_sprite_basepos);
  52. os << ", is_visible=" << is_visible;
  53. os << ", makes_footstep_sound=" << makes_footstep_sound;
  54. os << ", automatic_rotate="<< automatic_rotate;
  55. os << ", backface_culling="<< backface_culling;
  56. os << ", glow=" << glow;
  57. os << ", nametag=" << nametag;
  58. os << ", nametag_color=" << "\"" << nametag_color.getAlpha() << "," << nametag_color.getRed()
  59. << "," << nametag_color.getGreen() << "," << nametag_color.getBlue() << "\" ";
  60. if (nametag_bgcolor)
  61. os << ", nametag_bgcolor=" << "\"" << nametag_color.getAlpha() << "," << nametag_color.getRed()
  62. << "," << nametag_color.getGreen() << "," << nametag_color.getBlue() << "\" ";
  63. else
  64. os << ", nametag_bgcolor=null ";
  65. os << ", selectionbox=" << PP(selectionbox.MinEdge) << "," << PP(selectionbox.MaxEdge);
  66. os << ", pointable=" << pointable;
  67. os << ", static_save=" << static_save;
  68. os << ", eye_height=" << eye_height;
  69. os << ", zoom_fov=" << zoom_fov;
  70. os << ", use_texture_alpha=" << use_texture_alpha;
  71. os << ", damage_texture_modifier=" << damage_texture_modifier;
  72. os << ", shaded=" << shaded;
  73. os << ", show_on_minimap=" << show_on_minimap;
  74. return os.str();
  75. }
  76. bool ObjectProperties::validate()
  77. {
  78. const char *func = "ObjectProperties::validate(): ";
  79. bool ret = true;
  80. // cf. where serializeString16 is used below
  81. for (u32 i = 0; i < textures.size(); i++) {
  82. if (textures[i].size() > U16_MAX) {
  83. warningstream << func << "texture " << (i+1) << " has excessive length, "
  84. "clearing it." << std::endl;
  85. textures[i].clear();
  86. ret = false;
  87. }
  88. }
  89. if (nametag.length() > U16_MAX) {
  90. warningstream << func << "nametag has excessive length, clearing it." << std::endl;
  91. nametag.clear();
  92. ret = false;
  93. }
  94. if (infotext.length() > U16_MAX) {
  95. warningstream << func << "infotext has excessive length, clearing it." << std::endl;
  96. infotext.clear();
  97. ret = false;
  98. }
  99. if (wield_item.length() > U16_MAX) {
  100. warningstream << func << "wield_item has excessive length, clearing it." << std::endl;
  101. wield_item.clear();
  102. ret = false;
  103. }
  104. return ret;
  105. }
  106. void ObjectProperties::serialize(std::ostream &os) const
  107. {
  108. writeU8(os, 4); // PROTOCOL_VERSION >= 37
  109. writeU16(os, hp_max);
  110. writeU8(os, physical);
  111. writeF32(os, 0.f); // Removed property (weight)
  112. writeV3F32(os, collisionbox.MinEdge);
  113. writeV3F32(os, collisionbox.MaxEdge);
  114. writeV3F32(os, selectionbox.MinEdge);
  115. writeV3F32(os, selectionbox.MaxEdge);
  116. writeU8(os, pointable);
  117. os << serializeString16(visual);
  118. writeV3F32(os, visual_size);
  119. writeU16(os, textures.size());
  120. for (const std::string &texture : textures) {
  121. os << serializeString16(texture);
  122. }
  123. writeV2S16(os, spritediv);
  124. writeV2S16(os, initial_sprite_basepos);
  125. writeU8(os, is_visible);
  126. writeU8(os, makes_footstep_sound);
  127. writeF32(os, automatic_rotate);
  128. os << serializeString16(mesh);
  129. writeU16(os, colors.size());
  130. for (video::SColor color : colors) {
  131. writeARGB8(os, color);
  132. }
  133. writeU8(os, collideWithObjects);
  134. writeF32(os, stepheight);
  135. writeU8(os, automatic_face_movement_dir);
  136. writeF32(os, automatic_face_movement_dir_offset);
  137. writeU8(os, backface_culling);
  138. os << serializeString16(nametag);
  139. writeARGB8(os, nametag_color);
  140. writeF32(os, automatic_face_movement_max_rotation_per_sec);
  141. os << serializeString16(infotext);
  142. os << serializeString16(wield_item);
  143. writeS8(os, glow);
  144. writeU16(os, breath_max);
  145. writeF32(os, eye_height);
  146. writeF32(os, zoom_fov);
  147. writeU8(os, use_texture_alpha);
  148. os << serializeString16(damage_texture_modifier);
  149. writeU8(os, shaded);
  150. writeU8(os, show_on_minimap);
  151. if (!nametag_bgcolor)
  152. writeARGB8(os, NULL_BGCOLOR);
  153. else if (nametag_bgcolor.value().getAlpha() == 0)
  154. writeARGB8(os, video::SColor(0, 0, 0, 0));
  155. else
  156. writeARGB8(os, nametag_bgcolor.value());
  157. // Add stuff only at the bottom.
  158. // Never remove anything, because we don't want new versions of this
  159. }
  160. void ObjectProperties::deSerialize(std::istream &is)
  161. {
  162. int version = readU8(is);
  163. if (version != 4)
  164. throw SerializationError("unsupported ObjectProperties version");
  165. hp_max = readU16(is);
  166. physical = readU8(is);
  167. readU32(is); // removed property (weight)
  168. collisionbox.MinEdge = readV3F32(is);
  169. collisionbox.MaxEdge = readV3F32(is);
  170. selectionbox.MinEdge = readV3F32(is);
  171. selectionbox.MaxEdge = readV3F32(is);
  172. pointable = readU8(is);
  173. visual = deSerializeString16(is);
  174. visual_size = readV3F32(is);
  175. textures.clear();
  176. u32 texture_count = readU16(is);
  177. for (u32 i = 0; i < texture_count; i++){
  178. textures.push_back(deSerializeString16(is));
  179. }
  180. spritediv = readV2S16(is);
  181. initial_sprite_basepos = readV2S16(is);
  182. is_visible = readU8(is);
  183. makes_footstep_sound = readU8(is);
  184. automatic_rotate = readF32(is);
  185. mesh = deSerializeString16(is);
  186. colors.clear();
  187. u32 color_count = readU16(is);
  188. for (u32 i = 0; i < color_count; i++){
  189. colors.push_back(readARGB8(is));
  190. }
  191. collideWithObjects = readU8(is);
  192. stepheight = readF32(is);
  193. automatic_face_movement_dir = readU8(is);
  194. automatic_face_movement_dir_offset = readF32(is);
  195. backface_culling = readU8(is);
  196. nametag = deSerializeString16(is);
  197. nametag_color = readARGB8(is);
  198. automatic_face_movement_max_rotation_per_sec = readF32(is);
  199. infotext = deSerializeString16(is);
  200. wield_item = deSerializeString16(is);
  201. glow = readS8(is);
  202. breath_max = readU16(is);
  203. eye_height = readF32(is);
  204. zoom_fov = readF32(is);
  205. use_texture_alpha = readU8(is);
  206. try {
  207. damage_texture_modifier = deSerializeString16(is);
  208. u8 tmp = readU8(is);
  209. if (is.eof())
  210. return;
  211. shaded = tmp;
  212. tmp = readU8(is);
  213. if (is.eof())
  214. return;
  215. show_on_minimap = tmp;
  216. auto bgcolor = readARGB8(is);
  217. if (bgcolor != NULL_BGCOLOR)
  218. nametag_bgcolor = bgcolor;
  219. else
  220. nametag_bgcolor = nullopt;
  221. } catch (SerializationError &e) {}
  222. }