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