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 ObjectProperties::ObjectProperties()
29 textures.emplace_back("unknown_object.png");
30 colors.emplace_back(255,255,255,255);
33 std::string ObjectProperties::dump()
35 std::ostringstream os(std::ios::binary);
36 os<<"hp_max="<<hp_max;
37 os<<", physical="<<physical;
38 os<<", collideWithObjects="<<collideWithObjects;
39 os<<", weight="<<weight;
40 os<<", collisionbox="<<PP(collisionbox.MinEdge)<<","<<PP(collisionbox.MaxEdge);
41 os<<", visual="<<visual;
43 os<<", visual_size="<<PP2(visual_size);
45 for (const std::string &texture : textures) {
46 os<<"\""<< texture <<"\" ";
50 for (const video::SColor &color : colors) {
51 os << "\"" << color.getAlpha() << "," << color.getRed() << ","
52 << color.getGreen() << "," << color.getBlue() << "\" ";
55 os<<", spritediv="<<PP2(spritediv);
56 os<<", initial_sprite_basepos="<<PP2(initial_sprite_basepos);
57 os<<", is_visible="<<is_visible;
58 os<<", makes_footstep_sound="<<makes_footstep_sound;
59 os<<", automatic_rotate="<<automatic_rotate;
60 os<<", backface_culling="<<backface_culling;
61 os << ", nametag=" << nametag;
62 os << ", nametag_color=" << "\"" << nametag_color.getAlpha() << "," << nametag_color.getRed()
63 << "," << nametag_color.getGreen() << "," << nametag_color.getBlue() << "\" ";
64 os << ", selectionbox=" << PP(selectionbox.MinEdge) << "," << PP(selectionbox.MaxEdge);
65 os << ", pointable=" << pointable;
69 void ObjectProperties::serialize(std::ostream &os) const
71 writeU8(os, 2); // version, protocol_version >= 36
73 writeU8(os, physical);
74 writeF1000(os, weight);
75 writeV3F1000(os, collisionbox.MinEdge);
76 writeV3F1000(os, collisionbox.MaxEdge);
77 writeV3F1000(os, selectionbox.MinEdge);
78 writeV3F1000(os, selectionbox.MaxEdge);
79 writeU8(os, pointable);
80 os << serializeString(visual);
81 writeV2F1000(os, visual_size);
82 writeU16(os, textures.size());
83 for (const std::string &texture : textures) {
84 os << serializeString(texture);
86 writeV2S16(os, spritediv);
87 writeV2S16(os, initial_sprite_basepos);
88 writeU8(os, is_visible);
89 writeU8(os, makes_footstep_sound);
90 writeF1000(os, automatic_rotate);
91 // Added in protocol version 14
92 os << serializeString(mesh);
93 writeU16(os, colors.size());
94 for (video::SColor color : colors) {
95 writeARGB8(os, color);
97 writeU8(os, collideWithObjects);
98 writeF1000(os,stepheight);
99 writeU8(os, automatic_face_movement_dir);
100 writeF1000(os, automatic_face_movement_dir_offset);
101 writeU8(os, backface_culling);
102 os << serializeString(nametag);
103 writeARGB8(os, nametag_color);
104 writeF1000(os, automatic_face_movement_max_rotation_per_sec);
105 os << serializeString(infotext);
106 os << serializeString(wield_item);
108 // Add stuff only at the bottom.
109 // Never remove anything, because we don't want new versions of this
112 void ObjectProperties::deSerialize(std::istream &is)
114 int version = readU8(is);
116 throw SerializationError("unsupported ObjectProperties version");
118 hp_max = readS16(is);
119 physical = readU8(is);
120 weight = readF1000(is);
121 collisionbox.MinEdge = readV3F1000(is);
122 collisionbox.MaxEdge = readV3F1000(is);
123 selectionbox.MinEdge = readV3F1000(is);
124 selectionbox.MaxEdge = readV3F1000(is);
125 pointable = readU8(is);
126 visual = deSerializeString(is);
127 visual_size = readV2F1000(is);
129 u32 texture_count = readU16(is);
130 for (u32 i = 0; i < texture_count; i++){
131 textures.push_back(deSerializeString(is));
133 spritediv = readV2S16(is);
134 initial_sprite_basepos = readV2S16(is);
135 is_visible = readU8(is);
136 makes_footstep_sound = readU8(is);
137 automatic_rotate = readF1000(is);
138 mesh = deSerializeString(is);
139 u32 color_count = readU16(is);
140 for (u32 i = 0; i < color_count; i++){
141 colors.push_back(readARGB8(is));
143 collideWithObjects = readU8(is);
144 stepheight = readF1000(is);
145 automatic_face_movement_dir = readU8(is);
146 automatic_face_movement_dir_offset = readF1000(is);
147 backface_culling = readU8(is);
148 nametag = deSerializeString(is);
149 nametag_color = readARGB8(is);
150 automatic_face_movement_max_rotation_per_sec = readF1000(is);
151 infotext = deSerializeString(is);
152 wield_item = deSerializeString(is);