]> git.lizzy.rs Git - minetest.git/blob - src/object_properties.cpp
Remove dead code behind Irrlicht version checks
[minetest.git] / src / object_properties.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
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"
25 #include <sstream>
26
27 static const video::SColor NULL_BGCOLOR{0, 1, 1, 1};
28
29 ObjectProperties::ObjectProperties()
30 {
31         textures.emplace_back("no_texture.png");
32         colors.emplace_back(255,255,255,255);
33 }
34
35 std::string ObjectProperties::dump()
36 {
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);
46         os << ", textures=[";
47         for (const std::string &texture : textures) {
48                 os << "\"" << texture << "\" ";
49         }
50         os << "]";
51         os << ", colors=[";
52         for (const video::SColor &color : colors) {
53                 os << "\"" << color.getAlpha() << "," << color.getRed() << ","
54                         << color.getGreen() << "," << color.getBlue() << "\" ";
55         }
56         os << "]";
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() << "\" ";
67
68         if (nametag_bgcolor)
69                 os << ", nametag_bgcolor=" << "\"" << nametag_color.getAlpha() << "," << nametag_color.getRed()
70                    << "," << nametag_color.getGreen() << "," << nametag_color.getBlue() << "\" ";
71         else
72                 os << ", nametag_bgcolor=null ";
73
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;
84         return os.str();
85 }
86
87 bool ObjectProperties::validate()
88 {
89         const char *func = "ObjectProperties::validate(): ";
90         bool ret = true;
91
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;
97                         textures[i].clear();
98                         ret = false;
99                 }
100         }
101         if (nametag.length() > U16_MAX) {
102                 warningstream << func << "nametag has excessive length, clearing it." << std::endl;
103                 nametag.clear();
104                 ret = false;
105         }
106         if (infotext.length() > U16_MAX) {
107                 warningstream << func << "infotext has excessive length, clearing it." << std::endl;
108                 infotext.clear();
109                 ret = false;
110         }
111         if (wield_item.length() > U16_MAX) {
112                 warningstream << func << "wield_item has excessive length, clearing it." << std::endl;
113                 wield_item.clear();
114                 ret = false;
115         }
116
117         return ret;
118 }
119
120 void ObjectProperties::serialize(std::ostream &os) const
121 {
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);
136         }
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);
146         }
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);
157         writeS8(os, glow);
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);
163         writeU8(os, shaded);
164         writeU8(os, show_on_minimap);
165
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));
170         else
171                 writeARGB8(os, nametag_bgcolor.value());
172
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
176 }
177
178 void ObjectProperties::deSerialize(std::istream &is)
179 {
180         int version = readU8(is);
181         if (version != 4)
182                 throw SerializationError("unsupported ObjectProperties version");
183
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);
194         textures.clear();
195         u32 texture_count = readU16(is);
196         for (u32 i = 0; i < texture_count; i++){
197                 textures.push_back(deSerializeString16(is));
198         }
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);
205         colors.clear();
206         u32 color_count = readU16(is);
207         for (u32 i = 0; i < color_count; i++){
208                 colors.push_back(readARGB8(is));
209         }
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);
220         glow = readS8(is);
221         breath_max = readU16(is);
222         eye_height = readF32(is);
223         zoom_fov = readF32(is);
224         use_texture_alpha = readU8(is);
225         try {
226                 damage_texture_modifier = deSerializeString16(is);
227                 u8 tmp = readU8(is);
228                 if (is.eof())
229                         return;
230                 shaded = tmp;
231                 tmp = readU8(is);
232                 if (is.eof())
233                         return;
234                 show_on_minimap = tmp;
235
236                 auto bgcolor = readARGB8(is);
237                 if (bgcolor != NULL_BGCOLOR)
238                         nametag_bgcolor = bgcolor;
239                 else
240                         nametag_bgcolor = nullopt;
241
242                 tmp = readU8(is);
243                 if (is.eof())
244                         return;
245                 rotate_selectionbox = tmp;
246         } catch (SerializationError &e) {}
247 }