3a36addc75e57265f4a761bfb7552f21bacac3a2
[oweals/minetest.git] / src / object_properties.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2012 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 "util/serialize.h"
22 #include <sstream>
23
24 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
25 #define PP2(x) "("<<(x).X<<","<<(x).Y<<")"
26
27 ObjectProperties::ObjectProperties():
28         hp_max(1),
29         physical(false),
30         weight(5),
31         collisionbox(-0.5,-0.5,-0.5, 0.5,0.5,0.5),
32         visual("sprite"),
33         mesh(""),
34         texture(""),
35         visual_size(1,1),
36         spritediv(1,1),
37         initial_sprite_basepos(0,0),
38         is_visible(true),
39         makes_footstep_sound(false),
40         automatic_rotate(0)
41 {
42         textures.push_back("unknown_object.png");
43 }
44
45 std::string ObjectProperties::dump()
46 {
47         std::ostringstream os(std::ios::binary);
48         os<<"hp_max="<<hp_max;
49         os<<", physical="<<physical;
50         os<<", weight="<<weight;
51         os<<", collisionbox="<<PP(collisionbox.MinEdge)<<","<<PP(collisionbox.MaxEdge);
52         os<<", visual="<<visual;
53         os<<", mesh="<<mesh;
54         os<<", texture="<<texture;
55         os<<", visual_size="<<PP2(visual_size);
56         os<<", textures=[";
57         for(u32 i=0; i<textures.size(); i++){
58                 os<<"\""<<textures[i]<<"\" ";
59         }
60         os<<"]";
61         os<<", spritediv="<<PP2(spritediv);
62         os<<", initial_sprite_basepos="<<PP2(initial_sprite_basepos);
63         os<<", is_visible="<<is_visible;
64         os<<", makes_footstep_sound="<<makes_footstep_sound;
65         os<<", automatic_rotate="<<automatic_rotate;
66         return os.str();
67 }
68
69 void ObjectProperties::serialize(std::ostream &os) const
70 {
71         writeU8(os, 1); // version
72         writeS16(os, hp_max);
73         writeU8(os, physical);
74         writeF1000(os, weight);
75         writeV3F1000(os, collisionbox.MinEdge);
76         writeV3F1000(os, collisionbox.MaxEdge);
77         os<<serializeString(visual);
78         os<<serializeString(mesh);
79         os<<serializeString(texture);
80         writeV2F1000(os, visual_size);
81         writeU16(os, textures.size());
82         for(u32 i=0; i<textures.size(); i++){
83                 os<<serializeString(textures[i]);
84         }
85         writeV2S16(os, spritediv);
86         writeV2S16(os, initial_sprite_basepos);
87         writeU8(os, is_visible);
88         writeU8(os, makes_footstep_sound);
89         writeF1000(os, automatic_rotate);
90 }
91
92 void ObjectProperties::deSerialize(std::istream &is)
93 {
94         int version = readU8(is);
95         if(version != 1) throw SerializationError(
96                         "unsupported ObjectProperties version");
97         hp_max = readS16(is);
98         physical = readU8(is);
99         weight = readF1000(is);
100         collisionbox.MinEdge = readV3F1000(is);
101         collisionbox.MaxEdge = readV3F1000(is);
102         visual = deSerializeString(is);
103         mesh = deSerializeString(is);
104         texture = deSerializeString(is);
105         visual_size = readV2F1000(is);
106         textures.clear();
107         u32 texture_count = readU16(is);
108         for(u32 i=0; i<texture_count; i++){
109                 textures.push_back(deSerializeString(is));
110         }
111         spritediv = readV2S16(is);
112         initial_sprite_basepos = readV2S16(is);
113         is_visible = readU8(is);
114         makes_footstep_sound = readU8(is);
115         try{
116                 automatic_rotate = readF1000(is);
117         }catch(SerializationError &e){}
118 }
119
120