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