Customizeable max breath for players (#6411)
[oweals/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 ObjectProperties::ObjectProperties()
28 {
29         textures.emplace_back("unknown_object.png");
30         colors.emplace_back(255,255,255,255);
31 }
32
33 std::string ObjectProperties::dump()
34 {
35         std::ostringstream os(std::ios::binary);
36         os << "hp_max=" << hp_max;
37         os << ", breath_max=" << breath_max;
38         os << ", physical=" << physical;
39         os << ", collideWithObjects=" << collideWithObjects;
40         os << ", weight=" << weight;
41         os << ", collisionbox=" << PP(collisionbox.MinEdge) << "," << PP(collisionbox.MaxEdge);
42         os << ", visual=" << visual;
43         os << ", mesh=" << mesh;
44         os << ", visual_size=" << PP2(visual_size);
45         os << ", textures=[";
46         for (const std::string &texture : textures) {
47                 os << "\"" << texture << "\" ";
48         }
49         os << "]";
50         os << ", colors=[";
51         for (const video::SColor &color : colors) {
52                 os << "\"" << color.getAlpha() << "," << color.getRed() << ","
53                         << color.getGreen() << "," << color.getBlue() << "\" ";
54         }
55         os << "]";
56         os << ", spritediv=" << PP2(spritediv);
57         os << ", initial_sprite_basepos=" << PP2(initial_sprite_basepos);
58         os << ", is_visible=" << is_visible;
59         os << ", makes_footstep_sound=" << makes_footstep_sound;
60         os << ", automatic_rotate="<< automatic_rotate;
61         os << ", backface_culling="<< backface_culling;
62         os << ", glow=" << glow;
63         os << ", nametag=" << nametag;
64         os << ", nametag_color=" << "\"" << nametag_color.getAlpha() << "," << nametag_color.getRed()
65                         << "," << nametag_color.getGreen() << "," << nametag_color.getBlue() << "\" ";
66         os << ", selectionbox=" << PP(selectionbox.MinEdge) << "," << PP(selectionbox.MaxEdge);
67         os << ", pointable=" << pointable;
68         os << ", can_zoom=" << can_zoom;
69         return os.str();
70 }
71
72 void ObjectProperties::serialize(std::ostream &os) const
73 {
74         writeU8(os, 2); // version, protocol_version >= 36
75         writeS16(os, hp_max);
76         writeU8(os, physical);
77         writeF1000(os, weight);
78         writeV3F1000(os, collisionbox.MinEdge);
79         writeV3F1000(os, collisionbox.MaxEdge);
80         writeV3F1000(os, selectionbox.MinEdge);
81         writeV3F1000(os, selectionbox.MaxEdge);
82         writeU8(os, pointable);
83         os << serializeString(visual);
84         writeV2F1000(os, visual_size);
85         writeU16(os, textures.size());
86         for (const std::string &texture : textures) {
87                 os << serializeString(texture);
88         }
89         writeV2S16(os, spritediv);
90         writeV2S16(os, initial_sprite_basepos);
91         writeU8(os, is_visible);
92         writeU8(os, makes_footstep_sound);
93         writeF1000(os, automatic_rotate);
94         // Added in protocol version 14
95         os << serializeString(mesh);
96         writeU16(os, colors.size());
97         for (video::SColor color : colors) {
98                 writeARGB8(os, color);
99         }
100         writeU8(os, collideWithObjects);
101         writeF1000(os,stepheight);
102         writeU8(os, automatic_face_movement_dir);
103         writeF1000(os, automatic_face_movement_dir_offset);
104         writeU8(os, backface_culling);
105         os << serializeString(nametag);
106         writeARGB8(os, nametag_color);
107         writeF1000(os, automatic_face_movement_max_rotation_per_sec);
108         os << serializeString(infotext);
109         os << serializeString(wield_item);
110         writeU8(os, can_zoom);
111         writeS8(os, glow);
112         writeU16(os, breath_max);
113
114         // Add stuff only at the bottom.
115         // Never remove anything, because we don't want new versions of this
116 }
117
118 void ObjectProperties::deSerialize(std::istream &is)
119 {
120         int version = readU8(is);
121         if (version != 2)
122                 throw SerializationError("unsupported ObjectProperties version");
123
124         hp_max = readS16(is);
125         physical = readU8(is);
126         weight = readF1000(is);
127         collisionbox.MinEdge = readV3F1000(is);
128         collisionbox.MaxEdge = readV3F1000(is);
129         selectionbox.MinEdge = readV3F1000(is);
130         selectionbox.MaxEdge = readV3F1000(is);
131         pointable = readU8(is);
132         visual = deSerializeString(is);
133         visual_size = readV2F1000(is);
134         textures.clear();
135         u32 texture_count = readU16(is);
136         for (u32 i = 0; i < texture_count; i++){
137                 textures.push_back(deSerializeString(is));
138         }
139         spritediv = readV2S16(is);
140         initial_sprite_basepos = readV2S16(is);
141         is_visible = readU8(is);
142         makes_footstep_sound = readU8(is);
143         automatic_rotate = readF1000(is);
144         mesh = deSerializeString(is);
145         u32 color_count = readU16(is);
146         for (u32 i = 0; i < color_count; i++){
147                 colors.push_back(readARGB8(is));
148         }
149         collideWithObjects = readU8(is);
150         stepheight = readF1000(is);
151         automatic_face_movement_dir = readU8(is);
152         automatic_face_movement_dir_offset = readF1000(is);
153         backface_culling = readU8(is);
154         nametag = deSerializeString(is);
155         nametag_color = readARGB8(is);
156         automatic_face_movement_max_rotation_per_sec = readF1000(is);
157         infotext = deSerializeString(is);
158         wield_item = deSerializeString(is);
159         can_zoom = readU8(is);
160
161         try {
162                 glow = readS8(is);
163                 breath_max = readU16(is);
164         } catch (SerializationError &e) {}
165 }