Joint positioning and rotation code, and fix a problem related to their lua API
[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 #include <map>
24
25 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
26 #define PP2(x) "("<<(x).X<<","<<(x).Y<<")"
27
28 ObjectProperties::ObjectProperties():
29         hp_max(1),
30         physical(false),
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 {
42         // Nothing to do for animation_bone_position
43         // Nothing to do for animation_bone_rotation
44         textures.push_back("unknown_object.png");
45 }
46
47 std::string ObjectProperties::dump()
48 {
49         std::ostringstream os(std::ios::binary);
50         os<<"hp_max="<<hp_max;
51         os<<", physical="<<physical;
52         os<<", weight="<<weight;
53         os<<", collisionbox="<<PP(collisionbox.MinEdge)<<","<<PP(collisionbox.MaxEdge);
54         os<<", visual="<<visual;
55         os<<", mesh="<<mesh;
56         os<<", visual_size="<<PP2(visual_size);
57
58         os<<", animation_bone_position=[";
59         for(std::map<std::string, v3f>::const_iterator ii = animation_bone_position.begin(); ii != animation_bone_position.end(); ++ii){
60                 std::string bone_name = (*ii).first;
61                 v3f bone_pos = (*ii).second;
62                 os<<bone_name<<" "<<bone_pos.X<<","<<bone_pos.Y<<","<<bone_pos.Z<<"\"";
63         }
64         os<<"]";
65         os<<", animation_bone_rotation=[";
66         for(std::map<std::string, v3f>::const_iterator ii = animation_bone_rotation.begin(); ii != animation_bone_rotation.end(); ++ii){
67                 std::string bone_name = (*ii).first;
68                 v3f bone_rot = (*ii).second;
69                 os<<bone_name<<" "<<bone_rot.X<<","<<bone_rot.Y<<","<<bone_rot.Z<<"\"";
70         }
71         os<<"]";
72
73         os<<", textures=[";
74         for(u32 i=0; i<textures.size(); i++){
75                 os<<"\""<<textures[i]<<"\" ";
76         }
77         os<<"]";
78         os<<", spritediv="<<PP2(spritediv);
79         os<<", initial_sprite_basepos="<<PP2(initial_sprite_basepos);
80         os<<", is_visible="<<is_visible;
81         os<<", makes_footstep_sound="<<makes_footstep_sound;
82         os<<", automatic_rotate="<<automatic_rotate;
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         os<<serializeString(mesh);
96
97         writeU16(os, animation_bone_position.size());
98         for(std::map<std::string, v3f>::const_iterator ii = animation_bone_position.begin(); ii != animation_bone_position.end(); ++ii){
99                 os<<serializeString((*ii).first);
100                 writeV3F1000(os, (*ii).second);
101         }
102         writeU16(os, animation_bone_rotation.size());
103         for(std::map<std::string, v3f>::const_iterator ii = animation_bone_rotation.begin(); ii != animation_bone_rotation.end(); ++ii){
104                 os<<serializeString((*ii).first);
105                 writeV3F1000(os, (*ii).second);
106         }
107
108         writeV2F1000(os, visual_size);
109         writeU16(os, textures.size());
110         for(u32 i=0; i<textures.size(); i++){
111                 os<<serializeString(textures[i]);
112         }
113
114         writeV2S16(os, spritediv);
115         writeV2S16(os, initial_sprite_basepos);
116         writeU8(os, is_visible);
117         writeU8(os, makes_footstep_sound);
118         writeF1000(os, automatic_rotate);
119 }
120
121 void ObjectProperties::deSerialize(std::istream &is)
122 {
123         int version = readU8(is);
124         if(version != 1) throw SerializationError(
125                         "unsupported ObjectProperties version");
126         hp_max = readS16(is);
127         physical = readU8(is);
128         weight = readF1000(is);
129         collisionbox.MinEdge = readV3F1000(is);
130         collisionbox.MaxEdge = readV3F1000(is);
131         visual = deSerializeString(is);
132         mesh = deSerializeString(is);
133
134         u32 animation_bone_position_count = readU16(is);
135         for(u32 i=0; i<animation_bone_position_count; i++){
136                 std::string bone_name = deSerializeString(is);
137                 v3f bone_pos = readV3F1000(is);
138                 animation_bone_position[bone_name] = bone_pos;
139         }
140         u32 animation_bone_rotation_count = readU16(is);
141         for(u32 i=0; i<animation_bone_rotation_count; i++){
142                 std::string bone_name = deSerializeString(is);
143                 v3f bone_rot = readV3F1000(is);
144                 animation_bone_rotation[bone_name] = bone_rot;
145         }
146
147         visual_size = readV2F1000(is);
148         textures.clear();
149         u32 texture_count = readU16(is);
150         for(u32 i=0; i<texture_count; i++){
151                 textures.push_back(deSerializeString(is));
152         }
153
154         spritediv = readV2S16(is);
155         initial_sprite_basepos = readV2S16(is);
156         is_visible = readU8(is);
157         makes_footstep_sound = readU8(is);
158         try{
159                 automatic_rotate = readF1000(is);
160         }catch(SerializationError &e){}
161 }
162
163