]> git.lizzy.rs Git - minetest-m13.git/blob - src/staticobject.h
80b5f1d0fed70fca318305fbd19ab52c8020c9f4
[minetest-m13.git] / src / staticobject.h
1 /*
2 Minetest-m13
3 Copyright (C) 2010-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 #ifndef STATICOBJECT_HEADER
21 #define STATICOBJECT_HEADER
22
23 #include "common_irrlicht.h"
24 #include <string>
25 #include <sstream>
26 #include "utility.h"
27
28 struct StaticObject
29 {
30         u8 type;
31         v3f pos;
32         std::string data;
33
34         StaticObject():
35                 type(0),
36                 pos(0,0,0)
37         {
38         }
39         StaticObject(u8 type_, v3f pos_, const std::string &data_):
40                 type(type_),
41                 pos(pos_),
42                 data(data_)
43         {
44         }
45
46         void serialize(std::ostream &os)
47         {
48                 char buf[12];
49                 // type
50                 buf[0] = type;
51                 os.write(buf, 1);
52                 // pos
53                 writeV3S32((u8*)buf, v3s32(pos.X*1000,pos.Y*1000,pos.Z*1000));
54                 os.write(buf, 12);
55                 // data
56                 os<<serializeString(data);
57         }
58         void deSerialize(std::istream &is, u8 version)
59         {
60                 char buf[12];
61                 // type
62                 is.read(buf, 1);
63                 type = buf[0];
64                 // pos
65                 is.read(buf, 12);
66                 v3s32 intp = readV3S32((u8*)buf);
67                 pos.X = (f32)intp.X/1000;
68                 pos.Y = (f32)intp.Y/1000;
69                 pos.Z = (f32)intp.Z/1000;
70                 // data
71                 data = deSerializeString(is);
72         }
73 };
74
75 class StaticObjectList
76 {
77 public:
78         /*
79                 Inserts an object to the container.
80                 Id must be unique or 0.
81         */
82         void insert(u16 id, StaticObject obj)
83         {
84                 if(id == 0)
85                 {
86                         m_stored.push_back(obj);
87                 }
88                 else
89                 {
90                         if(m_active.find(id) != NULL)
91                         {
92                                 dstream<<"ERROR: StaticObjectList::insert(): "
93                                                 <<"id already exists"<<std::endl;
94                                 assert(0);
95                                 return;
96                         }
97                         m_active.insert(id, obj);
98                 }
99         }
100
101         void remove(u16 id)
102         {
103                 assert(id != 0);
104                 if(m_active.find(id) == NULL)
105                 {
106                         dstream<<"WARNING: StaticObjectList::remove(): id="<<id
107                                         <<" not found"<<std::endl;
108                         return;
109                 }
110                 m_active.remove(id);
111         }
112
113         void serialize(std::ostream &os)
114         {
115                 char buf[12];
116                 // version
117                 buf[0] = 0;
118                 os.write(buf, 1);
119                 // count
120                 u16 count = m_stored.size() + m_active.size();
121                 writeU16((u8*)buf, count);
122                 os.write(buf, 2);
123                 for(core::list<StaticObject>::Iterator
124                                 i = m_stored.begin();
125                                 i != m_stored.end(); i++)
126                 {
127                         StaticObject &s_obj = *i;
128                         s_obj.serialize(os);
129                 }
130                 for(core::map<u16, StaticObject>::Iterator
131                                 i = m_active.getIterator();
132                                 i.atEnd()==false; i++)
133                 {
134                         StaticObject s_obj = i.getNode()->getValue();
135                         s_obj.serialize(os);
136                 }
137         }
138         void deSerialize(std::istream &is)
139         {
140                 char buf[12];
141                 // version
142                 is.read(buf, 1);
143                 u8 version = buf[0];
144                 // count
145                 is.read(buf, 2);
146                 u16 count = readU16((u8*)buf);
147                 for(u16 i=0; i<count; i++)
148                 {
149                         StaticObject s_obj;
150                         s_obj.deSerialize(is, version);
151                         m_stored.push_back(s_obj);
152                 }
153         }
154         
155         /*
156                 NOTE: When an object is transformed to active, it is removed
157                 from m_stored and inserted to m_active.
158                 The caller directly manipulates these containers.
159         */
160         core::list<StaticObject> m_stored;
161         core::map<u16, StaticObject> m_active;
162
163 private:
164 };
165
166 #endif
167