]> git.lizzy.rs Git - dragonfireclient.git/blob - src/object_properties.cpp
Add nametag background setting and object property (#10937)
[dragonfireclient.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("unknown_object.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 << ", pointable=" << pointable;
76         os << ", static_save=" << static_save;
77         os << ", eye_height=" << eye_height;
78         os << ", zoom_fov=" << zoom_fov;
79         os << ", use_texture_alpha=" << use_texture_alpha;
80         os << ", damage_texture_modifier=" << damage_texture_modifier;
81         os << ", shaded=" << shaded;
82         os << ", show_on_minimap=" << show_on_minimap;
83         return os.str();
84 }
85
86 void ObjectProperties::serialize(std::ostream &os) const
87 {
88         writeU8(os, 4); // PROTOCOL_VERSION >= 37
89         writeU16(os, hp_max);
90         writeU8(os, physical);
91         writeF32(os, 0.f); // Removed property (weight)
92         writeV3F32(os, collisionbox.MinEdge);
93         writeV3F32(os, collisionbox.MaxEdge);
94         writeV3F32(os, selectionbox.MinEdge);
95         writeV3F32(os, selectionbox.MaxEdge);
96         writeU8(os, pointable);
97         os << serializeString16(visual);
98         writeV3F32(os, visual_size);
99         writeU16(os, textures.size());
100         for (const std::string &texture : textures) {
101                 os << serializeString16(texture);
102         }
103         writeV2S16(os, spritediv);
104         writeV2S16(os, initial_sprite_basepos);
105         writeU8(os, is_visible);
106         writeU8(os, makes_footstep_sound);
107         writeF32(os, automatic_rotate);
108         // Added in protocol version 14
109         os << serializeString16(mesh);
110         writeU16(os, colors.size());
111         for (video::SColor color : colors) {
112                 writeARGB8(os, color);
113         }
114         writeU8(os, collideWithObjects);
115         writeF32(os, stepheight);
116         writeU8(os, automatic_face_movement_dir);
117         writeF32(os, automatic_face_movement_dir_offset);
118         writeU8(os, backface_culling);
119         os << serializeString16(nametag);
120         writeARGB8(os, nametag_color);
121         writeF32(os, automatic_face_movement_max_rotation_per_sec);
122         os << serializeString16(infotext);
123         os << serializeString16(wield_item);
124         writeS8(os, glow);
125         writeU16(os, breath_max);
126         writeF32(os, eye_height);
127         writeF32(os, zoom_fov);
128         writeU8(os, use_texture_alpha);
129         os << serializeString16(damage_texture_modifier);
130         writeU8(os, shaded);
131         writeU8(os, show_on_minimap);
132
133         if (!nametag_bgcolor)
134                 writeARGB8(os, NULL_BGCOLOR);
135         else if (nametag_bgcolor.value().getAlpha() == 0)
136                 writeARGB8(os, video::SColor(0, 0, 0, 0));
137         else
138                 writeARGB8(os, nametag_bgcolor.value());
139
140         // Add stuff only at the bottom.
141         // Never remove anything, because we don't want new versions of this
142 }
143
144 void ObjectProperties::deSerialize(std::istream &is)
145 {
146         int version = readU8(is);
147         if (version != 4)
148                 throw SerializationError("unsupported ObjectProperties version");
149
150         hp_max = readU16(is);
151         physical = readU8(is);
152         readU32(is); // removed property (weight)
153         collisionbox.MinEdge = readV3F32(is);
154         collisionbox.MaxEdge = readV3F32(is);
155         selectionbox.MinEdge = readV3F32(is);
156         selectionbox.MaxEdge = readV3F32(is);
157         pointable = readU8(is);
158         visual = deSerializeString16(is);
159         visual_size = readV3F32(is);
160         textures.clear();
161         u32 texture_count = readU16(is);
162         for (u32 i = 0; i < texture_count; i++){
163                 textures.push_back(deSerializeString16(is));
164         }
165         spritediv = readV2S16(is);
166         initial_sprite_basepos = readV2S16(is);
167         is_visible = readU8(is);
168         makes_footstep_sound = readU8(is);
169         automatic_rotate = readF32(is);
170         mesh = deSerializeString16(is);
171         colors.clear();
172         u32 color_count = readU16(is);
173         for (u32 i = 0; i < color_count; i++){
174                 colors.push_back(readARGB8(is));
175         }
176         collideWithObjects = readU8(is);
177         stepheight = readF32(is);
178         automatic_face_movement_dir = readU8(is);
179         automatic_face_movement_dir_offset = readF32(is);
180         backface_culling = readU8(is);
181         nametag = deSerializeString16(is);
182         nametag_color = readARGB8(is);
183         automatic_face_movement_max_rotation_per_sec = readF32(is);
184         infotext = deSerializeString16(is);
185         wield_item = deSerializeString16(is);
186         glow = readS8(is);
187         breath_max = readU16(is);
188         eye_height = readF32(is);
189         zoom_fov = readF32(is);
190         use_texture_alpha = readU8(is);
191         try {
192                 damage_texture_modifier = deSerializeString16(is);
193                 u8 tmp = readU8(is);
194                 if (is.eof())
195                         return;
196                 shaded = tmp;
197                 tmp = readU8(is);
198                 if (is.eof())
199                         return;
200                 show_on_minimap = tmp;
201
202                 auto bgcolor = readARGB8(is);
203                 if (bgcolor != NULL_BGCOLOR)
204                         nametag_bgcolor = bgcolor;
205                 else
206                         nametag_bgcolor = nullopt;
207         } catch (SerializationError &e) {}
208 }