3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "object_properties.h"
21 #include "irrlichttypes_bloated.h"
22 #include "exceptions.h"
23 #include "util/serialize.h"
24 #include "util/basic_macros.h"
27 static const video::SColor NULL_BGCOLOR{0, 1, 1, 1};
29 ObjectProperties::ObjectProperties()
31 textures.emplace_back("no_texture.png");
32 colors.emplace_back(255,255,255,255);
35 std::string ObjectProperties::dump()
37 std::ostringstream os(std::ios::binary);
38 os << "hp_max=" << hp_max;
39 os << ", breath_max=" << breath_max;
40 os << ", physical=" << physical;
41 os << ", collideWithObjects=" << collideWithObjects;
42 os << ", collisionbox=" << PP(collisionbox.MinEdge) << "," << PP(collisionbox.MaxEdge);
43 os << ", visual=" << visual;
44 os << ", mesh=" << mesh;
45 os << ", visual_size=" << PP(visual_size);
47 for (const std::string &texture : textures) {
48 os << "\"" << texture << "\" ";
52 for (const video::SColor &color : colors) {
53 os << "\"" << color.getAlpha() << "," << color.getRed() << ","
54 << color.getGreen() << "," << color.getBlue() << "\" ";
57 os << ", spritediv=" << PP2(spritediv);
58 os << ", initial_sprite_basepos=" << PP2(initial_sprite_basepos);
59 os << ", is_visible=" << is_visible;
60 os << ", makes_footstep_sound=" << makes_footstep_sound;
61 os << ", automatic_rotate="<< automatic_rotate;
62 os << ", backface_culling="<< backface_culling;
63 os << ", glow=" << glow;
64 os << ", nametag=" << nametag;
65 os << ", nametag_color=" << "\"" << nametag_color.getAlpha() << "," << nametag_color.getRed()
66 << "," << nametag_color.getGreen() << "," << nametag_color.getBlue() << "\" ";
69 os << ", nametag_bgcolor=" << "\"" << nametag_color.getAlpha() << "," << nametag_color.getRed()
70 << "," << nametag_color.getGreen() << "," << nametag_color.getBlue() << "\" ";
72 os << ", nametag_bgcolor=null ";
74 os << ", selectionbox=" << PP(selectionbox.MinEdge) << "," << PP(selectionbox.MaxEdge);
75 os << ", rotate_selectionbox=" << rotate_selectionbox;
76 os << ", pointable=" << pointable;
77 os << ", static_save=" << static_save;
78 os << ", eye_height=" << eye_height;
79 os << ", zoom_fov=" << zoom_fov;
80 os << ", use_texture_alpha=" << use_texture_alpha;
81 os << ", damage_texture_modifier=" << damage_texture_modifier;
82 os << ", shaded=" << shaded;
83 os << ", show_on_minimap=" << show_on_minimap;
87 bool ObjectProperties::validate()
89 const char *func = "ObjectProperties::validate(): ";
92 // cf. where serializeString16 is used below
93 for (u32 i = 0; i < textures.size(); i++) {
94 if (textures[i].size() > U16_MAX) {
95 warningstream << func << "texture " << (i+1) << " has excessive length, "
96 "clearing it." << std::endl;
101 if (nametag.length() > U16_MAX) {
102 warningstream << func << "nametag has excessive length, clearing it." << std::endl;
106 if (infotext.length() > U16_MAX) {
107 warningstream << func << "infotext has excessive length, clearing it." << std::endl;
111 if (wield_item.length() > U16_MAX) {
112 warningstream << func << "wield_item has excessive length, clearing it." << std::endl;
120 void ObjectProperties::serialize(std::ostream &os) const
122 writeU8(os, 4); // PROTOCOL_VERSION >= 37
123 writeU16(os, hp_max);
124 writeU8(os, physical);
125 writeF32(os, 0.f); // Removed property (weight)
126 writeV3F32(os, collisionbox.MinEdge);
127 writeV3F32(os, collisionbox.MaxEdge);
128 writeV3F32(os, selectionbox.MinEdge);
129 writeV3F32(os, selectionbox.MaxEdge);
130 writeU8(os, pointable);
131 os << serializeString16(visual);
132 writeV3F32(os, visual_size);
133 writeU16(os, textures.size());
134 for (const std::string &texture : textures) {
135 os << serializeString16(texture);
137 writeV2S16(os, spritediv);
138 writeV2S16(os, initial_sprite_basepos);
139 writeU8(os, is_visible);
140 writeU8(os, makes_footstep_sound);
141 writeF32(os, automatic_rotate);
142 os << serializeString16(mesh);
143 writeU16(os, colors.size());
144 for (video::SColor color : colors) {
145 writeARGB8(os, color);
147 writeU8(os, collideWithObjects);
148 writeF32(os, stepheight);
149 writeU8(os, automatic_face_movement_dir);
150 writeF32(os, automatic_face_movement_dir_offset);
151 writeU8(os, backface_culling);
152 os << serializeString16(nametag);
153 writeARGB8(os, nametag_color);
154 writeF32(os, automatic_face_movement_max_rotation_per_sec);
155 os << serializeString16(infotext);
156 os << serializeString16(wield_item);
158 writeU16(os, breath_max);
159 writeF32(os, eye_height);
160 writeF32(os, zoom_fov);
161 writeU8(os, use_texture_alpha);
162 os << serializeString16(damage_texture_modifier);
164 writeU8(os, show_on_minimap);
166 if (!nametag_bgcolor)
167 writeARGB8(os, NULL_BGCOLOR);
168 else if (nametag_bgcolor.value().getAlpha() == 0)
169 writeARGB8(os, video::SColor(0, 0, 0, 0));
171 writeARGB8(os, nametag_bgcolor.value());
173 writeU8(os, rotate_selectionbox);
174 // Add stuff only at the bottom.
175 // Never remove anything, because we don't want new versions of this
178 void ObjectProperties::deSerialize(std::istream &is)
180 int version = readU8(is);
182 throw SerializationError("unsupported ObjectProperties version");
184 hp_max = readU16(is);
185 physical = readU8(is);
186 readU32(is); // removed property (weight)
187 collisionbox.MinEdge = readV3F32(is);
188 collisionbox.MaxEdge = readV3F32(is);
189 selectionbox.MinEdge = readV3F32(is);
190 selectionbox.MaxEdge = readV3F32(is);
191 pointable = readU8(is);
192 visual = deSerializeString16(is);
193 visual_size = readV3F32(is);
195 u32 texture_count = readU16(is);
196 for (u32 i = 0; i < texture_count; i++){
197 textures.push_back(deSerializeString16(is));
199 spritediv = readV2S16(is);
200 initial_sprite_basepos = readV2S16(is);
201 is_visible = readU8(is);
202 makes_footstep_sound = readU8(is);
203 automatic_rotate = readF32(is);
204 mesh = deSerializeString16(is);
206 u32 color_count = readU16(is);
207 for (u32 i = 0; i < color_count; i++){
208 colors.push_back(readARGB8(is));
210 collideWithObjects = readU8(is);
211 stepheight = readF32(is);
212 automatic_face_movement_dir = readU8(is);
213 automatic_face_movement_dir_offset = readF32(is);
214 backface_culling = readU8(is);
215 nametag = deSerializeString16(is);
216 nametag_color = readARGB8(is);
217 automatic_face_movement_max_rotation_per_sec = readF32(is);
218 infotext = deSerializeString16(is);
219 wield_item = deSerializeString16(is);
221 breath_max = readU16(is);
222 eye_height = readF32(is);
223 zoom_fov = readF32(is);
224 use_texture_alpha = readU8(is);
226 damage_texture_modifier = deSerializeString16(is);
234 show_on_minimap = tmp;
236 auto bgcolor = readARGB8(is);
237 if (bgcolor != NULL_BGCOLOR)
238 nametag_bgcolor = bgcolor;
240 nametag_bgcolor = nullopt;
245 rotate_selectionbox = tmp;
246 } catch (SerializationError &e) {}