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 textures.push_back("unknown_object.png");
49 colors.push_back(video::SColor(255,255,255,255));
52 std::string ObjectProperties::dump()
54 std::ostringstream os(std::ios::binary);
55 os<<"hp_max="<<hp_max;
56 os<<", physical="<<physical;
57 os<<", collideWithObjects="<<collideWithObjects;
58 os<<", weight="<<weight;
59 os<<", collisionbox="<<PP(collisionbox.MinEdge)<<","<<PP(collisionbox.MaxEdge);
60 os<<", visual="<<visual;
62 os<<", visual_size="<<PP2(visual_size);
64 for(u32 i=0; i<textures.size(); i++){
65 os<<"\""<<textures[i]<<"\" ";
69 for(u32 i=0; i<colors.size(); i++){
70 os<<"\""<<colors[i].getAlpha()<<","<<colors[i].getRed()<<","<<colors[i].getGreen()<<","<<colors[i].getBlue()<<"\" ";
73 os<<", spritediv="<<PP2(spritediv);
74 os<<", initial_sprite_basepos="<<PP2(initial_sprite_basepos);
75 os<<", is_visible="<<is_visible;
76 os<<", makes_footstep_sound="<<makes_footstep_sound;
77 os<<", automatic_rotate="<<automatic_rotate;
78 os<<", backface_culling="<<backface_culling;
82 void ObjectProperties::serialize(std::ostream &os) const
84 writeU8(os, 1); // version
86 writeU8(os, physical);
87 writeF1000(os, weight);
88 writeV3F1000(os, collisionbox.MinEdge);
89 writeV3F1000(os, collisionbox.MaxEdge);
90 os<<serializeString(visual);
91 writeV2F1000(os, visual_size);
92 writeU16(os, textures.size());
93 for(u32 i=0; i<textures.size(); i++){
94 os<<serializeString(textures[i]);
96 writeV2S16(os, spritediv);
97 writeV2S16(os, initial_sprite_basepos);
98 writeU8(os, is_visible);
99 writeU8(os, makes_footstep_sound);
100 writeF1000(os, automatic_rotate);
101 // Added in protocol version 14
102 os<<serializeString(mesh);
103 writeU16(os, colors.size());
104 for(u32 i=0; i<colors.size(); i++){
105 writeARGB8(os, colors[i]);
107 writeU8(os, collideWithObjects);
108 writeF1000(os,stepheight);
109 writeU8(os, automatic_face_movement_dir);
110 writeF1000(os, automatic_face_movement_dir_offset);
111 writeU8(os, backface_culling);
112 // Add stuff only at the bottom.
113 // Never remove anything, because we don't want new versions of this
116 void ObjectProperties::deSerialize(std::istream &is)
118 int version = readU8(is);
122 hp_max = readS16(is);
123 physical = readU8(is);
124 weight = readF1000(is);
125 collisionbox.MinEdge = readV3F1000(is);
126 collisionbox.MaxEdge = readV3F1000(is);
127 visual = deSerializeString(is);
128 visual_size = readV2F1000(is);
130 u32 texture_count = readU16(is);
131 for(u32 i=0; i<texture_count; i++){
132 textures.push_back(deSerializeString(is));
134 spritediv = readV2S16(is);
135 initial_sprite_basepos = readV2S16(is);
136 is_visible = readU8(is);
137 makes_footstep_sound = readU8(is);
138 automatic_rotate = readF1000(is);
139 mesh = deSerializeString(is);
140 u32 color_count = readU16(is);
141 for(u32 i=0; i<color_count; i++){
142 colors.push_back(readARGB8(is));
144 collideWithObjects = readU8(is);
145 stepheight = readF1000(is);
146 automatic_face_movement_dir = readU8(is);
147 automatic_face_movement_dir_offset = readF1000(is);
148 backface_culling = readU8(is);
149 }catch(SerializationError &e){}
153 throw SerializationError("unsupported ObjectProperties version");