Hardware coloring for itemstacks
[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 "exceptions.h"
23 #include "util/serialize.h"
24 #include "util/basic_macros.h"
25 #include <sstream>
26
27 ObjectProperties::ObjectProperties():
28         hp_max(1),
29         physical(false),
30         collideWithObjects(true),
31         weight(5),
32         collisionbox(-0.5,-0.5,-0.5, 0.5,0.5,0.5),
33         visual("sprite"),
34         mesh(""),
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         stepheight(0),
42         automatic_face_movement_dir(false),
43         automatic_face_movement_dir_offset(0.0),
44         backface_culling(true),
45         nametag(""),
46         nametag_color(255, 255, 255, 255),
47         automatic_face_movement_max_rotation_per_sec(-1)
48 {
49         textures.push_back("unknown_object.png");
50         colors.push_back(video::SColor(255,255,255,255));
51 }
52
53 std::string ObjectProperties::dump()
54 {
55         std::ostringstream os(std::ios::binary);
56         os<<"hp_max="<<hp_max;
57         os<<", physical="<<physical;
58         os<<", collideWithObjects="<<collideWithObjects;
59         os<<", weight="<<weight;
60         os<<", collisionbox="<<PP(collisionbox.MinEdge)<<","<<PP(collisionbox.MaxEdge);
61         os<<", visual="<<visual;
62         os<<", mesh="<<mesh;
63         os<<", visual_size="<<PP2(visual_size);
64         os<<", textures=[";
65         for(u32 i=0; i<textures.size(); i++){
66                 os<<"\""<<textures[i]<<"\" ";
67         }
68         os<<"]";
69         os<<", colors=[";
70         for(u32 i=0; i<colors.size(); i++){
71                 os<<"\""<<colors[i].getAlpha()<<","<<colors[i].getRed()<<","<<colors[i].getGreen()<<","<<colors[i].getBlue()<<"\" ";
72         }
73         os<<"]";
74         os<<", spritediv="<<PP2(spritediv);
75         os<<", initial_sprite_basepos="<<PP2(initial_sprite_basepos);
76         os<<", is_visible="<<is_visible;
77         os<<", makes_footstep_sound="<<makes_footstep_sound;
78         os<<", automatic_rotate="<<automatic_rotate;
79         os<<", backface_culling="<<backface_culling;
80         os << ", nametag=" << nametag;
81         os << ", nametag_color=" << "\"" << nametag_color.getAlpha() << "," << nametag_color.getRed()
82                         << "," << nametag_color.getGreen() << "," << nametag_color.getBlue() << "\" ";
83         return os.str();
84 }
85
86 void ObjectProperties::serialize(std::ostream &os) const
87 {
88         writeU8(os, 1); // version
89         writeS16(os, hp_max);
90         writeU8(os, physical);
91         writeF1000(os, weight);
92         writeV3F1000(os, collisionbox.MinEdge);
93         writeV3F1000(os, collisionbox.MaxEdge);
94         os<<serializeString(visual);
95         writeV2F1000(os, visual_size);
96         writeU16(os, textures.size());
97         for(u32 i=0; i<textures.size(); i++){
98                 os<<serializeString(textures[i]);
99         }
100         writeV2S16(os, spritediv);
101         writeV2S16(os, initial_sprite_basepos);
102         writeU8(os, is_visible);
103         writeU8(os, makes_footstep_sound);
104         writeF1000(os, automatic_rotate);
105         // Added in protocol version 14
106         os<<serializeString(mesh);
107         writeU16(os, colors.size());
108         for(u32 i=0; i<colors.size(); i++){
109                 writeARGB8(os, colors[i]);
110         }
111         writeU8(os, collideWithObjects);
112         writeF1000(os,stepheight);
113         writeU8(os, automatic_face_movement_dir);
114         writeF1000(os, automatic_face_movement_dir_offset);
115         writeU8(os, backface_culling);
116         os << serializeString(nametag);
117         writeARGB8(os, nametag_color);
118         writeF1000(os, automatic_face_movement_max_rotation_per_sec);
119         os << serializeString(infotext);
120         os << serializeString(wield_item);
121
122         // Add stuff only at the bottom.
123         // Never remove anything, because we don't want new versions of this
124 }
125
126 void ObjectProperties::deSerialize(std::istream &is)
127 {
128         int version = readU8(is);
129         if(version == 1)
130         {
131                 try{
132                         hp_max = readS16(is);
133                         physical = readU8(is);
134                         weight = readF1000(is);
135                         collisionbox.MinEdge = readV3F1000(is);
136                         collisionbox.MaxEdge = readV3F1000(is);
137                         visual = deSerializeString(is);
138                         visual_size = readV2F1000(is);
139                         textures.clear();
140                         u32 texture_count = readU16(is);
141                         for(u32 i=0; i<texture_count; i++){
142                                 textures.push_back(deSerializeString(is));
143                         }
144                         spritediv = readV2S16(is);
145                         initial_sprite_basepos = readV2S16(is);
146                         is_visible = readU8(is);
147                         makes_footstep_sound = readU8(is);
148                         automatic_rotate = readF1000(is);
149                         mesh = deSerializeString(is);
150                         u32 color_count = readU16(is);
151                         for(u32 i=0; i<color_count; i++){
152                                 colors.push_back(readARGB8(is));
153                         }
154                         collideWithObjects = readU8(is);
155                         stepheight = readF1000(is);
156                         automatic_face_movement_dir = readU8(is);
157                         automatic_face_movement_dir_offset = readF1000(is);
158                         backface_culling = readU8(is);
159                         nametag = deSerializeString(is);
160                         nametag_color = readARGB8(is);
161                         automatic_face_movement_max_rotation_per_sec = readF1000(is);
162                         infotext = deSerializeString(is);
163                         wield_item = deSerializeString(is);
164                 }catch(SerializationError &e){}
165         }
166         else
167         {
168                 throw SerializationError("unsupported ObjectProperties version");
169         }
170 }