3 Copyright (C) 2012 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 "util/serialize.h"
25 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
26 #define PP2(x) "("<<(x).X<<","<<(x).Y<<")"
28 ObjectProperties::ObjectProperties():
32 collisionbox(-0.5,-0.5,-0.5, 0.5,0.5,0.5),
35 animation_frames(1,1),
40 initial_sprite_basepos(0,0),
42 makes_footstep_sound(false),
45 animation_bone_position[""] = v3f(0,0,0);
46 animation_bone_rotation[""] = v3f(0,0,0);
47 textures.push_back("unknown_object.png");
50 std::string ObjectProperties::dump()
52 std::ostringstream os(std::ios::binary);
53 os<<"hp_max="<<hp_max;
54 os<<", physical="<<physical;
55 os<<", weight="<<weight;
56 os<<", collisionbox="<<PP(collisionbox.MinEdge)<<","<<PP(collisionbox.MaxEdge);
57 os<<", visual="<<visual;
59 os<<", animation_frames="<<animation_frames.X<<","<<animation_frames.Y;
60 os<<", animation_speed="<<animation_speed;
61 os<<", animation_blend="<<animation_blend;
62 os<<", visual_size="<<PP2(visual_size);
64 os<<", animation_bone_position=[";
65 for(std::map<std::string, v3f>::const_iterator ii = animation_bone_position.begin(); ii != animation_bone_position.end(); ++ii){
66 std::string bone_name = (*ii).first;
67 v3f bone_pos = (*ii).second;
68 os<<bone_name<<" "<<bone_pos.X<<","<<bone_pos.Y<<","<<bone_pos.Z<<"\"";
71 os<<", animation_bone_rotation=[";
72 for(std::map<std::string, v3f>::const_iterator ii = animation_bone_rotation.begin(); ii != animation_bone_rotation.end(); ++ii){
73 std::string bone_name = (*ii).first;
74 v3f bone_rot = (*ii).second;
75 os<<bone_name<<" "<<bone_rot.X<<","<<bone_rot.Y<<","<<bone_rot.Z<<"\"";
80 for(u32 i=0; i<textures.size(); i++){
81 os<<"\""<<textures[i]<<"\" ";
84 os<<", spritediv="<<PP2(spritediv);
85 os<<", initial_sprite_basepos="<<PP2(initial_sprite_basepos);
86 os<<", is_visible="<<is_visible;
87 os<<", makes_footstep_sound="<<makes_footstep_sound;
88 os<<", automatic_rotate="<<automatic_rotate;
92 void ObjectProperties::serialize(std::ostream &os) const
94 writeU8(os, 1); // version
96 writeU8(os, physical);
97 writeF1000(os, weight);
98 writeV3F1000(os, collisionbox.MinEdge);
99 writeV3F1000(os, collisionbox.MaxEdge);
100 os<<serializeString(visual);
101 os<<serializeString(mesh);
102 writeF1000(os, animation_frames.X);
103 writeF1000(os, animation_frames.Y);
104 writeF1000(os, animation_speed);
105 writeF1000(os, animation_blend);
107 writeU16(os, animation_bone_position.size());
108 for(std::map<std::string, v3f>::const_iterator ii = animation_bone_position.begin(); ii != animation_bone_position.end(); ++ii){
109 os<<serializeString((*ii).first);
110 writeV3F1000(os, (*ii).second);
112 writeU16(os, animation_bone_rotation.size());
113 for(std::map<std::string, v3f>::const_iterator ii = animation_bone_rotation.begin(); ii != animation_bone_rotation.end(); ++ii){
114 os<<serializeString((*ii).first);
115 writeV3F1000(os, (*ii).second);
118 writeV2F1000(os, visual_size);
119 writeU16(os, textures.size());
120 for(u32 i=0; i<textures.size(); i++){
121 os<<serializeString(textures[i]);
124 writeV2S16(os, spritediv);
125 writeV2S16(os, initial_sprite_basepos);
126 writeU8(os, is_visible);
127 writeU8(os, makes_footstep_sound);
128 writeF1000(os, automatic_rotate);
131 void ObjectProperties::deSerialize(std::istream &is)
133 int version = readU8(is);
134 if(version != 1) throw SerializationError(
135 "unsupported ObjectProperties version");
136 hp_max = readS16(is);
137 physical = readU8(is);
138 weight = readF1000(is);
139 collisionbox.MinEdge = readV3F1000(is);
140 collisionbox.MaxEdge = readV3F1000(is);
141 visual = deSerializeString(is);
142 mesh = deSerializeString(is);
143 animation_frames.X = readF1000(is);
144 animation_frames.Y = readF1000(is);
145 animation_speed = readF1000(is);
146 animation_blend = readF1000(is);
148 u32 animation_bone_position_count = readU16(is);
149 for(u32 i=0; i<animation_bone_position_count; i++){
150 std::string bone_name = deSerializeString(is);
151 v3f bone_pos = readV3F1000(is);
152 animation_bone_position[bone_name] = bone_pos;
154 u32 animation_bone_rotation_count = readU16(is);
155 for(u32 i=0; i<animation_bone_rotation_count; i++){
156 std::string bone_name = deSerializeString(is);
157 v3f bone_rot = readV3F1000(is);
158 animation_bone_rotation[bone_name] = bone_rot;
161 visual_size = readV2F1000(is);
163 u32 texture_count = readU16(is);
164 for(u32 i=0; i<texture_count; i++){
165 textures.push_back(deSerializeString(is));
168 spritediv = readV2S16(is);
169 initial_sprite_basepos = readV2S16(is);
170 is_visible = readU8(is);
171 makes_footstep_sound = readU8(is);
173 automatic_rotate = readF1000(is);
174 }catch(SerializationError &e){}