object_properties.cpp 7.7 KB

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