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"
26 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
27 #define PP2(x) "("<<(x).X<<","<<(x).Y<<")"
29 ObjectProperties::ObjectProperties():
32 collideWithObjects(true),
34 collisionbox(-0.5,-0.5,-0.5, 0.5,0.5,0.5),
39 initial_sprite_basepos(0,0),
41 makes_footstep_sound(false),
44 automatic_face_movement_dir(false),
45 automatic_face_movement_dir_offset(0.0),
46 backface_culling(true),
48 nametag_color(255, 255, 255, 255),
49 automatic_face_movement_max_rotation_per_sec(-1)
51 textures.push_back("unknown_object.png");
52 colors.push_back(video::SColor(255,255,255,255));
55 std::string ObjectProperties::dump()
57 std::ostringstream os(std::ios::binary);
58 os<<"hp_max="<<hp_max;
59 os<<", physical="<<physical;
60 os<<", collideWithObjects="<<collideWithObjects;
61 os<<", weight="<<weight;
62 os<<", collisionbox="<<PP(collisionbox.MinEdge)<<","<<PP(collisionbox.MaxEdge);
63 os<<", visual="<<visual;
65 os<<", visual_size="<<PP2(visual_size);
67 for(u32 i=0; i<textures.size(); i++){
68 os<<"\""<<textures[i]<<"\" ";
72 for(u32 i=0; i<colors.size(); i++){
73 os<<"\""<<colors[i].getAlpha()<<","<<colors[i].getRed()<<","<<colors[i].getGreen()<<","<<colors[i].getBlue()<<"\" ";
76 os<<", spritediv="<<PP2(spritediv);
77 os<<", initial_sprite_basepos="<<PP2(initial_sprite_basepos);
78 os<<", is_visible="<<is_visible;
79 os<<", makes_footstep_sound="<<makes_footstep_sound;
80 os<<", automatic_rotate="<<automatic_rotate;
81 os<<", backface_culling="<<backface_culling;
82 os << ", nametag=" << nametag;
83 os << ", nametag_color=" << "\"" << nametag_color.getAlpha() << "," << nametag_color.getRed()
84 << "," << nametag_color.getGreen() << "," << nametag_color.getBlue() << "\" ";
88 void ObjectProperties::serialize(std::ostream &os) const
90 writeU8(os, 1); // version
92 writeU8(os, physical);
93 writeF1000(os, weight);
94 writeV3F1000(os, collisionbox.MinEdge);
95 writeV3F1000(os, collisionbox.MaxEdge);
96 os<<serializeString(visual);
97 writeV2F1000(os, visual_size);
98 writeU16(os, textures.size());
99 for(u32 i=0; i<textures.size(); i++){
100 os<<serializeString(textures[i]);
102 writeV2S16(os, spritediv);
103 writeV2S16(os, initial_sprite_basepos);
104 writeU8(os, is_visible);
105 writeU8(os, makes_footstep_sound);
106 writeF1000(os, automatic_rotate);
107 // Added in protocol version 14
108 os<<serializeString(mesh);
109 writeU16(os, colors.size());
110 for(u32 i=0; i<colors.size(); i++){
111 writeARGB8(os, colors[i]);
113 writeU8(os, collideWithObjects);
114 writeF1000(os,stepheight);
115 writeU8(os, automatic_face_movement_dir);
116 writeF1000(os, automatic_face_movement_dir_offset);
117 writeU8(os, backface_culling);
118 os << serializeString(nametag);
119 writeARGB8(os, nametag_color);
120 writeF1000(os, automatic_face_movement_max_rotation_per_sec);
121 os << serializeString(infotext);
123 // Add stuff only at the bottom.
124 // Never remove anything, because we don't want new versions of this
127 void ObjectProperties::deSerialize(std::istream &is)
129 int version = readU8(is);
133 hp_max = readS16(is);
134 physical = readU8(is);
135 weight = readF1000(is);
136 collisionbox.MinEdge = readV3F1000(is);
137 collisionbox.MaxEdge = readV3F1000(is);
138 visual = deSerializeString(is);
139 visual_size = readV2F1000(is);
141 u32 texture_count = readU16(is);
142 for(u32 i=0; i<texture_count; i++){
143 textures.push_back(deSerializeString(is));
145 spritediv = readV2S16(is);
146 initial_sprite_basepos = readV2S16(is);
147 is_visible = readU8(is);
148 makes_footstep_sound = readU8(is);
149 automatic_rotate = readF1000(is);
150 mesh = deSerializeString(is);
151 u32 color_count = readU16(is);
152 for(u32 i=0; i<color_count; i++){
153 colors.push_back(readARGB8(is));
155 collideWithObjects = readU8(is);
156 stepheight = readF1000(is);
157 automatic_face_movement_dir = readU8(is);
158 automatic_face_movement_dir_offset = readF1000(is);
159 backface_culling = readU8(is);
160 nametag = deSerializeString(is);
161 nametag_color = readARGB8(is);
162 automatic_face_movement_max_rotation_per_sec = readF1000(is);
163 infotext = deSerializeString(is);
164 }catch(SerializationError &e){}
168 throw SerializationError("unsupported ObjectProperties version");