Freeze-melt adjust
[oweals/minetest.git] / src / object_properties.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 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 "irrlichttypes_bloated.h"
22 #include "util/serialize.h"
23 #include <sstream>
24 #include <map>
25
26 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
27 #define PP2(x) "("<<(x).X<<","<<(x).Y<<")"
28
29 ObjectProperties::ObjectProperties():
30         hp_max(1),
31         physical(false),
32         collideWithObjects(true),
33         weight(5),
34         collisionbox(-0.5,-0.5,-0.5, 0.5,0.5,0.5),
35         visual("sprite"),
36         mesh(""),
37         visual_size(1,1),
38         spritediv(1,1),
39         initial_sprite_basepos(0,0),
40         is_visible(true),
41         makes_footstep_sound(false),
42         automatic_rotate(0),
43         stepheight(0)
44 {
45         textures.push_back("unknown_object.png");
46         colors.push_back(video::SColor(255,255,255,255));
47 }
48
49 std::string ObjectProperties::dump()
50 {
51         std::ostringstream os(std::ios::binary);
52         os<<"hp_max="<<hp_max;
53         os<<", physical="<<physical;
54         os<<", collideWithObjects="<<collideWithObjects;
55         os<<", weight="<<weight;
56         os<<", collisionbox="<<PP(collisionbox.MinEdge)<<","<<PP(collisionbox.MaxEdge);
57         os<<", visual="<<visual;
58         os<<", mesh="<<mesh;
59         os<<", visual_size="<<PP2(visual_size);
60         os<<", textures=[";
61         for(u32 i=0; i<textures.size(); i++){
62                 os<<"\""<<textures[i]<<"\" ";
63         }
64         os<<"]";
65         os<<", colors=[";
66         for(u32 i=0; i<colors.size(); i++){
67                 os<<"\""<<colors[i].getAlpha()<<","<<colors[i].getRed()<<","<<colors[i].getGreen()<<","<<colors[i].getBlue()<<"\" ";
68         }
69         os<<"]";
70         os<<", spritediv="<<PP2(spritediv);
71         os<<", initial_sprite_basepos="<<PP2(initial_sprite_basepos);
72         os<<", is_visible="<<is_visible;
73         os<<", makes_footstep_sound="<<makes_footstep_sound;
74         os<<", automatic_rotate="<<automatic_rotate;
75         return os.str();
76 }
77
78 void ObjectProperties::serialize(std::ostream &os) const
79 {
80         writeU8(os, 1); // version
81         writeS16(os, hp_max);
82         writeU8(os, physical);
83         writeF1000(os, weight);
84         writeV3F1000(os, collisionbox.MinEdge);
85         writeV3F1000(os, collisionbox.MaxEdge);
86         os<<serializeString(visual);
87         writeV2F1000(os, visual_size);
88         writeU16(os, textures.size());
89         for(u32 i=0; i<textures.size(); i++){
90                 os<<serializeString(textures[i]);
91         }
92         writeV2S16(os, spritediv);
93         writeV2S16(os, initial_sprite_basepos);
94         writeU8(os, is_visible);
95         writeU8(os, makes_footstep_sound);
96         writeF1000(os, automatic_rotate);
97         // Added in protocol version 14
98         os<<serializeString(mesh);
99         writeU16(os, colors.size());
100         for(u32 i=0; i<colors.size(); i++){
101                 writeARGB8(os, colors[i]);
102         }
103         writeU8(os, collideWithObjects);
104         writeF1000(os,stepheight);
105         // Add stuff only at the bottom.
106         // Never remove anything, because we don't want new versions of this
107 }
108
109 void ObjectProperties::deSerialize(std::istream &is)
110 {
111         int version = readU8(is);
112         if(version == 1)
113         {
114                 try{
115                         hp_max = readS16(is);
116                         physical = readU8(is);
117                         weight = readF1000(is);
118                         collisionbox.MinEdge = readV3F1000(is);
119                         collisionbox.MaxEdge = readV3F1000(is);
120                         visual = deSerializeString(is);
121                         visual_size = readV2F1000(is);
122                         textures.clear();
123                         u32 texture_count = readU16(is);
124                         for(u32 i=0; i<texture_count; i++){
125                                 textures.push_back(deSerializeString(is));
126                         }
127                         spritediv = readV2S16(is);
128                         initial_sprite_basepos = readV2S16(is);
129                         is_visible = readU8(is);
130                         makes_footstep_sound = readU8(is);
131                         automatic_rotate = readF1000(is);
132                         mesh = deSerializeString(is);
133                         u32 color_count = readU16(is);
134                         for(u32 i=0; i<color_count; i++){
135                                 colors.push_back(readARGB8(is));
136                         }
137                         collideWithObjects = readU8(is);
138                         stepheight = readF1000(is);
139                 }catch(SerializationError &e){}
140         }
141         else
142         {
143                 throw SerializationError("unsupported ObjectProperties version");
144         }
145 }
146