08e9f63cf71c47c2636b047890681a0e9a5f5878
[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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
14
15 You should have received a copy of the GNU 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 "utility.h"
22
23 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
24 #define PP2(x) "("<<(x).X<<","<<(x).Y<<")"
25
26 ObjectProperties::ObjectProperties():
27         hp_max(1),
28         physical(false),
29         weight(5),
30         collisionbox(-0.5,-0.5,-0.5, 0.5,0.5,0.5),
31         visual("sprite"),
32         visual_size(1,1),
33         spritediv(1,1),
34         initial_sprite_basepos(0,0),
35         is_visible(true),
36         makes_footstep_sound(false),
37         automatic_rotate(0)
38 {
39         textures.push_back("unknown_object.png");
40 }
41
42 std::string ObjectProperties::dump()
43 {
44         std::ostringstream os(std::ios::binary);
45         os<<"hp_max="<<hp_max;
46         os<<", physical="<<physical;
47         os<<", weight="<<weight;
48         os<<", collisionbox="<<PP(collisionbox.MinEdge)<<","<<PP(collisionbox.MaxEdge);
49         os<<", visual="<<visual;
50         os<<", visual_size="<<PP2(visual_size);
51         os<<", textures=[";
52         for(u32 i=0; i<textures.size(); i++){
53                 os<<"\""<<textures[i]<<"\" ";
54         }
55         os<<"]";
56         os<<", spritediv="<<PP2(spritediv);
57         os<<", initial_sprite_basepos="<<PP2(initial_sprite_basepos);
58         os<<", is_visible="<<is_visible;
59         os<<", makes_footstep_sound="<<makes_footstep_sound;
60         os<<", automatic_rotate="<<automatic_rotate;
61         return os.str();
62 }
63
64 void ObjectProperties::serialize(std::ostream &os) const
65 {
66         writeU8(os, 1); // version
67         writeS16(os, hp_max);
68         writeU8(os, physical);
69         writeF1000(os, weight);
70         writeV3F1000(os, collisionbox.MinEdge);
71         writeV3F1000(os, collisionbox.MaxEdge);
72         os<<serializeString(visual);
73         writeV2F1000(os, visual_size);
74         writeU16(os, textures.size());
75         for(u32 i=0; i<textures.size(); i++){
76                 os<<serializeString(textures[i]);
77         }
78         writeV2S16(os, spritediv);
79         writeV2S16(os, initial_sprite_basepos);
80         writeU8(os, is_visible);
81         writeU8(os, makes_footstep_sound);
82         writeF1000(os, automatic_rotate);
83 }
84
85 void ObjectProperties::deSerialize(std::istream &is)
86 {
87         int version = readU8(is);
88         if(version != 1) throw SerializationError(
89                         "unsupported ObjectProperties version");
90         hp_max = readS16(is);
91         physical = readU8(is);
92         weight = readF1000(is);
93         collisionbox.MinEdge = readV3F1000(is);
94         collisionbox.MaxEdge = readV3F1000(is);
95         visual = deSerializeString(is);
96         visual_size = readV2F1000(is);
97         textures.clear();
98         u32 texture_count = readU16(is);
99         for(u32 i=0; i<texture_count; i++){
100                 textures.push_back(deSerializeString(is));
101         }
102         spritediv = readV2S16(is);
103         initial_sprite_basepos = readV2S16(is);
104         is_visible = readU8(is);
105         makes_footstep_sound = readU8(is);
106         try{
107                 automatic_rotate = readF1000(is);
108         }catch(SerializationError &e){}
109 }
110
111