Scripting WIP
[oweals/minetest.git] / src / luaentity_common.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2011 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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
14
15 You should have received a copy of the GNU 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 "luaentity_common.h"
21
22 #include "utility.h"
23
24 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
25
26 LuaEntityProperties::LuaEntityProperties():
27         physical(true),
28         weight(5),
29         collisionbox(-0.5,-0.5,-0.5, 0.5,0.5,0.5),
30         visual("single_sprite")
31 {
32         textures.push_back("unknown_block.png");
33 }
34
35 std::string LuaEntityProperties::dump()
36 {
37         std::ostringstream os(std::ios::binary);
38         os<<"physical="<<physical;
39         os<<", weight="<<weight;
40         os<<", collisionbox="<<PP(collisionbox.MinEdge)<<","<<PP(collisionbox.MaxEdge);
41         os<<", visual="<<visual;
42         os<<", textures=[";
43         for(core::list<std::string>::Iterator i = textures.begin();
44                         i != textures.end(); i++){
45                 os<<"\""<<(*i)<<"\" ";
46         }
47         os<<"]";
48         return os.str();
49 }
50
51 void LuaEntityProperties::serialize(std::ostream &os)
52 {
53         writeU8(os, 0); // version
54         writeU8(os, physical);
55         writeF1000(os, weight);
56         writeV3F1000(os, collisionbox.MinEdge);
57         writeV3F1000(os, collisionbox.MaxEdge);
58         os<<serializeString(visual);
59         writeU16(os, textures.size());
60         for(core::list<std::string>::Iterator i = textures.begin();
61                         i != textures.end(); i++){
62                 os<<serializeString(*i);
63         }
64 }
65
66 void LuaEntityProperties::deSerialize(std::istream &is)
67 {
68         int version = readU8(is);
69         if(version != 0) throw SerializationError(
70                         "unsupported LuaEntityProperties version");
71         physical = readU8(is);
72         weight = readF1000(is);
73         collisionbox.MinEdge = readV3F1000(is);
74         collisionbox.MaxEdge = readV3F1000(is);
75         visual = deSerializeString(is);
76         textures.clear();
77         int texture_count = readU16(is);
78         for(int i=0; i<texture_count; i++){
79                 textures.push_back(deSerializeString(is));
80         }
81 }
82
83