Fix shift-descend to ladders from a floor
[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         visual_size(1,1),
34         spritediv(1,1),
35         initial_sprite_basepos(0,0),
36         is_visible(true),
37         makes_footstep_sound(false),
38         automatic_rotate(0)
39 {
40         textures.push_back("unknown_object.png");
41 }
42
43 std::string ObjectProperties::dump()
44 {
45         std::ostringstream os(std::ios::binary);
46         os<<"hp_max="<<hp_max;
47         os<<", physical="<<physical;
48         os<<", weight="<<weight;
49         os<<", collisionbox="<<PP(collisionbox.MinEdge)<<","<<PP(collisionbox.MaxEdge);
50         os<<", visual="<<visual;
51         os<<", visual_size="<<PP2(visual_size);
52         os<<", textures=[";
53         for(u32 i=0; i<textures.size(); i++){
54                 os<<"\""<<textures[i]<<"\" ";
55         }
56         os<<"]";
57         os<<", spritediv="<<PP2(spritediv);
58         os<<", initial_sprite_basepos="<<PP2(initial_sprite_basepos);
59         os<<", is_visible="<<is_visible;
60         os<<", makes_footstep_sound="<<makes_footstep_sound;
61         os<<", automatic_rotate="<<automatic_rotate;
62         return os.str();
63 }
64
65 void ObjectProperties::serialize(std::ostream &os) const
66 {
67         writeU8(os, 1); // version
68         writeS16(os, hp_max);
69         writeU8(os, physical);
70         writeF1000(os, weight);
71         writeV3F1000(os, collisionbox.MinEdge);
72         writeV3F1000(os, collisionbox.MaxEdge);
73         os<<serializeString(visual);
74         writeV2F1000(os, visual_size);
75         writeU16(os, textures.size());
76         for(u32 i=0; i<textures.size(); i++){
77                 os<<serializeString(textures[i]);
78         }
79         writeV2S16(os, spritediv);
80         writeV2S16(os, initial_sprite_basepos);
81         writeU8(os, is_visible);
82         writeU8(os, makes_footstep_sound);
83         writeF1000(os, automatic_rotate);
84 }
85
86 void ObjectProperties::deSerialize(std::istream &is)
87 {
88         int version = readU8(is);
89         if(version != 1) throw SerializationError(
90                         "unsupported ObjectProperties version");
91         hp_max = readS16(is);
92         physical = readU8(is);
93         weight = readF1000(is);
94         collisionbox.MinEdge = readV3F1000(is);
95         collisionbox.MaxEdge = readV3F1000(is);
96         visual = deSerializeString(is);
97         visual_size = readV2F1000(is);
98         textures.clear();
99         u32 texture_count = readU16(is);
100         for(u32 i=0; i<texture_count; i++){
101                 textures.push_back(deSerializeString(is));
102         }
103         spritediv = readV2S16(is);
104         initial_sprite_basepos = readV2S16(is);
105         is_visible = readU8(is);
106         makes_footstep_sound = readU8(is);
107         try{
108                 automatic_rotate = readF1000(is);
109         }catch(SerializationError &e){}
110 }
111
112