]> git.lizzy.rs Git - dragonfireclient.git/blob - src/staticobject.cpp
Change Minetest-c55 to Minetest
[dragonfireclient.git] / src / staticobject.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-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 "staticobject.h"
21 #include "util/serialize.h"
22
23 void StaticObject::serialize(std::ostream &os)
24 {
25         char buf[12];
26         // type
27         buf[0] = type;
28         os.write(buf, 1);
29         // pos
30         writeV3S32((u8*)buf, v3s32(pos.X*1000,pos.Y*1000,pos.Z*1000));
31         os.write(buf, 12);
32         // data
33         os<<serializeString(data);
34 }
35 void StaticObject::deSerialize(std::istream &is, u8 version)
36 {
37         char buf[12];
38         // type
39         is.read(buf, 1);
40         type = buf[0];
41         // pos
42         is.read(buf, 12);
43         v3s32 intp = readV3S32((u8*)buf);
44         pos.X = (f32)intp.X/1000;
45         pos.Y = (f32)intp.Y/1000;
46         pos.Z = (f32)intp.Z/1000;
47         // data
48         data = deSerializeString(is);
49 }
50
51 void StaticObjectList::serialize(std::ostream &os)
52 {
53         char buf[12];
54         // version
55         buf[0] = 0;
56         os.write(buf, 1);
57         // count
58         u16 count = m_stored.size() + m_active.size();
59         writeU16((u8*)buf, count);
60         os.write(buf, 2);
61         for(core::list<StaticObject>::Iterator
62                         i = m_stored.begin();
63                         i != m_stored.end(); i++)
64         {
65                 StaticObject &s_obj = *i;
66                 s_obj.serialize(os);
67         }
68         for(core::map<u16, StaticObject>::Iterator
69                         i = m_active.getIterator();
70                         i.atEnd()==false; i++)
71         {
72                 StaticObject s_obj = i.getNode()->getValue();
73                 s_obj.serialize(os);
74         }
75 }
76 void StaticObjectList::deSerialize(std::istream &is)
77 {
78         char buf[12];
79         // version
80         is.read(buf, 1);
81         u8 version = buf[0];
82         // count
83         is.read(buf, 2);
84         u16 count = readU16((u8*)buf);
85         for(u16 i=0; i<count; i++)
86         {
87                 StaticObject s_obj;
88                 s_obj.deSerialize(is, version);
89                 m_stored.push_back(s_obj);
90         }
91 }
92